Skip to content

feat: added llm based context specific for issues in post/debugging#235

Open
vedanshkap2692 wants to merge 2 commits into
imDarshanGK:mainfrom
vedanshkap2692:feat/context-aware-suggestion
Open

feat: added llm based context specific for issues in post/debugging#235
vedanshkap2692 wants to merge 2 commits into
imDarshanGK:mainfrom
vedanshkap2692:feat/context-aware-suggestion

Conversation

@vedanshkap2692
Copy link
Copy Markdown

Description

Implements optional LLM-powered, context-aware fix suggestions with caching, UI indicators, and graceful fallback to the existing rule-based engine.

Key behavior

  • When LLM_ENABLED=true and a valid LLM_API_KEY is available, the engine calls the LLM to generate concise, context-aware fixes for detected issues and replaces the generic rule-based suggestion.
  • LLM responses are cached in-process for 1 hour (configurable TTL) to avoid duplicate calls for identical issues.
  • If the LLM call fails or is disabled, the engine automatically falls back to the existing rule-based suggestion engine.
  • API responses now include ai_generated and ai_cached flags so clients can surface an “AI” badge and tooltip when appropriate.
  • Health endpoint and startup visibility added so frontend/Swagger can determine whether LLM integration is enabled.

Related Issue

Fixes #


Type of change

  • Bug fix (duplicate append removal, improved .env loading visibility)
  • New feature / enhancement (LLM integration, caching, metadata flags)
  • Documentation update (README, health endpoint usage)
  • Test addition (LLM tests, cache behavior, fallback validation)
  • Refactor (startup/env loading cleanup and health reporting improvements)

Files changed (high level)

Backend

  • backend/app/services/ai_provider.py

    • Added call_llm_sync()
    • Added get_fix_for_issue()
    • Implemented _LLM_FIX_CACHE with TTL-based caching
    • Improved prompts and fallback handling
  • backend/app/services/code_assistant.py

    • run_bug_detection() now integrates with ai_provider.get_fix_for_issue()
    • Added ai_generated and ai_cached metadata
    • Removed duplicate append bug
  • backend/app/schemas.py

    • Extended Issue model with:
      • code_context
      • ai_generated
      • ai_cached
    • Extended HealthResponse with:
      • llm_enabled
      • llm_model
  • backend/app/main.py

    • Loads .env early during startup/import
    • Health endpoint now reports LLM status
    • Added startup logging for easier debugging

Frontend

  • frontend/index.html
  • frontend/script.js
    • Added .ai-badge UI support
    • Added rendering logic for AI-generated fixes
    • Updated default backend API port to 8010
    • Added health-driven engine status badge

Tests

  • backend/tests/test_endpoints.py
    • Added tests for:
      • AI-generated suggestion replacement
      • Fallback to rule-based suggestions on provider failure
      • Cache reuse behavior

Checklist

  • I have read CONTRIBUTING.md
  • My branch is up to date with main
  • I have run pytest -v and all tests pass
  • I have not introduced duplicate issues or features
  • My PR title follows the format: feat/fix/docs/test: short description
  • I have added tests for new features
  • No hardcoded secrets or API keys are included
  • This PR is linked to a GSSoC 2026 issue

How to test locally

1. Configure environment variables

Create a .env file in the project root:

LLM_ENABLED=true
LLM_API_KEY=<your_api_key>

# Example
LLM_BASE_URL=https://openrouter.ai/api/v1
LLM_MODEL=<provider-specific-model>

## How to test

1. Add to `.env`:

```env
LLM_ENABLED=true
LLM_API_KEY=<your_key>
LLM_BASE_URL=https://openrouter.ai/api/v1
LLM_MODEL=<model_name>
  1. Start backend:
py -3.11 -m uvicorn app.main:app --reload --host 127.0.0.1 --port 8010
  1. Check health:
http://127.0.0.1:8010/health
  1. Open Swagger:
http://127.0.0.1:8010/docs

Test POST /debugging/ with:

{
  "code": "eval('2+2')"
}

Expected:

  • "ai_generated": true
  • "ai_cached": false on first request
  • "ai_cached": true on repeated request
  1. Run tests:
py -3.11 -m pytest -v

Expected:

63 passed
Screenshot (111)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant