Skip to content

Commit af6e530

Browse files
authored
fix(session): reduce inline image churn in chat (anomalyco#925)
1 parent 6acc6f7 commit af6e530

5 files changed

Lines changed: 121 additions & 52 deletions

File tree

‎packages/app/src/app/app.tsx‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ import { createSystemState } from "./system-state";
138138
import { relaunch } from "@tauri-apps/plugin-process";
139139
import { fetch as tauriFetch } from "@tauri-apps/plugin-http";
140140
import { createSessionStore } from "./context/session";
141+
142+
const fileToDataUrl = (file: File) =>
143+
new Promise<string>((resolve, reject) => {
144+
const reader = new FileReader();
145+
reader.onerror = () => reject(new Error(`Failed to read attachment: ${file.name}`));
146+
reader.onload = () => {
147+
const result = typeof reader.result === "string" ? reader.result : "";
148+
resolve(result);
149+
};
150+
reader.readAsDataURL(file);
151+
});
141152
import { createExtensionsStore } from "./context/extensions";
142153
import { useGlobalSync } from "./context/global-sync";
143154
import { createWorkspaceStore } from "./context/workspace";
@@ -1448,7 +1459,14 @@ export default function App() {
14481459

14491460
type PartInput = TextPartInput | FilePartInput | AgentPartInput | SubtaskPartInput;
14501461

1451-
const buildPromptParts = (draft: ComposerDraft): PartInput[] => {
1462+
const attachmentToFilePart = async (attachment: ComposerAttachment): Promise<FilePartInput> => ({
1463+
type: "file",
1464+
url: await fileToDataUrl(attachment.file),
1465+
filename: attachment.name,
1466+
mime: attachment.mimeType,
1467+
});
1468+
1469+
const buildPromptParts = async (draft: ComposerDraft): Promise<PartInput[]> => {
14521470
const parts: PartInput[] = [];
14531471
const text = draft.resolvedText ?? draft.text;
14541472
parts.push({ type: "text", text } as TextPartInput);
@@ -1488,19 +1506,12 @@ export default function App() {
14881506
}
14891507
}
14901508

1491-
for (const attachment of draft.attachments) {
1492-
parts.push({
1493-
type: "file",
1494-
url: attachment.dataUrl,
1495-
filename: attachment.name,
1496-
mime: attachment.mimeType,
1497-
} as FilePartInput);
1498-
}
1509+
parts.push(...(await Promise.all(draft.attachments.map(attachmentToFilePart))));
14991510

15001511
return parts;
15011512
};
15021513

1503-
const buildCommandFileParts = (draft: ComposerDraft): FilePartInput[] => {
1514+
const buildCommandFileParts = async (draft: ComposerDraft): Promise<FilePartInput[]> => {
15041515
const parts: FilePartInput[] = [];
15051516
const root = workspaceProjectDir().trim();
15061517

@@ -1531,14 +1542,7 @@ export default function App() {
15311542
} as FilePartInput);
15321543
}
15331544

1534-
for (const attachment of draft.attachments) {
1535-
parts.push({
1536-
type: "file",
1537-
url: attachment.dataUrl,
1538-
filename: attachment.name,
1539-
mime: attachment.mimeType,
1540-
} as FilePartInput);
1541-
}
1545+
parts.push(...(await Promise.all(draft.attachments.map(attachmentToFilePart))));
15421546

15431547
return parts;
15441548
};
@@ -1682,7 +1686,7 @@ export default function App() {
16821686

16831687
const model = selectedSessionModel();
16841688
const agent = selectedSessionAgent();
1685-
const parts = buildPromptParts(resolvedDraft);
1689+
const parts = await buildPromptParts(resolvedDraft);
16861690
const selectedVariant = modelVariant() ?? undefined;
16871691
const reasoningEffort = resolveCodexReasoningEffort(model.modelID, selectedVariant ?? null);
16881692
const requestVariant = reasoningEffort ? undefined : selectedVariant;
@@ -1710,7 +1714,7 @@ export default function App() {
17101714

17111715
// Slash command: route through session.command() API
17121716
const modelString = `${model.providerID}/${model.modelID}`;
1713-
const files = buildCommandFileParts(resolvedDraft);
1717+
const files = await buildCommandFileParts(resolvedDraft);
17141718

17151719
// session.command() expects `model` as a provider/model string and only supports file parts.
17161720
unwrap(

‎packages/app/src/app/components/part-view.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ function createCustomRenderer(tone: "light" | "dark") {
470470
src="${safeHref}"
471471
alt="${escapeHtml(text || "")}"
472472
${safeTitle ? `title="${safeTitle}"` : ""}
473+
loading="lazy"
474+
decoding="async"
473475
class="max-w-full h-auto rounded-lg my-4"
474476
/>
475477
`;
@@ -1071,6 +1073,8 @@ export default function PartView(props: Props) {
10711073
<img
10721074
src={image.src}
10731075
alt={image.alt || ""}
1076+
loading="lazy"
1077+
decoding="async"
10741078
class="max-w-full h-auto rounded-lg border border-gray-6/50"
10751079
/>
10761080
)}
@@ -1126,6 +1130,8 @@ export default function PartView(props: Props) {
11261130
<img
11271131
src={inlineImage()!}
11281132
alt=""
1133+
loading="lazy"
1134+
decoding="async"
11291135
class="max-w-full h-auto rounded-xl border border-gray-6/50"
11301136
/>
11311137
</Match>

‎packages/app/src/app/components/session/composer.tsx‎

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,12 @@ const formatLinks = (links: Array<{ name: string; target: string }>) =>
138138
.map((entry) => `[${escapeMarkdownLabel(entry.name || "file")}](${entry.target})`)
139139
.join("\n");
140140

141-
const fileToDataUrl = (file: File) =>
142-
new Promise<string>((resolve, reject) => {
143-
const reader = new FileReader();
144-
reader.onerror = () => reject(new Error("Failed to read attachment"));
145-
reader.onload = () => {
146-
const result = typeof reader.result === "string" ? reader.result : "";
147-
resolve(result);
148-
};
149-
reader.readAsDataURL(file);
150-
});
141+
const estimateInlineAttachmentBytes = (file: Blob) => {
142+
const mimeType = file.type || "application/octet-stream";
143+
const prefixBytes = `data:${mimeType};base64,`.length;
144+
const base64Bytes = Math.ceil(file.size / 3) * 4;
145+
return prefixBytes + base64Bytes + 512;
146+
};
151147

152148
/**
153149
* Compress an image file to JPEG using OffscreenCanvas (off main thread when possible).
@@ -463,6 +459,11 @@ export default function Composer(props: ComposerProps) {
463459
objectUrls.add(url);
464460
return url;
465461
};
462+
const releaseObjectUrl = (url?: string) => {
463+
if (!url) return;
464+
if (!objectUrls.delete(url)) return;
465+
URL.revokeObjectURL(url);
466+
};
466467
// Track IME composition state so we can combine it with keyCode === 229 to
467468
// reliably suppress Enter during CJK input across Chrome, Safari, and WebKit.
468469
let imeComposing = false;
@@ -491,6 +492,23 @@ export default function Composer(props: ComposerProps) {
491492
objectUrls.clear();
492493
});
493494

495+
const clearSentAttachments = () => {
496+
const current = attachments();
497+
for (const attachment of current) {
498+
releaseObjectUrl(attachment.previewUrl);
499+
}
500+
setAttachments([]);
501+
};
502+
503+
const removeAttachment = (attachmentId: string) => {
504+
setAttachments((current: ComposerAttachment[]) => {
505+
const target = current.find((item) => item.id === attachmentId);
506+
releaseObjectUrl(target?.previewUrl);
507+
return current.filter((item) => item.id !== attachmentId);
508+
});
509+
emitDraftChange();
510+
};
511+
494512
const createPasteSpan = (part: Extract<ComposerPart, { type: "paste" }>) => {
495513
pasteTextById.set(part.id, part.text);
496514
const span = document.createElement("span");
@@ -1047,7 +1065,7 @@ export default function Composer(props: ComposerProps) {
10471065
props.onSend(draft);
10481066
setSlashOpen(false);
10491067
setSlashQuery("");
1050-
setAttachments([]);
1068+
clearSentAttachments();
10511069
setEditorText("");
10521070
emitDraftChange();
10531071
queueMicrotask(() => focusEditorEnd());
@@ -1087,11 +1105,9 @@ export default function Composer(props: ComposerProps) {
10871105
continue;
10881106
}
10891107
try {
1090-
// Compress images before encoding to data URL
1108+
// Compress images before keeping them in local draft state.
10911109
const processed = isImageMime(file.type) ? await compressImageFile(file) : file;
1092-
const dataUrl = await fileToDataUrl(processed);
1093-
// Pre-check: data URL will be embedded in JSON body; reject if too large
1094-
const estimatedJsonBytes = dataUrl.length + 512; // data URL + JSON overhead
1110+
const estimatedJsonBytes = estimateInlineAttachmentBytes(processed);
10951111
if (estimatedJsonBytes > MAX_ATTACHMENT_BYTES) {
10961112
props.onToast(`${file.name} is too large after encoding. Try a smaller image.`);
10971113
continue;
@@ -1102,7 +1118,8 @@ export default function Composer(props: ComposerProps) {
11021118
mimeType: processed.type || "application/octet-stream",
11031119
size: processed.size,
11041120
kind: isImageMime(processed.type) ? "image" : "file",
1105-
dataUrl,
1121+
file: processed,
1122+
previewUrl: isImageMime(processed.type) ? createObjectUrl(processed) : undefined,
11061123
});
11071124
} catch (error) {
11081125
props.onToast(error instanceof Error ? error.message : "Failed to read attachment");
@@ -1683,7 +1700,12 @@ export default function Composer(props: ComposerProps) {
16831700
fallback={<FileIcon size={14} class="text-gray-9" />}
16841701
>
16851702
<div class="h-10 w-10 rounded-xl bg-gray-1 overflow-hidden border border-gray-6">
1686-
<img src={attachment.dataUrl} alt={attachment.name} class="h-full w-full object-cover" />
1703+
<img
1704+
src={attachment.previewUrl!}
1705+
alt={attachment.name}
1706+
decoding="async"
1707+
class="h-full w-full object-cover"
1708+
/>
16871709
</div>
16881710
</Show>
16891711
<div class="max-w-[160px]">
@@ -1695,12 +1717,7 @@ export default function Composer(props: ComposerProps) {
16951717
<button
16961718
type="button"
16971719
class="ml-1 rounded-full p-1 text-gray-10 hover:text-gray-11 hover:bg-gray-4"
1698-
onClick={() => {
1699-
setAttachments((current: ComposerAttachment[]) =>
1700-
current.filter((item) => item.id !== attachment.id)
1701-
);
1702-
emitDraftChange();
1703-
}}
1720+
onClick={() => removeAttachment(attachment.id)}
17041721
>
17051722
<X size={12} />
17061723
</button>

‎packages/app/src/app/components/session/message-list.tsx‎

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ type MessageBlock = {
7676
kind: "message";
7777
message: MessageWithParts;
7878
renderableParts: Part[];
79+
attachments: Array<{
80+
url: string;
81+
filename: string;
82+
mime: string;
83+
}>;
7984
groups: MessageGroup[];
8085
isUser: boolean;
8186
messageId: string;
@@ -392,14 +397,16 @@ function toolHeadline(part: Part) {
392397
export default function MessageList(props: MessageListProps) {
393398
const [copyingId, setCopyingId] = createSignal<string | null>(null);
394399
let previousMessagePartCountById = new Map<string, number>();
400+
let previousMessageBlockById = new Map<string, MessageBlock>();
401+
let previousBlockRenderKey = "";
395402
let copyTimeout: number | undefined;
396403
const isAttachmentPart = (part: Part) => {
397404
if (part.type !== "file") return false;
398405
const url = (part as { url?: string }).url;
399406
return typeof url === "string" && !url.startsWith("file://");
400407
};
401-
const attachmentsForMessage = (message: MessageWithParts) =>
402-
message.parts
408+
const attachmentsForParts = (parts: Part[]) =>
409+
parts
403410
.filter(isAttachmentPart)
404411
.map((part) => {
405412
const record = part as {
@@ -505,8 +512,10 @@ export default function MessageList(props: MessageListProps) {
505512

506513
const messageBlocks = createMemo<MessageBlockItem[]>(() => {
507514
const startedAt = perfNow();
515+
const renderKey = `${props.developerMode ? 1 : 0}:${props.showThinking ? 1 : 0}`;
508516
const blocks: MessageBlockItem[] = [];
509517
const nextMessagePartCountById = new Map<string, number>();
518+
const nextMessageBlockById = new Map<string, MessageBlock>();
510519
let changedMessageCount = 0;
511520
let addedMessageCount = 0;
512521
let toolPartCount = 0;
@@ -521,19 +530,44 @@ export default function MessageList(props: MessageListProps) {
521530
const totalParts = message.parts.length;
522531
nextMessagePartCountById.set(idKey, totalParts);
523532
const previousPartCount = previousMessagePartCountById.get(idKey);
533+
const previousBlock = previousMessageBlockById.get(idKey);
524534
if (previousPartCount === undefined) {
525535
addedMessageCount += 1;
526536
} else if (previousPartCount !== totalParts) {
527537
changedMessageCount += 1;
528538
}
529539

540+
const isUser = (message.info as any).role === "user";
541+
const canReuseStableBlock =
542+
previousBlockRenderKey === renderKey &&
543+
index < props.messages.length - 1 &&
544+
previousPartCount !== undefined &&
545+
previousPartCount === totalParts &&
546+
previousBlock?.kind === "message" &&
547+
previousBlock.isUser === isUser;
548+
549+
if (canReuseStableBlock && previousBlock) {
550+
toolPartCount += previousBlock.renderableParts.reduce(
551+
(count, part) => (part.type === "tool" ? count + 1 : count),
552+
0,
553+
);
554+
stepGroupCount += previousBlock.groups.reduce(
555+
(count, group) => (group.kind === "steps" ? count + 1 : count),
556+
0,
557+
);
558+
blocks.push(previousBlock);
559+
nextMessageBlockById.set(idKey, previousBlock);
560+
return;
561+
}
562+
530563
toolPartCount += renderableParts.reduce(
531564
(count, part) => (part.type === "tool" ? count + 1 : count),
532565
0,
533566
);
534567
const groupId = String((message.info as any).id ?? "message");
535-
const groups = groupMessageParts(renderableParts, groupId);
536-
const isUser = (message.info as any).role === "user";
568+
const attachments = attachmentsForParts(renderableParts);
569+
const nonAttachmentParts = renderableParts.filter((part) => !isAttachmentPart(part));
570+
const groups = groupMessageParts(nonAttachmentParts, groupId);
537571
const isStepsOnly =
538572
groups.length > 0 && groups.every((group) => group.kind === "steps");
539573
const stepGroups = isStepsOnly
@@ -565,14 +599,17 @@ export default function MessageList(props: MessageListProps) {
565599
return;
566600
}
567601

568-
blocks.push({
602+
const block: MessageBlock = {
569603
kind: "message",
570604
message,
571605
renderableParts,
606+
attachments,
572607
groups,
573608
isUser,
574609
messageId,
575-
});
610+
};
611+
blocks.push(block);
612+
nextMessageBlockById.set(idKey, block);
576613
});
577614

578615
let removedMessageCount = 0;
@@ -582,6 +619,8 @@ export default function MessageList(props: MessageListProps) {
582619
}
583620
});
584621
previousMessagePartCountById = nextMessagePartCountById;
622+
previousMessageBlockById = nextMessageBlockById;
623+
previousBlockRenderKey = renderKey;
585624

586625
const elapsedMs = Math.round((perfNow() - startedAt) * 100) / 100;
587626
if (
@@ -926,15 +965,15 @@ export default function MessageList(props: MessageListProps) {
926965
: "w-full relative max-w-[760px] text-[15px] leading-[1.72] text-dls-text antialiased group"
927966
} ${searchOutlineClass}`}
928967
>
929-
<Show when={attachmentsForMessage(block.message).length > 0}>
968+
<Show when={block.attachments.length > 0}>
930969
<div
931970
class={
932971
block.isUser
933972
? "mb-3 flex flex-wrap gap-2"
934973
: "mb-4 flex flex-wrap gap-2"
935974
}
936975
>
937-
<For each={attachmentsForMessage(block.message)}>
976+
<For each={block.attachments}>
938977
{(attachment) => (
939978
<div class="flex items-center gap-2 rounded-[18px] border border-dls-border bg-dls-surface px-3 py-2 text-xs text-gray-11 shadow-[var(--dls-card-shadow)]">
940979
<Show
@@ -945,6 +984,8 @@ export default function MessageList(props: MessageListProps) {
945984
<img
946985
src={attachment.url}
947986
alt={attachment.filename}
987+
loading="lazy"
988+
decoding="async"
948989
class="h-full w-full object-cover"
949990
/>
950991
</div>

‎packages/app/src/app/types.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ export type ComposerAttachment = {
9696
mimeType: string;
9797
size: number;
9898
kind: "image" | "file";
99-
dataUrl: string;
99+
file: File;
100+
previewUrl?: string;
100101
};
101102

102103
export type SlashCommandOption = {

0 commit comments

Comments
 (0)