Merge pull request #2524 from SiYue-ZO/feature/sogou-web-search-default
Add configurable Sogou-backed web search
This commit is contained in:
commit
ead2dc9699
18 changed files with 1656 additions and 55 deletions
|
|
@ -269,10 +269,15 @@
|
|||
"base_url": "",
|
||||
"max_results": 0
|
||||
},
|
||||
"duckduckgo": {
|
||||
"provider": "auto",
|
||||
"sogou": {
|
||||
"enabled": true,
|
||||
"max_results": 5
|
||||
},
|
||||
"duckduckgo": {
|
||||
"enabled": false,
|
||||
"max_results": 5
|
||||
},
|
||||
"perplexity": {
|
||||
"enabled": false,
|
||||
"api_key": "pplx-xxx",
|
||||
|
|
|
|||
|
|
@ -194,6 +194,7 @@ func registerSharedTools(
|
|||
|
||||
if cfg.Tools.IsToolEnabled("web") {
|
||||
searchTool, err := tools.NewWebSearchTool(tools.WebSearchToolOptions{
|
||||
Provider: cfg.Tools.Web.Provider,
|
||||
BraveAPIKeys: cfg.Tools.Web.Brave.APIKeys.Values(),
|
||||
BraveMaxResults: cfg.Tools.Web.Brave.MaxResults,
|
||||
BraveEnabled: cfg.Tools.Web.Brave.Enabled,
|
||||
|
|
@ -201,6 +202,8 @@ func registerSharedTools(
|
|||
TavilyBaseURL: cfg.Tools.Web.Tavily.BaseURL,
|
||||
TavilyMaxResults: cfg.Tools.Web.Tavily.MaxResults,
|
||||
TavilyEnabled: cfg.Tools.Web.Tavily.Enabled,
|
||||
SogouMaxResults: cfg.Tools.Web.Sogou.MaxResults,
|
||||
SogouEnabled: cfg.Tools.Web.Sogou.Enabled,
|
||||
DuckDuckGoMaxResults: cfg.Tools.Web.DuckDuckGo.MaxResults,
|
||||
DuckDuckGoEnabled: cfg.Tools.Web.DuckDuckGo.Enabled,
|
||||
PerplexityAPIKeys: cfg.Tools.Web.Perplexity.APIKeys.Values(),
|
||||
|
|
|
|||
|
|
@ -664,6 +664,11 @@ type DuckDuckGoConfig struct {
|
|||
MaxResults int `json:"max_results" env:"PICOCLAW_TOOLS_WEB_DUCKDUCKGO_MAX_RESULTS"`
|
||||
}
|
||||
|
||||
type SogouConfig struct {
|
||||
Enabled bool `json:"enabled" env:"PICOCLAW_TOOLS_WEB_SOGOU_ENABLED"`
|
||||
MaxResults int `json:"max_results" env:"PICOCLAW_TOOLS_WEB_SOGOU_MAX_RESULTS"`
|
||||
}
|
||||
|
||||
type PerplexityConfig struct {
|
||||
Enabled bool `json:"enabled" yaml:"-" env:"PICOCLAW_TOOLS_WEB_PERPLEXITY_ENABLED"`
|
||||
APIKeys SecureStrings `json:"api_keys,omitzero" yaml:"api_keys,omitempty" env:"PICOCLAW_TOOLS_WEB_PERPLEXITY_API_KEYS"`
|
||||
|
|
@ -710,11 +715,13 @@ type WebToolsConfig struct {
|
|||
ToolConfig ` yaml:"-" envPrefix:"PICOCLAW_TOOLS_WEB_"`
|
||||
Brave BraveConfig `yaml:"brave,omitempty" json:"brave"`
|
||||
Tavily TavilyConfig `yaml:"tavily,omitempty" json:"tavily"`
|
||||
Sogou SogouConfig `yaml:"-" json:"sogou"`
|
||||
DuckDuckGo DuckDuckGoConfig `yaml:"-" json:"duckduckgo"`
|
||||
Perplexity PerplexityConfig `yaml:"perplexity,omitempty" json:"perplexity"`
|
||||
SearXNG SearXNGConfig `yaml:"-" json:"searxng"`
|
||||
GLMSearch GLMSearchConfig `yaml:"glm_search,omitempty" json:"glm_search"`
|
||||
BaiduSearch BaiduSearchConfig `yaml:"baidu_search,omitempty" json:"baidu_search"`
|
||||
Provider string `yaml:"-" json:"provider,omitempty" env:"PICOCLAW_TOOLS_WEB_PROVIDER"`
|
||||
// PreferNative controls whether to use provider-native web search when
|
||||
// the active LLM supports it (e.g. OpenAI web_search_preview). When true,
|
||||
// the client-side web_search tool is hidden to avoid duplicate search surfaces,
|
||||
|
|
|
|||
|
|
@ -760,6 +760,28 @@ func TestDefaultConfig_WebPreferNativeEnabled(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestDefaultConfig_WebProviderIsAuto(t *testing.T) {
|
||||
cfg := DefaultConfig()
|
||||
if cfg.Tools.Web.Provider != "auto" {
|
||||
t.Fatalf("DefaultConfig().Tools.Web.Provider = %q, want auto", cfg.Tools.Web.Provider)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigExample_WebProviderIsAuto(t *testing.T) {
|
||||
data, err := os.ReadFile(filepath.Join("..", "..", "config", "config.example.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile(config.example.json) error: %v", err)
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
t.Fatalf("Unmarshal(config.example.json) error: %v", err)
|
||||
}
|
||||
if cfg.Tools.Web.Provider != "auto" {
|
||||
t.Fatalf("config.example.json tools.web.provider = %q, want auto", cfg.Tools.Web.Provider)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultConfig_ToolFeedbackDisabled(t *testing.T) {
|
||||
cfg := DefaultConfig()
|
||||
if cfg.Agents.Defaults.ToolFeedback.Enabled {
|
||||
|
|
|
|||
|
|
@ -278,6 +278,7 @@ func DefaultConfig() *Config {
|
|||
ToolConfig: ToolConfig{
|
||||
Enabled: true,
|
||||
},
|
||||
Provider: "auto",
|
||||
PreferNative: true,
|
||||
Proxy: "",
|
||||
FetchLimitBytes: 10 * 1024 * 1024, // 10MB by default
|
||||
|
|
@ -290,10 +291,14 @@ func DefaultConfig() *Config {
|
|||
Enabled: false,
|
||||
MaxResults: 5,
|
||||
},
|
||||
DuckDuckGo: DuckDuckGoConfig{
|
||||
Sogou: SogouConfig{
|
||||
Enabled: true,
|
||||
MaxResults: 5,
|
||||
},
|
||||
DuckDuckGo: DuckDuckGoConfig{
|
||||
Enabled: false,
|
||||
MaxResults: 5,
|
||||
},
|
||||
Perplexity: PerplexityConfig{
|
||||
Enabled: false,
|
||||
MaxResults: 5,
|
||||
|
|
|
|||
424
pkg/tools/web.go
424
pkg/tools/web.go
|
|
@ -15,6 +15,7 @@ import (
|
|||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/config"
|
||||
"github.com/sipeed/picoclaw/pkg/logger"
|
||||
|
|
@ -23,6 +24,7 @@ import (
|
|||
|
||||
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"
|
||||
sogouUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1"
|
||||
userAgentHonest = "picoclaw/%s (+https://github.com/sipeed/picoclaw; AI assistant bot)"
|
||||
|
||||
// HTTP client timeouts for web tool providers.
|
||||
|
|
@ -46,9 +48,18 @@ var (
|
|||
reDDGLink = regexp.MustCompile(
|
||||
`<a[^>]*class="[^"]*result__a[^"]*"[^>]*href="([^"]+)"[^>]*>([\s\S]*?)</a>`,
|
||||
)
|
||||
reDDGSnippet = regexp.MustCompile(`<a class="result__snippet[^"]*".*?>([\s\S]*?)</a>`)
|
||||
reDDGSnippet = regexp.MustCompile(
|
||||
`<a class="result__snippet[^"]*".*?>([\s\S]*?)</a>`,
|
||||
)
|
||||
reSogouTitle = regexp.MustCompile(
|
||||
`<a\s+class=resultLink\s+href="([^"]+)"[^>]*id="sogou_vr_\d+_\d+"[^>]*>\s*(.*?)\s*</a>`,
|
||||
)
|
||||
reSogouSnippet = regexp.MustCompile(`<div class="clamp\d*">\s*(.*?)\s*</div>`)
|
||||
reSogouRealURL = regexp.MustCompile(`url=([^&]+)`)
|
||||
)
|
||||
|
||||
var preferredWebSearchLanguage atomic.Value
|
||||
|
||||
type APIKeyPool struct {
|
||||
keys []string
|
||||
current uint32
|
||||
|
|
@ -91,6 +102,39 @@ type SearchProvider interface {
|
|||
Search(ctx context.Context, query string, count int, rangeCode string) (string, error)
|
||||
}
|
||||
|
||||
type SearchResultItem struct {
|
||||
Title string
|
||||
URL string
|
||||
Snippet string
|
||||
}
|
||||
|
||||
func extractSogouURL(href string) string {
|
||||
match := reSogouRealURL.FindStringSubmatch(href)
|
||||
if len(match) < 2 {
|
||||
return ""
|
||||
}
|
||||
decoded, err := url.QueryUnescape(match[1])
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return decoded
|
||||
}
|
||||
|
||||
func applySogouRangeHint(query string, rangeCode string) string {
|
||||
switch rangeCode {
|
||||
case "d":
|
||||
return query + " 最近一天"
|
||||
case "w":
|
||||
return query + " 最近一周"
|
||||
case "m":
|
||||
return query + " 最近一个月"
|
||||
case "y":
|
||||
return query + " 最近一年"
|
||||
default:
|
||||
return query
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeSearchRange(raw string) (string, error) {
|
||||
rangeCode := strings.ToLower(strings.TrimSpace(raw))
|
||||
switch rangeCode {
|
||||
|
|
@ -206,6 +250,27 @@ func mapBaiduRecencyFilter(rangeCode string) string {
|
|||
}
|
||||
}
|
||||
|
||||
func normalizePreferredWebSearchLanguage(lang string) string {
|
||||
lang = strings.ToLower(strings.TrimSpace(lang))
|
||||
switch {
|
||||
case strings.HasPrefix(lang, "zh"), lang == "chinese":
|
||||
return "zh"
|
||||
case strings.HasPrefix(lang, "en"), lang == "english":
|
||||
return "en"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func SetPreferredWebSearchLanguage(lang string) {
|
||||
preferredWebSearchLanguage.Store(normalizePreferredWebSearchLanguage(lang))
|
||||
}
|
||||
|
||||
func GetPreferredWebSearchLanguage() string {
|
||||
lang, _ := preferredWebSearchLanguage.Load().(string)
|
||||
return lang
|
||||
}
|
||||
|
||||
type BraveSearchProvider struct {
|
||||
keyPool *APIKeyPool
|
||||
proxy string
|
||||
|
|
@ -425,6 +490,104 @@ func (p *TavilySearchProvider) Search(
|
|||
return "", fmt.Errorf("all api keys failed, last error: %w", lastErr)
|
||||
}
|
||||
|
||||
type SogouSearchProvider struct {
|
||||
proxy string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func (p *SogouSearchProvider) Search(
|
||||
ctx context.Context,
|
||||
query string,
|
||||
count int,
|
||||
rangeCode string,
|
||||
) (string, error) {
|
||||
const sogouWAPURL = "https://wap.sogou.com/web/searchList.jsp"
|
||||
|
||||
results := make([]SearchResultItem, 0, count)
|
||||
seenURLs := make(map[string]bool)
|
||||
maxPages := min(3, (count+1)/2+1)
|
||||
|
||||
for page := 1; page <= maxPages && len(results) < count; page++ {
|
||||
params := url.Values{}
|
||||
params.Set("keyword", applySogouRangeHint(query, rangeCode))
|
||||
params.Set("v", "5")
|
||||
params.Set("p", fmt.Sprintf("%d", page))
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, sogouWAPURL+"?"+params.Encode(), nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
req.Header.Set("User-Agent", sogouUserAgent)
|
||||
|
||||
resp, err := p.client.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
resp.Body.Close()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("Sogou returned status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
html := string(body)
|
||||
if len(html) < 200 {
|
||||
break
|
||||
}
|
||||
|
||||
matches := reSogouTitle.FindAllStringSubmatch(html, -1)
|
||||
for _, match := range matches {
|
||||
if len(match) < 3 {
|
||||
continue
|
||||
}
|
||||
|
||||
title := stripTags(match[2])
|
||||
link := extractSogouURL(match[1])
|
||||
if title == "" || link == "" || seenURLs[link] {
|
||||
continue
|
||||
}
|
||||
seenURLs[link] = true
|
||||
|
||||
start := strings.Index(html, match[0])
|
||||
snippet := ""
|
||||
if start >= 0 {
|
||||
after := html[start+len(match[0]):]
|
||||
if len(after) > 2000 {
|
||||
after = after[:2000]
|
||||
}
|
||||
if snippetMatch := reSogouSnippet.FindStringSubmatch(after); len(snippetMatch) > 1 {
|
||||
snippet = stripTags(snippetMatch[1])
|
||||
}
|
||||
}
|
||||
|
||||
results = append(results, SearchResultItem{
|
||||
Title: title,
|
||||
URL: link,
|
||||
Snippet: snippet,
|
||||
})
|
||||
if len(results) >= count {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
return fmt.Sprintf("No results for: %s", query), nil
|
||||
}
|
||||
|
||||
lines := []string{fmt.Sprintf("Results for: %s (via Sogou)", query)}
|
||||
for i, item := range results {
|
||||
lines = append(lines, fmt.Sprintf("%d. %s\n %s", i+1, item.Title, item.URL))
|
||||
if item.Snippet != "" {
|
||||
lines = append(lines, fmt.Sprintf(" %s", item.Snippet))
|
||||
}
|
||||
}
|
||||
return strings.Join(lines, "\n"), nil
|
||||
}
|
||||
|
||||
type DuckDuckGoSearchProvider struct {
|
||||
proxy string
|
||||
client *http.Client
|
||||
|
|
@ -909,11 +1072,13 @@ func (p *BaiduSearchProvider) Search(
|
|||
}
|
||||
|
||||
type WebSearchTool struct {
|
||||
provider SearchProvider
|
||||
maxResults int
|
||||
provider SearchProvider
|
||||
maxResults int
|
||||
providerResolver func(query string) (SearchProvider, int)
|
||||
}
|
||||
|
||||
type WebSearchToolOptions struct {
|
||||
Provider string
|
||||
BraveAPIKeys []string
|
||||
BraveMaxResults int
|
||||
BraveEnabled bool
|
||||
|
|
@ -921,6 +1086,8 @@ type WebSearchToolOptions struct {
|
|||
TavilyBaseURL string
|
||||
TavilyMaxResults int
|
||||
TavilyEnabled bool
|
||||
SogouMaxResults int
|
||||
SogouEnabled bool
|
||||
DuckDuckGoMaxResults int
|
||||
DuckDuckGoEnabled bool
|
||||
PerplexityAPIKeys []string
|
||||
|
|
@ -941,100 +1108,256 @@ type WebSearchToolOptions struct {
|
|||
Proxy string
|
||||
}
|
||||
|
||||
func NewWebSearchTool(opts WebSearchToolOptions) (*WebSearchTool, error) {
|
||||
var provider SearchProvider
|
||||
maxResults := 10
|
||||
// Priority: Perplexity > Brave > SearXNG > Tavily > DuckDuckGo > Baidu Search > GLM Search
|
||||
if opts.PerplexityEnabled {
|
||||
func (opts WebSearchToolOptions) providerByName(name string) (SearchProvider, int, error) {
|
||||
switch strings.ToLower(strings.TrimSpace(name)) {
|
||||
case "", "auto":
|
||||
return nil, 0, nil
|
||||
case "sogou":
|
||||
if !opts.SogouEnabled {
|
||||
return nil, 0, nil
|
||||
}
|
||||
client, err := utils.CreateHTTPClient(opts.Proxy, searchTimeout)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("failed to create HTTP client for Sogou: %w", err)
|
||||
}
|
||||
maxResults := 10
|
||||
if opts.SogouMaxResults > 0 {
|
||||
maxResults = min(opts.SogouMaxResults, 10)
|
||||
}
|
||||
return &SogouSearchProvider{
|
||||
proxy: opts.Proxy,
|
||||
client: client,
|
||||
}, maxResults, nil
|
||||
case "perplexity":
|
||||
if !opts.PerplexityEnabled {
|
||||
return nil, 0, nil
|
||||
}
|
||||
client, err := utils.CreateHTTPClient(opts.Proxy, perplexityTimeout)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create HTTP client for Perplexity: %w", err)
|
||||
}
|
||||
provider = &PerplexitySearchProvider{
|
||||
keyPool: NewAPIKeyPool(opts.PerplexityAPIKeys),
|
||||
proxy: opts.Proxy,
|
||||
client: client,
|
||||
return nil, 0, fmt.Errorf("failed to create HTTP client for Perplexity: %w", err)
|
||||
}
|
||||
maxResults := 10
|
||||
if opts.PerplexityMaxResults > 0 {
|
||||
maxResults = min(opts.PerplexityMaxResults, 10)
|
||||
}
|
||||
} else if opts.BraveEnabled {
|
||||
return &PerplexitySearchProvider{
|
||||
keyPool: NewAPIKeyPool(opts.PerplexityAPIKeys),
|
||||
proxy: opts.Proxy,
|
||||
client: client,
|
||||
}, maxResults, nil
|
||||
case "brave":
|
||||
if !opts.BraveEnabled {
|
||||
return nil, 0, nil
|
||||
}
|
||||
client, err := utils.CreateHTTPClient(opts.Proxy, searchTimeout)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create HTTP client for Brave: %w", err)
|
||||
return nil, 0, fmt.Errorf("failed to create HTTP client for Brave: %w", err)
|
||||
}
|
||||
provider = &BraveSearchProvider{keyPool: NewAPIKeyPool(opts.BraveAPIKeys), proxy: opts.Proxy, client: client}
|
||||
maxResults := 10
|
||||
if opts.BraveMaxResults > 0 {
|
||||
maxResults = min(opts.BraveMaxResults, 10)
|
||||
}
|
||||
} else if opts.SearXNGEnabled {
|
||||
provider = &SearXNGSearchProvider{baseURL: opts.SearXNGBaseURL}
|
||||
return &BraveSearchProvider{
|
||||
keyPool: NewAPIKeyPool(opts.BraveAPIKeys),
|
||||
proxy: opts.Proxy,
|
||||
client: client,
|
||||
}, maxResults, nil
|
||||
case "searxng":
|
||||
if !opts.SearXNGEnabled {
|
||||
return nil, 0, nil
|
||||
}
|
||||
maxResults := 10
|
||||
if opts.SearXNGMaxResults > 0 {
|
||||
maxResults = min(opts.SearXNGMaxResults, 10)
|
||||
}
|
||||
} else if opts.TavilyEnabled {
|
||||
return &SearXNGSearchProvider{
|
||||
baseURL: opts.SearXNGBaseURL,
|
||||
}, maxResults, nil
|
||||
case "tavily":
|
||||
if !opts.TavilyEnabled {
|
||||
return nil, 0, nil
|
||||
}
|
||||
client, err := utils.CreateHTTPClient(opts.Proxy, searchTimeout)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create HTTP client for Tavily: %w", err)
|
||||
return nil, 0, fmt.Errorf("failed to create HTTP client for Tavily: %w", err)
|
||||
}
|
||||
provider = &TavilySearchProvider{
|
||||
maxResults := 10
|
||||
if opts.TavilyMaxResults > 0 {
|
||||
maxResults = min(opts.TavilyMaxResults, 10)
|
||||
}
|
||||
return &TavilySearchProvider{
|
||||
keyPool: NewAPIKeyPool(opts.TavilyAPIKeys),
|
||||
baseURL: opts.TavilyBaseURL,
|
||||
proxy: opts.Proxy,
|
||||
client: client,
|
||||
}, maxResults, nil
|
||||
case "duckduckgo":
|
||||
if !opts.DuckDuckGoEnabled {
|
||||
return nil, 0, nil
|
||||
}
|
||||
if opts.TavilyMaxResults > 0 {
|
||||
maxResults = min(opts.TavilyMaxResults, 10)
|
||||
}
|
||||
} else if opts.DuckDuckGoEnabled {
|
||||
client, err := utils.CreateHTTPClient(opts.Proxy, searchTimeout)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create HTTP client for DuckDuckGo: %w", err)
|
||||
return nil, 0, fmt.Errorf("failed to create HTTP client for DuckDuckGo: %w", err)
|
||||
}
|
||||
provider = &DuckDuckGoSearchProvider{proxy: opts.Proxy, client: client}
|
||||
maxResults := 10
|
||||
if opts.DuckDuckGoMaxResults > 0 {
|
||||
maxResults = min(opts.DuckDuckGoMaxResults, 10)
|
||||
}
|
||||
} else if opts.BaiduSearchEnabled {
|
||||
return &DuckDuckGoSearchProvider{
|
||||
proxy: opts.Proxy,
|
||||
client: client,
|
||||
}, maxResults, nil
|
||||
case "baidu_search":
|
||||
if !opts.BaiduSearchEnabled {
|
||||
return nil, 0, nil
|
||||
}
|
||||
client, err := utils.CreateHTTPClient(opts.Proxy, perplexityTimeout)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create HTTP client for Baidu Search: %w", err)
|
||||
return nil, 0, fmt.Errorf("failed to create HTTP client for Baidu Search: %w", err)
|
||||
}
|
||||
provider = &BaiduSearchProvider{
|
||||
maxResults := 10
|
||||
if opts.BaiduSearchMaxResults > 0 {
|
||||
maxResults = min(opts.BaiduSearchMaxResults, 10)
|
||||
}
|
||||
return &BaiduSearchProvider{
|
||||
apiKey: opts.BaiduSearchAPIKey,
|
||||
baseURL: opts.BaiduSearchBaseURL,
|
||||
proxy: opts.Proxy,
|
||||
client: client,
|
||||
}, maxResults, nil
|
||||
case "glm_search":
|
||||
if !opts.GLMSearchEnabled {
|
||||
return nil, 0, nil
|
||||
}
|
||||
if opts.BaiduSearchMaxResults > 0 {
|
||||
maxResults = min(opts.BaiduSearchMaxResults, 10)
|
||||
}
|
||||
} else if opts.GLMSearchEnabled {
|
||||
client, err := utils.CreateHTTPClient(opts.Proxy, searchTimeout)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create HTTP client for GLM Search: %w", err)
|
||||
return nil, 0, fmt.Errorf("failed to create HTTP client for GLM Search: %w", err)
|
||||
}
|
||||
searchEngine := opts.GLMSearchEngine
|
||||
if searchEngine == "" {
|
||||
searchEngine = "search_std"
|
||||
}
|
||||
provider = &GLMSearchProvider{
|
||||
maxResults := 10
|
||||
if opts.GLMSearchMaxResults > 0 {
|
||||
maxResults = min(opts.GLMSearchMaxResults, 10)
|
||||
}
|
||||
return &GLMSearchProvider{
|
||||
apiKey: opts.GLMSearchAPIKey,
|
||||
baseURL: opts.GLMSearchBaseURL,
|
||||
searchEngine: searchEngine,
|
||||
proxy: opts.Proxy,
|
||||
client: client,
|
||||
}, maxResults, nil
|
||||
default:
|
||||
return nil, 0, fmt.Errorf("unknown web search provider %q", name)
|
||||
}
|
||||
}
|
||||
|
||||
func containsHan(text string) bool {
|
||||
for _, r := range text {
|
||||
if unicode.Is(unicode.Han, r) {
|
||||
return true
|
||||
}
|
||||
if opts.GLMSearchMaxResults > 0 {
|
||||
maxResults = min(opts.GLMSearchMaxResults, 10)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func containsLatinLetter(text string) bool {
|
||||
for _, r := range text {
|
||||
if unicode.IsLetter(r) && unicode.In(r, unicode.Latin) {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func prefersDuckDuckGoQuery(text string) bool {
|
||||
trimmed := strings.TrimSpace(text)
|
||||
if trimmed == "" {
|
||||
return GetPreferredWebSearchLanguage() == "en"
|
||||
}
|
||||
if containsHan(trimmed) {
|
||||
return false
|
||||
}
|
||||
if containsLatinLetter(trimmed) {
|
||||
return true
|
||||
}
|
||||
return GetPreferredWebSearchLanguage() == "en"
|
||||
}
|
||||
|
||||
func (opts WebSearchToolOptions) buildProviderResolver() (func(query string) (SearchProvider, int), error) {
|
||||
providerName := strings.ToLower(strings.TrimSpace(opts.Provider))
|
||||
if providerName != "" && providerName != "auto" {
|
||||
provider, maxResults, err := opts.providerByName(providerName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if provider == nil {
|
||||
return func(string) (SearchProvider, int) { return nil, 0 }, nil
|
||||
}
|
||||
return func(string) (SearchProvider, int) { return provider, maxResults }, nil
|
||||
}
|
||||
|
||||
for _, name := range []string{"perplexity", "brave", "searxng", "tavily"} {
|
||||
provider, maxResults, err := opts.providerByName(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if provider != nil {
|
||||
return func(string) (SearchProvider, int) { return provider, maxResults }, nil
|
||||
}
|
||||
}
|
||||
|
||||
sogouProvider, sogouMaxResults, err := opts.providerByName("sogou")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
duckProvider, duckMaxResults, err := opts.providerByName("duckduckgo")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if sogouProvider != nil && duckProvider != nil {
|
||||
return func(query string) (SearchProvider, int) {
|
||||
if prefersDuckDuckGoQuery(query) {
|
||||
return duckProvider, duckMaxResults
|
||||
}
|
||||
return sogouProvider, sogouMaxResults
|
||||
}, nil
|
||||
}
|
||||
if sogouProvider != nil {
|
||||
return func(string) (SearchProvider, int) { return sogouProvider, sogouMaxResults }, nil
|
||||
}
|
||||
if duckProvider != nil {
|
||||
return func(string) (SearchProvider, int) { return duckProvider, duckMaxResults }, nil
|
||||
}
|
||||
|
||||
for _, name := range []string{"baidu_search", "glm_search"} {
|
||||
provider, maxResults, err := opts.providerByName(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if provider != nil {
|
||||
return func(string) (SearchProvider, int) { return provider, maxResults }, nil
|
||||
}
|
||||
}
|
||||
|
||||
return func(string) (SearchProvider, int) { return nil, 0 }, nil
|
||||
}
|
||||
|
||||
func NewWebSearchTool(opts WebSearchToolOptions) (*WebSearchTool, error) {
|
||||
resolver, err := opts.buildProviderResolver()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
provider, maxResults := resolver("")
|
||||
if provider == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &WebSearchTool{
|
||||
provider: provider,
|
||||
maxResults: maxResults,
|
||||
provider: provider,
|
||||
maxResults: maxResults,
|
||||
providerResolver: resolver,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -1077,13 +1400,22 @@ func (t *WebSearchTool) Execute(ctx context.Context, args map[string]any) *ToolR
|
|||
}
|
||||
query = strings.TrimSpace(query)
|
||||
|
||||
count64, err := getInt64Arg(args, "count", int64(t.maxResults))
|
||||
provider := t.provider
|
||||
maxResults := t.maxResults
|
||||
if t.providerResolver != nil {
|
||||
provider, maxResults = t.providerResolver(query)
|
||||
}
|
||||
if provider == nil {
|
||||
return ErrorResult("search provider is not configured")
|
||||
}
|
||||
|
||||
count64, err := getInt64Arg(args, "count", int64(maxResults))
|
||||
if err != nil {
|
||||
return ErrorResult(err.Error())
|
||||
}
|
||||
count := t.maxResults
|
||||
count := maxResults
|
||||
if count64 > 0 && count64 <= 10 {
|
||||
count = int(count64)
|
||||
count = min(int(count64), maxResults)
|
||||
}
|
||||
|
||||
rangeCode, err := normalizeSearchRange("")
|
||||
|
|
@ -1101,7 +1433,7 @@ func (t *WebSearchTool) Execute(ctx context.Context, args map[string]any) *ToolR
|
|||
}
|
||||
}
|
||||
|
||||
result, err := t.provider.Search(ctx, query, count, rangeCode)
|
||||
result, err := provider.Search(ctx, query, count, rangeCode)
|
||||
if err != nil {
|
||||
return ErrorResult(fmt.Sprintf("search failed: %v", err))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -385,19 +385,24 @@ func TestWebFetchTool_PayloadTooLarge(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestWebTool_WebSearch_NoApiKey verifies that no tool is created when API key is missing
|
||||
// TestWebTool_WebSearch_NoApiKey verifies missing credentials are surfaced at execution time.
|
||||
func TestWebTool_WebSearch_NoApiKey(t *testing.T) {
|
||||
tool, err := NewWebSearchTool(WebSearchToolOptions{BraveEnabled: true, BraveAPIKeys: nil})
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
if tool == nil {
|
||||
t.Fatalf("Expected tool to be created")
|
||||
t.Fatalf("Expected tool when Brave is enabled, even without API keys")
|
||||
}
|
||||
ctx := context.Background()
|
||||
result := tool.Execute(ctx, map[string]any{"query": "test"})
|
||||
|
||||
result := tool.Execute(context.Background(), map[string]any{
|
||||
"query": "test query",
|
||||
})
|
||||
if !result.IsError {
|
||||
t.Errorf("Expected error when API key is missing")
|
||||
t.Fatalf("Expected missing Brave API key to return error")
|
||||
}
|
||||
if !strings.Contains(result.ForLLM, "no API key provided") {
|
||||
t.Fatalf("Unexpected error message: %s", result.ForLLM)
|
||||
}
|
||||
|
||||
// Also nil when nothing is enabled
|
||||
|
|
@ -1672,3 +1677,197 @@ func TestWebTool_GLMSearch_Priority(t *testing.T) {
|
|||
t.Errorf("Expected GLMSearchProvider when only GLM enabled, got %T", tool2.provider)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebTool_SogouSearch_Success(t *testing.T) {
|
||||
provider := &SogouSearchProvider{
|
||||
client: &http.Client{
|
||||
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||
rec := httptest.NewRecorder()
|
||||
fmt.Fprint(rec, `<html><body>
|
||||
<a class=resultLink href="/link?url=https%3A%2F%2Fexample.com%2Fa" id="sogou_vr_0_0">Result A</a>
|
||||
<div class="clamp3">Snippet A</div>
|
||||
<a class=resultLink href="/link?url=https%3A%2F%2Fexample.com%2Fb" id="sogou_vr_0_1">Result B</a>
|
||||
<div class="clamp3">Snippet B</div>
|
||||
</body></html>`)
|
||||
return rec.Result(), nil
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
out, err := provider.Search(context.Background(), "test query", 2, "")
|
||||
if err != nil {
|
||||
t.Fatalf("Search() error: %v", err)
|
||||
}
|
||||
if !strings.Contains(out, "via Sogou") || !strings.Contains(out, "https://example.com/a") {
|
||||
t.Fatalf("unexpected output: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplySogouRangeHint(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
query string
|
||||
rangeCode string
|
||||
want string
|
||||
}{
|
||||
{name: "empty range", query: "golang", rangeCode: "", want: "golang"},
|
||||
{name: "day", query: "golang", rangeCode: "d", want: "golang 最近一天"},
|
||||
{name: "week", query: "golang", rangeCode: "w", want: "golang 最近一周"},
|
||||
{name: "month", query: "golang", rangeCode: "m", want: "golang 最近一个月"},
|
||||
{name: "year", query: "golang", rangeCode: "y", want: "golang 最近一年"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := applySogouRangeHint(tt.query, tt.rangeCode); got != tt.want {
|
||||
t.Fatalf("applySogouRangeHint(%q, %q) = %q, want %q", tt.query, tt.rangeCode, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrefersDuckDuckGoQuery(t *testing.T) {
|
||||
SetPreferredWebSearchLanguage("")
|
||||
t.Cleanup(func() {
|
||||
SetPreferredWebSearchLanguage("")
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
query string
|
||||
want bool
|
||||
}{
|
||||
{name: "english words", query: "golang web search", want: true},
|
||||
{name: "english with numbers", query: "OpenAI o3 price 2026", want: true},
|
||||
{name: "chinese", query: "今天上海天气", want: false},
|
||||
{name: "mixed with han", query: "golang 中文 教程", want: false},
|
||||
{name: "numbers only", query: "2026 04 15", want: false},
|
||||
{name: "blank", query: " ", want: false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := prefersDuckDuckGoQuery(tt.query); got != tt.want {
|
||||
t.Fatalf("prefersDuckDuckGoQuery(%q) = %v, want %v", tt.query, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrefersDuckDuckGoQuery_FallsBackToPreferredLanguage(t *testing.T) {
|
||||
SetPreferredWebSearchLanguage("en")
|
||||
t.Cleanup(func() {
|
||||
SetPreferredWebSearchLanguage("")
|
||||
})
|
||||
|
||||
if !prefersDuckDuckGoQuery("2026 04 15") {
|
||||
t.Fatal("numeric query should prefer DuckDuckGo when preferred language is English")
|
||||
}
|
||||
|
||||
SetPreferredWebSearchLanguage("zh")
|
||||
if prefersDuckDuckGoQuery("2026 04 15") {
|
||||
t.Fatal("numeric query should prefer Sogou when preferred language is Chinese")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebTool_SogouPriorityAndExplicitProvider(t *testing.T) {
|
||||
tool, err := NewWebSearchTool(WebSearchToolOptions{
|
||||
SogouEnabled: true,
|
||||
SogouMaxResults: 5,
|
||||
DuckDuckGoEnabled: true,
|
||||
DuckDuckGoMaxResults: 5,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewWebSearchTool() error: %v", err)
|
||||
}
|
||||
if _, ok := tool.provider.(*SogouSearchProvider); !ok {
|
||||
t.Fatalf("expected SogouSearchProvider, got %T", tool.provider)
|
||||
}
|
||||
|
||||
tool, err = NewWebSearchTool(WebSearchToolOptions{
|
||||
Provider: "duckduckgo",
|
||||
SogouEnabled: true,
|
||||
SogouMaxResults: 5,
|
||||
DuckDuckGoEnabled: true,
|
||||
DuckDuckGoMaxResults: 5,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewWebSearchTool() error: %v", err)
|
||||
}
|
||||
if _, ok := tool.provider.(*DuckDuckGoSearchProvider); !ok {
|
||||
t.Fatalf("expected DuckDuckGoSearchProvider, got %T", tool.provider)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebTool_AutoProviderPrefersConfiguredProvidersBeforeSogou(t *testing.T) {
|
||||
tool, err := NewWebSearchTool(WebSearchToolOptions{
|
||||
SogouEnabled: true,
|
||||
SogouMaxResults: 5,
|
||||
BraveEnabled: true,
|
||||
BraveAPIKeys: []string{"brave-key"},
|
||||
BraveMaxResults: 5,
|
||||
DuckDuckGoEnabled: true,
|
||||
DuckDuckGoMaxResults: 5,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewWebSearchTool() error: %v", err)
|
||||
}
|
||||
if _, ok := tool.provider.(*BraveSearchProvider); !ok {
|
||||
t.Fatalf("expected BraveSearchProvider, got %T", tool.provider)
|
||||
}
|
||||
}
|
||||
|
||||
type stubSearchProvider struct {
|
||||
result string
|
||||
calls []string
|
||||
}
|
||||
|
||||
func (p *stubSearchProvider) Search(
|
||||
_ context.Context,
|
||||
query string,
|
||||
_ int,
|
||||
_ string,
|
||||
) (string, error) {
|
||||
p.calls = append(p.calls, query)
|
||||
return p.result, nil
|
||||
}
|
||||
|
||||
func TestWebTool_AutoProviderRoutesQueryLanguageBetweenSogouAndDuckDuckGo(t *testing.T) {
|
||||
sogouProvider := &stubSearchProvider{result: "via sogou"}
|
||||
duckProvider := &stubSearchProvider{result: "via duckduckgo"}
|
||||
tool := &WebSearchTool{
|
||||
provider: sogouProvider,
|
||||
maxResults: 5,
|
||||
providerResolver: func(query string) (SearchProvider, int) {
|
||||
if prefersDuckDuckGoQuery(query) {
|
||||
return duckProvider, 3
|
||||
}
|
||||
return sogouProvider, 5
|
||||
},
|
||||
}
|
||||
|
||||
enResult := tool.Execute(context.Background(), map[string]any{"query": "golang concurrency", "count": 10})
|
||||
if enResult.IsError {
|
||||
t.Fatalf("english Execute() returned error: %s", enResult.ForLLM)
|
||||
}
|
||||
if len(duckProvider.calls) != 1 || duckProvider.calls[0] != "golang concurrency" {
|
||||
t.Fatalf("english query should use DuckDuckGo provider, calls=%v", duckProvider.calls)
|
||||
}
|
||||
if len(sogouProvider.calls) != 0 {
|
||||
t.Fatalf("english query should not call Sogou provider, calls=%v", sogouProvider.calls)
|
||||
}
|
||||
|
||||
zhResult := tool.Execute(context.Background(), map[string]any{"query": "今天上海天气"})
|
||||
if zhResult.IsError {
|
||||
t.Fatalf("chinese Execute() returned error: %s", zhResult.ForLLM)
|
||||
}
|
||||
if len(sogouProvider.calls) != 1 || sogouProvider.calls[0] != "今天上海天气" {
|
||||
t.Fatalf("chinese query should use Sogou provider, calls=%v", sogouProvider.calls)
|
||||
}
|
||||
}
|
||||
|
||||
type roundTripFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (fn roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return fn(req)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -89,6 +89,7 @@ func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
|
|||
// Skills and tools support/actions
|
||||
h.registerSkillRoutes(mux)
|
||||
h.registerToolRoutes(mux)
|
||||
h.registerUIRoutes(mux)
|
||||
|
||||
// OS startup / launch-at-login
|
||||
h.registerStartupRoutes(mux)
|
||||
|
|
|
|||
|
|
@ -5,8 +5,10 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/config"
|
||||
picotools "github.com/sipeed/picoclaw/pkg/tools"
|
||||
)
|
||||
|
||||
type toolCatalogEntry struct {
|
||||
|
|
@ -33,6 +35,39 @@ type toolStateRequest struct {
|
|||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
type webSearchProviderOption struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
Configured bool `json:"configured"`
|
||||
Current bool `json:"current"`
|
||||
RequiresAuth bool `json:"requires_auth"`
|
||||
}
|
||||
|
||||
type webSearchProviderConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
MaxResults int `json:"max_results"`
|
||||
BaseURL string `json:"base_url,omitempty"`
|
||||
APIKey string `json:"api_key,omitempty"`
|
||||
APIKeys []string `json:"api_keys,omitempty"`
|
||||
APIKeySet bool `json:"api_key_set,omitempty"`
|
||||
}
|
||||
|
||||
type webSearchConfigResponse struct {
|
||||
Provider string `json:"provider"`
|
||||
CurrentService string `json:"current_service"`
|
||||
PreferNative bool `json:"prefer_native"`
|
||||
Proxy string `json:"proxy,omitempty"`
|
||||
Providers []webSearchProviderOption `json:"providers"`
|
||||
Settings map[string]webSearchProviderConfig `json:"settings"`
|
||||
}
|
||||
|
||||
type webSearchConfigRequest struct {
|
||||
Provider string `json:"provider"`
|
||||
PreferNative bool `json:"prefer_native"`
|
||||
Proxy string `json:"proxy"`
|
||||
Settings map[string]webSearchProviderConfig `json:"settings"`
|
||||
}
|
||||
|
||||
var toolCatalog = []toolCatalogEntry{
|
||||
{
|
||||
Name: "read_file",
|
||||
|
|
@ -153,6 +188,8 @@ var toolCatalog = []toolCatalogEntry{
|
|||
func (h *Handler) registerToolRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/tools", h.handleListTools)
|
||||
mux.HandleFunc("PUT /api/tools/{name}/state", h.handleUpdateToolState)
|
||||
mux.HandleFunc("GET /api/tools/web-search-config", h.handleGetWebSearchConfig)
|
||||
mux.HandleFunc("PUT /api/tools/web-search-config", h.handleUpdateWebSearchConfig)
|
||||
}
|
||||
|
||||
func (h *Handler) handleListTools(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -333,3 +370,324 @@ func applyToolState(cfg *config.Config, toolName string, enabled bool) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Handler) handleGetWebSearchConfig(w http.ResponseWriter, r *http.Request) {
|
||||
cfg, err := config.LoadConfig(h.configPath)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Failed to load config: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(buildWebSearchConfigResponse(cfg)); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) handleUpdateWebSearchConfig(w http.ResponseWriter, r *http.Request) {
|
||||
cfg, err := config.LoadConfig(h.configPath)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Failed to load config: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var req webSearchConfigRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, fmt.Sprintf("Invalid JSON: %v", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
provider := normalizeWebSearchProvider(req.Provider)
|
||||
if provider == "" {
|
||||
http.Error(w, "invalid web search provider", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
cfg.Tools.Web.Provider = provider
|
||||
cfg.Tools.Web.PreferNative = req.PreferNative
|
||||
cfg.Tools.Web.Proxy = strings.TrimSpace(req.Proxy)
|
||||
|
||||
if settings, ok := req.Settings["sogou"]; ok {
|
||||
cfg.Tools.Web.Sogou.Enabled = settings.Enabled
|
||||
cfg.Tools.Web.Sogou.MaxResults = settings.MaxResults
|
||||
}
|
||||
if settings, ok := req.Settings["duckduckgo"]; ok {
|
||||
cfg.Tools.Web.DuckDuckGo.Enabled = settings.Enabled
|
||||
cfg.Tools.Web.DuckDuckGo.MaxResults = settings.MaxResults
|
||||
}
|
||||
if settings, ok := req.Settings["brave"]; ok {
|
||||
cfg.Tools.Web.Brave.Enabled = settings.Enabled
|
||||
cfg.Tools.Web.Brave.MaxResults = settings.MaxResults
|
||||
if keys, ok := normalizeWebSearchAPIKeys(settings.APIKeys, settings.APIKey); ok {
|
||||
cfg.Tools.Web.Brave.SetAPIKeys(keys)
|
||||
}
|
||||
}
|
||||
if settings, ok := req.Settings["tavily"]; ok {
|
||||
cfg.Tools.Web.Tavily.Enabled = settings.Enabled
|
||||
cfg.Tools.Web.Tavily.MaxResults = settings.MaxResults
|
||||
cfg.Tools.Web.Tavily.BaseURL = strings.TrimSpace(settings.BaseURL)
|
||||
if keys, ok := normalizeWebSearchAPIKeys(settings.APIKeys, settings.APIKey); ok {
|
||||
cfg.Tools.Web.Tavily.SetAPIKeys(keys)
|
||||
}
|
||||
}
|
||||
if settings, ok := req.Settings["perplexity"]; ok {
|
||||
cfg.Tools.Web.Perplexity.Enabled = settings.Enabled
|
||||
cfg.Tools.Web.Perplexity.MaxResults = settings.MaxResults
|
||||
if keys, ok := normalizeWebSearchAPIKeys(settings.APIKeys, settings.APIKey); ok {
|
||||
cfg.Tools.Web.Perplexity.APIKeys = config.SimpleSecureStrings(keys...)
|
||||
}
|
||||
}
|
||||
if settings, ok := req.Settings["searxng"]; ok {
|
||||
cfg.Tools.Web.SearXNG.Enabled = settings.Enabled
|
||||
cfg.Tools.Web.SearXNG.MaxResults = settings.MaxResults
|
||||
cfg.Tools.Web.SearXNG.BaseURL = strings.TrimSpace(settings.BaseURL)
|
||||
}
|
||||
if settings, ok := req.Settings["glm_search"]; ok {
|
||||
cfg.Tools.Web.GLMSearch.Enabled = settings.Enabled
|
||||
cfg.Tools.Web.GLMSearch.MaxResults = settings.MaxResults
|
||||
cfg.Tools.Web.GLMSearch.BaseURL = strings.TrimSpace(settings.BaseURL)
|
||||
if key := strings.TrimSpace(settings.APIKey); key != "" {
|
||||
cfg.Tools.Web.GLMSearch.APIKey = *config.NewSecureString(key)
|
||||
}
|
||||
}
|
||||
if settings, ok := req.Settings["baidu_search"]; ok {
|
||||
cfg.Tools.Web.BaiduSearch.Enabled = settings.Enabled
|
||||
cfg.Tools.Web.BaiduSearch.MaxResults = settings.MaxResults
|
||||
cfg.Tools.Web.BaiduSearch.BaseURL = strings.TrimSpace(settings.BaseURL)
|
||||
if key := strings.TrimSpace(settings.APIKey); key != "" {
|
||||
cfg.Tools.Web.BaiduSearch.APIKey = *config.NewSecureString(key)
|
||||
}
|
||||
}
|
||||
|
||||
if err := config.SaveConfig(h.configPath, cfg); err != nil {
|
||||
http.Error(w, fmt.Sprintf("Failed to save config: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(buildWebSearchConfigResponse(cfg)); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeWebSearchProvider(provider string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(provider)) {
|
||||
case "", "auto":
|
||||
return "auto"
|
||||
case "sogou", "brave", "tavily", "duckduckgo", "perplexity", "searxng", "glm_search", "baidu_search":
|
||||
return strings.ToLower(strings.TrimSpace(provider))
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeWebSearchAPIKeys(apiKeys []string, apiKey string) ([]string, bool) {
|
||||
if apiKeys != nil {
|
||||
keys := make([]string, 0, len(apiKeys))
|
||||
seen := make(map[string]struct{}, len(apiKeys))
|
||||
for _, key := range apiKeys {
|
||||
trimmed := strings.TrimSpace(key)
|
||||
if trimmed == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[trimmed]; ok {
|
||||
continue
|
||||
}
|
||||
seen[trimmed] = struct{}{}
|
||||
keys = append(keys, trimmed)
|
||||
}
|
||||
return keys, true
|
||||
}
|
||||
|
||||
if trimmed := strings.TrimSpace(apiKey); trimmed != "" {
|
||||
return []string{trimmed}, true
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func buildWebSearchConfigResponse(cfg *config.Config) webSearchConfigResponse {
|
||||
current := resolveCurrentWebSearchProvider(cfg)
|
||||
settings := map[string]webSearchProviderConfig{
|
||||
"sogou": {
|
||||
Enabled: cfg.Tools.Web.Sogou.Enabled,
|
||||
MaxResults: cfg.Tools.Web.Sogou.MaxResults,
|
||||
},
|
||||
"duckduckgo": {
|
||||
Enabled: cfg.Tools.Web.DuckDuckGo.Enabled,
|
||||
MaxResults: cfg.Tools.Web.DuckDuckGo.MaxResults,
|
||||
},
|
||||
"brave": {
|
||||
Enabled: cfg.Tools.Web.Brave.Enabled,
|
||||
MaxResults: cfg.Tools.Web.Brave.MaxResults,
|
||||
APIKeySet: len(cfg.Tools.Web.Brave.APIKeys.Values()) > 0,
|
||||
},
|
||||
"tavily": {
|
||||
Enabled: cfg.Tools.Web.Tavily.Enabled,
|
||||
MaxResults: cfg.Tools.Web.Tavily.MaxResults,
|
||||
BaseURL: cfg.Tools.Web.Tavily.BaseURL,
|
||||
APIKeySet: len(cfg.Tools.Web.Tavily.APIKeys.Values()) > 0,
|
||||
},
|
||||
"perplexity": {
|
||||
Enabled: cfg.Tools.Web.Perplexity.Enabled,
|
||||
MaxResults: cfg.Tools.Web.Perplexity.MaxResults,
|
||||
APIKeySet: len(cfg.Tools.Web.Perplexity.APIKeys.Values()) > 0,
|
||||
},
|
||||
"searxng": {
|
||||
Enabled: cfg.Tools.Web.SearXNG.Enabled,
|
||||
MaxResults: cfg.Tools.Web.SearXNG.MaxResults,
|
||||
BaseURL: cfg.Tools.Web.SearXNG.BaseURL,
|
||||
},
|
||||
"glm_search": {
|
||||
Enabled: cfg.Tools.Web.GLMSearch.Enabled,
|
||||
MaxResults: cfg.Tools.Web.GLMSearch.MaxResults,
|
||||
BaseURL: cfg.Tools.Web.GLMSearch.BaseURL,
|
||||
APIKeySet: cfg.Tools.Web.GLMSearch.APIKey.String() != "",
|
||||
},
|
||||
"baidu_search": {
|
||||
Enabled: cfg.Tools.Web.BaiduSearch.Enabled,
|
||||
MaxResults: cfg.Tools.Web.BaiduSearch.MaxResults,
|
||||
BaseURL: cfg.Tools.Web.BaiduSearch.BaseURL,
|
||||
APIKeySet: cfg.Tools.Web.BaiduSearch.APIKey.String() != "",
|
||||
},
|
||||
}
|
||||
|
||||
providers := []webSearchProviderOption{
|
||||
{
|
||||
ID: "auto",
|
||||
Label: "Auto",
|
||||
Configured: current != "",
|
||||
Current: cfg.Tools.Web.Provider == "" ||
|
||||
cfg.Tools.Web.Provider == "auto",
|
||||
},
|
||||
{
|
||||
ID: "sogou",
|
||||
Label: "Sogou",
|
||||
Configured: cfg.Tools.Web.Sogou.Enabled,
|
||||
Current: current == "sogou",
|
||||
},
|
||||
{
|
||||
ID: "duckduckgo",
|
||||
Label: "DuckDuckGo",
|
||||
Configured: cfg.Tools.Web.DuckDuckGo.Enabled,
|
||||
Current: current == "duckduckgo",
|
||||
},
|
||||
{
|
||||
ID: "brave",
|
||||
Label: "Brave Search",
|
||||
Configured: cfg.Tools.Web.Brave.Enabled &&
|
||||
len(cfg.Tools.Web.Brave.APIKeys.Values()) > 0,
|
||||
Current: current == "brave",
|
||||
RequiresAuth: true,
|
||||
},
|
||||
{
|
||||
ID: "tavily",
|
||||
Label: "Tavily",
|
||||
Configured: cfg.Tools.Web.Tavily.Enabled &&
|
||||
len(cfg.Tools.Web.Tavily.APIKeys.Values()) > 0,
|
||||
Current: current == "tavily",
|
||||
RequiresAuth: true,
|
||||
},
|
||||
{
|
||||
ID: "perplexity",
|
||||
Label: "Perplexity",
|
||||
Configured: cfg.Tools.Web.Perplexity.Enabled &&
|
||||
len(cfg.Tools.Web.Perplexity.APIKeys.Values()) > 0,
|
||||
Current: current == "perplexity",
|
||||
RequiresAuth: true,
|
||||
},
|
||||
{
|
||||
ID: "searxng",
|
||||
Label: "SearXNG",
|
||||
Configured: cfg.Tools.Web.SearXNG.Enabled &&
|
||||
strings.TrimSpace(cfg.Tools.Web.SearXNG.BaseURL) != "",
|
||||
Current: current == "searxng",
|
||||
},
|
||||
{
|
||||
ID: "glm_search",
|
||||
Label: "GLM Search",
|
||||
Configured: cfg.Tools.Web.GLMSearch.Enabled &&
|
||||
cfg.Tools.Web.GLMSearch.APIKey.String() != "",
|
||||
Current: current == "glm_search",
|
||||
RequiresAuth: true,
|
||||
},
|
||||
{
|
||||
ID: "baidu_search",
|
||||
Label: "Baidu Search",
|
||||
Configured: cfg.Tools.Web.BaiduSearch.Enabled &&
|
||||
cfg.Tools.Web.BaiduSearch.APIKey.String() != "",
|
||||
Current: current == "baidu_search",
|
||||
RequiresAuth: true,
|
||||
},
|
||||
}
|
||||
|
||||
provider := cfg.Tools.Web.Provider
|
||||
if provider == "" {
|
||||
provider = "auto"
|
||||
}
|
||||
|
||||
return webSearchConfigResponse{
|
||||
Provider: provider,
|
||||
CurrentService: current,
|
||||
PreferNative: cfg.Tools.Web.PreferNative,
|
||||
Proxy: cfg.Tools.Web.Proxy,
|
||||
Providers: providers,
|
||||
Settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
func resolveCurrentWebSearchProvider(cfg *config.Config) string {
|
||||
selected := normalizeWebSearchProvider(cfg.Tools.Web.Provider)
|
||||
if selected != "" && selected != "auto" && webSearchProviderConfigured(cfg, selected) {
|
||||
return selected
|
||||
}
|
||||
|
||||
for _, name := range []string{"perplexity", "brave", "searxng", "tavily"} {
|
||||
if webSearchProviderConfigured(cfg, name) {
|
||||
return name
|
||||
}
|
||||
}
|
||||
|
||||
if webSearchProviderConfigured(cfg, "sogou") && webSearchProviderConfigured(cfg, "duckduckgo") {
|
||||
if picotools.GetPreferredWebSearchLanguage() == "en" {
|
||||
return "duckduckgo"
|
||||
}
|
||||
return "sogou"
|
||||
}
|
||||
if webSearchProviderConfigured(cfg, "sogou") {
|
||||
return "sogou"
|
||||
}
|
||||
if webSearchProviderConfigured(cfg, "duckduckgo") {
|
||||
return "duckduckgo"
|
||||
}
|
||||
|
||||
for _, name := range []string{"baidu_search", "glm_search"} {
|
||||
if webSearchProviderConfigured(cfg, name) {
|
||||
return name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func webSearchProviderConfigured(cfg *config.Config, name string) bool {
|
||||
switch name {
|
||||
case "sogou":
|
||||
return cfg.Tools.Web.Sogou.Enabled
|
||||
case "duckduckgo":
|
||||
return cfg.Tools.Web.DuckDuckGo.Enabled
|
||||
case "brave":
|
||||
return cfg.Tools.Web.Brave.Enabled && len(cfg.Tools.Web.Brave.APIKeys.Values()) > 0
|
||||
case "tavily":
|
||||
return cfg.Tools.Web.Tavily.Enabled && len(cfg.Tools.Web.Tavily.APIKeys.Values()) > 0
|
||||
case "perplexity":
|
||||
return cfg.Tools.Web.Perplexity.Enabled && len(cfg.Tools.Web.Perplexity.APIKeys.Values()) > 0
|
||||
case "searxng":
|
||||
return cfg.Tools.Web.SearXNG.Enabled && strings.TrimSpace(cfg.Tools.Web.SearXNG.BaseURL) != ""
|
||||
case "glm_search":
|
||||
return cfg.Tools.Web.GLMSearch.Enabled && cfg.Tools.Web.GLMSearch.APIKey.String() != ""
|
||||
case "baidu_search":
|
||||
return cfg.Tools.Web.BaiduSearch.Enabled && cfg.Tools.Web.BaiduSearch.APIKey.String() != ""
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/config"
|
||||
picotools "github.com/sipeed/picoclaw/pkg/tools"
|
||||
)
|
||||
|
||||
func TestHandleListTools(t *testing.T) {
|
||||
|
|
@ -196,3 +197,219 @@ func TestHandleUpdateToolState(t *testing.T) {
|
|||
t.Fatalf("cron should be enabled: %#v", updated.Tools.Cron)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGetWebSearchConfig(t *testing.T) {
|
||||
configPath, cleanup := setupOAuthTestEnv(t)
|
||||
defer cleanup()
|
||||
|
||||
cfg, err := config.LoadConfig(configPath)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig() error = %v", err)
|
||||
}
|
||||
cfg.Tools.Web.Provider = "sogou"
|
||||
cfg.Tools.Web.Sogou.Enabled = true
|
||||
cfg.Tools.Web.Sogou.MaxResults = 6
|
||||
cfg.Tools.Web.Brave.Enabled = true
|
||||
cfg.Tools.Web.Brave.SetAPIKey("brave-test-key")
|
||||
if err := config.SaveConfig(configPath, cfg); err != nil {
|
||||
t.Fatalf("SaveConfig() error = %v", err)
|
||||
}
|
||||
|
||||
h := NewHandler(configPath)
|
||||
mux := http.NewServeMux()
|
||||
h.RegisterRoutes(mux)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/tools/web-search-config", nil)
|
||||
mux.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
|
||||
}
|
||||
|
||||
var resp webSearchConfigResponse
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("Unmarshal() error = %v", err)
|
||||
}
|
||||
if resp.Provider != "sogou" {
|
||||
t.Fatalf("provider = %q, want sogou", resp.Provider)
|
||||
}
|
||||
if resp.CurrentService != "sogou" {
|
||||
t.Fatalf("current_service = %q, want sogou", resp.CurrentService)
|
||||
}
|
||||
if !resp.Settings["brave"].APIKeySet {
|
||||
t.Fatalf("brave api_key_set should be true: %#v", resp.Settings["brave"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleUpdateWebSearchConfig(t *testing.T) {
|
||||
configPath, cleanup := setupOAuthTestEnv(t)
|
||||
defer cleanup()
|
||||
|
||||
cfg, err := config.LoadConfig(configPath)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig() error = %v", err)
|
||||
}
|
||||
cfg.Tools.Web.Brave.SetAPIKeys([]string{"brave-old-1", "brave-old-2"})
|
||||
if saveErr := config.SaveConfig(configPath, cfg); saveErr != nil {
|
||||
t.Fatalf("SaveConfig() error = %v", saveErr)
|
||||
}
|
||||
|
||||
h := NewHandler(configPath)
|
||||
mux := http.NewServeMux()
|
||||
h.RegisterRoutes(mux)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(
|
||||
http.MethodPut,
|
||||
"/api/tools/web-search-config",
|
||||
bytes.NewBufferString(`{
|
||||
"provider":"brave",
|
||||
"prefer_native":false,
|
||||
"proxy":"http://127.0.0.1:7890",
|
||||
"settings":{
|
||||
"sogou":{"enabled":true,"max_results":4},
|
||||
"brave":{"enabled":true,"max_results":7,"api_key":"brave-new-key"},
|
||||
"duckduckgo":{"enabled":false,"max_results":3}
|
||||
}
|
||||
}`),
|
||||
)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
mux.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
|
||||
}
|
||||
|
||||
updated, err := config.LoadConfig(configPath)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig() error = %v", err)
|
||||
}
|
||||
if updated.Tools.Web.Provider != "brave" {
|
||||
t.Fatalf("provider = %q, want brave", updated.Tools.Web.Provider)
|
||||
}
|
||||
if updated.Tools.Web.PreferNative {
|
||||
t.Fatal("prefer_native should be false after update")
|
||||
}
|
||||
if updated.Tools.Web.Proxy != "http://127.0.0.1:7890" {
|
||||
t.Fatalf("proxy = %q", updated.Tools.Web.Proxy)
|
||||
}
|
||||
if !updated.Tools.Web.Sogou.Enabled || updated.Tools.Web.Sogou.MaxResults != 4 {
|
||||
t.Fatalf("sogou config not updated: %#v", updated.Tools.Web.Sogou)
|
||||
}
|
||||
if !updated.Tools.Web.Brave.Enabled || updated.Tools.Web.Brave.MaxResults != 7 {
|
||||
t.Fatalf("brave config not updated: %#v", updated.Tools.Web.Brave)
|
||||
}
|
||||
if updated.Tools.Web.Brave.APIKey() != "brave-new-key" {
|
||||
t.Fatalf("brave api key not updated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleUpdateWebSearchConfig_PreservesAndReplacesMultiKeys(t *testing.T) {
|
||||
configPath, cleanup := setupOAuthTestEnv(t)
|
||||
defer cleanup()
|
||||
|
||||
cfg, err := config.LoadConfig(configPath)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig() error = %v", err)
|
||||
}
|
||||
cfg.Tools.Web.Brave.SetAPIKeys([]string{"brave-old-1", "brave-old-2"})
|
||||
if saveErr := config.SaveConfig(configPath, cfg); saveErr != nil {
|
||||
t.Fatalf("SaveConfig() error = %v", saveErr)
|
||||
}
|
||||
|
||||
h := NewHandler(configPath)
|
||||
mux := http.NewServeMux()
|
||||
h.RegisterRoutes(mux)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(
|
||||
http.MethodPut,
|
||||
"/api/tools/web-search-config",
|
||||
bytes.NewBufferString(`{
|
||||
"provider":"auto",
|
||||
"prefer_native":true,
|
||||
"proxy":"",
|
||||
"settings":{
|
||||
"brave":{"enabled":true,"max_results":7}
|
||||
}
|
||||
}`),
|
||||
)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
mux.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
|
||||
}
|
||||
|
||||
updated, err := config.LoadConfig(configPath)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig() error = %v", err)
|
||||
}
|
||||
if got := updated.Tools.Web.Brave.APIKeys.Values(); len(got) != 2 ||
|
||||
got[0] != "brave-old-1" || got[1] != "brave-old-2" {
|
||||
t.Fatalf("brave api keys should be preserved, got %#v", got)
|
||||
}
|
||||
|
||||
rec = httptest.NewRecorder()
|
||||
req = httptest.NewRequest(
|
||||
http.MethodPut,
|
||||
"/api/tools/web-search-config",
|
||||
bytes.NewBufferString(`{
|
||||
"provider":"auto",
|
||||
"prefer_native":true,
|
||||
"proxy":"",
|
||||
"settings":{
|
||||
"brave":{"enabled":true,"max_results":7,"api_keys":["brave-new-1","brave-new-2","brave-new-1"]}
|
||||
}
|
||||
}`),
|
||||
)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
mux.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
|
||||
}
|
||||
|
||||
updated, err = config.LoadConfig(configPath)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig() error = %v", err)
|
||||
}
|
||||
if got := updated.Tools.Web.Brave.APIKeys.Values(); len(got) != 2 ||
|
||||
got[0] != "brave-new-1" || got[1] != "brave-new-2" {
|
||||
t.Fatalf("brave api keys should be replaced by api_keys, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveCurrentWebSearchProvider_PrefersConfiguredProvidersBeforeSogou(t *testing.T) {
|
||||
cfg := config.DefaultConfig()
|
||||
cfg.Tools.Web.Provider = "auto"
|
||||
cfg.Tools.Web.Sogou.Enabled = true
|
||||
cfg.Tools.Web.Brave.Enabled = true
|
||||
cfg.Tools.Web.Brave.SetAPIKey("brave-test-key")
|
||||
|
||||
if got := resolveCurrentWebSearchProvider(cfg); got != "brave" {
|
||||
t.Fatalf("resolveCurrentWebSearchProvider() = %q, want brave", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveCurrentWebSearchProvider_UsesPreferredLanguageForSogouAndDuckDuckGo(t *testing.T) {
|
||||
cfg := config.DefaultConfig()
|
||||
cfg.Tools.Web.Provider = "auto"
|
||||
cfg.Tools.Web.Sogou.Enabled = true
|
||||
cfg.Tools.Web.DuckDuckGo.Enabled = true
|
||||
|
||||
picotools.SetPreferredWebSearchLanguage("en")
|
||||
t.Cleanup(func() {
|
||||
picotools.SetPreferredWebSearchLanguage("")
|
||||
})
|
||||
|
||||
if got := resolveCurrentWebSearchProvider(cfg); got != "duckduckgo" {
|
||||
t.Fatalf("resolveCurrentWebSearchProvider() = %q, want duckduckgo", got)
|
||||
}
|
||||
|
||||
picotools.SetPreferredWebSearchLanguage("zh")
|
||||
if got := resolveCurrentWebSearchProvider(cfg); got != "sogou" {
|
||||
t.Fatalf("resolveCurrentWebSearchProvider() = %q, want sogou", got)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
27
web/backend/api/ui.go
Normal file
27
web/backend/api/ui.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/tools"
|
||||
)
|
||||
|
||||
type uiLanguageRequest struct {
|
||||
Language string `json:"language"`
|
||||
}
|
||||
|
||||
func (h *Handler) registerUIRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("POST /api/ui/language", h.handleSetUILanguage)
|
||||
}
|
||||
|
||||
func (h *Handler) handleSetUILanguage(w http.ResponseWriter, r *http.Request) {
|
||||
var req uiLanguageRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
tools.SetPreferredWebSearchLanguage(req.Language)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
48
web/backend/api/ui_test.go
Normal file
48
web/backend/api/ui_test.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/tools"
|
||||
)
|
||||
|
||||
func TestHandleSetUILanguage(t *testing.T) {
|
||||
tools.SetPreferredWebSearchLanguage("")
|
||||
t.Cleanup(func() {
|
||||
tools.SetPreferredWebSearchLanguage("")
|
||||
})
|
||||
|
||||
h := NewHandler("")
|
||||
mux := http.NewServeMux()
|
||||
h.RegisterRoutes(mux)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/ui/language", strings.NewReader(`{"language":"zh"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
mux.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusNoContent, rec.Body.String())
|
||||
}
|
||||
if got := tools.GetPreferredWebSearchLanguage(); got != "zh" {
|
||||
t.Fatalf("preferred web search language = %q, want zh", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleSetUILanguage_RejectsInvalidJSON(t *testing.T) {
|
||||
h := NewHandler("")
|
||||
mux := http.NewServeMux()
|
||||
h.RegisterRoutes(mux)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/ui/language", strings.NewReader(`{`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
mux.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
|
@ -29,6 +29,7 @@ import (
|
|||
"github.com/sipeed/picoclaw/pkg/config"
|
||||
"github.com/sipeed/picoclaw/pkg/logger"
|
||||
"github.com/sipeed/picoclaw/pkg/netbind"
|
||||
"github.com/sipeed/picoclaw/pkg/tools"
|
||||
"github.com/sipeed/picoclaw/web/backend/api"
|
||||
"github.com/sipeed/picoclaw/web/backend/dashboardauth"
|
||||
"github.com/sipeed/picoclaw/web/backend/launcherconfig"
|
||||
|
|
@ -404,6 +405,7 @@ func main() {
|
|||
if *lang != "" {
|
||||
SetLanguage(*lang)
|
||||
}
|
||||
tools.SetPreferredWebSearchLanguage(string(GetLanguage()))
|
||||
|
||||
// Resolve config path
|
||||
configPath := utils.GetDefaultConfigPath()
|
||||
|
|
|
|||
|
|
@ -17,6 +17,31 @@ interface ToolActionResponse {
|
|||
status: string
|
||||
}
|
||||
|
||||
export interface WebSearchProviderOption {
|
||||
id: string
|
||||
label: string
|
||||
configured: boolean
|
||||
current: boolean
|
||||
requires_auth: boolean
|
||||
}
|
||||
|
||||
export interface WebSearchProviderConfig {
|
||||
enabled: boolean
|
||||
max_results: number
|
||||
base_url?: string
|
||||
api_key?: string
|
||||
api_key_set?: boolean
|
||||
}
|
||||
|
||||
export interface WebSearchConfigResponse {
|
||||
provider: string
|
||||
current_service: string
|
||||
prefer_native: boolean
|
||||
proxy?: string
|
||||
providers: WebSearchProviderOption[]
|
||||
settings: Record<string, WebSearchProviderConfig>
|
||||
}
|
||||
|
||||
async function request<T>(path: string, options?: RequestInit): Promise<T> {
|
||||
const res = await launcherFetch(path, options)
|
||||
if (!res.ok) {
|
||||
|
|
@ -56,3 +81,17 @@ export async function setToolEnabled(
|
|||
},
|
||||
)
|
||||
}
|
||||
|
||||
export async function getWebSearchConfig(): Promise<WebSearchConfigResponse> {
|
||||
return request<WebSearchConfigResponse>("/api/tools/web-search-config")
|
||||
}
|
||||
|
||||
export async function updateWebSearchConfig(
|
||||
payload: WebSearchConfigResponse,
|
||||
): Promise<WebSearchConfigResponse> {
|
||||
return request<WebSearchConfigResponse>("/api/tools/web-search-config", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,21 @@
|
|||
import { IconSearch } from "@tabler/icons-react"
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
|
||||
import { useMemo, useState } from "react"
|
||||
import { useEffect, useMemo, useState } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { toast } from "sonner"
|
||||
|
||||
import { type ToolSupportItem, getTools, setToolEnabled } from "@/api/tools"
|
||||
import {
|
||||
getTools,
|
||||
getWebSearchConfig,
|
||||
setToolEnabled,
|
||||
type ToolSupportItem,
|
||||
type WebSearchConfigResponse,
|
||||
updateWebSearchConfig,
|
||||
} from "@/api/tools"
|
||||
import { PageHeader } from "@/components/page-header"
|
||||
import { maskedSecretPlaceholder } from "@/components/secret-placeholder"
|
||||
import { KeyInput } from "@/components/shared-form"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
|
|
@ -33,9 +43,25 @@ export function ToolsPage() {
|
|||
queryKey: ["tools"],
|
||||
queryFn: getTools,
|
||||
})
|
||||
const {
|
||||
data: webSearchData,
|
||||
isLoading: isWebSearchLoading,
|
||||
error: webSearchError,
|
||||
} = useQuery({
|
||||
queryKey: ["tools", "web-search-config"],
|
||||
queryFn: getWebSearchConfig,
|
||||
})
|
||||
|
||||
const [searchQuery, setSearchQuery] = useState("")
|
||||
const [statusFilter, setStatusFilter] = useState("all")
|
||||
const [webSearchDraft, setWebSearchDraft] =
|
||||
useState<WebSearchConfigResponse | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (webSearchData) {
|
||||
setWebSearchDraft(webSearchData)
|
||||
}
|
||||
}, [webSearchData])
|
||||
|
||||
const toggleMutation = useMutation({
|
||||
mutationFn: async ({ name, enabled }: { name: string; enabled: boolean }) =>
|
||||
|
|
@ -58,6 +84,24 @@ export function ToolsPage() {
|
|||
},
|
||||
})
|
||||
|
||||
const webSearchMutation = useMutation({
|
||||
mutationFn: updateWebSearchConfig,
|
||||
onSuccess: (updated) => {
|
||||
setWebSearchDraft(updated)
|
||||
toast.success(t("pages.agent.tools.web_search.save_success"))
|
||||
void queryClient.invalidateQueries({ queryKey: ["tools", "web-search-config"] })
|
||||
void queryClient.invalidateQueries({ queryKey: ["tools"] })
|
||||
void refreshGatewayState({ force: true })
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(
|
||||
err instanceof Error
|
||||
? err.message
|
||||
: t("pages.agent.tools.web_search.save_error"),
|
||||
)
|
||||
},
|
||||
})
|
||||
|
||||
// Filter and group tools
|
||||
const { groupedTools, totalFilteredCount } = useMemo(() => {
|
||||
if (!data) return { groupedTools: [], totalFilteredCount: 0 }
|
||||
|
|
@ -91,12 +135,254 @@ export function ToolsPage() {
|
|||
}
|
||||
}, [data, searchQuery, statusFilter])
|
||||
|
||||
const providerLabelMap = useMemo(() => {
|
||||
const entries = webSearchDraft?.providers ?? []
|
||||
return new Map(entries.map((item) => [item.id, item.label]))
|
||||
}, [webSearchDraft])
|
||||
|
||||
const currentProviderLabel = webSearchDraft?.current_service
|
||||
? (providerLabelMap.get(webSearchDraft.current_service) ??
|
||||
webSearchDraft.current_service)
|
||||
: t("pages.agent.tools.web_search.none")
|
||||
|
||||
const updateDraft = (
|
||||
updater: (current: WebSearchConfigResponse) => WebSearchConfigResponse,
|
||||
) => {
|
||||
setWebSearchDraft((current) => (current ? updater(current) : current))
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="bg-background flex h-full flex-col">
|
||||
<PageHeader title={t("navigation.tools")} />
|
||||
|
||||
<div className="flex-1 overflow-auto px-6 py-6">
|
||||
<div className="mx-auto w-full max-w-6xl space-y-8">
|
||||
{webSearchError ? (
|
||||
<Card className="border-destructive/50 bg-destructive/10 cursor-default">
|
||||
<CardHeader>
|
||||
<CardTitle>{t("pages.agent.tools.web_search.title")}</CardTitle>
|
||||
<CardDescription>{t("pages.agent.tools.web_search.load_error")}</CardDescription>
|
||||
</CardHeader>
|
||||
</Card>
|
||||
) : isWebSearchLoading || !webSearchDraft ? (
|
||||
<Card className="border-border/60 shadow-none">
|
||||
<CardHeader>
|
||||
<Skeleton className="h-5 w-48" />
|
||||
<Skeleton className="h-4 w-80" />
|
||||
</CardHeader>
|
||||
<CardContent className="grid gap-4 lg:grid-cols-2">
|
||||
<Skeleton className="h-9 w-full" />
|
||||
<Skeleton className="h-9 w-full" />
|
||||
<Skeleton className="h-24 w-full lg:col-span-2" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
) : (
|
||||
<Card className="border-border/60 shadow-none">
|
||||
<CardHeader>
|
||||
<CardTitle>{t("pages.agent.tools.web_search.title")}</CardTitle>
|
||||
<CardDescription>
|
||||
{t("pages.agent.tools.web_search.description")}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
<div className="grid gap-4 lg:grid-cols-3">
|
||||
<div className="space-y-2">
|
||||
<div className="text-sm font-medium">
|
||||
{t("pages.agent.tools.web_search.current_service")}
|
||||
</div>
|
||||
<div className="text-muted-foreground rounded-md border px-3 py-2 text-sm">
|
||||
{currentProviderLabel}
|
||||
</div>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<div className="text-sm font-medium">
|
||||
{t("pages.agent.tools.web_search.provider")}
|
||||
</div>
|
||||
<Select
|
||||
value={webSearchDraft.provider}
|
||||
onValueChange={(value) =>
|
||||
updateDraft((current) => ({ ...current, provider: value }))
|
||||
}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{webSearchDraft.providers.map((provider) => (
|
||||
<SelectItem key={provider.id} value={provider.id}>
|
||||
{provider.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<div className="text-sm font-medium">
|
||||
{t("pages.agent.tools.web_search.proxy")}
|
||||
</div>
|
||||
<Input
|
||||
value={webSearchDraft.proxy ?? ""}
|
||||
onChange={(e) =>
|
||||
updateDraft((current) => ({
|
||||
...current,
|
||||
proxy: e.target.value,
|
||||
}))
|
||||
}
|
||||
placeholder="http://127.0.0.1:7890"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between rounded-md border px-4 py-3">
|
||||
<div>
|
||||
<div className="text-sm font-medium">
|
||||
{t("pages.agent.tools.web_search.prefer_native")}
|
||||
</div>
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{t("pages.agent.tools.web_search.prefer_native_hint")}
|
||||
</div>
|
||||
</div>
|
||||
<Switch
|
||||
checked={webSearchDraft.prefer_native}
|
||||
onCheckedChange={(checked) =>
|
||||
updateDraft((current) => ({
|
||||
...current,
|
||||
prefer_native: checked,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 lg:grid-cols-2">
|
||||
{Object.entries(webSearchDraft.settings).map(([providerId, settings]) => {
|
||||
const providerLabel = providerLabelMap.get(providerId) ?? providerId
|
||||
const apiKeyPlaceholder = maskedSecretPlaceholder(
|
||||
settings.api_key_set ? `${providerId}-configured` : "",
|
||||
t("pages.agent.tools.web_search.api_key_placeholder"),
|
||||
)
|
||||
|
||||
return (
|
||||
<Card key={providerId} className="border-border/60 shadow-none">
|
||||
<CardHeader className="pb-3">
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div>
|
||||
<CardTitle className="text-base">{providerLabel}</CardTitle>
|
||||
<CardDescription className="mt-1 text-xs">
|
||||
{t("pages.agent.tools.web_search.provider_hint")}
|
||||
</CardDescription>
|
||||
</div>
|
||||
<Switch
|
||||
checked={settings.enabled}
|
||||
onCheckedChange={(checked) =>
|
||||
updateDraft((current) => ({
|
||||
...current,
|
||||
settings: {
|
||||
...current.settings,
|
||||
[providerId]: {
|
||||
...current.settings[providerId],
|
||||
enabled: checked,
|
||||
},
|
||||
},
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-3">
|
||||
<div className="space-y-2">
|
||||
<div className="text-sm font-medium">
|
||||
{t("pages.agent.tools.web_search.max_results")}
|
||||
</div>
|
||||
<Input
|
||||
type="number"
|
||||
min={1}
|
||||
max={10}
|
||||
value={settings.max_results || 5}
|
||||
onChange={(e) =>
|
||||
updateDraft((current) => ({
|
||||
...current,
|
||||
settings: {
|
||||
...current.settings,
|
||||
[providerId]: {
|
||||
...current.settings[providerId],
|
||||
max_results: Number(e.target.value) || 0,
|
||||
},
|
||||
},
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
{(providerId === "tavily" ||
|
||||
providerId === "searxng" ||
|
||||
providerId === "glm_search" ||
|
||||
providerId === "baidu_search") && (
|
||||
<div className="space-y-2">
|
||||
<div className="text-sm font-medium">
|
||||
{t("pages.agent.tools.web_search.base_url")}
|
||||
</div>
|
||||
<Input
|
||||
value={settings.base_url ?? ""}
|
||||
onChange={(e) =>
|
||||
updateDraft((current) => ({
|
||||
...current,
|
||||
settings: {
|
||||
...current.settings,
|
||||
[providerId]: {
|
||||
...current.settings[providerId],
|
||||
base_url: e.target.value,
|
||||
},
|
||||
},
|
||||
}))
|
||||
}
|
||||
placeholder={t("pages.agent.tools.web_search.base_url_placeholder")}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
{(providerId === "brave" ||
|
||||
providerId === "tavily" ||
|
||||
providerId === "perplexity" ||
|
||||
providerId === "glm_search" ||
|
||||
providerId === "baidu_search") && (
|
||||
<div className="space-y-2">
|
||||
<div className="text-sm font-medium">
|
||||
{t("pages.agent.tools.web_search.api_key")}
|
||||
</div>
|
||||
<KeyInput
|
||||
value={settings.api_key ?? ""}
|
||||
onChange={(value) =>
|
||||
updateDraft((current) => ({
|
||||
...current,
|
||||
settings: {
|
||||
...current.settings,
|
||||
[providerId]: {
|
||||
...current.settings[providerId],
|
||||
api_key: value,
|
||||
},
|
||||
},
|
||||
}))
|
||||
}
|
||||
placeholder={apiKeyPlaceholder}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end">
|
||||
<Button
|
||||
onClick={() => webSearchMutation.mutate(webSearchDraft)}
|
||||
disabled={webSearchMutation.isPending}
|
||||
>
|
||||
{t("pages.agent.tools.web_search.save")}
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Header & Description */}
|
||||
<div className="flex flex-col gap-4 sm:flex-row sm:items-end sm:justify-end">
|
||||
{/* Filters Toolbar */}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import i18n from "i18next"
|
|||
import LanguageDetector from "i18next-browser-languagedetector"
|
||||
import { initReactI18next } from "react-i18next"
|
||||
|
||||
import { launcherFetch } from "@/api/http"
|
||||
|
||||
import en from "./locales/en.json"
|
||||
import zh from "./locales/zh.json"
|
||||
|
||||
|
|
@ -44,6 +46,14 @@ i18n.on("languageChanged", (lng) => {
|
|||
} else {
|
||||
dayjs.locale("en")
|
||||
}
|
||||
|
||||
void launcherFetch("/api/ui/language", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ language: lng }),
|
||||
}).catch(() => {
|
||||
// Keep UI language changes responsive even if backend sync fails.
|
||||
})
|
||||
})
|
||||
|
||||
export default i18n
|
||||
|
|
|
|||
|
|
@ -533,6 +533,26 @@
|
|||
"enable_success": "Tool enabled.",
|
||||
"disable_success": "Tool disabled.",
|
||||
"toggle_error": "Failed to update tool state.",
|
||||
"web_search": {
|
||||
"title": "Web Search Service",
|
||||
"description": "Choose the default web search backend and configure supported providers.",
|
||||
"load_error": "Failed to load web search configuration.",
|
||||
"save": "Save Web Search Settings",
|
||||
"save_success": "Web search configuration updated.",
|
||||
"save_error": "Failed to update web search configuration.",
|
||||
"current_service": "Current Service",
|
||||
"provider": "Preferred Provider",
|
||||
"proxy": "Proxy",
|
||||
"prefer_native": "Prefer Provider Native Search",
|
||||
"prefer_native_hint": "When the active model supports built-in web search, prefer that capability over the client-side tool.",
|
||||
"provider_hint": "Enable this provider and fill any required connection settings.",
|
||||
"max_results": "Max Results",
|
||||
"base_url": "Base URL",
|
||||
"base_url_placeholder": "https://api.example.com/search",
|
||||
"api_key": "API Key",
|
||||
"api_key_placeholder": "Leave blank to keep the existing key",
|
||||
"none": "Unavailable"
|
||||
},
|
||||
"status": {
|
||||
"enabled": "Enabled",
|
||||
"disabled": "Disabled",
|
||||
|
|
|
|||
|
|
@ -533,6 +533,26 @@
|
|||
"enable_success": "工具已启用。",
|
||||
"disable_success": "工具已禁用。",
|
||||
"toggle_error": "更新工具状态失败。",
|
||||
"web_search": {
|
||||
"title": "Web Search 服务",
|
||||
"description": "选择默认网页搜索后端,并配置已支持的搜索服务。",
|
||||
"load_error": "加载 Web Search 配置失败。",
|
||||
"save": "保存 Web Search 配置",
|
||||
"save_success": "Web Search 配置已更新。",
|
||||
"save_error": "更新 Web Search 配置失败。",
|
||||
"current_service": "当前服务",
|
||||
"provider": "首选服务",
|
||||
"proxy": "代理",
|
||||
"prefer_native": "优先使用模型原生搜索",
|
||||
"prefer_native_hint": "如果当前模型支持内建网页搜索,优先使用模型原生能力而不是客户端工具。",
|
||||
"provider_hint": "启用该服务后,可继续填写所需的连接参数。",
|
||||
"max_results": "最大结果数",
|
||||
"base_url": "基础 URL",
|
||||
"base_url_placeholder": "https://api.example.com/search",
|
||||
"api_key": "API Key",
|
||||
"api_key_placeholder": "留空则保留现有密钥",
|
||||
"none": "不可用"
|
||||
},
|
||||
"status": {
|
||||
"enabled": "已启用",
|
||||
"disabled": "已禁用",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue