# Tasks: LLM Analysis & Documentation Plugins

**Feature**: `017-llm-analysis-plugin`
**Status**: Completed
**Spec**: [specs/017-llm-analysis-plugin/spec.md](spec.md)

## Dependencies

- **Phase 1 (Setup)**: Must run first.
- **Phase 2 (Foundational)**: Must complete before any User Stories.
- **Phase 3 (US1 - Dashboard Analysis)**: Depends on Phase 2.
- **Phase 4 (US2 - Documentation)**: Depends on Phase 2. Parallel with Phase 3.
- **Phase 5 (US3 - Configuration)**: Depends on Phase 2. Recommended before Phase 3/4 for easier testing, but technically parallelizable if config is mocked.
- **Phase 6 (US4 - Git Integration)**: Independent of other stories, depends on Phase 2.

## Phase 1: Setup

**Goal**: Initialize project structure and dependencies.

- [x] T001 Install backend dependencies (openai, playwright, tenacity) in `backend/requirements.txt`
- [x] T002 Create plugin directory structure `backend/src/plugins/llm_analysis/`
- [x] T003 Initialize `backend/src/plugins/llm_analysis/__init__.py`
- [x] T004 Create `backend/src/plugins/llm_analysis/models.py` for Pydantic models (LLMProviderConfig, ValidationResult)
- [x] T005 Update `backend/src/core/plugin_loader.py` to recognize new plugin type if necessary
- [x] T006 Create `backend/src/api/routes/llm.py` placeholder
- [x] T007 Register new route in `backend/src/app.py`

## Phase 2: Foundational

**Goal**: Implement core services and shared infrastructure.

- [x] T008 Implement `LLMProviderService` in `backend/src/services/llm_provider.py` (CRUD for providers, AES-256 encryption)
- [x] T009 Implement `ScreenshotService` in `backend/src/plugins/llm_analysis/service.py` (Playwright + API strategies, fallback logic, 1920px width, full page height, CDP screenshot, recursive tab switching)
- [x] T010 Implement `LLMClient` in `backend/src/plugins/llm_analysis/service.py` (OpenAI SDK wrapper, retry logic with tenacity, rate limit handling)
- [x] T011 Create `backend/src/plugins/llm_analysis/plugin.py` with `PluginBase` implementation stubs
- [x] T012 Define database schema updates for `LLMProviderConfig` in `backend/src/models/llm.py` (or appropriate location)
- [x] T013 Run migration to create new tables (if using SQLAlchemy/Alembic) or update SQLite schema

## Phase 3: Dashboard Health Analysis (US1)

**Goal**: Enable automated dashboard validation with multimodal analysis.

- [x] T014 [US1] Implement `validate_dashboard` task logic in `backend/src/plugins/llm_analysis/plugin.py`
- [x] T015 [US1] Implement log retrieval logic (fetch recent logs, limit 100 lines/24h) in `backend/src/plugins/llm_analysis/plugin.py`
- [x] T016 [US1] Construct multimodal prompt (image + text) in `backend/src/plugins/llm_analysis/service.py` (implement PII/credential filtering)
- [x] T017 [US1] Implement result parsing and persistence (ValidationResult) in `backend/src/plugins/llm_analysis/plugin.py` (ensure JSON structure: status, issues, summary, log results to task output with `[ANALYSIS_SUMMARY]` and `[ANALYSIS_ISSUE]` markers)
- [x] T018 [US1] Add `validate` endpoint trigger in `backend/src/api/routes/tasks.py` (or reuse existing dispatch)
- [x] T019 [US1] Implement notification dispatch (Email/Pulse) on failure in `backend/src/plugins/llm_analysis/plugin.py` (Summary + Link format)
- [x] T020 [US1] Create `frontend/src/components/llm/ValidationReport.svelte` for viewing results
- [x] T021 [US1] Add "Validate" button with loading state and disabling logic to `frontend/src/components/DashboardGrid.svelte` (FR-019)
- [x] T022 [US1] Enable scheduling for validation tasks in `backend/src/core/scheduler.py` (if not automatic via TaskManager)

## Phase 4: Automated Dataset Documentation (US2)

**Goal**: Generate and persist dataset documentation.

- [x] T023 [US2] Implement `generate_documentation` task logic in `backend/src/plugins/llm_analysis/plugin.py`
- [x] T024 [US2] Implement metadata fetching (schema, columns) in `backend/src/plugins/llm_analysis/plugin.py`
- [x] T025 [US2] Construct documentation prompt in `backend/src/plugins/llm_analysis/service.py` (handle schema changes)
- [x] T026 [US2] Implement metadata update logic (write back to DB) in `backend/src/services/mapping_service.py` (handle partial failures with rollback, ignore invalid columns)
- [x] T045 [US2] Implement optimistic locking for concurrent metadata updates
- [x] T027 [US2] Add "Generate Docs" button to `frontend/src/components/tools/MapperTool.svelte` using standard system icons (FR-021)
- [x] T028 [US2] Create feedback/preview UI component `frontend/src/components/llm/DocPreview.svelte` (optional but recommended)

## Phase 5: LLM Provider Configuration (US3)

**Goal**: Manage LLM providers via UI.

- [x] T029 [US3] Implement CRUD endpoints for providers in `backend/src/api/routes/llm.py`
- [x] T030 [US3] Create `frontend/src/components/llm/ProviderConfig.svelte` form
- [x] T031 [US3] Create `frontend/src/routes/admin/settings/llm/+page.svelte` settings page
- [x] T032 [US3] Implement "Test Connection" button logic with Toast feedback (FR-020)
- [x] T033 [US3] Ensure API keys are masked in frontend responses

## Phase 6: Git Commit Message Suggestion (US4)

**Goal**: AI-assisted commit messages.

- [x] T034 [US4] Implement `generate_commit_message` logic in `backend/src/plugins/git/llm_extension.py` (validate conventional commit format)
- [x] T035 [US4] Create endpoint `POST /api/git/generate-message` in `backend/src/api/routes/git.py`
- [x] T036 [US4] Construct commit generation prompt (Diff + History) in `backend/src/plugins/git/llm_extension.py`
- [x] T037 [US4] Add "Generate" button to `frontend/src/components/git/CommitModal.svelte`
- [x] T038 [US4] Wire up frontend to call generation endpoint and populate textarea (handle empty/invalid response with Toast)

## Phase 7: Polish & Cross-Cutting

**Goal**: Finalize and verify.

- [x] T039 Verify all permissions (`plugin:llm:validate`, etc.) are registered and enforceable
- [x] T040 Verify i18n strings for all new UI components
- [x] T041 Run full end-to-end test of Dashboard Validation flow (including fallback scenarios)
- [x] T042 Run full end-to-end test of Documentation flow
- [x] T043 Update `README.md` with new plugin capabilities
- [x] T044 Verify API key masking in all UI responses and logs

## Implementation Strategy

1. **MVP**: Focus on Phase 1, 2, 3, and 5. This delivers the core value (Dashboard Validation) and the necessary configuration.
2. **Increment 1**: Add Phase 4 (Documentation).
3. **Increment 2**: Add Phase 6 (Git Integration).

## Parallel Execution Examples

- **Backend/Frontend Split**: T020 (Frontend Report) can be built while T014-T017 (Backend Logic) are in progress, using mock data.
- **Story Parallelism**: US2 (Documentation) and US4 (Git) are largely independent and can be worked on simultaneously by different developers (or sequentially without blocking).