# hectorta1989/test architecture

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

- Interactive diagram: https://gitdiagram.com/hectorta1989/test
- Repository: https://github.com/hectorta1989/test

## Overview

#### 1. Purpose and Runtime Shape
SkillTwin VN is a factory knowledge-transfer MVP: capture expert know-how into versioned SOPs, approve them, then deliver RAG Q&A and guided checklist execution with photo evidence and expert review. It is a Next.js client dashboard backed by a FastAPI API and SQLite/SQLAlchemy persistence.

Entry points: `backend/app/main.py` starts FastAPI and wires routers; `frontend/pages/_app.tsx` initializes the Pages Router client; `backend/seed.py` provisions demo data.

#### 2. API, Identity, and Organization Boundary
FastAPI routers provide the system boundary for the frontend and enforce authenticated, role-based workflows. JWT/password handling and RBAC dependencies establish organization- and role-scoped access for admin, expert, and worker actions.

Paths: `backend/app/api/routers/auth.py`, `backend/app/core/security.py`, `backend/app/core/deps.py`.

The frontend uses a typed API client and auth context rather than server-side Next.js actions: `frontend/lib/api.ts`, `frontend/lib/auth.tsx`.

#### 3. Core Domain and Approval Lifecycle
The relational domain models organizations, users, processes, SOPs/SOP versions, chat, learning, audit events, kill switches, and execution runs. SOPs are created as drafts, edited/versioned, submitted, then explicitly approved or rejected by experts. Only approved SOP content becomes operational knowledge.

Paths: `backend/app/models/models.py`, `backend/app/api/routers/sops.py`, `backend/app/api/routers/processes.py`.

The SOP UI is split between the library and per-SOP editor: `frontend/pages/sops/index.tsx`, `frontend/pages/sops/[id].tsx`, `frontend/components/RecipeEditor.tsx`.

#### 4. Knowledge Capture and Generation Boundary
Capture converts technician transcript/audio input into a structured draft SOP. Generation uses a transparent provider chain—Gemini, then Groq, then deterministic templates—and records generation method/model rather than presenting fallback output as AI-generated.

Paths: `backend/app/api/routers/capture.py`, `backend/app/services/capture_service.py`, `backend/app/services/llm_service.py`.

This boundary also supports SOP-derived quizzes, avoiding unsupported external knowledge: `backend/app/services/quiz_service.py`, `backend/app/api/routers/quiz.py`.

#### 5. Approved-SOP RAG and AI Governance
Chat retrieves only approved SOP material using local TF-IDF and returns citations plus an escalation path when no source, low confidence, or AI disablement applies. It does not depend on an embeddings service.

Paths: `backend/app/services/rag_service.py`, `backend/app/api/routers/chat.py`, `frontend/pages/chat.tsx`.

An admin kill switch can disable AI generation per process or organization; audit logging captures AI recommendations and human decisions as append-only events.

Paths: `backend/app/api/routers/admin.py`, `backend/app/services/audit_service.py`, `backend/app/models/models.py`.

#### 6. Guided Execution and Evidence Review
Workers execute an approved SOP as a stateful checklist, complete or skip steps, and upload required photo evidence. Evidence analysis is deliberately limited to classical image-quality checks—blur, brightness, and resolution—not defect detection. Expert reviewers make the actual accept/reject/invalid/needs-review decision.

Paths: `backend/app/api/routers/execution.py`, `backend/app/services/execution_service.py`, `backend/app/services/evidence_service.py`.

Corresponding worker and reviewer surfaces: `frontend/pages/execution/index.tsx`, `frontend/pages/execution/review.tsx`.

#### 7. Learning and Operational Reporting
Quiz grading feeds skill-matrix updates using EMA-style scoring, with expert sign-off/override. Dashboard endpoints aggregate pilot KPIs and workflow activity rather than acting as a separate analytics platform.

Paths: `backend/app/services/skill_service.py`, `backend/app/api/routers/skills.py`, `backend/app/api/routers/dashboard.py`.

Material infrastructure: SQLite via `backend/app/core/database.py`; local-disk evidence storage for the MVP; optional Gemini and Groq external LLM APIs configured through `backend/app/core/config.py`.

Main entry points: [`backend/app/models/models.py`](https://github.com/hectorta1989/test/blob/main/backend/app/models/models.py) (Versioned Domain Models), [`backend/app/core/security.py`](https://github.com/hectorta1989/test/blob/main/backend/app/core/security.py) (JWT & RBAC), [`backend/app/services/capture_service.py`](https://github.com/hectorta1989/test/blob/main/backend/app/services/capture_service.py) (Draft SOP Capture), [`backend/app/services/execution_service.py`](https://github.com/hectorta1989/test/blob/main/backend/app/services/execution_service.py) (Stateful Checklist Runs)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Next.js Dashboard"]
  node_next_app["Pages Router App<br/>Next.js entry<br/>[_app.tsx]"]
  node_web_auth["Auth Context &amp; API Client<br/>frontend auth<br/>[auth.tsx]"]
  node_sop_ui["SOP Library &amp; Editor<br/>SOP workspace<br/>[&#91;id&#93;.tsx]"]
  node_chat_ui["RAG Chat<br/>chat screen<br/>[chat.tsx]"]
  node_execution_ui["Worker &amp; Review Screens<br/>execution UI<br/>[review.tsx]"]
end

subgraph group_api["FastAPI Boundary"]
  node_api_app["FastAPI App<br/>API entry<br/>[main.py]"]
  node_identity["JWT &amp; RBAC<br/>access control<br/>[security.py]"]
  node_sop_api["SOP &amp; Process API<br/>approval API<br/>[sops.py]"]
  node_capture_api["Capture API<br/>[capture.py]"]
  node_chat_api["Chat API<br/>RAG API<br/>[chat.py]"]
  node_execution_api["Execution API<br/>checklist API<br/>[execution.py]"]
  node_admin_api["Admin &amp; Reporting API<br/>governance API<br/>[admin.py]"]
end

subgraph group_services["Domain Services"]
  node_capture_service["Draft SOP Capture<br/>capture service<br/>[capture_service.py]"]
  node_llm_chain["Provider Fallback Chain<br/>LLM generation<br/>[llm_service.py]"]
  node_rag_service["Approved-SOP RAG<br/>TF-IDF retrieval<br/>[rag_service.py]"]
  node_execution_service["Stateful Checklist Runs<br/>execution service"]
  node_evidence_service["Image Quality Checks<br/>evidence service"]
end

subgraph group_data["Persistence &amp; External"]
  node_domain_models["Versioned Domain Models<br/>SQLAlchemy models<br/>[models.py]"]
  node_sqlite[("SQLite Database<br/>relational store<br/>[database.py]")]
  node_evidence_store["Local Evidence Storage<br/>file store"]
  node_llm_providers{{"Gemini &amp; Groq<br/>external LLM APIs<br/>[config.py]"}}
end

node_next_app -->|"initializes"| node_web_auth
node_web_auth -->|"typed authenticated calls"| node_api_app
node_sop_ui -->|"manage drafts and approval"| node_sop_api
node_chat_ui -->|"ask cited questions"| node_chat_api
node_execution_ui -->|"run and review evidence"| node_execution_api
node_api_app -->|"enforces access"| node_identity
node_identity -->|"authorizes roles"| node_sop_api
node_identity -->|"authorizes roles"| node_execution_api
node_sop_api -->|"versions and approves SOPs"| node_domain_models
node_capture_api -->|"creates draft"| node_capture_service
node_capture_service -->|"generates structured content"| node_llm_chain
node_llm_chain -->|"uses when available"| node_llm_providers
node_capture_service -->|"stores draft and method"| node_domain_models
node_chat_api -->|"answers questions"| node_rag_service
node_rag_service -->|"retrieves approved SOPs only"| node_domain_models
node_admin_api -->|"sets kill switches and audits"| node_domain_models
node_execution_api -->|"advances checklist state"| node_execution_service
node_execution_service -->|"validates uploads"| node_evidence_service
node_evidence_service -->|"stores photos"| node_evidence_store
node_execution_service -->|"records runs and decisions"| node_domain_models
node_domain_models -->|"persists through"| node_sqlite
node_admin_api -.->|"controls AI availability"| node_llm_chain

click node_next_app "https://github.com/hectorta1989/test/blob/main/frontend/pages/_app.tsx"
click node_web_auth "https://github.com/hectorta1989/test/blob/main/frontend/lib/auth.tsx"
click node_sop_ui "https://github.com/hectorta1989/test/blob/main/frontend/pages/sops/%5Bid%5D.tsx"
click node_chat_ui "https://github.com/hectorta1989/test/blob/main/frontend/pages/chat.tsx"
click node_execution_ui "https://github.com/hectorta1989/test/blob/main/frontend/pages/execution/review.tsx"
click node_api_app "https://github.com/hectorta1989/test/blob/main/backend/app/main.py"
click node_identity "https://github.com/hectorta1989/test/blob/main/backend/app/core/security.py"
click node_sop_api "https://github.com/hectorta1989/test/blob/main/backend/app/api/routers/sops.py"
click node_capture_api "https://github.com/hectorta1989/test/blob/main/backend/app/api/routers/capture.py"
click node_chat_api "https://github.com/hectorta1989/test/blob/main/backend/app/api/routers/chat.py"
click node_execution_api "https://github.com/hectorta1989/test/blob/main/backend/app/api/routers/execution.py"
click node_admin_api "https://github.com/hectorta1989/test/blob/main/backend/app/api/routers/admin.py"
click node_capture_service "https://github.com/hectorta1989/test/blob/main/backend/app/services/capture_service.py"
click node_llm_chain "https://github.com/hectorta1989/test/blob/main/backend/app/services/llm_service.py"
click node_rag_service "https://github.com/hectorta1989/test/blob/main/backend/app/services/rag_service.py"
click node_execution_service "https://github.com/hectorta1989/test/blob/main/backend/app/services/execution_service.py"
click node_evidence_service "https://github.com/hectorta1989/test/blob/main/backend/app/services/evidence_service.py"
click node_domain_models "https://github.com/hectorta1989/test/blob/main/backend/app/models/models.py"
click node_sqlite "https://github.com/hectorta1989/test/blob/main/backend/app/core/database.py"
click node_llm_providers "https://github.com/hectorta1989/test/blob/main/backend/app/core/config.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_next_app,node_web_auth,node_sop_ui,node_chat_ui,node_execution_ui toneBlue
class node_api_app,node_identity,node_sop_api,node_capture_api,node_chat_api,node_execution_api,node_admin_api toneAmber
class node_capture_service,node_llm_chain,node_rag_service,node_execution_service,node_evidence_service toneMint
class node_domain_models,node_sqlite,node_evidence_store,node_llm_providers toneRose
```

## Components

### Next.js Dashboard

- [Pages Router App](https://github.com/hectorta1989/test/blob/main/frontend/pages/_app.tsx): Next.js entry · `frontend/pages/_app.tsx`
- [Auth Context & API Client](https://github.com/hectorta1989/test/blob/main/frontend/lib/auth.tsx): frontend auth · `frontend/lib/auth.tsx`
- [SOP Library & Editor](https://github.com/hectorta1989/test/blob/main/frontend/pages/sops/%5Bid%5D.tsx): SOP workspace · `frontend/pages/sops/[id].tsx`
- [RAG Chat](https://github.com/hectorta1989/test/blob/main/frontend/pages/chat.tsx): chat screen · `frontend/pages/chat.tsx`
- [Worker & Review Screens](https://github.com/hectorta1989/test/blob/main/frontend/pages/execution/review.tsx): execution UI · `frontend/pages/execution/review.tsx`

### FastAPI Boundary

- [FastAPI App](https://github.com/hectorta1989/test/blob/main/backend/app/main.py): API entry · Wires workflow routers. · `backend/app/main.py`
- [JWT & RBAC](https://github.com/hectorta1989/test/blob/main/backend/app/core/security.py): access control · Scopes actions by organization and role. · `backend/app/core/security.py`
- [SOP & Process API](https://github.com/hectorta1989/test/blob/main/backend/app/api/routers/sops.py): approval API · `backend/app/api/routers/sops.py`
- [Capture API](https://github.com/hectorta1989/test/blob/main/backend/app/api/routers/capture.py): knowledge capture API · `backend/app/api/routers/capture.py`
- [Chat API](https://github.com/hectorta1989/test/blob/main/backend/app/api/routers/chat.py): RAG API · `backend/app/api/routers/chat.py`
- [Execution API](https://github.com/hectorta1989/test/blob/main/backend/app/api/routers/execution.py): checklist API · `backend/app/api/routers/execution.py`
- [Admin & Reporting API](https://github.com/hectorta1989/test/blob/main/backend/app/api/routers/admin.py): governance API · `backend/app/api/routers/admin.py`

### Domain Services

- [Draft SOP Capture](https://github.com/hectorta1989/test/blob/main/backend/app/services/capture_service.py): capture service · Turns transcript or audio input into structured drafts. · `backend/app/services/capture_service.py`
- [Provider Fallback Chain](https://github.com/hectorta1989/test/blob/main/backend/app/services/llm_service.py): LLM generation · Gemini, then Groq, then deterministic templates. · `backend/app/services/llm_service.py`
- [Approved-SOP RAG](https://github.com/hectorta1989/test/blob/main/backend/app/services/rag_service.py): TF-IDF retrieval · Cites approved SOPs and escalates uncertain answers. · `backend/app/services/rag_service.py`
- [Stateful Checklist Runs](https://github.com/hectorta1989/test/blob/main/backend/app/services/execution_service.py): execution service · `backend/app/services/execution_service.py`
- [Image Quality Checks](https://github.com/hectorta1989/test/blob/main/backend/app/services/evidence_service.py): evidence service · Checks blur, brightness, and resolution only. · `backend/app/services/evidence_service.py`

### Persistence & External

- [Versioned Domain Models](https://github.com/hectorta1989/test/blob/main/backend/app/models/models.py): SQLAlchemy models · Contains SOP lifecycle, runs, skills, audit events, and kill switches. · `backend/app/models/models.py`
- [SQLite Database](https://github.com/hectorta1989/test/blob/main/backend/app/core/database.py): relational store · `backend/app/core/database.py`
- **Local Evidence Storage**: file store
- [Gemini & Groq](https://github.com/hectorta1989/test/blob/main/backend/app/core/config.py): external LLM APIs · Optional providers configured for generation. · `backend/app/core/config.py`

## Connections

- Pages Router App → Auth Context & API Client: initializes
- Auth Context & API Client → FastAPI App: typed authenticated calls
- SOP Library & Editor → SOP & Process API: manage drafts and approval
- RAG Chat → Chat API: ask cited questions
- Worker & Review Screens → Execution API: run and review evidence
- FastAPI App → JWT & RBAC: enforces access
- JWT & RBAC → SOP & Process API: authorizes roles
- JWT & RBAC → Execution API: authorizes roles
- SOP & Process API → Versioned Domain Models: versions and approves SOPs
- Capture API → Draft SOP Capture: creates draft
- Draft SOP Capture → Provider Fallback Chain: generates structured content
- Provider Fallback Chain → Gemini & Groq: uses when available
- Draft SOP Capture → Versioned Domain Models: stores draft and method
- Chat API → Approved-SOP RAG: answers questions
- Approved-SOP RAG → Versioned Domain Models: retrieves approved SOPs only
- Admin & Reporting API → Versioned Domain Models: sets kill switches and audits
- Execution API → Stateful Checklist Runs: advances checklist state
- Stateful Checklist Runs → Image Quality Checks: validates uploads
- Image Quality Checks → Local Evidence Storage: stores photos
- Stateful Checklist Runs → Versioned Domain Models: records runs and decisions
- Versioned Domain Models → SQLite Database: persists through
- Admin & Reporting API → Provider Fallback Chain: controls AI availability
