# Infrastructure Deployment via Chat - System Diagrams
## 1. Complete System Architecture
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ USER INTERFACE LAYER │
├─────────────────────────────────────────────────────────────────────────────┤
│ ┌──────────────────────┐ ┌──────────────────────┐ ┌──────────────────┐ │
│ │ Chat Interface │ │ Web Terminal UI │ │ API Clients │ │
│ │ (Natural Language) │ │ (Manual Terminal) │ │ (Programmatic) │ │
│ └──────────────────────┘ └──────────────────────┘ └──────────────────┘ │
│ │ │ │ │
└──────────────┼────────────────────────┼────────────────────────┼─────────────┘
│ │ │
┌──────────────┼────────────────────────┼────────────────────────┼─────────────┐
│ ▼ ▼ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ API Router Layer │ │
│ │ POST /v1/deployments/* → DeploymentAPIRoutes │ │
│ │ POST /v1/chat/completions → ChatIntegration │ │
│ │ GET /v1/terminal/sessions → WebTerminal │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │ │ │ │
│ APPLICATION │ LAYER │ │ │
└──────────────┼────────────────────────┼────────────────────────┼─────────────┘
│ │ │
┌──────────────┼────────────────────────┼────────────────────────┼─────────────┐
│ ▼ ▼ ▼ │
│ ┌──────────────────┐ ┌──────────────────┐ ┌──────────────────────┐ │
│ │ Chat Context │ │ Deployment │ │ Terminal Executor │ │
│ │ Builder │ │ Handler │ │ (WebSocket/SSH) │ │
│ │ │ │ │ │ │ │
│ │ - Message │ │ - Detection │ │ - Command queuing │ │
│ │ persistence │ │ - Context extract│ │ - Execution tracking │ │
│ │ - Context │ │ - Generation │ │ - Result collection │ │
│ │ retrieval │ │ - Confirmation │ │ - Error handling │ │
│ │ - Summarization │ │ - Results │ │ - Output parsing │ │
│ └──────────────────┘ └──────────────────┘ └──────────────────────┘ │
│ │ │ │ │
│ CORE │ SERVICES │ │ │
└─────────┼───────────────────────┼────────────────────────┼───────────────────┘
│ │ │
┌─────────┼───────────────────────┼────────────────────────┼───────────────────┐
│ ▼ ▼ ▼ │
│ ┌──────────────────┐ ┌──────────────────┐ ┌──────────────────────┐ │
│ │ Command │ │ Command │ │ Execution │ │
│ │ Generation │ │ Validation │ │ Manager │ │
│ │ │ │ │ │ │ │
│ │ - LLM prompt │ │ - Zod schemas │ │ - Session tracking │ │
│ │ - Context build │ │ - Pattern detect │ │ - Result logging │ │
│ │ - Risk assess │ │ - Safety checks │ │ - Confirmation wait │ │
│ │ - Mock generator │ │ - Prerequisites │ │ - Cleanup schedule │ │
│ └──────────────────┘ └──────────────────┘ └──────────────────────┘ │
│ │ │ │ │
│ BUSINESS │ LOGIC │ │ │
└──────────┼───────────────────────┼────────────────────────┼─────────────────┘
│ │ │
┌──────────┼───────────────────────┼────────────────────────┼─────────────────┐
│ ▼ ▼ ▼ │
│ ┌──────────────────────────────────────────────────────────────────┐ │
│ │ Data Access Layer │ │
│ │ ┌─────────────┐ ┌──────────────┐ ┌──────────────────────┐ │ │
│ │ │ PostgreSQL │ │ Redis │ │ Terminal Sessions │ │ │
│ │ │ │ │ │ │ (In-memory) │ │ │
│ │ │ - Sessions │ │ - Cache │ │ │ │ │
│ │ │ - Results │ │ - Temp state │ │ - Connection data │ │ │
│ │ │ - Audit log │ │ │ │ - Command results │ │ │
│ │ └─────────────┘ └──────────────┘ └──────────────────────┘ │ │
│ └──────────────────────────────────────────────────────────────────┘ │
│ │ │ │ │
│ DATA │ PERSISTENCE │ │ │
└─────────┼───────────────────────┼────────────────────────┼─────────────────┘
│ │ │
┌─────────┼───────────────────────┼────────────────────────┼─────────────────┐
│ ▼ ▼ ▼ │
│ ┌──────────────────┐ ┌──────────────────┐ ┌──────────────────────┐ │
│ │ External │ │ Saved Device │ │ Target Devices │ │
│ │ LLM │ │ Connections │ │ │ │
│ │ (OpenAI, │ │ (SSH, Telnet) │ │ - MikroTik RouterOS │ │
│ │ Anthropic) │ │ │ │ - Linux servers │ │
│ │ │ │ From terminal │ │ - Windows servers │ │
│ │ - Command gen │ │ connection API │ │ │ │
│ │ - Reasoning │ │ │ │ Real devices being │ │
│ │ - Explanations │ │ Credentials: │ │ deployed │ │
│ │ │ │ - Host, port │ │ │ │
│ │ │ │ - Username │ │ 172.251.96.200 │ │
│ │ │ │ - Auth type │ │ (DHCP deployment) │ │
│ └──────────────────┘ └──────────────────┘ └──────────────────────┘ │
│ │
│ EXTERNAL │ SYSTEMS │
└────────────────────────────────────────────────────────────────────────────┘
```
## 2. Deployment Workflow Sequence
```
┌─────────────┐ ┌──────────────────┐ ┌───────────────┐
│ User │ │ Gateway System │ │ Target Dev │
│ (Browser) │ │ (Deployment) │ │ (MikroTik) │
└──────┬──────┘ └────────┬─────────┘ └───────┬───────┘
│ │ │
│ 1. Chat Message │ │
│ "Deploy DHCP on 172.251.96.200"│ │
│──────────────────────────────>│ │
│ │ │
│ 2. Detect Request │
│ - isDeploymentRequest: true │
│ - taskType: dhcp │
│ - confidence: 0.95 │
│ │ │
│ 3. Generate Commands │
│ - LLM creates 3 commands │
│ - Risk assessment done │
│ - Validation passed │
│ │ │
│ 4. Display for Review │ │
│ [Command 1] Create pool │ │
│ [Command 2] Configure network │ │
│ [Command 3] Enable service │ │
│ ⚠️ Warnings + Risks │ │
│<────────────────────────────────│ │
│ │ │
│ 5. User Approval │ │
│ "✅ Approve All" │ │
│──────────────────────────────>│ │
│ │ │
│ 6. Get Connection Details │
│ From saved: MikroTik-Core-01 │
│ Host: 172.251.96.200 │
│ Auth: SSH + credentials │
│ │ │
│ │ 7. Execute Command 1 │
│ │──────────────────────────>│
│ │ /ip/pool/add ... │
│ │ │
│ │ 8. Return Result │
│ │ ✅ Pool created │
│ │<──────────────────────────│
│ │ │
│ │ 9. Execute Command 2 │
│ │──────────────────────────>│
│ │ /ip/dhcp-server/network...│
│ │ │
│ │ 10. Return Result │
│ │ ✅ Network configured │
│ │<──────────────────────────│
│ │ │
│ │ 11. Execute Command 3 │
│ │──────────────────────────>│
│ │ /ip/dhcp-server/add ... │
│ │ │
│ │ 12. Return Result │
│ │ ✅ Service enabled │
│ │<──────────────────────────│
│ │ │
│ 13. Display Results │ │
│ ✅ EXECUTION RESULTS │ │
│ Status: COMPLETED │ │
│ 3/3 commands successful │ │
│ Duration: 4.23s │ │
│<────────────────────────────────│ │
│ │ │
│ 14. Optional: Rollback │ │
│ "🔄 Rollback" │ │
│──────────────────────────────>│ │
│ (if deployment needs to undo) │ │
│ │ Send rollback commands │
│ │──────────────────────────>│
│ │ │
│ 15. Done │ │
│ │ │
```
## 3. Decision Flow for Deployment Detection
```
┌─────────────────────┐
│ User Message │
│ (Chat Input) │
└──────────┬──────────┘
│
▼
┌───────────────────────────────┐
│ Extract Keywords │
│ - Deploy, configure, setup │
│ - DHCP, DNS, firewall, etc │
│ - Device IPs (regex match) │
└───────────┬───────────────────┘
│
▼
┌──────────────────────────┐
│ Check Task Type Patterns │
└──────────┬───────────────┘
│
┌──────────────┼──────────────┐
│ │ │
▼ ▼ ▼
┌─────────────┐ ┌─────────────┐ ┌──────────────┐
│ DHCP Match │ │ DNS Match │ │ Firewall │
│ /dhcp|pool/ │ │ /dns|domain/│ │ Match │
│ confidence │ │ confidence │ │ /firewall| │
│ = 0.8-0.9 │ │ = 0.8-0.9 │ │ iptables/ │
└──────┬──────┘ └──────┬──────┘ │ confidence │
│ │ │ = 0.75-0.8 │
│ │ └──────┬───────┘
│ │ │
└───────────────┼───────────────┘
│
┌──────▼────────┐
│ Extract Device│
│ Context: │
│ - Device IP │
│ - Device Type │
│ - Conn Type │
│ - Task Type │
└──────┬────────┘
│
┌──────▼────────┐
│ Confidence │
│ Score >= 0.6? │
└──────┬────────┘
│
┌──────────┴──────────┐
│ │
YES NO
│ │
▼ ▼
┌──────────────────┐ ┌──────────────────┐
│ isDeployment │ │ isDeployment │
│ = true │ │ = false │
│ confidence high │ │ confidence low │
│ Return: taskType │ │ Return: n/a │
│ & details │ │ (Skip deployment)│
└──────────────────┘ └──────────────────┘
```
## 4. Command Generation Process
```
┌──────────────────────────────────────┐
│ User Request + Context │
│ "Deploy DHCP on 172.251.96.200" │
│ taskType: dhcp │
│ deviceType: mikrotik │
│ connectionType: ssh │
└──────────────┬───────────────────────┘
│
▼
┌──────────────────────────────────────┐
│ Build LLM Prompt │
│ │
│ System Prompt: │
│ - Device context (RouterOS syntax) │
│ - Task type (DHCP) │
│ - Required output format (JSON) │
│ - Risk assessment rules │
│ - Prerequisites & dependencies │
│ │
│ User Prompt: │
│ - Actual user request │
│ - Device details │
│ - Task-specific parameters │
└──────────────┬───────────────────────┘
│
▼
┌──────────────────────────────────────┐
│ Call LLM (OpenAI/Anthropic) │
│ │
│ Request: │
│ messages: [ │
│ {role: "system", content: ...}, │
│ {role: "user", content: ...} │
│ ] │
└──────────────┬───────────────────────┘
│
▼
┌──────────────────────────────────────┐
│ Parse JSON Response │
│ │
│ Expected Structure: │
│ { │
│ "taskDescription": "...", │
│ "commands": [ │
│ { │
│ "id": "cmd_001", │
│ "command": "...", │
│ "description": "...", │
│ "riskLevel": "medium", │
│ "prerequisites": [], │
│ "rollbackCommand": "..." │
│ } │
│ ], │
│ "explanation": "...", │
│ "warnings": [], │
│ "affectedServices": [] │
│ } │
└──────────────┬───────────────────────┘
│
▼
┌──────────────────────────────────────┐
│ Validate Commands │
│ │
│ - Check Zod schema │
│ - Detect suspicious patterns │
│ - Verify prerequisites │
│ - Require rollback for critical │
│ - Empty command list rejection │
└──────────────┬───────────────────────┘
│
▼
┌──────┴──────┐
│ │
✅ ❌
Valid Invalid
│ │
│ ▼
│ ┌──────────────────┐
│ │ Return Error │
│ │ with details │
│ └──────────────────┘
│
▼
┌──────────────────────────────────────┐
│ Format for Display │
│ │
│ Create readable output: │
│ - Task description │
│ - Step-by-step explanation │
│ - Commands list with details │
│ - Risk levels highlighted │
│ - Warnings prominent │
│ - Affected services listed │
│ - Estimated duration │
└──────────────┬───────────────────────┘
│
▼
┌──────────────────────────────────────┐
│ Return CommandGenerationResponse │
│ Ready for User Confirmation │
└──────────────────────────────────────┘
```
## 5. Error Handling Flow
```
┌────────────────────┐
│ Operation Start │
└─────────┬──────────┘
│
┌─────────▼──────────┐
│ Try-Catch Block │
└─────────┬──────────┘
│
┌──────────────────┼──────────────────┐
│ │ │
▼ ▼ ▼
┌────────────────┐ ┌────────────────┐ ┌──────────────┐
│ Validation │ │ LLM Error │ │ Network │
│ Error │ │ (API timeout) │ │ Error │
│ │ │ │ │ (Connection) │
│ - Schema fail │ │ - Rate limit │ │ │
│ - Invalid input│ │ - Auth failed │ │ - Connection │
│ - Missing data │ │ - Malformed │ │ timeout │
│ │ │ response │ │ - DNS failed │
└────────┬───────┘ └────────┬───────┘ └──────┬───────┘
│ │ │
└──────────────────┼─────────────────┘
│
▼
┌────────────────────────┐
│ Log Error │
│ - Severity level │
│ - Error message │
│ - Context/Stack trace │
│ - User/Session info │
└────────────┬───────────┘
│
┌──────────▼──────────┐
│ Error Type Check │
└────────────┬────────┘
│
┌──────────────────────┼──────────────────────┐
│ │ │
USER RECOVERABLE FATAL
ERROR ERROR ERROR
│ │ │
▼ ▼ ▼
┌──────────────────┐ ┌──────────────────┐ ┌──────────────────┐
│ Return to User │ │ Retry Logic │ │ Fail Fast │
│ │ │ │ │ │
│ "Invalid input: │ │ - Exponential │ │ - Log error │
│ [details]" │ │ backoff │ │ - Alert ops │
│ │ │ - Max retries: 3 │ │ - Return 500 │
│ Suggest fix │ │ - Jitter added │ │ │
└──────────────────┘ └────────┬─────────┘ └──────────────────┘
│
┌────────▼────────┐
│ Success? │
└────────┬────────┘
│
┌────────▴────────┐
│ │
YES NO
│ │
▼ │
┌──────────────┐ │
│ Continue │ │
│ Normal Flow │ │
└──────────────┘ │
│
(Retry loop)
```
## 6. Risk Assessment Matrix
```
┌──────────────┬──────────────┬────────────────┬──────────────────────┐
│ Risk Level │ Impact │ Example │ Requires │
├──────────────┼──────────────┼────────────────┼──────────────────────┤
│ 🟢 LOW │ Non- │ Query commands │ User approval only │
│ │ disruptive │ List pools │ (standard workflow) │
│ │ │ Show config │ │
├──────────────┼──────────────┼────────────────┼──────────────────────┤
│ 🟡 MEDIUM │ Configuration│ Add DNS record │ Enhanced review + │
│ │ changes │ Create VLAN │ Warnings displayed │
│ │ May need │ Add route │ (standard workflow) │
│ │ restart │ Update pool │ │
├──────────────┼──────────────┼────────────────┼──────────────────────┤
│ 🟠 HIGH │ Service │ Enable DHCP │ Explicit warnings + │
│ │ restart │ Restart service│ Affected services │
│ │ Temporary │ Change IP │ listed + Require │
│ │ downtime │ Reconfigure IF │ explicit confirmation│
├──────────────┼──────────────┼────────────────┼──────────────────────┤
│ 🔴 CRITICAL │ Data loss │ Clear config │ ⚠️ MANDATORY │
│ │ Multi-service│ Factory reset │ ROLLBACK COMMAND │
│ │ impact │ Delete all │ ⚠️ 2x confirmation │
│ │ │ Mass deletion │ ⚠️ Timeout warning │
└──────────────┴──────────────┴────────────────┴──────────────────────┘
```
## 7. Session State Machine
```
┌─────────────────────────────────────────────────────┐
│ SESSION STATE TRANSITIONS │
└─────────────────────────────────────────────────────┘
┌──────────────────┐
│ SESSION START │
│ createSession() │
└────────┬─────────┘
│
▼
┌──────────────────┐
│ [PENDING] │ ◄─── Waiting for user approval
│ Commands ready │ - sessionId: exec_xxx
│ Display shown │ - commandIds: [cmd_001, ...]
│ User reviewing │ - status: pending
└────────┬─────────┘
│
┌─────────┴──────────┐
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ [CONFIRMED] │ │ [CANCELLED] │
│ Approved by │ │ User rejected│
│ user │ │ or timeout │
│ status: OK │ │ status: FAIL │
└────────┬─────┘ └──────────────┘
│
▼
┌──────────────────┐
│ [EXECUTING] │ ◄─── Running commands one by one
│ Commands │ - Tracking each result
│ running on │ - Collecting output
│ target device │ - On error: stop or continue?
└────────┬─────────┘
│
┌────────▼─────────┐
│ All commands │
│ completed? │
└────────┬─────────┘
│
┌────┴─────┐
│ │
YES NO (partial success)
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│[COMPLETED] │ │[FAILED] │
│All success │ │Some failed │
│status: true │ │status: false │
│endTime: set │ │endTime: set │
└──────────────┘ └──────────────┘
│ │
└───────┬───────┘
│
▼
┌──────────────────┐
│ [DONE] │
│ Results stored │
│ Available for │
│ 24 hours │
│ Then cleanup │
└──────────────────┘
```
## 8. Complete Execution Timeline
```
Timeline: DHCP Deployment
T+0.0s ┌─ User sends: "Deploy DHCP on 172.251.96.200"
│
T+0.1s ├─ Message received by API
│
T+0.2s ├─ Deployment detection: DHCP (conf: 0.95)
│
T+0.5s ├─ Context extraction: IP, device type, connection
│
T+0.7s ├─ LLM call to generate commands (call LLM)
│
T+1.5s ├─ LLM response received
│
T+1.6s ├─ JSON parsing & validation
│
T+1.7s ├─ Format for display
│
T+1.8s ├─ Return to user with command list
│ ┌─────────────────────────────────┐
│ │ DEPLOYMENT PLAN SHOWN │
│ │ User reviewing... │
│ └─────────────────────────────────┘
│
T+5.0s ├─ User approves: "✅ Approve All"
│
T+5.1s ├─ Get connection details (MikroTik-Core-01)
│
T+5.2s ├─ Establish SSH connection
│
T+5.3s ├─ Execute Command 1: /ip/pool/add ...
│ └─ Device response: ✅
│
T+5.5s ├─ Execute Command 2: /ip/dhcp-server/network/add ...
│ └─ Device response: ✅
│
T+5.8s ├─ Execute Command 3: /ip/dhcp-server/add ...
│ └─ Device response: ✅
│
T+5.9s ├─ All results collected
│
T+6.0s ├─ Session marked as COMPLETED
│
T+6.1s ├─ Results formatted
│
T+6.2s └─ User receives: "✅ EXECUTION RESULTS: All 3 commands succeeded"
```
---
These diagrams provide visual representation of the complete system architecture, workflows, and data flows for the infrastructure deployment via chat system.