feat: Improve error handling and result processing in Assistant API

- Update streamChat method to set a default prompt for error messages, enhancing user guidance.
- Refactor case handling in streamChat to better manage boolean and map results, improving response clarity.
- Modify HookRetry method to streamline result extraction and handle action presence more effectively, ensuring robust error handling.
This commit is contained in:
Max 2025-04-29 20:35:27 +08:00
parent a865595254
commit bc13b0d83f
2 changed files with 28 additions and 10 deletions

View file

@ -687,9 +687,18 @@ func (ast *Assistant) streamChat(
return nil, retry return nil, retry
} }
var prompt string = "" // Default prompt
var prompt string = fmt.Sprintf("Try to fix the error following the error message. error:\n %s", exception.Trim(retry))
switch v := promptAny.(type) { switch v := promptAny.(type) {
case NextAction: case bool: // Ignore the error, and return the specific result
if v == false {
return nil, retry
}
case map[string]interface{}: // Ignore the error, and return the specific result
return v, nil
case NextAction: // Execute the next action
result, err := v.Execute(c, ctx, contents, cb) result, err := v.Execute(c, ctx, contents, cb)
if err != nil { if err != nil {
// chatMessage.New().Error(err.Error()).Done().Callback(cb).Write(c.Writer) // chatMessage.New().Error(err.Error()).Done().Callback(cb).Write(c.Writer)
@ -697,7 +706,7 @@ func (ast *Assistant) streamChat(
} }
return result, nil return result, nil
case string: case string: // Add the prompt to the messages
prompt = v prompt = v
} }

View file

@ -164,16 +164,25 @@ func (ast *Assistant) HookRetry(c *gin.Context, context chatctx.Context, input [
} }
switch v := v.(type) { switch v := v.(type) {
case string: case string, bool:
return v, nil return v, nil
case map[string]interface{}: case map[string]interface{}:
var next NextAction
raw, _ := jsoniter.MarshalToString(v) // Has Action
err := jsoniter.UnmarshalFromString(raw, &next) if _, has := v["action"]; has {
if err != nil { var next NextAction
return nil, err raw, _ := jsoniter.MarshalToString(v)
err := jsoniter.UnmarshalFromString(raw, &next)
if err != nil {
return nil, err
}
return &next, nil
} }
return &next, nil
// Ignore the error, and return the specific result
return v, nil
} }
return nil, nil return nil, nil