yao/agent/assistant/hook/script.go
Max 0fe0843b43 Refactor agent and assistant structure for improved clarity and performance
- Removed deprecated API and vision components, streamlining the agent's architecture.
- Updated the Load function to utilize a new agentDSL variable, enhancing the management of assistant capabilities.
- Enhanced context handling by introducing a message metadata store for thread-safe operations, improving message tracking and management.
- Refactored context methods to eliminate deprecated fields, ensuring cleaner and more maintainable code.
- Improved documentation and comments throughout the codebase to clarify changes and enhance developer understanding.
2025-11-30 11:47:25 +08:00

40 lines
938 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
}
var sid = ""
if ctx.Authorized != nil {
sid = ctx.Authorized.SessionID
}
scriptCtx, err := s.NewContext(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
}