You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ai-net/server/tools.ts

69 lines
1.8 KiB
TypeScript

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<string>;
}
const tools: ToolFunctions = {
delegate: async function ({
agentId,
prompt,
}: DelegateParams): Promise<string> {
// 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<string> {
return message;
},
};
export default tools;