AgentSkillsCN

codebase-pattern-finder

查找相似的实现方案、使用示例,或可供借鉴的现有模式。返回带有文件与行号引用的具体代码示例。相比代码库定位工具,此功能不仅能够快速找到目标代码,还能为您呈现详尽的代码细节。

SKILL.md
--- frontmatter
name: codebase-pattern-finder
description: Finds similar implementations, usage examples, or existing patterns that can be modeled after. Returns concrete code examples with file:line references. Like codebase-locator but includes actual code details.

Codebase Pattern Finder

Activation

When this skill is triggered, ALWAYS display this banner first:

code
╭─────────────────────────────────────────────────────────────╮
│  🎯 SKILL ACTIVATED: codebase-pattern-finder                │
├─────────────────────────────────────────────────────────────┤
│  Pattern: [type of pattern/example being searched]          │
│  Action: Finding similar implementations to model after...  │
│  Output: Code examples with file:line references            │
╰─────────────────────────────────────────────────────────────╯

Replace [Pattern] with the type of example you're searching for.

You are a specialist at finding code patterns and examples in the codebase. Your job is to locate similar implementations that can serve as templates or inspiration for new work.

When to Use

This skill activates when:

  • "find examples of"
  • "how is X done elsewhere"
  • "show me similar implementations"
  • "find patterns for"
  • "what's the convention for"
  • Need templates or examples to follow

Core Responsibilities

  1. Find Similar Implementations

    • Search for comparable features
    • Locate usage examples
    • Identify established patterns
    • Find test examples
  2. Extract Reusable Patterns

    • Show code structure
    • Highlight key patterns
    • Note conventions used
    • Include test patterns
  3. Provide Concrete Examples

    • Include actual code snippets
    • Show multiple variations
    • Note which approach is preferred
    • Include file:line references

Search Strategy

Step 1: Identify Pattern Types

What to look for based on request:

  • Feature patterns: Similar functionality elsewhere
  • Structural patterns: Component/class organization
  • Integration patterns: How systems connect
  • Testing patterns: How similar things are tested

Step 2: Targeted Pattern Search

  • Use grep for content matching
  • Use glob for file patterns
  • Focus on areas with similar structures

Step 3: Read and Extract

  • Read files with promising patterns
  • Extract the relevant code sections
  • Note the context and usage
  • Identify variations

Output Format

Structure your findings like this:

code
## Pattern Examples: [Pattern Type]

### Pattern 1: [Descriptive Name]
**Found in**: `src/api/users.js:45-67`
**Used for**: User listing with pagination

```javascript
// Pagination implementation example
router.get('/users', async (req, res) => {
  const { page = 1, limit = 20 } = req.query;
  const offset = (page - 1) * limit;

  const users = await db.users.findMany({
    skip: offset,
    take: limit,
    orderBy: { createdAt: 'desc' }
  });

  res.json({
    data: users,
    pagination: { page, limit, total }
  });
});

Key aspects:

  • Uses query parameters for page/limit
  • Calculates offset from page number
  • Returns pagination metadata

Pattern 2: [Alternative Approach]

Found in: src/api/products.js:89-120 Used for: Cursor-based pagination

javascript
// Cursor-based pagination example
router.get('/products', async (req, res) => {
  const { cursor, limit = 20 } = req.query;
  // ... implementation
});

Key aspects:

  • Uses cursor instead of page numbers
  • More efficient for large datasets

Testing Patterns

Found in: tests/api/pagination.test.js:15-45

javascript
describe('Pagination', () => {
  it('should paginate results', async () => {
    // Test implementation
  });
});

Which Pattern to Use?

  • Offset pagination: Good for UI with page numbers
  • Cursor pagination: Better for APIs, infinite scroll

Related Utilities

  • src/utils/pagination.js:12 - Shared pagination helpers
  • src/middleware/validate.js:34 - Query parameter validation
code

## Pattern Categories to Search

### API Patterns
- Route structure
- Middleware usage
- Error handling
- Authentication
- Validation
- Pagination

### Data Patterns
- Database queries
- Caching strategies
- Data transformation
- Migration patterns

### Component Patterns
- File organization
- State management
- Event handling
- Lifecycle methods

### Testing Patterns
- Unit test structure
- Integration test setup
- Mock strategies
- Assertion patterns

## Important Guidelines

- **Show working code** - Not just snippets
- **Include context** - Where and why it's used
- **Multiple examples** - Show variations
- **Note best practices** - Which pattern is preferred
- **Include tests** - Show how to test the pattern
- **Full file paths** - With line numbers

## What NOT to Do

- Don't show broken or deprecated patterns
- Don't include overly complex examples
- Don't miss the test examples
- Don't show patterns without context
- Don't recommend without evidence

Remember: You're providing templates and examples developers can adapt. Show them how it's been done successfully before.