Files
ForgeBase/src/main/coach/visionCapture.ts
T
williamandClaude Sonnet 5 e9690a49f9
CI / quality (push) Failing after 6m22s
feat: add CI/CD, code quality, testing and security hardening
Implements Max_Code_Quality_CICD_Production.md: TypeScript strict mode,
ESLint flat config with type-checking and security plugin, Prettier,
Husky + lint-staged, Vitest with 80%+ coverage (473 tests), Playwright
e2e, Electron security hardening (contextIsolation, CSP, IPC validation),
electron-builder + electron-updater, Dockerfile, Gitea Actions CI/release
workflows, and SonarQube docker-compose setup. Also fixes two real bugs
found while writing tests: unawaited clipboard promises and a mermaid
container ref timing issue in CoachDrawingView.

Fixes lint-staged on Windows/Node 24 by invoking eslint/prettier via
`node <bin>.js` instead of their .cmd shims, which Node's child_process
now refuses to spawn without shell:true.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-29 09:05:21 +01:00

71 lines
2.0 KiB
TypeScript

import { app, desktopCapturer, screen } from 'electron'
import { Worker } from 'node:worker_threads'
import { existsSync, mkdirSync, readdirSync } from 'fs'
import { join } from 'path'
const CAPTURE_INTERVAL_MS = 1000
const ROLLING_WINDOW_MS = 7 * 60 * 60 * 1000
let worker: Worker | null = null
let intervalHandle: ReturnType<typeof setInterval> | null = null
function captureDir(): string {
const dir = join(app.getPath('userData'), 'coach-captures')
if (!existsSync(dir)) mkdirSync(dir, { recursive: true })
return dir
}
function ensureWorker(): Worker {
if (worker) return worker
worker = new Worker(join(__dirname, 'captureWorker.js'))
worker.on('error', (error) => console.error('[coach] erreur worker de capture', error))
return worker
}
async function captureOnce(): Promise<void> {
const display = screen.getPrimaryDisplay()
const sources = await desktopCapturer.getSources({
types: ['screen'],
thumbnailSize: display.size
})
const source = sources[0]
if (!source) return
const png = source.thumbnail.toPNG()
const filePath = join(captureDir(), `capture-${Date.now()}.png`)
ensureWorker().postMessage({ filePath, png })
}
function purgeOldCaptures(): void {
const cutoff = Date.now() - ROLLING_WINDOW_MS
ensureWorker().postMessage({ purgeDir: captureDir(), cutoff })
}
let tickCount = 0
export function startVisionCapture(): void {
if (intervalHandle) return
intervalHandle = setInterval(() => {
tickCount += 1
captureOnce().catch((error) => console.error('[coach] echec capture ecran', error))
if (tickCount % 60 === 0) purgeOldCaptures()
}, CAPTURE_INTERVAL_MS)
}
export function stopVisionCapture(): void {
if (intervalHandle) {
clearInterval(intervalHandle)
intervalHandle = null
}
}
export function getRecentCaptures(count: number): string[] {
const dir = captureDir()
if (!existsSync(dir)) return []
return readdirSync(dir)
.filter((f: string) => f.endsWith('.png'))
.sort()
.slice(-count)
.map((f: string) => join(dir, f))
}