Skip to content

About

AI automation tools for companies to automate their entire architecture and data labelling

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

ACE — Autonomous Commerce Engine

"Your business runs itself while you sleep."

ACE Technologies Limited · Nigerian C-Corp · Delaware flip option post-Series A


The 30-Second Pitch

ACE is an invisible operating system for informal commerce. We turn WhatsApp chaos into autonomous businesses through radical AI automation. Merchants keep texting customers on WhatsApp — our AI handles inventory, payments, negotiations, logistics, and customer retention completely in the background.

No new apps for customers. No manual work for merchants. Just autonomous growth.


The Problem

In Nigeria and across emerging markets, $2.3 trillion in commerce happens entirely on WhatsApp. ~480,000 merchants in Lagos alone manage 30–150 customer conversations daily through unstructured text, voice notes in Pidgin, and screenshots of bank transfers. These merchants are growing 40% YoY but hit a hard ceiling at $50K annual revenue — they can't scale past their personal bandwidth.

Current "Solution" Why It Fails
WhatsApp Business App Zero state management, no payment integration, no inventory tracking
Respond.io / Zoko / Hilos Routes chats to human agents — still requires merchants to check bank apps, call riders, update inventory manually
Shopify / WooCommerce Requires customers to leave WhatsApp → 78% conversion drop in emerging markets
Traditional ERP (Zoho, Odoo) Desktop-oriented, requires structured data input — alien to merchants who think in conversations

The core insight: The merchant's current system isn't broken — it's optimised for n=1. WhatsApp works perfectly for one customer. It catastrophically fails at 50 concurrent conversations. The merchant doesn't need a better inbox. They need an autonomous operating system that executes business logic while they sleep.


The Paradigm Shift

❌ What We're NOT Building ✅ What We ARE Building
An AI chatbot that drafts responses for merchants to review An autonomous OS that executes
A fancy CRM with "AI-powered insights" An event-driven engine with no manual steps
Another unified inbox with message routing Invisible back-office infrastructure
A tool requiring customers to change behaviour Invisible layer on existing WhatsApp behaviour

The AI doesn't ask permission. It executes complex multi-step business logic and reports outcomes. The merchant's job shifts from operational execution to strategic exception management.


System Workings & Architecture

1. End-to-End Negotiation Flow Across the ACE Architecture

sequenceDiagram
    autonumber
    actor Customer as Customer (WhatsApp)
    participant Comms as Comms Router (5s Debounce)
    participant Agent as AI Negotiator (Groq Llama 3.3)
    participant Arc as Negotiation Arc (Redis/PG)
    participant Pricing as Pricing Service & Postgres
    participant StateMachine as Order State Machine (Pure Reducer)
    participant Outbound as Outbound Dispatcher

    Customer->>Comms: "How much for the black sneakers size 43? Can you do 12k?"
    Note over Comms: Sliding Debounce Window (5s per customer buffer)
    Comms->>Agent: Dispatches ConversationTurn (messages + orderState)

    Agent->>Arc: loadOrCreateArc(customerId, merchantId)
    Note over Agent: Model Tool Iteration Loop (Max 8 iterations)
    Agent->>Pricing: tool: check_inventory("black sneakers") & get_customer_profile()
    Pricing-->>Agent: SKU: SNK-BLK-43 | Stock: 10 | Base: ₦18k | Floor: ₦14.5k | Tier: loyal
    Agent->>Arc: projectRangeOntoArc(anchor: 18000, floor: 14500)

    Agent->>Arc: availableTactics(arc, stock)
    Arc-->>Agent: [relationship_anchor, bundle_pivot, scarcity_signal]

    rect rgb(240, 248, 255)
        Note over Agent: AI Strategic Action & Arc Advance
        Agent->>Arc: tool: deploy_tactic(relationship_anchor) + propose_price(15000)
        Arc-->>Agent: advanceArc(AGENT_COUNTERED, offer: 15000) (Attempt 1/3)
    end

    Agent->>Outbound: Split into natural chat bubbles (Typing indicator -> Chunks)
    Outbound-->>Customer: "Ah my loyal customer! Regular price is ₦18k, but for you I fit drop am to ₦15k last."

    Customer->>Comms: "Oya ₦15k is fine, I take am"
    Comms->>Agent: Dispatches customer acceptance turn

    rect rgb(240, 255, 240)
        Note over Agent: Deal Closure & Two-Stage Order Transition
        Agent->>Arc: advanceArc(DEAL_ACCEPTED, finalPrice: 15000)
        Agent->>StateMachine: tool: close_deal(15000) -> transition(QUOTE_CREATED) -> draft
        Agent->>StateMachine: tool: issue_payment_link() -> transition(PAYMENT_LINK_ISSUED) -> awaiting_payment
        Note over StateMachine: Mint dynamic VAN via Paystack DVA / Bank Transfer (Providus/Wema)
        Agent->>Outbound: Send WhatsApp checkout card
        Outbound-->>Customer: "Transfer ₦15,000 to Account: 9023481234 (Wema/Providus). Expires in 15 mins."
    end
Loading

2. End-to-End Multimodal Pipeline & Execution Flow

flowchart TD
    subgraph Inbound["1. Inbound Channels & Normalization"]
        direction TB
        C_WA[Customer WhatsApp] -->|Text / Audio / Photo| GW{Gateway Layer}
        GW -->|Baileys WebSockets :3005| BG[baileys-gateway]
        GW -->|Meta Graph Webhook :3001| IS[ingestion-service]
        
        BG -->|Voice Note / Audio| W1[Groq Whisper Turbo\nFast Audio STT in mediaProcessor]
        BG -->|Photo / Image| P1[Resolve Media Buffer\nBase64 Encoding]
        IS -->|Meta Cloud Webhook| P2[HMAC SHA-256 Verify\nRedis Idempotency Dedup]
        
        W1 --> NORM[Normalized InboundMessage]
        P1 --> NORM
        P2 --> NORM
    end

    subgraph Buffering["2. Sliding Debounce & Conversation Turn"]
        NORM --> DB1[comms-router: debounce.ts]
        DB1 -->|5s Sliding Window Timer| REDIS[(Redis Scratch Buffer)]
        REDIS -->|Window Closes| TURN[Atomic ConversationTurn\nMessages + Existing OrderState]
    end

    subgraph Agent["3. AI Negotiator Loop (agentLoop.ts)"]
        TURN --> AG1[Hydrate NegotiationArc & Dialect Tone]
        AG1 --> AG2[Extract Spoken/Typed Counter-Offer\nRegex 15k, ₦12k]
        AG2 --> AG3[RAG Exemplar Store\nDynamic Dialect Few-Shot Injection]
        AG3 --> AG4[Groq Llama 3.3 Multi-Tool Loop]
        
        AG4 --> TL{Tool Execution}
        TL -->|check_inventory| T1[Postgres Catalog + CLIP Embeddings]
        TL -->|propose_price| T2[Circuit Breaker Hard Floor Gate]
        TL -->|deploy_tactic| T3[Tactics Guard: Scarcity / Bundle / Credit]
        TL -->|escalate_to_merchant| T4[Vendor Communiqué SMS / Baileys Direct]
        TL -->|close_deal| T5[orderStateMachine: QUOTE_CREATED -> draft]
        TL -->|issue_payment_link| T6[orderStateMachine: PAYMENT_LINK_ISSUED -> awaiting_payment]
    end

    subgraph Payment["4. Payment Verification (:3002)"]
        T6 --> PY1[Customer Bank Transfer]
        PY1 --> PY2[Payment Webhook :3002\nPaystack / Monnify HMAC Verify]
        PY2 --> PY3{Amount Reconciliation}
        PY3 -->|Exact / Overpayment| PY4[orderStateMachine: PAYMENT_CONFIRMED\nState -> payment_verified]
        PY3 -->|Underpayment| PY5[State locked in awaiting_payment\nPrompt customer for remaining balance]
        PY4 --> PY6[Double-Entry Ledger Row in transactions Table]
    end
Loading

3. Multi-Service Event Topology

flowchart TB
    subgraph Inbound Channels
        WA_Cloud["WhatsApp Cloud API\n(Meta Webhook :3001)"]
        WA_Baileys["WhatsApp Multi-Device Web\n(Baileys Socket :3005)"]
        PSP_Hook["PSP Webhook\n(Paystack / Monnify :3002)"]
        Vendor_App["Merchant Expo Mobile App\n(REST API :3004)"]
    end

    subgraph Core Services
        Ingest["ingestion-service (:3001)\nMeta Webhook Receiver"]
        Baileys["baileys-gateway (:3005)\nBaileys Sockets & Status Poster"]
        Comms["comms-router (BullMQ)\nSliding Debounce & Outbound"]
        Agent["ai-negotiator (Groq SDK)\nAutonomous Multi-Tool Agent Loop"]
        PayVerif["payment-verification (:3002)\nReconciliation & Ledger"]
        CatSync["catalog-sync (:3003)\nMeta Commerce Catalog Pull"]
        MerchantAPI["merchant-api (:3004)\nMerchant Control Plane & App Backend"]
    end

    subgraph InProcess Engines ["In-Process Deterministic Engines"]
        Arc["NegotiationArc\nPure Strategy Reducer"]
        StateMachine["orderStateMachine\nPure Invariant Reducer"]
        BanditEngine["PricingBandit & ExemplarStore\nLinUCB RL & RAG Memory"]
    end

    subgraph Persistence Layer
        PG[("Neon PostgreSQL\n(merchants, products, orders,\ntransactions, conversation_outcomes)")]
        RedisDB[("Redis Cache & BullMQ\n(Debounce scratch, Locks,\nSessions, Idempotency)")]
    end

    WA_Cloud -->|HMAC POST| Ingest
    WA_Baileys -->|WebSocket Events| Baileys
    Ingest -->|Enqueue Message| Comms
    Baileys -->|Inbound Customer Turns| Comms
    Vendor_App <-->|REST API + JWT| MerchantAPI

    Comms -->|Debounced Turn| Agent
    Agent <--> Arc
    Agent <--> BanditEngine
    Agent --> StateMachine
    Agent <--> PG
    Comms <--> RedisDB

    PSP_Hook -->|HMAC POST| PayVerif
    PayVerif --> StateMachine
    PayVerif --> PG

    MerchantAPI <--> PG
    CatSync <-->|Sync Catalog| PG
    Baileys <--> RedisDB
Loading

4. Circuit Breaker & Vendor Escalation Flow

sequenceDiagram
    autonumber
    actor Customer as Customer (WhatsApp)
    participant Agent as AI Negotiator (agentLoop.ts)
    participant Arc as Negotiation Arc
    participant Comms as Vendor Communiqué Engine
    actor Merchant as Merchant Phone (SMS / WhatsApp)
    participant Router as Comms Router (debounce.ts)

    Customer->>Agent: "Do ₦10k for the dress (Floor is ₦14,250)"
    Agent->>Arc: Check tactical attempts (Bundle + Credit)
    Arc-->>Agent: Both tactics attempted & exhausted, price < floor
    Agent->>Comms: escalate_to_merchant(customerOffer: 10000, reason)
    
    rect rgb(255, 245, 230)
        Note over Comms: Dispatch SMS (Africa's Talking) / Baileys
        Comms->>Merchant: "ACE ESCALATION ⚠️ Customer Amaka wants Dress at ₦10,000 (floor: ₦14,250).\nReply: 1-Approve exception, 2-Hold firm at floor, 3-Offer bundle pivot"
        Comms-->>Arc: advanceArc(ESCALATED_TO_MERCHANT)
        Agent-->>Customer: "Hold on small, let me check with my oga..."
    end

    alt Merchant Replies "1" (Approve exception)
        Merchant->>Comms: "1"
        Comms->>Comms: Log choice to vendor_decisions table & clear Redis session
        Comms->>Router: enqueueInboundMessage("[SYSTEM: Merchant approved exception at ₦10,000]")
        Router->>Agent: Resume Agent Loop with approved override
        Agent->>StateMachine: close_deal(10000) -> issue_payment_link()
        Agent-->>Customer: "Oga don agree! Deal confirmed at ₦10,000. Generating payment details..."
    else Merchant Replies "2" (Hold firm)
        Merchant->>Comms: "2"
        Comms->>Router: enqueueInboundMessage("[SYSTEM: Merchant rejected exception. Hold firm at floor ₦14,250]")
        Router->>Agent: Resume Agent Loop
        Agent-->>Customer: "Oga say lowest we fit do na ₦14,250 last last. How you see am?"
    else Merchant Replies "3" (Offer bundle pivot)
        Merchant->>Comms: "3"
        Comms->>Router: enqueueInboundMessage("[SYSTEM: Merchant requested bundle pivot]")
        Router->>Agent: Resume Agent Loop
        Agent-->>Customer: "If you add matching scarf with am, I fit give you both for ₦16,000!"
    end
Loading

5. Deterministic Order State Machine Lifecycle

stateDiagram-v2
    [*] --> no_order
    
    no_order --> draft: QUOTE_CREATED (Item selected & quote generated)
    draft --> draft: QUOTE_CREATED (Price updated / re-quoted)
    draft --> awaiting_payment: PAYMENT_LINK_ISSUED (Virtual Account assigned)
    draft --> cancelled: ORDER_CANCELLED (Explicit cancellation)
    
    awaiting_payment --> payment_verified: PAYMENT_CONFIRMED (Amount >= total)
    awaiting_payment --> cancelled: PAYMENT_TIMEOUT (15-min window expired)
    awaiting_payment --> cancelled: ORDER_CANCELLED (Customer cancelled)
    
    payment_verified --> out_for_delivery: RIDER_ASSIGNED (Dispatch tracking URL linked)
    payment_verified --> cancelled: ORDER_CANCELLED (Merchant refund/cancel)
    
    out_for_delivery --> delivered: DELIVERY_CONFIRMED (Delivery verified)
    out_for_delivery --> cancelled: ORDER_CANCELLED (Delivery aborted)
    
    delivered --> [*]
    cancelled --> [*]

    note right of draft
        Pure reducer in orderStateMachine.ts
        Compiler-enforced exhaustiveness (assertNever)
    end note
Loading

6. Vendor WhatsApp Inventory Ingestion & Auto-Status Posting

sequenceDiagram
    autonumber
    actor Vendor as Merchant / Vendor (WhatsApp)
    participant Gateway as Baileys Gateway (:3005)
    participant Parser as Multimodal Parser (inventoryParser.ts)
    participant Storage as Cloudinary CDN
    participant Catalog as PostgreSQL Catalog (products)
    participant Cron as Auto-Status Scheduler (statusPoster.ts)

    Note over Vendor,Gateway: [Phase 1 Pipeline - Live in core/baileys-gateway]
    Vendor->>Gateway: Sends product photo with voice note / caption:<br/>"I get 10 pieces of this original sneaker, selling for 18k"
    Gateway->>Gateway: 10s sliding debounce (vendorBuffers)
    Gateway->>Parser: Media + Audio Extraction (Hooked in messageClassifier)
    Parser->>Parser: Groq Whisper Turbo STT + Groq Vision (Llama 3.2 11B)
    Parser->>Storage: uploadImage() (Streams to Cloudinary CDN -> secure_url)
    Parser->>Parser: Generate 512-dim CLIP embedding (embeddings.ts)
    Parser->>Catalog: Upsert SKU, Stock (10), Price (₦18k), Tags & Embedding
    Catalog-->>Gateway: Upsert Confirmed
    Gateway-->>Vendor: WhatsApp Confirmation: "✅ Added 'Classic Black Sneaker' (₦18,000) to catalog!"

    Note over Cron: 30-Minute Status Cron (statusPoster.ts)
    rect rgb(245, 255, 245)
        Note over Cron,Catalog: Algorithmic Promotion & Direct-Buy Execution
        Cron->>Catalog: SELECT product via Sales Velocity & Gross Margin CTE (ORDER BY promotion_score DESC)
        Note over Catalog: Prioritizes low-velocity, high-margin inventory for status promotion
        Cron->>Gateway: Post to status@broadcast with pre-filled wa.me deep-link
        Note over Gateway: Direct-Buy WhatsApp link: https://wa.me/{phone}?text=Buy%20SKU...
    end
Loading

7. Complete Step-by-Step Vendor Experience (Tri-Channel Authentication & Session Verification)

sequenceDiagram
    autonumber
    actor Merchant as Vendor Phone
    participant App as Biblio Mobile App
    participant API as Merchant API (:3004)
    participant Redis as Redis (:6380)
    participant Baileys as Baileys WhatsApp Gateway (:3005)
    participant WA as Merchant WhatsApp

    Merchant->>App: Enters phone (e.g. 0812 345 6789)
    Merchant->>App: Selects "WhatsApp Chat" & taps Send
    App->>API: POST /auth/merchant/login { phone, channel: 'whatsapp' }
    API->>Redis: SET otp:phone:08123456789 = "849201" (EX 300s)
    API->>Baileys: sendCustomerMessage(toPhone, text)
    Baileys->>WA: Dispatches chat message over WebSocket
    WA-->>Merchant: WhatsApp notification buzzes with 849201
    Merchant->>App: Types 849201 into 6-digit boxes
    App->>API: POST /auth/merchant/verify { phone, code: "849201" }
    API->>Redis: Validates against cached OTP & deletes key
    API-->>App: HTTP 200 OK with Merchant JWT & Profile
    App-->>Merchant: Logs into Dashboard
Loading

Architectural Walkthrough by Feature

1. Regional Dialect & Cultural Persona (Pidgin, English, Yoruba, Hausa, Igbo)

  • Dialect Guidance: dialectGuidance() injects culture-specific prompt framing based on the merchant's configuration:
    • Pidgin: Street-smart Nigerian English with subtle Pidgin flair ("no wahala", "I don hear" — strict anti-pattern guards avoid clichés like "sharp sharp" or forced slang).
    • Yoruba: Yoruba-inflected English ("ẹ kú iṣẹ́", "ó dára").
    • Igbo: Igbo-inflected English ("daalụ", "ọ dị mma").
    • Hausa: Hausa-inflected English ("sannu", "madalla").
    • English: Clear, polite Nigerian English.
  • Trace Persistence: The dialect is stamped onto both the in-memory NegotiationArc and the historical negotiation_traces database table.

2. Voice Notes (.ogg / .opus WhatsApp Audio)

  • Transcription: In mediaProcessor.ts, inbound audio streams are converted to in-memory files and transcribed with Groq Whisper (whisper-large-v3-turbo).
  • Context Injection: Transcriptions are passed to the agent as [Voice note]: <transcript>, instructing the model to respond naturally to the customer's speech without robotic meta-commentary.
  • Price Extraction: Spoken price counter-offers (e.g. "I get 12k for you") are parsed by extractCustomerPriceOffer, triggering advanceArc(CUSTOMER_COUNTERED) automatically.

3. Image Processing & Auto-Status Posting

  • Vendor Inventory Push: Vendor product photos sent to their line are parsed via Groq Vision (llama-3.2-11b-vision-preview) in inventoryParser.ts, cataloged into PostgreSQL, and optionally broadcast to WhatsApp Status via postProductToStatus().
  • Customer Photo Queries: Customer images sent during negotiation are base64-encoded and passed directly to the vision model to identify matching stock.

4. Payment Structures & Underpayment Protection

  • Virtual Account Numbers (VAN): issuePaymentLink generates dynamic transfer cards with merchant bank details.
  • Webhook Reconciliation: payment-verification/src/index.ts listens on :3002, validates HMAC headers (x-paystack-signature, x-ace-signature), and checks amounts:
    • Full Payment: Transitions state to payment_verified, unlocks fulfillment, and sends receipt.
    • Underpayment: Locks state in awaiting_payment, prompts for the remaining deficit, and emits telemetry to DataIntelligenceEngine.

5. Burst Debouncing (ManyChat-Style Smoothness)

  • debounce.ts consolidates rapid customer messages sent within 10 seconds into a single, cohesive ConversationTurn, preventing the AI from interrupting or double-responding.

6. RL + RAG Pipeline — Adaptive Negotiation & Context Engine

  • Core Principle: Learning Proposes; Circuit Breakers Enforce: The AI's learning components only recommend strategic actions (discount tiers, few-shot framing); deterministic state machines and pre-computed floor circuits enforce invariant boundaries. The bandit can never violate the merchant's configured minimum margin.
flowchart TD
    subgraph Inbound["Inbound Customer Message"]
        M1[Customer Message] --> M2[Comms Router: 5s Sliding Debounce]
    end

    subgraph Active_RAG["RAG Exemplar Memory (ACTIVE IN RUNTIME)"]
        M2 --> R1[Exemplar Store: exemplarStore.ts]
        R1 -->|Query by Dialect, Tier & Scenario| R2[(interaction_exemplars Table)]
        R2 -->|Dynamic Nigerian Dialogue Few-Shots| A1[System Prompt Injection in agentLoop.ts]
    end

    subgraph RL_Bandit["LinUCB Contextual Bandit (ACTIVE IN RUNTIME)"]
        M2 -->|Context Extraction| B1[Feature Extractor: 6D Context Vector\nLTV, recency, margin, value, dialect, hour]
        B1 --> B2[LinUCB Pricing Bandit: pricingBandit.ts]
        B2 -->|Select Arm with Matrix Inversion| B3[7 Discrete Arms: 0% to 30%]
        B3 -->|Proposed Arm Signal| A1
    end

    subgraph Safety_Execution["Execution & Circuit Breakers (ACTIVE IN RUNTIME)"]
        A1 --> EX1[Groq Llama 3.3 Versatile LLM]
        EX1 --> CB{Circuit Breaker Gate: pricingService.ts}
        CB -->|Valid Margin (>= Floor)| OUT1[propose_price Tool: Send Counter-Offer]
        CB -->|Breaches Floor| OUT2[Circuit Breaker Trip: Force Bundle / Credit / Escalate]
    end

    subgraph Feedback_Loop["Telemetry & Outcome Feedback (ACTIVE RUNTIME FEEDBACK)"]
        OUT1 --> FB1[interactionFeedback.recordInteraction]
        FB1 --> FB2[(conversation_outcomes Table with bandit_arm)]
        ORD[Payment Confirmed / Deal Closed] -->|Backdated Outcome| FB3[Margin-Based Reward: recordOrderOutcome]
        FB3 -->|Reward Update & Weight Inversion| B2
    end
Loading
  • Data Foundation (conversation_outcomes & interaction_exemplars):
    • conversation_outcomes in Neon PostgreSQL stores full (context, action, outcome) tuples including LinUCB state representations, customer reactions, turn counts, and final realized margin.
    • interaction_exemplars maintains authenticated conversational patterns across Nigerian Pidgin, Yoruba, Igbo, Hausa, and English.
  • RAG Exemplar Store (shared/src/rag/exemplarStore.ts):
    • Dynamically extracts few-shot exemplars matching the customer's dialect, tier (new, regular, vip), and scenario (first_order, hesitation, low_stock, high_value).
    • Pre-formats authentic Nigerian commerce dialogue into the system prompt of agentLoop.ts.
  • LinUCB Contextual Bandit (shared/src/rl/pricingBandit.ts):
    • Feature Representation (6D): $[1.0, \text{price_ratio}, \text{clv_scaled}, \text{is_vip}, \text{turns_scaled}, \text{sentiment}]$.
    • Discrete Action Arms: $7$ discount options: $[0%, 5%, 10%, 15%, 20%, 25%, 30%]$.
    • Upper Confidence Bound: Action selection via $UCB_a = \hat{\theta}_a^T x + \alpha \sqrt{x^T A_a^{-1} x}$.
    • Circuit-Breaker Masking: Clamps proposed discount to prevent any violation of the merchant's configured absolute floor price.
    • Dual Persistence: Matrix states persisted in Redis with zero-downtime in-memory caching.
    • Interaction Feedback Loop (shared/src/feedback/interactionFeedback.ts):
    • Tracks live turns and merchant manual overrides from the Exception Queue.
    • Backdates order outcomes when a transaction clears, calculating realized margin and updating the LinUCB model: $$r = \mathbf{1}{{\text{order_completed}}} \cdot \text{margin_realized} - \lambda \cdot \mathbf{1}{{\text{merchant_override}}}$$

7. Graceful Offline Fallback & Edge Resilience Architecture

ACE and Biblio are engineered specifically for emerging market connectivity (cellular volatility, power cuts, WhatsApp session drops). A multi-tier offline-first pipeline protects commerce transactions across every layer:

flowchart TD
    subgraph MobileApp ["📱 Merchant Mobile App (Expo / React Native)"]
        UI[Merchant Screens: Inbox, Catalog, Settings]
        RQ["React Query (networkMode: 'offlineFirst')"]
        Persist["AsyncStorage Cache (24h gcTime)"]
        NetBanner["Offline Banner (Pill Indicator)"]
        NativeLink["wa.me Deep-Link Native WhatsApp"]
    end

    subgraph Backend ["⚡ Biblio Backend & Gateway"]
        BullMQ["BullMQ + Redis Persistent Queues"]
        Debounce["Inbound Debounce Buffer"]
        Poster["Status Poster Engine"]
        Communique["Vendor Communiqué Engine"]
    end

    subgraph Fallbacks ["🛡️ Fallback Channels"]
        SMS["Africa's Talking / Termii SMS"]
        NativeWA["Merchant's Native WhatsApp App"]
        InternalLogistics["Internal Manual Dispatch"]
    end

    UI --> RQ
    RQ <--> Persist
    RQ -.->|No Internet| NetBanner
    UI -->|Baileys Socket Offline| NativeLink --> NativeWA

    Poster -->|Socket Connected| Baileys[Baileys Outbound Socket]
    Poster -->|Socket Offline| NativeLink

    Communique -->|Primary| Baileys
    Communique -->|Socket Offline / 5m Timeout| SMS

    Debounce --> BullMQ
    BullMQ -.->|Retry Exponential Backoff| DB[(Neon Postgres)]
Loading
  • Client Persistence: React Query with networkMode: 'offlineFirst' and a 24-hour AsyncStorage persistence layer renders cached inventory, drafts, and analytics instantaneously when offline.
  • Deep-Link Failover: If the Baileys gateway socket is disconnected during a status broadcast, the engine returns a pre-filled wa.me deep-link opening the native WhatsApp app directly on the merchant's physical phone.
  • Vendor Communiqué SMS Dual-Track: Escalations attempt WhatsApp delivery via business line socket first; on socket failure, the engine immediately dispatches via Africa's Talking / Termii SMS with an automated 5-minute delayed BullMQ safety reminder.

Production Execution & Architecture Verification Matrix

The table below details all 9 architectural pipelines and confirms their live implementation, active hooks, and production readiness across the physical codebase on disk.

# Flow / Claim Codebase Location Current Reality on Disk Status Production Implementation Details
1 Vendor WhatsApp Intake Dispatching messageClassifier.ts
inventoryParser.ts
Pipeline logic is fully written and live-hooked in messageClassifier.ts. Inbound merchant messages from business and personal lines automatically route directly to the autonomous Biblio WhatsApp Agent (for natural business queries, settings, inquiries, and Providus VAN management) or the inventory ingestion pipeline (for product photos and voice notes with Whisper and Groq Vision parsing). ✅ LIVE & HOOKED Extend vendor command vocabulary (e.g. multi-variant stock updates) as needed.
2 Persistent Media Storage (Cloudinary CDN) inventoryParser.ts:uploadImage Upgraded to production Cloudinary SDK v2. Images are streamed directly to Cloudinary (ace/vendors/{vendorId}) using upload_stream and return permanent public HTTPS CDN URLs (secure_url) stored in products.image_url. ✅ LIVE (Cloudinary) Optional: Configure custom CNAME CDN domain in Cloudinary dashboard.
3 Sales-Velocity & Margin Status Scheduler statusPoster.ts:postNextProduct 30-minute cron scheduler runs an algorithmic ranking CTE. It calculates gross margins (price - floor)/price and evaluates 14-day sales velocity (orders table lookup) to prioritize slow-moving, high-margin inventory for WhatsApp Status broadcasts. ✅ LIVE (Algorithmic) Tune promotion scoring weights based on merchant seasonality feedback.
4 Automated Direct-Buy Status Trigger inventoryParser.ts:buildStatusCaption Status post captions dynamically generate pre-filled wa.me click-to-chat deep-links (https://wa.me/{phone}?text=...) carrying the exact SKU, product name, and formatted price. Tapping the link instantly opens the chat with pre-filled buyer intent. ✅ LIVE (wa.me Deep-Link) Optional: Generate short-links with UTM analytics tracking.
5 LinUCB Contextual Bandit Live Action Gating pricingBandit.ts
agentLoop.ts
Live in runtime! agentLoop.ts computes the 6D context vector (LTV, order recency, product margin, order value, dialect, time of day) and invokes proposeBanditPrice to inject learned pricing signals into the system prompt. Every turn records bandit_arm, and recordOrderOutcome backdates margin rewards into Redis. ✅ LIVE & GATING Periodic automated hyperparameter sweeps for exploration factor alpha.
6 Live Bank Virtual Account Provisioning (VAN) tools.ts:issuePaymentLink
payment-verification
Integrates with Paystack's Charge API (bank_transfer channel) to generate real dynamic dedicated virtual accounts (Providus/Wema). Inbound webhooks on :3002 verify HMAC signatures and reconcile payments automatically. ✅ LIVE (Paystack DVA) Add Monnify / Wema Bank direct API backup adapters for multi-PSP redundancy.
7 Meta Cloud API Voice Note STT debounce.ts:turnWorker
ingestion-service
Meta Cloud API inbound audio notes are downloaded from the Graph API using per-merchant system tokens, buffered into memory, and transcribed in real-time via Groq Whisper (mediaProcessor.ts). ✅ LIVE (Graph API + Whisper) Cache transcribed audio buffers in Redis to prevent re-fetching on replays.
8 Automated Rider Dispatch & Carrier Booking logistics/src/streamConsumer.ts
dispatchService.ts
Redis Stream stream:payments.verified publishes verified orders on PAYMENT_CONFIRMED. The logistics stream consumer auto-dispatches riders via Sendstack/Kwik APIs, transitions order state to out_for_delivery, and notifies customers with live tracking URLs. ✅ LIVE (Redis Stream + Sendstack) Add courier webhooks to advance state machine from out_for_delivery to delivered.
9 Offline-First Resilience & Deep-Link Failover _layout.tsx
statusPoster.ts
vendorCommunique.ts
React Query offlineFirst with 24h AsyncStorage cache; automatic wa.me deep-link failover on socket drop; dual-track SMS escalation with 5-minute delayed BullMQ reminder. ✅ LIVE & ACTIVE Zero-downtime offline continuity across volatile network conditions.

8. Biblio Mobile App Architecture & Hardware Polish

  • Full-Height Modal Drawers (SidePanel.tsx):
    • Converted the slide-in drawer to a root <Modal visible transparent statusBarTranslucent> with position: 'absolute', top: 0, bottom: 0, right: 0, height: '100%'.
    • Fixes the Android/iOS hardware navigation bar gap where drawers were previously raised above the screen bottom.
  • Zero White-Flash Screen Transitions:
    • Added React Navigation ThemeProvider directly in app/_layout.tsx with theme background matching.
    • Bound backgroundColor: c.bg across GestureHandlerRootView, SafeAreaProvider, and Tabs sceneStyle.
    • Configured root window dark background #090A0C in app.json.
  • Clean Standardized Header Architecture (ScreenHeader.tsx):
    • Removed redundant boxed container wrappers around headers in suppliers.tsx and crm.tsx.
    • Standardized all sub-screens to consistent hit targets, back arrows, and pill search bars.
  • Live Inventory & Sync Carousel:
    • Added horizontal carousel to app/(tabs)/index.tsx displaying stock levels, WhatsApp origin tags, and Naira prices.
    • Seamless "Add First Product" routing into crm.tsx, prioritizing manual 1-tap addition with optional WhatsApp catalog syncing.
  • 100% Scroll & Safe-Area Coverage:
    • All screens (index, inbox, more, crm, suppliers, chats, chat/[id], finance, settings, notifications, support, search) use ScrollView or FlatList with keyboardShouldPersistTaps="handled" and contentContainerStyle padding ($100\text{--}140\text{px}$) ensuring elements are never clipped by the floating bottom tab bar.

The Three-Layer Architecture

┌──────────────────────────────────────────────────────────────────┐
│  LAYER 3: ENTERPRISE INTELLIGENCE PLATFORM (B2B)                 │
│  AI Training Data · FMCG Market Pulse · ACE TrustScore API       │
│  The "Scale AI of Informal Commerce" — $16.5M ARR by Year 2      │
└───────────────────────────────┬──────────────────────────────────┘
                                │ (Refined data → enterprise buyers)
┌───────────────────────────────▼──────────────────────────────────┐
│  LAYER 2: AUTONOMOUS STATE ENGINE (Core IP)                       │
│  11 microservices: Rust core + Vercel AI SDK agent layer          │
│  Ingestion → Identity → AI Negotiator → State Machine →           │
│  Payment → Logistics → Supplier → Visual Context → Comms Router   │
│  Every interaction: data collected, refined, monetised            │
└───────────────────────────────┬──────────────────────────────────┘
                                │ (Raw commerce events)
┌───────────────────────────────▼──────────────────────────────────┐
│  LAYER 1: INTERFACE LAYER                                         │
│  Merchant: React Native app (exception dashboard)                 │
│  Customer: WhatsApp → PWA Trojan Horse → Global Buyer ID          │
│  Vendor Communiqué: SMS reply-code for merchant decisions         │
└──────────────────────────────────────────────────────────────────┘

The Four Autonomous Workflows

ACE automates the entire commercial lifecycle — from first customer message to post-sale retention. The merchant interacts only with exceptions.

Workflow What ACE Does Merchant Input
Order Fulfillment Customer texts → AI parses intent (Groq Llama 3.3) → AI Negotiator closes deal → virtual account issued → payment verified → rider dispatched → merchant notified Zero
Demand-Driven Restocking Inventory Oracle detects stockout (16hr lead) → pings supplier WhatsApp → negotiates price → margin analysis → drafts PO 1 tap (8 seconds)
Customer Retention Engine Nightly analyser detects at-risk VIP → generates culturally-nuanced message → sends after 4hr hold if no merchant action Optional review
Multimodal Visual Resolution "That blue dress in your last reel" → CLIP / Llama 3.2 Vision embeddings → SKU resolved → price negotiated autonomously Zero

The AI Negotiator — ACE's Most Differentiated Feature

ACE doesn't apply discounts. It negotiates — like a skilled market trader who knows the customer's full history, operates in their dialect, and closes deals autonomously within merchant-defined boundaries.

The negotiation arc: Anchor → Acknowledge → Counter → Close / Pivot / Escalate

6 autonomous tactics: Relationship Anchor · Bundle Pivot · Inventory-Verified Scarcity · Future Credit · Urgency Window · Sentiment-Aware Soft Close

The anti-haggling concession guard: Physical limit of max 3 agent offers per conversation thread before pricing locks permanently.

The Rust / State Machine circuit breaker: The AI is physically prevented from closing below the merchant's floor price. On below-floor requests: Bundle Pivot → Future Credit → Vendor Communiqué SMS to merchant.

Every negotiation is a data asset: NegotiationTrace logs price elasticity per SKU, per geography, per customer tier → sold to FMCG brands as market intelligence.


The Vendor Communiqué System

ACE keeps merchants in control without requiring them to be at a dashboard.

Exception detected → Channel selected by urgency:
  > ₦50K order dispute  → AI voice call
  Pricing exception     → SMS: "Amaka wants dress at ₦12K (floor: ₦14,250). Reply 1-approve, 2-hold, 3-bundle"
  Restock approval      → SMS: "Red Ankara running out. Alhaji: ₦42K for 50yds (44% margin). Reply 1 to approve."
  Routine orders        → WhatsApp morning digest
  Stats                 → App push (weekly)

Merchants respond with a single digit from any phone. No app needed. Works on feature phones.


Instant 1-Step Auto-Provisioning & Admin Portal

1. Zero-Config WhatsApp Line Onboarding

Any new Nigerian WhatsApp phone number can be provisioned in one seamless command or single button click in the Admin Portal without manual database seeding:

  • CLI:
    npm run pair 2348012345678
  • Admin Portal UI:
    npm run admin-portal
    Navigate to WhatsApp Auto-Pairing (/vendor-status), enter the phone number, and click Auto-Provision & Pair.
    • Automatically provisions PostgreSQL merchants and vendors records.
    • Seeds standard Nigerian pricing rules (floor margin, counter-offer step limits).
    • Configures cultural dialect voice (Pidgin, Yoruba, Igbo, Hausa, English).
    • Immediately requests and displays the 8-digit WhatsApp pairing code.

2. Autonomous WhatsApp Biblio Assistant & Live Inventory Ingestion

Merchants operate directly through natural WhatsApp conversation with zero commands or state toggles:

  • Zero Keywords Needed: You never need to send business or end-business. The Biblio WhatsApp Agent is active 24/7.
  • Natural Store Operations: Asking questions ("What are my orders today?", "Show my Providus VAN settlement account", "What are my recent customer inquiries?", "Set floor price of sneakers to 15k") automatically routes directly to the Biblio Agent, which executes tools and replies in real time.
  • Instant Product Ingestion: Sending product photos, voice notes describing items and prices, or typing item prices directly into your WhatsApp chat immediately activates Whisper and Groq Vision to index and add products to your store catalog.

Technology Stack

Layer Stack
Agent / AI orchestration TypeScript + Groq SDK (llama-3.3-70b-versatile & llama-3.2-11b-vision-preview)
RL & Contextual Bandits LinUCB Contextual Bandit (shared/src/rl/pricingBandit.ts) with 6D state vector & margin-based reward feedback
RAG & Exemplar Memory Dialect/tier-indexed Exemplar Store (shared/src/rag/exemplarStore.ts) with dynamic prompt injection
Outcome & Feedback Loop Interaction Feedback Engine (shared/src/feedback/interactionFeedback.ts) & conversation_outcomes ledger
Core microservices Fastify, TypeScript, BullMQ, Redis, PostgreSQL
WhatsApp Gateways Meta WhatsApp Cloud API (ingestion-service) & Baileys Multi-Device Web Sockets (baileys-gateway)
Merchant app React Native + Expo (v57, Reanimated 4, NativeWind 4, TanStack Query)
Customer interface WhatsApp Direct UI & Progressive Web App
Primary DB PostgreSQL (ACID, Neon serverless, partitioned by merchant_id)
State cache & Queue Redis + BullMQ (4.5s debounce window, rate-limiting, locks)
Payments Monnify / Paystack / Flutterwave Dynamic Virtual Account Numbers (VAN)

🚀 Quickstart & Local Testing Guide

1. Prerequisites

Ensure you have the following installed:

  • Node.js >= v20
  • Redis running locally (redis://localhost:6379) or via cloud Redis
  • PostgreSQL (Neon cloud connection string in DATABASE_URL)

2. Environment Configuration

Verify that .env in the root repository contains:

DATABASE_URL=postgresql://neondb_owner:***@ep-***.us-east-2.aws.neon.tech/biblio?sslmode=require
REDIS_URL=redis://localhost:6379
MERCHANT_API_PORT=3004
BAILEYS_GATEWAY_PORT=3005
APILAYER_API_KEY=your_apilayer_key_here
GROQ_API_KEY=your_groq_key_here
ANTHROPIC_API_KEY=your_anthropic_key_here
PAYSTACK_SECRET_KEY=your_paystack_key_here

3. Starting the Backend Services

Option A: Single-Command Unified Backend (Recommended)

Start the entire core backend suite at once with a single command from the project root:

npm run app

What npm run app does:

  • Launches and supervises all 7 core backend microservices simultaneously in a single unified terminal session:
    1. [merchant-api] Fastify REST & Realtime Stream Server (Port 3004)
    2. [baileys-gateway] WhatsApp Multi-Device Socket Manager (Port 3005)
    3. [comms-router] Sliding Debounce Queue Worker (4.5s Customer Turn Builder)
    4. [orchestrator] State Machine Transitions & Automation Daemon
    5. [payment] Payment Verification, Webhooks & Escrow Settlement (Port 3002)
    6. [logistics] Automated Courier Dispatch Rider Broker (Kwik/Fez/Gokada)
    7. [sms-webhook] Vendor Communiqué 1-Digit SMS Decision Receiver (Port 3006)
  • Provides color-coded prefix logging ([merchant-api], [baileys-gateway], [comms-router], [orchestrator], [payment], [logistics], [sms-webhook]) with clean line buffering.
  • Features graceful, unified teardown: Pressing Ctrl+C cleanly terminates the entire process tree on both Windows and POSIX without leaving orphaned background processes or locked ports.

Option B: Starting Microservices Individually

If you prefer isolated terminal windows for individual debugging, you can run each service independently:

1. Core Runtime Services
  • npm run merchant-api
    • Starts: ace-whatsapp/core/merchant-api/src/index.ts on port 3004.
    • What it does: Provides the primary REST and Server-Sent Events (SSE) / WebSocket endpoints for the merchant mobile app and web clients. Handles vendor authentication, merchant profile settings, dashboard metrics, inventory CRUD, Providus VAN settlement accounts, order actions, and the autonomous Biblio Assistant agent loop.
  • npm run baileys-gateway
    • Starts: ace-whatsapp/core/baileys-gateway/src/index.ts on port 3005.
    • What it does: Manages persistent WhatsApp Web Multi-Device WebSocket connections using @whiskeysockets/baileys. Generates QR codes and 8-digit phone pairing codes, streams incoming customer chats into Redis, dispatches outbound AI replies to WhatsApp, and handles automated 30-minute status broadcasts.
  • npm run comms-router
    • Starts: ace-whatsapp/core/comms-router/src/debounce.ts.
    • What it does: Implements the sliding 4.5-second debounce buffer for customer messages. When a buyer sends multiple burst messages (e.g. "Hi", "How much for this?", "Can you do 15k?"), it coalesces them into a single ConversationTurn and invokes the Groq LLaMA 3.3 AI Negotiator agent loop.
  • npm run event-orchestrator
    • Starts: ace-whatsapp/core/event-orchestrator/src/index.ts.
    • What it does: Subscribes to Redis PubSub order events (QUOTE_CREATED, PAYMENT_CONFIRMED, RIDER_ASSIGNED). Executes pure reducer state transitions, drafts vendor action cards (Restock PO drafts, Winback offers, Customer discount approvals), and processes the transactional outbox queue.
2. Specialized Pipeline & Worker Services
  • npm run payment
    • Starts: ace-whatsapp/core/payment-verification/src/index.ts.
    • What it does: Listens for Monnify, Paystack, and Providus Dynamic Virtual Account (VAN) settlement webhooks. Automatically matches transfer references against pending quotes, verifies zero-deduction amounts, and transitions orders to PAYMENT_CONFIRMED.
  • npm run catalog-sync
    • Starts: ace-whatsapp/core/catalog-sync/src/index.ts.
    • What it does: Ingests vendor product catalogs from CSV/Excel or unstructured text. Uses @xenova/transformers to generate 384-dimensional vector embeddings and syncs them with pgvector in PostgreSQL for RAG semantic search.
  • npm run logistics
    • Starts: ace-whatsapp/core/logistics/src/index.ts.
    • What it does: Coordinates automated dispatch logistics across Nigerian courier platforms (Kwik, Fez, Gokada, Sendbox). Calculates delivery rates, books riders upon cleared escrow payment, and generates real-time customer tracking links.
  • npm run ingestion
    • Starts: ace-whatsapp/core/ingestion-service/src/index.ts on port 3000.
    • What it does: Alternate webhook gateway for merchants utilizing official Meta WhatsApp Cloud API accounts instead of Baileys WebSockets. Verifies Meta HMAC-SHA256 signatures and normalizes incoming Graph API payloads into standard InboundMessage DTOs.
  • npm run sms-webhook
    • Starts: ace-whatsapp/core/comms-router/src/smsWebhook.ts.
    • What it does: Ingests 1-digit SMS approvals from merchants (e.g. 1 to approve discount, 2 to counter) via Africa's Talking / Twilio, allowing merchants to operate their store offline from basic feature phones.
3. Provisioning & Admin Portal
  • npm run pair <phone>
    • Starts: pair.ts.
    • What it does: One-step merchant onboarding CLI. Automatically provisions merchant records in PostgreSQL and requests an instant 8-digit WhatsApp pairing code (e.g. npm run pair 2349064982841).
  • npm run admin-portal
    • Starts: Next.js development server in ace-whatsapp/apps/admin-portal on port 3001.
    • What it does: Visual web dashboard for internal operators. Features real-time WhatsApp session monitoring, QR code pairing, order pipeline tracking, tone guide configuration, and token ledger inspection.

4. Starting the Mobile & Web Operator App

Launch the merchant mobile app:

# Navigate to mobile app directory
cd ace-merchant-app

# Run in Expo Go / Android Emulator / iOS Simulator (with 4GB memory allocation)
npx expo start -c

# Or run in Web Browser (accessible at http://localhost:8081)
npm run web

Testing & Login Details:

  • Local Web URL: http://localhost:8081
  • Local Network URL (for physical smartphones on Wi-Fi): http://192.168.100.75:8081
  • Merchant Login Testing:
    • Enter any phone number (e.g. 08012345678).
    • When prompted for OTP verification, enter the developer test code: 123456.
    • Automatically loads the Bento Grid Dashboard, Inbox card stack, Live Chats, and Biblio Assistant.

5. Testing, Verification & Infrastructure Tools

Quality & Code Integrity

  • npm test: Executes the complete Vitest test suite (129+ tests covering order state machines, negotiation arcs, LinUCB pricing bandits, and schema contracts).
  • npm run test:watch: Interactive Vitest runner for continuous TDD test execution.
  • npm run typecheck: Runs strict TypeScript compilation checks (tsc --noEmit) across all packages without emitting JavaScript.

Infrastructure Daemons

  • Redis (WSL / Local):
    # Start high-performance Redis daemon in WSL on port 6380:
    wsl redis-server --port 6380 --daemonize yes
  • PostgreSQL Database:
    • Hosted serverless PostgreSQL on Neon. Database connection string configured via DATABASE_URL in .env.
  • Health Verification Probes:
    # Check Merchant API health (Port 3004)
    curl http://localhost:3004/health
    # Expected: {"ok":true}
    
    # Check Baileys Gateway health (Port 3005)
    curl http://localhost:3005/health
    # Expected: {"ok":true,"activeSessions":0,"sessions":[]}

Key Documents

Product & Architecture

Engineering & Infrastructure

About

AI automation tools for companies to automate their entire architecture and data labelling

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages