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