Skip to content

Commit

Permalink
feat: migrate to mapstruct
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Jul 22, 2024
1 parent c69b51a commit f62f3af
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 159 deletions.
69 changes: 0 additions & 69 deletions internal/core/dify_invocation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dify_invocation

import (
"encoding/json"
"fmt"

"github.com/langgenius/dify-plugin-daemon/internal/types/entities/model_entities"
)
Expand All @@ -13,23 +12,6 @@ type BaseInvokeDifyRequest struct {
Type InvokeType `json:"type"`
}

func (r *BaseInvokeDifyRequest) FromMap(data map[string]any) error {
var ok bool
if r.TenantId, ok = data["tenant_id"].(string); !ok {
return fmt.Errorf("tenant_id is not a string")
}

if r.UserId, ok = data["user_id"].(string); !ok {
return fmt.Errorf("user_id is not a string")
}

if r.Type, ok = data["type"].(InvokeType); !ok {
return fmt.Errorf("type is not a string")
}

return nil
}

type InvokeType string

const (
Expand All @@ -46,27 +28,6 @@ type InvokeModelRequest struct {
Parameters map[string]any `json:"parameters"`
}

func (r *InvokeModelRequest) FromMap(base map[string]any, data map[string]any) error {
var ok bool
if r.Provider, ok = data["provider"].(string); !ok {
return fmt.Errorf("provider is not a string")
}

if r.Model, ok = data["model"].(string); !ok {
return fmt.Errorf("model is not a string")
}

if r.ModelType, ok = data["model_type"].(model_entities.ModelType); !ok {
return fmt.Errorf("model_type is not a string")
}

if r.Parameters, ok = data["parameters"].(map[string]any); !ok {
return fmt.Errorf("parameters is not a map")
}

return nil
}

func (r InvokeModelRequest) MarshalJSON() ([]byte, error) {
flattened := make(map[string]any)
flattened["tenant_id"] = r.TenantId
Expand All @@ -87,23 +48,6 @@ type InvokeToolRequest struct {
Parameters map[string]any `json:"parameters"`
}

func (r *InvokeToolRequest) FromMap(base map[string]any, data map[string]any) error {
var ok bool
if r.Provider, ok = data["provider"].(string); !ok {
return fmt.Errorf("provider is not a string")
}

if r.Tool, ok = data["tool"].(string); !ok {
return fmt.Errorf("tool is not a string")
}

if r.Parameters, ok = data["parameters"].(map[string]any); !ok {
return fmt.Errorf("parameters is not a map")
}

return nil
}

func (r InvokeToolRequest) MarshalJSON() ([]byte, error) {
flattened := make(map[string]any)
flattened["tenant_id"] = r.TenantId
Expand All @@ -123,19 +67,6 @@ type InvokeNodeRequest[T WorkflowNodeData] struct {
NodeData T `json:"node_data"`
}

func (r *InvokeNodeRequest[T]) FromMap(data map[string]any) error {
var ok bool
if r.NodeType, ok = data["node_type"].(NodeType); !ok {
return fmt.Errorf("node_type is not a string")
}

if err := r.NodeData.FromMap(data["node_data"].(map[string]any)); err != nil {
return err
}

return nil
}

func (r InvokeNodeRequest[T]) MarshalJSON() ([]byte, error) {
flattened := make(map[string]any)
flattened["tenant_id"] = r.TenantId
Expand Down
16 changes: 1 addition & 15 deletions internal/core/dify_invocation/workflow_node_data.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package dify_invocation

type WorkflowNodeData interface {
FromMap(map[string]any) error

*KnowledgeRetrievalNodeData | *QuestionClassifierNodeData | *ParameterExtractorNodeData
KnowledgeRetrievalNodeData | QuestionClassifierNodeData | ParameterExtractorNodeData
}

type NodeType string
Expand All @@ -18,20 +16,8 @@ const (
type KnowledgeRetrievalNodeData struct {
}

func (r *KnowledgeRetrievalNodeData) FromMap(data map[string]any) error {
return nil
}

type QuestionClassifierNodeData struct {
}

func (r *QuestionClassifierNodeData) FromMap(data map[string]any) error {
return nil
}

type ParameterExtractorNodeData struct {
}

func (r *ParameterExtractorNodeData) FromMap(data map[string]any) error {
return nil
}
43 changes: 43 additions & 0 deletions internal/core/plugin_daemon/backwards_invocation/entities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package backwards_invocation

type RequestEvent string

const (
REQUEST_EVENT_RESPONSE RequestEvent = "response"
REQUEST_EVENT_ERROR RequestEvent = "error"
REQUEST_EVENT_END RequestEvent = "end"
)

type BaseRequestEvent struct {
BackwardsRequestId string `json:"backwards_request_id"`
Event RequestEvent `json:"event"`
Message string `json:"message"`
Data map[string]any `json:"data"`
}

func NewResponseEvent(request_id string, message string, data map[string]any) *BaseRequestEvent {
return &BaseRequestEvent{
BackwardsRequestId: request_id,
Event: REQUEST_EVENT_RESPONSE,
Message: message,
Data: data,
}
}

func NewErrorEvent(request_id string, message string) *BaseRequestEvent {
return &BaseRequestEvent{
BackwardsRequestId: request_id,
Event: REQUEST_EVENT_ERROR,
Message: message,
Data: nil,
}
}

func NewEndEvent(request_id string) *BaseRequestEvent {
return &BaseRequestEvent{
BackwardsRequestId: request_id,
Event: REQUEST_EVENT_END,
Message: "",
Data: nil,
}
}
52 changes: 52 additions & 0 deletions internal/core/plugin_daemon/backwards_invocation/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package backwards_invocation

import (
"github.com/langgenius/dify-plugin-daemon/internal/core/dify_invocation"
"github.com/langgenius/dify-plugin-daemon/internal/core/session_manager"
"github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
)

type BackwardsInvocationType = dify_invocation.InvokeType

type BackwardsInvocation struct {
typ BackwardsInvocationType
id string
detailed_request map[string]any
session *session_manager.Session
}

func NewBackwardsInvocation(
typ BackwardsInvocationType,
id string, session *session_manager.Session, detailed_request map[string]any,
) *BackwardsInvocation {
return &BackwardsInvocation{
typ: typ,
id: id,
detailed_request: detailed_request,
session: session,
}
}

func (bi *BackwardsInvocation) GetID() string {
return bi.id
}

func (bi *BackwardsInvocation) WriteError(err error) {
bi.session.Write(parser.MarshalJsonBytes(NewErrorEvent(bi.id, err.Error())))
}

func (bi *BackwardsInvocation) Write(message string, data map[string]any) {
bi.session.Write(parser.MarshalJsonBytes(NewResponseEvent(bi.id, message, data)))
}

func (bi *BackwardsInvocation) End() {
bi.session.Write(parser.MarshalJsonBytes(NewEndEvent(bi.id)))
}

func (bi *BackwardsInvocation) Type() BackwardsInvocationType {
return bi.typ
}

func (bi *BackwardsInvocation) RequestData() map[string]any {
return bi.detailed_request
}
81 changes: 54 additions & 27 deletions internal/core/plugin_daemon/invoke_dify.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/langgenius/dify-plugin-daemon/internal/core/dify_invocation"
"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/utils/log"
Expand All @@ -13,6 +14,7 @@ import (

func invokeDify(
runtime entities.PluginRuntimeInterface,
invoke_from PluginAccessType,
session *session_manager.Session, data []byte,
) error {
// unmarshal invoke data
Expand All @@ -22,37 +24,67 @@ func invokeDify(
return fmt.Errorf("unmarshal invoke request failed: %s", err.Error())
}

// prepare invocation arguments
request_handle, err := prepareDifyInvocationArguments(session, request)
if err != nil {
return err
}
defer request_handle.End()

if invoke_from == PLUGIN_ACCESS_TYPE_MODEL {
request_handle.WriteError(fmt.Errorf("you can not invoke dify from %s", invoke_from))
return nil
}

// dispatch invocation task
dispatchDifyInvocationTask(request_handle)

return nil
}

func prepareDifyInvocationArguments(session *session_manager.Session, request map[string]any) (*backwards_invocation.BackwardsInvocation, error) {
typ, ok := request["type"].(string)
if !ok {
return fmt.Errorf("invoke request missing type: %s", data)
return nil, fmt.Errorf("invoke request missing type: %s", request)
}

// get request id
request_id, ok := request["request_id"].(string)
backwards_request_id, ok := request["backwards_request_id"].(string)
if !ok {
return fmt.Errorf("invoke request missing request_id: %s", data)
return nil, fmt.Errorf("invoke request missing request_id: %s", request)
}

// get request
detailed_request, ok := request["request"].(map[string]any)
if !ok {
return fmt.Errorf("invoke request missing request: %s", data)
return nil, fmt.Errorf("invoke request missing request: %s", request)
}

switch typ {
case "tool":
r := dify_invocation.InvokeToolRequest{}
if err := r.FromMap(request, detailed_request); err != nil {
return fmt.Errorf("unmarshal tool invoke request failed: %s", err.Error())
return backwards_invocation.NewBackwardsInvocation(
backwards_invocation.BackwardsInvocationType(typ),
backwards_request_id, session, detailed_request,
), nil
}

func dispatchDifyInvocationTask(handle *backwards_invocation.BackwardsInvocation) {
switch handle.Type() {
case dify_invocation.INVOKE_TYPE_TOOL:
r, err := parser.MapToStruct[dify_invocation.InvokeToolRequest](handle.RequestData())
if err != nil {
handle.WriteError(fmt.Errorf("unmarshal invoke tool request failed: %s", err.Error()))
return
}
submitToolTask(runtime, session, request_id, &r)
case "model":
r := dify_invocation.InvokeModelRequest{}
if err := r.FromMap(request, detailed_request); err != nil {
return fmt.Errorf("unmarshal model invoke request failed: %s", err.Error())

submitToolTask(runtime, session, backwards_request_id, &r)

Check failure on line 78 in internal/core/plugin_daemon/invoke_dify.go

View workflow job for this annotation

GitHub Actions / test

undefined: runtime

Check failure on line 78 in internal/core/plugin_daemon/invoke_dify.go

View workflow job for this annotation

GitHub Actions / test

undefined: session

Check failure on line 78 in internal/core/plugin_daemon/invoke_dify.go

View workflow job for this annotation

GitHub Actions / test

undefined: backwards_request_id
case dify_invocation.INVOKE_TYPE_MODEL:
r, err := parser.MapToStruct[dify_invocation.InvokeModelRequest](handle.RequestData())
if err != nil {
handle.WriteError(fmt.Errorf("unmarshal invoke model request failed: %s", err.Error()))
return
}
submitModelTask(runtime, session, request_id, &r)
case "node":

submitModelTask(runtime, session, backwards_request_id, &r)

Check failure on line 86 in internal/core/plugin_daemon/invoke_dify.go

View workflow job for this annotation

GitHub Actions / test

undefined: runtime

Check failure on line 86 in internal/core/plugin_daemon/invoke_dify.go

View workflow job for this annotation

GitHub Actions / test

undefined: session

Check failure on line 86 in internal/core/plugin_daemon/invoke_dify.go

View workflow job for this annotation

GitHub Actions / test

undefined: backwards_request_id
case dify_invocation.INVOKE_TYPE_NODE:
node_type, ok := detailed_request["node_type"].(dify_invocation.NodeType)

Check failure on line 88 in internal/core/plugin_daemon/invoke_dify.go

View workflow job for this annotation

GitHub Actions / test

undefined: detailed_request
if !ok {
return fmt.Errorf("invoke request missing node_type: %s", data)

Check failure on line 90 in internal/core/plugin_daemon/invoke_dify.go

View workflow job for this annotation

GitHub Actions / test

too many return values

Check failure on line 90 in internal/core/plugin_daemon/invoke_dify.go

View workflow job for this annotation

GitHub Actions / test

undefined: data
Expand All @@ -63,40 +95,35 @@ func invokeDify(
}
switch node_type {
case dify_invocation.QUESTION_CLASSIFIER:
d := dify_invocation.InvokeNodeRequest[*dify_invocation.QuestionClassifierNodeData]{
d := dify_invocation.InvokeNodeRequest[dify_invocation.QuestionClassifierNodeData]{
NodeType: dify_invocation.QUESTION_CLASSIFIER,
NodeData: &dify_invocation.QuestionClassifierNodeData{},
}
if err := d.FromMap(node_data); err != nil {
return fmt.Errorf("unmarshal question classifier node data failed: %s", err.Error())
}
submitNodeInvocationRequestTask(runtime, session, request_id, &d)
submitNodeInvocationRequestTask(runtime, session, backwards_request_id, &d)
case dify_invocation.KNOWLEDGE_RETRIEVAL:
d := dify_invocation.InvokeNodeRequest[*dify_invocation.KnowledgeRetrievalNodeData]{
d := dify_invocation.InvokeNodeRequest[dify_invocation.KnowledgeRetrievalNodeData]{
NodeType: dify_invocation.KNOWLEDGE_RETRIEVAL,
NodeData: &dify_invocation.KnowledgeRetrievalNodeData{},
}
if err := d.FromMap(node_data); err != nil {
return fmt.Errorf("unmarshal knowledge retrieval node data failed: %s", err.Error())
}
submitNodeInvocationRequestTask(runtime, session, request_id, &d)
submitNodeInvocationRequestTask(runtime, session, backwards_request_id, &d)
case dify_invocation.PARAMETER_EXTRACTOR:
d := dify_invocation.InvokeNodeRequest[*dify_invocation.ParameterExtractorNodeData]{
d := dify_invocation.InvokeNodeRequest[dify_invocation.ParameterExtractorNodeData]{
NodeType: dify_invocation.PARAMETER_EXTRACTOR,
NodeData: &dify_invocation.ParameterExtractorNodeData{},
}
if err := d.FromMap(node_data); err != nil {
return fmt.Errorf("unmarshal parameter extractor node data failed: %s", err.Error())
}
submitNodeInvocationRequestTask(runtime, session, request_id, &d)
submitNodeInvocationRequestTask(runtime, session, backwards_request_id, &d)
default:
return fmt.Errorf("unknown node type: %s", node_type)
}
default:
return fmt.Errorf("unknown invoke type: %s", typ)
}

return nil
}

func setTaskContext(session *session_manager.Session, r *dify_invocation.BaseInvokeDifyRequest) {
Expand Down
2 changes: 1 addition & 1 deletion internal/core/plugin_daemon/model_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func genericInvokePlugin[Req any, Rsp any](
}
response.Write(chunk)
case plugin_entities.SESSION_MESSAGE_TYPE_INVOKE:
invokeDify(runtime, session, chunk.Data)
invokeDify(runtime, typ, session, chunk.Data)
case plugin_entities.SESSION_MESSAGE_TYPE_END:
response.Close()
case plugin_entities.SESSION_MESSAGE_TYPE_ERROR:
Expand Down
Loading

0 comments on commit f62f3af

Please sign in to comment.