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

composer-logic.ts

280 lines7.4 KB

1import { splitPromptIntoComposerSegments } from "./composer-editor-mentions";
2import { INLINE_TERMINAL_CONTEXT_PLACEHOLDER } from "./lib/terminalContext";
3​
4export type ComposerTriggerKind = "path" | "slash-command" | "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.source.length;
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.source.length;
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 return {
228 kind: "slash-command",
229 query: commandQuery,
230 rangeStart: lineStart,
231 rangeEnd: cursor,
232 };
233 }
234 }
235​
236 const tokenStart = tokenStartForCursor(text, cursor);
237 const token = text.slice(tokenStart, cursor);
238 if (token.startsWith("$")) {
239 return {
240 kind: "skill",
241 query: token.slice(1),
242 rangeStart: tokenStart,
243 rangeEnd: cursor,
244 };
245 }
246 if (!token.startsWith("@")) {
247 return null;
248 }
249​
250 return {
251 kind: "path",
252 query: token.slice(1),
253 rangeStart: tokenStart,
254 rangeEnd: cursor,
255 };
257​
258export function parseStandaloneComposerSlashCommand(
259 text: string,
260): Exclude<ComposerSlashCommand, "model"> | null {
261 const match = /^\/(plan|default)\s*$/i.exec(text.trim());
262 if (!match) {
263 return null;
264 }
265 const command = match[1]?.toLowerCase();
266 if (command === "plan") return "plan";
267 return "default";
269​
270export function replaceTextRange(
271 text: string,
272 rangeStart: number,
273 rangeEnd: number,
274 replacement: string,
275): { text: string; cursor: number } {
276 const safeStart = Math.max(0, Math.min(text.length, rangeStart));
277 const safeEnd = Math.max(safeStart, Math.min(text.length, rangeEnd));
278 const nextText = `${text.slice(0, safeStart)}${replacement}${text.slice(safeEnd)}`;
279 return { text: nextText, cursor: safeStart + replacement.length };