Add configurable Sogou-backed web search
This commit is contained in:
parent
08fc305d5e
commit
93977bf348
12 changed files with 1027 additions and 45 deletions
|
|
@ -269,10 +269,15 @@
|
||||||
"base_url": "",
|
"base_url": "",
|
||||||
"max_results": 0
|
"max_results": 0
|
||||||
},
|
},
|
||||||
"duckduckgo": {
|
"provider": "sogou",
|
||||||
|
"sogou": {
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"max_results": 5
|
"max_results": 5
|
||||||
},
|
},
|
||||||
|
"duckduckgo": {
|
||||||
|
"enabled": false,
|
||||||
|
"max_results": 5
|
||||||
|
},
|
||||||
"perplexity": {
|
"perplexity": {
|
||||||
"enabled": false,
|
"enabled": false,
|
||||||
"api_key": "pplx-xxx",
|
"api_key": "pplx-xxx",
|
||||||
|
|
|
||||||
|
|
@ -194,6 +194,7 @@ func registerSharedTools(
|
||||||
|
|
||||||
if cfg.Tools.IsToolEnabled("web") {
|
if cfg.Tools.IsToolEnabled("web") {
|
||||||
searchTool, err := tools.NewWebSearchTool(tools.WebSearchToolOptions{
|
searchTool, err := tools.NewWebSearchTool(tools.WebSearchToolOptions{
|
||||||
|
Provider: cfg.Tools.Web.Provider,
|
||||||
BraveAPIKeys: cfg.Tools.Web.Brave.APIKeys.Values(),
|
BraveAPIKeys: cfg.Tools.Web.Brave.APIKeys.Values(),
|
||||||
BraveMaxResults: cfg.Tools.Web.Brave.MaxResults,
|
BraveMaxResults: cfg.Tools.Web.Brave.MaxResults,
|
||||||
BraveEnabled: cfg.Tools.Web.Brave.Enabled,
|
BraveEnabled: cfg.Tools.Web.Brave.Enabled,
|
||||||
|
|
@ -201,6 +202,8 @@ func registerSharedTools(
|
||||||
TavilyBaseURL: cfg.Tools.Web.Tavily.BaseURL,
|
TavilyBaseURL: cfg.Tools.Web.Tavily.BaseURL,
|
||||||
TavilyMaxResults: cfg.Tools.Web.Tavily.MaxResults,
|
TavilyMaxResults: cfg.Tools.Web.Tavily.MaxResults,
|
||||||
TavilyEnabled: cfg.Tools.Web.Tavily.Enabled,
|
TavilyEnabled: cfg.Tools.Web.Tavily.Enabled,
|
||||||
|
SogouMaxResults: cfg.Tools.Web.Sogou.MaxResults,
|
||||||
|
SogouEnabled: cfg.Tools.Web.Sogou.Enabled,
|
||||||
DuckDuckGoMaxResults: cfg.Tools.Web.DuckDuckGo.MaxResults,
|
DuckDuckGoMaxResults: cfg.Tools.Web.DuckDuckGo.MaxResults,
|
||||||
DuckDuckGoEnabled: cfg.Tools.Web.DuckDuckGo.Enabled,
|
DuckDuckGoEnabled: cfg.Tools.Web.DuckDuckGo.Enabled,
|
||||||
PerplexityAPIKeys: cfg.Tools.Web.Perplexity.APIKeys.Values(),
|
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"`
|
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 {
|
type PerplexityConfig struct {
|
||||||
Enabled bool `json:"enabled" yaml:"-" env:"PICOCLAW_TOOLS_WEB_PERPLEXITY_ENABLED"`
|
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"`
|
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_"`
|
ToolConfig ` yaml:"-" envPrefix:"PICOCLAW_TOOLS_WEB_"`
|
||||||
Brave BraveConfig `yaml:"brave,omitempty" json:"brave"`
|
Brave BraveConfig `yaml:"brave,omitempty" json:"brave"`
|
||||||
Tavily TavilyConfig `yaml:"tavily,omitempty" json:"tavily"`
|
Tavily TavilyConfig `yaml:"tavily,omitempty" json:"tavily"`
|
||||||
|
Sogou SogouConfig `yaml:"-" json:"sogou"`
|
||||||
DuckDuckGo DuckDuckGoConfig `yaml:"-" json:"duckduckgo"`
|
DuckDuckGo DuckDuckGoConfig `yaml:"-" json:"duckduckgo"`
|
||||||
Perplexity PerplexityConfig `yaml:"perplexity,omitempty" json:"perplexity"`
|
Perplexity PerplexityConfig `yaml:"perplexity,omitempty" json:"perplexity"`
|
||||||
SearXNG SearXNGConfig `yaml:"-" json:"searxng"`
|
SearXNG SearXNGConfig `yaml:"-" json:"searxng"`
|
||||||
GLMSearch GLMSearchConfig `yaml:"glm_search,omitempty" json:"glm_search"`
|
GLMSearch GLMSearchConfig `yaml:"glm_search,omitempty" json:"glm_search"`
|
||||||
BaiduSearch BaiduSearchConfig `yaml:"baidu_search,omitempty" json:"baidu_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
|
// PreferNative controls whether to use provider-native web search when
|
||||||
// the active LLM supports it (e.g. OpenAI web_search_preview). When true,
|
// 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,
|
// the client-side web_search tool is hidden to avoid duplicate search surfaces,
|
||||||
|
|
|
||||||
|
|
@ -278,6 +278,7 @@ func DefaultConfig() *Config {
|
||||||
ToolConfig: ToolConfig{
|
ToolConfig: ToolConfig{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
},
|
},
|
||||||
|
Provider: "sogou",
|
||||||
PreferNative: true,
|
PreferNative: true,
|
||||||
Proxy: "",
|
Proxy: "",
|
||||||
FetchLimitBytes: 10 * 1024 * 1024, // 10MB by default
|
FetchLimitBytes: 10 * 1024 * 1024, // 10MB by default
|
||||||
|
|
@ -290,10 +291,14 @@ func DefaultConfig() *Config {
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
MaxResults: 5,
|
MaxResults: 5,
|
||||||
},
|
},
|
||||||
DuckDuckGo: DuckDuckGoConfig{
|
Sogou: SogouConfig{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
MaxResults: 5,
|
MaxResults: 5,
|
||||||
},
|
},
|
||||||
|
DuckDuckGo: DuckDuckGoConfig{
|
||||||
|
Enabled: false,
|
||||||
|
MaxResults: 5,
|
||||||
|
},
|
||||||
Perplexity: PerplexityConfig{
|
Perplexity: PerplexityConfig{
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
MaxResults: 5,
|
MaxResults: 5,
|
||||||
|
|
|
||||||
263
pkg/tools/web.go
263
pkg/tools/web.go
|
|
@ -46,7 +46,10 @@ var (
|
||||||
reDDGLink = regexp.MustCompile(
|
reDDGLink = regexp.MustCompile(
|
||||||
`<a[^>]*class="[^"]*result__a[^"]*"[^>]*href="([^"]+)"[^>]*>([\s\S]*?)</a>`,
|
`<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=([^&]+)`)
|
||||||
)
|
)
|
||||||
|
|
||||||
type APIKeyPool struct {
|
type APIKeyPool struct {
|
||||||
|
|
@ -91,6 +94,24 @@ type SearchProvider interface {
|
||||||
Search(ctx context.Context, query string, count int, rangeCode string) (string, error)
|
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) {
|
func normalizeSearchRange(raw string) (string, error) {
|
||||||
rangeCode := strings.ToLower(strings.TrimSpace(raw))
|
rangeCode := strings.ToLower(strings.TrimSpace(raw))
|
||||||
switch rangeCode {
|
switch rangeCode {
|
||||||
|
|
@ -417,6 +438,104 @@ func (p *TavilySearchProvider) Search(
|
||||||
return "", fmt.Errorf("all api keys failed, last error: %w", lastErr)
|
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 {
|
type DuckDuckGoSearchProvider struct {
|
||||||
proxy string
|
proxy string
|
||||||
client *http.Client
|
client *http.Client
|
||||||
|
|
@ -890,6 +1009,7 @@ type WebSearchTool struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type WebSearchToolOptions struct {
|
type WebSearchToolOptions struct {
|
||||||
|
Provider string
|
||||||
BraveAPIKeys []string
|
BraveAPIKeys []string
|
||||||
BraveMaxResults int
|
BraveMaxResults int
|
||||||
BraveEnabled bool
|
BraveEnabled bool
|
||||||
|
|
@ -897,6 +1017,8 @@ type WebSearchToolOptions struct {
|
||||||
TavilyBaseURL string
|
TavilyBaseURL string
|
||||||
TavilyMaxResults int
|
TavilyMaxResults int
|
||||||
TavilyEnabled bool
|
TavilyEnabled bool
|
||||||
|
SogouMaxResults int
|
||||||
|
SogouEnabled bool
|
||||||
DuckDuckGoMaxResults int
|
DuckDuckGoMaxResults int
|
||||||
DuckDuckGoEnabled bool
|
DuckDuckGoEnabled bool
|
||||||
PerplexityAPIKeys []string
|
PerplexityAPIKeys []string
|
||||||
|
|
@ -917,94 +1039,157 @@ type WebSearchToolOptions struct {
|
||||||
Proxy string
|
Proxy string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWebSearchTool(opts WebSearchToolOptions) (*WebSearchTool, error) {
|
func (opts WebSearchToolOptions) providerByName(name string) (SearchProvider, int, error) {
|
||||||
var provider SearchProvider
|
switch strings.ToLower(strings.TrimSpace(name)) {
|
||||||
maxResults := 10
|
case "", "auto":
|
||||||
// Priority: Perplexity > Brave > SearXNG > Tavily > DuckDuckGo > Baidu Search > GLM Search
|
return nil, 0, nil
|
||||||
if opts.PerplexityEnabled && len(opts.PerplexityAPIKeys) > 0 {
|
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)
|
client, err := utils.CreateHTTPClient(opts.Proxy, perplexityTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create HTTP client for Perplexity: %w", err)
|
return nil, 0, fmt.Errorf("failed to create HTTP client for Perplexity: %w", err)
|
||||||
}
|
|
||||||
provider = &PerplexitySearchProvider{
|
|
||||||
keyPool: NewAPIKeyPool(opts.PerplexityAPIKeys),
|
|
||||||
proxy: opts.Proxy,
|
|
||||||
client: client,
|
|
||||||
}
|
}
|
||||||
|
maxResults := 10
|
||||||
if opts.PerplexityMaxResults > 0 {
|
if opts.PerplexityMaxResults > 0 {
|
||||||
maxResults = min(opts.PerplexityMaxResults, 10)
|
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)
|
client, err := utils.CreateHTTPClient(opts.Proxy, searchTimeout)
|
||||||
if err != nil {
|
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 {
|
if opts.BraveMaxResults > 0 {
|
||||||
maxResults = min(opts.BraveMaxResults, 10)
|
maxResults = min(opts.BraveMaxResults, 10)
|
||||||
}
|
}
|
||||||
} else if opts.SearXNGEnabled && opts.SearXNGBaseURL != "" {
|
return &BraveSearchProvider{keyPool: NewAPIKeyPool(opts.BraveAPIKeys), proxy: opts.Proxy, client: client}, maxResults, nil
|
||||||
provider = &SearXNGSearchProvider{baseURL: opts.SearXNGBaseURL}
|
case "searxng":
|
||||||
|
if !opts.SearXNGEnabled || opts.SearXNGBaseURL == "" {
|
||||||
|
return nil, 0, nil
|
||||||
|
}
|
||||||
|
maxResults := 10
|
||||||
if opts.SearXNGMaxResults > 0 {
|
if opts.SearXNGMaxResults > 0 {
|
||||||
maxResults = min(opts.SearXNGMaxResults, 10)
|
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)
|
client, err := utils.CreateHTTPClient(opts.Proxy, searchTimeout)
|
||||||
if err != nil {
|
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),
|
keyPool: NewAPIKeyPool(opts.TavilyAPIKeys),
|
||||||
baseURL: opts.TavilyBaseURL,
|
baseURL: opts.TavilyBaseURL,
|
||||||
proxy: opts.Proxy,
|
proxy: opts.Proxy,
|
||||||
client: client,
|
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)
|
client, err := utils.CreateHTTPClient(opts.Proxy, searchTimeout)
|
||||||
if err != nil {
|
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 {
|
if opts.DuckDuckGoMaxResults > 0 {
|
||||||
maxResults = min(opts.DuckDuckGoMaxResults, 10)
|
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)
|
client, err := utils.CreateHTTPClient(opts.Proxy, perplexityTimeout)
|
||||||
if err != nil {
|
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,
|
apiKey: opts.BaiduSearchAPIKey,
|
||||||
baseURL: opts.BaiduSearchBaseURL,
|
baseURL: opts.BaiduSearchBaseURL,
|
||||||
proxy: opts.Proxy,
|
proxy: opts.Proxy,
|
||||||
client: client,
|
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)
|
client, err := utils.CreateHTTPClient(opts.Proxy, searchTimeout)
|
||||||
if err != nil {
|
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
|
searchEngine := opts.GLMSearchEngine
|
||||||
if searchEngine == "" {
|
if searchEngine == "" {
|
||||||
searchEngine = "search_std"
|
searchEngine = "search_std"
|
||||||
}
|
}
|
||||||
provider = &GLMSearchProvider{
|
maxResults := 10
|
||||||
|
if opts.GLMSearchMaxResults > 0 {
|
||||||
|
maxResults = min(opts.GLMSearchMaxResults, 10)
|
||||||
|
}
|
||||||
|
return &GLMSearchProvider{
|
||||||
apiKey: opts.GLMSearchAPIKey,
|
apiKey: opts.GLMSearchAPIKey,
|
||||||
baseURL: opts.GLMSearchBaseURL,
|
baseURL: opts.GLMSearchBaseURL,
|
||||||
searchEngine: searchEngine,
|
searchEngine: searchEngine,
|
||||||
proxy: opts.Proxy,
|
proxy: opts.Proxy,
|
||||||
client: client,
|
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)
|
if provider == nil {
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1667,3 +1667,63 @@ func TestWebTool_GLMSearch_Priority(t *testing.T) {
|
||||||
t.Errorf("Expected GLMSearchProvider when only GLM enabled, got %T", tool2.provider)
|
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 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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/sipeed/picoclaw/pkg/config"
|
"github.com/sipeed/picoclaw/pkg/config"
|
||||||
)
|
)
|
||||||
|
|
@ -33,6 +34,38 @@ type toolStateRequest struct {
|
||||||
Enabled bool `json:"enabled"`
|
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{
|
var toolCatalog = []toolCatalogEntry{
|
||||||
{
|
{
|
||||||
Name: "read_file",
|
Name: "read_file",
|
||||||
|
|
@ -153,6 +186,8 @@ var toolCatalog = []toolCatalogEntry{
|
||||||
func (h *Handler) registerToolRoutes(mux *http.ServeMux) {
|
func (h *Handler) registerToolRoutes(mux *http.ServeMux) {
|
||||||
mux.HandleFunc("GET /api/tools", h.handleListTools)
|
mux.HandleFunc("GET /api/tools", h.handleListTools)
|
||||||
mux.HandleFunc("PUT /api/tools/{name}/state", h.handleUpdateToolState)
|
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) {
|
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
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -196,3 +196,101 @@ func TestHandleUpdateToolState(t *testing.T) {
|
||||||
t.Fatalf("cron should be enabled: %#v", updated.Tools.Cron)
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,31 @@ interface ToolActionResponse {
|
||||||
status: string
|
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> {
|
async function request<T>(path: string, options?: RequestInit): Promise<T> {
|
||||||
const res = await launcherFetch(path, options)
|
const res = await launcherFetch(path, options)
|
||||||
if (!res.ok) {
|
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 { IconSearch } from "@tabler/icons-react"
|
||||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
|
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 { useTranslation } from "react-i18next"
|
||||||
import { toast } from "sonner"
|
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 { PageHeader } from "@/components/page-header"
|
||||||
|
import { maskedSecretPlaceholder } from "@/components/secret-placeholder"
|
||||||
|
import { KeyInput } from "@/components/shared-form"
|
||||||
|
import { Button } from "@/components/ui/button"
|
||||||
import {
|
import {
|
||||||
Card,
|
Card,
|
||||||
CardContent,
|
CardContent,
|
||||||
|
|
@ -33,9 +43,25 @@ export function ToolsPage() {
|
||||||
queryKey: ["tools"],
|
queryKey: ["tools"],
|
||||||
queryFn: getTools,
|
queryFn: getTools,
|
||||||
})
|
})
|
||||||
|
const {
|
||||||
|
data: webSearchData,
|
||||||
|
isLoading: isWebSearchLoading,
|
||||||
|
error: webSearchError,
|
||||||
|
} = useQuery({
|
||||||
|
queryKey: ["tools", "web-search-config"],
|
||||||
|
queryFn: getWebSearchConfig,
|
||||||
|
})
|
||||||
|
|
||||||
const [searchQuery, setSearchQuery] = useState("")
|
const [searchQuery, setSearchQuery] = useState("")
|
||||||
const [statusFilter, setStatusFilter] = useState("all")
|
const [statusFilter, setStatusFilter] = useState("all")
|
||||||
|
const [webSearchDraft, setWebSearchDraft] =
|
||||||
|
useState<WebSearchConfigResponse | null>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (webSearchData) {
|
||||||
|
setWebSearchDraft(webSearchData)
|
||||||
|
}
|
||||||
|
}, [webSearchData])
|
||||||
|
|
||||||
const toggleMutation = useMutation({
|
const toggleMutation = useMutation({
|
||||||
mutationFn: async ({ name, enabled }: { name: string; enabled: boolean }) =>
|
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
|
// Filter and group tools
|
||||||
const { groupedTools, totalFilteredCount } = useMemo(() => {
|
const { groupedTools, totalFilteredCount } = useMemo(() => {
|
||||||
if (!data) return { groupedTools: [], totalFilteredCount: 0 }
|
if (!data) return { groupedTools: [], totalFilteredCount: 0 }
|
||||||
|
|
@ -91,12 +135,254 @@ export function ToolsPage() {
|
||||||
}
|
}
|
||||||
}, [data, searchQuery, statusFilter])
|
}, [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 (
|
return (
|
||||||
<div className="bg-background flex h-full flex-col">
|
<div className="bg-background flex h-full flex-col">
|
||||||
<PageHeader title={t("navigation.tools")} />
|
<PageHeader title={t("navigation.tools")} />
|
||||||
|
|
||||||
<div className="flex-1 overflow-auto px-6 py-6">
|
<div className="flex-1 overflow-auto px-6 py-6">
|
||||||
<div className="mx-auto w-full max-w-6xl space-y-8">
|
<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 */}
|
{/* Header & Description */}
|
||||||
<div className="flex flex-col gap-4 sm:flex-row sm:items-end sm:justify-end">
|
<div className="flex flex-col gap-4 sm:flex-row sm:items-end sm:justify-end">
|
||||||
{/* Filters Toolbar */}
|
{/* Filters Toolbar */}
|
||||||
|
|
|
||||||
|
|
@ -503,6 +503,26 @@
|
||||||
"enable_success": "Tool enabled.",
|
"enable_success": "Tool enabled.",
|
||||||
"disable_success": "Tool disabled.",
|
"disable_success": "Tool disabled.",
|
||||||
"toggle_error": "Failed to update tool state.",
|
"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": {
|
"status": {
|
||||||
"enabled": "Enabled",
|
"enabled": "Enabled",
|
||||||
"disabled": "Disabled",
|
"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."
|
"description": "Need more help? Click the documentation button in the top right corner to view detailed guides and configuration docs."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -503,6 +503,26 @@
|
||||||
"enable_success": "工具已启用。",
|
"enable_success": "工具已启用。",
|
||||||
"disable_success": "工具已禁用。",
|
"disable_success": "工具已禁用。",
|
||||||
"toggle_error": "更新工具状态失败。",
|
"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": {
|
"status": {
|
||||||
"enabled": "已启用",
|
"enabled": "已启用",
|
||||||
"disabled": "已禁用",
|
"disabled": "已禁用",
|
||||||
|
|
@ -656,4 +676,4 @@
|
||||||
"description": "需要更多帮助?点击右上角的文档按钮,查看详细的使用文档和配置指南。"
|
"description": "需要更多帮助?点击右上角的文档按钮,查看详细的使用文档和配置指南。"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue