yao/agent/assistant/hook/create.go
Max e0f69368fb Refactor assistant creation handling and response types
- Updated the Create method in the Script type to return a structured HookCreateResponse instead of the previous ResponseHookCreate.
- Enhanced the getHookCreateResponse method to handle various response scenarios, including nil and undefined results.
- Refactored context types to introduce the new HookCreateResponse structure, which includes fields for messages, audio configuration, generation parameters, and metadata.
- Adjusted related methods in the Assistant to ensure compatibility with the new response structure.
2025-11-13 18:27:15 +08:00

44 lines
1.1 KiB
Go

package hook
import (
"encoding/json"
"fmt"
"github.com/yaoapp/gou/runtime/v8/bridge"
"github.com/yaoapp/yao/agent/context"
)
// Create create a new assistant
func (s *Script) Create(ctx *context.Context, messages []context.Message) (*context.HookCreateResponse, error) {
res, err := s.Execute(ctx, "Create", messages)
if err != nil {
return nil, err
}
return s.getHookCreateResponse(res)
}
// getHookCreateResponse convert the result to a HookCreateResponse
func (s *Script) getHookCreateResponse(res interface{}) (*context.HookCreateResponse, error) {
// Handle nil result
if res == nil {
return nil, nil
}
// Handle undefined result (treat as nil)
if _, ok := res.(bridge.UndefinedT); ok {
return nil, nil
}
// Marshal to JSON and unmarshal to HookCreateResponse
raw, err := json.Marshal(res)
if err != nil {
return nil, fmt.Errorf("failed to marshal result: %w", err)
}
var response context.HookCreateResponse
if err := json.Unmarshal(raw, &response); err != nil {
return nil, fmt.Errorf("failed to unmarshal to HookCreateResponse: %w", err)
}
return &response, nil
}