Open History to see the last commit
1import { splitPromptIntoComposerSegments } from "./composer-editor-mentions";
2import { INLINE_TERMINAL_CONTEXT_PLACEHOLDER } from "./lib/terminalContext";
3
4export type ComposerTriggerKind = "path" | "slash-command" | "skill";
5export type ComposerSlashCommand = "model" | "plan" | "default";
6export type ComposerSubmissionIntent = "foreground" | "background";
7
8export interface ComposerTrigger {
9 kind: ComposerTriggerKind;
10 query: string;
11 rangeStart: number;
12 rangeEnd: number;
13}
14
15export function composerSubmissionIntentForEnter(input: {
16 isMobileViewport: boolean;
17 shiftKey: boolean;
18 modifierKey: boolean;
19 isDraftThread: boolean;
20}): ComposerSubmissionIntent | null {
21 if (input.isMobileViewport || input.shiftKey) {
22 return null;
23 }
24 return input.modifierKey && input.isDraftThread ? "background" : "foreground";
25}
26
27const isInlineTokenSegment = (
28 segment:
29 | { type: "text"; text: string }
30 | { type: "mention" }
31 | { type: "skill" }
32 | { type: "terminal-context" },
33): boolean => segment.type !== "text";
34
35function clampCursor(text: string, cursor: number): number {
36 if (!Number.isFinite(cursor)) return text.length;
37 return Math.max(0, Math.min(text.length, Math.floor(cursor)));
38}
39
40function isWhitespace(char: string): boolean {
41 return (
42 char === " " ||
43 char === "\n" ||
44 char === "\t" ||
45 char === "\r" ||
46 char === INLINE_TERMINAL_CONTEXT_PLACEHOLDER
47 );
48}
49
50function tokenStartForCursor(text: string, cursor: number): number {
51 let index = cursor - 1;
52 while (index >= 0 && !isWhitespace(text[index] ?? "")) {
53 index -= 1;
54 }
55 return index + 1;
56}
57
58export function expandCollapsedComposerCursor(text: string, cursorInput: number): number {
59 const collapsedCursor = clampCursor(text, cursorInput);
60 const segments = splitPromptIntoComposerSegments(text);
61 if (segments.length === 0) {
62 return collapsedCursor;
63 }
64
65 let remaining = collapsedCursor;
66 let expandedCursor = 0;
67
68 for (const segment of segments) {
69 if (segment.type === "mention") {
70 const expandedLength = segment.source.length;
71 if (remaining <= 1) {
72 return expandedCursor + (remaining === 0 ? 0 : expandedLength);
73 }
74 remaining -= 1;
75 expandedCursor += expandedLength;
76 continue;
77 }
78 if (segment.type === "skill") {
79 const expandedLength = segment.name.length + 1;
80 if (remaining <= 1) {
81 return expandedCursor + (remaining === 0 ? 0 : expandedLength);
82 }
83 remaining -= 1;
84 expandedCursor += expandedLength;
85 continue;
86 }
87 if (segment.type === "terminal-context") {
88 if (remaining <= 1) {
89 return expandedCursor + remaining;
90 }
91 remaining -= 1;
92 expandedCursor += 1;
93 continue;
94 }
95
96 const segmentLength = segment.text.length;
97 if (remaining <= segmentLength) {
98 return expandedCursor + remaining;
99 }
100 remaining -= segmentLength;
101 expandedCursor += segmentLength;
102 }
103
104 return expandedCursor;
105}
106
107function collapsedSegmentLength(
108 segment:
109 | { type: "text"; text: string }
110 | { type: "mention" }
111 | { type: "skill" }
112 | { type: "terminal-context" },
113): number {
114 if (segment.type === "text") {
115 return segment.text.length;
116 }
117 return 1;
118}
119
120function clampCollapsedComposerCursorForSegments(
121 segments: ReadonlyArray<
122 | { type: "text"; text: string }
123 | { type: "mention" }
124 | { type: "skill" }
125 | { type: "terminal-context" }
126 >,
127 cursorInput: number,
128): number {
129 const collapsedLength = segments.reduce(
130 (total, segment) => total + collapsedSegmentLength(segment),
131 0,
132 );
133 if (!Number.isFinite(cursorInput)) {
134 return collapsedLength;
135 }
136 return Math.max(0, Math.min(collapsedLength, Math.floor(cursorInput)));
137}
138
139export function clampCollapsedComposerCursor(text: string, cursorInput: number): number {
140 return clampCollapsedComposerCursorForSegments(
141 splitPromptIntoComposerSegments(text),
142 cursorInput,
143 );
144}
145
146export function collapseExpandedComposerCursor(text: string, cursorInput: number): number {
147 const expandedCursor = clampCursor(text, cursorInput);
148 const segments = splitPromptIntoComposerSegments(text);
149 if (segments.length === 0) {
150 return expandedCursor;
151 }
152
153 let remaining = expandedCursor;
154 let collapsedCursor = 0;
155
156 for (const segment of segments) {
157 if (segment.type === "mention") {
158 const expandedLength = segment.source.length;
159 if (remaining === 0) {
160 return collapsedCursor;
161 }
162 if (remaining <= expandedLength) {
163 return collapsedCursor + 1;
164 }
165 remaining -= expandedLength;
166 collapsedCursor += 1;
167 continue;
168 }
169 if (segment.type === "skill") {
170 const expandedLength = segment.name.length + 1;
171 if (remaining === 0) {
172 return collapsedCursor;
173 }
174 if (remaining <= expandedLength) {
175 return collapsedCursor + 1;
176 }
177 remaining -= expandedLength;
178 collapsedCursor += 1;
179 continue;
180 }
181 if (segment.type === "terminal-context") {
182 if (remaining <= 1) {
183 return collapsedCursor + remaining;
184 }
185 remaining -= 1;
186 collapsedCursor += 1;
187 continue;
188 }
189
190 const segmentLength = segment.text.length;
191 if (remaining <= segmentLength) {
192 return collapsedCursor + remaining;
193 }
194 remaining -= segmentLength;
195 collapsedCursor += segmentLength;
196 }
197
198 return collapsedCursor;
199}
200
201export function isCollapsedCursorAdjacentToInlineToken(
202 text: string,
203 cursorInput: number,
204 direction: "left" | "right",
205): boolean {
206 const segments = splitPromptIntoComposerSegments(text);
207 if (!segments.some(isInlineTokenSegment)) {
208 return false;
209 }
210
211 const cursor = clampCollapsedComposerCursorForSegments(segments, cursorInput);
212 let collapsedOffset = 0;
213
214 for (const segment of segments) {
215 if (isInlineTokenSegment(segment)) {
216 if (direction === "left" && cursor === collapsedOffset + 1) {
217 return true;
218 }
219 if (direction === "right" && cursor === collapsedOffset) {
220 return true;
221 }
222 }
223 collapsedOffset += collapsedSegmentLength(segment);
224 }
225
226 return false;
227}
228
229export const isCollapsedCursorAdjacentToMention = isCollapsedCursorAdjacentToInlineToken;
230
231export function detectComposerTrigger(text: string, cursorInput: number): ComposerTrigger | null {
232 const cursor = clampCursor(text, cursorInput);
233 const lineStart = text.lastIndexOf("\n", Math.max(0, cursor - 1)) + 1;
234 const linePrefix = text.slice(lineStart, cursor);
235
236 if (linePrefix.startsWith("/")) {
237 const commandMatch = /^\/(\S*)$/.exec(linePrefix);
238 if (commandMatch) {
239 const commandQuery = commandMatch[1] ?? "";
240 return {
241 kind: "slash-command",
242 query: commandQuery,
243 rangeStart: lineStart,
244 rangeEnd: cursor,
245 };
246 }
247 }
248
249 const tokenStart = tokenStartForCursor(text, cursor);
250 const token = text.slice(tokenStart, cursor);
251 if (token.startsWith("$")) {
252 return {
253 kind: "skill",
254 query: token.slice(1),
255 rangeStart: tokenStart,
256 rangeEnd: cursor,
257 };
258 }
259 if (!token.startsWith("@")) {
260 return null;
261 }
262
263 return {
264 kind: "path",
265 query: token.slice(1),
266 rangeStart: tokenStart,
267 rangeEnd: cursor,
268 };
269}
270
271export function parseStandaloneComposerSlashCommand(
272 text: string,
273): Exclude<ComposerSlashCommand, "model"> | null {
274 const match = /^\/(plan|default)\s*$/i.exec(text.trim());
275 if (!match) {
276 return null;
277 }
278 const command = match[1]?.toLowerCase();
279 if (command === "plan") return "plan";
280 return "default";
281}
282
283export function replaceTextRange(
284 text: string,
285 rangeStart: number,
286 rangeEnd: number,
287 replacement: string,
288): { text: string; cursor: number } {
289 const safeStart = Math.max(0, Math.min(text.length, rangeStart));
290 const safeEnd = Math.max(safeStart, Math.min(text.length, rangeEnd));
291 const nextText = `${text.slice(0, safeStart)}${replacement}${text.slice(safeEnd)}`;
292 return { text: nextText, cursor: safeStart + replacement.length };
293}