platform/doc/ai_assistance_guide.md
2024-11-25 20:33:45 -07:00

5.3 KiB

AI Assistance Features Guide

This guide details how AI assistance works in our Laravel platform development workflow, focusing on practical examples and specific use cases.

1. Code Analysis & Generation

Laravel API Analysis

// Example: Implementing Container's bind method
// Command: /ai analyze-laravel Container::bind

AI will analyze:
1. Laravel's implementation:
```php
public function bind($abstract, $concrete = null, $shared = false)
{
    $this->dropStaleInstances($abstract);
    if (is_null($concrete)) {
        $concrete = $abstract;
    }
    // ...
}
  1. Generate Dart equivalent:
// GENERATED BY AI
/// Matches Laravel's Container::bind implementation
/// @see https://laravel.com/docs/container#binding
void bind<T>(T Function(Container) concrete, {bool shared = false}) {
  dropStaleInstances(T);
  // ...
}
  1. Provide compatibility notes:
API Compatibility Notes:
- Laravel uses dynamic typing, we use generics
- Laravel's null concrete defaults to abstract
- Maintain same binding behavior

Implementation Suggestions

// Command: /ai suggest-implementation Container::bind

AI provides:
1. Implementation pattern:
```dart
void bind<T>(T Function(Container) concrete, {bool shared = false}) {
  // AI suggests pattern based on Laravel's behavior
  _dropStaleInstances(typeOf<T>());
  
  final binding = shared 
    ? (c) => _shared(concrete)
    : concrete;
    
  _bindings[typeOf<T>()] = binding;
}
  1. Edge cases to handle:
// AI identifies critical scenarios:
- Rebinding existing types
- Handling shared vs non-shared
- Circular dependencies

2. Test Generation

Laravel Test Analysis

// Command: /ai generate-tests Container::bind

AI analyzes Laravel's tests:
```php
public function testBindingResolution()
{
    $container = new Container;
    $container->bind('name', function () { return 'Taylor'; });
    $this->assertEquals('Taylor', $container->make('name'));
}

Generates Dart tests:

@Test('matches Laravel binding resolution')
void testBindingResolution() {
  final container = Container();
  container.bind<String>((c) => 'Taylor');
  expect(container.make<String>(), equals('Taylor'));
}

Coverage Analysis

// Command: /ai analyze-test-coverage Container

AI provides:
1. Coverage report comparing Laravel & Dart tests
2. Missing test scenarios
3. Additional Dart-specific tests needed

3. Real-Time Development Assistance

API Compatibility Checking

// While coding, AI actively checks:
class Container {
  // AI Warning: Laravel's bind allows string abstracts
  void bind<T>(T Function(Container) concrete) { // <-- Compatibility issue
    
  }
}

// AI Suggestion:
Consider supporting both generic and string-based binding:
```dart
void bind<T>(T Function(Container) concrete) { ... }
void bindType(String abstract, Function concrete) { ... }

Pattern Recognition

// AI recognizes Laravel patterns and suggests Dart equivalents
// Laravel:
$container->singleton('api', function () { ... });

// AI suggests:
1. Direct equivalent:
```dart
container.singleton<ApiService>((c) => ApiService());
  1. Attribute-based (more Dart-like):
@Singleton()
class ApiService { ... }

4. Documentation Generation

API Documentation

// Command: /ai generate-docs Container::bind

AI generates:
```dart
/// Registers a binding with the container.
/// 
/// Equivalent to Laravel's Container::bind method.
/// ```php
/// $container->bind('api', function () { return new ApiService; });
/// ```
/// 
/// Dart usage:
/// ```dart
/// container.bind<ApiService>((c) => ApiService());
/// ```
void bind<T>(T Function(Container) concrete, {bool shared = false});

Migration Guides

// Command: /ai generate-migration-guide Container

AI generates guides showing:
1. Laravel to Dart mappings
2. Common patterns
3. Best practices

5. Interactive Development

AI Chat Commands

/ai explain Laravel's Container lifecycle
/ai compare Laravel vs Dart Container
/ai suggest refactoring for Container::bind
/ai review current implementation

Context-Aware Suggestions

// AI maintains context across files
// When implementing ServiceProvider:
class ServiceProvider {
  // AI suggests methods based on Laravel's ServiceProvider
  void register() { ... }  // <- AI: Required by Laravel
  void boot() { ... }      // <- AI: Optional but common
}

Getting Started

  1. Install required VS Code extensions:

    • GitHub Copilot
    • Platform AI Assistant
  2. Configure AI settings:

    {
      "platform.ai.laravel.version": "10.x",
      "platform.ai.analysis.autoCheck": true,
      "platform.ai.suggestions.enabled": true
    }
    
  3. Start using AI commands:

    • Type /ai in any Dart file to see available commands
    • Use AI suggestions for Laravel compatibility
    • Let AI help generate tests and documentation

Best Practices

  1. Always verify AI suggestions against Laravel's documentation
  2. Use AI for initial implementation, then review and refine
  3. Let AI help maintain API compatibility
  4. Use AI-generated tests as a starting point, then add edge cases
  5. Review AI-generated documentation for accuracy

Support

For issues or questions about AI assistance:

  1. Check the AI documentation
  2. Use /ai help command
  3. Report issues in the platform repository