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
65export function detectComposerTrigger(
66 text: string,
67 cursorInput: number,
68 isWhitespaceChar?: (char: string) => boolean,
69): ComposerTrigger | null {
70 const cursor = clampCursor(text, cursorInput);
71 const lineStart = text.lastIndexOf("\n", Math.max(0, cursor - 1)) + 1;
72 const linePrefix = text.slice(lineStart, cursor);
73
74 if (linePrefix.startsWith("/")) {
75 const commandMatch = /^\/(\S*)$/.exec(linePrefix);
76 if (commandMatch && isPathWord(text, lineStart, cursor, isWhitespaceChar ?? isWhitespace)) {
77 return null;
78 }
79 if (commandMatch) {
80 const commandQuery = commandMatch[1] ?? "";
81 if (commandQuery.toLowerCase() === "model") {
82 return {
83 kind: "slash-model",
84 query: "",
85 rangeStart: lineStart,
86 rangeEnd: cursor,
87 };
88 }
89 return {
90 kind: "slash-command",
91 query: commandQuery,
92 rangeStart: lineStart,
93 rangeEnd: cursor,
94 };
95 }
96
97 const modelMatch = /^\/model(?:\s+(.*))?$/.exec(linePrefix);
98 if (modelMatch) {
99 return {
100 kind: "slash-model",
101 query: (modelMatch[1] ?? "").trim(),
102 rangeStart: lineStart,
103 rangeEnd: cursor,
104 };
105 }
106 }
107
108 const wsCheck = isWhitespaceChar ?? isWhitespace;
109 let tokenIdx = cursor - 1;
110 while (tokenIdx >= 0 && !wsCheck(text[tokenIdx] ?? "")) {
111 tokenIdx -= 1;
112 }
113 const tokenStart = tokenIdx + 1;
114
115 const token = text.slice(tokenStart, cursor);
116 if (token.startsWith("/")) {
117 if (isPathWord(text, tokenStart, cursor, wsCheck)) return null;
118 return {
119 kind: "slash-command",
120 query: token.slice(1),
121 rangeStart: tokenStart,
122 rangeEnd: cursor,
123 };
124 }
125 const pullRequestMatch = /^#([\p{L}\p{N}][\p{L}\p{N}_-]*)?$/u.exec(token);
126 if (pullRequestMatch)
127 return {
128 kind: "pull-request",
129 query: pullRequestMatch[1] ?? "",
130 rangeStart: tokenStart,
131 rangeEnd: cursor,
132 };
133 const skillPrefix = /^\p{Sc}/u.exec(token);
134 if (skillPrefix) {
135 return {
136 kind: "skill",
137 query: token.slice(skillPrefix[0].length),
138 rangeStart: tokenStart,
139 rangeEnd: cursor,
140 };
141 }
142 if (!token.startsWith("@")) {
143 return null;
144 }
145
146 return {
147 kind: "path",
148 query: token.slice(1),
149 rangeStart: tokenStart,
150 rangeEnd: cursor,
151 };
152}
153
154export function replaceTextRange(
155 text: string,
156 rangeStart: number,
157 rangeEnd: number,
158 replacement: string,
159): { text: string; cursor: number } {
160 const safeStart = Math.max(0, Math.min(text.length, rangeStart));
161 const safeEnd = Math.max(safeStart, Math.min(text.length, rangeEnd));
162 const nextText = `${text.slice(0, safeStart)}${replacement}${text.slice(safeEnd)}`;
163 return { text: nextText, cursor: safeStart + replacement.length };
164}