invoke_tool.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package service
  2. import (
  3. "errors"
  4. "github.com/gin-gonic/gin"
  5. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon"
  6. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/access_types"
  7. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
  8. "github.com/langgenius/dify-plugin-daemon/internal/core/session_manager"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/requests"
  11. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/tool_entities"
  12. "github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
  13. )
  14. func createSession[T any](
  15. r *plugin_entities.InvokePluginRequest[T],
  16. access_type access_types.PluginAccessType,
  17. access_action access_types.PluginAccessAction,
  18. cluster_id string,
  19. ) (*session_manager.Session, error) {
  20. manager := plugin_manager.Manager()
  21. if manager == nil {
  22. return nil, errors.New("failed to get plugin manager")
  23. }
  24. runtime := manager.Get(r.PluginUniqueIdentifier)
  25. if runtime == nil {
  26. return nil, errors.New("failed to get plugin runtime")
  27. }
  28. session := session_manager.NewSession(
  29. r.TenantId,
  30. r.UserId,
  31. r.PluginUniqueIdentifier,
  32. cluster_id,
  33. access_type,
  34. access_action,
  35. runtime.Configuration(),
  36. manager.BackwardsInvocation(),
  37. )
  38. session.BindRuntime(runtime)
  39. return session, nil
  40. }
  41. func InvokeTool(
  42. r *plugin_entities.InvokePluginRequest[requests.RequestInvokeTool],
  43. ctx *gin.Context,
  44. max_timeout_seconds int,
  45. ) {
  46. // create session
  47. session, err := createSession(
  48. r,
  49. access_types.PLUGIN_ACCESS_TYPE_TOOL,
  50. access_types.PLUGIN_ACCESS_ACTION_INVOKE_TOOL,
  51. ctx.GetString("cluster_id"),
  52. )
  53. if err != nil {
  54. ctx.JSON(500, gin.H{"error": err.Error()})
  55. return
  56. }
  57. defer session.Close()
  58. baseSSEService(
  59. func() (*stream.Stream[tool_entities.ToolResponseChunk], error) {
  60. return plugin_daemon.InvokeTool(session, &r.Data)
  61. },
  62. ctx,
  63. max_timeout_seconds,
  64. )
  65. }
  66. func ValidateToolCredentials(
  67. r *plugin_entities.InvokePluginRequest[requests.RequestValidateToolCredentials],
  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_VALIDATE_TOOL_CREDENTIALS,
  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()
  83. baseSSEService(
  84. func() (*stream.Stream[tool_entities.ValidateCredentialsResult], error) {
  85. return plugin_daemon.ValidateToolCredentials(session, &r.Data)
  86. },
  87. ctx,
  88. max_timeout_seconds,
  89. )
  90. }