從 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>
120 lines
4.4 KiB
Go
120 lines
4.4 KiB
Go
//go:build windows
|
||
|
||
package main
|
||
|
||
import (
|
||
"fmt"
|
||
"os"
|
||
"os/exec"
|
||
"path/filepath"
|
||
"strings"
|
||
"syscall"
|
||
)
|
||
|
||
// configureSysProcAttr 設定子行程的 Windows 特有屬性。
|
||
// CREATE_NO_WINDOW (0x08000000) 讓 server 子行程不彈 console 視窗(小黑窗)。
|
||
// HideWindow 對某些情境也一起加保險。
|
||
func configureSysProcAttr(cmd *exec.Cmd) {
|
||
if cmd.SysProcAttr == nil {
|
||
cmd.SysProcAttr = &syscall.SysProcAttr{}
|
||
}
|
||
cmd.SysProcAttr.HideWindow = true
|
||
cmd.SysProcAttr.CreationFlags |= 0x08000000 // CREATE_NO_WINDOW
|
||
}
|
||
|
||
// platformDataDir 回傳 Windows 的應用程式資料目錄。
|
||
// %APPDATA%\visiona-agent
|
||
func platformDataDir() string {
|
||
appdata := os.Getenv("APPDATA")
|
||
if appdata == "" {
|
||
home, _ := os.UserHomeDir()
|
||
appdata = filepath.Join(home, "AppData", "Roaming")
|
||
}
|
||
return filepath.Join(appdata, appName)
|
||
}
|
||
|
||
// installKneronWinUSBDriver 呼叫 KneronPLUS SDK 的 libwdi wrapper 安裝 WinUSB driver。
|
||
//
|
||
// 為什麼需要:
|
||
// - Kneron USB 裝置預設沒有綁定 WinUSB driver,KneronPLUS SDK 無法打開 handle → connect 失敗(error 28)
|
||
// - inf-based pnputil 安裝需要 .cat 簽章,我們沒有;libwdi 會自己用臨時自簽憑證解決
|
||
//
|
||
// 實作參考 edge-ai-platform installer/platform_windows.go 的 installKneronDriverViaSDK:
|
||
// - 組一段 Python script 呼叫 kp.core.install_driver_for_windows(pid) 對 KL520/KL720/KL720_LEGACY 三種 PID
|
||
// - 用 PowerShell Start-Process -Verb RunAs 提權執行(libwdi 要求 admin)
|
||
// - 結果寫到 temp 檔讓 Go 這邊讀回來
|
||
//
|
||
// 呼叫者:前端「安裝 Kneron USB driver」按鈕 → App.InstallKneronDriver() binding → 這個函式
|
||
// 需要:venv 已建好(ensureBundledPython 完成)且 kp 模組已 import
|
||
func installKneronWinUSBDriver(pythonBin string) error {
|
||
if pythonBin == "" {
|
||
return fmt.Errorf("python interpreter not available — 請確認 bundled Python runtime 已完成初始化")
|
||
}
|
||
if _, err := os.Stat(pythonBin); err != nil {
|
||
return fmt.Errorf("python interpreter not found at %s: %w", pythonBin, err)
|
||
}
|
||
|
||
resultPath := filepath.Join(os.TempDir(), "visiona-agent-driver-result.txt")
|
||
_ = os.Remove(resultPath)
|
||
|
||
// venv 根目錄(python.exe 在 venv\Scripts\python.exe 底下)
|
||
venvRoot := filepath.Dir(filepath.Dir(pythonBin))
|
||
|
||
pyScript := fmt.Sprintf(`
|
||
import sys, os
|
||
result_path = r'%s'
|
||
try:
|
||
# 讓 kp 模組能找到 native DLL
|
||
kp_lib = os.path.join(r'%s', 'Lib', 'site-packages', 'kp', 'lib')
|
||
if os.path.isdir(kp_lib):
|
||
os.environ['PATH'] = kp_lib + ';' + os.environ.get('PATH', '')
|
||
os.add_dll_directory(kp_lib)
|
||
import kp
|
||
results = []
|
||
for pid in [kp.ProductId.KP_DEVICE_KL520, kp.ProductId.KP_DEVICE_KL720, kp.ProductId.KP_DEVICE_KL720_LEGACY]:
|
||
try:
|
||
kp.core.install_driver_for_windows(pid)
|
||
results.append(f"OK: {pid.name}")
|
||
except Exception as e:
|
||
results.append(f"SKIP: {pid.name}: {e}")
|
||
with open(result_path, 'w') as f:
|
||
f.write('\n'.join(results) + '\nDONE\n')
|
||
except ImportError as e:
|
||
with open(result_path, 'w') as f:
|
||
f.write(f'ERROR: kp module not available: {e}\n')
|
||
except Exception as e:
|
||
with open(result_path, 'w') as f:
|
||
f.write(f'ERROR: {e}\n')
|
||
`, resultPath, venvRoot)
|
||
|
||
scriptPath := filepath.Join(os.TempDir(), "visiona-agent-install-usb-driver.py")
|
||
if err := os.WriteFile(scriptPath, []byte(pyScript), 0o644); err != nil {
|
||
return fmt.Errorf("write driver install script: %w", err)
|
||
}
|
||
defer os.Remove(scriptPath)
|
||
|
||
// 用 PowerShell Start-Process -Verb RunAs 觸發 UAC,libwdi 要求 admin
|
||
elevateCmd := fmt.Sprintf(
|
||
`Start-Process -FilePath '%s' -ArgumentList '"%s"' -Verb RunAs -Wait -WindowStyle Hidden`,
|
||
pythonBin, scriptPath,
|
||
)
|
||
cmd := exec.Command("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", elevateCmd)
|
||
configureSysProcAttr(cmd)
|
||
if out, err := cmd.CombinedOutput(); err != nil {
|
||
return fmt.Errorf("driver 安裝需要系統管理員權限:%s", strings.TrimSpace(string(out)))
|
||
}
|
||
|
||
resultData, err := os.ReadFile(resultPath)
|
||
_ = os.Remove(resultPath)
|
||
if err != nil {
|
||
return fmt.Errorf("driver 安裝已執行但無法讀取結果(請在裝置管理員確認,或使用 Zadig 手動安裝 https://zadig.akeo.ie/)")
|
||
}
|
||
|
||
result := strings.TrimSpace(string(resultData))
|
||
if strings.Contains(result, "ERROR:") {
|
||
return fmt.Errorf("driver 安裝失敗:%s(可改用 Zadig 手動安裝 https://zadig.akeo.ie/)", result)
|
||
}
|
||
|
||
return nil
|
||
}
|