Browse Source

feat: backwards invoke summary

Yeuoly 9 months ago
parent
commit
9f561c63e3

+ 2 - 0
internal/core/dify_invocation/invcation.go

@@ -29,4 +29,6 @@ type BackwardsInvocation interface {
 	InvokeQuestionClassifier(payload *InvokeQuestionClassifierRequest) (*InvokeNodeResponse, error)
 	// InvokeEncrypt
 	InvokeEncrypt(payload *InvokeEncryptRequest) (map[string]any, error)
+	// InvokeSummary
+	InvokeSummary(payload *InvokeSummaryRequest) (*InvokeSummaryResponse, error)
 }

+ 4 - 0
internal/core/dify_invocation/real/http_request.go

@@ -149,3 +149,7 @@ func (i *RealBackwardsInvocation) InvokeEncrypt(payload *dify_invocation.InvokeE
 
 	return data.Data, nil
 }
+
+func (i *RealBackwardsInvocation) InvokeSummary(payload *dify_invocation.InvokeSummaryRequest) (*dify_invocation.InvokeSummaryResponse, error) {
+	return Request[dify_invocation.InvokeSummaryResponse](i, "POST", "invoke/summary", http_requests.HttpPayloadJson(payload))
+}

+ 6 - 0
internal/core/dify_invocation/tester/mock.go

@@ -313,3 +313,9 @@ func (m *MockedDifyInvocation) InvokeQuestionClassifier(payload *dify_invocation
 		Inputs: map[string]any{},
 	}, nil
 }
+
+func (m *MockedDifyInvocation) InvokeSummary(payload *dify_invocation.InvokeSummaryRequest) (*dify_invocation.InvokeSummaryResponse, error) {
+	return &dify_invocation.InvokeSummaryResponse{
+		Summary: payload.Text,
+	}, nil
+}

+ 15 - 0
internal/core/dify_invocation/types.go

@@ -29,6 +29,7 @@ const (
 	INVOKE_TYPE_APP                      InvokeType = "app"
 	INVOKE_TYPE_STORAGE                  InvokeType = "storage"
 	INVOKE_TYPE_ENCRYPT                  InvokeType = "encrypt"
+	INVOKE_TYPE_SUMMARY                  InvokeType = "summary"
 )
 
 type InvokeLLMRequest struct {
@@ -216,3 +217,17 @@ type InvokeEncryptionResponse struct {
 	Error string         `json:"error"`
 	Data  map[string]any `json:"data"`
 }
+
+type InvokeSummarySchema struct {
+	Text        string `json:"text" validate:"required"`
+	Instruction string `json:"instruction" validate:"omitempty"`
+}
+
+type InvokeSummaryRequest struct {
+	BaseInvokeDifyRequest
+	InvokeSummarySchema
+}
+
+type InvokeSummaryResponse struct {
+	Summary string `json:"summary"`
+}

+ 22 - 0
internal/core/plugin_daemon/backwards_invocation/task.go

@@ -131,6 +131,12 @@ var (
 			},
 			"error": "permission denied, you need to enable storage access in plugin manifest",
 		},
+		dify_invocation.INVOKE_TYPE_SUMMARY: {
+			"func": func(declaration *plugin_entities.PluginDeclaration) bool {
+				return declaration.Resource.Permission.AllowInvokeLLM()
+			},
+			"error": "permission denied, you need to enable llm access in plugin manifest",
+		},
 	}
 )
 
@@ -218,6 +224,9 @@ var (
 		dify_invocation.INVOKE_TYPE_STORAGE: func(handle *BackwardsInvocation) {
 			genericDispatchTask(handle, executeDifyInvocationStorageTask)
 		},
+		dify_invocation.INVOKE_TYPE_SUMMARY: func(handle *BackwardsInvocation) {
+			genericDispatchTask(handle, executeDifyInvocationSummaryTask)
+		},
 	}
 )
 
@@ -492,3 +501,16 @@ func executeDifyInvocationStorageTask(
 		})
 	}
 }
+
+func executeDifyInvocationSummaryTask(
+	handle *BackwardsInvocation,
+	request *dify_invocation.InvokeSummaryRequest,
+) {
+	response, err := handle.backwardsInvocation.InvokeSummary(request)
+	if err != nil {
+		handle.WriteError(fmt.Errorf("invoke summary failed: %s", err.Error()))
+		return
+	}
+
+	handle.WriteResponse("struct", response)
+}