Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerTrigger.ts

142 lines4.5 KB

1export type ComposerTriggerKind = "path" | "pull-request" | "slash-command" | "skill";
2export type ComposerSlashCommand = "model" | "plan" | "default";
3​
4export interface ComposerTrigger {
5 kind: ComposerTriggerKind;
6 query: string;
7 rangeStart: number;
8 rangeEnd: number;
9}
10​
11function composerFileLinkBasename(path: string): string {
12 const separatorIndex = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
13 return separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
15​
16// Emphasis markers too: `__init__.py` would otherwise render as bold "init.py",
17// which no longer matches the path, so the label shows beside the chip.
18function escapeMarkdownLinkLabel(label: string): string {
19 return label.replace(/[\\[\]*_]/g, "\\$&");
21​
22function encodeMarkdownLinkDestination(path: string): string {
23 return encodeURI(path)
24 .replaceAll("(", "%28")
25 .replaceAll(")", "%29")
26 .replaceAll("#", "%23")
27 .replaceAll("?", "%3F")
28 .replaceAll("\\", "%5C");
30​
31export function serializeComposerFileLink(path: string): string {
32 const label = escapeMarkdownLinkLabel(composerFileLinkBasename(path));
33 return `[${label}](${encodeMarkdownLinkDestination(path)})`;
35​
36function clampCursor(text: string, cursor: number): number {
37 if (!Number.isFinite(cursor)) return text.length;
38 return Math.max(0, Math.min(text.length, Math.floor(cursor)));
40​
41function isWhitespace(char: string): boolean {
42 return char === " " || char === "\n" || char === "\t" || char === "\r";
44​
45/** A second slash or backslash makes the whole word a path, even when the caret precedes it. */
46export function isPathWord(
47 text: string,
48 wordStart: number,
49 cursor: number,
50 isWhitespaceChar: (char: string) => boolean,
51): boolean {
52 let wordEnd = cursor;
53 while (wordEnd < text.length && !isWhitespaceChar(text[wordEnd] ?? "")) {
54 wordEnd += 1;
55 }
56 return /[/\\]/.test(text.slice(wordStart + 1, wordEnd));
58​
59/**
60 * `$5`, `€9.99` and `$10k` are money, not skills. Matches the amounts the inline skill
61 * token regexes already refuse to render as skills, plus the punctuation prose puts after
62 * one (`$10k.`, `($5)`).
63 */
64export function isCurrencyAmountQuery(query: string): boolean {
65 return /^\.?[0-9][0-9,._]*(?:[kKmMbBtT]|[eE][0-9]+)?[.,;:!?)]*$/.test(query);
67​
68export function detectComposerTrigger(
69 text: string,
70 cursorInput: number,
71 isWhitespaceChar?: (char: string) => boolean,
72): ComposerTrigger | null {
73 const cursor = clampCursor(text, cursorInput);
74 const wsCheck = isWhitespaceChar ?? isWhitespace;
75 let tokenIdx = cursor - 1;
76 while (tokenIdx >= 0 && !wsCheck(text[tokenIdx] ?? "")) {
77 tokenIdx -= 1;
78 }
79 const tokenStart = tokenIdx + 1;
80​
81 const token = text.slice(tokenStart, cursor);
82 if (token.startsWith("/")) {
83 if (isPathWord(text, tokenStart, cursor, wsCheck)) return null;
84 return {
85 kind: "slash-command",
86 query: token.slice(1),
87 rangeStart: tokenStart,
88 rangeEnd: cursor,
89 };
90 }
91 const pullRequestMatch = /^#([\p{L}\p{N}][\p{L}\p{N}_-]*)?$/u.exec(token);
92 if (pullRequestMatch)
93 return {
94 kind: "pull-request",
95 query: pullRequestMatch[1] ?? "",
96 rangeStart: tokenStart,
97 rangeEnd: cursor,
98 };
99 const skillPrefix = /^\p{Sc}/u.exec(token);
100 if (skillPrefix) {
101 if (isCurrencyAmountQuery(token.slice(skillPrefix[0].length))) return null;
102 return {
103 kind: "skill",
104 query: token.slice(skillPrefix[0].length),
105 rangeStart: tokenStart,
106 rangeEnd: cursor,
107 };
108 }
109 if (!token.startsWith("@")) {
110 return null;
111 }
112​
113 return {
114 kind: "path",
115 query: token.slice(1),
116 rangeStart: tokenStart,
117 rangeEnd: cursor,
118 };
120​
121export function replaceTextRange(
122 text: string,
123 rangeStart: number,
124 rangeEnd: number,
125 replacement: string,
126): { text: string; cursor: number } {
127 const safeStart = Math.max(0, Math.min(text.length, rangeStart));
128 const safeEnd = Math.max(safeStart, Math.min(text.length, rangeEnd));
129 const nextText = `${text.slice(0, safeStart)}${replacement}${text.slice(safeEnd)}`;
130 return { text: nextText, cursor: safeStart + replacement.length };
132​
133/** `/clear` as the whole message arms a fresh session instead of sending. */
134export function isClearComposerCommand(text: string): boolean {
135 return /^\/clear\s*$/i.test(text.trim());
137​
138/** `/side [prompt]`, or its alias `/btw [prompt]`, as the whole message. */
139export function parseSideChatCommand(text: string): { prompt: string } | null {
140 const match = /^\/(?:side|btw)(?:\s+([\s\S]*))?$/i.exec(text.trim());
141 return match ? { prompt: match[1]?.trim() ?? "" } : null;