1import { Type } from "@sinclair/typebox";
2import type { AgentTool } from "@mariozechner/pi-ai";
3import { search } from "kagi-ken";
4import { ToolInputError } from "../../util/errors.js";
5
6const SearchSchema = Type.Object({
7 query: Type.String({ description: "Search query" }),
8});
9
10export const createWebSearchTool = (sessionToken: string): AgentTool => ({
11 name: "web_search",
12 label: "Web Search",
13 description: "Search the web using Kagi (session token required).",
14 parameters: SearchSchema as any,
15 execute: async (_toolCallId: string, params: any) => {
16 if (!sessionToken) {
17 throw new ToolInputError("Missing Kagi session token");
18 }
19
20 const result = await search(params.query, sessionToken);
21 return {
22 content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
23 details: { query: params.query, resultCount: result?.data?.length ?? 0 },
24 };
25 },
26});