# gptplusplus/salesclaw architecture

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

- Interactive diagram: https://gitdiagram.com/gptplusplus/salesclaw
- Repository: https://github.com/gptplusplus/salesclaw

## Overview

1. **Overall shape**
   SalesClaw is a two-tier product: a **FastAPI backend** that owns domain data, reasoning, actions, and realtime coordination, and a **React + TypeScript frontend** that presents decision workflows, analysis panels, and agent-facing UI. The repository is centered on a closed-loop architecture: **perception → reasoning → planning → execution → learning**.

2. **Primary system boundary**
   The backend is the authoritative application core. It exposes REST endpoints, WebSocket streams, and LLM-backed chat/inference endpoints. The frontend is a consumer of those APIs and also contains some client-side reasoning/analysis modules for interactive visualization and recommendation UX.

3. **Data and persistence layer**
   Persistence is based on **SQLite** with **SQLAlchemy models** and **Alembic migrations**. The backend models directory shows the main persisted concepts: ontology objects, domain entities, inference results, execution logs, actions, memory, notifications, reminders, scenarios, permissions, audits, and users. The schema is organized around a unified ontology/object model rather than many unrelated CRUD tables.

4. **Domain model and ontology core**
   The dominant business abstraction is the **ontology/object layer**. Domain objects such as doctors, hospitals, products, sales targets, and compliance-related entities are represented through a unified object model and typed relationships. This is the foundation for graph-like reasoning and for presenting contextual views in the UI.

5. **Backend service layer**
   The backend’s `services/` package is the main orchestration layer. It separates concerns such as ontology management, reasoning, inference, chat, memory, permissions, scenario simulation, notifications, action execution, effect tracking, and lifecycle management. The routers are thin API adapters; the services contain the real application logic and cross-model coordination.

6. **Reasoning and inference subsystem**
   A major architectural feature is the reasoning stack. There are dedicated routers and services for causal, temporal, consistency, implicit-relation, abductive, analogical, counterfactual, hierarchical, multi-step, constraint-checking, and coordinated reasoning. This subsystem consumes ontology and domain data and produces inference artifacts that are surfaced in the UI and used by suggestion/insight features.

7. **Agent execution loop**
   The `backend/agents/` package and corresponding frontend `agent/` modules represent the agent-centric workflow. The backend agent layer defines state, tools, and a cognitive graph; it drives tool-using behavior such as fetching context, assessing compliance risk, executing actions, and sending notifications. This is tied to an **execution log** and approval lifecycle, so agent decisions are traceable rather than directly mutating state.

8. **Actions, approvals, and auditability**
   Action handling is a distinct subsystem, not just another CRUD path. Actions move through a lifecycle such as pending → approved → executed, with execution logs persisted for audit and effect tracking. This creates a governance boundary between “recommendation” and “system action,” which is important in regulated sales/medical contexts.

9. **Perception, suggestions, and insights**
   The system includes a realtime analytical layer that turns raw synchronized business data into higher-level judgments: anomaly detection, risk scoring, loyalty/perception signals, proactive suggestions, and aggregated insights. These features are exposed via dedicated routers/services and feed the decision UI, giving the platform a monitoring/early-warning function in addition to reasoning.

10. **Chat and realtime communication**
   There is a conversational interface backed by the backend chat service and optional OpenAI-compatible LLM integration. Realtime updates are delivered through a WebSocket manager and `/api/ws/{user_id}`, which the frontend uses for alerts, action-status changes, and reminder updates. This makes the system event-driven at the user interaction layer even though the core persistence is relational.

11. **Frontend architecture**
   The frontend is structured around feature-aligned modules rather than only page-based routing. It contains contexts for auth, ontology, perception, object state, and cognitive interaction; API hooks; domain-specific engines for perception, planning, inference, decision, interaction, and scenario simulation; and many focused UI panels for reasoning, ontology graphs, suggestions, reminders, and execution tracking. Tauri packaging indicates the same frontend can be delivered as a desktop app.

12. **External dependencies and integration boundaries**
   SalesClaw does not appear to own source data ingestion; it assumes upstream systems like ERP/CRM/work collaboration tools populate the database. LLM usage is optional and behind an OpenAI-compatible interface, with fallback behavior when not configured. The key external boundary is therefore: **enterprise source systems → SalesClaw database → reasoning/decision services → human review/execution**.

13. **Operational and delivery structure**
   The repo includes tests, seed data, environment templates, and build configuration for both backend and frontend. Alembic migrations define the persistence contract, backend tests cover the service/router layers, and frontend tests focus on core decision logic. The architecture is designed to be deployable as a standard web stack or as a desktop-capable client layered over the same backend.

14. **What to show in an architecture diagram**
   The most important boxes are: external business systems, SQLite database, FastAPI backend, service subsystems (ontology/reasoning/perception/actions/chat/agent/event-bus), WebSocket channel, and the React/Tauri frontend. The central data flow should emphasize that domain objects feed reasoning, reasoning feeds suggestions/actions, actions emit execution logs and realtime updates, and the frontend reads both the analytical results and the live event stream.

Main entry points: [`backend/services`](https://github.com/gptplusplus/salesclaw/tree/main/backend/services) (Service Layer), [`frontend/src/App.tsx`](https://github.com/gptplusplus/salesclaw/blob/main/frontend/src/App.tsx) (React Client), [`backend/services/action_service.py`](https://github.com/gptplusplus/salesclaw/blob/main/backend/services/action_service.py) (Actions & Effects), [`backend/models`](https://github.com/gptplusplus/salesclaw/tree/main/backend/models) (ORM Models)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_external["External Systems"]
  node_ext_sources["Business Systems<br/>upstream data"]
  node_ext_users(("Human Reviewers<br/>operators"))
end

subgraph group_backend["Backend"]
  node_backend_app["FastAPI Core<br/>api app<br/>[main.py]"]
  node_backend_routers["API Routers<br/>http adapters"]
  node_backend_services["Service Layer<br/>orchestration services"]
  node_backend_agents{{"Agent Runtime<br/>cognitive agent"}}
  node_backend_chat["Chat & LLM<br/>conversation layer<br/>[llm.py]"]
  node_backend_ws["Realtime Bus<br/>ws manager<br/>[ws_manager.py]"]
  node_backend_eventbus["Event Bus<br/>event coordination<br/>[event_bus.py]"]
  node_backend_reasoning{{"Reasoning Stack<br/>inference services"}}
  node_backend_perception{{"Perception & Insights<br/>analytics services<br/>[perception.py]"}}
  node_backend_actions{{"Actions & Effects<br/>governed execution<br/>[action_service.py]"}}
  node_backend_ontology[("Ontology Core<br/>domain graph")]
  node_backend_memory["Memory & Context<br/>state services<br/>[memory_service.py]"]
end

subgraph group_frontend["Frontend"]
  node_frontend_app["React Client<br/>ui app<br/>[App.tsx]"]
  node_frontend_contexts["App Contexts<br/>state providers"]
  node_frontend_feature_engines{{"Feature Engines<br/>client logic"}}
  node_frontend_panels["Decision Panels<br/>ui components"]
  node_frontend_agent{{"Agent Modules<br/>agent ui"}}
  node_frontend_api["API Client<br/>http client"]
end

subgraph group_data["Persistence"]
  node_db_models[("ORM Models<br/>sqlalchemy models")]
  node_db_migrations["Migrations<br/>alembic schema"]
  node_db_sqlite[("SQLite DB<br/>persistent store<br/>[salesclaw.db-wal]")]
end

node_ext_sources -->|"populate"| node_db_sqlite
node_ext_users -->|"use"| node_frontend_app
node_frontend_app -->|"composes"| node_frontend_contexts
node_frontend_app -->|"uses"| node_frontend_feature_engines
node_frontend_app -->|"renders"| node_frontend_panels
node_frontend_app -->|"includes"| node_frontend_agent
node_frontend_app -->|"calls"| node_frontend_api
node_frontend_api -->|"requests"| node_backend_app
node_backend_app -->|"mounts"| node_backend_routers
node_backend_routers -->|"delegates"| node_backend_services
node_backend_services -->|"reads/writes"| node_backend_ontology
node_backend_services -->|"invokes"| node_backend_reasoning
node_backend_services -->|"produces"| node_backend_perception
node_backend_services -->|"executes"| node_backend_actions
node_backend_services -->|"enriches"| node_backend_memory
node_backend_services -->|"uses"| node_backend_chat
node_backend_services -->|"publishes"| node_backend_eventbus
node_backend_eventbus -->|"delivers"| node_backend_ws
node_backend_actions -->|"notifies"| node_backend_ws
node_backend_reasoning -->|"supports"| node_backend_agents
node_backend_agents -->|"triggers"| node_backend_actions
node_backend_ontology -->|"persists"| node_db_models
node_backend_actions -->|"persists"| node_db_models
node_backend_memory -->|"persists"| node_db_models
node_db_models -->|"evolves via"| node_db_migrations
node_backend_app -->|"connects"| node_db_sqlite
node_backend_ws -->|"streams"| node_frontend_panels

click node_backend_app "https://github.com/gptplusplus/salesclaw/blob/main/backend/main.py"
click node_backend_routers "https://github.com/gptplusplus/salesclaw/tree/main/backend/routers"
click node_backend_services "https://github.com/gptplusplus/salesclaw/tree/main/backend/services"
click node_backend_agents "https://github.com/gptplusplus/salesclaw/tree/main/backend/agents"
click node_backend_chat "https://github.com/gptplusplus/salesclaw/blob/main/backend/llm.py"
click node_backend_ws "https://github.com/gptplusplus/salesclaw/blob/main/backend/services/ws_manager.py"
click node_backend_eventbus "https://github.com/gptplusplus/salesclaw/blob/main/backend/services/event_bus.py"
click node_backend_reasoning "https://github.com/gptplusplus/salesclaw/blob/main/backend/services/reasoning_service.py"
click node_backend_perception "https://github.com/gptplusplus/salesclaw/blob/main/backend/routers/perception.py"
click node_backend_actions "https://github.com/gptplusplus/salesclaw/blob/main/backend/services/action_service.py"
click node_backend_ontology "https://github.com/gptplusplus/salesclaw/blob/main/backend/services/ontology_service.py"
click node_backend_memory "https://github.com/gptplusplus/salesclaw/blob/main/backend/services/memory_service.py"
click node_db_models "https://github.com/gptplusplus/salesclaw/tree/main/backend/models"
click node_db_migrations "https://github.com/gptplusplus/salesclaw/tree/main/backend/alembic"
click node_db_sqlite "https://github.com/gptplusplus/salesclaw/blob/main/backend/salesclaw.db-wal"
click node_frontend_app "https://github.com/gptplusplus/salesclaw/blob/main/frontend/src/App.tsx"
click node_frontend_contexts "https://github.com/gptplusplus/salesclaw/tree/main/frontend/src/contexts"
click node_frontend_feature_engines "https://github.com/gptplusplus/salesclaw/tree/main/frontend/src/perception"
click node_frontend_panels "https://github.com/gptplusplus/salesclaw/tree/main/frontend/src/components"
click node_frontend_agent "https://github.com/gptplusplus/salesclaw/tree/main/frontend/src/agent"
click node_frontend_api "https://github.com/gptplusplus/salesclaw/tree/main/frontend/src/api"

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_ext_sources,node_ext_users toneBlue
class node_backend_app,node_backend_routers,node_backend_services,node_backend_agents,node_backend_chat,node_backend_ws,node_backend_eventbus,node_backend_reasoning,node_backend_perception,node_backend_actions,node_backend_ontology,node_backend_memory toneAmber
class node_frontend_app,node_frontend_contexts,node_frontend_feature_engines,node_frontend_panels,node_frontend_agent,node_frontend_api toneMint
class node_db_models,node_db_migrations,node_db_sqlite toneRose
```

## Components

### External Systems

Upstream business sources and human operators that interact with SalesClaw.

- **Business Systems**: upstream data · ERP, CRM, and other enterprise systems that populate the SalesClaw domain store.
- **Human Reviewers**: operators · Users who review reasoning, approve actions, and monitor live updates.

### Backend

FastAPI application core with data, services, agents, and realtime coordination.

- [FastAPI Core](https://github.com/gptplusplus/salesclaw/blob/main/backend/main.py): api app · Application entrypoint exposing REST, WebSocket, and orchestration endpoints. · `backend/main.py`
- [API Routers](https://github.com/gptplusplus/salesclaw/tree/main/backend/routers): http adapters · Thin route layer for ontology, reasoning, inference, actions, chat, insights, perception, and events. · `backend/routers`
- [Service Layer](https://github.com/gptplusplus/salesclaw/tree/main/backend/services): orchestration services · Main application logic for ontology, reasoning, inference, memory, permissions, scenarios, actions, and effects. · `backend/services`
- [Agent Runtime](https://github.com/gptplusplus/salesclaw/tree/main/backend/agents): cognitive agent · Tool-using agent loop with state, tools, and a cognitive graph for traceable execution. · `backend/agents`
- [Chat & LLM](https://github.com/gptplusplus/salesclaw/blob/main/backend/llm.py): conversation layer · Chat service and optional OpenAI-compatible LLM integration with guardrails. · `backend/llm.py`
- [Realtime Bus](https://github.com/gptplusplus/salesclaw/blob/main/backend/services/ws_manager.py): ws manager · WebSocket coordination and event delivery for alerts, reminders, and action updates. · `backend/services/ws_manager.py`
- [Event Bus](https://github.com/gptplusplus/salesclaw/blob/main/backend/services/event_bus.py): event coordination · Internal event dispatch used to connect action execution, inference, and notifications. · `backend/services/event_bus.py`
- [Reasoning Stack](https://github.com/gptplusplus/salesclaw/blob/main/backend/services/reasoning_service.py): inference services · Causal, temporal, abductive, analogical, counterfactual, hierarchical, and multi-step reasoning. · `backend/services/reasoning_service.py`
- [Perception & Insights](https://github.com/gptplusplus/salesclaw/blob/main/backend/routers/perception.py): analytics services · Anomaly detection, risk scoring, proactive suggestions, and insight generation from synchronized business data. · `backend/routers/perception.py`
- [Actions & Effects](https://github.com/gptplusplus/salesclaw/blob/main/backend/services/action_service.py): governed execution · Action lifecycle, approval, execution, and effect tracking with auditability. · `backend/services/action_service.py`
- [Ontology Core](https://github.com/gptplusplus/salesclaw/blob/main/backend/services/ontology_service.py): domain graph · Unified ontology and object model for domain entities, typed relationships, and contextual views. · `backend/services/ontology_service.py`
- [Memory & Context](https://github.com/gptplusplus/salesclaw/blob/main/backend/services/memory_service.py): state services · Memory, ontology context, lifecycle, and domain mapping used to enrich reasoning and UI context. · `backend/services/memory_service.py`

### Frontend

React + TypeScript client for decision workflows, analysis, and agent UI.

- [React Client](https://github.com/gptplusplus/salesclaw/blob/main/frontend/src/App.tsx): ui app · TypeScript frontend that assembles decision, ontology, perception, and agent workflows. · `frontend/src/App.tsx`
- [App Contexts](https://github.com/gptplusplus/salesclaw/tree/main/frontend/src/contexts): state providers · Auth, ontology, perception, object, and cognitive interaction state shared across the UI. · `frontend/src/contexts`
- [Feature Engines](https://github.com/gptplusplus/salesclaw/tree/main/frontend/src/perception): client logic · Perception, planning, inference, decision, interaction, scenario, and ontology logic used by the UI. · `frontend/src/perception`
- [Decision Panels](https://github.com/gptplusplus/salesclaw/tree/main/frontend/src/components): ui components · Reasoning, suggestion, execution, chat, ontology graph, reminders, and detail views. · `frontend/src/components`
- [Agent Modules](https://github.com/gptplusplus/salesclaw/tree/main/frontend/src/agent): agent ui · Client-side agent learning, memory, and cognitive coordination modules. · `frontend/src/agent`
- [API Client](https://github.com/gptplusplus/salesclaw/tree/main/frontend/src/api): http client · Typed client and hooks for consuming backend REST and realtime APIs. · `frontend/src/api`

### Persistence

SQLite schema, ORM models, and migrations.

- [ORM Models](https://github.com/gptplusplus/salesclaw/tree/main/backend/models): sqlalchemy models · Persisted concepts including ontology objects, domain entities, inference, execution, memory, notifications, reminders, scenarios, permissions, audits, and users. · `backend/models`
- [Migrations](https://github.com/gptplusplus/salesclaw/tree/main/backend/alembic): alembic schema · Database migration history defining the SQLite persistence contract. · `backend/alembic`
- [SQLite DB](https://github.com/gptplusplus/salesclaw/blob/main/backend/salesclaw.db-wal): persistent store · Local relational database backing domain data, logs, and derived artifacts. · `backend/salesclaw.db-wal`

## Connections

- Business Systems → SQLite DB: populate · Upstream operational systems feed SalesClaw's persisted domain store.
- Human Reviewers → React Client: use · Humans interact with the decision UI and approve or review actions.
- React Client → App Contexts: composes · The app is structured around shared UI state providers.
- React Client → Feature Engines: uses · Client-side reasoning and planning modules drive interactive UX.
- React Client → Decision Panels: renders · Decision and analysis panels present ontology, reasoning, and execution state.
- React Client → Agent Modules: includes · Agent-facing modules support learning and cognitive workflows.
- React Client → API Client: calls · The frontend consumes backend REST and realtime endpoints through a typed client.
- API Client → FastAPI Core: requests · Client API traffic reaches the FastAPI core.
- FastAPI Core → API Routers: mounts · The application delegates HTTP surface area to focused routers.
- API Routers → Service Layer: delegates · Routers hand off business logic to services.
- Service Layer → Ontology Core: reads/writes · Ontology services manage the unified domain object graph.
- Service Layer → Reasoning Stack: invokes · Reasoning services compute inferences and consistency checks.
- Service Layer → Perception & Insights: produces · Perception and insight services generate anomaly and suggestion outputs.
- Service Layer → Actions & Effects: executes · Action services govern approval, execution, and effect tracking.
- Service Layer → Memory & Context: enriches · Memory and context services supply conversational and operational state.
- Service Layer → Chat & LLM: uses · Chat service provides conversational access and LLM-backed assistance.
- Service Layer → Event Bus: publishes · Services emit internal events for downstream consumers.
- Event Bus → Realtime Bus: delivers · Events are pushed to connected clients over realtime channels.
- Actions & Effects → Realtime Bus: notifies · Action status changes are broadcast to the UI.
- Reasoning Stack → Agent Runtime: supports · Agent behavior relies on reasoning outputs and tool orchestration.
- Agent Runtime → Actions & Effects: triggers · Agents propose or initiate governed actions through the action layer.
- Ontology Core → ORM Models: persists · Ontology objects and relationships are stored via ORM models.
- Actions & Effects → ORM Models: persists · Action lifecycle, execution logs, and audit data are recorded in the schema.
- Memory & Context → ORM Models: persists · Memory, notifications, reminders, and related context are stored in the database.
- ORM Models → Migrations: evolves via · Alembic migrations define and update the schema.
- FastAPI Core → SQLite DB: connects · The backend uses SQLite as the relational persistence layer.
- Realtime Bus → Decision Panels: streams · Realtime updates feed alerts, execution tracking, and reminder UI.
