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