UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

147 lines • 6.99 kB
/** Format the user-attributed payload for one or more explicit skill invocations. */ export function formatSkillInvocationPrompt(skills, userRequest) { const skillBlocks = skills.map((skill) => `The user explicitly invoked the "${skill.name}" skill. Follow the instructions in <skill-instruction> as binding for this request, while respecting higher-priority instructions.\n\n<skill-instruction name="${skill.name}" location="${skill.filePath}">\nReferences are relative to ${skill.baseDir}.\n\n${skill.body}\n</skill-instruction>`); const expandedSkills = skillBlocks.join("\n\n"); return userRequest && /\S/.test(userRequest) ? `${expandedSkills}\n\n<user-request>\n${userRequest}\n</user-request>` : expandedSkills; } const SKILL_INSTRUCTION_PATTERN = /^The user explicitly invoked the "([^"]+)" skill\. Follow the instructions in <skill-instruction> as binding for this request, while respecting higher-priority instructions\.\n\n<skill-instruction name="([^"]+)" location="([^"]+)">\n([\s\S]*?)\n<\/skill-instruction>/; function matchSkillInstruction(text) { const match = text.match(SKILL_INSTRUCTION_PATTERN); if (!match || match[1] !== match[2]) return null; return { skill: { name: match[1], location: match[3], content: match[4] }, length: match[0].length }; } function toParsedSkillBlock(skills, userMessage) { const first = skills[0]; if (!first) return null; return { ...first, skills, userMessage }; } /** * Parse every chained skill block from message text. * Returns null if the text doesn't contain a skill block. */ export function parseSkillBlock(text) { const first = matchSkillInstruction(text); if (first) { const skills = [first.skill]; let remainder = text.slice(first.length); while (remainder.startsWith("\n\nThe user explicitly invoked the ")) { const chained = matchSkillInstruction(remainder.slice(2)); if (!chained) return null; skills.push(chained.skill); remainder = remainder.slice(chained.length + 2); } const requestMatch = remainder.match(/^\n\n<user-request>\n([\s\S]*?)\n<\/user-request>$/); if (remainder && !requestMatch) return null; return toParsedSkillBlock(skills, requestMatch?.[1].trim() || undefined); } const legacyMatch = text.match(/^<skill name="([^"]+)" location="([^"]+)">\n([\s\S]*?)\n<\/skill>(?:\n\n([\s\S]+))?$/); if (!legacyMatch) return null; return toParsedSkillBlock([{ name: legacyMatch[1], location: legacyMatch[2], content: legacyMatch[3] }], legacyMatch[4]?.trim() || undefined); } const SKILL_NAMESPACE = "skill:"; const LEADING_SKILL_INVOCATION_PATTERN = /^(?:\/skill:([a-zA-Z][a-zA-Z0-9:_-]*)|\$([a-zA-Z][a-zA-Z0-9:_-]*))(?=\s|$)/; const INLINE_DOLLAR_SKILL_INVOCATION_PATTERN = /(^|\s)\$([a-zA-Z][a-zA-Z0-9:_-]*)(?=\s|$)/g; function inlineSkillName(token, knownSkillNames) { if (token.startsWith(SKILL_NAMESPACE)) return token.slice(SKILL_NAMESPACE.length) || null; return knownSkillNames?.has(token) ? token : null; } /** * Find explicit skill invocation tokens without treating ordinary inline dollar * prose (for example `$HOME`) as executable. * * Leading runs accept `/skill:name`, `$name`, and `$skill:name`. Outside the * leading run the explicit `$skill:name` token is always executable and a bare * `$name` is executable when it names a loaded skill. */ export function parseSkillInvocationTokens(text, options = {}) { const tokens = []; let cursor = 0; while (cursor < text.length) { while (cursor < text.length && /\s/.test(text[cursor])) cursor++; const match = text.slice(cursor).match(LEADING_SKILL_INVOCATION_PATTERN); if (!match) break; const syntax = match[1] ? "slash" : "dollar"; const dollarName = match[2]; const name = match[1] ?? (dollarName?.startsWith("skill:") ? dollarName.slice("skill:".length) : dollarName); if (!name) break; tokens.push({ name, syntax, start: cursor, end: cursor + match[0].length, position: "leading", }); cursor += match[0].length; } INLINE_DOLLAR_SKILL_INVOCATION_PATTERN.lastIndex = cursor; for (const match of text.matchAll(INLINE_DOLLAR_SKILL_INVOCATION_PATTERN)) { const token = match[2] ?? ""; const name = inlineSkillName(token, options.knownSkillNames); if (name === null) continue; const start = (match.index ?? 0) + (match[1] ?? "").length; tokens.push({ name, syntax: "dollar", start, end: start + token.length + 1, position: "inline", }); } return tokens; } function stripLeadingInvocationSeparators(text) { let cursor = 0; while (text[cursor] === " " || text[cursor] === "\t") cursor++; while (text[cursor] === "\n" || (text[cursor] === "\r" && text[cursor + 1] === "\n")) { cursor += text[cursor] === "\r" ? 2 : 1; const lineStart = cursor; while (text[cursor] === " " || text[cursor] === "\t") cursor++; if (text[cursor] !== "\n" && !(text[cursor] === "\r" && text[cursor + 1] === "\n")) { return text.slice(lineStart); } } return text.slice(cursor); } /** Remove expanded invocation tokens, leaving `[skill: name]` where an inline one stood. */ export function removeSkillInvocationTokens(text, tokens) { let cursor = 0; let result = ""; for (const token of tokens) { result += text.slice(cursor, token.start); if (token.position === "inline") result += `[skill: ${token.name}]`; cursor = token.end; if (token.position === "inline" && (result.endsWith(" ") || result.endsWith("\t")) && (text[cursor] === " " || text[cursor] === "\t")) { cursor++; } } result += text.slice(cursor); return tokens.some((token) => token.position === "leading") ? stripLeadingInvocationSeparators(result) : result; } /** * Skill-body characters one prompt inlines. Every invoked skill still loads: once the inlined bodies * would pass this budget, a skill is expanded as a reference to its file instead (the first skill is * always inlined), so composing many skills never drops one and never grows the prompt without bound. */ export const SKILL_INLINE_BUDGET_CHARS = 128 * 1024; /** Body of a skill block whose instructions stay in the skill file, for the model to read before acting. */ export function formatSkillReferenceBody(filePath) { return `This skill's instructions are not inlined here, to keep the prompt within the skill size budget. Read ${filePath} with the read tool before acting on the request, and follow it.`; } //# sourceMappingURL=skill-invocation.js.map