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 readonly leadingText: string | undefined;
19 /** `/name` plus the trailing text, ready to be the message's last text block. */
20 readonly commandText: string;
21 readonly skillName: string;
22}
23
24export function planClaudeSkillDispatch(
25 prompt: string,
26 skillNames: ReadonlySet<string>,
27): ClaudeSkillDispatch | undefined {
28 const mentions = [...prompt.matchAll(SKILL_MENTION_PATTERN)].flatMap((match) => {
29 const name = match[2] ?? "";
30 if (!skillNames.has(name)) return [];
31 const start = (match.index ?? 0) + (match[1]?.length ?? 0);
32 return [{ name, start, end: (match.index ?? 0) + match[0].length }];
33 });
34 const last = mentions.at(-1);
35 if (!last) {
36 return undefined;
37 }
38
39 const leading = prompt.slice(0, last.start);
40 const trailing = prompt.slice(last.end);
41 const leadingWithInlineSlashes = mentions
42 .slice(0, -1)
43 .reduceRight(
44 (text, mention) =>
45 `${text.slice(0, mention.start)}/${mention.name}${text.slice(mention.end)}`,
46 leading,
47 )
48 .trimEnd();
49
50 return {
51 leadingText: leadingWithInlineSlashes.length > 0 ? leadingWithInlineSlashes : undefined,
52 commandText: `/${last.name}${trailing}`.trimEnd(),
53 skillName: last.name,
54 };
55}