jcrewai
Version:
Multi-agent automation framework written in TypeScript. Patterned after CrewAI.
59 lines (58 loc) • 2.39 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CrewAgentExecutor = void 0;
const agentUtils_1 = require("../utilities/agentUtils");
const AgentFinish_1 = require("./AgentFinish");
class CrewAgentExecutor {
constructor(agent, crew, prompt, llm) {
this.agent = agent;
this.crew = crew;
this.prompt = prompt;
this.llm = llm;
this.messages = [];
}
async invoke(inputs) {
let formattedAnswer;
if (this.prompt['system']) {
const systemPrompt = this.formatPrompt(this.prompt['system'] ?? '', inputs);
const userPrompt = this.formatPrompt(this.prompt['user'] ?? '', inputs);
this.messages.push((0, agentUtils_1.formatMessageForLlm)(systemPrompt, 'system'));
this.messages.push((0, agentUtils_1.formatMessageForLlm)(userPrompt));
}
else {
const userPrompt = this.formatPrompt(this.prompt['prompt'] ?? '', inputs);
this.messages.push((0, agentUtils_1.formatMessageForLlm)(userPrompt));
}
this.showStartLogs();
try {
formattedAnswer = await this.invokeLoop();
return { output: formattedAnswer.output };
}
catch (error) {
console.error('Agent failed to reach a final answer. This is likely a bug - please report it.');
throw new Error('Error calling CrewAgentExecutor.invoke');
}
}
async invokeLoop() {
let formattedAnswer;
while (!(formattedAnswer instanceof AgentFinish_1.AgentFinish)) {
// TODO: Add logic for tools and handle max iterations
const answer = await (0, agentUtils_1.getLlmResponse)(this.llm, this.messages);
formattedAnswer = (0, agentUtils_1.processLlmResponse)(answer);
}
return formattedAnswer;
}
showStartLogs() {
const verbose = this.agent.verbose || this.crew.verbose;
if (verbose) {
console.log(`== ${this.agent.role} ==\n`);
}
}
formatPrompt(prompt, inputs) {
prompt = prompt.replaceAll('{input}', inputs['input'] ?? '');
prompt = prompt.replaceAll('{tool_names}', inputs['tool_names'] ?? '');
prompt = prompt.replaceAll('{tools}', inputs['tools'] ?? '');
return prompt;
}
}
exports.CrewAgentExecutor = CrewAgentExecutor;