Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

timelineMinimapItems.ts

108 lines3.2 KB

1import type { Nodes } from "mdast";
2import remarkGfm from "remark-gfm";
3import remarkParse from "remark-parse";
4import { unified } from "unified";
5​
6import type { MessagesTimelineRow } from "./MessagesTimeline.logic";
7​
8export interface TimelineMinimapItem {
9 readonly id: string;
10 readonly rowIndex: number;
11 readonly userText: string | null;
12 readonly assistantText: string | null;
14​
15export function deriveTimelineMinimapItems(
16 rows: ReadonlyArray<MessagesTimelineRow>,
17): TimelineMinimapItem[] {
18 const items: TimelineMinimapItem[] = [];
19 for (let index = 0; index < rows.length; index += 1) {
20 const row = rows[index];
21 if (row?.kind !== "message" || row.message.role !== "user") {
22 continue;
23 }
24​
25 items.push({
26 id: row.id,
27 rowIndex: index,
28 userText: row.message.text,
29 assistantText: resolveFinalAssistantTextForTurn(rows, index),
30 });
31 }
32 return items;
34​
35function resolveFinalAssistantTextForTurn(
36 rows: ReadonlyArray<MessagesTimelineRow>,
37 userRowIndex: number,
38) {
39 let finalAssistantText: string | null = null;
40 for (let index = userRowIndex + 1; index < rows.length; index += 1) {
41 const row = rows[index];
42 if (row?.kind !== "message") {
43 continue;
44 }
45 if (row.message.role === "user") {
46 break;
47 }
48 if (row.message.role === "assistant") {
49 finalAssistantText = row.message.text ?? null;
50 }
51 }
52 return finalAssistantText;
54​
55const markdownPreviewParser = unified().use(remarkParse).use(remarkGfm).freeze();
56​
57const INLINE_PARENT_TYPES = new Set<Nodes["type"]>([
58 "paragraph",
59 "heading",
60 "tableCell",
61 "emphasis",
62 "strong",
63 "delete",
64 "link",
65 "linkReference",
66]);
67​
68function markdownNodeText(node: Nodes): string {
69 if (node.type === "html") {
70 return node.value
71 .replace(/<!--[\s\S]*?(?:-->|$)/g, "")
72 .replace(/<\/?([a-z][\w-]*)(?:[^"'<>]|"[^"]*"|'[^']*')*>/gi, (_, tag: string) =>
73 /^(?:address|article|aside|blockquote|br|dd|details|div|dl|dt|figcaption|figure|footer|h[1-6]|header|hr|li|main|nav|ol|p|pre|section|summary|table|tbody|td|th|thead|tr|ul)$/i.test(
74 tag,
75 )
76 ? " "
77 : "",
78 );
79 }
80 if (node.type === "break") return " ";
81 if ("value" in node) return node.value;
82 if (node.type === "image" || node.type === "imageReference") return node.alt ?? "";
83 if (!("children" in node)) return "";
84 const separator = INLINE_PARENT_TYPES.has(node.type) ? "" : " ";
85 return node.children.map(markdownNodeText).join(separator);
87​
88function compactMinimapPreview(text: string | null | undefined) {
89 const compact = text?.replace(/\s+/g, " ").trim() ?? "";
90 return compact.length > 0 ? compact : null;
92​
93export function resolveTimelineMinimapPreview(
94 item: TimelineMinimapItem | null,
95): TimelineMinimapItem | null {
96 return item === null
97 ? null
98 : {
99 ...item,
100 userText: compactMinimapPreview(item.userText),
101 // The tooltip shows three lines; bound parsing work while long responses stream.
102 assistantText: compactMinimapPreview(
103 item.assistantText === null
104 ? null
105 : markdownNodeText(markdownPreviewParser.parse(item.assistantText.slice(0, 1_500))),
106 ),
107 };