@@ -14,6 +14,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
agents "aichat/agenttool"
|
||||
|
||||
"github.com/volcengine/volcengine-go-sdk/service/arkruntime/model"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -82,6 +84,10 @@ type State struct {
|
||||
activeName string
|
||||
}
|
||||
|
||||
type LoadedTool struct {
|
||||
state *State
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
@@ -98,6 +104,80 @@ type ToolArgs struct {
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
agents.Register(agents.Descriptor{Name: ToolName, Load: func(path string, options agents.LoadOptions) (agents.LoadedTool, error) {
|
||||
legacy, err := legacyProfilesFromOption(options.Value(agents.LegacySearchProfilesKey))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg, err := LoadConfig(path, legacy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state, err := NewState(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &LoadedTool{state: state}, nil
|
||||
}})
|
||||
}
|
||||
|
||||
func legacyProfilesFromOption(value any) ([]ProfileConfig, error) {
|
||||
switch profiles := value.(type) {
|
||||
case nil:
|
||||
return nil, nil
|
||||
case []ProfileConfig:
|
||||
return profiles, nil
|
||||
case []map[string]any:
|
||||
data, err := yaml.Marshal(profiles)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("转换旧搜索配置失败: %w", err)
|
||||
}
|
||||
var parsed []ProfileConfig
|
||||
if err := yaml.Unmarshal(data, &parsed); err != nil {
|
||||
return nil, fmt.Errorf("解析旧搜索配置失败: %w", err)
|
||||
}
|
||||
return parsed, nil
|
||||
default:
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (t *LoadedTool) Name() string { return ToolName }
|
||||
|
||||
func (t *LoadedTool) Enabled() bool { return t != nil && t.state != nil && t.state.Enabled() }
|
||||
|
||||
func (t *LoadedTool) ToolDefinition(description string) *model.Tool {
|
||||
if t == nil || t.state == nil {
|
||||
return nil
|
||||
}
|
||||
return t.state.ToolDefinition(description)
|
||||
}
|
||||
|
||||
func (t *LoadedTool) Execute(ctx context.Context, args string, runtime agents.Runtime) (string, error) {
|
||||
if runtime.Emit != nil {
|
||||
runtime.Emit(agents.Frame{Type: "trace", Tool: ToolName, Stage: "request", Status: "running", Message: "正在联网搜索"})
|
||||
}
|
||||
result, err := t.state.ExecuteTool(ctx, args)
|
||||
if runtime.Emit != nil {
|
||||
status := "success"
|
||||
messageText := "联网搜索完成"
|
||||
if err != nil {
|
||||
status = "error"
|
||||
messageText = "联网搜索失败"
|
||||
}
|
||||
runtime.Emit(agents.Frame{Type: "trace", Tool: ToolName, Stage: "results", Status: status, Message: messageText})
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (t *LoadedTool) RawState() any {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
return t.state
|
||||
}
|
||||
|
||||
type braveSearchResponse struct {
|
||||
Web struct {
|
||||
Results []Result `json:"results"`
|
||||
|
||||
Reference in New Issue
Block a user