The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { ServerProviderSkill } from "@t3tools/contracts"; |
| 2 | |
| 3 | /** |
| 4 | * Claude expands one skill per message only when `/name` starts the text block. |
| 5 | * Everything after the name, including newlines or a second `/x`, becomes `ARGUMENTS`. |
| 6 | * https://github.com/anthropics/claude-code/issues/87113 |
| 7 | */ |
| 8 | |
| 9 | /** Match packages/shared/src/composerInlineTokens.ts so displayed chips and dispatched skills accept the same tokens. */ |
| 10 | const SKILL_MENTION_PATTERN = |
| 11 | /(^|\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; |
| 12 | |
| 13 | export type ClaudeSkillTarget = Pick<ServerProviderSkill, "path" | "userInvocationOnly">; |
| 14 | |
| 15 | export interface ClaudeSkillDispatch { |
| 16 | /** The whole prompt with mentions as `/name`, or `undefined` when the mention opens it. */ |
| 17 | readonly leadingText: string | undefined; |
| 18 | /** `/name` plus the trailing text, ready to be the message's last text block. */ |
| 19 | readonly commandText: string; |
| 20 | } |
| 21 | |
| 22 | export function planClaudeSkillDispatch( |
| 23 | prompt: string, |
| 24 | skills: ReadonlyMap<string, ClaudeSkillTarget>, |
| 25 | /** Mentions before this offset, such as inline handoff history, stay plain text. */ |
| 26 | mentionsFrom = 0, |
| 27 | ): ClaudeSkillDispatch | undefined { |
| 28 | const mentions = [...prompt.matchAll(SKILL_MENTION_PATTERN)].flatMap((match) => { |
| 29 | const name = match[2] ?? ""; |
| 30 | const skill = skills.get(name); |
| 31 | const start = (match.index ?? 0) + (match[1]?.length ?? 0); |
| 32 | if (!skill || start < mentionsFrom) return []; |
| 33 | return [{ name, skill, start, end: (match.index ?? 0) + match[0].length }]; |
| 34 | }); |
| 35 | const last = mentions.at(-1); |
| 36 | if (!last) { |
| 37 | return undefined; |
| 38 | } |
| 39 | |
| 40 | // Only the final skill gets native expansion. Earlier user-only skills need |
| 41 | // a fallback to their raw file because Claude's Skill tool refuses them. |
| 42 | const annotated = new Set([last.name]); |
| 43 | let promptWithInlineSlashes = ""; |
| 44 | let cursor = 0; |
| 45 | for (const mention of mentions) { |
| 46 | promptWithInlineSlashes += `${prompt.slice(cursor, mention.start)}/${mention.name}`; |
| 47 | if (mention.skill.userInvocationOnly === true && !annotated.has(mention.name)) { |
| 48 | promptWithInlineSlashes += ` [The user invoked this skill. Follow the instructions at: ${mention.skill.path}]`; |
| 49 | annotated.add(mention.name); |
| 50 | } |
| 51 | cursor = mention.end; |
| 52 | } |
| 53 | promptWithInlineSlashes = `${promptWithInlineSlashes}${prompt.slice(cursor)}`.trimEnd(); |
| 54 | |
| 55 | return { |
| 56 | leadingText: |
| 57 | prompt.slice(0, last.start).trim().length > 0 ? promptWithInlineSlashes : undefined, |
| 58 | commandText: `/${last.name}${prompt.slice(last.end)}`.trimEnd(), |
| 59 | }; |
| 60 | } |