Skip to content

Refactor into modular architecture with Supabase integrationf - #46

Open
RomeoJackson199 wants to merge 4 commits into
twilio-samples:mainfrom
RomeoJackson199:claude/improvement-plan-sAjcK
Open

Refactor into modular architecture with Supabase integrationf#46
RomeoJackson199 wants to merge 4 commits into
twilio-samples:mainfrom
RomeoJackson199:claude/improvement-plan-sAjcK

Conversation

@RomeoJackson199

Copy link
Copy Markdown

Summary

This PR refactors the monolithic voice assistant application into a modular, production-ready architecture with support for Supabase backend integration, comprehensive configuration management, and test coverage.

Key Changes

Architecture & Code Organization

  • Extracted modular routes: Split monolithic index.js into separate route modules (health.js, incoming-call.js, media-stream.js) with a central route registry
  • Centralized configuration: Created config.js with environment variable management, validation, and frozen object pattern for immutability
  • Logging infrastructure: Added structured logging via Pino with environment-aware formatting (pretty-print in dev, JSON in production)
  • Tool definitions: Extracted OpenAI function calling tools into tools.js for maintainability

Backend Integration

  • Supabase client service: Implemented supabase-client.js with:
    • Tool execution layer for appointment management (lookup, book, cancel, check availability)
    • Input validation for phone numbers and dates
    • Request timeout handling and error recovery
    • Support for Supabase Edge Functions integration
  • Dynamic greeting: Initial call greeting now performs patient lookup via Supabase to personalize responses

Prompt & System Design

  • System prompt builder: Created prompts.js with dynamic prompt generation including current date and clinic-specific instructions
  • Caller identification: Enhanced incoming call handling to extract and sanitize caller phone number from Twilio

Testing & Quality

  • Comprehensive test suite: Added tests for:
    • Configuration loading and defaults
    • Phone/date validation
    • Tool execution and error handling
    • Incoming call route and TwiML generation
    • System prompt generation
  • Linting: Added ESLint configuration with recommended rules
  • Editor config: Added .editorconfig for consistent formatting

Configuration & Documentation

  • Enhanced .env.example: Documented all configuration options with sensible defaults
  • Updated package.json: Added npm scripts (start, dev, lint, test) and updated description
  • Version bump: Updated to v2.0.0 reflecting significant refactoring

Media Stream Improvements

  • Reconnection logic: Added exponential backoff retry mechanism for OpenAI WebSocket failures
  • Enhanced event handling: Improved logging and error handling for OpenAI Realtime API events
  • Tool call integration: Seamless integration with Supabase backend for function execution

Notable Implementation Details

  • Configuration uses frozen objects to prevent accidental mutations
  • Phone number validation supports international formats with flexible formatting
  • Tool argument validation happens before backend calls to fail fast
  • Supabase requests include configurable timeouts with AbortController
  • Caller phone number is passed through the entire call lifecycle for context
  • Graceful shutdown handling for clean server termination

https://claude.ai/code/session_01Se6xgYt2AtSNkYkwAjVK7E

Romeo Julian B Jackson and others added 4 commits February 3, 2026 17:36
- Added 5 function tools: lookup_patient, check_availability, book_appointment, cancel_appointment, get_patient_appointments
- Integrated with DentiBot voice-call-ai Edge Function
- Patient lookup on call start using caller ID
- Updated system prompt for dental receptionist workflow
- Added Supabase environment variables
… testing

- Extract config, logging, tools, prompts, routes, and services into src/ modules
- Add structured logging with pino (Fastify-native) replacing console.log
- Add input validation for phone numbers, dates, and TwiML injection prevention
- Add OpenAI WebSocket reconnection logic (up to 2 retries with backoff)
- Add Supabase fetch timeout via AbortController (configurable)
- Add graceful shutdown on SIGTERM/SIGINT
- Add safer JSON parsing with error recovery for function call arguments
- Add ESLint config, .editorconfig, and npm scripts (start, dev, lint, test)
- Add 27 tests using Node.js built-in test runner (node:test)
- Make all hardcoded values configurable via environment variables

https://claude.ai/code/session_01Se6xgYt2AtSNkYkwAjVK7E
@RomeoJackson199

Copy link
Copy Markdown
Author

g

@RomeoJackson199
RomeoJackson199 marked this pull request as draft February 14, 2026 12:18
@RomeoJackson199
RomeoJackson199 marked this pull request as ready for review February 14, 2026 12:18
@RomeoJackson199
RomeoJackson199 marked this pull request as draft February 14, 2026 12:20
@RomeoJackson199 RomeoJackson199 changed the title Refactor into modular architecture with Supabase integration Refactor into modular architecture with Supabase integrationf Feb 14, 2026
@RomeoJackson199
RomeoJackson199 marked this pull request as ready for review February 14, 2026 12:21

@RomeoJackson199 RomeoJackson199 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants