依 R5 五輪決策把 visionA-local 從「Wails 內嵌 Next.js」重構為「Wails
本機伺服器控制台 + 瀏覽器 Web UI」模式(類比 Docker Desktop / Ollama)。
程式碼變動
- M8-1 砍 yt-dlp 全套(後端 resolver / URL handler / 前端 URL tab /
Makefile vendor / installer / bootstrap / CI workflow,-555 行)
- M8-2 砍 Mock 模式全套(driver/mock、mock_camera、Settings runtimeMode、
VISIONA_MOCK 環境變數,-528 行)
- M8-3 ffmpeg 從 GPL 切換到 LGPL 混合方案:Windows/Linux 用 BtbN 現成
LGPL binary,macOS 自 build minimal decoder-only 進 git
(vendor/ffmpeg/macos/ffmpeg 5.7MB + ffprobe 5.6MB,比 GPL 版省 85% 空間)
- M8-4 Wails Server Controller:state machine、log ring buffer 2000 行、
preferences.json atomic write、boot-id、Gin SkipPaths、shutdown 7+1 秒、
notify_*.go 三平台 OS 通知、watchServer 改 Error state 不 os.Exit
- M8-4b 啟動階段管線 R5-E:6 階段進度 event、20s soft / 60s hard timeout、
stage 5/6 skip 規則、sentinel file、RestartStartupSequence 5 步驟
- M8-5 Wails 控制台 vanilla HTML/JS/CSS(9 檔 ~2012 行)取代 M7-B splash:
state 視覺、log panel、startup progress panel、Stage 6 manual CTA
pulse、shutdown modal、Settings、Dark Mode、i18n 中英雙語
- M8-6 上傳影片副檔名擴充(mp4/avi/mov/mpeg/mpg)
- M8-7 Web UI Server Offline Overlay(role=alertdialog + focus trap +
wsEverConnected 容錯 + Page Visibility)
- M8-8 CORS middleware(127.0.0.1/localhost only + suffix attack 防護)+
ws/origin.go 獨立 WebSocket CheckOrigin 避 package cycle
- MAJ-4 server:shutdown-imminent WebSocket broadcast 機制
(/ws/system endpoint + notifyShutdownImminent helper)
- M8-9 Boot-ID + 瀏覽器 tab 自動重連(sessionStorage loop guard)
品質
- ~105+ 新 unit test + race detector (-count=2) 全綠
- 10 個 milestone 全部通過 Reviewer 審查
- 三方 v2 + v2.1 文件(PRD / Design Spec / TDD)+ 交叉互審紀錄
收錄在 .autoflow/
交付前待處理(M8-10)
- 重跑 make payload-macos 把舊 GPL 77MB binary 換成新 LGPL
- 三平台 end-to-end build 驗證
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
97 lines
3.3 KiB
Go
97 lines
3.3 KiB
Go
package main
|
||
|
||
// preferences.go — M8-4:Wails 控制台偏好設定持久化
|
||
//
|
||
// TDD ground truth:
|
||
// - .autoflow/04-architecture/v2/server-lifecycle.md §11「Preferences 持久化」
|
||
// - .autoflow/04-architecture/v2/control-panel.md §4.3(Preferences struct)
|
||
//
|
||
// 檔案位置:<dataDir>/preferences.json
|
||
// 寫入策略:atomic write-rename(tmp → rename),避免 crash 中途造成檔案損毀
|
||
// 讀取失敗策略:fallback 到 DefaultPreferences(),不阻止 app 啟動
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"os"
|
||
"path/filepath"
|
||
"runtime"
|
||
)
|
||
|
||
// Preferences 定義控制台偏好。
|
||
// 對應 TDD v2/control-panel.md §4.3 的 Preferences struct。
|
||
type Preferences struct {
|
||
// AutoOpenBrowser — StartServer 成功後是否自動開瀏覽器。
|
||
// 預設值由 DefaultPreferences() 依 runtime.GOOS 決定:
|
||
// macOS / Windows → true
|
||
// Linux → false (R5-D2:Linux 桌面環境差異大,預設關)
|
||
AutoOpenBrowser bool `json:"autoOpenBrowser"`
|
||
|
||
// Locale — 控制台 UI 的語系覆寫;空字串 → 自動偵測(navigator.language)
|
||
Locale string `json:"locale,omitempty"`
|
||
|
||
// LogRingSize — log panel ring buffer 行數上限。0 → 使用預設 2000。
|
||
LogRingSize int `json:"logRingSize,omitempty"`
|
||
}
|
||
|
||
// DefaultPreferences 回傳平台相關的預設值。
|
||
//
|
||
// R5-D2:Linux 預設關 AutoOpenBrowser;macOS/Windows 預設開。
|
||
func DefaultPreferences() Preferences {
|
||
return Preferences{
|
||
AutoOpenBrowser: runtime.GOOS != "linux",
|
||
Locale: "",
|
||
LogRingSize: 0,
|
||
}
|
||
}
|
||
|
||
// preferencesPath 回傳 preferences.json 的絕對路徑。
|
||
func preferencesPath(dataDir string) string {
|
||
return filepath.Join(dataDir, "preferences.json")
|
||
}
|
||
|
||
// LoadPreferences 讀取 preferences.json。
|
||
//
|
||
// 讀取失敗 / 檔案不存在 / JSON 損毀 → 回傳 DefaultPreferences(),不回 error。
|
||
// (讀取失敗不應阻止 app 啟動;壞檔留在磁碟供使用者 debug。)
|
||
func LoadPreferences(dataDir string) Preferences {
|
||
path := preferencesPath(dataDir)
|
||
data, err := os.ReadFile(path)
|
||
if err != nil {
|
||
// 檔案不存在 / 權限錯誤:用預設
|
||
return DefaultPreferences()
|
||
}
|
||
var p Preferences
|
||
if err := json.Unmarshal(data, &p); err != nil {
|
||
// JSON 壞了:記一行 warning 到 stderr(讓使用者在 log 中看到原因),用預設
|
||
fmt.Fprintf(os.Stderr, "[visiona-local] preferences.json parse failed, using defaults: %v\n", err)
|
||
return DefaultPreferences()
|
||
}
|
||
return p
|
||
}
|
||
|
||
// SavePreferences 把 p 寫入 preferences.json,採 atomic write-rename:
|
||
// 1. 寫到 <path>.tmp
|
||
// 2. os.Rename(tmp, real)
|
||
//
|
||
// Rename 在同一 filesystem 上是 atomic 的,避免寫到一半 crash 造成檔案損毀。
|
||
func SavePreferences(dataDir string, p Preferences) error {
|
||
if err := os.MkdirAll(dataDir, 0o755); err != nil {
|
||
return fmt.Errorf("mkdir data dir: %w", err)
|
||
}
|
||
data, err := json.MarshalIndent(p, "", " ")
|
||
if err != nil {
|
||
return fmt.Errorf("marshal preferences: %w", err)
|
||
}
|
||
realPath := preferencesPath(dataDir)
|
||
tmpPath := realPath + ".tmp"
|
||
if err := os.WriteFile(tmpPath, data, 0o644); err != nil {
|
||
return fmt.Errorf("write tmp: %w", err)
|
||
}
|
||
if err := os.Rename(tmpPath, realPath); err != nil {
|
||
_ = os.Remove(tmpPath) // cleanup 失敗的 tmp
|
||
return fmt.Errorf("rename tmp → real: %w", err)
|
||
}
|
||
return nil
|
||
}
|