How ama-cmfai/lambda works
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: Chat.tsx Analysis chat workspace · conversations.py Conversation, file, execution, export APIs · agent_service.py Agent control loop · code_execution_service.py Stateful code execution
Components 20
Browser client
- React app shell
frontend/src/App.tsx - Analysis chat workspace · Composes the primary conversational analysis experience.
frontend/src/pages/Chat.tsx - Prompt, files, and stream UI
frontend/src/components/chat/ChatInput.tsx - Conversation navigation
frontend/src/components/layout/Sidebar.tsx - Artifact panel
frontend/src/components/layout/RightPanel.tsx - API and export client · Centralizes browser requests and export support.
frontend/src/services/api.ts
FastAPI service
- FastAPI host · Hosts versioned API routes.
backend/main.py - Chat API · Drives agent chat requests.
backend/app/api/v1/chat.py - Conversation, file, execution, export APIs
backend/app/api/v1/conversations.py - SQLite persistence · Database configuration and models for local runtime state.
backend/app/core/database.py
Agent and execution
- Agent control loop · Turns context, data, and intent into model-guided tool use.
backend/app/services/agent_service.py - LLM integration
backend/app/services/llm_service.py - OpenAI-compatible provider · Configured external /chat/completions endpoint.
backend/app/core/config.py - Stateful code execution · Preserves a per-conversation analysis workspace across turns.
backend/app/services/code_execution_service.py - Local notebook runtime · Default backend-machine execution path.
backend/app/services/notebook_kernel.py - Optional sandbox runtime
backend/app/services/opensandbox_service.py
Workspace outputs
- Workspace file manager · Manages uploaded and generated workspace files.
backend/app/services/sandbox_file_manager.py - Notebook and report exports · Builds notebook, report, PDF, and LaTeX deliverables.
backend/app/services/notebook_service.py - TeX/PDF toolchain · Supports PDF and Chinese report generation.
scripts/install-system-deps.sh
Other components
- Local launcher · Launches the browser client and API service.
start.sh
Connections 23
- 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