The best way to control Claude Code, Codex and more from any computer or phone
| 1 | export type ComposerTriggerKind = "path" | "pull-request" | "slash-command" | "skill"; |
| 2 | export type ComposerSlashCommand = "model" | "plan" | "default"; |
| 3 | |
| 4 | export interface ComposerTrigger { |
| 5 | kind: ComposerTriggerKind; |
| 6 | query: string; |
| 7 | rangeStart: number; |
| 8 | rangeEnd: number; |
| 9 | } |
| 10 | |
| 11 | function composerFileLinkBasename(path: string): string { |
| 12 | const separatorIndex = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\")); |
| 13 | return separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path; |
| 14 | } |
| 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. |
| 18 | function escapeMarkdownLinkLabel(label: string): string { |
| 19 | return label.replace(/[\\[\]*_]/g, "\\$&"); |
| 20 | } |
| 21 | |
| 22 | function encodeMarkdownLinkDestination(path: string): string { |
| 23 | return encodeURI(path) |
| 24 | .replaceAll("(", "%28") |
| 25 | .replaceAll(")", "%29") |
| 26 | .replaceAll("#", "%23") |
| 27 | .replaceAll("?", "%3F") |
| 28 | .replaceAll("\\", "%5C"); |
| 29 | } |
| 30 | |
| 31 | export function serializeComposerFileLink(path: string): string { |
| 32 | const label = escapeMarkdownLinkLabel(composerFileLinkBasename(path)); |
| 33 | return `[${label}](${encodeMarkdownLinkDestination(path)})`; |
| 34 | } |
| 35 | |
| 36 | function 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))); |
| 39 | } |
| 40 | |
| 41 | function isWhitespace(char: string): boolean { |
| 42 | return char === " " || char === "\n" || char === "\t" || char === "\r"; |
| 43 | } |
| 44 | |
| 45 | /** A second slash or backslash makes the whole word a path, even when the caret precedes it. */ |
| 46 | export 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)); |
| 57 | } |
| 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 | */ |
| 64 | export function isCurrencyAmountQuery(query: string): boolean { |
| 65 | return /^\.?[0-9][0-9,._]*(?:[kKmMbBtT]|[eE][0-9]+)?[.,;:!?)]*$/.test(query); |
| 66 | } |
| 67 | |
| 68 | export 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 | }; |
| 79 | } |
| 80 | |
| 81 | export 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 | }; |
| 126 | } |
| 127 | |
| 128 | export 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 }; |
| 138 | } |
| 139 | |
| 140 | /** `/clear` as the whole message arms a fresh session instead of sending. */ |
| 141 | export function isClearComposerCommand(text: string): boolean { |
| 142 | return /^\/clear\s*$/i.test(text.trim()); |
| 143 | } |
| 144 | |
| 145 | /** `/side [prompt]`, or its alias `/btw [prompt]`, as the whole message. */ |
| 146 | export function parseSideChatCommand(text: string): { prompt: string } | null { |
| 147 | const match = /^\/(?:side|btw)(?:\s+([\s\S]*))?$/i.exec(text.trim()); |
| 148 | return match ? { prompt: match[1]?.trim() ?? "" } : null; |
| 149 | } |