Open History to see the last commit
1import {
2 assistantCitationsToPlainText,
3 splitsSurrogatePair,
4} from "@t3tools/shared/assistantCitations";
5import { parseThreadLinkHref } from "@t3tools/shared/threadLinks";
6import {
7 isAttachmentOnlyBootstrapPrompt,
8 replaceComposerContextReferences,
9} from "@t3tools/shared/composerContextReferences";
10import type { Nodes } from "hast";
11
12import { markdownTree } from "~/markdown/wordsHast";
13
14import type { MessagesTimelineRow } from "./MessagesTimeline.logic";
15
16export interface TimelineMinimapItem {
17 readonly id: string;
18 readonly rowIndex: number;
19 readonly userText: string | null;
20 readonly assistantText: string | null;
21}
22
23export function deriveTimelineMinimapItems(
24 rows: ReadonlyArray<MessagesTimelineRow>,
25): TimelineMinimapItem[] {
26 const items: TimelineMinimapItem[] = [];
27 for (let index = 0; index < rows.length; index += 1) {
28 const row = rows[index];
29 if (row?.kind !== "message" || row.message.role !== "user") {
30 continue;
31 }
32
33 items.push({
34 id: row.id,
35 rowIndex: index,
36 userText: isAttachmentOnlyBootstrapPrompt(row.message.text) ? "" : row.message.text,
37 assistantText: resolveFinalAssistantTextForTurn(rows, index),
38 });
39 }
40 return items;
41}
42
43function resolveFinalAssistantTextForTurn(
44 rows: ReadonlyArray<MessagesTimelineRow>,
45 userRowIndex: number,
46) {
47 let finalAssistantText: string | null = null;
48 for (let index = userRowIndex + 1; index < rows.length; index += 1) {
49 const row = rows[index];
50 if (row?.kind !== "message") {
51 continue;
52 }
53 if (row.message.role === "user") {
54 break;
55 }
56 if (row.message.role === "assistant") {
57 finalAssistantText = row.message.text ?? null;
58 }
59 }
60 return finalAssistantText;
61}
62
63// Elements whose children run together; every other element is separated by a space.
64// A tight list item holds its text bare, without the paragraph a loose one wraps it in.
65const INLINE_PARENTS = new Set([
66 "p",
67 "li",
68 "h1",
69 "h2",
70 "h3",
71 "h4",
72 "h5",
73 "h6",
74 "td",
75 "th",
76 "em",
77 "strong",
78 "del",
79 "a",
80 "code",
81 "span",
82 "sup",
83 "sub",
84]);
85
86function markdownNodeText(node: Nodes): string {
87 if (node.type === "text") return node.value;
88 if (node.type === "element" && node.tagName === "br") return " ";
89 if (node.type === "element" && node.tagName === "img") return String(node.properties.alt ?? "");
90 if (node.type !== "element" && node.type !== "root") return "";
91 // Footnote markers and the footnote list are not part of the message's prose.
92 if (
93 node.type === "element" &&
94 (node.properties.dataFootnotes !== undefined || node.properties.dataFootnoteRef !== undefined)
95 )
96 return "";
97 const separator = node.type === "element" && INLINE_PARENTS.has(node.tagName) ? "" : " ";
98 const text = node.children.map(markdownNodeText).join(separator);
99 // A thread autolink's only text is its URL.
100 return node.type === "element" && node.tagName === "a" && parseThreadLinkHref(text) !== null
101 ? "a thread"
102 : text;
103}
104
105/** Markdown as one line of the text it renders, without markup or link targets. */
106export function markdownPlainText(markdown: string): string {
107 return markdownNodeText(markdownTree(markdown)).replace(/\s+/g, " ").trim();
108}
109
110// The tooltip shows a few lines; bound parsing work for long messages and streaming responses.
111function markdownMinimapPreview(markdown: string | null) {
112 if (markdown === null) return null;
113 const compact = markdownPlainText(
114 markdown.slice(0, splitsSurrogatePair(markdown, 1_500) ? 1_499 : 1_500),
115 );
116 return compact.length > 0 ? compact : null;
117}
118
119export function resolveTimelineMinimapPreview(
120 item: TimelineMinimapItem | null,
121): TimelineMinimapItem | null {
122 return item === null
123 ? null
124 : {
125 ...item,
126 // Before markdown parsing, which would reduce a citation link to "Assistant quote".
127 userText: markdownMinimapPreview(
128 item.userText === null
129 ? null
130 : assistantCitationsToPlainText(
131 replaceComposerContextReferences(item.userText, (reference) => reference.label),
132 ),
133 ),
134 assistantText: markdownMinimapPreview(item.assistantText),
135 };
136}