Open History to see the last commit
1export type ComposerPromptSegment =
2 | {
3 type: "text";
4 text: string;
5 }
6 | {
7 type: "mention";
8 path: string;
9 };
10
11const MENTION_TOKEN_REGEX = /(^|\s)@([^\s@]+)(?=\s)/g;
12
13function pushTextSegment(segments: ComposerPromptSegment[], text: string): void {
14 if (!text) return;
15 const last = segments[segments.length - 1];
16 if (last && last.type === "text") {
17 last.text += text;
18 return;
19 }
20 segments.push({ type: "text", text });
21}
22
23export function splitPromptIntoComposerSegments(prompt: string): ComposerPromptSegment[] {
24 const segments: ComposerPromptSegment[] = [];
25 if (!prompt) {
26 return segments;
27 }
28
29 let cursor = 0;
30 for (const match of prompt.matchAll(MENTION_TOKEN_REGEX)) {
31 const fullMatch = match[0];
32 const prefix = match[1] ?? "";
33 const path = match[2] ?? "";
34 const matchIndex = match.index ?? 0;
35 const mentionStart = matchIndex + prefix.length;
36 const mentionEnd = mentionStart + fullMatch.length - prefix.length;
37
38 if (mentionStart > cursor) {
39 pushTextSegment(segments, prompt.slice(cursor, mentionStart));
40 }
41
42 if (path.length > 0) {
43 segments.push({ type: "mention", path });
44 } else {
45 pushTextSegment(segments, prompt.slice(mentionStart, mentionEnd));
46 }
47
48 cursor = mentionEnd;
49 }
50
51 if (cursor < prompt.length) {
52 pushTextSegment(segments, prompt.slice(cursor));
53 }
54
55 return segments;
56}