okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1import { tr } from '../lib/i18n' ; 2import { useRef , useState } from 'react' ; 3import { Btn , Dialog , type IconName } from './ui' ; 4import { api } from '../lib/client' ; 5import { plainFailureText } from '../lib/plain-errors' ; 6import { compactTokens , elapsed } from '../lib/chat-metrics' ; 7import type { Session } from '../lib/types' ; 8type Agent = NonNullable < Session [ 'agents' ]>[ number ]; 9const states :Record < Agent [ 'status' ], { 10label :string ; 11icon :IconName ; 12}> = { 13working :{ label :'In progress' , icon :'clock' }, completed :{ label :'Completed' , icon :'check' }, 14failed :{ label :'Failed' , icon :'error' }, queued :{ label :'Queued' , icon :'clock' }, cancelled :{ label :'Cancelled' , icon :'x' }, 15}; 16export function SubagentRow ({ agent :a , now= Date . now (), sessionId} :{ 17agent :Agent ; 18now ?:number ; 19/** The chat the helper belongs to: with it the row can take a message for the helper (#4749). */ 20sessionId ?:string ; 21}) { 22const [ log , setLog ] = useState ( false ); 23const [ writing , setWriting ] = useState ( false ), [ draft , setDraft ] = useState ( '' ), [ busy , setBusy ] = useState ( false ), [ sent , setSent ] = useState ( false ), [ problem , setProblem ] = useState ( '' ); 24// A working helper gets it at its next step (steer); one that has finished or waits gets a new turn with it (queue). 25async function send () { 26const message = draft . trim (); 27if ( ! message || ! sessionId || busy ) return ; 28setBusy ( true ); setProblem ( '' ); setSent ( false ); 29try { await api . post ( '/api/chat/helper' , { sessionId, helperId :a . id , message, delivery :a . status === 'working' ?'steer' :'queue' }); setDraft ( '' ); setSent ( true ); } 30catch ( e ) { setProblem ( plainFailureText ( e instanceof Error ?e . message :String ( e )). message ); } 31finally { setBusy ( false ); } 32} 33const observedAt = useRef ( Date . now ()); 34const duration = a . durationMs + ( a . status === 'working' ?Math . max ( 0 , now - ( a . updatedAt ?? observedAt . current )) :0 ); 35const state = states [ a . status ] ?? states . queued ; 36return < div className = "subagent-row" data-status = { a . status }> 37< span className = "subagent-row__dot" aria-label = { tr ( state . label )}/> 38< div className = "subagent-row__copy" >< div className = "subagent-row__title" >< strong title = { a . name }>{ a . name }</ strong >< span className = "subagent-row__status" >{ tr ( state . label )}</ span ></ div >< p title = { a . description }>{ a . description }</ p >{ tr ( a . status === 'failed' ?< button className = "text-action" onClick = {() => setLog ( true )}>{ tr ( "View log" )}</ button > :null )}{ tr ( sessionId && a . status !== 'queued' && a . status !== 'cancelled' ?<>{ writing ?< form className = "subagent-row__reply" onSubmit = { e => { e . preventDefault (); void send (); }}>< input className = "st-input" value = { draft } maxLength = { 8000 } aria-label = { tr ( "Message to {0}" , [ a . name ])} placeholder = { tr ( "Write to {0}\u2026" , [ a . name ])} disabled = { busy } onChange = { e => { setDraft ( e . target . value ); setSent ( false ); }}/>< Btn size = "sm" variant = "primary" type = "submit" disabled = { busy || ! draft . trim ()}>{ tr ( "Send" )}</ Btn ></ form > :< button className = "text-action" onClick = {() => setWriting ( true )}>{ tr ( "Message this helper" )}</ button >}{ tr ( sent ?< p className = "small subagent-row__note" role = "status" >{ tr ( "Sent to {0}." , [ a . name ])}</ p > :null )}{ tr ( problem ?< p className = "small inline-error subagent-row__note" role = "alert" >{ problem }</ p > :null )}</> :null )}</ div > 39< div className = "subagent-row__metrics" >< span >{ tr ( compactTokens ( a . tokens ))}{ tr ( " tok" )}</ span >< span >{ tr ( elapsed ( duration ))}</ span ></ div > 40< Dialog open = { log } title = { tr ( "{0} \u00B7 log" , [ a . name ])} onClose = {() => setLog ( false )} actions = {< Btn onClick = {() => setLog ( false )}>{ tr ( "Done" )}</ Btn >}>< p >{ a . description }</ p >< p className = "small muted" >{ tr ( "Status: " )}{ tr ( state . label )} · { tr ( elapsed ( a . durationMs ))} · { tr ( compactTokens ( a . tokens ))}{ tr ( " tokens" )}</ p ></ Dialog ></ div >; 41}