okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-023-int into f-023-guifcb0c2d0e

main
4.2 KiB42 linesraw
1import type { MonitorState, Session } from './types';
2
3export function chatMetrics(session:Session|null, monitor:MonitorState|null, configuredContext?:number, now:number=Date.now()) {
4  const messages=session?.messages??[];
5  const replies=messages.filter(m=>m.role==='assistant'&&!m.timeSkipId);
6  const latest=[...replies].reverse().find(m=>m.inputTokens!==undefined);
7  const remote=replies.at(-1)?.remoteModel===true;
8  const working=replies.some(m=>m.streaming);
9  // The Monitor's reading belongs to this chat when it names it, and also while this chat is the one working: a goal or an agent runs under its own
10  // session ids, so matching on the chat id alone left the numbers frozen during a goal.
11  const active=!remote&&!!session&&!!monitor&&(monitor.context?.sessionId===session.id||(working&&['reading','thinking','generating'].includes(monitor.phase)));
12  // One source: what the context held after the last reply (the engine's own count for that request); the monitor's live figure only while a reply streams.
13  const finished=latest?(latest.contextUsed??latest.inputTokens!+(latest.tokens??0)):null;
14  const liveUsed=active&&Number.isFinite(monitor!.context.usedTokens)&&monitor!.context.usedTokens>0?monitor!.context.usedTokens:null;
15  const live=replies.some(m=>m.streaming)||(active&&['reading','thinking','generating'].includes(monitor!.phase));
16  // While a turn runs, its own figure comes first: the host reports what the context holds after each model request of the turn. The engine's live
17  // figure belongs to the request in flight (a small one between two tools read as 1%), and `finished` to an earlier reply.
18  const turnUsed=replies.find(m=>m.streaming&&Number.isFinite(m.contextUsed)&&m.contextUsed!>0)?.contextUsed??null;
19  const used=live?(turnUsed??liveUsed??finished):(finished??liveUsed);
20  const max=remote?(replies.at(-1)?.contextMax||null):configuredContext??(active?monitor!.context.maxTokens:latest?.contextMax??null);
21  const streaming=replies.find(m=>m.streaming);
22  // Answer speed: everything the model generated over its decode time. While a reply streams it is that reply's; after it, the LAST reply's own
23  // (never an older reply's number under the words "last reply").
24  const lastReply=streaming??replies.at(-1);
25  const speed=lastReply?.tps??null,promptSpeed=lastReply?.promptTps??null;
26  const ttfts=replies.flatMap(m=>m.ttftMs===undefined?[]:[m.ttftMs]);
27  const hasUsage=session?.statsAvailable===true;
28  const llmTotal=replies.reduce((n,m)=>n+(m.durationMs??(m.streaming?Math.max(0,now-(Date.parse(m.at)||now)):0)),0),thinkTotal=replies.reduce((n,m)=>n+(m.reasoningMs??(m.streaming&&m.reasoningStartedAt?Math.max(0,now-m.reasoningStartedAt):0)),0);
29  return {streaming:!!streaming,writeMs:Math.max(0,llmTotal-thinkTotal),estimated:streaming?.usageSource==='estimated',hasReplies:replies.length>0,example:!!session?.showcase||replies.some(m=>m.usageSource==='demo'),
30    speed:active&&['generating','thinking'].includes(monitor?.phase??'')&&monitor!.outputTps>0?monitor!.outputTps:speed,promptSpeed,
31    total:session?.statsAvailable===false?null:hasUsage?session.tokensIn+session.tokensOut+(streaming?.tokens??0)+(streaming?.inputTokens??0):replies.every(m=>m.inputTokens!==undefined&&m.tokens!==undefined)?replies.reduce((n,m)=>n+m.inputTokens!+m.tokens!,0):null,
32    input:hasUsage?session.tokensIn:null,output:hasUsage?session.tokensOut:null,
33    llmMs:replies.reduce((n,m)=>n+(m.durationMs??(m.streaming?Math.max(0,now-(Date.parse(m.at)||now)):0)),0),
34    thinkMs:replies.reduce((n,m)=>n+(m.reasoningMs??(m.streaming&&m.reasoningStartedAt?Math.max(0,now-m.reasoningStartedAt):0)),0),
35    toolMs:replies.reduce((n,m)=>n+(m.tools??[]).reduce((v,t)=>v+(t.durationMs??0),0),0),
36    ttft:ttfts.length?ttfts.reduce((n,v)=>n+v,0)/ttfts.length:null,
37    used,max,pct:used!==null&&max?Math.min(100,100*used/max):null,
38    turns:messages.filter(m=>m.role==='user').length,
39    steps:messages.reduce((n,m)=>n+(m.tools?.length??0),0)};
40}
41export const compactTokens=(n:number|null)=>n===null?'—':new Intl.NumberFormat('en',{notation:'compact',maximumFractionDigits:1}).format(n);
42export function elapsed(ms:number){if(ms<1000)return `${Math.round(ms)} ms`;const sec=Math.round(ms/1000);return sec<60?`${sec}s`:`${Math.floor(sec/60)}m ${sec%60}s`;}