xonecas.io
62159
Sy

symb / commit 5e596fa

feat: exa_ai mcp tools instead of homemade ones

xonecas2026-02-19 5e596fa6992466258c9241a252c5d385441091c2 parent cb209ab

8 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

TODO.md+0−3
@@ -1,3 +0,0 @@
1-- [ ] Investigate increasing slowdown as conversation grows
2-
3-- [ ] Investigate empty messages with some models
cmd/symb/main.go+9−11
@@ -99,8 +99,6 @@ func main() {
9999 svc.lspManager,
100100 svc.deltaTracker,
101101 svc.shell,
102- svc.webCache,
103- svc.exaKey,
104102 tools,
105103 )
106104 svc.proxy.RegisterTool(mcptools.NewSubAgentTool(), subAgentHandler.Handle)
@@ -192,13 +190,19 @@ type services struct {
192190 deltaTracker *delta.Tracker
193191 scratchpad *mcptools.Scratchpad
194192 shell *shell.Shell
195- exaKey string
196193 }
197194
198195 func setupServices(cfg *config.Config, creds *config.Credentials) services {
196+ exaKey := creds.GetAPIKey("exa_ai")
197+
198+ upstream := cfg.MCP.Upstream
199+ if upstream == "" && exaKey != "" {
200+ upstream = "https://mcp.exa.ai/mcp?tools=web_search_exa,get_code_context_exa&exaApiKey=" + exaKey
201+ }
202+
199203 var mcpClient mcp.UpstreamClient
200- if cfg.MCP.Upstream != "" {
201- mcpClient = mcp.NewClient(cfg.MCP.Upstream)
204+ if upstream != "" {
205+ mcpClient = mcp.NewClient(upstream)
202206 }
203207 proxy := mcp.NewProxy(mcpClient)
204208 if err := proxy.Initialize(context.Background()); err != nil {
@@ -224,11 +228,6 @@ func setupServices(cfg *config.Config, creds *config.Credentials) services {
224228 editHandler := mcptools.NewEditHandler(fileTracker, lspManager, dt)
225229 proxy.RegisterTool(mcptools.NewEditTool(), editHandler.Handle)
226230
227- proxy.RegisterTool(mcptools.NewWebFetchTool(), mcptools.MakeWebFetchHandler(webCache))
228-
229- exaKey := creds.GetAPIKey("exa_ai")
230- proxy.RegisterTool(mcptools.NewWebSearchTool(), mcptools.MakeWebSearchHandler(webCache, exaKey, ""))
231-
232231 // Shell tool — in-process POSIX interpreter with command blocking.
233232 sh := shell.New("", shell.DefaultBlockFuncs())
234233 shellHandler := mcptools.NewShellHandler(sh)
@@ -249,7 +248,6 @@ func setupServices(cfg *config.Config, creds *config.Credentials) services {
249248 deltaTracker: dt,
250249 scratchpad: pad,
251250 shell: sh,
252- exaKey: exaKey,
253251 }
254252 }
255253
docs/DESIGN.md+91−96
@@ -12,24 +12,84 @@ Click-to-focus between panes. Alt-screen, mouse cell-motion mode with 15ms
1212 throttle on wheel/motion events.
1313
1414 ```
15-╭──────────────────────┬───────────────────────╮
16-│ editor (read-only) │ conversation log │
17-│ - chroma highlighting│ - reasoning (muted) │
18-│ - line numbers │ - → tool calls │
19-│ - soft line wrap │ - ← tool results │
20-│ - mouse scroll/select│ - content │
21-│ - click, drag, copy │ - scroll, select, copy│
22-│ ├───────────────────────┤
23-│ │ input (editable) │
24-├──────────────────────┴───────────────────────┤
25-│ status bar with spinner │
26-╰──────────────────────────────────────────────╯
15+╭─────────────────────────────────────────────╮
16+│ conversation log │
17+│ - reasoning (muted) │
18+│ - → tool calls │
19+│ - ← tool results │
20+│ - content │
21+│ - scroll, select, copy │
22+├─────────────────────────────────────────────┤
23+│ input (editable) │
24+├─────────────────────────────────────────────┤
25+│ status bar with spinner │
26+╰─────────────────────────────────────────────╯
2727 ```
2828
29+// TODO: Remove editor pane. keep the editor for agent input
2930 Editor component (`internal/tui/editor`): full editing capability (insert,
3031 delete, paste, tab indent) gated behind `ReadOnly` flag. Left pane is read-only
3132 viewer. Input pane uses same component with `ReadOnly=false`.
3233
34+Make a generic modal with inputbox + list combo for re-use. Then use that to make the file search one.
35+(It will be used for a command box maybe)
36+
37+// TODO: Drop editor pane, use full with.
38+// Bigger refactor, means making a modal to view tool results
39+// Filesearch modal now becomes an autocomple, an inserts the file into the prompt when sent. User triggers the modal with `@`
40+
41+Conversation log, an interactive conversation log where the user can click on tool messages to open a modal
42+and view the contents of that tool call (call and results). Streams results in modal if tool is running.
43+
44+
45+### Agent input interactivity:
46+
47+Agent input, simple multiline textarea with markdown highlighting.
48+
49+Use a special key `@` to spawn an autocomplete/filesearch modal. This can match files, skills, commands or subagents.
50+When the user selects a match with `Enter` it replaces the @ with the selected item once the message is sent to the, includes hashes when injected into the user message.
51+Start with file search only for the initial implementation.
52+
53+### Search modal
54+
55+Opens via keybind: `@` in the agent input
56+
57+Search modal:
58+
59+- Centered in the UI, 80% of length and width of the main app window, resizable with the rest of the app.
60+- Top row is the input for the file search query
61+- Rest of the modal is the list of matches.
62+- Results update after the user stops typing for a few hundred miliseconds.
63+- Up/down arrow select from the search results. If focus is on input and user presses down arrow, it focus the list. If focus is on first result and user presses up arrow focus the input.
64+- `Enter` on input selects the first match closing the modal
65+- `Enter` on result list row selected the selected row. closing the modal
66+- `ESC` cancels the file search closing the modal.
67+
68+Selection is inserted into the user message on send at the place of the `@`.
69+
70+### Statusbar implementation
71+
72+Status bar, a simple bar showing the current git status current model and animated icon
73+
74+(in order: left to right)
75+
76+Left:
77+
78+- Show current branch + dirty status in status bar. (needs to be updated every X frames/time) to be responsive
79+
80+Right (right aligned text)
81+
82+- Current llm provider config name
83+- Animated icon:
84+ - Slow animation when idle.
85+ - Fast animation while a request to the LLM is in-flight (whole turn)
86+ - Animated icon becomes red on network errors until next successful request.
87+
88+### Other modals:
89+
90+- Select active model (lists all available models from all providers)
91+- Show keybinds/help
92+
3393 ### LLM Loop (`internal/llm`)
3494
3595 Streaming multi-turn. Up to 20 tool rounds per turn. Calls
@@ -42,10 +102,11 @@ Prompt system: model-specific base prompts (Claude, Gemini, Qwen, GPT).
42102 `AGENTS.md` support: walks directory tree upward collecting all AGENTS.md files,
43103 prepends to system prompt. Checks `~/.config/symb/AGENTS.md` too.
44104
105+// TODO: guard against massive folders needed.
106+
45107 ### Providers (`internal/provider`)
46108
47-- **Ollama** — local, OpenAI-compatible `/v1` endpoint. Extracts reasoning from
48- `reasoning`/`reasoning_content` fields.
109+- **Ollama**, **Zen** — local, and remote.
49110 - SSE streaming with retry on initial connection (3 retries, 429/5xx).
50111 - Single `ChatStream()` interface method replaces `Chat`/`ChatWithTools`/`Stream`.
51112 - Deterministic JSON tool schemas, provider kv cache support
@@ -56,6 +117,8 @@ Proxy merges local tool handlers with optional upstream MCP server (HTTP
56117 Streamable-HTTP transport, SSE support, session tracking). Retry with
57118 Retry-After parsing.
58119
120+Used for exa.ai tools
121+
59122 ### Tools (`internal/mcptools`)
60123
61124 7 tools registered:
@@ -69,10 +132,13 @@ Retry-After parsing.
69132 - **Shell** — sandboxed shell execution. Command blocking for dangerous ops,
70133 streaming output, timeout support.
71134 - **TodoWrite** — LLM scratchpad/plan persistence. Visible at end of context window.
72-- **WebSearch** and **WebFetch** — read and search the web (search by exa.ai).
135+- **WebSearch** and **WebFetch** — read and search the web (by exa.ai via mcp).
73136
74137 ### Git Integration
75138
139+// TODO: Reconsider git integration without the editor pane
140+// Use git diffs and worktrees for edits?
141+
76142 - git markers in the number column for editted files in the editor (needs work but does what it's meant to do)
77143 - TODO: Read includes diff for the file (or diff status, we need to consider token usage) edit tool includes updated file diff after change
78144
@@ -86,9 +152,12 @@ Retry-After parsing.
86152 - table for conversation messages
87153 - each message includes all tool calls
88154 - opening the app opens a new session (more controls later). Same behaviour
155+- modal to resume past sessions
89156
90157 ### Tree-Sitter Context
91158
159+// TODO: remove context injection. Use project treesitter info to improve Grep tool
160+
92161 Parse project with tree-sitter for structural awareness. Feed relevant
93162 symbols/scope to LLM as auto-context instead of whole files.
94163 **Uses 4k tokens on a small project like this** -- ohoh... Maybe return updated treesiter information with Grep matches for less token usage overall? Context inhection then can reduced/compacted?
@@ -98,11 +167,16 @@ symbols/scope to LLM as auto-context instead of whole files.
98167 - User should be able to undo conversation turns. The most recent entry should show a clear clickable area labelled undo.
99168 - Clicking undo reverses context history, tool calls, filesystem changes, file changes. Resets the conversation to that exact point
100169
170+### Interrupt!
171+
172+// TODO: Interrupted messages should still show the message footer in the TUI
173+
174+- User can interrupt the LLM on exit or via `ESC` when an assitant is replying
175+
101176 ### Shell Execution Tool
102177
103178 Run commands in sandbox (container isolation or restricted shell). Command
104179 whitelisting. Output streaming to conversation.
105-Needs an undo.
106180
107181 ### TUI Rendering loop
108182
@@ -115,7 +189,7 @@ Needs an undo.
115189 1. Context token count: input, output, and total tokens. Store them in db. Shown each message with the timestamp:
116190
117191 ```
118-<time elapsed> <timestamp> <toekns in current count>/<tokens out current count> (<total tokens in context at current count).
192+<time elapsed> <timestamp> <tokens in current count>/<tokens out current count> (<total tokens in context at current count).
119193 <undo if most recent agent message>
120194 ```
121195
@@ -131,59 +205,12 @@ https://manus.im/blog/Context-Engineering-for-AI-Agents-Lessons-from-Building-Ma
131205
132206 Implement findings?
133207
134-### Statusbar implementation
135-
136-(in order: left to right)
137-
138-Left:
139-
140-- Show current branch + dirty status in status bar. (needs to be updated every X frames/time) to be responsive
141-- Show lsp warnings and errors count for opened editor content, if any.
142-- Show path/filename for opened file if any and there are no LSP warnings/errors. (NOT DONE)
143-
144-Right (right aligned text)
145-
146-- Network errors to providers (llm, and exa_search), truncated.
147- - NOTE: Animated icon becomes red until next successful request.
148-- Current llm provider config name
149-- Animated icon:
150- - Slow animation when idle.
151- - Fast animation while a request to the LLM is in-flight (whole turn)
152-
153-### File Search modal
154-
155-Opens via keybind: <ctrl + f>
156-
157-File search modal:
158-
159-- Centered in the UI, 80% of legth and width of the main app window, resizable with the rest of the app.
160-- Top row is the input for the file search query
161-- Rest of the modal is the list of matches.
162-- Results update after the user stops typing for a few hundred miliseconds.
163-- Up/down arrow select from the search results. If focus is on input and user presses down arrow, it focus the list. If focus is on first result and user presses up arrow focus the input.
164-- `Enter` on input selects the first match closing the modal
165-- `Enter` on result list row selected the selected row. closing the modal
166-- `ESC` cancels the file search closing the modal.
167-
168-Selection is opened in the editor.
169-
170-Make a generic modal with inputbox + list combo for re-use. The use that to make the file search one.
171-(It will be used for a command box maybe)
172-
173208 ### Session cli flags:
174209
175210 - `-s`, `--session`: Takes a session id
176211 - `-l`, `--list`: Lists sessions with id, last user message timestamp, and 50 characters of the last user user message.
177212 - `-c`, `--continue`: continues last session (most recent user message)
178213
179-### Code viewer pane -> Becomes Code editor pane
180-
181-- Enable the same editting abilities as the agent input box.
182-- <ctrl + s> Saves the file:
183-- File has changes -> send to llm to apply diff
184-- Need to consider if this makes sense, or if the LLM should also be aware of user changes,
185- and we should always send the diff for the LLM to apply?
186-
187214 ### Sub-Agent Tool
188215
189216 Spawn a child LLM turn scoped to a single task. Useful for parallel work
@@ -204,22 +231,10 @@ Testing, cleaning up.
204231 - Custom subagent prompts for custom agents
205232 - Commands and skills implementation to match community expectations
206233
207-### Agent input interactivity:
208-
209-Use a special key `@` to spawn an autocomplete. This can match files, skills, commands or subagents.
210-When the user selects a match with `Enter` it replaces the @ with the selected item once the message is sent to the
211-LLM
212-
213234 ### Tool hooks:
214235
215236 Like git hooks, but after a specific tool call (lint after edit, or format when Y, etc).
216237
217-# TODO (after features)
218-
219-2. Investigate if turn latency has improved
220-
221----
222-
223238 ## Next version plans:
224239
225240 ### Git write commands and worktrees
@@ -232,26 +247,6 @@ Pause before executing tool calls. Show tool name + args in a dialog. User
232247 approves/rejects. Configurable per-tool permissions in `config.toml` (allow,
233248 ask, deny). Some tools (Read/Grep) default allow, mutations (Edit) default ask.
234249
235-### Parallel Tool Execution
236-
237-Execute multiple independent tool calls concurrently within a single LLM
238-turn. Needs careful coordination with FileReadTracker and TUI updates.
239-
240-### Editor-LLM link:
241-
242-LLM input (in the spirit of the app, symbiotic):
243-
244-- User types in agent input
245-- with `@cursor` or `@selected` -- Works with `@` filesearh modal.
246-- creates a reference with hashes of where the user's cursor or selection is in the filesystem/file
247-
248250 ### UI Polish
249251
250252 - Empty-state decoration in conversation pane and editor
251-
252-### Tests
253-
254-Logs should be in app data home dir, not in .local
255-
256-29 tests, all passing. Coverage: hashline 96.7%, filesearch 76%,
257-mcp_tools 43.2%, tui/editor 42.1%, tui 41.9%.
internal/mcptools/subagent.go+0−12
@@ -10,7 +10,6 @@ import (
1010 "github.com/xonecas/symb/internal/mcp"
1111 "github.com/xonecas/symb/internal/provider"
1212 "github.com/xonecas/symb/internal/shell"
13- "github.com/xonecas/symb/internal/store"
1413 "github.com/xonecas/symb/internal/subagent"
1514 )
1615
@@ -44,8 +43,6 @@ type SubAgentHandler struct {
4443 lspManager *lsp.Manager
4544 deltaTracker *delta.Tracker
4645 sh *shell.Shell
47- webCache *store.Cache
48- exaKey string
4946 allTools []mcp.Tool
5047 }
5148
@@ -55,8 +52,6 @@ func NewSubAgentHandler(
5552 lspManager *lsp.Manager,
5653 deltaTracker *delta.Tracker,
5754 sh *shell.Shell,
58- webCache *store.Cache,
59- exaKey string,
6055 allTools []mcp.Tool,
6156 ) *SubAgentHandler {
6257 // Validate required dependencies
@@ -66,15 +61,12 @@ func NewSubAgentHandler(
6661 if sh == nil {
6762 panic("SubAgentHandler: shell cannot be nil")
6863 }
69- // lspManager, deltaTracker, webCache can be nil (handlers check internally)
7064
7165 return &SubAgentHandler{
7266 provider: prov,
7367 lspManager: lspManager,
7468 deltaTracker: deltaTracker,
7569 sh: sh,
76- webCache: webCache,
77- exaKey: exaKey,
7870 allTools: allTools,
7971 }
8072 }
@@ -120,10 +112,6 @@ func (h *SubAgentHandler) Handle(ctx context.Context, arguments json.RawMessage)
120112 // Sub-agents get their own scratchpad
121113 subPad := &Scratchpad{}
122114 subProxy.RegisterTool(tool, MakeTodoWriteHandler(subPad))
123- case "WebFetch":
124- subProxy.RegisterTool(tool, MakeWebFetchHandler(h.webCache))
125- case "WebSearch":
126- subProxy.RegisterTool(tool, MakeWebSearchHandler(h.webCache, h.exaKey, ""))
127115 }
128116 }
129117
internal/mcptools/web.go+0−360
@@ -1,360 +0,0 @@
1-package mcptools
2-
3-import (
4- "bytes"
5- "context"
6- "encoding/json"
7- "fmt"
8- "io"
9- "net/http"
10- "strings"
11- "time"
12-
13- "github.com/rs/zerolog/log"
14- "github.com/xonecas/symb/internal/mcp"
15- "github.com/xonecas/symb/internal/store"
16- "golang.org/x/net/html"
17-)
18-
19-// noSearchResults is the message returned when no search results are found.
20-const noSearchResults = "No results found."
21-
22-// --- WebFetch ---
23-
24-// WebFetchArgs represents arguments for the WebFetch tool.
25-type WebFetchArgs struct {
26- URL string `json:"url"`
27- MaxChars int `json:"max_chars,omitempty"`
28-}
29-
30-// NewWebFetchTool creates the WebFetch tool definition.
31-func NewWebFetchTool() mcp.Tool {
32- return mcp.Tool{
33- Name: "WebFetch",
34- Description: "Fetch a URL and return its content as cleaned text (HTML tags, scripts, and styles stripped). Results are cached.",
35- InputSchema: json.RawMessage(`{
36- "type": "object",
37- "properties": {
38- "url": {"type": "string", "description": "The URL to fetch."},
39- "max_chars": {"type": "integer", "description": "Maximum characters to return. Default: 10000"}
40- },
41- "required": ["url"]
42- }`),
43- }
44-}
45-
46-// MakeWebFetchHandler creates a handler for the WebFetch tool.
47-func MakeWebFetchHandler(cache *store.Cache) mcp.ToolHandler {
48- client := &http.Client{Timeout: 15 * time.Second}
49-
50- return func(ctx context.Context, arguments json.RawMessage) (*mcp.ToolResult, error) {
51- var args WebFetchArgs
52- if err := json.Unmarshal(arguments, &args); err != nil {
53- return toolError("Invalid arguments: %v", err), nil
54- }
55-
56- if args.URL == "" {
57- return toolError("url is required"), nil
58- }
59- if args.MaxChars <= 0 {
60- args.MaxChars = 10000
61- }
62-
63- // Check cache (keyed by URL).
64- if cached, ok := cache.GetFetch(args.URL); ok {
65- log.Debug().Str("url", args.URL).Msg("WebFetch cache hit")
66- return toolText(truncate(cached, args.MaxChars)), nil
67- }
68-
69- req, err := http.NewRequestWithContext(ctx, http.MethodGet, args.URL, nil)
70- if err != nil {
71- return toolError("Bad URL: %v", err), nil
72- }
73- req.Header.Set("User-Agent", "Symb/0.1")
74- req.Header.Set("Accept", "text/html, text/plain;q=0.9, */*;q=0.5")
75-
76- resp, err := client.Do(req)
77- if err != nil {
78- return toolError("Fetch failed: %v", err), nil
79- }
80- defer resp.Body.Close()
81-
82- if resp.StatusCode >= 400 {
83- return toolError("HTTP %d: %s", resp.StatusCode, resp.Status), nil
84- }
85-
86- // Cap read at 1MB to avoid blowing up memory.
87- body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
88- if err != nil {
89- return toolError("Read failed: %v", err), nil
90- }
91-
92- contentType := resp.Header.Get("Content-Type")
93- var text string
94- if strings.Contains(contentType, "text/html") {
95- text = extractText(body)
96- } else {
97- text = string(body)
98- }
99-
100- cache.SetFetch(args.URL, text)
101- return toolText(truncate(text, args.MaxChars)), nil
102- }
103-}
104-
105-// --- WebSearch ---
106-
107-// WebSearchArgs represents arguments for the WebSearch tool.
108-type WebSearchArgs struct {
109- Query string `json:"query"`
110- NumResults int `json:"num_results,omitempty"`
111- Type string `json:"type,omitempty"`
112- IncludeDomains []string `json:"include_domains,omitempty"`
113-}
114-
115-// exaSearchRequest is the request body for POST https://api.exa.ai/search.
116-type exaSearchRequest struct {
117- Query string `json:"query"`
118- Type string `json:"type"`
119- NumResults int `json:"numResults"`
120- Contents exaSearchContents `json:"contents"`
121- IncludeDomains []string `json:"includeDomains,omitempty"`
122-}
123-
124-type exaSearchContents struct {
125- Text exaTextOptions `json:"text"`
126-}
127-
128-type exaTextOptions struct {
129- MaxCharacters int `json:"maxCharacters"`
130-}
131-
132-// exaSearchResponse is the response from Exa's /search endpoint.
133-type exaSearchResponse struct {
134- Results []exaResult `json:"results"`
135-}
136-
137-type exaResult struct {
138- Title string `json:"title"`
139- URL string `json:"url"`
140- Text string `json:"text"`
141- PublishedDate string `json:"publishedDate,omitempty"`
142-}
143-
144-// NewWebSearchTool creates the WebSearch tool definition.
145-func NewWebSearchTool() mcp.Tool {
146- return mcp.Tool{
147- Name: "WebSearch",
148- Description: "Search the web using Exa AI. Use this to look up documentation, APIs, libraries, or current information. Results are cached.",
149- InputSchema: json.RawMessage(`{
150- "type": "object",
151- "properties": {
152- "query": {"type": "string", "description": "Search query."},
153- "num_results": {"type": "integer", "description": "Number of results to return. Default: 5"},
154- "type": {"type": "string", "description": "Search type: \"auto\" (default), \"fast\", or \"deep\".", "enum": ["auto", "fast", "deep"]},
155- "include_domains": {"type": "array", "items": {"type": "string"}, "description": "Only include results from these domains."}
156- },
157- "required": ["query"]
158- }`),
159- }
160-}
161-
162-const exaDefaultEndpoint = "https://api.exa.ai/search"
163-
164-// MakeWebSearchHandler creates a handler for the WebSearch tool.
165-// endpoint is the Exa API URL; pass "" to use the default (https://api.exa.ai/search).
166-func MakeWebSearchHandler(cache *store.Cache, apiKey, endpoint string) mcp.ToolHandler {
167- if endpoint == "" {
168- endpoint = exaDefaultEndpoint
169- }
170- client := &http.Client{Timeout: 15 * time.Second}
171-
172- return func(ctx context.Context, arguments json.RawMessage) (*mcp.ToolResult, error) {
173- var args WebSearchArgs
174- if err := json.Unmarshal(arguments, &args); err != nil {
175- return toolError("Invalid arguments: %v", err), nil
176- }
177-
178- if args.Query == "" {
179- return toolError("query is required"), nil
180- }
181- if apiKey == "" {
182- return toolError("Exa AI API key not configured in credentials.json (providers.exa_ai.api_key)"), nil
183- }
184- if args.NumResults <= 0 {
185- args.NumResults = 5
186- }
187- if args.Type == "" {
188- args.Type = "auto"
189- }
190-
191- // Build exact cache key including params so different num_results/type
192- // don't return wrong cached results.
193- exactKey := fmt.Sprintf("%s|n=%d|t=%s|d=%s",
194- args.Query, args.NumResults, args.Type,
195- strings.Join(args.IncludeDomains, ","))
196-
197- // Check exact cache hit first (query + params).
198- if cached, ok := cache.GetSearch(exactKey); ok {
199- log.Debug().Str("query", args.Query).Msg("WebSearch exact cache hit")
200- return toolText(cached), nil
201- }
202-
203- // Search cached result content for query keywords — avoids API call
204- // if the answer already exists in a previously cached result.
205- if cached, ok := cache.SearchCachedContent(args.Query); ok {
206- log.Debug().Str("query", args.Query).Msg("WebSearch content cache hit")
207- return toolText(cached), nil
208- }
209-
210- body := exaSearchRequest{
211- Query: args.Query,
212- Type: args.Type,
213- NumResults: args.NumResults,
214- Contents: exaSearchContents{
215- Text: exaTextOptions{MaxCharacters: 2000},
216- },
217- IncludeDomains: args.IncludeDomains,
218- }
219-
220- bodyJSON, err := json.Marshal(body)
221- if err != nil {
222- return toolError("Marshal failed: %v", err), nil
223- }
224-
225- req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(bodyJSON))
226- if err != nil {
227- return toolError("Request failed: %v", err), nil
228- }
229- req.Header.Set("Content-Type", "application/json")
230- req.Header.Set("x-api-key", apiKey)
231-
232- resp, err := client.Do(req)
233- if err != nil {
234- return toolError("Search failed: %v", err), nil
235- }
236- defer resp.Body.Close()
237-
238- respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
239- if err != nil {
240- return toolError("Read response failed: %v", err), nil
241- }
242-
243- if resp.StatusCode >= 400 {
244- return toolError("Exa API error %d: %s", resp.StatusCode, string(respBody)), nil
245- }
246-
247- var exaResp exaSearchResponse
248- if err := json.Unmarshal(respBody, &exaResp); err != nil {
249- return toolError("Parse response failed: %v", err), nil
250- }
251-
252- result := formatSearchResults(exaResp.Results)
253- cache.SetSearch(exactKey, result)
254- return toolText(result), nil
255- }
256-}
257-
258-// --- Helpers ---
259-
260-// formatSearchResults formats Exa results into readable text.
261-func formatSearchResults(results []exaResult) string {
262- if len(results) == 0 {
263- return noSearchResults
264- }
265-
266- var b strings.Builder
267- b.WriteString(fmt.Sprintf("Found %d result(s):\n", len(results)))
268- for i, r := range results {
269- b.WriteString(fmt.Sprintf("\n--- %d. %s ---\n", i+1, r.Title))
270- b.WriteString(fmt.Sprintf("URL: %s\n", r.URL))
271- if r.PublishedDate != "" {
272- b.WriteString(fmt.Sprintf("Published: %s\n", r.PublishedDate))
273- }
274- if r.Text != "" {
275- b.WriteString(r.Text)
276- b.WriteByte('\n')
277- }
278- }
279- return b.String()
280-}
281-
282-// isSkipTag returns true for tags whose content should be suppressed.
283-func isSkipTag(tag string) bool {
284- return tag == "script" || tag == "style" || tag == "noscript"
285-}
286-
287-// extractText parses HTML and returns visible text content.
288-// Strips script, style, and noscript elements.
289-func extractText(data []byte) string {
290- tokenizer := html.NewTokenizer(bytes.NewReader(data))
291- var b strings.Builder
292- skip := 0
293-
294- for {
295- tt := tokenizer.Next()
296- if tt == html.ErrorToken {
297- return collapseWhitespace(b.String())
298- }
299- tn, _ := tokenizer.TagName()
300- tag := string(tn)
301-
302- switch tt {
303- case html.StartTagToken, html.SelfClosingTagToken:
304- if isSkipTag(tag) {
305- skip++
306- }
307- if isBlockElement(tag) && b.Len() > 0 {
308- b.WriteByte('\n')
309- }
310- case html.EndTagToken:
311- if isSkipTag(tag) && skip > 0 {
312- skip--
313- }
314- case html.TextToken:
315- if skip == 0 {
316- b.Write(tokenizer.Text())
317- }
318- }
319- }
320-}
321-
322-// isBlockElement returns true for HTML elements that typically start a new line.
323-func isBlockElement(tag string) bool {
324- switch tag {
325- case "p", "div", "br", "h1", "h2", "h3", "h4", "h5", "h6",
326- "li", "tr", "td", "th", "blockquote", "pre", "hr",
327- "header", "footer", "section", "article", "nav", "main":
328- return true
329- }
330- return false
331-}
332-
333-// collapseWhitespace trims each line and collapses multiple blank lines.
334-func collapseWhitespace(s string) string {
335- lines := strings.Split(s, "\n")
336- var out []string
337- blanks := 0
338- for _, line := range lines {
339- trimmed := strings.TrimSpace(line)
340- if trimmed == "" {
341- blanks++
342- if blanks <= 1 {
343- out = append(out, "")
344- }
345- continue
346- }
347- blanks = 0
348- out = append(out, trimmed)
349- }
350- return strings.TrimSpace(strings.Join(out, "\n"))
351-}
352-
353-// truncate cuts a string to maxChars (rune-safe).
354-func truncate(s string, maxChars int) string {
355- runes := []rune(s)
356- if len(runes) <= maxChars {
357- return s
358- }
359- return string(runes[:maxChars]) + "\n\n[Truncated]"
360-}
internal/mcptools/web_test.go+0−276
@@ -1,276 +0,0 @@
1-package mcptools
2-
3-import (
4- "context"
5- "encoding/json"
6- "net/http"
7- "net/http/httptest"
8- "path/filepath"
9- "testing"
10- "time"
11-
12- "github.com/xonecas/symb/internal/store"
13-)
14-
15-func testCache(t *testing.T) *store.Cache {
16- t.Helper()
17- dbPath := filepath.Join(t.TempDir(), "test.db")
18- c, err := store.Open(dbPath, 24*time.Hour)
19- if err != nil {
20- t.Fatalf("failed to open test cache: %v", err)
21- }
22- t.Cleanup(func() { c.Close() })
23- return c
24-}
25-
26-func TestExtractText(t *testing.T) {
27- html := []byte(`<html><head><title>Hello</title><script>var x=1;</script><style>body{}</style></head>
28-<body><h1>Title</h1><p>Some <b>bold</b> text.</p><div>Another block</div></body></html>`)
29-
30- text := extractText(html)
31-
32- for _, want := range []string{"Title", "Some bold text.", "Another block"} {
33- if !containsStr(text, want) {
34- t.Errorf("expected text to contain %q, got:\n%s", want, text)
35- }
36- }
37-
38- // Should NOT contain script/style content.
39- for _, unwanted := range []string{"var x=1", "body{}"} {
40- if containsStr(text, unwanted) {
41- t.Errorf("expected text to NOT contain %q, got:\n%s", unwanted, text)
42- }
43- }
44-}
45-
46-func TestExtractText_PlainText(t *testing.T) {
47- text := extractText([]byte("just plain text"))
48- if text != "just plain text" {
49- t.Errorf("expected plain passthrough, got %q", text)
50- }
51-}
52-
53-func TestTruncate(t *testing.T) {
54- s := "hello world"
55- if got := truncate(s, 100); got != s {
56- t.Errorf("should not truncate, got %q", got)
57- }
58- if got := truncate(s, 5); got != "hello\n\n[Truncated]" {
59- t.Errorf("unexpected truncation, got %q", got)
60- }
61-}
62-
63-func TestCollapseWhitespace(t *testing.T) {
64- input := " line1 \n\n\n\n line2 \n \n line3 "
65- got := collapseWhitespace(input)
66- want := "line1\n\nline2\n\nline3"
67- if got != want {
68- t.Errorf("collapseWhitespace:\ngot: %q\nwant: %q", got, want)
69- }
70-}
71-
72-func TestWebFetchHandler_HTML(t *testing.T) {
73- srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
74- w.Header().Set("Content-Type", "text/html")
75- w.Write([]byte(`<html><body><p>Hello from test</p></body></html>`))
76- }))
77- defer srv.Close()
78-
79- cache := testCache(t)
80- handler := MakeWebFetchHandler(cache)
81-
82- args, _ := json.Marshal(WebFetchArgs{URL: srv.URL})
83- result, err := handler(context.Background(), args)
84- if err != nil {
85- t.Fatalf("unexpected error: %v", err)
86- }
87- if result.IsError {
88- t.Fatalf("unexpected tool error: %s", result.Content[0].Text)
89- }
90- if !containsStr(result.Content[0].Text, "Hello from test") {
91- t.Errorf("expected 'Hello from test' in result, got %q", result.Content[0].Text)
92- }
93-
94- // Second call should hit cache.
95- result2, _ := handler(context.Background(), args)
96- if result2.Content[0].Text != result.Content[0].Text {
97- t.Error("expected identical cached result")
98- }
99-}
100-
101-func TestWebFetchHandler_PlainText(t *testing.T) {
102- srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
103- w.Header().Set("Content-Type", "text/plain")
104- w.Write([]byte("plain content"))
105- }))
106- defer srv.Close()
107-
108- cache := testCache(t)
109- handler := MakeWebFetchHandler(cache)
110-
111- args, _ := json.Marshal(WebFetchArgs{URL: srv.URL})
112- result, err := handler(context.Background(), args)
113- if err != nil {
114- t.Fatalf("unexpected error: %v", err)
115- }
116- if result.Content[0].Text != "plain content" {
117- t.Errorf("expected 'plain content', got %q", result.Content[0].Text)
118- }
119-}
120-
121-func TestWebFetchHandler_Error(t *testing.T) {
122- srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
123- w.WriteHeader(http.StatusNotFound)
124- }))
125- defer srv.Close()
126-
127- cache := testCache(t)
128- handler := MakeWebFetchHandler(cache)
129-
130- args, _ := json.Marshal(WebFetchArgs{URL: srv.URL})
131- result, _ := handler(context.Background(), args)
132- if !result.IsError {
133- t.Fatal("expected error result for 404")
134- }
135-}
136-
137-func TestWebFetchHandler_MissingURL(t *testing.T) {
138- cache := testCache(t)
139- handler := MakeWebFetchHandler(cache)
140-
141- args, _ := json.Marshal(WebFetchArgs{})
142- result, _ := handler(context.Background(), args)
143- if !result.IsError {
144- t.Fatal("expected error for missing URL")
145- }
146-}
147-
148-func TestWebSearchHandler_MissingKey(t *testing.T) {
149- cache := testCache(t)
150- handler := MakeWebSearchHandler(cache, "", "")
151-
152- args, _ := json.Marshal(WebSearchArgs{Query: "test"})
153- result, _ := handler(context.Background(), args)
154- if !result.IsError {
155- t.Fatal("expected error for missing API key")
156- }
157- if !containsStr(result.Content[0].Text, "not configured") {
158- t.Errorf("expected 'not configured' in error, got %q", result.Content[0].Text)
159- }
160-}
161-
162-func TestWebSearchHandler_MissingQuery(t *testing.T) {
163- cache := testCache(t)
164- handler := MakeWebSearchHandler(cache, "fake-key", "")
165-
166- args, _ := json.Marshal(WebSearchArgs{})
167- result, _ := handler(context.Background(), args)
168- if !result.IsError {
169- t.Fatal("expected error for missing query")
170- }
171-}
172-
173-func TestWebSearchHandler_Success(t *testing.T) {
174- srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
175- if r.Header.Get("x-api-key") != "test-key" {
176- w.WriteHeader(http.StatusUnauthorized)
177- return
178- }
179-
180- var req exaSearchRequest
181- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
182- w.WriteHeader(http.StatusBadRequest)
183- return
184- }
185-
186- resp := exaSearchResponse{
187- Results: []exaResult{
188- {Title: "Result 1", URL: "https://example.com/1", Text: "Some text"},
189- {Title: "Result 2", URL: "https://example.com/2", Text: "More text", PublishedDate: "2025-01-01"},
190- },
191- }
192- json.NewEncoder(w).Encode(resp)
193- }))
194- defer srv.Close()
195-
196- t.Run("integration", func(t *testing.T) {
197- cache := testCache(t)
198- handler := MakeWebSearchHandler(cache, "test-key", srv.URL)
199-
200- args, _ := json.Marshal(WebSearchArgs{Query: "test query", NumResults: 2})
201- result, err := handler(context.Background(), args)
202- if err != nil {
203- t.Fatalf("unexpected error: %v", err)
204- }
205- if result.IsError {
206- t.Fatalf("unexpected tool error: %s", result.Content[0].Text)
207- }
208- if !containsStr(result.Content[0].Text, "Result 1") {
209- t.Errorf("expected 'Result 1' in output, got %q", result.Content[0].Text)
210- }
211- if !containsStr(result.Content[0].Text, "Published: 2025-01-01") {
212- t.Errorf("expected published date in output, got %q", result.Content[0].Text)
213- }
214-
215- // Second call should hit cache.
216- result2, _ := handler(context.Background(), args)
217- if result2.Content[0].Text != result.Content[0].Text {
218- t.Error("expected identical cached result on second call")
219- }
220- })
221-
222- t.Run("bad_auth", func(t *testing.T) {
223- cache := testCache(t)
224- handler := MakeWebSearchHandler(cache, "wrong-key", srv.URL)
225-
226- args, _ := json.Marshal(WebSearchArgs{Query: "test"})
227- result, _ := handler(context.Background(), args)
228- if !result.IsError {
229- t.Fatal("expected error for bad auth")
230- }
231- })
232-
233- t.Run("formatSearchResults", func(t *testing.T) {
234- results := []exaResult{
235- {Title: "Go docs", URL: "https://go.dev", Text: "Go programming language"},
236- {Title: "News", URL: "https://news.com", Text: "Latest", PublishedDate: "2025-06-01"},
237- }
238- out := formatSearchResults(results)
239- if !containsStr(out, "Go docs") || !containsStr(out, "https://go.dev") {
240- t.Errorf("missing expected content in:\n%s", out)
241- }
242- if !containsStr(out, "Published: 2025-06-01") {
243- t.Errorf("missing published date in:\n%s", out)
244- }
Cut short: this commit's diff is over 1000 lines.
internal/subagent/subagent.go+1−1
Cut short: this commit's diff is over 1000 lines.
internal/tui/mouse.go+1−1
Cut short: this commit's diff is over 1000 lines.