# Omniloq — Phase 2 : Résultats **Statut : ✅ 12/12 tests d'acceptation — 35/35 au total (spike + runtime + Phase 1.5 + Phase 2)** Phase 2 = premier canal réel : **WhatsApp Meta**, en ingestion **asynchrone** (outbox PostgreSQL + worker), outbound texte, tracking `sent/delivered/read`. ## Chemin implémenté ``` Meta webhook → verify/parse → WhatsAppMetaChannel.handleInbound → InboundMessage normalisé → résoudre ChannelAccount (phone_number_id) → résoudre/créer ChannelIdentity → résoudre/créer Conversation → TX: Message inbound + Job → ACK Meta (HTTP 200) → [PostgreSQL worker] claim (SKIP LOCKED) → Conversation lease → Runtime Omniloq → Message outbound (queued) → WhatsAppMetaChannel.sendMessage() → wamid → sent ``` **Le webhook n'est jamais synchrone avec le runtime** : POST → validation → normalisation → TX → COMMIT → 200. Le LLM/MCP tourne ensuite dans le worker. Jamais « Meta attend pendant DeepSeek ». ## Frontière de canal (respectée) L'adaptateur Meta connaît (et confine) : `phone_number_id`, WABA ID, version Graph API, access token, wamid, payload webhook, statuts. **Aucun JSON Meta n'entre dans core.** Le runtime ne voit que : `InboundMessage`, `OutboundMessage`, `ChannelIdentity`, `SendResult`, `ChannelEvent`. ## Deux types d'événements webhook - `message_received` → `InboundMessage` → ingestion (Message + Job). - `message_status` (`sent/delivered/read/failed`) → mise à jour de `Message.status`, `deliveredAt`, `readAt` — **jamais d'AgentRun**. ## Idempotence - `Job @@unique([tenantId, dedupKey])` avec `dedupKey = meta:{channelAccountId}:{wamid}`. - Webhook renvoyé (même wamid) → un seul Message/Job logique, ACK silencieux. La DB décide (P2002). ## Credentials - **Access token** → `ChannelAccount.secretId → Secret.encryptedValue` (AES-256-GCM, décrypté au runtime). Jamais `config.accessToken`. - **phone_number_id** (non secret) → `ChannelAccount.externalAccountId`, sert au mapping multi-tenant. - `verifyToken` / `graphApiVersion` → `ChannelAccount.config`. ## Outbound : persister d'abord, envoyer ensuite `AgentRun final → Message outbound status=queued → sendMessage() → wamid → externalMessageId=wamid status=sent`. Erreur Graph API → `status=failed` + `providerMetadata.error` (le runtime/audit sont conservés). ## Fichiers livrés - `packages/channels/src/types.ts` — types normalisés + interface `Channel` - `packages/channels/src/whatsapp/meta.ts` — `WhatsAppMetaChannel` (verify/parse/send) - `packages/db/src/crypto.ts` — `encryptSecret` / `decryptSecret` (AES-256-GCM) - `packages/core/src/ingest.ts` — `ingestInbound` / `ingestStatus` - `packages/core/src/worker.ts` — `processNextJob` (claim SKIP LOCKED + runtime + envoi) - `packages/core/src/channel-resolver.ts` — résolution ChannelAccount → Channel - `apps/api/src/server.ts` — Fastify (GET verify + POST ingest, ACK immédiat) - `packages/core/test/phase2.test.ts` — 12 tests ## Les 12 tests d'acceptation | # | Test | Vérification | |---|------|--------------| | 1 | Vérification webhook valide | acceptée, challenge renvoyé | | 2 | Vérification invalide | refusée | | 3 | Message texte Meta | `InboundMessage` normalisé (from/id/text/phone_number_id) | | 4 | phone_number_id → bon compte | multi-tenant, bon `tenantId` | | 5 | Premier contact | création `ChannelIdentity` | | 6 | Contact existant | identité réutilisée (pas de doublon) | | 7 | Webhook dupliqué | aucun doublon Message/Job (idempotence) | | 8 | Webhook → ACK | Job pending, 0 AgentRun (jamais synchrone) | | 9 | Worker → runtime → envoi | réponse enregistrée, wamid persisté, Job done | | 10 | sendMessage() | bon endpoint + vrai wamid retourné | | 11 | delivered/read | update Message, aucun AgentRun | | 12 | Erreur Graph API | outbound failed, runtime/audit conservés | ## Reste à faire (hors périmètre Phase 2, volontairement) Embedded Signup · templates marketing · media/voice · Bird/Gupshup · Telegram · dashboard · billing. ## Prochaine étape **E2E réel** : ton téléphone → WhatsApp Meta → Omniloq → DeepSeek → réponse → ton WhatsApp. Nécessite un ChannelAccount Meta réel (token, phone_number_id, WABA, verify_token) + URL de webhook publique (HTTPS).