# bug1857/agriroute-ai architecture

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

- Interactive diagram: https://gitdiagram.com/bug1857/agriroute-ai
- Repository: https://github.com/bug1857/agriroute-ai

## Overview

AgriRoute AI is a prototype for harvest planning, with separate synthetic-demo, live-assisted, and synthetic-Mandi workspaces. The principal workflow is coordinator sign-in and lot planning, human review and approval, then delivery of an approved snapshot to the assigned farmer. The graph also preserves buyer-offer review, what-if analysis, and the optional Gemini agent. Source excerpts establish several API-to-library calls; README statements support the documented user workflows and provider boundaries. Some UI-to-API wiring is not shown, so it is omitted.

Main entry points: [`app/api/coordinator/route.ts`](https://github.com/bug1857/agriroute-ai/blob/main/app/api/coordinator/route.ts) (Coordinator workflow API), [`lib/coordinator.ts`](https://github.com/bug1857/agriroute-ai/blob/main/lib/coordinator.ts) (Auth and approvals), [`lib/mandi.ts`](https://github.com/bug1857/agriroute-ai/blob/main/lib/mandi.ts) (Mandi analysis), [`app/api/live/route.ts`](https://github.com/bug1857/agriroute-ai/blob/main/app/api/live/route.ts) (Live evidence API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_coordination["Coordinator and farmer"]
  node_coordinator_ui["Coordinator workspace"]
  node_farmer_view["Farmer decision<br/>[FarmerDecision.tsx]"]
  node_coord_api["Coordinator workflow API<br/>[route.ts]"]
  node_workflow["Auth and approvals<br/>[coordinator.ts]"]
  node_farmer_record["Farmer snapshot<br/>[farmer-record.ts]"]
end

subgraph group_planning["Planning and analysis"]
  node_planner["Lot planner<br/>[planner.ts]"]
  node_world["Demo world data<br/>[world.ts]"]
  node_explorer["Demo explorer<br/>[Explorer.tsx]"]
  node_whatif_ui["What-if analysis<br/>[WhatIfAnalysis.tsx]"]
  node_whatif_api["What-if API<br/>[route.ts]"]
  node_whatif["Sensitivity analysis<br/>[whatif.ts]"]
end

subgraph group_live["Live assisted"]
  node_live_ui["Live workspace<br/>[LiveWorkspace.tsx]"]
  node_live_api["Live evidence API<br/>[route.ts]"]
  node_live_logic["Live assessment<br/>[live.ts]"]
  node_providers["Weather and routing<br/>[live-providers.ts]"]
end

subgraph group_mandi["Synthetic Mandi"]
  node_mandi_ui["Mandi workspace<br/>[MandiWorkspace.tsx]"]
  node_mandi_logic["Mandi analysis<br/>[mandi.ts]"]
  node_mandi_data["Synthetic prices<br/>[mandi-data.ts]"]
  node_offer_review["Buyer offer review"]
  node_offer_api["Offer review API<br/>[route.ts]"]
  node_offers["Offer parsing<br/>[offers.ts]"]
end

subgraph group_services["Persistence and agent"]
  node_agent_api["Agent API<br/>[route.ts]"]
  node_agent["Planning agent<br/>[agent.ts]"]
  node_gemini["Gemini adapter<br/>[gemini-server.ts]"]
  node_agent_store[("Replay records<br/>[agent-store.ts]")]
  node_database[("PostgreSQL<br/>[database.ts]")]
end

node_coordinator(("Coordinator"))
node_farmer(("Farmer"))

node_coordinator -->|"plans lots"| node_coordinator_ui
node_coordinator_ui -.->|"submits workflow"| node_coord_api
node_coord_api -->|"authenticates and routes"| node_workflow
node_coord_api -->|"plans in transaction"| node_database
node_coord_api -->|"plans lot"| node_mandi_logic
node_coord_api -->|"reads prices"| node_mandi_data
node_workflow -->|"reads and writes"| node_database
node_workflow -->|"validates offers"| node_offers
node_workflow -->|"validates plans"| node_mandi_logic
node_workflow -.->|"formats snapshot"| node_farmer_record
node_farmer -->|"views decision"| node_farmer_view
node_explorer -->|"plans scenarios"| node_planner
node_explorer -->|"uses world data"| node_world
node_whatif_api -->|"analyzes scenario"| node_whatif
node_whatif_api -->|"reads world"| node_world
node_whatif_ui -.->|"requests analysis"| node_whatif_api
node_live_ui -.->|"requests evidence"| node_live_api
node_live_api -->|"recalculates workflow"| node_live_logic
node_live_api -->|"fetches evidence"| node_providers
node_mandi_ui -->|"uses analysis"| node_mandi_logic
node_mandi_ui -->|"uses dataset"| node_mandi_data
node_offer_review -->|"submits review"| node_offer_api
node_offer_api -.->|"parses offer"| node_offers
node_agent_api -->|"invokes agent"| node_gemini
node_gemini -->|"runs function calls"| node_agent
node_gemini -->|"records traces"| node_agent_store
node_mandi_logic -->|"uses calculations"| node_planner
node_live_logic -->|"uses risk and storage"| node_planner

click node_coordinator_ui "https://github.com/bug1857/agriroute-ai/blob/main/components/CoordinatorWorkspace.tsx"
click node_farmer_view "https://github.com/bug1857/agriroute-ai/blob/main/components/FarmerDecision.tsx"
click node_coord_api "https://github.com/bug1857/agriroute-ai/blob/main/app/api/coordinator/route.ts"
click node_workflow "https://github.com/bug1857/agriroute-ai/blob/main/lib/coordinator.ts"
click node_farmer_record "https://github.com/bug1857/agriroute-ai/blob/main/lib/farmer-record.ts"
click node_planner "https://github.com/bug1857/agriroute-ai/blob/main/lib/planner.ts"
click node_world "https://github.com/bug1857/agriroute-ai/blob/main/lib/world.ts"
click node_explorer "https://github.com/bug1857/agriroute-ai/blob/main/components/Explorer.tsx"
click node_whatif_ui "https://github.com/bug1857/agriroute-ai/blob/main/components/WhatIfAnalysis.tsx"
click node_whatif_api "https://github.com/bug1857/agriroute-ai/blob/main/app/api/whatif/route.ts"
click node_whatif "https://github.com/bug1857/agriroute-ai/blob/main/lib/whatif.ts"
click node_live_ui "https://github.com/bug1857/agriroute-ai/blob/main/components/LiveWorkspace.tsx"
click node_live_api "https://github.com/bug1857/agriroute-ai/blob/main/app/api/live/route.ts"
click node_live_logic "https://github.com/bug1857/agriroute-ai/blob/main/lib/live.ts"
click node_providers "https://github.com/bug1857/agriroute-ai/blob/main/lib/live-providers.ts"
click node_mandi_ui "https://github.com/bug1857/agriroute-ai/blob/main/components/MandiWorkspace.tsx"
click node_mandi_logic "https://github.com/bug1857/agriroute-ai/blob/main/lib/mandi.ts"
click node_mandi_data "https://github.com/bug1857/agriroute-ai/blob/main/lib/mandi-data.ts"
click node_offer_review "https://github.com/bug1857/agriroute-ai/blob/main/components/BuyerOfferReview.tsx"
click node_offer_api "https://github.com/bug1857/agriroute-ai/blob/main/app/api/offers/route.ts"
click node_offers "https://github.com/bug1857/agriroute-ai/blob/main/lib/offers.ts"
click node_agent_api "https://github.com/bug1857/agriroute-ai/blob/main/app/api/agent/route.ts"
click node_agent "https://github.com/bug1857/agriroute-ai/blob/main/lib/agent.ts"
click node_gemini "https://github.com/bug1857/agriroute-ai/blob/main/lib/gemini-server.ts"
click node_agent_store "https://github.com/bug1857/agriroute-ai/blob/main/lib/agent-store.ts"
click node_database "https://github.com/bug1857/agriroute-ai/blob/main/lib/database.ts"

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_coordinator_ui,node_farmer_view,node_coord_api,node_workflow,node_farmer_record toneBlue
class node_planner,node_world,node_explorer,node_whatif_ui,node_whatif_api,node_whatif toneAmber
class node_live_ui,node_live_api,node_live_logic,node_providers toneMint
class node_mandi_ui,node_mandi_logic,node_mandi_data,node_offer_review,node_offer_api,node_offers toneRose
class node_agent_api,node_agent,node_gemini,node_agent_store,node_database,node_coordinator,node_farmer toneIndigo
```

## Components

### Coordinator and farmer

- [Coordinator workspace](https://github.com/bug1857/agriroute-ai/blob/main/components/CoordinatorWorkspace.tsx): `components/CoordinatorWorkspace.tsx`
- [Farmer decision](https://github.com/bug1857/agriroute-ai/blob/main/components/FarmerDecision.tsx): `components/FarmerDecision.tsx`
- [Coordinator workflow API](https://github.com/bug1857/agriroute-ai/blob/main/app/api/coordinator/route.ts): `app/api/coordinator/route.ts`
- [Auth and approvals](https://github.com/bug1857/agriroute-ai/blob/main/lib/coordinator.ts): `lib/coordinator.ts`
- [Farmer snapshot](https://github.com/bug1857/agriroute-ai/blob/main/lib/farmer-record.ts): `lib/farmer-record.ts`

### Planning and analysis

- [Lot planner](https://github.com/bug1857/agriroute-ai/blob/main/lib/planner.ts): `lib/planner.ts`
- [Demo world data](https://github.com/bug1857/agriroute-ai/blob/main/lib/world.ts): `lib/world.ts`
- [Demo explorer](https://github.com/bug1857/agriroute-ai/blob/main/components/Explorer.tsx): `components/Explorer.tsx`
- [What-if analysis](https://github.com/bug1857/agriroute-ai/blob/main/components/WhatIfAnalysis.tsx): `components/WhatIfAnalysis.tsx`
- [What-if API](https://github.com/bug1857/agriroute-ai/blob/main/app/api/whatif/route.ts): `app/api/whatif/route.ts`
- [Sensitivity analysis](https://github.com/bug1857/agriroute-ai/blob/main/lib/whatif.ts): `lib/whatif.ts`

### Live assisted

- [Live workspace](https://github.com/bug1857/agriroute-ai/blob/main/components/LiveWorkspace.tsx): `components/LiveWorkspace.tsx`
- [Live evidence API](https://github.com/bug1857/agriroute-ai/blob/main/app/api/live/route.ts): `app/api/live/route.ts`
- [Live assessment](https://github.com/bug1857/agriroute-ai/blob/main/lib/live.ts): `lib/live.ts`
- [Weather and routing](https://github.com/bug1857/agriroute-ai/blob/main/lib/live-providers.ts): `lib/live-providers.ts`

### Synthetic Mandi

- [Mandi workspace](https://github.com/bug1857/agriroute-ai/blob/main/components/MandiWorkspace.tsx): `components/MandiWorkspace.tsx`
- [Mandi analysis](https://github.com/bug1857/agriroute-ai/blob/main/lib/mandi.ts): `lib/mandi.ts`
- [Synthetic prices](https://github.com/bug1857/agriroute-ai/blob/main/lib/mandi-data.ts): `lib/mandi-data.ts`
- [Buyer offer review](https://github.com/bug1857/agriroute-ai/blob/main/components/BuyerOfferReview.tsx): `components/BuyerOfferReview.tsx`
- [Offer review API](https://github.com/bug1857/agriroute-ai/blob/main/app/api/offers/route.ts): `app/api/offers/route.ts`
- [Offer parsing](https://github.com/bug1857/agriroute-ai/blob/main/lib/offers.ts): `lib/offers.ts`

### Persistence and agent

- [Agent API](https://github.com/bug1857/agriroute-ai/blob/main/app/api/agent/route.ts): `app/api/agent/route.ts`
- [Planning agent](https://github.com/bug1857/agriroute-ai/blob/main/lib/agent.ts): `lib/agent.ts`
- [Gemini adapter](https://github.com/bug1857/agriroute-ai/blob/main/lib/gemini-server.ts): `lib/gemini-server.ts`
- [Replay records](https://github.com/bug1857/agriroute-ai/blob/main/lib/agent-store.ts): `lib/agent-store.ts`
- [PostgreSQL](https://github.com/bug1857/agriroute-ai/blob/main/lib/database.ts): `lib/database.ts`

### Other components

- **Coordinator**
- **Farmer**

## Connections

- Coordinator → Coordinator workspace: plans lots (evidence: `README.md`)
- Coordinator workspace → Coordinator workflow API: submits workflow
- Coordinator workflow API → Auth and approvals: authenticates and routes (evidence: `app/api/coordinator/route.ts`)
- Coordinator workflow API → PostgreSQL: plans in transaction (evidence: `app/api/coordinator/route.ts`)
- Coordinator workflow API → Mandi analysis: plans lot (evidence: `app/api/coordinator/route.ts`)
- Coordinator workflow API → Synthetic prices: reads prices (evidence: `app/api/coordinator/route.ts`)
- Auth and approvals → PostgreSQL: reads and writes (evidence: `lib/coordinator.ts`)
- Auth and approvals → Offer parsing: validates offers (evidence: `lib/coordinator.ts`)
- Auth and approvals → Mandi analysis: validates plans (evidence: `lib/coordinator.ts`)
- Auth and approvals → Farmer snapshot: formats snapshot
- Farmer → Farmer decision: views decision (evidence: `README.md`)
- Demo explorer → Lot planner: plans scenarios (evidence: `components/Explorer.tsx`)
- Demo explorer → Demo world data: uses world data (evidence: `components/Explorer.tsx`)
- What-if API → Sensitivity analysis: analyzes scenario (evidence: `app/api/whatif/route.ts`)
- What-if API → Demo world data: reads world (evidence: `app/api/whatif/route.ts`)
- What-if analysis → What-if API: requests analysis
- Live workspace → Live evidence API: requests evidence
- Live evidence API → Live assessment: recalculates workflow (evidence: `app/api/live/route.ts`)
- Live evidence API → Weather and routing: fetches evidence (evidence: `app/api/live/route.ts`)
- Mandi workspace → Mandi analysis: uses analysis (evidence: `components/MandiWorkspace.tsx`)
- Mandi workspace → Synthetic prices: uses dataset (evidence: `components/MandiWorkspace.tsx`)
- Buyer offer review → Offer review API: submits review (evidence: `components/BuyerOfferReview.tsx`)
- Offer review API → Offer parsing: parses offer
- Agent API → Gemini adapter: invokes agent (evidence: `app/api/agent/route.ts`)
- Gemini adapter → Planning agent: runs function calls (evidence: `lib/gemini-server.ts`)
- Gemini adapter → Replay records: records traces (evidence: `lib/gemini-server.ts`)
- Mandi analysis → Lot planner: uses calculations (evidence: `lib/mandi.ts`)
- Live assessment → Lot planner: uses risk and storage (evidence: `lib/live.ts`)
