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

composer-logic.ts

184 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(segment: { type: "text"; text: string } | { type: "mention" }): number {
65 return segment.type === "mention" ? 1 : segment.text.length;
67​
68function clampCollapsedComposerCursor(
69 segments: ReadonlyArray<{ type: "text"; text: string } | { type: "mention" }>,
70 cursorInput: number,
71): number {
72 const collapsedLength = segments.reduce(
73 (total, segment) => total + collapsedSegmentLength(segment),
74 0,
75 );
76 if (!Number.isFinite(cursorInput)) {
77 return collapsedLength;
78 }
79 return Math.max(0, Math.min(collapsedLength, Math.floor(cursorInput)));
81​
82export function isCollapsedCursorAdjacentToMention(
83 text: string,
84 cursorInput: number,
85 direction: "left" | "right",
86): boolean {
87 const segments = splitPromptIntoComposerSegments(text);
88 if (!segments.some((segment) => segment.type === "mention")) {
89 return false;
90 }
91​
92 const cursor = clampCollapsedComposerCursor(segments, cursorInput);
93 let collapsedOffset = 0;
94​
95 for (const segment of segments) {
96 if (segment.type === "mention") {
97 if (direction === "left" && cursor === collapsedOffset + 1) {
98 return true;
99 }
100 if (direction === "right" && cursor === collapsedOffset) {
101 return true;
102 }
103 }
104 collapsedOffset += collapsedSegmentLength(segment);
105 }
106​
107 return false;
109​
110export function detectComposerTrigger(text: string, cursorInput: number): ComposerTrigger | null {
111 const cursor = clampCursor(text, cursorInput);
112 const lineStart = text.lastIndexOf("\n", Math.max(0, cursor - 1)) + 1;
113 const linePrefix = text.slice(lineStart, cursor);
114​
115 if (linePrefix.startsWith("/")) {
116 const commandMatch = /^\/(\S*)$/.exec(linePrefix);
117 if (commandMatch) {
118 const commandQuery = commandMatch[1] ?? "";
119 if (commandQuery.toLowerCase() === "model") {
120 return {
121 kind: "slash-model",
122 query: "",
123 rangeStart: lineStart,
124 rangeEnd: cursor,
125 };
126 }
127 if (SLASH_COMMANDS.some((command) => command.startsWith(commandQuery.toLowerCase()))) {
128 return {
129 kind: "slash-command",
130 query: commandQuery,
131 rangeStart: lineStart,
132 rangeEnd: cursor,
133 };
134 }
135 return null;
136 }
137​
138 const modelMatch = /^\/model(?:\s+(.*))?$/.exec(linePrefix);
139 if (modelMatch) {
140 return {
141 kind: "slash-model",
142 query: (modelMatch[1] ?? "").trim(),
143 rangeStart: lineStart,
144 rangeEnd: cursor,
145 };
146 }
147 }
148​
149 const tokenStart = tokenStartForCursor(text, cursor);
150 const token = text.slice(tokenStart, cursor);
151 if (!token.startsWith("@")) {
152 return null;
153 }
154​
155 return {
156 kind: "path",
157 query: token.slice(1),
158 rangeStart: tokenStart,
159 rangeEnd: cursor,
160 };
162​
163export function parseStandaloneComposerSlashCommand(text: string): Exclude<
164 ComposerSlashCommand,
165 "model"
166> | null {
167 const match = /^\/(plan|default)\s*$/i.exec(text.trim());
168 if (!match) {
169 return null;
170 }
171 return match[1]?.toLowerCase() === "plan" ? "plan" : "default";
173​
174export function replaceTextRange(
175 text: string,
176 rangeStart: number,
177 rangeEnd: number,
178 replacement: string,
179): { text: string; cursor: number } {
180 const safeStart = Math.max(0, Math.min(text.length, rangeStart));
181 const safeEnd = Math.max(safeStart, Math.min(text.length, rangeEnd));
182 const nextText = `${text.slice(0, safeStart)}${replacement}${text.slice(safeEnd)}`;
183 return { text: nextText, cursor: safeStart + replacement.length };