How davidpal3c/carbonsage_agentic_ai works
Purpose and deployment shape
CarbonSage is a modular-monolith ESG decision agent for freight Scope 3 analysis. It combines workspace-isolated shipment data, supplier evidence, deterministic emissions/scenario tools, hybrid retrieval, and cited conversational answers. The deployable surfaces are a Next.js client and FastAPI API, locally composed with Docker; production targets Vercel, Render, and Neon PostgreSQL.
Paths: nzeroesg-client/app/page.tsx, nzeroesg-api/main.py, docker-compose.yml, render.yaml.
Client control plane
The Next.js/React client provides the public landing page, authenticated workspace dashboard, routed operational pages, and the agent interaction/structured-answer presentation. Dashboard state is shared across route transitions; artifact viewing and workspace-scoped data are client boundaries rather than being embedded in domain logic.
Paths: nzeroesg-client/app/dashboard/layout.tsx, nzeroesg-client/app/dashboard/WorkspaceShell.tsx, nzeroesg-client/app/components/chat_ui/ChatInterface.tsx.
API composition and workspace boundary
FastAPI is the backend entry point and exposes separate HTTP route modules for workspace/session lifecycle, uploads/artifacts, shipments, evidence, scenarios, reports, and conversations. Workspace principals and sessions establish isolation; persistence repositories enforce workspace-scoped access and bounded retained state.
Paths: nzeroesg-api/main.py, nzeroesg-api/api/workspaces.py, nzeroesg-api/domain/workspaces/sessions.py, nzeroesg-api/persistence/workspaces.py.
Ingestion and artifact lifecycle
User or demo inputs enter as shipment files and evidence documents. Domain ingestion normalizes records and preserves recoverable source/citation identity; the artifact catalog tracks provenance, downloadable objects, rename, and soft deletion. Object bytes are abstracted behind artifact storage, allowing AWS S3-backed storage without coupling APIs to S3.
Paths: nzeroesg-api/domain/shipments/ingestion.py, nzeroesg-api/domain/evidence/ingestion.py, nzeroesg-api/services/artifact_storage.py, nzeroesg-api/integrations/aws_s3.py.
Deterministic decision domain
Freight calculations remain deterministic domain code: distances, modes, units, emission factors, and calculation provenance produce the trusted metrics. Shipment analysis derives hotspots and breakdowns; scenario comparison evaluates alternatives. The agent may request these capabilities but does not own their numerical results.
Paths: nzeroesg-api/domain/emissions/calculator.py, nzeroesg-api/domain/shipments/analysis.py, nzeroesg-api/domain/scenarios/comparison.py.
Evidence retrieval and safety gate
Evidence is chunked and stored with lexical-search data and optional embeddings. Retrieval supports lexical, semantic pgvector, and deterministic hybrid modes, always filtered by workspace; lexical retrieval remains usable without an embedding provider. A typed evidence assessment gate prevents unsupported retrieved passages from becoming rendered or persisted citations.
Paths: nzeroesg-api/domain/evidence/retrieval.py, nzeroesg-api/domain/evidence/embeddings.py, nzeroesg-api/domain/evidence/evaluation.py, nzeroesg-api/persistence/migrations/004_pgvector_retrieval.sql.
Agent orchestration and external AI boundary
The conversation API invokes a typed runtime composed from policy-constrained tools, a planner, and an optional LLM adapter. Tool calls, usage, citations, and answers are persisted separately. The assistant fails closed without compatible OpenRouter or OpenAI configuration; deterministic tools, artifacts, and retrieval remain independently available. LangChain is optional orchestration, not the source of truth.
Paths: nzeroesg-api/api/agent.py, nzeroesg-api/services/agent_runtime.py, nzeroesg-api/services/agent_tools.py, nzeroesg-api/agent/policy/v1.md.
Data infrastructure and evaluation
Neon PostgreSQL is the system of record for workspace data, normalized shipments, evidence, full-text search, pgvector embeddings, artifacts, and agent runtime records. Schema evolution is explicit in SQL migrations. Checked-in retrieval and answer-support datasets/reports provide regression evaluation independently of live provider credentials.
Paths: nzeroesg-api/persistence/migrations/001_workspaces.sql, nzeroesg-api/persistence/migrations/007_typed_agent_runtime.sql, nzeroesg-api/evaluation/retrieval_cases.json.
Main entry points: main.py FastAPI API · agent_runtime.py Typed agent runtime · retrieval.py Hybrid evidence retrieval · agent_tools.py Agent tools
Components 21
Client
- Next.js client · Landing, dashboard, routed operations, and answer presentation.
nzeroesg-client/app/page.tsx - Workspace shell · Shares dashboard state across route transitions.
nzeroesg-client/app/dashboard/WorkspaceShell.tsx - Chat interface
nzeroesg-client/app/components/chat_ui/ChatInterface.tsx
API & workspace
- FastAPI API · Composes workspace, artifact, shipment, evidence, scenario, report, and conversation routes.
nzeroesg-api/main.py - Workspace sessions · Establishes workspace principals and sessions.
nzeroesg-api/domain/workspaces/sessions.py - Workspace repositories · Enforces workspace-scoped access and retained-state bounds.
nzeroesg-api/persistence/workspaces.py
Decision domain
- Shipment & evidence ingestion · Normalizes inputs while preserving source and citation identity.
nzeroesg-api/domain/shipments/ingestion.py - Artifact storage · Manages downloadable artifact bytes independently of S3.
nzeroesg-api/services/artifact_storage.py - Emissions calculator · Produces trusted metrics and calculation provenance.
nzeroesg-api/domain/emissions/calculator.py - Shipment analysis · Derives hotspots and breakdowns.
nzeroesg-api/domain/shipments/analysis.py - Scenario comparison · Evaluates freight alternatives.
nzeroesg-api/domain/scenarios/comparison.py
Agent & retrieval
- Conversation API
nzeroesg-api/api/agent.py - Typed agent runtime · Combines policy-constrained tools, planning, and an optional LLM adapter.
nzeroesg-api/services/agent_runtime.py - Agent tools · Exposes deterministic calculations, artifacts, and retrieval to the agent.
nzeroesg-api/services/agent_tools.py - Hybrid evidence retrieval · Uses lexical, optional semantic, and deterministic hybrid search.
nzeroesg-api/domain/evidence/retrieval.py - Evidence assessment gate · Blocks unsupported passages from rendered or persisted citations.
nzeroesg-api/domain/evidence/evaluation.py - OpenRouter / OpenAI · Required for compatible conversational generation; unavailable configuration fails closed.
Data infrastructure
- Neon PostgreSQL · Stores workspace, shipment, evidence, artifact, search, embedding, and agent records.
nzeroesg-api/persistence/migrations/001_workspaces.sql - pgvector retrieval schema · Adds embedding-backed semantic retrieval.
nzeroesg-api/persistence/migrations/004_pgvector_retrieval.sql - AWS S3
nzeroesg-api/integrations/aws_s3.py - Regression evaluation · Validates retrieval and answer support without live provider credentials.
nzeroesg-api/evaluation/retrieval_cases.json
Connections 26
- Next.js client → Workspace shell: hosts dashboard
- Workspace shell → Chat interface: shares workspace state
- Next.js client → FastAPI API: HTTP requests
- Chat interface → Conversation API: conversations
- FastAPI API → Workspace sessions: authenticates
- FastAPI API → Workspace repositories: workspace-scoped data
- Workspace repositories → Neon PostgreSQL: persists
- FastAPI API → Shipment & evidence ingestion: uploads inputs
- Shipment & evidence ingestion → Neon PostgreSQL: normalized records
- FastAPI API → Artifact storage: artifact lifecycle
- Artifact storage → AWS S3: object bytes
- FastAPI API → Emissions calculator: metrics requests
- Emissions calculator → Shipment analysis: trusted metrics
- FastAPI API → Scenario comparison: scenario requests
- Conversation API → Typed agent runtime: invokes
- Typed agent runtime → Agent tools: planned tool calls
- Typed agent runtime → OpenRouter / OpenAI: optional generation
- Agent tools → Emissions calculator: calculation capability
- Agent tools → Scenario comparison: scenario capability
- Agent tools → Hybrid evidence retrieval: retrieval capability
- Hybrid evidence retrieval → pgvector retrieval schema: optional semantic search
- Hybrid evidence retrieval → Neon PostgreSQL: lexical search and chunks
- Hybrid evidence retrieval → Evidence assessment gate: candidate passages
- Evidence assessment gate → Typed agent runtime: validated citations
- Typed agent runtime → Neon PostgreSQL: answers, citations, tool calls, usage
- Regression evaluation → Hybrid evidence retrieval: retrieval regression cases