Open History to see the last commit
1import {
2 INLINE_TERMINAL_CONTEXT_PLACEHOLDER,
3 type TerminalContextDraft,
4} from "./lib/terminalContext";
5
6export type ComposerPromptSegment =
7 | {
8 type: "text";
9 text: string;
10 }
11 | {
12 type: "mention";
13 path: string;
14 }
15 | {
16 type: "skill";
17 name: string;
18 }
19 | {
20 type: "terminal-context";
21 context: TerminalContextDraft | null;
22 };
23
24const MENTION_TOKEN_REGEX = /(^|\s)@([^\s@]+)(?=\s)/g;
25const SKILL_TOKEN_REGEX = /(^|\s)\$([a-zA-Z][a-zA-Z0-9:_-]*)(?=\s)/g;
26
27function rangeIncludesIndex(start: number, end: number, index: number): boolean {
28 return start <= index && index < end;
29}
30
31function pushTextSegment(segments: ComposerPromptSegment[], text: string): void {
32 if (!text) return;
33 const last = segments[segments.length - 1];
34 if (last && last.type === "text") {
35 last.text += text;
36 return;
37 }
38 segments.push({ type: "text", text });
39}
40
41type InlineTokenMatch =
42 | {
43 type: "mention";
44 value: string;
45 start: number;
46 end: number;
47 }
48 | {
49 type: "skill";
50 value: string;
51 start: number;
52 end: number;
53 };
54
55function collectInlineTokenMatches(text: string): InlineTokenMatch[] {
56 const matches: InlineTokenMatch[] = [];
57
58 for (const match of text.matchAll(MENTION_TOKEN_REGEX)) {
59 const fullMatch = match[0];
60 const prefix = match[1] ?? "";
61 const path = match[2] ?? "";
62 const matchIndex = match.index ?? 0;
63 const start = matchIndex + prefix.length;
64 const end = start + fullMatch.length - prefix.length;
65 if (path.length > 0) {
66 matches.push({ type: "mention", value: path, start, end });
67 }
68 }
69
70 for (const match of text.matchAll(SKILL_TOKEN_REGEX)) {
71 const fullMatch = match[0];
72 const prefix = match[1] ?? "";
73 const skillName = match[2] ?? "";
74 const matchIndex = match.index ?? 0;
75 const start = matchIndex + prefix.length;
76 const end = start + fullMatch.length - prefix.length;
77 if (skillName.length > 0) {
78 matches.push({ type: "skill", value: skillName, start, end });
79 }
80 }
81
82 return matches.toSorted((left, right) => left.start - right.start);
83}
84
85function forEachPromptSegmentSlice(
86 prompt: string,
87 visitor: (
88 slice:
89 | {
90 type: "text";
91 text: string;
92 promptOffset: number;
93 }
94 | {
95 type: "terminal-context";
96 promptOffset: number;
97 },
98 ) => boolean | void,
99): boolean {
100 let textCursor = 0;
101
102 for (let index = 0; index < prompt.length; index += 1) {
103 if (prompt[index] !== INLINE_TERMINAL_CONTEXT_PLACEHOLDER) {
104 continue;
105 }
106
107 if (
108 index > textCursor &&
109 visitor({
110 type: "text",
111 text: prompt.slice(textCursor, index),
112 promptOffset: textCursor,
113 }) === true
114 ) {
115 return true;
116 }
117 if (visitor({ type: "terminal-context", promptOffset: index }) === true) {
118 return true;
119 }
120 textCursor = index + 1;
121 }
122
123 if (
124 textCursor < prompt.length &&
125 visitor({
126 type: "text",
127 text: prompt.slice(textCursor),
128 promptOffset: textCursor,
129 }) === true
130 ) {
131 return true;
132 }
133
134 return false;
135}
136
137function forEachPromptTextSlice(
138 prompt: string,
139 visitor: (text: string, promptOffset: number) => boolean | void,
140): boolean {
141 return forEachPromptSegmentSlice(prompt, (slice) => {
142 if (slice.type !== "text") {
143 return false;
144 }
145 return visitor(slice.text, slice.promptOffset);
146 });
147}
148
149function forEachMentionMatch(
150 prompt: string,
151 visitor: (match: RegExpMatchArray, promptOffset: number) => boolean | void,
152): boolean {
153 return forEachPromptTextSlice(prompt, (text, promptOffset) => {
154 for (const match of text.matchAll(MENTION_TOKEN_REGEX)) {
155 if (visitor(match, promptOffset) === true) {
156 return true;
157 }
158 }
159 return false;
160 });
161}
162
163function splitPromptTextIntoComposerSegments(text: string): ComposerPromptSegment[] {
164 const segments: ComposerPromptSegment[] = [];
165 if (!text) {
166 return segments;
167 }
168
169 const tokenMatches = collectInlineTokenMatches(text);
170 let cursor = 0;
171 for (const match of tokenMatches) {
172 if (match.start < cursor) {
173 continue;
174 }
175
176 if (match.start > cursor) {
177 pushTextSegment(segments, text.slice(cursor, match.start));
178 }
179
180 if (match.type === "mention") {
181 segments.push({ type: "mention", path: match.value });
182 } else {
183 segments.push({ type: "skill", name: match.value });
184 }
185
186 cursor = match.end;
187 }
188
189 if (cursor < text.length) {
190 pushTextSegment(segments, text.slice(cursor));
191 }
192
193 return segments;
194}
195
196export function selectionTouchesMentionBoundary(
197 prompt: string,
198 start: number,
199 end: number,
200): boolean {
201 if (!prompt || start >= end) {
202 return false;
203 }
204
205 return forEachMentionMatch(prompt, (match, promptOffset) => {
206 const fullMatch = match[0];
207 const prefix = match[1] ?? "";
208 const matchIndex = match.index ?? 0;
209 const mentionStart = promptOffset + matchIndex + prefix.length;
210 const mentionEnd = mentionStart + fullMatch.length - prefix.length;
211 const beforeMentionIndex = mentionStart - 1;
212 const afterMentionIndex = mentionEnd;
213
214 if (
215 beforeMentionIndex >= 0 &&
216 /\s/.test(prompt[beforeMentionIndex] ?? "") &&
217 rangeIncludesIndex(start, end, beforeMentionIndex)
218 ) {
219 return true;
220 }
221
222 if (
223 afterMentionIndex < prompt.length &&
224 /\s/.test(prompt[afterMentionIndex] ?? "") &&
225 rangeIncludesIndex(start, end, afterMentionIndex)
226 ) {
227 return true;
228 }
229 return false;
230 });
231}
232
233export function splitPromptIntoComposerSegments(
234 prompt: string,
235 terminalContexts: ReadonlyArray<TerminalContextDraft> = [],
236): ComposerPromptSegment[] {
237 if (!prompt) {
238 return [];
239 }
240
241 const segments: ComposerPromptSegment[] = [];
242 let terminalContextIndex = 0;
243 forEachPromptSegmentSlice(prompt, (slice) => {
244 if (slice.type === "text") {
245 segments.push(...splitPromptTextIntoComposerSegments(slice.text));
246 return false;
247 }
248
249 segments.push({
250 type: "terminal-context",
251 context: terminalContexts[terminalContextIndex] ?? null,
252 });
253 terminalContextIndex += 1;
254 return false;
255 });
256
257 return segments;
258}