Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat: user-uploaded image thumbnails with flicker-free transitions

Guse12af1e550f2
diff --git a/src/web/src/app/api/conversations/[id]/messages/route.ts b/src/web/src/app/api/conversations/[id]/messages/route.ts
index dd51ea370..c8b9b0a81 100644
--- a/src/web/src/app/api/conversations/[id]/messages/route.ts
+++ b/src/web/src/app/api/conversations/[id]/messages/route.ts
@@ -72,6 +72,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
let content: string;
let messageMetadata: Record<string, unknown> | undefined;
const files: File[] = [];
+ const thumbnails = new Map<number, File>();
if (isMultipart) {
let formData: FormData;
@@ -90,6 +91,10 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
files.push(value);
}
}
+ for (const [key, value] of formData.entries()) {
+ const m = key.match(/^thumbnail:(\d+)$/);
+ if (m && value instanceof File) thumbnails.set(Number(m[1]), value);
+ }
} else {
const [body, valErr] = await parseBody(req, CreateMessageRequestSchema);
if (valErr) return valErr;
@@ -118,7 +123,8 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
// Upload files to R2 and create artifact rows
const artifactIds: string[] = [];
- for (const file of files) {
+ for (let fi = 0; fi < files.length; fi++) {
+ const file = files[fi];
const filename = sanitizeFilename(file.name);
const fileContentType = file.type || "application/octet-stream";
const artifactId = "art_" + nanoid();
@@ -128,6 +134,15 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
httpMetadata: { contentType: fileContentType },
});
+ let thumbnailR2Key: string | undefined;
+ const thumb = thumbnails.get(fi);
+ if (thumb && thumb.size <= 50 * 1024) {
+ thumbnailR2Key = `artifacts/${ws.workspaceId}/${conversation.agentId}/${id}/${artifactId}/thumbnail.jpg`;
+ await bucket.put(thumbnailR2Key, await thumb.arrayBuffer(), {
+ httpMetadata: { contentType: "image/jpeg" },
+ });
+ }
+
await queries.artifact.createArtifact(db, {
id: artifactId,
conversationId: id,
@@ -137,6 +152,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
contentType: fileContentType,
size: file.size,
r2Key,
+ thumbnailR2Key,
source: "attachment",
});
diff --git a/src/web/src/components/agent-chat/agent-chat-view.tsx b/src/web/src/components/agent-chat/agent-chat-view.tsx
index eb83b38f5..da97babb4 100644
--- a/src/web/src/components/agent-chat/agent-chat-view.tsx
+++ b/src/web/src/components/agent-chat/agent-chat-view.tsx
@@ -179,6 +179,7 @@ export function AgentChatView({
issueTaskId,
} = useChatSheets(workspaceId);
const [lightboxArtifact, setLightboxArtifact] = useState<Artifact | null>(null);
+ const [lightboxLocalUrl, setLightboxLocalUrl] = useState<{ url: string; filename: string } | null>(null);
const {
pendingFiles,
setPendingFiles,
@@ -286,6 +287,7 @@ export function AgentChatView({
napping,
pendingFilesByMessage,
failedSends,
+ stableKeyMap,
agentArtifacts,
agentName,
timeline,
@@ -356,6 +358,14 @@ export function AgentChatView({
// First name only — presence copy reads socially ("Maya is typing…").
const agentFirstName = useMemo(() => agentName.split(/\s+/)[0] || agentName, [agentName]);
+ const handlePendingImageClick = useCallback(
+ (file: File) => {
+ const url = URL.createObjectURL(file);
+ setLightboxLocalUrl({ url, filename: file.name });
+ },
+ [],
+ );
+
const handleArtifactClick = useCallback(
(artifact: Artifact) => {
if (artifact.content_type.startsWith("image/")) {
@@ -525,6 +535,44 @@ export function AgentChatView({
// eslint-disable-next-line react-hooks/exhaustive-deps -- composerRef is stable
}, []);
+ const handleEmailClick = useCallback((emailId: string) => {
+ setSelectedEmailId(emailId);
+ setEmailSheetOpen(true);
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- setState fns are stable
+ }, []);
+
+ const handleCalendarEventClick = useCallback((id: string) => {
+ setSelectedCalendarEventId(id);
+ setCalendarEventSheetOpen(true);
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- setState fns are stable
+ }, []);
+
+ const handleIssueClick = useCallback((issueId: string) => {
+ openIssue(issueId);
+ }, [openIssue]);
+
+ const handleAgentChatOpen = useCallback((agId: string, convId: string) => {
+ openAgentChat(agId, { conversationId: convId });
+ }, [openAgentChat]);
+
+ const threadSummariesRef = useLatest(threadSummaries);
+ const conversationRef = useLatest(conversation);
+ const fetchThreadSummariesRef = useLatest(fetchThreadSummaries);
+
+ const handleReplyInThread = useCallback(async (msgId: string) => {
+ const summary = threadSummariesRef.current.get(msgId);
+ if (summary?.thread_id) {
+ openAgentChat(agentId, { conversationId: summary.thread_id });
+ } else if (conversationRef.current) {
+ try {
+ const result = await createThread(conversationRef.current.id, msgId, "", workspaceId);
+ openAgentChat(agentId, { conversationId: result.conversation.id });
+ fetchThreadSummariesRef.current();
+ } catch {}
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- refs are stable
+ }, [openAgentChat, agentId, workspaceId]);
+
useEffect(() => {
if (!issueSheetOpen || !selectedIssueId) return;
@@ -716,6 +764,7 @@ export function AgentChatView({
taskMessages={[]}
connectionLost={false}
pendingFilesByMessage={new Map()}
+ workspaceId={workspaceId}
onArtifactClick={() => {}}
onEmailClick={() => {}}
onIssueClick={() => {}}
@@ -827,8 +876,14 @@ export function AgentChatView({
}
const msg = item.data;
+ // Use the original optimistic ID as the React key when available,
+ // so replacing "temp-xxx" → "msg_abc" is an in-place UPDATE (same
+ // key, new props) instead of an unmount+remount — eliminating the
+ // visual flicker that occurred when the entire message row was torn
+ // down and rebuilt with a different key.
+ const stableKey = stableKeyMap.get(msg.id) ?? msg.id;
return (
- <div key={msg.id} className={spacing}>
+ <div key={stableKey} className={spacing}>
<MessageItem
msg={msg}
agents={agents}
@@ -839,16 +894,12 @@ export function AgentChatView({
connectionLost={connectionLost}
conversationType={conversation?.type}
pendingFilesByMessage={pendingFilesByMessage}
+ workspaceId={workspaceId}
onArtifactClick={handleArtifactClick}
- onEmailClick={(emailId) => {
- setSelectedEmailId(emailId);
- setEmailSheetOpen(true);
- }}
- onCalendarEventClick={(id) => {
- setSelectedCalendarEventId(id);
- setCalendarEventSheetOpen(true);
- }}
- onIssueClick={(issueId) => openIssue(issueId)}
+ onPendingImageClick={handlePendingImageClick}
+ onEmailClick={handleEmailClick}
+ onCalendarEventClick={handleCalendarEventClick}
+ onIssueClick={handleIssueClick}
onRetry={handleRetryTask}
mentionComponents={MENTION_COMPONENTS}
isFlagged={flaggedIds.has(msg.id)}
@@ -862,20 +913,9 @@ export function AgentChatView({
isSendFailed={failedSends.has(msg.id)}
onRetrySend={handleRetrySend}
onQuote={handleQuoteMessage}
- onReplyInThread={conversation?.parent_message_id ? undefined : async (msgId) => {
- const summary = threadSummaries.get(msgId);
- if (summary?.thread_id) {
- openAgentChat(agentId, { conversationId: summary.thread_id });
- } else if (conversation) {
- try {
- const result = await createThread(conversation.id, msgId, "", workspaceId);
- openAgentChat(agentId, { conversationId: result.conversation.id });
- fetchThreadSummaries();
- } catch {}
- }
- }}
+ onReplyInThread={conversation?.parent_message_id ? undefined : handleReplyInThread}
threadSummary={conversation?.parent_message_id ? null : threadSummaries.get(msg.id) ?? null}
- onAgentChatOpen={(agId, convId) => openAgentChat(agId, { conversationId: convId })}
+ onAgentChatOpen={handleAgentChatOpen}
/>
</div>
);
@@ -1090,30 +1130,67 @@ export function AgentChatView({
onSelect={slashCommand.selectSkill}
anchorPos={slashCommand.anchorPos}
/>
- {/* Pending file pills — above the input text, never disturbs the buttons */}
- {pendingFiles.length > 0 && (
- <div className="flex flex-wrap gap-1.5 px-3.5 pt-3 pb-0.5">
- {pendingFiles.map((file, i) => (
- <span
- key={`${file.name}-${i}`}
- className="inline-flex items-center gap-1.5 rounded-md bg-muted px-2 py-1 text-xs text-muted-foreground"
- >
- <FileText className="size-3 shrink-0" />
- <span className="truncate max-w-30">{file.name}</span>
- <span className="text-muted-foreground/60">
- {formatSize(file.size)}
- </span>
- <button
- type="button"
- onClick={() => removePendingFile(i)}
- className="ml-0.5 rounded-sm p-0.5 hover:bg-muted-foreground/20 transition-colors"
- >
- <X className="size-3" />
- </button>
- </span>
- ))}
- </div>
- )}
+ {/* Pending file previews — images and non-images in separate rows */}
+ {pendingFiles.length > 0 && (() => {
+ const images = pendingFiles.map((pf, i) => ({ pf, i })).filter(({ pf }) => pf.thumbnailUrl);
+ const others = pendingFiles.map((pf, i) => ({ pf, i })).filter(({ pf }) => !pf.thumbnailUrl);
+ return (
+ <div className="px-3.5 pt-3 pb-0.5 space-y-1.5">
+ {images.length > 0 && (
+ <div className="flex flex-wrap gap-1.5">
+ {images.map(({ pf, i }) => (
+ <div key={`${pf.file.name}-${i}`} className="relative group">
+ <button
+ type="button"
+ onClick={() => {
+ const url = URL.createObjectURL(pf.file);
+ setLightboxLocalUrl({ url, filename: pf.file.name });
+ }}
+ className="cursor-pointer"
+ >
+ <img
+ src={pf.thumbnailUrl!}
+ alt={pf.file.name}
+ className="h-12 w-auto rounded-md object-cover"
+ />
+ </button>
+ <button
+ type="button"
+ onClick={() => removePendingFile(i)}
+ className="absolute -top-1.5 -right-1.5 size-4 rounded-full bg-muted-foreground text-background flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity"
+ >
+ <X className="size-2.5" />
+ </button>
+ </div>
+ ))}
+ </div>
+ )}
+ {others.length > 0 && (
+ <div className="flex flex-wrap gap-1.5">
+ {others.map(({ pf, i }) => (
+ <span
+ key={`${pf.file.name}-${i}`}
+ className="inline-flex items-center gap-1.5 rounded-md bg-muted px-2 py-1 text-xs text-muted-foreground"
+ >
+ <FileText className="size-3 shrink-0" />
+ <span className="truncate max-w-30">{pf.file.name}</span>
+ <span className="text-muted-foreground/60">
+ {formatSize(pf.file.size)}
+ </span>
+ <button
+ type="button"
+ onClick={() => removePendingFile(i)}
+ className="ml-0.5 rounded-sm p-0.5 hover:bg-muted-foreground/20 transition-colors"
+ >
+ <X className="size-3" />
+ </button>
+ </span>
+ ))}
+ </div>
+ )}
+ </div>
+ );
+ })()}
{/* Composer + absolutely-positioned buttons */}
<div className="relative">
<input
@@ -1224,6 +1301,17 @@ export function AgentChatView({
artifact={lightboxArtifact}
workspaceId={workspaceId}
/>
+ {lightboxLocalUrl && (
+ <ImageLightbox
+ open
+ onClose={() => {
+ URL.revokeObjectURL(lightboxLocalUrl.url);
+ setLightboxLocalUrl(null);
+ }}
+ imageUrl={lightboxLocalUrl.url}
+ filename={lightboxLocalUrl.filename}
+ />
+ )}
<ArtifactSheet
open={artifactSheetOpen}
diff --git a/src/web/src/components/agent-chat/image-lightbox.tsx b/src/web/src/components/agent-chat/image-lightbox.tsx
index eff99741d..5bee0a913 100644
--- a/src/web/src/components/agent-chat/image-lightbox.tsx
+++ b/src/web/src/components/agent-chat/image-lightbox.tsx
@@ -6,17 +6,17 @@ import type { Artifact } from "@alook/shared";
import { X, Download } from "lucide-react";
import { getArtifactUrl } from "@/components/artifact-content-renderer";
-export function ImageLightbox({
- open,
- onClose,
- artifact,
- workspaceId,
-}: {
+type LightboxProps = {
open: boolean;
onClose: () => void;
- artifact: Artifact | null;
- workspaceId: string;
-}) {
+} & (
+ | { artifact: Artifact | null; workspaceId: string; imageUrl?: undefined }
+ | { imageUrl: string; filename: string; artifact?: undefined; workspaceId?: undefined }
+);
+
+export function ImageLightbox(props: LightboxProps) {
+ const { open, onClose } = props;
+
const handleKeyDown = useCallback(
(e: KeyboardEvent) => {
if (e.key === "Escape") onClose();
@@ -30,10 +30,20 @@ export function ImageLightbox({
return () => document.removeEventListener("keydown", handleKeyDown);
}, [open, handleKeyDown]);
- if (!open || !artifact) return null;
+ let src: string;
+ let alt: string;
+ let downloadUrl: string | undefined;
- const fullUrl = getArtifactUrl(artifact.id, workspaceId);
- const downloadUrl = getArtifactUrl(artifact.id, workspaceId, true);
+ if (props.imageUrl) {
+ if (!open) return null;
+ src = props.imageUrl;
+ alt = props.filename;
+ } else {
+ if (!open || !props.artifact) return null;
+ src = getArtifactUrl(props.artifact.id, props.workspaceId);
+ alt = props.artifact.filename;
+ downloadUrl = getArtifactUrl(props.artifact.id, props.workspaceId, true);
+ }
return createPortal(
<div
@@ -41,14 +51,16 @@ export function ImageLightbox({
onClick={onClose}
>
<div className="absolute top-4 right-4 flex items-center gap-2">
- <a
- href={downloadUrl}
- download={artifact.filename}
- onClick={(e) => e.stopPropagation()}
- className="rounded-full p-2 text-white/70 hover:text-white hover:bg-white/10 transition-colors"
- >
- <Download className="size-5" />
- </a>
+ {downloadUrl && (
+ <a
+ href={downloadUrl}
+ download={alt}
+ onClick={(e) => e.stopPropagation()}
+ className="rounded-full p-2 text-white/70 hover:text-white hover:bg-white/10 transition-colors"
+ >
+ <Download className="size-5" />
+ </a>
+ )}
<button
type="button"
onClick={onClose}
@@ -58,8 +70,8 @@ export function ImageLightbox({
</button>
</div>
<img
- src={fullUrl}
- alt={artifact.filename}
+ src={src}
+ alt={alt}
onClick={(e) => e.stopPropagation()}
className="max-w-[90vw] max-h-[90vh] object-contain rounded-lg"
/>
diff --git a/src/web/src/components/agent-chat/message-list.tsx b/src/web/src/components/agent-chat/message-list.tsx
index 5f257e248..fd0cf9c8c 100644
--- a/src/web/src/components/agent-chat/message-list.tsx
+++ b/src/web/src/components/agent-chat/message-list.tsx
@@ -12,6 +12,8 @@ import { TaskStream } from "@/components/task-stream";
import { RuntimeErrorBlock } from "@/components/agent-chat/runtime-error-block";
import { AnimatedAvatar, type AvatarConfig } from "@/components/avatar";
import { FileText, Flag, Copy, Check, MessageSquareQuote, MessageSquare, Image } from "lucide-react";
+import { getArtifactThumbnailUrl } from "@/components/artifact-content-renderer";
+import type { PendingFile } from "@/hooks/use-file-attachments";
import { EmailCard } from "@/components/agent-chat/event-cards/email-card";
import { CalendarCard } from "@/components/agent-chat/event-cards/calendar-card";
import { IssueCard } from "@/components/agent-chat/event-cards/issue-card";
@@ -64,7 +66,8 @@ export interface MessageItemProps {
taskMessages: TaskMessageResponse[];
connectionLost: boolean;
conversationType?: string | null;
- pendingFilesByMessage: Map<string, File[]>;
+ pendingFilesByMessage: Map<string, PendingFile[]>;
+ workspaceId: string;
onArtifactClick: (a: Artifact) => void;
onEmailClick: (emailId: string) => void;
onIssueClick: (issueId: string) => void;
@@ -82,6 +85,7 @@ export interface MessageItemProps {
/** This optimistic user message failed to send — show the inline retry affordance. */
isSendFailed?: boolean;
onRetrySend?: (messageId: string) => void;
+ onPendingImageClick?: (file: File) => void;
onQuote?: (messageId: string, excerpt: string) => void;
onReplyInThread?: (messageId: string) => void;
threadSummary?: { thread_id: string; reply_count: number; last_reply_at: string | null; thread_title: string } | null;
@@ -182,7 +186,8 @@ function AttachmentChips({
}) {
const matched = attachmentIds
.map((id) => artifacts.find((a) => a.id === id))
- .filter((a): a is Artifact => !!a);
+ .filter((a): a is Artifact => !!a)
+ .filter((a) => !a.content_type.startsWith("image/"));
if (matched.length === 0) return null;
@@ -195,7 +200,7 @@ function AttachmentChips({
onClick={(e) => { e.stopPropagation(); onArtifactClick(a); }}
className="inline-flex items-center gap-1 rounded-md bg-primary-foreground/10 border border-primary-foreground/20 px-2 py-0.5 text-xs text-primary-foreground/80 hover:bg-primary-foreground/20 transition-colors cursor-pointer"
>
- {a.content_type.startsWith("image/") ? <Image className="size-3 shrink-0" /> : <FileText className="size-3 shrink-0" />}
+ <FileText className="size-3 shrink-0" />
<span className="truncate max-w-37.5">{a.filename}</span>
</button>
))}
@@ -203,32 +208,54 @@ function AttachmentChips({
);
}
-function PendingFileChips({
- pendingFiles,
- messageId,
+function ImageAttachmentCards({
+ attachmentIds,
+ artifacts,
+ workspaceId,
+ onArtifactClick,
}: {
- pendingFiles: Map<string, File[]>;
- messageId: string;
+ attachmentIds: string[];
+ artifacts: Artifact[];
+ workspaceId: string;
+ onArtifactClick: (a: Artifact) => void;
}) {
- const files = pendingFiles.get(messageId);
- if (!files || files.length === 0) return null;
+ const images = attachmentIds
+ .map((id) => artifacts.find((a) => a.id === id))
+ .filter((a): a is Artifact => !!a && a.content_type.startsWith("image/"));
+
+ if (images.length === 0) return null;
return (
<div className="flex flex-wrap gap-1.5 mt-1.5">
- {files.map((f, i) => (
- <span
- key={i}
- className="inline-flex items-center gap-1 rounded-md bg-primary-foreground/10 border border-primary-foreground/20 px-2 py-0.5 text-xs text-primary-foreground/80"
- >
- <FileText className="size-3 shrink-0" />
- <span className="truncate max-w-37.5">{f.name}</span>
- </span>
- ))}
+ {images.map((a) => {
+ const thumbUrl = a.has_thumbnail
+ ? getArtifactThumbnailUrl(a.id, workspaceId)
+ : undefined;
+ return thumbUrl ? (
+ <button
+ key={a.id}
+ type="button"
+ onClick={(e) => { e.stopPropagation(); onArtifactClick(a); }}
+ className="max-w-48 overflow-hidden rounded-(--radius) border border-(--border) cursor-pointer [transition:translate_.2s_cubic-bezier(.2,.8,.2,1),box-shadow_.2s_ease] hover:-translate-y-0.5 [box-shadow:var(--e1)] hover:[box-shadow:var(--e2)]"
+ >
+ <img src={thumbUrl} alt={a.filename} loading="lazy" className="block w-full h-auto" />
+ </button>
+ ) : (
+ <button
+ key={a.id}
+ type="button"
+ onClick={(e) => { e.stopPropagation(); onArtifactClick(a); }}
+ className="inline-flex items-center gap-1 rounded-md bg-primary-foreground/10 border border-primary-foreground/20 px-2 py-0.5 text-xs text-primary-foreground/80 hover:bg-primary-foreground/20 transition-colors cursor-pointer"
+ >
+ <Image className="size-3 shrink-0" />
+ <span className="truncate max-w-37.5">{a.filename}</span>
+ </button>
+ );
+ })}
</div>
);
}
-
// Slack/Discord cluster model: delegates to <MessageCluster> from
// chat-primitives. This wrapper adapts the product's AnimatedAvatar + the
// `forceSpacer` concept into the primitive's props interface.
@@ -398,6 +425,7 @@ export const MessageItem = memo(function MessageItem({
connectionLost,
conversationType,
pendingFilesByMessage,
+ workspaceId,
onArtifactClick,
onEmailClick,
onIssueClick,
@@ -411,6 +439,7 @@ export const MessageItem = memo(function MessageItem({
agentName,
agentAvatarConfig,
isSendFailed,
+ onPendingImageClick,
onRetrySend,
onQuote,
onReplyInThread,
@@ -599,14 +628,62 @@ export const MessageItem = memo(function MessageItem({
</div>
)}
{(() => {
- if (msg.attachment_ids && msg.attachment_ids.length > 0) {
- const hasMatch = msg.attachment_ids.some((id: string) => artifacts.some((a) => a.id === id));
- if (hasMatch) return <AttachmentChips attachmentIds={msg.attachment_ids} artifacts={artifacts} onArtifactClick={onArtifactClick} />;
+ // Non-image file chips inside the bubble.
+ // Prefer local pending pills (instant) to avoid flash on transition.
+ const pfs = pendingFilesByMessage.get(msg.id);
+ const nonImagePfs = pfs?.filter((pf) => !pf.thumbnailUrl);
+ if (nonImagePfs && nonImagePfs.length > 0) {
+ return (
+ <div className="flex flex-wrap gap-1.5 mt-1.5">
+ {nonImagePfs.map((pf, i) => (
+ <span key={i} className="inline-flex items-center gap-1 rounded-md bg-primary-foreground/10 border border-primary-foreground/20 px-2 py-0.5 text-xs text-primary-foreground/80">
+ <FileText className="size-3 shrink-0" />
+ <span className="truncate max-w-37.5">{pf.file.name}</span>
+ </span>
+ ))}
+ </div>
+ );
+ }
+ const ids = msg.attachment_ids;
+ if (ids && ids.length > 0) {
+ const resolved = ids.map((id: string) => artifacts.find((a) => a.id === id)).filter((a): a is Artifact => !!a);
+ const nonImageResolved = resolved.filter((a) => !a.content_type.startsWith("image/"));
+ if (nonImageResolved.length > 0) return <AttachmentChips attachmentIds={nonImageResolved.map((a) => a.id)} artifacts={artifacts} onArtifactClick={onArtifactClick} />;
}
- return <PendingFileChips pendingFiles={pendingFilesByMessage} messageId={msg.id} />;
+ return null;
})()}
{toolbar}
</MessageBubble>
+ {(() => {
+ // Image thumbnail cards below the bubble.
+ // Prefer local blob thumbnails (instant, no network fetch) over
+ // server thumbnails to avoid a flash when transitioning sources.
+ const pfs = pendingFilesByMessage.get(msg.id);
+ const imagePfs = pfs?.filter((pf) => pf.thumbnailUrl);
+ if (imagePfs && imagePfs.length > 0) {
+ return (
+ <div className="flex flex-wrap gap-1.5 mt-1.5">
+ {imagePfs.map((pf, i) => (
+ <button
+ key={i}
+ type="button"
+ onClick={(e) => { e.stopPropagation(); onPendingImageClick?.(pf.file); }}
+ className="max-w-48 overflow-hidden rounded-(--radius) border border-(--border) cursor-pointer [transition:translate_.2s_cubic-bezier(.2,.8,.2,1),box-shadow_.2s_ease] hover:-translate-y-0.5 [box-shadow:var(--e1)] hover:[box-shadow:var(--e2)]"
+ >
+ <img src={pf.thumbnailUrl!} alt={pf.file.name} className="block w-full h-auto" />
+ </button>
+ ))}
+ </div>
+ );
+ }
+ const ids = msg.attachment_ids;
+ if (ids && ids.length > 0) {
+ const resolved = ids.map((id: string) => artifacts.find((a) => a.id === id)).filter((a): a is Artifact => !!a);
+ const imageResolved = resolved.filter((a) => a.content_type.startsWith("image/"));
+ if (imageResolved.length > 0) return <ImageAttachmentCards attachmentIds={imageResolved.map((a) => a.id)} artifacts={artifacts} workspaceId={workspaceId} onArtifactClick={onArtifactClick} />;
+ }
+ return null;
+ })()}
{actionSheet}
{isSendFailed && (
<button
diff --git a/src/web/src/hooks/use-agent-chat.ts b/src/web/src/hooks/use-agent-chat.ts
index df3ad3124..36edb8118 100644
--- a/src/web/src/hooks/use-agent-chat.ts
+++ b/src/web/src/hooks/use-agent-chat.ts
@@ -65,7 +65,9 @@ import type {
WsMessage,
} from "@alook/shared";
import { toast } from "sonner";
+import type { PendingFile } from "@/hooks/use-file-attachments";
import type { ChatComposerHandle } from "@/components/agent-chat/chat-composer";
+import { getArtifactThumbnailUrl } from "@/components/artifact-content-renderer";
import { useCachedMessages } from "@/hooks/use-cached-messages";
import { trackAgentChatOpened, trackMessageSent } from "@/lib/analytics";
@@ -90,7 +92,7 @@ export interface UseAgentChatProps {
export interface UseAgentChatExternal {
// (a) Setters the hook WRITES — state owned outside the hook, passed IN.
setFlaggedIds: (ids: Set<string>) => void;
- setPendingFiles: (files: File[]) => void;
+ setPendingFiles: (files: PendingFile[]) => void;
setInput: (value: string) => void;
setQuotedMessage: (value: { id: string; excerpt: string } | null) => void;
setActiveSkill: (skill: SkillEntry | null) => void;
@@ -98,7 +100,7 @@ export interface UseAgentChatExternal {
// (b) Values the hook READS — owned outside, passed IN via useLatest ref.
inputRef: MutableRefObject<string>;
quotedMessageRef: MutableRefObject<{ id: string; excerpt: string } | null>;
- pendingFilesRef: MutableRefObject<File[]>;
+ pendingFilesRef: MutableRefObject<PendingFile[]>;
activeSkillRef: MutableRefObject<SkillEntry | null>;
// Component-owned ref written by the load effect (gates draft-meta persist).
draftMetaRestoredRef: MutableRefObject<boolean>;
@@ -138,6 +140,8 @@ export function useAgentChat(
const [conversation, setConversation] = useState<Conversation | null>(null);
const [messages, setMessages] = useState<Message[]>([]);
+ const messagesSnapshotRef = useRef<Message[]>([]);
+ messagesSnapshotRef.current = messages;
const [sending, setSending] = useState(false);
const [activeTask, setActiveTask] = useState<Task | null>(null);
const [taskMessages, setTaskMessages] = useState<TaskMessageResponse[]>([]);
@@ -145,7 +149,29 @@ export function useAgentChat(
const [connectionLost, setConnectionLost] = useState(false);
const [hasMore, setHasMore] = useState(false);
const [loadingMore, setLoadingMore] = useState(false);
- const [artifacts, setArtifacts] = useState<Artifact[]>([]);
+ const [artifacts, setArtifactsRaw] = useState<Artifact[]>([]);
+
+ // Stable setter: skip the state update when the artifact list hasn't materially
+ // changed (same ids in same order, same thumbnail readiness). This prevents
+ // `listArtifacts` from creating a new array reference that cascades through
+ // `agentArtifacts` → `timeline` → every MessageItem re-render.
+ const artifactsRef = useRef(artifacts);
+ artifactsRef.current = artifacts;
+ const setArtifacts = useCallback((next: Artifact[] | ((prev: Artifact[]) => Artifact[])) => {
+ setArtifactsRaw((prev) => {
+ const resolved = typeof next === "function" ? next(prev) : next;
+ if (resolved === prev) return prev;
+ if (
+ resolved.length === prev.length &&
+ resolved.every((a, i) =>
+ a.id === prev[i].id && a.has_thumbnail === prev[i].has_thumbnail,
+ )
+ ) {
+ return prev; // content-equal — keep the old reference
+ }
+ return resolved;
+ });
+ }, []);
const [previousConversations, setPreviousConversations] = useState<
PreviousConversation[]
>([]);
@@ -153,15 +179,19 @@ export function useAgentChat(
const [napMarkers, setNapMarkers] = useState<NapMarker[]>([]);
const [pendingFilesByMessage, setPendingFilesByMessage] = useState<
- Map<string, File[]>
+ Map<string, PendingFile[]>
>(() => new Map());
- // Optimistic sends that failed to reach the server. The bubble stays in place
- // with an inline "Not delivered · tap to retry" affordance (iMessage-style),
- // keyed by the optimistic message id → the content + files to resend.
const [failedSends, setFailedSends] = useState<
- Map<string, { content: string; files: File[] }>
+ Map<string, { content: string; files: PendingFile[] }>
>(() => new Map());
+ // Maps real (server) message IDs back to their optimistic (temp-*) IDs so
+ // the React key used in the timeline stays stable when the optimistic message
+ // is replaced — preventing an unmount/remount flash of the entire row.
+ const [stableKeyMap, setStableKeyMap] = useState<Map<string, string>>(
+ () => new Map(),
+ );
+
const { writeToCache } = useCachedMessages(targetConvId ?? null, workspaceId);
const writeToCacheRef = useRef(writeToCache);
useEffect(() => {
@@ -283,6 +313,54 @@ export function useAgentChat(
}, 50);
}, []);
+ // Preload server thumbnail images in the background, then delete the
+ // corresponding pendingFilesByMessage entries and revoke their blob URLs.
+ // This prevents layout shift: the browser image cache is warm before we
+ // remove the local blob source, so the <img> switches without a flash.
+ const preloadThenCleanPending = useCallback(
+ (arts: Artifact[], conversationId: string) => {
+ // Collect thumbnail URLs for image artifacts that have server thumbnails.
+ const thumbUrls = arts
+ .filter((a) => a.content_type.startsWith("image/") && a.has_thumbnail)
+ .map((a) => getArtifactThumbnailUrl(a.id, workspaceId));
+
+ function preloadImage(url: string): Promise<void> {
+ return new Promise((resolve) => {
+ const img = new window.Image();
+ img.onload = () => resolve();
+ img.onerror = () => resolve(); // don't block cleanup on failure
+ img.src = url;
+ });
+ }
+
+ // Preload all thumbnails, then clean up only pending entries whose
+ // artifacts are actually resolved — avoids prematurely clearing entries
+ // for a second message still being uploaded.
+ const artIdSet = new Set(arts.map((a) => a.id));
+ Promise.all(thumbUrls.map(preloadImage)).then(() => {
+ setPendingFilesByMessage((prev) => {
+ if (prev.size === 0) return prev;
+ const msgs = messagesSnapshotRef.current;
+ const next = new Map(prev);
+ let changed = false;
+ for (const [msgId, files] of prev) {
+ const msg = msgs.find((m) => m.id === msgId);
+ const ids = msg?.attachment_ids;
+ if (ids && ids.length > 0 && ids.every((id: string) => artIdSet.has(id))) {
+ for (const pf of files) {
+ if (pf.thumbnailUrl) URL.revokeObjectURL(pf.thumbnailUrl);
+ }
+ next.delete(msgId);
+ changed = true;
+ }
+ }
+ return changed ? next : prev;
+ });
+ });
+ },
+ [workspaceId],
+ );
+
useEffect(() => {
// The slow path (no targetConvId) resolves the conversation id via the
// server using activeChannel, so it must wait for the channel list. The
@@ -314,8 +392,18 @@ export function useAgentChat(
initialScrollDone.current = false;
setActiveTask(null);
setTaskMessages([]);
- setPendingFilesByMessage(new Map());
+ // Revoke blob URLs before clearing the map — they were kept alive for the
+ // session to avoid layout shift, so this is the only place they get freed.
+ setPendingFilesByMessage((prev) => {
+ for (const files of prev.values()) {
+ for (const pf of files) {
+ if (pf.thumbnailUrl) URL.revokeObjectURL(pf.thumbnailUrl);
+ }
+ }
+ return new Map();
+ });
setFailedSends(new Map());
+ setStableKeyMap(new Map());
setNapMarkers([]);
setPreviousConversations([]);
setHasMoreConversations(false);
@@ -1316,6 +1404,11 @@ export function useAgentChat(
// Full-replace persist of the authoritative post-task artifacts
// so the cache stays consistent with what's rendered.
persistArtifactsToCache(conversationId, arts);
+ // Preload server thumbnails, then clean up pending blob entries.
+ // This prevents layout shift: the browser cache is warm before
+ // we remove the local blob, so the <img> switches sources without
+ // a visible flash or reflow.
+ preloadThenCleanPending(arts, conversationId);
}
setActiveTask(task);
} catch {
@@ -1499,27 +1592,38 @@ export function useAgentChat(
).catch(() => { });
}
if (msg.conversationId === conversation?.id) {
+ const incomingTime = new Date(msg.message.created_at).getTime();
+ const optimisticMatch = messagesRef.current.find(
+ (m) =>
+ m.id.startsWith("temp-") &&
+ m.role === msg.message.role &&
+ m.content === msg.message.content &&
+ Math.abs(new Date(m.created_at).getTime() - incomingTime) < 2000,
+ );
+ if (optimisticMatch) {
+ setPendingFilesByMessage((p) => {
+ if (!p.has(optimisticMatch.id)) return p;
+ const files = p.get(optimisticMatch.id)!;
+ const next = new Map(p);
+ next.delete(optimisticMatch.id);
+ next.set(msg.message.id, files);
+ return next;
+ });
+ setStableKeyMap((prev) => {
+ if (prev.has(msg.message.id)) return prev;
+ const next = new Map(prev);
+ next.set(msg.message.id, optimisticMatch.id);
+ return next;
+ });
+ }
setMessages((prev) => {
- const incomingTime = new Date(msg.message.created_at).getTime();
- const optimisticIdx = prev.findIndex(
- (m) =>
- m.id.startsWith("temp-") &&
- m.role === msg.message.role &&
- m.content === msg.message.content &&
- Math.abs(new Date(m.created_at).getTime() - incomingTime) <
- 2000,
- );
- if (optimisticIdx !== -1) {
- const optimisticId = prev[optimisticIdx].id;
- setPendingFilesByMessage((p) => {
- if (!p.has(optimisticId)) return p;
- const next = new Map(p);
- next.delete(optimisticId);
- return next;
- });
- const updated = [...prev];
- updated[optimisticIdx] = msg.message;
- return updated;
+ if (optimisticMatch) {
+ const idx = prev.findIndex((m) => m.id === optimisticMatch.id);
+ if (idx !== -1) {
+ const updated = [...prev];
+ updated[idx] = msg.message;
+ return updated;
+ }
}
return mergeMessages(prev, [msg.message]);
});
@@ -1597,6 +1701,16 @@ export function useAgentChat(
}
const filesToSend = [...pendingFilesRef.current];
+ // Create independent blob URLs for the copies that will live in
+ // pendingFilesByMessage. The originals will be revoked when
+ // setPendingFiles([]) clears the composer state below, so the copies
+ // need their own URLs to keep thumbnails visible until real artifacts load.
+ const filesToRender = filesToSend.map((pf) => ({
+ ...pf,
+ thumbnailUrl: pf.thumbnailBlob
+ ? URL.createObjectURL(pf.thumbnailBlob)
+ : null,
+ }));
trackMessageSent({ agent_id: agentId, message_length: content.length });
setInput("");
setPendingFiles([]);
@@ -1620,11 +1734,12 @@ export function useAgentChat(
created_at: new Date().toISOString(),
};
- // Store pending files for the optimistic message rendering
- if (filesToSend.length > 0) {
+ // Store pending files (with independent blob URLs) for optimistic rendering.
+ // filesToRender has fresh blob URLs that won't be revoked by setPendingFiles([]).
+ if (filesToRender.length > 0) {
setPendingFilesByMessage((prev) => {
const next = new Map(prev);
- next.set(optimisticId, filesToSend);
+ next.set(optimisticId, filesToRender);
return next;
});
}
@@ -1640,11 +1755,21 @@ export function useAgentChat(
filesToSend.length > 0 ? filesToSend : undefined,
quoteRef ? { quote: { messageId: quoteRef.id, excerpt: quoteRef.excerpt } } : undefined,
);
- // Clean up pending files ref
+ // Remap pending files from optimistic ID → real message ID so the local
+ // blob thumbnails / file pills stay visible until real artifacts are loaded.
setPendingFilesByMessage((prev) => {
if (!prev.has(optimisticId)) return prev;
+ const files = prev.get(optimisticId)!;
const next = new Map(prev);
next.delete(optimisticId);
+ next.set(message.id, files);
+ return next;
+ });
+ // Record the optimistic→real ID mapping so the React key stays stable
+ // and React updates the existing DOM node instead of unmounting/remounting.
+ setStableKeyMap((prev) => {
+ const next = new Map(prev);
+ next.set(message.id, optimisticId);
return next;
});
setMessages((prev) => {
@@ -1662,13 +1787,12 @@ export function useAgentChat(
() => { },
);
if (message.attachment_ids && message.attachment_ids.length > 0) {
- const convId = conversation.id;
- listArtifacts(convId, workspaceId)
+ listArtifacts(conversation.id, workspaceId)
.then((arts) => {
setArtifacts(arts);
- // Keep the cached card metadata consistent with the artifacts shown
- // after a send-with-attachments refresh (full replace).
- persistArtifactsToCache(convId, arts);
+ persistArtifactsToCache(conversation.id, arts);
+ // Preload server thumbnails, then clean up pending blob entries.
+ preloadThenCleanPending(arts, conversation.id);
})
.catch(() => { });
}
@@ -1678,9 +1802,10 @@ export function useAgentChat(
} catch {
// Keep the optimistic bubble in place and surface an inline
// "Not delivered · tap to retry" affordance instead of a toast (Priya).
+ // Store the rendering copies (with live blob URLs) so retries can re-use them.
setFailedSends((prev) => {
const next = new Map(prev);
- next.set(optimisticId, { content, files: filesToSend });
+ next.set(optimisticId, { content, files: filesToRender });
return next;
});
} finally {
@@ -1738,8 +1863,15 @@ export function useAgentChat(
.then(({ message, task }) => {
setPendingFilesByMessage((prev) => {
if (!prev.has(optimisticId)) return prev;
+ const files = prev.get(optimisticId)!;
const next = new Map(prev);
next.delete(optimisticId);
+ next.set(message.id, files);
+ return next;
+ });
+ setStableKeyMap((prev) => {
+ const next = new Map(prev);
+ next.set(message.id, optimisticId);
return next;
});
setMessages((prev) => {
@@ -1751,6 +1883,16 @@ export function useAgentChat(
appendCachedMessage(conversation.id, message, workspaceId).catch(
() => { },
);
+ if (message.attachment_ids && message.attachment_ids.length > 0) {
+ listArtifacts(conversation.id, workspaceId)
+ .then((arts) => {
+ setArtifacts(arts);
+ persistArtifactsToCache(conversation.id, arts);
+ // Preload server thumbnails, then clean up pending blob entries.
+ preloadThenCleanPending(arts, conversation.id);
+ })
+ .catch(() => { });
+ }
setActiveTask(task);
setTaskMessages([]);
startPolling(task.id, conversation.id);
@@ -1819,8 +1961,17 @@ export function useAgentChat(
setTaskMessages([]);
setArtifacts([]);
setPendingFiles([]);
- setPendingFilesByMessage(new Map());
+ // Revoke blob URLs before clearing (same as conversation-switch path).
+ setPendingFilesByMessage((prev) => {
+ for (const files of prev.values()) {
+ for (const pf of files) {
+ if (pf.thumbnailUrl) URL.revokeObjectURL(pf.thumbnailUrl);
+ }
+ }
+ return new Map();
+ });
setFailedSends(new Map());
+ setStableKeyMap(new Map());
lastSeqRef.current = 0;
setConnectionLost(false);
setHasMore(false);
@@ -1852,6 +2003,7 @@ export function useAgentChat(
napping,
pendingFilesByMessage,
failedSends,
+ stableKeyMap,
// derived
agentArtifacts,
agentName,
diff --git a/src/web/src/hooks/use-file-attachments.ts b/src/web/src/hooks/use-file-attachments.ts
index 38df99c7f..549c9f306 100644
--- a/src/web/src/hooks/use-file-attachments.ts
+++ b/src/web/src/hooks/use-file-attachments.ts
@@ -1,25 +1,43 @@
-import React, { useCallback, useRef, useState } from "react";
+import React, { useCallback, useEffect, useRef, useState } from "react";
import { toast } from "sonner";
+import { generateThumbnail } from "../lib/image-thumbnail";
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB
-/**
- * Owns the composer's file-staging UI: the pending (not-yet-sent) files, the
- * hidden file-input ref, add/remove handlers, and drag-and-drop state.
- *
- * Extracted verbatim from agent-chat-view.tsx. The optimistic-send bookkeeping
- * (`failedSends`, `pendingFilesByMessage`) intentionally stays OWNED by the
- * component — send/retry read and write it — so this hook only manages the
- * staging concern. `pendingFiles` + `setPendingFiles` are returned so the
- * component's send/retry path (Tier 3) can clear/restore them exactly as before.
- *
- * Has no effects, so it carries no effect-order constraints.
- */
+export type PendingFile = {
+ file: File;
+ thumbnailUrl: string | null;
+ thumbnailBlob: Blob | null;
+};
+
+function revokeThumbnailUrls(files: PendingFile[]) {
+ for (const pf of files) {
+ if (pf.thumbnailUrl) URL.revokeObjectURL(pf.thumbnailUrl);
+ }
+}
+
export function useFileAttachments() {
- const [pendingFiles, setPendingFiles] = useState<File[]>([]);
+ const [pendingFiles, _setPendingFiles] = useState<PendingFile[]>([]);
+ const pendingFilesRef = useRef(pendingFiles);
+ pendingFilesRef.current = pendingFiles;
+
+ const setPendingFiles = useCallback((next: PendingFile[] | ((prev: PendingFile[]) => PendingFile[])) => {
+ _setPendingFiles((prev) => {
+ const nextVal = typeof next === "function" ? next(prev) : next;
+ if (nextVal.length === 0 && prev.length > 0) {
+ revokeThumbnailUrls(prev);
+ }
+ return nextVal;
+ });
+ }, []);
+
+ useEffect(() => {
+ return () => revokeThumbnailUrls(pendingFilesRef.current);
+ }, []);
+
const fileInputRef = useRef<HTMLInputElement>(null);
- const addPendingFiles = useCallback((files: File[]) => {
+ const addPendingFiles = useCallback(async (files: File[]) => {
const valid: File[] = [];
for (const file of files) {
if (file.size > MAX_FILE_SIZE) {
@@ -28,9 +46,17 @@ export function useFileAttachments() {
valid.push(file);
}
}
- if (valid.length > 0) {
- setPendingFiles((prev) => [...prev, ...valid]);
- }
+ if (valid.length === 0) return;
+
+ const pending: PendingFile[] = await Promise.all(
+ valid.map(async (file) => {
+ const blob = await generateThumbnail(file);
+ const thumbnailUrl = blob ? URL.createObjectURL(blob) : null;
+ return { file, thumbnailUrl, thumbnailBlob: blob };
+ }),
+ );
+
+ _setPendingFiles((prev) => [...prev, ...pending]);
}, []);
const handleFileSelect = useCallback(
@@ -38,14 +64,17 @@ export function useFileAttachments() {
const fileList = e.target.files;
if (!fileList) return;
addPendingFiles(Array.from(fileList));
- // Reset input so re-selecting the same file works
e.target.value = "";
},
[addPendingFiles],
);
const removePendingFile = useCallback((index: number) => {
- setPendingFiles((prev) => prev.filter((_, i) => i !== index));
+ _setPendingFiles((prev) => {
+ const removed = prev[index];
+ if (removed?.thumbnailUrl) URL.revokeObjectURL(removed.thumbnailUrl);
+ return prev.filter((_, i) => i !== index);
+ });
}, []);
const [dragging, setDragging] = useState(false);
diff --git a/src/web/src/lib/api.ts b/src/web/src/lib/api.ts
index 26f44216a..3b0a327a7 100644
--- a/src/web/src/lib/api.ts
+++ b/src/web/src/lib/api.ts
@@ -30,6 +30,7 @@ import type {
Workspace,
} from "@alook/shared";
import { ApiError } from "@/lib/errors";
+import type { PendingFile } from "@/hooks/use-file-attachments";
const API_BASE = "";
@@ -367,7 +368,7 @@ export const sendMessage = async (
conversationId: string,
content: string,
workspaceId: string,
- files?: File[],
+ files?: PendingFile[],
metadata?: Record<string, unknown>,
): Promise<{ message: Message; task: TaskApi }> => {
if (!files || files.length === 0) {
@@ -383,8 +384,12 @@ export const sendMessage = async (
const fd = new FormData();
fd.append("content", content);
if (metadata) fd.append("metadata", JSON.stringify(metadata));
- for (const file of files) {
- fd.append("file", file);
+ for (const pf of files) {
+ fd.append("file", pf.file);
+ }
+ for (let i = 0; i < files.length; i++) {
+ const blob = files[i].thumbnailBlob;
+ if (blob) fd.append(`thumbnail:${i}`, blob, "thumbnail.jpg");
}
let res: Response;
diff --git a/src/web/src/lib/image-thumbnail.ts b/src/web/src/lib/image-thumbnail.ts
new file mode 100644
index 000000000..7b2e15296
--- /dev/null
+++ b/src/web/src/lib/image-thumbnail.ts
@@ -0,0 +1,44 @@
+const MAX_SIZE = 200;
+
+export async function generateThumbnail(file: File): Promise<Blob | null> {
+ if (!file.type.startsWith("image/") || file.type === "image/svg+xml") return null;
+
+ let objectUrl: string | undefined;
+ try {
+ objectUrl = URL.createObjectURL(file);
+ const img = await loadImage(objectUrl);
+ const { w, h } = fitWithin(img.naturalWidth, img.naturalHeight, MAX_SIZE);
+
+ const canvas = document.createElement("canvas");
+ canvas.width = w;
+ canvas.height = h;
+ const ctx = canvas.getContext("2d");
+ if (!ctx) return null;
+ ctx.drawImage(img, 0, 0, w, h);
+
+ return await canvasToBlob(canvas, "image/jpeg", 0.7);
+ } catch {
+ return null;
+ } finally {
+ if (objectUrl) URL.revokeObjectURL(objectUrl);
+ }
+}
+
+function loadImage(src: string): Promise<HTMLImageElement> {
+ return new Promise((resolve, reject) => {
+ const img = new Image();
+ img.onload = () => resolve(img);
+ img.onerror = reject;
+ img.src = src;
+ });
+}
+
+function fitWithin(srcW: number, srcH: number, max: number) {
+ if (srcW <= max && srcH <= max) return { w: srcW, h: srcH };
+ const scale = Math.min(max / srcW, max / srcH);
+ return { w: Math.round(srcW * scale), h: Math.round(srcH * scale) };
+}
+
+function canvasToBlob(canvas: HTMLCanvasElement, type: string, quality: number): Promise<Blob | null> {
+ return new Promise((resolve) => canvas.toBlob(resolve, type, quality));
+}