File size: 7,591 Bytes
accf76b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
 * UI rendering and DOM manipulation
 */

// WebGPU/Model control getters
function getModelSelect() {
    return document.getElementById('model-select');
}

function getLoadModelBtn() {
    return document.getElementById('load-model-btn');
}

function getLoadingProgress() {
    return document.getElementById('loading-progress');
}

function getProgressFill() {
    return document.getElementById('progress-fill');
}

function getProgressText() {
    return document.getElementById('progress-text');
}

function getModelStatus() {
    return document.getElementById('model-status');
}

function getWebGPUStatus() {
    return document.getElementById('webgpu-status');
}

function getModelSection() {
    return document.getElementById('model-section');
}

function getReloadModelBtn() {
    return document.getElementById('reload-model-btn');
}

function getClearCacheBtn() {
    return document.getElementById('clear-cache-btn');
}

function getCacheInfo() {
    return document.getElementById('cache-info');
}

/**
 * Populate model selector with available models
 * @param {Array} models - Array of model configurations
 */
export function populateModelSelector(models) {
    const modelSelect = getModelSelect();
    if (!modelSelect) return;

    modelSelect.innerHTML = '';
    models.forEach(model => {
        const option = document.createElement('option');
        option.value = model.id;
        const noteText = model.note ? ` - ${model.note}` : '';
        option.textContent = `${model.label} (${model.size}${noteText})`;
        modelSelect.appendChild(option);
    });
}

/**
 * Show/hide model section based on inference mode
 * @param {boolean} show - Whether to show the model section
 */
export function toggleModelSection(show) {
    const modelSection = getModelSection();
    if (modelSection) {
        if (show) {
            modelSection.classList.remove('hidden');
        } else {
            modelSection.classList.add('hidden');
        }
    }
}

/**
 * Update loading progress
 * @param {number} percent - Progress percentage (0-100), or -1 for indeterminate
 */
export function updateLoadingProgress(percent) {
    const progressFill = getProgressFill();
    const progressBar = getLoadingProgress();

    // Handle indeterminate state (percent < 0)
    if (progressBar) {
        if (percent < 0) {
            progressBar.classList.add('indeterminate');
        } else {
            progressBar.classList.remove('indeterminate');
        }
    }

    if (progressFill) {
        progressFill.style.width = percent < 0 ? '30%' : `${percent}%`;
    }
}

/**
 * Show/hide loading progress
 * @param {boolean} show - Whether to show the progress bar
 */
export function showLoadingProgress(show) {
    const loadingProgress = getLoadingProgress();
    if (loadingProgress) {
        loadingProgress.style.display = show ? 'block' : 'none';
    }
}

/**
 * Show/hide the model input wrapper (dropdown + load button)
 * @param {boolean} show - Whether to show the input wrapper
 */
export function showModelInputWrapper(show) {
    const wrapper = document.querySelector('.model-input-wrapper');
    if (wrapper) {
        wrapper.style.display = show ? 'flex' : 'none';
    }
}

/**
 * Update model status display
 * @param {string} message - Status message
 * @param {string} type - Status type ('success', 'error', 'loading', or '')
 */
export function updateModelStatus(message, type = '') {
    const modelStatus = getModelStatus();
    if (modelStatus) {
        modelStatus.textContent = message;
        modelStatus.className = 'model-status';
        if (type) {
            modelStatus.classList.add(type);
        }
        modelStatus.style.display = message ? 'block' : 'none';
    }
}

/**
 * Update WebGPU status display
 * @param {string} message - Status message
 * @param {boolean} available - Whether WebGPU is available
 */
export function updateWebGPUStatus(message, available) {
    const webgpuStatus = getWebGPUStatus();
    if (webgpuStatus) {
        webgpuStatus.textContent = message;
        webgpuStatus.className = 'webgpu-status';
        if (available) {
            webgpuStatus.classList.add('available');
        } else {
            webgpuStatus.classList.add('unavailable');
        }
    }
}

/**
 * Enable/disable model loading button
 * @param {boolean} enabled - Whether to enable the button
 */
export function setLoadModelButtonEnabled(enabled) {
    const loadModelBtn = getLoadModelBtn();
    if (loadModelBtn) {
        loadModelBtn.disabled = !enabled;
    }
}

/**
 * Get selected model ID
 * @returns {string|null}
 */
export function getSelectedModelId() {
    const modelSelect = getModelSelect();
    return modelSelect ? modelSelect.value : null;
}

/**
 * Update button states based on model load status
 * @param {boolean} modelLoaded - Whether the model is loaded
 */
export function updateButtonStates(modelLoaded) {
    const tooltipText = modelLoaded ? '' : 'Load a model first';

    // Update live caption button
    const liveCaptionBtn = document.getElementById('start-live-caption-btn');
    if (liveCaptionBtn) {
        liveCaptionBtn.disabled = !modelLoaded;
        liveCaptionBtn.title = tooltipText;
        // Update button text based on model state
        if (!modelLoaded) {
            liveCaptionBtn.textContent = 'Load Model Below';
        } else {
            liveCaptionBtn.textContent = 'Start';
        }
    }
}

/**
 * Set up event listeners (simplified - only for model loading)
 * @param {Function} onSend - Not used (kept for compatibility)
 * @param {Function} onLoadModel - Callback for load model button
 * @param {Function} onReloadModel - Callback for reload model button
 */
export function setupEventListeners(onSend, onLoadModel, onReloadModel) {
    // Load model button
    const loadModelBtn = getLoadModelBtn();
    if (loadModelBtn && onLoadModel) {
        loadModelBtn.addEventListener('click', onLoadModel);
    }

    // Reload model button
    const reloadModelBtn = getReloadModelBtn();
    if (reloadModelBtn && onReloadModel) {
        reloadModelBtn.addEventListener('click', onReloadModel);
    }
}

/**
 * Update cache info display
 * @param {number} usedBytes - Bytes used by cache
 */
export function updateCacheInfo(usedBytes) {
    const cacheInfoEl = getCacheInfo();
    const clearCacheBtn = getClearCacheBtn();

    if (!cacheInfoEl) return;

    if (usedBytes > 0) {
        const usedMB = usedBytes / 1024 / 1024;
        if (usedMB >= 1000) {
            cacheInfoEl.textContent = `${(usedMB / 1024).toFixed(1)} GB cached`;
        } else if (usedMB >= 1) {
            cacheInfoEl.textContent = `${usedMB.toFixed(0)} MB cached`;
        } else {
            cacheInfoEl.textContent = 'No models cached';
        }
        if (clearCacheBtn) {
            clearCacheBtn.disabled = usedMB < 1;
        }
    } else {
        cacheInfoEl.textContent = 'No models cached';
        if (clearCacheBtn) {
            clearCacheBtn.disabled = true;
        }
    }
}

/**
 * Set up clear cache button handler
 * @param {Function} onClearCache - Callback for clear cache button
 */
export function setupClearCacheHandler(onClearCache) {
    const clearCacheBtn = getClearCacheBtn();
    if (clearCacheBtn && onClearCache) {
        clearCacheBtn.addEventListener('click', onClearCache);
    }
}

/**
 * Set clear cache button text
 * @param {string} text - Button text
 */
export function setClearCacheButtonText(text) {
    const clearCacheBtn = getClearCacheBtn();
    if (clearCacheBtn) {
        clearCacheBtn.textContent = text;
    }
}