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

composer-editor-mentions.ts

247 lines5.9 KB

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