From 13ab7d7f1f2050e2368c8208680ef2e185973924 Mon Sep 17 00:00:00 2001 From: Yeuoly Date: Fri, 30 Aug 2024 13:12:28 +0800 Subject: [PATCH] fix: unmarshal map --- internal/core/dify_invocation/http_request.go | 23 +++++++------- internal/core/dify_invocation/types.go | 30 +++++++++++++++++++ internal/types/entities/requests/endpoint.go | 3 +- internal/types/models/endpoint.go | 12 +++++++- internal/utils/http_requests/http_warpper.go | 5 ++++ internal/utils/parser/json.go | 6 ++++ 6 files changed, 64 insertions(+), 15 deletions(-) diff --git a/internal/core/dify_invocation/http_request.go b/internal/core/dify_invocation/http_request.go index 311e53d..23af743 100644 --- a/internal/core/dify_invocation/http_request.go +++ b/internal/core/dify_invocation/http_request.go @@ -31,18 +31,6 @@ func StreamResponse[T any](method string, path string, options ...http_requests. return http_requests.RequestAndParseStream[T](client, difyPath(path), method, options...) } -func StreamResponseMap(method string, path string, options ...http_requests.HttpOptions) (*stream.StreamResponse[map[string]any], error) { - options = append( - options, http_requests.HttpHeader(map[string]string{ - "X-Inner-Api-Key": PLUGIN_INNER_API_KEY, - }), - http_requests.HttpWriteTimeout(5000), - http_requests.HttpReadTimeout(60000), - ) - - return http_requests.RequestAndParseStreamMap(client, difyPath(path), method, options...) -} - func InvokeLLM(payload *InvokeLLMRequest) (*stream.StreamResponse[model_entities.LLMResultChunk], error) { return StreamResponse[model_entities.LLMResultChunk]("POST", "invoke/llm", http_requests.HttpPayloadJson(payload)) } @@ -72,5 +60,14 @@ func InvokeTool(payload *InvokeToolRequest) (*stream.StreamResponse[tool_entitie } func InvokeApp(payload *InvokeAppRequest) (*stream.StreamResponse[map[string]any], error) { - return StreamResponseMap("POST", "invoke/app", http_requests.HttpPayloadJson(payload)) + return StreamResponse[map[string]any]("POST", "invoke/app", http_requests.HttpPayloadJson(payload)) +} + +func InvokeEncrypt(payload *InvokeEncryptRequest) (map[string]any, error) { + data, err := Request[map[string]any]("POST", "invoke/encrypt", http_requests.HttpPayloadJson(payload)) + if err != nil { + return nil, err + } + + return *data, nil } diff --git a/internal/core/dify_invocation/types.go b/internal/core/dify_invocation/types.go index 04c21ba..4c6d960 100644 --- a/internal/core/dify_invocation/types.go +++ b/internal/core/dify_invocation/types.go @@ -3,6 +3,7 @@ package dify_invocation import ( "github.com/go-playground/validator/v10" "github.com/langgenius/dify-plugin-daemon/internal/types/entities/app_entities" + "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities" "github.com/langgenius/dify-plugin-daemon/internal/types/entities/requests" "github.com/langgenius/dify-plugin-daemon/internal/types/validators" ) @@ -26,6 +27,7 @@ const ( INVOKE_TYPE_NODE InvokeType = "node" INVOKE_TYPE_APP InvokeType = "app" INVOKE_TYPE_STORAGE InvokeType = "storage" + INVOKE_TYPE_ENCRYPT InvokeType = "encrypt" ) type InvokeLLMRequest struct { @@ -103,6 +105,34 @@ type InvokeAppRequest struct { InvokeAppSchema } +type EncryptOpt string + +const ( + ENCRYPT_OPT_ENCRYPT EncryptOpt = "encrypt" + ENCRYPT_OPT_DECRYPT EncryptOpt = "decrypt" +) + +func isEncryptOpt(fl validator.FieldLevel) bool { + opt := EncryptOpt(fl.Field().String()) + return opt == ENCRYPT_OPT_ENCRYPT || opt == ENCRYPT_OPT_DECRYPT +} + +func init() { + validators.GlobalEntitiesValidator.RegisterValidation("encrypt_opt", isEncryptOpt) +} + +type InvokeEncryptSchema struct { + Opt EncryptOpt `json:"opt" validate:"required,encrypt_opt"` + Data map[string]any `json:"data" validate:"omitempty"` + Config map[string]plugin_entities.ProviderConfig `json:"config" validate:"omitempty,dive"` +} + +type InvokeEncryptRequest struct { + BaseInvokeDifyRequest + + InvokeEncryptSchema +} + type InvokeToolRequest struct { BaseInvokeDifyRequest ToolType requests.ToolType `json:"tool_type" validate:"required,tool_type"` diff --git a/internal/types/entities/requests/endpoint.go b/internal/types/entities/requests/endpoint.go index adfdee5..19b41f6 100644 --- a/internal/types/entities/requests/endpoint.go +++ b/internal/types/entities/requests/endpoint.go @@ -1,5 +1,6 @@ package requests type RequestInvokeEndpoint struct { - RawHttpRequest string `json:"raw_http_request" validate:"required"` + RawHttpRequest string `json:"raw_http_request" validate:"required"` + Settings map[string]any `json:"settings" validate:"omitempty"` } diff --git a/internal/types/models/endpoint.go b/internal/types/models/endpoint.go index bce04bc..80349d5 100644 --- a/internal/types/models/endpoint.go +++ b/internal/types/models/endpoint.go @@ -1,6 +1,10 @@ package models -import "time" +import ( + "time" + + "github.com/langgenius/dify-plugin-daemon/internal/utils/parser" +) // HookID is a pointer to plugin id and tenant id, using it to identify the endpoint plugin type Endpoint struct { @@ -10,5 +14,11 @@ type Endpoint struct { UserID string `json:"user_id" orm:"index;size:64;column:user_id"` PluginID string `json:"plugin_id" orm:"index;size:64;column:plugin_id"` ExpiredAt time.Time `json:"expired_at" orm:"column:expired_at"` + Settings string `json:"settings" orm:"column:settings;size:2048"` PluginInstallationId string `json:"plugin_installation_id" orm:"index;size:64;column:plugin_installation_id"` } + +func (e *Endpoint) GetSettings() map[string]any { + d, _ := parser.UnmarshalJson2Map(e.Settings) + return d +} diff --git a/internal/utils/http_requests/http_warpper.go b/internal/utils/http_requests/http_warpper.go index d8d9fd0..02c1ed3 100644 --- a/internal/utils/http_requests/http_warpper.go +++ b/internal/utils/http_requests/http_warpper.go @@ -23,6 +23,11 @@ func parseJsonBody(resp *http.Response, ret interface{}) error { func RequestAndParse[T any](client *http.Client, url string, method string, options ...HttpOptions) (*T, error) { var ret T + // check if ret is a map, if so, create a new map + if _, ok := any(ret).(map[string]any); ok { + ret = *new(T) + } + resp, err := Request(client, url, method, options...) if err != nil { return nil, err diff --git a/internal/utils/parser/json.go b/internal/utils/parser/json.go index 74bea91..488408a 100644 --- a/internal/utils/parser/json.go +++ b/internal/utils/parser/json.go @@ -2,6 +2,7 @@ package parser import ( "encoding/json" + "reflect" "github.com/langgenius/dify-plugin-daemon/internal/types/validators" ) @@ -17,6 +18,11 @@ func UnmarshalJsonBytes[T any](data []byte) (T, error) { return result, err } + // skip validate if T is a map + if reflect.TypeOf(result).Kind() == reflect.Map { + return result, nil + } + if err := validators.GlobalEntitiesValidator.Struct(&result); err != nil { return result, err }