# wikiepeidia/anser architecture

> Architecture diagram of the GitHub repository wikiepeidia/anser, 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/wikiepeidia/anser
- Repository: https://github.com/wikiepeidia/anser

## Overview

#### Purpose and Entry Points
ANSER is a Flask-based retail-operations platform: invoice OCR, sales/inventory forecasting, AI-assisted operations, workflow automation, and back-office management. The main web-process entry point is `app.py`; the separately runnable ML/OCR process is `run_dl_service.py` / `dl_service/model_app.py`.

#### Web Application Boundary
The Flask application follows a route-to-service-to-persistence shape. HTTP handlers are partitioned by domain—inventory, sales, workflows, AI, Google, administration, wallet, and workspace—then delegate business work to `core/` services. Key paths: `routes/inventory_routes.py`, `routes/ai_routes.py`, `routes/workflow_routes.py`.

#### Identity, Ownership, and Persistence
Authentication, request/agent middleware, configuration, database setup, and ORM/domain models are centralized in `core/`. This is the primary trust boundary for user-scoped business data and authenticated actions. PostgreSQL is the stated production datastore and SQLite supports local development. Key paths: `core/auth.py`, `core/database.py`, `core/models.py`.

#### Retail Operations Domain
Inventory mutations are isolated behind a transaction service rather than implemented directly in routes; sales, product/catalog, customer, reporting, subscription, and wallet surfaces are exposed through domain route modules. This separation is important for consistent stock updates and ownership enforcement. Key paths: `core/services/inventory_tx_service.py`, `routes/sales_routes.py`, `routes/operations_routes.py`.

#### Automation and External Integrations
Workflow definitions and execution are split between workflow/automation engines and integration adapters. The application connects to Google Workspace capabilities and Make.com; README also identifies n8n and generic webhooks as automation targets. Treat outbound integration code as a security boundary, particularly for webhook destination validation. Key paths: `core/workflow_engine.py`, `core/automation_engine.py`, `core/google_integration.py`, `core/make_integration.py`.

#### AI, OCR, and Forecasting Service
The web app reaches a separate deep-learning service through a client abstraction. That service exposes model/OCR/history API modules and composes OCR, invoice/layout extraction, and forecasting services. Model assets include LSTM forecasting and Vietnamese OCR/vision implementations. Key paths: `core/services/dl_client.py`, `dl_service/model_app.py`, `dl_service/api/ocr_routes.py`, `dl_service/services/forecast_service.py`.

#### UI and Asynchronous Work
The UI is server-rendered Jinja templates with static JavaScript/CSS enhancement, not a separate SPA. Page and domain routes render or serve these interfaces, including dashboard, sales, workspace builder, and chat. README states Redis/RQ is used for slow jobs such as AI chat; its queue bootstrap is not present in the supplied tree. Key paths: `ui/templates/base.html`, `ui/templates/workspace_builder.html`, `static/js/chat.js`, `core/services/ai_chat_service.py`.

Main entry points: [`routes/inventory_routes.py`](https://github.com/wikiepeidia/anser/blob/main/routes/inventory_routes.py) (Domain routes), [`dl_service/model_app.py`](https://github.com/wikiepeidia/anser/blob/main/dl_service/model_app.py) (Model API), [`core/models.py`](https://github.com/wikiepeidia/anser/blob/main/core/models.py) (Domain models), [`app.py`](https://github.com/wikiepeidia/anser/blob/main/app.py) (Web process)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Flask Web App"]
  node_web_entry{{"Web process<br/>Flask entry point<br/>[app.py]"}}
  node_domain_routes["Domain routes<br/>HTTP handlers"]
  node_sales_ops_routes["Sales &amp; operations routes<br/>HTTP handlers<br/>[sales_routes.py]"]
end

subgraph group_core["Core Domain &amp; Trust"]
  node_auth_boundary["Auth &amp; request boundary<br/>identity middleware<br/>[auth.py]"]
  node_database_setup["Database setup<br/>persistence bootstrap<br/>[database.py]"]
  node_domain_models["Domain models<br/>ORM models<br/>[models.py]"]
  node_inventory_tx["Inventory transactions<br/>domain service"]
  node_ai_chat["AI chat service<br/>AI service<br/>[ai_chat_service.py]"]
  node_dl_client["DL service client<br/>[dl_client.py]"]
end

subgraph group_automation["Automation &amp; Integrations"]
  node_workflow_engine["Workflow engine<br/>workflow runtime<br/>[workflow_engine.py]"]
  node_automation_engine["Automation engine<br/>automation runtime"]
  node_google_adapter["Google Workspace adapter<br/>integration adapter"]
  node_make_adapter["Make.com adapter<br/>integration adapter"]
end

subgraph group_ml["ML / OCR Service"]
  node_dl_entry{{"DL service process<br/>ML API entry point<br/>[run_dl_service.py]"}}
  node_model_api["Model API<br/>OCR and history API<br/>[model_app.py]"]
  node_ocr_api["OCR endpoints<br/>OCR API module<br/>[ocr_routes.py]"]
  node_forecasting["Forecasting service<br/>ML forecasting"]
end

subgraph group_ui["Server-Rendered UI"]
  node_templates["Jinja templates<br/>server-rendered UI<br/>[base.html]"]
  node_workspace_ui["Workspace builder<br/>Jinja page"]
  node_chat_ui["Chat enhancement<br/>static JavaScript<br/>[chat.js]"]
end

node_postgres[("PostgreSQL / SQLite<br/>datastore")]
node_job_queue["Redis / RQ<br/>background jobs"]

node_web_entry -->|"registers"| node_domain_routes
node_web_entry -->|"registers"| node_sales_ops_routes
node_web_entry -->|"uses"| node_auth_boundary
node_domain_routes -->|"mutates stock through"| node_inventory_tx
node_sales_ops_routes -->|"serves domain data"| node_domain_models
node_auth_boundary -->|"enforces ownership on"| node_domain_models
node_inventory_tx -->|"updates"| node_domain_models
node_domain_models -->|"persists via"| node_database_setup
node_database_setup -->|"connects to"| node_postgres
node_domain_routes -->|"runs workflows"| node_workflow_engine
node_workflow_engine -->|"delegates automation"| node_automation_engine
node_automation_engine -->|"invokes"| node_google_adapter
node_automation_engine -->|"invokes"| node_make_adapter
node_domain_routes -->|"requests OCR and forecasts"| node_dl_client
node_dl_client -->|"calls"| node_model_api
node_dl_entry -->|"serves"| node_model_api
node_model_api -->|"exposes"| node_ocr_api
node_model_api -->|"uses"| node_forecasting
node_domain_routes -->|"handles AI requests"| node_ai_chat
node_ai_chat -.->|"queues slow work"| node_job_queue
node_domain_routes -->|"renders"| node_templates
node_templates -->|"includes"| node_workspace_ui
node_workspace_ui -->|"enhanced by"| node_chat_ui

click node_web_entry "https://github.com/wikiepeidia/anser/blob/main/app.py"
click node_domain_routes "https://github.com/wikiepeidia/anser/blob/main/routes/inventory_routes.py"
click node_sales_ops_routes "https://github.com/wikiepeidia/anser/blob/main/routes/sales_routes.py"
click node_auth_boundary "https://github.com/wikiepeidia/anser/blob/main/core/auth.py"
click node_database_setup "https://github.com/wikiepeidia/anser/blob/main/core/database.py"
click node_domain_models "https://github.com/wikiepeidia/anser/blob/main/core/models.py"
click node_inventory_tx "https://github.com/wikiepeidia/anser/blob/main/core/services/inventory_tx_service.py"
click node_ai_chat "https://github.com/wikiepeidia/anser/blob/main/core/services/ai_chat_service.py"
click node_dl_client "https://github.com/wikiepeidia/anser/blob/main/core/services/dl_client.py"
click node_workflow_engine "https://github.com/wikiepeidia/anser/blob/main/core/workflow_engine.py"
click node_automation_engine "https://github.com/wikiepeidia/anser/blob/main/core/automation_engine.py"
click node_google_adapter "https://github.com/wikiepeidia/anser/blob/main/core/google_integration.py"
click node_make_adapter "https://github.com/wikiepeidia/anser/blob/main/core/make_integration.py"
click node_dl_entry "https://github.com/wikiepeidia/anser/blob/main/run_dl_service.py"
click node_model_api "https://github.com/wikiepeidia/anser/blob/main/dl_service/model_app.py"
click node_ocr_api "https://github.com/wikiepeidia/anser/blob/main/dl_service/api/ocr_routes.py"
click node_forecasting "https://github.com/wikiepeidia/anser/blob/main/dl_service/services/forecast_service.py"
click node_templates "https://github.com/wikiepeidia/anser/blob/main/ui/templates/base.html"
click node_workspace_ui "https://github.com/wikiepeidia/anser/blob/main/ui/templates/workspace_builder.html"
click node_chat_ui "https://github.com/wikiepeidia/anser/blob/main/static/js/chat.js"

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_web_entry,node_domain_routes,node_sales_ops_routes toneBlue
class node_auth_boundary,node_database_setup,node_domain_models,node_inventory_tx,node_ai_chat,node_dl_client toneAmber
class node_workflow_engine,node_automation_engine,node_google_adapter,node_make_adapter toneMint
class node_dl_entry,node_model_api,node_ocr_api,node_forecasting toneRose
class node_templates,node_workspace_ui,node_chat_ui toneIndigo
class node_postgres,node_job_queue toneNeutral
```

## Components

### Flask Web App

- [Web process](https://github.com/wikiepeidia/anser/blob/main/app.py): Flask entry point · `app.py`
- [Domain routes](https://github.com/wikiepeidia/anser/blob/main/routes/inventory_routes.py): HTTP handlers · Inventory, sales, workflow, AI, admin, wallet, and workspace endpoints. · `routes/inventory_routes.py`
- [Sales & operations routes](https://github.com/wikiepeidia/anser/blob/main/routes/sales_routes.py): HTTP handlers · `routes/sales_routes.py`

### Core Domain & Trust

- [Auth & request boundary](https://github.com/wikiepeidia/anser/blob/main/core/auth.py): identity middleware · Protects authenticated, user-scoped actions. · `core/auth.py`
- [Database setup](https://github.com/wikiepeidia/anser/blob/main/core/database.py): persistence bootstrap · `core/database.py`
- [Domain models](https://github.com/wikiepeidia/anser/blob/main/core/models.py): ORM models · Ownership-scoped retail and back-office data. · `core/models.py`
- [Inventory transactions](https://github.com/wikiepeidia/anser/blob/main/core/services/inventory_tx_service.py): domain service · Centralizes consistent stock mutations. · `core/services/inventory_tx_service.py`
- [AI chat service](https://github.com/wikiepeidia/anser/blob/main/core/services/ai_chat_service.py): AI service · `core/services/ai_chat_service.py`
- [DL service client](https://github.com/wikiepeidia/anser/blob/main/core/services/dl_client.py): service client · `core/services/dl_client.py`

### Automation & Integrations

- [Workflow engine](https://github.com/wikiepeidia/anser/blob/main/core/workflow_engine.py): workflow runtime · `core/workflow_engine.py`
- [Automation engine](https://github.com/wikiepeidia/anser/blob/main/core/automation_engine.py): automation runtime · `core/automation_engine.py`
- [Google Workspace adapter](https://github.com/wikiepeidia/anser/blob/main/core/google_integration.py): integration adapter · `core/google_integration.py`
- [Make.com adapter](https://github.com/wikiepeidia/anser/blob/main/core/make_integration.py): integration adapter · Outbound webhook destinations require validation. · `core/make_integration.py`

### ML / OCR Service

- [DL service process](https://github.com/wikiepeidia/anser/blob/main/run_dl_service.py): ML API entry point · `run_dl_service.py`
- [Model API](https://github.com/wikiepeidia/anser/blob/main/dl_service/model_app.py): OCR and history API · `dl_service/model_app.py`
- [OCR endpoints](https://github.com/wikiepeidia/anser/blob/main/dl_service/api/ocr_routes.py): OCR API module · `dl_service/api/ocr_routes.py`
- [Forecasting service](https://github.com/wikiepeidia/anser/blob/main/dl_service/services/forecast_service.py): ML forecasting · Uses forecasting model assets including LSTM. · `dl_service/services/forecast_service.py`

### Server-Rendered UI

- [Jinja templates](https://github.com/wikiepeidia/anser/blob/main/ui/templates/base.html): server-rendered UI · `ui/templates/base.html`
- [Workspace builder](https://github.com/wikiepeidia/anser/blob/main/ui/templates/workspace_builder.html): Jinja page · `ui/templates/workspace_builder.html`
- [Chat enhancement](https://github.com/wikiepeidia/anser/blob/main/static/js/chat.js): static JavaScript · `static/js/chat.js`

### Other components

- **PostgreSQL / SQLite**: datastore · PostgreSQL in production; SQLite locally.
- **Redis / RQ**: background jobs · Runs slow jobs such as AI chat.

## Connections

- Web process → Domain routes: registers
- Web process → Sales & operations routes: registers
- Web process → Auth & request boundary: uses
- Domain routes → Inventory transactions: mutates stock through
- Sales & operations routes → Domain models: serves domain data
- Auth & request boundary → Domain models: enforces ownership on
- Inventory transactions → Domain models: updates
- Domain models → Database setup: persists via
- Database setup → PostgreSQL / SQLite: connects to
- Domain routes → Workflow engine: runs workflows
- Workflow engine → Automation engine: delegates automation
- Automation engine → Google Workspace adapter: invokes
- Automation engine → Make.com adapter: invokes
- Domain routes → DL service client: requests OCR and forecasts
- DL service client → Model API: calls
- DL service process → Model API: serves
- Model API → OCR endpoints: exposes
- Model API → Forecasting service: uses
- Domain routes → AI chat service: handles AI requests
- AI chat service → Redis / RQ: queues slow work
- Domain routes → Jinja templates: renders
- Jinja templates → Workspace builder: includes
- Workspace builder → Chat enhancement: enhanced by
