From a8655952540544abc10bbe22bfb9b9c9cd865827 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 28 Apr 2025 12:47:35 +0800 Subject: [PATCH] feat: Enhance result handling in Assistant API and hooks - Add result field to ResHookInit struct for improved response handling. - Update HookCreate method to initialize result in response and handle result extraction from input. - Modify execute method to return result directly if available, improving response efficiency. --- neo/assistant/api.go | 13 +++++++++++++ neo/assistant/hooks.go | 7 ++++++- neo/assistant/types.go | 1 + 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/neo/assistant/api.go b/neo/assistant/api.go index 95258bf7..86d5106b 100644 --- a/neo/assistant/api.go +++ b/neo/assistant/api.go @@ -112,6 +112,19 @@ func (ast *Assistant) execute(c *gin.Context, ctx chatctx.Context, userInput int input = res.Input } + // Has result return directly + if res != nil && res.Result != nil { + // Has callback function + if len(callback) > 0 { + output := chatMessage.New() + output.Result = res.Result + output.Callback(callback[0]).Write(c.Writer) + } + + // Return the result directly + return res.Result, nil + } + // Handle next action // It's not used, return the new assistant_id and chat_id // if res != nil && res.Next != nil { diff --git a/neo/assistant/hooks.go b/neo/assistant/hooks.go index 90e84069..76ffad8f 100644 --- a/neo/assistant/hooks.go +++ b/neo/assistant/hooks.go @@ -27,7 +27,7 @@ func (ast *Assistant) HookCreate(c *gin.Context, context chatctx.Context, input return nil, err } - response := &ResHookInit{} + response := &ResHookInit{Result: nil} switch v := v.(type) { case map[string]interface{}: if res, ok := v["assistant_id"].(string); ok { @@ -48,6 +48,11 @@ func (ast *Assistant) HookCreate(c *gin.Context, context chatctx.Context, input response.Input = vv } + // result + if result, has := v["result"]; has { + response.Result = result + } + if res, ok := v["next"].(map[string]interface{}); ok { response.Next = &NextAction{} if name, ok := res["action"].(string); ok { diff --git a/neo/assistant/types.go b/neo/assistant/types.go index c3a4097a..3a533448 100644 --- a/neo/assistant/types.go +++ b/neo/assistant/types.go @@ -44,6 +44,7 @@ type ResHookInit struct { Next *NextAction `json:"next,omitempty"` Input []message.Message `json:"input,omitempty"` Options map[string]interface{} `json:"options,omitempty"` + Result any `json:"result,omitempty"` } // ResHookStream the response of the stream hook