init_skill.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #!/usr/bin/env python3
  2. """
  3. Skill Initializer - Creates a new skill from template
  4. Usage:
  5. init_skill.py <skill-name> --path <path>
  6. Examples:
  7. init_skill.py my-new-skill --path skills/public
  8. init_skill.py my-api-helper --path skills/private
  9. init_skill.py custom-skill --path /custom/location
  10. """
  11. import sys
  12. from pathlib import Path
  13. SKILL_TEMPLATE = """---
  14. name: {skill_name}
  15. description: [TODO: Complete and informative explanation of what the skill does and when to use it. Include WHEN to use this skill - specific scenarios, file types, or tasks that trigger it.]
  16. ---
  17. # {skill_title}
  18. ## Overview
  19. [TODO: 1-2 sentences explaining what this skill enables]
  20. ## Structuring This Skill
  21. [TODO: Choose the structure that best fits this skill's purpose. Common patterns:
  22. **1. Workflow-Based** (best for sequential processes)
  23. - Works well when there are clear step-by-step procedures
  24. - Example: DOCX skill with "Workflow Decision Tree" → "Reading" → "Creating" → "Editing"
  25. - Structure: ## Overview → ## Workflow Decision Tree → ## Step 1 → ## Step 2...
  26. **2. Task-Based** (best for tool collections)
  27. - Works well when the skill offers different operations/capabilities
  28. - Example: PDF skill with "Quick Start" → "Merge PDFs" → "Split PDFs" → "Extract Text"
  29. - Structure: ## Overview → ## Quick Start → ## Task Category 1 → ## Task Category 2...
  30. **3. Reference/Guidelines** (best for standards or specifications)
  31. - Works well for brand guidelines, coding standards, or requirements
  32. - Example: Brand styling with "Brand Guidelines" → "Colors" → "Typography" → "Features"
  33. - Structure: ## Overview → ## Guidelines → ## Specifications → ## Usage...
  34. **4. Capabilities-Based** (best for integrated systems)
  35. - Works well when the skill provides multiple interrelated features
  36. - Example: Product Management with "Core Capabilities" → numbered capability list
  37. - Structure: ## Overview → ## Core Capabilities → ### 1. Feature → ### 2. Feature...
  38. Patterns can be mixed and matched as needed. Most skills combine patterns (e.g., start with task-based, add workflow for complex operations).
  39. Delete this entire "Structuring This Skill" section when done - it's just guidance.]
  40. ## [TODO: Replace with the first main section based on chosen structure]
  41. [TODO: Add content here. See examples in existing skills:
  42. - Code samples for technical skills
  43. - Decision trees for complex workflows
  44. - Concrete examples with realistic user requests
  45. - References to scripts/templates/references as needed]
  46. ## Resources
  47. This skill includes example resource directories that demonstrate how to organize different types of bundled resources:
  48. ### scripts/
  49. Executable code (Python/Bash/etc.) that can be run directly to perform specific operations.
  50. **Examples from other skills:**
  51. - PDF skill: `fill_fillable_fields.py`, `extract_form_field_info.py` - utilities for PDF manipulation
  52. - DOCX skill: `document.py`, `utilities.py` - Python modules for document processing
  53. **Appropriate for:** Python scripts, shell scripts, or any executable code that performs automation, data processing, or specific operations.
  54. **Note:** Scripts may be executed without loading into context, but can still be read by Claude for patching or environment adjustments.
  55. ### references/
  56. Documentation and reference material intended to be loaded into context to inform Claude's process and thinking.
  57. **Examples from other skills:**
  58. - Product management: `communication.md`, `context_building.md` - detailed workflow guides
  59. - BigQuery: API reference documentation and query examples
  60. - Finance: Schema documentation, company policies
  61. **Appropriate for:** In-depth documentation, API references, database schemas, comprehensive guides, or any detailed information that Claude should reference while working.
  62. ### assets/
  63. Files not intended to be loaded into context, but rather used within the output Claude produces.
  64. **Examples from other skills:**
  65. - Brand styling: PowerPoint template files (.pptx), logo files
  66. - Frontend builder: HTML/React boilerplate project directories
  67. - Typography: Font files (.ttf, .woff2)
  68. **Appropriate for:** Templates, boilerplate code, document templates, images, icons, fonts, or any files meant to be copied or used in the final output.
  69. ---
  70. **Any unneeded directories can be deleted.** Not every skill requires all three types of resources.
  71. """
  72. EXAMPLE_SCRIPT = '''#!/usr/bin/env python3
  73. """
  74. Example helper script for {skill_name}
  75. This is a placeholder script that can be executed directly.
  76. Replace with actual implementation or delete if not needed.
  77. Example real scripts from other skills:
  78. - pdf/scripts/fill_fillable_fields.py - Fills PDF form fields
  79. - pdf/scripts/convert_pdf_to_images.py - Converts PDF pages to images
  80. """
  81. def main():
  82. print("This is an example script for {skill_name}")
  83. # TODO: Add actual script logic here
  84. # This could be data processing, file conversion, API calls, etc.
  85. if __name__ == "__main__":
  86. main()
  87. '''
  88. EXAMPLE_REFERENCE = """# Reference Documentation for {skill_title}
  89. This is a placeholder for detailed reference documentation.
  90. Replace with actual reference content or delete if not needed.
  91. Example real reference docs from other skills:
  92. - product-management/references/communication.md - Comprehensive guide for status updates
  93. - product-management/references/context_building.md - Deep-dive on gathering context
  94. - bigquery/references/ - API references and query examples
  95. ## When Reference Docs Are Useful
  96. Reference docs are ideal for:
  97. - Comprehensive API documentation
  98. - Detailed workflow guides
  99. - Complex multi-step processes
  100. - Information too lengthy for main SKILL.md
  101. - Content that's only needed for specific use cases
  102. ## Structure Suggestions
  103. ### API Reference Example
  104. - Overview
  105. - Authentication
  106. - Endpoints with examples
  107. - Error codes
  108. - Rate limits
  109. ### Workflow Guide Example
  110. - Prerequisites
  111. - Step-by-step instructions
  112. - Common patterns
  113. - Troubleshooting
  114. - Best practices
  115. """
  116. EXAMPLE_ASSET = """# Example Asset File
  117. This placeholder represents where asset files would be stored.
  118. Replace with actual asset files (templates, images, fonts, etc.) or delete if not needed.
  119. Asset files are NOT intended to be loaded into context, but rather used within
  120. the output Claude produces.
  121. Example asset files from other skills:
  122. - Brand guidelines: logo.png, slides_template.pptx
  123. - Frontend builder: hello-world/ directory with HTML/React boilerplate
  124. - Typography: custom-font.ttf, font-family.woff2
  125. - Data: sample_data.csv, test_dataset.json
  126. ## Common Asset Types
  127. - Templates: .pptx, .docx, boilerplate directories
  128. - Images: .png, .jpg, .svg, .gif
  129. - Fonts: .ttf, .otf, .woff, .woff2
  130. - Boilerplate code: Project directories, starter files
  131. - Icons: .ico, .svg
  132. - Data files: .csv, .json, .xml, .yaml
  133. Note: This is a text placeholder. Actual assets can be any file type.
  134. """
  135. def title_case_skill_name(skill_name):
  136. """Convert hyphenated skill name to Title Case for display."""
  137. return ' '.join(word.capitalize() for word in skill_name.split('-'))
  138. def init_skill(skill_name, path):
  139. """
  140. Initialize a new skill directory with template SKILL.md.
  141. Args:
  142. skill_name: Name of the skill
  143. path: Path where the skill directory should be created
  144. Returns:
  145. Path to created skill directory, or None if error
  146. """
  147. # Determine skill directory path
  148. skill_dir = Path(path).resolve() / skill_name
  149. # Check if directory already exists
  150. if skill_dir.exists():
  151. print(f"❌ Error: Skill directory already exists: {skill_dir}")
  152. return None
  153. # Create skill directory
  154. try:
  155. skill_dir.mkdir(parents=True, exist_ok=False)
  156. print(f"✅ Created skill directory: {skill_dir}")
  157. except Exception as e:
  158. print(f"❌ Error creating directory: {e}")
  159. return None
  160. # Create SKILL.md from template
  161. skill_title = title_case_skill_name(skill_name)
  162. skill_content = SKILL_TEMPLATE.format(
  163. skill_name=skill_name,
  164. skill_title=skill_title
  165. )
  166. skill_md_path = skill_dir / 'SKILL.md'
  167. try:
  168. skill_md_path.write_text(skill_content)
  169. print("✅ Created SKILL.md")
  170. except Exception as e:
  171. print(f"❌ Error creating SKILL.md: {e}")
  172. return None
  173. # Create resource directories with example files
  174. try:
  175. # Create scripts/ directory with example script
  176. scripts_dir = skill_dir / 'scripts'
  177. scripts_dir.mkdir(exist_ok=True)
  178. example_script = scripts_dir / 'example.py'
  179. example_script.write_text(EXAMPLE_SCRIPT.format(skill_name=skill_name))
  180. example_script.chmod(0o755)
  181. print("✅ Created scripts/example.py")
  182. # Create references/ directory with example reference doc
  183. references_dir = skill_dir / 'references'
  184. references_dir.mkdir(exist_ok=True)
  185. example_reference = references_dir / 'api_reference.md'
  186. example_reference.write_text(EXAMPLE_REFERENCE.format(skill_title=skill_title))
  187. print("✅ Created references/api_reference.md")
  188. # Create assets/ directory with example asset placeholder
  189. assets_dir = skill_dir / 'assets'
  190. assets_dir.mkdir(exist_ok=True)
  191. example_asset = assets_dir / 'example_asset.txt'
  192. example_asset.write_text(EXAMPLE_ASSET)
  193. print("✅ Created assets/example_asset.txt")
  194. except Exception as e:
  195. print(f"❌ Error creating resource directories: {e}")
  196. return None
  197. # Print next steps
  198. print(f"\n✅ Skill '{skill_name}' initialized successfully at {skill_dir}")
  199. print("\nNext steps:")
  200. print("1. Edit SKILL.md to complete the TODO items and update the description")
  201. print("2. Customize or delete the example files in scripts/, references/, and assets/")
  202. print("3. Run the validator when ready to check the skill structure")
  203. return skill_dir
  204. def main():
  205. if len(sys.argv) < 4 or sys.argv[2] != '--path':
  206. print("Usage: init_skill.py <skill-name> --path <path>")
  207. print("\nSkill name requirements:")
  208. print(" - Hyphen-case identifier (e.g., 'data-analyzer')")
  209. print(" - Lowercase letters, digits, and hyphens only")
  210. print(" - Max 40 characters")
  211. print(" - Must match directory name exactly")
  212. print("\nExamples:")
  213. print(" init_skill.py my-new-skill --path skills/public")
  214. print(" init_skill.py my-api-helper --path skills/private")
  215. print(" init_skill.py custom-skill --path /custom/location")
  216. sys.exit(1)
  217. skill_name = sys.argv[1]
  218. path = sys.argv[3]
  219. print(f"🚀 Initializing skill: {skill_name}")
  220. print(f" Location: {path}")
  221. print()
  222. result = init_skill(skill_name, path)
  223. if result:
  224. sys.exit(0)
  225. else:
  226. sys.exit(1)
  227. if __name__ == "__main__":
  228. main()