// visionA Local — splash / bootstrap // 職責:顯示 app 啟動進度 → server 就緒後跳轉到 Next.js 主 UI import { GetServerStatus, GetServerURL, GetBootstrapStatus } from './wailsjs/go/main/App.js'; const statusEl = document.getElementById('status'); const errorEl = document.getElementById('error'); const POLL_INTERVAL_MS = 400; // 首次啟動最長容忍時間:venv 解壓(10s) + 建 venv(5s) + pip install wheels(30-60s) + // libwdi driver install with UAC(15-30s) + server spawn(3s) + health check(2s) ≈ 60-110s // 給到 240 秒以涵蓋慢速硬碟 / UAC 被使用者拖延的情況 const MAX_WAIT_MS = 240_000; const startTime = Date.now(); let lastStatus = ''; async function poll() { const elapsed = Date.now() - startTime; if (elapsed > MAX_WAIT_MS) { showError( `啟動逾時(${Math.round(MAX_WAIT_MS / 1000)} 秒)。\n` + '請關閉此視窗並重新啟動應用程式,或查看 log:\n' + '%APPDATA%\\visiona-local\\logs\\wails.log' ); return; } try { // 先更新 bootstrap 進度文字(venv / pip / driver / server...) const bootstrapMsg = await GetBootstrapStatus(); if (bootstrapMsg && bootstrapMsg !== lastStatus) { statusEl.textContent = bootstrapMsg; lastStatus = bootstrapMsg; } // 檢查 server 是否已就緒 const status = await GetServerStatus(); if (status && status.lastError) { showError('伺服器啟動失敗:' + status.lastError); return; } if (status && status.running && status.url) { statusEl.textContent = '載入主介面...'; window.location.replace(status.url + '/'); return; } // 備用:直接問 URL const url = await GetServerURL(); if (url) { statusEl.textContent = '載入主介面...'; window.location.replace(url + '/'); return; } } catch (e) { // binding 尚未就緒時會 throw,繼續輪詢 } setTimeout(poll, POLL_INTERVAL_MS); } function showError(msg) { statusEl.hidden = true; errorEl.textContent = msg; errorEl.hidden = false; } // 等 Wails runtime 就緒再開始輪詢 if (window.runtime) { poll(); } else { window.addEventListener('load', () => setTimeout(poll, 200)); }