MCP: the agent facade moves into src/agent/, assembled into the development page only

The command tables and the page-side reads, screens and dispatch (scripts/lib/mcp-commands.js and
mcp-page.js) become src/agent/, the facade window.TUBES.call(tool, args) in the page's own scope.
A new <!-- @include-dev agent --> line in src/index.html pastes it in after the game, but only in the
development page (npm run dev, the MCP server, i18n-check, tests that ask with load({agent:true})).
The build drops the line whole: _site/ is byte for byte what main builds.

Tests load the page players get by default, the build test fails if anything outside src/agent/ names
the facade's api/API_ names, and the pages test checks the built page carries none of it and still
plays. ADR 0004 records the rule.
This commit is contained in:
Wolfgang Nagele 2026-11-03 14:51:42 -05:00
parent b2366ed260
commit 5b76685e5c
27 changed files with 1052 additions and 869 deletions

43
src/agent/90-facade.js Normal file
View file

@ -0,0 +1,43 @@
/* ================= 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. */
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);
// toast() is a top-level function, so the game reaches it by its global name and this stands in for it.
function apiListen(on){
if(on){if(API_TOASTS.depth++)return;const real=API_TOASTS.real=window.toast;API_TOASTS.list=[];
window.toast=function(h,kind){API_TOASTS.list.push({kind:kind||'',text:apiText(String(h),600)});return real.apply(this,arguments);};}
else if(!--API_TOASTS.depth){window.toast=API_TOASTS.real;API_TOASTS.real=null;API_TOASTS.list=[];}}
const API_TOOLS={
status:()=>apiStatus(),
network:a=>apiNetwork(a.network),
city:a=>apiCity(a.city),
find:a=>apiFind(a.query,a.kind),
traceroute:a=>apiTrace(a.from,a.to),
peering_check:a=>apiPeering(a.network,a.city,a.via),
hot_spots:a=>apiHotSpots(a.limit||10),
schema:a=>apiSchema(a.section),
catalog:a=>apiCatalog(a.name),
state:a=>apiState(a.path),
results:a=>apiResults(a.path),
ui:()=>apiUi(),
actions:()=>apiActions(),
classes:()=>apiClasses(),
act:a=>{const name=a.action||a.command;if(!name)apiFail('Give action, e.g. {"action":"buildPop","args":{"city":"ams"}}.');
return apiRun(name,a.args||{},{dryRun:!!a.dryRun});},
screen:a=>apiScreen(a.where,a.offset),
panel:a=>apiPanel(a),
click:a=>apiClick(a.button,a.value),
open_link:a=>apiOpenLink(a.from,a.to),
advance:a=>apiAdvance(a.days),
};
function apiCall(tool,args){
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.');
apiListen(true);
try{return API_TOOLS[tool](args&&typeof args==='object'&&!Array.isArray(args)?args:{});}
finally{apiListen(false);}}
window.TUBES={version:1,tools:Object.keys(API_TOOLS),call:apiCall};