import { DynamicStructuredTool } from '@langchain/core/tools'; import { z } from 'zod'; import type { FastifyBaseLogger } from 'fastify'; import type { WebExploreSubagent } from '../../harness/subagents/web-explore/index.js'; import type { SubagentContext } from '../../harness/subagents/base-subagent.js'; export interface WebExploreAgentToolConfig { webExploreSubagent: WebExploreSubagent; context: SubagentContext; logger: FastifyBaseLogger; } /** * Creates a LangChain tool that delegates to the web-explore subagent. * The subagent decides whether to use web search or arXiv based on the instruction. */ export function createWebExploreAgentTool(config: WebExploreAgentToolConfig): DynamicStructuredTool { const { webExploreSubagent, context, logger } = config; return new DynamicStructuredTool({ name: 'web_explore', description: `Search the web or academic databases and return a summarized answer. Use this tool when the user asks about: - Current events, news, or real-time information - Documentation, tutorials, or how-to guides - Academic papers, research findings, or scientific topics - Any topic that benefits from external sources The subagent will search the web (or arXiv for academic queries), fetch relevant content, and return a markdown summary with cited sources.`, schema: z.object({ instruction: z.string().describe( 'What to search for and summarize. Be specific — include the topic, what aspects matter, ' + 'and any context that helps narrow the search (e.g. "recent papers on momentum factor in equities" ' + 'or "how to configure rate limiting in Fastify").' ), }), func: async ({ instruction }: { instruction: string }): Promise => { logger.info({ instruction: instruction.substring(0, 100) }, 'Delegating to web-explore subagent'); try { return await webExploreSubagent.execute(context, instruction); } catch (error) { logger.error({ error, errorMessage: (error as Error)?.message }, 'Web explore subagent failed'); throw error; } }, }); }