浏览代码

fix: invoke to dify

Yeuoly 1 年之前
父节点
当前提交
e9eb6b6fd6

+ 1 - 1
internal/core/dify_invocation/http_request.go

@@ -36,7 +36,7 @@ func InvokeLLM(payload *InvokeLLMRequest) (*stream.StreamResponse[model_entities
 }
 
 func InvokeTextEmbedding(payload *InvokeTextEmbeddingRequest) (*model_entities.TextEmbeddingResult, error) {
-	return Request[model_entities.TextEmbeddingResult]("POST", "invoke/text_embedding", http_requests.HttpPayloadJson(payload))
+	return Request[model_entities.TextEmbeddingResult]("POST", "invoke/text-embedding", http_requests.HttpPayloadJson(payload))
 }
 
 func InvokeRerank(payload *InvokeRerankRequest) (*model_entities.RerankResult, error) {

+ 1 - 1
internal/core/dify_invocation/types.go

@@ -75,7 +75,7 @@ type InvokeToolRequest struct {
 	BaseInvokeDifyRequest
 	Data struct {
 		ToolType requests.ToolType `json:"tool_type" validate:"required,tool_type"`
-		requests.RequestInvokeTool
+		requests.InvokeToolSchema
 	} `json:"data" validate:"required"`
 }
 

+ 19 - 9
internal/core/plugin_daemon/invoke_dify.go

@@ -7,7 +7,6 @@ import (
 	"github.com/langgenius/dify-plugin-daemon/internal/core/plugin_daemon/backwards_invocation"
 	"github.com/langgenius/dify-plugin-daemon/internal/core/session_manager"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/entities"
-	"github.com/langgenius/dify-plugin-daemon/internal/types/entities/tool_entities"
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
 	"github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
 )
@@ -42,6 +41,7 @@ func invokeDify(
 	// dispatch invocation task
 	routine.Submit(func() {
 		dispatchDifyInvocationTask(request_handle)
+		defer request_handle.EndResponse()
 	})
 
 	return nil
@@ -99,12 +99,22 @@ func dispatchDifyInvocationTask(handle *backwards_invocation.BackwardsInvocation
 	}
 }
 
-func executeDifyInvocationToolTask(handle *backwards_invocation.BackwardsInvocation, request *dify_invocation.InvokeToolRequest) {
-	handle.WriteResponse("stream", tool_entities.ToolResponseChunk{
-		Type: "text",
-		Message: map[string]any{
-			"text": "hello world",
-		},
-	})
-	handle.EndResponse()
+func executeDifyInvocationToolTask(
+	handle *backwards_invocation.BackwardsInvocation,
+	request *dify_invocation.InvokeToolRequest,
+) {
+	response, err := dify_invocation.InvokeTool(request)
+	if err != nil {
+		handle.WriteError(fmt.Errorf("invoke tool failed: %s", err.Error()))
+		return
+	}
+
+	for response.Next() {
+		data, err := response.Read()
+		if err != nil {
+			return
+		}
+
+		handle.WriteResponse("stream", data)
+	}
 }