UNPKG

@assistant-ui/react

Version:

Typescript/React library for AI Chat

136 lines (135 loc) 4.74 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/external-store/ThreadMessageLike.tsx var ThreadMessageLike_exports = {}; __export(ThreadMessageLike_exports, { fromThreadMessageLike: () => fromThreadMessageLike }); module.exports = __toCommonJS(ThreadMessageLike_exports); var import_internal = require("../../internal.js"); var import_parse_partial_json = require("../../utils/json/parse-partial-json.js"); var fromThreadMessageLike = (like, fallbackId, fallbackStatus) => { const { role, id, createdAt, attachments, status, metadata } = like; const common = { id: id ?? fallbackId, createdAt: createdAt ?? /* @__PURE__ */ new Date() }; const content = typeof like.content === "string" ? [{ type: "text", text: like.content }] : like.content; if (role !== "user" && attachments) throw new Error("attachments are only supported for user messages"); if (role !== "assistant" && status) throw new Error("status is only supported for assistant messages"); if (role !== "assistant" && metadata?.steps) throw new Error("metadata.steps is only supported for assistant messages"); switch (role) { case "assistant": return { ...common, role, content: content.map((part) => { const type = part.type; switch (type) { case "text": case "reasoning": if (part.text.trim().length === 0) return null; return part; case "file": case "source": return part; case "tool-call": { if (part.args) { return { ...part, toolCallId: part.toolCallId ?? "tool-" + (0, import_internal.generateId)(), args: part.args, argsText: JSON.stringify(part.args) }; } return { ...part, toolCallId: part.toolCallId ?? "tool-" + (0, import_internal.generateId)(), args: part.args ?? (0, import_parse_partial_json.parsePartialJson)(part.argsText ?? "{}"), argsText: part.argsText ?? "" }; } default: { const unhandledType = type; throw new Error( `Unsupported assistant content part type: ${unhandledType}` ); } } }).filter((c) => !!c), status: status ?? fallbackStatus, metadata: { unstable_annotations: metadata?.unstable_annotations ?? [], unstable_data: metadata?.unstable_data ?? [], custom: metadata?.custom ?? {}, steps: metadata?.steps ?? [] } }; case "user": return { ...common, role, content: content.map((part) => { const type = part.type; switch (type) { case "text": case "image": case "audio": case "file": return part; default: { const unhandledType = type; throw new Error( `Unsupported user content part type: ${unhandledType}` ); } } }), attachments: attachments ?? [], metadata: { custom: metadata?.custom ?? {} } }; case "system": if (content.length !== 1 || content[0].type !== "text") throw new Error( "System messages must have exactly one text content part." ); return { ...common, role, content, metadata: { custom: metadata?.custom ?? {} } }; default: { const unsupportedRole = role; throw new Error(`Unknown message role: ${unsupportedRole}`); } } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { fromThreadMessageLike }); //# sourceMappingURL=ThreadMessageLike.js.map