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.
This commit is contained in:
Max 2025-04-28 12:47:35 +08:00
parent 93be3acb97
commit a865595254
3 changed files with 20 additions and 1 deletions

View file

@ -112,6 +112,19 @@ func (ast *Assistant) execute(c *gin.Context, ctx chatctx.Context, userInput int
input = res.Input 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 // Handle next action
// It's not used, return the new assistant_id and chat_id // It's not used, return the new assistant_id and chat_id
// if res != nil && res.Next != nil { // if res != nil && res.Next != nil {

View file

@ -27,7 +27,7 @@ func (ast *Assistant) HookCreate(c *gin.Context, context chatctx.Context, input
return nil, err return nil, err
} }
response := &ResHookInit{} response := &ResHookInit{Result: nil}
switch v := v.(type) { switch v := v.(type) {
case map[string]interface{}: case map[string]interface{}:
if res, ok := v["assistant_id"].(string); ok { 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 response.Input = vv
} }
// result
if result, has := v["result"]; has {
response.Result = result
}
if res, ok := v["next"].(map[string]interface{}); ok { if res, ok := v["next"].(map[string]interface{}); ok {
response.Next = &NextAction{} response.Next = &NextAction{}
if name, ok := res["action"].(string); ok { if name, ok := res["action"].(string); ok {

View file

@ -44,6 +44,7 @@ type ResHookInit struct {
Next *NextAction `json:"next,omitempty"` Next *NextAction `json:"next,omitempty"`
Input []message.Message `json:"input,omitempty"` Input []message.Message `json:"input,omitempty"`
Options map[string]interface{} `json:"options,omitempty"` Options map[string]interface{} `json:"options,omitempty"`
Result any `json:"result,omitempty"`
} }
// ResHookStream the response of the stream hook // ResHookStream the response of the stream hook