@assistant-ui/react
Version:
React components for AI chat.
78 lines • 2.32 kB
JavaScript
// src/runtimes/external-store/ThreadMessageLike.tsx
var fromThreadMessageLike = (like, fallbackId, fallbackStatus) => {
const { role, id, createdAt, attachments, status } = 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");
switch (role) {
case "assistant":
return {
...common,
role,
content: content.map((part) => {
const type = part.type;
switch (type) {
case "text":
if (part.text.trim().length === 0) return null;
return part;
case "ui":
return part;
case "tool-call": {
if ("argsText" in part) return part;
return {
...part,
argsText: JSON.stringify(part.args)
};
}
default: {
const unhandledType = type;
throw new Error(`Unknown content part type: ${unhandledType}`);
}
}
}).filter((c) => !!c),
status: status ?? fallbackStatus
};
case "user":
return {
...common,
role,
content: content.map((part) => {
const type = part.type;
switch (type) {
case "text":
case "ui":
case "image":
case "audio":
return part;
default: {
const unhandledType = type;
throw new Error(`Unknown content part type: ${unhandledType}`);
}
}
}),
attachments: attachments ?? []
};
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
};
default: {
const unsupportedRole = role;
throw new Error(`Unknown message role: ${unsupportedRole}`);
}
}
};
export {
fromThreadMessageLike
};
//# sourceMappingURL=ThreadMessageLike.mjs.map