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