100 lines
3.7 KiB
TypeScript
100 lines
3.7 KiB
TypeScript
// example_coordinator.ts
|
|
|
|
/**
|
|
* This file provides an example implementation of a coordinator agent using the AI SDK.
|
|
* Coordinator agents are responsible for routing requests to specialized agents and
|
|
* managing the overall workflow of a multi-agent system.
|
|
*
|
|
* This example demonstrates basic routing logic. You can customize this to fit your specific use case.
|
|
*/
|
|
|
|
import { Agent, AgentContext, AgentOutput, AgentConfig } from "@ai-sdk/core"; // Replace with actual import path if needed
|
|
|
|
// Define the interface for the agent's input. Customize this based on what your agents need.
|
|
interface CoordinatorInput {
|
|
query: string;
|
|
// Add other relevant input parameters here
|
|
}
|
|
|
|
// Define the interface for the agent's output. Customize this based on your needs.
|
|
interface CoordinatorOutput extends AgentOutput {
|
|
response: string;
|
|
routedToAgent?: string; // Optional: Indicate which agent handled the request
|
|
}
|
|
|
|
// Define the configuration options for the coordinator agent.
|
|
interface CoordinatorConfig extends AgentConfig {
|
|
// Add any configuration options specific to the coordinator agent here, such as:
|
|
// - List of available agents and their descriptions
|
|
// - Routing rules
|
|
// - Error handling strategies
|
|
agent1: Agent; // Define agent1
|
|
agent2: Agent; // Define agent2
|
|
}
|
|
|
|
// Implement the coordinator agent class.
|
|
class CoordinatorAgent implements Agent<CoordinatorInput, CoordinatorOutput, CoordinatorConfig> {
|
|
config: CoordinatorConfig;
|
|
|
|
constructor(config: CoordinatorConfig) {
|
|
this.config = config;
|
|
}
|
|
|
|
async execute(input: CoordinatorInput, context: AgentContext): Promise<CoordinatorOutput> {
|
|
const { query } = input;
|
|
|
|
// Implement your routing logic here. This is a simplified example.
|
|
// Consider using more sophisticated methods like:
|
|
// - Natural language understanding to determine the intent of the query
|
|
// - A knowledge base to match the query to the appropriate agent
|
|
// - A machine learning model to predict the best agent to handle the request
|
|
|
|
let routedToAgent: Agent | null = null;
|
|
let agentName: string | undefined = undefined;
|
|
|
|
if (query.toLowerCase().includes("agent1")) {
|
|
routedToAgent = this.config.agent1;
|
|
agentName = "Agent1";
|
|
} else if (query.toLowerCase().includes("agent2")) {
|
|
routedToAgent = this.config.agent2;
|
|
agentName = "Agent2";
|
|
} else {
|
|
// Default routing logic - you should customize this
|
|
routedToAgent = this.config.agent1; // Example: Default to agent1
|
|
agentName = "Agent1";
|
|
}
|
|
|
|
|
|
if (!routedToAgent) {
|
|
return {
|
|
response: "Error: No suitable agent found to handle the request.",
|
|
};
|
|
}
|
|
|
|
// Execute the selected agent.
|
|
const agentOutput: any = await routedToAgent.execute({ query: query }, context); // Replace 'any' with the actual expected output type from the agent.
|
|
|
|
// Process the output from the selected agent and format it as the coordinator's output.
|
|
return {
|
|
response: `[${agentName ?? "Unknown Agent"}] ${agentOutput.response}`, // Customize formatting as needed
|
|
routedToAgent: agentName,
|
|
// Include any other relevant information in the output
|
|
};
|
|
}
|
|
}
|
|
|
|
export { CoordinatorAgent, CoordinatorInput, CoordinatorOutput, CoordinatorConfig };
|
|
|
|
// Example Usage (in another file):
|
|
// import { CoordinatorAgent, CoordinatorConfig } from './example_coordinator';
|
|
// import { Agent1, Agent2 } from './your_agents'; // Assuming you have Agent1 and Agent2 defined
|
|
|
|
// const config: CoordinatorConfig = {
|
|
// agent1: new Agent1(),
|
|
// agent2: new Agent2(),
|
|
// // ... other configuration options
|
|
// };
|
|
|
|
// const coordinator = new CoordinatorAgent(config);
|
|
// const result = await coordinator.execute({ query: "Route this to agent1" }, context);
|
|
// console.log(result.response);
|