Open History to see the last commit
1import { parseThreadLinkHref } from "@t3tools/shared/threadLinks";
2import type { Nodes } from "mdast";
3import remarkGfm from "remark-gfm";
4import remarkParse from "remark-parse";
5import { unified } from "unified";
6
7import type { MessagesTimelineRow } from "./MessagesTimeline.logic";
8
9export interface TimelineMinimapItem {
10 readonly id: string;
11 readonly rowIndex: number;
12 readonly userText: string | null;
13 readonly assistantText: string | null;
14}
15
16export function deriveTimelineMinimapItems(
17 rows: ReadonlyArray<MessagesTimelineRow>,
18): TimelineMinimapItem[] {
19 const items: TimelineMinimapItem[] = [];
20 for (let index = 0; index < rows.length; index += 1) {
21 const row = rows[index];
22 if (row?.kind !== "message" || row.message.role !== "user") {
23 continue;
24 }
25
26 items.push({
27 id: row.id,
28 rowIndex: index,
29 userText: row.message.text,
30 assistantText: resolveFinalAssistantTextForTurn(rows, index),
31 });
32 }
33 return items;
34}
35
36function resolveFinalAssistantTextForTurn(
37 rows: ReadonlyArray<MessagesTimelineRow>,
38 userRowIndex: number,
39) {
40 let finalAssistantText: string | null = null;
41 for (let index = userRowIndex + 1; index < rows.length; index += 1) {
42 const row = rows[index];
43 if (row?.kind !== "message") {
44 continue;
45 }
46 if (row.message.role === "user") {
47 break;
48 }
49 if (row.message.role === "assistant") {
50 finalAssistantText = row.message.text ?? null;
51 }
52 }
53 return finalAssistantText;
54}
55
56const markdownPreviewParser = unified().use(remarkParse).use(remarkGfm).freeze();
57
58const INLINE_PARENT_TYPES = new Set<Nodes["type"]>([
59 "paragraph",
60 "heading",
61 "tableCell",
62 "emphasis",
63 "strong",
64 "delete",
65 "link",
66 "linkReference",
67]);
68
69function markdownNodeText(node: Nodes): string {
70 if (node.type === "html") {
71 return node.value
72 .replace(/<!--[\s\S]*?(?:-->|$)/g, "")
73 .replace(/<\/?([a-z][\w-]*)(?:[^"'<>]|"[^"]*"|'[^']*')*>/gi, (_, tag: string) =>
74 /^(?: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(
75 tag,
76 )
77 ? " "
78 : "",
79 );
80 }
81 if (node.type === "break") return " ";
82 if ("value" in node) return node.value;
83 if (node.type === "image" || node.type === "imageReference") return node.alt ?? "";
84 if (!("children" in node)) return "";
85 const separator = INLINE_PARENT_TYPES.has(node.type) ? "" : " ";
86 const text = node.children.map(markdownNodeText).join(separator);
87 // A thread autolink's only text is its URL.
88 return node.type === "link" && parseThreadLinkHref(text) !== null ? "a thread" : text;
89}
90
91function compactMinimapPreview(text: string | null | undefined) {
92 const compact = text?.replace(/\s+/g, " ").trim() ?? "";
93 return compact.length > 0 ? compact : null;
94}
95
96export function resolveTimelineMinimapPreview(
97 item: TimelineMinimapItem | null,
98): TimelineMinimapItem | null {
99 return item === null
100 ? null
101 : {
102 ...item,
103 userText: compactMinimapPreview(item.userText),
104 // The tooltip shows three lines; bound parsing work while long responses stream.
105 assistantText: compactMinimapPreview(
106 item.assistantText === null
107 ? null
108 : markdownNodeText(markdownPreviewParser.parse(item.assistantText.slice(0, 1_500))),
109 ),
110 };
111}