Architecture
The service uses a queue-based architecture with the following components:Processing Flow
1. Enqueue Request
Calls are enqueued via the/api/v1/process endpoint. The system:
- Validates
call_idandtenant_id - Checks if the call is already completed (idempotent)
- Sets MongoDB status to
pending - Adds job to in-memory queue
2. Worker Processing
The background worker (WorkerLoop) continuously processes jobs:
- Dequeue: Removes job from queue and marks as in-progress
- Fetch Data: Retrieves transcript and metadata from MongoDB
- Assemble Transcript: Reconstructs ordered transcript from ASR chunks
- Generate Summary: Calls OpenAI API for non-empty transcripts
- Send Email: Delivers summary to configured recipients
- Update Status: Marks call as
completedorfailed
src/apps/post_call/app/worker_loop.py:69-83
3. Transcript Assembly
Transcripts are assembled from MongoDB using linked-list ordering:- Items are connected via
previous_item_idpointers - ASR chunks are joined in sequence order
- User and assistant roles are distinguished
- Empty transcripts (seq ≤ 2 or no content) trigger fallback flow
src/apps/post_call/app/utils/transcript.py:181-237
4. AI Summarization
For non-empty transcripts, the worker:- Sends transcript to OpenAI with structured output schema
- Receives
PostCallSummarywith:- One-line summary
- Caller request
- Key points
- Actions to take
- Contact information
src/apps/post_call/app/worker_loop.py:429-462
5. Email Notification
Emails are sent via SMTP with:- HTML body containing summary and transcript
- Subject line with tenant name and caller number
- Configured recipient list from tenant config
- Optional BCC for monitoring
src/apps/post_call/app/worker_loop.py:464-514
Queue Management
TheCallQueue class manages job lifecycle:
- Idempotent enqueuing: Jobs already queued or in-progress are rejected
- State tracking: Separate sets for
queued_keysandin_progress_keys - Thread-safe: All operations protected by asyncio lock
src/apps/post_call/app/call_queue.py:18-65
Retry Logic
Failed jobs are retried up to 3 times with exponential backoff:- Retryable errors: Network issues, timeouts, 5xx status codes
- Non-retryable errors: Validation errors, 4xx status codes (except 429)
- Backoff: min(2^(attempt-1), 8) seconds
- Terminal failure: Status set to
failedafter max attempts
src/apps/post_call/app/worker_loop.py:85-152
Status States
Calls progress through the following states:| Status | Description |
|---|---|
pending | Queued for processing |
processing | Currently being processed (internal) |
completed | Successfully processed and email sent |
failed | Processing failed after max retries |
src/models/utils/enums.py:4-8
Configuration
The service requires:- MongoDB: Collections for transcripts, metadata, and tenant configs
- OpenAI API: For structured summarization
- SMTP: For email delivery
- Tenant Config: Display name and recipient list per tenant
src/apps/post_call/main.py:18-41
Next Steps
Process Endpoint
Trigger post-call processing for a specific call