fix: make Laya status consistent between Settings and ChannelList (#12)

The model was reported as ready while the AI runtime was actually
unreachable, and the Download action stayed visible afterwards.
Distinguish model-downloaded from runtime-available in
layaStatusPresentation, derive the Download action from the real
combined state, and align Settings and ChannelList rendering on that
single source of truth. Cover the combined status cases with tests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
blomios
2026-10-02 13:31:35 +02:00
parent 726d78e05b
commit d253642dec
6 changed files with 142 additions and 17 deletions

View File

@ -33,6 +33,7 @@ class ChannelList {
this.channelLanguagePreferences = this.loadChannelLanguagePreferences();
this.layaLanguageResults = new Map();
this.languageDetectionNotice = '';
this.languageDetectionFallback = false;
this._languageDetectionRequest = 0;
this.loadCollapsedState();
@ -335,6 +336,7 @@ class ChannelList {
const safeMode = ['algo', 'laya', 'hybrid'].includes(mode) ? mode : 'algo';
this.layaLanguageResults.clear();
this.languageDetectionNotice = '';
this.languageDetectionFallback = false;
if (this.channels.length > 0) {
this.render();
void this.refreshLayaLanguages(safeMode);
@ -346,6 +348,7 @@ class ChannelList {
if (mode === 'algo') {
this.layaLanguageResults.clear();
this.languageDetectionNotice = '';
this.languageDetectionFallback = false;
return;
}
@ -359,6 +362,15 @@ class ChannelList {
});
const results = new Map();
try {
const status = await API.laya.getStatus();
if (!status?.modelPresent || !status?.runtimeAvailable) {
if (requestId !== this._languageDetectionRequest) return;
this.layaLanguageResults.clear();
this.languageDetectionFallback = true;
this.languageDetectionNotice = 'Local AI service is unavailable. Standard channel matching is being used.';
this.render();
return;
}
for (let index = 0; index < descriptors.length; index += 64) {
const response = await API.laya.detectLanguages(descriptors.slice(index, index + 64));
(response.results || []).forEach(result => results.set(String(result.id), result));
@ -366,12 +378,12 @@ class ChannelList {
if (requestId !== this._languageDetectionRequest) return;
this.layaLanguageResults = results;
this.languageDetectionNotice = '';
this.languageDetectionFallback = false;
} catch (err) {
if (requestId !== this._languageDetectionRequest) return;
this.layaLanguageResults.clear();
this.languageDetectionNotice = mode === 'hybrid'
? 'Local AI is unavailable; using standard channel matching.'
: 'Local AI is unavailable. Download or start the model in Settings.';
this.languageDetectionFallback = true;
this.languageDetectionNotice = 'Local AI service is unavailable. Standard channel matching is being used.';
console.warn('[ChannelList] Local AI language detection unavailable:', err.message);
}
this.render();
@ -405,7 +417,9 @@ class ChannelList {
selectedVariants: this.selectedVariants,
preferredLanguage: this.preferredLanguage,
languagePreferences: this.channelLanguagePreferences,
languageMode: window.app?.player?.settings?.languageDetectionMode || 'algo',
languageMode: this.languageDetectionFallback
? 'algo'
: (window.app?.player?.settings?.languageDetectionMode || 'algo'),
languageResults: this.layaLanguageResults,
isFavorite: channel => this.isFavorite(channel.sourceId, channel.id),
isHidden: channel => this.isHidden('channel', channel.sourceId, channel.streamId || channel.id)
@ -420,6 +434,15 @@ class ChannelList {
notice.className = 'laya-language-notice';
notice.setAttribute('role', 'status');
notice.textContent = this.languageDetectionNotice;
const settingsButton = document.createElement('button');
settingsButton.type = 'button';
settingsButton.className = 'btn btn-ghost laya-notice-action';
settingsButton.textContent = 'Open settings';
settingsButton.addEventListener('click', () => {
window.app?.navigateTo?.('settings');
window.app?.pages?.settings?.switchTab?.('player');
});
notice.appendChild(settingsButton);
this.container.appendChild(notice);
}

View File

@ -126,6 +126,8 @@ class SettingsPage {
}
});
document.getElementById('laya-use-standard')?.addEventListener('click', () => this.useStandardLanguageDetection());
document.getElementById('laya-check-again')?.addEventListener('click', () => void this.loadLayaStatus());
document.getElementById('laya-copy-command')?.addEventListener('click', () => void this.copyLayaStartCommand());
document.getElementById('laya-show-details')?.addEventListener('click', () => {
const details = document.getElementById('laya-error-details');
const button = document.getElementById('laya-show-details');
@ -149,11 +151,16 @@ class SettingsPage {
renderLayaStatus(status = {}) {
const statusElement = document.getElementById('laya-model-status');
const modelStateElement = document.getElementById('laya-model-state');
const serviceStateElement = document.getElementById('laya-service-state');
const sizeElement = document.getElementById('laya-model-size');
const messageElement = document.getElementById('laya-model-message');
const downloadButton = document.getElementById('laya-download-model');
const removeButton = document.getElementById('laya-remove-model');
const useStandardButton = document.getElementById('laya-use-standard');
const checkAgainButton = document.getElementById('laya-check-again');
const copyCommandButton = document.getElementById('laya-copy-command');
const startCommand = document.getElementById('laya-start-command');
const progress = document.getElementById('laya-download-progress');
const progressTrack = document.getElementById('laya-progress-track');
const progressBar = document.getElementById('laya-progress-bar');
@ -165,12 +172,17 @@ class SettingsPage {
const mode = this.app.player?.settings?.languageDetectionMode || 'algo';
const presentation = LayaStatusPresentation.presentLayaStatus(status, mode);
statusElement.textContent = presentation.statusLabel;
modelStateElement.textContent = presentation.modelLabel;
serviceStateElement.textContent = presentation.serviceLabel;
const size = status.sizeBytes || status.runtime?.modelSizeBytes;
sizeElement.textContent = size ? this.formatBytes(size) : '';
sizeElement.textContent = size ? `· ${this.formatBytes(size)}` : '';
downloadButton.hidden = !presentation.showDownload;
downloadButton.textContent = presentation.downloadLabel;
removeButton.hidden = !presentation.showRemove;
useStandardButton.hidden = !presentation.showUseStandard;
checkAgainButton.hidden = !presentation.showCheckAgain;
copyCommandButton.hidden = !presentation.showStartCommand;
startCommand.hidden = !presentation.showStartCommand;
downloadButton.disabled = presentation.busy || Boolean(this.layaOperationPending);
removeButton.disabled = presentation.busy || Boolean(this.layaOperationPending);
@ -199,6 +211,7 @@ class SettingsPage {
details.hidden = true;
details.textContent = presentation.errorDetail;
if (presentation.errorMessage) messageElement.textContent = presentation.errorMessage;
else if (presentation.availabilityMessage) messageElement.textContent = presentation.availabilityMessage;
else if (this.layaOperationPending) messageElement.textContent = this.layaOperationPending;
else if (mode !== 'algo' && (!status.modelPresent || !status.runtimeAvailable)) {
messageElement.textContent = 'Local AI is not ready. Download the model and ensure the runtime is running. Live TV will remain available.';
@ -266,6 +279,24 @@ class SettingsPage {
this.renderLayaStatus(this.layaStatus);
}
async copyLayaStartCommand() {
const command = 'docker compose --profile laya up -d';
const button = document.getElementById('laya-copy-command');
try {
await navigator.clipboard.writeText(command);
button.textContent = 'Copied';
} catch {
const code = document.querySelector('#laya-start-command code');
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(code);
selection.removeAllRanges();
selection.addRange(range);
button.textContent = 'Command selected';
}
setTimeout(() => { button.textContent = 'Copy start command'; }, 2000);
}
async initTranscodingSettings() {
// Encoder settings
const hwEncoderSelect = document.getElementById('setting-hw-encoder');

View File

@ -37,11 +37,24 @@
const progressPercent = determined ? Math.max(0, Math.min(100, Math.round(percentValue))) : null;
const error = errorParts(status);
const failed = phase === 'failed';
const modelPresent = status.modelPresent === true;
const runtimeAvailable = status.runtimeAvailable === true;
const ready = modelPresent && runtimeAvailable && !busy && !failed;
const statusLabel = ready
? 'Ready'
: failed || busy
? PHASE_LABELS[phase]
: modelPresent && !runtimeAvailable
? 'Local AI service unavailable'
: !modelPresent
? 'Model not downloaded'
: PHASE_LABELS[phase];
return {
phase,
statusLabel: status.modelPresent && phase === 'idle'
? (status.runtimeAvailable ? 'Model ready' : 'Model downloaded · runtime unavailable')
: PHASE_LABELS[phase],
ready,
statusLabel,
modelLabel: `Model: ${modelPresent ? 'Downloaded' : 'Not downloaded'}`,
serviceLabel: `Local AI service: ${runtimeAvailable ? 'Available' : 'Unavailable'}`,
busy,
showProgress: phase === 'downloading',
progressIndeterminate: phase === 'downloading' && progressPercent === null,
@ -53,9 +66,14 @@
errorMessage: failed ? (error.message || 'The local AI model could not be downloaded.') : '',
errorDetail: failed ? error.detail : '',
downloadLabel: failed ? 'Retry download' : 'Download model',
showDownload: !busy && !status.modelPresent && (phase === 'idle' || failed),
showRemove: !busy && Boolean(status.modelPresent) && !failed,
showUseStandard: failed && mode !== 'algo'
showDownload: !busy && !modelPresent && (phase === 'idle' || failed),
showRemove: !busy && modelPresent,
showUseStandard: failed && mode !== 'algo',
showCheckAgain: !busy && modelPresent && !runtimeAvailable,
showStartCommand: !busy && modelPresent && !runtimeAvailable,
availabilityMessage: modelPresent && !runtimeAvailable
? 'The model is installed, but the Local AI service is not running. Standard channel matching is being used.'
: ''
};
}