How ayush-github123/scamsentinalai works
1. Top-level shape
- This repo is a Python/FastAPI backend for an “agentic honeypot” that receives suspicious messages, classifies them, extracts intelligence, and generates a persona-consistent reply.
- The codebase is split into an HTTP API layer, a core orchestration layer, analysis/extraction services, an LLM agent layer, and persistence/migration support.
2. HTTP ingress boundary
- app/main.py wires the FastAPI application.
- app/routes/messages.py is the main message-processing endpoint, while app/routes/whatsapp_webhook.py is the WhatsApp-facing ingress.
- app/routes/auth.py plus app/middleware.py enforce request authentication and cross-cutting request handling.
- app/schemas/message.py defines the request/response contract at the API boundary.
3. Core orchestration pipeline
- The main control flow appears to live in app/core/message_processor.py.
- That layer coordinates preprocessing, detection, extraction, state updates, and response selection rather than letting the route handlers contain business logic.
- app/core/response_decider.py and app/core/state_manager.py are the decision points that convert analysis results into the next conversational action.
4. Message analysis subsystem
- app/services/message_preprocessor.py normalizes incoming text before analysis.
- app/services/scam_detector.py performs rule/pattern-based scam classification and confidence scoring.
- app/services/message_scorer.py likely enriches or combines multiple signals into a final score.
- app/services/intelligence_extractor.py pulls structured entities such as URLs, phone numbers, payment identifiers, and other indicators from the message stream.
- These services are the main “analysis” boundary between raw user content and structured intelligence.
5. Conversation and session state
- app/core/session_context.py and app/core/state_manager.py hold the current conversation state, turn count, extracted artifacts, and decision context.
- The README’s state-machine behavior is reflected here: the system moves through suspicious/engaging/extracting/terminated-style phases based on confidence and collected intelligence.
- app/core/encryption.py suggests sensitive session material or stored artifacts can be protected before persistence or transport.
6. LLM agent layer
- app/agent/llm_client.py is the model gateway; it is responsible for calling the external LLM provider and handling fallback behavior.
- app/agent/prompts.py generates the dynamic system/user prompts that enforce persona, guardrails, and state-specific behavior.
- This layer is intentionally downstream of the rule-based detector/extractor: the LLM is used to craft responses, not to decide whether the message is suspicious.
7. Domain model and storage
- app/models/enhanced_models.py contains the persistence/domain entities used by the system.
- app/core/database.py connects the app to its database, and migrations/ with Alembic config show the schema is versioned rather than ad hoc.
- The migration history includes table initialization, quota tracking, and API-key removal, which indicates the database is used for operational state and auditability, not just ephemeral session data.
8. WhatsApp integration boundary
- app/services/whatsapp_listener.py and app/services/whatsapp_mcp_integration.py connect the system to WhatsApp-oriented message sources.
- app/services/mcp_agent_connector.py indicates an adapter layer for MCP-style integration.
- The nested wtsp-mcp project is a separate integration stack:
- whatsapp-bridge is a Go service,
- whatsapp-mcp-server is a Python server,
- together they act as an external bridge between WhatsApp transport and the agent backend.
- This is the clearest “edge adapter” in the repo.
9. Outbound reporting / external side effects
- The system is designed to emit extracted intelligence and conversation results to external endpoints once a session terminates or reaches a reporting condition.
- In architecture terms, that is a distinct outbound integration path separate from the user-facing response path.
- The repo’s webhook-oriented files and MCP integration imply the system both receives from and reports to external services.
10. Cross-cutting concerns
- app/logger.py, app/exceptions.py, and app/middleware.py provide observability, error handling, and request-level controls.
- app/core/config.py centralizes environment-driven settings such as API keys, model configuration, thresholds, and callback URLs.
- This means most runtime behavior is configured externally rather than hard-coded.
11. Runtime and deployment shape
- The primary runtime is Python/FastAPI, with Uvicorn as the ASGI server.
- The presence of requirements.txt, Alembic migrations, and REST test files suggests a service-oriented backend that can be exercised over HTTP without a separate frontend.
- The tests/ directory and tests/test.rest file indicate the repo is designed for API-level verification and manual endpoint testing.
12. How the data flows end-to-end
- Incoming message arrives through the API or WhatsApp webhook.
- It is authenticated, normalized, scored for scam likelihood, and scanned for structured intelligence.
- The session/state manager updates conversation context and decides the next conversational state.
- The prompt builder feeds the current state and missing intelligence into the LLM client.
- The generated reply is returned to the caller, while session data and extracted intelligence are stored or forwarded to external reporting endpoints when needed.
Main entry points: message_processor.py Processor · main.py App entry · messages.py Messages API · whatsapp_webhook.py WhatsApp hook
Components 22
HTTP API
FastAPI ingress, auth, middleware, and message/webhook routes.
- App entry · Creates and wires the ASGI application.
app/main.py - Messages API · Primary message-processing endpoint.
app/routes/messages.py - WhatsApp hook · Webhook ingress from WhatsApp-facing transport.
app/routes/whatsapp_webhook.py - Auth API · Authentication-related endpoint wiring.
app/routes/auth.py - Middleware · Cross-cutting request handling and enforcement.
app/middleware.py - Message schema · Request and response payload definitions.
app/schemas/message.py
Core Flow
Orchestration, state, decisioning, and shared runtime controls.
- Processor · Coordinates preprocessing, detection, extraction, state updates, and reply selection.
app/core/message_processor.py - Decider · Chooses the next conversational action.
app/core/response_decider.py - State manager · Tracks phase, turn context, and decision state.
app/core/state_manager.py - Session context · Holds per-conversation runtime context and artifacts.
app/core/session_context.py
Analysis
Preprocessing, scoring, scam detection, and intelligence extraction.
- Preprocessor · Normalizes incoming messages before analysis.
app/services/message_preprocessor.py - Scam detector · Rule and pattern-based suspiciousness scoring.
app/services/scam_detector.py - Scorer · Combines signals into a final score.
app/services/message_scorer.py - Extractor · Pulls URLs, phone numbers, payment IDs, and indicators.
app/services/intelligence_extractor.py
Agent & Integrations
LLM prompting and external transport/adapters.
- LLM client · Calls the external language model and handles fallback behavior.
app/agent/llm_client.py - Prompt builder · Builds persona and state-aware prompts.
app/agent/prompts.py - Listener · Listens for WhatsApp-oriented message sources.
app/services/whatsapp_listener.py - WhatsApp MCP · Connects WhatsApp flows to MCP-style integration.
app/services/whatsapp_mcp_integration.py - MCP connector · Adapter for MCP agent integration.
app/services/mcp_agent_connector.py
Persistence
Database models, migrations, and configuration/observability support.
- Config · Environment-driven thresholds, keys, and callbacks.
app/core/config.py - Database · Database connection and session wiring.
app/core/database.py - Domain models · Stored entities and operational records.
app/models/enhanced_models.py
Connections 26
- App entry → Middleware: wraps · Registers request middleware around the API.
- App entry → Messages API: mounts · Exposes the main message endpoint.
- App entry → WhatsApp hook: mounts · Exposes WhatsApp webhook ingress.
- App entry → Auth API: mounts · Exposes auth-related routes.
- Messages API → Message schema: uses · Validates request and response payloads.
- WhatsApp hook → Message schema: maps to · Converts webhook data into API message shapes.
- Auth API → Config: checks · Uses configured auth values and secrets.
- Messages API → Processor: delegates · Hands business logic to the orchestrator.
- WhatsApp hook → Listener: ingests · Receives inbound transport messages.
- Listener → Processor: forwards · Routes inbound content into the core pipeline.
- Processor → Preprocessor: normalizes · Cleans and standardizes incoming text.
- Processor → Scam detector: classifies · Evaluates suspiciousness.
- Processor → Scorer: scores · Combines scoring signals.
- Processor → Extractor: extracts · Pulls structured indicators from content.
- Processor → State manager: updates · Advances conversation phase and decision state.
- Processor → Decider: decides · Selects the next action based on analysis and state.
- Decider → Session context: reads · Uses current conversation context.
- State manager → Session context: persists · Stores and refreshes session context.
- State manager → Database: writes · Persists operational conversation state.
- Session context → Prompt builder: feeds · Provides state and missing intel to prompts.
- Prompt builder → LLM client: drives · Supplies the generated prompt for response creation.
- LLM client → Decider: returns reply · Supplies generated response candidates or fallback output.
- Processor → Domain models: stores · Records extracted artifacts and session metadata.
- Domain models → Database: persists · Stored through the database layer.
- Processor → WhatsApp MCP: reports · Sends terminal or reporting outcomes outward.
- WhatsApp MCP → MCP connector: adapts · Bridges to MCP agent integration.