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

composer-logic.ts

286 lines7.9 KB

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