xonecas.io
64875
Mc

mini-coder / commit a30a472

feat: implement native Exa web search and content tools

Sean2026-03-01 a30a472ef7b56e2d6b7920fa9d3ec36ce9be8749 parent 9348b39

2 files changed

src/agent/tools.ts+15−2
@@ -1,4 +1,5 @@
11 import type { ToolDef } from "../llm-api/types.ts";
2+import { webContentTool, webSearchTool } from "../tools/exa.ts";
23 import type { SubagentOutput } from "../tools/subagent.ts";
34
45 import type { CreateOutput } from "../tools/create.ts";
@@ -115,7 +116,7 @@ export function buildToolSet(opts: {
115116 const depth = opts.depth ?? 0;
116117 const lookupHook = createHookCache(HOOKABLE_TOOLS, cwd);
117118
118- return [
119+ const tools: ToolDef[] = [
119120 // Read-only: discover and inspect files
120121 withHooks(
121122 withCwdDefault(globTool as ToolDef, cwd) as ToolDef<
@@ -209,14 +210,26 @@ export function buildToolSet(opts: {
209210 opts.parentLabel,
210211 ) as ToolDef,
211212 ];
213+
214+ if (process.env.EXA_API_KEY) {
215+ tools.push(webSearchTool as ToolDef, webContentTool as ToolDef);
216+ }
217+
218+ return tools;
212219 }
213220
214221 // Plan mode: read-only tools only, no hooks (hooks are a write-tool concern).
215222 export function buildReadOnlyToolSet(opts: { cwd: string }): ToolDef[] {
216223 const { cwd } = opts;
217- return [
224+ const tools: ToolDef[] = [
218225 withCwdDefault(globTool as ToolDef, cwd),
219226 withCwdDefault(grepTool as ToolDef, cwd),
220227 withCwdDefault(readTool as ToolDef, cwd),
221228 ];
229+
230+ if (process.env.EXA_API_KEY) {
231+ tools.push(webSearchTool as ToolDef, webContentTool as ToolDef);
232+ }
233+
234+ return tools;
222235 }
src/tools/exa.ts+87−0
@@ -0,0 +1,87 @@
1+import { z } from "zod";
2+import type { ToolDef } from "../llm-api/types.ts";
3+
4+const ExaSearchSchema = z.object({
5+ query: z.string().describe("The search query"),
6+});
7+
8+export const webSearchTool: ToolDef<
9+ z.infer<typeof ExaSearchSchema>,
10+ unknown
11+> = {
12+ name: "webSearch",
13+ description: "Search the web for a query using Exa.",
14+ schema: ExaSearchSchema,
15+ execute: async (input) => {
16+ const apiKey = process.env.EXA_API_KEY;
17+ if (!apiKey) {
18+ throw new Error("EXA_API_KEY is not set.");
19+ }
20+
21+ const response = await fetch("https://api.exa.ai/search", {
22+ method: "POST",
23+ headers: {
24+ "Content-Type": "application/json",
25+ "x-api-key": apiKey,
26+ },
27+ body: JSON.stringify({
28+ query: input.query,
29+ type: "auto",
30+ numResults: 10,
31+ contents: { text: { maxCharacters: 4000 } },
32+ }),
33+ });
34+
35+ if (!response.ok) {
36+ const errorBody = await response.text();
37+ throw new Error(
38+ `Exa API error: ${response.status} ${response.statusText} - ${errorBody}`,
39+ );
40+ }
41+
42+ return await response.json();
43+ },
44+};
45+
46+const ExaContentSchema = z.object({
47+ urls: z
48+ .array(z.string())
49+ .max(3)
50+ .describe("Array of URLs to retrieve content for (max 3)"),
51+});
52+
53+export const webContentTool: ToolDef<
54+ z.infer<typeof ExaContentSchema>,
55+ unknown
56+> = {
57+ name: "webContent",
58+ description: "Get the full content of specific URLs using Exa.",
59+ schema: ExaContentSchema,
60+ execute: async (input) => {
61+ const apiKey = process.env.EXA_API_KEY;
62+ if (!apiKey) {
63+ throw new Error("EXA_API_KEY is not set.");
64+ }
65+
66+ const response = await fetch("https://api.exa.ai/contents", {
67+ method: "POST",
68+ headers: {
69+ "Content-Type": "application/json",
70+ "x-api-key": apiKey,
71+ },
72+ body: JSON.stringify({
73+ urls: input.urls,
74+ text: { maxCharacters: 10000 },
75+ }),
76+ });
77+
78+ if (!response.ok) {
79+ const errorBody = await response.text();
80+ throw new Error(
81+ `Exa API error: ${response.status} ${response.statusText} - ${errorBody}`,
82+ );
83+ }
84+
85+ return await response.json();
86+ },
87+};