# aprizal543/palm-agronomy architecture

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

- Interactive diagram: https://gitdiagram.com/aprizal543/palm-agronomy
- Repository: https://github.com/aprizal543/palm-agronomy

## Overview

PalmAgronomy is a FastAPI backend for farm/block GIS, production tracking, and agronomy assistance through Telegram and HTTP APIs. The main workflow is a Telegram update entering the webhook, being handled by the agent and its allow-listed tools, then producing a response or confirmed write; HTTP routes also expose farm, block, health, and knowledge operations. PostgreSQL/PostGIS is the system of record, with repositories handling persisted domain data. The README documents broader monitoring, mapping validation, and polling capabilities; links for unsampled internals are omitted where implementation ownership cannot be verified.

Main entry points: [`app/agent/tools.py`](https://github.com/aprizal543/palm-agronomy/blob/main/app/agent/tools.py) (Allow-listed tools), [`app/api/routes`](https://github.com/aprizal543/palm-agronomy/tree/main/app/api/routes) (API routes), [`app/db/session.py`](https://github.com/aprizal543/palm-agronomy/blob/main/app/db/session.py) (Async DB sessions), [`app/services/telegram_agent.py`](https://github.com/aprizal543/palm-agronomy/blob/main/app/services/telegram_agent.py) (Telegram agent)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["API and Telegram"]
  node_app["FastAPI application<br/>[main.py]"]
  node_routes["API routes"]
  node_webhook["Telegram webhook"]
  node_adapter["Telegram adapter<br/>[telegram.py]"]
end

subgraph group_agent["Conversation and tools"]
  node_agent_service["Telegram agent<br/>[telegram_agent.py]"]
  node_conversation["Conversation router"]
  node_tools["Allow-listed tools<br/>[tools.py]"]
  node_prod_insight["Production insight"]
  node_prod_monitor["Production monitoring"]
end

subgraph group_farm["Farm and GIS"]
  node_farm_services["Farm and block services"]
  node_gis["GIS operations<br/>[gis.py]"]
end

subgraph group_knowledge["Agronomy knowledge"]
  node_knowledge_api["Knowledge API<br/>[knowledge.py]"]
  node_rag["RAG processing<br/>[rag.py]"]
  node_knowledge_repo["Knowledge retrieval<br/>[knowledge.py]"]
end

subgraph group_persistence["Persistence"]
  node_db_session["Async DB sessions<br/>[session.py]"]
  node_models["Domain models"]
  node_telegram_state["Conversation and audit state<br/>[telegram.py]"]
  node_farm_repo["Farm and block repositories"]
  node_production_repo["Production repository<br/>[production.py]"]
  node_knowledge_store[("PostgreSQL knowledge store<br/>[knowledge.py]")]
end

node_user(("Farmer or operator"))
node_telegram(("Telegram"))
node_postgres[("PostgreSQL / PostGIS")]

node_user -->|"sends updates"| node_telegram
node_telegram -->|"posts updates"| node_webhook
node_app -->|"registers routes"| node_routes
node_app -->|"registers webhook"| node_webhook
node_webhook -->|"handles updates"| node_adapter
node_routes -->|"accepts Telegram input"| node_agent_service
node_routes -->|"serves farm APIs"| node_farm_services
node_routes -->|"serves search"| node_knowledge_api
node_agent_service -.->|"routes messages"| node_conversation
node_agent_service -->|"dispatches tools"| node_tools
node_tools -.->|"gets analysis"| node_prod_insight
node_tools -.->|"gets monitoring"| node_prod_monitor
node_tools -->|"resolves blocks"| node_farm_repo
node_tools -->|"retrieves knowledge"| node_knowledge_repo
node_knowledge_api -->|"builds answers"| node_rag
node_knowledge_api -->|"searches chunks"| node_knowledge_repo
node_knowledge_repo -->|"reads and logs"| node_knowledge_store
node_farm_services -->|"validates geometry"| node_gis
node_farm_services -.->|"reads and writes"| node_farm_repo
node_production_repo -->|"reads and writes records"| node_postgres
node_farm_repo -.->|"reads and writes farms"| node_postgres
node_db_session -->|"opens sessions"| node_postgres
node_models -->|"maps database tables"| node_db_session
node_telegram_state -->|"maps chat state"| node_db_session
node_knowledge_store -.->|"uses ORM base"| node_db_session

click node_app "https://github.com/aprizal543/palm-agronomy/blob/main/app/main.py"
click node_routes "https://github.com/aprizal543/palm-agronomy/tree/main/app/api/routes"
click node_webhook "https://github.com/aprizal543/palm-agronomy/blob/main/app/services/telegram_webhook.py"
click node_adapter "https://github.com/aprizal543/palm-agronomy/blob/main/app/integrations/telegram.py"
click node_agent_service "https://github.com/aprizal543/palm-agronomy/blob/main/app/services/telegram_agent.py"
click node_conversation "https://github.com/aprizal543/palm-agronomy/blob/main/app/services/conversation_router.py"
click node_tools "https://github.com/aprizal543/palm-agronomy/blob/main/app/agent/tools.py"
click node_prod_insight "https://github.com/aprizal543/palm-agronomy/blob/main/app/services/production_insight.py"
click node_prod_monitor "https://github.com/aprizal543/palm-agronomy/blob/main/app/services/production_monitoring.py"
click node_farm_services "https://github.com/aprizal543/palm-agronomy/tree/main/app/services"
click node_gis "https://github.com/aprizal543/palm-agronomy/blob/main/app/services/gis.py"
click node_knowledge_api "https://github.com/aprizal543/palm-agronomy/blob/main/app/api/routes/knowledge.py"
click node_rag "https://github.com/aprizal543/palm-agronomy/blob/main/app/services/rag.py"
click node_knowledge_repo "https://github.com/aprizal543/palm-agronomy/blob/main/app/repositories/knowledge.py"
click node_db_session "https://github.com/aprizal543/palm-agronomy/blob/main/app/db/session.py"
click node_models "https://github.com/aprizal543/palm-agronomy/tree/main/app/models"
click node_telegram_state "https://github.com/aprizal543/palm-agronomy/blob/main/app/models/telegram.py"
click node_farm_repo "https://github.com/aprizal543/palm-agronomy/tree/main/app/repositories"
click node_production_repo "https://github.com/aprizal543/palm-agronomy/blob/main/app/repositories/production.py"
click node_knowledge_store "https://github.com/aprizal543/palm-agronomy/blob/main/app/models/knowledge.py"

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_routes,node_webhook,node_adapter toneBlue
class node_agent_service,node_conversation,node_tools,node_prod_insight,node_prod_monitor,node_postgres toneAmber
class node_farm_services,node_gis toneMint
class node_knowledge_api,node_rag,node_knowledge_repo toneRose
class node_db_session,node_models,node_telegram_state,node_farm_repo,node_production_repo,node_knowledge_store,node_user,node_telegram toneIndigo
```

## Components

### API and Telegram

- [FastAPI application](https://github.com/aprizal543/palm-agronomy/blob/main/app/main.py): `app/main.py`
- [API routes](https://github.com/aprizal543/palm-agronomy/tree/main/app/api/routes): `app/api/routes`
- [Telegram webhook](https://github.com/aprizal543/palm-agronomy/blob/main/app/services/telegram_webhook.py): `app/services/telegram_webhook.py`
- [Telegram adapter](https://github.com/aprizal543/palm-agronomy/blob/main/app/integrations/telegram.py): `app/integrations/telegram.py`

### Conversation and tools

- [Telegram agent](https://github.com/aprizal543/palm-agronomy/blob/main/app/services/telegram_agent.py): `app/services/telegram_agent.py`
- [Conversation router](https://github.com/aprizal543/palm-agronomy/blob/main/app/services/conversation_router.py): `app/services/conversation_router.py`
- [Allow-listed tools](https://github.com/aprizal543/palm-agronomy/blob/main/app/agent/tools.py): `app/agent/tools.py`
- [Production insight](https://github.com/aprizal543/palm-agronomy/blob/main/app/services/production_insight.py): `app/services/production_insight.py`
- [Production monitoring](https://github.com/aprizal543/palm-agronomy/blob/main/app/services/production_monitoring.py): `app/services/production_monitoring.py`

### Farm and GIS

- [Farm and block services](https://github.com/aprizal543/palm-agronomy/tree/main/app/services): `app/services`
- [GIS operations](https://github.com/aprizal543/palm-agronomy/blob/main/app/services/gis.py): `app/services/gis.py`

### Agronomy knowledge

- [Knowledge API](https://github.com/aprizal543/palm-agronomy/blob/main/app/api/routes/knowledge.py): `app/api/routes/knowledge.py`
- [RAG processing](https://github.com/aprizal543/palm-agronomy/blob/main/app/services/rag.py): `app/services/rag.py`
- [Knowledge retrieval](https://github.com/aprizal543/palm-agronomy/blob/main/app/repositories/knowledge.py): `app/repositories/knowledge.py`

### Persistence

- [Async DB sessions](https://github.com/aprizal543/palm-agronomy/blob/main/app/db/session.py): `app/db/session.py`
- [Domain models](https://github.com/aprizal543/palm-agronomy/tree/main/app/models): `app/models`
- [Conversation and audit state](https://github.com/aprizal543/palm-agronomy/blob/main/app/models/telegram.py): `app/models/telegram.py`
- [Farm and block repositories](https://github.com/aprizal543/palm-agronomy/tree/main/app/repositories): `app/repositories`
- [Production repository](https://github.com/aprizal543/palm-agronomy/blob/main/app/repositories/production.py): `app/repositories/production.py`
- [PostgreSQL knowledge store](https://github.com/aprizal543/palm-agronomy/blob/main/app/models/knowledge.py): `app/models/knowledge.py`

### Other components

- **Farmer or operator**
- **Telegram**
- **PostgreSQL / PostGIS**

## Connections

- Farmer or operator → Telegram: sends updates
- Telegram → Telegram webhook: posts updates
- FastAPI application → API routes: registers routes (evidence: `app/main.py`)
- FastAPI application → Telegram webhook: registers webhook (evidence: `app/main.py`)
- Telegram webhook → Telegram adapter: handles updates (evidence: `app/services/telegram_webhook.py`)
- API routes → Telegram agent: accepts Telegram input (evidence: `app/api/routes/telegram.py`)
- API routes → Farm and block services: serves farm APIs (evidence: `app/api/routes/blocks.py`)
- API routes → Knowledge API: serves search (evidence: `app/api/router.py`)
- Telegram agent → Conversation router: routes messages
- Telegram agent → Allow-listed tools: dispatches tools (evidence: `app/agent/tools.py`)
- Allow-listed tools → Production insight: gets analysis
- Allow-listed tools → Production monitoring: gets monitoring
- Allow-listed tools → Farm and block repositories: resolves blocks (evidence: `app/agent/tools.py`)
- Allow-listed tools → Knowledge retrieval: retrieves knowledge (evidence: `app/agent/tools.py`)
- Knowledge API → RAG processing: builds answers (evidence: `app/api/routes/knowledge.py`)
- Knowledge API → Knowledge retrieval: searches chunks (evidence: `app/api/routes/knowledge.py`)
- Knowledge retrieval → PostgreSQL knowledge store: reads and logs (evidence: `app/repositories/knowledge.py`)
- Farm and block services → GIS operations: validates geometry (evidence: `app/api/routes/blocks.py`)
- Farm and block services → Farm and block repositories: reads and writes (evidence: `app/repositories/knowledge.py`)
- Production repository → PostgreSQL / PostGIS: reads and writes records (evidence: `app/repositories/production.py`)
- Farm and block repositories → PostgreSQL / PostGIS: reads and writes farms
- Async DB sessions → PostgreSQL / PostGIS: opens sessions (evidence: `app/db/session.py`)
- Domain models → Async DB sessions: maps database tables (evidence: `app/models/block.py`)
- Conversation and audit state → Async DB sessions: maps chat state (evidence: `app/models/telegram.py`)
- PostgreSQL knowledge store → Async DB sessions: uses ORM base
