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

composerTrigger.ts

149 lines4.3 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 composerFileLinkBasename(path: string): string {
21 const separatorIndex = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
22 return separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
24​
25function escapeMarkdownLinkLabel(label: string): string {
26 return label.replaceAll("\\", "\\\\").replaceAll("[", "\\[").replaceAll("]", "\\]");
28​
29function encodeMarkdownLinkDestination(path: string): string {
30 return encodeURI(path)
31 .replaceAll("(", "%28")
32 .replaceAll(")", "%29")
33 .replaceAll("#", "%23")
34 .replaceAll("?", "%3F")
35 .replaceAll("\\", "%5C");
37​
38export function serializeComposerFileLink(path: string): string {
39 const label = escapeMarkdownLinkLabel(composerFileLinkBasename(path));
40 return `[${label}](${encodeMarkdownLinkDestination(path)})`;
42​
43function clampCursor(text: string, cursor: number): number {
44 if (!Number.isFinite(cursor)) return text.length;
45 return Math.max(0, Math.min(text.length, Math.floor(cursor)));
47​
48function isWhitespace(char: string): boolean {
49 return char === " " || char === "\n" || char === "\t" || char === "\r";
51​
52/**
53 * Detect an active trigger (@path, $skill, /command) at the cursor position.
54 *
55 * Accepts an optional `isWhitespaceChar` override so callers with inline
56 * placeholder characters (e.g. terminal context chips on web) can treat
57 * those as token boundaries.
58 */
59export function detectComposerTrigger(
60 text: string,
61 cursorInput: number,
62 isWhitespaceChar?: (char: string) => boolean,
63): ComposerTrigger | null {
64 const cursor = clampCursor(text, cursorInput);
65 const lineStart = text.lastIndexOf("\n", Math.max(0, cursor - 1)) + 1;
66 const linePrefix = text.slice(lineStart, cursor);
67​
68 if (linePrefix.startsWith("/")) {
69 const commandMatch = /^\/(\S*)$/.exec(linePrefix);
70 if (commandMatch) {
71 const commandQuery = commandMatch[1] ?? "";
72 if (commandQuery.toLowerCase() === "model") {
73 return {
74 kind: "slash-model",
75 query: "",
76 rangeStart: lineStart,
77 rangeEnd: cursor,
78 };
79 }
80 return {
81 kind: "slash-command",
82 query: commandQuery,
83 rangeStart: lineStart,
84 rangeEnd: cursor,
85 };
86 }
87​
88 const modelMatch = /^\/model(?:\s+(.*))?$/.exec(linePrefix);
89 if (modelMatch) {
90 return {
91 kind: "slash-model",
92 query: (modelMatch[1] ?? "").trim(),
93 rangeStart: lineStart,
94 rangeEnd: cursor,
95 };
96 }
97 }
98​
99 const wsCheck = isWhitespaceChar ?? isWhitespace;
100 let tokenIdx = cursor - 1;
101 while (tokenIdx >= 0 && !wsCheck(text[tokenIdx] ?? "")) {
102 tokenIdx -= 1;
103 }
104 const tokenStart = tokenIdx + 1;
105​
106 const token = text.slice(tokenStart, cursor);
107 if (token.startsWith("$")) {
108 return {
109 kind: "skill",
110 query: token.slice(1),
111 rangeStart: tokenStart,
112 rangeEnd: cursor,
113 };
114 }
115 if (!token.startsWith("@")) {
116 return null;
117 }
118​
119 return {
120 kind: "path",
121 query: token.slice(1),
122 rangeStart: tokenStart,
123 rangeEnd: cursor,
124 };
126​
127export function parseStandaloneComposerSlashCommand(
128 text: string,
129): Exclude<ComposerSlashCommand, "model"> | null {
130 const match = /^\/(plan|default)\s*$/i.exec(text.trim());
131 if (!match) {
132 return null;
133 }
134 const command = match[1]?.toLowerCase();
135 if (command === "plan") return "plan";
136 return "default";
138​
139export function replaceTextRange(
140 text: string,
141 rangeStart: number,
142 rangeEnd: number,
143 replacement: string,
144): { text: string; cursor: number } {
145 const safeStart = Math.max(0, Math.min(text.length, rangeStart));
146 const safeEnd = Math.max(safeStart, Math.min(text.length, rangeEnd));
147 const nextText = `${text.slice(0, safeStart)}${replacement}${text.slice(safeEnd)}`;
148 return { text: nextText, cursor: safeStart + replacement.length };