From 93977bf348b6d8b9760a38215e425aeef785f40e Mon Sep 17 00:00:00 2001 From: SiYue-ZO <2835601846@qq.com> Date: Tue, 14 Apr 2026 22:58:07 +0800 Subject: [PATCH] Add configurable Sogou-backed web search --- config/config.example.json | 7 +- pkg/agent/loop.go | 3 + pkg/config/config.go | 7 + pkg/config/defaults.go | 7 +- pkg/tools/web.go | 263 +++++++++++++--- pkg/tools/web_test.go | 60 ++++ web/backend/api/tools.go | 254 +++++++++++++++ web/backend/api/tools_test.go | 98 ++++++ web/frontend/src/api/tools.ts | 39 +++ .../src/components/agent/tools/tools-page.tsx | 290 +++++++++++++++++- web/frontend/src/i18n/locales/en.json | 22 +- web/frontend/src/i18n/locales/zh.json | 22 +- 12 files changed, 1027 insertions(+), 45 deletions(-) diff --git a/config/config.example.json b/config/config.example.json index 2d2d38496..d56b1cff7 100644 --- a/config/config.example.json +++ b/config/config.example.json @@ -269,10 +269,15 @@ "base_url": "", "max_results": 0 }, - "duckduckgo": { + "provider": "sogou", + "sogou": { "enabled": true, "max_results": 5 }, + "duckduckgo": { + "enabled": false, + "max_results": 5 + }, "perplexity": { "enabled": false, "api_key": "pplx-xxx", diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index bc71fa088..507d1c96f 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -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(), diff --git a/pkg/config/config.go b/pkg/config/config.go index 683f68951..ae6a5cdb0 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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, diff --git a/pkg/config/defaults.go b/pkg/config/defaults.go index d67b7a668..5f5e3d0b3 100644 --- a/pkg/config/defaults.go +++ b/pkg/config/defaults.go @@ -278,6 +278,7 @@ func DefaultConfig() *Config { ToolConfig: ToolConfig{ Enabled: true, }, + Provider: "sogou", 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, diff --git a/pkg/tools/web.go b/pkg/tools/web.go index 342f7458b..e98770b3f 100644 --- a/pkg/tools/web.go +++ b/pkg/tools/web.go @@ -46,7 +46,10 @@ var ( reDDGLink = regexp.MustCompile( `]*class="[^"]*result__a[^"]*"[^>]*href="([^"]+)"[^>]*>([\s\S]*?)`, ) - reDDGSnippet = regexp.MustCompile(`([\s\S]*?)`) + reDDGSnippet = regexp.MustCompile(`([\s\S]*?)`) + reSogouTitle = regexp.MustCompile(`]*id="sogou_vr_\d+_\d+"[^>]*>\s*(.*?)\s*`) + reSogouSnippet = regexp.MustCompile(`
\s*(.*?)\s*
`) + reSogouRealURL = regexp.MustCompile(`url=([^&]+)`) ) type APIKeyPool struct { @@ -91,6 +94,24 @@ 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 normalizeSearchRange(raw string) (string, error) { rangeCode := strings.ToLower(strings.TrimSpace(raw)) switch rangeCode { @@ -417,6 +438,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", query) + 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", "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") + + 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 @@ -890,6 +1009,7 @@ type WebSearchTool struct { } type WebSearchToolOptions struct { + Provider string BraveAPIKeys []string BraveMaxResults int BraveEnabled bool @@ -897,6 +1017,8 @@ type WebSearchToolOptions struct { TavilyBaseURL string TavilyMaxResults int TavilyEnabled bool + SogouMaxResults int + SogouEnabled bool DuckDuckGoMaxResults int DuckDuckGoEnabled bool PerplexityAPIKeys []string @@ -917,94 +1039,157 @@ 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 && len(opts.PerplexityAPIKeys) > 0 { +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 || len(opts.PerplexityAPIKeys) == 0 { + 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 && len(opts.BraveAPIKeys) > 0 { + return &PerplexitySearchProvider{ + keyPool: NewAPIKeyPool(opts.PerplexityAPIKeys), + proxy: opts.Proxy, + client: client, + }, maxResults, nil + case "brave": + if !opts.BraveEnabled || len(opts.BraveAPIKeys) == 0 { + 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 && opts.SearXNGBaseURL != "" { - provider = &SearXNGSearchProvider{baseURL: opts.SearXNGBaseURL} + return &BraveSearchProvider{keyPool: NewAPIKeyPool(opts.BraveAPIKeys), proxy: opts.Proxy, client: client}, maxResults, nil + case "searxng": + if !opts.SearXNGEnabled || opts.SearXNGBaseURL == "" { + return nil, 0, nil + } + maxResults := 10 if opts.SearXNGMaxResults > 0 { maxResults = min(opts.SearXNGMaxResults, 10) } - } else if opts.TavilyEnabled && len(opts.TavilyAPIKeys) > 0 { + return &SearXNGSearchProvider{baseURL: opts.SearXNGBaseURL}, maxResults, nil + case "tavily": + if !opts.TavilyEnabled || len(opts.TavilyAPIKeys) == 0 { + 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 && opts.BaiduSearchAPIKey != "" { + return &DuckDuckGoSearchProvider{proxy: opts.Proxy, client: client}, maxResults, nil + case "baidu_search": + if !opts.BaiduSearchEnabled || opts.BaiduSearchAPIKey == "" { + 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 || opts.GLMSearchAPIKey == "" { + return nil, 0, nil } - if opts.BaiduSearchMaxResults > 0 { - maxResults = min(opts.BaiduSearchMaxResults, 10) - } - } else if opts.GLMSearchEnabled && opts.GLMSearchAPIKey != "" { 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 NewWebSearchTool(opts WebSearchToolOptions) (*WebSearchTool, error) { + provider, maxResults, err := opts.providerByName(opts.Provider) + if err != nil { + return nil, err + } + + if provider == nil { + for _, name := range []string{"sogou", "perplexity", "brave", "searxng", "tavily", "duckduckgo", "baidu_search", "glm_search"} { + provider, maxResults, err = opts.providerByName(name) + if err != nil { + return nil, err + } + if provider != nil { + break + } } - if opts.GLMSearchMaxResults > 0 { - maxResults = min(opts.GLMSearchMaxResults, 10) - } - } else { + } + if provider == nil { return nil, nil } diff --git a/pkg/tools/web_test.go b/pkg/tools/web_test.go index de6187cfa..94faa9374 100644 --- a/pkg/tools/web_test.go +++ b/pkg/tools/web_test.go @@ -1667,3 +1667,63 @@ 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, ` +Result A +
Snippet A
+Result B +
Snippet B
+`) + 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 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) + } +} + +type roundTripFunc func(*http.Request) (*http.Response, error) + +func (fn roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { + return fn(req) +} diff --git a/web/backend/api/tools.go b/web/backend/api/tools.go index 9df4a7091..cb0bd0d3a 100644 --- a/web/backend/api/tools.go +++ b/web/backend/api/tools.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "runtime" + "strings" "github.com/sipeed/picoclaw/pkg/config" ) @@ -33,6 +34,38 @@ 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"` + 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 +186,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 +368,222 @@ 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 key := strings.TrimSpace(settings.APIKey); key != "" { + cfg.Tools.Web.Brave.SetAPIKey(key) + } + } + 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 key := strings.TrimSpace(settings.APIKey); key != "" { + cfg.Tools.Web.Tavily.SetAPIKey(key) + } + } + if settings, ok := req.Settings["perplexity"]; ok { + cfg.Tools.Web.Perplexity.Enabled = settings.Enabled + cfg.Tools.Web.Perplexity.MaxResults = settings.MaxResults + if key := strings.TrimSpace(settings.APIKey); key != "" { + cfg.Tools.Web.Perplexity.SetAPIKey(key) + } + } + 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 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{"sogou", "perplexity", "brave", "searxng", "tavily", "duckduckgo", "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 + } +} diff --git a/web/backend/api/tools_test.go b/web/backend/api/tools_test.go index 646cefbe2..a4337bcde 100644 --- a/web/backend/api/tools_test.go +++ b/web/backend/api/tools_test.go @@ -196,3 +196,101 @@ 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() + + 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") + } +} diff --git a/web/frontend/src/api/tools.ts b/web/frontend/src/api/tools.ts index 824bcc0fa..a77f3ba80 100644 --- a/web/frontend/src/api/tools.ts +++ b/web/frontend/src/api/tools.ts @@ -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 +} + async function request(path: string, options?: RequestInit): Promise { const res = await launcherFetch(path, options) if (!res.ok) { @@ -56,3 +81,17 @@ export async function setToolEnabled( }, ) } + +export async function getWebSearchConfig(): Promise { + return request("/api/tools/web-search-config") +} + +export async function updateWebSearchConfig( + payload: WebSearchConfigResponse, +): Promise { + return request("/api/tools/web-search-config", { + method: "PUT", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(payload), + }) +} diff --git a/web/frontend/src/components/agent/tools/tools-page.tsx b/web/frontend/src/components/agent/tools/tools-page.tsx index 034d21649..634dd1b7f 100644 --- a/web/frontend/src/components/agent/tools/tools-page.tsx +++ b/web/frontend/src/components/agent/tools/tools-page.tsx @@ -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(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 (
+ {webSearchError ? ( + + + {t("pages.agent.tools.web_search.title")} + {t("pages.agent.tools.web_search.load_error")} + + + ) : isWebSearchLoading || !webSearchDraft ? ( + + + + + + + + + + + + ) : ( + + + {t("pages.agent.tools.web_search.title")} + + {t("pages.agent.tools.web_search.description")} + + + +
+
+
+ {t("pages.agent.tools.web_search.current_service")} +
+
+ {currentProviderLabel} +
+
+
+
+ {t("pages.agent.tools.web_search.provider")} +
+ +
+
+
+ {t("pages.agent.tools.web_search.proxy")} +
+ + updateDraft((current) => ({ + ...current, + proxy: e.target.value, + })) + } + placeholder="http://127.0.0.1:7890" + /> +
+
+ +
+
+
+ {t("pages.agent.tools.web_search.prefer_native")} +
+
+ {t("pages.agent.tools.web_search.prefer_native_hint")} +
+
+ + updateDraft((current) => ({ + ...current, + prefer_native: checked, + })) + } + /> +
+ +
+ {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 ( + + +
+
+ {providerLabel} + + {t("pages.agent.tools.web_search.provider_hint")} + +
+ + updateDraft((current) => ({ + ...current, + settings: { + ...current.settings, + [providerId]: { + ...current.settings[providerId], + enabled: checked, + }, + }, + })) + } + /> +
+
+ +
+
+ {t("pages.agent.tools.web_search.max_results")} +
+ + updateDraft((current) => ({ + ...current, + settings: { + ...current.settings, + [providerId]: { + ...current.settings[providerId], + max_results: Number(e.target.value) || 0, + }, + }, + })) + } + /> +
+ {(providerId === "tavily" || + providerId === "searxng" || + providerId === "glm_search" || + providerId === "baidu_search") && ( +
+
+ {t("pages.agent.tools.web_search.base_url")} +
+ + 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")} + /> +
+ )} + {(providerId === "brave" || + providerId === "tavily" || + providerId === "perplexity" || + providerId === "glm_search" || + providerId === "baidu_search") && ( +
+
+ {t("pages.agent.tools.web_search.api_key")} +
+ + updateDraft((current) => ({ + ...current, + settings: { + ...current.settings, + [providerId]: { + ...current.settings[providerId], + api_key: value, + }, + }, + })) + } + placeholder={apiKeyPlaceholder} + /> +
+ )} +
+
+ ) + })} +
+ +
+ +
+
+
+ )} + {/* Header & Description */}
{/* Filters Toolbar */} diff --git a/web/frontend/src/i18n/locales/en.json b/web/frontend/src/i18n/locales/en.json index 2434d4576..b5ba80533 100644 --- a/web/frontend/src/i18n/locales/en.json +++ b/web/frontend/src/i18n/locales/en.json @@ -503,6 +503,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", @@ -656,4 +676,4 @@ "description": "Need more help? Click the documentation button in the top right corner to view detailed guides and configuration docs." } } -} \ No newline at end of file +} diff --git a/web/frontend/src/i18n/locales/zh.json b/web/frontend/src/i18n/locales/zh.json index c03d4181d..710dfa437 100644 --- a/web/frontend/src/i18n/locales/zh.json +++ b/web/frontend/src/i18n/locales/zh.json @@ -503,6 +503,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": "已禁用", @@ -656,4 +676,4 @@ "description": "需要更多帮助?点击右上角的文档按钮,查看详细的使用文档和配置指南。" } } -} \ No newline at end of file +}