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