UNPKG

@assistant-ui/react

Version:

Typescript/React library for AI Chat

111 lines 3.59 kB
// src/runtimes/external-store/ThreadMessageLike.tsx import { generateId } from "../../internal.mjs"; import { parsePartialJson } from "../../utils/json/parse-partial-json.mjs"; 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-" + generateId(), args: part.args, argsText: JSON.stringify(part.args) }; } return { ...part, toolCallId: part.toolCallId ?? "tool-" + generateId(), args: part.args ?? 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}`); } } }; export { fromThreadMessageLike }; //# sourceMappingURL=ThreadMessageLike.mjs.map