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

composerTrigger.ts

126 lines3.5 KB

1export type ComposerTriggerKind = "path" | "slash-command" | "slash-model" | "skill";
2export type ComposerSlashCommand = "model" | "plan" | "default";
3​
4export interface ComposerTrigger {
5 kind: ComposerTriggerKind;
6 query: string;
7 rangeStart: number;
8 rangeEnd: number;
9}
10​
11const SIMPLE_MENTION_PATH_REGEX = /^[^\s@"\\]+$/;
12​
13export function serializeComposerMentionPath(path: string): string {
14 if (SIMPLE_MENTION_PATH_REGEX.test(path)) {
15 return path;
16 }
17 return `"${path.replaceAll("\\", "\\\\").replaceAll('"', '\\"')}"`;
19​
20function clampCursor(text: string, cursor: number): number {
21 if (!Number.isFinite(cursor)) return text.length;
22 return Math.max(0, Math.min(text.length, Math.floor(cursor)));
24​
25function isWhitespace(char: string): boolean {
26 return char === " " || char === "\n" || char === "\t" || char === "\r";
28​
29/**
30 * Detect an active trigger (@path, $skill, /command) at the cursor position.
31 *
32 * Accepts an optional `isWhitespaceChar` override so callers with inline
33 * placeholder characters (e.g. terminal context chips on web) can treat
34 * those as token boundaries.
35 */
36export function detectComposerTrigger(
37 text: string,
38 cursorInput: number,
39 isWhitespaceChar?: (char: string) => boolean,
40): ComposerTrigger | null {
41 const cursor = clampCursor(text, cursorInput);
42 const lineStart = text.lastIndexOf("\n", Math.max(0, cursor - 1)) + 1;
43 const linePrefix = text.slice(lineStart, cursor);
44​
45 if (linePrefix.startsWith("/")) {
46 const commandMatch = /^\/(\S*)$/.exec(linePrefix);
47 if (commandMatch) {
48 const commandQuery = commandMatch[1] ?? "";
49 if (commandQuery.toLowerCase() === "model") {
50 return {
51 kind: "slash-model",
52 query: "",
53 rangeStart: lineStart,
54 rangeEnd: cursor,
55 };
56 }
57 return {
58 kind: "slash-command",
59 query: commandQuery,
60 rangeStart: lineStart,
61 rangeEnd: cursor,
62 };
63 }
64​
65 const modelMatch = /^\/model(?:\s+(.*))?$/.exec(linePrefix);
66 if (modelMatch) {
67 return {
68 kind: "slash-model",
69 query: (modelMatch[1] ?? "").trim(),
70 rangeStart: lineStart,
71 rangeEnd: cursor,
72 };
73 }
74 }
75​
76 const wsCheck = isWhitespaceChar ?? isWhitespace;
77 let tokenIdx = cursor - 1;
78 while (tokenIdx >= 0 && !wsCheck(text[tokenIdx] ?? "")) {
79 tokenIdx -= 1;
80 }
81 const tokenStart = tokenIdx + 1;
82​
83 const token = text.slice(tokenStart, cursor);
84 if (token.startsWith("$")) {
85 return {
86 kind: "skill",
87 query: token.slice(1),
88 rangeStart: tokenStart,
89 rangeEnd: cursor,
90 };
91 }
92 if (!token.startsWith("@")) {
93 return null;
94 }
95​
96 return {
97 kind: "path",
98 query: token.slice(1),
99 rangeStart: tokenStart,
100 rangeEnd: cursor,
101 };
103​
104export function parseStandaloneComposerSlashCommand(
105 text: string,
106): Exclude<ComposerSlashCommand, "model"> | null {
107 const match = /^\/(plan|default)\s*$/i.exec(text.trim());
108 if (!match) {
109 return null;
110 }
111 const command = match[1]?.toLowerCase();
112 if (command === "plan") return "plan";
113 return "default";
115​
116export function replaceTextRange(
117 text: string,
118 rangeStart: number,
119 rangeEnd: number,
120 replacement: string,
121): { text: string; cursor: number } {
122 const safeStart = Math.max(0, Math.min(text.length, rangeStart));
123 const safeEnd = Math.max(safeStart, Math.min(text.length, rangeEnd));
124 const nextText = `${text.slice(0, safeStart)}${replacement}${text.slice(safeEnd)}`;
125 return { text: nextText, cursor: safeStart + replacement.length };