# nehanasir987/contextflow architecture

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

- Interactive diagram: https://gitdiagram.com/nehanasir987/contextflow
- Repository: https://github.com/nehanasir987/contextflow

## Overview

LinkAI is a LinkedIn AI content studio backend. A client submits post details to the API, which starts a LangGraph workflow: draft generation, hashtag addition, evaluation, optional optimization and retry, human approval, then image generation. On resume, the pipeline saves the result and exposes job status/streaming and history. The application also provides user registration. The workflow and API paths are evidenced in excerpts; image-provider details and some agent internals were not sampled.

Main entry points: [`linkai/core/pipeline.py`](https://github.com/nehanasir987/contextflow/blob/main/linkai/core/pipeline.py) (LangGraph Pipeline), [`linkai/api/routes.py`](https://github.com/nehanasir987/contextflow/blob/main/linkai/api/routes.py) (HTTP and SSE Routes), [`linkai/db/session.py`](https://github.com/nehanasir987/contextflow/blob/main/linkai/db/session.py) (Database Sessions), [`linkai/app/server.py`](https://github.com/nehanasir987/contextflow/blob/main/linkai/app/server.py) (FastAPI Server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API and Application"]
  node_server["FastAPI Server<br/>[server.py]"]
  node_routes["HTTP and SSE Routes<br/>[routes.py]"]
  node_request_schemas["Request Schemas<br/>[schemas.py]"]
  node_config["Runtime Configuration<br/>[config.py]"]
end

subgraph group_content["Content Workflow"]
  node_pipeline["LangGraph Pipeline<br/>[pipeline.py]"]
  node_post_state["Post and Job State<br/>[state.py]"]
  node_writing["Draft and Optimization<br/>[nodes.py]"]
  node_hashtags["Hashtag Generation<br/>[nodes.py]"]
  node_evaluation["Post Evaluation<br/>[nodes.py]"]
  node_approval["Human Approval Gate<br/>[pipeline.py]"]
  node_image["Image Generation<br/>[image.py]"]
  node_prompts["Agent Prompts<br/>[prompts.py]"]
end

subgraph group_persistence["Persistence and Users"]
  node_db_session["Database Sessions<br/>[session.py]"]
  node_postgres[("PostgreSQL")]
  node_history[("Generated Post History<br/>[content_history.py]")]
  node_user_routes["User Registration<br/>[user_controller.py]"]
  node_user_model[("User Records<br/>[user.py]")]
end

subgraph group_integrations["External Services"]
  node_llms["Gemini Model Clients<br/>[llms.py]"]
  node_gemini(("Google Gemini"))
  node_image_service(("Image Provider"))
end

node_client(("API Client"))

node_client -->|"sends requests"| node_server
node_server -->|"mounts routes"| node_routes
node_server -->|"initializes database"| node_db_session
node_server -->|"loads settings"| node_config
node_client -->|"submits post"| node_routes
node_routes -->|"validates request"| node_request_schemas
node_routes -->|"starts generation"| node_pipeline
node_routes -->|"reads job snapshot"| node_post_state
node_pipeline -->|"resets and updates"| node_post_state
node_pipeline -->|"dispatches stages"| node_writing
node_pipeline -->|"dispatches stages"| node_hashtags
node_pipeline -->|"dispatches stages"| node_evaluation
node_pipeline -->|"interrupts for review"| node_approval
node_pipeline -->|"dispatches after review"| node_image
node_writing -->|"invokes models"| node_llms
node_hashtags -->|"invokes models"| node_llms
node_evaluation -->|"invokes evaluator"| node_llms
node_writing -->|"uses prompts"| node_prompts
node_hashtags -->|"uses prompts"| node_prompts
node_evaluation -->|"uses prompts"| node_prompts
node_llms -->|"calls model API"| node_gemini
node_pipeline -->|"opens session"| node_db_session
node_pipeline -->|"saves completed post"| node_history
node_db_session -->|"connects to"| node_postgres
node_db_session -->|"creates tables"| node_history
node_routes -->|"queries history"| node_db_session
node_routes -->|"reads recent posts"| node_history
node_routes -->|"delegates registration"| node_user_routes
node_user_routes -->|"queries and stores users"| node_user_model
node_user_routes -->|"accepts user schema"| node_request_schemas
node_user_routes -->|"uses token settings"| node_config
node_image -.-> node_image_service

click node_server "https://github.com/nehanasir987/contextflow/blob/main/linkai/app/server.py"
click node_routes "https://github.com/nehanasir987/contextflow/blob/main/linkai/api/routes.py"
click node_request_schemas "https://github.com/nehanasir987/contextflow/blob/main/linkai/models/schemas.py"
click node_pipeline "https://github.com/nehanasir987/contextflow/blob/main/linkai/core/pipeline.py"
click node_post_state "https://github.com/nehanasir987/contextflow/blob/main/linkai/core/state.py"
click node_writing "https://github.com/nehanasir987/contextflow/blob/main/linkai/agents/nodes.py"
click node_hashtags "https://github.com/nehanasir987/contextflow/blob/main/linkai/agents/nodes.py"
click node_evaluation "https://github.com/nehanasir987/contextflow/blob/main/linkai/agents/nodes.py"
click node_approval "https://github.com/nehanasir987/contextflow/blob/main/linkai/core/pipeline.py"
click node_image "https://github.com/nehanasir987/contextflow/blob/main/linkai/agents/image.py"
click node_prompts "https://github.com/nehanasir987/contextflow/blob/main/linkai/agents/prompts.py"
click node_llms "https://github.com/nehanasir987/contextflow/blob/main/linkai/agents/llms.py"
click node_db_session "https://github.com/nehanasir987/contextflow/blob/main/linkai/db/session.py"
click node_history "https://github.com/nehanasir987/contextflow/blob/main/linkai/models/content_history.py"
click node_user_routes "https://github.com/nehanasir987/contextflow/blob/main/linkai/controllers/user_controller.py"
click node_user_model "https://github.com/nehanasir987/contextflow/blob/main/linkai/models/user.py"
click node_config "https://github.com/nehanasir987/contextflow/blob/main/linkai/core/config.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_server,node_routes,node_request_schemas,node_config,node_client toneBlue
class node_pipeline,node_post_state,node_writing,node_hashtags,node_evaluation,node_approval,node_image,node_prompts toneAmber
class node_db_session,node_postgres,node_history,node_user_routes,node_user_model toneMint
class node_llms,node_gemini,node_image_service toneRose
```

## Components

### API and Application

- [FastAPI Server](https://github.com/nehanasir987/contextflow/blob/main/linkai/app/server.py): `linkai/app/server.py`
- [HTTP and SSE Routes](https://github.com/nehanasir987/contextflow/blob/main/linkai/api/routes.py): `linkai/api/routes.py`
- [Request Schemas](https://github.com/nehanasir987/contextflow/blob/main/linkai/models/schemas.py): `linkai/models/schemas.py`
- [Runtime Configuration](https://github.com/nehanasir987/contextflow/blob/main/linkai/core/config.py): `linkai/core/config.py`

### Content Workflow

- [LangGraph Pipeline](https://github.com/nehanasir987/contextflow/blob/main/linkai/core/pipeline.py): `linkai/core/pipeline.py`
- [Post and Job State](https://github.com/nehanasir987/contextflow/blob/main/linkai/core/state.py): `linkai/core/state.py`
- [Draft and Optimization](https://github.com/nehanasir987/contextflow/blob/main/linkai/agents/nodes.py): `linkai/agents/nodes.py`
- [Hashtag Generation](https://github.com/nehanasir987/contextflow/blob/main/linkai/agents/nodes.py): `linkai/agents/nodes.py`
- [Post Evaluation](https://github.com/nehanasir987/contextflow/blob/main/linkai/agents/nodes.py): `linkai/agents/nodes.py`
- [Human Approval Gate](https://github.com/nehanasir987/contextflow/blob/main/linkai/core/pipeline.py): `linkai/core/pipeline.py`
- [Image Generation](https://github.com/nehanasir987/contextflow/blob/main/linkai/agents/image.py): `linkai/agents/image.py`
- [Agent Prompts](https://github.com/nehanasir987/contextflow/blob/main/linkai/agents/prompts.py): `linkai/agents/prompts.py`

### Persistence and Users

- [Database Sessions](https://github.com/nehanasir987/contextflow/blob/main/linkai/db/session.py): `linkai/db/session.py`
- **PostgreSQL**
- [Generated Post History](https://github.com/nehanasir987/contextflow/blob/main/linkai/models/content_history.py): `linkai/models/content_history.py`
- [User Registration](https://github.com/nehanasir987/contextflow/blob/main/linkai/controllers/user_controller.py): `linkai/controllers/user_controller.py`
- [User Records](https://github.com/nehanasir987/contextflow/blob/main/linkai/models/user.py): `linkai/models/user.py`

### External Services

- [Gemini Model Clients](https://github.com/nehanasir987/contextflow/blob/main/linkai/agents/llms.py): `linkai/agents/llms.py`
- **Google Gemini**
- **Image Provider**

### Other components

- **API Client**

## Connections

- API Client → FastAPI Server: sends requests (evidence: `linkai/app/server.py`)
- FastAPI Server → HTTP and SSE Routes: mounts routes (evidence: `linkai/app/server.py`)
- FastAPI Server → Database Sessions: initializes database (evidence: `linkai/app/server.py`)
- FastAPI Server → Runtime Configuration: loads settings (evidence: `linkai/app/server.py`)
- API Client → HTTP and SSE Routes: submits post (evidence: `linkai/api/routes.py`)
- HTTP and SSE Routes → Request Schemas: validates request (evidence: `linkai/api/routes.py`)
- HTTP and SSE Routes → LangGraph Pipeline: starts generation (evidence: `linkai/api/routes.py`)
- HTTP and SSE Routes → Post and Job State: reads job snapshot (evidence: `linkai/api/routes.py`)
- LangGraph Pipeline → Post and Job State: resets and updates (evidence: `linkai/core/pipeline.py`)
- LangGraph Pipeline → Draft and Optimization: dispatches stages (evidence: `linkai/core/pipeline.py`)
- LangGraph Pipeline → Hashtag Generation: dispatches stages (evidence: `linkai/core/pipeline.py`)
- LangGraph Pipeline → Post Evaluation: dispatches stages (evidence: `linkai/core/pipeline.py`)
- LangGraph Pipeline → Human Approval Gate: interrupts for review (evidence: `linkai/core/pipeline.py`)
- LangGraph Pipeline → Image Generation: dispatches after review (evidence: `linkai/core/pipeline.py`)
- Draft and Optimization → Gemini Model Clients: invokes models (evidence: `linkai/agents/nodes.py`)
- Hashtag Generation → Gemini Model Clients: invokes models (evidence: `linkai/agents/nodes.py`)
- Post Evaluation → Gemini Model Clients: invokes evaluator (evidence: `linkai/agents/nodes.py`)
- Draft and Optimization → Agent Prompts: uses prompts (evidence: `linkai/agents/nodes.py`)
- Hashtag Generation → Agent Prompts: uses prompts (evidence: `linkai/agents/nodes.py`)
- Post Evaluation → Agent Prompts: uses prompts (evidence: `linkai/agents/nodes.py`)
- Gemini Model Clients → Google Gemini: calls model API (evidence: `linkai/agents/llms.py`)
- LangGraph Pipeline → Database Sessions: opens session (evidence: `linkai/core/pipeline.py`)
- LangGraph Pipeline → Generated Post History: saves completed post (evidence: `linkai/core/pipeline.py`)
- Database Sessions → PostgreSQL: connects to (evidence: `linkai/db/session.py`)
- Database Sessions → Generated Post History: creates tables (evidence: `linkai/db/session.py`)
- HTTP and SSE Routes → Database Sessions: queries history (evidence: `linkai/api/routes.py`)
- HTTP and SSE Routes → Generated Post History: reads recent posts (evidence: `linkai/api/routes.py`)
- HTTP and SSE Routes → User Registration: delegates registration (evidence: `linkai/api/routes.py`)
- User Registration → User Records: queries and stores users (evidence: `linkai/controllers/user_controller.py`)
- User Registration → Request Schemas: accepts user schema (evidence: `linkai/controllers/user_controller.py`)
- User Registration → Runtime Configuration: uses token settings (evidence: `linkai/controllers/user_controller.py`)
- Image Generation → Image Provider
