Merge pull request #935 from trheyi/main

feat: Enhance result handling in Assistant API and hooks
This commit is contained in:
Max 2025-04-28 12:50:14 +08:00 committed by GitHub
commit 591215ca77
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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
}
// 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 {

View file

@ -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 {

View file

@ -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