yao/agent/assistant/source.go
Max 51092e3324 Refactor assistant model and enhance data handling
- Removed the GetByConnector method from the Assistant model to streamline the retrieval process.
- Introduced new fields for connector options and prompt presets, allowing for more flexible configurations.
- Updated the Map method to include additional fields such as connector options and prompt presets.
- Enhanced the Clone method to support deep copying of new fields.
- Improved the Update method to handle updates for the new source, connector options, and prompt presets fields.
- Refactored tests to ensure comprehensive coverage of the new functionalities and maintain clarity in the assistant structure.
2025-12-02 14:54:35 +08:00

36 lines
1.1 KiB
Go

package assistant
import (
"fmt"
"time"
v8 "github.com/yaoapp/gou/runtime/v8"
"github.com/yaoapp/yao/agent/assistant/hook"
)
// loadSource loads hook script from source code string
// The source field stores TypeScript code directly
// Priority: script field > source field (if script exists, source is ignored)
func loadSource(source string, assistantID string) (*hook.Script, error) {
if source == "" {
return nil, nil
}
// Generate a virtual file path for the script
file := fmt.Sprintf("assistants/%s/source.ts", assistantID)
script, err := v8.MakeScript([]byte(source), file, 5*time.Second, true)
if err != nil {
return nil, fmt.Errorf("failed to compile source script: %w", err)
}
return &hook.Script{Script: script}, nil
}
// TODO: Future enhancement - support multiple files merged with special comment delimiter
// Format: // file: index.ts
// This would allow splitting large scripts into multiple logical files while storing as single source
// func loadSourceMultiFile(source string, assistantID string) (*hook.Script, error) {
// // Parse source by "// file: xxx.ts" delimiter
// // Merge and compile
// }