File size: 2,734 Bytes
90d354b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export interface JevAnswer { abstained: boolean; choice: string | null }
export interface JevResponse { answer: JevAnswer; latencyMs: number; inferenceMs?: number; recordId: string }
export interface RequestMeta { kind?: 'grounding' | 'observation'; itemId?: string; itemName?: string; camera?: string; source?: string; sourceName?: string; mediaTime?: number; captureId?: string }
export class ModelError extends Error { recordId: string; latencyMs: number; constructor(message: string, recordId = '', latencyMs = 0) { super(message); this.recordId = recordId; this.latencyMs = latencyMs } }
export async function askJev(instructions: string, criteria: Record<string,string>, image: string, meta: RequestMeta = {}): Promise<JevResponse> {
  const res = await fetch('/api/analyze', { method:'POST', headers: { 'Content-Type':'application/json' }, body:JSON.stringify({ instructions,criteria,image,meta }), signal:AbortSignal.timeout(150000) })
  const data = await res.json()
  if (!res.ok) throw new ModelError(data.error || '请求失败',data.recordId,data.latencyMs)
  return data
}
export async function commitDecision(id: string, data: { stateChanged: boolean; notified: boolean; verdict: string; engineMs: number }) {
  if (!id) return
  const res = await fetch('/api/decision', { method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({id,...data}) })
  if (!res.ok) throw new Error('决策记录未保存')
}
export function captureFrame(video: HTMLVideoElement): string {
  if (video.readyState < 2 || !video.videoWidth) throw new Error('画面尚未就绪,请稍后重试')
  const scale = Math.min(1,640/Math.max(video.videoWidth,video.videoHeight))
  const c = document.createElement('canvas'); c.width = Math.round(video.videoWidth*scale); c.height = Math.round(video.videoHeight*scale)
  c.getContext('2d')!.drawImage(video,0,0,c.width,c.height)
  return c.toDataURL('image/jpeg',.82)
}
export function grabFrameFromClip(src: string, atSec = 1): Promise<string> {
  return new Promise((resolve,reject) => {
    const v = document.createElement('video'); v.muted = true; v.preload = 'auto'
    const timer = setTimeout(() => finish(new Error('画面加载超时')),10000)
    const finish = (error?: Error, result?: string) => { clearTimeout(timer); v.onloadedmetadata = null; v.onseeked = null; v.onerror = null; v.removeAttribute('src'); v.load(); if(error) reject(error); else resolve(result!) }
    v.onerror = () => finish(new Error('画面加载失败'))
    v.onloadedmetadata = () => { v.currentTime = Math.max(.01,Math.min(atSec,v.duration-.05)) }
    v.onseeked = () => { try { finish(undefined,captureFrame(v)) } catch(e) { finish(e as Error) } }
    v.src = src; v.load()
  })
}