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