How kailasss2k05/ghost_operator_core works
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: ws.py Chat WS · executor.py Executor · llm_service.py LLM service · registry.py Tool registry
Components 25
Node bridge
WhatsApp-facing transport layer and per-sender delivery control.
- WhatsApp client · Authenticates to WhatsApp and receives/sends messages.
node-app/src/whatsapp/client.js - Queueing · Keeps each sender ordered and isolated for delivery.
node-app/src/websocket/queueManager.js - WS manager · Maintains reconnectable WebSocket links to the Python backend.
node-app/src/websocket/wsManager.js - Message handler · Receives WhatsApp messages and forwards them into the backend flow.
node-app/src/handlers/messageHandler.js - Formatter · 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 · Backend startup and route wiring.
app/main.py - Chat WS · Primary `/ws/{sender}` conversational endpoint.
app/api/ws.py - Conn manager · Tracks backend websocket sessions by sender.
app/core/websocket_manager.py - LLM service · Structured tool selection and response generation using Groq-hosted models.
app/services/llm_service.py - Memory · Sender-keyed conversation memory stored only in process memory.
app/services/memory_service.py - FuncLink client · Creates guided-session handoff URLs and receives completion callbacks.
app/services/funclink_service.py - Tool registry · Registers tool definitions and dispatches async tool runners.
app/tool_registry/registry.py - Executor · Runs selected tools safely through the registry.
app/tool_registry/executor.py - Loader · Loads tool modules and self-registration hooks.
app/tool_registry/loader.py - Browser tools · Playwright-backed automation for Amazon, LinkedIn, and related browser flows.
app/tool_registry/tools/amazon_search.py - API tools · Scraping and API-backed tools for housing, Practo, and similar data fetches.
app/tool_registry/tools/housing_listings.py - IRCTC live · Real-time IRCTC data path when API credentials are available.
app/services/irctc_live_service.py - IRCTC browser · 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 · Persistent guided-session service for browser workflows.
funclink/app/api/generate.py - Session WS · WebSocket channel for session steps and updates.
funclink/app/api/websocket.py - Session DB · 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 · Hosted model used for routing and response synthesis.
- WhatsApp · End-user messaging channel.
- Web targets · Amazon, LinkedIn, housing, Practo, Hindu, and other web destinations.
- IRCTC API · RapidAPI-backed IRCTC live service.
Connections 27
- 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.