seriesoftubes/src/agent/85-ticker.js
Wolfgang Nagele 7438fd1065 Agent mode: an AI agent plays in the player's own tab, with the agent's clock or a running one
The MCP server gains a second session, --browser: the game in the player's tab, reached through the
agent relay npm run dev hosts at /agent (loopback unless --host; Host and Origin checked, no token).
The player turns it on in Settings → Agent API, a section only the development page has, and picks the
clock: a pace of 30 s to 5 min a day that nobody can pause, or the agent's own. Headless gets the
running clock too (--clock realtime --pace MS) and both get a wait tool.

All of it lives in src/agent/ (development page only). The game offers HOOKS, UI.clockLocked,
setSpeed() and runClock(dt), the frame's day block moved out unchanged, and nothing more: the page
players get grows by under 1 KB gzipped and runs exactly as before.
2026-11-03 16:18:43 -05:00

24 lines
2 KiB
JavaScript

/* ================= AGENT MODE: the clock where frames do not run =================
A paced clock runs through the game's own loop (UI.speed is the pace, runClock() the days), but frames stop in a
hidden tab, and the frame runs no days behind an open window. Agent mode keeps time in both: a hidden tab is the
normal place for an agent to drive while the player does something else, and an agent that deliberates over a
window pays for it in days. This ticker covers exactly those moments, under the frame's other conditions, and
leaves every other moment to the frame. Hidden-tab timers are throttled (to 1 Hz, after five minutes to once a
minute in Chrome), so an unthrottled worker posts the ticks; a plain interval is the fallback without Worker.
The agent's clock (pace 0) needs no ticks: its days run when the agent asks. Never under __TUBES_TEST__: tests call
apiAgentTick() by hand. */
const API_TICKER={on:false,worker:null,interval:null,last:0};
// Days for the time since the last tick, if the frame is not running them now.
function apiAgentTick(now=performance.now()){
const dt=API_TICKER.last?Math.max(0,Math.min(1000,now-API_TICKER.last)):0;API_TICKER.last=now;
const behind=document.visibilityState==='hidden'||!$('modal').hidden;
if(behind&&UI.clockLocked&&S&&!S.lost&&UI.speed&&!pzOn())runClock(dt);}
function apiTickerSync(){
const want=UI.clockLocked&&apiAgentMs()>0&&!window.__TUBES_TEST__;
if(want===API_TICKER.on)return;API_TICKER.on=want;API_TICKER.last=0;
if(!want){if(API_TICKER.worker){try{API_TICKER.worker.terminate();}catch(e){}API_TICKER.worker=null;}
if(API_TICKER.interval){clearInterval(API_TICKER.interval);API_TICKER.interval=null;}return;}
try{const url=URL.createObjectURL(new Blob(['setInterval(()=>postMessage(0),250)'],{type:'text/javascript'}));
API_TICKER.worker=new Worker(url);URL.revokeObjectURL(url);
API_TICKER.worker.onmessage=()=>apiAgentTick();}
catch(e){API_TICKER.interval=setInterval(()=>apiAgentTick(),250);}}