Changeset 0.13.1 (#65)
This commit is contained in:
152
server/tools/search/duckduckgo.go
Normal file
152
server/tools/search/duckduckgo.go
Normal file
@@ -0,0 +1,152 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProvider(&DuckDuckGo{})
|
||||
// Default active provider — no API key needed
|
||||
_ = SetActive("duckduckgo")
|
||||
}
|
||||
|
||||
// DuckDuckGo implements Provider using DDG's HTML lite endpoint.
|
||||
// No API key required. Returns organic web results.
|
||||
type DuckDuckGo struct{}
|
||||
|
||||
func (d *DuckDuckGo) Name() string { return "duckduckgo" }
|
||||
|
||||
func (d *DuckDuckGo) Search(ctx context.Context, query string, maxResults int) ([]SearchResult, error) {
|
||||
if maxResults <= 0 {
|
||||
maxResults = 5
|
||||
}
|
||||
if maxResults > 10 {
|
||||
maxResults = 10
|
||||
}
|
||||
|
||||
endpoint := "https://html.duckduckgo.com/html/?q=" + url.QueryEscape(query)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build request: %w", err)
|
||||
}
|
||||
req.Header.Set("User-Agent", "ChatSwitchboard/1.0 (Web Search Tool)")
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("search request failed: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("search returned HTTP %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// Limit body read to 512KB
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 512*1024))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
return parseDDGHTML(string(body), maxResults), nil
|
||||
}
|
||||
|
||||
// parseDDGHTML extracts search results from DuckDuckGo's HTML lite page.
|
||||
// Results are in <a class="result__a"> tags with snippets in <a class="result__snippet">.
|
||||
func parseDDGHTML(htmlContent string, maxResults int) []SearchResult {
|
||||
doc, err := html.Parse(strings.NewReader(htmlContent))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var results []SearchResult
|
||||
|
||||
// Walk the DOM looking for result blocks
|
||||
var walk func(*html.Node)
|
||||
walk = func(n *html.Node) {
|
||||
if len(results) >= maxResults {
|
||||
return
|
||||
}
|
||||
|
||||
if n.Type == html.ElementNode && n.Data == "a" {
|
||||
class := getAttr(n, "class")
|
||||
|
||||
if strings.Contains(class, "result__a") {
|
||||
href := getAttr(n, "href")
|
||||
title := textContent(n)
|
||||
// DDG wraps URLs through a redirect; extract the actual URL
|
||||
actualURL := extractDDGURL(href)
|
||||
if actualURL != "" && title != "" {
|
||||
results = append(results, SearchResult{
|
||||
Title: strings.TrimSpace(title),
|
||||
URL: actualURL,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(class, "result__snippet") {
|
||||
snippet := strings.TrimSpace(textContent(n))
|
||||
// Attach snippet to the most recent result
|
||||
if len(results) > 0 && results[len(results)-1].Snippet == "" {
|
||||
results[len(results)-1].Snippet = snippet
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
walk(c)
|
||||
}
|
||||
}
|
||||
walk(doc)
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
// extractDDGURL extracts the actual URL from DDG's redirect wrapper.
|
||||
// DDG links look like: //duckduckgo.com/l/?uddg=https%3A%2F%2Fexample.com&rut=...
|
||||
func extractDDGURL(href string) string {
|
||||
if strings.Contains(href, "uddg=") {
|
||||
u, err := url.Parse(href)
|
||||
if err != nil {
|
||||
return href
|
||||
}
|
||||
uddg := u.Query().Get("uddg")
|
||||
if uddg != "" {
|
||||
return uddg
|
||||
}
|
||||
}
|
||||
// Direct URL
|
||||
if strings.HasPrefix(href, "http") {
|
||||
return href
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func getAttr(n *html.Node, key string) string {
|
||||
for _, a := range n.Attr {
|
||||
if a.Key == key {
|
||||
return a.Val
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func textContent(n *html.Node) string {
|
||||
if n.Type == html.TextNode {
|
||||
return n.Data
|
||||
}
|
||||
var sb strings.Builder
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
sb.WriteString(textContent(c))
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
103
server/tools/search/provider.go
Normal file
103
server/tools/search/provider.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// SearchResult represents a single web search result.
|
||||
type SearchResult struct {
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
Snippet string `json:"snippet"`
|
||||
}
|
||||
|
||||
// Provider is the interface for web search backends.
|
||||
type Provider interface {
|
||||
// Search performs a web search and returns up to maxResults results.
|
||||
Search(ctx context.Context, query string, maxResults int) ([]SearchResult, error)
|
||||
|
||||
// Name returns the provider identifier (e.g. "duckduckgo", "searxng").
|
||||
Name() string
|
||||
}
|
||||
|
||||
// ── Provider Registry ───────────────────────
|
||||
|
||||
var (
|
||||
mu sync.RWMutex
|
||||
providers = map[string]Provider{}
|
||||
active string
|
||||
)
|
||||
|
||||
// RegisterProvider adds a search provider to the registry.
|
||||
func RegisterProvider(p Provider) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
providers[p.Name()] = p
|
||||
log.Printf("🔍 Registered search provider: %s", p.Name())
|
||||
}
|
||||
|
||||
// SetActive sets the active search provider by name.
|
||||
func SetActive(name string) error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if _, ok := providers[name]; !ok {
|
||||
return fmt.Errorf("unknown search provider: %s", name)
|
||||
}
|
||||
active = name
|
||||
log.Printf("🔍 Active search provider: %s", name)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Active returns the currently active provider, or nil if none configured.
|
||||
func Active() Provider {
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
if active == "" {
|
||||
return nil
|
||||
}
|
||||
return providers[active]
|
||||
}
|
||||
|
||||
// Available returns the names of all registered providers.
|
||||
func Available() []string {
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
names := make([]string, 0, len(providers))
|
||||
for name := range providers {
|
||||
names = append(names, name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// Config holds the persisted search configuration.
|
||||
type Config struct {
|
||||
Provider string `json:"provider"` // "duckduckgo" or "searxng"
|
||||
Endpoint string `json:"endpoint"` // SearXNG endpoint URL
|
||||
APIKey string `json:"api_key"` // SearXNG API key (optional)
|
||||
MaxResults int `json:"max_results"` // default results per search
|
||||
}
|
||||
|
||||
// DefaultConfig returns the default search config (DuckDuckGo, no key needed).
|
||||
func DefaultConfig() Config {
|
||||
return Config{Provider: "duckduckgo", MaxResults: 5}
|
||||
}
|
||||
|
||||
// ApplyConfig applies a Config to the active provider registry.
|
||||
// Called on startup and when admin saves settings.
|
||||
func ApplyConfig(cfg Config) error {
|
||||
if cfg.Provider == "" {
|
||||
cfg.Provider = "duckduckgo"
|
||||
}
|
||||
|
||||
// Configure SearXNG if selected
|
||||
mu.RLock()
|
||||
if sxng, ok := providers["searxng"].(*SearXNG); ok {
|
||||
sxng.Configure(cfg.Endpoint, cfg.APIKey)
|
||||
}
|
||||
mu.RUnlock()
|
||||
|
||||
return SetActive(cfg.Provider)
|
||||
}
|
||||
116
server/tools/search/searxng.go
Normal file
116
server/tools/search/searxng.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SearXNG implements Provider using a self-hosted SearXNG instance.
|
||||
// Suitable for airgapped deployments. Requires a configured endpoint.
|
||||
type SearXNG struct {
|
||||
endpoint string // base URL, e.g. "https://search.internal.example.com"
|
||||
apiKey string // optional API key (sent as X-API-Key header)
|
||||
}
|
||||
|
||||
func (s *SearXNG) Name() string { return "searxng" }
|
||||
|
||||
// Configure sets the SearXNG endpoint and optional API key.
|
||||
func (s *SearXNG) Configure(endpoint, apiKey string) {
|
||||
s.endpoint = endpoint
|
||||
s.apiKey = apiKey
|
||||
}
|
||||
|
||||
// Endpoint returns the configured endpoint (for admin display).
|
||||
func (s *SearXNG) Endpoint() string { return s.endpoint }
|
||||
|
||||
func (s *SearXNG) Search(ctx context.Context, query string, maxResults int) ([]SearchResult, error) {
|
||||
if s.endpoint == "" {
|
||||
return nil, fmt.Errorf("SearXNG endpoint not configured")
|
||||
}
|
||||
|
||||
if maxResults <= 0 {
|
||||
maxResults = 5
|
||||
}
|
||||
if maxResults > 10 {
|
||||
maxResults = 10
|
||||
}
|
||||
|
||||
// Build request URL: /search?q=...&format=json
|
||||
u, err := url.Parse(s.endpoint)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid SearXNG endpoint: %w", err)
|
||||
}
|
||||
u.Path = "/search"
|
||||
q := u.Query()
|
||||
q.Set("q", query)
|
||||
q.Set("format", "json")
|
||||
u.RawQuery = q.Encode()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build request: %w", err)
|
||||
}
|
||||
req.Header.Set("User-Agent", "ChatSwitchboard/1.0 (Web Search Tool)")
|
||||
if s.apiKey != "" {
|
||||
req.Header.Set("X-API-Key", s.apiKey)
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("SearXNG request failed: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("SearXNG returned HTTP %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 512*1024))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read SearXNG response: %w", err)
|
||||
}
|
||||
|
||||
return parseSearXNGJSON(body, maxResults)
|
||||
}
|
||||
|
||||
// SearXNG JSON response format
|
||||
type searxngResponse struct {
|
||||
Results []struct {
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
Content string `json:"content"` // snippet
|
||||
} `json:"results"`
|
||||
}
|
||||
|
||||
func parseSearXNGJSON(data []byte, maxResults int) ([]SearchResult, error) {
|
||||
var resp searxngResponse
|
||||
if err := json.Unmarshal(data, &resp); err != nil {
|
||||
return nil, fmt.Errorf("parse SearXNG response: %w", err)
|
||||
}
|
||||
|
||||
results := make([]SearchResult, 0, maxResults)
|
||||
for _, r := range resp.Results {
|
||||
if len(results) >= maxResults {
|
||||
break
|
||||
}
|
||||
if r.URL == "" {
|
||||
continue
|
||||
}
|
||||
results = append(results, SearchResult{
|
||||
Title: r.Title,
|
||||
URL: r.URL,
|
||||
Snippet: r.Content,
|
||||
})
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProvider(&SearXNG{})
|
||||
}
|
||||
Reference in New Issue
Block a user