daggerai
Version:
A simple and powerful Typescript based agent framework to help businesses thrive in the AI Agent revolution.
72 lines • 2.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolRunner = exports.ToolError = void 0;
const helpers_1 = require("./helpers");
const prompts_1 = require("./prompts");
class ToolError {
observation;
constructor(observation) {
this.observation = observation;
}
}
exports.ToolError = ToolError;
/**
* This class is responsible for running the tools
* the LLM decides to call.
*/
class ToolRunner {
tools;
events;
runAttempts = 0;
maxRunAttemps = 5;
constructor(tools, events) {
this.tools = tools;
this.events = events;
}
async run(calling) {
const toolNames = this.tools.map(t => t.name).join(', ');
const selectedTool = this.tools.find(tool => tool.name === calling.tool);
// We limit the number of times we try to run a tool
const hasToolAttemptsReachedMax = this.runAttempts >= this.maxRunAttemps;
if (hasToolAttemptsReachedMax) {
const formatPrompt = (0, helpers_1.interpolateVariablesIntoPrompt)(prompts_1.SQUAD_PROMPTS.format, {
toolNames,
});
this.events.emit('tool.failed', {
...calling,
text: 'Max attempts reached. Moving on.',
});
return new ToolError(`\nMoving on then. ${formatPrompt}`);
}
// Returns ToolError if the tool does not exist
if (!selectedTool) {
this.runAttempts++;
this.events.emit('tool.failed', {
...calling,
text: 'Action does not exist.',
});
return new ToolError(`Action '${calling.tool}' don't exist, these are the only available Actions: ${toolNames}`);
}
// Returns ToolError if the tool call does not have a name
const selectedToolHasName = calling.tool;
if (!selectedToolHasName) {
this.runAttempts++;
this.events.emit('tool.failed', {
...calling,
text: 'Forgot the action name.',
});
return new ToolError(`I forgot the Action name, these are the only available Actions: ${toolNames}`);
}
try {
this.events.emit('tool.called', calling);
const response = await selectedTool.execute(calling.toolInput);
this.events.emit('tool.finished', { ...calling, ...response });
return response;
}
catch (error) {
return new ToolError(`I used the tool wrong, these are the correct instructions: ${selectedTool.description}`);
}
}
}
exports.ToolRunner = ToolRunner;
//# sourceMappingURL=tool.js.map