Open History to see the last commit
1/**
2 * Claude Code's only user-side invocation is a text block whose first character
3 * is `/`: the harness expands `/name args` into the SKILL.md body, and every
4 * character after the name (newlines included) arrives as `ARGUMENTS`. Only one
5 * skill expands per message; a second `/x` becomes argument text
6 * (anthropics/claude-code#87113).
7 */
8
9/**
10 * Same token shape the composer and timeline chips recognise
11 * (`packages/shared/src/composerInlineTokens.ts`), so a rendered chip and a
12 * dispatched skill are always the same set.
13 */
14const SKILL_MENTION_PATTERN =
15 /(^|\s)\p{Sc}(?![0-9][0-9_]*(?:[kKmMbBtT]|[eE][0-9]+)?(?:\s|$))(?=[a-zA-Z0-9:_-]*[a-zA-Z])([a-zA-Z0-9][a-zA-Z0-9:_-]*)(?=\s|$)/gu;
16
17export interface ClaudeSkillDispatch {
18 /** The whole prompt with mentions as `/name`, or `undefined` when the mention opens it. */
19 readonly leadingText: string | undefined;
20 /** `/name` plus the trailing text, ready to be the message's last text block. */
21 readonly commandText: string;
22 readonly skillName: string;
23}
24
25export function planClaudeSkillDispatch(
26 prompt: string,
27 skillNames: ReadonlySet<string>,
28): ClaudeSkillDispatch | undefined {
29 const mentions = [...prompt.matchAll(SKILL_MENTION_PATTERN)].flatMap((match) => {
30 const name = match[2] ?? "";
31 if (!skillNames.has(name)) return [];
32 const start = (match.index ?? 0) + (match[1]?.length ?? 0);
33 return [{ name, start, end: (match.index ?? 0) + match[0].length }];
34 });
35 const last = mentions.at(-1);
36 if (!last) {
37 return undefined;
38 }
39
40 const trailing = prompt.slice(last.end);
41 const promptWithInlineSlashes = mentions
42 .reduceRight(
43 (text, mention) =>
44 `${text.slice(0, mention.start)}/${mention.name}${text.slice(mention.end)}`,
45 prompt,
46 )
47 .trimEnd();
48
49 return {
50 leadingText:
51 prompt.slice(0, last.start).trim().length > 0 ? promptWithInlineSlashes : undefined,
52 commandText: `/${last.name}${trailing}`.trimEnd(),
53 skillName: last.name,
54 };
55}