-
Notifications
You must be signed in to change notification settings - Fork 59
Similar Code
geoffrey fernald edited this page Jan 28, 2026
·
1 revision
Find code semantically similar to what you're about to write. Returns relevant examples with patterns and conventions to use as templates.
When creating new code, find existing code that does something similar. This ensures consistency and helps you follow established patterns.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β SIMILAR CODE SEARCH β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Query: "user preferences CRUD" β
β Intent: api_endpoint β
β β
β Results: β
β β
β 1. src/controllers/settings.ts (92% match) β
β ββ User settings CRUD operations β
β ββ Patterns: REST controller, validation, auth β
β β
β 2. src/controllers/profile.ts (87% match) β
β ββ User profile management β
β ββ Patterns: REST controller, DTO validation β
β β
β 3. src/controllers/notifications.ts (78% match) β
β ββ Notification preferences β
β ββ Patterns: REST controller, partial updates β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
drift_similar({
intent: "api_endpoint", // What kind of code
description: "user preferences CRUD", // Natural language description
limit: 3, // Max results
scope: "src/controllers" // Optional: limit to directory
})Intent Options:
| Intent | Description |
|---|---|
api_endpoint |
REST/GraphQL endpoints |
service |
Business logic services |
component |
UI components |
hook |
React/Vue hooks |
utility |
Helper functions |
test |
Test files |
middleware |
Request middleware |
Response:
{
"summary": "Found 3 similar api_endpoint example(s). Top: src/controllers/settings.ts",
"data": {
"matches": [
{
"file": "src/controllers/settings.ts",
"function": "getSettings",
"class": "SettingsController",
"similarity": 0.92,
"reason": "Has api_endpoint decorator. Matches 3 api_endpoint patterns",
"preview": "@Get export async function getSettings(user: AuthUser): Promise<Settings>",
"patterns": ["REST Controller", "DTO Validation", "Auth Middleware"]
},
{
"file": "src/controllers/profile.ts",
"function": "updateProfile",
"similarity": 0.87,
"reason": "Has api_endpoint decorator. Name matches 2 keywords",
"preview": "@Put export async function updateProfile(dto: UpdateProfileDto): Promise<Profile>",
"patterns": ["REST Controller", "DTO Validation"]
},
{
"file": "src/controllers/notifications.ts",
"function": "getNotificationPreferences",
"similarity": 0.78,
"reason": "Matches 2 api_endpoint patterns",
"preview": "@Get export async function getNotificationPreferences(): Promise<NotificationSettings>",
"patterns": ["REST Controller"]
}
],
"conventions": {
"naming": "camelCase",
"errorHandling": "Async with try/catch",
"imports": "Path alias (@/)"
}
},
"hints": {
"nextActions": [
"Use drift_signature to get full signature for \"getSettings\"",
"Use drift_imports to get correct imports for your new file"
],
"relatedTools": ["drift_signature", "drift_imports", "drift_code_examples"]
}
}// Find similar endpoints
const similar = await drift_similar({
intent: "api_endpoint",
description: "order history with pagination"
});
// Use the most similar as template
// similar.data.results[0].snippetdrift_similar({
intent: "component",
description: "modal dialog with form validation"
})drift_similar({
intent: "service",
description: "email notification service"
})drift_similar({
intent: "hook",
description: "data fetching with caching"
})Drift analyzes your description using:
- Keyword extraction
- Intent classification
- Domain matching
All code is indexed by:
- Function/class names
- Comments and documentation
- Pattern associations
- File structure
Results are scored by:
- Semantic similarity to description
- Pattern overlap
- Structural similarity
- Recency (newer code preferred)
// 1. Get context for your task
const context = await drift_context({
intent: "add_feature",
focus: "user preferences"
});
// 2. Find similar code
const similar = await drift_similar({
intent: "api_endpoint",
description: "user preferences CRUD"
});
// 3. Get detailed examples if needed
const examples = await drift_code_examples({
pattern: similar.data.commonPatterns[0].id
});
// 4. Generate code using templates
// 5. Validate generated code
await drift_validate_change({
file: "src/controllers/preferences.ts",
content: generatedCode
});drift_similar({
intent: "service",
description: "payment processing",
scope: "src/services" // Only search in services
})// First find patterns
const patterns = await drift_patterns_list({
categories: ["api"]
});
// Then find similar code with those patterns
drift_similar({
intent: "api_endpoint",
description: "user management"
})// β
Good - specific
drift_similar({
description: "paginated list endpoint with filtering and sorting"
})
// β Bad - too vague
drift_similar({
description: "list endpoint"
})// β
Match intent to what you're creating
drift_similar({
intent: "hook", // Creating a hook
description: "form validation with error handling"
})
// β Wrong intent
drift_similar({
intent: "api_endpoint", // But creating a hook
description: "form validation"
})// β
Scoped search is faster and more relevant
drift_similar({
intent: "component",
description: "data table",
scope: "src/components"
})The response includes conventions β these show the coding style used in similar code:
{
"conventions": {
"naming": "camelCase",
"errorHandling": "Async with try/catch",
"imports": "Path alias (@/)"
}
}Follow these conventions when writing your new code.
---
## Next Steps
- [Code Examples](Code-Examples) β Get detailed code snippets
- [Validate Change](Validate-Change) β Verify generated code
- [Pattern Categories](Pattern-Categories) β All 15 categories
- Cortex V2 Overview
- Memory Setup Wizard
- Memory CLI
- Universal Memory Types
- Learning System
- Token Efficiency
- Causal Graphs
- Code Generation
- Predictive Retrieval
- Architecture
- Call Graph Analysis
- Impact Analysis
- Security Analysis
- Data Boundaries
- Test Topology
- Coupling Analysis
- Error Handling Analysis
- Wrappers Detection
- Environment Variables
- Constants Analysis
- Styling DNA
- Constraints
- Contracts
- Decision Mining
- Speculative Execution
- Watch Mode
- Trends Analysis
- Projects Management
- Package Context
- Monorepo Support
- Reports & Export
- Dashboard
- 10 Languages
- 21 Frameworks
- 16 ORMs
- 400+ Detectors
- 50+ MCP Tools
- 60+ CLI Commands
- 23 Memory Types