# goodnight77/torq architecture

> Architecture diagram of the GitHub repository goodnight77/torq, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/goodnight77/torq
- Repository: https://github.com/goodnight77/torq

## Overview

#### 1. Purpose and deployment shape
TORQ is an event-driven predictive-maintenance prototype: machine faults become retrieval-grounded AI diagnoses, supervisor-approved work orders, technician dispatches, and repair-history feedback. The primary backend is FastAPI; it can serve a built-in dashboard and support a separate React/Vite dashboard.
- `src/torq/api/main.py`
- `src/torq/pipeline.py`
- `web/src/App.jsx`

Docker Compose runs the API plus Qdrant; local defaults use SQLite and on-disk Qdrant. Optional external integrations are an OpenAI-compatible LLM, MQTT broker, Twilio WhatsApp, and PostgreSQL.
- `docker-compose.yml`
- `Dockerfile`
- `src/torq/config.py`

#### 2. Entry points and integration surfaces
HTTP clients enter through FastAPI routes, notably fault submission, work-order review/outcomes, machine registration, metrics, and evaluation. The API application is started as `torq.api.main:app`.
- `src/torq/api/main.py`
- `src/torq/api/routes.py`

Other boundary adapters are MQTT fault listening/simulation and an MCP server exposing knowledge search to compatible AI clients.
- `src/torq/events/listener.py`
- `src/torq/events/simulator.py`
- `src/torq/mcp/server.py`

Operational runners exercise individual integrations and the complete mocked loop.
- `scripts/run_loop.py`
- `scripts/run_mqtt.py`
- `scripts/run_mcp.py`

#### 3. Fault-to-diagnosis orchestration
`pipeline.py` is the central synchronous control flow: accept a normalized fault, retrieve contextual evidence, invoke structured diagnosis, generate and persist a pending work order. It connects otherwise separate ingestion/retrieval, agent, persistence, and work-order concerns.
- `src/torq/pipeline.py`
- `src/torq/agent/diagnose.py`
- `src/torq/agent/schemas.py`

Diagnosis uses prompts and an OpenAI-compatible chat-completions endpoint, producing a source-aware structured result rather than free-form text.
- `src/torq/agent/prompts.py`
- `src/torq/agent/diagnose.py`

#### 4. Knowledge retrieval and learning loop
Manuals and repair history are ingested into separate Qdrant-backed knowledge collections. Retrieval combines dense and BM25 sparse search, RRF fusion, and optional cross-encoder reranking; history can provide machine-specific evidence before broader matches.
- `src/torq/ingest/manuals.py`
- `src/torq/ingest/history.py`
- `src/torq/mcp/connectors.py`

Resolved work outcomes feed actual fixes, notes, and repair time back into history, then rebuild/reindex historical knowledge for future diagnoses.
- `src/torq/knowledge/feedback.py`
- `src/torq/ingest/history.py`

Source datasets are intentionally operator-supplied under:
- `data/manuals/`
- `data/history/`

#### 5. Work-order lifecycle, persistence, and documents
A diagnosis becomes a persistent work order containing repair guidance, safety/parts/tool data, confidence, and source references. The system generates deterministic English plus LLM-produced French/Arabic content and can render multilingual PDFs.
- `src/torq/workorder/generate.py`
- `src/torq/workorder/pdf.py`
- `src/torq/db/models.py`

Persistence is abstracted behind database session setup, defaulting to SQLite and allowing PostgreSQL via environment configuration.
- `src/torq/db/session.py`
- `src/torq/db/models.py`
- `src/torq/config.py`

#### 6. Approval and dispatch boundary
Work orders remain pending until explicit approval or rejection. Approval performs skill-based routing against technician shift data, then sends a WhatsApp notification through Twilio when configured; otherwise it returns an in-app delivery result.
- `src/torq/dispatch/approval.py`
- `src/torq/dispatch/routing.py`
- `src/torq/dispatch/notify.py`

Repair outcomes close the workflow, update downtime/workflow metrics, and trigger the knowledge feedback path.
- `src/torq/api/routes.py`
- `src/torq/knowledge/feedback.py`

#### 7. Operator interfaces
The backend dashboard/API is the operational control surface. The optional React dashboard consumes `/api`, providing supervisor views for work-order operations, PDFs, retrieval evaluation, and ROI-related display.
- `src/torq/api/main.py`
- `web/src/api.js`
- `web/src/pages/Dashboard.jsx`

The architecture is prototype-oriented: open local CORS, no API authentication, synchronous workflow execution, and public-default MQTT require production hardening with identity, queues/retries, observability, and secure transport.

Main entry points: [`src/torq/api/routes.py`](https://github.com/goodnight77/torq/blob/main/src/torq/api/routes.py) (API routes), [`src/torq/pipeline.py`](https://github.com/goodnight77/torq/blob/main/src/torq/pipeline.py) (Fault pipeline), [`src/torq/workorder/generate.py`](https://github.com/goodnight77/torq/blob/main/src/torq/workorder/generate.py) (Work-order generation), [`src/torq/mcp/connectors.py`](https://github.com/goodnight77/torq/blob/main/src/torq/mcp/connectors.py) (Hybrid retrieval)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["Interfaces"]
  node_http_api["FastAPI API &amp; dashboard<br/>[main.py]"]
  node_api_routes["API routes<br/>route handlers<br/>[routes.py]"]
  node_react_dashboard["React dashboard<br/>Vite client<br/>[App.jsx]"]
  node_mqtt_listener["MQTT listener<br/>event adapter<br/>[listener.py]"]
  node_mcp_server["MCP server<br/>AI integration<br/>[server.py]"]
end

subgraph group_core["Core workflow"]
  node_pipeline{{"Fault pipeline<br/>orchestrator<br/>[pipeline.py]"}}
end

subgraph group_knowledge["Knowledge &amp; AI"]
  node_diagnosis_agent{{"Structured diagnosis<br/>LLM agent<br/>[diagnose.py]"}}
  node_manual_ingest["Manual ingestion<br/>knowledge ingestion<br/>[manuals.py]"]
  node_history_ingest["History ingestion<br/>knowledge ingestion<br/>[history.py]"]
  node_retrieval["Hybrid retrieval<br/>knowledge search<br/>[connectors.py]"]
  node_feedback["Outcome feedback<br/>learning loop<br/>[feedback.py]"]
end

subgraph group_operations["Work orders &amp; dispatch"]
  node_workorder_generation["Work-order generation<br/>document builder<br/>[generate.py]"]
  node_pdf_renderer["Multilingual PDF renderer<br/>document renderer<br/>[pdf.py]"]
  node_approval{{"Approval lifecycle<br/>workflow gate<br/>[approval.py]"}}
  node_routing["Technician routing<br/>dispatch matcher<br/>[routing.py]"]
  node_notification["WhatsApp notification<br/>delivery adapter<br/>[notify.py]"]
end

subgraph group_storage["Persistence"]
  node_qdrant[("Qdrant collections<br/>vector store<br/>[docker-compose.yml]")]
  node_database[("Operational database<br/>SQLite / PostgreSQL<br/>[session.py]")]
end

node_react_dashboard -->|"/api"| node_http_api
node_http_api -->|"serves"| node_api_routes
node_api_routes -->|"submits faults"| node_pipeline
node_mqtt_listener -->|"fault events"| node_pipeline
node_mcp_server -->|"knowledge search"| node_retrieval
node_pipeline -->|"retrieves evidence"| node_retrieval
node_retrieval -->|"hybrid search"| node_qdrant
node_manual_ingest -->|"manual collection"| node_qdrant
node_history_ingest -->|"history collection"| node_qdrant
node_pipeline -->|"fault + evidence"| node_diagnosis_agent
node_diagnosis_agent -->|"structured diagnosis"| node_workorder_generation
node_workorder_generation -->|"persists pending order"| node_database
node_workorder_generation -->|"renders"| node_pdf_renderer
node_api_routes -->|"review decision"| node_approval
node_approval -->|"on approval"| node_routing
node_routing -->|"assigned technician"| node_notification
node_api_routes -->|"resolved outcome"| node_feedback
node_feedback -->|"reindex history"| node_history_ingest
node_api_routes -->|"work orders &amp; metrics"| node_database

click node_http_api "https://github.com/goodnight77/torq/blob/main/src/torq/api/main.py"
click node_api_routes "https://github.com/goodnight77/torq/blob/main/src/torq/api/routes.py"
click node_react_dashboard "https://github.com/goodnight77/torq/blob/main/web/src/App.jsx"
click node_mqtt_listener "https://github.com/goodnight77/torq/blob/main/src/torq/events/listener.py"
click node_mcp_server "https://github.com/goodnight77/torq/blob/main/src/torq/mcp/server.py"
click node_pipeline "https://github.com/goodnight77/torq/blob/main/src/torq/pipeline.py"
click node_diagnosis_agent "https://github.com/goodnight77/torq/blob/main/src/torq/agent/diagnose.py"
click node_manual_ingest "https://github.com/goodnight77/torq/blob/main/src/torq/ingest/manuals.py"
click node_history_ingest "https://github.com/goodnight77/torq/blob/main/src/torq/ingest/history.py"
click node_retrieval "https://github.com/goodnight77/torq/blob/main/src/torq/mcp/connectors.py"
click node_qdrant "https://github.com/goodnight77/torq/blob/main/docker-compose.yml"
click node_workorder_generation "https://github.com/goodnight77/torq/blob/main/src/torq/workorder/generate.py"
click node_pdf_renderer "https://github.com/goodnight77/torq/blob/main/src/torq/workorder/pdf.py"
click node_approval "https://github.com/goodnight77/torq/blob/main/src/torq/dispatch/approval.py"
click node_routing "https://github.com/goodnight77/torq/blob/main/src/torq/dispatch/routing.py"
click node_notification "https://github.com/goodnight77/torq/blob/main/src/torq/dispatch/notify.py"
click node_feedback "https://github.com/goodnight77/torq/blob/main/src/torq/knowledge/feedback.py"
click node_database "https://github.com/goodnight77/torq/blob/main/src/torq/db/session.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_http_api,node_api_routes,node_react_dashboard,node_mqtt_listener,node_mcp_server toneBlue
class node_pipeline toneAmber
class node_diagnosis_agent,node_manual_ingest,node_history_ingest,node_retrieval,node_feedback toneMint
class node_workorder_generation,node_pdf_renderer,node_approval,node_routing,node_notification toneRose
class node_qdrant,node_database toneIndigo
```

## Components

### Interfaces

- [FastAPI API & dashboard](https://github.com/goodnight77/torq/blob/main/src/torq/api/main.py): FastAPI · HTTP control surface and built-in dashboard. · `src/torq/api/main.py`
- [API routes](https://github.com/goodnight77/torq/blob/main/src/torq/api/routes.py): route handlers · Fault, review, outcome, machine, metrics, and evaluation endpoints. · `src/torq/api/routes.py`
- [React dashboard](https://github.com/goodnight77/torq/blob/main/web/src/App.jsx): Vite client · Optional supervisor interface consuming /api. · `web/src/App.jsx`
- [MQTT listener](https://github.com/goodnight77/torq/blob/main/src/torq/events/listener.py): event adapter · Receives machine fault events. · `src/torq/events/listener.py`
- [MCP server](https://github.com/goodnight77/torq/blob/main/src/torq/mcp/server.py): AI integration · Exposes knowledge search to MCP clients. · `src/torq/mcp/server.py`

### Core workflow

- [Fault pipeline](https://github.com/goodnight77/torq/blob/main/src/torq/pipeline.py): orchestrator · Normalizes faults, retrieves evidence, diagnoses, and creates pending work orders. · `src/torq/pipeline.py`

### Knowledge & AI

- [Structured diagnosis](https://github.com/goodnight77/torq/blob/main/src/torq/agent/diagnose.py): LLM agent · Produces source-aware diagnoses through an OpenAI-compatible chat endpoint. · `src/torq/agent/diagnose.py`
- [Manual ingestion](https://github.com/goodnight77/torq/blob/main/src/torq/ingest/manuals.py): knowledge ingestion · Indexes operator-supplied manuals. · `src/torq/ingest/manuals.py`
- [History ingestion](https://github.com/goodnight77/torq/blob/main/src/torq/ingest/history.py): knowledge ingestion · Indexes repair history and rebuilds it after feedback. · `src/torq/ingest/history.py`
- [Hybrid retrieval](https://github.com/goodnight77/torq/blob/main/src/torq/mcp/connectors.py): knowledge search · Dense and BM25 search with RRF fusion and optional reranking. · `src/torq/mcp/connectors.py`
- [Outcome feedback](https://github.com/goodnight77/torq/blob/main/src/torq/knowledge/feedback.py): learning loop · Feeds fixes, notes, and repair time into historical knowledge. · `src/torq/knowledge/feedback.py`

### Work orders & dispatch

- [Work-order generation](https://github.com/goodnight77/torq/blob/main/src/torq/workorder/generate.py): document builder · Builds repair guidance and multilingual work-order content. · `src/torq/workorder/generate.py`
- [Multilingual PDF renderer](https://github.com/goodnight77/torq/blob/main/src/torq/workorder/pdf.py): document renderer · `src/torq/workorder/pdf.py`
- [Approval lifecycle](https://github.com/goodnight77/torq/blob/main/src/torq/dispatch/approval.py): workflow gate · Keeps orders pending until supervisor approval or rejection. · `src/torq/dispatch/approval.py`
- [Technician routing](https://github.com/goodnight77/torq/blob/main/src/torq/dispatch/routing.py): dispatch matcher · Matches approved work to skills and shifts. · `src/torq/dispatch/routing.py`
- [WhatsApp notification](https://github.com/goodnight77/torq/blob/main/src/torq/dispatch/notify.py): delivery adapter · Uses Twilio when configured, otherwise returns in-app delivery. · `src/torq/dispatch/notify.py`

### Persistence

- [Qdrant collections](https://github.com/goodnight77/torq/blob/main/docker-compose.yml): vector store · Separate manual and repair-history knowledge collections. · `docker-compose.yml`
- [Operational database](https://github.com/goodnight77/torq/blob/main/src/torq/db/session.py): SQLite / PostgreSQL · Persists machines, work orders, outcomes, and workflow metrics. · `src/torq/db/session.py`

## Connections

- React dashboard → FastAPI API & dashboard: /api
- FastAPI API & dashboard → API routes: serves
- API routes → Fault pipeline: submits faults
- MQTT listener → Fault pipeline: fault events
- MCP server → Hybrid retrieval: knowledge search
- Fault pipeline → Hybrid retrieval: retrieves evidence
- Hybrid retrieval → Qdrant collections: hybrid search
- Manual ingestion → Qdrant collections: manual collection
- History ingestion → Qdrant collections: history collection
- Fault pipeline → Structured diagnosis: fault + evidence
- Structured diagnosis → Work-order generation: structured diagnosis
- Work-order generation → Operational database: persists pending order
- Work-order generation → Multilingual PDF renderer: renders
- API routes → Approval lifecycle: review decision
- Approval lifecycle → Technician routing: on approval
- Technician routing → WhatsApp notification: assigned technician
- API routes → Outcome feedback: resolved outcome
- Outcome feedback → History ingestion: reindex history
- API routes → Operational database: work orders & metrics
