visionA/local-agent/server/internal/camera/multi_image_source.go
jim800121chen 3f0175f1a9 feat(local-agent): Phase 0.5 visionA Agent — Wails 桌面 + tunnel client + 配對 UI
從 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>
2026-05-01 11:22:01 +08:00

147 lines
3.8 KiB
Go

package camera
import (
"bytes"
"fmt"
"image"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"strings"
"sync"
)
// BatchImageEntry holds metadata and decoded JPEG data for a single image in a batch.
type BatchImageEntry struct {
Filename string
FilePath string
JpegData []byte
Width int
Height int
}
// MultiImageSource provides sequential JPEG frames from multiple uploaded images.
// It implements FrameSource. The pipeline calls ReadFrame for the current image,
// then Advance to move to the next one. After the last image, ReadFrame returns an error.
type MultiImageSource struct {
images []BatchImageEntry
currentIdx int
mu sync.Mutex
}
// NewMultiImageSource creates a source from multiple file paths.
// Each file is decoded (JPG/PNG) and converted to JPEG in memory.
func NewMultiImageSource(filePaths []string, filenames []string) (*MultiImageSource, error) {
if len(filePaths) != len(filenames) {
return nil, fmt.Errorf("filePaths and filenames length mismatch")
}
entries := make([]BatchImageEntry, 0, len(filePaths))
for i, fp := range filePaths {
entry, err := loadBatchImageEntry(fp, filenames[i])
if err != nil {
// Clean up already-loaded temp files
for _, e := range entries {
os.Remove(e.FilePath)
}
return nil, fmt.Errorf("image %d (%s): %w", i, filenames[i], err)
}
entries = append(entries, entry)
}
return &MultiImageSource{images: entries}, nil
}
func loadBatchImageEntry(filePath, filename string) (BatchImageEntry, error) {
f, err := os.Open(filePath)
if err != nil {
return BatchImageEntry{}, fmt.Errorf("failed to open: %w", err)
}
defer f.Close()
ext := strings.ToLower(filepath.Ext(filePath))
var img image.Image
switch ext {
case ".jpg", ".jpeg":
img, err = jpeg.Decode(f)
case ".png":
img, err = png.Decode(f)
default:
return BatchImageEntry{}, fmt.Errorf("unsupported format: %s", ext)
}
if err != nil {
return BatchImageEntry{}, fmt.Errorf("failed to decode: %w", err)
}
bounds := img.Bounds()
var buf bytes.Buffer
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 90}); err != nil {
return BatchImageEntry{}, fmt.Errorf("failed to encode JPEG: %w", err)
}
return BatchImageEntry{
Filename: filename,
FilePath: filePath,
JpegData: buf.Bytes(),
Width: bounds.Dx(),
Height: bounds.Dy(),
}, nil
}
// ReadFrame returns the current image's JPEG data.
func (s *MultiImageSource) ReadFrame() ([]byte, error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.currentIdx >= len(s.images) {
return nil, fmt.Errorf("all images consumed")
}
return s.images[s.currentIdx].JpegData, nil
}
// Advance moves to the next image. Returns false if no more images remain.
func (s *MultiImageSource) Advance() bool {
s.mu.Lock()
defer s.mu.Unlock()
s.currentIdx++
return s.currentIdx < len(s.images)
}
// CurrentIndex returns the 0-based index of the current image.
func (s *MultiImageSource) CurrentIndex() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.currentIdx
}
// CurrentEntry returns metadata for the current image.
func (s *MultiImageSource) CurrentEntry() BatchImageEntry {
s.mu.Lock()
defer s.mu.Unlock()
return s.images[s.currentIdx]
}
// TotalImages returns the number of images in the batch.
func (s *MultiImageSource) TotalImages() int {
return len(s.images)
}
// GetImageByIndex returns JPEG data for a specific image by index.
func (s *MultiImageSource) GetImageByIndex(index int) ([]byte, error) {
if index < 0 || index >= len(s.images) {
return nil, fmt.Errorf("image index %d out of range [0, %d)", index, len(s.images))
}
return s.images[index].JpegData, nil
}
// Images returns all batch entries.
func (s *MultiImageSource) Images() []BatchImageEntry {
return s.images
}
// Close removes all temporary files.
func (s *MultiImageSource) Close() error {
for _, entry := range s.images {
os.Remove(entry.FilePath)
}
return nil
}