feat: `personalAssistant` successfully delegates to other agents
parent
be6f48659f
commit
0c1f85996c
@ -0,0 +1,29 @@
|
||||
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
|
||||
import { personalAssistantAgent } from "./agents/personalAssistant.js";
|
||||
import { chefAgent } from "./agents/chef.js";
|
||||
import { Agent } from "./types.js";
|
||||
|
||||
// This declaration is primarily for providing type hints in your code
|
||||
interface Env {
|
||||
OPENROUTER_API_KEY: string;
|
||||
}
|
||||
|
||||
declare global {
|
||||
const env: Env;
|
||||
}
|
||||
|
||||
// Create OpenRouter instance
|
||||
export const openrouter = createOpenRouter({
|
||||
apiKey: import.meta.env.VITE_OPENROUTER_API_KEY || env.OPENROUTER_API_KEY,
|
||||
});
|
||||
|
||||
// Define the agents by ID
|
||||
export const agentsById: Record<string, Agent> = {
|
||||
"personal-assistant": personalAssistantAgent,
|
||||
chef: chefAgent,
|
||||
};
|
||||
|
||||
// Helper function to get an agent by ID
|
||||
export function getAgentById(agentId: string): Agent | undefined {
|
||||
return agentsById[agentId];
|
||||
}
|
@ -1,5 +1,68 @@
|
||||
export default {
|
||||
delegate: async function () {
|
||||
return "Here's a vegetarian lasagna recipe for 4 people:";
|
||||
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;
|
||||
|
Loading…
Reference in New Issue