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

composer-logic.ts

280 lines7.8 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";
10​
11export type ComposerTriggerKind = "path" | "pull-request" | "slash-command" | "skill";
12export type ComposerSlashCommand = "model" | "plan" | "default";
13export type ComposerSubmissionIntent = "foreground" | "background";
14​
15export interface ComposerTrigger {
16 kind: ComposerTriggerKind;
17 query: string;
18 rangeStart: number;
19 rangeEnd: number;
21​
22export function formatAssistantCitationForComposer(citation: AssistantCitation, comment = "") {
23 return `${serializeAssistantCitation(withAssistantCitationComment(citation, comment))} `;
25​
26export function composerSubmissionIntentForEnter(input: {
27 isMobileViewport: boolean;
28 shiftKey: boolean;
29 modifierKey: boolean;
30 isDraftThread: boolean;
31}): ComposerSubmissionIntent | null {
32 if (input.isMobileViewport || input.shiftKey) {
33 return null;
34 }
35 return input.modifierKey && input.isDraftThread ? "background" : "foreground";
37​
38const isInlineTokenSegment = (segment: ComposerPromptSegment): boolean => segment.type !== "text";
39​
40function clampCursor(text: string, cursor: number): number {
41 if (!Number.isFinite(cursor)) return text.length;
42 return Math.max(0, Math.min(text.length, Math.floor(cursor)));
44​
45function isWhitespace(char: string): boolean {
46 return char === " " || char === "\n" || char === "\t" || char === "\r";
48​
49function tokenStartForCursor(text: string, cursor: number): number {
50 let index = cursor - 1;
51 while (index >= 0 && !isWhitespace(text[index] ?? "")) {
52 index -= 1;
53 }
54 return index + 1;
56​
57export function expandCollapsedComposerCursor(text: string, cursorInput: number): number {
58 const collapsedCursor = clampCursor(text, cursorInput);
59 const segments = splitPromptIntoComposerSegments(text);
60 if (segments.length === 0) {
61 return collapsedCursor;
62 }
63​
64 let remaining = collapsedCursor;
65 let expandedCursor = 0;
66​
67 for (const segment of segments) {
68 if (
69 segment.type === "mention" ||
70 segment.type === "citation" ||
71 segment.type === "context-reference"
72 ) {
73 const expandedLength = segment.source.length;
74 if (remaining <= 1) {
75 return expandedCursor + (remaining === 0 ? 0 : expandedLength);
76 }
77 remaining -= 1;
78 expandedCursor += expandedLength;
79 continue;
80 }
81 if (segment.type === "skill") {
82 const expandedLength = segment.name.length + 1;
83 if (remaining <= 1) {
84 return expandedCursor + (remaining === 0 ? 0 : expandedLength);
85 }
86 remaining -= 1;
87 expandedCursor += expandedLength;
88 continue;
89 }
90​
91 const segmentLength = segment.text.length;
92 if (remaining <= segmentLength) {
93 return expandedCursor + remaining;
94 }
95 remaining -= segmentLength;
96 expandedCursor += segmentLength;
97 }
98​
99 return expandedCursor;
101​
102function collapsedSegmentLength(segment: ComposerPromptSegment): number {
103 if (segment.type === "text") {
104 return segment.text.length;
105 }
106 return 1;
108​
109function clampCollapsedComposerCursorForSegments(
110 segments: ReadonlyArray<ComposerPromptSegment>,
111 cursorInput: number,
112): number {
113 const collapsedLength = segments.reduce(
114 (total, segment) => total + collapsedSegmentLength(segment),
115 0,
116 );
117 if (!Number.isFinite(cursorInput)) {
118 return collapsedLength;
119 }
120 return Math.max(0, Math.min(collapsedLength, Math.floor(cursorInput)));
122​
123export function clampCollapsedComposerCursor(text: string, cursorInput: number): number {
124 return clampCollapsedComposerCursorForSegments(
125 splitPromptIntoComposerSegments(text),
126 cursorInput,
127 );
129​
130export function collapseExpandedComposerCursor(text: string, cursorInput: number): number {
131 const expandedCursor = clampCursor(text, cursorInput);
132 const segments = splitPromptIntoComposerSegments(text);
133 if (segments.length === 0) {
134 return expandedCursor;
135 }
136​
137 let remaining = expandedCursor;
138 let collapsedCursor = 0;
139​
140 for (const segment of segments) {
141 if (
142 segment.type === "mention" ||
143 segment.type === "citation" ||
144 segment.type === "context-reference"
145 ) {
146 const expandedLength = segment.source.length;
147 if (remaining === 0) {
148 return collapsedCursor;
149 }
150 if (remaining <= expandedLength) {
151 return collapsedCursor + 1;
152 }
153 remaining -= expandedLength;
154 collapsedCursor += 1;
155 continue;
156 }
157 if (segment.type === "skill") {
158 const expandedLength = segment.name.length + 1;
159 if (remaining === 0) {
160 return collapsedCursor;
161 }
162 if (remaining <= expandedLength) {
163 return collapsedCursor + 1;
164 }
165 remaining -= expandedLength;
166 collapsedCursor += 1;
167 continue;
168 }
169​
170 const segmentLength = segment.text.length;
171 if (remaining <= segmentLength) {
172 return collapsedCursor + remaining;
173 }
174 remaining -= segmentLength;
175 collapsedCursor += segmentLength;
176 }
177​
178 return collapsedCursor;
180​
181export function isCollapsedCursorAdjacentToInlineToken(
182 text: string,
183 cursorInput: number,
184 direction: "left" | "right",
185): boolean {
186 const segments = splitPromptIntoComposerSegments(text);
187 if (!segments.some(isInlineTokenSegment)) {
188 return false;
189 }
190​
191 const cursor = clampCollapsedComposerCursorForSegments(segments, cursorInput);
192 let collapsedOffset = 0;
193​
194 for (const segment of segments) {
195 if (isInlineTokenSegment(segment)) {
196 if (direction === "left" && cursor === collapsedOffset + 1) {
197 return true;
198 }
199 if (direction === "right" && cursor === collapsedOffset) {
200 return true;
201 }
202 }
203 collapsedOffset += collapsedSegmentLength(segment);
204 }
205​
206 return false;
208​
209export function detectComposerTrigger(text: string, cursorInput: number): ComposerTrigger | null {
210 const cursor = clampCursor(text, cursorInput);
211 const lineStart = text.lastIndexOf("\n", Math.max(0, cursor - 1)) + 1;
212 const linePrefix = text.slice(lineStart, cursor);
213​
214 if (linePrefix.startsWith("/")) {
215 const commandMatch = /^\/(\S*)$/.exec(linePrefix);
216 if (commandMatch) {
217 const commandQuery = commandMatch[1] ?? "";
218 return {
219 kind: "slash-command",
220 query: commandQuery,
221 rangeStart: lineStart,
222 rangeEnd: cursor,
223 };
224 }
225 }
226​
227 const tokenStart = tokenStartForCursor(text, cursor);
228 const token = text.slice(tokenStart, cursor);
229 const pullRequestMatch = /^#([\p{L}\p{N}][\p{L}\p{N}_-]*)?$/u.exec(token);
230 if (pullRequestMatch) {
231 return {
232 kind: "pull-request",
233 query: pullRequestMatch[1] ?? "",
234 rangeStart: tokenStart,
235 rangeEnd: cursor,
236 };
237 }
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 };