# rangonroyutsab/ledgerly architecture

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

- Interactive diagram: https://gitdiagram.com/rangonroyutsab/ledgerly
- Repository: https://github.com/rangonroyutsab/ledgerly

## Overview

#### Purpose and Runtime
Ledgerly is a personal expense tracker with conventional CRUD workflows and an AI chat interface for recording and querying expenses. The backend is Django/Django REST Framework; the client is a React/Vite single-page application. Backend startup is through `manage.py`, `core/asgi.py`, or `core/wsgi.py`; frontend startup begins at `frontend/src/main.jsx`.

#### HTTP and Application Composition
Django settings and top-level route composition are centralized in `core/settings.py` and `core/urls.py`. Domain apps expose their own route/view boundaries:
- `apps/users/urls.py`, `apps/users/views.py`
- `apps/expenses/urls.py`, `apps/expenses/views.py`
- `apps/categories/urls.py`, `apps/categories/views.py`
- `apps/presets/urls.py`, `apps/presets/views.py`
- `apps/assistant/urls.py`, `apps/assistant/views.py`

Views/serializers form the REST boundary; services and selectors keep domain operations and read access out of transport handlers.

#### Identity and Data Isolation
The users domain owns the custom user model and authentication-facing API contracts in `apps/users/models.py`, `apps/users/serializers.py`, and `apps/users/views.py`. JWT-protected frontend routing is represented by `frontend/src/contexts/AuthContext.jsx` and `frontend/src/components/ProtectedRoute.jsx`. Expense, category, preset, and assistant operations are intended to be scoped to the authenticated user rather than globally shared.

#### Expense and Category Domain
Expenses are the core ledger records, modeled and operated through `apps/expenses/models.py`, `apps/expenses/services/expenses.py`, and `apps/expenses/selectors/expenses.py`. Categories have independent persistence and business operations in `apps/categories/models.py`, `apps/categories/services/categories.py`, and `apps/categories/selectors/`. Category signals (`apps/categories/signals.py`) imply lifecycle coupling, likely for maintaining defaults or related state. Presets provide reusable expense-oriented data/templates through `apps/presets/models.py`, `apps/presets/serializers.py`, and `apps/presets/views.py`.

#### Assistant Processing Pipeline
The assistant API delegates natural-language handling into a staged service layer:
1. Request/chat transport: `apps/assistant/views.py`
2. Provider-neutral AI interface and selection: `apps/assistant/providers/base.py`, `apps/assistant/providers/factory.py`
3. Provider invocation: `apps/assistant/providers/gemini.py` or `apps/assistant/providers/ollama.py`
4. AI-output parsing and intent orchestration: `apps/assistant/services/parser.py`, `apps/assistant/services/intent_handler.py`
5. Domain resolution and mutation/query messaging: `apps/assistant/services/resolvers.py`, `apps/assistant/services/category_resolution.py`, `apps/assistant/services/messaging.py`

This separates nondeterministic LLM interpretation from deterministic resolution against user, category, and expense data.

#### Frontend Control Flow
`frontend/src/App.jsx` composes page-level routes for login, dashboard, expenses, categories, presets, settings, and assistant. API communication is centralized through `frontend/src/api/axios.js`; auth state and assistant conversation state are held in `frontend/src/contexts/AuthContext.jsx` and `frontend/src/contexts/AssistantChatContext.jsx`. Primary interaction surfaces include `frontend/src/pages/Expenses.jsx`, `frontend/src/pages/Categories.jsx`, and `frontend/src/pages/Assistant.jsx`.

#### Infrastructure and External Boundaries
PostgreSQL is the persistent store, configured through `DATABASE_URL`; schema evolution lives in per-app migrations such as `apps/expenses/migrations/0001_initial.py`. Docker deployment is defined by `Dockerfile` and `docker-compose.yml`. AI is pluggable via `AI_PROVIDER`: Gemini requires an external API key, while Ollama targets a configurable local/network inference endpoint. Backend API and database run in Compose; the Vite frontend is run separately in development.

Main entry points: [`core/urls.py`](https://github.com/rangonroyutsab/ledgerly/blob/master/core/urls.py) (API composition), [`frontend/src/App.jsx`](https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/App.jsx) (App routes), [`frontend/src/api/axios.js`](https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/api/axios.js) (API client), [`apps/assistant/services/resolvers.py`](https://github.com/rangonroyutsab/ledgerly/blob/master/apps/assistant/services/resolvers.py) (Domain resolution)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["React SPA"]
  node_spa["App routes<br/>React composition<br/>[App.jsx]"]
  node_auth_state["Auth state<br/>JWT session<br/>[AuthContext.jsx]"]
  node_protected_routes["Protected routes<br/>route guard<br/>[ProtectedRoute.jsx]"]
  node_ledger_pages["Ledger pages<br/>expense/category UI<br/>[Expenses.jsx]"]
  node_assistant_ui["Assistant UI<br/>chat page<br/>[Assistant.jsx]"]
  node_api_client["API client<br/>Axios transport<br/>[axios.js]"]
end

subgraph group_backend["Django API"]
  node_django_composition["API composition<br/>Django settings/routes<br/>[urls.py]"]
  node_identity_api["Users &amp; auth API<br/>DRF domain boundary<br/>[views.py]"]
  node_expense_domain["Expense domain<br/>services/selectors<br/>[expenses.py]"]
  node_category_domain["Category domain<br/>services/selectors<br/>[categories.py]"]
  node_preset_api["Preset API<br/>DRF domain boundary<br/>[views.py]"]
end

subgraph group_assistant["Assistant pipeline"]
  node_assistant_api["Assistant API<br/>chat transport<br/>[views.py]"]
  node_provider_selection["AI provider selection<br/>provider factory<br/>[factory.py]"]
  node_llm_provider{{"Gemini or Ollama<br/>LLM invocation<br/>[gemini.py]"}}
  node_intent_pipeline["Parse &amp; intent handling<br/>AI orchestration<br/>[intent_handler.py]"]
  node_domain_resolution["Domain resolution<br/>assistant services<br/>[resolvers.py]"]
end

subgraph group_external["Infrastructure"]
  node_postgres[("PostgreSQL<br/>persistent store")]
  node_compose["Docker Compose<br/>deployment<br/>[docker-compose.yml]"]
end

node_spa -->|"uses"| node_auth_state
node_spa -->|"guards"| node_protected_routes
node_spa -->|"routes to"| node_ledger_pages
node_spa -->|"routes to"| node_assistant_ui
node_auth_state -->|"supplies JWT"| node_api_client
node_ledger_pages -->|"CRUD requests"| node_api_client
node_assistant_ui -->|"chat requests"| node_api_client
node_api_client -->|"HTTP API"| node_django_composition
node_django_composition -->|"routes"| node_identity_api
node_django_composition -->|"routes"| node_expense_domain
node_django_composition -->|"routes"| node_category_domain
node_django_composition -->|"routes"| node_preset_api
node_django_composition -->|"routes"| node_assistant_api
node_identity_api -->|"users"| node_postgres
node_expense_domain -->|"expenses"| node_postgres
node_category_domain -->|"categories"| node_postgres
node_preset_api -->|"presets"| node_postgres
node_assistant_api -->|"delegates"| node_provider_selection
node_provider_selection -->|"invokes"| node_llm_provider
node_llm_provider -->|"model output"| node_intent_pipeline
node_intent_pipeline -->|"intent"| node_domain_resolution
node_domain_resolution -->|"queries or mutates"| node_expense_domain
node_domain_resolution -->|"resolves categories"| node_category_domain
node_compose -->|"runs API"| node_django_composition
node_compose -->|"runs database"| node_postgres

click node_spa "https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/App.jsx"
click node_auth_state "https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/contexts/AuthContext.jsx"
click node_protected_routes "https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/components/ProtectedRoute.jsx"
click node_ledger_pages "https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/pages/Expenses.jsx"
click node_assistant_ui "https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/pages/Assistant.jsx"
click node_api_client "https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/api/axios.js"
click node_django_composition "https://github.com/rangonroyutsab/ledgerly/blob/master/core/urls.py"
click node_identity_api "https://github.com/rangonroyutsab/ledgerly/blob/master/apps/users/views.py"
click node_expense_domain "https://github.com/rangonroyutsab/ledgerly/blob/master/apps/expenses/services/expenses.py"
click node_category_domain "https://github.com/rangonroyutsab/ledgerly/blob/master/apps/categories/services/categories.py"
click node_preset_api "https://github.com/rangonroyutsab/ledgerly/blob/master/apps/presets/views.py"
click node_assistant_api "https://github.com/rangonroyutsab/ledgerly/blob/master/apps/assistant/views.py"
click node_provider_selection "https://github.com/rangonroyutsab/ledgerly/blob/master/apps/assistant/providers/factory.py"
click node_llm_provider "https://github.com/rangonroyutsab/ledgerly/blob/master/apps/assistant/providers/gemini.py"
click node_intent_pipeline "https://github.com/rangonroyutsab/ledgerly/blob/master/apps/assistant/services/intent_handler.py"
click node_domain_resolution "https://github.com/rangonroyutsab/ledgerly/blob/master/apps/assistant/services/resolvers.py"
click node_compose "https://github.com/rangonroyutsab/ledgerly/blob/master/docker-compose.yml"

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_spa,node_auth_state,node_protected_routes,node_ledger_pages,node_assistant_ui,node_api_client toneBlue
class node_django_composition,node_identity_api,node_expense_domain,node_category_domain,node_preset_api toneAmber
class node_assistant_api,node_provider_selection,node_llm_provider,node_intent_pipeline,node_domain_resolution toneMint
class node_postgres,node_compose toneRose
```

## Components

### React SPA

- [App routes](https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/App.jsx): React composition · Composes login, ledger, settings, and assistant pages. · `frontend/src/App.jsx`
- [Auth state](https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/contexts/AuthContext.jsx): JWT session · Maintains authenticated client state. · `frontend/src/contexts/AuthContext.jsx`
- [Protected routes](https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/components/ProtectedRoute.jsx): route guard · `frontend/src/components/ProtectedRoute.jsx`
- [Ledger pages](https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/pages/Expenses.jsx): expense/category UI · `frontend/src/pages/Expenses.jsx`
- [Assistant UI](https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/pages/Assistant.jsx): chat page · `frontend/src/pages/Assistant.jsx`
- [API client](https://github.com/rangonroyutsab/ledgerly/blob/master/frontend/src/api/axios.js): Axios transport · `frontend/src/api/axios.js`

### Django API

- [API composition](https://github.com/rangonroyutsab/ledgerly/blob/master/core/urls.py): Django settings/routes · Central settings and top-level URL composition. · `core/urls.py`
- [Users & auth API](https://github.com/rangonroyutsab/ledgerly/blob/master/apps/users/views.py): DRF domain boundary · Owns the custom user and authentication contracts. · `apps/users/views.py`
- [Expense domain](https://github.com/rangonroyutsab/ledgerly/blob/master/apps/expenses/services/expenses.py): services/selectors · User-scoped ledger writes and reads. · `apps/expenses/services/expenses.py`
- [Category domain](https://github.com/rangonroyutsab/ledgerly/blob/master/apps/categories/services/categories.py): services/selectors · Independent categories with lifecycle signals. · `apps/categories/services/categories.py`
- [Preset API](https://github.com/rangonroyutsab/ledgerly/blob/master/apps/presets/views.py): DRF domain boundary · Reusable expense-oriented templates. · `apps/presets/views.py`

### Assistant pipeline

- [Assistant API](https://github.com/rangonroyutsab/ledgerly/blob/master/apps/assistant/views.py): chat transport · `apps/assistant/views.py`
- [AI provider selection](https://github.com/rangonroyutsab/ledgerly/blob/master/apps/assistant/providers/factory.py): provider factory · Selects a provider-neutral AI implementation. · `apps/assistant/providers/factory.py`
- [Gemini or Ollama](https://github.com/rangonroyutsab/ledgerly/blob/master/apps/assistant/providers/gemini.py): LLM invocation · External Gemini API or configurable Ollama endpoint. · `apps/assistant/providers/gemini.py`
- [Parse & intent handling](https://github.com/rangonroyutsab/ledgerly/blob/master/apps/assistant/services/intent_handler.py): AI orchestration · Turns model output into deterministic intents. · `apps/assistant/services/intent_handler.py`
- [Domain resolution](https://github.com/rangonroyutsab/ledgerly/blob/master/apps/assistant/services/resolvers.py): assistant services · Resolves users and categories, mutates or queries ledger data, and formats replies. · `apps/assistant/services/resolvers.py`

### Infrastructure

- **PostgreSQL**: persistent store · Schema is evolved through per-app migrations.
- [Docker Compose](https://github.com/rangonroyutsab/ledgerly/blob/master/docker-compose.yml): deployment · Runs backend API and database. · `docker-compose.yml`

## Connections

- App routes → Auth state: uses
- App routes → Protected routes: guards
- App routes → Ledger pages: routes to
- App routes → Assistant UI: routes to
- Auth state → API client: supplies JWT
- Ledger pages → API client: CRUD requests
- Assistant UI → API client: chat requests
- API client → API composition: HTTP API
- API composition → Users & auth API: routes
- API composition → Expense domain: routes
- API composition → Category domain: routes
- API composition → Preset API: routes
- API composition → Assistant API: routes
- Users & auth API → PostgreSQL: users
- Expense domain → PostgreSQL: expenses
- Category domain → PostgreSQL: categories
- Preset API → PostgreSQL: presets
- Assistant API → AI provider selection: delegates
- AI provider selection → Gemini or Ollama: invokes
- Gemini or Ollama → Parse & intent handling: model output
- Parse & intent handling → Domain resolution: intent
- Domain resolution → Expense domain: queries or mutates
- Domain resolution → Category domain: resolves categories
- Docker Compose → API composition: runs API
- Docker Compose → PostgreSQL: runs database
