# usman-gen-ai/itu-rag-document-backend architecture

> Architecture diagram of the GitHub repository usman-gen-ai/itu-rag-document-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/usman-gen-ai/itu-rag-document-backend
- Repository: https://github.com/usman-gen-ai/itu-rag-document-backend

## Overview

Poocho AI is a serverless study-assistant backend. Its main workflow issues a user-scoped upload URL, stores documents in S3, syncs them into a Bedrock knowledge base, then answers filtered questions with citations. The graph also preserves document listing/deletion, retrieval debugging, and sync-status capabilities. Relationships come from the shown source excerpts or explicit README workflow; infrastructure wiring is documented rather than inspected in source.

Main entry points: [`src/app.js`](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/app.js) (Express API routes), [`src/syncWorker.js`](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/syncWorker.js) (Ingestion worker), [`src/handler.js`](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/handler.js) (Lambda HTTP handler), [`src/lib/cors.js`](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/lib/cors.js) (CORS middleware)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API handling"]
  node_lambda_handler["Lambda HTTP handler<br/>[handler.js]"]
  node_express_app["Express API routes<br/>[app.js]"]
  node_cors["CORS middleware<br/>[cors.js]"]
  node_validation["Input validation<br/>[validate.js]"]
  node_aws_clients["AWS clients<br/>[clients.js]"]
end

subgraph group_documents["Document lifecycle"]
  node_upload_flow["Upload URL and metadata<br/>[app.js]"]
  node_document_management["Document list and delete<br/>[app.js]"]
  node_source_bucket[("S3 source bucket")]
end

subgraph group_knowledge["Knowledge and chat"]
  node_bedrock_kb[("Bedrock knowledge base")]
  node_s3_vectors[("S3 Vectors index")]
  node_chat_flow["Grounded chat<br/>[app.js]"]
  node_retrieval_debug["Retrieval debug<br/>[app.js]"]
  node_chat_prompt["Chat prompts<br/>[prompt.js]"]
end

subgraph group_sync["Ingestion sync"]
  node_s3_event_queue["SQS sync queue"]
  node_sync_worker["Ingestion worker<br/>[syncWorker.js]"]
  node_ingestion_status["Sync status routes<br/>[app.js]"]
end

node_frontend(("Frontend"))
node_function_url{{"Lambda Function URL"}}

node_frontend -->|"sends requests"| node_function_url
node_function_url -->|"invokes"| node_lambda_handler
node_lambda_handler -->|"wraps"| node_express_app
node_lambda_handler -->|"uses headers"| node_cors
node_express_app -->|"installs"| node_cors
node_express_app -->|"validates inputs"| node_validation
node_express_app -->|"uses clients"| node_aws_clients
node_upload_flow -->|"writes metadata"| node_source_bucket
node_upload_flow -->|"returns PUT URL"| node_frontend
node_frontend -->|"uploads file"| node_source_bucket
node_document_management -->|"lists and deletes"| node_source_bucket
node_document_management -->|"checks documents"| node_bedrock_kb
node_document_management -->|"starts delete sync"| node_bedrock_kb
node_source_bucket -->|"sends upload events"| node_s3_event_queue
node_s3_event_queue -->|"triggers"| node_sync_worker
node_sync_worker -->|"uses clients"| node_aws_clients
node_sync_worker -->|"starts ingestion"| node_bedrock_kb
node_sync_worker -->|"backs off retries"| node_s3_event_queue
node_ingestion_status -->|"reads job status"| node_bedrock_kb
node_chat_flow -->|"retrieves and generates"| node_bedrock_kb
node_chat_flow -->|"configures prompts"| node_chat_prompt
node_chat_flow -->|"returns answer and citations"| node_frontend
node_retrieval_debug -->|"retrieves results"| node_bedrock_kb
node_bedrock_kb -->|"uses index"| node_s3_vectors

click node_lambda_handler "https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/handler.js"
click node_express_app "https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/app.js"
click node_cors "https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/lib/cors.js"
click node_validation "https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/lib/validate.js"
click node_aws_clients "https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/lib/clients.js"
click node_upload_flow "https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/app.js"
click node_document_management "https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/app.js"
click node_sync_worker "https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/syncWorker.js"
click node_ingestion_status "https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/app.js"
click node_chat_flow "https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/app.js"
click node_retrieval_debug "https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/app.js"
click node_chat_prompt "https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/lib/prompt.js"

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_lambda_handler,node_express_app,node_cors,node_validation,node_aws_clients,node_frontend toneBlue
class node_upload_flow,node_document_management,node_source_bucket toneAmber
class node_bedrock_kb,node_s3_vectors,node_chat_flow,node_retrieval_debug,node_chat_prompt toneMint
class node_s3_event_queue,node_sync_worker,node_ingestion_status toneRose
class node_function_url toneIndigo
```

## Components

### API handling

- [Lambda HTTP handler](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/handler.js): `src/handler.js`
- [Express API routes](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/app.js): `src/app.js`
- [CORS middleware](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/lib/cors.js): `src/lib/cors.js`
- [Input validation](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/lib/validate.js): `src/lib/validate.js`
- [AWS clients](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/lib/clients.js): `src/lib/clients.js`

### Document lifecycle

- [Upload URL and metadata](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/app.js): `src/app.js`
- [Document list and delete](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/app.js): `src/app.js`
- **S3 source bucket**

### Knowledge and chat

- **Bedrock knowledge base**
- **S3 Vectors index**
- [Grounded chat](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/app.js): `src/app.js`
- [Retrieval debug](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/app.js): `src/app.js`
- [Chat prompts](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/lib/prompt.js): `src/lib/prompt.js`

### Ingestion sync

- **SQS sync queue**
- [Ingestion worker](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/syncWorker.js): `src/syncWorker.js`
- [Sync status routes](https://github.com/usman-gen-ai/itu-rag-document-backend/blob/main/src/app.js): `src/app.js`

### Other components

- **Frontend**
- **Lambda Function URL**

## Connections

- Frontend → Lambda Function URL: sends requests (evidence: `README.md`)
- Lambda Function URL → Lambda HTTP handler: invokes (evidence: `README.md`)
- Lambda HTTP handler → Express API routes: wraps (evidence: `src/handler.js`)
- Lambda HTTP handler → CORS middleware: uses headers (evidence: `src/handler.js`)
- Express API routes → CORS middleware: installs (evidence: `src/app.js`)
- Express API routes → Input validation: validates inputs (evidence: `src/app.js`)
- Express API routes → AWS clients: uses clients (evidence: `src/app.js`)
- Upload URL and metadata → S3 source bucket: writes metadata (evidence: `src/app.js`)
- Upload URL and metadata → Frontend: returns PUT URL (evidence: `README.md`)
- Frontend → S3 source bucket: uploads file (evidence: `README.md`)
- Document list and delete → S3 source bucket: lists and deletes (evidence: `src/app.js`)
- Document list and delete → Bedrock knowledge base: checks documents (evidence: `src/app.js`)
- Document list and delete → Bedrock knowledge base: starts delete sync (evidence: `src/app.js`)
- S3 source bucket → SQS sync queue: sends upload events (evidence: `README.md`)
- SQS sync queue → Ingestion worker: triggers (evidence: `README.md`)
- Ingestion worker → AWS clients: uses clients (evidence: `src/syncWorker.js`)
- Ingestion worker → Bedrock knowledge base: starts ingestion (evidence: `src/syncWorker.js`)
- Ingestion worker → SQS sync queue: backs off retries (evidence: `src/syncWorker.js`)
- Sync status routes → Bedrock knowledge base: reads job status (evidence: `src/app.js`)
- Grounded chat → Bedrock knowledge base: retrieves and generates (evidence: `src/app.js`)
- Grounded chat → Chat prompts: configures prompts (evidence: `src/lib/prompt.js`)
- Grounded chat → Frontend: returns answer and citations (evidence: `README.md`)
- Retrieval debug → Bedrock knowledge base: retrieves results (evidence: `src/app.js`)
- Bedrock knowledge base → S3 Vectors index: uses index (evidence: `README.md`)
