Files
harness/tools/web/websearch.go
T
sausheong 123a233cb7 Drop Felix references; harness stands as a platform on its own
Removes every Felix mention from public-facing surfaces so the framework
reads as a standalone agent platform rather than an extracted module.

User-visible changes:
- HTTP User-Agent on web_fetch / web_search: Felix/1.0 → harness/1.0
- bash tool path-substitution prefix: '[felix]' → '[harness]'
- On-disk paths: <workspace>/.felix/ → <workspace>/.harness/
  (todos.json + spill/ tree); tests updated in lockstep.
- OTel trace attributes: felix.trace_id / felix.total_ms /
  felix.phase_count → harness.* equivalents.

Doc-comment changes:
- Examples (lta-agent, data-agent) lose 'non-Felix consumer' framing.
- README drops the 'extracted from Felix' lede and the 'What was lifted
  from Felix' section, replaced with a 'Design' overview that points
  to developer.md.
- Runtime / tool / compaction doc comments rewritten so behaviour
  notes describe what the harness does without naming the prior origin.
- Test-fixture text 'you are felix' → 'you are an assistant'.

Verified: zero 'felix' (case-insensitive) in tracked .go / .md files.
All 16 packages build and pass tests.
2026-05-08 00:24:06 +08:00

277 lines
7.1 KiB
Go

package web
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/sausheong/harness/tool"
)
const searchTimeout = 15 * time.Second
// WebSearchTool searches the web and returns results via a configurable
// backend. The default backend (when Backend is nil) scrapes DuckDuckGo's
// HTML interface — no API key required, but fragile to upstream HTML
// changes. Production users who care about reliability should configure a
// stable backend (Brave, Tavily) or self-host SearXNG.
type WebSearchTool struct {
// Backend is the active search strategy. Constructed at registration
// time from config; nil means "use the default DDG scraper" so a
// no-config install still works.
Backend WebSearchBackend
}
type webSearchInput struct {
Query string `json:"query"`
MaxResults int `json:"max_results,omitempty"`
}
func (t *WebSearchTool) Name() string { return "web_search" }
func (t *WebSearchTool) Description() string {
return "Search the web for information. Returns search results with titles, URLs, and snippets. Use this when you need current information, documentation, or to find web resources."
}
func (t *WebSearchTool) Parameters() json.RawMessage {
return json.RawMessage(`{
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query"
},
"max_results": {
"type": "integer",
"description": "Maximum number of results to return (default: 5, max: 10)"
}
},
"required": ["query"]
}`)
}
// IsConcurrencySafe returns true — web_search is a pure read.
func (t *WebSearchTool) IsConcurrencySafe(_ json.RawMessage) bool { return true }
// SetBackend swaps the active backend. Called from the config hot-reload
// path so settings-page edits take effect on the next agent turn without
// a restart. Concurrency-safe with Execute calls because only the
// pointer is replaced — the old backend remains usable until any
// in-flight Execute returns.
func (t *WebSearchTool) SetBackend(b WebSearchBackend) {
t.Backend = b
}
func (t *WebSearchTool) Execute(ctx context.Context, input json.RawMessage) (tool.ToolResult, error) {
var in webSearchInput
if err := json.Unmarshal(input, &in); err != nil {
return tool.ToolResult{Error: fmt.Sprintf("invalid input: %v", err)}, nil
}
if in.Query == "" {
return tool.ToolResult{Error: "query is required"}, nil
}
maxResults := in.MaxResults
if maxResults <= 0 {
maxResults = 5
}
if maxResults > 10 {
maxResults = 10
}
ctx, cancel := context.WithTimeout(ctx, searchTimeout)
defer cancel()
backend := t.Backend
if backend == nil {
backend = newDDGBackend()
}
results, err := backend.Search(ctx, in.Query, maxResults)
if err != nil {
return tool.ToolResult{Error: fmt.Sprintf("search failed (%s): %v", backend.Name(), err)}, nil
}
if len(results) == 0 {
return tool.ToolResult{Output: "No results found for: " + in.Query}, nil
}
var sb strings.Builder
for i, r := range results {
fmt.Fprintf(&sb, "%d. **%s**\n %s\n %s\n\n", i+1, r.Title, r.URL, r.Snippet)
}
return tool.ToolResult{
Output: sb.String(),
Metadata: map[string]any{
"query": in.Query,
"num_results": len(results),
},
}, nil
}
type searchResult struct {
Title string
URL string
Snippet string
}
// duckDuckGoSearch performs a search using DuckDuckGo's HTML interface
// and extracts results. No API key required.
func duckDuckGoSearch(ctx context.Context, query string, maxResults int) ([]searchResult, error) {
searchURL := "https://html.duckduckgo.com/html/?q=" + url.QueryEscape(query)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, searchURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "harness/1.0 (Go agent framework)")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("search returned HTTP %d", resp.StatusCode)
}
body, err := io.ReadAll(io.LimitReader(resp.Body, 1*1024*1024))
if err != nil {
return nil, err
}
return parseDDGResults(string(body), maxResults), nil
}
// parseDDGResults extracts search results from DuckDuckGo's HTML response.
func parseDDGResults(html string, maxResults int) []searchResult {
var results []searchResult
// DuckDuckGo HTML results are in <a class="result__a" ...> tags
// with snippets in <a class="result__snippet" ...> tags
remaining := html
for len(results) < maxResults {
// Find result link
linkIdx := strings.Index(remaining, `class="result__a"`)
if linkIdx == -1 {
break
}
remaining = remaining[linkIdx:]
// Extract href
href := extractAttr(remaining, "href")
// Extract title text
title := extractTagText(remaining, "a")
// Find snippet
snippetIdx := strings.Index(remaining, `class="result__snippet"`)
snippet := ""
if snippetIdx != -1 {
snippet = extractTagText(remaining[snippetIdx:], "a")
}
// Clean up the URL — DDG wraps URLs in a redirect
cleanURL := cleanDDGURL(href)
if cleanURL != "" && title != "" {
results = append(results, searchResult{
Title: cleanHTMLText(title),
URL: cleanURL,
Snippet: cleanHTMLText(snippet),
})
}
// Move past this result
nextIdx := strings.Index(remaining[1:], `class="result__a"`)
if nextIdx == -1 {
break
}
remaining = remaining[nextIdx+1:]
}
return results
}
// extractAttr extracts the value of the first href attribute found.
func extractAttr(html, attr string) string {
needle := attr + `="`
idx := strings.Index(html, needle)
if idx == -1 {
return ""
}
start := idx + len(needle)
end := strings.Index(html[start:], `"`)
if end == -1 {
return ""
}
return html[start : start+end]
}
// extractTagText extracts text content from the first occurrence of a tag.
func extractTagText(html, tag string) string {
start := strings.Index(html, ">")
if start == -1 {
return ""
}
start++
endTag := "</" + tag + ">"
end := strings.Index(html[start:], endTag)
if end == -1 {
return ""
}
return html[start : start+end]
}
// cleanDDGURL extracts the actual URL from DuckDuckGo's redirect wrapper.
func cleanDDGURL(rawURL string) string {
// DDG wraps URLs like: //duckduckgo.com/l/?uddg=https%3A%2F%2Fexample.com&rut=...
if strings.Contains(rawURL, "uddg=") {
parsed, err := url.Parse(rawURL)
if err != nil {
return rawURL
}
uddg := parsed.Query().Get("uddg")
if uddg != "" {
return uddg
}
}
if strings.HasPrefix(rawURL, "//") {
return "https:" + rawURL
}
return rawURL
}
// cleanHTMLText removes HTML tags and decodes common entities.
func cleanHTMLText(s string) string {
// Strip HTML tags
var out strings.Builder
inTag := false
for _, r := range s {
if r == '<' {
inTag = true
continue
}
if r == '>' {
inTag = false
continue
}
if !inTag {
out.WriteRune(r)
}
}
result := out.String()
result = strings.ReplaceAll(result, "&amp;", "&")
result = strings.ReplaceAll(result, "&lt;", "<")
result = strings.ReplaceAll(result, "&gt;", ">")
result = strings.ReplaceAll(result, "&quot;", `"`)
result = strings.ReplaceAll(result, "&#x27;", "'")
result = strings.ReplaceAll(result, "&nbsp;", " ")
return strings.TrimSpace(result)
}