Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composer-editor-mentions.ts

223 lines4.9 KB

1import {
2 INLINE_TERMINAL_CONTEXT_PLACEHOLDER,
3 type TerminalContextDraft,
4} from "./lib/terminalContext";
5import {
6 collectComposerInlineTokens,
7 type ComposerInlineToken,
8} from "@t3tools/shared/composerInlineTokens";
9​
10export type ComposerPromptSegment =
11 | {
12 type: "text";
13 text: string;
14 }
15 | {
16 type: "mention";
17 path: string;
18 source: string;
19 }
20 | {
21 type: "skill";
22 name: string;
23 }
24 | {
25 type: "terminal-context";
26 context: TerminalContextDraft | null;
27 };
28​
29function rangeIncludesIndex(start: number, end: number, index: number): boolean {
30 return start <= index && index < end;
32​
33function pushTextSegment(segments: ComposerPromptSegment[], text: string): void {
34 if (!text) return;
35 const last = segments[segments.length - 1];
36 if (last && last.type === "text") {
37 last.text += text;
38 return;
39 }
40 segments.push({ type: "text", text });
42​
43function forEachPromptSegmentSlice(
44 prompt: string,
45 visitor: (
46 slice:
47 | {
48 type: "text";
49 text: string;
50 promptOffset: number;
51 }
52 | {
53 type: "terminal-context";
54 promptOffset: number;
55 },
56 ) => boolean | void,
57): boolean {
58 let textCursor = 0;
59​
60 for (let index = 0; index < prompt.length; index += 1) {
61 if (prompt[index] !== INLINE_TERMINAL_CONTEXT_PLACEHOLDER) {
62 continue;
63 }
64​
65 if (
66 index > textCursor &&
67 visitor({
68 type: "text",
69 text: prompt.slice(textCursor, index),
70 promptOffset: textCursor,
71 }) === true
72 ) {
73 return true;
74 }
75 if (visitor({ type: "terminal-context", promptOffset: index }) === true) {
76 return true;
77 }
78 textCursor = index + 1;
79 }
80​
81 if (
82 textCursor < prompt.length &&
83 visitor({
84 type: "text",
85 text: prompt.slice(textCursor),
86 promptOffset: textCursor,
87 }) === true
88 ) {
89 return true;
90 }
91​
92 return false;
94​
95function forEachPromptTextSlice(
96 prompt: string,
97 visitor: (text: string, promptOffset: number) => boolean | void,
98): boolean {
99 return forEachPromptSegmentSlice(prompt, (slice) => {
100 if (slice.type !== "text") {
101 return false;
102 }
103 return visitor(slice.text, slice.promptOffset);
104 });
106​
107function forEachMentionMatch(
108 prompt: string,
109 visitor: (
110 match: Extract<ComposerInlineToken, { type: "mention" }>,
111 promptOffset: number,
112 ) => boolean | void,
113): boolean {
114 return forEachPromptTextSlice(prompt, (text, promptOffset) => {
115 for (const match of collectComposerInlineTokens(text)) {
116 if (match.type !== "mention") {
117 continue;
118 }
119 if (visitor(match, promptOffset) === true) {
120 return true;
121 }
122 }
123 return false;
124 });
126​
127function splitPromptTextIntoComposerSegments(text: string): ComposerPromptSegment[] {
128 const segments: ComposerPromptSegment[] = [];
129 if (!text) {
130 return segments;
131 }
132​
133 const tokenMatches = collectComposerInlineTokens(text);
134 let cursor = 0;
135 for (const match of tokenMatches) {
136 if (match.start < cursor) {
137 continue;
138 }
139​
140 if (match.start > cursor) {
141 pushTextSegment(segments, text.slice(cursor, match.start));
142 }
143​
144 if (match.type === "mention") {
145 segments.push({
146 type: "mention",
147 path: match.value,
148 source: match.source,
149 });
150 } else {
151 segments.push({ type: "skill", name: match.value });
152 }
153​
154 cursor = match.end;
155 }
156​
157 if (cursor < text.length) {
158 pushTextSegment(segments, text.slice(cursor));
159 }
160​
161 return segments;
163​
164export function selectionTouchesMentionBoundary(
165 prompt: string,
166 start: number,
167 end: number,
168): boolean {
169 if (!prompt || start >= end) {
170 return false;
171 }
172​
173 return forEachMentionMatch(prompt, (match, promptOffset) => {
174 const mentionStart = promptOffset + match.start;
175 const mentionEnd = promptOffset + match.end;
176 const beforeMentionIndex = mentionStart - 1;
177 const afterMentionIndex = mentionEnd;
178​
179 if (
180 beforeMentionIndex >= 0 &&
181 /\s/.test(prompt[beforeMentionIndex] ?? "") &&
182 rangeIncludesIndex(start, end, beforeMentionIndex)
183 ) {
184 return true;
185 }
186​
187 if (
188 afterMentionIndex < prompt.length &&
189 /\s/.test(prompt[afterMentionIndex] ?? "") &&
190 rangeIncludesIndex(start, end, afterMentionIndex)
191 ) {
192 return true;
193 }
194 return false;
195 });
197​
198export function splitPromptIntoComposerSegments(
199 prompt: string,
200 terminalContexts: ReadonlyArray<TerminalContextDraft> = [],
201): ComposerPromptSegment[] {
202 if (!prompt) {
203 return [];
204 }
205​
206 const segments: ComposerPromptSegment[] = [];
207 let terminalContextIndex = 0;
208 forEachPromptSegmentSlice(prompt, (slice) => {
209 if (slice.type === "text") {
210 segments.push(...splitPromptTextIntoComposerSegments(slice.text));
211 return false;
212 }
213​
214 segments.push({
215 type: "terminal-context",
216 context: terminalContexts[terminalContextIndex] ?? null,
217 });
218 terminalContextIndex += 1;
219 return false;
220 });
221​
222 return segments;