Skip to content

Commit

Permalink
feat: backwards invoke summary
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Oct 17, 2024
1 parent e7e9358 commit 9f561c6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/core/dify_invocation/invcation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 4 additions & 0 deletions internal/core/dify_invocation/real/http_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 6 additions & 0 deletions internal/core/dify_invocation/tester/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 15 additions & 0 deletions internal/core/dify_invocation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 changes: 22 additions & 0 deletions internal/core/plugin_daemon/backwards_invocation/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
)

Expand Down Expand Up @@ -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)
},
}
)

Expand Down Expand Up @@ -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)
}

0 comments on commit 9f561c6

Please sign in to comment.