// What the tiles `look` draws show: each call world.mjs names, as a HUD // server answers it at /mcp with no one signed in, recorded into // tools.json beside this, with the DIDs of the people the world names and // their profiles, and what was picked from one answer for another call (a // post for a thread, a document). Recorded once, and again only to change // what the pictures show; `look` itself asks no server. // // node harness/look/record.mjs http://localhost:8790 // node harness/look/record.mjs http://localhost:8790 --only gallery,photo // // With --only, only the calls named (world.mjs's `calls`) are asked again, // each put into tools.json beside what it has already, which stays as it is. import { existsSync, readFileSync, writeFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { root, shown, viewOf } from '../gallery/common.mjs'; import { calls, filled, people, savedFeeds } from './world.mjs'; const here = dirname(fileURLToPath(import.meta.url)); const argv = process.argv.slice(2); const at = argv.indexOf('--only'); const only = at === -1 ? null : (argv.splice(at, 2)[1] ?? '').split(',').filter(Boolean); const [base] = argv; if (!base || only?.length === 0) { console.log('usage: node harness/look/record.mjs [--only ,...]'); process.exit(1); } const unknown = only?.filter((name) => !(name in calls)) ?? []; if (unknown.length) { console.log(`==> no such call: ${unknown.join(', ')}; one of ${Object.keys(calls).join(', ')}`); process.exit(2); } let id = 0; /** A tool's result, as /mcp answers it to no one, kept as `hud resolve` reads one: in place of the `_meta.ui` the server drew, the bundled view that draws it (`_meta.view`), or for records no view draws, their collection's lexicon and their account's handle. */ async function call(name, args) { const res = await fetch(new URL('/mcp', base), { method: 'POST', headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream', 'mcp-protocol-version': '2026-07-28', 'mcp-method': 'tools/call', 'mcp-name': name }, body: JSON.stringify({ jsonrpc: '2.0', id: ++id, method: 'tools/call', params: { name, arguments: args, _meta: { 'io.modelcontextprotocol/protocolVersion': '2026-07-28', 'io.modelcontextprotocol/clientCapabilities': {} } } }) }); const body = await res.json(); if (body.error) throw new Error(`${name}: ${body.error.message}`); const result = body.result; if (result.isError) console.log(`==> ${name} ${JSON.stringify(args)}: ${result.content?.[0]?.text}`); const { ui, ...meta } = result._meta ?? {}; if (!result.isError && name !== 'describe_repo') { const doc = viewOf(name, args); if (doc) meta.view = shown(name, doc); else if (name === 'list_records' || name === 'get_record') { const { collection: nsid, did, handle } = result.structuredContent; const file = join(root, 'lexicons', `${nsid}.json`); meta.lexicon = existsSync(file) ? { nsid, doc: JSON.parse(readFileSync(file, 'utf8')) } : null; meta.handles = handle ? { [did]: handle } : {}; } } return { ...result, _meta: meta }; } /** JSON with each object's keys in order, as the workspace's callKey reads a call. */ const canonical = (value) => Array.isArray(value) ? `[${value.map(canonical).join(',')}]` : typeof value === 'object' && value !== null ? `{${Object.keys(value) .sort() .map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`) .join(',')}}` : JSON.stringify(value); const key = (name, args) => `${name} ${canonical(args)}`; if (only) { const file = join(here, 'tools.json'); const was = readFileSync(file, 'utf8'); const kept = JSON.parse(was); const answers = {}; for (const name of only) { const { name: tool, args } = calls[name]; const asked = filled(name === 'feeds' ? { ...args, params: { feeds: savedFeeds } } : args, kept.picked); answers[key(tool, filled(args, kept.picked))] = await call(tool, asked); console.log(`recorded ${name}`); } // Answers new to the file go at the end of `calls`, its last key, so the rest of it is as it was; one recorded again rewrites it whole. const added = Object.entries(answers).map(([k, answer]) => `\t\t${JSON.stringify(k)}: ${JSON.stringify(answer, null, '\t').replaceAll('\n', '\n\t\t')}`); const text = Object.keys(answers).some((k) => k in kept.calls) ? `${JSON.stringify({ ...kept, calls: { ...kept.calls, ...answers } }, null, '\t')}\n` : was.replace(/\n\t}\n}\n$/, `,\n${added.join(',\n')}\n\t}\n}\n`); writeFileSync(file, text); console.log(`==> ${only.length} calls recorded in ${file}`); process.exit(0); } const recorded = {}; const picked = {}; // The people's DIDs and profiles, as getProfiles answers for the avatars. const profiles = await call('query', { method: 'app.bsky.actor.getProfiles', params: { actors: ['chadtmiller.com', ...people] }, service: 'did:web:api.bsky.app#bsky_appview' }); const dids = Object.fromEntries(profiles.structuredContent.response.profiles.map((p) => [p.handle, p.did])); // The launcher describes the account by its DID. picked.me = dids['chadtmiller.com']; // A post to open as a thread, from the feed. const feed = await call(calls.feed.name, calls.feed.args); recorded[key(calls.feed.name, calls.feed.args)] = feed; picked.post = feed.structuredContent.response.feed.find((item) => item.post?.embed)?.post.uri ?? feed.structuredContent.response.feed[0].post.uri; // A document to open: the first writer of these who has one. for (const [writer, collection, icon] of [ ['pfrazee.com', 'pub.leaflet.document', '/icon/leaflet.pub'], ['danabra.mov', 'site.standard.document', '/icon/standard.site'], ['dame.blog', 'pub.leaflet.document', '/icon/leaflet.pub'] ]) { const args = { actor: writer, collection, limit: 5 }; const found = await call('list_records', args); const records = found.structuredContent?.records ?? found.structuredContent?.page?.records ?? []; const first = Array.isArray(records) ? records[0] : null; if (!first?.uri) continue; Object.assign(picked, { writer, documents: collection, document: first.uri, documentsIcon: icon }); break; } if (!picked.document) throw new Error('No writer of these has a document.'); for (const [name, { name: tool, args }] of Object.entries(calls)) { const asked = filled(name === 'feeds' ? { ...args, params: { feeds: savedFeeds } } : args, picked); const k = key(tool, filled(args, picked)); if (recorded[k]) continue; recorded[k] = await call(tool, asked); console.log(`recorded ${name}`); } writeFileSync(join(here, 'tools.json'), `${JSON.stringify({ about: 'Recorded by record.mjs: the answers the look tiles draw. Do not edit.', dids, picked, profiles: profiles.structuredContent.response.profiles, calls: recorded }, null, '\t')}\n`); console.log(`==> ${Object.keys(recorded).length} calls in ${join(here, 'tools.json')}`);