This document provides a comprehensive overview of all C and C++ source files in the mod_openai FreeSWITCH module, explaining their purpose, functionality, and key contributions to the system.
The mod_openai module is a FreeSWITCH module that enables AI-powered voice conversations, real-time transcription, translation, and text-to-speech capabilities. The module integrates with OpenAI, Azure, and other AI services to provide intelligent voice interaction features.
Lines of Code: 4,623
Purpose: Main FreeSWITCH module implementation and entry point
Key Functionality:
openai
) for voice AI conversationsKey Functions:
openai_execute()
- Main application executionprocess_action()
- Handles AI function calls and SWML executionload_globals()
- Loads module configurationcheck_commands()
- Processes queued commandsLines of Code: 9,690
Purpose: Core AI session utilities and conversation management
Key Functionality:
Key Functions:
ai_session_create()
/ ai_session_destroy()
- Session managementai_conversation_*()
functions - Conversation handlingai_send_text()
- Send text to AI modelslaunch_threads()
/ end_threads()
- Thread managementwait_for_speaking()
- Speech synchronizationLines of Code: 1,145
Purpose: HTTP webhook handling and external communication
Key Functionality:
Key Functions:
send_debug_data()
- Synchronous webhook postingsend_debug_data_async()
- Asynchronous webhook postingopenai_post_data()
- Generic HTTP POST functionalityfire_relay_event()
- Event system integrationLines of Code: 2,767
Purpose: SWAIG (SignalWire AI Gateway) protocol implementation
Key Functionality:
~FN:
, ~LN:
, ~EM:
, ~VL:
patterns)Key Functions:
parse_swaig()
- Parse SWAIG inline commandsexpand_jsonvars()
- JSON variable expansionextract_json()
- JSON extraction from textremove_emoji()
- Emoji processingstrip_markdown()
- Markdown cleaningregex_replace_function()
- Regex utilitiesLines of Code: 2,996
Purpose: Real-time translation and transcription services
Key Functionality:
Key Functions:
openai_transcribe_init()
- Initialize transcriptionopenai_translate_speech_init()
- Initialize translationlive_conversation_*()
functions - Conversation managementtranscribe_callback()
/ translate_callback()
- Media processingprocess_frame()
- Audio frame processingLines of Code: 2,280
Purpose: Text-to-Speech engine implementation
Key Functionality:
Key Functions:
tts_rest_speech_open()
/ tts_rest_speech_close()
- TTS session managementtts_rest_speech_feed_tts()
- Feed text for synthesistts_rest_speech_read_tts()
- Read synthesized audioeleven_labs_voice()
/ playht_voice()
- Voice mappingLines of Code: 475
Purpose: Safe time parsing utilities
Key Functionality:
Key Functions:
parse_time()
- Main time parsing functionget_error_message()
- Error code to message conversionLines of Code: 213
Purpose: Billing and usage tracking
Key Functionality:
Key Functions:
start_wallet_ticks()
/ stop_wallet_ticks()
- Billing controltts_tick()
- TTS usage billingcheck_billing()
- Usage validationget_wallet_tick_json_args()
- Billing data preparationLines of Code: 838
Purpose: Test utilities and validation
Key Functionality:
Key Functions:
test_time()
- Time parsing testsmain()
- Test execution and examplesLines of Code: 993
Purpose: Main header with structure definitions and prototypes
Key Definitions:
ai_session_t
- Core AI session structureopenai_application_t
- Application configuration structureai_conversation_t
- Conversation management structureai_settings_t
- AI model settings structureLines of Code: 137
Purpose: SWAIG utilities and Unicode integration
Key Definitions:
Lines of Code: 214
Purpose: Multi-language token counting using SentencePiece
Key Functionality:
Key Functions:
count_tokens_string()
- Count tokens in textcount_tokens_file()
- Count tokens in filestokens_init()
/ tokens_uninit()
- InitializationLines of Code: 486
Purpose: High-performance UTF-8 validation (Google's algorithm)
Key Functionality:
Key Functions:
IsStructurallyValid()
- UTF-8 validationSpanStructurallyValid()
- Find valid UTF-8 prefixValidUTF8()
- Core validation algorithmLines of Code: 58
Purpose: Phone number formatting using Google libphonenumber
Key Functionality:
Key Functions:
format_number()
- Main number formatting functionThe mod_openai codebase is a comprehensive implementation of AI-powered voice communication features for FreeSWITCH. The files are well-organized with clear separation of concerns:
The architecture supports real-time AI conversations, multi-language processing, advanced text-to-speech synthesis, and comprehensive billing integration, making it a full-featured AI communication platform.