mirror of
https://github.com/hanzoai/dev.git
synced 2026-11-21 09:59:11 +00:00
61 lines
2.5 KiB
TypeScript
61 lines
2.5 KiB
TypeScript
import type { ServerProviderSkill } from "@hanzodev/contracts";
|
|
|
|
/**
|
|
* Claude expands one skill per message only when `/name` starts the text block.
|
|
* Everything after the name, including newlines or a second `/x`, becomes `ARGUMENTS`.
|
|
* https://github.com/anthropics/claude-code/issues/87113
|
|
*/
|
|
|
|
/** Match packages/shared/src/composerInlineTokens.ts so displayed chips and dispatched skills accept the same tokens. */
|
|
const SKILL_MENTION_PATTERN =
|
|
/(^|\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;
|
|
|
|
export type ClaudeSkillTarget = Pick<ServerProviderSkill, "path" | "userInvocationOnly">;
|
|
|
|
export interface ClaudeSkillDispatch {
|
|
/** The whole prompt with mentions as `/name`, or `undefined` when the mention opens it. */
|
|
readonly leadingText: string | undefined;
|
|
/** `/name` plus the trailing text, ready to be the message's last text block. */
|
|
readonly commandText: string;
|
|
}
|
|
|
|
export function planClaudeSkillDispatch(
|
|
prompt: string,
|
|
skills: ReadonlyMap<string, ClaudeSkillTarget>,
|
|
/** Mentions before this offset, such as inline handoff history, stay plain text. */
|
|
mentionsFrom = 0,
|
|
): ClaudeSkillDispatch | undefined {
|
|
const mentions = [...prompt.matchAll(SKILL_MENTION_PATTERN)].flatMap((match) => {
|
|
const name = match[2] ?? "";
|
|
const skill = skills.get(name);
|
|
const start = (match.index ?? 0) + (match[1]?.length ?? 0);
|
|
if (!skill || start < mentionsFrom) return [];
|
|
return [{ name, skill, start, end: (match.index ?? 0) + match[0].length }];
|
|
});
|
|
const last = mentions.at(-1);
|
|
if (!last) {
|
|
return undefined;
|
|
}
|
|
|
|
// Only the final skill gets native expansion. Earlier user-only skills need
|
|
// a fallback to their raw file because Claude's Skill tool refuses them.
|
|
const annotated = new Set([last.name]);
|
|
let promptWithInlineSlashes = "";
|
|
let cursor = 0;
|
|
for (const mention of mentions) {
|
|
promptWithInlineSlashes += `${prompt.slice(cursor, mention.start)}/${mention.name}`;
|
|
if (mention.skill.userInvocationOnly === true && !annotated.has(mention.name)) {
|
|
promptWithInlineSlashes += ` [The user invoked this skill. Follow the instructions at: ${mention.skill.path}]`;
|
|
annotated.add(mention.name);
|
|
}
|
|
cursor = mention.end;
|
|
}
|
|
promptWithInlineSlashes = `${promptWithInlineSlashes}${prompt.slice(cursor)}`.trimEnd();
|
|
|
|
return {
|
|
leadingText:
|
|
prompt.slice(0, last.start).trim().length > 0 ? promptWithInlineSlashes : undefined,
|
|
commandText: `/${last.name}${prompt.slice(last.end)}`.trimEnd(),
|
|
};
|
|
}
|