Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Symfony Ai Context Bundle Laravel Package

ai-context/symfony-ai-context-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require ai-context/symfony-ai-context-bundle --dev
    

    Ensure the bundle is enabled in config/bundles.php (Symfony Flex handles this automatically).

  2. Generate Context:

    php bin/console ai-context:generate
    

    Outputs a structured JSON file at var/ai_context/ai-context.json by default.

  3. First Use Case: Use the generated JSON to query an LLM (e.g., GPT) for project insights:

    "Analyze this Symfony project structure and explain how the ArticleController interacts with the StockManagerService."
    

Implementation Patterns

Workflows

  1. Development Workflow:

    • Run ai-context:generate in a pre-commit hook or CI pipeline to keep the context updated.
    • Store the JSON in version control (e.g., var/ai_context/) for traceability.
  2. Integration with AI Tools:

    • Local Testing: Use the JSON with local AI tools (e.g., Ollama, LM Studio) for offline debugging.
    • Cloud APIs: Pipe the JSON to OpenAI/GPT via a custom script or API wrapper:
      $context = file_get_contents('var/ai_context/ai-context.json');
      $response = Http::post('https://api.openai.com/v1/chat/completions', [
          'body' => ['messages' => [['role' => 'user', 'content' => $context]]],
      ]);
      
  3. Dynamic Context:

    • Extend the bundle by creating custom extractors (see Supported extractors table). Example:
      # config/packages/ai_context.yaml
      ai_context:
          include:
              custom_extractors: true
      
      Then implement AiContextBundle\Extractor\ExtractorInterface.
  4. Symfony Events:

    • Listen to ai_context.generate events to post-process the context:
      // src/EventListener/AiContextListener.php
      public function onGenerate(AiContextGenerateEvent $event) {
          $context = $event->getContext();
          $context['custom_data'] = ['env' => $_ENV['APP_ENV']];
      }
      

Gotchas and Tips

Pitfalls

  1. Performance:

    • Large projects may slow down generation. Exclude unused paths in config/packages/ai_context.yaml:
      ai_context:
          paths:
              entities: ['%kernel.project_dir%/src/Entity/*']  # Exclude subdirectories
      
    • Cache the output aggressively (e.g., symlink to public/ and set HTTP cache headers).
  2. Doctrine Metadata:

    • If entities aren’t detected, ensure doctrine/orm is installed and metadata is cached:
      php bin/console doctrine:cache:clear-metadata
      
  3. Type Hinting:

    • The bundle relies on PHPStan for type resolution. If types are missing, install:
      composer require --dev phpstan/phpstan-symfony
      
  4. Symfony Flex Conflicts:

    • If the bundle isn’t auto-enabled, manually add it to config/bundles.php with dev: true.

Debugging

  • Verbose Output:
    php bin/console ai-context:generate --verbose
    
  • Checksum Validation: The bundle generates a checksum file (ai-context-checksum.json). Use it to detect changes:
    diff var/ai-context/ai-context-checksum.json var/ai-context/ai-context-checksum.json.bak
    

Extension Points

  1. Custom Extractors:

    • Implement ExtractorInterface to add support for new features (e.g., custom services):
      namespace App\Extractor;
      
      use AiContextBundle\Extractor\ExtractorInterface;
      
      class CustomServiceExtractor implements ExtractorInterface {
          public function extract(array &$context): void {
              $context['custom_services'] = $this->getCustomServices();
          }
      }
      
    • Register in config/packages/ai_context.yaml:
      ai_context:
          extractors:
              - App\Extractor\CustomServiceExtractor
      
  2. Post-Processing:

    • Use the ai_context.post_generate event to modify the JSON before saving:
      $event->getContext()['metadata']['generated_at'] = (new \DateTime())->format('c');
      
  3. Output Format:

    • Override the default JSON serializer by binding a custom JsonEncoder service:
      services:
          ai_context.json_encoder:
              class: App\Encoder\CustomJsonEncoder
              tags: ['ai_context.encoder']
      

Pro Tips

  • Git Integration: Add var/ai_context/ to .gitignore but commit the JSON periodically for historical context.
  • CI/CD: Use the bundle in GitHub Actions to auto-generate context on PRs:
    - name: Generate AI Context
      run: php bin/console ai-context:generate
    
  • Security: Sanitize the JSON before sending to external APIs to avoid exposing sensitive data (e.g., passwords in logs).
  • Partial Updates: Regenerate only specific sections by excluding others in config:
    ai_context:
        include:
            entities: false  # Skip entities if unchanged
    

```markdown
---
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle