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

composer-logic.ts

298 lines7.9 KB

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