UNPKG

aiwrapper

Version:

A Universal AI Wrapper for JavaScript & TypeScript

247 lines (246 loc) 8.52 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { LangMessage } from "../messages.js"; class AnthropicStreamHandler { constructor(messages, onResult) { __publicField(this, "messages"); __publicField(this, "onResult"); __publicField(this, "currentAssistantMessage"); __publicField(this, "textBlocks", /* @__PURE__ */ new Map()); __publicField(this, "reasoningBlocks", /* @__PURE__ */ new Map()); __publicField(this, "toolBlocks", /* @__PURE__ */ new Map()); __publicField(this, "toolArgBuffers", /* @__PURE__ */ new Map()); this.messages = messages; this.onResult = onResult; } setOnResult(onResult) { this.onResult = onResult; } handleEvent(data) { var _a; if (!data) return; let updated = false; switch (data.type) { case "message_start": updated = this.handleMessageStart(data); break; case "content_block_start": updated = this.handleContentBlockStart(data); break; case "content_block_delta": updated = this.handleContentBlockDelta(data); break; case "content_block_stop": updated = false; break; case "message_delta": break; case "message_stop": updated = this.handleMessageStop(); break; default: break; } if (updated && this.currentAssistantMessage) { (_a = this.onResult) == null ? void 0 : _a.call(this, this.currentAssistantMessage); } } ensureAssistantMessage() { if (!this.currentAssistantMessage) { this.currentAssistantMessage = new LangMessage("assistant", []); this.messages.push(this.currentAssistantMessage); } return this.currentAssistantMessage; } handleMessageStart(data) { var _a, _b; const message = this.ensureAssistantMessage(); this.textBlocks.clear(); this.reasoningBlocks.clear(); this.toolBlocks.clear(); this.toolArgBuffers.clear(); if ((_a = data.message) == null ? void 0 : _a.id) { message.meta = __spreadProps(__spreadValues({}, (_b = message.meta) != null ? _b : {}), { anthropicMessageId: data.message.id }); } return false; } handleContentBlockStart(data) { const block = data.content_block; if (!block) return false; const index = typeof data.index === "number" ? data.index : 0; const message = this.ensureAssistantMessage(); switch (block.type) { case "text": { const textItem = this.getOrCreateTextItem(index, message); if (typeof block.text === "string" && block.text.length > 0) { textItem.text += block.text; return true; } return false; } case "thinking": { const reasoningItem = this.getOrCreateReasoningItem(index, message); if (typeof block.thinking === "string" && block.thinking.length > 0) { reasoningItem.text += block.thinking; return true; } return false; } case "tool_use": { const id = typeof block.id === "string" && block.id.length > 0 ? block.id : `tool_${index}`; const name = typeof block.name === "string" ? block.name : ""; const toolItem = this.createToolItem(index, id, name, message); if (block.input && typeof block.input === "object") { toolItem.arguments = block.input; } else if (typeof block.input === "string") { this.toolArgBuffers.set(id, block.input); this.tryParseToolArguments(id, toolItem); } return true; } default: return false; } } handleContentBlockDelta(data) { var _a, _b; const delta = data.delta; if (!delta) return false; const index = typeof data.index === "number" ? data.index : 0; const message = this.ensureAssistantMessage(); if (delta.type === "text_delta" || typeof delta.text === "string") { const text = (_a = delta.text) != null ? _a : ""; if (text.length === 0) return false; const textItem = this.getOrCreateTextItem(index, message); textItem.text += text; return true; } if (delta.type === "thinking_delta" || typeof delta.thinking === "string") { const thinking = (_b = delta.thinking) != null ? _b : ""; if (thinking.length === 0) return false; const reasoningItem = this.getOrCreateReasoningItem(index, message); reasoningItem.text += thinking; return true; } if (delta.type === "input_json_delta" || delta.type === "tool_use_delta" || typeof delta.partial_json === "string" || typeof delta.input_json_delta === "object" || typeof delta.text === "string") { return this.applyToolDelta(index, delta); } return false; } handleMessageStop() { this.finalizeToolArguments(); if (this.messages.length > 0) { this.messages.finished = true; } this.currentAssistantMessage = void 0; return true; } getOrCreateTextItem(index, message) { let item = this.textBlocks.get(index); if (!item) { item = { type: "text", text: "" }; message.items.push(item); this.textBlocks.set(index, item); } return item; } getOrCreateReasoningItem(index, message) { let item = this.reasoningBlocks.get(index); if (!item) { item = { type: "reasoning", text: "" }; message.items.push(item); this.reasoningBlocks.set(index, item); } return item; } createToolItem(index, id, name, message) { const toolItem = { type: "tool", callId: id, name, arguments: {} }; message.items.push(toolItem); this.toolBlocks.set(index, toolItem); if (!this.toolArgBuffers.has(id)) { this.toolArgBuffers.set(id, ""); } return toolItem; } applyToolDelta(index, delta) { var _a, _b, _c; const toolItem = this.toolBlocks.get(index); if (!toolItem) return false; const callId = toolItem.callId; if (typeof delta.partial_json === "string") { const buffer = ((_a = this.toolArgBuffers.get(callId)) != null ? _a : "") + delta.partial_json; this.toolArgBuffers.set(callId, buffer); this.tryParseToolArguments(callId, toolItem); return true; } if (typeof delta.text === "string") { const buffer = ((_b = this.toolArgBuffers.get(callId)) != null ? _b : "") + delta.text; this.toolArgBuffers.set(callId, buffer); this.tryParseToolArguments(callId, toolItem); return true; } if (typeof delta.input_json_delta === "object" && delta.input_json_delta !== null) { const currentArgs = __spreadValues({}, (_c = toolItem.arguments) != null ? _c : {}); Object.assign(currentArgs, delta.input_json_delta); toolItem.arguments = currentArgs; return true; } return false; } tryParseToolArguments(callId, toolItem) { const buffer = this.toolArgBuffers.get(callId); if (!buffer || buffer.trim().length === 0) { return; } try { toolItem.arguments = JSON.parse(buffer); } catch (e) { } } finalizeToolArguments() { for (const [callId, buffer] of this.toolArgBuffers.entries()) { const toolItem = Array.from(this.toolBlocks.values()).find((item) => item.callId === callId); if (!toolItem) continue; if (!buffer || buffer.trim().length === 0) { continue; } try { toolItem.arguments = JSON.parse(buffer); } catch (e) { toolItem.arguments = {}; } } this.toolArgBuffers.clear(); } } export { AnthropicStreamHandler }; //# sourceMappingURL=anthropic-stream-handler.js.map