Open History to see the last commit
1import type { MessagesTimelineRow } from "./MessagesTimeline.logic";
2
3export interface TimelineMinimapItem {
4 readonly id: string;
5 readonly rowIndex: number;
6 readonly userText: string | null;
7 readonly assistantText: string | null;
8}
9
10export function deriveTimelineMinimapItems(
11 rows: ReadonlyArray<MessagesTimelineRow>,
12): TimelineMinimapItem[] {
13 const items: TimelineMinimapItem[] = [];
14 for (let index = 0; index < rows.length; index += 1) {
15 const row = rows[index];
16 if (row?.kind !== "message" || row.message.role !== "user") {
17 continue;
18 }
19
20 items.push({
21 id: row.id,
22 rowIndex: index,
23 userText: row.message.text,
24 assistantText: resolveFinalAssistantTextForTurn(rows, index),
25 });
26 }
27 return items;
28}
29
30function resolveFinalAssistantTextForTurn(
31 rows: ReadonlyArray<MessagesTimelineRow>,
32 userRowIndex: number,
33) {
34 let finalAssistantText: string | null = null;
35 for (let index = userRowIndex + 1; index < rows.length; index += 1) {
36 const row = rows[index];
37 if (row?.kind !== "message") {
38 continue;
39 }
40 if (row.message.role === "user") {
41 break;
42 }
43 if (row.message.role === "assistant") {
44 finalAssistantText = row.message.text ?? null;
45 }
46 }
47 return finalAssistantText;
48}
49
50function compactMinimapPreview(text: string | null | undefined) {
51 const compact = text?.replace(/\s+/g, " ").trim() ?? "";
52 return compact.length > 0 ? compact : null;
53}
54
55export function resolveTimelineMinimapPreview(
56 item: TimelineMinimapItem | null,
57): TimelineMinimapItem | null {
58 return item === null
59 ? null
60 : {
61 ...item,
62 userText: compactMinimapPreview(item.userText),
63 assistantText: compactMinimapPreview(item.assistantText),
64 };
65}