# ama-cmfai/lambda architecture

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

## Overview

#### Purpose and Runtime Topology
LAMBDA is a local, conversational data-analysis agent: users upload datasets, ask questions, and receive executable analysis, visualizations, artifacts, and exports. It runs as a React/Vite browser client plus a Python 3.11+ FastAPI service. Primary entry points are `frontend/src/main.tsx`, `frontend/src/App.tsx`, and `backend/main.py`; `start.sh` launches both.

#### Frontend Application Shell
The frontend presents the conversation workspace, including chat, file inspection, settings, and navigation. `frontend/src/pages/Chat.tsx` composes the main analysis experience; `frontend/src/components/chat/ChatInput.tsx` submits prompts/files and `frontend/src/components/chat/MessageList.tsx` renders streamed conversational/tool output. `frontend/src/components/layout/Sidebar.tsx` manages conversation navigation, while `frontend/src/components/layout/RightPanel.tsx` exposes workspace artifacts.

#### Client/API Boundary
The browser communicates with FastAPI through centralized client wrappers in `frontend/src/services/api.ts` and export support in `frontend/src/services/export.ts`. Shared UI request/result shapes are represented in `frontend/src/types/index.ts`. This is the boundary between untrusted browser interaction and backend-controlled persistence, LLM calls, file operations, and code execution.

#### FastAPI API and Session Persistence
`backend/main.py` hosts versioned API routes. `backend/app/api/v1/chat.py` drives agent chat requests; `backend/app/api/v1/conversations.py`, `backend/app/api/v1/files.py`, `backend/app/api/v1/code_execution.py`, and `backend/app/api/v1/export.py` expose conversation lifecycle, workspace files, execution, and deliverable generation. Database configuration and persistence models are centralized in `backend/app/core/database.py` and `backend/app/models/models.py`, using local SQLite runtime storage.

#### Agent Orchestration and Model Integration
The analysis control loop is centered on `backend/app/services/agent_service.py`: it turns conversation context, uploaded data, and user intent into model-guided tool use and responses. `backend/app/services/llm_service.py` calls an externally configured OpenAI-compatible `/chat/completions` provider. Model endpoint, API key, available models, and default selection enter through `backend/app/core/config.py`; the model provider is therefore an external dependency, not embedded inference.

#### Stateful Code-Execution Workspace
Analysis code is executed against a per-conversation workspace, preserving variables and generated files across turns. `backend/app/services/code_execution_service.py` coordinates execution; `backend/app/services/notebook_kernel.py` and `backend/app/services/local_code_execution.py` provide notebook/kernel and local-runtime paths. `backend/app/services/opensandbox_service.py` represents an alternate sandbox integration, but the documented default executes on the backend machine. This is the primary security boundary: deployment must restrict access to trusted users/data or provide isolation.

#### Files, Artifacts, and Exports
Uploads and generated outputs are tracked as conversation workspace artifacts. `backend/app/services/sandbox_file_manager.py` manages workspace-facing file operations and `backend/app/services/oss_service.py` covers object-storage-oriented handling. Export stages use `backend/app/services/notebook_service.py` for notebooks, `backend/app/services/report_service.py` for report bundles, and `backend/app/services/pdf_service.py` plus `backend/app/services/latex_service.py` for PDF/LaTeX output. Optional TeX Live/XeLaTeX and CJK fonts, installed via `scripts/install-system-deps.sh`, support PDF and Chinese reports.

Main entry points: [`frontend/src/pages/Chat.tsx`](https://github.com/ama-cmfai/lambda/blob/main/frontend/src/pages/Chat.tsx) (Analysis chat workspace), [`backend/app/api/v1/conversations.py`](https://github.com/ama-cmfai/lambda/blob/main/backend/app/api/v1/conversations.py) (Conversation, file, execution, export APIs), [`backend/app/services/agent_service.py`](https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/agent_service.py) (Agent control loop), [`backend/app/services/code_execution_service.py`](https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/code_execution_service.py) (Stateful code execution)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Browser client"]
  node_app["React app shell<br/>React/Vite entry<br/>[App.tsx]"]
  node_chat_ui["Analysis chat workspace<br/>page<br/>[Chat.tsx]"]
  node_chat_controls["Prompt, files, and stream UI<br/>chat components<br/>[ChatInput.tsx]"]
  node_navigation["Conversation navigation<br/>layout component<br/>[Sidebar.tsx]"]
  node_artifacts_ui["Artifact panel<br/>layout component<br/>[RightPanel.tsx]"]
  node_api_client["API and export client<br/>client boundary<br/>[api.ts]"]
end

subgraph group_api["FastAPI service"]
  node_api_host["FastAPI host<br/>application entry<br/>[main.py]"]
  node_chat_api["Chat API<br/>v1 route<br/>[chat.py]"]
  node_workspace_apis["Conversation, file, execution, export APIs<br/>v1 routes<br/>[conversations.py]"]
  node_persistence[("SQLite persistence<br/>database layer<br/>[database.py]")]
end

subgraph group_agent["Agent and execution"]
  node_agent_service{{"Agent control loop<br/>orchestrator<br/>[agent_service.py]"}}
  node_llm_service["LLM integration<br/>model client<br/>[llm_service.py]"]
  node_model_provider(("OpenAI-compatible provider<br/>external dependency<br/>[config.py]"))
  node_execution_service{{"Stateful code execution<br/>execution coordinator"}}
  node_local_kernel["Local notebook runtime<br/>execution runtime<br/>[notebook_kernel.py]"]
  node_sandbox["Optional sandbox runtime<br/>sandbox integration"]
end

subgraph group_outputs["Workspace outputs"]
  node_workspace_files["Workspace file manager<br/>artifact service"]
  node_exports["Notebook and report exports<br/>export pipeline"]
  node_pdf_toolchain["TeX/PDF toolchain<br/>system dependency"]
end

node_start["Local launcher<br/>startup script<br/>[start.sh]"]

node_start -->|"launches"| node_app
node_start -->|"launches"| node_api_host
node_app -->|"renders"| node_chat_ui
node_chat_ui -->|"uses"| node_chat_controls
node_chat_ui -->|"uses"| node_navigation
node_chat_ui -->|"shows"| node_artifacts_ui
node_chat_controls -->|"submits prompts and files"| node_api_client
node_api_client -->|"versioned HTTP API"| node_api_host
node_api_host -->|"routes chat"| node_chat_api
node_api_host -->|"routes workspace operations"| node_workspace_apis
node_chat_api -->|"invokes"| node_agent_service
node_workspace_apis -->|"reads and writes"| node_persistence
node_agent_service -->|"loads conversation context"| node_persistence
node_agent_service -->|"requests tool-guided responses"| node_llm_service
node_llm_service -->|"chat completions"| node_model_provider
node_agent_service -->|"executes analysis tools"| node_execution_service
node_execution_service -->|"default runtime"| node_local_kernel
node_execution_service -.->|"alternate runtime"| node_sandbox
node_execution_service -->|"reads and generates artifacts"| node_workspace_files
node_workspace_apis -->|"manages workspace files"| node_workspace_files
node_workspace_apis -->|"generates deliverables"| node_exports
node_exports -->|"renders PDF/LaTeX"| node_pdf_toolchain
node_artifacts_ui -->|"retrieves artifacts"| node_api_client

click node_start "https://github.com/ama-cmfai/lambda/blob/main/start.sh"
click node_app "https://github.com/ama-cmfai/lambda/blob/main/frontend/src/App.tsx"
click node_chat_ui "https://github.com/ama-cmfai/lambda/blob/main/frontend/src/pages/Chat.tsx"
click node_chat_controls "https://github.com/ama-cmfai/lambda/blob/main/frontend/src/components/chat/ChatInput.tsx"
click node_navigation "https://github.com/ama-cmfai/lambda/blob/main/frontend/src/components/layout/Sidebar.tsx"
click node_artifacts_ui "https://github.com/ama-cmfai/lambda/blob/main/frontend/src/components/layout/RightPanel.tsx"
click node_api_client "https://github.com/ama-cmfai/lambda/blob/main/frontend/src/services/api.ts"
click node_api_host "https://github.com/ama-cmfai/lambda/blob/main/backend/main.py"
click node_chat_api "https://github.com/ama-cmfai/lambda/blob/main/backend/app/api/v1/chat.py"
click node_workspace_apis "https://github.com/ama-cmfai/lambda/blob/main/backend/app/api/v1/conversations.py"
click node_persistence "https://github.com/ama-cmfai/lambda/blob/main/backend/app/core/database.py"
click node_agent_service "https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/agent_service.py"
click node_llm_service "https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/llm_service.py"
click node_model_provider "https://github.com/ama-cmfai/lambda/blob/main/backend/app/core/config.py"
click node_execution_service "https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/code_execution_service.py"
click node_local_kernel "https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/notebook_kernel.py"
click node_sandbox "https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/opensandbox_service.py"
click node_workspace_files "https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/sandbox_file_manager.py"
click node_exports "https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/notebook_service.py"
click node_pdf_toolchain "https://github.com/ama-cmfai/lambda/blob/main/scripts/install-system-deps.sh"

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_app,node_chat_ui,node_chat_controls,node_navigation,node_artifacts_ui,node_api_client toneBlue
class node_api_host,node_chat_api,node_workspace_apis,node_persistence toneAmber
class node_agent_service,node_llm_service,node_model_provider,node_execution_service,node_local_kernel,node_sandbox toneMint
class node_workspace_files,node_exports,node_pdf_toolchain toneRose
class node_start toneNeutral
```

## Components

### Browser client

- [React app shell](https://github.com/ama-cmfai/lambda/blob/main/frontend/src/App.tsx): React/Vite entry · `frontend/src/App.tsx`
- [Analysis chat workspace](https://github.com/ama-cmfai/lambda/blob/main/frontend/src/pages/Chat.tsx): page · Composes the primary conversational analysis experience. · `frontend/src/pages/Chat.tsx`
- [Prompt, files, and stream UI](https://github.com/ama-cmfai/lambda/blob/main/frontend/src/components/chat/ChatInput.tsx): chat components · `frontend/src/components/chat/ChatInput.tsx`
- [Conversation navigation](https://github.com/ama-cmfai/lambda/blob/main/frontend/src/components/layout/Sidebar.tsx): layout component · `frontend/src/components/layout/Sidebar.tsx`
- [Artifact panel](https://github.com/ama-cmfai/lambda/blob/main/frontend/src/components/layout/RightPanel.tsx): layout component · `frontend/src/components/layout/RightPanel.tsx`
- [API and export client](https://github.com/ama-cmfai/lambda/blob/main/frontend/src/services/api.ts): client boundary · Centralizes browser requests and export support. · `frontend/src/services/api.ts`

### FastAPI service

- [FastAPI host](https://github.com/ama-cmfai/lambda/blob/main/backend/main.py): application entry · Hosts versioned API routes. · `backend/main.py`
- [Chat API](https://github.com/ama-cmfai/lambda/blob/main/backend/app/api/v1/chat.py): v1 route · Drives agent chat requests. · `backend/app/api/v1/chat.py`
- [Conversation, file, execution, export APIs](https://github.com/ama-cmfai/lambda/blob/main/backend/app/api/v1/conversations.py): v1 routes · `backend/app/api/v1/conversations.py`
- [SQLite persistence](https://github.com/ama-cmfai/lambda/blob/main/backend/app/core/database.py): database layer · Database configuration and models for local runtime state. · `backend/app/core/database.py`

### Agent and execution

- [Agent control loop](https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/agent_service.py): orchestrator · Turns context, data, and intent into model-guided tool use. · `backend/app/services/agent_service.py`
- [LLM integration](https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/llm_service.py): model client · `backend/app/services/llm_service.py`
- [OpenAI-compatible provider](https://github.com/ama-cmfai/lambda/blob/main/backend/app/core/config.py): external dependency · Configured external /chat/completions endpoint. · `backend/app/core/config.py`
- [Stateful code execution](https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/code_execution_service.py): execution coordinator · Preserves a per-conversation analysis workspace across turns. · `backend/app/services/code_execution_service.py`
- [Local notebook runtime](https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/notebook_kernel.py): execution runtime · Default backend-machine execution path. · `backend/app/services/notebook_kernel.py`
- [Optional sandbox runtime](https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/opensandbox_service.py): sandbox integration · `backend/app/services/opensandbox_service.py`

### Workspace outputs

- [Workspace file manager](https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/sandbox_file_manager.py): artifact service · Manages uploaded and generated workspace files. · `backend/app/services/sandbox_file_manager.py`
- [Notebook and report exports](https://github.com/ama-cmfai/lambda/blob/main/backend/app/services/notebook_service.py): export pipeline · Builds notebook, report, PDF, and LaTeX deliverables. · `backend/app/services/notebook_service.py`
- [TeX/PDF toolchain](https://github.com/ama-cmfai/lambda/blob/main/scripts/install-system-deps.sh): system dependency · Supports PDF and Chinese report generation. · `scripts/install-system-deps.sh`

### Other components

- [Local launcher](https://github.com/ama-cmfai/lambda/blob/main/start.sh): startup script · Launches the browser client and API service. · `start.sh`

## Connections

- Local launcher → React app shell: launches
- Local launcher → FastAPI host: launches
- React app shell → Analysis chat workspace: renders
- Analysis chat workspace → Prompt, files, and stream UI: uses
- Analysis chat workspace → Conversation navigation: uses
- Analysis chat workspace → Artifact panel: shows
- Prompt, files, and stream UI → API and export client: submits prompts and files
- API and export client → FastAPI host: versioned HTTP API
- FastAPI host → Chat API: routes chat
- FastAPI host → Conversation, file, execution, export APIs: routes workspace operations
- Chat API → Agent control loop: invokes
- Conversation, file, execution, export APIs → SQLite persistence: reads and writes
- Agent control loop → SQLite persistence: loads conversation context
- Agent control loop → LLM integration: requests tool-guided responses
- LLM integration → OpenAI-compatible provider: chat completions
- Agent control loop → Stateful code execution: executes analysis tools
- Stateful code execution → Local notebook runtime: default runtime
- Stateful code execution → Optional sandbox runtime: alternate runtime
- Stateful code execution → Workspace file manager: reads and generates artifacts
- Conversation, file, execution, export APIs → Workspace file manager: manages workspace files
- Conversation, file, execution, export APIs → Notebook and report exports: generates deliverables
- Notebook and report exports → TeX/PDF toolchain: renders PDF/LaTeX
- Artifact panel → API and export client: retrieves artifacts
