# kailasss2k05/ghost_operator_core architecture

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

- Interactive diagram: https://gitdiagram.com/kailasss2k05/ghost_operator_core
- Repository: https://github.com/kailasss2k05/ghost_operator_core

## Overview

1. **System shape**
   - This repo is a multi-process chatbot automation system with three boundaries: a **Node.js WhatsApp bridge** (`node-app/`), a **Python FastAPI intelligence/backend layer** (`app/`), and a separate **FuncLink service** (`funclink/`) for guided browser-session workflows.

2. **Primary user path**
   - End users only interact through **WhatsApp**.
   - WhatsApp messages enter the Node bridge, are forwarded over a **per-sender WebSocket** to the Python backend, processed there, then sent back to WhatsApp as formatted replies.

3. **Node.js bridge responsibilities**
   - `node-app/` is intentionally thin: it authenticates to WhatsApp via `whatsapp-web.js`, maintains the session locally, and handles message delivery.
   - It also adds **per-sender queueing** and **reconnectable WebSocket connections** so one user’s conversation stays ordered and isolated from others.

4. **Python backend responsibilities**
   - `app/` contains the actual conversation logic, routing, browser automation, and data fetching.
   - Its main conversational endpoint is `/ws/{sender}`, which receives chat text, consults memory, asks the LLM whether a tool is needed, executes the tool, and generates the final reply.

5. **LLM-driven routing layer**
   - The backend uses **Groq-hosted Llama models** through `services/llm_service.py`.
   - The `/ws/{sender}` flow is two-stage: first it selects a tool from the registry with a structured JSON decision, then it turns tool output into a WhatsApp-friendly response.
   - This makes the LLM the orchestration layer, not just a text generator.

6. **Tool plugin architecture**
   - `app/tool_registry/` is a lightweight plugin system.
   - Tool modules self-register definitions and async runners, and the executor dispatches them safely.
   - This is the main extension point for adding new capabilities without changing the router shape.

7. **Tool execution boundaries**
   - Tools split into two categories:
     - **Browser automation tools** using **Playwright** for Amazon, LinkedIn, and some IRCTC flows.
     - **Scraping/API tools** using `httpx`, `requests`, or external APIs for housing, Practo, Hindu news, and live IRCTC data.
   - This separation matters because browser-based tools are heavier, stateful, and sometimes Windows-specific.

8. **Interactive WebSocket endpoints**
   - Besides the main chat endpoint, the backend exposes specialized WebSocket routes like `/ws/housing`, `/ws/hindu/news`, `/ws/irctc`, `/ws/olx`, and `/ws/practo`.
   - These are step-based sessions for structured searches, where the backend collects parameters in stages and returns staged results.

9. **State and persistence**
   - Conversation memory in `app/services/memory_service.py` is **in-memory only**, keyed by sender, so chat history is lost on restart.
   - In contrast, the `funclink/` subproject has its own **database-backed session model** and Alembic migrations, so guided-session state is persistent there.

10. **FuncLink integration path**
    - `app/services/funclink_service.py` calls the FuncLink service to generate a guided browser session URL for tasks that should be handed off to a human-assisted browser flow.
    - The backend also exposes a webhook endpoint so FuncLink can notify completion and the backend can push a follow-up message back to the user’s active WhatsApp session.

11. **IRCTC is a dual-path subsystem**
    - IRCTC support has two data paths: a **RapidAPI live service** when credentials are present, and a **Playwright/browser fallback** when live API access is unavailable.
    - This is a resilience boundary: the system prefers real-time API data but can degrade to scraping/public web flows.

12. **Windows/browser runtime constraints**
    - Playwright automation is treated as a special runtime concern, especially on Windows.
    - The backend includes event-loop handling around Playwright so browser subprocesses can run correctly without blocking the main async server.

13. **Formatting and delivery**
    - The Node bridge reformats backend replies into WhatsApp-safe chunks and cleans up callouts/lists for readability.
    - That means the final presentation layer is separated from the backend reasoning layer.

14. **What an architecture diagram should emphasize**
    - Show WhatsApp as the entry point, Node as the transport bridge, Python as the orchestration core, the tool registry as the execution hub, and external systems as leaf services: Groq, Playwright-driven websites, IRCTC RapidAPI, FuncLink, and RSS/scraping targets.

Main entry points: [`app/api/ws.py`](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/api/ws.py) (Chat WS), [`app/tool_registry/executor.py`](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/tool_registry/executor.py) (Executor), [`app/services/llm_service.py`](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/services/llm_service.py) (LLM service), [`app/tool_registry/registry.py`](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/tool_registry/registry.py) (Tool registry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_node["Node bridge"]
  node_whatsapp_client["WhatsApp client<br/>wa client<br/>[client.js]"]
  node_queue_manager["Queueing<br/>sender queue<br/>[queueManager.js]"]
  node_ws_manager_node["WS manager<br/>bridge ws<br/>[wsManager.js]"]
  node_message_handler["Message handler<br/>bridge flow<br/>[messageHandler.js]"]
  node_message_formatter["Formatter<br/>wa formatting"]
end

subgraph group_group_backend["Python backend"]
  node_backend_main["App entry<br/>fastapi app<br/>[main.py]"]
  node_ws_chat["Chat WS<br/>[ws.py]"]
  node_websocket_manager["Conn manager<br/>session ws"]
  node_llm_service["LLM service<br/>groq llm<br/>[llm_service.py]"]
  node_memory_service[("Memory<br/>[memory_service.py]")]
  node_funclink_service["FuncLink client<br/>handoff client"]
  node_tool_registry["Tool registry<br/>plugin hub<br/>[registry.py]"]
  node_tool_executor["Executor<br/>tool dispatch<br/>[executor.py]"]
  node_tool_loader["Loader<br/>tool discovery<br/>[loader.py]"]
  node_browser_tools["Browser tools<br/>playwright tools<br/>[amazon_search.py]"]
  node_api_tools["API tools<br/>data tools"]
  node_irctc_live["IRCTC live<br/>rapidapi service"]
  node_irctc_browser["IRCTC browser<br/>playwright fallback"]
end

subgraph group_group_funclink["FuncLink service"]
  node_funclink_api["FuncLink API<br/>guided sessions<br/>[generate.py]"]
  node_funclink_ws["Session WS<br/>guided ws<br/>[websocket.py]"]
  node_funclink_db[("Session DB<br/>persistent storage<br/>[database.py]")]
end

subgraph group_group_external["External systems"]
  node_groq_llm(("Groq Llama<br/>external llm"))
  node_whatsapp_network(("WhatsApp<br/>external channel"))
  node_browser_sites(("Web targets<br/>browser targets"))
  node_irctc_api(("IRCTC API<br/>external api"))
end

node_whatsapp_network -->|"messages"| node_whatsapp_client
node_whatsapp_client -->|"order"| node_queue_manager
node_queue_manager -->|"deliver"| node_message_handler
node_message_handler -->|"forward"| node_ws_manager_node
node_ws_manager_node -->|"chat"| node_ws_chat
node_backend_main -->|"routes"| node_ws_chat
node_ws_chat -->|"track"| node_websocket_manager
node_ws_chat -->|"context"| node_memory_service
node_ws_chat -->|"decide"| node_llm_service
node_llm_service -->|"invoke"| node_groq_llm
node_llm_service -->|"select tool"| node_tool_registry
node_tool_registry -->|"load"| node_tool_loader
node_tool_registry -->|"dispatch"| node_tool_executor
node_tool_executor -->|"browser"| node_browser_tools
node_tool_executor -->|"fetch"| node_api_tools
node_tool_executor -->|"prefer live"| node_irctc_live
node_tool_executor -->|"fallback"| node_irctc_browser
node_browser_tools -->|"automate"| node_browser_sites
node_api_tools -->|"scrape"| node_browser_sites
node_irctc_live -->|"query"| node_irctc_api
node_funclink_service -->|"handoff"| node_funclink_api
node_funclink_api -->|"update"| node_funclink_ws
node_funclink_api -->|"persist"| node_funclink_db
node_funclink_ws -->|"state"| node_funclink_db
node_funclink_service -->|"callback"| node_ws_chat
node_llm_service -->|"draft reply"| node_message_formatter
node_message_formatter -->|"send"| node_whatsapp_client

click node_whatsapp_client "https://github.com/kailasss2k05/ghost_operator_core/blob/main/node-app/src/whatsapp/client.js"
click node_queue_manager "https://github.com/kailasss2k05/ghost_operator_core/blob/main/node-app/src/websocket/queueManager.js"
click node_ws_manager_node "https://github.com/kailasss2k05/ghost_operator_core/blob/main/node-app/src/websocket/wsManager.js"
click node_message_handler "https://github.com/kailasss2k05/ghost_operator_core/blob/main/node-app/src/handlers/messageHandler.js"
click node_message_formatter "https://github.com/kailasss2k05/ghost_operator_core/blob/main/node-app/src/whatsapp/messageFormatter.js"
click node_backend_main "https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/main.py"
click node_ws_chat "https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/api/ws.py"
click node_websocket_manager "https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/core/websocket_manager.py"
click node_llm_service "https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/services/llm_service.py"
click node_memory_service "https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/services/memory_service.py"
click node_funclink_service "https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/services/funclink_service.py"
click node_tool_registry "https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/tool_registry/registry.py"
click node_tool_executor "https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/tool_registry/executor.py"
click node_tool_loader "https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/tool_registry/loader.py"
click node_browser_tools "https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/tool_registry/tools/amazon_search.py"
click node_api_tools "https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/tool_registry/tools/housing_listings.py"
click node_irctc_live "https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/services/irctc_live_service.py"
click node_irctc_browser "https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/services/irctc_browser_service.py"
click node_funclink_api "https://github.com/kailasss2k05/ghost_operator_core/blob/main/funclink/app/api/generate.py"
click node_funclink_ws "https://github.com/kailasss2k05/ghost_operator_core/blob/main/funclink/app/api/websocket.py"
click node_funclink_db "https://github.com/kailasss2k05/ghost_operator_core/blob/main/funclink/app/core/database.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_whatsapp_client,node_queue_manager,node_ws_manager_node,node_message_handler,node_message_formatter toneBlue
class node_backend_main,node_ws_chat,node_websocket_manager,node_llm_service,node_memory_service,node_funclink_service,node_tool_registry,node_tool_executor,node_tool_loader,node_browser_tools,node_api_tools,node_irctc_live,node_irctc_browser toneAmber
class node_funclink_api,node_funclink_ws,node_funclink_db toneMint
class node_groq_llm,node_whatsapp_network,node_browser_sites,node_irctc_api toneRose
```

## Components

### Node bridge

WhatsApp-facing transport layer and per-sender delivery control.

- [WhatsApp client](https://github.com/kailasss2k05/ghost_operator_core/blob/main/node-app/src/whatsapp/client.js): wa client · Authenticates to WhatsApp and receives/sends messages. · `node-app/src/whatsapp/client.js`
- [Queueing](https://github.com/kailasss2k05/ghost_operator_core/blob/main/node-app/src/websocket/queueManager.js): sender queue · Keeps each sender ordered and isolated for delivery. · `node-app/src/websocket/queueManager.js`
- [WS manager](https://github.com/kailasss2k05/ghost_operator_core/blob/main/node-app/src/websocket/wsManager.js): bridge ws · Maintains reconnectable WebSocket links to the Python backend. · `node-app/src/websocket/wsManager.js`
- [Message handler](https://github.com/kailasss2k05/ghost_operator_core/blob/main/node-app/src/handlers/messageHandler.js): bridge flow · Receives WhatsApp messages and forwards them into the backend flow. · `node-app/src/handlers/messageHandler.js`
- [Formatter](https://github.com/kailasss2k05/ghost_operator_core/blob/main/node-app/src/whatsapp/messageFormatter.js): wa formatting · Cleans and chunks backend replies into WhatsApp-safe output. · `node-app/src/whatsapp/messageFormatter.js`

### Python backend

FastAPI orchestration core for chat routing, tools, and responses.

- [App entry](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/main.py): fastapi app · Backend startup and route wiring. · `app/main.py`
- [Chat WS](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/api/ws.py): chat ws · Primary `/ws/{sender}` conversational endpoint. · `app/api/ws.py`
- [Conn manager](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/core/websocket_manager.py): session ws · Tracks backend websocket sessions by sender. · `app/core/websocket_manager.py`
- [LLM service](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/services/llm_service.py): groq llm · Structured tool selection and response generation using Groq-hosted models. · `app/services/llm_service.py`
- [Memory](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/services/memory_service.py): in-memory state · Sender-keyed conversation memory stored only in process memory. · `app/services/memory_service.py`
- [FuncLink client](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/services/funclink_service.py): handoff client · Creates guided-session handoff URLs and receives completion callbacks. · `app/services/funclink_service.py`
- [Tool registry](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/tool_registry/registry.py): plugin hub · Registers tool definitions and dispatches async tool runners. · `app/tool_registry/registry.py`
- [Executor](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/tool_registry/executor.py): tool dispatch · Runs selected tools safely through the registry. · `app/tool_registry/executor.py`
- [Loader](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/tool_registry/loader.py): tool discovery · Loads tool modules and self-registration hooks. · `app/tool_registry/loader.py`
- [Browser tools](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/tool_registry/tools/amazon_search.py): playwright tools · Playwright-backed automation for Amazon, LinkedIn, and related browser flows. · `app/tool_registry/tools/amazon_search.py`
- [API tools](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/tool_registry/tools/housing_listings.py): data tools · Scraping and API-backed tools for housing, Practo, and similar data fetches. · `app/tool_registry/tools/housing_listings.py`
- [IRCTC live](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/services/irctc_live_service.py): rapidapi service · Real-time IRCTC data path when API credentials are available. · `app/services/irctc_live_service.py`
- [IRCTC browser](https://github.com/kailasss2k05/ghost_operator_core/blob/main/app/services/irctc_browser_service.py): playwright fallback · Browser fallback for IRCTC when live API access is unavailable. · `app/services/irctc_browser_service.py`

### FuncLink service

Separate guided-session service with persistent state.

- [FuncLink API](https://github.com/kailasss2k05/ghost_operator_core/blob/main/funclink/app/api/generate.py): guided sessions · Persistent guided-session service for browser workflows. · `funclink/app/api/generate.py`
- [Session WS](https://github.com/kailasss2k05/ghost_operator_core/blob/main/funclink/app/api/websocket.py): guided ws · WebSocket channel for session steps and updates. · `funclink/app/api/websocket.py`
- [Session DB](https://github.com/kailasss2k05/ghost_operator_core/blob/main/funclink/app/core/database.py): persistent storage · Database-backed sessions, steps, task memory, and migrations. · `funclink/app/core/database.py`

### External systems

LLM, browser targets, APIs, and message channel outside the repo.

- **Groq Llama**: external llm · Hosted model used for routing and response synthesis.
- **WhatsApp**: external channel · End-user messaging channel.
- **Web targets**: browser targets · Amazon, LinkedIn, housing, Practo, Hindu, and other web destinations.
- **IRCTC API**: external api · RapidAPI-backed IRCTC live service.

## Connections

- WhatsApp → WhatsApp client: messages · User chat enters through WhatsApp.
- WhatsApp client → Queueing: order · Per-sender queueing preserves conversation order.
- Queueing → Message handler: deliver · Queued messages are handed to the bridge handler.
- Message handler → WS manager: forward · Bridge opens or reuses sender-specific websocket links.
- WS manager → Chat WS: chat · Sends chat text to the Python `/ws/{sender}` endpoint.
- App entry → Chat WS: routes · App entry wires conversational websocket routes.
- Chat WS → Conn manager: track · Backend tracks active sender sessions.
- Chat WS → Memory: context · Conversation history is loaded and updated per sender.
- Chat WS → LLM service: decide · LLM first chooses whether a tool is needed.
- LLM service → Groq Llama: invoke · Groq-hosted models handle routing and synthesis.
- LLM service → Tool registry: select tool · Structured decision maps to a registry tool.
- Tool registry → Loader: load · Tool modules self-register through loader discovery.
- Tool registry → Executor: dispatch · Executor runs selected tool implementations.
- Executor → Browser tools: browser · Playwright-backed tools handle browser automation.
- Executor → API tools: fetch · Scraping and API tools gather structured data.
- Executor → IRCTC live: prefer live · IRCTC uses live API when available.
- Executor → IRCTC browser: fallback · IRCTC can degrade to browser-based access.
- Browser tools → Web targets: automate · Browser tools operate on external websites.
- API tools → Web targets: scrape · Data tools query or scrape external targets.
- IRCTC live → IRCTC API: query · Live IRCTC data comes from RapidAPI.
- FuncLink client → FuncLink API: handoff · Backend requests guided-session URLs from FuncLink.
- FuncLink API → Session WS: update · FuncLink sessions progress over its websocket channel.
- FuncLink API → Session DB: persist · FuncLink persists sessions and step state.
- Session WS → Session DB: state · Websocket steps read and update durable session data.
- FuncLink client → Chat WS: callback · Completion webhooks can trigger a follow-up user reply.
- LLM service → Formatter: draft reply · Final model output is prepared for WhatsApp delivery.
- Formatter → WhatsApp client: send · Formatted chunks are delivered back to WhatsApp.
