Merge pull request #879 from trheyi/main
Add result handling in assistant streaming and hook responses
This commit is contained in:
commit
d42f96ffc7
4 changed files with 19 additions and 5 deletions
|
|
@ -131,7 +131,7 @@ func (ast *Assistant) execute(c *gin.Context, ctx chatctx.Context, input []chatM
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute the next action
|
// Execute the next action
|
||||||
func (next *NextAction) Execute(c *gin.Context, ctx chatctx.Context, contents *chatMessage.Contents) error {
|
func (next *NextAction) Execute(c *gin.Context, ctx chatctx.Context, contents *chatMessage.Contents, callback ...interface{}) error {
|
||||||
switch next.Action {
|
switch next.Action {
|
||||||
|
|
||||||
// It's not used, because the process could be executed in the hook script
|
// It's not used, because the process could be executed in the hook script
|
||||||
|
|
@ -256,7 +256,7 @@ func (next *NextAction) Execute(c *gin.Context, ctx chatctx.Context, contents *c
|
||||||
|
|
||||||
// Update the context id
|
// Update the context id
|
||||||
ctx.AssistantID = assistant.ID
|
ctx.AssistantID = assistant.ID
|
||||||
return assistant.execute(c, ctx, messages, options, newContents)
|
return assistant.execute(c, ctx, messages, options, newContents, callback...)
|
||||||
|
|
||||||
case "exit":
|
case "exit":
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -534,7 +534,8 @@ func (ast *Assistant) streamChat(
|
||||||
|
|
||||||
// Some error occurred in the hook, return the error
|
// Some error occurred in the hook, return the error
|
||||||
if hookErr != nil {
|
if hookErr != nil {
|
||||||
chatMessage.New().Error(hookErr.Error()).Done().Write(c.Writer)
|
chatMessage.New().Error(hookErr.Error()).Done().Callback(cb).Write(c.Writer)
|
||||||
|
|
||||||
done <- true
|
done <- true
|
||||||
return 0 // break
|
return 0 // break
|
||||||
}
|
}
|
||||||
|
|
@ -544,9 +545,9 @@ func (ast *Assistant) streamChat(
|
||||||
|
|
||||||
// If the hook is successful, execute the next action
|
// If the hook is successful, execute the next action
|
||||||
if res != nil && res.Next != nil {
|
if res != nil && res.Next != nil {
|
||||||
err := res.Next.Execute(c, ctx, contents)
|
err := res.Next.Execute(c, ctx, contents, cb)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
chatMessage.New().Error(err.Error()).Done().Write(c.Writer)
|
chatMessage.New().Error(err.Error()).Done().Callback(cb).Write(c.Writer)
|
||||||
}
|
}
|
||||||
done <- true
|
done <- true
|
||||||
return 0 // break
|
return 0 // break
|
||||||
|
|
@ -559,6 +560,12 @@ func (ast *Assistant) streamChat(
|
||||||
output.Retry = ctx.Retry
|
output.Retry = ctx.Retry
|
||||||
output.Silent = ctx.Silent
|
output.Silent = ctx.Silent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// has result
|
||||||
|
if res != nil && res.Result != nil && cb != nil {
|
||||||
|
output.Result = res.Result // Add the result to the output message
|
||||||
|
}
|
||||||
|
|
||||||
output.Callback(cb).Write(c.Writer)
|
output.Callback(cb).Write(c.Writer)
|
||||||
done <- true
|
done <- true
|
||||||
return 0 // break
|
return 0 // break
|
||||||
|
|
|
||||||
|
|
@ -217,6 +217,11 @@ func (ast *Assistant) HookDone(c *gin.Context, context chatctx.Context, input []
|
||||||
response.Output = vv
|
response.Output = vv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// has result
|
||||||
|
if res, has := v["result"]; has {
|
||||||
|
response.Result = res
|
||||||
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ type ResHookDone struct {
|
||||||
Next *NextAction `json:"next,omitempty"`
|
Next *NextAction `json:"next,omitempty"`
|
||||||
Input []message.Message `json:"input,omitempty"`
|
Input []message.Message `json:"input,omitempty"`
|
||||||
Output []message.Data `json:"output,omitempty"`
|
Output []message.Data `json:"output,omitempty"`
|
||||||
|
Result any `json:"result,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResHookFail the response of the fail hook
|
// ResHookFail the response of the fail hook
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@ type Message struct {
|
||||||
IsTool bool `json:"-"` // is tool for the message for native tool_calls
|
IsTool bool `json:"-"` // is tool for the message for native tool_calls
|
||||||
IsBeginTool bool `json:"-"` // is new tool for the message for native tool_calls
|
IsBeginTool bool `json:"-"` // is new tool for the message for native tool_calls
|
||||||
IsEndTool bool `json:"-"` // is end tool for the message for native tool_calls
|
IsEndTool bool `json:"-"` // is end tool for the message for native tool_calls
|
||||||
|
Result any `json:"result,omitempty"` // result for the message
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mention represents a mention
|
// Mention represents a mention
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue