2026-11-06 08:28:04 +00:00
async function fetchEvents () {
if ( stream . busy ) {
stream . again = true ;
return ;
}
const key = ` ${ windowKey () } & ${ filterQuery () } ` ;
if ( stream . key !== key ) {
stream . key = key ;
stream . events = [];
stream . entries = [];
stream . last = 0 ;
2026-11-26 08:44:15 +00:00
stream . tag = "" ;
2026-11-06 08:28:04 +00:00
}
stream . busy = true ;
try {
2026-11-26 08:44:15 +00:00
const response = await fetch ( `/api/events? ${ windowQuery () } & ${ filterQuery () } &after= ${ stream . last } ` , { cache : "no-store" , headers : stream . tag ? { "If-None-Match" : stream . tag } : {}});
if ( response . status === 304 ) {
stream . error = "" ;
} else if ( response . ok ) {
const tag = response . headers . get ( "ETag" ) || "" ;
2026-11-06 08:28:04 +00:00
const data = await response . json ();
if ( stream . key === key ) {
const seen = new Set ( stream . entries . map (( entry ) => entry . id ));
stream . events = data . events ;
stream . entries = [... stream . entries , ... data . entries . filter (( entry ) => ! seen . has ( entry . id ))]. sort (( a , b ) => a . ts - b . ts ). slice ( - STREAM_KEEP );
stream . last = data . last ;
stream . sessions = data . sessions ;
2026-11-26 08:44:15 +00:00
stream . tag = tag ;
2026-11-06 08:28:04 +00:00
stream . error = "" ;
}
} else {
stream . error = ` ${ response . status } ${ ( await response . text ()). trim () } ` ;
}
} catch {
ui . offline = true ;
}
stream . busy = false ;
2026-11-26 08:44:15 +00:00
drawStream ( true );
2026-11-06 08:28:04 +00:00
if ( stream . again ) {
stream . again = false ;
fetchEvents ();
}
}
async function expand ( entry , holder , button ) {
button . disabled = true ;
const response = await fetch ( `/api/stream/ ${ entry . ref . entry } ` , { cache : "no-store" });
if ( response . ok ) {
const data = await response . json ();
entry . text = data . text ;
entry . ref . truncated = false ;
holder . textContent = data . text ;
2026-11-28 01:47:41 +00:00
vanish ( button );
2026-11-06 08:28:04 +00:00
} else {
2026-11-08 17:26:51 +00:00
button . textContent = "No longer available" ;
2026-11-06 08:28:04 +00:00
}
}
function focusSub ( session , sub ) {
filter . view = "" ;
filter . saved = "" ;
filter . kinds = new Set ([ "transcript" ]);
filter . waiting = false ;
filter . session = session ;
filter . sub = sub ;
2026-11-08 21:18:55 +00:00
closePopup ();
2026-11-06 08:28:04 +00:00
if ( ui . screen === "stream" && stream . dom ) {
refilter ();
} else {
go ( "stream" , null );
}
}
function inboxOf ( entry ) {
const id = entry . kind === "operator" ? entry . ref . task : entry . ref . id ;
return state . inbox . find (( item ) => item . kind === entry . kind && item . id === id ) || null ;
}
function kindPill ( entry ) {
if ( entry . kind === "radio" ) {
return klass ( entry . part );
}
if ( entry . kind === "transcript" ) {
return pill (( PARTS . find (([ id ]) => id === entry . part ) || [ entry . part , entry . part ])[ 1 ], PART_TONES [ entry . part ] || "neutral" );
}
2026-11-22 15:10:26 +00:00
if ( entry . kind === "operator" && entry . ref . check === "decision" ) {
return pill ( "Decision" , KIND_TONES . decision );
}
2026-11-06 08:28:04 +00:00
return pill (( KINDS . find (([ id ]) => id === entry . kind ) || [ entry . kind , entry . kind ])[ 1 ], KIND_TONES [ entry . kind ] || "neutral" );
}
function extras ( entry ) {
const ref = entry . ref ;
if ( entry . kind === "review" ) {
2026-11-08 17:26:51 +00:00
return [ status ( ref . status ), ref . gate ? pill ( ref . gate === "pass" ? "Gate green" : "Gate red" , ref . gate === "pass" ? "ok" : "bad" ) : null ];
2026-11-06 08:28:04 +00:00
}
if ( entry . kind === "ask" ) {
return [ status ( ref . status ), ... ref . warnings . map (( warning ) => pill ( warning , "bad" ))];
}
if ( entry . kind === "decision" ) {
return [ status ( ref . status ), ref . choice ? pill ( ref . choice , "ok" ) : null ];
}
if ( entry . kind === "cron" ) {
2026-11-08 17:26:51 +00:00
return [ pill ( ref . active ? "active" : "inactive" , ref . active ? "ok" : "neutral" )];
2026-11-06 08:28:04 +00:00
}
2026-11-25 10:30:15 +00:00
if ( entry . kind === "operator" && ref . answered ) {
return [ status ( "decided" ), pill ( ref . answered , "ok" )];
}
2026-11-06 08:28:04 +00:00
if ( entry . kind === "run" || entry . kind === "operator" || entry . kind === "register" ) {
return [ ref . status ? status ( ref . status ) : null ];
}
return [];
}
function body ( entry ) {
if ( entry . kind === "transcript" ) {
const text = h ( "pre" , {}, entry . text );
2026-11-08 17:26:51 +00:00
const more = entry . ref . truncated ? h ( "button" , { type : "button" , class : "btn small ghost" }, "Show full text" ) : null ;
2026-11-06 08:28:04 +00:00
if ( more ) {
more . addEventListener ( "click" , () => expand ( entry , text , more ));
}
return [ text , more ];
}
2026-11-22 15:10:26 +00:00
if ( entry . kind === "operator" && entry . ref . check === "decision" ) {
return [ h ( "p" , { class : "text question" }, revealed ( entry . title )), entry . text !== "-" ? h ( "p" , { class : "text reason" }, revealed ( entry . text )) : null ];
}
2026-11-06 08:28:04 +00:00
if ( entry . kind === "ask" || entry . kind === "operator" ) {
2026-11-26 08:44:15 +00:00
return [ entry . kind === "operator" && entry . text !== "-" ? h ( "p" , { class : "text" }, entry . text ) : null ];
2026-11-06 08:28:04 +00:00
}
if ( entry . kind === "review" ) {
2026-11-26 08:44:15 +00:00
return [ entry . text ? h ( "pre" , { class : "notes" }, entry . text ) : null ];
2026-11-06 08:28:04 +00:00
}
if ( entry . kind === "cron" ) {
const ref = entry . ref ;
2026-11-08 17:26:51 +00:00
const ended = ref . deleted ? [ " · ended " , when ( ref . deleted )] : [];
return [ entry . text ? h ( "pre" , {}, entry . text ) : null , h ( "span" , { class : "muted" }, "created " , when ( ref . created ), ended )];
2026-11-06 08:28:04 +00:00
}
if ( entry . kind === "run" ) {
return [ entry . text ? h ( "pre" , {}, entry . text ) : null ];
}
return [ entry . text ? h ( "p" , { class : "text" }, entry . text ) : null ];
}
2026-11-26 08:44:15 +00:00
/**
* @param {Object} entry a stream entry.
* @param {Object|null} item its waiting inbox item, or null.
* @returns {HTMLElement|null} one code box with the entry's command and diff and the item's actions in its foot; a
* review entry without a waiting item loads its diff on the eye's first click; null for an entry with neither.
*/
function streamCode ( entry , item ) {
const commanded = entry . kind === "ask" || ( entry . kind === "operator" && entry . ref . check !== "decision" );
const late = entry . kind === "review" && ! item ;
const key = `message: ${ entry . ts } / ${ entry . id } ` ;
const opened = late && codeOpened ( key , "preview" );
const diff = item ? diffBlocks ( item ) : opened ? diffBlocks ({ kind : "review" , id : entry . ref . id , diff : 0 }) : null ;
if ( ! commanded && ! diff && ! late ) {
return null ;
}
const text = entry . kind === "ask" ? entry . text : entry . title ;
const where = entry . kind === "ask" ? entry . ref . cwd : entry . ref . directory ;
const missing = late ? h ( "p" , { class : "muted" }, opened ? "No diff stored for this review." : "The eye loads the diff of this review." ) : null ;
return codeBox ( key , "preview" , [ commanded ? h ( "div" , { class : "command" , "data-path" : where || "-" }, h ( "code" , {}, text )) : null , commanded ? h ( "span" , { class : "muted code-where" }, `in ${ dash ( where ) } ` ) : null , diff || missing ],
commanded ? text : "" , item ? actionsFor ( item ) : [], late ? drawStream : null );
}
2026-11-06 08:28:04 +00:00
function eventRow ( entry ) {
2026-11-25 10:30:15 +00:00
const answered = entry . kind === "operator" && entry . ref . answered ;
const item = entry . waiting && ! answered ? inboxOf ( entry ) : null ;
2026-11-26 08:44:15 +00:00
const code = streamCode ( entry , item );
2026-11-22 15:10:26 +00:00
const choice = item ? choiceOf ( item , () => {}) : null ;
2026-11-06 08:28:04 +00:00
const alarm = entry . kind === "radio" && ( entry . part === "mayday" || entry . part === "pan-pan" );
2026-11-26 08:44:15 +00:00
const renamed = entry . now && entry . now !== entry . agent ? h ( "span" , { title : `now: ${ entry . now } ` }, agentLink ( entry . agent )) : agentLink ( entry . agent );
const who = entry . agent ? renamed : entry . session ? h ( "strong" , { title : entry . session }, entry . session . slice ( 0 , 8 )) : null ;
2026-11-13 17:19:39 +00:00
const headline = entry . kind === "transcript" || entry . kind === "ask" || entry . kind === "operator" ? null : h ( "span" , { class : "headline" }, Receipts . headline ( entry ));
2026-11-28 01:47:41 +00:00
const blocked = ( item && item . blocked ) || ( entry . kind === "operator" && entry . ref . blocked ) || ( entry . ref && entry . ref . status === "claude" ? IN_CLAUDE_HINT : "" ) || "" ;
2026-11-25 10:30:15 +00:00
return h ( "li" , { class : `entry k- ${ entry . kind }${ entry . waiting && ! answered ? " waiting" : "" }${ alarm ? " alarm" : "" } ` , "data-ref" : item ? keyOf ( item ) : null , "data-item" : `message: ${ entry . ts } / ${ entry . id } ` , "data-to" : item && item . to ? item . to : null ,
"data-blocked" : blocked ? "" : null , "data-answered" : answered ? "" : null , title : blocked || null },
2026-11-06 08:28:04 +00:00
h ( "div" , { class : "msg-meta" }, when ( entry . ts ), kindPill ( entry ), extras ( entry )),
h ( "div" , {},
2026-11-26 08:44:15 +00:00
h ( "div" , { class : "route" }, who , entry . sub ? h ( "button" , { type : "button" , class : "linkish muted" , on : { click : () => focusSub ( entry . session , entry . sub )}}, `› Subagent ${ entry . sub . slice ( 0 , 12 ) } ` ) : null , entry . kind === "ask" ? h ( "strong" , {}, entry . title ) : null , headline ),
2026-11-22 15:10:26 +00:00
entry . kind === "decision" && choice ? choice : [ body ( entry ), choice ],
2026-11-13 17:19:39 +00:00
entry . kind === "radio" ? readerRow ( entry . ref ) : null ,
2026-11-25 01:22:03 +00:00
blocked ? h ( "p" , { class : "item-blocked" }, icon ( "bi-slash-circle" ), blocked ) : null ,
2026-11-26 08:44:15 +00:00
code ,
2026-11-25 01:22:03 +00:00
item && item . job ? consoleOf ( item . job ) : null ,
2026-11-12 16:13:58 +00:00
item ? feedbackList ( keyOf ( item )) : null ,
2026-11-26 08:44:15 +00:00
item && ! code ? h ( "div" , { class : "line-actions" }, actionsFor ( item )) : null ));
2026-11-06 08:28:04 +00:00
}
2026-11-28 01:47:41 +00:00
const BEHIND_FOOTER = [ "expired" , "stale" ];
2026-11-26 08:44:15 +00:00
/**
2026-11-28 01:47:41 +00:00
* @returns {Object[]} the expired and stale requests the Stream keeps behind its footer line, the newest first; one
* open in Claude is open and stays inline with its "open in Claude" status.
*/
const staleRequests = () => [... state . inbox , ...( state . lapsed || [])]. filter (( item ) => BEHIND_FOOTER . includes ( item . request )). sort (( a , b ) => b . ts - a . ts );
/**
* @param {Object} entry a stream entry.
* @param {Set<string>} behind kind:id of every staleRequests() item.
* @param {Set<string>} told the hub jobs of the failed rows whose operator entry the feed shows.
* @returns {boolean} whether the feed leaves the entry out: an expired or stale request, which only the footer lists,
* and the runner's line of a failed row the feed already shows once.
*/
function streamHides ( entry , behind , told ) {
const ref = entry . ref || {};
return behind . has ( ` ${ entry . kind } : ${ entry . kind === "operator" ? ref . task : ref . id } ` ) || ( entry . kind === "run" && told . has ( ref . job ));
}
/**
* @param {Object[]} items staleRequests().
* @returns {HTMLElement|null} one collapsed line naming how many stale requests wait and the count per kind; opened it
* lists them in place with select all and Delete N. Null without any.
*/
function staleFooter ( items ) {
if ( ! items . length ) {
return null ;
}
const words = BEHIND_FOOTER . map (( kind ) => [ kind , items . filter (( item ) => item . request === kind ). length ]). filter (([, count ]) => count ). map (([ kind , count ]) => ` ${ count } ${ kind } ` ). join ( " · " );
const row = ( item ) => h ( "li" , { class : `entry k- ${ item . kind } ` , "data-ref" : keyOf ( item ), "data-request" : item . request },
h ( "div" , { class : "msg-meta" }, when ( item . ts ), pill ( ITEM_LABELS [ item . kind ], KIND_TONES [ item . kind ]), pill ( item . request , REQUEST_TONES [ item . request ]), item . agent ? agentLink ( item . agent ) : null ),
h ( "p" , { class : "text stale-title" }, revealed ( item . title )));
return h ( "details" , { class : "card stale-footer" , "data-key" : "stale-footer" , open : triage . footer , on : { toggle : ( event ) => {
triage . footer = event . currentTarget . open ;
}}}, h ( "summary" , { title : `Expired and stale requests: ${ words } ; open to select and delete them` }, ` ${ items . length } stale ${ items . length === 1 ? "request" : "requests" } ` , h ( "span" , { class : "muted" }, ` · ${ words } ` )),
triagePicker ( TRIAGE_FOOTER , items ), h ( "ol" , { class : "stale-list" }, items . map ( row )));
}
/**
* @param {boolean} [polled] true from a poll: the rows stay when the shown entries, their inbox items, the stale
* requests and the error are unchanged; any other call redraws them.
2026-11-26 08:44:15 +00:00
*/
function drawStream ( polled ) {
2026-11-06 08:28:04 +00:00
if ( ! stream . dom || ui . screen !== "stream" ) {
return ;
}
2026-11-28 01:47:41 +00:00
const backlog = staleRequests ();
const behind = new Set ( backlog . map (( item ) => ` ${ item . kind } : ${ item . id } ` ));
const told = new Set ( state . inbox . filter (( item ) => item . request === "failed" && ! item . settled ). map (( item ) => item . job . id ));
2026-11-06 08:28:04 +00:00
const floor = live () && ui . preset > 0 ? nowSeconds () - ui . preset : - Infinity ;
2026-11-28 01:47:41 +00:00
const rows = [... stream . events , ... stream . entries . filter (( entry ) => entry . ts >= floor )]. filter (( entry ) => ! streamHides ( entry , behind , told )). sort (( a , b ) => b . ts - a . ts || ( a . id < b . id ? 1 : - 1 ));
2026-11-06 08:28:04 +00:00
const shown = rows . slice ( 0 , FEED_LIMIT );
2026-11-08 17:26:51 +00:00
stream . dom . count . textContent = ` ${ rows . length } entries ${ live () ? " · following live" : "" } ` ;
2026-11-28 01:47:41 +00:00
const footing = backlog . length ? [ backlog . map (( item ) => [ item . kind , item . id , item . request , item . title , item . ts ]), triage . footer , triage . picked . get ( TRIAGE_FOOTER ) || [], ui . busy ] : null ;
const drawn = JSON . stringify ([ shown , shown . map (( entry ) => entry . waiting ? inboxOf ( entry ) : null ), rows . length , stream . error , rows . length ? null : [ stream . busy , activeWaiting ()], footing ]);
2026-11-26 08:44:15 +00:00
if ( polled === true && drawn === stream . dom . drawn ) {
return ;
}
stream . dom . drawn = drawn ;
2026-11-06 08:28:04 +00:00
if ( stream . error ) {
2026-11-28 01:47:41 +00:00
fadeSwap ( stream . dom . list , [ emptyState ( "Stream not readable" , stream . error )]);
2026-11-06 08:28:04 +00:00
return ;
}
2026-11-28 01:47:41 +00:00
fadeSwap ( stream . dom . list , [ rows . length
2026-11-06 08:28:04 +00:00
? h ( "div" , { class : "card flush" }, h ( "ol" , { class : "feed" }, shown . map ( eventRow )),
2026-11-08 17:26:51 +00:00
rows . length > shown . length ? h ( "p" , { class : "footnote" }, ` ${ rows . length - shown . length } older entries hidden; narrow down with a filter or the time range.` ) : null )
2026-11-28 01:47:41 +00:00
: emptyState ( stream . busy ? "Loading …" : "No entries" , activeWaiting () ? "Nothing is waiting for you right now." : "Nothing in the chosen time range and filter." ), staleFooter ( backlog )],
( root , swap ) => keepTyping ( root , () => keepScroll ( root , swap )));
2026-11-06 08:28:04 +00:00
}
function streamScreen () {
2026-11-08 17:26:51 +00:00
const search = h ( "input" , { type : "search" , placeholder : "Search … agent:Name kind:radio since:2h (/)" , value : filter . q , "aria-label" : "Search the stream: words, agent:, kind: and since:" , on : { input : ( event ) => {
2026-11-06 08:28:04 +00:00
filter . q = event . target . value ;
2026-11-06 22:20:06 +00:00
filter . saved = "" ;
2026-11-06 08:28:04 +00:00
window . clearTimeout ( searchTimer );
searchTimer = window . setTimeout ( fetchEvents , 300 );
}}});
const count = h ( "span" , { class : "muted" });
2026-11-08 17:26:51 +00:00
const categories = h ( "div" , { class : "chips" , role : "group" , "aria-label" : "Categories" });
const kinds = h ( "div" , { class : "chips" , role : "group" , "aria-label" : "Kinds" });
const parts = h ( "div" , { class : "chips" , role : "group" , "aria-label" : "Transcript parts" });
const saved = h ( "div" , { class : "chips" , role : "group" , "aria-label" : "Saved views" });
2026-11-06 22:20:06 +00:00
const focus = h ( "div" , { class : "chips" });
2026-11-13 12:51:20 +00:00
const list = h ( "div" , { "data-panel" : "stream" });
2026-11-08 17:26:51 +00:00
const more = h ( "details" , { class : "menu" }, h ( "summary" , named ( "Filter" , "Filter: kinds, transcript parts, saved views, custom time range" ), icon ( "bi-funnel" ), caret ()), h ( "div" , { class : "menu-box end" },
h ( "h3" , {}, "Kinds" ), kinds , h ( "h3" , {}, "Transcript parts" ), parts , h ( "h3" , {}, "Saved views" ), saved , h ( "h3" , {}, "Custom time range" ), customRange ()));
2026-11-06 22:20:06 +00:00
stream . dom = { search , count , categories , kinds , parts , saved , focus , list };
2026-11-06 08:28:04 +00:00
window . setTimeout (() => {
drawFilters ();
drawStream ();
fetchEvents ();
}, 0 );
2026-11-26 08:44:15 +00:00
return h ( "div" , {}, boxHead ( "Stream" , "filters stream-head" , categories , search , more , count ), focus , list );
2026-11-06 08:28:04 +00:00
}