feat: SearXNG 搜索集成 + DevTools Docker + PG 备份 + 文档更新
- web_search 工具/插件接入自托管 SearXNG,支持百度/必应/搜狗/360搜索 - DevTools 加入 docker-compose.dev.yml,devtools/Dockerfile - scripts/pg-backup.sh 数据库备份恢复脚本,docs/pg-backup-migration.md - 后台思考 + datetime 插件时区默认 Asia/Shanghai - docker-compose 对齐 volume 名称,清理 tool-engine 残留引用 - README.md / Deploy.md 更新至当前架构(移除简报/tool-engine,新增搜索/跨端同步/DevTools) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -126,7 +126,11 @@ func (t *DatetimeTool) Execute(_ context.Context, args map[string]interface{}) (
|
||||
|
||||
func parseLocation(tz string) (*time.Location, error) {
|
||||
if tz == "" {
|
||||
return time.UTC, nil
|
||||
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
return time.UTC, nil
|
||||
}
|
||||
return loc, nil
|
||||
}
|
||||
return time.LoadLocation(tz)
|
||||
}
|
||||
|
||||
@@ -14,33 +14,62 @@ import (
|
||||
|
||||
type WebSearchPlugin struct {
|
||||
sdk.BasePlugin
|
||||
client *http.Client
|
||||
client *http.Client
|
||||
searxngURL string
|
||||
}
|
||||
|
||||
func NewWebSearchPlugin() *WebSearchPlugin {
|
||||
return &WebSearchPlugin{client: &http.Client{Timeout: 10 * time.Second}}
|
||||
}
|
||||
|
||||
func NewWebSearchPluginWithURL(searxngURL string) *WebSearchPlugin {
|
||||
return &WebSearchPlugin{
|
||||
client: &http.Client{Timeout: 10 * time.Second},
|
||||
searxngURL: strings.TrimRight(searxngURL, "/"),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *WebSearchPlugin) Metadata() sdk.PluginMetadata {
|
||||
return sdk.PluginMetadata{
|
||||
Name: "web_search", DisplayName: "Web Search", Version: "1.0.0",
|
||||
Description: "Search the internet via DuckDuckGo Instant Answer API",
|
||||
Name: "web_search", DisplayName: "Web Search", Version: "1.1.0",
|
||||
Description: "Search the internet via SearXNG (or DuckDuckGo fallback)",
|
||||
Category: "network", Author: sdk.PluginAuthor{Name: "Cyrene Team"},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *WebSearchPlugin) Tools() []sdk.Tool { return []sdk.Tool{&WebSearchTool{client: p.client}} }
|
||||
func (p *WebSearchPlugin) Tools() []sdk.Tool {
|
||||
return []sdk.Tool{&WebSearchTool{client: p.client, searxngURL: p.searxngURL}}
|
||||
}
|
||||
|
||||
type WebSearchTool struct {
|
||||
sdk.BaseTool
|
||||
client *http.Client
|
||||
client *http.Client
|
||||
searxngURL string
|
||||
}
|
||||
|
||||
// ---- SearXNG response types ----
|
||||
type searxngResponse struct {
|
||||
Query string `json:"query"`
|
||||
NumberOrResults int `json:"number_of_results"`
|
||||
Results []searxngResult `json:"results"`
|
||||
Answers []string `json:"answers"`
|
||||
Suggestions []string `json:"suggestions"`
|
||||
}
|
||||
|
||||
type searxngResult struct {
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
Content string `json:"content"`
|
||||
Engine string `json:"engine"`
|
||||
Score float64 `json:"score"`
|
||||
}
|
||||
|
||||
// ---- DuckDuckGo response types (fallback) ----
|
||||
type ddgResponse struct {
|
||||
Abstract string `json:"Abstract"`
|
||||
AbstractText string `json:"AbstractText"`
|
||||
Answer string `json:"Answer"`
|
||||
Heading string `json:"Heading"`
|
||||
Abstract string `json:"Abstract"`
|
||||
AbstractText string `json:"AbstractText"`
|
||||
Answer string `json:"Answer"`
|
||||
Heading string `json:"Heading"`
|
||||
Results []ddgTopic `json:"Results"`
|
||||
RelatedTopics []ddgTopic `json:"RelatedTopics"`
|
||||
}
|
||||
@@ -53,7 +82,7 @@ type ddgTopic struct {
|
||||
func (t *WebSearchTool) Definition() sdk.ToolDefinition {
|
||||
return sdk.ToolDefinition{
|
||||
ID: "web_search", Name: "web_search", DisplayName: "Web Search",
|
||||
Description: "Search the internet using DuckDuckGo Instant Answer API. Returns up to 5 results.",
|
||||
Description: "Search the internet. SearXNG backend with DuckDuckGo fallback. Returns up to 5 results.",
|
||||
Category: "network", Complexity: sdk.ComplexitySimple,
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object",
|
||||
@@ -72,6 +101,71 @@ func (t *WebSearchTool) Validate(args map[string]interface{}) error {
|
||||
|
||||
func (t *WebSearchTool) Execute(_ context.Context, args map[string]interface{}) (*sdk.ToolResult, error) {
|
||||
query, _ := args["query"].(string)
|
||||
if query == "" {
|
||||
return &sdk.ToolResult{ToolName: "web_search", Success: false, Error: "empty query"}, nil
|
||||
}
|
||||
|
||||
if t.searxngURL != "" {
|
||||
return t.searchViaSearXNG(query)
|
||||
}
|
||||
return t.searchViaDuckDuckGo(query)
|
||||
}
|
||||
|
||||
// China-accessible SearXNG engines (baidu, sogou, 360search, bing all work from China)
|
||||
const searxngEngines = "baidu,sogou,360search,bing"
|
||||
|
||||
func (t *WebSearchTool) searchViaSearXNG(query string) (*sdk.ToolResult, error) {
|
||||
apiURL := fmt.Sprintf("%s/search?format=json&engines=%s&q=%s",
|
||||
t.searxngURL, searxngEngines, url.QueryEscape(query))
|
||||
|
||||
resp, err := t.client.Get(apiURL)
|
||||
if err != nil {
|
||||
return &sdk.ToolResult{ToolName: "web_search", Success: false,
|
||||
Error: fmt.Sprintf("SearXNG request failed: %v", err)}, nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return &sdk.ToolResult{ToolName: "web_search", Success: false,
|
||||
Error: fmt.Sprintf("SearXNG returned HTTP %d", resp.StatusCode)}, nil
|
||||
}
|
||||
|
||||
var result searxngResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return &sdk.ToolResult{ToolName: "web_search", Success: false,
|
||||
Error: fmt.Sprintf("SearXNG parse error: %v", err)}, nil
|
||||
}
|
||||
|
||||
var out strings.Builder
|
||||
out.WriteString(fmt.Sprintf("搜索: %s (共%d条结果)\n\n", query, result.NumberOrResults))
|
||||
|
||||
// 优先显示答案(如 Wikipedia infobox)
|
||||
for _, answer := range result.Answers {
|
||||
out.WriteString(fmt.Sprintf("📌 %s\n\n", answer))
|
||||
}
|
||||
|
||||
// 搜索结果(最多5条,按score排序)
|
||||
count := 0
|
||||
for _, r := range result.Results {
|
||||
if count >= 5 {
|
||||
break
|
||||
}
|
||||
if r.Title == "" || r.URL == "" {
|
||||
continue
|
||||
}
|
||||
content := cleanSnippet(r.Content)
|
||||
out.WriteString(fmt.Sprintf("%d. **%s**\n %s\n %s\n\n", count+1, r.Title, r.URL, content))
|
||||
count++
|
||||
}
|
||||
|
||||
if out.Len() == 0 {
|
||||
return &sdk.ToolResult{ToolName: "web_search", Success: true,
|
||||
Output: fmt.Sprintf("未找到与「%s」相关的结果。", query)}, nil
|
||||
}
|
||||
return &sdk.ToolResult{ToolName: "web_search", Success: true, Output: out.String()}, nil
|
||||
}
|
||||
|
||||
func (t *WebSearchTool) searchViaDuckDuckGo(query string) (*sdk.ToolResult, error) {
|
||||
apiURL := fmt.Sprintf("https://api.duckduckgo.com/?q=%s&format=json&no_html=1", url.QueryEscape(query))
|
||||
resp, err := t.client.Get(apiURL)
|
||||
if err != nil {
|
||||
@@ -111,11 +205,20 @@ func (t *WebSearchTool) Execute(_ context.Context, args map[string]interface{})
|
||||
count++
|
||||
}
|
||||
if out.Len() == 0 {
|
||||
return &sdk.ToolResult{ToolName: "web_search", Success: true, Output: "No results found for: " + query}, nil
|
||||
return &sdk.ToolResult{ToolName: "web_search", Success: true,
|
||||
Output: "No results found for: " + query}, nil
|
||||
}
|
||||
return &sdk.ToolResult{ToolName: "web_search", Success: true, Output: out.String()}, nil
|
||||
}
|
||||
|
||||
func cleanSnippet(s string) string {
|
||||
runes := []rune(strings.TrimSpace(s))
|
||||
if len(runes) > 200 {
|
||||
return string(runes[:200]) + "..."
|
||||
}
|
||||
return string(runes)
|
||||
}
|
||||
|
||||
func stripHTML(s string) string {
|
||||
result := make([]rune, 0, len([]rune(s)))
|
||||
inTag := false
|
||||
|
||||
Reference in New Issue
Block a user