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.
64const INLINE_PARENTS = new Set([
65 "p",
66 "h1",
67 "h2",
68 "h3",
69 "h4",
70 "h5",
71 "h6",
72 "td",
73 "th",
74 "em",
75 "strong",
76 "del",
77 "a",
78 "code",
79 "span",
80 "sup",
81 "sub",
82]);
83
84function markdownNodeText(node: Nodes): string {
85 if (node.type === "text") return node.value;
86 if (node.type === "element" && node.tagName === "br") return " ";
87 if (node.type === "element" && node.tagName === "img") return String(node.properties.alt ?? "");
88 if (node.type !== "element" && node.type !== "root") return "";
89 const separator = node.type === "element" && INLINE_PARENTS.has(node.tagName) ? "" : " ";
90 const text = node.children.map(markdownNodeText).join(separator);
91 // A thread autolink's only text is its URL.
92 return node.type === "element" && node.tagName === "a" && parseThreadLinkHref(text) !== null
93 ? "a thread"
94 : text;
95}
96
97/** Markdown as one line of the text it renders, without markup or link targets. */
98export function markdownPlainText(markdown: string): string {
99 return markdownNodeText(markdownTree(markdown)).replace(/\s+/g, " ").trim();
100}
101
102// The tooltip shows a few lines; bound parsing work for long messages and streaming responses.
103function markdownMinimapPreview(markdown: string | null) {
104 if (markdown === null) return null;
105 const compact = markdownPlainText(
106 markdown.slice(0, splitsSurrogatePair(markdown, 1_500) ? 1_499 : 1_500),
107 );
108 return compact.length > 0 ? compact : null;
109}
110
111export function resolveTimelineMinimapPreview(
112 item: TimelineMinimapItem | null,
113): TimelineMinimapItem | null {
114 return item === null
115 ? null
116 : {
117 ...item,
118 // Before markdown parsing, which would reduce a citation link to "Assistant quote".
119 userText: markdownMinimapPreview(
120 item.userText === null
121 ? null
122 : assistantCitationsToPlainText(
123 replaceComposerContextReferences(item.userText, (reference) => reference.label),
124 ),
125 ),
126 assistantText: markdownMinimapPreview(item.assistantText),
127 };
128}