invoke_tool.go 2.6 KB

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