OneAlign / web /src /lib /domain.ts
loveaa's picture
Deploy Home Pulse: grounded setup, isolated workspaces and shared HF visual inference
90d354b verified
Raw History Blame Contribute Delete
4.36 kB
import { TEMPLATES, type Template, type Level } from './templates'
import type {MediaSource,Detail,Plan} from './grounded'
export type NotifyMode = 'auto' | 'quiet'
export type Trigger = 'event' | 'interval' | 'window' | 'external'
export interface Observation {
id: string; capturedAt: string; time: string; outcome: string | null; unknown: boolean;
reason?: string; evidence: string | null; latencyMs: number; source: string; sourceName: string;
frameKey: string; sceneKey: string; mediaTime: number; pending?: boolean; feedback?: 'correct' | 'incorrect';
text?:string;details?:Detail[];engineMs?:number;
}
export interface Preference { id: string; at: string; mode: NotifyMode; reason: string }
export interface WatchItem {
id: string; templateId: string; name: string; camera: string; enabled: boolean; grounded: boolean;
outcome: string | null; unknown: boolean; unknownReason?: string; updatedAt: string | null;
evidence: string | null; latencyMs?: number; notifyMode: NotifyMode; observations: Observation[];
createdAt: string; trigger: Trigger; interval: number; windowStart: string; windowEnd: string;
preferences: Preference[]; pending?: { choice: string; count: number; frameKey: string; sceneKey: string };
sceneKey?: string; lastFrameKey?: string; note?: string; effectiveLevel?: Level;
contractId?:string;contractPlan?:Plan;customTemplate?:Template;referenceSource?:MediaSource;memoryId?:string;confirmedAt?:string;outcomeText?:string;
}
export interface Verdict { stateChanged: boolean; notified: boolean; level: Level; text: string; pending: boolean; unknown: boolean; duplicate: boolean; outcome: string | null }
export function templateOf(item: Pick<WatchItem, 'templateId'|'customTemplate'>): Template { return item.customTemplate ?? TEMPLATES.find(t => t.id === item.templateId) ?? TEMPLATES[0] }
export function adjudicate(item: WatchItem, choice: string | null, abstained: boolean, frameKey: string, sceneKey: string): Verdict {
const t = templateOf(item)
const v: Verdict = { stateChanged: false, notified: false, level: 0, text: '', pending: false, unknown: false, duplicate: false, outcome: choice }
if (!item.enabled) return { ...v, text: '事项已暂停' }
if (item.lastFrameKey === frameKey) return { ...v, text: '同一帧已记录', duplicate: true }
if (abstained || !choice || choice === 'unclear' || !(choice in t.labels)) return { ...v, text: '等待更清晰的画面', outcome: null, unknown: true, stateChanged: !item.unknown }
const sameScene = item.sceneKey === sceneKey
if (t.confirmation.includes(choice) && !(sameScene && !item.unknown && item.outcome === choice)) {
const pending = item.pending
if (!pending || pending.choice !== choice || pending.sceneKey !== sceneKey || pending.frameKey === frameKey) return { ...v, pending: true, text: '已获得 1 帧证据,等待下一帧' }
}
const changed = !sameScene || item.unknown || item.outcome !== choice
let level = t.levels[choice] ?? 0
if (t.id === 'package' && choice === 'absent' && (!sameScene || item.outcome !== 'visible')) level = 0
if (item.notifyMode === 'quiet' && level < 3) level = 0
return { ...v, stateChanged: changed, level, notified: changed && level > 0, text: !changed ? '状态保持,安静记录' : level > 0 ? t.labels[choice] : '已更新 · ' + t.labels[choice] }
}
export function withinWindow(start: string, end: string, at = new Date()): boolean {
const current = new Intl.DateTimeFormat('en-GB', { timeZone: 'Asia/Shanghai', hour: '2-digit', minute: '2-digit', hourCycle: 'h23' }).format(at)
return start <= end ? current >= start && current <= end : current >= start || current <= end
}
export function dateKey(at: string | Date = new Date()): string { return new Intl.DateTimeFormat('en-CA', { timeZone: 'Asia/Shanghai', year: 'numeric', month: '2-digit', day: '2-digit' }).format(new Date(at)) }
export function formatTime(at: string | Date = new Date(), seconds = false) { const d = new Date(at); return Number.isNaN(+d) ? String(at) : new Intl.DateTimeFormat('zh-CN', { timeZone: 'Asia/Shanghai', hour: '2-digit', minute: '2-digit', ...(seconds ? { second: '2-digit' as const } : {}), hourCycle: 'h23' }).format(d) }
export const TRIGGERS: Record<Trigger, string> = { event: '事件触发', interval: '间隔观察', window: '时间窗', external: '外部联动' }