Merge branch 'main' of https://github.com/decker502/picoclaw
This commit is contained in:
commit
5c92986ef2
47 changed files with 906 additions and 365 deletions
|
|
@ -66,7 +66,6 @@ linters:
|
||||||
- testifylint
|
- testifylint
|
||||||
- thelper
|
- thelper
|
||||||
- unparam
|
- unparam
|
||||||
- unused
|
|
||||||
- usestdlibvars
|
- usestdlibvars
|
||||||
- usetesting
|
- usetesting
|
||||||
- wastedassign
|
- wastedassign
|
||||||
|
|
@ -152,6 +151,9 @@ linters:
|
||||||
- gocognit
|
- gocognit
|
||||||
- gocyclo
|
- gocyclo
|
||||||
path: _test\.go$
|
path: _test\.go$
|
||||||
|
- linters:
|
||||||
|
- nolintlint
|
||||||
|
path: 'pkg/tools/(i2c\.go|spi\.go)$'
|
||||||
|
|
||||||
issues:
|
issues:
|
||||||
max-issues-per-linter: 0
|
max-issues-per-linter: 0
|
||||||
|
|
|
||||||
8
Makefile
8
Makefile
|
|
@ -14,7 +14,7 @@ GO_VERSION=$(shell $(GO) version | awk '{print $$3}')
|
||||||
LDFLAGS=-ldflags "-X main.version=$(VERSION) -X main.gitCommit=$(GIT_COMMIT) -X main.buildTime=$(BUILD_TIME) -X main.goVersion=$(GO_VERSION) -s -w"
|
LDFLAGS=-ldflags "-X main.version=$(VERSION) -X main.gitCommit=$(GIT_COMMIT) -X main.buildTime=$(BUILD_TIME) -X main.goVersion=$(GO_VERSION) -s -w"
|
||||||
|
|
||||||
# Go variables
|
# Go variables
|
||||||
GO?=go
|
GO?=CGO_ENABLED=0 go
|
||||||
GOFLAGS?=-v -tags stdjson
|
GOFLAGS?=-v -tags stdjson
|
||||||
|
|
||||||
# Golangci-lint
|
# Golangci-lint
|
||||||
|
|
@ -144,6 +144,10 @@ fmt:
|
||||||
lint:
|
lint:
|
||||||
@$(GOLANGCI_LINT) run
|
@$(GOLANGCI_LINT) run
|
||||||
|
|
||||||
|
## fix: Fix linting issues
|
||||||
|
fix:
|
||||||
|
@$(GOLANGCI_LINT) run --fix
|
||||||
|
|
||||||
## deps: Download dependencies
|
## deps: Download dependencies
|
||||||
deps:
|
deps:
|
||||||
@$(GO) mod download
|
@$(GO) mod download
|
||||||
|
|
@ -169,7 +173,7 @@ help:
|
||||||
@echo " make [target]"
|
@echo " make [target]"
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "Targets:"
|
@echo "Targets:"
|
||||||
@grep -E '^## ' $(MAKEFILE_LIST) | sed 's/## / /'
|
@grep -E '^## ' $(MAKEFILE_LIST) | sort | awk -F': ' '{printf " %-16s %s\n", substr($$1, 4), $$2}'
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "Examples:"
|
@echo "Examples:"
|
||||||
@echo " make build # Build for current platform"
|
@echo " make build # Build for current platform"
|
||||||
|
|
|
||||||
|
|
@ -226,7 +226,7 @@ picoclaw onboard
|
||||||
],
|
],
|
||||||
"agents": {
|
"agents": {
|
||||||
"defaults": {
|
"defaults": {
|
||||||
"model": "gpt4"
|
"model_name": "gpt4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"channels": {
|
"channels": {
|
||||||
|
|
|
||||||
|
|
@ -188,7 +188,7 @@ picoclaw onboard
|
||||||
],
|
],
|
||||||
"agents": {
|
"agents": {
|
||||||
"defaults": {
|
"defaults": {
|
||||||
"model": "gpt4"
|
"model_name": "gpt4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"channels": {
|
"channels": {
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,9 @@
|
||||||
<br>
|
<br>
|
||||||
<a href="https://picoclaw.io"><img src="https://img.shields.io/badge/Website-picoclaw.io-blue?style=flat&logo=google-chrome&logoColor=white" alt="Website"></a>
|
<a href="https://picoclaw.io"><img src="https://img.shields.io/badge/Website-picoclaw.io-blue?style=flat&logo=google-chrome&logoColor=white" alt="Website"></a>
|
||||||
<a href="https://x.com/SipeedIO"><img src="https://img.shields.io/badge/X_(Twitter)-SipeedIO-black?style=flat&logo=x&logoColor=white" alt="Twitter"></a>
|
<a href="https://x.com/SipeedIO"><img src="https://img.shields.io/badge/X_(Twitter)-SipeedIO-black?style=flat&logo=x&logoColor=white" alt="Twitter"></a>
|
||||||
|
<br>
|
||||||
|
<a href="./assets/wechat.png"><img src="https://img.shields.io/badge/WeChat-Group-41d56b?style=flat&logo=wechat&logoColor=white"></a>
|
||||||
|
<a href="https://discord.gg/V4sAZ9XWpN"><img src="https://img.shields.io/badge/Discord-Community-4c60eb?style=flat&logo=discord&logoColor=white" alt="Discord"></a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
[中文](README.zh.md) | [日本語](README.ja.md) | [Português](README.pt-br.md) | [Tiếng Việt](README.vi.md) | [Français](README.fr.md) | **English**
|
[中文](README.zh.md) | [日本語](README.ja.md) | [Português](README.pt-br.md) | [Tiếng Việt](README.vi.md) | [Français](README.fr.md) | **English**
|
||||||
|
|
@ -219,7 +222,7 @@ picoclaw onboard
|
||||||
"agents": {
|
"agents": {
|
||||||
"defaults": {
|
"defaults": {
|
||||||
"workspace": "~/.picoclaw/workspace",
|
"workspace": "~/.picoclaw/workspace",
|
||||||
"model": "gpt4",
|
"model_name": "gpt4",
|
||||||
"max_tokens": 8192,
|
"max_tokens": 8192,
|
||||||
"temperature": 0.7,
|
"temperature": 0.7,
|
||||||
"max_tool_iterations": 20
|
"max_tool_iterations": 20
|
||||||
|
|
|
||||||
|
|
@ -227,7 +227,7 @@ picoclaw onboard
|
||||||
],
|
],
|
||||||
"agents": {
|
"agents": {
|
||||||
"defaults": {
|
"defaults": {
|
||||||
"model": "gpt4"
|
"model_name": "gpt4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"tools": {
|
"tools": {
|
||||||
|
|
|
||||||
|
|
@ -207,7 +207,7 @@ picoclaw onboard
|
||||||
],
|
],
|
||||||
"agents": {
|
"agents": {
|
||||||
"defaults": {
|
"defaults": {
|
||||||
"model": "gpt4"
|
"model_name": "gpt4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"channels": {
|
"channels": {
|
||||||
|
|
|
||||||
|
|
@ -224,7 +224,7 @@ picoclaw onboard
|
||||||
"agents": {
|
"agents": {
|
||||||
"defaults": {
|
"defaults": {
|
||||||
"workspace": "~/.picoclaw/workspace",
|
"workspace": "~/.picoclaw/workspace",
|
||||||
"model": "gpt4",
|
"model_name": "gpt4",
|
||||||
"max_tokens": 8192,
|
"max_tokens": 8192,
|
||||||
"temperature": 0.7,
|
"temperature": 0.7,
|
||||||
"max_tool_iterations": 20
|
"max_tool_iterations": 20
|
||||||
|
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 141 KiB After Width: | Height: | Size: 147 KiB |
|
|
@ -56,7 +56,7 @@ func agentCmd() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if modelOverride != "" {
|
if modelOverride != "" {
|
||||||
cfg.Agents.Defaults.Model = modelOverride
|
cfg.Agents.Defaults.ModelName = modelOverride
|
||||||
}
|
}
|
||||||
|
|
||||||
provider, modelID, err := providers.CreateProvider(cfg)
|
provider, modelID, err := providers.CreateProvider(cfg)
|
||||||
|
|
@ -66,7 +66,7 @@ func agentCmd() {
|
||||||
}
|
}
|
||||||
// Use the resolved model ID from provider creation
|
// Use the resolved model ID from provider creation
|
||||||
if modelID != "" {
|
if modelID != "" {
|
||||||
cfg.Agents.Defaults.Model = modelID
|
cfg.Agents.Defaults.ModelName = modelID
|
||||||
}
|
}
|
||||||
|
|
||||||
msgBus := bus.NewMessageBus()
|
msgBus := bus.NewMessageBus()
|
||||||
|
|
|
||||||
|
|
@ -144,7 +144,7 @@ func authLoginOpenAI(useDeviceCode bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update default model to use OpenAI
|
// Update default model to use OpenAI
|
||||||
appCfg.Agents.Defaults.Model = "gpt-5.2"
|
appCfg.Agents.Defaults.ModelName = "gpt-5.2"
|
||||||
|
|
||||||
if err := config.SaveConfig(getConfigPath(), appCfg); err != nil {
|
if err := config.SaveConfig(getConfigPath(), appCfg); err != nil {
|
||||||
fmt.Printf("Warning: could not update config: %v\n", err)
|
fmt.Printf("Warning: could not update config: %v\n", err)
|
||||||
|
|
@ -218,7 +218,7 @@ func authLoginGoogleAntigravity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update default model
|
// Update default model
|
||||||
appCfg.Agents.Defaults.Model = "gemini-flash"
|
appCfg.Agents.Defaults.ModelName = "gemini-flash"
|
||||||
|
|
||||||
if err := config.SaveConfig(getConfigPath(), appCfg); err != nil {
|
if err := config.SaveConfig(getConfigPath(), appCfg); err != nil {
|
||||||
fmt.Printf("Warning: could not update config: %v\n", err)
|
fmt.Printf("Warning: could not update config: %v\n", err)
|
||||||
|
|
@ -292,7 +292,7 @@ func authLoginPasteToken(provider string) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Update default model
|
// Update default model
|
||||||
appCfg.Agents.Defaults.Model = "claude-sonnet-4.6"
|
appCfg.Agents.Defaults.ModelName = "claude-sonnet-4.6"
|
||||||
case "openai":
|
case "openai":
|
||||||
appCfg.Providers.OpenAI.AuthMethod = "token"
|
appCfg.Providers.OpenAI.AuthMethod = "token"
|
||||||
// Update ModelList
|
// Update ModelList
|
||||||
|
|
@ -312,7 +312,7 @@ func authLoginPasteToken(provider string) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Update default model
|
// Update default model
|
||||||
appCfg.Agents.Defaults.Model = "gpt-5.2"
|
appCfg.Agents.Defaults.ModelName = "gpt-5.2"
|
||||||
}
|
}
|
||||||
if err := config.SaveConfig(getConfigPath(), appCfg); err != nil {
|
if err := config.SaveConfig(getConfigPath(), appCfg); err != nil {
|
||||||
fmt.Printf("Warning: could not update config: %v\n", err)
|
fmt.Printf("Warning: could not update config: %v\n", err)
|
||||||
|
|
@ -320,7 +320,7 @@ func authLoginPasteToken(provider string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Token saved for %s!\n", provider)
|
fmt.Printf("Token saved for %s!\n", provider)
|
||||||
fmt.Printf("Default model set to: %s\n", appCfg.Agents.Defaults.Model)
|
fmt.Printf("Default model set to: %s\n", appCfg.Agents.Defaults.GetModelName())
|
||||||
}
|
}
|
||||||
|
|
||||||
func authLogoutCmd() {
|
func authLogoutCmd() {
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ func gatewayCmd() {
|
||||||
}
|
}
|
||||||
// Use the resolved model ID from provider creation
|
// Use the resolved model ID from provider creation
|
||||||
if modelID != "" {
|
if modelID != "" {
|
||||||
cfg.Agents.Defaults.Model = modelID
|
cfg.Agents.Defaults.ModelName = modelID
|
||||||
}
|
}
|
||||||
|
|
||||||
msgBus := bus.NewMessageBus()
|
msgBus := bus.NewMessageBus()
|
||||||
|
|
@ -211,6 +211,9 @@ func gatewayCmd() {
|
||||||
<-sigChan
|
<-sigChan
|
||||||
|
|
||||||
fmt.Println("\nShutting down...")
|
fmt.Println("\nShutting down...")
|
||||||
|
if cp, ok := provider.(providers.StatefulProvider); ok {
|
||||||
|
cp.Close()
|
||||||
|
}
|
||||||
cancel()
|
cancel()
|
||||||
healthServer.Stop(context.Background())
|
healthServer.Stop(context.Background())
|
||||||
deviceService.Stop()
|
deviceService.Stop()
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ func statusCmd() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(configPath); err == nil {
|
if _, err := os.Stat(configPath); err == nil {
|
||||||
fmt.Printf("Model: %s\n", cfg.Agents.Defaults.Model)
|
fmt.Printf("Model: %s\n", cfg.Agents.Defaults.GetModelName())
|
||||||
|
|
||||||
hasOpenRouter := cfg.Providers.OpenRouter.APIKey != ""
|
hasOpenRouter := cfg.Providers.OpenRouter.APIKey != ""
|
||||||
hasAnthropic := cfg.Providers.Anthropic.APIKey != ""
|
hasAnthropic := cfg.Providers.Anthropic.APIKey != ""
|
||||||
|
|
|
||||||
|
|
@ -131,7 +131,7 @@ func main() {
|
||||||
|
|
||||||
workspace := cfg.WorkspacePath()
|
workspace := cfg.WorkspacePath()
|
||||||
installer := skills.NewSkillInstaller(workspace)
|
installer := skills.NewSkillInstaller(workspace)
|
||||||
// 获取全局配置目录和内置 skills 目录
|
// get global config directory and builtin skills directory
|
||||||
globalDir := filepath.Dir(getConfigPath())
|
globalDir := filepath.Dir(getConfigPath())
|
||||||
globalSkillsDir := filepath.Join(globalDir, "skills")
|
globalSkillsDir := filepath.Join(globalDir, "skills")
|
||||||
builtinSkillsDir := filepath.Join(globalDir, "picoclaw", "skills")
|
builtinSkillsDir := filepath.Join(globalDir, "picoclaw", "skills")
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
"defaults": {
|
"defaults": {
|
||||||
"workspace": "~/.picoclaw/workspace",
|
"workspace": "~/.picoclaw/workspace",
|
||||||
"restrict_to_workspace": true,
|
"restrict_to_workspace": true,
|
||||||
"model": "gpt4",
|
"model_name": "gpt4",
|
||||||
"max_tokens": 8192,
|
"max_tokens": 8192,
|
||||||
"temperature": 0.7,
|
"temperature": 0.7,
|
||||||
"max_tool_iterations": 20
|
"max_tool_iterations": 20
|
||||||
|
|
@ -217,7 +217,8 @@
|
||||||
"enabled": false,
|
"enabled": false,
|
||||||
"api_key": "pplx-xxx",
|
"api_key": "pplx-xxx",
|
||||||
"max_results": 5
|
"max_results": 5
|
||||||
}
|
},
|
||||||
|
"proxy": ""
|
||||||
},
|
},
|
||||||
"cron": {
|
"cron": {
|
||||||
"exec_timeout_minutes": 5
|
"exec_timeout_minutes": 5
|
||||||
|
|
|
||||||
|
|
@ -288,25 +288,6 @@ func (cb *ContextBuilder) AddAssistantMessage(
|
||||||
return messages
|
return messages
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cb *ContextBuilder) loadSkills() string {
|
|
||||||
allSkills := cb.skillsLoader.ListSkills()
|
|
||||||
if len(allSkills) == 0 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
var skillNames []string
|
|
||||||
for _, s := range allSkills {
|
|
||||||
skillNames = append(skillNames, s.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
content := cb.skillsLoader.LoadSkillsForContext(skillNames)
|
|
||||||
if content == "" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return "# Skill Definitions\n\n" + content
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetSkillsInfo returns information about loaded skills.
|
// GetSkillsInfo returns information about loaded skills.
|
||||||
func (cb *ContextBuilder) GetSkillsInfo() map[string]any {
|
func (cb *ContextBuilder) GetSkillsInfo() map[string]any {
|
||||||
allSkills := cb.skillsLoader.ListSkills()
|
allSkills := cb.skillsLoader.ListSkills()
|
||||||
|
|
|
||||||
|
|
@ -133,7 +133,7 @@ func resolveAgentModel(agentCfg *config.AgentConfig, defaults *config.AgentDefau
|
||||||
if agentCfg != nil && agentCfg.Model != nil && strings.TrimSpace(agentCfg.Model.Primary) != "" {
|
if agentCfg != nil && agentCfg.Model != nil && strings.TrimSpace(agentCfg.Model.Primary) != "" {
|
||||||
return strings.TrimSpace(agentCfg.Model.Primary)
|
return strings.TrimSpace(agentCfg.Model.Primary)
|
||||||
}
|
}
|
||||||
return defaults.Model
|
return defaults.GetModelName()
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolveAgentFallbacks resolves the fallback models for an agent.
|
// resolveAgentFallbacks resolves the fallback models for an agent.
|
||||||
|
|
|
||||||
|
|
@ -106,10 +106,11 @@ func registerSharedTools(
|
||||||
PerplexityAPIKey: cfg.Tools.Web.Perplexity.APIKey,
|
PerplexityAPIKey: cfg.Tools.Web.Perplexity.APIKey,
|
||||||
PerplexityMaxResults: cfg.Tools.Web.Perplexity.MaxResults,
|
PerplexityMaxResults: cfg.Tools.Web.Perplexity.MaxResults,
|
||||||
PerplexityEnabled: cfg.Tools.Web.Perplexity.Enabled,
|
PerplexityEnabled: cfg.Tools.Web.Perplexity.Enabled,
|
||||||
|
Proxy: cfg.Tools.Web.Proxy,
|
||||||
}); searchTool != nil {
|
}); searchTool != nil {
|
||||||
agent.Tools.Register(searchTool)
|
agent.Tools.Register(searchTool)
|
||||||
}
|
}
|
||||||
agent.Tools.Register(tools.NewWebFetchTool(50000))
|
agent.Tools.Register(tools.NewWebFetchToolWithProxy(50000, cfg.Tools.Web.Proxy))
|
||||||
|
|
||||||
// Hardware tools (I2C, SPI) - Linux only, returns error on other platforms
|
// Hardware tools (I2C, SPI) - Linux only, returns error on other platforms
|
||||||
agent.Tools.Register(tools.NewI2CTool())
|
agent.Tools.Register(tools.NewI2CTool())
|
||||||
|
|
@ -628,6 +629,7 @@ func (al *AgentLoop) runLLMIteration(
|
||||||
assistantMsg := providers.Message{
|
assistantMsg := providers.Message{
|
||||||
Role: "assistant",
|
Role: "assistant",
|
||||||
Content: response.Content,
|
Content: response.Content,
|
||||||
|
ReasoningContent: response.ReasoningContent,
|
||||||
}
|
}
|
||||||
for _, tc := range normalizedToolCalls {
|
for _, tc := range normalizedToolCalls {
|
||||||
argumentsJSON, _ := json.Marshal(tc.Arguments)
|
argumentsJSON, _ := json.Marshal(tc.Arguments)
|
||||||
|
|
|
||||||
|
|
@ -47,31 +47,31 @@ func (c *QQChannel) Start(ctx context.Context) error {
|
||||||
|
|
||||||
logger.InfoC("qq", "Starting QQ bot (WebSocket mode)")
|
logger.InfoC("qq", "Starting QQ bot (WebSocket mode)")
|
||||||
|
|
||||||
// 创建 token source
|
// create token source
|
||||||
credentials := &token.QQBotCredentials{
|
credentials := &token.QQBotCredentials{
|
||||||
AppID: c.config.AppID,
|
AppID: c.config.AppID,
|
||||||
AppSecret: c.config.AppSecret,
|
AppSecret: c.config.AppSecret,
|
||||||
}
|
}
|
||||||
c.tokenSource = token.NewQQBotTokenSource(credentials)
|
c.tokenSource = token.NewQQBotTokenSource(credentials)
|
||||||
|
|
||||||
// 创建子 context
|
// create child context
|
||||||
c.ctx, c.cancel = context.WithCancel(ctx)
|
c.ctx, c.cancel = context.WithCancel(ctx)
|
||||||
|
|
||||||
// 启动自动刷新 token 协程
|
// start auto-refresh token goroutine
|
||||||
if err := token.StartRefreshAccessToken(c.ctx, c.tokenSource); err != nil {
|
if err := token.StartRefreshAccessToken(c.ctx, c.tokenSource); err != nil {
|
||||||
return fmt.Errorf("failed to start token refresh: %w", err)
|
return fmt.Errorf("failed to start token refresh: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化 OpenAPI 客户端
|
// initialize OpenAPI client
|
||||||
c.api = botgo.NewOpenAPI(c.config.AppID, c.tokenSource).WithTimeout(5 * time.Second)
|
c.api = botgo.NewOpenAPI(c.config.AppID, c.tokenSource).WithTimeout(5 * time.Second)
|
||||||
|
|
||||||
// 注册事件处理器
|
// register event handlers
|
||||||
intent := event.RegisterHandlers(
|
intent := event.RegisterHandlers(
|
||||||
c.handleC2CMessage(),
|
c.handleC2CMessage(),
|
||||||
c.handleGroupATMessage(),
|
c.handleGroupATMessage(),
|
||||||
)
|
)
|
||||||
|
|
||||||
// 获取 WebSocket 接入点
|
// get WebSocket endpoint
|
||||||
wsInfo, err := c.api.WS(c.ctx, nil, "")
|
wsInfo, err := c.api.WS(c.ctx, nil, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get websocket info: %w", err)
|
return fmt.Errorf("failed to get websocket info: %w", err)
|
||||||
|
|
@ -81,10 +81,10 @@ func (c *QQChannel) Start(ctx context.Context) error {
|
||||||
"shards": wsInfo.Shards,
|
"shards": wsInfo.Shards,
|
||||||
})
|
})
|
||||||
|
|
||||||
// 创建并保存 sessionManager
|
// create and save sessionManager
|
||||||
c.sessionManager = botgo.NewSessionManager()
|
c.sessionManager = botgo.NewSessionManager()
|
||||||
|
|
||||||
// 在 goroutine 中启动 WebSocket 连接,避免阻塞
|
// start WebSocket connection in goroutine to avoid blocking
|
||||||
go func() {
|
go func() {
|
||||||
if err := c.sessionManager.Start(wsInfo, c.tokenSource, &intent); err != nil {
|
if err := c.sessionManager.Start(wsInfo, c.tokenSource, &intent); err != nil {
|
||||||
logger.ErrorCF("qq", "WebSocket session error", map[string]any{
|
logger.ErrorCF("qq", "WebSocket session error", map[string]any{
|
||||||
|
|
@ -116,12 +116,12 @@ func (c *QQChannel) Send(ctx context.Context, msg bus.OutboundMessage) error {
|
||||||
return fmt.Errorf("QQ bot not running")
|
return fmt.Errorf("QQ bot not running")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构造消息
|
// construct message
|
||||||
msgToCreate := &dto.MessageToCreate{
|
msgToCreate := &dto.MessageToCreate{
|
||||||
Content: msg.Content,
|
Content: msg.Content,
|
||||||
}
|
}
|
||||||
|
|
||||||
// C2C 消息发送
|
// send C2C message
|
||||||
_, err := c.api.PostC2CMessage(ctx, msg.ChatID, msgToCreate)
|
_, err := c.api.PostC2CMessage(ctx, msg.ChatID, msgToCreate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.ErrorCF("qq", "Failed to send C2C message", map[string]any{
|
logger.ErrorCF("qq", "Failed to send C2C message", map[string]any{
|
||||||
|
|
@ -133,15 +133,15 @@ func (c *QQChannel) Send(ctx context.Context, msg bus.OutboundMessage) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleC2CMessage 处理 QQ 私聊消息
|
// handleC2CMessage handles QQ private messages
|
||||||
func (c *QQChannel) handleC2CMessage() event.C2CMessageEventHandler {
|
func (c *QQChannel) handleC2CMessage() event.C2CMessageEventHandler {
|
||||||
return func(event *dto.WSPayload, data *dto.WSC2CMessageData) error {
|
return func(event *dto.WSPayload, data *dto.WSC2CMessageData) error {
|
||||||
// 去重检查
|
// deduplication check
|
||||||
if c.isDuplicate(data.ID) {
|
if c.isDuplicate(data.ID) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 提取用户信息
|
// extract user info
|
||||||
var senderID string
|
var senderID string
|
||||||
if data.Author != nil && data.Author.ID != "" {
|
if data.Author != nil && data.Author.ID != "" {
|
||||||
senderID = data.Author.ID
|
senderID = data.Author.ID
|
||||||
|
|
@ -150,7 +150,7 @@ func (c *QQChannel) handleC2CMessage() event.C2CMessageEventHandler {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 提取消息内容
|
// extract message content
|
||||||
content := data.Content
|
content := data.Content
|
||||||
if content == "" {
|
if content == "" {
|
||||||
logger.DebugC("qq", "Received empty message, ignoring")
|
logger.DebugC("qq", "Received empty message, ignoring")
|
||||||
|
|
@ -162,7 +162,7 @@ func (c *QQChannel) handleC2CMessage() event.C2CMessageEventHandler {
|
||||||
"length": len(content),
|
"length": len(content),
|
||||||
})
|
})
|
||||||
|
|
||||||
// 转发到消息总线
|
// forward to message bus
|
||||||
metadata := map[string]string{
|
metadata := map[string]string{
|
||||||
"message_id": data.ID,
|
"message_id": data.ID,
|
||||||
"peer_kind": "direct",
|
"peer_kind": "direct",
|
||||||
|
|
@ -175,15 +175,15 @@ func (c *QQChannel) handleC2CMessage() event.C2CMessageEventHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleGroupATMessage 处理群@消息
|
// handleGroupATMessage handles group @messages
|
||||||
func (c *QQChannel) handleGroupATMessage() event.GroupATMessageEventHandler {
|
func (c *QQChannel) handleGroupATMessage() event.GroupATMessageEventHandler {
|
||||||
return func(event *dto.WSPayload, data *dto.WSGroupATMessageData) error {
|
return func(event *dto.WSPayload, data *dto.WSGroupATMessageData) error {
|
||||||
// 去重检查
|
// deduplication check
|
||||||
if c.isDuplicate(data.ID) {
|
if c.isDuplicate(data.ID) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 提取用户信息
|
// extract user info
|
||||||
var senderID string
|
var senderID string
|
||||||
if data.Author != nil && data.Author.ID != "" {
|
if data.Author != nil && data.Author.ID != "" {
|
||||||
senderID = data.Author.ID
|
senderID = data.Author.ID
|
||||||
|
|
@ -192,7 +192,7 @@ func (c *QQChannel) handleGroupATMessage() event.GroupATMessageEventHandler {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 提取消息内容(去掉 @ 机器人部分)
|
// extract message content (remove @bot part)
|
||||||
content := data.Content
|
content := data.Content
|
||||||
if content == "" {
|
if content == "" {
|
||||||
logger.DebugC("qq", "Received empty group message, ignoring")
|
logger.DebugC("qq", "Received empty group message, ignoring")
|
||||||
|
|
@ -205,7 +205,7 @@ func (c *QQChannel) handleGroupATMessage() event.GroupATMessageEventHandler {
|
||||||
"length": len(content),
|
"length": len(content),
|
||||||
})
|
})
|
||||||
|
|
||||||
// 转发到消息总线(使用 GroupID 作为 ChatID)
|
// forward to message bus (use GroupID as ChatID)
|
||||||
metadata := map[string]string{
|
metadata := map[string]string{
|
||||||
"message_id": data.ID,
|
"message_id": data.ID,
|
||||||
"group_id": data.GroupID,
|
"group_id": data.GroupID,
|
||||||
|
|
@ -219,7 +219,7 @@ func (c *QQChannel) handleGroupATMessage() event.GroupATMessageEventHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// isDuplicate 检查消息是否重复
|
// isDuplicate checks if message is duplicate
|
||||||
func (c *QQChannel) isDuplicate(messageID string) bool {
|
func (c *QQChannel) isDuplicate(messageID string) bool {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
|
|
@ -230,9 +230,9 @@ func (c *QQChannel) isDuplicate(messageID string) bool {
|
||||||
|
|
||||||
c.processedIDs[messageID] = true
|
c.processedIDs[messageID] = true
|
||||||
|
|
||||||
// 简单清理:限制 map 大小
|
// simple cleanup: limit map size
|
||||||
if len(c.processedIDs) > 10000 {
|
if len(c.processedIDs) > 10000 {
|
||||||
// 清空一半
|
// clear half
|
||||||
count := 0
|
count := 0
|
||||||
for id := range c.processedIDs {
|
for id := range c.processedIDs {
|
||||||
if count >= 5000 {
|
if count >= 5000 {
|
||||||
|
|
|
||||||
|
|
@ -200,7 +200,7 @@ func (c *SlackChannel) handleMessageEvent(ev *slackevents.MessageEvent) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查白名单,避免为被拒绝的用户下载附件
|
// check allowlist to avoid downloading attachments for rejected users
|
||||||
if !c.IsAllowed(ev.User) {
|
if !c.IsAllowed(ev.User) {
|
||||||
logger.DebugCF("slack", "Message rejected by allowlist", map[string]any{
|
logger.DebugCF("slack", "Message rejected by allowlist", map[string]any{
|
||||||
"user_id": ev.User,
|
"user_id": ev.User,
|
||||||
|
|
@ -232,9 +232,9 @@ func (c *SlackChannel) handleMessageEvent(ev *slackevents.MessageEvent) {
|
||||||
content = c.stripBotMention(content)
|
content = c.stripBotMention(content)
|
||||||
|
|
||||||
var mediaPaths []string
|
var mediaPaths []string
|
||||||
localFiles := []string{} // 跟踪需要清理的本地文件
|
localFiles := []string{} // track local files that need cleanup
|
||||||
|
|
||||||
// 确保临时文件在函数返回时被清理
|
// ensure temp files are cleaned up when function returns
|
||||||
defer func() {
|
defer func() {
|
||||||
for _, file := range localFiles {
|
for _, file := range localFiles {
|
||||||
if err := os.Remove(file); err != nil {
|
if err := os.Remove(file); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -215,7 +215,7 @@ func (c *TelegramChannel) handleMessage(ctx context.Context, message *telego.Mes
|
||||||
senderID = fmt.Sprintf("%d|%s", user.ID, user.Username)
|
senderID = fmt.Sprintf("%d|%s", user.ID, user.Username)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查白名单,避免为被拒绝的用户下载附件
|
// check allowlist to avoid downloading attachments for rejected users
|
||||||
if !c.IsAllowed(senderID) {
|
if !c.IsAllowed(senderID) {
|
||||||
logger.DebugCF("telegram", "Message rejected by allowlist", map[string]any{
|
logger.DebugCF("telegram", "Message rejected by allowlist", map[string]any{
|
||||||
"user_id": senderID,
|
"user_id": senderID,
|
||||||
|
|
@ -228,9 +228,9 @@ func (c *TelegramChannel) handleMessage(ctx context.Context, message *telego.Mes
|
||||||
|
|
||||||
content := ""
|
content := ""
|
||||||
mediaPaths := []string{}
|
mediaPaths := []string{}
|
||||||
localFiles := []string{} // 跟踪需要清理的本地文件
|
localFiles := []string{} // track local files that need cleanup
|
||||||
|
|
||||||
// 确保临时文件在函数返回时被清理
|
// ensure temp files are cleaned up when function returns
|
||||||
defer func() {
|
defer func() {
|
||||||
for _, file := range localFiles {
|
for _, file := range localFiles {
|
||||||
if err := os.Remove(file); err != nil {
|
if err := os.Remove(file); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ func (c *cmd) Show(ctx context.Context, message telego.Message) error {
|
||||||
switch args {
|
switch args {
|
||||||
case "model":
|
case "model":
|
||||||
response = fmt.Sprintf("Current Model: %s (Provider: %s)",
|
response = fmt.Sprintf("Current Model: %s (Provider: %s)",
|
||||||
c.config.Agents.Defaults.Model,
|
c.config.Agents.Defaults.GetModelName(),
|
||||||
c.config.Agents.Defaults.Provider)
|
c.config.Agents.Defaults.Provider)
|
||||||
case "channel":
|
case "channel":
|
||||||
response = "Current Channel: telegram"
|
response = "Current Channel: telegram"
|
||||||
|
|
@ -120,7 +120,7 @@ func (c *cmd) List(ctx context.Context, message telego.Message) error {
|
||||||
provider = "configured default"
|
provider = "configured default"
|
||||||
}
|
}
|
||||||
response = fmt.Sprintf("Configured Model: %s\nProvider: %s\n\nTo change models, update config.yaml",
|
response = fmt.Sprintf("Configured Model: %s\nProvider: %s\n\nTo change models, update config.yaml",
|
||||||
c.config.Agents.Defaults.Model, provider)
|
c.config.Agents.Defaults.GetModelName(), provider)
|
||||||
|
|
||||||
case "channels":
|
case "channels":
|
||||||
var enabled []string
|
var enabled []string
|
||||||
|
|
|
||||||
|
|
@ -571,61 +571,6 @@ func (c *WeComAppChannel) sendTextMessage(ctx context.Context, accessToken, user
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// sendMarkdownMessage sends a markdown message to a user
|
|
||||||
func (c *WeComAppChannel) sendMarkdownMessage(ctx context.Context, accessToken, userID, content string) error {
|
|
||||||
apiURL := fmt.Sprintf("%s/cgi-bin/message/send?access_token=%s", wecomAPIBase, accessToken)
|
|
||||||
|
|
||||||
msg := WeComMarkdownMessage{
|
|
||||||
ToUser: userID,
|
|
||||||
MsgType: "markdown",
|
|
||||||
AgentID: c.config.AgentID,
|
|
||||||
}
|
|
||||||
msg.Markdown.Content = content
|
|
||||||
|
|
||||||
jsonData, err := json.Marshal(msg)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to marshal message: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use configurable timeout (default 5 seconds)
|
|
||||||
timeout := c.config.ReplyTimeout
|
|
||||||
if timeout <= 0 {
|
|
||||||
timeout = 5
|
|
||||||
}
|
|
||||||
|
|
||||||
reqCtx, cancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(reqCtx, http.MethodPost, apiURL, bytes.NewBuffer(jsonData))
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create request: %w", err)
|
|
||||||
}
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
client := &http.Client{Timeout: time.Duration(timeout) * time.Second}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to send message: %w", err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to read response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var sendResp WeComSendMessageResponse
|
|
||||||
if err := json.Unmarshal(body, &sendResp); err != nil {
|
|
||||||
return fmt.Errorf("failed to parse response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if sendResp.ErrCode != 0 {
|
|
||||||
return fmt.Errorf("API error: %s (code: %d)", sendResp.ErrMsg, sendResp.ErrCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleHealth handles health check requests
|
// handleHealth handles health check requests
|
||||||
func (c *WeComAppChannel) handleHealth(w http.ResponseWriter, r *http.Request) {
|
func (c *WeComAppChannel) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||||
status := map[string]any{
|
status := map[string]any{
|
||||||
|
|
|
||||||
|
|
@ -170,7 +170,8 @@ type AgentDefaults struct {
|
||||||
Workspace string `json:"workspace" env:"PICOCLAW_AGENTS_DEFAULTS_WORKSPACE"`
|
Workspace string `json:"workspace" env:"PICOCLAW_AGENTS_DEFAULTS_WORKSPACE"`
|
||||||
RestrictToWorkspace bool `json:"restrict_to_workspace" env:"PICOCLAW_AGENTS_DEFAULTS_RESTRICT_TO_WORKSPACE"`
|
RestrictToWorkspace bool `json:"restrict_to_workspace" env:"PICOCLAW_AGENTS_DEFAULTS_RESTRICT_TO_WORKSPACE"`
|
||||||
Provider string `json:"provider" env:"PICOCLAW_AGENTS_DEFAULTS_PROVIDER"`
|
Provider string `json:"provider" env:"PICOCLAW_AGENTS_DEFAULTS_PROVIDER"`
|
||||||
Model string `json:"model" env:"PICOCLAW_AGENTS_DEFAULTS_MODEL"`
|
ModelName string `json:"model_name,omitempty" env:"PICOCLAW_AGENTS_DEFAULTS_MODEL_NAME"`
|
||||||
|
Model string `json:"model,omitempty" env:"PICOCLAW_AGENTS_DEFAULTS_MODEL"` // Deprecated: use model_name instead
|
||||||
ModelFallbacks []string `json:"model_fallbacks,omitempty"`
|
ModelFallbacks []string `json:"model_fallbacks,omitempty"`
|
||||||
ImageModel string `json:"image_model,omitempty" env:"PICOCLAW_AGENTS_DEFAULTS_IMAGE_MODEL"`
|
ImageModel string `json:"image_model,omitempty" env:"PICOCLAW_AGENTS_DEFAULTS_IMAGE_MODEL"`
|
||||||
ImageModelFallbacks []string `json:"image_model_fallbacks,omitempty"`
|
ImageModelFallbacks []string `json:"image_model_fallbacks,omitempty"`
|
||||||
|
|
@ -179,6 +180,15 @@ type AgentDefaults struct {
|
||||||
MaxToolIterations int `json:"max_tool_iterations" env:"PICOCLAW_AGENTS_DEFAULTS_MAX_TOOL_ITERATIONS"`
|
MaxToolIterations int `json:"max_tool_iterations" env:"PICOCLAW_AGENTS_DEFAULTS_MAX_TOOL_ITERATIONS"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetModelName returns the effective model name for the agent defaults.
|
||||||
|
// It prefers the new "model_name" field but falls back to "model" for backward compatibility.
|
||||||
|
func (d *AgentDefaults) GetModelName() string {
|
||||||
|
if d.ModelName != "" {
|
||||||
|
return d.ModelName
|
||||||
|
}
|
||||||
|
return d.Model
|
||||||
|
}
|
||||||
|
|
||||||
type ChannelsConfig struct {
|
type ChannelsConfig struct {
|
||||||
WhatsApp WhatsAppConfig `json:"whatsapp"`
|
WhatsApp WhatsAppConfig `json:"whatsapp"`
|
||||||
Telegram TelegramConfig `json:"telegram"`
|
Telegram TelegramConfig `json:"telegram"`
|
||||||
|
|
@ -443,6 +453,9 @@ type WebToolsConfig struct {
|
||||||
Tavily TavilyConfig `json:"tavily"`
|
Tavily TavilyConfig `json:"tavily"`
|
||||||
DuckDuckGo DuckDuckGoConfig `json:"duckduckgo"`
|
DuckDuckGo DuckDuckGoConfig `json:"duckduckgo"`
|
||||||
Perplexity PerplexityConfig `json:"perplexity"`
|
Perplexity PerplexityConfig `json:"perplexity"`
|
||||||
|
// Proxy is an optional proxy URL for web tools (http/https/socks5/socks5h).
|
||||||
|
// For authenticated proxies, prefer HTTP_PROXY/HTTPS_PROXY env vars instead of embedding credentials in config.
|
||||||
|
Proxy string `json:"proxy,omitempty" env:"PICOCLAW_TOOLS_WEB_PROXY"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CronToolsConfig struct {
|
type CronToolsConfig struct {
|
||||||
|
|
@ -499,6 +512,20 @@ func LoadConfig(path string) (*Config, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pre-scan the JSON to check how many model_list entries the user provided.
|
||||||
|
// Go's JSON decoder reuses existing slice backing-array elements rather than
|
||||||
|
// zero-initializing them, so fields absent from the user's JSON (e.g. api_base)
|
||||||
|
// would silently inherit values from the DefaultConfig template at the same
|
||||||
|
// index position. We only reset cfg.ModelList when the user actually provides
|
||||||
|
// entries; when count is 0 we keep DefaultConfig's built-in list as fallback.
|
||||||
|
var tmp Config
|
||||||
|
if err := json.Unmarshal(data, &tmp); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(tmp.ModelList) > 0 {
|
||||||
|
cfg.ModelList = nil
|
||||||
|
}
|
||||||
|
|
||||||
if err := json.Unmarshal(data, cfg); err != nil {
|
if err := json.Unmarshal(data, cfg); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -392,3 +392,24 @@ func TestLoadConfig_OpenAIWebSearchCanBeDisabled(t *testing.T) {
|
||||||
t.Fatal("OpenAI codex web search should be false when disabled in config file")
|
t.Fatal("OpenAI codex web search should be false when disabled in config file")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadConfig_WebToolsProxy(t *testing.T) {
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
configPath := filepath.Join(tmpDir, "config.json")
|
||||||
|
configJSON := `{
|
||||||
|
"agents": {"defaults":{"workspace":"./workspace","model":"gpt4","max_tokens":8192,"max_tool_iterations":20}},
|
||||||
|
"model_list": [{"model_name":"gpt4","model":"openai/gpt-5.2","api_key":"x"}],
|
||||||
|
"tools": {"web":{"proxy":"http://127.0.0.1:7890"}}
|
||||||
|
}`
|
||||||
|
if err := os.WriteFile(configPath, []byte(configJSON), 0o600); err != nil {
|
||||||
|
t.Fatalf("os.WriteFile() error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := LoadConfig(configPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("LoadConfig() error: %v", err)
|
||||||
|
}
|
||||||
|
if cfg.Tools.Web.Proxy != "http://127.0.0.1:7890" {
|
||||||
|
t.Fatalf("Tools.Web.Proxy = %q, want %q", cfg.Tools.Web.Proxy, "http://127.0.0.1:7890")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ func ConvertProvidersToModelList(cfg *Config) []ModelConfig {
|
||||||
|
|
||||||
// Get user's configured provider and model
|
// Get user's configured provider and model
|
||||||
userProvider := strings.ToLower(cfg.Agents.Defaults.Provider)
|
userProvider := strings.ToLower(cfg.Agents.Defaults.Provider)
|
||||||
userModel := cfg.Agents.Defaults.Model
|
userModel := cfg.Agents.Defaults.GetModelName()
|
||||||
|
|
||||||
p := cfg.Providers
|
p := cfg.Providers
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -114,6 +115,137 @@ func TestGetModelConfig_Concurrent(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAgentDefaults_GetModelName_BackwardCompat(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
defaults AgentDefaults
|
||||||
|
wantName string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "new model_name field only",
|
||||||
|
defaults: AgentDefaults{ModelName: "new-model"},
|
||||||
|
wantName: "new-model",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "old model field only",
|
||||||
|
defaults: AgentDefaults{Model: "legacy-model"},
|
||||||
|
wantName: "legacy-model",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "both fields - model_name takes precedence",
|
||||||
|
defaults: AgentDefaults{ModelName: "new-model", Model: "old-model"},
|
||||||
|
wantName: "new-model",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := tt.defaults.GetModelName(); got != tt.wantName {
|
||||||
|
t.Errorf("GetModelName() = %q, want %q", got, tt.wantName)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAgentDefaults_JSON_BackwardCompat(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
json string
|
||||||
|
wantName string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "new model_name field",
|
||||||
|
json: `{"model_name": "gpt4"}`,
|
||||||
|
wantName: "gpt4",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "old model field",
|
||||||
|
json: `{"model": "gpt4"}`,
|
||||||
|
wantName: "gpt4",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "both fields - model_name wins",
|
||||||
|
json: `{"model_name": "new", "model": "old"}`,
|
||||||
|
wantName: "new",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var defaults AgentDefaults
|
||||||
|
if err := json.Unmarshal([]byte(tt.json), &defaults); err != nil {
|
||||||
|
t.Fatalf("Unmarshal error: %v", err)
|
||||||
|
}
|
||||||
|
if got := defaults.GetModelName(); got != tt.wantName {
|
||||||
|
t.Errorf("GetModelName() = %q, want %q", got, tt.wantName)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFullConfig_JSON_BackwardCompat(t *testing.T) {
|
||||||
|
// Test complete config with both old and new formats
|
||||||
|
oldFormat := `{
|
||||||
|
"agents": {
|
||||||
|
"defaults": {
|
||||||
|
"workspace": "~/.picoclaw/workspace",
|
||||||
|
"model": "gpt4",
|
||||||
|
"max_tokens": 4096
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"model_list": [
|
||||||
|
{
|
||||||
|
"model_name": "gpt4",
|
||||||
|
"model": "openai/gpt-4o",
|
||||||
|
"api_key": "test-key"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}`
|
||||||
|
|
||||||
|
newFormat := `{
|
||||||
|
"agents": {
|
||||||
|
"defaults": {
|
||||||
|
"workspace": "~/.picoclaw/workspace",
|
||||||
|
"model_name": "gpt4",
|
||||||
|
"max_tokens": 4096
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"model_list": [
|
||||||
|
{
|
||||||
|
"model_name": "gpt4",
|
||||||
|
"model": "openai/gpt-4o",
|
||||||
|
"api_key": "test-key"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}`
|
||||||
|
|
||||||
|
for name, jsonStr := range map[string]string{
|
||||||
|
"old format (model)": oldFormat,
|
||||||
|
"new format (model_name)": newFormat,
|
||||||
|
} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
cfg := &Config{}
|
||||||
|
if err := json.Unmarshal([]byte(jsonStr), cfg); err != nil {
|
||||||
|
t.Fatalf("Unmarshal error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that GetModelName returns correct value
|
||||||
|
if got := cfg.Agents.Defaults.GetModelName(); got != "gpt4" {
|
||||||
|
t.Errorf("GetModelName() = %q, want %q", got, "gpt4")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that GetModelConfig works
|
||||||
|
modelCfg, err := cfg.GetModelConfig("gpt4")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetModelConfig error: %v", err)
|
||||||
|
}
|
||||||
|
if modelCfg.Model != "openai/gpt-4o" {
|
||||||
|
t.Errorf("Model = %q, want %q", modelCfg.Model, "openai/gpt-4o")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestModelConfig_Validate(t *testing.T) {
|
func TestModelConfig_Validate(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
|
||||||
|
|
@ -345,7 +345,7 @@ func (cs *CronService) saveStoreUnsafe() error {
|
||||||
// truncation and completion leaves an empty or partial file.
|
// truncation and completion leaves an empty or partial file.
|
||||||
// os.Rename on the same filesystem is atomic on Linux.
|
// os.Rename on the same filesystem is atomic on Linux.
|
||||||
tmpPath := cs.storePath + ".tmp"
|
tmpPath := cs.storePath + ".tmp"
|
||||||
if err := os.WriteFile(tmpPath, data, 0600); err != nil {
|
if err := os.WriteFile(tmpPath, data, 0o600); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return os.Rename(tmpPath, cs.storePath)
|
return os.Rename(tmpPath, cs.storePath)
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,6 @@ var usbClassToCapability = map[string]string{
|
||||||
|
|
||||||
type USBMonitor struct {
|
type USBMonitor struct {
|
||||||
cmd *exec.Cmd
|
cmd *exec.Cmd
|
||||||
cancel context.CancelFunc
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,10 @@ func ConvertConfig(data map[string]any) (*config.Config, []string, error) {
|
||||||
|
|
||||||
if agents, ok := getMap(data, "agents"); ok {
|
if agents, ok := getMap(data, "agents"); ok {
|
||||||
if defaults, ok := getMap(agents, "defaults"); ok {
|
if defaults, ok := getMap(agents, "defaults"); ok {
|
||||||
if v, ok := getString(defaults, "model"); ok {
|
// Prefer model_name, fallback to model for backward compatibility
|
||||||
|
if v, ok := getString(defaults, "model_name"); ok {
|
||||||
|
cfg.Agents.Defaults.ModelName = v
|
||||||
|
} else if v, ok := getString(defaults, "model"); ok {
|
||||||
cfg.Agents.Defaults.Model = v
|
cfg.Agents.Defaults.Model = v
|
||||||
}
|
}
|
||||||
if v, ok := getFloat(defaults, "max_tokens"); ok {
|
if v, ok := getFloat(defaults, "max_tokens"); ok {
|
||||||
|
|
|
||||||
|
|
@ -404,64 +404,6 @@ type antigravityJSONResponse struct {
|
||||||
} `json:"usageMetadata"`
|
} `json:"usageMetadata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *AntigravityProvider) parseJSONResponse(body []byte) (*LLMResponse, error) {
|
|
||||||
var resp antigravityJSONResponse
|
|
||||||
if err := json.Unmarshal(body, &resp); err != nil {
|
|
||||||
return nil, fmt.Errorf("parsing antigravity response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(resp.Candidates) == 0 {
|
|
||||||
return nil, fmt.Errorf("antigravity: no candidates in response")
|
|
||||||
}
|
|
||||||
|
|
||||||
candidate := resp.Candidates[0]
|
|
||||||
var contentParts []string
|
|
||||||
var toolCalls []ToolCall
|
|
||||||
|
|
||||||
for _, part := range candidate.Content.Parts {
|
|
||||||
if part.Text != "" {
|
|
||||||
contentParts = append(contentParts, part.Text)
|
|
||||||
}
|
|
||||||
if part.FunctionCall != nil {
|
|
||||||
argumentsJSON, _ := json.Marshal(part.FunctionCall.Args)
|
|
||||||
toolCalls = append(toolCalls, ToolCall{
|
|
||||||
ID: fmt.Sprintf("call_%s_%d", part.FunctionCall.Name, time.Now().UnixNano()),
|
|
||||||
Name: part.FunctionCall.Name,
|
|
||||||
Arguments: part.FunctionCall.Args,
|
|
||||||
Function: &FunctionCall{
|
|
||||||
Name: part.FunctionCall.Name,
|
|
||||||
Arguments: string(argumentsJSON),
|
|
||||||
ThoughtSignature: extractPartThoughtSignature(part.ThoughtSignature, part.ThoughtSignatureSnake),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
finishReason := "stop"
|
|
||||||
if len(toolCalls) > 0 {
|
|
||||||
finishReason = "tool_calls"
|
|
||||||
}
|
|
||||||
if candidate.FinishReason == "MAX_TOKENS" {
|
|
||||||
finishReason = "length"
|
|
||||||
}
|
|
||||||
|
|
||||||
var usage *UsageInfo
|
|
||||||
if resp.UsageMetadata.TotalTokenCount > 0 {
|
|
||||||
usage = &UsageInfo{
|
|
||||||
PromptTokens: resp.UsageMetadata.PromptTokenCount,
|
|
||||||
CompletionTokens: resp.UsageMetadata.CandidatesTokenCount,
|
|
||||||
TotalTokens: resp.UsageMetadata.TotalTokenCount,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &LLMResponse{
|
|
||||||
Content: strings.Join(contentParts, ""),
|
|
||||||
ToolCalls: toolCalls,
|
|
||||||
FinishReason: finishReason,
|
|
||||||
Usage: usage,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AntigravityProvider) parseSSEResponse(body string) (*LLMResponse, error) {
|
func (p *AntigravityProvider) parseSSEResponse(body string) (*LLMResponse, error) {
|
||||||
var contentParts []string
|
var contentParts []string
|
||||||
var toolCalls []ToolCall
|
var toolCalls []ToolCall
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ type providerSelection struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveProviderSelection(cfg *config.Config) (providerSelection, error) {
|
func resolveProviderSelection(cfg *config.Config) (providerSelection, error) {
|
||||||
model := cfg.Agents.Defaults.Model
|
model := cfg.Agents.Defaults.GetModelName()
|
||||||
providerName := strings.ToLower(cfg.Agents.Defaults.Provider)
|
providerName := strings.ToLower(cfg.Agents.Defaults.Provider)
|
||||||
lowerModel := strings.ToLower(model)
|
lowerModel := strings.ToLower(model)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,12 +17,6 @@ func successRun(content string) func(ctx context.Context, provider, model string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func failRun(err error) func(ctx context.Context, provider, model string) (*LLMResponse, error) {
|
|
||||||
return func(ctx context.Context, provider, model string) (*LLMResponse, error) {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFallback_SingleCandidate_Success(t *testing.T) {
|
func TestFallback_SingleCandidate_Success(t *testing.T) {
|
||||||
ct := NewCooldownTracker()
|
ct := NewCooldownTracker()
|
||||||
fc := NewFallbackChain(ct)
|
fc := NewFallbackChain(ct)
|
||||||
|
|
|
||||||
|
|
@ -4,60 +4,84 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
copilot "github.com/github/copilot-sdk/go"
|
copilot "github.com/github/copilot-sdk/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GitHubCopilotProvider struct {
|
type GitHubCopilotProvider struct {
|
||||||
uri string
|
uri string
|
||||||
connectMode string // `stdio` or `grpc``
|
connectMode string // "stdio" or "grpc"
|
||||||
|
|
||||||
|
client *copilot.Client
|
||||||
session *copilot.Session
|
session *copilot.Session
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGitHubCopilotProvider(uri string, connectMode string, model string) (*GitHubCopilotProvider, error) {
|
func NewGitHubCopilotProvider(uri string, connectMode string, model string) (*GitHubCopilotProvider, error) {
|
||||||
var session *copilot.Session
|
|
||||||
if connectMode == "" {
|
if connectMode == "" {
|
||||||
connectMode = "grpc"
|
connectMode = "grpc"
|
||||||
}
|
}
|
||||||
switch connectMode {
|
|
||||||
|
|
||||||
|
switch connectMode {
|
||||||
case "stdio":
|
case "stdio":
|
||||||
// todo
|
// TODO:
|
||||||
|
return nil, fmt.Errorf("stdio mode not implemented")
|
||||||
case "grpc":
|
case "grpc":
|
||||||
client := copilot.NewClient(&copilot.ClientOptions{
|
client := copilot.NewClient(&copilot.ClientOptions{
|
||||||
CLIUrl: uri,
|
CLIUrl: uri,
|
||||||
})
|
})
|
||||||
if err := client.Start(context.Background()); err != nil {
|
if err := client.Start(context.Background()); err != nil {
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
"Can't connect to Github Copilot, https://github.com/github/copilot-sdk/blob/main/docs/getting-started.md#connecting-to-an-external-cli-server for details",
|
"can't connect to Github Copilot: %w; `https://github.com/github/copilot-sdk/blob/main/docs/getting-started.md#connecting-to-an-external-cli-server` for details",
|
||||||
|
err,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
defer client.Stop()
|
|
||||||
session, _ = client.CreateSession(context.Background(), &copilot.SessionConfig{
|
session, err := client.CreateSession(context.Background(), &copilot.SessionConfig{
|
||||||
Model: model,
|
Model: model,
|
||||||
Hooks: &copilot.SessionHooks{},
|
Hooks: &copilot.SessionHooks{},
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
|
client.Stop()
|
||||||
|
return nil, fmt.Errorf("create session failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &GitHubCopilotProvider{
|
return &GitHubCopilotProvider{
|
||||||
uri: uri,
|
uri: uri,
|
||||||
connectMode: connectMode,
|
connectMode: connectMode,
|
||||||
|
client: client,
|
||||||
session: session,
|
session: session,
|
||||||
}, nil
|
}, nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unknown connect mode: %s", connectMode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *GitHubCopilotProvider) Close() {
|
||||||
|
p.mu.Lock()
|
||||||
|
defer p.mu.Unlock()
|
||||||
|
if p.client != nil {
|
||||||
|
p.client.Stop()
|
||||||
|
p.client = nil
|
||||||
|
p.session = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chat sends a chat request to GitHub Copilot
|
|
||||||
func (p *GitHubCopilotProvider) Chat(
|
func (p *GitHubCopilotProvider) Chat(
|
||||||
ctx context.Context, messages []Message, tools []ToolDefinition, model string, options map[string]any,
|
ctx context.Context,
|
||||||
|
messages []Message,
|
||||||
|
tools []ToolDefinition,
|
||||||
|
model string,
|
||||||
|
options map[string]any,
|
||||||
) (*LLMResponse, error) {
|
) (*LLMResponse, error) {
|
||||||
type tempMessage struct {
|
type tempMessage struct {
|
||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
}
|
}
|
||||||
out := make([]tempMessage, 0, len(messages))
|
out := make([]tempMessage, 0, len(messages))
|
||||||
|
|
||||||
for _, msg := range messages {
|
for _, msg := range messages {
|
||||||
out = append(out, tempMessage{
|
out = append(out, tempMessage{
|
||||||
Role: msg.Role,
|
Role: msg.Role,
|
||||||
|
|
@ -65,12 +89,30 @@ func (p *GitHubCopilotProvider) Chat(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fullcontent, _ := json.Marshal(out)
|
fullcontent, err := json.Marshal(out)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("marshal messages: %w", err)
|
||||||
|
}
|
||||||
|
p.mu.Lock()
|
||||||
|
session := p.session
|
||||||
|
p.mu.Unlock()
|
||||||
|
|
||||||
content, _ := p.session.Send(ctx, copilot.MessageOptions{
|
if session == nil {
|
||||||
|
return nil, fmt.Errorf("provider closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := session.SendAndWait(ctx, copilot.MessageOptions{
|
||||||
Prompt: string(fullcontent),
|
Prompt: string(fullcontent),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if resp == nil {
|
||||||
|
return nil, fmt.Errorf("empty response from copilot")
|
||||||
|
}
|
||||||
|
if resp.Data.Content == nil {
|
||||||
|
return nil, fmt.Errorf("no content in copilot response")
|
||||||
|
}
|
||||||
|
content := *resp.Data.Content
|
||||||
|
|
||||||
return &LLMResponse{
|
return &LLMResponse{
|
||||||
FinishReason: "stop",
|
FinishReason: "stop",
|
||||||
Content: content,
|
Content: content,
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ import (
|
||||||
// The old providers config is automatically converted to model_list during config loading.
|
// The old providers config is automatically converted to model_list during config loading.
|
||||||
// Returns the provider, the model ID to use, and any error.
|
// Returns the provider, the model ID to use, and any error.
|
||||||
func CreateProvider(cfg *config.Config) (LLMProvider, string, error) {
|
func CreateProvider(cfg *config.Config) (LLMProvider, string, error) {
|
||||||
model := cfg.Agents.Defaults.Model
|
model := cfg.Agents.Defaults.GetModelName()
|
||||||
|
|
||||||
// Ensure model_list is populated (should be done by LoadConfig, but handle edge cases)
|
// Ensure model_list is populated (should be done by LoadConfig, but handle edge cases)
|
||||||
if len(cfg.ModelList) == 0 && cfg.HasProvidersConfig() {
|
if len(cfg.ModelList) == 0 && cfg.HasProvidersConfig() {
|
||||||
|
|
|
||||||
|
|
@ -149,6 +149,7 @@ func parseResponse(body []byte) (*LLMResponse, error) {
|
||||||
Choices []struct {
|
Choices []struct {
|
||||||
Message struct {
|
Message struct {
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
|
ReasoningContent string `json:"reasoning_content"`
|
||||||
ToolCalls []struct {
|
ToolCalls []struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
|
|
@ -222,6 +223,7 @@ func parseResponse(body []byte) (*LLMResponse, error) {
|
||||||
|
|
||||||
return &LLMResponse{
|
return &LLMResponse{
|
||||||
Content: choice.Message.Content,
|
Content: choice.Message.Content,
|
||||||
|
ReasoningContent: choice.Message.ReasoningContent,
|
||||||
ToolCalls: toolCalls,
|
ToolCalls: toolCalls,
|
||||||
FinishReason: choice.FinishReason,
|
FinishReason: choice.FinishReason,
|
||||||
Usage: apiResponse.Usage,
|
Usage: apiResponse.Usage,
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,50 @@ func TestProviderChat_ParsesToolCalls(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProviderChat_ParsesReasoningContent(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
resp := map[string]any{
|
||||||
|
"choices": []map[string]any{
|
||||||
|
{
|
||||||
|
"message": map[string]any{
|
||||||
|
"content": "The answer is 2",
|
||||||
|
"reasoning_content": "Let me think step by step... 1+1=2",
|
||||||
|
"tool_calls": []map[string]any{
|
||||||
|
{
|
||||||
|
"id": "call_1",
|
||||||
|
"type": "function",
|
||||||
|
"function": map[string]any{
|
||||||
|
"name": "calculator",
|
||||||
|
"arguments": "{\"expr\":\"1+1\"}",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"finish_reason": "tool_calls",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(resp)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
p := NewProvider("key", server.URL, "")
|
||||||
|
out, err := p.Chat(t.Context(), []Message{{Role: "user", Content: "1+1=?"}}, nil, "kimi-k2.5", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Chat() error = %v", err)
|
||||||
|
}
|
||||||
|
if out.ReasoningContent != "Let me think step by step... 1+1=2" {
|
||||||
|
t.Fatalf("ReasoningContent = %q, want %q", out.ReasoningContent, "Let me think step by step... 1+1=2")
|
||||||
|
}
|
||||||
|
if out.Content != "The answer is 2" {
|
||||||
|
t.Fatalf("Content = %q, want %q", out.Content, "The answer is 2")
|
||||||
|
}
|
||||||
|
if len(out.ToolCalls) != 1 {
|
||||||
|
t.Fatalf("len(ToolCalls) = %d, want 1", len(out.ToolCalls))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestProviderChat_HTTPError(t *testing.T) {
|
func TestProviderChat_HTTPError(t *testing.T) {
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, "bad request", http.StatusBadRequest)
|
http.Error(w, "bad request", http.StatusBadRequest)
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ type FunctionCall struct {
|
||||||
|
|
||||||
type LLMResponse struct {
|
type LLMResponse struct {
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
|
ReasoningContent string `json:"reasoning_content,omitempty"`
|
||||||
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
|
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
|
||||||
FinishReason string `json:"finish_reason"`
|
FinishReason string `json:"finish_reason"`
|
||||||
Usage *UsageInfo `json:"usage,omitempty"`
|
Usage *UsageInfo `json:"usage,omitempty"`
|
||||||
|
|
@ -40,6 +41,7 @@ type UsageInfo struct {
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
|
ReasoningContent string `json:"reasoning_content,omitempty"`
|
||||||
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
|
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
|
||||||
ToolCallID string `json:"tool_call_id,omitempty"`
|
ToolCallID string `json:"tool_call_id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,11 @@ type LLMProvider interface {
|
||||||
GetDefaultModel() string
|
GetDefaultModel() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type StatefulProvider interface {
|
||||||
|
LLMProvider
|
||||||
|
Close()
|
||||||
|
}
|
||||||
|
|
||||||
// FailoverReason classifies why an LLM request failed for fallback decisions.
|
// FailoverReason classifies why an LLM request failed for fallback decisions.
|
||||||
type FailoverReason string
|
type FailoverReason string
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,9 +55,9 @@ func (info SkillInfo) validate() error {
|
||||||
|
|
||||||
type SkillsLoader struct {
|
type SkillsLoader struct {
|
||||||
workspace string
|
workspace string
|
||||||
workspaceSkills string // workspace skills (项目级别)
|
workspaceSkills string // workspace skills (project-level)
|
||||||
globalSkills string // 全局 skills (~/.picoclaw/skills)
|
globalSkills string // global skills (~/.picoclaw/skills)
|
||||||
builtinSkills string // 内置 skills
|
builtinSkills string // builtin skills
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSkillsLoader(workspace string, globalSkills string, builtinSkills string) *SkillsLoader {
|
func NewSkillsLoader(workspace string, globalSkills string, builtinSkills string) *SkillsLoader {
|
||||||
|
|
@ -71,17 +71,28 @@ func NewSkillsLoader(workspace string, globalSkills string, builtinSkills string
|
||||||
|
|
||||||
func (sl *SkillsLoader) ListSkills() []SkillInfo {
|
func (sl *SkillsLoader) ListSkills() []SkillInfo {
|
||||||
skills := make([]SkillInfo, 0)
|
skills := make([]SkillInfo, 0)
|
||||||
|
seen := make(map[string]bool)
|
||||||
|
|
||||||
if sl.workspaceSkills != "" {
|
addSkills := func(dir, source string) {
|
||||||
if dirs, err := os.ReadDir(sl.workspaceSkills); err == nil {
|
if dir == "" {
|
||||||
for _, dir := range dirs {
|
return
|
||||||
if dir.IsDir() {
|
}
|
||||||
skillFile := filepath.Join(sl.workspaceSkills, dir.Name(), "SKILL.md")
|
dirs, err := os.ReadDir(dir)
|
||||||
if _, err := os.Stat(skillFile); err == nil {
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, d := range dirs {
|
||||||
|
if !d.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
skillFile := filepath.Join(dir, d.Name(), "SKILL.md")
|
||||||
|
if _, err := os.Stat(skillFile); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
info := SkillInfo{
|
info := SkillInfo{
|
||||||
Name: dir.Name(),
|
Name: d.Name(),
|
||||||
Path: skillFile,
|
Path: skillFile,
|
||||||
Source: "workspace",
|
Source: source,
|
||||||
}
|
}
|
||||||
metadata := sl.getSkillMetadata(skillFile)
|
metadata := sl.getSkillMetadata(skillFile)
|
||||||
if metadata != nil {
|
if metadata != nil {
|
||||||
|
|
@ -89,100 +100,27 @@ func (sl *SkillsLoader) ListSkills() []SkillInfo {
|
||||||
info.Name = metadata.Name
|
info.Name = metadata.Name
|
||||||
}
|
}
|
||||||
if err := info.validate(); err != nil {
|
if err := info.validate(); err != nil {
|
||||||
slog.Warn("invalid skill from workspace", "name", info.Name, "error", err)
|
slog.Warn("invalid skill from "+source, "name", info.Name, "error", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if seen[info.Name] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[info.Name] = true
|
||||||
skills = append(skills, info)
|
skills = append(skills, info)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 全局 skills (~/.picoclaw/skills) - 被 workspace skills 覆盖
|
// Priority: workspace > global > builtin
|
||||||
if sl.globalSkills != "" {
|
addSkills(sl.workspaceSkills, "workspace")
|
||||||
if dirs, err := os.ReadDir(sl.globalSkills); err == nil {
|
addSkills(sl.globalSkills, "global")
|
||||||
for _, dir := range dirs {
|
addSkills(sl.builtinSkills, "builtin")
|
||||||
if dir.IsDir() {
|
|
||||||
skillFile := filepath.Join(sl.globalSkills, dir.Name(), "SKILL.md")
|
|
||||||
if _, err := os.Stat(skillFile); err == nil {
|
|
||||||
// 检查是否已被 workspace skills 覆盖
|
|
||||||
exists := false
|
|
||||||
for _, s := range skills {
|
|
||||||
if s.Name == dir.Name() && s.Source == "workspace" {
|
|
||||||
exists = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if exists {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
info := SkillInfo{
|
|
||||||
Name: dir.Name(),
|
|
||||||
Path: skillFile,
|
|
||||||
Source: "global",
|
|
||||||
}
|
|
||||||
metadata := sl.getSkillMetadata(skillFile)
|
|
||||||
if metadata != nil {
|
|
||||||
info.Description = metadata.Description
|
|
||||||
info.Name = metadata.Name
|
|
||||||
}
|
|
||||||
if err := info.validate(); err != nil {
|
|
||||||
slog.Warn("invalid skill from global", "name", info.Name, "error", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
skills = append(skills, info)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if sl.builtinSkills != "" {
|
|
||||||
if dirs, err := os.ReadDir(sl.builtinSkills); err == nil {
|
|
||||||
for _, dir := range dirs {
|
|
||||||
if dir.IsDir() {
|
|
||||||
skillFile := filepath.Join(sl.builtinSkills, dir.Name(), "SKILL.md")
|
|
||||||
if _, err := os.Stat(skillFile); err == nil {
|
|
||||||
// 检查是否已被 workspace 或 global skills 覆盖
|
|
||||||
exists := false
|
|
||||||
for _, s := range skills {
|
|
||||||
if s.Name == dir.Name() && (s.Source == "workspace" || s.Source == "global") {
|
|
||||||
exists = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if exists {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
info := SkillInfo{
|
|
||||||
Name: dir.Name(),
|
|
||||||
Path: skillFile,
|
|
||||||
Source: "builtin",
|
|
||||||
}
|
|
||||||
metadata := sl.getSkillMetadata(skillFile)
|
|
||||||
if metadata != nil {
|
|
||||||
info.Description = metadata.Description
|
|
||||||
info.Name = metadata.Name
|
|
||||||
}
|
|
||||||
if err := info.validate(); err != nil {
|
|
||||||
slog.Warn("invalid skill from builtin", "name", info.Name, "error", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
skills = append(skills, info)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return skills
|
return skills
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sl *SkillsLoader) LoadSkill(name string) (string, bool) {
|
func (sl *SkillsLoader) LoadSkill(name string) (string, bool) {
|
||||||
// 1. 优先从 workspace skills 加载(项目级别)
|
// 1. load from workspace skills first (project-level)
|
||||||
if sl.workspaceSkills != "" {
|
if sl.workspaceSkills != "" {
|
||||||
skillFile := filepath.Join(sl.workspaceSkills, name, "SKILL.md")
|
skillFile := filepath.Join(sl.workspaceSkills, name, "SKILL.md")
|
||||||
if content, err := os.ReadFile(skillFile); err == nil {
|
if content, err := os.ReadFile(skillFile); err == nil {
|
||||||
|
|
@ -190,7 +128,7 @@ func (sl *SkillsLoader) LoadSkill(name string) (string, bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 其次从全局 skills 加载 (~/.picoclaw/skills)
|
// 2. then load from global skills (~/.picoclaw/skills)
|
||||||
if sl.globalSkills != "" {
|
if sl.globalSkills != "" {
|
||||||
skillFile := filepath.Join(sl.globalSkills, name, "SKILL.md")
|
skillFile := filepath.Join(sl.globalSkills, name, "SKILL.md")
|
||||||
if content, err := os.ReadFile(skillFile); err == nil {
|
if content, err := os.ReadFile(skillFile); err == nil {
|
||||||
|
|
@ -198,7 +136,7 @@ func (sl *SkillsLoader) LoadSkill(name string) (string, bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 最后从内置 skills 加载
|
// 3. finally load from builtin skills
|
||||||
if sl.builtinSkills != "" {
|
if sl.builtinSkills != "" {
|
||||||
skillFile := filepath.Join(sl.builtinSkills, name, "SKILL.md")
|
skillFile := filepath.Join(sl.builtinSkills, name, "SKILL.md")
|
||||||
if content, err := os.ReadFile(skillFile); err == nil {
|
if content, err := os.ReadFile(skillFile); err == nil {
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
package skills
|
package skills
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSkillsInfoValidate(t *testing.T) {
|
func TestSkillsInfoValidate(t *testing.T) {
|
||||||
|
|
@ -135,6 +138,134 @@ func TestExtractFrontmatter(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createSkillDir creates a skill directory with a SKILL.md file containing the given frontmatter.
|
||||||
|
func createSkillDir(t *testing.T, base, dirName, name, description string) {
|
||||||
|
t.Helper()
|
||||||
|
dir := filepath.Join(base, dirName)
|
||||||
|
require.NoError(t, os.MkdirAll(dir, 0o755))
|
||||||
|
content := "---\nname: " + name + "\ndescription: " + description + "\n---\n\n# " + name
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte(content), 0o644))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListSkillsWorkspaceOverridesGlobal(t *testing.T) {
|
||||||
|
tmp := t.TempDir()
|
||||||
|
ws := filepath.Join(tmp, "workspace")
|
||||||
|
global := filepath.Join(tmp, "global")
|
||||||
|
|
||||||
|
createSkillDir(t, filepath.Join(ws, "skills"), "my-skill", "my-skill", "workspace version")
|
||||||
|
createSkillDir(t, global, "my-skill", "my-skill", "global version")
|
||||||
|
|
||||||
|
sl := NewSkillsLoader(ws, global, "")
|
||||||
|
skills := sl.ListSkills()
|
||||||
|
|
||||||
|
assert.Len(t, skills, 1)
|
||||||
|
assert.Equal(t, "workspace", skills[0].Source)
|
||||||
|
assert.Equal(t, "workspace version", skills[0].Description)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListSkillsGlobalOverridesBuiltin(t *testing.T) {
|
||||||
|
tmp := t.TempDir()
|
||||||
|
ws := filepath.Join(tmp, "workspace")
|
||||||
|
global := filepath.Join(tmp, "global")
|
||||||
|
builtin := filepath.Join(tmp, "builtin")
|
||||||
|
|
||||||
|
createSkillDir(t, global, "my-skill", "my-skill", "global version")
|
||||||
|
createSkillDir(t, builtin, "my-skill", "my-skill", "builtin version")
|
||||||
|
|
||||||
|
sl := NewSkillsLoader(ws, global, builtin)
|
||||||
|
skills := sl.ListSkills()
|
||||||
|
|
||||||
|
assert.Len(t, skills, 1)
|
||||||
|
assert.Equal(t, "global", skills[0].Source)
|
||||||
|
assert.Equal(t, "global version", skills[0].Description)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListSkillsMetadataNameDedup(t *testing.T) {
|
||||||
|
tmp := t.TempDir()
|
||||||
|
ws := filepath.Join(tmp, "workspace")
|
||||||
|
global := filepath.Join(tmp, "global")
|
||||||
|
|
||||||
|
// Different directory names but same metadata name
|
||||||
|
createSkillDir(t, filepath.Join(ws, "skills"), "dir-a", "shared-name", "workspace version")
|
||||||
|
createSkillDir(t, global, "dir-b", "shared-name", "global version")
|
||||||
|
|
||||||
|
sl := NewSkillsLoader(ws, global, "")
|
||||||
|
skills := sl.ListSkills()
|
||||||
|
|
||||||
|
assert.Len(t, skills, 1)
|
||||||
|
assert.Equal(t, "shared-name", skills[0].Name)
|
||||||
|
assert.Equal(t, "workspace", skills[0].Source)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListSkillsMultipleDistinctSkills(t *testing.T) {
|
||||||
|
tmp := t.TempDir()
|
||||||
|
ws := filepath.Join(tmp, "workspace")
|
||||||
|
global := filepath.Join(tmp, "global")
|
||||||
|
builtin := filepath.Join(tmp, "builtin")
|
||||||
|
|
||||||
|
createSkillDir(t, filepath.Join(ws, "skills"), "skill-a", "skill-a", "desc a")
|
||||||
|
createSkillDir(t, global, "skill-b", "skill-b", "desc b")
|
||||||
|
createSkillDir(t, builtin, "skill-c", "skill-c", "desc c")
|
||||||
|
|
||||||
|
sl := NewSkillsLoader(ws, global, builtin)
|
||||||
|
skills := sl.ListSkills()
|
||||||
|
|
||||||
|
assert.Len(t, skills, 3)
|
||||||
|
names := map[string]string{}
|
||||||
|
for _, s := range skills {
|
||||||
|
names[s.Name] = s.Source
|
||||||
|
}
|
||||||
|
assert.Equal(t, "workspace", names["skill-a"])
|
||||||
|
assert.Equal(t, "global", names["skill-b"])
|
||||||
|
assert.Equal(t, "builtin", names["skill-c"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListSkillsInvalidSkillSkipped(t *testing.T) {
|
||||||
|
tmp := t.TempDir()
|
||||||
|
ws := filepath.Join(tmp, "workspace")
|
||||||
|
global := filepath.Join(tmp, "global")
|
||||||
|
|
||||||
|
// Invalid name (underscore)
|
||||||
|
createSkillDir(t, filepath.Join(ws, "skills"), "bad_skill", "bad_skill", "desc")
|
||||||
|
// Valid skill
|
||||||
|
createSkillDir(t, global, "good-skill", "good-skill", "desc")
|
||||||
|
|
||||||
|
sl := NewSkillsLoader(ws, global, "")
|
||||||
|
skills := sl.ListSkills()
|
||||||
|
|
||||||
|
assert.Len(t, skills, 1)
|
||||||
|
assert.Equal(t, "good-skill", skills[0].Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListSkillsEmptyAndNonexistentDirs(t *testing.T) {
|
||||||
|
tmp := t.TempDir()
|
||||||
|
ws := filepath.Join(tmp, "workspace")
|
||||||
|
emptyDir := filepath.Join(tmp, "empty")
|
||||||
|
require.NoError(t, os.MkdirAll(emptyDir, 0o755))
|
||||||
|
|
||||||
|
sl := NewSkillsLoader(ws, emptyDir, filepath.Join(tmp, "nonexistent"))
|
||||||
|
skills := sl.ListSkills()
|
||||||
|
|
||||||
|
assert.Empty(t, skills)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListSkillsDirWithoutSkillMD(t *testing.T) {
|
||||||
|
tmp := t.TempDir()
|
||||||
|
ws := filepath.Join(tmp, "workspace")
|
||||||
|
global := filepath.Join(tmp, "global")
|
||||||
|
|
||||||
|
// Directory exists but has no SKILL.md
|
||||||
|
require.NoError(t, os.MkdirAll(filepath.Join(global, "no-skillmd"), 0o755))
|
||||||
|
// Valid skill alongside
|
||||||
|
createSkillDir(t, global, "real-skill", "real-skill", "desc")
|
||||||
|
|
||||||
|
sl := NewSkillsLoader(ws, global, "")
|
||||||
|
skills := sl.ListSkills()
|
||||||
|
|
||||||
|
assert.Len(t, skills, 1)
|
||||||
|
assert.Equal(t, "real-skill", skills[0].Name)
|
||||||
|
}
|
||||||
|
|
||||||
func TestStripFrontmatter(t *testing.T) {
|
func TestStripFrontmatter(t *testing.T) {
|
||||||
sl := &SkillsLoader{}
|
sl := &SkillsLoader{}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -117,13 +117,19 @@ func (t *I2CTool) detect() *ToolResult {
|
||||||
return SilentResult(fmt.Sprintf("Found %d I2C bus(es):\n%s", len(buses), string(result)))
|
return SilentResult(fmt.Sprintf("Found %d I2C bus(es):\n%s", len(buses), string(result)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper functions for I2C operations (used by platform-specific implementations)
|
||||||
|
|
||||||
// isValidBusID checks that a bus identifier is a simple number (prevents path injection)
|
// isValidBusID checks that a bus identifier is a simple number (prevents path injection)
|
||||||
|
//
|
||||||
|
//nolint:unused // Used by i2c_linux.go
|
||||||
func isValidBusID(id string) bool {
|
func isValidBusID(id string) bool {
|
||||||
matched, _ := regexp.MatchString(`^\d+$`, id)
|
matched, _ := regexp.MatchString(`^\d+$`, id)
|
||||||
return matched
|
return matched
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseI2CAddress extracts and validates an I2C address from args
|
// parseI2CAddress extracts and validates an I2C address from args
|
||||||
|
//
|
||||||
|
//nolint:unused // Used by i2c_linux.go
|
||||||
func parseI2CAddress(args map[string]any) (int, *ToolResult) {
|
func parseI2CAddress(args map[string]any) (int, *ToolResult) {
|
||||||
addrFloat, ok := args["address"].(float64)
|
addrFloat, ok := args["address"].(float64)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
@ -137,6 +143,8 @@ func parseI2CAddress(args map[string]any) (int, *ToolResult) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseI2CBus extracts and validates an I2C bus from args
|
// parseI2CBus extracts and validates an I2C bus from args
|
||||||
|
//
|
||||||
|
//nolint:unused // Used by i2c_linux.go
|
||||||
func parseI2CBus(args map[string]any) (string, *ToolResult) {
|
func parseI2CBus(args map[string]any) (string, *ToolResult) {
|
||||||
bus, ok := args["bus"].(string)
|
bus, ok := args["bus"].(string)
|
||||||
if !ok || bus == "" {
|
if !ok || bus == "" {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package tools
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SpawnTool struct {
|
type SpawnTool struct {
|
||||||
|
|
@ -66,8 +67,8 @@ func (t *SpawnTool) SetAllowlistChecker(check func(targetAgentID string) bool) {
|
||||||
|
|
||||||
func (t *SpawnTool) Execute(ctx context.Context, args map[string]any) *ToolResult {
|
func (t *SpawnTool) Execute(ctx context.Context, args map[string]any) *ToolResult {
|
||||||
task, ok := args["task"].(string)
|
task, ok := args["task"].(string)
|
||||||
if !ok {
|
if !ok || strings.TrimSpace(task) == "" {
|
||||||
return ErrorResult("task is required")
|
return ErrorResult("task is required and must be a non-empty string")
|
||||||
}
|
}
|
||||||
|
|
||||||
label, _ := args["label"].(string)
|
label, _ := args["label"].(string)
|
||||||
|
|
|
||||||
79
pkg/tools/spawn_test.go
Normal file
79
pkg/tools/spawn_test.go
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
package tools
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSpawnTool_Execute_EmptyTask(t *testing.T) {
|
||||||
|
provider := &MockLLMProvider{}
|
||||||
|
manager := NewSubagentManager(provider, "test-model", "/tmp/test", nil)
|
||||||
|
tool := NewSpawnTool(manager)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args map[string]any
|
||||||
|
}{
|
||||||
|
{"empty string", map[string]any{"task": ""}},
|
||||||
|
{"whitespace only", map[string]any{"task": " "}},
|
||||||
|
{"tabs and newlines", map[string]any{"task": "\t\n "}},
|
||||||
|
{"missing task key", map[string]any{"label": "test"}},
|
||||||
|
{"wrong type", map[string]any{"task": 123}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result := tool.Execute(ctx, tt.args)
|
||||||
|
if result == nil {
|
||||||
|
t.Fatal("Result should not be nil")
|
||||||
|
}
|
||||||
|
if !result.IsError {
|
||||||
|
t.Error("Expected error for invalid task parameter")
|
||||||
|
}
|
||||||
|
if !strings.Contains(result.ForLLM, "task is required") {
|
||||||
|
t.Errorf("Error message should mention 'task is required', got: %s", result.ForLLM)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSpawnTool_Execute_ValidTask(t *testing.T) {
|
||||||
|
provider := &MockLLMProvider{}
|
||||||
|
manager := NewSubagentManager(provider, "test-model", "/tmp/test", nil)
|
||||||
|
tool := NewSpawnTool(manager)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
args := map[string]any{
|
||||||
|
"task": "Write a haiku about coding",
|
||||||
|
"label": "haiku-task",
|
||||||
|
}
|
||||||
|
|
||||||
|
result := tool.Execute(ctx, args)
|
||||||
|
if result == nil {
|
||||||
|
t.Fatal("Result should not be nil")
|
||||||
|
}
|
||||||
|
if result.IsError {
|
||||||
|
t.Errorf("Expected success for valid task, got error: %s", result.ForLLM)
|
||||||
|
}
|
||||||
|
if !result.Async {
|
||||||
|
t.Error("SpawnTool should return async result")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSpawnTool_Execute_NilManager(t *testing.T) {
|
||||||
|
tool := NewSpawnTool(nil)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
args := map[string]any{"task": "test task"}
|
||||||
|
|
||||||
|
result := tool.Execute(ctx, args)
|
||||||
|
if !result.IsError {
|
||||||
|
t.Error("Expected error for nil manager")
|
||||||
|
}
|
||||||
|
if !strings.Contains(result.ForLLM, "Subagent manager not configured") {
|
||||||
|
t.Errorf("Error message should mention manager not configured, got: %s", result.ForLLM)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -119,7 +119,11 @@ func (t *SPITool) list() *ToolResult {
|
||||||
return SilentResult(fmt.Sprintf("Found %d SPI device(s):\n%s", len(devices), string(result)))
|
return SilentResult(fmt.Sprintf("Found %d SPI device(s):\n%s", len(devices), string(result)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function for SPI operations (used by platform-specific implementations)
|
||||||
|
|
||||||
// parseSPIArgs extracts and validates common SPI parameters
|
// parseSPIArgs extracts and validates common SPI parameters
|
||||||
|
//
|
||||||
|
//nolint:unused // Used by spi_linux.go
|
||||||
func parseSPIArgs(args map[string]any) (device string, speed uint32, mode uint8, bits uint8, errMsg string) {
|
func parseSPIArgs(args map[string]any) (device string, speed uint32, mode uint8, bits uint8, errMsg string) {
|
||||||
dev, ok := args["device"].(string)
|
dev, ok := args["device"].(string)
|
||||||
if !ok || dev == "" {
|
if !ok || dev == "" {
|
||||||
|
|
|
||||||
|
|
@ -17,12 +17,50 @@ const (
|
||||||
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// createHTTPClient creates an HTTP client with optional proxy support
|
||||||
|
func createHTTPClient(proxyURL string, timeout time.Duration) (*http.Client, error) {
|
||||||
|
client := &http.Client{
|
||||||
|
Timeout: timeout,
|
||||||
|
Transport: &http.Transport{
|
||||||
|
MaxIdleConns: 10,
|
||||||
|
IdleConnTimeout: 30 * time.Second,
|
||||||
|
DisableCompression: false,
|
||||||
|
TLSHandshakeTimeout: 15 * time.Second,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if proxyURL != "" {
|
||||||
|
proxy, err := url.Parse(proxyURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid proxy URL: %w", err)
|
||||||
|
}
|
||||||
|
scheme := strings.ToLower(proxy.Scheme)
|
||||||
|
switch scheme {
|
||||||
|
case "http", "https", "socks5", "socks5h":
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"unsupported proxy scheme %q (supported: http, https, socks5, socks5h)",
|
||||||
|
proxy.Scheme,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if proxy.Host == "" {
|
||||||
|
return nil, fmt.Errorf("invalid proxy URL: missing host")
|
||||||
|
}
|
||||||
|
client.Transport.(*http.Transport).Proxy = http.ProxyURL(proxy)
|
||||||
|
} else {
|
||||||
|
client.Transport.(*http.Transport).Proxy = http.ProxyFromEnvironment
|
||||||
|
}
|
||||||
|
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
type SearchProvider interface {
|
type SearchProvider interface {
|
||||||
Search(ctx context.Context, query string, count int) (string, error)
|
Search(ctx context.Context, query string, count int) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type BraveSearchProvider struct {
|
type BraveSearchProvider struct {
|
||||||
apiKey string
|
apiKey string
|
||||||
|
proxy string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BraveSearchProvider) Search(ctx context.Context, query string, count int) (string, error) {
|
func (p *BraveSearchProvider) Search(ctx context.Context, query string, count int) (string, error) {
|
||||||
|
|
@ -37,7 +75,10 @@ func (p *BraveSearchProvider) Search(ctx context.Context, query string, count in
|
||||||
req.Header.Set("Accept", "application/json")
|
req.Header.Set("Accept", "application/json")
|
||||||
req.Header.Set("X-Subscription-Token", p.apiKey)
|
req.Header.Set("X-Subscription-Token", p.apiKey)
|
||||||
|
|
||||||
client := &http.Client{Timeout: 10 * time.Second}
|
client, err := createHTTPClient(p.proxy, 10*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to create HTTP client: %w", err)
|
||||||
|
}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("request failed: %w", err)
|
return "", fmt.Errorf("request failed: %w", err)
|
||||||
|
|
@ -167,7 +208,9 @@ func (p *TavilySearchProvider) Search(ctx context.Context, query string, count i
|
||||||
return strings.Join(lines, "\n"), nil
|
return strings.Join(lines, "\n"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type DuckDuckGoSearchProvider struct{}
|
type DuckDuckGoSearchProvider struct {
|
||||||
|
proxy string
|
||||||
|
}
|
||||||
|
|
||||||
func (p *DuckDuckGoSearchProvider) Search(ctx context.Context, query string, count int) (string, error) {
|
func (p *DuckDuckGoSearchProvider) Search(ctx context.Context, query string, count int) (string, error) {
|
||||||
searchURL := fmt.Sprintf("https://html.duckduckgo.com/html/?q=%s", url.QueryEscape(query))
|
searchURL := fmt.Sprintf("https://html.duckduckgo.com/html/?q=%s", url.QueryEscape(query))
|
||||||
|
|
@ -179,7 +222,10 @@ func (p *DuckDuckGoSearchProvider) Search(ctx context.Context, query string, cou
|
||||||
|
|
||||||
req.Header.Set("User-Agent", userAgent)
|
req.Header.Set("User-Agent", userAgent)
|
||||||
|
|
||||||
client := &http.Client{Timeout: 10 * time.Second}
|
client, err := createHTTPClient(p.proxy, 10*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to create HTTP client: %w", err)
|
||||||
|
}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("request failed: %w", err)
|
return "", fmt.Errorf("request failed: %w", err)
|
||||||
|
|
@ -261,6 +307,7 @@ func stripTags(content string) string {
|
||||||
|
|
||||||
type PerplexitySearchProvider struct {
|
type PerplexitySearchProvider struct {
|
||||||
apiKey string
|
apiKey string
|
||||||
|
proxy string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PerplexitySearchProvider) Search(ctx context.Context, query string, count int) (string, error) {
|
func (p *PerplexitySearchProvider) Search(ctx context.Context, query string, count int) (string, error) {
|
||||||
|
|
@ -295,7 +342,10 @@ func (p *PerplexitySearchProvider) Search(ctx context.Context, query string, cou
|
||||||
req.Header.Set("Authorization", "Bearer "+p.apiKey)
|
req.Header.Set("Authorization", "Bearer "+p.apiKey)
|
||||||
req.Header.Set("User-Agent", userAgent)
|
req.Header.Set("User-Agent", userAgent)
|
||||||
|
|
||||||
client := &http.Client{Timeout: 30 * time.Second}
|
client, err := createHTTPClient(p.proxy, 30*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to create HTTP client: %w", err)
|
||||||
|
}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("request failed: %w", err)
|
return "", fmt.Errorf("request failed: %w", err)
|
||||||
|
|
@ -348,6 +398,7 @@ type WebSearchToolOptions struct {
|
||||||
PerplexityAPIKey string
|
PerplexityAPIKey string
|
||||||
PerplexityMaxResults int
|
PerplexityMaxResults int
|
||||||
PerplexityEnabled bool
|
PerplexityEnabled bool
|
||||||
|
Proxy string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWebSearchTool(opts WebSearchToolOptions) *WebSearchTool {
|
func NewWebSearchTool(opts WebSearchToolOptions) *WebSearchTool {
|
||||||
|
|
@ -356,12 +407,12 @@ func NewWebSearchTool(opts WebSearchToolOptions) *WebSearchTool {
|
||||||
|
|
||||||
// Priority: Perplexity > Brave > Tavily > DuckDuckGo
|
// Priority: Perplexity > Brave > Tavily > DuckDuckGo
|
||||||
if opts.PerplexityEnabled && opts.PerplexityAPIKey != "" {
|
if opts.PerplexityEnabled && opts.PerplexityAPIKey != "" {
|
||||||
provider = &PerplexitySearchProvider{apiKey: opts.PerplexityAPIKey}
|
provider = &PerplexitySearchProvider{apiKey: opts.PerplexityAPIKey, proxy: opts.Proxy}
|
||||||
if opts.PerplexityMaxResults > 0 {
|
if opts.PerplexityMaxResults > 0 {
|
||||||
maxResults = opts.PerplexityMaxResults
|
maxResults = opts.PerplexityMaxResults
|
||||||
}
|
}
|
||||||
} else if opts.BraveEnabled && opts.BraveAPIKey != "" {
|
} else if opts.BraveEnabled && opts.BraveAPIKey != "" {
|
||||||
provider = &BraveSearchProvider{apiKey: opts.BraveAPIKey}
|
provider = &BraveSearchProvider{apiKey: opts.BraveAPIKey, proxy: opts.Proxy}
|
||||||
if opts.BraveMaxResults > 0 {
|
if opts.BraveMaxResults > 0 {
|
||||||
maxResults = opts.BraveMaxResults
|
maxResults = opts.BraveMaxResults
|
||||||
}
|
}
|
||||||
|
|
@ -374,7 +425,7 @@ func NewWebSearchTool(opts WebSearchToolOptions) *WebSearchTool {
|
||||||
maxResults = opts.TavilyMaxResults
|
maxResults = opts.TavilyMaxResults
|
||||||
}
|
}
|
||||||
} else if opts.DuckDuckGoEnabled {
|
} else if opts.DuckDuckGoEnabled {
|
||||||
provider = &DuckDuckGoSearchProvider{}
|
provider = &DuckDuckGoSearchProvider{proxy: opts.Proxy}
|
||||||
if opts.DuckDuckGoMaxResults > 0 {
|
if opts.DuckDuckGoMaxResults > 0 {
|
||||||
maxResults = opts.DuckDuckGoMaxResults
|
maxResults = opts.DuckDuckGoMaxResults
|
||||||
}
|
}
|
||||||
|
|
@ -441,6 +492,7 @@ func (t *WebSearchTool) Execute(ctx context.Context, args map[string]any) *ToolR
|
||||||
|
|
||||||
type WebFetchTool struct {
|
type WebFetchTool struct {
|
||||||
maxChars int
|
maxChars int
|
||||||
|
proxy string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWebFetchTool(maxChars int) *WebFetchTool {
|
func NewWebFetchTool(maxChars int) *WebFetchTool {
|
||||||
|
|
@ -452,6 +504,16 @@ func NewWebFetchTool(maxChars int) *WebFetchTool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewWebFetchToolWithProxy(maxChars int, proxy string) *WebFetchTool {
|
||||||
|
if maxChars <= 0 {
|
||||||
|
maxChars = 50000
|
||||||
|
}
|
||||||
|
return &WebFetchTool{
|
||||||
|
maxChars: maxChars,
|
||||||
|
proxy: proxy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (t *WebFetchTool) Name() string {
|
func (t *WebFetchTool) Name() string {
|
||||||
return "web_fetch"
|
return "web_fetch"
|
||||||
}
|
}
|
||||||
|
|
@ -511,20 +573,17 @@ func (t *WebFetchTool) Execute(ctx context.Context, args map[string]any) *ToolRe
|
||||||
|
|
||||||
req.Header.Set("User-Agent", userAgent)
|
req.Header.Set("User-Agent", userAgent)
|
||||||
|
|
||||||
client := &http.Client{
|
client, err := createHTTPClient(t.proxy, 60*time.Second)
|
||||||
Timeout: 60 * time.Second,
|
if err != nil {
|
||||||
Transport: &http.Transport{
|
return ErrorResult(fmt.Sprintf("failed to create HTTP client: %v", err))
|
||||||
MaxIdleConns: 10,
|
}
|
||||||
IdleConnTimeout: 30 * time.Second,
|
|
||||||
DisableCompression: false,
|
// Configure redirect handling
|
||||||
TLSHandshakeTimeout: 15 * time.Second,
|
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
||||||
},
|
|
||||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
||||||
if len(via) >= 5 {
|
if len(via) >= 5 {
|
||||||
return fmt.Errorf("stopped after 5 redirects")
|
return fmt.Errorf("stopped after 5 redirects")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestWebTool_WebFetch_Success verifies successful URL fetching
|
// TestWebTool_WebFetch_Success verifies successful URL fetching
|
||||||
|
|
@ -334,6 +335,172 @@ func TestWebTool_WebFetch_MissingDomain(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateHTTPClient_ProxyConfigured(t *testing.T) {
|
||||||
|
client, err := createHTTPClient("http://127.0.0.1:7890", 12*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("createHTTPClient() error: %v", err)
|
||||||
|
}
|
||||||
|
if client.Timeout != 12*time.Second {
|
||||||
|
t.Fatalf("client.Timeout = %v, want %v", client.Timeout, 12*time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
tr, ok := client.Transport.(*http.Transport)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("client.Transport type = %T, want *http.Transport", client.Transport)
|
||||||
|
}
|
||||||
|
if tr.Proxy == nil {
|
||||||
|
t.Fatal("transport.Proxy is nil, want non-nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", "https://example.com", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("http.NewRequest() error: %v", err)
|
||||||
|
}
|
||||||
|
proxyURL, err := tr.Proxy(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("transport.Proxy(req) error: %v", err)
|
||||||
|
}
|
||||||
|
if proxyURL == nil || proxyURL.String() != "http://127.0.0.1:7890" {
|
||||||
|
t.Fatalf("proxy URL = %v, want %q", proxyURL, "http://127.0.0.1:7890")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateHTTPClient_InvalidProxy(t *testing.T) {
|
||||||
|
_, err := createHTTPClient("://bad-proxy", 10*time.Second)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("createHTTPClient() expected error for invalid proxy URL, got nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateHTTPClient_Socks5ProxyConfigured(t *testing.T) {
|
||||||
|
client, err := createHTTPClient("socks5://127.0.0.1:1080", 8*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("createHTTPClient() error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tr, ok := client.Transport.(*http.Transport)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("client.Transport type = %T, want *http.Transport", client.Transport)
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest("GET", "https://example.com", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("http.NewRequest() error: %v", err)
|
||||||
|
}
|
||||||
|
proxyURL, err := tr.Proxy(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("transport.Proxy(req) error: %v", err)
|
||||||
|
}
|
||||||
|
if proxyURL == nil || proxyURL.String() != "socks5://127.0.0.1:1080" {
|
||||||
|
t.Fatalf("proxy URL = %v, want %q", proxyURL, "socks5://127.0.0.1:1080")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateHTTPClient_UnsupportedProxyScheme(t *testing.T) {
|
||||||
|
_, err := createHTTPClient("ftp://127.0.0.1:21", 10*time.Second)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("createHTTPClient() expected error for unsupported scheme, got nil")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "unsupported proxy scheme") {
|
||||||
|
t.Fatalf("error = %q, want to contain %q", err.Error(), "unsupported proxy scheme")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateHTTPClient_ProxyFromEnvironmentWhenConfigEmpty(t *testing.T) {
|
||||||
|
t.Setenv("HTTP_PROXY", "http://127.0.0.1:8888")
|
||||||
|
t.Setenv("http_proxy", "http://127.0.0.1:8888")
|
||||||
|
t.Setenv("HTTPS_PROXY", "http://127.0.0.1:8888")
|
||||||
|
t.Setenv("https_proxy", "http://127.0.0.1:8888")
|
||||||
|
t.Setenv("ALL_PROXY", "")
|
||||||
|
t.Setenv("all_proxy", "")
|
||||||
|
t.Setenv("NO_PROXY", "")
|
||||||
|
t.Setenv("no_proxy", "")
|
||||||
|
|
||||||
|
client, err := createHTTPClient("", 10*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("createHTTPClient() error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tr, ok := client.Transport.(*http.Transport)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("client.Transport type = %T, want *http.Transport", client.Transport)
|
||||||
|
}
|
||||||
|
if tr.Proxy == nil {
|
||||||
|
t.Fatal("transport.Proxy is nil, want proxy function from environment")
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", "https://example.com", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("http.NewRequest() error: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := tr.Proxy(req); err != nil {
|
||||||
|
t.Fatalf("transport.Proxy(req) error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewWebFetchToolWithProxy(t *testing.T) {
|
||||||
|
tool := NewWebFetchToolWithProxy(1024, "http://127.0.0.1:7890")
|
||||||
|
if tool.maxChars != 1024 {
|
||||||
|
t.Fatalf("maxChars = %d, want %d", tool.maxChars, 1024)
|
||||||
|
}
|
||||||
|
if tool.proxy != "http://127.0.0.1:7890" {
|
||||||
|
t.Fatalf("proxy = %q, want %q", tool.proxy, "http://127.0.0.1:7890")
|
||||||
|
}
|
||||||
|
|
||||||
|
tool = NewWebFetchToolWithProxy(0, "http://127.0.0.1:7890")
|
||||||
|
if tool.maxChars != 50000 {
|
||||||
|
t.Fatalf("default maxChars = %d, want %d", tool.maxChars, 50000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewWebSearchTool_PropagatesProxy(t *testing.T) {
|
||||||
|
t.Run("perplexity", func(t *testing.T) {
|
||||||
|
tool := NewWebSearchTool(WebSearchToolOptions{
|
||||||
|
PerplexityEnabled: true,
|
||||||
|
PerplexityAPIKey: "k",
|
||||||
|
PerplexityMaxResults: 3,
|
||||||
|
Proxy: "http://127.0.0.1:7890",
|
||||||
|
})
|
||||||
|
p, ok := tool.provider.(*PerplexitySearchProvider)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("provider type = %T, want *PerplexitySearchProvider", tool.provider)
|
||||||
|
}
|
||||||
|
if p.proxy != "http://127.0.0.1:7890" {
|
||||||
|
t.Fatalf("provider proxy = %q, want %q", p.proxy, "http://127.0.0.1:7890")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("brave", func(t *testing.T) {
|
||||||
|
tool := NewWebSearchTool(WebSearchToolOptions{
|
||||||
|
BraveEnabled: true,
|
||||||
|
BraveAPIKey: "k",
|
||||||
|
BraveMaxResults: 3,
|
||||||
|
Proxy: "http://127.0.0.1:7890",
|
||||||
|
})
|
||||||
|
p, ok := tool.provider.(*BraveSearchProvider)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("provider type = %T, want *BraveSearchProvider", tool.provider)
|
||||||
|
}
|
||||||
|
if p.proxy != "http://127.0.0.1:7890" {
|
||||||
|
t.Fatalf("provider proxy = %q, want %q", p.proxy, "http://127.0.0.1:7890")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("duckduckgo", func(t *testing.T) {
|
||||||
|
tool := NewWebSearchTool(WebSearchToolOptions{
|
||||||
|
DuckDuckGoEnabled: true,
|
||||||
|
DuckDuckGoMaxResults: 3,
|
||||||
|
Proxy: "http://127.0.0.1:7890",
|
||||||
|
})
|
||||||
|
p, ok := tool.provider.(*DuckDuckGoSearchProvider)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("provider type = %T, want *DuckDuckGoSearchProvider", tool.provider)
|
||||||
|
}
|
||||||
|
if p.proxy != "http://127.0.0.1:7890" {
|
||||||
|
t.Fatalf("provider proxy = %q, want %q", p.proxy, "http://127.0.0.1:7890")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// TestWebTool_TavilySearch_Success verifies successful Tavily search
|
// TestWebTool_TavilySearch_Success verifies successful Tavily search
|
||||||
func TestWebTool_TavilySearch_Success(t *testing.T) {
|
func TestWebTool_TavilySearch_Success(t *testing.T) {
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue