This document outlines the autonomous and semi-autonomous agents operating within the Therascript ecosystem. The system utilizes a combination of background workers (BullMQ), specialized API handlers, and LLM-driven strategies to process therapy sessions.
- Node 23.10.0 exactly (see
.nvmrc).better-sqlite3@11.xdoes not publish prebuilt binaries for Node 24+, so running on a newer Node will silently leave the native binding unbuilt and the API will fail to start withCould not connect or initialize database. Runnvm usebeforeyarn install. yarn installrunsscripts/check-native-bindings.mjsvia thepostinstallhook. If it exits non-zero, your environment is wrong — do not bypass it with--ignore-scripts.
For detailed implementation references, please see:
- Architecture: High-level system architecture, package organization, and infrastructure.
- Component Map: Breakdown of packages, libraries, and entry points.
- Navigation Guide: "Where to change what" – directory structure and common tasks.
- API Reference: Complete REST API endpoint documentation.
- Operational Flows: Step-by-step data flow for Transcription, Chat, and Analysis.
- Schema Reference: SQLite tables and Elasticsearch indices.
Located in packages/worker, these agents run asynchronously to handle heavy computational tasks.
- Type: BullMQ Worker (
transcription-jobs) - Source:
packages/worker/src/jobs/transcriptionProcessor.ts - Responsibility: "The Ears" (Now with Diarization)
- Workflow:
- Receives
sessionIdandnumSpeakersfrom the API after file upload. - Fetches audio path from SQLite.
- Submits audio to the WhisperX Service (
packages/whisper) and polls for completion. - Post-Processing:
- The pipeline runs 4 stages: Transcribe (ASR), Align (word-level timestamps), Diarize (speaker identification via Pyannote), and Assign (mapping speakers to text).
- Parses enriched segments into time-stamped paragraphs, splitting on speaker changes.
- Calculates token counts.
- Indexes paragraphs into Elasticsearch (
therascript_transcripts), including thespeakerlabel. - Creates the initial "AI" chat message in the database.
- Receives
- Infrastructure: Depends on GPU availability (via WhisperX Docker container/CUDA) or CPU (int8 quantization). Requires
HF_TOKENfor diarization model weights.
- Type: BullMQ Worker (
analysis-jobs) - Source:
packages/worker/src/jobs/analysisProcessor.ts - Responsibility: "The Analyst" (MapReduce Engine)
- Workflow:
- Receives a
jobIdand a defined Strategy (JSON plan). - Map Phase: Iterates through every selected session transcript.
- Context: The specific session transcript.
- Prompt: The
intermediate_questiondefined by the Strategy Agent. - Output: Stores an
IntermediateSummaryfor that session.
- Reduce Phase: Aggregates all intermediate summaries chronologically.
- Context: The collection of summaries.
- Prompt: The
final_synthesis_instructionsdefined by the Strategy Agent. - Output: Generates the final answer to the user's high-level query.
- Receives a
Located in packages/api, these "meta-agents" configure or summarize data to facilitate the main workflows.
- Type: API Service / LLM Chain
- Source:
packages/api/src/api/analysisHandler.ts - Prompt Template:
system_analysis_strategist(inpackages/db/src/sqliteService.ts) - Responsibility:
- Takes a complex user query (e.g., "How has the patient's anxiety evolved over the last 4 sessions?").
- Generates a JSON Plan containing:
intermediate_question: Instructions for the Analysis Agent to run on individual documents.final_synthesis_instructions: Instructions on how to combine those results.
- Trigger: Called immediately before queuing an Advanced Analysis Job.
- Type: API Background Task
- Source:
packages/api/src/api/analysisHandler.ts - Prompt Template:
system_short_prompt_generator - Responsibility:
- Takes a long, complex user prompt.
- Compresses it into a short (max 5 words) title for the UI list view.
- Example: "Analyze the recurring themes of abandonment..." -> "Abandonment Themes Analysis".
These operate in real-time via the API (packages/api) to facilitate user interaction.
- Type: RAG / Context-Aware Chatbot
- Source:
packages/api/src/api/sessionChatHandler.ts& active model manager. - Prompt Template:
system_prompt - Context:
- Full transcript of the specific session (up to context window limit).
- Recent chat history.
- Capabilities: Can quote the patient/therapist, identify CBT techniques used in the specific text, and clarify transcript ambiguities.
- Memory: Context is re-injected on every turn; state is stored in SQLite (
messagestable). - Cancellation: Model unload on STOP is triggered client-side due to an Elysia 1.2.25 bug; see
docs/DATA_FLOWS.md§6.
- Type: General Purpose Assistant
- Source:
packages/api/src/api/standaloneChatHandler.ts - Prompt Template:
system_standalone_prompt - Context: No transcript context. Pure chat history.
- Capabilities: General questions, brainstorming, or drafting emails/notes without specific session grounding.
- Cancellation: Model unload on STOP is triggered client-side due to an Elysia 1.2.25 bug; see
docs/DATA_FLOWS.md§6.
- Location:
packages/whisper(Python/FastAPI) - Role: Audio-to-Text inference engine.
- Tech: OpenAI Whisper (running on PyTorch/CUDA).
- Location: Native System (
lmsprocess) - Role: LLM Inference Provider.
- Models: Llama 3, Mistral, Gemma (configurable).
- API:
packages/api/src/services/llamaCppService.tsor related active model services manage the context lifecycle.