UNPKG

node-agency

Version:
416 lines 22.4 kB
"use strict"; 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"; const { parallelToolCalls, ANTHROPIC_API_KEY, model } = options || {}; this.apiKey = ANTHROPIC_API_KEY || process.env.ANTHROPIC_API_KEY || ""; this.parallelToolCalls = parallelToolCalls || false; this.model = model || this.model; } 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 (reflected && this.selfReflected >= 3) { (0, logger_1.Logger)({ type: "warn", payload: "Self-Reflection Limit Reached\n\n" }); } if (!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 currentToolCall = null; 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)); // console.log("Data: ", data); if (data.type === "content_block_delta") { const text = data.delta.text; if (text) { controller.enqueue(text); currentMessage += text; continue; } if (currentToolCall && data.delta.type === "input_json_delta") { currentToolCall.input += data.delta.partial_json; } } else if (data.type === "content_block_start") { if (data.content_block.type === "tool_use") { currentToolCall = Object.assign(Object.assign({}, data.content_block), { input: "" }); } } else if (data.type === "content_block_stop") { // if (currentToolCall) { // Logger({ // type: "function", // payload: JSON.stringify({ // name: currentToolCall.name, // params: currentToolCall.input, // }), // }); // const toolMessage = await _this.processingToolCall( // currentToolCall // ); // const toolRequestMessage: Message = { // role: "user", // content: [ // { // type: "tool_result", // tool_use_id: currentToolCall.id, // content: toolMessage, // }, // ], // }; // toolMessages.push(toolRequestMessage, toolMessage); // currentToolCall = null; // } } else if (data.type === "message_stop") { // if (currentMessage && !toolMessages.length) { // _this.history.push({ // role: "assistant", // content: currentMessage, // }); // callback(currentMessage); // } // if (toolMessages.length) { // _this.history.push(...toolMessages); // const newStream = await _this.callClaudeStream( // systemMessage, // _this.history, // callback, // tools // ); // for await (const newPart of newStream) { // controller.enqueue(newPart); // } // } // controller.close(); } else if (data.type === "message_delta") { if (data.delta.stop_reason === "tool_use") { // if (currentToolCall) { // const toolMessage = await _this.processingToolCall({ // ...currentToolCall, // input: JSON.parse(currentToolCall.input), // }); // const toolRequestMessage: Message = { // role: "user", // content: [ // { // type: "tool_result", // tool_use_id: currentToolCall.id, // content: toolMessage, // }, // ], // }; // toolMessages.push(toolRequestMessage, toolMessage); // currentToolCall = null; // } } } } } })); stream.on("end", () => __awaiter(this, void 0, void 0, function* () { var _a, e_1, _b, _c; if (currentToolCall) { const toolMessage = yield _this.processingToolCall(Object.assign(Object.assign({}, currentToolCall), { input: JSON.parse(currentToolCall.input) })); const toolRequestMessage = { role: "assistant", content: [ { type: "text", text: currentMessage, }, { type: "tool_use", id: currentToolCall.id, name: currentToolCall.name, input: JSON.parse(currentToolCall.input), }, ], }; toolMessages.push(toolRequestMessage, toolMessage); currentToolCall = null; } if (currentMessage && !toolMessages.length) { _this.history.push({ role: "assistant", content: currentMessage, }); callback(currentMessage); } if (toolMessages.length) { _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=cclaude_backup.js.map