從 local-tool 複製出獨立的「visionA Agent」桌面應用(A3 純橋樑: tunnel client + 配對 UI + 設定,不開 HTTP port、不做本機裝置/推論 UI)。 Bundle ID 與 local-tool 不同(com.innovedus.visiona-agent vs visiona-local), 雙 app 可共存。fork 後不主動 sync,需要時手動 cherry-pick。 Backend / Wails Go(AB1-AB13): - internal/tunnel:6 狀態機(Idle/Connecting/Connected/Reconnecting/Failed/Stopped) + Pair/Unpair/Reconnect/Disconnect binding + ClientHooks event - internal/auth:encrypted file token store(AES-GCM + scrypt + machineID fallback salt + 13 tests) - internal/config:YAML validation + atomic write + 11 tests - internal/log:ring buffer + ExportLog 升級 zip - visionA-backend /api/pairing/exchange:SessionTokenStore + 17 new tests - 三平台 build 驗證(macOS DMG 160 MB / Windows EXE / Linux AppImage) - end-to-end 5 milestone 全綠(pairing → tunnel → forward → reuse 防護 → tunnel drop failover) Frontend / Next.js(AF1-AF7,沿用 visionA-frontend 基礎): - AppShell + Header + TabNav(StatusView / PairView / SettingsView 三 tab) - ConnectionStatusBadge 5 種狀態 - TokenInput regex 驗證 + 7 種錯誤 + 0.5s auto-switch 到狀態頁 - 設定頁 4 區塊(含重新配對 AlertDialog) - agent-api.ts 封裝 Wails bindings(mock/real 雙實作)+ 90 tests Phase 0.7 review-driven fix(Round 2): - A1 Session fixation 防護(RotateSessionID) - A3 mock pairing 預設改 false(必須明確 opt-in)+ startup log - A4 Pair 失敗後 state 清理矩陣(exchange/Save/Start fail 各自終態) - A5 Pair/Unpair/Reconnect lifecycleMu + 50 goroutine race test - F1 重新配對次按鈕 / F2 PairView Esc cancel / F3 Wails BrowserOpenURL / F4 Settings draft 持久 + 未儲存 badge 驗證:agent backend go test -race -count=3 ./... 4 packages 全綠 / agent frontend pnpm test 119 tests 全綠 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
158 lines
4.3 KiB
Go
158 lines
4.3 KiB
Go
package flash
|
||
|
||
import (
|
||
"fmt"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
"time"
|
||
|
||
"visiona-agent/server/internal/device"
|
||
"visiona-agent/server/internal/driver"
|
||
"visiona-agent/server/internal/model"
|
||
)
|
||
|
||
func isCompatible(modelHardware []string, deviceType string) bool {
|
||
dt := strings.ToUpper(deviceType)
|
||
for _, hw := range modelHardware {
|
||
if strings.ToUpper(hw) == dt || strings.Contains(dt, strings.ToUpper(hw)) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
func resolveModelPath(filePath string, deviceType string) string {
|
||
if filePath == "" {
|
||
return filePath
|
||
}
|
||
|
||
targetChip := ""
|
||
if strings.Contains(strings.ToLower(deviceType), "kl720") {
|
||
targetChip = "kl720"
|
||
} else if strings.Contains(strings.ToLower(deviceType), "kl520") {
|
||
targetChip = "kl520"
|
||
}
|
||
if targetChip == "" {
|
||
return filePath
|
||
}
|
||
|
||
if strings.Contains(filePath, "/"+targetChip+"/") {
|
||
return filePath
|
||
}
|
||
|
||
dir := filepath.Dir(filePath)
|
||
base := filepath.Base(filePath)
|
||
|
||
sourceChip := ""
|
||
if strings.Contains(dir, "kl520") {
|
||
sourceChip = "kl520"
|
||
} else if strings.Contains(dir, "kl720") {
|
||
sourceChip = "kl720"
|
||
}
|
||
|
||
if sourceChip != "" && sourceChip != targetChip {
|
||
newDir := strings.Replace(dir, sourceChip, targetChip, 1)
|
||
newBase := strings.Replace(base, sourceChip, targetChip, 1)
|
||
candidate := filepath.Join(newDir, newBase)
|
||
if _, err := os.Stat(candidate); err == nil {
|
||
return candidate
|
||
}
|
||
}
|
||
|
||
return filePath
|
||
}
|
||
|
||
type Service struct {
|
||
deviceMgr *device.Manager
|
||
modelRepo *model.Repository
|
||
dataDir string
|
||
tracker *ProgressTracker
|
||
}
|
||
|
||
func NewService(deviceMgr *device.Manager, modelRepo *model.Repository, dataDir string) *Service {
|
||
return &Service{
|
||
deviceMgr: deviceMgr,
|
||
modelRepo: modelRepo,
|
||
dataDir: dataDir,
|
||
tracker: NewProgressTracker(),
|
||
}
|
||
}
|
||
|
||
// CleanupTask 清除已完成的 flash task(由 handler goroutine 在讀取完 progressCh 後呼叫)。
|
||
func (s *Service) CleanupTask(taskID string) {
|
||
s.tracker.Remove(taskID)
|
||
}
|
||
|
||
func (s *Service) StartFlash(deviceID, modelID string) (string, <-chan driver.FlashProgress, error) {
|
||
session, err := s.deviceMgr.GetDevice(deviceID)
|
||
if err != nil {
|
||
return "", nil, fmt.Errorf("device not found: %w", err)
|
||
}
|
||
if !session.Driver.IsConnected() {
|
||
return "", nil, fmt.Errorf("device not connected")
|
||
}
|
||
|
||
m, err := s.modelRepo.GetByID(modelID)
|
||
if err != nil {
|
||
return "", nil, fmt.Errorf("model not found: %w", err)
|
||
}
|
||
|
||
deviceInfo := session.Driver.Info()
|
||
if !isCompatible(m.SupportedHardware, deviceInfo.Type) {
|
||
return "", nil, fmt.Errorf("model not compatible with device type %s", deviceInfo.Type)
|
||
}
|
||
|
||
modelPath := m.FilePath
|
||
if modelPath == "" {
|
||
return "", nil, fmt.Errorf("model %s has no .nef file path", modelID)
|
||
}
|
||
|
||
// models.json 的 filePath 是相對路徑(例如 "data/nef/kl520/xxx.nef")。
|
||
// 如果不是絕對路徑,用 dataDir 解析:
|
||
// "data/nef/..." → 去掉 "data/" 前綴 → dataDir + "/nef/..."
|
||
// 其他相對路徑 → dataDir + "/" + filePath
|
||
if !filepath.IsAbs(modelPath) {
|
||
if strings.HasPrefix(modelPath, "data/") || strings.HasPrefix(modelPath, "data\\") {
|
||
modelPath = filepath.Join(s.dataDir, modelPath[len("data/"):])
|
||
} else {
|
||
modelPath = filepath.Join(s.dataDir, modelPath)
|
||
}
|
||
}
|
||
|
||
modelPath = resolveModelPath(modelPath, deviceInfo.Type)
|
||
|
||
taskID := fmt.Sprintf("flash-%s-%s", deviceID, modelID)
|
||
|
||
// M3 fix: 防止同裝置同模型重複 flash
|
||
task := s.tracker.Create(taskID, deviceID, modelID)
|
||
if task == nil {
|
||
return "", nil, fmt.Errorf("flash already in progress for device %s model %s", deviceID, modelID)
|
||
}
|
||
|
||
go func() {
|
||
// M1 fix: 先跑 driver.Flash,收集 error,最後才寫 error message + close channel。
|
||
// driver.Flash 內部會多次寫入 task.ProgressCh(進度更新),我們不能在它還在寫的時候 close。
|
||
// driver.Flash 返回時保證不會再寫入 progressCh。
|
||
|
||
time.Sleep(500 * time.Millisecond)
|
||
|
||
flashErr := session.Driver.Flash(modelPath, task.ProgressCh)
|
||
|
||
// Flash 完成或失敗後,driver 不會再寫 progressCh,安全地寫 error 訊息然後 close。
|
||
if flashErr != nil {
|
||
task.ProgressCh <- driver.FlashProgress{
|
||
Percent: -1,
|
||
Stage: "error",
|
||
Error: flashErr.Error(),
|
||
}
|
||
}
|
||
|
||
task.Done = true
|
||
close(task.ProgressCh)
|
||
// M2 note: 不在這裡 Remove — 讓 handler 讀完 progressCh 後呼叫 CleanupTask
|
||
}()
|
||
|
||
return taskID, task.ProgressCh, nil
|
||
}
|