yum-slop/TaSTT
Free self-hosted STT for VRChat.
git clone https://git.yummers.dev/yum-slop/TaSTT
672d0a5
v1.0.0-beta02
1// Import configuration schema 2const CONFIG_FIELDS = window . CONFIG_SCHEMA ; 3 4// Process state tracking 5let isProcessRunning = false ; 6let buttonManager ; 7let loadingOverlay ; 8 9// Auto-save functionality with debouncing 10let saveTimeout ; 11const SAVE_DELAY = 500 ; 12let isSettingValues = false ; 13 14// Console management 15const consoleContent = document . getElementById ( 'console-content' ) ; 16const MAX_CONSOLE_LINES = 512 ; 17let consoleLineCount = 0 ; 18 19// Button management system 20class ButtonManager { 21constructor () { 22this . buttons = { 23start : document . getElementById ( 'start-process' ) , 24stop : document . getElementById ( 'stop-process' ) , 25setupVenv : document . getElementById ( 'setup-venv' ) , 26resetVenv : document . getElementById ( 'reset-venv' ) , 27refreshMicrophones : document . getElementById ( 'refresh-microphones' ) 28} ; 29 30// Initialize button states - process is not running at startup 31this . setProcessStopped () ; 32} 33 34setState ( buttonName , disabled ) { 35const button = this . buttons [ buttonName ] ; 36if ( ! button ) return ; 37 38button . disabled = disabled ; 39} 40 41setProcessRunning () { 42this . setState ( 'start' , true ) ; 43this . setState ( 'stop' , false ) ; 44isProcessRunning = true ; 45} 46 47setProcessStopped () { 48this . setState ( 'start' , false ) ; 49this . setState ( 'stop' , true ) ; 50isProcessRunning = false ; 51} 52 53async withButtonLoading ( buttonName , asyncFn ) { 54this . setState ( buttonName , true ) ; 55try { 56return await asyncFn () ; 57} finally { 58this . setState ( buttonName , false ) ; 59} 60} 61} 62 63// Add loading overlay management 64class LoadingOverlay { 65constructor () { 66this . overlay = document . getElementById ( 'loading-overlay' ) ; 67this . form = document . getElementById ( 'config-form' ) ; 68this . messageElement = this . overlay . querySelector ( 'p' ) ; 69this . defaultMessage = 'Environment setup underway - please wait.' ; 70this . originalStates = new Map () ; // Track original disabled states 71} 72 73show ( message = null ) { 74this . messageElement . textContent = message || this . defaultMessage ; 75this . overlay . classList . remove ( 'hidden' ) ; 76// Disable all form inputs and buttons in the entire left panel 77const leftPanel = this . overlay . parentElement ; 78const inputs = leftPanel . querySelectorAll ( 'input, select, textarea, button' ) ; 79inputs . forEach ( input => { 80// Store original disabled state before disabling 81this . originalStates . set ( input , input . disabled ) ; 82input . disabled = true ; 83input . classList . add ( 'opacity-50' ) ; 84}) ; 85} 86 87hide () { 88this . overlay . classList . add ( 'hidden' ) ; 89// Restore original states of form inputs and buttons 90const leftPanel = this . overlay . parentElement ; 91const inputs = leftPanel . querySelectorAll ( 'input, select, textarea, button' ) ; 92inputs . forEach ( input => { 93// Restore original disabled state 94input . disabled = this . originalStates . get ( input ) || false ; 95input . classList . remove ( 'opacity-50' ) ; 96}) ; 97// Clear the stored states 98this . originalStates . clear () ; 99// Reset to default message 100this . messageElement . textContent = this . defaultMessage ; 101} 102} 103 104// Handle status messages with better color management 105function showStatus ( message , type = 'info' ) { 106const statusEl = document . getElementById ( 'status-message' ) ; 107statusEl . textContent = message ; 108 109// Remove all status classes 110const statusClasses = [ 'hidden' , 'bg-green-100' , 'bg-red-100' , 'bg-blue-100' , 'text-green-800' , 'text-red-800' , 'text-blue-800' ] ; 111statusEl . classList . remove ( ... statusClasses ) ; 112 113// Add appropriate classes based on type 114const typeMap = { 115success : [ 'bg-green-100' , 'text-green-800' ] , 116error : [ 'bg-red-100' , 'text-red-800' ] , 117info : [ 'bg-blue-100' , 'text-blue-800' ] 118} ; 119 120statusEl . classList . add ( ... ( typeMap [ type ] || typeMap . info )) ; 121 122// Also log to console 123appendToConsole ( message , type === 'error' ? 'stderr' : 'info' ) ; 124 125setTimeout (() => statusEl . classList . add ( 'hidden' ) , 5000 ) ; 126} 127 128// Get form values using field mappings 129function getFormValues () { 130const config = {} ; 131 132for ( const [ fieldName , fieldConfig ] of Object . entries ( CONFIG_FIELDS )) { 133const element = document . getElementById ( fieldName ) ; 134if ( ! element ) continue ; 135 136switch ( fieldConfig . type ) { 137case 'boolean' : 138config [ fieldName ] = element . checked ? 1 : 0 ; 139break ; 140case 'number' : 141const numValue = parseInt ( element . value ) ; 142config [ fieldName ] = isNaN ( numValue ) ? fieldConfig . default : numValue ; 143break ; 144case 'text' : 145config [ fieldName ] = element . value || fieldConfig . default ; 146break ; 147default : 148config [ fieldName ] = element . value || fieldConfig . default ; 149} 150} 151 152return config ; 153} 154 155// Set form values using field mappings 156function setFormValues ( config ) { 157isSettingValues = true ; // Disable auto-save temporarily 158 159for ( const [ fieldName , fieldConfig ] of Object . entries ( CONFIG_FIELDS )) { 160const element = document . getElementById ( fieldName ) ; 161if ( ! element ) continue ; 162 163const value = config [ fieldName ] ?? fieldConfig . default ; 164 165switch ( fieldConfig . type ) { 166case 'boolean' : 167element . checked = value === 1 ; 168break ; 169case 'text' : 170element . value = value || '' ; 171break ; 172default : 173element . value = value ; 174} 175} 176 177// Handle use_builtin toggle state 178const useBuiltin = config . use_builtin === 1 ; 179const customChatboxInputs = [ 'block_width' , 'num_blocks' , 'rows' , 'cols' ] ; 180customChatboxInputs . forEach ( inputId => { 181const input = document . getElementById ( inputId ) ; 182if ( input ) { 183input . disabled = useBuiltin ; 184if ( useBuiltin ) { 185input . classList . add ( 'opacity-50' , 'cursor-not-allowed' ) ; 186} else { 187input . classList . remove ( 'opacity-50' , 'cursor-not-allowed' ) ; 188} 189} 190}) ; 191 192// Update volume display 193if ( config . volume !== undefined ) { 194const volumePercent = Math . round ( config . volume ) ; 195document . getElementById ( 'volume-display' ) . textContent = ` ${ volumePercent } %` ; 196} 197 198isSettingValues = false ; // Re-enable auto-save 199} 200 201function appendToConsole ( message , type = 'stdout' ) { 202const timestamp = new Date () . toLocaleTimeString () ; 203const timestampSpan = document . createElement ( 'span' ) ; 204timestampSpan . className = 'console-timestamp' ; 205timestampSpan . textContent = `[ ${ timestamp } ] ` ; 206 207const messageSpan = document . createElement ( 'span' ) ; 208messageSpan . className = `console- ${ type } ` ; 209 210// Redact usernames from paths. 211const redactedMessage = message . replace ( / ([ A - Za - z ] : [ \/\\ ] Users [ \/\\ ])([ ^ \/\\ ] + ) /g , '$1*****' ) ; 212messageSpan . textContent = redactedMessage ; 213 214const lineDiv = document . createElement ( 'div' ) ; 215lineDiv . appendChild ( timestampSpan ) ; 216lineDiv . appendChild ( messageSpan ) ; 217 218consoleContent . appendChild ( lineDiv ) ; 219consoleLineCount ++ ; 220 221// Remove old lines if we exceed the limit 222if ( consoleLineCount > MAX_CONSOLE_LINES ) { 223// Calculate how many lines to remove (remove 10% to avoid frequent trimming) 224const linesToRemove = Math . floor ( MAX_CONSOLE_LINES * 0.1 ) ; 225 226// Remove the oldest lines 227for ( let i = 0 ; i < linesToRemove ; i ++ ) { 228if ( consoleContent . firstChild ) { 229consoleContent . removeChild ( consoleContent . firstChild ) ; 230} 231} 232 233consoleLineCount -= linesToRemove ; 234 235// Add a notice that lines were trimmed 236const trimNotice = document . createElement ( 'div' ) ; 237trimNotice . className = 'console-info' ; 238trimNotice . innerHTML = '<span class="console-timestamp">[System] </span><span class="console-info">... older lines removed to maintain performance ...</span>' ; 239consoleContent . insertBefore ( trimNotice , consoleContent . firstChild ) ; 240} 241 242// Auto-scroll to bottom 243const pythonConsole = document . getElementById ( 'python-console' ) ; 244pythonConsole . scrollTop = pythonConsole . scrollHeight ; 245} 246 247// Async action handler with better error handling 248async function handleAsyncAction ( actionName , actionFn ) { 249try { 250const result = await actionFn () ; 251if ( result ?. message ) { 252showStatus ( result . message , 'success' ) ; 253} 254return result ; 255} catch ( error ) { 256showStatus ( ` ${ actionName } failed: ${ error . message } ` , 'error' ) ; 257throw error ; 258} 259} 260 261async function autoSaveConfig () { 262if ( isSettingValues ) return ; 263 264clearTimeout ( saveTimeout ) ; 265saveTimeout = setTimeout ( async () => { 266try { 267const config = getFormValues () ; 268await window . electronAPI . saveConfig ( config ) ; 269showStatus ( 'Configuration saved' , 'success' ) ; 270 271// Restart process if running 272if ( isProcessRunning ) { 273appendToConsole ( 'Restarting process with new configuration...' , 'info' ) ; 274 275try { 276await window . electronAPI . stopProcess () ; 277await new Promise ( resolve => setTimeout ( resolve , 1000 )) ; 278await window . electronAPI . startProcess () ; 279buttonManager . setProcessRunning () ; 280appendToConsole ( 'Process restarted with new configuration' , 'info' ) ; 281} catch ( error ) { 282appendToConsole ( `Failed to restart process: ${ error . message } ` , 'stderr' ) ; 283buttonManager . setProcessStopped () ; 284} 285} 286} catch ( error ) { 287showStatus ( `Failed to save configuration: ${ error . message } ` , 'error' ) ; 288} 289} , SAVE_DELAY ) ; 290} 291 292// Auto-save setup 293function setupAutoSave () { 294const form = document . getElementById ( 'config-form' ) ; 295const inputs = form . querySelectorAll ( 'input, select, textarea' ) ; 296 297inputs . forEach ( input => { 298const eventType = input . type === 'checkbox' ? 'change' : 299( input . type === 'number' || input . type === 'text' || input . tagName === 'TEXTAREA' ) ? 'input' : 'change' ; 300input . addEventListener ( eventType , autoSaveConfig ) ; 301}) ; 302} 303 304// Microphone loading 305async function loadMicrophones () { 306const microphoneSelect = document . getElementById ( 'microphone' ) ; 307 308try { 309// Check/install requirements during startup 310appendToConsole ( 'Checking virtual environment and requirements...' , 'info' ) ; 311loadingOverlay . show ( 'Setting up environment - this can take several minutes.' ) ; 312try { 313await handleAsyncAction ( 'Install requirements' , () => window . electronAPI . installRequirements ()) ; 314} finally { 315loadingOverlay . hide () ; // Always hide overlay when done 316} 317 318appendToConsole ( 'Loading available microphones...' , 'info' ) ; 319const microphones = await window . electronAPI . getMicrophones () ; 320 321microphoneSelect . innerHTML = '' ; 322 323if ( microphones . length === 0 ) { 324microphoneSelect . innerHTML = '<option value="" disabled>No microphones found</option>' ; 325appendToConsole ( 'No microphones found' , 'stderr' ) ; 326return ; 327} 328 329appendToConsole ( `Found ${ microphones . length } microphone(s)` , 'info' ) ; 330microphones . forEach ( mic => { 331const option = document . createElement ( 'option' ) ; 332option . value = mic . index . toString () ; 333option . textContent = mic . name ; 334microphoneSelect . appendChild ( option ) ; 335appendToConsole ( ` - ${ mic . name } (Device ${ mic . index } )` , 'stdout' ) ; 336}) ; 337 338// Restore previously selected microphone 339try { 340const config = await window . electronAPI . loadConfig () ; 341if ( config . microphone ) { 342microphoneSelect . value = config . microphone ; 343} 344} catch ( error ) { 345// Ignore config load errors here 346} 347 348} catch ( error ) { 349appendToConsole ( `Failed to load microphones: ${ error . message } ` , 'stderr' ) ; 350microphoneSelect . innerHTML = '<option value="" disabled>Error loading microphones</option>' ; 351} 352} 353 354// Event handlers setup 355function setupEventHandlers () { 356// Advanced settings toggle 357document . getElementById ( 'toggle-advanced' ) . addEventListener ( 'click' , () => { 358const advancedSettings = document . getElementById ( 'advanced-settings' ) ; 359const chevron = document . getElementById ( 'chevron' ) ; 360 361if ( advancedSettings . classList . contains ( 'hidden' )) { 362advancedSettings . classList . remove ( 'hidden' ) ; 363chevron . classList . add ( 'rotate-90' ) ; 364} else { 365advancedSettings . classList . add ( 'hidden' ) ; 366chevron . classList . remove ( 'rotate-90' ) ; 367} 368}) ; 369 370// Use builtin chatbox toggle 371document . getElementById ( 'use_builtin' ) . addEventListener ( 'change' , ( e ) => { 372const customChatboxInputs = [ 'block_width' , 'num_blocks' , 'rows' , 'cols' ] ; 373const isBuiltin = e . target . checked ; 374 375customChatboxInputs . forEach ( inputId => { 376const input = document . getElementById ( inputId ) ; 377if ( input ) { 378input . disabled = isBuiltin ; 379if ( isBuiltin ) { 380input . classList . add ( 'opacity-50' , 'cursor-not-allowed' ) ; 381} else { 382input . classList . remove ( 'opacity-50' , 'cursor-not-allowed' ) ; 383} 384} 385}) ; 386}) ; 387 388// Volume slider update 389document . getElementById ( 'volume' ) . addEventListener ( 'input' , ( e ) => { 390const volumePercent = Math . round ( e . target . value ) ; 391document . getElementById ( 'volume-display' ) . textContent = ` ${ volumePercent } %` ; 392}) ; 393 394// Setup virtual environment 395document . getElementById ( 'setup-venv' ) . addEventListener ( 'click' , async () => { 396loadingOverlay . show ( 'Setting up virtual environment - please wait...' ) ; // Show overlay with custom message 397try { 398await buttonManager . withButtonLoading ( 'setupVenv' , async () => { 399await window . electronAPI . deleteVenvIndicatorFile () ; 400await handleAsyncAction ( 'Install requirements' , () => window . electronAPI . installRequirements ()) ; 401}) ; 402} finally { 403loadingOverlay . hide () ; // Always hide overlay when done 404} 405}) ; 406 407// Reset virtual environment 408document . getElementById ( 'reset-venv' ) . addEventListener ( 'click' , async () => { 409loadingOverlay . show ( 'Resetting virtual environment - please wait...' ) ; // Show overlay with custom message 410try { 411await buttonManager . withButtonLoading ( 'resetVenv' , async () => { 412await handleAsyncAction ( 'Reset virtual environment' , () => window . electronAPI . resetVenv ()) ; 413}) ; 414} finally { 415loadingOverlay . hide () ; // Always hide overlay when done 416} 417}) ; 418 419// Reset configuration 420document . getElementById ( 'reset-config' ) . addEventListener ( 'click' , async () => { 421const confirmReset = confirm ( 'Are you sure you want to reset all settings to defaults? This cannot be undone.' ) ; 422if ( ! confirmReset ) return ; 423 424try { 425// Stop process if running 426const wasRunning = isProcessRunning ; 427if ( wasRunning ) { 428appendToConsole ( 'Stopping process before resetting configuration...' , 'info' ) ; 429await window . electronAPI . stopProcess () ; 430buttonManager . setProcessStopped () ; 431await new Promise ( resolve => setTimeout ( resolve , 500 )) ; 432} 433 434// Reset configuration 435appendToConsole ( 'Resetting configuration to defaults...' , 'info' ) ; 436const result = await window . electronAPI . resetConfig () ; 437 438// Reload configuration with defaults 439const config = await window . electronAPI . loadConfig () ; 440setFormValues ( config ) ; 441 442showStatus ( result . message , 'success' ) ; 443appendToConsole ( 'Configuration reset successfully' , 'info' ) ; 444 445// Restart process if it was running 446if ( wasRunning ) { 447appendToConsole ( 'Restarting process with default configuration...' , 'info' ) ; 448await window . electronAPI . startProcess () ; 449buttonManager . setProcessRunning () ; 450appendToConsole ( 'Process restarted with default configuration' , 'info' ) ; 451} 452} catch ( error ) { 453showStatus ( `Failed to reset configuration: ${ error . message } ` , 'error' ) ; 454appendToConsole ( `Failed to reset configuration: ${ error . message } ` , 'stderr' ) ; 455} 456}) ; 457 458// Refresh microphones 459document . getElementById ( 'refresh-microphones' ) . addEventListener ( 'click' , async () => { 460await buttonManager . withButtonLoading ( 'refreshMicrophones' , async () => { 461await loadMicrophones () ; 462}) ; 463}) ; 464 465// Start process 466document . getElementById ( 'start-process' ) . addEventListener ( 'click' , async () => { 467buttonManager . setState ( 'start' , true ) ; 468 469try { 470// The installRequirements function will now check if venv is set up. 471loadingOverlay . show ( 'Verifying environment setup - please wait...' ) ; // Show overlay with custom message 472try { 473await window . electronAPI . installRequirements () ; 474appendToConsole ( 'Virtual environment setup checked/completed' , 'info' ) ; 475} finally { 476loadingOverlay . hide () ; // Always hide overlay when done 477} 478 479await window . electronAPI . startProcess () ; 480buttonManager . setProcessRunning () ; 481appendToConsole ( 'Process started successfully' , 'info' ) ; 482} catch ( error ) { 483appendToConsole ( `Failed to start process: ${ error . message } ` , 'stderr' ) ; 484buttonManager . setState ( 'start' , false ) ; 485} 486}) ; 487 488// Stop process 489document . getElementById ( 'stop-process' ) . addEventListener ( 'click' , async () => { 490buttonManager . setState ( 'stop' , true ) ; 491 492try { 493await window . electronAPI . stopProcess () ; 494appendToConsole ( 'Process stop initiated' , 'info' ) ; 495} catch ( error ) { 496appendToConsole ( `Failed to stop process: ${ error . message } ` , 'stderr' ) ; 497buttonManager . setState ( 'stop' , false ) ; 498} 499}) ; 500 501// Listen for process stopped event 502window . electronAPI . onProcessStopped (() => { 503buttonManager . setProcessStopped () ; 504}) ; 505} 506 507// Initialize application 508window . addEventListener ( 'load' , async () => { 509appendToConsole ( 'TaSTT Configuration UI initialized' , 'info' ) ; 510 511loadingOverlay = new LoadingOverlay () ; 512buttonManager = new ButtonManager () ; 513 514// Set up Python output listener first so we capture all output 515window . electronAPI . onPythonOutput (( data ) => { 516appendToConsole ( data . message , data . type ) ; 517}) ; 518 519// Load configuration 520try { 521const config = await window . electronAPI . loadConfig () ; 522setFormValues ( config ) ; 523appendToConsole ( 'Configuration loaded' , 'info' ) ; 524} catch ( error ) { 525appendToConsole ( `Failed to load configuration: ${ error . message } ` , 'stderr' ) ; 526} 527 528// Load microphones 529await loadMicrophones () ; 530 531// Setup event handlers and auto-save 532setupEventHandlers () ; 533setupAutoSave () ; 534}) ;