File size: 10,886 Bytes
8085285
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# System Architecture β€” AI API Testing Assistant

---

## 1. High-Level System Overview

```mermaid
graph TB
    subgraph CLIENT["πŸ–₯️ Client Layer"]
        UI["React + Vite SPA<br/>(frontend-react)"]
    end

    subgraph BACKEND["βš™οΈ Backend Layer (FastAPI)"]
        MW["Middleware Stack<br/>(CORS Β· JWT Auth Β· Rate Limiter Β· Logger)"]
        API["REST API Router<br/>/api/v1"]
        SVC["Service Layer"]
        INT["Integrations"]
    end

    subgraph DATA["πŸ—„οΈ Data Layer"]
        PG[("PostgreSQL<br/>(via SQLAlchemy)")]
        FS["File Storage<br/>(Uploads Dir / Supabase)"]
        ALB["Alembic<br/>Migrations"]
    end

    subgraph LLM["πŸ€– LLM Providers"]
        MISTRAL["Mistral AI"]
        OPENAI["OpenAI"]
        ANTHROPIC["Anthropic Claude"]
        GEMINI["Google Gemini"]
        GROQ["Groq"]
    end

    subgraph EXT["🌐 External APIs Under Test"]
        TARGET["Target REST APIs<br/>(user-specified base URL)"]
    end

    UI -->|"HTTPS / REST (JWT)"| MW
    MW --> API
    API --> SVC
    SVC --> INT
    SVC --> PG
    SVC --> FS
    INT -->|"LangChain"| MISTRAL
    INT -->|"LangChain"| OPENAI
    INT -->|"LangChain"| ANTHROPIC
    INT -->|"LangChain"| GEMINI
    INT -->|"LangChain"| GROQ
    SVC -->|"HTTP Requests"| TARGET
    ALB -.->|"schema migrations"| PG
```

---

## 2. Frontend Architecture (React SPA)

```mermaid
graph TD
    subgraph ENTRY["Entry Point"]
        MAIN["main.jsx"]
        APP["App.jsx<br/>(routing + layout)"]
    end

    subgraph PAGES["Pages"]
        SPECS["SpecsPage<br/>Upload & manage API specs"]
        EXEC["Execution<br/>(workflow/WorkflowPage)"]
        HIST["HistoryPage<br/>Test run history"]
        CHAT["ChatAssistant<br/>AI assistant chat"]
        MEDIA["MediaHubPage<br/>Assets & media"]
        COLL["CollectionsPage<br/>Asset collections"]
        USAGE["UsagePage<br/>LLM token usage"]
        SETTINGS["SettingsPage<br/>LLM provider config"]
        PROFILE["ProfilePage<br/>User profile"]
        LOGIN["LoginPage<br/>JWT auth"]
    end

    subgraph SHARED["Shared Components"]
        CTX["AppContext.jsx<br/>(React Context β€” global state)"]
        TOAST["Toast.jsx"]
        ERR["ErrorBoundary.jsx"]
    end

    subgraph SVCL["API Service Layer"]
        AXIO["services/<br/>Axios calls to /api/v1"]
    end

    MAIN --> APP
    APP --> CTX
    APP --> PAGES
    PAGES --> SVCL
    SVCL -->|"REST + JWT Bearer"| BACKEND
    PAGES --> SHARED
```

---

## 3. Backend API Routes

```mermaid
graph LR
    subgraph ROUTER["FastAPI Router  (/api/v1)"]
        AUTH["/auth<br/>πŸ”“ Public<br/>Login Β· Register"]
        SPEC["/specs<br/>πŸ” Protected<br/>Upload Postman / Swagger"]
        SESS["/sessions<br/>πŸ” Protected<br/>CRUD test sessions"]
        EXEC["/execution<br/>πŸ” Protected<br/>Plan Β· Run Β· Analyze steps"]
        HIST["/history<br/>πŸ” Protected<br/>Execution history"]
        CHAT["/chat<br/>πŸ” Protected<br/>AI assistant messages"]
        ASSET["/assets<br/>πŸ” Protected<br/>File uploads (media/docs)"]
        USE["/usage<br/>πŸ” Protected<br/>LLM token usage stats"]
        SET["/settings<br/>πŸ” Protected<br/>LLM provider & model config"]
    end

    AUTH -->|"issues JWT"| CLIENT
    SPEC --> postman_parser & swagger_parser
    EXEC --> llm_planner & api_executor & response_analyzer
    CHAT --> chat_assistant
    ASSET --> asset_service
    USE --> usage_service
    SET --> llm_factory["LLM Factory<br/>(provider registry)"]
```

---

## 4. Service Layer Internals

```mermaid
graph TD
    subgraph SERVICES["backend/services/"]
        PP["postman_parser.py<br/>Parse Postman collections"]
        SP["swagger_parser.py<br/>Parse OpenAPI / Swagger specs"]
        LP["llm_planner.py<br/>Generate AI execution plans"]
        AE["api_executor.py<br/>Execute HTTP steps"]
        RA["response_analyzer.py<br/>Analyze API responses with LLM"]
        CA["chat_assistant.py<br/>AI chat with context"]
        AS["asset_service.py<br/>Manage file assets"]
        CM["context_manager.py<br/>Track session variables"]
        ST["storage.py<br/>File I/O & Supabase storage"]
        US["usage_service.py<br/>Track LLM token consumption"]
    end

    PP -->|parsed endpoints| LP
    SP -->|parsed endpoints| LP
    LP -->|execution plan JSON| AE
    AE -->|request + response| RA
    RA -->|analysis JSON| DB[(PostgreSQL)]
    CA -->|chat history| DB
    CM -->|context vars| AE
    AS --> ST
    US --> DB
    LP --> LLM_INT["LLM Integration Layer"]
    RA --> LLM_INT
    CA --> LLM_INT
```

---

## 5. LLM Integration Layer

```mermaid
graph TD
    subgraph INTEGRATIONS["backend/integrations/llm/"]
        FAC["factory.py<br/>LLMClientFactory"]
        REG["registry.py<br/>Provider Registry"]
        RES["resolver.py<br/>Task→Provider resolver"]
        BASE["base.py<br/>BaseLLMClient (ABC)"]
        PROV["providers.py<br/>Concrete Clients"]
    end

    subgraph PROVIDERS["LLM Provider Clients"]
        MC["MistralClient"]
        OC["OpenAIClient"]
        AC["AnthropicClient"]
        GC["GeminiClient"]
        GRC["GroqClient"]
    end

    subgraph TASK_CONFIG["DB: llm_task_config"]
        T1["plan_generation β†’ provider + model"]
        T2["step_execution β†’ provider + model"]
        T3["response_analysis β†’ provider + model"]
        T4["chat β†’ provider + model"]
    end

    FAC --> REG
    REG --> RES
    RES -->|reads| TASK_CONFIG
    FAC --> BASE
    BASE --> PROV
    PROV --> MC & OC & AC & GC & GRC
    MC -->|"LangChain"| MistralAI_API["Mistral AI API"]
    OC -->|"LangChain"| OpenAI_API["OpenAI API"]
    AC -->|"LangChain"| Anthropic_API["Anthropic API"]
    GC -->|"LangChain"| Gemini_API["Google Gemini API"]
    GRC -->|"LangChain"| Groq_API["Groq API"]
```

---

## 6. Database Schema (ERD)

```mermaid
erDiagram
    spec_documents {
        string id PK
        string name
        text raw_content
        json parsed_json
        string base_url
        json login_credentials
        json execution_plan
        text scenario
        string file_path
        datetime uploaded_at
    }

    sessions {
        string id PK
        string user_id
        string spec_id FK
        string spec_name
        string base_url
        text scenario
        json context_variables
        datetime created_at
    }

    execution_history {
        int id PK
        string session_id
        int step
        json request_json
        json response_json
        json analysis_json
        json step_definition_json
        datetime timestamp
    }

    collections {
        string id PK
        string name
        text description
        string spec_id FK
        datetime created_at
    }

    assets {
        string 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
    }

    asset_collections {
        string asset_id FK
        string collection_id FK
    }

    chat_sessions {
        string id PK
        string user_id
        string name
        string spec_id FK
        string session_id
        datetime created_at
        datetime updated_at
    }

    chat_messages {
        int id PK
        string chat_session_id FK
        string role
        text content
        json action_json
        datetime timestamp
    }

    llm_usage {
        int id PK
        string session_id
        string user_id
        string call_type
        int prompt_tokens
        int completion_tokens
        int total_tokens
        datetime timestamp
    }

    llm_providers {
        string provider_id PK
        json api_key_enc
        boolean is_active
    }

    llm_task_config {
        string task_name PK
        string provider_id FK
        string model_name
        float temperature
    }

    spec_documents ||--o{ sessions : "has"
    spec_documents ||--o{ assets : "has"
    spec_documents ||--o{ collections : "has"
    spec_documents ||--o{ chat_sessions : "linked to"
    assets }o--o{ collections : "asset_collections"
    chat_sessions ||--o{ chat_messages : "contains"
    llm_providers ||--o{ llm_task_config : "configures"
```

---

## 7. Authentication & Security Flow

```mermaid
sequenceDiagram
    participant Browser as πŸ–₯️ Browser (React)
    participant API as βš™οΈ FastAPI
    participant JWT as πŸ”‘ JWT Middleware
    participant DB as πŸ—„οΈ PostgreSQL
    participant Enc as πŸ”’ Fernet Encryption

    Browser->>API: POST /api/v1/auth/login
    API->>DB: validate credentials
    DB-->>API: user record
    API-->>Browser: JWT token (HS256)

    Browser->>API: GET /api/v1/specs (Bearer token)
    API->>JWT: extract_user_state middleware
    JWT->>JWT: decode JWT (HS256 + JWT_SECRET)
    JWT-->>API: request.state.user = user_id
    API->>API: verify_token dependency
    API->>DB: fetch protected data
    DB-->>API: result
    API-->>Browser: JSON response

    Note over API,Enc: LLM API keys stored<br/>encrypted with Fernet<br/>(ENCRYPTION_KEY from .env)
    API->>Enc: decrypt api_key_enc on use
    Enc-->>API: plaintext API key
```

---

## 8. API Test Execution Flow

```mermaid
sequenceDiagram
    participant User as πŸ‘€ User
    participant FE as πŸ–₯️ Frontend
    participant BE as βš™οΈ Backend
    participant LLM as πŸ€– LLM Provider
    participant Target as 🌐 Target API

    User->>FE: Upload Postman/Swagger spec
    FE->>BE: POST /api/v1/specs
    BE->>BE: postman_parser / swagger_parser
    BE-->>FE: spec_id + parsed endpoints

    User->>FE: Set base URL + scenario
    FE->>BE: POST /api/v1/sessions
    BE-->>FE: session_id

    User->>FE: Generate execution plan
    FE->>BE: POST /api/v1/execution/plan
    BE->>LLM: llm_planner (prompt with endpoints + scenario)
    LLM-->>BE: structured execution plan (JSON)
    BE-->>FE: plan steps

    loop For each step
        User->>FE: Run step
        FE->>BE: POST /api/v1/execution/step
        BE->>BE: context_manager (resolve variables)
        BE->>Target: HTTP request (with auth headers)
        Target-->>BE: HTTP response
        BE->>LLM: response_analyzer (analyze result)
        LLM-->>BE: analysis + pass/fail verdict
        BE->>DB: save execution_history
        BE-->>FE: step result + analysis
    end
```

---

## 9. Technology Stack Summary

| Layer | Technology |
|---|---|
| **Frontend** | React 18, Vite, Vanilla CSS |
| **Backend** | FastAPI (Python), Uvicorn |
| **Database** | PostgreSQL (SQLAlchemy ORM, Alembic migrations) |
| **File Storage** | Local filesystem / Supabase Storage |
| **Authentication** | JWT (HS256), Fernet encryption for secrets |
| **LLM Integration** | LangChain (Mistral, OpenAI, Anthropic, Gemini, Groq) |
| **Rate Limiting** | SlowAPI |
| **Deployment** | Procfile (Heroku-style), `runtime.txt` |