返回 presentation-ai
search.ts
根目录 / src / ai / tools / search.ts
1 import { env } from "@/env";
2 import { requireOptionalIntegration } from "@/lib/env/optional-integrations";
3 import { tavily } from "@tavily/core";
4 import { tool } from "@langchain/core/tools";
5 import z from "zod";
6
7 export const search_tool = tool(
8 async ({ query }) => {
9 try {
10 const tavilyConfig = requireOptionalIntegration({
11 integration: "Tavily",
12 envVar: "TAVILY_API_KEY",
13 value: env.TAVILY_API_KEY,
14 feature: "web search",
15 });
16
17 if (!tavilyConfig.ok) {
18 return tavilyConfig.error;
19 }
20
21 const tavilyService = tavily({ apiKey: tavilyConfig.value });
22 const response = await tavilyService.search(query, { max_results: 5 });
23 return JSON.stringify(response);
24 } catch (error) {
25 console.error("Search error:", error);
26 return "Search failed";
27 }
28 },
29 {
30 name: "webSearch",
31 description:
32 "Search the web for current information relevant to the presentation topic.",
33 schema: z.object({
34 query: z.string(),
35 }),
36 },
37 );
38
38 lines TYPESCRIPT