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

composer-logic.ts

187 lines5.2 KB

1import { splitPromptIntoComposerSegments } from "./composer-editor-mentions";
2​
3export type ComposerTriggerKind = "path" | "slash-command" | "slash-model";
4export type ComposerSlashCommand = "model" | "plan" | "default";
5​
6export interface ComposerTrigger {
7 kind: ComposerTriggerKind;
8 query: string;
9 rangeStart: number;
10 rangeEnd: number;
12​
13const SLASH_COMMANDS: readonly ComposerSlashCommand[] = ["model", "plan", "default"];
14​
15function clampCursor(text: string, cursor: number): number {
16 if (!Number.isFinite(cursor)) return text.length;
17 return Math.max(0, Math.min(text.length, Math.floor(cursor)));
19​
20function isWhitespace(char: string): boolean {
21 return char === " " || char === "\n" || char === "\t" || char === "\r";
23​
24function tokenStartForCursor(text: string, cursor: number): number {
25 let index = cursor - 1;
26 while (index >= 0 && !isWhitespace(text[index] ?? "")) {
27 index -= 1;
28 }
29 return index + 1;
31​
32export function expandCollapsedComposerCursor(text: string, cursorInput: number): number {
33 const collapsedCursor = clampCursor(text, cursorInput);
34 const segments = splitPromptIntoComposerSegments(text);
35 if (segments.length === 0) {
36 return collapsedCursor;
37 }
38​
39 let remaining = collapsedCursor;
40 let expandedCursor = 0;
41​
42 for (const segment of segments) {
43 if (segment.type === "mention") {
44 const expandedLength = segment.path.length + 1;
45 if (remaining <= 1) {
46 return expandedCursor + (remaining === 0 ? 0 : expandedLength);
47 }
48 remaining -= 1;
49 expandedCursor += expandedLength;
50 continue;
51 }
52​
53 const segmentLength = segment.text.length;
54 if (remaining <= segmentLength) {
55 return expandedCursor + remaining;
56 }
57 remaining -= segmentLength;
58 expandedCursor += segmentLength;
59 }
60​
61 return expandedCursor;
63​
64function collapsedSegmentLength(
65 segment: { type: "text"; text: string } | { type: "mention" },
66): number {
67 return segment.type === "mention" ? 1 : segment.text.length;
69​
70function clampCollapsedComposerCursor(
71 segments: ReadonlyArray<{ type: "text"; text: string } | { type: "mention" }>,
72 cursorInput: number,
73): number {
74 const collapsedLength = segments.reduce(
75 (total, segment) => total + collapsedSegmentLength(segment),
76 0,
77 );
78 if (!Number.isFinite(cursorInput)) {
79 return collapsedLength;
80 }
81 return Math.max(0, Math.min(collapsedLength, Math.floor(cursorInput)));
83​
84export function isCollapsedCursorAdjacentToMention(
85 text: string,
86 cursorInput: number,
87 direction: "left" | "right",
88): boolean {
89 const segments = splitPromptIntoComposerSegments(text);
90 if (!segments.some((segment) => segment.type === "mention")) {
91 return false;
92 }
93​
94 const cursor = clampCollapsedComposerCursor(segments, cursorInput);
95 let collapsedOffset = 0;
96​
97 for (const segment of segments) {
98 if (segment.type === "mention") {
99 if (direction === "left" && cursor === collapsedOffset + 1) {
100 return true;
101 }
102 if (direction === "right" && cursor === collapsedOffset) {
103 return true;
104 }
105 }
106 collapsedOffset += collapsedSegmentLength(segment);
107 }
108​
109 return false;
111​
112export function detectComposerTrigger(text: string, cursorInput: number): ComposerTrigger | null {
113 const cursor = clampCursor(text, cursorInput);
114 const lineStart = text.lastIndexOf("\n", Math.max(0, cursor - 1)) + 1;
115 const linePrefix = text.slice(lineStart, cursor);
116​
117 if (linePrefix.startsWith("/")) {
118 const commandMatch = /^\/(\S*)$/.exec(linePrefix);
119 if (commandMatch) {
120 const commandQuery = commandMatch[1] ?? "";
121 if (commandQuery.toLowerCase() === "model") {
122 return {
123 kind: "slash-model",
124 query: "",
125 rangeStart: lineStart,
126 rangeEnd: cursor,
127 };
128 }
129 if (SLASH_COMMANDS.some((command) => command.startsWith(commandQuery.toLowerCase()))) {
130 return {
131 kind: "slash-command",
132 query: commandQuery,
133 rangeStart: lineStart,
134 rangeEnd: cursor,
135 };
136 }
137 return null;
138 }
139​
140 const modelMatch = /^\/model(?:\s+(.*))?$/.exec(linePrefix);
141 if (modelMatch) {
142 return {
143 kind: "slash-model",
144 query: (modelMatch[1] ?? "").trim(),
145 rangeStart: lineStart,
146 rangeEnd: cursor,
147 };
148 }
149 }
150​
151 const tokenStart = tokenStartForCursor(text, cursor);
152 const token = text.slice(tokenStart, cursor);
153 if (!token.startsWith("@")) {
154 return null;
155 }
156​
157 return {
158 kind: "path",
159 query: token.slice(1),
160 rangeStart: tokenStart,
161 rangeEnd: cursor,
162 };
164​
165export function parseStandaloneComposerSlashCommand(
166 text: string,
167): Exclude<ComposerSlashCommand, "model"> | null {
168 const match = /^\/(plan|default)\s*$/i.exec(text.trim());
169 if (!match) {
170 return null;
171 }
172 const command = match[1]?.toLowerCase();
173 if (command === "plan") return "plan";
174 return "default";
176​
177export function replaceTextRange(
178 text: string,
179 rangeStart: number,
180 rangeEnd: number,
181 replacement: string,
182): { text: string; cursor: number } {
183 const safeStart = Math.max(0, Math.min(text.length, rangeStart));
184 const safeEnd = Math.max(safeStart, Math.min(text.length, rangeEnd));
185 const nextText = `${text.slice(0, safeStart)}${replacement}${text.slice(safeEnd)}`;
186 return { text: nextText, cursor: safeStart + replacement.length };