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

composerTrigger.ts

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