UNPKG

aiwrapper

Version:

A Universal AI Wrapper for JavaScript & TypeScript

267 lines (266 loc) 9.07 kB
var __defProp = Object.defineProperty; 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 __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { LangMessage } from "../../messages.js"; class OpenAIResponseStreamHandler { constructor(messages, onResult) { __publicField(this, "id"); __publicField(this, "items"); __publicField(this, "itemIdToMessageItemIndex", /* @__PURE__ */ new Map()); __publicField(this, "itemIdToSummaryIndex", /* @__PURE__ */ new Map()); __publicField(this, "newMessage"); __publicField(this, "messages"); __publicField(this, "onResult"); this.items = []; this.messages = messages; this.onResult = onResult; } handleEvent(data) { var _a; if (!("type" in data)) { console.warn("Unknown data from server:", data); return; } if (!("type" in data)) { console.warn("Unknown data from server:", data); return; } switch (data.type) { case "response.created": this.handleNewResponse(data); break; case "response.output_item.added": this.handleNewItem(data); break; case "response.output_item.done": this.handleItemFinished(data); break; case "response.image_generation_call.partial_image": break; case "response.output_text.delta": this.applyTextDelta(data); break; case "response.function_call_arguments.delta": this.applyToolArgsDelta(data); break; case "response.reasoning_summary_text.delta": this.applyReasoningSummaryTextDelta(data); break; } (_a = this.onResult) == null ? void 0 : _a.call(this, this.newMessage); } handleNewResponse(data) { this.id = data.response.id; this.newMessage = new LangMessage("assistant", []); this.newMessage.meta = { openaiResponseId: this.id }; this.messages.push(this.newMessage); } handleNewItem(data) { var _a; const itemType = data.item.type; if (!this.newMessage) { console.warn("Received item without an active assistant message:", data); return; } switch (itemType) { case "message": { const textItem = { type: "text", text: typeof data.item.text === "string" ? data.item.text : "" }; this.newMessage.items.push(textItem); } break; case "function_call": { const toolItem = { type: "tool", name: typeof data.item.name === "string" ? data.item.name : "", callId: typeof data.item.call_id === "string" ? data.item.call_id : "", arguments: data.item.arguments && typeof data.item.arguments === "object" ? data.item.arguments : {} }; this.newMessage.items.push(toolItem); } break; case "apply_patch_call": { const toolItem = { type: "tool", name: "apply_patch", callId: typeof data.item.call_id === "string" ? data.item.call_id : "", arguments: { operation: data.item.operation || {}, status: data.item.status || "in_progress" } }; this.newMessage.items.push(toolItem); } break; case "image_generation_call": { const imageItem = { type: "image" }; if (typeof data.item.url === "string") { imageItem.url = data.item.url; } if (typeof data.item.b64_json === "string") { imageItem.base64 = data.item.b64_json; } if (typeof data.item.output_format === "string" || typeof data.item.format === "string") { imageItem.mimeType = (_a = data.item.output_format || data.item.format) != null ? _a : void 0; } this.newMessage.items.push(imageItem); } break; case "reasoning": { const reasoningItem = { type: "reasoning", text: "" }; this.newMessage.items.push(reasoningItem); } break; default: console.warn("Unknown item type:", itemType); return; } this.itemIdToMessageItemIndex.set(data.item.id, this.newMessage.items.length - 1); } handleItemFinished(data) { const itemFromResponse = data.item; const messageIndex = this.itemIdToMessageItemIndex.get(itemFromResponse.id); if (messageIndex === void 0) { console.warn("Unknown message index for item:", itemFromResponse); return; } const messageItem = this.newMessage.items[messageIndex]; this.applyItemToMessage(itemFromResponse, messageItem); } applyItemToMessage(resItem, messageItem) { switch (resItem.type) { case "message": this.applyTextMessage(resItem, messageItem); break; case "function_call": this.applyFunctionCall(resItem, messageItem); break; case "apply_patch_call": this.applyPatchCall(resItem, messageItem); break; case "image_generation_call": this.applyImageGenerationCall(resItem, messageItem); break; } } applyTextMessage(res, target) { target.text = res.content.map((part) => part.text).join("\n\n"); } applyFunctionCall(res, target) { target.callId = res.call_id; target.name = res.name; try { target.arguments = JSON.parse(res.arguments); } catch (error) { console.error("Error parsing arguments for function call:", error); target.arguments = {}; } } applyPatchCall(res, target) { target.callId = res.call_id; target.name = "apply_patch"; target.arguments = { operation: res.operation || {}, status: res.status || "completed" }; } applyImageGenerationCall(res, target) { var _a, _b; if (typeof res.url === "string") { target.url = res.url; } const base64 = res.b64_json || res.base64 || res.result; if (typeof base64 === "string") { target.base64 = base64; } const format = res.mimeType || res.mime_type || res.output_format || res.format; if (typeof format === "string") { target.mimeType = format.includes("/") ? format : `image/${format}`; } if (typeof res.width === "number") { target.width = res.width; } if (typeof res.height === "number") { target.height = res.height; } if (res.metadata || res.revised_prompt || res.background || res.quality || res.size || res.status) { const metadata = __spreadValues(__spreadValues({}, (_a = target.metadata) != null ? _a : {}), (_b = res.metadata) != null ? _b : {}); if (res.revised_prompt) metadata.revisedPrompt = res.revised_prompt; if (res.background) metadata.background = res.background; if (res.quality) metadata.quality = res.quality; if (res.size) metadata.size = res.size; if (res.status) metadata.status = res.status; target.metadata = metadata; } } getNewMessageItem(itemId) { const contentIndex = this.itemIdToMessageItemIndex.get(itemId); if (contentIndex === void 0) { return void 0; } return this.newMessage.items[contentIndex]; } applyTextDelta(data) { const messageItem = this.getNewMessageItem(data.item_id); if (messageItem === void 0) { console.warn("Unknown item:", data.item_id); return; } const delta = data.delta; messageItem.text += delta; } applyToolArgsDelta(data) { const messageItem = this.getNewMessageItem(data.item_id); if (messageItem === void 0) { console.warn("Unknown item:", data.item_id); return; } const delta = data.delta; } applyReasoningSummaryTextDelta(data) { const thinkingItem = this.getNewMessageItem(data.item_id); if (thinkingItem === void 0) { console.warn("Unknown reasoning item:", data.item_id); return; } const delta = data.delta; if (typeof data.summary_index === "number") { const previousIndex = this.itemIdToSummaryIndex.get(data.item_id); if (previousIndex !== void 0 && data.summary_index > previousIndex) { thinkingItem.text += "\n\n"; } this.itemIdToSummaryIndex.set(data.item_id, data.summary_index); } thinkingItem.text += delta; } } export { OpenAIResponseStreamHandler }; //# sourceMappingURL=openai-responses-stream-handler.js.map