|
| 1 | +/** |
| 2 | + * ClaudeSkillDispatch — turns `$skill` mentions in a composer prompt into the |
| 3 | + * slash invocation Claude Code actually runs. |
| 4 | + * |
| 5 | + * The composer inserts `$name` for every provider. Codex parses that natively; |
| 6 | + * Claude Code does not, and treats it as prose. Claude Code's only user-side |
| 7 | + * invocation is a text block whose first character is `/`: the harness |
| 8 | + * expands `/name args` into the SKILL.md body, and every character after the |
| 9 | + * name (newlines included) arrives as `ARGUMENTS`. Verified against the CLI in |
| 10 | + * stream-json mode, which is what the Agent SDK uses: |
| 11 | + * |
| 12 | + * - The check runs on the LAST text block of the message. Earlier text |
| 13 | + * blocks are preserved verbatim, and image blocks may sit before it. |
| 14 | + * - Leading whitespace, or a `/name` that starts a later line of the same |
| 15 | + * block, is literal text. |
| 16 | + * - Only one skill expands per message; a second `/x` becomes argument text |
| 17 | + * (anthropics/claude-code#87113). The model still starts the rest through |
| 18 | + * its Skill tool when it reads `/name` in the prompt, so earlier mentions |
| 19 | + * are rewritten to `/name` inline. |
| 20 | + * |
| 21 | + * So one mention anywhere in the prompt becomes a guaranteed invocation, and |
| 22 | + * the user's text on either side is kept in order. |
| 23 | + * |
| 24 | + * @module provider/Drivers/ClaudeSkillDispatch |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * Same token shape the composer and timeline chips recognise |
| 29 | + * (`packages/shared/src/composerInlineTokens.ts`), so a rendered chip and a |
| 30 | + * dispatched skill are always the same set. |
| 31 | + */ |
| 32 | +const SKILL_MENTION_PATTERN = /(^|\s)\$([a-zA-Z][a-zA-Z0-9:_-]*)(?=\s|$)/g; |
| 33 | + |
| 34 | +export interface ClaudeSkillDispatch { |
| 35 | + /** Text before the dispatched mention, or `undefined` when it opens the prompt. */ |
| 36 | + readonly leadingText: string | undefined; |
| 37 | + /** `/name` plus the trailing text, ready to be the message's last text block. */ |
| 38 | + readonly commandText: string; |
| 39 | + readonly skillName: string; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Split `prompt` around the last `$skill` mention that names a known skill. |
| 44 | + * Returns `undefined` when there is nothing to dispatch, in which case the |
| 45 | + * prompt should go out unchanged. Mentions that do not match a discovered |
| 46 | + * skill stay literal: a `$HOME` in prose must not become a command. |
| 47 | + */ |
| 48 | +export function planClaudeSkillDispatch( |
| 49 | + prompt: string, |
| 50 | + skillNames: ReadonlySet<string>, |
| 51 | +): ClaudeSkillDispatch | undefined { |
| 52 | + const mentions = [...prompt.matchAll(SKILL_MENTION_PATTERN)].flatMap((match) => { |
| 53 | + const name = match[2] ?? ""; |
| 54 | + if (!skillNames.has(name)) return []; |
| 55 | + const start = (match.index ?? 0) + (match[1]?.length ?? 0); |
| 56 | + return [{ name, start, end: start + name.length + 1 }]; |
| 57 | + }); |
| 58 | + const last = mentions.at(-1); |
| 59 | + if (!last) { |
| 60 | + return undefined; |
| 61 | + } |
| 62 | + |
| 63 | + const leading = prompt.slice(0, last.start); |
| 64 | + const trailing = prompt.slice(last.end); |
| 65 | + const leadingWithInlineSlashes = mentions |
| 66 | + .slice(0, -1) |
| 67 | + .reduceRight( |
| 68 | + (text, mention) => `${text.slice(0, mention.start)}/${text.slice(mention.start + 1)}`, |
| 69 | + leading, |
| 70 | + ) |
| 71 | + .trimEnd(); |
| 72 | + |
| 73 | + return { |
| 74 | + leadingText: leadingWithInlineSlashes.length > 0 ? leadingWithInlineSlashes : undefined, |
| 75 | + commandText: `/${last.name}${trailing}`.trimEnd(), |
| 76 | + skillName: last.name, |
| 77 | + }; |
| 78 | +} |
0 commit comments