@@ -65,6 +65,55 @@ interface AssistantRenderState {
6565 errorMessage?: string;
6666 }
6767
68+type ConversationMessage = AppState["messages"][number];
69+
70+type ToolCallRenderInfo = {
71+ name: string;
72+ args: Record<string, unknown>;
73+};
74+
75+interface ConversationRenderCache {
76+ messages: readonly ConversationMessage[] | null;
77+ count: number;
78+ showReasoning: boolean;
79+ verbose: boolean;
80+ theme: Theme | null;
81+ nodes: Node[];
82+ toolCallArgs: Map<string, ToolCallRenderInfo>;
83+}
84+
85+const EMPTY_STREAMING_CONTENT: AssistantMessage["content"] = [];
86+const EMPTY_PENDING_TOOL_RESULTS: readonly PendingToolResult[] = [];
87+const EMPTY_RENDER_NODES: Node[] = [];
88+const EMPTY_TOOL_RESULT_ARGS: Record<string, unknown> = Object.freeze({});
89+
90+const conversationRenderCache: ConversationRenderCache = {
91+ messages: null,
92+ count: 0,
93+ showReasoning: false,
94+ verbose: false,
95+ theme: null,
96+ nodes: EMPTY_RENDER_NODES,
97+ toolCallArgs: new Map(),
98+};
99+
100+let editToolDiffCache = new WeakMap<
101+ Record<string, unknown>,
102+ ToolRenderLine[]
103+>();
104+
105+/** Reset cached committed conversation renders and derived edit diffs. */
106+export function resetConversationRenderCache(): void {
107+ conversationRenderCache.messages = null;
108+ conversationRenderCache.count = 0;
109+ conversationRenderCache.showReasoning = false;
110+ conversationRenderCache.verbose = false;
111+ conversationRenderCache.theme = null;
112+ conversationRenderCache.nodes = EMPTY_RENDER_NODES;
113+ conversationRenderCache.toolCallArgs = new Map();
114+ editToolDiffCache = new WeakMap();
115+}
116+
68117 /** Semantic line kinds used when rendering tool output in the log. */
69118 type ToolRenderLineKind =
70119 | "command"
@@ -261,10 +310,137 @@ function buildShellToolLines(
261310 );
262311 }
263312
313+function splitDiffSourceLines(text: string): string[] {
314+ if (text === "") {
315+ return [];
316+ }
317+
318+ const lines = text.split("\n");
319+ if (lines.at(-1) === "") {
320+ lines.pop();
321+ }
322+ return lines;
323+}
324+
325+function findSharedPrefixLineCount(
326+ oldLines: readonly string[],
327+ newLines: readonly string[],
328+): number {
329+ let prefixCount = 0;
330+ while (
331+ prefixCount < oldLines.length &&
332+ prefixCount < newLines.length &&
333+ oldLines[prefixCount] === newLines[prefixCount]
334+ ) {
335+ prefixCount += 1;
336+ }
337+ return prefixCount;
338+}
339+
340+function findSharedSuffixBounds(
341+ oldLines: readonly string[],
342+ newLines: readonly string[],
343+ prefixCount: number,
344+): { oldSuffixIndex: number; newSuffixIndex: number } {
345+ let oldSuffixIndex = oldLines.length - 1;
346+ let newSuffixIndex = newLines.length - 1;
347+
348+ while (
349+ oldSuffixIndex >= prefixCount &&
350+ newSuffixIndex >= prefixCount &&
351+ oldLines[oldSuffixIndex] === newLines[newSuffixIndex]
352+ ) {
353+ oldSuffixIndex -= 1;
354+ newSuffixIndex -= 1;
355+ }
356+
357+ return { oldSuffixIndex, newSuffixIndex };
358+}
359+
360+function hasSharedMiddleLine(
361+ removedLines: readonly string[],
362+ addedLines: readonly string[],
363+): boolean {
364+ const largerMiddle =
365+ removedLines.length >= addedLines.length ? removedLines : addedLines;
366+ const smallerMiddle =
367+ removedLines.length >= addedLines.length ? addedLines : removedLines;
368+ const largerMiddleSet = new Set(largerMiddle);
369+ return smallerMiddle.some((line) => largerMiddleSet.has(line));
370+}
371+
372+function pushPrefixedDiffLines(
373+ lines: ToolRenderLine[],
374+ content: readonly string[],
375+ kind: "text" | "diffAdded" | "diffRemoved",
376+ prefix: string,
377+): void {
378+ for (const line of content) {
379+ lines.push({ kind, text: `${prefix}${line}` });
380+ }
381+}
382+
383+function buildSimpleStructuredDiffLines(
384+ oldText: string,
385+ newText: string,
386+): ToolRenderLine[] | null {
387+ const context = 2;
388+ const oldLines = splitDiffSourceLines(oldText);
389+ const newLines = splitDiffSourceLines(newText);
390+ const prefixCount = findSharedPrefixLineCount(oldLines, newLines);
391+ const { oldSuffixIndex, newSuffixIndex } = findSharedSuffixBounds(
392+ oldLines,
393+ newLines,
394+ prefixCount,
395+ );
396+ const removedLines = oldLines.slice(prefixCount, oldSuffixIndex + 1);
397+ const addedLines = newLines.slice(prefixCount, newSuffixIndex + 1);
398+
399+ if (removedLines.length === 0 && addedLines.length === 0) {
400+ return [];
401+ }
402+ if (hasSharedMiddleLine(removedLines, addedLines)) {
403+ return null;
404+ }
405+
406+ const contextStart = Math.max(0, prefixCount - context);
407+ const unchangedPrefix = oldLines.slice(contextStart, prefixCount);
408+ const unchangedSuffix = oldLines.slice(
409+ oldSuffixIndex + 1,
410+ Math.min(oldLines.length, oldSuffixIndex + 1 + context),
411+ );
412+ const oldStart =
413+ contextStart === 0 ? (removedLines.length > 0 ? 1 : 0) : contextStart + 1;
414+ const newStart =
415+ contextStart === 0 ? (addedLines.length > 0 ? 1 : 0) : contextStart + 1;
416+ const oldLineCount =
417+ unchangedPrefix.length + removedLines.length + unchangedSuffix.length;
418+ const newLineCount =
419+ unchangedPrefix.length + addedLines.length + unchangedSuffix.length;
420+ const diffLines: ToolRenderLine[] = [
421+ {
422+ kind: "text",
423+ text: `@@ -${oldStart},${oldLineCount} +${newStart},${newLineCount} @@`,
424+ },
425+ ];
426+
427+ pushPrefixedDiffLines(diffLines, unchangedPrefix, "text", " ");
428+ pushPrefixedDiffLines(diffLines, removedLines, "diffRemoved", "-");
429+ pushPrefixedDiffLines(diffLines, addedLines, "diffAdded", "+");
430+ pushPrefixedDiffLines(diffLines, unchangedSuffix, "text", " ");
431+
432+ return diffLines;
433+}
434+
264435 function buildStructuredDiffLines(
265436 oldText: string,
266437 newText: string,
267438 ): ToolRenderLine[] {
439+ const fastDiffLines = buildSimpleStructuredDiffLines(oldText, newText);
440+ if (fastDiffLines !== null) {
441+ return fastDiffLines;
442+ }
443+
268444 const patch = structuredPatch("", "", oldText, newText, "", "", {
269445 context: 2,
270446 });
@@ -290,6 +466,22 @@ function buildStructuredDiffLines(
290466 return diffLines;
291467 }
292468
469+function getCachedEditToolDiffLines(
470+ args: Record<string, unknown>,
471+): ToolRenderLine[] {
472+ const cached = editToolDiffCache.get(args);
473+ if (cached) {
474+ return cached;
475+ }
476+
477+ const diffLines = buildStructuredDiffLines(
478+ getToolArgString(args, "oldText"),
479+ getToolArgString(args, "newText"),
480+ );
481+ editToolDiffCache.set(args, diffLines);
482+ return diffLines;
483+}
484+
293485 function splitPrefixedToolTextLines(
294486 text: string,
295487 kind: "diffAdded" | "diffRemoved",
@@ -312,9 +504,7 @@ function splitPrefixedToolTextLines(
312504
313505 /** Build the logical render lines for an edit tool result. */
314506 function buildEditToolLines(
315- _filePath: string,
316- oldText: string,
317- newText: string,
507+ args: Record<string, unknown>,
318508 isError: boolean,
319509 resultText: string,
320510 verbose: boolean,
@@ -326,7 +516,7 @@ function buildEditToolLines(
326516 );
327517 }
328518
329- const diffLines = buildStructuredDiffLines(oldText, newText);
519+ const diffLines = getCachedEditToolDiffLines(args);
330520
331521 return previewToolRenderLines(
332522 diffLines.length > 0
@@ -516,9 +706,7 @@ function renderToolResultContent(
516706 if (toolName === "edit") {
517707 return renderToolBlock(
518708 buildEditToolLines(
519- getToolArgString(args, "path"),
520- getToolArgString(args, "oldText"),
521- getToolArgString(args, "newText"),
709+ args,
522710 isError,
523711 getToolContentText(content),
524712 opts.verbose,
@@ -574,8 +762,6 @@ function renderUiMessage(msg: UiMessage, theme: Theme): Node {
574762 ]);
575763 }
576764
577-type ConversationMessage = AppState["messages"][number];
578-
579765 function pushConversationNode(nodes: Node[], node: Node | null): void {
580766 if (!node) {
581767 return;
@@ -588,7 +774,7 @@ function pushConversationNode(nodes: Node[], node: Node | null): void {
588774
589775 function rememberToolCallArgs(
590776 message: AssistantMessage,
591- toolCallArgs: Map<string, { name: string; args: Record<string, unknown> }>,
777+ toolCallArgs: Map<string, ToolCallRenderInfo>,
592778 ): void {
593779 for (const block of message.content) {
594780 if (block.type !== "toolCall") {
@@ -614,7 +800,7 @@ function renderToolResultMessage(
614800 function renderConversationMessage(
615801 message: ConversationMessage,
616802 renderOpts: ConversationRenderOpts,
617- toolCallArgs: Map<string, { name: string; args: Record<string, unknown> }>,
803+ toolCallArgs: Map<string, ToolCallRenderInfo>,
618804 theme: Theme,
619805 ): Node | null {
620806 if (message.role === "ui") {
@@ -631,13 +817,66 @@ function renderConversationMessage(
631817 const info = toolCallArgs.get(message.toolCallId);
632818 return renderToolResultMessage(
633819 info?.name ?? message.toolName,
634- info?.args ?? {},
820+ info?.args ?? EMPTY_TOOL_RESULT_ARGS,
635821 message.content,
636822 message.isError,
637823 renderOpts,
638824 );
639825 }
640826
827+function canReuseCommittedConversationCache(
828+ state: Pick<AppState, "messages" | "showReasoning" | "verbose" | "theme">,
829+): boolean {
830+ return (
831+ conversationRenderCache.messages === state.messages &&
832+ conversationRenderCache.showReasoning === state.showReasoning &&
833+ conversationRenderCache.verbose === state.verbose &&
834+ conversationRenderCache.theme === state.theme &&
835+ conversationRenderCache.count <= state.messages.length
836+ );
837+}
838+
839+function cacheCommittedConversation(
840+ state: Pick<AppState, "messages" | "showReasoning" | "verbose" | "theme">,
841+ renderOpts: ConversationRenderOpts,
842+): void {
843+ if (!canReuseCommittedConversationCache(state)) {
844+ conversationRenderCache.messages = state.messages;
845+ conversationRenderCache.count = 0;
846+ conversationRenderCache.showReasoning = state.showReasoning;
847+ conversationRenderCache.verbose = state.verbose;
848+ conversationRenderCache.theme = state.theme;
849+ conversationRenderCache.nodes = [];
850+ conversationRenderCache.toolCallArgs = new Map();
851+ }
852+
853+ for (
854+ let index = conversationRenderCache.count;
855+ index < state.messages.length;
856+ index++
857+ ) {
858+ pushConversationNode(
859+ conversationRenderCache.nodes,
860+ renderConversationMessage(
861+ state.messages[index]!,
862+ renderOpts,
863+ conversationRenderCache.toolCallArgs,
864+ state.theme,
865+ ),
866+ );
867+ }
868+
869+ conversationRenderCache.count = state.messages.length;
870+}
871+
872+function hasStreamingTail(streaming: StreamingConversationState): boolean {
873+ return (
874+ (streaming.content !== EMPTY_STREAMING_CONTENT ||
875+ streaming.pendingToolResults !== EMPTY_PENDING_TOOL_RESULTS) &&
876+ (streaming.content.length > 0 || streaming.pendingToolResults.length > 0)
877+ );
878+}
Cut short: this commit's diff is over 1000 lines.