| name | template-extraction |
| description | Methodology for extracting reusable code templates from open-source reference projects. Use when (1) analyzing a reference project for extractable modules, (2) creating .template files from source code, (3) migrating modules into the project, (4) planning template extraction sprints. Keywords โ "extract template", "create template", "reference project", "module extraction", "migrate module" |
Template Extraction Methodology
Core Principle
่ฝ็ดๆฅ็จ็ปๅฏนไธ่ชๅทฑๅ โ If it exists in open-source, extract it as a template. Never write from scratch.
1. The 5-Level Thinking Framework
Different operations naturally map to different granularity levels. Module is the center of gravity:
Level 1: ้กน็ฎ Project โ SELECT sources (which project is worth studying?)
Level 2: ๅฑ Layer โ CLASSIFY placement (backend / frontend / agent / devops)
Level 3: ๆจกๅ Module โ DEFINE boundaries โ
CORE LEVEL โ
Level 4: ๆไปถ File โ EXTRACT templates (each .template file)
Level 5: ็ๆ Generate โ ADAPT to project (AI generates final code from template)
Why Module is the Best Granularity
| Level | Too Coarse? | Too Fine? | Module is Just Right |
|---|
| Project | Can't reuse directly โ architecture differs | โ | โ |
| Layer | Each layer contains very different things | โ | โ |
| Module | โ | โ | โ
Self-contained, clear boundaries, portable |
| File | โ | Loses context, missing dependencies | โ |
Operation-to-Level Mapping
| Operation | Best Level | What It Means |
|---|
| ๅ่ Reference | ๐๏ธ Project | Study architecture decisions and design philosophy |
| ๆจกๆฟ Template | ๐ File | Atomic unit of reuse โ one file with {{placeholders}} |
| ่ฟ็งป Migrate | ๐ฆ Module | Move a cohesive feature unit (e.g., entire auth module) |
| ๅๅปบ Create | ๐งฑ Layer | Build project skeleton layer by layer |
| ็ๆ Generate | ๐ File | AI fills in file details using module context |
The Dependency Chain
Reference(Project) โ Template(File) โ Migrate(Module) โ Create(Layer) โ Generate(File)
โ โ โ โ โ
Study Distill Transport Scaffold Fill details
2. Module Card โ Standard Format
Every extractable module MUST be documented as a Module Card. This is the atomic unit of the template system.
Module Card Template
## ๐ฆ Module: {{MODULE_NAME}}
**Source**: `{{REFERENCE_PROJECT}}/{{SOURCE_PATH}}/`
**Target**: `.agent/templates/{{TARGET_PATH}}/`
**Layer**: {{LAYER}} (backend / frontend / agent / orchestration / devops)
**Priority**: {{PRIORITY}} (๐ด Critical / ๐ High / ๐ก Medium / ๐ต Low)
### Description
{{One paragraph describing what this module does and why it's worth extracting}}
### File Manifest
| # | Source File | Template Output | Role |
|---|------------|-----------------|------|
| 1 | `source/component.tsx` | `target/component.tsx.template` | Main component |
| 2 | `source/types.ts` | `target/types.ts.template` | Type definitions |
| 3 | `source/hook.ts` | `target/hook.ts.template` | Business logic |
### Dependencies
- **npm**: `react-markdown`, `@radix-ui/popover`
- **internal**: `shared/components/ui/popover`
### Adaptation Notes
- Replace `{{API_BASE_URL}}` with project API endpoint
- Replace `{{ALIAS}}` with project path alias (`@/`)
- i18n: Wrap user-facing strings with `t()`
### Quality Checklist
- [ ] All files have `@source` annotation in header
- [ ] All `{{placeholder}}` variables documented
- [ ] Dependencies listed in package.json / requirements.txt
- [ ] Usage example provided
- [ ] Registered in `docs/templates/README.md`
3. Extraction Workflow (Step-by-Step)
Phase 1: Project-Level Analysis (ๅ่)
Input: A reference project in `.github/references/`
Output: List of extractable modules with priority ranking
Steps:
- Scan project structure โ
list_dir the src/ or app/ directory
- Identify high-value modules โ Look for:
- Self-contained feature directories
- Reusable utility libraries
- Patterns not yet in our template system
- Score each module using the Value Matrix:
| Factor | Weight | Question |
|---|
| Reusability | 30% | Can this be used in 3+ projects? |
| Complexity | 25% | Would this take >2h to write from scratch? |
| Quality | 25% | Is this production-grade code? |
| Relevance | 20% | Does our project need this? |
- Output: Module Cards for top-ranked modules
Phase 2: Module-Level Extraction (ๆจกๆฟ)
Input: A Module Card
Output: .template files in `.agent/templates/`
Steps:
- Read all source files in the module
- Identify hardcoded values โ Replace with
{{PLACEHOLDER}}
- Remove project-specific logic โ Keep only generic pattern
- Add file header annotations:
For Python:
"""
{{ModuleName}} - {{Description}}
@module {{module_path}}
@source {{reference_project}}/{{source_file_path}}
@reference {{github_url}}
@template {{template_id}}
"""
For TypeScript/TSX:
- Write
.template file to .agent/templates/{{target_path}}/
- Update documentation in
docs/templates/0X-*.md
Phase 3: Module-Level Migration (่ฟ็งป)
Input: .template files for a module
Output: Working code in the project
Steps:
- Copy all template files in the module to target directory
- Remove
.template extension
- Replace all
{{PLACEHOLDER}} values with project-specific values
- Install dependencies (
npm install / pip install)
- Update imports โ Adjust path aliases
- Add i18n โ Wrap user-facing strings
- Update
@template tag in each file to point to source template
- Verify build โ Run
npm run build / pytest
Phase 4: Layer-Level Creation (ๅๅปบ)
Input: A new layer or feature to build
Output: Complete feature skeleton
Steps:
- Identify which modules are needed for this layer
- Migrate modules in dependency order (types โ hooks โ components โ views)
- Wire up routing โ Add to
app.tsx or main.py
- Connect to existing infrastructure โ Auth, API client, stores
Phase 5: File-Level Generation (็ๆ)
Input: A template file + project context
Output: Final adapted code file
Steps:
- Read the template and understand its pattern
- Analyze project context โ Existing types, API endpoints, store shape
- Generate adapted code โ Fill in all placeholders, adjust types, add tests
- Add proper file header with
@template and @reference tags
4. Standard Placeholders
Use these standardized placeholder names consistently across all templates:
| Placeholder | Description | Example |
|---|
{{ModuleName}} | PascalCase module name | ChatCitation |
{{module_name}} | snake_case module name | chat_citation |
{{moduleName}} | camelCase module name | chatCitation |
{{FeatureName}} | PascalCase feature name | Document |
{{feature_name}} | snake_case feature name | document |
{{ALIAS}} | Import path alias | @ or @/ |
{{API_BASE_URL}} | API base URL | /api/v1 |
{{APP_NAME}} | Application name | Ottawa GenAI |
{{TABLE_NAME}} | Database table name | chat_sessions |
{{ROUTE_PREFIX}} | API route prefix | /chat |
5. Template File Naming Convention
.agent/templates/
โโโ {layer}/ # Layer directory
โ โโโ {module}/ # Module directory (if multi-file)
โ โ โโโ component.tsx.template # Individual template file
โ โ โโโ types.ts.template
โ โ โโโ hook.ts.template
โ โโโ single-file.py.template # Single-file module
Rules:
- Extension: always
.template appended to the real extension
- Naming: match the target filename (e.g.,
chat-input.tsx.template)
- Organization: mirror the project's directory structure
6. Documentation Registry
Every template must be registered in two places:
6.1 Detail Doc (docs/templates/0X-*.md)
Each layer has its own detail document:
01-backend-templates.md
02-frontend-templates.md
03-ai-agent-templates.md
- etc.
The detail doc contains:
- Full code snippets for each template
- Source file links
- Key features and usage notes
6.2 Master Index (docs/templates/README.md)
The master index contains:
- Directory tree overview
- Source traceability table (template โ reference project)
- Status summary (extracted count / pending count)
7. Decision Tree: When to Extract vs. Skip
Is this module available in a reference project?
โโโ YES โ Is it self-contained (< 10 files, clear boundaries)?
โ โโโ YES โ Is it generic enough to reuse in 3+ projects?
โ โ โโโ YES โ โ
EXTRACT as template
โ โ โโโ NO โ ๐ง EXTRACT but mark as "project-specific adaptation needed"
โ โโโ NO โ Is the valuable part isolatable?
โ โโโ YES โ โ
EXTRACT the valuable subset
โ โโโ NO โ โธ๏ธ SKIP โ use as reference only, document patterns in ARCHITECTURE_ANALYSIS.md
โโโ NO โ Is there a well-known npm/pip package?
โโโ YES โ ๐ฆ USE the package directly (e.g., `npx shadcn add`)
โโโ NO โ โ๏ธ WRITE from scratch (last resort)
8. Quick Reference: Module Extraction Checklist
When extracting a module, verify each step: