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

composerTrigger.ts

157 lines5.1 KB

1export type ComposerTriggerKind = "path" | "pull-request" | "slash-command" | "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 composerFileLinkBasename(path: string): string {
12 const separatorIndex = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
13 return separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
15​
16// Emphasis markers too: `__init__.py` would otherwise render as bold "init.py",
17// which no longer matches the path, so the label shows beside the chip.
18function escapeMarkdownLinkLabel(label: string): string {
19 return label.replace(/[\\[\]*_]/g, "\\$&");
21​
22function encodeMarkdownLinkDestination(path: string): string {
23 return encodeURI(path)
24 .replaceAll("(", "%28")
25 .replaceAll(")", "%29")
26 .replaceAll("#", "%23")
27 .replaceAll("?", "%3F")
28 .replaceAll("\\", "%5C");
30​
31export function serializeComposerFileLink(path: string): string {
32 const label = escapeMarkdownLinkLabel(composerFileLinkBasename(path));
33 return `[${label}](${encodeMarkdownLinkDestination(path)})`;
35​
36function clampCursor(text: string, cursor: number): number {
37 if (!Number.isFinite(cursor)) return text.length;
38 return Math.max(0, Math.min(text.length, Math.floor(cursor)));
40​
41function isWhitespace(char: string): boolean {
42 return char === " " || char === "\n" || char === "\t" || char === "\r";
44​
45/** A second slash or backslash makes the whole word a path, even when the caret precedes it. */
46export function isPathWord(
47 text: string,
48 wordStart: number,
49 cursor: number,
50 isWhitespaceChar: (char: string) => boolean,
51): boolean {
52 let wordEnd = cursor;
53 while (wordEnd < text.length && !isWhitespaceChar(text[wordEnd] ?? "")) {
54 wordEnd += 1;
55 }
56 return /[/\\]/.test(text.slice(wordStart + 1, wordEnd));
58​
59/**
60 * `$5`, `€9.99` and `$10k` are money, not skills. Matches the amounts the inline skill
61 * token regexes already refuse to render as skills, plus the punctuation prose puts after
62 * one (`$10k.`, `($5)`).
63 */
64export function isCurrencyAmountQuery(query: string): boolean {
65 return /^\.?[0-9][0-9,._]*(?:[kKmMbBtT]|[eE][0-9]+)?[.,;:!?)]*$/.test(query);
67​
68export function detectComposerPullRequestTrigger(token: string, cursor: number) {
69 if (/^[@\p{Sc}]/u.test(token)) return null;
70 const match = /(?:^|\p{Ps})#([\p{L}\p{N}][\p{L}\p{N}_-]*)?$/u.exec(token);
71 if (!match) return null;
72 const query = match[1] ?? "";
73 return {
74 kind: "pull-request" as const,
75 query,
76 rangeStart: cursor - query.length - 1,
77 rangeEnd: cursor,
78 };
80​
81export function detectComposerTrigger(
82 text: string,
83 cursorInput: number,
84 isWhitespaceChar?: (char: string) => boolean,
85): ComposerTrigger | null {
86 const cursor = clampCursor(text, cursorInput);
87 const wsCheck = isWhitespaceChar ?? isWhitespace;
88 let tokenIdx = cursor - 1;
89 while (tokenIdx >= 0 && !wsCheck(text[tokenIdx] ?? "")) {
90 tokenIdx -= 1;
91 }
92 const tokenStart = tokenIdx + 1;
93​
94 const token = text.slice(tokenStart, cursor);
95 if (token.startsWith("/")) {
96 if (isPathWord(text, tokenStart, cursor, wsCheck)) return null;
97 return {
98 kind: "slash-command",
99 query: token.slice(1),
100 rangeStart: tokenStart,
101 rangeEnd: cursor,
102 };
103 }
104 const pullRequestTrigger = detectComposerPullRequestTrigger(token, cursor);
105 if (pullRequestTrigger) return pullRequestTrigger;
106 const skillPrefix = /^\p{Sc}/u.exec(token);
107 if (skillPrefix) {
108 if (isCurrencyAmountQuery(token.slice(skillPrefix[0].length))) return null;
109 return {
110 kind: "skill",
111 query: token.slice(skillPrefix[0].length),
112 rangeStart: tokenStart,
113 rangeEnd: cursor,
114 };
115 }
116 if (!token.startsWith("@")) {
117 return null;
118 }
119​
120 return {
121 kind: "path",
122 query: token.slice(1),
123 rangeStart: tokenStart,
124 rangeEnd: cursor,
125 };
127​
128export function replaceTextRange(
129 text: string,
130 rangeStart: number,
131 rangeEnd: number,
132 replacement: string,
133): { text: string; cursor: number } {
134 const safeStart = Math.max(0, Math.min(text.length, rangeStart));
135 const safeEnd = Math.max(safeStart, Math.min(text.length, rangeEnd));
136 const nextText = `${text.slice(0, safeStart)}${replacement}${text.slice(safeEnd)}`;
137 return { text: nextText, cursor: safeStart + replacement.length };
139​
140/** A message that only invokes a slash command, such as `/context all`, not a path like `/src/a.ts`. */
141export function isSlashCommand(message: {
142 readonly text: string;
143 readonly attachments: ReadonlyArray<unknown>;
144}): boolean {
145 return message.attachments.length === 0 && /^\/[\w:-]+(?:\s|$)/u.test(message.text.trim());
147​
148/** `/clear` as the whole message arms a fresh session instead of sending. */
149export function isClearComposerCommand(text: string): boolean {
150 return /^\/clear\s*$/i.test(text.trim());
152​
153/** `/side [prompt]`, or its alias `/btw [prompt]`, as the whole message. */
154export function parseSideChatCommand(text: string): { prompt: string } | null {
155 const match = /^\/(?:side|btw)(?:\s+([\s\S]*))?$/i.exec(text.trim());
156 return match ? { prompt: match[1]?.trim() ?? "" } : null;