examples / ai-agents

AI Agents

Examples of using gnaw's AI-powered agent mode for intelligent code analysis and understanding.

Code Understanding

Function Discovery

Find and understand functions in your codebase:

# Build index for semantic search
gnaw agent index build

# Find authentication functions
gnaw agent ask "authentication functions"

# Find error handling functions
gnaw agent ask "error handling functions" --type rs

# Find database-related functions
gnaw agent ask "database functions" --dir src/db
```bash Terminal gnaw agent ask "authentication functions" ```
Query: authentication functions

→ src/auth.rs (score: 0.95)
  Lines 15-25
  • Function definition matches query
  • Contains authentication keywords
  • High semantic similarity
  pub fn authenticate_user(token: &str) -> Result<User, AuthError> {
      // Authentication logic here
  }

→ src/middleware.rs (score: 0.87)
  Lines 45-55
  • Middleware function for authentication
  • Contains auth-related logic
  pub fn auth_middleware(req: &Request) -> Result<(), AuthError> {
      // Middleware logic here
  }

Pattern Recognition

Identify common coding patterns:

# Find async/await patterns
gnaw agent ask "async await patterns" --type js,ts

# Find error handling patterns
gnaw agent ask "error handling with try catch" --type js

# Find API endpoint patterns
gnaw agent ask "API endpoint definitions" --type rs,go

Code Relationships

Understand how code components relate:

# Find functions that call authentication
gnaw agent ask "functions that call authentication"

# Find error handling dependencies
gnaw agent ask "error handling dependencies"

# Find database connection usage
gnaw agent ask "database connection usage"

Architecture Analysis

Component Discovery

Analyze system architecture:

# Find microservice boundaries
gnaw agent ask "microservice boundaries"

# Find API gateway patterns
gnaw agent ask "API gateway patterns"

# Find service discovery
gnaw agent ask "service discovery patterns"

Design Patterns

Identify design patterns:

# Find singleton patterns
gnaw agent ask "singleton pattern implementation"

# Find factory patterns
gnaw agent ask "factory pattern implementation"

# Find observer patterns
gnaw agent ask "observer pattern implementation"

Security Analysis

Vulnerability Detection

Find potential security issues:

# Find hardcoded credentials
gnaw agent ask "hardcoded credentials or secrets"

# Find SQL injection vulnerabilities
gnaw agent ask "SQL injection vulnerabilities"

# Find authentication bypasses
gnaw agent ask "authentication bypass patterns"

Security Patterns

Identify security implementations:

# Find encryption usage
gnaw agent ask "encryption and cryptographic functions"

# Find input validation
gnaw agent ask "input validation and sanitization"

# Find access control
gnaw agent ask "access control and authorization"

Performance Analysis

Performance Bottlenecks

Identify performance issues:

# Find slow database queries
gnaw agent ask "slow database queries and N+1 problems"

# Find memory leaks
gnaw agent ask "memory leak patterns"

# Find inefficient algorithms
gnaw agent ask "inefficient algorithms and O(n²) complexity"

Optimization Opportunities

Find optimization opportunities:

# Find caching opportunities
gnaw agent ask "caching and memoization opportunities"

# Find parallel processing opportunities
gnaw agent ask "parallel processing and concurrency"

# Find database optimization
gnaw agent ask "database optimization and indexing"

Testing Analysis

Test Coverage

Analyze test coverage:

# Find untested functions
gnaw agent ask "untested functions and methods"

# Find test patterns
gnaw agent ask "test patterns and testing strategies"

# Find mock usage
gnaw agent ask "mock objects and test doubles"

Test Quality

Assess test quality:

# Find integration tests
gnaw agent ask "integration test patterns"

# Find unit test patterns
gnaw agent ask "unit test patterns and best practices"

# Find test data management
gnaw agent ask "test data management and fixtures"

Documentation Analysis

Code Documentation

Analyze code documentation:

# Find undocumented functions
gnaw agent ask "undocumented functions and methods"

# Find documentation patterns
gnaw agent ask "documentation patterns and examples"

# Find API documentation
gnaw agent ask "API documentation and OpenAPI"

Knowledge Gaps

Identify knowledge gaps:

# Find complex code sections
gnaw agent ask "complex code sections that need documentation"

# Find business logic
gnaw agent ask "business logic and domain knowledge"

# Find configuration management
gnaw agent ask "configuration management and environment setup"

Integration Examples

CI/CD Integration

Integrate with CI/CD pipelines:

#!/bin/bash
# CI/CD script for code analysis
echo "Building code index..."
gnaw agent index build

echo "Analyzing code quality..."
gnaw agent ask "code quality issues" --json > quality-report.json

echo "Checking for security issues..."
gnaw agent ask "security vulnerabilities" --json > security-report.json

echo "Analyzing performance..."
gnaw agent ask "performance bottlenecks" --json > performance-report.json

Automated Reporting

Generate automated reports:

#!/bin/bash
# Generate weekly code analysis report
DATE=$(date +%Y-%m-%d)
REPORT_DIR="reports/$DATE"

mkdir -p "$REPORT_DIR"

# Analyze different aspects
gnaw agent ask "authentication functions" --json > "$REPORT_DIR/auth-analysis.json"
gnaw agent ask "error handling patterns" --json > "$REPORT_DIR/error-analysis.json"
gnaw agent ask "performance issues" --json > "$REPORT_DIR/performance-analysis.json"

# Generate summary
echo "Code Analysis Report - $DATE" > "$REPORT_DIR/summary.txt"
echo "Authentication functions: $(jq '.results | length' "$REPORT_DIR/auth-analysis.json")" >> "$REPORT_DIR/summary.txt"
echo "Error handling patterns: $(jq '.results | length' "$REPORT_DIR/error-analysis.json")" >> "$REPORT_DIR/summary.txt"
echo "Performance issues: $(jq '.results | length' "$REPORT_DIR/performance-analysis.json")" >> "$REPORT_DIR/summary.txt"

IDE Integration

Integrate with development environments:

#!/bin/bash
# VS Code extension script
QUERY="$1"
FILE_PATH="$2"

# Get semantic search results
gnaw agent ask "$QUERY" --dir "$FILE_PATH" --json | jq '.results[] | {file: .file, snippet: .snippet, score: .score}'

Advanced Use Cases

Code Migration

Assist with code migration:

# Find deprecated patterns
gnaw agent ask "deprecated patterns and legacy code"

# Find migration opportunities
gnaw agent ask "migration opportunities and refactoring"

# Find compatibility issues
gnaw agent ask "compatibility issues and breaking changes"

Code Review

Assist with code reviews:

# Find potential issues
gnaw agent ask "potential issues and code smells"

# Find best practices
gnaw agent ask "best practices and coding standards"

# Find code duplication
gnaw agent ask "code duplication and DRY violations"

Knowledge Transfer

Facilitate knowledge transfer:

# Find expert knowledge
gnaw agent ask "expert knowledge and domain expertise"

# Find learning opportunities
gnaw agent ask "learning opportunities and skill development"

# Find mentoring opportunities
gnaw agent ask "mentoring opportunities and knowledge sharing"

Best Practices

- Use specific, descriptive queries - Include context in your questions - Use multiple queries for comprehensive analysis - Combine with traditional search when needed - Rebuild index regularly - Monitor index size and performance - Use appropriate file type filters - Consider directory-specific indexes - Review semantic scores - Consider context and relevance - Verify results manually - Use JSON output for automation

Troubleshooting

- Rebuild index if results seem outdated - Check index status with `gnaw agent index status` - Verify file permissions - Monitor index size and performance - Use more specific queries - Try different phrasings - Combine with traditional search - Check file type and directory filters - Monitor memory usage - Check CPU utilization - Consider index size - Use appropriate limits AI agents are most effective for understanding large, complex codebases where traditional text search falls short. Use them to discover patterns, relationships, and insights that would be difficult to find manually.