yao/agent/assistant/hook/script.go
Max 3bfd40b701 Refactor Assistant's Stream method to support Next hook responses
- Updated the Stream method to return a flexible final response, accommodating both standard and Next hook responses.
- Introduced NextProcessContext to encapsulate context for processing Next hook responses, enhancing clarity and maintainability.
- Removed deprecated Done and Failback hooks, streamlining the hook management process.
- Enhanced error handling and logging for improved traceability during streaming operations.
2025-11-29 22:52:06 +08:00

35 lines
863 B
Go

package hook
import (
"strings"
"github.com/yaoapp/yao/agent/context"
)
// Execute execute the script
func (s *Script) Execute(ctx *context.Context, method string, args ...interface{}) (interface{}, error) {
if s == nil || s.Script == nil {
return nil, nil
}
scriptCtx, err := s.NewContext(ctx.Sid, nil)
if err != nil {
return nil, err
}
defer scriptCtx.Close()
// The first argument is the context
args = append([]interface{}{ctx}, args...)
// Try to call the method
result, err := scriptCtx.CallWith(ctx.Context, method, args...)
// If method doesn't exist (ReferenceError or similar), return nil without error
if err != nil && (strings.Contains(err.Error(), "is not defined") ||
strings.Contains(err.Error(), "is not a function") ||
strings.Contains(err.Error(), "is not a Function")) {
return nil, nil
}
return result, err
}