import { streamText } from "ai"; import { getAgentById, openrouter } from "./agentRegistry.js"; // Define the tools with explicit type export interface DelegateParams { agentId?: string; prompt?: string; [key: string]: any; } export interface EchoParams { message: string; } interface ToolFunctions { [key: string]: (params: any) => Promise; } const tools: ToolFunctions = { delegate: async function ({ agentId, prompt, }: DelegateParams): Promise { // Validate required parameters if (!agentId || !prompt) { return "Error: Missing required parameters. Both 'agentId' and 'prompt' are required."; } // Find the target agent const agent = getAgentById(agentId); if (!agent) { return `Error: No such agent: ${agentId}`; } try { // Generate a response from the agent using the prompt const result = streamText({ model: openrouter(agent.modelName), messages: [ { role: "system", content: agent.systemMessage }, { role: "user", content: prompt }, ], tools: agent.tools, }); // Collect the response text let responseText = ""; // The textStream is already an AsyncIterable, no need to call it as a function for await (const chunk of result.textStream) { responseText += chunk; } // Return the agent's response return responseText; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : "Unknown error"; console.error("Error delegating to agent:", error); return `Error delegating to agent ${agentId}: ${errorMessage}`; } }, echo: async function ({ message }: EchoParams): Promise { return message; }, }; export default tools;