invoke_tool.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package service
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon"
  5. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/access_types"
  6. "github.com/langgenius/dify-plugin-daemon/internal/core/session_manager"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/requests"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/tool_entities"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/exception"
  11. "github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
  12. )
  13. func InvokeTool(
  14. r *plugin_entities.InvokePluginRequest[requests.RequestInvokeTool],
  15. ctx *gin.Context,
  16. max_timeout_seconds int,
  17. ) {
  18. // create session
  19. session, err := createSession(
  20. r,
  21. access_types.PLUGIN_ACCESS_TYPE_TOOL,
  22. access_types.PLUGIN_ACCESS_ACTION_INVOKE_TOOL,
  23. ctx.GetString("cluster_id"),
  24. )
  25. if err != nil {
  26. ctx.JSON(500, exception.InternalServerError(err).ToResponse())
  27. return
  28. }
  29. defer session.Close(session_manager.CloseSessionPayload{
  30. IgnoreCache: false,
  31. })
  32. baseSSEService(
  33. func() (*stream.Stream[tool_entities.ToolResponseChunk], error) {
  34. return plugin_daemon.InvokeTool(session, &r.Data)
  35. },
  36. ctx,
  37. max_timeout_seconds,
  38. )
  39. }
  40. func ValidateToolCredentials(
  41. r *plugin_entities.InvokePluginRequest[requests.RequestValidateToolCredentials],
  42. ctx *gin.Context,
  43. max_timeout_seconds int,
  44. ) {
  45. // create session
  46. session, err := createSession(
  47. r,
  48. access_types.PLUGIN_ACCESS_TYPE_TOOL,
  49. access_types.PLUGIN_ACCESS_ACTION_VALIDATE_TOOL_CREDENTIALS,
  50. ctx.GetString("cluster_id"),
  51. )
  52. if err != nil {
  53. ctx.JSON(500, exception.InternalServerError(err).ToResponse())
  54. return
  55. }
  56. defer session.Close(session_manager.CloseSessionPayload{
  57. IgnoreCache: false,
  58. })
  59. baseSSEService(
  60. func() (*stream.Stream[tool_entities.ValidateCredentialsResult], error) {
  61. return plugin_daemon.ValidateToolCredentials(session, &r.Data)
  62. },
  63. ctx,
  64. max_timeout_seconds,
  65. )
  66. }
  67. func GetToolRuntimeParameters(
  68. r *plugin_entities.InvokePluginRequest[requests.RequestGetToolRuntimeParameters],
  69. ctx *gin.Context,
  70. max_timeout_seconds int,
  71. ) {
  72. // create session
  73. session, err := createSession(
  74. r,
  75. access_types.PLUGIN_ACCESS_TYPE_TOOL,
  76. access_types.PLUGIN_ACCESS_ACTION_GET_TOOL_RUNTIME_PARAMETERS,
  77. ctx.GetString("cluster_id"),
  78. )
  79. if err != nil {
  80. ctx.JSON(500, exception.InternalServerError(err).ToResponse())
  81. return
  82. }
  83. defer session.Close(session_manager.CloseSessionPayload{
  84. IgnoreCache: false,
  85. })
  86. baseSSEService(
  87. func() (*stream.Stream[tool_entities.GetToolRuntimeParametersResponse], error) {
  88. return plugin_daemon.GetToolRuntimeParameters(session, &r.Data)
  89. },
  90. ctx,
  91. max_timeout_seconds,
  92. )
  93. }