@@ -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+};