@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 278 lines 8.68 KB
Raw
1 import {
2 matchInlineSkills,
3 formatProviderSkillDisplayName,
4 type InlineSkill,
5 } from "./inlineSkills.ts";
6 import {
7 artifactTemplateFromHastProperties,
8 renderCodexInlineDirectivesAsMarkdown,
9 } from "./codexMarkdownDirectives.ts";
10 import { codexArtifactTemplatePresentationLabel } from "./codexArtifactTemplates.ts";
11 import { buildFileLinkParentSuffixByPath, fileLinkLabel } from "./fileLinks.ts";
12 import {
13 classifyMarkdownImageSource,
14 isMarkdownFileLinkLabel,
15 extractMarkdownLinkHrefs,
16 extractInlineCodeSpans,
17 inlineCodeFilePathCandidate,
18 resolveMarkdownFileLink,
19 shouldRenderMarkdownImageAsFileLink,
20 } from "./markdownLinks.ts";
21 import { mediaKindFromPath } from "./mediaPreview.ts";
22 import { upgradeLegacyContextMessage } from "./composerContextLegacy.ts";
23 import {
24 isAttachmentOnlyBootstrapPrompt,
25 parseComposerContextHref,
26 } from "./composerContextReferences.ts";
27 import { parseAssistantCitationHref } from "./assistantCitations.ts";
28 import type { OrchestrationV2ConversationMessage } from "@t3tools/contracts";
29 import { proposedPlanTitle, stripDisplayedPlanMarkdown } from "./proposedPlanText.ts";
30 import { unified } from "unified";
31 import remarkParse from "remark-parse";
32 import remarkRehype from "remark-rehype";
33 import {
34 shouldPreserveAssistantLineBreaks,
35 remarkAssistantInsights,
36 CHAT_MARKDOWN_REHYPE_PLUGINS,
37 CHAT_MARKDOWN_REMARK_PLUGINS,
38 CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS,
39 CHAT_USER_MARKDOWN_REMARK_PLUGINS,
40 chatMarkdownImageSource,
41 decodeMarkdownLinkEscapes,
42 type ChatMarkdownImageProperties,
43 } from "./markdownPipeline.ts";
44
45 // Inline wrappers (including Shiki token spans) must not split a search phrase.
46 export const THREAD_FIND_BLOCK_TAGS = new Set([
47 "address",
48 "article",
49 "aside",
50 "blockquote",
51 "br",
52 "dd",
53 "details",
54 "div",
55 "dl",
56 "dt",
57 "figcaption",
58 "figure",
59 "footer",
60 "h1",
61 "h2",
62 "h3",
63 "h4",
64 "h5",
65 "h6",
66 "header",
67 "hr",
68 "li",
69 "main",
70 "nav",
71 "ol",
72 "p",
73 "pre",
74 "section",
75 "summary",
76 "table",
77 "tbody",
78 "td",
79 "th",
80 "thead",
81 "tr",
82 "ul",
83 ]);
84
85 function assistantTextProcessor(lineBreaks = false, renderInsights = false) {
86 return unified()
87 .use(remarkParse)
88 .use(renderInsights ? [remarkAssistantInsights] : [])
89 .use(lineBreaks ? CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : CHAT_MARKDOWN_REMARK_PLUGINS)
90 .use(remarkRehype, { allowDangerousHtml: true })
91 .use(CHAT_MARKDOWN_REHYPE_PLUGINS);
92 }
93 const assistantProcessor = assistantTextProcessor();
94 const assistantBreaksProcessor = assistantTextProcessor(true);
95 const insightProcessor = assistantTextProcessor(false, true);
96 const insightBreaksProcessor = assistantTextProcessor(true, true);
97 const userProcessor = unified()
98 .use(remarkParse)
99 .use(CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS)
100 .use(CHAT_USER_MARKDOWN_REMARK_PLUGINS)
101 .use(remarkRehype, { allowDangerousHtml: true });
102
103 interface TextTree {
104 readonly type: string;
105 readonly tagName?: string;
106 readonly value?: string;
107 readonly properties?: ChatMarkdownImageProperties & {
108 readonly href?: unknown;
109 readonly dataInlineCode?: unknown;
110 };
111 readonly children?: ReadonlyArray<TextTree>;
112 }
113
114 function markdownThreadFindText(
115 markdown: string,
116 {
117 userMessage = false,
118 cwd,
119 skills = [],
120 lineBreaks = false,
121 renderInsights = false,
122 theme,
123 }: {
124 userMessage?: boolean;
125 cwd: string | undefined;
126 skills?: readonly InlineSkill[];
127 lineBreaks?: boolean;
128 renderInsights?: boolean;
129 theme: "dark" | "light" | undefined;
130 },
131 ): string[] {
132 const processor = userMessage
133 ? userProcessor
134 : lineBreaks
135 ? assistantBreaksProcessor
136 : assistantProcessor;
137 const renderer = renderInsights
138 ? lineBreaks
139 ? insightBreaksProcessor
140 : insightProcessor
141 : processor;
142 const tree = renderer.runSync(renderer.parse(markdown), markdown);
143 const paths = [
144 ...extractMarkdownLinkHrefs(renderCodexInlineDirectivesAsMarkdown(markdown)).map(
145 decodeMarkdownLinkEscapes,
146 ),
147 ...extractInlineCodeSpans(markdown).flatMap((span) => inlineCodeFilePathCandidate(span) ?? []),
148 ].flatMap((href) => {
149 const resolved = resolveMarkdownFileLink(href, cwd);
150 return resolved ? [resolved.position.path] : [];
151 });
152 const parentSuffixes = buildFileLinkParentSuffixByPath(paths);
153 const segments: string[] = [];
154 let text = "";
155 const flush = () => {
156 if (text.trim()) segments.push(text);
157 text = "";
158 };
159 const visit = (node: TextTree, inPre = false, inlineSkills = false) => {
160 const href = node.properties?.href ?? node.properties?.src;
161 if (userMessage && typeof href === "string" && parseComposerContextHref(href)) {
162 flush();
163 return;
164 }
165 const citation =
166 node.tagName === "a" && typeof href === "string" ? parseAssistantCitationHref(href) : null;
167 if (citation) {
168 // The quote card is not searched, and it splits the text before it from its comment.
169 flush();
170 text += (citation.comment ?? "").replace(/\r?\n/g, " ");
171 return;
172 }
173 {
174 let candidate: string | null = null;
175 if (node.tagName === "a" && typeof href === "string") {
176 candidate = href;
177 } else if (node.tagName === "img") {
178 const source = chatMarkdownImageSource(node.properties, theme);
179 if (source) {
180 const { authoredSrc, classifiedSrc } = source;
181 if (
182 shouldRenderMarkdownImageAsFileLink(
183 classifyMarkdownImageSource(classifiedSrc, cwd),
184 mediaKindFromPath(classifiedSrc),
185 )
186 ) {
187 candidate = authoredSrc;
188 }
189 }
190 } else if (
191 node.tagName === "code" &&
192 !inPre &&
193 node.properties?.dataInlineCode !== undefined
194 ) {
195 candidate = inlineCodeFilePathCandidate(
196 (node.children ?? []).map((child) => child.value ?? "").join(""),
197 );
198 }
199 const resolved = candidate ? resolveMarkdownFileLink(candidate, cwd) : null;
200 if (candidate && resolved) {
201 const labelText = (node: TextTree): string =>
202 node.value ?? (node.children ?? []).map(labelText).join("");
203 const label = labelText(node);
204 if (node.tagName === "a" && !isMarkdownFileLinkLabel(label, candidate)) text += `${label} `;
205 text += fileLinkLabel(resolved.position, parentSuffixes);
206 return;
207 }
208 }
209 const template = artifactTemplateFromHastProperties(node.properties);
210 if (template) {
211 flush();
212 segments.push(
213 template.displayName,
214 codexArtifactTemplatePresentationLabel(template.artifactKind),
215 );
216 return;
217 }
218 const block = THREAD_FIND_BLOCK_TAGS.has(node.tagName ?? "");
219 if (block) flush();
220 if (node.type === "text" || (userMessage && node.type === "raw")) {
221 let value = node.value ?? "";
222 if (inlineSkills) {
223 let cursor = 0;
224 let rendered = "";
225 for (const { start, end, skill } of matchInlineSkills(value, skills)) {
226 rendered += value.slice(cursor, start) + formatProviderSkillDisplayName(skill);
227 cursor = end;
228 }
229 value = rendered + value.slice(cursor);
230 }
231 text += inPre ? value : value.replace(/\r?\n/g, " ");
232 }
233 const renderSkills =
234 node.tagName === "code" || node.tagName === "a"
235 ? false
236 : inlineSkills || node.tagName === "p" || node.tagName === "li";
237 for (const child of node.children ?? [])
238 visit(child, inPre || node.tagName === "pre", renderSkills);
239 if (block) flush();
240 };
241 visit(tree);
242 flush();
243 return segments;
244 }
245
246 /** Plans render `$skill` tokens literally (no skill chips), so they are indexed as written. */
247 export function searchablePlanSegments(
248 markdown: string,
249 cwd?: string,
250 theme?: "dark" | "light",
251 ): readonly string[] {
252 return [
253 proposedPlanTitle(markdown) ?? "Proposed plan",
254 ...markdownThreadFindText(stripDisplayedPlanMarkdown(markdown), { cwd, theme }),
255 ];
256 }
257
258 export function searchableMessageSegments(
259 message: Pick<OrchestrationV2ConversationMessage, "role" | "text" | "streaming" | "context">,
260 cwd?: string,
261 skills: readonly InlineSkill[] = [],
262 theme?: "dark" | "light",
263 ): readonly string[] | null {
264 if (message.role === "user") {
265 // Clients hide this prompt, so find must not match text nobody can see.
266 if (isAttachmentOnlyBootstrapPrompt(message.text)) return [];
267 const text = message.context ? message.text : upgradeLegacyContextMessage(message.text).text;
268 return markdownThreadFindText(text, { userMessage: true, cwd, skills, theme });
269 }
270 if (message.role !== "assistant") return null;
271 return markdownThreadFindText(message.text || (message.streaming ? "" : "(empty response)"), {
272 cwd,
273 skills,
274 lineBreaks: shouldPreserveAssistantLineBreaks(message.text),
275 renderInsights: true,
276 theme,
277 });
278 }