Kaynağa Gözat

feat: support tool get runtime parameters

Yeuoly 10 ay önce
ebeveyn
işleme
1fae793cdf

+ 2 - 0
internal/core/plugin_daemon/access_types/access.go

@@ -19,6 +19,7 @@ type PluginAccessAction string
 const (
 	PLUGIN_ACCESS_ACTION_INVOKE_TOOL                   PluginAccessAction = "invoke_tool"
 	PLUGIN_ACCESS_ACTION_VALIDATE_TOOL_CREDENTIALS     PluginAccessAction = "validate_tool_credentials"
+	PLUGIN_ACCESS_ACTION_GET_TOOL_RUNTIME_PARAMETERS   PluginAccessAction = "get_tool_runtime_parameters"
 	PLUGIN_ACCESS_ACTION_INVOKE_LLM                    PluginAccessAction = "invoke_llm"
 	PLUGIN_ACCESS_ACTION_INVOKE_TEXT_EMBEDDING         PluginAccessAction = "invoke_text_embedding"
 	PLUGIN_ACCESS_ACTION_INVOKE_RERANK                 PluginAccessAction = "invoke_rerank"
@@ -37,6 +38,7 @@ const (
 func (p PluginAccessAction) IsValid() bool {
 	return p == PLUGIN_ACCESS_ACTION_INVOKE_TOOL ||
 		p == PLUGIN_ACCESS_ACTION_VALIDATE_TOOL_CREDENTIALS ||
+		p == PLUGIN_ACCESS_ACTION_GET_TOOL_RUNTIME_PARAMETERS ||
 		p == PLUGIN_ACCESS_ACTION_INVOKE_LLM ||
 		p == PLUGIN_ACCESS_ACTION_INVOKE_TEXT_EMBEDDING ||
 		p == PLUGIN_ACCESS_ACTION_INVOKE_RERANK ||

+ 13 - 0
internal/core/plugin_daemon/tool_service.go

@@ -131,3 +131,16 @@ func ValidateToolCredentials(
 		1,
 	)
 }
+
+func GetToolRuntimeParameters(
+	session *session_manager.Session,
+	request *requests.RequestGetToolRuntimeParameters,
+) (
+	*stream.Stream[tool_entities.GetToolRuntimeParametersResponse], error,
+) {
+	return GenericInvokePlugin[requests.RequestGetToolRuntimeParameters, tool_entities.GetToolRuntimeParametersResponse](
+		session,
+		request,
+		1,
+	)
+}

+ 12 - 0
internal/server/controllers/tool.go

@@ -36,6 +36,18 @@ func ValidateToolCredentials(config *app.Config) gin.HandlerFunc {
 	}
 }
 
+func GetToolRuntimeParameters(config *app.Config) gin.HandlerFunc {
+	type request = plugin_entities.InvokePluginRequest[requests.RequestGetToolRuntimeParameters]
+
+	return func(c *gin.Context) {
+		BindPluginDispatchRequest(
+			c,
+			func(itr request) {
+				service.GetToolRuntimeParameters(&itr, c, config.PluginMaxExecutionTimeout)
+			},
+		)
+	}
+}
 func ListTools(c *gin.Context) {
 	BindRequest(c, func(request struct {
 		TenantID string `uri:"tenant_id" validate:"required"`

+ 1 - 0
internal/server/http_server.go

@@ -58,6 +58,7 @@ func (app *App) pluginDispatchGroup(group *gin.RouterGroup, config *app.Config)
 
 	group.POST("/tool/invoke", controllers.InvokeTool(config))
 	group.POST("/tool/validate_credentials", controllers.ValidateToolCredentials(config))
+	group.POST("/tool/get_runtime_parameters", controllers.GetToolRuntimeParameters(config))
 	group.POST("/llm/invoke", controllers.InvokeLLM(config))
 	group.POST("/llm/num_tokens", controllers.GetLLMNumTokens(config))
 	group.POST("/text_embedding/invoke", controllers.InvokeTextEmbedding(config))

+ 29 - 0
internal/service/invoke_tool.go

@@ -107,3 +107,32 @@ func ValidateToolCredentials(
 		max_timeout_seconds,
 	)
 }
+
+func GetToolRuntimeParameters(
+	r *plugin_entities.InvokePluginRequest[requests.RequestGetToolRuntimeParameters],
+	ctx *gin.Context,
+	max_timeout_seconds int,
+) {
+	// create session
+	session, err := createSession(
+		r,
+		access_types.PLUGIN_ACCESS_TYPE_TOOL,
+		access_types.PLUGIN_ACCESS_ACTION_GET_TOOL_RUNTIME_PARAMETERS,
+		ctx.GetString("cluster_id"),
+	)
+	if err != nil {
+		ctx.JSON(500, gin.H{"error": err.Error()})
+		return
+	}
+	defer session.Close(session_manager.CloseSessionPayload{
+		IgnoreCache: false,
+	})
+
+	baseSSEService(
+		func() (*stream.Stream[tool_entities.GetToolRuntimeParametersResponse], error) {
+			return plugin_daemon.GetToolRuntimeParameters(session, &r.Data)
+		},
+		ctx,
+		max_timeout_seconds,
+	)
+}

+ 5 - 4
internal/types/entities/plugin_entities/tool_declaration.go

@@ -96,10 +96,11 @@ type ToolDescription struct {
 type ToolOutputSchema map[string]any
 
 type ToolDeclaration struct {
-	Identity     ToolIdentity     `json:"identity" validate:"required"`
-	Description  ToolDescription  `json:"description" validate:"required"`
-	Parameters   []ToolParameter  `json:"parameters" validate:"omitempty,dive"`
-	OutputSchema ToolOutputSchema `json:"output_schema" validate:"omitempty,json_schema"`
+	Identity             ToolIdentity     `json:"identity" validate:"required"`
+	Description          ToolDescription  `json:"description" validate:"required"`
+	Parameters           []ToolParameter  `json:"parameters" validate:"omitempty,dive"`
+	OutputSchema         ToolOutputSchema `json:"output_schema" validate:"omitempty,json_schema"`
+	HasRuntimeParameters bool             `json:"has_runtime_parameters"`
 }
 
 func isJSONSchema(fl validator.FieldLevel) bool {

+ 6 - 0
internal/types/entities/requests/tool.go

@@ -38,3 +38,9 @@ type RequestValidateToolCredentials struct {
 	Provider    string         `json:"provider" validate:"required"`
 	Credentials map[string]any `json:"credentials" validate:"omitempty,dive,is_basic_type"`
 }
+
+type RequestGetToolRuntimeParameters struct {
+	Provider    string         `json:"provider" validate:"required"`
+	Tool        string         `json:"tool" validate:"required"`
+	Credentials map[string]any `json:"credentials" validate:"omitempty,dive,is_basic_type"`
+}

+ 5 - 0
internal/types/entities/tool_entities/tool.go

@@ -2,6 +2,7 @@ package tool_entities
 
 import (
 	"github.com/go-playground/validator/v10"
+	"github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/validators"
 )
 
@@ -46,3 +47,7 @@ type ToolResponseChunk struct {
 	Type    ToolResponseChunkType `json:"type" validate:"required,is_valid_tool_response_chunk_type"`
 	Message map[string]any        `json:"message"`
 }
+
+type GetToolRuntimeParametersResponse struct {
+	Parameters []plugin_entities.ToolParameter `json:"parameters"`
+}