source dump of claude code
at main 197 lines 9.5 kB view raw
1import { getSessionMemoryContent } from '../../services/SessionMemory/sessionMemoryUtils.js' 2import type { Message } from '../../types/message.js' 3import { getMessagesAfterCompactBoundary } from '../../utils/messages.js' 4import { registerBundledSkill } from '../bundledSkills.js' 5 6function extractUserMessages(messages: Message[]): string[] { 7 return messages 8 .filter((m): m is Extract<typeof m, { type: 'user' }> => m.type === 'user') 9 .map(m => { 10 const content = m.message.content 11 if (typeof content === 'string') return content 12 return content 13 .filter( 14 (b): b is Extract<typeof b, { type: 'text' }> => b.type === 'text', 15 ) 16 .map(b => b.text) 17 .join('\n') 18 }) 19 .filter(text => text.trim().length > 0) 20} 21 22const SKILLIFY_PROMPT = `# Skillify {{userDescriptionBlock}} 23 24You are capturing this session's repeatable process as a reusable skill. 25 26## Your Session Context 27 28Here is the session memory summary: 29<session_memory> 30{{sessionMemory}} 31</session_memory> 32 33Here are the user's messages during this session. Pay attention to how they steered the process, to help capture their detailed preferences in the skill: 34<user_messages> 35{{userMessages}} 36</user_messages> 37 38## Your Task 39 40### Step 1: Analyze the Session 41 42Before asking any questions, analyze the session to identify: 43- What repeatable process was performed 44- What the inputs/parameters were 45- The distinct steps (in order) 46- The success artifacts/criteria (e.g. not just "writing code," but "an open PR with CI fully passing") for each step 47- Where the user corrected or steered you 48- What tools and permissions were needed 49- What agents were used 50- What the goals and success artifacts were 51 52### Step 2: Interview the User 53 54You will use the AskUserQuestion to understand what the user wants to automate. Important notes: 55- Use AskUserQuestion for ALL questions! Never ask questions via plain text. 56- For each round, iterate as much as needed until the user is happy. 57- The user always has a freeform "Other" option to type edits or feedback -- do NOT add your own "Needs tweaking" or "I'll provide edits" option. Just offer the substantive choices. 58 59**Round 1: High level confirmation** 60- Suggest a name and description for the skill based on your analysis. Ask the user to confirm or rename. 61- Suggest high-level goal(s) and specific success criteria for the skill. 62 63**Round 2: More details** 64- Present the high-level steps you identified as a numbered list. Tell the user you will dig into the detail in the next round. 65- If you think the skill will require arguments, suggest arguments based on what you observed. Make sure you understand what someone would need to provide. 66- If it's not clear, ask if this skill should run inline (in the current conversation) or forked (as a sub-agent with its own context). Forked is better for self-contained tasks that don't need mid-process user input; inline is better when the user wants to steer mid-process. 67- Ask where the skill should be saved. Suggest a default based on context (repo-specific workflows → repo, cross-repo personal workflows → user). Options: 68 - **This repo** (\`.claude/skills/<name>/SKILL.md\`) — for workflows specific to this project 69 - **Personal** (\`~/.claude/skills/<name>/SKILL.md\`) — follows you across all repos 70 71**Round 3: Breaking down each step** 72For each major step, if it's not glaringly obvious, ask: 73- What does this step produce that later steps need? (data, artifacts, IDs) 74- What proves that this step succeeded, and that we can move on? 75- Should the user be asked to confirm before proceeding? (especially for irreversible actions like merging, sending messages, or destructive operations) 76- Are any steps independent and could run in parallel? (e.g., posting to Slack and monitoring CI at the same time) 77- How should the skill be executed? (e.g. always use a Task agent to conduct code review, or invoke an agent team for a set of concurrent steps) 78- What are the hard constraints or hard preferences? Things that must or must not happen? 79 80You may do multiple rounds of AskUserQuestion here, one round per step, especially if there are more than 3 steps or many clarification questions. Iterate as much as needed. 81 82IMPORTANT: Pay special attention to places where the user corrected you during the session, to help inform your design. 83 84**Round 4: Final questions** 85- Confirm when this skill should be invoked, and suggest/confirm trigger phrases too. (e.g. For a cherrypick workflow you could say: Use when the user wants to cherry-pick a PR to a release branch. Examples: 'cherry-pick to release', 'CP this PR', 'hotfix.') 86- You can also ask for any other gotchas or things to watch out for, if it's still unclear. 87 88Stop interviewing once you have enough information. IMPORTANT: Don't over-ask for simple processes! 89 90### Step 3: Write the SKILL.md 91 92Create the skill directory and file at the location the user chose in Round 2. 93 94Use this format: 95 96\`\`\`markdown 97--- 98name: {{skill-name}} 99description: {{one-line description}} 100allowed-tools: 101 {{list of tool permission patterns observed during session}} 102when_to_use: {{detailed description of when Claude should automatically invoke this skill, including trigger phrases and example user messages}} 103argument-hint: "{{hint showing argument placeholders}}" 104arguments: 105 {{list of argument names}} 106context: {{inline or fork -- omit for inline}} 107--- 108 109# {{Skill Title}} 110Description of skill 111 112## Inputs 113- \`$arg_name\`: Description of this input 114 115## Goal 116Clearly stated goal for this workflow. Best if you have clearly defined artifacts or criteria for completion. 117 118## Steps 119 120### 1. Step Name 121What to do in this step. Be specific and actionable. Include commands when appropriate. 122 123**Success criteria**: ALWAYS include this! This shows that the step is done and we can move on. Can be a list. 124 125IMPORTANT: see the next section below for the per-step annotations you can optionally include for each step. 126 127... 128\`\`\` 129 130**Per-step annotations**: 131- **Success criteria** is REQUIRED on every step. This helps the model understand what the user expects from their workflow, and when it should have the confidence to move on. 132- **Execution**: \`Direct\` (default), \`Task agent\` (straightforward subagents), \`Teammate\` (agent with true parallelism and inter-agent communication), or \`[human]\` (user does it). Only needs specifying if not Direct. 133- **Artifacts**: Data this step produces that later steps need (e.g., PR number, commit SHA). Only include if later steps depend on it. 134- **Human checkpoint**: When to pause and ask the user before proceeding. Include for irreversible actions (merging, sending messages), error judgment (merge conflicts), or output review. 135- **Rules**: Hard rules for the workflow. User corrections during the reference session can be especially useful here. 136 137**Step structure tips:** 138- Steps that can run concurrently use sub-numbers: 3a, 3b 139- Steps requiring the user to act get \`[human]\` in the title 140- Keep simple skills simple -- a 2-step skill doesn't need annotations on every step 141 142**Frontmatter rules:** 143- \`allowed-tools\`: Minimum permissions needed (use patterns like \`Bash(gh:*)\` not \`Bash\`) 144- \`context\`: Only set \`context: fork\` for self-contained skills that don't need mid-process user input. 145- \`when_to_use\` is CRITICAL -- tells the model when to auto-invoke. Start with "Use when..." and include trigger phrases. Example: "Use when the user wants to cherry-pick a PR to a release branch. Examples: 'cherry-pick to release', 'CP this PR', 'hotfix'." 146- \`arguments\` and \`argument-hint\`: Only include if the skill takes parameters. Use \`$name\` in the body for substitution. 147 148### Step 4: Confirm and Save 149 150Before writing the file, output the complete SKILL.md content as a yaml code block in your response so the user can review it with proper syntax highlighting. Then ask for confirmation using AskUserQuestion with a simple question like "Does this SKILL.md look good to save?" — do NOT use the body field, keep the question concise. 151 152After writing, tell the user: 153- Where the skill was saved 154- How to invoke it: \`/{{skill-name}} [arguments]\` 155- That they can edit the SKILL.md directly to refine it 156` 157 158export function registerSkillifySkill(): void { 159 if (process.env.USER_TYPE !== 'ant') { 160 return 161 } 162 163 registerBundledSkill({ 164 name: 'skillify', 165 description: 166 "Capture this session's repeatable process into a skill. Call at end of the process you want to capture with an optional description.", 167 allowedTools: [ 168 'Read', 169 'Write', 170 'Edit', 171 'Glob', 172 'Grep', 173 'AskUserQuestion', 174 'Bash(mkdir:*)', 175 ], 176 userInvocable: true, 177 disableModelInvocation: true, 178 argumentHint: '[description of the process you want to capture]', 179 async getPromptForCommand(args, context) { 180 const sessionMemory = 181 (await getSessionMemoryContent()) ?? 'No session memory available.' 182 const userMessages = extractUserMessages( 183 getMessagesAfterCompactBoundary(context.messages), 184 ) 185 186 const userDescriptionBlock = args 187 ? `The user described this process as: "${args}"` 188 : '' 189 190 const prompt = SKILLIFY_PROMPT.replace('{{sessionMemory}}', sessionMemory) 191 .replace('{{userMessages}}', userMessages.join('\n\n---\n\n')) 192 .replace('{{userDescriptionBlock}}', userDescriptionBlock) 193 194 return [{ type: 'text', text: prompt }] 195 }, 196 }) 197}