How goodnight77/torq works
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: routes.py API routes · pipeline.py Fault pipeline · generate.py Work-order generation · connectors.py Hybrid retrieval
Components 18
Interfaces
- FastAPI API & dashboard · HTTP control surface and built-in dashboard.
src/torq/api/main.py - API routes · Fault, review, outcome, machine, metrics, and evaluation endpoints.
src/torq/api/routes.py - React dashboard · Optional supervisor interface consuming /api.
web/src/App.jsx - MQTT listener · Receives machine fault events.
src/torq/events/listener.py - MCP server · Exposes knowledge search to MCP clients.
src/torq/mcp/server.py
Core workflow
- Fault pipeline · Normalizes faults, retrieves evidence, diagnoses, and creates pending work orders.
src/torq/pipeline.py
Knowledge & AI
- Structured diagnosis · Produces source-aware diagnoses through an OpenAI-compatible chat endpoint.
src/torq/agent/diagnose.py - Manual ingestion · Indexes operator-supplied manuals.
src/torq/ingest/manuals.py - History ingestion · Indexes repair history and rebuilds it after feedback.
src/torq/ingest/history.py - Hybrid retrieval · Dense and BM25 search with RRF fusion and optional reranking.
src/torq/mcp/connectors.py - Outcome feedback · Feeds fixes, notes, and repair time into historical knowledge.
src/torq/knowledge/feedback.py
Work orders & dispatch
- Work-order generation · Builds repair guidance and multilingual work-order content.
src/torq/workorder/generate.py - Multilingual PDF renderer
src/torq/workorder/pdf.py - Approval lifecycle · Keeps orders pending until supervisor approval or rejection.
src/torq/dispatch/approval.py - Technician routing · Matches approved work to skills and shifts.
src/torq/dispatch/routing.py - WhatsApp notification · Uses Twilio when configured, otherwise returns in-app delivery.
src/torq/dispatch/notify.py
Persistence
- Qdrant collections · Separate manual and repair-history knowledge collections.
docker-compose.yml - Operational database · Persists machines, work orders, outcomes, and workflow metrics.
src/torq/db/session.py
Connections 19
- 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