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