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.
This commit is contained in:
Wolfgang Nagele 2026-11-03 15:30:04 -05:00
parent 2526dd458d
commit 7438fd1065
53 changed files with 1827 additions and 170 deletions

View file

@ -1,7 +1,10 @@
/* ================= AGENT API: the facade =================
window.TUBES.call(tool, args) is the whole surface: one tool from API_TOOLS by name, with the MCP tool's own
arguments, answered as plain JSON. Nothing here listens, polls or runs on the day loop: toast() is only listened to
while a call runs. */
arguments, answered as plain JSON (or a promise of it, for days that run where the player can watch). Nothing here
listens, polls or runs on the day loop: toast() is only listened to while a call runs.
ctx.relay: the call came over the relay into the player's tab (80-relay.js), where the player owns the game, its
clock and their windows, and the dice stay hidden. */
let API_CTX={relay:false};
const API_TOASTS={depth:0,list:[],real:null};
function apiTake(){const t=API_TOASTS.list;API_TOASTS.list=[];return t;}
const apiToastLines=t=>t.map(x=>(x.kind?x.kind+': ':'')+x.text);
@ -32,12 +35,20 @@ const API_TOOLS={
panel:a=>apiPanel(a),
click:a=>apiClick(a.button,a.value),
open_link:a=>apiOpenLink(a.from,a.to),
advance:a=>apiAdvance(a.days),
advance:a=>API_CTX.relay?apiAdvanceLive(a.days):apiAdvance(a.days),
clock:()=>apiClock(),
since:a=>apiSince(a),
};
function apiCall(tool,args){
function apiCall(tool,args,ctx){
if(typeof tool!=='string'||!own(API_TOOLS,tool))apiFail(`No tool "${tool}".`);
if(!S&&!['schema','catalog','classes','actions'].includes(tool))apiFail('No game is running.');
if(!S&&!['schema','catalog','classes','actions','clock'].includes(tool))apiFail('No game is running.');
const was=API_CTX;API_CTX={relay:!!(ctx&&ctx.relay)};
apiListen(true);
try{return API_TOOLS[tool](args&&typeof args==='object'&&!Array.isArray(args)?args:{});}
finally{apiListen(false);}}
let out;
try{out=API_TOOLS[tool](args&&typeof args==='object'&&!Array.isArray(args)?args:{});}
catch(e){apiListen(false);throw e;}
finally{API_CTX=was;}
if(out&&typeof out.then==='function')return out.finally(()=>apiListen(false));
apiListen(false);
return out;}
window.TUBES={version:1,tools:Object.keys(API_TOOLS),call:apiCall};