regent-ai
Version:
An AI multi-agent orchestration framework
108 lines (105 loc) • 4.49 kB
JavaScript
import { BaseAgent } from "./Agent";
export class OrchestratorAgent extends BaseAgent {
getSystemMessage(context) {
let systemMessageContent = "The current time is " +
new Date().toString() +
"\n" +
this.systemMessage;
if (this.agents?.length > 0) {
systemMessageContent +=
"\n\n" +
"You have access to these Operator Agents" +
"\n" +
this.agents
.map((agent) => agent.name + ": " + agent.description)
.join("\n");
}
if (context) {
systemMessageContent +=
"\n\n" + "Context: " + JSON.stringify(context, null, 2);
}
return {
role: "system",
content: systemMessageContent,
};
}
getTools() {
return [
{
type: "function",
function: {
name: "call_operator_agent",
description: "Call an Operator Agent to perform a task. The Operator Agent will return a response with the result of the task.",
parameters: {
type: "object",
properties: {
agent: {
type: "string",
description: "The exact name of the Operator Agent to call",
},
},
required: ["agent"],
},
},
},
];
}
constructor(options = {}) {
super("Orchestrator", "You are the Orchestrator Agent - the primary coordinator for this application's AI-driven business logic layer", `You are the Orchestrator Agent - the primary coordinator for this application's AI-driven business logic layer. Your responsibility is to analyze user messages and route them to the appropriate Operator Agent.
Your process:
1. Analyze the user's message to understand the intent
2. For greetings or pleasantries, respond briefly and guide toward available operations
3. For operational requests, identify if an available Operator Agent can handle it
4. If a suitable operator exists, hand off using call_operator_agent
5. If no suitable operator exists, explain which operations are available
Key rules:
- Keep responses brief and focused
- Never ask clarifying questions
- Never execute operations directly - only route to operators
- Each request must either:
a) Route to exactly one operator
b) Provide a brief greeting + list of available operations
c) Explain available operations when request is unsupported
Response templates:
- For greetings: "Hello! I can help you with {list of available operator capabilities}. What would you like to do?"
- For unsupported operations: "I can help you with {list of available operator capabilities}. What would you like to do?"`);
this.defaultOptions = {
model: "gpt-4",
max_tokens: 2000,
temperature: 0.5,
tool_choice: "auto",
};
this.agents = [];
this.options = { ...this.defaultOptions, ...options };
}
async run(conversationId, inputMessages, context = {}) {
const coreCompletionResult = await super._getCompletion([this.getSystemMessage(context), ...inputMessages], conversationId);
const c0m = coreCompletionResult.completion.choices[0].message;
if (c0m.tool_calls?.length) {
const toolCall = c0m.tool_calls[0];
const name = toolCall.function.name;
if (name !== "call_operator_agent") {
throw new Error(`Expected tool call to be "call_operator_agent"`);
}
const { agent: handoffToAgent } = JSON.parse(toolCall.function.arguments);
return {
producerAgent: this.name,
isFinalResponse: false,
handoffToAgent: {
name: handoffToAgent,
},
context,
};
}
else {
return {
producerAgent: this.name,
isFinalResponse: true,
chatCompletions: [coreCompletionResult.completion],
handoffToAgent: null,
context,
};
}
}
}
//# sourceMappingURL=OrchestratorAgent.js.map