UNPKG

@assistant-ui/react

Version:

React components for AI chat.

144 lines (143 loc) 5.11 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/runtimes/edge/streams/assistantDecoderStream.ts var assistantDecoderStream_exports = {}; __export(assistantDecoderStream_exports, { assistantDecoderStream: () => assistantDecoderStream }); module.exports = __toCommonJS(assistantDecoderStream_exports); var import_AssistantStreamChunkType = require("./AssistantStreamChunkType.cjs"); function assistantDecoderStream() { const toolCallNames = /* @__PURE__ */ new Map(); let currentToolCall; return new TransformStream({ transform({ type, value }, controller) { if (currentToolCall && type !== import_AssistantStreamChunkType.AssistantStreamChunkType.ToolCallDelta && type !== import_AssistantStreamChunkType.AssistantStreamChunkType.Error) { controller.enqueue({ type: "tool-call", toolCallType: "function", toolCallId: currentToolCall.id, toolName: currentToolCall.name, args: currentToolCall.argsText }); currentToolCall = void 0; } switch (type) { case import_AssistantStreamChunkType.AssistantStreamChunkType.TextDelta: { controller.enqueue({ type: "text-delta", textDelta: value }); break; } case import_AssistantStreamChunkType.AssistantStreamChunkType.ToolCallBegin: { const { toolCallId: id, toolName: name } = value; toolCallNames.set(id, name); currentToolCall = { id, name, argsText: "" }; controller.enqueue({ type: "tool-call-delta", toolCallType: "function", toolCallId: id, toolName: name, argsTextDelta: "" }); break; } case import_AssistantStreamChunkType.AssistantStreamChunkType.ToolCallDelta: { const { toolCallId, argsTextDelta } = value; const toolName = toolCallNames.get(toolCallId); if (currentToolCall?.id === toolCallId) { currentToolCall.argsText += argsTextDelta; } controller.enqueue({ type: "tool-call-delta", toolCallType: "function", toolCallId, toolName, argsTextDelta }); break; } case import_AssistantStreamChunkType.AssistantStreamChunkType.ToolCallResult: { controller.enqueue({ type: "tool-result", toolCallType: "function", toolCallId: value.toolCallId, toolName: toolCallNames.get(value.toolCallId), result: value.result }); break; } case import_AssistantStreamChunkType.AssistantStreamChunkType.Finish: { controller.enqueue({ type: "finish", ...value }); break; } case import_AssistantStreamChunkType.AssistantStreamChunkType.Error: { controller.enqueue({ type: "error", error: value }); break; } case import_AssistantStreamChunkType.AssistantStreamChunkType.ToolCall: { const { toolCallId, toolName, args } = value; toolCallNames.set(toolCallId, toolName); const argsText = JSON.stringify(args); controller.enqueue({ type: "tool-call-delta", toolCallType: "function", toolCallId, toolName, argsTextDelta: argsText }); controller.enqueue({ type: "tool-call", toolCallType: "function", toolCallId, toolName, args: argsText }); break; } case import_AssistantStreamChunkType.AssistantStreamChunkType.StepFinish: { controller.enqueue({ type: "step-finish", ...value }); break; } // TODO case import_AssistantStreamChunkType.AssistantStreamChunkType.Data: break; default: { const unhandledType = type; throw new Error(`Unhandled chunk type: ${unhandledType}`); } } } }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { assistantDecoderStream }); //# sourceMappingURL=assistantDecoderStream.js.map