import { For, Show } from "solid-js" import type { MessageInfo, ClientPart } from "../types/message" import { partHasRenderableText } from "../types/message" import type { MessageRecord } from "../stores/message-v2/types" import MessagePart from "./message-part" interface MessageItemProps { record: MessageRecord messageInfo?: MessageInfo instanceId: string sessionId: string isQueued?: boolean parts: ClientPart[] onRevert?: (messageId: string) => void onFork?: (messageId?: string) => void showAgentMeta?: boolean onContentRendered?: () => void } export default function MessageItem(props: MessageItemProps) { const isUser = () => props.record.role === "user" const createdTimestamp = () => props.messageInfo?.time?.created ?? props.record.createdAt const timestamp = () => { const date = new Date(createdTimestamp()) return date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }) } const timestampIso = () => new Date(createdTimestamp()).toISOString() type FilePart = Extract & { url?: string mime?: string filename?: string } const messageParts = () => props.parts const fileAttachments = () => messageParts().filter((part): part is FilePart => part?.type === "file" && typeof (part as FilePart).url === "string") const getAttachmentName = (part: FilePart) => { if (part.filename && part.filename.trim().length > 0) { return part.filename } const url = part.url || "" if (url.startsWith("data:")) { return "attachment" } try { const parsed = new URL(url) const segments = parsed.pathname.split("/") return segments.pop() || "attachment" } catch (error) { const fallback = url.split("/").pop() return fallback && fallback.length > 0 ? fallback : "attachment" } } const isImageAttachment = (part: FilePart) => { if (part.mime && typeof part.mime === "string" && part.mime.startsWith("image/")) { return true } return typeof part.url === "string" && part.url.startsWith("data:image/") } const handleAttachmentDownload = async (part: FilePart) => { const url = part.url if (!url) return const filename = getAttachmentName(part) const directDownload = (href: string) => { const anchor = document.createElement("a") anchor.href = href anchor.download = filename anchor.target = "_blank" anchor.rel = "noopener" document.body.appendChild(anchor) anchor.click() document.body.removeChild(anchor) } if (url.startsWith("data:")) { directDownload(url) return } if (url.startsWith("file://")) { window.open(url, "_blank", "noopener") return } try { const response = await fetch(url) if (!response.ok) throw new Error(`Failed to fetch attachment: ${response.status}`) const blob = await response.blob() const objectUrl = URL.createObjectURL(blob) directDownload(objectUrl) URL.revokeObjectURL(objectUrl) } catch (error) { directDownload(url) } } const errorMessage = () => { const info = props.messageInfo if (!info || info.role !== "assistant" || !info.error) return null const error = info.error if (error.name === "ProviderAuthError") { return error.data?.message || "Authentication error" } if (error.name === "MessageOutputLengthError") { return "Message output length exceeded" } if (error.name === "MessageAbortedError") { return "Request was aborted" } if (error.name === "UnknownError") { return error.data?.message || "Unknown error occurred" } return null } const hasContent = () => { if (errorMessage() !== null) { return true } return messageParts().some((part) => partHasRenderableText(part)) } const isGenerating = () => { const info = props.messageInfo return !hasContent() && info && info.role === "assistant" && info.time.completed !== undefined && info.time.completed === 0 } const handleRevert = () => { if (props.onRevert && isUser()) { props.onRevert(props.record.id) } } if (!isUser() && !hasContent()) { return null } const containerClass = () => isUser() ? "message-item-base bg-[var(--message-user-bg)] border-l-4 border-[var(--message-user-border)]" : "message-item-base assistant-message bg-[var(--message-assistant-bg)] border-l-4 border-[var(--message-assistant-border)]" const speakerLabel = () => (isUser() ? "You" : "Assistant") const agentIdentifier = () => { if (isUser()) return "" const info = props.messageInfo if (!info || info.role !== "assistant") return "" return info.mode || "" } const modelIdentifier = () => { if (isUser()) return "" const info = props.messageInfo if (!info || info.role !== "assistant") return "" const modelID = info.modelID || "" const providerID = info.providerID || "" if (modelID && providerID) return `${providerID}/${modelID}` return modelID } const agentMeta = () => { if (isUser() || !props.showAgentMeta) return "" const segments: string[] = [] const agent = agentIdentifier() const model = modelIdentifier() if (agent) { segments.push(`Agent: ${agent}`) } if (model) { segments.push(`Model: ${model}`) } return segments.join(" • ") } return (
{speakerLabel()} {(meta) => {meta()}}
QUEUED
⚠️ {errorMessage()}
Generating...
{(part) => ( )} 0}>
{(attachment) => { const name = getAttachmentName(attachment) const isImage = isImageAttachment(attachment) return (
}> {name} {name}
{name}
) }}
Sending...
⚠ Message failed to send
) }