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
49 changes: 36 additions & 13 deletions apps/web/src/components/chat/MessagesTimeline.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
deriveMessagesTimelineRows,
normalizeCompactToolLabel,
resolveAssistantMessageCopyState,
type TimelineLatestTurn,
shouldPreserveAssistantLineBreaks,
} from "./MessagesTimeline.logic";

Expand Down Expand Up @@ -337,7 +338,7 @@ describe("deriveMessagesTimelineRows", () => {
},
},
],
expandedTurnIds: new Set(["turn-1" as never]),
expandedFoldKeys: new Set(["assistant-thought-entry"]),

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些旧测试原来用 turn id 展开一个已完成 turn 的折叠;现在已完成 turn 的折叠键是锚点 entry id,所以改成对应的 entry id。其余几处(544、556、728、1313、1536、1632、1780 行)同理,只是键值替换。

isWorking: false,
activeTurnStartedAt: null,
turnDiffSummaryByAssistantMessageId: new Map(),
Expand Down Expand Up @@ -540,7 +541,7 @@ describe("deriveMessagesTimelineRows", () => {
(row): row is Extract<(typeof collapsedRows)[number], { kind: "turn-fold" }> =>
row.kind === "turn-fold",
);
expect(foldRow?.turnId).toBe("turn-1");
expect(foldRow?.expandKey).toBe("assistant-first-entry");
expect(foldRow?.expanded).toBe(false);
// User message boundary (00:00:00) → terminal message updatedAt (00:00:22).
expect(foldRow?.label).toBe("Worked for 22s");
Expand All @@ -552,7 +553,7 @@ describe("deriveMessagesTimelineRows", () => {

const expandedRows = deriveMessagesTimelineRows({
timelineEntries,
expandedTurnIds: new Set(["turn-1" as never]),
expandedFoldKeys: new Set(["assistant-first-entry"]),
isWorking: false,
activeTurnStartedAt: null,
turnDiffSummaryByAssistantMessageId: new Map(),
Expand Down Expand Up @@ -724,7 +725,7 @@ describe("deriveMessagesTimelineRows", () => {
row.kind === "turn-fold",
);
// User message (00:00:00) → trailing work entry (00:00:12).
expect(foldRow?.turnId).toBe("turn-1");
expect(foldRow?.expandKey).toBe("assistant-commentary-entry");
expect(foldRow?.label).toBe("Worked for 12s");
});

Expand Down Expand Up @@ -759,7 +760,7 @@ describe("deriveMessagesTimelineRows", () => {
expect(rows).toEqual([
expect.objectContaining({
kind: "turn-fold",
turnId: "turn-1",
expandKey: "turn-1",
label: "You stopped after 47s",
expanded: false,
}),
Expand Down Expand Up @@ -1309,8 +1310,8 @@ describe("deriveMessagesTimelineRows", () => {
revertTurnCountByUserMessageId: new Map(),
});

expect(rows.filter((row) => row.kind === "turn-fold").map((row) => row.turnId)).toEqual([
"turn-1",
expect(rows.filter((row) => row.kind === "turn-fold").map((row) => row.expandKey)).toEqual([
"previous-work-entry",
]);
expect(rows.map((row) => row.id)).toContain("live-activity-row");
});
Expand Down Expand Up @@ -1477,6 +1478,28 @@ describe("deriveMessagesTimelineRows", () => {
row.kind === "message" && row.showsTurnFoldSeparator ? [row.id] : [],
),
).toEqual(["assistant-final-entry"]);

// Each side of the steer expands on its own.
const expandedSide = (keys: ReadonlyArray<string>, latestTurn?: TimelineLatestTurn) =>
deriveMessagesTimelineRows({
...baseFoldInput,
timelineEntries,
...(latestTurn ? { latestTurn } : {}),
expandedFoldKeys: new Set(keys),
})
.filter((row) => row.kind === "turn-fold")
.map((row) => row.kind === "turn-fold" && row.expanded);
expect(expandedSide(["work-1-entry"])).toEqual([true, false]);
expect(expandedSide(["work-2-entry"])).toEqual([false, true]);
// An interrupt knows only the turn id; that expands the side it cut short.
expect(
expandedSide(["turn-1"], {
turnId: "turn-1" as never,
state: "interrupted",
startedAt: "2026-01-01T00:00:00Z",
completedAt: "2026-01-01T00:00:30Z",
}),
).toEqual([false, true]);
Comment on lines +1481 to +1502

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新增断言:steer 前半段用自己的锚点键展开、后半段用自己的锚点键展开,两者互不影响;被打断时用 turn id 能展开被打断的那一段(后半段)。

});

it("keys the fold after a user message by the new turn even when old-turn residue lands first", () => {
Expand Down Expand Up @@ -1510,9 +1533,9 @@ describe("deriveMessagesTimelineRows", () => {
"turn-fold:work-late-entry",
"assistant-two-entry",
]);
expect(rows.filter((row) => row.kind === "turn-fold").map((row) => row.turnId)).toEqual([
"turn-1",
"turn-2",
expect(rows.filter((row) => row.kind === "turn-fold").map((row) => row.expandKey)).toEqual([
"work-1-entry",
"work-late-entry",
]);
});

Expand Down Expand Up @@ -1606,7 +1629,7 @@ describe("deriveMessagesTimelineRows", () => {
...baseFoldInput,
timelineEntries,
latestTurn,
expandedTurnIds: new Set(["turn-2" as never]),
expandedFoldKeys: new Set(["work-1-entry"]),
});
expect(expandedRows.map((row) => row.id)).toContain("work-toggle:work-1-entry");
expect(expandedRows.map((row) => row.id)).toContain("spawn-1-entry");
Expand Down Expand Up @@ -1670,7 +1693,7 @@ describe("deriveMessagesTimelineRows", () => {
// The segment's own entries end at 00:00:10, but this segment holds the
// latest turn's entries, so the end is latestTurn.completedAt (00:00:47).
expect(rows.filter((row) => row.kind === "turn-fold")).toEqual([
expect.objectContaining({ turnId: "turn-2", label: "You stopped after 47s" }),
expect.objectContaining({ expandKey: "turn-2", label: "You stopped after 47s" }),
]);
});

Expand Down Expand Up @@ -1754,7 +1777,7 @@ describe("deriveMessagesTimelineRows", () => {
},
},
],
expandedTurnIds: new Set(["turn-1" as never]),
expandedFoldKeys: new Set(["assistant-thought-entry"]),
isWorking: false,
activeTurnStartedAt: null,
turnDiffSummaryByAssistantMessageId: new Map(),
Expand Down
23 changes: 14 additions & 9 deletions apps/web/src/components/chat/MessagesTimeline.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export type MessagesTimelineRow =
kind: "turn-fold";
id: string;
createdAt: string;
turnId: TurnId;
expandKey: string;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

折叠行不再暴露 turnId,改为暴露 expandKey:它就是“展开集合”里对应这一行的那个键。组件点击时原样传回,不需要知道它是 turn id 还是 entry id。

label: string;
expanded: boolean;
}
Expand Down Expand Up @@ -338,7 +338,12 @@ function deriveTerminalAssistantMessageIds(timelineEntries: ReadonlyArray<Timeli
}

interface TurnFold {
turnId: TurnId;
/**
* What the expanded-folds set holds for this fold. Normally the anchor
* entry id, so each side of a steer expands on its own; the fold that an
* interrupt cut short keys by its turn id, which is all the interrupt knows.
*/
expandKey: string;
Comment on lines +341 to +346

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里定义了键的取法:默认用折叠里第一条被隐藏的 entry 的 id,所以同一个 turn 被 steer 切成的两段各有各的键。只有被 Stop 打断的那一段仍用 turn id,因为打断时组件只知道 turn id(见组件里的 effect)。

anchorEntryId: string;
createdAt: string;
hiddenEntryIds: ReadonlySet<string>;
Expand Down Expand Up @@ -407,7 +412,7 @@ function deriveTurnFolds(input: {
unsettledTurnId: TurnId | null;
}): ReadonlyMap<string, TurnFold> {
interface Segment {
/** Last turn id in the segment; folds expand per turn id. */
/** Last turn id in the segment. */

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释里删掉了“folds expand per turn id”,因为现在展开不再按 turn id。

turnId: TurnId | null;
entries: Array<TimelineEntry>;
/**
Expand Down Expand Up @@ -491,7 +496,7 @@ function deriveTurnFolds(input: {
: "Worked";

foldsByAnchorEntryId.set(firstHiddenEntry.id, {
turnId,
expandKey: turn?.state === "interrupted" ? turnId : firstHiddenEntry.id,

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

核心一行。turn 只对“最新 turn 的收尾段”非空(见上面的 latestTurnSegment),所以 turn?.state === "interrupted" 恰好只命中被打断的那一段;其余所有折叠都用锚点 entry id,键稳定且互不相同。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep the per-fold expansion key stable across pagination

Using firstHiddenEntry.id makes an expanded fold lose its identity when older history is prepended. mergeOlderPage prepends rows in packages/client-runtime/src/state/threads.ts, and the server explicitly permits a large fan-out group to be split across pages; loading the preceding chunk of that same user-message segment therefore selects an earlier firstHiddenEntry, while expandedFoldKeys still contains the old ID, so the open fold unexpectedly snaps closed. Derive the key from a stable segment boundary or migrate the expansion state when the anchor changes.

Useful? React with 👍 / 👎.

anchorEntryId: firstHiddenEntry.id,
createdAt: firstHiddenEntry.createdAt,
hiddenEntryIds,
Expand All @@ -506,7 +511,7 @@ export function deriveMessagesTimelineRows(input: {
timelineEntries: ReadonlyArray<TimelineEntry>;
latestTurn?: TimelineLatestTurn | null;
runningTurnId?: TurnId | null;
expandedTurnIds?: ReadonlySet<TurnId>;
expandedFoldKeys?: ReadonlySet<string>;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

入参从 expandedTurnIds(turn id 集合)改成 expandedFoldKeys(键集合),类型放宽到 string,因为键可能是 entry id。

expandedWorkGroupIds?: ReadonlySet<string>;
isWorking: boolean;
activeTurnStartedAt: string | null;
Expand All @@ -533,7 +538,7 @@ export function deriveMessagesTimelineRows(input: {
if (fold.terminalEntryId !== null) {
foldTerminalEntryIds.add(fold.terminalEntryId);
}
if (!input.expandedTurnIds?.has(fold.turnId)) {
if (!input.expandedFoldKeys?.has(fold.expandKey)) {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

判断“该不该隐藏折叠内容”时改用折叠自己的键。

for (const entryId of fold.hiddenEntryIds) {
collapsedEntryIds.add(entryId);
}
Expand Down Expand Up @@ -655,9 +660,9 @@ export function deriveMessagesTimelineRows(input: {
kind: "turn-fold",
id: `turn-fold:${anchoredTurnFold.anchorEntryId}`,
createdAt: anchoredTurnFold.createdAt,
turnId: anchoredTurnFold.turnId,
expandKey: anchoredTurnFold.expandKey,
label: anchoredTurnFold.label,
expanded: input.expandedTurnIds?.has(anchoredTurnFold.turnId) ?? false,
expanded: input.expandedFoldKeys?.has(anchoredTurnFold.expandKey) ?? false,
Comment on lines +663 to +665

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

折叠行输出改成携带 expandKey,expanded 也按同一个键判断。

});
}

Expand Down Expand Up @@ -868,7 +873,7 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): boolean
case "turn-fold": {
const bf = b as typeof a;
return (
a.turnId === bf.turnId &&
a.expandKey === bf.expandKey &&

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

行比较函数同步改比 expandKey:turnId 不再渲染,而点击回调用的是 expandKey,所以要比它,否则键变了行却被当成“没变”而复用旧的 onClick。

a.createdAt === bf.createdAt &&
a.label === bf.label &&
a.expanded === bf.expanded
Expand Down
24 changes: 12 additions & 12 deletions apps/web/src/components/chat/MessagesTimeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ interface TimelineRowSharedState {
onFileOpen: (attachment: ChatFileAttachment) => void;
openingVideoAttachmentId: string | null;
onOpenTurnDiff: (turnId: TurnId, filePath?: string) => void;
onToggleTurnFold: (turnId: TurnId, anchorKey: string) => void;
onToggleTurnFold: (foldKey: string, anchorKey: string) => void;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回调签名改成接收折叠键(string),组件不再关心它是 turn id 还是 entry id。

onToggleWorkGroup: (groupId: string, anchorKey: string) => void;
agentPanelModel: AgentPanelModel;
onOpenAgents: () => void;
Expand Down Expand Up @@ -314,7 +314,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
topFadeEnabled = false,
loadEarlier = null,
}: MessagesTimelineProps) {
const [expandedTurnIds, setExpandedTurnIds] = useState<ReadonlySet<TurnId>>(new Set());
const [expandedFoldKeys, setExpandedFoldKeys] = useState<ReadonlySet<string>>(new Set());

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

展开集合状态改名并放宽类型,存的是折叠键。

const [expandedWorkGroupIds, setExpandedWorkGroupIds] = useState<ReadonlySet<string>>(new Set());
const [disclosureToggleSettling, setDisclosureToggleSettling] = useState(false);
const [minimapStripMap] = useState(() => new Map<string, HTMLSpanElement>());
Expand Down Expand Up @@ -378,14 +378,14 @@ export const MessagesTimeline = memo(function MessagesTimeline({
);

const onToggleTurnFold = useCallback(
(turnId: TurnId, anchorKey: string) => {
(foldKey: string, anchorKey: string) => {
suspendEndScrollMaintenanceForDisclosure(anchorKey);
setExpandedTurnIds((existing) => {
setExpandedFoldKeys((existing) => {
const next = new Set(existing);
if (next.has(turnId)) {
next.delete(turnId);
if (next.has(foldKey)) {
next.delete(foldKey);
} else {
next.add(turnId);
next.add(foldKey);
Comment on lines +381 to +388

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

点击切换逻辑本身没变,只是把 turnId 换成 foldKey。

}
return next;
});
Expand Down Expand Up @@ -419,15 +419,15 @@ export const MessagesTimeline = memo(function MessagesTimeline({
}
if (latestTurn.turnId === previous.turnId) {
if (previous.state === "running" && latestTurn.state === "interrupted") {
setExpandedTurnIds((existing) => {
setExpandedFoldKeys((existing) => {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

打断时自动展开的 effect 逻辑不变:仍然往集合里加 latestTurn.turnId。之所以还能生效,是因为 logic.ts 里被打断那一段的键正好就是 turn id。

const next = new Set(existing);
next.add(latestTurn.turnId);
return next;
});
}
return;
}
setExpandedTurnIds((existing) => {
setExpandedFoldKeys((existing) => {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下一个 turn 开始时删掉上一个 turn id,同样不变。

if (!existing.has(previous.turnId)) {
return existing;
}
Expand All @@ -443,7 +443,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
timelineEntries,
latestTurn,
runningTurnId,
expandedTurnIds,
expandedFoldKeys,

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useMemo 传入和依赖数组同步改名。

expandedWorkGroupIds,
isWorking,
activeTurnStartedAt,
Expand All @@ -454,7 +454,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
timelineEntries,
latestTurn,
runningTurnId,
expandedTurnIds,
expandedFoldKeys,
expandedWorkGroupIds,
isWorking,
activeTurnStartedAt,
Expand Down Expand Up @@ -1249,7 +1249,7 @@ function TurnFoldTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "turn-
type="button"
aria-expanded={row.expanded}
data-scroll-anchor-ignore
onClick={() => ctx.onToggleTurnFold(row.turnId, row.id)}
onClick={() => ctx.onToggleTurnFold(row.expandKey, row.id)}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

点击折叠行时传回该行自己的 expandKey。

className="flex cursor-pointer select-none items-center gap-1 rounded-md px-1 text-sm leading-relaxed text-muted-foreground tabular-nums transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70"
>
<span>{row.label}</span>
Expand Down
Loading