diff --git a/.claude/settings.local.json b/.claude/settings.local.json new file mode 100644 index 0000000000000000000000000000000000000000..fcaac134841e80a6c081d72ebfd03a6890e44d77 --- /dev/null +++ b/.claude/settings.local.json @@ -0,0 +1,13 @@ +{ + "permissions": { + "allow": [ + "Bash(pip install *)", + "Bash(python -c \"from backend.main import app; print\\('OK — routes:', len\\(app.routes\\)\\)\")", + "Bash(python -c ' *)", + "Bash(python -c \"from backend.main import app; from backend.core.url_guard import assert_safe_url; print\\('OK routes:', len\\(app.routes\\)\\)\")", + "Bash(python -c \"from backend.main import app; from backend.core.errors import AppError; print\\('OK routes:', len\\(app.routes\\)\\)\")", + "Bash(Get-ChildItem -Path \"C:\\\\Users\\\\amitk\\\\OneDrive\\\\Desktop\\\\Testing\" -Recurse -Directory)", + "Bash(Select-Object -ExpandProperty FullName)" + ] + } +} diff --git a/.env.example b/.env.example deleted file mode 100644 index eef31edafdfe41ded5e1ab00b91a8295b10afd4f..0000000000000000000000000000000000000000 --- a/.env.example +++ /dev/null @@ -1,27 +0,0 @@ -# AI API Testing Tool - Environment Variables - -# Mistral AI configuration -MISTRAL_API_KEY=your_mistral_api_key_here -MISTRAL_MODEL=mistral-large-latest - -# Backend configuration (Local default: 8001) -# On Render, the PORT will be assigned automatically. -PORT=8001 - -# Database Configuration (Supabase PostgreSQL) -# Get your Connection String from Supabase -> Project Settings -> Database -DATABASE_URL=postgresql://postgres.your-project:your-password@aws-0-region.pooler.supabase.com:5432/postgres - -# Supabase Storage & Client Configuration -# Get these from Supabase -> Project Settings -> API -SUPABASE_URL=https://your-project.supabase.co -# Use the Service Role Key for backend storage access -SUPABASE_SERVICE_ROLE_KEY=your-service-role-key-here -SUPABASE_STORAGE_BUCKET=assets - -# Frontend Configuration (Vite) -# Points to the Backend URL. -# Local: http://localhost:8001 -# Render: https://your-backend.onrender.com -VITE_BACKEND_URL=http://localhost:8001 -VITE_API_URL=http://localhost:8001 diff --git a/.gitignore b/.gitignore index 111ee4866d0950b7696a6b6944cf64ea7954494b..2b2fde7bc1a525516f164396f8dbe458dddcab5e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,36 +1,63 @@ -# Dependencies +# ── Node ───────────────────────────────────────── node_modules/ dist/ build/ +.npm/ -# Python +# ── Python ─────────────────────────────────────── __pycache__/ -*.pyc -*.pyo +*.py[cod] *.pyd -.Python +*.pyo +*.egg-info/ +.eggs/ env/ venv/ +.venv/ pip-log.txt -pip-delete-this-directory.txt -# Databases -*.db -*.sqlite3 +# ── Testing ────────────────────────────────────── +.pytest_cache/ +htmlcov/ +.coverage +coverage.xml -# Environment variables +# ── Environment & secrets ───────────────────────── .env +.env.* +!.env.example + +# ── Uploaded user files ─────────────────────────── +backend/uploads/audio/* +backend/uploads/video/* +backend/uploads/documents/* +backend/uploads/images/* +backend/uploads/specs/* +!backend/uploads/**/.gitkeep -# Logs +# ── Archives & temp artifacts ───────────────────── +*.zip +*.tar.gz +*.rar +temp_review/ +tmp_*/ + +# ── Logs ───────────────────────────────────────── *.log +logs/ -# OS files +# ── OS ─────────────────────────────────────────── .DS_Store Thumbs.db desktop.ini +ehthumbs.db -# IDEs +# ── IDEs ───────────────────────────────────────── .vscode/ .idea/ *.swp *.swo + +# ── Databases ──────────────────────────────────── +*.db +*.sqlite3 diff --git a/Current UI/Screenshot 2026-05-01 135942.png b/Current UI/Screenshot 2026-05-01 135942.png new file mode 100644 index 0000000000000000000000000000000000000000..e305130136cf03866e9f0be321600786f192df4a --- /dev/null +++ b/Current UI/Screenshot 2026-05-01 135942.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8b25ff6fb4391dc1aff89790b6fcf7a97adf42281b917dc480f4732225f792f6 +size 44205 diff --git a/Current UI/Screenshot 2026-05-01 140023.png b/Current UI/Screenshot 2026-05-01 140023.png new file mode 100644 index 0000000000000000000000000000000000000000..ffbd673dc0469e83fe259f61ffeeac0b4a2af52b --- /dev/null +++ b/Current UI/Screenshot 2026-05-01 140023.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c47a749977cf7f6e459492d77ee276198f36afea293428ab98220668ba098744 +size 95992 diff --git a/Current UI/Screenshot 2026-05-01 140040.png b/Current UI/Screenshot 2026-05-01 140040.png new file mode 100644 index 0000000000000000000000000000000000000000..3088e48c107ba01b0c29d4834008a91dc18290cc --- /dev/null +++ b/Current UI/Screenshot 2026-05-01 140040.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:88b8441a748c59f39d1dc7c63d771b43ed23f6fee2c50b2ee826c471f8dac07c +size 89128 diff --git a/Current UI/Screenshot 2026-05-01 140058.png b/Current UI/Screenshot 2026-05-01 140058.png new file mode 100644 index 0000000000000000000000000000000000000000..14794ff6c0a2f634e781db2b26fc4ce9ecaa3018 --- /dev/null +++ b/Current UI/Screenshot 2026-05-01 140058.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a45ee7a048f5ac25c833b155bff8bf46cb7361c58943deca881c5c80098d47b1 +size 135962 diff --git a/Current UI/Screenshot 2026-05-01 140111.png b/Current UI/Screenshot 2026-05-01 140111.png new file mode 100644 index 0000000000000000000000000000000000000000..763daf8b75a2a1cced7fd41090162753591bb110 --- /dev/null +++ b/Current UI/Screenshot 2026-05-01 140111.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ee152153f3d9700ab939f56c1c7136ac7ceac714d4b9356ce6cd712089d36b4a +size 61141 diff --git a/Current UI/Screenshot 2026-05-01 140124.png b/Current UI/Screenshot 2026-05-01 140124.png new file mode 100644 index 0000000000000000000000000000000000000000..103de16862670ad58502820d50dfe1d7dbf46cb1 --- /dev/null +++ b/Current UI/Screenshot 2026-05-01 140124.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c1d2a7f0208ca0e1f40cc84cfe5122785d4c8f280c75e9a5a953b5565f9d8ae3 +size 98460 diff --git a/Current UI/Screenshot 2026-05-01 140140.png b/Current UI/Screenshot 2026-05-01 140140.png new file mode 100644 index 0000000000000000000000000000000000000000..79bda8790acc933edb6ab83291f0e38222477c3a --- /dev/null +++ b/Current UI/Screenshot 2026-05-01 140140.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e127abbc76002a1caa2c056e01b35e75265cf768079cb0f4aee0adfc5a8a9289 +size 79628 diff --git a/Old Chats claude/1.md b/Old Chats claude/1.md new file mode 100644 index 0000000000000000000000000000000000000000..153255ac133a39f402ae640e37a9addf0dc9ccd3 --- /dev/null +++ b/Old Chats claude/1.md @@ -0,0 +1,2 @@ +claude --resume b12b89ce-94d3-48fc-b8f8-b73ba5ddee29 +claude --resume abc2b770-ce67-4c01-8f04-0e1e84cb120d , remaining diff --git a/Plans/plan 1.md b/Plans/plan 1.md new file mode 100644 index 0000000000000000000000000000000000000000..8a0682b2bdddeca8edc60f13cc5d1e8b5f030b6b --- /dev/null +++ b/Plans/plan 1.md @@ -0,0 +1,231 @@ +# Backend Refactor Execution Plan (Actionable) + +## Phase 0 — Preparation + +* Create new branch: `refactor/architecture-v1` +* Freeze new feature development +* Add `.env` and `core/config.py` + +--- + +## Phase 1 — Restructure (No Logic Changes) + +### Create Directories + +``` +backend/app/api/routes/ +backend/core/ +backend/domain/models/ +backend/services/ +backend/integrations/llm/ +backend/integrations/http/ +backend/integrations/parsers/ +backend/repositories/ +backend/workers/ +backend/schemas/ +backend/utils/ +``` + +### Move Files + +* `main.py` → `app/main.py` +* routes → `app/api/routes/` +* shared utils → `utils/` +* existing services → `services/` + +### Update Imports + +* Replace relative imports with absolute: + +``` +from backend.services... +``` + +--- + +## Phase 2 — Config + Core Systems + +### Create `core/config.py` + +* Use `BaseSettings` +* Add: + + * `ALLOWED_ORIGINS` + * `API_KEYS` + * `UPLOAD_DIR` + * `TIMEOUT` + +### Replace Hardcoded Values + +* Replace all constants with config usage + +### Add Logging + +* Create `core/logging.py` +* Replace all `print()` with logger + +--- + +## Phase 3 — LLM Isolation + +### Move LLM Logic + +FROM: + +* `services/llm_planner.py` +* `services/chat_assistant.py` + +TO: + +``` +integrations/llm/ + client.py + planner.py + analyzer.py +``` + +### Enforce Rule + +* No direct OpenAI calls outside `client.py` + +--- + +## Phase 4 — Execution Isolation + +### Move Executor + +* `api_executor.py` → `integrations/http/sandbox.py` + +### Add Controls + +* allowlist domains +* enforce timeout +* restrict HTTP methods + +--- + +## Phase 5 — Validation Layer + +### Create + +`services/validation_service.py` + +### Add Checks + +* validate LLM JSON schema +* reject unsafe URLs +* sanitize inputs + +### Integrate + +* execution_service MUST call validation before execution + +--- + +## Phase 6 — Split Responsibilities + +### Create + +* `planning_service.py` +* `execution_service.py` + +### Flow + +``` +route → planning_service → validation_service → execution_service +``` + +--- + +## Phase 7 — Remove Global State + +### Delete + +* `get_context` +* `set_context_variables` + +### Replace With + +* request-scoped data OR +* `domain/models/user_session.py` +* `repositories/session_repo.py` + +--- + +## Phase 8 — Background Jobs + +### Create + +`workers/tasks.py` + +### Move Heavy Tasks + +* LLM calls +* API chains + +### Use + +* FastAPI BackgroundTasks (initial) + +--- + +## Phase 9 — Security Layer + +### Create + +`core/security.py` + +### Add + +* JWT auth +* rate limiting middleware + +### Apply + +* protect all routes except health + +--- + +## Phase 10 — Frontend Alignment + +### Create + +``` +src/api/ +src/hooks/ +src/store/ +``` + +### Add + +* API client wrapper +* React Query + +--- + +## Phase 11 — Cleanup + +* Remove unused files +* Remove duplicate logic +* Remove debug prints + +--- + +## Phase 12 — Testing + +* Add unit tests for: + + * validation_service + * execution_service +* Add integration tests for routes + +--- + +## Final Architecture Rule + +* routes = HTTP only +* services = orchestration only +* integrations = external calls only +* domain = pure logic only + +NO cross-layer violations allowed. diff --git a/Plans/plan 2.md b/Plans/plan 2.md new file mode 100644 index 0000000000000000000000000000000000000000..be28ad289ee290965d847b597505a17a0004f00c --- /dev/null +++ b/Plans/plan 2.md @@ -0,0 +1,161 @@ +# Project Completion Plan — AI API Testing Assistant + +**Goal:** Make the project fully functional, secure, and production-ready. +**Based on:** Actual code audit of the current codebase. + +--- + +## ✅ Phase 1 — Fix Broken Foundations — DONE + +- [x] Fix `requirements.txt`: removed `streamlit`, `websockets`; added `prance`, `pyyaml`, `pydantic-settings`, `slowapi`, `cryptography`; upgraded LangChain + Mistral packages; fixed `psycopg2-binary>=2.9.10` (Python 3.13 wheel), `sqlalchemy>=2.0.36`, gunicorn platform guard +- [x] Fix CORS: replaced `allow_origins=["*"]` with `settings.allowed_origins_list` from env var +- [x] Fix silent failures: removed bare `except: pass` blocks; all errors logged or returned as HTTP +- [x] Clean `App.jsx`: deleted ~800 lines of commented-out dead code at top; active code only +- [x] Fix `chat_assistant.py`: replaced `HumanMessage` with `AIMessage` for assistant replies +- [x] Add `.gitignore`: covers `*.zip`, `*.tar.gz`, `.env.*`, `__pycache__`, `.venv`, uploads, etc. + +--- + +## ✅ Phase 2 — Backend Restructure — DONE + +- [x] Created `backend/core/config.py` — `BaseSettings` with all env vars +- [x] Created `backend/core/logging.py` — `get_logger()` structured logger +- [x] Created `backend/core/errors.py` — `AppError` + `app_error_handler` +- [x] Split `main.py` into 6 focused route files (no logic changes): + - `backend/app/api/routes/specs.py` — `/specs`, `/upload-spec`, `/credentials` + - `backend/app/api/routes/sessions.py` — `/session`, `/context` + - `backend/app/api/routes/execution.py` — `/plan`, `/execute-step`, `/analyze-response` + - `backend/app/api/routes/assets.py` — `/assets`, `/collections` + - `backend/app/api/routes/chat.py` — `/chat`, `/chat-sessions` + - `backend/app/api/routes/history.py` — `/history` +- [x] `main.py` reduced to ~60 lines (app setup only) +- [x] All 36 routes preserved throughout + +--- + +## ✅ Phase 3 — Security & Validation — DONE + +- [x] URL sandboxing (`backend/core/url_guard.py`): blocks localhost, private IP ranges (10.x, 172.16-31.x, 192.168.x, 169.254.x, ::1, fc00::/7), non-HTTP schemes; resolves hostname to IP before check +- [x] `assert_safe_url()` called in `/execute-step` before every outbound HTTP request +- [x] Spec file size validation: 5MB limit in `/upload-spec` +- [x] Rate limiting via `slowapi`: `/plan` 10/min, `/execute-step` 30/min, `/chat` 20/min + +--- + +## ✅ Phase 4 — Error Handling — DONE + +- [x] Standardized `AppError` exception with `{ "error", "code", "detail" }` shape +- [x] LLM retry: one automatic retry on JSON parse failure in both `llm_planner.py` and `response_analyzer.py` +- [x] Execution error context: `TimeoutException → 504`, `ConnectError → 502`, 4xx/5xx responses logged with URL + body snippet + +--- + +## ✅ Phase 5 — Complete Half-Built Features — DONE + +- [x] OTP modal UI: inline modal shown when `is_otp_required` detected; saves to session context +- [x] Selection modal UI: inline modal shown when `is_selection_required` detected; saves chosen value +- [x] Token usage display: shown per-step and as running total in the top bar +- [x] **Run All** button: runs all execution plan steps sequentially; stops if OTP/selection prompt appears +- [x] **Retry** button: amber button appears next to failed steps (status = 'failure') +- [x] **Export session** (`GET /history/{session_id}/export`): returns full JSON report with pass/fail counts; forces browser download +- [x] `HistoryService.exportSession()` in `api.js` +- [x] Export button in `HistoryPage.jsx` (Download icon, between Delete and Import) + +--- + +## 🔲 Phase 6 — LLM Isolation + +Consolidate all LLM calls into one place. + +### Create `backend/integrations/llm/client.py` +```python +class MistralClient: + def complete(self, prompt, temperature=0, timeout=60) -> str: ... + def structured(self, prompt, schema) -> dict: ... # validates JSON output +``` + +### Migrate all callers: +- `llm_planner.py` → uses `MistralClient` +- `api_executor.py` → uses `MistralClient` +- `response_analyzer.py` → uses `MistralClient` +- `chat_assistant.py` → uses `MistralClient` + +**Rule:** No direct `ChatMistralAI` instantiation outside `client.py`. + +--- + +## 🔲 Phase 7 — Frontend Polish + +### 7.1 Error UI +- Replace `alert()` calls with toast notifications or inline error messages +- Add error boundary component to catch React crashes +- Show which step failed and why + +### 7.2 API Client Improvements (`services/api.js`) +- Add global axios error interceptor +- Centralize timeout and base URL + +### 7.3 Frontend Structure (optional) +``` +src/ +├── api/ # Move api.js here, split by domain +├── hooks/ # Custom hooks (useSpec, usePlan, useExecution) +└── store/ # AppContext split into smaller contexts +``` + +--- + +## 🔲 Phase 8 — Testing + +### Unit Tests +- `services/url_validator.py` — URL blocking, private IP detection +- `services/llm_planner.py` — mock LLM, test output parsing +- `services/api_executor.py` — test request building, context resolution + +### Integration Tests +- `POST /upload-spec` — valid OpenAPI file → correct parse +- `POST /plan` — returns valid step list +- `POST /execute-step` — mock HTTP target → correct history entry +- `POST /chat` — returns valid action JSON + +### Tools +- `pytest` + `pytest-asyncio` +- `httpx.AsyncClient` for route testing +- `respx` for mocking outbound HTTP + +--- + +## 🔲 Phase 9 — Deployment + +### 9.1 Docker +``` +Dockerfile # backend +docker-compose.yml # backend + postgres (local dev) +``` + +### 9.2 Environment Separation +- `.env.development` — local DB, debug logging +- `.env.production` — Supabase, structured JSON logs, strict CORS + +### 9.3 Health Check +- `GET /health` returns `{ "status": "ok", "db": "ok", "llm": "ok" }` + +--- + +## Progress Summary + +| Phase | Status | Notes | +|-------|--------|-------| +| 1 — Fix Foundations | ✅ Done | | +| 2 — Backend Restructure | ✅ Done | 36 routes across 6 files | +| 3 — Security | ✅ Done | SSRF protection, rate limiting, file size validation | +| 4 — Error Handling | ✅ Done | LLM retry, typed HTTP errors | +| 5 — Complete Features | ✅ Done | Run All, Retry, Export, OTP/Selection modals, token display | +| 6 — LLM Isolation | 🔲 Pending | | +| 7 — Frontend Polish | 🔲 Pending | Replace alerts with toasts, error boundary | +| 8 — Testing | 🔲 Pending | | +| 9 — Deployment | 🔲 Pending | Docker + health check | + + + + diff --git a/backend/app/__init__.py b/backend/app/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/backend/app/api/__init__.py b/backend/app/api/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/backend/app/api/routes/__init__.py b/backend/app/api/routes/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/backend/app/api/routes/assets.py b/backend/app/api/routes/assets.py new file mode 100644 index 0000000000000000000000000000000000000000..7066c6bc2d91e0907d8cc32c41a2324fd16f569a --- /dev/null +++ b/backend/app/api/routes/assets.py @@ -0,0 +1,109 @@ +import json +from typing import Optional + +from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile +from sqlalchemy import or_ +from sqlalchemy.orm import Session + +from backend import database +from backend.services.asset_service import AssetService + +router = APIRouter() + + +@router.post("/assets") +async def upload_asset( + spec_id: str = Form(None), + collection_id: str = Form(None), + description: str = Form(None), + tags: str = Form("[]"), + file: UploadFile = File(...), + db: Session = Depends(database.get_db), +): + try: + tag_list = json.loads(tags) + asset = AssetService.upload_asset(db, file, spec_id, collection_id, description, tag_list) + return { + "id": asset.id, + "name": asset.name, + "type": asset.file_type, + "size": asset.file_size, + "category": asset.category, + "description": asset.description, + "tags": asset.tags, + "spec_id": asset.spec_id, + "uploaded_at": asset.uploaded_at.isoformat(), + } + except Exception as e: + raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}") + + +@router.get("/assets") +def list_assets( + spec_id: Optional[str] = None, + category: Optional[str] = None, + db: Session = Depends(database.get_db), +): + assets = AssetService.get_assets(db, spec_id, category) + return [ + { + "id": a.id, + "name": a.name, + "type": a.file_type, + "size": a.file_size, + "category": a.category, + "description": a.description, + "tags": a.tags, + "spec_id": a.spec_id, + "path": a.file_path, + "uploaded_at": a.uploaded_at.isoformat(), + "collection_ids": [c.id for c in a.collections], + } + for a in assets + ] + + +@router.delete("/assets/{asset_id}") +def delete_asset(asset_id: str, db: Session = Depends(database.get_db)): + success = AssetService.delete_asset(db, asset_id) + if not success: + raise HTTPException(status_code=404, detail="Asset not found") + return {"status": "deleted", "id": asset_id} + + +@router.get("/collections") +def list_collections(spec_id: Optional[str] = None, db: Session = Depends(database.get_db)): + query = db.query(database.Collection) + if spec_id: + query = query.filter( + or_(database.Collection.spec_id == spec_id, database.Collection.spec_id == None) + ) + cols = query.all() + return [{"id": c.id, "name": c.name, "description": c.description, "spec_id": c.spec_id} for c in cols] + + +@router.post("/collections") +def create_collection( + name: str = Form(...), + spec_id: str = Form(None), + description: str = Form(None), + db: Session = Depends(database.get_db), +): + col = AssetService.create_collection(db, name, spec_id, description) + return {"id": col.id, "name": col.name, "spec_id": col.spec_id} + + +@router.post("/collections/{collection_id}/assets/{asset_id}") +def add_asset_to_collection(collection_id: str, asset_id: str, db: Session = Depends(database.get_db)): + success = AssetService.add_to_collection(db, asset_id, collection_id) + if not success: + raise HTTPException(status_code=404, detail="Asset or Collection not found") + return {"status": "success"} + + +@router.delete("/collections/{collection_id}") +def delete_collection(collection_id: str, db: Session = Depends(database.get_db)): + success = AssetService.delete_collection(db, collection_id) + if not success: + raise HTTPException(status_code=404, detail="Collection not found") + return {"status": "success"} diff --git a/backend/app/api/routes/chat.py b/backend/app/api/routes/chat.py new file mode 100644 index 0000000000000000000000000000000000000000..82cfcbb0f6810e915bd35bbad19f0f19b64af851 --- /dev/null +++ b/backend/app/api/routes/chat.py @@ -0,0 +1,183 @@ +import datetime +import uuid +from typing import Optional + +from fastapi import APIRouter, Depends, HTTPException, Request +from slowapi import Limiter +from slowapi.util import get_remote_address +from sqlalchemy.orm import Session + +from backend import database, schemas +from backend.services.chat_assistant import process_message as chat_process_message + +router = APIRouter() +limiter = Limiter(key_func=get_remote_address) + + +@router.post("/chat", response_model=schemas.ChatResponse) +@limiter.limit("20/minute") +def chat_endpoint(request: Request, body: schemas.ChatRequest, db: Session = Depends(database.get_db)): + try: + chat_session = None + if body.chat_session_id: + chat_session = db.query(database.ChatSession).filter( + database.ChatSession.id == body.chat_session_id + ).first() + + if not chat_session: + session_name = body.message[:60].strip() + if len(body.message) > 60: + session_name += "..." + chat_session = database.ChatSession( + id=str(uuid.uuid4()), + name=session_name, + spec_id=body.spec_id, + session_id=body.session_id, + ) + db.add(chat_session) + db.flush() + + user_msg = database.ChatMessage( + chat_session_id=chat_session.id, + role="user", + content=body.message, + ) + db.add(user_msg) + db.flush() + + history_rows = ( + db.query(database.ChatMessage) + .filter(database.ChatMessage.chat_session_id == chat_session.id) + .order_by(database.ChatMessage.id.asc()) + .all() + ) + chat_history = [ + {"role": m.role, "content": m.content} + for m in history_rows[:-1] + ] + + ai_result = chat_process_message( + message=body.message, + spec_name=body.spec_name, + execution_plan=body.execution_plan or [], + context_variables=body.context_variables or {}, + last_step_result=body.last_step_result, + all_step_results=body.all_step_results, + chat_history=chat_history, + ) + + assistant_content = ai_result.get("message", "") + db.add(database.ChatMessage( + chat_session_id=chat_session.id, + role="assistant", + content=assistant_content, + action_json=ai_result, + )) + + chat_session.updated_at = datetime.datetime.utcnow() + db.commit() + + return schemas.ChatResponse( + chat_session_id=chat_session.id, + chat_session_name=chat_session.name, + action=ai_result.get("action", "reply"), + message=assistant_content, + step=ai_result.get("step"), + changes=ai_result.get("changes"), + assertions=ai_result.get("assertions"), + explanation=ai_result.get("explanation"), + ) + except Exception as e: + import traceback + traceback.print_exc() + raise HTTPException(status_code=500, detail=f"Chat failed: {str(e)}") + + +@router.get("/chat-sessions") +def list_chat_sessions(spec_id: Optional[str] = None, db: Session = Depends(database.get_db)): + query = db.query(database.ChatSession) + if spec_id: + query = query.filter(database.ChatSession.spec_id == spec_id) + sessions = query.order_by(database.ChatSession.updated_at.desc()).all() + + return [ + { + "id": s.id, + "name": s.name, + "spec_id": s.spec_id, + "session_id": s.session_id, + "message_count": db.query(database.ChatMessage) + .filter(database.ChatMessage.chat_session_id == s.id) + .count(), + "created_at": s.created_at.isoformat() if s.created_at else None, + "updated_at": s.updated_at.isoformat() if s.updated_at else None, + } + for s in sessions + ] + + +@router.get("/chat-sessions/{chat_session_id}") +def get_chat_session(chat_session_id: str, db: Session = Depends(database.get_db)): + session = db.query(database.ChatSession).filter( + database.ChatSession.id == chat_session_id + ).first() + if not session: + raise HTTPException(status_code=404, detail="Chat session not found") + + messages = ( + db.query(database.ChatMessage) + .filter(database.ChatMessage.chat_session_id == chat_session_id) + .order_by(database.ChatMessage.id.asc()) + .all() + ) + return { + "id": session.id, + "name": session.name, + "spec_id": session.spec_id, + "session_id": session.session_id, + "created_at": session.created_at.isoformat() if session.created_at else None, + "updated_at": session.updated_at.isoformat() if session.updated_at else None, + "messages": [ + { + "id": m.id, + "role": m.role, + "content": m.content, + "action_json": m.action_json, + "timestamp": m.timestamp.isoformat() if m.timestamp else None, + } + for m in messages + ], + } + + +@router.patch("/chat-sessions/{chat_session_id}") +def rename_chat_session(chat_session_id: str, payload: dict, db: Session = Depends(database.get_db)): + session = db.query(database.ChatSession).filter( + database.ChatSession.id == chat_session_id + ).first() + if not session: + raise HTTPException(status_code=404, detail="Chat session not found") + if "name" in payload: + session.name = payload["name"] + db.commit() + return {"id": session.id, "name": session.name} + + +@router.delete("/chat-sessions/{chat_session_id}") +def delete_chat_session(chat_session_id: str, db: Session = Depends(database.get_db)): + session = db.query(database.ChatSession).filter( + database.ChatSession.id == chat_session_id + ).first() + if not session: + raise HTTPException(status_code=404, detail="Chat session not found") + db.delete(session) + db.commit() + return {"status": "deleted", "id": chat_session_id} + + +@router.delete("/chat-sessions") +def clear_all_chat_sessions(db: Session = Depends(database.get_db)): + db.query(database.ChatMessage).delete() + db.query(database.ChatSession).delete() + db.commit() + return {"status": "cleared"} diff --git a/backend/app/api/routes/execution.py b/backend/app/api/routes/execution.py new file mode 100644 index 0000000000000000000000000000000000000000..f92d74550c3ba0b05b57714ec6b3e3963b9937a6 --- /dev/null +++ b/backend/app/api/routes/execution.py @@ -0,0 +1,343 @@ +import json +import os +import re +import traceback +from urllib.parse import parse_qsl, urlparse + +import httpx +from fastapi import APIRouter, Depends, HTTPException, Request +from slowapi import Limiter +from slowapi.util import get_remote_address +from sqlalchemy.orm import Session +from sqlalchemy.orm.attributes import flag_modified + +from backend import database, schemas +from backend.core.logging import get_logger +from backend.core.url_guard import assert_safe_url +from backend.services.api_executor import prepare_step_request +from backend.services.asset_service import AssetService, storage +from backend.services.context_manager import set_context_variables +from backend.services.llm_planner import generate_execution_plan +from backend.services.response_analyzer import analyze_api_response + +router = APIRouter() +logger = get_logger(__name__) +limiter = Limiter(key_func=get_remote_address) + +PLACEHOLDER_HOSTS = {"api.example.com", "example.com", "", "your-api.com"} + + +def _resolve_refs(obj, full_spec, _resolving=None): + if _resolving is None: + _resolving = set() + if isinstance(obj, dict): + if "$ref" in obj: + ref = obj["$ref"] + if ref in _resolving: + return {"$ref_loop": ref} + _resolving = _resolving | {ref} + parts = ref.lstrip("#/").split("/") + target = full_spec + for part in parts: + target = target.get(part, {}) if isinstance(target, dict) else {} + return _resolve_refs(target, full_spec, _resolving) + return {k: _resolve_refs(v, full_spec, _resolving) for k, v in obj.items()} + elif isinstance(obj, list): + return [_resolve_refs(item, full_spec, _resolving) for item in obj] + return obj + + +def _shorthand_spec(obj): + if isinstance(obj, dict): + if "type" in obj: + t = obj["type"] + fmt = f":{obj['format']}" if "format" in obj else "" + enum_vals = f"[{'|'.join(map(str, obj['enum']))}]" if "enum" in obj else "" + if t == "object" and "properties" in obj: + req_list = obj.get("required", []) + props = [ + f"{k}{'*' if k in req_list or v.get('nullable') == False else ''}({_shorthand_spec(v)})" + for k, v in obj["properties"].items() + ] + return "{" + ",".join(props) + "}" + if t == "array" and "items" in obj: + return f"[{_shorthand_spec(obj['items'])}]" + return f"{t}{fmt}{enum_vals}" + return {k: _shorthand_spec(v) for k, v in obj.items()} + return str(obj) + + +def _prune_spec(obj): + DROP_KEYS = {"description", "summary", "example", "examples", "format", "title", "default", "enum_descriptions"} + if isinstance(obj, dict): + return {k: _prune_spec(v) for k, v in obj.items() if k not in DROP_KEYS} + elif isinstance(obj, list): + return [_prune_spec(item) for item in obj] + return obj + + +def _match_path(path_template: str, actual_path: str) -> bool: + pattern = re.sub(r'\{[^}]+\}', r'[^/]+', path_template) + pattern = "^" + pattern.rstrip("/") + "/?$" + return bool(re.match(pattern, actual_path.split("?")[0])) + + +def _is_placeholder(url: str) -> bool: + try: + host = urlparse(url).hostname or "" + return host.lower() in PLACEHOLDER_HOSTS or url.startswith("") + except Exception: + return False + + +@router.post("/plan", response_model=schemas.ExecutionPlan) +@limiter.limit("10/minute") +def generate_plan(request: Request, body: schemas.PlanRequest, db: Session = Depends(database.get_db)): + try: + parsed_spec = json.loads(body.spec_content) if isinstance(body.spec_content, str) else body.spec_content + + credentials = body.user_credentials or {} + if body.spec_id: + spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == body.spec_id).first() + if spec and spec.login_credentials: + credentials = spec.login_credentials + + plan = generate_execution_plan(parsed_spec, body.base_url, body.scenario, credentials) + + if body.spec_id: + spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == body.spec_id).first() + if spec: + spec.execution_plan = plan.model_dump()["execution_plan"] + spec.scenario = body.scenario + flag_modified(spec, "execution_plan") + flag_modified(spec, "scenario") + + if body.session_id: + session = db.query(database.SessionModel).filter( + database.SessionModel.id == body.session_id + ).first() + if session: + session.spec_id = spec.id + session.spec_name = spec.name + session.scenario = body.scenario + session.base_url = body.base_url + + if plan.suggested_credentials and isinstance(plan.suggested_credentials, dict): + current_creds = dict(spec.login_credentials or {}) + if any(k not in current_creds or current_creds[k] != v for k, v in plan.suggested_credentials.items()): + current_creds.update(plan.suggested_credentials) + spec.login_credentials = current_creds + flag_modified(spec, "login_credentials") + logger.info("AI suggested credentials merged for spec '%s'", spec.name) + + db.commit() + + return plan + except Exception as e: + raise HTTPException(status_code=500, detail=f"Planning failed: {str(e)}") + + +@router.post("/execute-step") +@limiter.limit("30/minute") +async def execute_step(request: Request, body: schemas.ExecuteStepRequest, db: Session = Depends(database.get_db)): + try: + endpoint_str = body.step_definition.get("endpoint", "") + actual_endpoint_spec = {"endpoint": endpoint_str} + + if body.spec_id: + spec_doc = db.query(database.SpecDocument).filter( + database.SpecDocument.id == body.spec_id + ).first() + if spec_doc and spec_doc.parsed_json: + full_spec = spec_doc.parsed_json + paths = full_spec.get("paths", {}) + parts = endpoint_str.split(" ", 1) + path_only = parts[1] if len(parts) == 2 else parts[0] + + matched_path = path_only if path_only in paths else next( + (p for p in paths if _match_path(p, path_only)), None + ) + + if matched_path: + raw_spec = {matched_path: paths[matched_path]} + resolved = _resolve_refs(raw_spec, full_spec) + path_data = resolved.get(matched_path, {}) + for method in path_data: + if isinstance(path_data[method], dict): + path_data[method].pop("responses", None) + actual_endpoint_spec = _shorthand_spec(_prune_spec(resolved)) + else: + logger.warning("Path match failed for %s, falling back", path_only) + actual_endpoint_spec = {"endpoint": endpoint_str, "warning": "Full schema match failed"} + + rich_assets = AssetService.get_ai_context(db, body.spec_id) + + effective_credentials = body.user_credentials + if not effective_credentials and body.spec_id: + spec = db.query(database.SpecDocument).filter( + database.SpecDocument.id == body.spec_id + ).first() + if spec and spec.login_credentials: + effective_credentials = spec.login_credentials + logger.info("Using stored credentials for spec '%s'", spec.name) + + req_plan, executor_token_usage = prepare_step_request( + endpoint_spec=actual_endpoint_spec, + base_url=body.base_url, + scenario="", + step_definition=body.step_definition, + context_variables=body.context_variables, + user_credentials=effective_credentials, + available_assets=rich_assets, + ) + + url = req_plan.request.url.strip() + if url.startswith("/"): + url = body.base_url.rstrip("/") + url + elif _is_placeholder(url): + parsed = urlparse(url) + path = parsed.path or "/" + url = body.base_url.rstrip("/") + path + if parsed.query: + url += "?" + parsed.query + logger.info("Placeholder URL replaced: %s", url) + elif not url.startswith("http"): + url = f"https://{url}" + + parsed = urlparse(url) + if parsed.query: + url_keys = {k for k, _ in parse_qsl(parsed.query)} + for k in [k for k in req_plan.request.query_params if k in url_keys]: + del req_plan.request.query_params[k] + + # Block SSRF — reject private/internal IPs before executing + assert_safe_url(url) + + async with httpx.AsyncClient(timeout=120.0) as client: + http_req = client.build_request( + method=req_plan.request.method.strip().upper(), + url=url, + headers=req_plan.request.headers, + params=req_plan.request.query_params, + data=req_plan.request.form_data if req_plan.request.form_data else None, + json=req_plan.request.body if req_plan.request.body and not req_plan.request.form_data else None, + ) + + files_payload = {} + opened_files = [] + try: + if req_plan.request.files: + req_plan.request.headers = { + k: v for k, v in req_plan.request.headers.items() if k.lower() != "content-type" + } + for field_name, file_name in req_plan.request.files.items(): + asset = db.query(database.Asset).filter(database.Asset.name == file_name).first() + if asset: + full_path = storage.get_full_path(asset.file_path) + if os.path.exists(full_path): + f = open(full_path, "rb") + opened_files.append(f) + files_payload[field_name] = (asset.name, f, asset.file_type) + else: + logger.warning("Asset file not found on disk: %s", full_path) + else: + logger.warning("Asset not found in Media Hub: %s", file_name) + + logger.info("Executing %s %s", http_req.method, http_req.url) + + try: + if files_payload: + response = await client.request( + method=req_plan.request.method.strip().upper(), + url=url, + headers=req_plan.request.headers, + params=req_plan.request.query_params, + data=req_plan.request.form_data if req_plan.request.form_data else None, + files=files_payload, + ) + else: + response = await client.send(http_req) + except httpx.TimeoutException: + raise HTTPException( + status_code=504, + detail=f"Request to {url} timed out after {120}s. Check the API server is reachable.", + ) + except httpx.ConnectError as e: + raise HTTPException( + status_code=502, + detail=f"Could not connect to {url}: {str(e)}", + ) + except Exception as e: + raise HTTPException(status_code=500, detail=f"Request to {url} failed: {str(e)}") + finally: + for f in opened_files: + f.close() + + body_text = response.text + resp_data = { + "status_code": response.status_code, + "body": body_text, + "headers": dict(response.headers), + } + + # Log a clear error line for 4xx/5xx so it's visible in the backend console + if response.status_code >= 400: + snippet = body_text[:300].replace("\n", " ") + logger.warning( + "Step %s returned %s from %s: %s", + req_plan.step, response.status_code, url, snippet, + ) + + req_plan.request.url = url + + db.add(database.ExecutionHistory( + session_id=body.session_id, + step=req_plan.step, + request_json=req_plan.request.model_dump(), + response_json=resp_data, + step_definition_json=body.step_definition, + )) + db.commit() + + return { + "executed_request": req_plan.request.model_dump(), + "response": resp_data, + "analysis_needed": req_plan.step, + "token_usage": executor_token_usage, + } + except Exception as e: + logger.error("Execution error: %s", traceback.format_exc()) + raise HTTPException(status_code=500, detail=f"Execution failed: {str(e)}") + + +@router.post("/analyze-response", response_model=schemas.AnalyzeResponse) +def analyze_response_endpoint(request: schemas.AnalyzeRequest, db: Session = Depends(database.get_db)): + try: + step_def = request.step_definition or {} + analysis = analyze_api_response( + step=request.step, + endpoint=step_def.get("endpoint", "Unknown"), + purpose=step_def.get("purpose", "Unknown"), + status_code=request.response_data.get("status_code", 0), + response_body=request.response_data.get("body", ""), + ) + + if analysis.extracted_variables: + set_context_variables(db, request.session_id, analysis.extracted_variables) + + history = ( + db.query(database.ExecutionHistory) + .filter( + database.ExecutionHistory.session_id == request.session_id, + database.ExecutionHistory.step == request.step, + ) + .order_by(database.ExecutionHistory.id.desc()) + .first() + ) + if history: + history.analysis_json = analysis.model_dump() + db.commit() + + return analysis + except Exception as e: + raise HTTPException(status_code=500, detail=f"Analysis failed: {str(e)}") diff --git a/backend/app/api/routes/history.py b/backend/app/api/routes/history.py new file mode 100644 index 0000000000000000000000000000000000000000..510f67d5ee0842693133b92948af2a4554fb5cb5 --- /dev/null +++ b/backend/app/api/routes/history.py @@ -0,0 +1,93 @@ +from fastapi import APIRouter, Depends, HTTPException +from fastapi.responses import JSONResponse +from sqlalchemy.orm import Session + +from backend import database + +router = APIRouter() + + +@router.get("/history/{session_id}/export") +def export_history_session(session_id: str, db: Session = Depends(database.get_db)): + sess = db.query(database.SessionModel).filter(database.SessionModel.id == session_id).first() + if not sess: + raise HTTPException(status_code=404, detail="Session not found") + + steps = ( + db.query(database.ExecutionHistory) + .filter(database.ExecutionHistory.session_id == session_id) + .order_by(database.ExecutionHistory.step) + .all() + ) + + report = { + "session_id": session_id, + "spec_name": sess.spec_name or "Unknown", + "scenario": sess.scenario or "", + "base_url": sess.base_url or "", + "context_variables": sess.context_variables or {}, + "steps": [ + { + "step": h.step, + "step_definition": h.step_definition_json, + "request": h.request_json, + "response": h.response_json, + "analysis": h.analysis_json, + "timestamp": h.timestamp.isoformat(), + } + for h in steps + ], + "total_steps": len(steps), + "passed": sum(1 for h in steps if (h.analysis_json or {}).get("status") == "success"), + "failed": sum(1 for h in steps if (h.analysis_json or {}).get("status") != "success"), + } + + return JSONResponse( + content=report, + headers={"Content-Disposition": f'attachment; filename="session_{session_id[:8]}.json"'}, + ) + + +@router.get("/history") +def get_all_history(db: Session = Depends(database.get_db)): + results = ( + db.query(database.ExecutionHistory, database.SessionModel) + .join(database.SessionModel, database.ExecutionHistory.session_id == database.SessionModel.id) + .order_by(database.ExecutionHistory.timestamp.desc()) + .all() + ) + return [ + { + "id": hist.id, + "session_id": hist.session_id, + "spec_id": sess.spec_id, + "spec_name": sess.spec_name or "Unknown", + "scenario": sess.scenario or "No scenario", + "context_variables": sess.context_variables or {}, + "step": hist.step, + "request": hist.request_json, + "response": hist.response_json, + "analysis": hist.analysis_json, + "step_definition": hist.step_definition_json, + "timestamp": hist.timestamp.isoformat(), + } + for hist, sess in results + ] + + +@router.delete("/history/{session_id}") +def delete_history_session(session_id: str, db: Session = Depends(database.get_db)): + db.query(database.ExecutionHistory).filter( + database.ExecutionHistory.session_id == session_id + ).delete() + db.query(database.SessionModel).filter(database.SessionModel.id == session_id).delete() + db.commit() + return {"status": "deleted", "session_id": session_id} + + +@router.delete("/history") +def clear_all_history(db: Session = Depends(database.get_db)): + db.query(database.ExecutionHistory).delete() + db.query(database.SessionModel).delete() + db.commit() + return {"status": "cleared"} diff --git a/backend/app/api/routes/sessions.py b/backend/app/api/routes/sessions.py new file mode 100644 index 0000000000000000000000000000000000000000..25ce1e50199b3b92626c370b9ce3b167c7705799 --- /dev/null +++ b/backend/app/api/routes/sessions.py @@ -0,0 +1,45 @@ +import uuid + +from fastapi import APIRouter, Depends +from sqlalchemy.orm import Session + +from backend import database, schemas +from backend.services.context_manager import get_context, set_context_variables + +router = APIRouter() + + +@router.post("/session") +def create_session(db: Session = Depends(database.get_db)): + session_id = str(uuid.uuid4()) + new_session = database.SessionModel(id=session_id) + db.add(new_session) + db.commit() + return {"session_id": session_id} + + +@router.delete("/session/{session_id}") +def clear_session(session_id: str, db: Session = Depends(database.get_db)): + session = db.query(database.SessionModel).filter(database.SessionModel.id == session_id).first() + if session: + db.delete(session) + db.query(database.ExecutionHistory).filter( + database.ExecutionHistory.session_id == session_id + ).delete() + db.commit() + return {"status": "ok"} + + +@router.get("/context/{session_id}") +def get_context_endpoint(session_id: str, db: Session = Depends(database.get_db)): + return get_context(db, session_id) + + +@router.post("/context/{session_id}") +def update_context_endpoint( + session_id: str, + payload: schemas.ContextUpdate, + db: Session = Depends(database.get_db), +): + variables = {payload.key: payload.value} + return set_context_variables(db, session_id, variables) diff --git a/backend/app/api/routes/specs.py b/backend/app/api/routes/specs.py new file mode 100644 index 0000000000000000000000000000000000000000..acbbf31c787f7b14f685a58452af6e870ad4ef9d --- /dev/null +++ b/backend/app/api/routes/specs.py @@ -0,0 +1,163 @@ +import json +import uuid +from typing import Optional + +from fastapi import APIRouter, Depends, File, HTTPException, UploadFile +from sqlalchemy.orm import Session +from sqlalchemy.orm.attributes import flag_modified + +from backend import database, schemas +from backend.core.logging import get_logger +from backend.services.swagger_parser import parse_spec +from backend.services.postman_parser import parse_postman_collection as parse_postman +from backend.services.asset_service import AssetService, storage + +router = APIRouter() +logger = get_logger(__name__) + + +@router.post("/upload-spec") +async def upload_spec(file: UploadFile = File(...), db: Session = Depends(database.get_db)): + try: + content = await file.read() + if len(content) > 5 * 1024 * 1024: + raise HTTPException(status_code=400, detail="Spec file exceeds 5 MB limit.") + decoded_content = content.decode("utf-8", errors="replace") + + is_postman = False + try: + temp_json = json.loads(decoded_content) + if isinstance(temp_json, dict): + info = temp_json.get("info", {}) + if isinstance(info, dict) and "postman.com/json/collection" in info.get("schema", ""): + is_postman = True + elif "info" in temp_json and "item" in temp_json: + if "openapi" not in temp_json and "swagger" not in temp_json: + is_postman = True + except Exception: + pass + + parsed_spec = parse_postman(decoded_content) if is_postman else parse_spec(decoded_content) + safe_spec = json.loads(json.dumps(parsed_spec, ensure_ascii=True)) + + spec_id = str(uuid.uuid4()) + file_path = f"specs/{spec_id}_{file.filename}" + + logger.info("Uploading %s to %s", file.filename, file_path) + await file.seek(0) + storage.save(file.file, file_path) + + spec_doc = database.SpecDocument( + id=spec_id, + name=file.filename, + raw_content=decoded_content, + parsed_json=safe_spec, + file_path=file_path, + ) + db.add(spec_doc) + db.commit() + + safe_spec["id"] = spec_id + return {"status": "success", "spec_id": spec_id, "parsed_spec": safe_spec} + except Exception as e: + import traceback + traceback.print_exc() + raise HTTPException(status_code=400, detail=f"Failed to parse spec: {str(e)}") + + +@router.get("/specs") +def list_specs(db: Session = Depends(database.get_db)): + specs = db.query(database.SpecDocument).order_by(database.SpecDocument.uploaded_at.desc()).all() + return [ + {"id": s.id, "name": s.name, "base_url": s.base_url or "", "uploaded_at": s.uploaded_at.isoformat()} + for s in specs + ] + + +@router.get("/specs/{spec_id}") +def get_spec(spec_id: str, db: Session = Depends(database.get_db)): + spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == spec_id).first() + if not spec: + raise HTTPException(status_code=404, detail="Spec not found") + return { + "id": spec.id, + "name": spec.name, + "base_url": spec.base_url or "", + "login_credentials": spec.login_credentials or {}, + "parsed_spec": spec.parsed_json, + "execution_plan": spec.execution_plan, + "scenario": spec.scenario, + "uploaded_at": spec.uploaded_at.isoformat(), + } + + +@router.patch("/specs/{spec_id}") +def update_spec(spec_id: str, payload: dict, db: Session = Depends(database.get_db)): + spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == spec_id).first() + if not spec: + raise HTTPException(status_code=404, detail="Spec not found") + if "base_url" in payload: + spec.base_url = payload["base_url"] + db.commit() + return {"status": "updated", "id": spec.id, "base_url": spec.base_url} + + +@router.delete("/specs/{spec_id}") +def delete_spec(spec_id: str, db: Session = Depends(database.get_db)): + spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == spec_id).first() + if not spec: + raise HTTPException(status_code=404, detail="Spec not found") + + assets = db.query(database.Asset).filter(database.Asset.spec_id == spec_id).all() + for asset in assets: + AssetService.delete_asset(db, asset.id) + + if spec.file_path: + try: + storage.delete(spec.file_path) + except Exception as e: + logger.warning("Failed to delete spec file %s: %s", spec.file_path, e) + + db.delete(spec) + db.commit() + return {"status": "deleted", "id": spec_id, "assets_removed": len(assets)} + + +@router.get("/specs/{spec_id}/credentials") +def get_credentials(spec_id: str, db: Session = Depends(database.get_db)): + spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == spec_id).first() + if not spec: + raise HTTPException(status_code=404, detail="Spec not found") + return spec.login_credentials or {} + + +@router.post("/specs/{spec_id}/credentials") +def save_credentials(spec_id: str, payload: schemas.CredentialUpdate, db: Session = Depends(database.get_db)): + spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == spec_id).first() + if not spec: + raise HTTPException(status_code=404, detail="Spec not found") + + if payload.extra and payload.extra.get("_clear"): + spec.login_credentials = {} + flag_modified(spec, "login_credentials") + db.commit() + db.refresh(spec) + return {"status": "cleared", "credentials": {}} + + current: dict = dict(spec.login_credentials or {}) + if payload.name is not None: + current["name"] = payload.name + if payload.username is not None: + current["username"] = payload.username + current["email"] = payload.username + if payload.password is not None: + current["password"] = payload.password + if payload.extra: + extra_clean = {k: v for k, v in payload.extra.items() if not k.startswith("_")} + current.update(extra_clean) + + spec.login_credentials = current + flag_modified(spec, "login_credentials") + db.commit() + db.refresh(spec) + return {"status": "saved", "credentials": spec.login_credentials} diff --git a/backend/core/__init__.py b/backend/core/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/backend/core/config.py b/backend/core/config.py new file mode 100644 index 0000000000000000000000000000000000000000..179600eedf49069c54ee0c00cc8176c0e01ae372 --- /dev/null +++ b/backend/core/config.py @@ -0,0 +1,19 @@ +from pydantic_settings import BaseSettings + + +class Settings(BaseSettings): + MISTRAL_API_KEY: str = "" + DATABASE_URL: str = "" + ALLOWED_ORIGINS: str = "http://localhost:5173,http://localhost:3000" + UPLOAD_DIR: str = "backend/uploads" + LLM_TIMEOUT: int = 60 + HTTP_TIMEOUT: int = 120 + + model_config = {"env_file": ".env", "extra": "ignore"} + + @property + def allowed_origins_list(self) -> list[str]: + return [o.strip() for o in self.ALLOWED_ORIGINS.split(",") if o.strip()] + + +settings = Settings() diff --git a/backend/core/errors.py b/backend/core/errors.py new file mode 100644 index 0000000000000000000000000000000000000000..4d261dab574fdad99c74a1e62b686b097f59400c --- /dev/null +++ b/backend/core/errors.py @@ -0,0 +1,19 @@ +from fastapi import Request +from fastapi.responses import JSONResponse + + +class AppError(Exception): + """Raise this anywhere in route/service code for a clean, consistent error response.""" + def __init__(self, message: str, code: str = "ERROR", status: int = 400, detail: dict = None): + self.message = message + self.code = code + self.status = status + self.detail = detail or {} + super().__init__(message) + + +async def app_error_handler(request: Request, exc: AppError) -> JSONResponse: + return JSONResponse( + status_code=exc.status, + content={"error": exc.message, "code": exc.code, "detail": exc.detail}, + ) diff --git a/backend/core/logging.py b/backend/core/logging.py new file mode 100644 index 0000000000000000000000000000000000000000..fa291a9d50c4cd48fe7082161fb365a4b917c79d --- /dev/null +++ b/backend/core/logging.py @@ -0,0 +1,15 @@ +import logging +import sys + + +def get_logger(name: str) -> logging.Logger: + logger = logging.getLogger(name) + if not logger.handlers: + handler = logging.StreamHandler(sys.stdout) + handler.setFormatter(logging.Formatter( + "%(asctime)s [%(levelname)s] %(name)s: %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", + )) + logger.addHandler(handler) + logger.setLevel(logging.INFO) + return logger diff --git a/backend/core/url_guard.py b/backend/core/url_guard.py new file mode 100644 index 0000000000000000000000000000000000000000..f249a227b19bc149eda2a98be6d3eda7cef848b0 --- /dev/null +++ b/backend/core/url_guard.py @@ -0,0 +1,50 @@ +import ipaddress +import socket +from urllib.parse import urlparse + +from fastapi import HTTPException + +_BLOCKED_NETS = [ + ipaddress.ip_network("127.0.0.0/8"), + ipaddress.ip_network("10.0.0.0/8"), + ipaddress.ip_network("172.16.0.0/12"), + ipaddress.ip_network("192.168.0.0/16"), + ipaddress.ip_network("0.0.0.0/8"), + ipaddress.ip_network("169.254.0.0/16"), # AWS/GCP metadata + ipaddress.ip_network("::1/128"), + ipaddress.ip_network("fc00::/7"), +] + +_ALLOWED_SCHEMES = {"http", "https"} + + +def assert_safe_url(url: str) -> None: + """Raise HTTP 400 if the URL targets a private/internal address or disallowed scheme.""" + try: + parsed = urlparse(url) + except Exception: + raise HTTPException(status_code=400, detail=f"Malformed URL: {url}") + + if parsed.scheme not in _ALLOWED_SCHEMES: + raise HTTPException( + status_code=400, + detail=f"URL scheme '{parsed.scheme}' is not allowed. Only http/https are permitted.", + ) + + hostname = parsed.hostname + if not hostname: + raise HTTPException(status_code=400, detail="URL has no hostname.") + + # Resolve to IP and check against blocked ranges + try: + ip_str = socket.getaddrinfo(hostname, None)[0][4][0] + ip = ipaddress.ip_address(ip_str) + except Exception: + raise HTTPException(status_code=400, detail=f"Could not resolve hostname: {hostname}") + + for net in _BLOCKED_NETS: + if ip in net: + raise HTTPException( + status_code=400, + detail=f"Requests to private/internal addresses are not allowed (resolved to {ip}).", + ) diff --git a/backend/integrations/__init__.py b/backend/integrations/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/backend/integrations/llm/__init__.py b/backend/integrations/llm/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/backend/integrations/llm/client.py b/backend/integrations/llm/client.py new file mode 100644 index 0000000000000000000000000000000000000000..3e80ddc7e9e839ad9b1b1bb9fecabbc329a1b1b2 --- /dev/null +++ b/backend/integrations/llm/client.py @@ -0,0 +1,34 @@ +from langchain_mistralai.chat_models import ChatMistralAI +from backend.core.logging import get_logger + +logger = get_logger(__name__) + +_MODEL = "mistral-large-latest" + + +def _extract_usage(ai_msg) -> dict | None: + usage = ai_msg.response_metadata.get("token_usage", {}) + if not usage: + return None + return { + "prompt_tokens": usage.get("prompt_tokens", 0), + "completion_tokens": usage.get("completion_tokens", 0), + "total_tokens": usage.get("total_tokens", 0), + } + + +class MistralClient: + """Centralized Mistral LLM client. All services use the module-level instances below.""" + + def __init__(self, temperature: float = 0, timeout: int = 120): + self._llm = ChatMistralAI(model_name=_MODEL, temperature=temperature, timeout=timeout) + + def invoke(self, messages) -> tuple[str, dict | None]: + """Invoke the LLM. Returns (content_str, token_usage_or_None).""" + ai_msg = self._llm.invoke(messages) + return ai_msg.content, _extract_usage(ai_msg) + + +# One instance per temperature setting — instantiated once per process, not per request. +deterministic = MistralClient(temperature=0) +chat = MistralClient(temperature=0.2) diff --git a/backend/main.py b/backend/main.py index 220ddd4a7b7f85442e309475d63c1c41ba567e62..112edf5af74eafa316c87495f9b84151cda39d65 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,935 +1,65 @@ -from fastapi import FastAPI, Depends, HTTPException, File, UploadFile, Form -from fastapi.staticfiles import StaticFiles -from sqlalchemy.orm import Session -from sqlalchemy.orm.attributes import flag_modified -from . import database, schemas -from typing import Dict, Any, Optional -import httpx -import json -import uuid import os -import shutil -from fastapi.middleware.cors import CORSMiddleware + +from fastapi import FastAPI, Request from fastapi.exceptions import RequestValidationError +from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse +from fastapi.staticfiles import StaticFiles +from slowapi import Limiter, _rate_limit_exceeded_handler +from slowapi.errors import RateLimitExceeded +from slowapi.util import get_remote_address -from backend.services.swagger_parser import parse_spec -from backend.services.postman_parser import parse_postman_collection as parse_postman -from backend.services.llm_planner import generate_execution_plan -from backend.services.api_executor import prepare_step_request -from backend.services.response_analyzer import analyze_api_response -from backend.services.asset_service import storage -from backend.services.context_manager import get_context, set_context_variables -from backend.services.asset_service import AssetService -from backend.services.chat_assistant import process_message as chat_process_message +from backend import database +from backend.core.config import settings +from backend.core.errors import AppError, app_error_handler +from backend.core.logging import get_logger +from backend.app.api.routes import specs, assets, sessions, history, chat, execution +logger = get_logger(__name__) -# Ensure uploads subdirectories exist -UPLOAD_DIR = "backend/uploads" -CATEGORIES = ["audio", "video", "documents", "images"] -for cat in CATEGORIES: - os.makedirs(os.path.join(UPLOAD_DIR, cat), exist_ok=True) +# Rate limiter — keyed by client IP +limiter = Limiter(key_func=get_remote_address, default_limits=["200/minute"]) -app = FastAPI(title="AI API Testing Assistant") +# Ensure upload directories exist +for category in ["audio", "video", "documents", "images", "specs"]: + os.makedirs(os.path.join(settings.UPLOAD_DIR, category), exist_ok=True) -# Mount static files for uploads -app.mount("/uploads", StaticFiles(directory=UPLOAD_DIR), name="uploads") +# Create DB tables +database.Base.metadata.create_all(bind=database.engine) -@app.exception_handler(RequestValidationError) -async def validation_exception_handler(request, exc): - print(f"VALIDATION ERROR: {exc.errors()}") - print(f"BODY: {await request.body()}") - return JSONResponse( - status_code=422, - content={"detail": exc.errors(), "body": str(await request.body())}, - ) +app = FastAPI(title="AI API Testing Assistant") +app.state.limiter = limiter +app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) +app.add_exception_handler(AppError, app_error_handler) -# Configure CORS +# Static file serving for uploads +app.mount("/uploads", StaticFiles(directory=settings.UPLOAD_DIR), name="uploads") + +# CORS app.add_middleware( CORSMiddleware, - allow_origins=["*"], # Allow all origins for production ease + allow_origins=settings.allowed_origins_list, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) -@app.get("/") -def read_root(): - return {"status": "ok", "message": "AI API Testing Tool Backend running."} - -@app.post("/session") -def create_session(db: Session = Depends(database.get_db)): - session_id = str(uuid.uuid4()) - new_session = database.SessionModel(id=session_id) - db.add(new_session) - db.commit() - return {"session_id": session_id} - -@app.post("/upload-spec") -async def upload_spec(file: UploadFile = File(...), db: Session = Depends(database.get_db)): - try: - content = await file.read() - decoded_content = content.decode("utf-8", errors="replace") - - # Detect if it's a Postman collection - is_postman = False - try: - temp_json = json.loads(decoded_content) - if isinstance(temp_json, dict): - # Primary check: schema URL - info = temp_json.get("info", {}) - if isinstance(info, dict) and "postman.com/json/collection" in info.get("schema", ""): - is_postman = True - # Secondary check: unique structure (info + item) - elif "info" in temp_json and "item" in temp_json: - # Swagger/OpenAPI usually has 'openapi' or 'swagger' key at top level - if "openapi" not in temp_json and "swagger" not in temp_json: - is_postman = True - except: - pass - - if is_postman: - parsed_spec = parse_postman(decoded_content) - else: - parsed_spec = parse_spec(decoded_content) - - safe_spec = json.loads(json.dumps(parsed_spec, ensure_ascii=True)) - - spec_id = str(uuid.uuid4()) - # PERSIST to Storage (Supabase or Local) - file_path = f"specs/{spec_id}_{file.filename}" - - # Log for debugging - print(f"[DEBUG] Uploading {file.filename} to {file_path}") - - # Reset cursor since we read it for parsing - await file.seek(0) - storage.save(file.file, file_path) - - # Persist the spec to the database - spec_doc = database.SpecDocument( - id=spec_id, - name=file.filename, - raw_content=decoded_content, - parsed_json=safe_spec, - file_path=file_path - ) - db.add(spec_doc) - db.commit() - - # Prepare return object including the ID for the frontend - safe_spec["id"] = spec_id - - return {"status": "success", "spec_id": spec_id, "parsed_spec": safe_spec} - except Exception as e: - print(f"[ERROR] upload_spec failed: {str(e)}") - import traceback - traceback.print_exc() - raise HTTPException(status_code=400, detail=str(e)) - except Exception as e: - raise HTTPException(status_code=400, detail=f"Failed to parse spec: {str(e)}") - -@app.get("/specs") -def list_specs(db: Session = Depends(database.get_db)): - specs = db.query(database.SpecDocument).order_by(database.SpecDocument.uploaded_at.desc()).all() - return [ - {"id": s.id, "name": s.name, "base_url": s.base_url or "", "uploaded_at": s.uploaded_at.isoformat()} - for s in specs - ] - -@app.get("/specs/{spec_id}") -def get_spec(spec_id: str, db: Session = Depends(database.get_db)): - spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == spec_id).first() - if not spec: - raise HTTPException(status_code=404, detail="Spec not found") - return { - "id": spec.id, - "name": spec.name, - "base_url": spec.base_url or "", - "login_credentials": spec.login_credentials or {}, - "parsed_spec": spec.parsed_json, - "execution_plan": spec.execution_plan, - "scenario": spec.scenario, - "uploaded_at": spec.uploaded_at.isoformat() - } - -@app.patch("/specs/{spec_id}") -def update_spec(spec_id: str, payload: dict, db: Session = Depends(database.get_db)): - spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == spec_id).first() - if not spec: - raise HTTPException(status_code=404, detail="Spec not found") - if "base_url" in payload: - spec.base_url = payload["base_url"] - db.commit() - return {"status": "updated", "id": spec.id, "base_url": spec.base_url} - - -def get_category(filename: str, content_type: str) -> str: - ext = os.path.splitext(filename)[1].lower() - if ext in ['.mp3', '.wav', '.ogg', '.m4a'] or (content_type and content_type.startswith('audio/')): - return 'audio' - if ext in ['.mp4', '.mov', '.avi', '.mkv'] or (content_type and content_type.startswith('video/')): - return 'video' - return 'documents' - -@app.post("/assets") -async def upload_asset( - spec_id: str = Form(None), - collection_id: str = Form(None), - description: str = Form(None), - tags: str = Form("[]"), - file: UploadFile = File(...), - db: Session = Depends(database.get_db) -): - try: - tag_list = json.loads(tags) - asset = AssetService.upload_asset(db, file, spec_id, collection_id, description, tag_list) - return { - "id": asset.id, - "name": asset.name, - "type": asset.file_type, - "size": asset.file_size, - "category": asset.category, - "description": asset.description, - "tags": asset.tags, - "spec_id": asset.spec_id, - "uploaded_at": asset.uploaded_at.isoformat() - } - except Exception as e: - raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}") - -@app.get("/assets") -def list_assets(spec_id: Optional[str] = None, category: Optional[str] = None, db: Session = Depends(database.get_db)): - assets = AssetService.get_assets(db, spec_id, category) - return [ - { - "id": a.id, - "name": a.name, - "type": a.file_type, - "size": a.file_size, - "category": a.category, - "description": a.description, - "tags": a.tags, - "spec_id": a.spec_id, - "path": a.file_path, - "uploaded_at": a.uploaded_at.isoformat(), - "collection_ids": [c.id for c in a.collections] - } - for a in assets - ] - -@app.delete("/assets/{asset_id}") -def delete_asset(asset_id: str, db: Session = Depends(database.get_db)): - success = AssetService.delete_asset(db, asset_id) - if not success: - raise HTTPException(status_code=404, detail="Asset not found") - return {"status": "deleted", "id": asset_id} - -@app.get("/collections") -def list_collections(spec_id: Optional[str] = None, db: Session = Depends(database.get_db)): - query = db.query(database.Collection) - if spec_id: - from sqlalchemy import or_ - query = query.filter(or_(database.Collection.spec_id == spec_id, database.Collection.spec_id == None)) - cols = query.all() - return [{"id": c.id, "name": c.name, "description": c.description, "spec_id": c.spec_id} for c in cols] - -@app.post("/collections") -def create_collection(name: str = Form(...), spec_id: str = Form(None), description: str = Form(None), db: Session = Depends(database.get_db)): - col = AssetService.create_collection(db, name, spec_id, description) - return {"id": col.id, "name": col.name, "spec_id": col.spec_id} - -@app.post("/collections/{collection_id}/assets/{asset_id}") -def add_asset_to_collection(collection_id: str, asset_id: str, db: Session = Depends(database.get_db)): - success = AssetService.add_to_collection(db, asset_id, collection_id) - if not success: - raise HTTPException(status_code=404, detail="Asset or Collection not found") - return {"status": "success"} - -@app.delete("/collections/{collection_id}") -def delete_collection(collection_id: str, db: Session = Depends(database.get_db)): - success = AssetService.delete_collection(db, collection_id) - if not success: - raise HTTPException(status_code=404, detail="Collection not found") - return {"status": "success"} - -@app.delete("/specs/{spec_id}") -def delete_spec(spec_id: str, db: Session = Depends(database.get_db)): - spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == spec_id).first() - if not spec: - raise HTTPException(status_code=404, detail="Spec not found") - - # Delete all associated assets (storage + DB) - assets = db.query(database.Asset).filter(database.Asset.spec_id == spec_id).all() - for asset in assets: - AssetService.delete_asset(db, asset.id) - - # Delete spec file from storage - if spec.file_path: - try: - storage.delete(spec.file_path) - except Exception as e: - print(f"[CLEANUP ERROR] Failed to delete spec file {spec.file_path}: {e}") - - db.delete(spec) - db.commit() - return {"status": "deleted", "id": spec_id, "assets_removed": len(assets)} - -@app.post("/plan", response_model=schemas.ExecutionPlan) -def generate_plan(request: schemas.PlanRequest, db: Session = Depends(database.get_db)): - try: - # Expected `request.spec_content` to be the dict from `/upload-spec` - parsed_spec = json.loads(request.spec_content) if isinstance(request.spec_content, str) else request.spec_content - - # Override with DB-stored credentials if a spec_id is provided - credentials = request.user_credentials or {} - if request.spec_id: - spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == request.spec_id).first() - if spec and spec.login_credentials: - credentials = spec.login_credentials - - plan = generate_execution_plan( - parsed_spec, - request.base_url, - request.scenario, - credentials - ) - - # PERSIST: Save the generated plan and update session metadata - if request.spec_id: - spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == request.spec_id).first() - if spec: - spec.execution_plan = plan.model_dump()["execution_plan"] - spec.scenario = request.scenario - flag_modified(spec, "execution_plan") - flag_modified(spec, "scenario") - - # Update session metadata if session_id provided - if request.session_id: - session = db.query(database.SessionModel).filter(database.SessionModel.id == request.session_id).first() - if session: - session.spec_id = spec.id - session.spec_name = spec.name - session.scenario = request.scenario - session.base_url = request.base_url - - # NEW: Merge AI-generated credentials if present - if plan.suggested_credentials and isinstance(plan.suggested_credentials, dict): - current_creds = dict(spec.login_credentials or {}) - # Only merge if there's actually something new to add - if any(k not in current_creds or current_creds[k] != v for k, v in plan.suggested_credentials.items()): - current_creds.update(plan.suggested_credentials) - spec.login_credentials = current_creds - flag_modified(spec, "login_credentials") - print(f"[AUTH MERGE] AI suggested credentials for '{spec.name}' merged into DB.") - - db.commit() - - return plan - except Exception as e: - raise HTTPException(status_code=500, detail=f"Planning failed: {str(e)}") - -@app.post("/execute-step") -async def execute_step(request: schemas.ExecuteStepRequest, db: Session = Depends(database.get_db)): - try: - # Extract endpoint string (e.g., "GET /buses/{bus_id}" or "/buses/{bus_id}") - endpoint_str = request.step_definition.get("endpoint", "") - - def resolve_refs(obj, full_spec, _resolving=None): - """Recursively inline all $ref pointers so the LLM gets a flat, self-contained spec.""" - if _resolving is None: - _resolving = set() - if isinstance(obj, dict): - if "$ref" in obj: - ref = obj["$ref"] - if ref in _resolving: - return {"$ref_loop": ref} - _resolving = _resolving | {ref} - # Only support local refs: "#/components/schemas/Foo" - parts = ref.lstrip("#/").split("/") - target = full_spec - for part in parts: - target = target.get(part, {}) if isinstance(target, dict) else {} - return resolve_refs(target, full_spec, _resolving) - return {k: resolve_refs(v, full_spec, _resolving) for k, v in obj.items()} - elif isinstance(obj, list): - return [resolve_refs(item, full_spec, _resolving) for item in obj] - return obj - - def shorthand_spec(obj): - """Convert JSON schema to ultra-compact shorthand string including enums and formats.""" - if isinstance(obj, dict): - if "type" in obj: - t = obj["type"] - fmt = f":{obj['format']}" if "format" in obj else "" - enum_vals = f"[{'|'.join(map(str, obj['enum']))}]" if "enum" in obj else "" - - if t == "object" and "properties" in obj: - req_list = obj.get("required", []) - props = [] - for k, v in obj["properties"].items(): - req = "*" if k in req_list or v.get("nullable") == False else "" - props.append(f"{k}{req}({shorthand_spec(v)})") - return "{" + ",".join(props) + "}" - if t == "array" and "items" in obj: - return f"[{shorthand_spec(obj['items'])}]" - - return f"{t}{fmt}{enum_vals}" - return {k: shorthand_spec(v) for k, v in obj.items()} - return str(obj) - - def prune_spec_for_execution(obj): - """Recursively remove non-functional metadata (descriptions, examples, formats) to save tokens.""" - if isinstance(obj, dict): - # Keys we definitely don't need for building an HTTP request - DROP_KEYS = {"description", "summary", "example", "examples", "format", "title", "default", "enum_descriptions"} - pruned = {} - for k, v in obj.items(): - if k in DROP_KEYS: - continue - pruned[k] = prune_spec_for_execution(v) - return pruned - elif isinstance(obj, list): - return [prune_spec_for_execution(item) for item in obj] - return obj - - actual_endpoint_spec = {"endpoint": endpoint_str} - - if request.spec_id: - spec_doc = db.query(database.SpecDocument).filter(database.SpecDocument.id == request.spec_id).first() - if spec_doc and spec_doc.parsed_json: - full_spec = spec_doc.parsed_json - paths = full_spec.get("paths", {}) - parts = endpoint_str.split(" ", 1) - def _match_path(path_template, actual_path): - """Simple matcher for OpenAPI path templates like /buses/{bus_id}""" - import re - # Replace {param} with a regex group - pattern = re.sub(r'\{[^}]+\}', r'[^/]+', path_template) - # Handle trailing slashes - pattern = "^" + pattern.rstrip("/") + "/?$" - return bool(re.match(pattern, actual_path.split("?")[0])) - - parts = endpoint_str.split(" ", 1) - path_only = parts[1] if len(parts) == 2 else parts[0] - - # Try to find the exact or template match - matched_path = None - if path_only in paths: - matched_path = path_only - else: - for p in paths: - if _match_path(p, path_only): - matched_path = p - break - - if matched_path: - raw_spec = {matched_path: paths[matched_path]} - # Inline all $ref so LLM sees the exact required fields - resolved = resolve_refs(raw_spec, full_spec) - - # Optimization: Prune 'responses' as they are not needed for building the request - path_data = resolved.get(matched_path, {}) - for method in path_data: - if isinstance(path_data[method], dict): - path_data[method].pop("responses", None) - - # Pass through aggressive pruning to remove descriptions, examples, etc. - pruned = prune_spec_for_execution(resolved) - # CONVERT TO SHORTHAND - actual_endpoint_spec = shorthand_spec(pruned) - else: - print(f"[PATH MATCH FAIL] Could not find match for {path_only}. Falling back to simplified path list.") - # Fallback: Just send the relevant path/method info if we can't find the full schema - actual_endpoint_spec = {"endpoint": endpoint_str, "warning": "Full schema match failed"} - - # Fetch semantic assets for AI context - rich_assets = AssetService.get_ai_context(db, request.spec_id) - - # NEW: Fallback to stored credentials if none provided in request - effective_credentials = request.user_credentials - if not effective_credentials and request.spec_id: - spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == request.spec_id).first() - if spec and spec.login_credentials: - effective_credentials = spec.login_credentials - print(f"[AUTH FALLBACK] Using stored credentials for spec '{spec.name}'") - - # 1. Ask LLM to format the actual httpx request - req_plan, executor_token_usage = prepare_step_request( - endpoint_spec=actual_endpoint_spec, - base_url=request.base_url, - scenario="", - step_definition=request.step_definition, - context_variables=request.context_variables, - user_credentials=effective_credentials, - available_assets=rich_assets - ) - - # Sanitize URL: fix relative, missing-scheme, or placeholder URLs from the LLM - url = req_plan.request.url.strip() - PLACEHOLDER_HOSTS = {"api.example.com", "example.com", "", "your-api.com"} - def _is_placeholder(u: str) -> bool: - try: - from urllib.parse import urlparse - host = urlparse(u).hostname or "" - return host.lower() in PLACEHOLDER_HOSTS or u.startswith("") - except Exception: - return False - - if url.startswith("/"): - # Relative path — prepend base URL - url = request.base_url.rstrip("/") + url - elif _is_placeholder(url): - # LLM returned a placeholder domain — extract just the path and rebuild - from urllib.parse import urlparse - parsed = urlparse(url) - path = parsed.path or "/" - url = request.base_url.rstrip("/") + path - if parsed.query: - url += "?" + parsed.query - print(f"[URL FIX] Placeholder URL detected, rebuilt to: {url}") - elif not url.startswith("http"): - url = f"https://{url}" - - # Deduplicate query parameters gently: remove from dict if already in URL string - from urllib.parse import urlparse, parse_qsl - parsed = urlparse(url) - if parsed.query: - url_keys = set(k for k, v in parse_qsl(parsed.query)) - # Remove keys from query_params if they are already hardcoded in the url - keys_to_delete = [k for k in req_plan.request.query_params if k in url_keys] - for k in keys_to_delete: - del req_plan.request.query_params[k] - - # 2. Execute via httpx - async with httpx.AsyncClient(timeout=120.0) as client: - http_req = client.build_request( - method=req_plan.request.method.strip().upper(), - url=url, - headers=req_plan.request.headers, - params=req_plan.request.query_params, - data=req_plan.request.form_data if req_plan.request.form_data else None, - json=req_plan.request.body if req_plan.request.body and not req_plan.request.form_data else None - ) - - # Handle File Attachments if provided by LLM - files_payload = {} - opened_files = [] - - try: - if req_plan.request.files: - # Strip manually set Content-Type if files are present to let httpx handle the boundary - original_headers = {k: v for k, v in req_plan.request.headers.items() if k.lower() != 'content-type'} - req_plan.request.headers = original_headers - - for field_name, file_name in req_plan.request.files.items(): - # Find asset record by name - asset = db.query(database.Asset).filter(database.Asset.name == file_name).first() - if asset: - # Use StorageProvider to get full path - from backend.services.asset_service import storage - full_path = storage.get_full_path(asset.file_path) - if os.path.exists(full_path): - # Open file for reading - f = open(full_path, "rb") - opened_files.append(f) - files_payload[field_name] = (asset.name, f, asset.file_type) - else: - print(f"[ASSET ERROR] File '{file_name}' not found on disk at {full_path}") - else: - print(f"[ASSET ERROR] File '{file_name}' not found in Media Hub.") - - # Print to backend console for debugging - print(f"Executing: {http_req.method} {http_req.url}") - - try: - if files_payload: - response = await client.request( - method=req_plan.request.method.strip().upper(), - url=url, - headers=req_plan.request.headers, - params=req_plan.request.query_params, - data=req_plan.request.form_data if req_plan.request.form_data else None, - files=files_payload - ) - else: - response = await client.send(http_req) - except Exception as e: - raise HTTPException(status_code=500, detail=f"Request to {url} failed: {str(e)}") - finally: - # CLOSE ALL FILE HANDLES - for f in opened_files: - f.close() - - # Format the raw response - resp_data = { - "status_code": response.status_code, - "body": response.text, - "headers": dict(response.headers) - } - - # Update the plan to reflect any URL cleanup - req_plan.request.url = url - - # 3. Save to History - history = database.ExecutionHistory( - session_id=request.session_id, - step=req_plan.step, - request_json=req_plan.request.model_dump(), - response_json=resp_data, - step_definition_json=request.step_definition - ) - db.add(history) - db.commit() - - return { - "executed_request": req_plan.request.model_dump(), - "response": resp_data, - "analysis_needed": req_plan.step, - "token_usage": executor_token_usage - } - except Exception as e: - import traceback - trace = traceback.format_exc() - print(f"EXECUTION ERROR: {trace}") - raise HTTPException(status_code=500, detail=f"Execution failed: {str(e)}\n{trace}") - -@app.post("/analyze-response", response_model=schemas.AnalyzeResponse) -def analyze_response_endpoint(request: schemas.AnalyzeRequest, db: Session = Depends(database.get_db)): - try: - step_def = request.step_definition or {} - endpoint_str = step_def.get("endpoint", "Unknown") - purpose_str = step_def.get("purpose", "Unknown") - - # 1. Analyze it - analysis = analyze_api_response( - step=request.step, - endpoint=endpoint_str, - purpose=purpose_str, - status_code=request.response_data.get("status_code", 0), - response_body=request.response_data.get("body", "") - ) - - # 2. Save extracted variables ONLY to session context (session-scoped, not persisted to spec) - if analysis.extracted_variables: - set_context_variables(db, request.session_id, analysis.extracted_variables) - - # 3. Update History - history = db.query(database.ExecutionHistory).filter( - database.ExecutionHistory.session_id == request.session_id, - database.ExecutionHistory.step == request.step - ).order_by(database.ExecutionHistory.id.desc()).first() - - if history: - history.analysis_json = analysis.model_dump() - db.commit() - - return analysis - except Exception as e: - raise HTTPException(status_code=500, detail=f"Analysis failed: {str(e)}") - -# ───────────────────────────────────────────── -# CREDENTIAL ENDPOINTS (per spec) -# ───────────────────────────────────────────── - -@app.get("/specs/{spec_id}/credentials") -def get_credentials(spec_id: str, db: Session = Depends(database.get_db)): - """Return stored login credentials for a spec.""" - spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == spec_id).first() - if not spec: - raise HTTPException(status_code=404, detail="Spec not found") - return spec.login_credentials or {} - -@app.post("/specs/{spec_id}/credentials") -def save_credentials(spec_id: str, payload: schemas.CredentialUpdate, db: Session = Depends(database.get_db)): - """Save/merge login credentials to a spec. Called manually from the UI.""" - spec = db.query(database.SpecDocument).filter(database.SpecDocument.id == spec_id).first() - if not spec: - raise HTTPException(status_code=404, detail="Spec not found") - - # If _clear flag is present, wipe all credentials - if payload.extra and payload.extra.get("_clear"): - spec.login_credentials = {} - flag_modified(spec, "login_credentials") - db.commit() - db.refresh(spec) - return {"status": "cleared", "credentials": {}} - - current: dict = dict(spec.login_credentials or {}) - if payload.name is not None: - current["name"] = payload.name - if payload.username is not None: - current["username"] = payload.username - current["email"] = payload.username # store under both keys - if payload.password is not None: - current["password"] = payload.password - if payload.extra: - # Filter out internal-only keys like _clear before saving - extra_clean = {k: v for k, v in payload.extra.items() if not k.startswith("_")} - current.update(extra_clean) - spec.login_credentials = current - flag_modified(spec, "login_credentials") - db.commit() - db.refresh(spec) - return {"status": "saved", "credentials": spec.login_credentials} - -@app.get("/context/{session_id}") -def get_context_endpoint(session_id: str, db: Session = Depends(database.get_db)): - """Return only session context variables (dynamic values from previous steps).""" - context = get_context(db, session_id) - return context - -@app.post("/context/{session_id}") -def update_context_endpoint(session_id: str, payload: schemas.ContextUpdate, db: Session = Depends(database.get_db)): - """Update session context variables manually (e.g., from selection).""" - # Assuming set_context_variables exists and merges the dict - variables = {payload.key: payload.value} - updated_context = set_context_variables(db, session_id, variables) - return updated_context - -@app.delete("/session/{session_id}") -def clear_session(session_id: str, db: Session = Depends(database.get_db)): - session = db.query(database.SessionModel).filter(database.SessionModel.id == session_id).first() - if session: - db.delete(session) - db.query(database.ExecutionHistory).filter(database.ExecutionHistory.session_id == session_id).delete() - db.commit() - return {"status": "ok"} - -@app.get("/history") -def get_all_history(db: Session = Depends(database.get_db)): - """Fetch all execution history joined with session metadata.""" - results = db.query( - database.ExecutionHistory, - database.SessionModel - ).join( - database.SessionModel, - database.ExecutionHistory.session_id == database.SessionModel.id - ).order_by(database.ExecutionHistory.timestamp.desc()).all() - - history_list = [] - for hist, sess in results: - history_list.append({ - "id": hist.id, - "session_id": hist.session_id, - "spec_id": sess.spec_id, - "spec_name": sess.spec_name or "Unknown", - "scenario": sess.scenario or "No scenario", - "context_variables": sess.context_variables or {}, - "step": hist.step, - "request": hist.request_json, - "response": hist.response_json, - "analysis": hist.analysis_json, - "step_definition": hist.step_definition_json, - "timestamp": hist.timestamp.isoformat() - }) - return history_list - -@app.delete("/history/{session_id}") -def delete_history_session(session_id: str, db: Session = Depends(database.get_db)): - """Delete a specific session and all its execution history.""" - db.query(database.ExecutionHistory).filter(database.ExecutionHistory.session_id == session_id).delete() - db.query(database.SessionModel).filter(database.SessionModel.id == session_id).delete() - db.commit() - return {"status": "deleted", "session_id": session_id} - -@app.delete("/history") -def clear_all_history_endpoint(db: Session = Depends(database.get_db)): - """Wipe all execution history and session metadata.""" - db.query(database.ExecutionHistory).delete() - db.query(database.SessionModel).delete() - db.commit() - return {"status": "cleared"} - - -# ───────────────────────────────────────────── -# CHAT ASSISTANT ENDPOINTS -# ───────────────────────────────────────────── - -@app.post("/chat", response_model=schemas.ChatResponse) -def chat_endpoint(request: schemas.ChatRequest, db: Session = Depends(database.get_db)): - """ - Send a message to the AI chat assistant. - - If chat_session_id is None → auto-creates a new named chat session. - - Saves user message + AI response to DB (persistent across refreshes). - - Returns a structured action so the frontend can trigger executions, etc. - """ - try: - # 1. Resolve or create chat session - chat_session = None - if request.chat_session_id: - chat_session = db.query(database.ChatSession).filter( - database.ChatSession.id == request.chat_session_id - ).first() - - if not chat_session: - # Auto-name from first message (truncate to 60 chars) - session_name = request.message[:60].strip() - if len(request.message) > 60: - session_name += "..." - chat_session = database.ChatSession( - id=str(uuid.uuid4()), - name=session_name, - spec_id=request.spec_id, - session_id=request.session_id, - ) - db.add(chat_session) - db.flush() # get the ID before committing - - # 2. Save user message to DB - user_msg = database.ChatMessage( - chat_session_id=chat_session.id, - role="user", - content=request.message, - ) - db.add(user_msg) - db.flush() - - # 3. Load chat history for context window (last 20 messages) - history_rows = db.query(database.ChatMessage).filter( - database.ChatMessage.chat_session_id == chat_session.id - ).order_by(database.ChatMessage.id.asc()).all() - - chat_history = [ - {"role": m.role, "content": m.content} - for m in history_rows[:-1] # exclude the message we just inserted - ] - - # 4. Call the AI - ai_result = chat_process_message( - message=request.message, - spec_name=request.spec_name, - execution_plan=request.execution_plan or [], - context_variables=request.context_variables or {}, - last_step_result=request.last_step_result, - all_step_results=request.all_step_results, - chat_history=chat_history, - ) - - # 5. Save assistant response to DB - assistant_content = ai_result.get("message", "") - assistant_msg = database.ChatMessage( - chat_session_id=chat_session.id, - role="assistant", - content=assistant_content, - action_json=ai_result, - ) - db.add(assistant_msg) - - # 6. Update session timestamp - chat_session.updated_at = __import__("datetime").datetime.utcnow() - db.commit() - - return schemas.ChatResponse( - chat_session_id=chat_session.id, - chat_session_name=chat_session.name, - action=ai_result.get("action", "reply"), - message=assistant_content, - step=ai_result.get("step"), - changes=ai_result.get("changes"), - assertions=ai_result.get("assertions"), - explanation=ai_result.get("explanation"), - ) - - except Exception as e: - import traceback - traceback.print_exc() - raise HTTPException(status_code=500, detail=f"Chat failed: {str(e)}") - - -@app.get("/chat-sessions") -def list_chat_sessions(spec_id: Optional[str] = None, db: Session = Depends(database.get_db)): - """List all chat sessions, optionally filtered by spec_id. Sorted newest first.""" - query = db.query(database.ChatSession) - if spec_id: - query = query.filter(database.ChatSession.spec_id == spec_id) - sessions = query.order_by(database.ChatSession.updated_at.desc()).all() - - result = [] - for s in sessions: - msg_count = db.query(database.ChatMessage).filter( - database.ChatMessage.chat_session_id == s.id - ).count() - result.append({ - "id": s.id, - "name": s.name, - "spec_id": s.spec_id, - "session_id": s.session_id, - "message_count": msg_count, - "created_at": s.created_at.isoformat() if s.created_at else None, - "updated_at": s.updated_at.isoformat() if s.updated_at else None, - }) - return result - - -@app.get("/chat-sessions/{chat_session_id}") -def get_chat_session(chat_session_id: str, db: Session = Depends(database.get_db)): - """Get a single chat session with its full message history.""" - session = db.query(database.ChatSession).filter( - database.ChatSession.id == chat_session_id - ).first() - if not session: - raise HTTPException(status_code=404, detail="Chat session not found") - - messages = db.query(database.ChatMessage).filter( - database.ChatMessage.chat_session_id == chat_session_id - ).order_by(database.ChatMessage.id.asc()).all() - - return { - "id": session.id, - "name": session.name, - "spec_id": session.spec_id, - "session_id": session.session_id, - "created_at": session.created_at.isoformat() if session.created_at else None, - "updated_at": session.updated_at.isoformat() if session.updated_at else None, - "messages": [ - { - "id": m.id, - "role": m.role, - "content": m.content, - "action_json": m.action_json, - "timestamp": m.timestamp.isoformat() if m.timestamp else None, - } - for m in messages - ], - } - - -@app.patch("/chat-sessions/{chat_session_id}") -def rename_chat_session(chat_session_id: str, payload: dict, db: Session = Depends(database.get_db)): - """Rename a chat session.""" - session = db.query(database.ChatSession).filter( - database.ChatSession.id == chat_session_id - ).first() - if not session: - raise HTTPException(status_code=404, detail="Chat session not found") - if "name" in payload: - session.name = payload["name"] - db.commit() - return {"id": session.id, "name": session.name} - - -@app.delete("/chat-sessions/{chat_session_id}") -def delete_chat_session(chat_session_id: str, db: Session = Depends(database.get_db)): - """Delete a chat session and all its messages (cascade).""" - session = db.query(database.ChatSession).filter( - database.ChatSession.id == chat_session_id - ).first() - if not session: - raise HTTPException(status_code=404, detail="Chat session not found") - db.delete(session) - db.commit() - return {"status": "deleted", "id": chat_session_id} +@app.exception_handler(RequestValidationError) +async def validation_exception_handler(request, exc): + logger.warning("Validation error: %s", exc.errors()) + return JSONResponse(status_code=422, content={"detail": exc.errors()}) -@app.delete("/chat-sessions") -def clear_all_chat_sessions(db: Session = Depends(database.get_db)): - """Wipe all chat sessions and messages.""" - # Delete messages first due to FK constraint - db.query(database.ChatMessage).delete() - db.query(database.ChatSession).delete() - db.commit() - return {"status": "cleared"} +# Routers +app.include_router(specs.router) +app.include_router(assets.router) +app.include_router(sessions.router) +app.include_router(history.router) +app.include_router(chat.router) +app.include_router(execution.router) +@app.get("/") +def health_check(): + return {"status": "ok", "message": "AI API Testing Tool Backend running."} diff --git a/backend/services/api_executor.py b/backend/services/api_executor.py index a13d686405a2b0b61ebfe4854887bfe5e832ba1b..a86683caed3a8a5ff9637d84c48ab80819a9c8e6 100644 --- a/backend/services/api_executor.py +++ b/backend/services/api_executor.py @@ -1,9 +1,9 @@ import json from typing import Dict, Any, List from langchain_core.prompts import PromptTemplate -from langchain_mistralai.chat_models import ChatMistralAI from langchain_core.output_parsers import JsonOutputParser from pydantic import BaseModel, Field +from backend.integrations.llm.client import deterministic as _llm # Using Pydantic to structure LangChain output for step execution class APIRequest(BaseModel): @@ -104,9 +104,6 @@ def prepare_step_request( input_variables=["endpoint_spec", "base_url", "scenario", "step_definition", "user_credentials", "context_variables", "available_assets"], ) - llm = ChatMistralAI(model_name="mistral-large-latest", temperature=0) - chain = prompt | llm | parser - msg = prompt.format_prompt( endpoint_spec=json.dumps(endpoint_spec, separators=(',', ':')), base_url=base_url, @@ -117,17 +114,7 @@ def prepare_step_request( available_assets=json.dumps(available_assets, separators=(',', ':')) ).to_messages() - ai_msg = llm.invoke(msg) - parsed_response = parser.parse(ai_msg.content) - - # Extract token usage - usage = ai_msg.response_metadata.get("token_usage", {}) - token_usage = None - if usage: - token_usage = { - "prompt_tokens": usage.get("prompt_tokens", 0), - "completion_tokens": usage.get("completion_tokens", 0), - "total_tokens": usage.get("total_tokens", 0) - } - + content, token_usage = _llm.invoke(msg) + parsed_response = parser.parse(content) + return StepExecutionPlan(**parsed_response), token_usage diff --git a/backend/services/chat_assistant.py b/backend/services/chat_assistant.py index 435f768d524fd2f95913aa2f014f588bf14e97d4..6566ed31dd2a2dbc308724ec43d215acaa987554 100644 --- a/backend/services/chat_assistant.py +++ b/backend/services/chat_assistant.py @@ -9,8 +9,8 @@ what to do (execute a step, explain an error, modify a request, generate tests, import json import re from typing import Any, Dict, List, Optional -from langchain_mistralai.chat_models import ChatMistralAI -from langchain_core.messages import SystemMessage, HumanMessage +from langchain_core.messages import SystemMessage, HumanMessage, AIMessage +from backend.integrations.llm.client import chat as _llm SYSTEM_PROMPT = """You are an AI assistant integrated into an API testing tool (similar to Postman AI). Your job is to help the user control their API testing workflow through natural language. @@ -112,7 +112,7 @@ def _build_history_messages(chat_history: List[Dict]) -> List: if role == "user": messages.append(HumanMessage(content=content)) else: - messages.append(HumanMessage(content=f"[Your previous response]: {content}")) + messages.append(AIMessage(content=content)) return messages @@ -133,15 +133,13 @@ def process_message( full_system = SYSTEM_PROMPT + f"\n\n--- CURRENT CONTEXT ---\n{context_block}\n--- END CONTEXT ---" - llm = ChatMistralAI(model_name="mistral-large-latest", temperature=0.2, timeout=120) - messages = [SystemMessage(content=full_system)] messages.extend(_build_history_messages(chat_history)) messages.append(HumanMessage(content=message)) try: - response = llm.invoke(messages) - raw = response.content.strip() + content, _ = _llm.invoke(messages) + raw = content.strip() # Strip markdown code fences if present raw = re.sub(r"^```(?:json)?\s*", "", raw, flags=re.MULTILINE) diff --git a/backend/services/llm_planner.py b/backend/services/llm_planner.py index 821b9668ac8a28c3569237f29716d7d5abd569d0..cfce143830b7f03a20b7dd310ffa1737b2f0dd7c 100644 --- a/backend/services/llm_planner.py +++ b/backend/services/llm_planner.py @@ -1,9 +1,12 @@ import json from typing import Dict, Any, List, Optional from langchain_core.prompts import PromptTemplate -from langchain_mistralai.chat_models import ChatMistralAI from langchain_core.output_parsers import JsonOutputParser +from backend.core.logging import get_logger from backend.schemas import ExecutionPlan +from backend.integrations.llm.client import deterministic as _llm + +logger = get_logger(__name__) # Prompt template from the user prompt PLANNER_PROMPT = """ @@ -73,28 +76,21 @@ def generate_execution_plan(parsed_spec: Dict[str, Any], base_url: str, scenario input_variables=["parsed_spec", "base_url", "scenario", "user_credentials_str"], ) - # Initialize Mistral Model (requires MISTRAL_API_KEY in env) - llm = ChatMistralAI(model_name="mistral-large-latest", temperature=0, timeout=120) - msg = prompt.format_prompt( parsed_spec=json.dumps(parsed_spec.get("endpoints", parsed_spec), separators=(',', ':')), base_url=base_url, scenario=scenario, user_credentials_str=json.dumps(user_credentials or {}, separators=(',', ':')) ).to_messages() - - ai_msg = llm.invoke(msg) - parsed_response = parser.parse(ai_msg.content) - - # Extract token usage - usage = ai_msg.response_metadata.get("token_usage", {}) - token_usage = None - if usage: - token_usage = { - "prompt_tokens": usage.get("prompt_tokens", 0), - "completion_tokens": usage.get("completion_tokens", 0), - "total_tokens": usage.get("total_tokens", 0) - } + + # Invoke with one retry on parse failure + content, token_usage = _llm.invoke(msg) + try: + parsed_response = parser.parse(content) + except Exception as parse_err: + logger.warning("Plan parse failed, retrying: %s", parse_err) + content, token_usage = _llm.invoke(msg) + parsed_response = parser.parse(content) # Return as Pydantic model return ExecutionPlan( diff --git a/backend/services/response_analyzer.py b/backend/services/response_analyzer.py index 7cd5d22d58f0109c5497ba1ff588c920318ef1b2..f1e22cca743d44c80ab02430619b62c789367814 100644 --- a/backend/services/response_analyzer.py +++ b/backend/services/response_analyzer.py @@ -1,9 +1,12 @@ import json from typing import Dict, Any from langchain_core.prompts import PromptTemplate -from langchain_mistralai.chat_models import ChatMistralAI from langchain_core.output_parsers import JsonOutputParser +from backend.core.logging import get_logger from backend.schemas import AnalyzeResponse +from backend.integrations.llm.client import deterministic as _llm + +logger = get_logger(__name__) ANALYSIS_PROMPT = """ You are a response analyzer. @@ -44,9 +47,6 @@ def analyze_api_response( input_variables=["step_info", "status_code", "response_body"], ) - llm = ChatMistralAI(model_name="mistral-large-latest", temperature=0) - chain = prompt | llm | parser - # Aggressive truncation: keep only 500 chars from start and end (1KB total) if len(response_body) > 1000: response_body = response_body[:500] + "\n... [TRUNCATED] ...\n" + response_body[-500:] @@ -57,18 +57,13 @@ def analyze_api_response( response_body=response_body ).to_messages() - ai_msg = llm.invoke(msg) - parsed_response = parser.parse(ai_msg.content) - - # Extract token usage - usage = ai_msg.response_metadata.get("token_usage", {}) - token_usage = None - if usage: - token_usage = { - "prompt_tokens": usage.get("prompt_tokens", 0), - "completion_tokens": usage.get("completion_tokens", 0), - "total_tokens": usage.get("total_tokens", 0) - } + content, token_usage = _llm.invoke(msg) + try: + parsed_response = parser.parse(content) + except Exception as parse_err: + logger.warning("Analysis parse failed, retrying: %s", parse_err) + content, token_usage = _llm.invoke(msg) + parsed_response = parser.parse(content) # Ensure step matches the requested step and add token usage parsed_response["step"] = step diff --git a/backend/tests/__init__.py b/backend/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py new file mode 100644 index 0000000000000000000000000000000000000000..1bc48e28aabdea3b563f6501879624f08b4bf0cd --- /dev/null +++ b/backend/tests/conftest.py @@ -0,0 +1,44 @@ +import os + +# Must be set before any backend module is imported — database.py reads this at import time. +os.environ["DATABASE_URL"] = "sqlite:///:memory:" +os.environ.setdefault("MISTRAL_API_KEY", "test-key") +os.environ.setdefault("ALLOWED_ORIGINS", "http://localhost:3000") + +import pytest +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from fastapi.testclient import TestClient + +from backend.database import Base, get_db +from backend.main import app + +_engine = create_engine( + "sqlite:///:memory:", + connect_args={"check_same_thread": False}, +) +_TestingSession = sessionmaker(autocommit=False, autoflush=False, bind=_engine) + + +def _override_get_db(): + db = _TestingSession() + try: + yield db + finally: + db.close() + + +app.dependency_overrides[get_db] = _override_get_db + + +@pytest.fixture(scope="session", autouse=True) +def setup_db(): + Base.metadata.create_all(bind=_engine) + yield + Base.metadata.drop_all(bind=_engine) + + +@pytest.fixture +def client(): + with TestClient(app) as c: + yield c diff --git a/backend/tests/test_api_executor.py b/backend/tests/test_api_executor.py new file mode 100644 index 0000000000000000000000000000000000000000..801ff9c4e10ec983e787c7cedfd51b7c9e2af45d --- /dev/null +++ b/backend/tests/test_api_executor.py @@ -0,0 +1,78 @@ +import json +from unittest.mock import patch + +from backend.services.api_executor import prepare_step_request + +_VALID_RESPONSE = { + "step": 1, + "request": { + "method": "POST", + "url": "https://api.example.com/auth/login", + "headers": {"Content-Type": "application/json"}, + "query_params": {}, + "form_data": {}, + "body": {"username": "test@example.com", "password": "Pass123!"}, + "files": {}, + }, + "analysis": { + "required_inputs": ["username", "password"], + "values_from_previous_steps": [], + "generated_test_data": [], + }, +} + +_TOKEN_USAGE = {"prompt_tokens": 200, "completion_tokens": 80, "total_tokens": 280} + + +def _run(**overrides): + kwargs = dict( + endpoint_spec={"method": "POST", "path": "/auth/login"}, + base_url="https://api.example.com", + scenario="Login user", + step_definition={"step": 1, "endpoint": "POST /auth/login", "purpose": "Login"}, + context_variables={}, + user_credentials={"username": "test@example.com", "password": "Pass123!"}, + ) + kwargs.update(overrides) + return prepare_step_request(**kwargs) + + +def test_returns_step_execution_plan(): + with patch("backend.services.api_executor._llm") as mock_llm: + mock_llm.invoke.return_value = (json.dumps(_VALID_RESPONSE), _TOKEN_USAGE) + + result, token_usage = _run() + + assert result.request.method == "POST" + assert result.request.url == "https://api.example.com/auth/login" + assert result.request.body == {"username": "test@example.com", "password": "Pass123!"} + + +def test_token_usage_returned(): + with patch("backend.services.api_executor._llm") as mock_llm: + mock_llm.invoke.return_value = (json.dumps(_VALID_RESPONSE), _TOKEN_USAGE) + + _, token_usage = _run() + + assert token_usage["total_tokens"] == 280 + + +def test_context_variables_included_in_prompt(): + with patch("backend.services.api_executor._llm") as mock_llm: + mock_llm.invoke.return_value = (json.dumps(_VALID_RESPONSE), None) + + _run(context_variables={"access_token": "tok_abc"}) + + call_args = mock_llm.invoke.call_args + # The rendered prompt messages should contain the context variable + prompt_text = str(call_args) + assert "access_token" in prompt_text + + +def test_token_usage_none_when_llm_returns_none(): + with patch("backend.services.api_executor._llm") as mock_llm: + mock_llm.invoke.return_value = (json.dumps(_VALID_RESPONSE), None) + + _, token_usage = _run() + + assert token_usage is None diff --git a/backend/tests/test_llm_planner.py b/backend/tests/test_llm_planner.py new file mode 100644 index 0000000000000000000000000000000000000000..86471cdb712d3e5a0e05183fdf63c61a65f392a9 --- /dev/null +++ b/backend/tests/test_llm_planner.py @@ -0,0 +1,71 @@ +import json +from unittest.mock import patch + +import pytest + +from backend.services.llm_planner import generate_execution_plan + +_VALID_RESPONSE = { + "execution_plan": [ + {"step": 1, "endpoint": "POST /auth/login", "purpose": "Authenticate the user"}, + {"step": 2, "endpoint": "GET /profile", "purpose": "Fetch user profile"}, + ], + "suggested_credentials": {"username": "tester@example.com", "password": "Pass123!"}, +} + +_TOKEN_USAGE = {"prompt_tokens": 120, "completion_tokens": 60, "total_tokens": 180} + + +def test_returns_execution_plan(): + with patch("backend.services.llm_planner._llm") as mock_llm: + mock_llm.invoke.return_value = (json.dumps(_VALID_RESPONSE), _TOKEN_USAGE) + + result = generate_execution_plan( + parsed_spec={"endpoints": []}, + base_url="https://api.example.com", + scenario="Login and view profile", + ) + + assert len(result.execution_plan) == 2 + assert result.execution_plan[0]["step"] == 1 + assert result.suggested_credentials["username"] == "tester@example.com" + + +def test_token_usage_is_passed_through(): + with patch("backend.services.llm_planner._llm") as mock_llm: + mock_llm.invoke.return_value = (json.dumps(_VALID_RESPONSE), _TOKEN_USAGE) + + result = generate_execution_plan( + parsed_spec={}, base_url="https://api.example.com", scenario="Login" + ) + + assert result.token_usage == _TOKEN_USAGE + + +def test_retries_once_on_parse_failure(): + with patch("backend.services.llm_planner._llm") as mock_llm: + # First call returns invalid JSON; second returns valid + mock_llm.invoke.side_effect = [ + ("not valid json {{{{", None), + (json.dumps(_VALID_RESPONSE), _TOKEN_USAGE), + ] + + result = generate_execution_plan( + parsed_spec={}, base_url="https://api.example.com", scenario="Login" + ) + + assert mock_llm.invoke.call_count == 2 + assert len(result.execution_plan) == 2 + + +def test_raises_after_two_parse_failures(): + with patch("backend.services.llm_planner._llm") as mock_llm: + mock_llm.invoke.side_effect = [ + ("bad json", None), + ("still bad json", None), + ] + + with pytest.raises(Exception): + generate_execution_plan( + parsed_spec={}, base_url="https://api.example.com", scenario="Login" + ) diff --git a/backend/tests/test_routes.py b/backend/tests/test_routes.py new file mode 100644 index 0000000000000000000000000000000000000000..bd8a2ed36dee54d9fc153d1ec7cb19c0aa9af136 --- /dev/null +++ b/backend/tests/test_routes.py @@ -0,0 +1,140 @@ +""" +Integration tests for HTTP routes. +All LLM calls are mocked so no real Mistral API key is needed. +""" +import io +import json +from unittest.mock import patch + +import pytest + +# client fixture is provided by conftest.py + + +# ── Health / root ───────────────────────────────────────────────────────────── + +def test_root_health(client): + response = client.get("/") + assert response.status_code == 200 + assert response.json()["status"] == "ok" + + +# ── Session ─────────────────────────────────────────────────────────────────── + +def test_create_session(client): + response = client.post("/session") + assert response.status_code == 200 + data = response.json() + assert "session_id" in data + + +# ── Spec upload ─────────────────────────────────────────────────────────────── + +_MINIMAL_SPEC = json.dumps({ + "openapi": "3.0.0", + "info": {"title": "Test API", "version": "1.0.0"}, + "paths": { + "/ping": { + "get": { + "summary": "Ping", + "responses": {"200": {"description": "OK"}}, + } + } + }, +}) + + +def test_upload_spec_valid(client): + response = client.post( + "/upload-spec", + files={"file": ("api.json", _MINIMAL_SPEC.encode(), "application/json")}, + ) + assert response.status_code == 200 + data = response.json() + assert "id" in data + return data["id"] + + +def test_upload_spec_rejects_large_file(client): + big_content = b"x" * (6 * 1024 * 1024) # 6 MB — over the 5 MB limit + response = client.post( + "/upload-spec", + files={"file": ("big.json", big_content, "application/json")}, + ) + assert response.status_code == 400 + + +def test_list_specs(client): + response = client.get("/specs") + assert response.status_code == 200 + assert isinstance(response.json(), list) + + +# ── Plan generation (mocked LLM) ────────────────────────────────────────────── + +_MOCK_PLAN = { + "execution_plan": [ + {"step": 1, "endpoint": "GET /ping", "purpose": "Health check"}, + ], + "suggested_credentials": {}, +} + +_MOCK_TOKEN_USAGE = {"prompt_tokens": 50, "completion_tokens": 20, "total_tokens": 70} + + +def test_plan_endpoint(client): + # Upload a spec first to get a valid spec_id + upload = client.post( + "/upload-spec", + files={"file": ("api.json", _MINIMAL_SPEC.encode(), "application/json")}, + ) + spec_id = upload.json()["id"] + + session = client.post("/session").json()["session_id"] + + with patch("backend.services.llm_planner._llm") as mock_llm: + mock_llm.invoke.return_value = (json.dumps(_MOCK_PLAN), _MOCK_TOKEN_USAGE) + + response = client.post("/plan", json={ + "spec_content": _MINIMAL_SPEC, + "base_url": "https://api.example.com", + "scenario": "Ping the API", + "user_credentials": {}, + "spec_id": spec_id, + "session_id": session, + }) + + assert response.status_code == 200 + data = response.json() + assert len(data["execution_plan"]) == 1 + assert data["token_usage"]["total_tokens"] == 70 + + +# ── Chat endpoint (mocked LLM) ──────────────────────────────────────────────── + +_MOCK_CHAT_RESPONSE = json.dumps({ + "action": "reply", + "message": "I can help you run step 1.", +}) + + +def test_chat_endpoint(client): + session = client.post("/session").json()["session_id"] + + with patch("backend.services.chat_assistant._llm") as mock_llm: + mock_llm.invoke.return_value = (_MOCK_CHAT_RESPONSE, None) + + response = client.post("/chat", json={ + "message": "Run step 1 for me", + "session_id": session, + "spec_id": None, + "execution_plan": [], + "context_variables": {}, + "last_step_result": None, + "all_step_results": {}, + "chat_session_id": None, + }) + + assert response.status_code == 200 + data = response.json() + assert data["action"] == "reply" diff --git a/backend/tests/test_url_guard.py b/backend/tests/test_url_guard.py new file mode 100644 index 0000000000000000000000000000000000000000..877d59c06f2f71605904e8226d5ce67d11c34dc7 --- /dev/null +++ b/backend/tests/test_url_guard.py @@ -0,0 +1,75 @@ +import socket +from unittest.mock import patch +import pytest +from fastapi import HTTPException + +from backend.core.url_guard import assert_safe_url + + +def _mock_resolve(ip: str): + """Return a socket.getaddrinfo patch that resolves any hostname to `ip`.""" + return patch("backend.core.url_guard.socket.getaddrinfo", return_value=[(None, None, None, None, (ip, 0))]) + + +# ── Scheme checks ───────────────────────────────────────────────────────────── + +def test_blocks_ftp_scheme(): + with pytest.raises(HTTPException) as exc: + assert_safe_url("ftp://example.com/file") + assert exc.value.status_code == 400 + + +def test_blocks_file_scheme(): + with pytest.raises(HTTPException): + assert_safe_url("file:///etc/passwd") + + +def test_blocks_no_hostname(): + with pytest.raises(HTTPException): + assert_safe_url("http:///path") + + +# ── Private / loopback IP checks ────────────────────────────────────────────── + +def test_blocks_localhost_ip(): + with _mock_resolve("127.0.0.1"): + with pytest.raises(HTTPException) as exc: + assert_safe_url("http://localhost/api") + assert exc.value.status_code == 400 + + +def test_blocks_10x_range(): + with _mock_resolve("10.0.0.1"): + with pytest.raises(HTTPException): + assert_safe_url("http://internal.corp/api") + + +def test_blocks_192168_range(): + with _mock_resolve("192.168.1.100"): + with pytest.raises(HTTPException): + assert_safe_url("http://router.local/api") + + +def test_blocks_172_16_range(): + with _mock_resolve("172.16.0.5"): + with pytest.raises(HTTPException): + assert_safe_url("http://private.example.com/api") + + +def test_blocks_link_local(): + with _mock_resolve("169.254.169.254"): + with pytest.raises(HTTPException): + assert_safe_url("http://metadata.example.com/api") + + +# ── Allow public addresses ───────────────────────────────────────────────────── + +def test_allows_public_ip(): + with _mock_resolve("93.184.216.34"): # example.com + assert_safe_url("https://example.com/api") # should not raise + + +def test_allows_http_and_https(): + with _mock_resolve("93.184.216.34"): + assert_safe_url("http://example.com/api") + assert_safe_url("https://example.com/api") diff --git a/backend/uploads/audio/.gitkeep b/backend/uploads/audio/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/backend/uploads/documents/.gitkeep b/backend/uploads/documents/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/backend/uploads/images/.gitkeep b/backend/uploads/images/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/backend/uploads/specs/.gitkeep b/backend/uploads/specs/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/backend/uploads/video/.gitkeep b/backend/uploads/video/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/PROJECT_DOCUMENTATION.md b/documents/PROJECT_DOCUMENTATION.md similarity index 80% rename from PROJECT_DOCUMENTATION.md rename to documents/PROJECT_DOCUMENTATION.md index 4bcdc02e75596d21f69f27db6efa64ac1c2a0cdb..4f37236a4edc0c81b90ee88aadeaa6b43ea201ba 100644 --- a/PROJECT_DOCUMENTATION.md +++ b/documents/PROJECT_DOCUMENTATION.md @@ -59,27 +59,29 @@ | Web Framework | **FastAPI** | ≥0.109 | REST API server | | ASGI Server | **Uvicorn** | ≥0.27 | Runs FastAPI in development | | Production Server | **Gunicorn** | 21.2 | Runs Uvicorn workers in production | -| LLM Orchestration | **LangChain** | 0.1.0 | Prompt chaining with Mistral | +| LLM Orchestration | **LangChain** | ≥0.3.0 | Prompt chaining with Mistral | | LLM Provider | **Mistral AI** | `mistral-large-latest` | Plan generation, step execution, analysis | | HTTP Client | **httpx** | ≥0.27 | Sends actual API requests during execution | -| ORM | **SQLAlchemy** | 2.0.23 | Database queries and model definitions | +| ORM | **SQLAlchemy** | ≥2.0.36 | Database queries and model definitions | | Database | **PostgreSQL** (via Supabase) | — | Persistent storage | -| DB Driver | **psycopg2-binary** | 2.9.9 | PostgreSQL adapter | +| DB Driver | **psycopg2-binary** | 2.9.10 | PostgreSQL adapter | | Cloud Storage | **Supabase Storage** | 2.28.2 | Stores uploaded spec files and assets | | Spec Parsing | **prance** + **PyYAML** | — | Parse OpenAPI YAML/JSON | | Validation | **Pydantic** | ≥2.6 | Request/response data models | +| Rate Limiting | **slowapi** | 0.1.9 | API request throttling | | Auth/Config | **python-dotenv** | 1.0.0 | Load env variables from `.env` | +| Security | **cryptography** | 42.0.0 | Sensitive data handling | ### Frontend | Layer | Technology | Version | Purpose | |---|---|---|---| -| UI Framework | **React** | 19.x | Component-based UI | +| UI Framework | **React** | 19.2.4 | Component-based UI | | Build Tool | **Vite** | 8.x | Fast dev server + bundler | -| Styling | **Tailwind CSS** | 4.x | Utility-first CSS | +| Styling | **Tailwind CSS** | 4.2.1 | Utility-first CSS | | Icons | **lucide-react** | 0.577 | Icon library | | HTTP Client | **Axios** | 1.x | API communication with backend | -| Routing | **react-router-dom** | 7.x | Client-side navigation | +| Routing | **react-router-dom** | 7.13 | Client-side navigation | --- @@ -95,22 +97,35 @@ Testing/ ← Project root ├── runtime.txt ← Python version pin ├── start.ps1 ← PowerShell startup script ├── openapi.yaml ← Sample OpenAPI spec for testing +├── documents/ ← Project documentation and diagrams │ ├── backend/ ← FastAPI application -│ ├── __init__.py -│ ├── main.py ← All FastAPI routes (API gateway) +│ ├── main.py ← Entry point & app configuration │ ├── database.py ← SQLAlchemy models + DB connection │ ├── schemas.py ← Pydantic request/response models -│ └── services/ ← Business logic layer -│ ├── swagger_parser.py ← Parses OpenAPI/Swagger specs -│ ├── postman_parser.py ← Parses Postman Collections -│ ├── llm_planner.py ← Mistral AI: generates execution plan -│ ├── api_executor.py ← Mistral AI: formats HTTP request for each step -│ ├── response_analyzer.py ← Mistral AI: analyzes step response + selection detection -│ ├── chat_assistant.py ← Mistral AI: powers the AI Chat Assistant -│ ├── context_manager.py ← Session context variable storage -│ ├── asset_service.py ← Media asset CRUD + collection management -│ └── storage.py ← Abstraction: local or Supabase file storage +│ ├── app/ +│ │ └── api/ +│ │ └── routes/ ← Route definitions (APIRouters) +│ │ ├── specs.py ← Spec management routes +│ │ ├── assets.py ← Media hub routes +│ │ ├── sessions.py ← Test session routes +│ │ ├── history.py ← Audit log routes +│ │ ├── chat.py ← AI chat assistant routes +│ │ └── execution.py ← Core LLM execution routes +│ ├── core/ ← Shared utilities & config +│ │ ├── config.py ← Settings management +│ │ ├── errors.py │ Error handling logic +│ │ └── logging.py ← Centralized logger +│ ├── integrations/ ← External service clients +│ │ └── llm/ ← Mistral/LangChain integration +│ ├── services/ ← Business logic layer +│ │ ├── swagger_parser.py ← OpenAPI/Postman parsing +│ │ ├── llm_planner.py ← Execution plan generation +│ │ ├── api_executor.py ← HTTP request construction +│ │ ├── response_analyzer.py ← Semantic analysis +│ │ ├── chat_assistant.py ← AI chat logic +│ │ └── storage.py ← Local/Supabase file storage +│ └── tests/ ← Automated test suite │ └── frontend-react/ ← React + Vite frontend ├── index.html @@ -118,18 +133,17 @@ Testing/ ← Project root ├── package.json └── src/ ├── main.jsx ← React entry point - ├── App.jsx ← Main layout + execution workflow page - ├── App.css / index.css ← Global styles + CSS variables (theme) - ├── components/ - │ ├── AppContext.jsx ← Global React Context (state management) - │ ├── CollectionsPage.jsx ← View/manage uploaded API specs - │ ├── ProfilePage.jsx ← Manage test-user credentials - │ ├── HistoryPage.jsx ← View all past test sessions - │ ├── MediaHubPage.jsx ← Upload and manage test assets - │ ├── ChatAssistant.jsx ← AI Chat Assistant (full-page + compact panel) - │ └── SettingsPage.jsx ← Application settings + ├── App.jsx ← Main layout + routing + ├── App.css / index.css ← Styles and theming + ├── components/ ← UI Components + │ ├── AppContext.jsx ← Global state management + │ ├── CollectionsPage.jsx ← Spec management + │ ├── ProfilePage.jsx ← Test user profile management + │ ├── HistoryPage.jsx ← Session history & audit log + │ ├── MediaHubPage.jsx ← Asset management + │ └── ChatAssistant.jsx ← Persistent AI chat └── services/ - └── api.js ← All Axios API calls to the backend + └── api.js ← Axios service layer ``` --- @@ -182,21 +196,22 @@ Testing/ ← Project root ### 5.1 Entry Point — `main.py` -`main.py` is the **single FastAPI application file** containing all route definitions. It does not use `APIRouter` — all routes are mounted directly on the `app` instance. +`main.py` is the entry point for the FastAPI application. It initializes the app, ensures directory structures, and mounts the various module routers. **Key responsibilities:** -- CORS middleware (currently allows all origins — suitable for dev/demo) -- Static file serving for the `/uploads` directory -- Validation error handler (returns 422 with body for debugging) -- Orchestrating all service calls - -**Key logic in `/execute-step`:** -The most complex route. It performs: -1. **Schema Resolution** — Looks up the matching OpenAPI path definition, inlines all `$ref` references recursively, prunes metadata (descriptions, examples), and converts the schema to a compact shorthand notation (`{prop*(str)}`) to minimize LLM tokens. -2. **URL Sanitization** — Detects if the LLM returned a placeholder URL (like `api.example.com`) and rewrites it to use the real base URL. -3. **Asset Loading** — Opens local files from the Media Hub for multipart uploads. -4. **HTTP Execution** — Uses `httpx.AsyncClient` to send the real request. -5. **History Persistence** — Saves request + response to the database. +- **Rate Limiting**: Uses `slowapi` to protect endpoints (default 200/min). +- **Global Error Handling**: Custom handlers for `AppError` and `RequestValidationError`. +- **CORS Configuration**: Loaded from `settings`. +- **Static Files**: Serves the `/uploads` directory. +- **Router Orchestration**: Includes routers from `backend.app.api.routes`. + +**Modules (Routers):** +- `specs`: Spec upload, retrieval, and credential management. +- `assets`: Media Hub file operations. +- `sessions`: Test session lifecycle. +- `history`: Audit log access. +- `chat`: Persistent AI chat interactions. +- `execution`: Core LLM pipeline (`/plan`, `/execute-step`, `/analyze-response`). --- @@ -213,6 +228,8 @@ Uses **SQLAlchemy 2.0** with a declarative base. Database URL is loaded from the | `ExecutionHistory` | `execution_history` | One row per step executed. Stores request JSON, response JSON, and AI analysis | | `Asset` | `assets` | Uploaded files (audio/video/docs). Tagged, categorized, linked to a spec | | `Collection` | `collections` | Named groups of assets. Many-to-many with Asset | +| `ChatSession` | `chat_sessions` | A persistent AI chat thread. Linked to a spec and optionally to a test session. Auto-named from first message. | +| `ChatMessage` | `chat_messages` | A single message within a chat session — stores role, content, and the structured `action_json` returned by the AI | #### Relationships @@ -221,6 +238,8 @@ SpecDocument ──< Asset (one-to-many) Asset >──< Collection (many-to-many via asset_collections junction table) SessionModel ──> SpecDocument (foreign key, nullable) ExecutionHistory ──> SessionModel (foreign key via session_id) +ChatSession ──> SpecDocument (foreign key, ON DELETE SET NULL, nullable) +ChatMessage ──> ChatSession (foreign key, ON DELETE CASCADE) ``` #### Connection Details @@ -245,9 +264,15 @@ All request/response validation is done via Pydantic v2 models: | `ExecutionPlan` | ← Backend | Response from `/plan` — list of steps + suggested credentials | | `StepDefinition` | — | Single step: step number, endpoint string, purpose text | | `ExecuteStepRequest` | → Backend | Payload for `/execute-step` — session ID, step def, context vars | -| `AnalyzeRequest` | → Backend | Payload for `/analyze-response` — step number, response data | -| `AnalyzeResponse` | ← Backend | AI analysis result — status, extracted variables, OTP flag | -| `CredentialUpdate` | → Backend | Payload for credential save — username, password, extra fields | +| `AnalyzeRequest` | → Backend | Payload for `/analyze-response` — step number, response data, optional `spec_id` and `step_definition` | +| `AnalyzeResponse` | ← Backend | AI analysis result — `status`, `is_otp_required`, `otp_field_name`, `is_selection_required`, `selection_field_name`, `selection_options`, `response_summary`, `extracted_variables`, `token_usage` | +| `SelectionOption` | — | A single choosable item when `is_selection_required=true` — `label` (display text) and `value` (ID to save) | +| `ContextUpdate` | → Backend | Payload for `POST /context/{session_id}` — `key` and `value` to merge into session context | +| `CredentialUpdate` | → Backend | Payload for credential save — `name`, `username`, `password`, `extra` dict. Passing `extra._clear=true` **wipes all stored credentials** for the spec. | +| `ChatRequest` | → Backend | Payload for `/chat` — message text, optional `chat_session_id`, spec context, execution plan, step results | +| `ChatResponse` | ← Backend | Structured AI response — `chat_session_id`, `chat_session_name`, `action`, `message`, optional `step`, `changes`, `assertions`, `explanation` | +| `ChatSessionSummary` | ← Backend | Summary of a chat session — id, name, spec_id, session_id, message count, timestamps | +| `ChatMessageSchema` | ← Backend | A single chat message — role, content, action_json, timestamp | | `TokenUsage` | ← Backend | LLM token counts (prompt, completion, total) | --- @@ -277,6 +302,7 @@ All request/response validation is done via Pydantic v2 models: - Returns `StepExecutionPlan` containing the complete HTTP request object (`method`, `url`, `headers`, `query_params`, `form_data`, `body`, `files`) - **Token-efficient schema notation** (shorthand): Instead of sending verbose JSON schema, converts it to compact strings like `{name*(str),age(int),status(str[active|inactive])}` - **Data source priority**: Context Variables → User Credentials → Step Definition +- **Asset Integration**: Consults `asset_service` to provide the LLM with a list of available files from the Media Hub. #### `response_analyzer.py` - Uses `ChatMistralAI` to analyze HTTP response (status code + body) @@ -499,9 +525,11 @@ User clicks "Execute" on a step | Method | Path | Description | |---|---|---| | `POST` | `/chat` | Send a message; returns structured AI action + saves the conversation to DB | -| `GET` | `/chat/sessions` | List all chat sessions for a given spec or test session | -| `GET` | `/chat/sessions/{chat_session_id}/messages` | Get full message history for a chat session | -| `DELETE` | `/chat/sessions/{chat_session_id}` | Delete a chat session and its messages | +| `GET` | `/chat-sessions` | List all chat sessions, optionally filtered by `spec_id` | +| `GET` | `/chat-sessions/{chat_session_id}` | Get a single chat session with its full message history | +| `PATCH` | `/chat-sessions/{chat_session_id}` | Rename a chat session | +| `DELETE` | `/chat-sessions/{chat_session_id}` | Delete a chat session and all its messages (cascade) | +| `DELETE` | `/chat-sessions` | Wipe **all** chat sessions and messages | ### History @@ -595,6 +623,26 @@ CREATE TABLE asset_collections ( collection_id VARCHAR REFERENCES collections(id), PRIMARY KEY (asset_id, collection_id) ); + +-- AI Chat sessions (each represents one conversation thread) +CREATE TABLE chat_sessions ( + id VARCHAR PRIMARY KEY, -- UUID + name VARCHAR NOT NULL, -- auto-named from first message; user-renameable + spec_id VARCHAR REFERENCES spec_documents(id) ON DELETE SET NULL, + session_id VARCHAR, -- linked API test session (nullable) + created_at TIMESTAMP DEFAULT NOW(), + updated_at TIMESTAMP DEFAULT NOW() +); + +-- Individual messages within a chat session +CREATE TABLE chat_messages ( + id SERIAL PRIMARY KEY, + chat_session_id VARCHAR REFERENCES chat_sessions(id) ON DELETE CASCADE NOT NULL, + role VARCHAR NOT NULL, -- 'user' | 'assistant' + content TEXT NOT NULL, -- raw message text + action_json JSON, -- structured action returned by AI (execute_step, explain, etc.) + timestamp TIMESTAMP DEFAULT NOW() +); ``` --- @@ -642,8 +690,10 @@ The `ChatAssistant` maintains conversation history in the database (`ChatSession | `DATABASE_URL` | ✅ | PostgreSQL connection string (e.g., `postgresql://user:pass@host/db`) | | `MISTRAL_API_KEY` | ✅ | API key for Mistral AI (used by LangChain) | | `SUPABASE_URL` | ⬜ | Supabase project URL (enables cloud storage; falls back to local if missing) | -| `SUPABASE_KEY` | ⬜ | Supabase service role key | -| `SUPABASE_BUCKET` | ⬜ | Name of the Supabase Storage bucket | +| `SUPABASE_SERVICE_ROLE_KEY` | ⬜ | **Preferred** Supabase key — bypasses Row-Level Security for storage operations | +| `SUPABASE_SECRET_KEY` | ⬜ | Alternative Supabase key name (checked if `SUPABASE_SERVICE_ROLE_KEY` is absent) | +| `SUPABASE_KEY` | ⬜ | Fallback Supabase key (used only if the two above are not set) | +| `SUPABASE_STORAGE_BUCKET` | ⬜ | Name of the Supabase Storage bucket (defaults to `"assets"` if not set) | > **Note:** The `DATABASE_URL` string starting with `postgres://` is auto-converted to `postgresql://` for SQLAlchemy compatibility. @@ -715,5 +765,5 @@ Use this table when explaining or comparing this project to similar tools: --- -*Documentation updated: April 2026* +*Documentation updated: April 2026 — reflects actual codebase state as of this date* *Project: AI API Testing Assistant | Stack: FastAPI + React + Mistral AI + PostgreSQL* diff --git a/Project_Diagrams.md b/documents/Project_Diagrams.md similarity index 81% rename from Project_Diagrams.md rename to documents/Project_Diagrams.md index cd67f82ad3619662e69c5ad4b0c0bf4414a6996d..492cf2a39d3fefc3267cf8e614898ea7f18c890e 100644 --- a/Project_Diagrams.md +++ b/documents/Project_Diagrams.md @@ -8,49 +8,45 @@ This document contains ready-to-use **Mermaid diagram code** for all major diagr *Shows the high-level structure of the entire system and how the four tiers interact.* ```mermaid -graph TB - subgraph "Presentation Tier" - FE["React + Vite Frontend
(SPA on port 5173)"] - M1["API Collections Module"] - M2["Execution Hub"] - M3["Test User Profile"] - M4["Execution History"] - M5["Media Hub"] - M6["AI Chat Assistant"] - end - - subgraph "Orchestration Tier" - API["FastAPI Backend
(Uvicorn on port 8001)"] - CORS["CORS Middleware"] - VALID["Request Validation
(Pydantic)"] +graph TD + subgraph "Frontend (React + Vite)" + UI[User Interface] + State[AppContext / State] + Axios[Axios API Client] + UI --> State + State --> Axios end - subgraph "Intelligence Tier" - LLM1["LLM Planner
(llm_planner.py)"] - LLM2["API Executor
(api_executor.py)"] - LLM3["Response Analyzer
(response_analyzer.py)"] - LLM4["Chat Assistant
(chat_assistant.py)"] - MISTRAL["Mistral-Large-Latest
via LangChain"] + subgraph "Orchestration (FastAPI)" + Router[main.py / APIRouters] + Core[Core Utilities / Config] + Services[Business Logic / Services] + Router --> Core + Router --> Services end - subgraph "Data Tier" - PG["PostgreSQL
(via Supabase)"] - SUP["Supabase File Storage
(Specs + Assets)"] + subgraph "Intelligence (LLM Services)" + Planner[LLM Planner] + Executor[API Executor] + Analyzer[Response Analyzer] + Chat[Chat Assistant] + Integrations[Integrations / LLM] + Services --> Planner + Services --> Executor + Services --> Analyzer + Services --> Chat + Planner & Executor & Analyzer & Chat --> Integrations end - subgraph "External" - TARGET["Target API Server
(Under Test)"] + subgraph "Data & Storage" + DB[(PostgreSQL / SQLAlchemy)] + Storage[Supabase / Local Storage] + Services --> DB + Services --> Storage end - FE --> CORS --> VALID --> API - API --> LLM1 - API --> LLM2 - API --> LLM3 - API --> LLM4 - LLM1 & LLM2 & LLM3 & LLM4 --> MISTRAL - API --> PG - API --> SUP - LLM2 --> TARGET + Axios <--> Router + Integrations <--> LLM_Provider[Mistral-Large API] ``` --- @@ -60,50 +56,43 @@ graph TB ```mermaid sequenceDiagram - actor User - participant FE as React Frontend - participant BE as FastAPI Backend - participant DB as PostgreSQL - participant LLM as Mistral AI - participant API as Target API - - User->>FE: Upload API spec file - FE->>BE: POST /upload-spec (file) - BE->>BE: Detect format (OpenAPI/Postman) - BE->>BE: parse_spec() → simplified endpoints - BE->>DB: INSERT SpecDocument (raw + parsed) - BE-->>FE: {spec_id, endpoints} - - User->>FE: Type natural language scenario - FE->>BE: POST /plan {scenario, spec_id} - BE->>DB: GET login_credentials for spec - BE->>LLM: generate_execution_plan(endpoints, scenario, creds) - LLM-->>BE: {execution_plan[], suggested_credentials} - BE->>DB: UPDATE SpecDocument (plan, merged creds) - BE-->>FE: {execution_plan[]} - - loop For each step in plan - User->>FE: Click "Execute Step N" - FE->>BE: POST /execute-step {step_def, session_id} - BE->>DB: GET full spec schema - BE->>BE: resolve_refs() + prune + shorthand - BE->>DB: GET context_variables for session - BE->>LLM: prepare_step_request(schema, context, creds) - LLM-->>BE: {StepExecutionPlan} - BE->>BE: sanitize_url() - BE->>API: HTTP request (method, url, headers, body) - API-->>BE: {status_code, body} - BE->>DB: INSERT ExecutionHistory (request + response) - BE-->>FE: {request, response} - - FE->>BE: POST /analyze-response {step, response} - BE->>LLM: analyze_api_response(status, body, purpose) - LLM-->>BE: {status, extracted_variables, is_otp_required} - BE->>DB: UPDATE context_variables (merge new vars) - BE->>DB: UPDATE ExecutionHistory (analysis) - BE-->>FE: {analysis_result} - FE-->>User: Show result + extracted variables + participant User + participant Frontend + participant FastAPI as router/execution.py + participant DB as database.py + participant LLM as integrations/llm + participant TargetAPI + + User->>Frontend: Clicks "Execute Step" + Frontend->>FastAPI: POST /execute-step (StepDef) + + rect rgb(240, 240, 240) + Note over FastAPI: PRE-EXECUTION PIPELINE + FastAPI->>DB: Fetch Spec (parsed_json) + FastAPI->>FastAPI: resolve_refs() + prune + shorthand + FastAPI->>DB: Fetch session context + profile + FastAPI->>LLM: prepare_step_request(schema, context) + LLM-->>FastAPI: {APIRequest} + FastAPI->>FastAPI: sanitize_url(url) end + + rect rgb(220, 240, 220) + Note over FastAPI: HTTP EXECUTION + FastAPI->>TargetAPI: httpx.send(request) + TargetAPI-->>FastAPI: {status, body, headers} + end + + FastAPI->>DB: Save to ExecutionHistory + FastAPI-->>Frontend: {request, response} + Frontend->>User: Display Result (Cards) + + Frontend->>FastAPI: POST /analyze-response {step, response} + FastAPI->>LLM: analyze_api_response(status, body, purpose) + LLM-->>FastAPI: {status, extracted_variables, is_otp_required} + FastAPI->>DB: UPDATE context_variables (merge new vars) + FastAPI->>DB: UPDATE ExecutionHistory (analysis) + FastAPI-->>Frontend: {analysis_result} + Frontend-->>User: Show result + extracted variables ``` --- @@ -113,8 +102,14 @@ sequenceDiagram ```mermaid erDiagram + SPEC_DOCUMENT ||--o{ SESSION_MODEL : "has" + SPEC_DOCUMENT ||--o{ ASSET : "linked to" + SESSION_MODEL ||--o{ EXECUTION_HISTORY : "logs" + CHAT_SESSION ||--o{ CHAT_MESSAGE : "contains" + SPEC_DOCUMENT ||--o{ CHAT_SESSION : "associated with" + SPEC_DOCUMENT { - string id PK + uuid id PK string name text raw_content json parsed_json @@ -122,44 +117,32 @@ erDiagram json login_credentials json execution_plan text scenario - string file_path - datetime uploaded_at } SESSION_MODEL { - string id PK - string spec_id FK - string spec_name - string base_url - text scenario + uuid id PK + uuid spec_id FK json context_variables - datetime created_at + string base_url } EXECUTION_HISTORY { int id PK - string session_id FK + uuid session_id FK int step json request_json json response_json json analysis_json json step_definition_json - datetime timestamp } ASSET { - string id PK + uuid id PK string name string file_type string file_path - int file_size string category - text description json tags - json metadata_json - string spec_id FK - datetime uploaded_at - } COLLECTION { string id PK diff --git a/README.md b/documents/README.md similarity index 58% rename from README.md rename to documents/README.md index 71dbca14a6c7cc65196eb29a7ecb3cae261670cd..4209e0c6958ec884c1645d40ed90e306c267d90f 100644 --- a/README.md +++ b/documents/README.md @@ -4,11 +4,13 @@ An automated API testing system powered by Mistral AI, FastAPI, and React. This ## Features -- **OpenAPI Parsing**: Upload your API spec (JSON or YAML) — extracts all endpoints, schemas, methods, and auth details. -- **LLM-Powered Execution Plan**: Mistral AI analyzes your natural-language intent and builds a step-by-step execution plan. -- **Dynamic API Execution**: Executes HTTP requests based on the plan, chaining responses automatically. -- **Context Management**: Extracts tokens/IDs from responses and passes them to subsequent steps. -- **React Frontend**: Modern UI built with React + Vite + Tailwind CSS. +- **Intelligent Planning**: Generates multi-step test scenarios from natural language. +- **Context-Aware Execution**: Automatically propagates tokens and variables (IDs, slugs, etc.) between steps. +- **Persistent AI Chat**: An interactive assistant that understands your full testing context and can control the workflow. +- **Media Hub**: Manage assets (images, PDFs, audio) for API requests requiring file uploads. +- **Zero-Code Testing**: Describe your intent (e.g., "Register, login, and update my profile picture") and let the AI handle the rest. +- **Format Agnostic**: Supports OpenAPI (Swagger) and Postman Collections. +- **Clean Organization**: Documentation is centrally managed in this `documents/` folder. ## Requirements @@ -57,6 +59,14 @@ cd frontend-react && npm run dev | Backend | http://localhost:8001 | | API Docs | http://localhost:8001/docs | +## 📂 Project Structure + +- `backend/`: FastAPI server with LLM pipeline logic. +- `frontend-react/`: Modern React dashboard built with Vite and Tailwind CSS. +- `documents/`: Comprehensive documentation, diagrams, and research notes (you are here). +- `start.ps1`: One-click startup script for Windows. +- `project_for_review.zip`: Optimized archive for sharing the project with reviewers. + ## Usage 1. Open the frontend at `http://localhost:5173`. diff --git a/Research_Paper.md b/documents/Research_Paper.md similarity index 99% rename from Research_Paper.md rename to documents/Research_Paper.md index 5b4eb9fde352dbd806f399a11973943f0f1a4bf9..d5fe74d68b7fe5045fc7780155bcf3c10cdcdd1d 100644 --- a/Research_Paper.md +++ b/documents/Research_Paper.md @@ -2,7 +2,7 @@ **Author:** [Your Name] **Affiliation:** [Your Institution / Department of Computer Science] -**Date:** April 2026 +**Date:** May 2026 --- @@ -80,7 +80,7 @@ Despite progress in individual components, a gap exists in the integration of LL ## 3. Methodology -This section provides a comprehensive and technically detailed description of the system's design philosophy, data architecture, algorithmic components, prompt engineering strategies, and end-to-end execution workflow. The system is founded on a novel **Triple-Stage AI Pipeline** — Strategic Planning, Context-Aware Execution, and Semantic Response Analysis — underpinned by a persistent, session-scoped state management system and a modular service architecture. +This section provides a comprehensive and technically detailed description of the system's design philosophy, data architecture, algorithmic components, prompt engineering strategies, and end-to-end execution workflow. The system is founded on a novel **Four-Stage AI Pipeline** — Strategic Planning, Context-Aware Execution, Semantic Response Analysis, and Conversational AI Assistance — underpinned by a persistent, session-scoped state management system and a modular service architecture. --- @@ -643,15 +643,15 @@ Both entry points share the same backend and conversation history, providing a s | Layer | Technology | Version / Notes | |---|---|---| | Backend Language | Python | 3.10+ | -| API Framework | FastAPI | ≥ 0.109.0 (async, auto-docs) | -| LLM Orchestration | LangChain + langchain-mistralai | 0.1.0 / 0.0.1 | -| LLM Model | Mistral Large Latest | `mistral-large-latest`, temp=0 | +| API Framework | FastAPI | ≥ 0.109.0 (async, routers) | +| LLM Orchestration | LangChain | ≥ 0.3.0 | +| LLM Model | Mistral Large | `mistral-large-latest`, temp=0 | | HTTP Client | httpx | ≥ 0.27.2 (async) | -| ORM | SQLAlchemy | 2.0.23 | +| ORM | SQLAlchemy | ≥ 2.0.36 | | Database | PostgreSQL (via Supabase) | + `psycopg2-binary` driver | | File Storage | Supabase Storage | SDK v2.28.2 | | Frontend Framework | React + Vite | ES6+, SWC compiler | -| Frontend Styling | Tailwind CSS | Glassmorphic Dark Design | +| Frontend Styling | Tailwind CSS | 4.x Modern Design | | ASGI Server | Uvicorn (dev) / Gunicorn (prod) | With standard extras | ### 4.2 FastAPI Endpoint Design diff --git a/UI_DOCUMENTATION.md b/documents/UI_DOCUMENTATION.md similarity index 90% rename from UI_DOCUMENTATION.md rename to documents/UI_DOCUMENTATION.md index 90eee12fd19cf5373f87f598239d637f12ca43ad..22fa6b2634b00563b31a28e157a16ead5a340a99 100644 --- a/UI_DOCUMENTATION.md +++ b/documents/UI_DOCUMENTATION.md @@ -5,13 +5,13 @@ This document provides a comprehensive overview of the User Interface (UI) funct ## 1. Activity Bar (Sidebar) The left sidebar provides global navigation across different modules of the application. -- **API Collections / Specs**: Browse, upload, and manage OpenAPI/Swagger specifications. -- **Execution Workflow (Testing)**: The core interface for defining test scenarios and executing steps. -- **Test User Profile**: Manage credentials and custom variables for the active collection. -- **Execution History**: View and review past test sessions and their detailed logs. -- **Media Hub**: Manage assets (images, audio, video, documents) used for API testing. -- **AI Chat Assistant**: A dedicated full-page AI assistant that understands your full testing context and can control the execution workflow through natural language commands. -- **System Settings**: Access global application configuration and preferences. +- **API Collections**: (`CollectionsPage.jsx`) Browse, upload, and manage OpenAPI/Swagger/Postman specifications. +- **Execution Workflow**: (Main `App.jsx` workspace) The core interface for defining scenarios and executing steps. +- **Test User Profile**: (`ProfilePage.jsx`) Manage credentials and variables for the active collection. +- **Execution History**: (`HistoryPage.jsx`) View past test sessions and their detailed logs. +- **Media Hub**: (`MediaHubPage.jsx`) Manage assets (images, audio, video, documents) for API testing. +- **AI Chat Assistant**: (`ChatAssistant.jsx`) Full-page AI context-aware interaction. +- **System Settings**: (`SettingsPage.jsx`) Global application configuration. --- diff --git a/frontend-react/src/App.css b/frontend-react/src/App.css deleted file mode 100644 index f90339d8f765fa2c69d9a341959a8ddb9fff5720..0000000000000000000000000000000000000000 --- a/frontend-react/src/App.css +++ /dev/null @@ -1,184 +0,0 @@ -.counter { - font-size: 16px; - padding: 5px 10px; - border-radius: 5px; - color: var(--accent); - background: var(--accent-bg); - border: 2px solid transparent; - transition: border-color 0.3s; - margin-bottom: 24px; - - &:hover { - border-color: var(--accent-border); - } - &:focus-visible { - outline: 2px solid var(--accent); - outline-offset: 2px; - } -} - -.hero { - position: relative; - - .base, - .framework, - .vite { - inset-inline: 0; - margin: 0 auto; - } - - .base { - width: 170px; - position: relative; - z-index: 0; - } - - .framework, - .vite { - position: absolute; - } - - .framework { - z-index: 1; - top: 34px; - height: 28px; - transform: perspective(2000px) rotateZ(300deg) rotateX(44deg) rotateY(39deg) - scale(1.4); - } - - .vite { - z-index: 0; - top: 107px; - height: 26px; - width: auto; - transform: perspective(2000px) rotateZ(300deg) rotateX(40deg) rotateY(39deg) - scale(0.8); - } -} - -#center { - display: flex; - flex-direction: column; - gap: 25px; - place-content: center; - place-items: center; - flex-grow: 1; - - @media (max-width: 1024px) { - padding: 32px 20px 24px; - gap: 18px; - } -} - -#next-steps { - display: flex; - border-top: 1px solid var(--border); - text-align: left; - - & > div { - flex: 1 1 0; - padding: 32px; - @media (max-width: 1024px) { - padding: 24px 20px; - } - } - - .icon { - margin-bottom: 16px; - width: 22px; - height: 22px; - } - - @media (max-width: 1024px) { - flex-direction: column; - text-align: center; - } -} - -#docs { - border-right: 1px solid var(--border); - - @media (max-width: 1024px) { - border-right: none; - border-bottom: 1px solid var(--border); - } -} - -#next-steps ul { - list-style: none; - padding: 0; - display: flex; - gap: 8px; - margin: 32px 0 0; - - .logo { - height: 18px; - } - - a { - color: var(--text-h); - font-size: 16px; - border-radius: 6px; - background: var(--social-bg); - display: flex; - padding: 6px 12px; - align-items: center; - gap: 8px; - text-decoration: none; - transition: box-shadow 0.3s; - - &:hover { - box-shadow: var(--shadow); - } - .button-icon { - height: 18px; - width: 18px; - } - } - - @media (max-width: 1024px) { - margin-top: 20px; - flex-wrap: wrap; - justify-content: center; - - li { - flex: 1 1 calc(50% - 8px); - } - - a { - width: 100%; - justify-content: center; - box-sizing: border-box; - } - } -} - -#spacer { - height: 88px; - border-top: 1px solid var(--border); - @media (max-width: 1024px) { - height: 48px; - } -} - -.ticks { - position: relative; - width: 100%; - - &::before, - &::after { - content: ''; - position: absolute; - top: -4.5px; - border: 5px solid transparent; - } - - &::before { - left: 0; - border-left-color: var(--border); - } - &::after { - right: 0; - border-right-color: var(--border); - } -} diff --git a/frontend-react/src/App.jsx b/frontend-react/src/App.jsx index 8d26488bd745c40ffcc4063966d2a09a7b92b169..b4889805df05acb6a591c5739caf4466dc121cba 100644 --- a/frontend-react/src/App.jsx +++ b/frontend-react/src/App.jsx @@ -1,9 +1,10 @@ import React, { useState, useRef, useCallback } from 'react'; -import { - MessageSquare, LayoutTemplate, Settings, Database, History, - ChevronRight, ChevronLeft, Send, Zap, Trash2, RefreshCw, +import { + MessageSquare, LayoutTemplate, Settings, Database, History, + ChevronRight, ChevronLeft, Send, Zap, Trash2, RefreshCw, Upload, Image, FileText, Music, Video, Filter, - User, Layers, FolderOpen, Globe, X, Bot, Code + User, Layers, FolderOpen, Globe, X, Bot, Code, + Activity, Sparkles, Terminal, CheckCircle2, AlertCircle, Circle } from 'lucide-react'; import { AppProvider, useAppContext } from './components/AppContext'; import CollectionsPage from './components/CollectionsPage'; @@ -12,57 +13,249 @@ import HistoryPage from './components/HistoryPage'; import MediaHubPage from './components/MediaHubPage'; import SettingsPage from './components/SettingsPage'; import ChatAssistant from './components/ChatAssistant'; +import { ToastContainer } from './components/Toast'; +import ErrorBoundary from './components/ErrorBoundary'; import { SpecService, SessionService, PlanService, CredentialService, AssetService } from './services/api'; +/* --------------------------------------------------------------- + Design System — injected once at mount. + Corporate / refined / Linear-Stripe inspired palette. + --------------------------------------------------------------- */ +const DesignSystem = () => ( + +); + +/* --------------------------------------------------------------- + Sidebar nav button — single source of truth + --------------------------------------------------------------- */ +const NavButton = ({ icon: Icon, label, active, onClick }) => ( + +); + function MainApp() { - const { - collections, loadSpec, deleteSpec, + const { + collections, loadSpec, deleteSpec, activeSpecId, activeSpecName, activeBaseUrl, setActiveBaseUrl, isLoading, fetchCollections, storedCredentials, setStoredCredentials, saveProfile, parsedSpec, - sessionContextVars, refreshSessionContext, sessionId, + sessionContextVars, refreshSessionContext, sessionId, executionPlan, setExecutionPlan, scenario, setScenario, - startNewSession + startNewSession, + toasts, addToast, removeToast, } = useAppContext(); - + const [activePage, setActivePage] = useState('testing'); const [rightPanelOpen, setRightPanelOpen] = useState(true); - const [rightPanelMode, setRightPanelMode] = useState('context'); // 'context' | 'chat' + const [rightPanelMode, setRightPanelMode] = useState('context'); - // Global Navigation Listener React.useEffect(() => { const handleNav = (e) => setActivePage(e.detail); window.addEventListener('nav-change', handleNav); return () => window.removeEventListener('nav-change', handleNav); }, []); - + const fileInputRef = useRef(null); const [isUploading, setIsUploading] = useState(false); - // Chat/Execution State - // scenario input is handled by AppContext now const [isPlanning, setIsPlanning] = useState(false); const [executingStep, setExecutingStep] = useState(null); + const [isRunningAll, setIsRunningAll] = useState(false); const [stepResults, setStepResults] = useState({}); - const [otpPrompt, setOtpPrompt] = useState(null); // { stepDef, otpFieldName } + const [otpPrompt, setOtpPrompt] = useState(null); const [otpInput, setOtpInput] = useState(''); - const [selectionPrompt, setSelectionPrompt] = useState(null); // { stepDef, fieldName, options } + const [selectionPrompt, setSelectionPrompt] = useState(null); - // Media Hub / Assets State const [assets, setAssets] = useState([]); const [isAssetLoading, setIsAssetLoading] = useState(false); - const [activeAssetCategory, setActiveAssetCategory] = useState('all'); // all, audio, video, documents + const [activeAssetCategory, setActiveAssetCategory] = useState('all'); const assetFileInputRef = useRef(null); - const [planTokenUsage, setPlanTokenUsage] = useState(null); const [totalScenarioTokens, setTotalScenarioTokens] = useState({ prompt: 0, completion: 0, total: 0 }); const handleGeneratePlan = async () => { - if (!activeSpecId) return alert("Please select a collection first."); - if (!activeBaseUrl) return alert("Please set a Base URL for this collection."); - if (!scenario.trim()) return alert("Please describe a test scenario."); + if (!activeSpecId) { addToast('warning', "Please select a collection first."); return; } + if (!activeBaseUrl) { addToast('warning', "Please set a Base URL for this collection."); return; } + if (!scenario.trim()) { addToast('warning', "Please describe a test scenario."); return; } setIsPlanning(true); setPlanTokenUsage(null); @@ -98,642 +291,815 @@ function MainApp() { setStepResults({}); } catch (err) { console.error("Failed to generate plan", err); - alert("Plan generation failed. See console."); + addToast('error', err.message || "Plan generation failed."); } finally { setIsPlanning(false); } }; const executeStep = async (stepDef) => { - setExecutingStep(stepDef.step); - - try { - if (!sessionId) { - alert("Session not initialized. Please refresh the page."); - return; + setExecutingStep(stepDef.step); + + try { + if (!sessionId) { + addToast('error', "Session not initialized. Please refresh the page."); + return; + } + if (!activeBaseUrl) { + addToast('warning', "Base URL is missing. Please set it in the header."); + return; + } + + await refreshSessionContext(); + + const payload = { + session_id: sessionId, + step_definition: stepDef, + context_variables: sessionContextVars || {}, + user_credentials: storedCredentials || {}, + base_url: activeBaseUrl, + spec_id: activeSpecId + }; + + console.log("Executing Step Payload:", payload); + + const execData = await PlanService.executeStep(payload); + + setStepResults(prev => ({ + ...prev, + [stepDef.step]: { + request: execData.executed_request, + response: execData.response, + analysis: { status: 'pending', response_summary: 'Analyzing response with AI...' }, + token_usage: execData.token_usage || { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 } } - if (!activeBaseUrl) { - alert("Base URL is missing. Please set it in the header."); - return; + })); + + const analyzePayload = { + session_id: sessionId, + step: stepDef.step, + response_data: execData.response, + step_definition: stepDef + }; + const analyzeData = await PlanService.analyzeResponse(analyzePayload); + + const stepTokens = { + prompt_tokens: (execData.token_usage?.prompt_tokens || 0) + (analyzeData.token_usage?.prompt_tokens || 0), + completion_tokens: (execData.token_usage?.completion_tokens || 0) + (analyzeData.token_usage?.completion_tokens || 0), + total_tokens: (execData.token_usage?.total_tokens || 0) + (analyzeData.token_usage?.total_tokens || 0) + }; + + setStepResults(prev => ({ + ...prev, + [stepDef.step]: { + ...prev[stepDef.step], + analysis: analyzeData, + token_usage: stepTokens } + })); + + setTotalScenarioTokens(prev => ({ + prompt: prev.prompt + stepTokens.prompt_tokens, + completion: prev.completion + stepTokens.completion_tokens, + total: prev.total + stepTokens.total_tokens + })); + + await refreshSessionContext(); + + if (analyzeData.is_otp_required) { + setOtpPrompt({ stepDef, otpFieldName: analyzeData.otp_field_name || 'otp' }); + } + + if (analyzeData.is_selection_required && analyzeData.selection_options?.length > 0) { + setSelectionPrompt({ + stepDef, + fieldName: analyzeData.selection_field_name || 'selected_item', + options: analyzeData.selection_options, + responseBody: execData.response.body + }); + } + + } catch (err) { + console.error(`Execution failed for step ${stepDef.step}`, err); + } finally { + setExecutingStep(null); + } + }; - await refreshSessionContext(); - - const payload = { - session_id: sessionId, - step_definition: stepDef, - context_variables: sessionContextVars || {}, - user_credentials: storedCredentials || {}, - base_url: activeBaseUrl, - spec_id: activeSpecId - }; - - console.log("Executing Step Payload:", payload); - - // Step 1: Execute - const execData = await PlanService.executeStep(payload); - - // Display early results so user sees the raw response immediately - setStepResults(prev => ({ - ...prev, - [stepDef.step]: { - request: execData.executed_request, - response: execData.response, - analysis: { status: 'pending', response_summary: 'Analyzing response with AI...' }, - token_usage: execData.token_usage || { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 } - } - })); - - // Step 2: Analyze - const analyzePayload = { - session_id: sessionId, - step: stepDef.step, - response_data: execData.response, - step_definition: stepDef - }; - const analyzeData = await PlanService.analyzeResponse(analyzePayload); - - // Calculate cumulative tokens for this specific step (Prep + Analysis) - const stepTokens = { - prompt_tokens: (execData.token_usage?.prompt_tokens || 0) + (analyzeData.token_usage?.prompt_tokens || 0), - completion_tokens: (execData.token_usage?.completion_tokens || 0) + (analyzeData.token_usage?.completion_tokens || 0), - total_tokens: (execData.token_usage?.total_tokens || 0) + (analyzeData.token_usage?.total_tokens || 0) - }; - - // Update with final analysis - setStepResults(prev => ({ - ...prev, - [stepDef.step]: { - ...prev[stepDef.step], - analysis: analyzeData, - token_usage: stepTokens - } - })); - - // Update cumulative scenario total - setTotalScenarioTokens(prev => ({ - prompt: prev.prompt + stepTokens.prompt_tokens, - completion: prev.completion + stepTokens.completion_tokens, - total: prev.total + stepTokens.total_tokens - })); - - await refreshSessionContext(); - - // Handle OTP requirement - if (analyzeData.is_otp_required) { - setOtpPrompt({ stepDef, otpFieldName: analyzeData.otp_field_name || 'otp' }); - } - - // Handle Selection requirement - if (analyzeData.is_selection_required && analyzeData.selection_options?.length > 0) { - setSelectionPrompt({ - stepDef, - fieldName: analyzeData.selection_field_name || 'selected_item', - options: analyzeData.selection_options, - responseBody: execData.response.body - }); - } - - } catch (err) { - console.error(`Execution failed for step ${stepDef.step}`, err); - } finally { - setExecutingStep(null); - } + const runAllSteps = async () => { + if (!executionPlan?.length) return; + setIsRunningAll(true); + for (const step of executionPlan) { + // Stop if an OTP or selection prompt is waiting — user must resolve it manually + if (otpPrompt || selectionPrompt) break; + await executeStep(step); + } + setIsRunningAll(false); }; const handleOtpSubmit = async () => { if (!otpInput.trim()) return; const { stepDef, otpFieldName } = otpPrompt; - + try { - // Save OTP to Extra Fields - const payload = { - extra: { [otpFieldName]: otpInput } - }; - - // Use saveProfile from context to update both DB and local state + const payload = { extra: { [otpFieldName]: otpInput } }; await saveProfile(payload); - - // Clear OTP state setOtpPrompt(null); setOtpInput(''); - - // Refresh context (though saveProfile already updated local storedCredentials) await refreshSessionContext(); - alert(`OTP "${otpInput}" saved to profile as "${otpFieldName}". You can now proceed.`); + addToast('success', `OTP "${otpInput}" saved as "${otpFieldName}". You can now proceed.`); } catch (err) { console.error("Failed to save OTP", err); - alert("Failed to save OTP. See console."); + addToast('error', "Failed to save OTP."); } }; const handleSelectionSubmit = async (option) => { if (!selectionPrompt) return; const { fieldName } = selectionPrompt; - + try { await SessionService.updateContext(sessionId, fieldName, option.value); - - // Clear selection state setSelectionPrompt(null); - - // Refresh context so next steps see it await refreshSessionContext(); } catch (err) { console.error("Failed to save selection", err); - alert("Failed to save selection. See console."); + addToast('error', "Failed to save selection."); } }; + const navItems = [ + { id: 'collections', icon: Layers, label: 'Collections' }, + { id: 'testing', icon: Activity, label: 'Workflow' }, + { id: 'profile', icon: User, label: 'Test Profile' }, + { id: 'history', icon: History, label: 'History' }, + { id: 'media', icon: FolderOpen, label: 'Media Hub' }, + { id: 'chat', icon: Bot, label: 'AI Assistant' }, + { id: 'settings', icon: Settings, label: 'Settings' }, + ]; + return ( -
- - {/* 1. LEFT SIDEBAR (Activity Bar) */} -
- {/* Activity Bar */} -
- {/* Logo */} -
- -
- - - + <> + +
- + {/* ===== LEFT SIDEBAR ===== */} + - + {/* ===== MAIN CONTENT ===== */} +
- + {activePage === 'testing' && ( +
- -
-
+ {/* TOP BAR */} +
+
+
+
+

+ {activeSpecName !== 'None' ? activeSpecName : 'Workflow Studio'} +

+
-
- - {activePage === 'testing' && ( -
- {/* Header / Tabs */} -
-
- - {activeSpecName !== 'None' ? activeSpecName : 'workflow.plan'} - {planTokenUsage && ( - - LLM Plan: {planTokenUsage.total_tokens} - - )} - {totalScenarioTokens.total > 0 && ( - - Scenario Total: {totalScenarioTokens.total} tokens - - )} -
- {/* Base URL Input in Header */} - {activeSpecId && ( -
- BASE_URL: - setActiveBaseUrl(e.target.value)} - onBlur={async () => { + {(planTokenUsage || totalScenarioTokens.total > 0) && ( +
+ {planTokenUsage && ( +
+ + Plan + {planTokenUsage.total_tokens} +
+ )} + {totalScenarioTokens.total > 0 && ( +
+ + Total + {totalScenarioTokens.total} +
+ )} +
+ )} +
+ + {activeSpecId && ( +
+ Base URL +
+ + + + setActiveBaseUrl(e.target.value)} + onBlur={async () => { if (activeBaseUrl) { try { await SpecService.updateBaseUrl(activeSpecId, activeBaseUrl); } - catch(e) { console.error("URL Save failed", e); } + catch (e) { console.error("URL Save failed", e); } } - }} - placeholder="https://api.example.com" - /> + }} + placeholder="https://api.example.com" + /> +
- )} -
+ )} +
- {/* Workflow Feed Area */} -
-
- {!executionPlan ? ( -
- -

AI API Testing Assistant

-

- {activeSpecId ? "Specify a test scenario below to generate an API execution workflow." : "Load a collection from the sidebar to begin."} + {/* WORKFLOW FEED */} +

+
+ + {!executionPlan ? ( +
+
+
+
+ +
+
+

+ AI-Powered API Testing +

+

+ {activeSpecId + ? "Describe a test scenario below and Claude will generate a complete execution workflow for your API." + : "Start by loading an API collection from the sidebar."}

-
- ) : ( -
- {executionPlan.map((step) => { - const isRunning = executingStep === step.step; - const res = stepResults[step.step]; - return ( -
-
-
-
- Step {step.step} - {step.endpoint} -
-

{step.purpose}

+ + {!activeSpecId && ( + + )} +
+ ) : ( +
+
+
+

Execution Plan

+

{executionPlan.length} step{executionPlan.length !== 1 ? 's' : ''}

+
+
+
+
+ {Object.values(stepResults).filter(r => r.analysis?.status === 'success').length} completed +
+ +
+
+ + {executionPlan.map((step, idx) => { + const isRunning = executingStep === step.step; + const res = stepResults[step.step]; + const status = res?.analysis?.status; + + return ( +
+ {/* Step header */} +
+
+ {/* Step indicator */} +
+ {status === 'success' ? ( +
+ +
+ ) : status === 'failed' ? ( +
+ +
+ ) : isRunning ? ( +
+ +
+ ) : ( +
+ {step.step} +
+ )} +
+ +
+
+ {step.endpoint}
-
+
+ +
+ {status === 'failure' && ( + -
- - {/* Results Dropdown Area */} - {res && ( -
- {/* Analysis */} -
-
-
AI Analysis
- {res.token_usage && ( -
- {res.token_usage.total_tokens} tokens (P:{res.token_usage.prompt_tokens} / C:{res.token_usage.completion_tokens}) -
- )} -
-
{res.analysis.response_summary || (res.analysis.status === 'success' ? 'Step completed successfully.' : 'Step encountered an error.')}
- {res.analysis.extracted_variables && Object.keys(res.analysis.extracted_variables).length > 0 && ( -
Extracted: {JSON.stringify(res.analysis.extracted_variables)}
+ )} + +
+
+ + {/* Results */} + {res && ( +
+ {/* AI Analysis bar */} +
+ +
+
+ AI Analysis + {res.token_usage && ( + + {res.token_usage.total_tokens} tok · P:{res.token_usage.prompt_tokens} C:{res.token_usage.completion_tokens} + )}
- -
- {/* Request Container */} -
-
Request
-
-
{res.request.method} {res.request.url}
- - {res.request.headers && Object.keys(res.request.headers).length > 0 && ( -
-
Headers
- {Object.entries(res.request.headers).map(([k,v]) => ( -
{k}: {v}
- ))} -
- )} - - {res.request.query_params && Object.keys(res.request.query_params).length > 0 && ( -
-
Query Params
- {Object.entries(res.request.query_params).map(([k,v]) => ( -
{k}={v}
- ))} -
- )} - - {res.request.body &&
-
Body
-
{JSON.stringify(res.request.body, null, 2)}
-
} - - {res.request.form_data && Object.keys(res.request.form_data).length > 0 && ( -
-
Form Data
-
{JSON.stringify(res.request.form_data, null, 2)}
-
- )} - - {res.request.files && Object.keys(res.request.files).length > 0 && ( -
-
Files
- {Object.entries(res.request.files).map(([k,v]) => ( -
{k}: {v}
- ))} -
- )} -
+

+ {res.analysis.response_summary || (status === 'success' ? 'Step completed successfully.' : 'Step encountered an error.')} +

+ {res.analysis.extracted_variables && Object.keys(res.analysis.extracted_variables).length > 0 && ( +
+ {Object.entries(res.analysis.extracted_variables).map(([k, v]) => ( + + {k} + = + {String(v)} + + ))}
- {/* Response Container */} -
-
Response ({res.response.status_code})
-
-
-                                              {(() => {
-                                                 if (!res.response.body) return (Empty Response from API);
-                                                 try { return JSON.stringify(JSON.parse(res.response.body), null, 2); } 
-                                                 catch { return String(res.response.body); }
-                                              })()}
-                                            
-
+ )} +
+
+ {/* Request / Response grid */} +
+ {/* Request */} +
+
+ Request +
+ {!res.request ? ( +
+ Preparing request…
+ ) : ( +
+
+ + {res.request.method || 'REQ'} + + {res.request.url || '(no url)'} +
+ + {res.request.headers && Object.keys(res.request.headers).length > 0 && ( +
+
Headers
+
+ {Object.entries(res.request.headers).map(([k, v]) => ( +
{k}:{String(v)}
+ ))} +
+
+ )} + + {res.request.query_params && Object.keys(res.request.query_params).length > 0 && ( +
+
Query Params
+
+ {Object.entries(res.request.query_params).map(([k, v]) => ( +
{k}={String(v)}
+ ))} +
+
+ )} + + {res.request.body !== undefined && res.request.body !== null && ( +
+
Body
+
{
+                                              typeof res.request.body === 'string'
+                                                ? res.request.body
+                                                : JSON.stringify(res.request.body, null, 2)
+                                            }
+
+ )} + + {res.request.form_data && Object.keys(res.request.form_data).length > 0 && ( +
+
Form Data
+
{JSON.stringify(res.request.form_data, null, 2)}
+
+ )} + + {res.request.files && Object.keys(res.request.files).length > 0 && ( +
+
Files
+
+ {Object.entries(res.request.files).map(([k, v]) => ( +
{k}:{String(v)}
+ ))} +
+
+ )} +
+ )} +
+ + {/* Response */} +
+
+ Response + {res.response?.status_code !== undefined && ( + = 200 && res.response.status_code < 300 ? 'bg-[var(--success-bg)] text-[var(--success)]' : + res.response.status_code >= 400 ? 'bg-[var(--danger-bg)] text-[var(--danger)]' : + 'bg-[var(--warning-bg)] text-[var(--warning)]'}`}> + {res.response.status_code} + + )}
-
- )} -
- ); + {!res.response ? ( +
+ Waiting for response… +
+ ) : ( +
+                                        {(() => {
+                                          const body = res.response.body;
+                                          if (body === undefined || body === null || body === '') {
+                                            return (empty body — status {res.response.status_code ?? 'unknown'});
+                                          }
+                                          if (typeof body === 'object') {
+                                            try { return JSON.stringify(body, null, 2); }
+                                            catch { return String(body); }
+                                          }
+                                          // body is a string
+                                          try { return JSON.stringify(JSON.parse(body), null, 2); }
+                                          catch { return String(body); }
+                                        })()}
+                                      
+ )} +
+
+
+ )} +
+ ); })} -
- )} - - {/* OTP PROMPT MODAL/OVERLAY */} - {otpPrompt && ( -
-
-
-

- - OTP Required -

- +
+ )} + + {/* OTP MODAL */} + {otpPrompt && ( +
+
+
+
+
+ +
+

Verification Required

+
+
-
-

- The API response indicates that an OTP or verification code is required. - Please enter it below to save it to your session context. -

-
- - setOtpInput(e.target.value)} - onKeyDown={e => { if (e.key === 'Enter') handleOtpSubmit(); }} - /> -
-
- - -
+
+

+ The API response indicates a verification code is required. Enter it below to save it to your session context. +

+
+ + setOtpInput(e.target.value)} + onKeyDown={e => { if (e.key === 'Enter') handleOtpSubmit(); }} + /> +
+
+ + +
-
-
- )} - - {/* SELECTION PROMPT MODAL/OVERLAY */} - {selectionPrompt && ( -
-
-
-

- - Selection Required -

- +
+
+ )} + + {/* SELECTION MODAL */} + {selectionPrompt && ( +
+
+
+
+
+ +
+

Choose an Option

+
+
-

- Multiple options found. Please select one to save as "{selectionPrompt.fieldName}". -

-
- {selectionPrompt.options.map((opt, idx) => ( - - ))} -
- - {selectionPrompt.responseBody && ( -
-
- - View Raw API Response - -
-
-                                        {(() => {
-                                           try { return JSON.stringify(JSON.parse(selectionPrompt.responseBody), null, 2); } 
-                                           catch { return selectionPrompt.responseBody; }
-                                        })()}
-                                      
-
-
-
- )} - -
-
+
+ {typeof opt.value === 'object' ? JSON.stringify(opt.value) : String(opt.value)} +
-
-
-
-
- )} -
-
+ ))} +
+ {selectionPrompt.responseBody && ( +
+ + + Raw API Response + +
+                                {(() => {
+                                  try { return JSON.stringify(JSON.parse(selectionPrompt.responseBody), null, 2); }
+                                  catch { return selectionPrompt.responseBody; }
+                                })()}
+                              
+
+ )} - {/* Bottom Scenario Input — always visible */} -
-
-
- -
-