Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
// Security: local files and videos load only after an explicit action, through the server's exact-message file
// authorization.
import { useEffect, useMemo, useRef, useState } from "react";
import { fromMarkdown } from "mdast-util-from-markdown";
import { ChevronDown, ChevronUp, Download, Film, LoaderCircle, Music, Play, X } from "lucide-react";
import { attachmentBasename, PREVIEW_MAX_BYTES, type TranscriptFileAttachment, type TranscriptImageAttachment } from "@/lib/composer-attachments";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import { windowsPathDestinations } from "../../shared/markdown-windows-paths";
import { decodedLinkPath, localFilePath } from "./ChatMarkdown";
import {
AttachedFileChip,
AttachmentPreviewDialog,
AttachmentThumbnail,
previewImage,
requestMessageFile,
safeDownloadFilename,
type MessageAttachmentContext,
type PreviewImage,
} from "./AttachmentPreview";
import { useRevealOriginalMenu } from "./RevealOriginalMenu";
import { WhatChangedButton } from "./WhatChangedButton";
import { useEditSources } from "@/lib/use-edit-sources";
export interface GalleryFile extends TranscriptFileAttachment {
linked?: boolean;
shownPath?: string;
}
type MarkdownNode = {
type: string;
url?: string;
identifier?: string;
children?: MarkdownNode[];
};
export function fileIdentity(path: string): string {
// Presentation deduplication only, never an authorization check.
if (/^file:\/\//i.test(path)) return localFilePath(path) ?? path;
return decodedLinkPath(path);
}
export function collectMessageFiles(text: string, existingPaths: readonly string[] = []): GalleryFile[] {
const tree: MarkdownNode = fromMarkdown(text, { mdastExtensions: [windowsPathDestinations] });
const definitions = new Map<string, string>();
const links: MarkdownNode[] = [];
const pending = [tree];
while (pending.length) {
const node = pending.pop()!;
if (node.type === "definition" && node.identifier && node.url && !definitions.has(node.identifier)) {
definitions.set(node.identifier, node.url);
} else if (node.type === "link" || node.type === "linkReference") {
if (!node.children?.some((child) => child.type === "image" || child.type === "imageReference")) links.push(node);
}
if (node.children) pending.push(...[...node.children].reverse());
}
return linkedFiles(links.map((node) => node.url ?? (node.identifier ? definitions.get(node.identifier) : undefined)), existingPaths);
}
/** The local files among link targets, as their chips show them, once each; never one of `existingPaths`. */
export function linkedFiles(hrefs: ReadonlyArray<string | undefined>, existingPaths: readonly string[] = []): GalleryFile[] {
const seen = new Set(existingPaths.map(fileIdentity));
return hrefs.flatMap((href) => {
const path = localFilePath(href);
if (!path || !href) return [];
const identity = fileIdentity(href);
if (seen.has(identity)) return [];
seen.add(identity);
// Security: send the authored spelling; the server owns the one decode at its authorization boundary.
return [{ path: href, shownPath: identity, name: safeDownloadFilename(attachmentBasename(identity)), linked: true }];
});
}
export function isVideoAttachment(path: string): boolean {
return /\.(?:mp4|m4v|webm|mov)$/i.test(fileIdentity(path));
}
export function isAudioAttachment(path: string): boolean {
return /\.(?:mp3|m4a|aac|wav|ogg|oga|opus|flac)$/i.test(fileIdentity(path));
}
type MessageAttachmentEntry = { path: string; kind?: string; name?: string; source?: string };
/** A bot's attach-file copy path -> the original it was copied from, for the desktop app's Show in Finder / folder. */
export type AttachmentOriginals = Readonly<Record<string, string>>;
const originalOf = (originals: AttachmentOriginals | undefined, path: string) => originals && Object.hasOwn(originals, path) ? originals[path] : undefined;
const NO_GENERATED_ATTACHMENTS: readonly MessageAttachmentEntry[] = [];
export function splitMessageAttachments(attachments: readonly MessageAttachmentEntry[] = NO_GENERATED_ATTACHMENTS): { images: string[]; files: GalleryFile[]; originals: AttachmentOriginals } {
const images: string[] = [];
const files: GalleryFile[] = [];
const originals: Record<string, string> = {};
for (const attachment of attachments) {
if (attachment.source && (attachment.kind === "file" || attachment.kind === "image")) originals[attachment.path] = attachment.source;
if (attachment.kind === "file") files.push({ path: attachment.path, name: attachment.name || attachmentBasename(attachment.path), private: true });
else if (attachment.kind === "image" || attachment.kind === undefined) images.push(attachment.path);
}
return { images, files, originals };
}
export function replyAttachmentGroup(text: string, attachments: readonly MessageAttachmentEntry[] = NO_GENERATED_ATTACHMENTS): {
images: string[];
files: GalleryFile[];
delivered: Record<string, string>;
originals: AttachmentOriginals;
} {
const attached = splitMessageAttachments(attachments);
const links = collectMessageFiles(text);
const linkedNames = new Set(links.map((link) => link.name));
const delivered: Record<string, string> = {};
const files: GalleryFile[] = [];
const deliveredNames = new Map<string, number>();
for (const file of attached.files) deliveredNames.set(file.name, (deliveredNames.get(file.name) ?? 0) + 1);
for (const file of attached.files) {
if (linkedNames.has(file.name) && deliveredNames.get(file.name) === 1) delivered[file.name] = file.path;
else files.push(file);
}
const taken = new Set([...attached.images, ...attached.files.map((file) => file.path)].map(fileIdentity));
for (const link of links) {
if (Object.hasOwn(delivered, link.name) || taken.has(fileIdentity(link.path))) continue;
if (isVideoAttachment(link.path) || isAudioAttachment(link.path)) files.push(link);
}
return { images: attached.images, files, delivered, originals: attached.originals };
}
export function MessageAttachmentGallery({ text, attachments = NO_GENERATED_ATTACHMENTS, message, eager, className }: {
text: string;
attachments?: readonly MessageAttachmentEntry[];
message: MessageAttachmentContext;
eager?: boolean;
className?: string;
}) {
const attached = useMemo(() => splitMessageAttachments(attachments), [attachments]);
const files = useMemo(
() => [...attached.files, ...collectMessageFiles(text, [...attached.images, ...attached.files.map((file) => file.path)])],
[text, attached],
);
return <AttachmentGallery images={attached.images} files={files} originals={attached.originals} message={message} eager={eager} className={className} />;
}
const MEDIA_STALL_MS = 15_000;
const MEDIA_STALL_RETRIES = 2;
/**
* A proxy or tailnet hiccup can leave a response open with no bytes moving, which would spin "Loading" forever.
* Abort an attempt after a quiet stretch and start a fresh request; the caller's signal still cancels everything.
*/
async function withStallRetry<T>(
signal: AbortSignal,
unavailable: string,
load: (signal: AbortSignal, progress: () => void) => Promise<T>,
): Promise<T> {
for (let attempt = 0; ; attempt++) {
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
const controller = new AbortController();
let stalled = false;
let timer: ReturnType<typeof setTimeout> | undefined;
const progress = () => {
clearTimeout(timer);
timer = setTimeout(() => { stalled = true; controller.abort(); }, MEDIA_STALL_MS);
};
const forward = () => controller.abort();
signal.addEventListener("abort", forward, { once: true });
progress();
try {
return await load(controller.signal, progress);
} catch (reason) {
if (!stalled || signal.aborted) throw reason;
if (attempt >= MEDIA_STALL_RETRIES) throw new Error(unavailable);
} finally {
clearTimeout(timer);
signal.removeEventListener("abort", forward);
}
}
}
const VIDEO_MIMES = ["video/mp4", "video/webm", "video/quicktime", "video/x-m4v"];
/** Security: the server's MIME is required as well as the filename, so arbitrary bytes never become UI. */
export function loadMessageVideo(
file: GalleryFile,
message: MessageAttachmentContext,
signal: AbortSignal,
): Promise<Blob | string> {
return withStallRetry(signal, t("attach.videoUnavailable"), (attempt, progress) => readMessageVideo(file, message, attempt, progress));
}
async function readMessageVideo(
file: GalleryFile,
message: MessageAttachmentContext,
signal: AbortSignal,
progress: () => void,
): Promise<Blob | string> {
const response = await requestMessageFile(file.path, message, signal, { mediaLink: true });
progress();
if (response.headers.get("x-openmausbot-media") === "1") return messageMediaLink(await response.json().catch(() => null), message, VIDEO_MIMES, t("attach.videoUnavailable"));
const mime = response.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase();
if (!mime || !VIDEO_MIMES.includes(mime)) {
await response.body?.cancel();
throw new Error(t("attach.videoUnavailable"));
}
const declared = Number(response.headers.get("content-length"));
if (declared > PREVIEW_MAX_BYTES) {
await response.body?.cancel();
throw previewTooLarge(declared);
}
// Keep this bounded even if a reverse proxy omits Content-Length.
const reader = response.body?.getReader();
if (!reader) throw new Error(t("attach.videoUnavailable"));
const chunks: Uint8Array<ArrayBuffer>[] = [];
let size = 0;
try {
for (;;) {
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
const { value, done } = await reader.read();
if (done) break;
size += value.byteLength;
// Bytes read so far are not the file size, so this says only the preview limit; still no Retry.
if (size > PREVIEW_MAX_BYTES) throw Object.assign(new Error(t("attach.fileTooLarge")), { code: "file_too_large" });
chunks.push(new Uint8Array(value));
progress();
}
} finally {
await reader.cancel().catch(() => undefined);
reader.releaseLock();
}
return new Blob(chunks, { type: mime });
}
/** Security: only this message's own file route, never a URL the response could point anywhere else. */
function messageMediaLink(body: unknown, message: MessageAttachmentContext, mimes: readonly string[], unavailable: string): string {
const { url, mime } = (body ?? {}) as { url?: unknown; mime?: unknown };
const route = `/api/threads/${encodeURIComponent(message.threadId)}/messages/${encodeURIComponent(message.messageId)}/file?media=`;
if (typeof url !== "string" || !url.startsWith(route) || !/^[\w-]+$/.test(url.slice(route.length))
|| typeof mime !== "string" || !mimes.includes(mime)) {
throw new Error(unavailable);
}
return url;
}
function megabytes(bytes: number): string {
return `${Math.ceil(bytes / (1024 * 102.4)) / 10} MB`;
}
/** Only the inline player reads the whole file into memory; the download below still saves it. */
export function previewTooLarge(bytes: number): Error {
const message = t("attach.previewTooLarge", { size: megabytes(bytes), limit: megabytes(PREVIEW_MAX_BYTES) });
return Object.assign(new Error(message), { code: "file_too_large" });
}
function VideoAttachment({ file, message }: { file: GalleryFile; message: MessageAttachmentContext }) {
const [preview, setPreview] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const [tooLarge, setTooLarge] = useState(false);
const request = useRef<AbortController | null>(null);
const playButton = useRef<HTMLButtonElement>(null);
const videoRef = useRef<HTMLVideoElement>(null);
useEffect(() => () => request.current?.abort(), []);
useEffect(() => {
if (preview) videoRef.current?.focus();
return () => { if (preview?.startsWith("blob:")) URL.revokeObjectURL(preview); };
}, [preview]);
const load = async () => {
if (request.current) return;
const controller = new AbortController();
request.current = controller;
setLoading(true);
setError("");
setPreview(null);
try {
const source = await loadMessageVideo(file, message, controller.signal);
if (!controller.signal.aborted) setPreview(typeof source === "string" ? source : URL.createObjectURL(source));
} catch (reason) {
if (!controller.signal.aborted) {
setError(reason instanceof Error ? reason.message : t("attach.videoUnavailable"));
setTooLarge((reason as { code?: unknown } | null)?.code === "file_too_large");
}
} finally {
if (!controller.signal.aborted) setLoading(false);
if (request.current === controller) request.current = null;
}
};
const close = () => {
request.current?.abort();
request.current = null;
setLoading(false);
setPreview(null);
setError("");
setTooLarge(false);
requestAnimationFrame(() => playButton.current?.focus());
};
return (
<div className="min-w-0 overflow-hidden rounded-xl border border-hairline/40 bg-inset/40">
<div className="relative flex aspect-[4/3] items-center justify-center bg-inset">
{preview && error ? (
<div className="flex size-full flex-col items-center justify-center gap-2 p-3 text-center">
<Film size={24} className="text-ink-tertiary" aria-hidden="true" />
<p role="alert" className="text-[12px] text-ink-secondary">{error}</p>
<a href={preview} download={file.name} aria-label={t("attach.downloadAria", { name: file.name })}
className="inline-flex items-center gap-1.5 rounded-lg px-2.5 py-1.5 text-[13px] text-accent-text hover:bg-ink/[0.06] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus">
<Download size={14} aria-hidden="true" />{t("attach.download")}
</a>
</div>
) : preview ? (
<video
ref={videoRef}
src={preview}
tabIndex={0}
controls
playsInline
preload="metadata"
aria-label={t("attach.previewVideo", { name: file.name })}
className="max-h-full max-w-full"
onError={() => setError(t("attach.videoUnavailable"))}
/>
) : (
<button
ref={playButton}
type="button"
disabled={loading || tooLarge}
onClick={() => void load()}
aria-label={t("attach.previewVideo", { name: file.name })}
className={`flex size-full flex-col items-center justify-center gap-2 p-3 text-center text-ink-secondary transition-colors hover:bg-raised/60 hover:text-ink focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-accent/60 ${tooLarge ? "disabled:cursor-default" : "disabled:cursor-wait"} disabled:hover:bg-transparent`}
>
{loading ? <LoaderCircle size={24} className="animate-spin" /> : <Play size={24} />}
<span className="text-[12px]">{loading ? t("attach.videoLoading") : error && !tooLarge ? t("chat.retry") : t("attach.loadVideo")}</span>
{!error && !loading && <span className="text-[11px]">{t("attach.videoHintStreams")}</span>}
</button>
)}
{(preview || loading || error) && (
<button type="button" aria-label={t("attach.closeVideo")} onClick={close} className="absolute right-2 top-2 flex size-7 items-center justify-center rounded-full bg-panel/90 text-ink-secondary hover:bg-raised hover:text-ink focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/60">
<X size={14} />
</button>
)}
{!preview && !loading && <Film size={13} className="pointer-events-none absolute left-2 top-2 text-ink-secondary/50" />}
</div>
{error && !preview && <p role="alert" className="px-3 py-2 text-[11px] text-danger">{error}</p>}
<AttachedFileChip file={file} linked={file.linked} message={message} className="max-w-none rounded-none border-0 border-t border-hairline/30 bg-transparent" />
</div>
);
}
const AUDIO_MIMES = ["audio/mpeg", "audio/mp4", "audio/x-m4a", "audio/aac", "audio/wav", "audio/x-wav", "audio/wave", "audio/ogg", "audio/opus", "audio/flac", "audio/x-flac"];
export function loadMessageAudio(
file: GalleryFile,
message: MessageAttachmentContext,
signal: AbortSignal,
): Promise<Blob | string> {
return withStallRetry(signal, t("attach.audioUnavailable"), (attempt, progress) => readMessageAudio(file, message, attempt, progress));
}
async function readMessageAudio(
file: GalleryFile,
message: MessageAttachmentContext,
signal: AbortSignal,
progress: () => void,
): Promise<Blob | string> {
// A server that links only videos answers 415; it still sends the bytes to a plain request.
const response = await requestMessageFile(file.path, message, signal, { mediaLink: true }).catch((reason: unknown) => {
if ((reason as { status?: unknown } | null)?.status !== 415) throw reason;
return requestMessageFile(file.path, message, signal);
});
progress();
if (response.headers.get("x-openmausbot-media") === "1") return messageMediaLink(await response.json().catch(() => null), message, AUDIO_MIMES, t("attach.audioUnavailable"));
const mime = response.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase();
if (!mime || !AUDIO_MIMES.includes(mime)) {
await response.body?.cancel();
throw new Error(t("attach.audioUnavailable"));
}
const declared = Number(response.headers.get("content-length"));
if (declared > PREVIEW_MAX_BYTES) {
await response.body?.cancel();
throw new Error(t("attach.fileTooLarge"));
}
const reader = response.body?.getReader();
if (!reader) throw new Error(t("attach.audioUnavailable"));
const chunks: Uint8Array<ArrayBuffer>[] = [];
let size = 0;
try {
for (;;) {
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
const { value, done } = await reader.read();
if (done) break;
size += value.byteLength;
if (size > PREVIEW_MAX_BYTES) throw new Error(t("attach.fileTooLarge"));
chunks.push(new Uint8Array(value));
progress();
}
} finally {
await reader.cancel().catch(() => undefined);
reader.releaseLock();
}
return new Blob(chunks, { type: mime });
}
function AudioAttachment({ file, message }: { file: GalleryFile; message: MessageAttachmentContext }) {
const [src, setSrc] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const request = useRef<AbortController | null>(null);
useEffect(() => () => request.current?.abort(), []);
useEffect(() => () => { if (src?.startsWith("blob:")) URL.revokeObjectURL(src); }, [src]);
const load = async () => {
if (request.current) return;
const controller = new AbortController();
request.current = controller;
setLoading(true);
setError("");
try {
const source = await loadMessageAudio(file, message, controller.signal);
if (!controller.signal.aborted) setSrc(typeof source === "string" ? source : URL.createObjectURL(source));
} catch (reason) {
if (!controller.signal.aborted) setError(reason instanceof Error ? reason.message : t("attach.audioUnavailable"));
} finally {
if (!controller.signal.aborted) setLoading(false);
if (request.current === controller) request.current = null;
}
};
return (
<div className="inline-flex w-72 max-w-full flex-col gap-1.5 rounded-2xl border border-hairline/30 bg-inset/60 px-3 py-2.5 text-left">
<div className="flex items-center gap-2">
{src ? <Music size={14} className="shrink-0 text-accent" aria-hidden="true" />
: (
<button type="button" disabled={loading} onClick={() => void load()} aria-label={t("attach.previewAudio", { name: file.name })}
className="flex size-7 shrink-0 items-center justify-center rounded-full bg-accent/15 text-accent transition-colors hover:bg-accent/25 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/60 disabled:cursor-wait">
{loading ? <LoaderCircle size={14} className="animate-spin" /> : <Play size={13} fill="currentColor" />}
</button>
)}
<span className="min-w-0 flex-1 truncate text-[12px] font-medium text-ink" title={file.name}>{file.name}</span>
{src && <a href={src} download={file.name} aria-label={t("attach.saveAria", { name: file.name })} title={t("attach.saveAria", { name: file.name })}
className="shrink-0 rounded-lg p-1 text-ink-secondary hover:bg-raised hover:text-ink"><Download size={14} /></a>}
</div>
{src && !error && <audio src={src} controls autoPlay preload="metadata" aria-label={t("attach.previewAudio", { name: file.name })} onError={() => { setSrc(null); setError(t("attach.audioUnavailable")); }} className="h-9 w-full" />}
{error && <p role="alert" className="text-[11px] text-danger">{error}</p>}
</div>
);
}
type GalleryItem = { key: string; source?: string } & (
| { kind: "image"; image: PreviewImage }
| { kind: "video" | "audio" | "file"; file: GalleryFile }
);
const BENEATH_LIMIT = 6;
export function AttachmentGallery({ images = [], files = [], originals, message, eager = false, beneath = false, className }: {
images?: Array<string | TranscriptImageAttachment>;
files?: GalleryFile[];
originals?: AttachmentOriginals;
message?: MessageAttachmentContext;
eager?: boolean;
beneath?: boolean;
className?: string;
}) {
const [expanded, setExpanded] = useState(false);
const [selected, setSelected] = useState<PreviewImage | null>(null);
const items = useMemo(() => {
const seen = new Set<string>();
const result: GalleryItem[] = [];
for (const reference of images) {
const path = typeof reference === "string" ? reference : reference.path;
const key = fileIdentity(path);
if (seen.has(key)) continue;
seen.add(key);
const image = typeof reference === "string" || reference.private
? previewImage(path, typeof reference === "string" ? undefined : reference.name)
: null;
const source = originalOf(originals, path);
if (image) result.push({ key, source, kind: "image", image });
else result.push({ key, source, kind: "file", file: typeof reference === "string" ? { path, name: attachmentBasename(path) } : reference });
}
for (const file of files) {
const key = fileIdentity(file.path);
if (seen.has(key)) continue;
seen.add(key);
const trusted = Boolean(message && (file.private || file.linked));
const source = originalOf(originals, file.path) ?? (message && file.linked ? key : undefined);
result.push({ key, source, kind: trusted && isVideoAttachment(file.path) ? "video" : trusted && isAudioAttachment(file.path) ? "audio" : "file", file });
}
return result;
}, [images, files, originals, message]);
const reveal = useRevealOriginalMenu();
const edits = useEditSources(message, files);
if (!items.length) return null;
const limit = beneath ? BENEATH_LIMIT : 4;
const shown = expanded ? items : items.slice(0, limit);
const media = shown.filter((item) => item.kind === "image" || item.kind === "video");
const documents = shown.filter((item) => item.kind === "file" || item.kind === "audio");
const previews = items.flatMap((item) => item.kind === "image" ? [item.image] : []);
const label = items.length === 1 ? t("attach.gallerySingle") : t("attach.galleryCount", { count: items.length });
const dialog = selected && previews.some((image) => image.src === selected.src) && (
<AttachmentPreviewDialog image={selected} images={previews} initialIndex={previews.findIndex((image) => image.src === selected.src)} onClose={() => setSelected(null)} />
);
if (beneath) {
const thumbs = shown.filter((item) => item.kind === "image");
const videos = shown.filter((item) => item.kind === "video");
const rest = shown.filter((item) => item.kind === "file" || item.kind === "audio");
return (
<section aria-label={label} className={cn("mt-2.5 flex max-w-full flex-col items-start gap-2 text-start whitespace-normal", className)}>
{thumbs.length > 0 && (
<div className="flex max-w-full flex-wrap gap-2">
{thumbs.map((item) => item.kind === "image" && (
<div key={item.key} className="min-w-0" title={item.image.name} {...reveal.targetProps(item.source)}>
<AttachmentThumbnail key={item.image.src} image={item.image} eager={eager} onPreview={() => setSelected(item.image)} className="h-24 w-auto max-w-48 rounded-2xl border-0" />
</div>
))}
</div>
)}
{videos.length > 0 && message && (
<div className={cn("grid w-[min(34rem,70vw)] max-w-full gap-2", videos.length === 1 ? "grid-cols-1" : "grid-cols-2")}>
{videos.map((item) => item.kind === "video" && (
<span key={`${message.threadId}:${message.messageId}:${item.key}`} className="contents" {...reveal.targetProps(item.source)}><VideoAttachment file={item.file} message={message} /></span>
))}
</div>
)}
{(rest.length > 0 || items.length > limit) && (
<div className="flex max-w-full flex-wrap items-start gap-1.5">
{rest.map((item) => item.kind === "audio" && message
? <span key={`${message.threadId}:${message.messageId}:${item.key}`} className="contents" {...reveal.targetProps(item.source)}><AudioAttachment file={item.file} message={message} /></span>
: item.kind === "file" && <span key={item.key} className="contents" {...reveal.targetProps(item.source)}><AttachedFileChip file={item.file} linked={item.file.linked} message={message} compact />{message && edits.has(item.file.path) && <WhatChangedButton path={item.file.path} name={item.file.name} message={message} source={edits.get(item.file.path)!} compact />}</span>)}
{items.length > limit && (
<button type="button" onClick={() => setExpanded(!expanded)} aria-expanded={expanded} className="inline-flex h-7 items-center rounded-full px-2.5 text-[13px] leading-none text-ink-secondary transition-colors hover:bg-ink/[0.06] hover:text-ink focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus">
{expanded ? t("attach.showLess") : t("attach.moreCount", { count: items.length - limit })}
</button>
)}
</div>
)}
{dialog}{reveal.menu}
</section>
);
}
return (
<section aria-label={label} className={cn("mb-1.5 w-[min(34rem,70vw)] max-w-full space-y-1.5 text-left whitespace-normal", className)}>
{media.length > 0 && (
<div className={cn("grid gap-2", media.length === 1 ? "grid-cols-1" : "grid-cols-2")}>
{media.map((item) => item.kind === "image" ? (
<div key={item.key} className="min-w-0" title={item.image.name} {...reveal.targetProps(item.source)}>
<AttachmentThumbnail key={item.image.src} image={item.image} eager={eager} onPreview={() => setSelected(item.image)} className="max-h-72 rounded-2xl border-0 bg-transparent" />
</div>
) : item.kind === "video" && message ? (
<span key={`${message.threadId}:${message.messageId}:${item.key}`} className="contents" {...reveal.targetProps(item.source)}><VideoAttachment file={item.file} message={message} /></span>
) : null)}
</div>
)}
{documents.length > 0 && (
<div className="flex flex-col items-start gap-1.5">
{documents.map((item) => item.kind === "audio" && message
? <span key={`${message.threadId}:${message.messageId}:${item.key}`} className="contents" {...reveal.targetProps(item.source)}><AudioAttachment file={item.file} message={message} /></span>
: item.kind === "file" && <span key={item.key} className={message && edits.has(item.file.path) ? "flex max-w-full items-center gap-1" : "contents"} {...reveal.targetProps(item.source)}><AttachedFileChip file={item.file} linked={item.file.linked} message={message} className="max-w-none rounded-xl border-hairline/25 bg-transparent" />{message && edits.has(item.file.path) && <WhatChangedButton path={item.file.path} name={item.file.name} message={message} source={edits.get(item.file.path)!} />}</span>)}
</div>
)}
{items.length > limit && (
<button type="button" onClick={() => setExpanded(!expanded)} aria-expanded={expanded} className="flex min-h-8 items-center gap-1 rounded-lg px-1 py-1 text-[11px] text-ink-secondary transition-colors hover:text-ink focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/60">
{expanded ? <ChevronUp size={13} /> : <ChevronDown size={13} />}
{expanded ? t("attach.showLess") : t("attach.showMore", { count: items.length - limit })}
</button>
)}
{dialog}{reveal.menu}
</section>
);
}