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
21function escapeMarkdownLinkLabel(label: string): string {
22 return label.replaceAll("\\", "\\\\").replaceAll("[", "\\[").replaceAll("]", "\\]");
23}
24
25function encodeMarkdownLinkDestination(path: string): string {
26 return encodeURI(path)
27 .replaceAll("(", "%28")
28 .replaceAll(")", "%29")
29 .replaceAll("#", "%23")
30 .replaceAll("?", "%3F")
31 .replaceAll("\\", "%5C");
32}
33
34export function serializeComposerFileLink(path: string): string {
35 const label = escapeMarkdownLinkLabel(composerFileLinkBasename(path));
36 return `[${label}](${encodeMarkdownLinkDestination(path)})`;
37}
38
39function clampCursor(text: string, cursor: number): number {
40 if (!Number.isFinite(cursor)) return text.length;
41 return Math.max(0, Math.min(text.length, Math.floor(cursor)));
42}
43
44function isWhitespace(char: string): boolean {
45 return char === " " || char === "\n" || char === "\t" || char === "\r";
46}
47
48/**
49 * A slash word with a second slash, such as `/tmp/build.sh`, is a path, not a
50 * command. The whole word counts, so a caret before the second slash still sees it.
51 */
52export function isPathWord(
53 text: string,
54 wordStart: number,
55 cursor: number,
56 isWhitespaceChar: (char: string) => boolean,
57): boolean {
58 let wordEnd = cursor;
59 while (wordEnd < text.length && !isWhitespaceChar(text[wordEnd] ?? "")) {
60 wordEnd += 1;
61 }
62 return text.slice(wordStart + 1, wordEnd).includes("/");
63}
64
65/**
66 * `$5`, `€9.99` and `$10k` are money, not skills. Matches the amounts the inline skill
67 * token regexes already refuse to render as skills, plus the punctuation prose puts after
68 * one (`$10k.`, `($5)`).
69 */
70export function isCurrencyAmountQuery(query: string): boolean {
71 return /^\.?[0-9][0-9,._]*(?:[kKmMbBtT]|[eE][0-9]+)?[.,;:!?)]*$/.test(query);
72}
73
74export function detectComposerTrigger(
75 text: string,
76 cursorInput: number,
77 isWhitespaceChar?: (char: string) => boolean,
78): ComposerTrigger | null {
79 const cursor = clampCursor(text, cursorInput);
80 const lineStart = text.lastIndexOf("\n", Math.max(0, cursor - 1)) + 1;
81 const linePrefix = text.slice(lineStart, cursor);
82
83 if (linePrefix.startsWith("/")) {
84 const commandMatch = /^\/(\S*)$/.exec(linePrefix);
85 if (commandMatch && isPathWord(text, lineStart, cursor, isWhitespaceChar ?? isWhitespace)) {
86 return null;
87 }
88 if (commandMatch) {
89 const commandQuery = commandMatch[1] ?? "";
90 if (commandQuery.toLowerCase() === "model") {
91 return {
92 kind: "slash-model",
93 query: "",
94 rangeStart: lineStart,
95 rangeEnd: cursor,
96 };
97 }
98 return {
99 kind: "slash-command",
100 query: commandQuery,
101 rangeStart: lineStart,
102 rangeEnd: cursor,
103 };
104 }
105
106 const modelMatch = /^\/model(?:\s+(.*))?$/.exec(linePrefix);
107 if (modelMatch) {
108 return {
109 kind: "slash-model",
110 query: (modelMatch[1] ?? "").trim(),
111 rangeStart: lineStart,
112 rangeEnd: cursor,
113 };
114 }
115 }
116
117 const wsCheck = isWhitespaceChar ?? isWhitespace;
118 let tokenIdx = cursor - 1;
119 while (tokenIdx >= 0 && !wsCheck(text[tokenIdx] ?? "")) {
120 tokenIdx -= 1;
121 }
122 const tokenStart = tokenIdx + 1;
123
124 const token = text.slice(tokenStart, cursor);
125 if (token.startsWith("/")) {
126 if (isPathWord(text, tokenStart, cursor, wsCheck)) return null;
127 return {
128 kind: "slash-command",
129 query: token.slice(1),
130 rangeStart: tokenStart,
131 rangeEnd: cursor,
132 };
133 }
134 const pullRequestMatch = /^#([\p{L}\p{N}][\p{L}\p{N}_-]*)?$/u.exec(token);
135 if (pullRequestMatch)
136 return {
137 kind: "pull-request",
138 query: pullRequestMatch[1] ?? "",
139 rangeStart: tokenStart,
140 rangeEnd: cursor,
141 };
142 const skillPrefix = /^\p{Sc}/u.exec(token);
143 if (skillPrefix) {
144 if (isCurrencyAmountQuery(token.slice(skillPrefix[0].length))) return null;
145 return {
146 kind: "skill",
147 query: token.slice(skillPrefix[0].length),
148 rangeStart: tokenStart,
149 rangeEnd: cursor,
150 };
151 }
152 if (!token.startsWith("@")) {
153 return null;
154 }
155
156 return {
157 kind: "path",
158 query: token.slice(1),
159 rangeStart: tokenStart,
160 rangeEnd: cursor,
161 };
162}
163
164export function replaceTextRange(
165 text: string,
166 rangeStart: number,
167 rangeEnd: number,
168 replacement: string,
169): { text: string; cursor: number } {
170 const safeStart = Math.max(0, Math.min(text.length, rangeStart));
171 const safeEnd = Math.max(safeStart, Math.min(text.length, rangeEnd));
172 const nextText = `${text.slice(0, safeStart)}${replacement}${text.slice(safeEnd)}`;
173 return { text: nextText, cursor: safeStart + replacement.length };
174}