node-agency
Version:
A node package for building AI agents
375 lines • 19.6 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Model = void 0;
const axios_1 = __importDefault(require("axios"));
const utils_1 = require("../utils");
const logger_1 = require("../logger");
class Model {
constructor(options) {
this.history = [];
this.selfReflected = 0;
this.parallelToolCalls = false;
this.isManager = false;
this.model = "claude-3-5-sonnet-20240620";
this.selfReflect = true;
const { parallelToolCalls, ANTHROPIC_API_KEY, model, selfReflect } = options || {};
this.apiKey = ANTHROPIC_API_KEY || process.env.ANTHROPIC_API_KEY || "";
this.parallelToolCalls = parallelToolCalls || false;
this.model = model || this.model;
this.selfReflect = selfReflect !== null && selfReflect !== void 0 ? selfReflect : true;
}
call(systemMessage, prompt, tools, context) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof prompt.content === "string") {
prompt.content =
prompt.content +
(context
? "\n\n## This is results from your coworkers to help you with your task:\n" +
context
: "");
}
this.history.push(prompt);
const messages = this.history;
try {
const message = yield this.callClaude(systemMessage, messages, tools);
this.history.push({
role: "assistant",
content: message,
});
const [content, ...calls] = message;
const tool_calls = calls.filter((call) => typeof call !== "string" && call.type === "tool_use");
if (tool_calls.length > 0) {
const toolMessagesResolved = [];
if (typeof content === "string") {
(0, logger_1.Logger)({
type: "info",
payload: "\n\n" + content + "\n\n",
});
}
const coWorkerCalls = tool_calls.filter((tc) => {
return tc.name === "delegate_task" || tc.name === "ask_question";
});
if (this.parallelToolCalls && !this.isManager) {
const filteredCalls = tool_calls.filter((tool_call) => {
return (tool_call.name !== "delegate_task" &&
tool_call.name !== "ask_question");
});
const toolMessagePromises = filteredCalls.map((tool_call) => __awaiter(this, void 0, void 0, function* () {
return this.processingToolCall(tool_call);
}));
const toolMessagesSettled = yield Promise.allSettled(toolMessagePromises);
for (const toolMessage of toolMessagesSettled) {
if (toolMessage.status === "fulfilled") {
toolMessagesResolved.push(toolMessage.value);
}
}
for (const tool_call of coWorkerCalls) {
const toolMessage = yield this.processingToolCall(tool_call);
toolMessagesResolved.push(toolMessage);
}
}
else {
for (const tool_call of tool_calls) {
const toolMessage = yield this.processingToolCall(tool_call);
toolMessagesResolved.push(toolMessage);
}
}
const allMessagesHasResvoled = tool_calls.every((message) => {
return toolMessagesResolved.find((toolMessage) => typeof toolMessage.content === "object" &&
toolMessage.content.find((content) => "tool_use_id" in content && content.tool_use_id === message.id));
});
if (!allMessagesHasResvoled) {
const missingToolCalls = tool_calls.filter((message) => {
return !toolMessagesResolved.find((toolMessage) => {
typeof toolMessage.content === "object" &&
toolMessage.content.find((content) => "tool_use_id" in content &&
content.tool_use_id === message.id);
});
});
throw new Error("Failed to resolve all tool calls Missing: " +
missingToolCalls
.map((message) => `Name: '${message.name}', ID:${message.id}`)
.join(", "));
}
const lastMessage = toolMessagesResolved[toolMessagesResolved.length - 1];
const allButLastMessage = toolMessagesResolved.slice(0, toolMessagesResolved.length - 1);
this.history.push(...allButLastMessage);
return this.call(systemMessage, lastMessage, tools);
}
if (!(typeof content !== "string" && "text" in content)) {
throw new Error("Failed to resolve content");
}
return content.text || "Unknown Error Occurred, Please try again.";
}
catch (error) {
console.error(error);
throw new Error("Failed to call Claude");
}
});
}
callStream(systemMessage, prompt, callback, tools, context) {
return __awaiter(this, void 0, void 0, function* () {
prompt.content =
prompt.content +
(context
? "\n\nHere is further context to help you with your task:\n" + context
: "");
this.history.push({
role: prompt.role,
content: [
{
type: "text",
text: prompt.content,
},
],
});
const messages = this.history;
try {
const message = yield this.callClaudeStream(systemMessage, messages, callback, tools);
return message;
}
catch (error) {
console.error(error);
throw new Error("Failed to call Claude");
}
});
}
callClaude(systemMessage_1, messages_1, tools_1) {
return __awaiter(this, arguments, void 0, function* (systemMessage, messages, tools, reflected = false) {
const converteredTools = tools === null || tools === void 0 ? void 0 : tools.map((tool) => {
const schema = tool.function.parameters;
return {
name: tool.function.name,
description: tool.function.description,
input_schema: schema,
};
});
try {
const response = yield axios_1.default.post("https://api.anthropic.com/v1/messages", {
model: this.model,
system: systemMessage,
messages,
tools: converteredTools,
max_tokens: 1000,
}, {
headers: {
"Content-Type": "application/json",
"x-api-key": this.apiKey,
"anthropic-version": "2023-06-01",
},
});
const message = response.data.content;
if (this.selfReflect && reflected && this.selfReflected >= 3) {
(0, logger_1.Logger)({ type: "warn", payload: "Self-Reflection Limit Reached\n\n" });
}
if (this.selfReflect &&
!reflected &&
!message[1] &&
this.selfReflected < 3) {
(0, logger_1.Logger)({
type: "info",
payload: `Self-Reflecting On Output (${this.selfReflected})...\n\n`,
});
this.selfReflected++;
return this.callClaude(systemMessage, [
...messages,
{ role: "assistant", content: message },
{
role: "user",
content: "Reflect on your response, find ways to improve it, respond with only the improved version, with no mention of the reflection process, or changes made.",
},
], tools, true);
}
return message;
}
catch (error) {
console.error(error);
console.debug("History: ", this.history);
throw new Error("Failed to call Claude");
}
});
}
processingToolCall(tool_call) {
return __awaiter(this, void 0, void 0, function* () {
const { name, input } = tool_call;
(0, logger_1.Logger)({
type: "function",
payload: JSON.stringify({
name,
params: input,
}),
});
const result = yield (0, utils_1.callFunction)(name, JSON.stringify(input));
const toolMessage = {
role: "user",
content: [
{
type: "tool_result",
tool_use_id: tool_call.id,
content: result,
},
],
};
return toolMessage;
});
}
callClaudeStream(systemMessage, messages, callback, tools) {
return __awaiter(this, void 0, void 0, function* () {
const converteredTools = tools === null || tools === void 0 ? void 0 : tools.map((tool) => {
const schema = tool.function.parameters;
return {
name: tool.function.name,
description: tool.function.description,
input_schema: schema,
};
});
const response = yield axios_1.default.post("https://api.anthropic.com/v1/messages", {
model: this.model,
system: systemMessage,
messages,
tools: converteredTools,
max_tokens: 1000,
stream: true,
}, {
headers: {
"Content-Type": "application/json",
"x-api-key": this.apiKey,
"anthropic-version": "2023-06-01",
},
responseType: "stream",
});
const stream = response.data;
let currentMessage = "";
let currentToolCalls = [];
const _this = this;
const toolMessages = [];
const readableStream = new ReadableStream({
start(controller) {
return __awaiter(this, void 0, void 0, function* () {
stream.on("data", (chunk) => __awaiter(this, void 0, void 0, function* () {
const lines = chunk.toString().split("\n");
for (const line of lines) {
if (line.startsWith("data: ")) {
const data = JSON.parse(line.slice(6));
if (data.type === "content_block_delta") {
const text = data.delta.text;
if (text) {
controller.enqueue(text);
currentMessage += text;
continue;
}
if (currentToolCalls.length > 0 &&
data.delta.type === "input_json_delta") {
currentToolCalls[currentToolCalls.length - 1].input +=
data.delta.partial_json;
}
}
else if (data.type === "content_block_start") {
if (data.content_block.type === "tool_use") {
currentToolCalls.push(Object.assign(Object.assign({}, data.content_block), { input: "" }));
}
}
else if (data.type === "content_block_stop") {
// if (currentToolCalls.length > 0) {
// const completedToolCall = currentToolCalls.pop();
// if (completedToolCall) {
// Logger({
// type: "function",
// payload: JSON.stringify({
// name: completedToolCall.name,
// params: completedToolCall.input,
// }),
// });
// const toolMessage = await _this.processingToolCall({
// ...completedToolCall,
// input: JSON.parse(completedToolCall.input),
// });
// toolMessages.push(toolMessage);
// }
// }
}
}
}
}));
stream.on("end", () => __awaiter(this, void 0, void 0, function* () {
var _a, e_1, _b, _c;
if (currentToolCalls.length > 0) {
const toolRequestMessage = {
role: "assistant",
content: [
{
type: "text",
text: currentMessage,
},
...currentToolCalls.map((toolCall) => {
return {
type: "tool_use",
id: toolCall.id,
name: toolCall.name,
input: JSON.parse(toolCall.input),
};
}),
],
};
toolMessages.push(toolRequestMessage);
for (const toolCall of currentToolCalls) {
const toolMessage = yield _this.processingToolCall(Object.assign(Object.assign({}, toolCall), { input: JSON.parse(toolCall.input) }));
toolMessages.push(toolMessage);
}
}
if (currentMessage && toolMessages.length === 0) {
_this.history.push({
role: "assistant",
content: currentMessage,
});
callback(currentMessage);
}
if (toolMessages.length > 0) {
_this.history.push(...toolMessages);
const newStream = yield _this.callClaudeStream(systemMessage, _this.history, callback, tools);
try {
for (var _d = true, newStream_1 = __asyncValues(newStream), newStream_1_1; newStream_1_1 = yield newStream_1.next(), _a = newStream_1_1.done, !_a; _d = true) {
_c = newStream_1_1.value;
_d = false;
const newPart = _c;
controller.enqueue(newPart);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = newStream_1.return)) yield _b.call(newStream_1);
}
finally { if (e_1) throw e_1.error; }
}
}
controller.close();
}));
});
},
});
return (0, utils_1.readableStreamAsyncIterable)(readableStream);
});
}
}
exports.Model = Model;
//# sourceMappingURL=claude.js.map