import {localeTag} from '../lib/i18n'; import { tr } from '../lib/i18n'; import { useRef, useState } from 'react'; import { useNative } from '../lib/native'; import { ReferenceSpark } from './charts'; import { Screen } from './layout'; import { IconBtn, Select } from './ui'; import { monitorPng } from '../lib/snapshot'; import { SnapshotDialog } from './SnapshotDialog'; const number = (n: any, d = 1) => typeof n === 'number' && Number.isFinite(n) ? n.toLocaleString(localeTag(), { maximumFractionDigits: d }) : '—'; export function NativeMonitor() { const native = useNative(); const [selection, setSelection] = useState('all'), [snapshot, setSnapshot] = useState(''), [open, setOpen] = useState(false), [saving, setSaving] = useState(false), [error, setError] = useState(''); const capture = useRef(null); const instances = native?.instances ?? [], selected = instances.find(i => i.id === selection) ?? (instances.length === 1 ? instances[0] : null), rows = selected ? [selected] : instances; const devices = new Map>(); for (const i of instances) for (const gpu of i.metrics?.hardware?.gpus ?? []) devices.set(gpu.index, gpu); for (const i of instances) { const h = i.metrics?.hardware; if (!h || h.gpus?.length) continue; const index = i.gpus.length === 1 ? i.gpus[0] : native?.hardware?.gpus.length === 1 ? native.hardware.gpus[0].index : null; if (index != null && !devices.has(index)) devices.set(index, { util: h.gpu_util, mem_used: h.gpu_mem_used, temp: h.gpu_temp, power: h.gpu_power, mem_total: h.gpu_mem_total }); } const hardware = instances.find(i => i.metrics)?.metrics?.hardware; const toolServers = rows.flatMap(i => (i.metrics?.mcp?.servers ?? []).map((s: any) => ({ ...s, model: i.label, instanceId: i.id }))); const requests = rows.flatMap(i => (i.metrics?.requests ?? []).map((r: any) => ({ ...r, model: i.apiModelId }))).sort((a, b) => b.time - a.time).slice(0, 20); async function save() { setOpen(true); setSaving(true); setError(''); try { if (capture.current) setSnapshot(await monitorPng(capture.current)); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setSaving(false); } } const detail = selected?.metrics, live = detail?.live, last = detail?.requests?.[0]; const context = live && live.state !== 'idle' ? (live.prompt_tokens ?? 0) + (live.generated ?? 0) : (last?.prompt_tokens ?? 0) + (last?.output_tokens ?? 0); return 1 ?