Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/ui/src/components/session-viewer/rendering.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
ReasoningContent,
} from "@/components/ai-elements/reasoning";
import {
mergeAdjacentTextBlocks,
normalizeToolName,
} from "@/components/session-viewer/utils";
import type { SubAgentTurn, NestedToolCalls } from "@/components/session-viewer/types";
Expand Down Expand Up @@ -176,9 +177,10 @@ export function renderContent(
}

if (Array.isArray(content)) {
const blocks = mergeAdjacentTextBlocks(content);
return (
<div className="flex flex-col gap-2">
{content.map((block, i) => {
{blocks.map((block, i) => {
if (!block || typeof block !== "object") {
return (
<pre
Expand Down Expand Up @@ -210,7 +212,7 @@ export function renderContent(
}

if (b.type === "thinking") {
const isLastBlock = i === (content as unknown[]).length - 1;
const isLastBlock = i === blocks.length - 1;
const thinkingIsStreaming = isLastBlock && (isThinkingActive ?? false);
const thinkingDuration = typeof b.durationSeconds === "number" ? b.durationSeconds : undefined;
const thinkingText = typeof b.thinking === "string" ? b.thinking : "";
Expand Down
23 changes: 23 additions & 0 deletions packages/ui/src/components/session-viewer/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test";
import {
detectInFlightTools,
hasVisibleContent,
mergeAdjacentTextBlocks,
tryParseJsonObject,
normalizeToolName,
extractTextFromToolContent,
Expand Down Expand Up @@ -562,3 +563,25 @@ describe("resolveCommandPopoverState", () => {
expect(resolveCommandPopoverState("mcp re", knownWithMcp, keepOpenWithMcp)).toEqual({ open: true, query: "mcp re" });
});
});

describe("mergeAdjacentTextBlocks", () => {
test("joins citation-split text fragments, keeps other blocks separate", () => {
const search = { type: "text", text: "", _webSearchResult: {} };
const tool = { type: "toolCall", name: "x" };
expect(mergeAdjacentTextBlocks([
{ type: "text", text: "says a neuron " },
{ type: "text", text: "\"can predict\"" },
{ type: "text", text: ", and more." },
search,
{ type: "text", text: "after" },
tool,
{ type: "text", text: "end" },
])).toEqual([
{ type: "text", text: "says a neuron \"can predict\", and more." },
search,
{ type: "text", text: "after" },
tool,
{ type: "text", text: "end" },
]);
});
});
20 changes: 20 additions & 0 deletions packages/ui/src/components/session-viewer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ export function hasVisibleContent(content: unknown): boolean {
return true;
}

/**
* Anthropic splits text at every citation boundary into separate text blocks.
* Glue adjacent plain text blocks so they render as one flow instead of one
* paragraph per fragment. Server-tool metadata blocks are left alone.
*/
export function mergeAdjacentTextBlocks(content: unknown[]): unknown[] {
const isPlainText = (b: unknown): b is { type: "text"; text: string } => {
if (!b || typeof b !== "object") return false;
const r = b as Record<string, unknown>;
return r.type === "text" && typeof r.text === "string" && !r._serverToolUse && !r._webSearchResult;
};
const out: unknown[] = [];
for (const block of content) {
const prev = out[out.length - 1];
if (isPlainText(block) && isPlainText(prev)) out[out.length - 1] = { ...prev, text: prev.text + block.text };
else out.push(block);
}
return out;
}

export function tryParseJsonObject(text: string): Record<string, unknown> | null {
try {
const parsed = JSON.parse(text);
Expand Down
Loading