UNPKG

regent-ai

Version:

An AI multi-agent orchestration framework

168 lines 6.98 kB
import { BaseAgent } from "./Agent"; export class OperatorAgent extends BaseAgent { getSystemMessage(context) { let systemMessageContent = "The current time is " + new Date().toString() + "\n" + this.systemMessage; if (this.toolObjects?.length > 0) { systemMessageContent += "\n\n" + "You have access to these tools" + "\n" + this.toolObjects .map((TO) => TO.tool.function.name + ": " + TO.tool.function.description) .join("\n"); } if (context) { systemMessageContent += "\n\n" + "You have access to the following context" + "\n" + JSON.stringify(context, null, 2); } systemMessageContent += "\n\n" + "You should use your available tools to perform operations and never attempt direct database access. At the end of processing user's request hand off control back to parent agent by calling report_agent_result function with appropriate success/failure status and descriptive message." + "\n\n" + "CORE RULES:" + "\n" + "1. If a tool throws an error, report the error back to the user" + "\n" + "2. If a task is outside your capabilities, call `silent_handoff_to_parent` tool" + "\n" + "3. Stay focused on your specific role and tools" + "\n" + "4. Never attempt tasks you're not equipped to handle"; return { role: "system", content: systemMessageContent, }; } getTools() { return this.getToolsFromToolObjects(this.toolObjects); } constructor(name, description, systemMessage, toolObjects, options = {}) { super(name, description, systemMessage); this.toolObjects = toolObjects; this.defaultOptions = { model: "gpt-4", max_tokens: 2000, temperature: 0.5, tool_choice: "auto", }; this.options = { ...this.defaultOptions, ...options }; const report_agent_result_tool = { functionReference: null, tool: { type: "function", function: { name: "report_agent_result", description: "Report the result of an Operator Agent's work", parameters: { type: "object", properties: { result: { type: "string", description: "The result of the Operator Agent's work", }, isSuccessful: { type: "boolean", description: "Whether the Operator Agent's work was successful", }, }, required: ["result", "isSuccessful"], }, }, }, }; const silent_handoff_to_parent_tool = { functionReference: null, tool: { type: "function", function: { name: "silent_handoff_to_parent", description: "Handoff to the parent agent", }, }, }; toolObjects.push(...[report_agent_result_tool, silent_handoff_to_parent_tool]); } static fromServiceMeta(meta) { return new OperatorAgent(meta.name, meta.description, meta.systemMessage, meta.toolObjects(), meta.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 toolName = toolCall.function.name; const toolArgs = JSON.parse(toolCall.function.arguments); if (toolName == "report_agent_result") { return this.reportAgentResult(coreCompletionResult.completion, toolArgs, context); } else if (toolName == "silent_handoff_to_parent") { return this.handoffToParent(context); } else { return await this.callTool(toolName, toolArgs, conversationId, inputMessages, c0m, toolCall, context, coreCompletionResult.completion); } } else { return { producerAgent: this.name, isFinalResponse: coreCompletionResult.completion.choices[0].finish_reason == "stop", chatCompletions: [coreCompletionResult.completion], handoffToAgent: null, context: context, }; } } async callTool(toolName, toolArgs, conversationId, inputMessages, c0m, toolCall, context, completion) { const toolObject = this.toolObjects.find((tool) => tool.tool.function.name === toolName); const funcCallResult = await toolObject.functionReference({ ...toolArgs, ...context, }); if (funcCallResult.error) funcCallResult.error = funcCallResult.error?.message || funcCallResult.error?.toString(); const result = await this.run(conversationId, [ ...inputMessages, c0m, { role: "tool", tool_call_id: toolCall.id, content: JSON.stringify(funcCallResult), }, ], this.updateContextLog(context, funcCallResult)); return { producerAgent: this.name, isFinalResponse: completion.choices[0].finish_reason == "stop" || result.isFinalResponse, chatCompletions: [result.chatCompletions[0]], handoffToAgent: result.handoffToAgent, context: result.context, }; } handoffToParent(context) { return { producerAgent: this.name, isFinalResponse: false, chatCompletions: [], handoffToAgent: { previous: true }, context, }; } reportAgentResult(completion, toolArgs, context) { delete completion.choices[0].message.tool_calls; completion.choices[0].message.content = toolArgs.result; return { producerAgent: this.name, isFinalResponse: true, chatCompletions: [completion], handoffToAgent: { previous: true }, context, }; } } //# sourceMappingURL=OperatorAgent.js.map