truemap-cli 0.3.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -27
- package/kit/fonts/OFL-commit-mono.txt +90 -0
- package/kit/fonts/OFL-instrument-sans.txt +93 -0
- package/kit/fonts/commit-mono-latin-400-normal.woff2 +0 -0
- package/kit/fonts/instrument-sans-latin-400-normal.woff2 +0 -0
- package/kit/fonts/instrument-sans-latin-500-normal.woff2 +0 -0
- package/kit/fonts/instrument-sans-latin-600-normal.woff2 +0 -0
- package/kit/truemap.css +183 -62
- package/package.json +1 -1
- package/skills/truemap/SKILL.md +10 -5
- package/src/changes.js +164 -0
- package/src/check.js +102 -8
- package/src/cli.js +234 -18
- package/src/common.js +15 -1
- package/src/export.js +2 -2
- package/src/guide.md +150 -115
- package/src/plan.js +185 -0
- package/src/server.js +285 -30
- package/src/snippet.js +63 -0
- package/src/theme.js +46 -0
- package/src/ui/diagram.js +598 -0
- package/src/ui/sdk.js +402 -53
- package/src/ui/wires.js +114 -22
- package/src/ui/workspace.css +376 -157
- package/src/ui/workspace.html +29 -14
- package/src/ui/workspace.js +592 -76
package/src/server.js
CHANGED
|
@@ -1,17 +1,23 @@
|
|
|
1
1
|
// The local truemap server: one per repo, started on demand by the CLI, invisible to the user.
|
|
2
2
|
// Serves the workspace UI, the .truemap pages (with the SDK injected), a chat queue between
|
|
3
3
|
// the browser and the agent's CLI (listen/reply), and live updates over Server-Sent Events.
|
|
4
|
-
import { createServer } from 'node:http';
|
|
4
|
+
import { createServer, request as httpRequest } from 'node:http';
|
|
5
5
|
import { randomBytes } from 'node:crypto';
|
|
6
|
-
import { existsSync, lstatSync, readFileSync, realpathSync, rmSync, statSync, watch } from 'node:fs';
|
|
7
|
-
import { extname, join, resolve, sep } from 'node:path';
|
|
8
|
-
import {
|
|
6
|
+
import { existsSync, lstatSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, watch } from 'node:fs';
|
|
7
|
+
import { basename, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
8
|
+
import { themeable } from './theme.js';
|
|
9
|
+
import { FOLDER, PKG_DIR, VERSION, addrPath, appendPrivate, buildId, stateHome, historyPath, serverStatePath, serversDir, snapshotsPath, writePrivate } from './common.js';
|
|
10
|
+
import { changes, fileDiff, snapshotTree } from './changes.js';
|
|
11
|
+
import { cleanPlan, currentPlan, parseReport, planCheck, planMessage, planWarnings, readPlan, savePlan, srcOfBox, stepCheck } from './plan.js';
|
|
9
12
|
import { checkAll, listPages } from './check.js';
|
|
10
13
|
import { exportPage, kitCss } from './export.js';
|
|
14
|
+
import { snippet } from './snippet.js';
|
|
11
15
|
|
|
12
16
|
const root = resolve(process.argv[2] || process.cwd());
|
|
13
17
|
const folder = join(root, FOLDER);
|
|
14
|
-
const
|
|
18
|
+
const BUILD = buildId();
|
|
19
|
+
const lastAddr = (() => { try { return JSON.parse(readFileSync(addrPath(root), 'utf8')) || {}; } catch { return {}; } })();
|
|
20
|
+
const token = typeof lastAddr.token === 'string' && /^[\w-]{24,}$/.test(lastAddr.token) ? lastAddr.token : randomBytes(18).toString('base64url');
|
|
15
21
|
const statePath = serverStatePath(root);
|
|
16
22
|
const IDLE_MS = Number(process.env.TRUEMAP_IDLE_MS || 30 * 60 * 1000);
|
|
17
23
|
const CLOSE_GRACE_MS = Number(process.env.TRUEMAP_CLOSE_GRACE_MS || 10_000);
|
|
@@ -37,6 +43,10 @@ let agentSince = 0; // when the current state began
|
|
|
37
43
|
let endRequested = false; // the user pressed End conversation while the agent wasn't listening
|
|
38
44
|
let annotateWish; // set by `truemap <page> --annotate` before a tab is open
|
|
39
45
|
let lastAgentActivity = 0;
|
|
46
|
+
let lastSeenAt = Date.now(); // when a person last had this project open
|
|
47
|
+
let lastReplyAt = 0; // when the agent last wrote in the chat
|
|
48
|
+
let everConnected = false; // a tab has been open since this server started
|
|
49
|
+
const asking = () => { const a = [...history].reverse().find((h) => h.role === 'agent'); return !!(a?.ask && !history.some((h) => h.kind === 'answer' && h.at > a.at)); };
|
|
40
50
|
let currentPage = null;
|
|
41
51
|
let closeTimer = null;
|
|
42
52
|
let idleTimer = null;
|
|
@@ -44,6 +54,72 @@ let seq = 0;
|
|
|
44
54
|
let lastCheck = { gitRepo: false, results: [] };
|
|
45
55
|
const history = loadHistory();
|
|
46
56
|
|
|
57
|
+
// ---------- change review: where "what changed" is measured from ----------
|
|
58
|
+
const snapshots = (() => { try { return JSON.parse(readFileSync(snapshotsPath(root), 'utf8')); } catch { return {}; } })();
|
|
59
|
+
function saveSnapshots() { try { writePrivate(snapshotsPath(root), JSON.stringify(snapshots)); } catch { /* ignore */ } }
|
|
60
|
+
async function takeSnapshot(kind, extra = {}) {
|
|
61
|
+
const tree = await snapshotTree(root).catch(() => null);
|
|
62
|
+
if (!tree) return null;
|
|
63
|
+
snapshots[kind] = { tree, at: new Date().toISOString(), ...extra };
|
|
64
|
+
saveSnapshots(); changeCache.clear();
|
|
65
|
+
broadcast({ type: 'changes-base', kind });
|
|
66
|
+
return snapshots[kind];
|
|
67
|
+
}
|
|
68
|
+
const changeCache = new Map(); // base -> { at, promise }
|
|
69
|
+
|
|
70
|
+
// ---------- what changed in a turn comes to the user, in the conversation ----------
|
|
71
|
+
let lastPostedChanges = snapshots.posted || '';
|
|
72
|
+
async function postChanges(base = 'turn') {
|
|
73
|
+
if (base === 'turn' && !snapshots.turn) return null;
|
|
74
|
+
const r = await changes(root, base, snapshots).catch(() => null);
|
|
75
|
+
if (!r || r.error || !r.files) return r;
|
|
76
|
+
const key = `${r.base.kind}:${snapshots[r.base.kind]?.tree || ''}:${r.now}`;
|
|
77
|
+
if (key === lastPostedChanges) return r;
|
|
78
|
+
lastPostedChanges = key; snapshots.posted = key; saveSnapshots();
|
|
79
|
+
const p = plan && snapshots.plan?.slug === plan.slug && r.base.kind === 'plan' ? planCheck(root, plan, r) : null;
|
|
80
|
+
remember({
|
|
81
|
+
role: 'agent', kind: 'changes', id: `c${Date.now().toString(36)}${(seq++).toString(36)}`, at: new Date().toISOString(), agent: agentName,
|
|
82
|
+
text: `Changed ${r.files} file${r.files > 1 ? 's' : ''}`,
|
|
83
|
+
changes: {
|
|
84
|
+
base: r.base.kind, label: r.base.label, files: r.files, add: r.add, del: r.del,
|
|
85
|
+
boxes: r.boxes.slice(0, 16).map((b) => ({ src: b.src, name: b.name, isNew: !!b.isNew, add: b.add, del: b.del, pages: b.pages, files: b.files.map((x) => x.path) })),
|
|
86
|
+
unmapped: r.unmapped.flatMap((g) => g.files).slice(0, 30).map((x) => ({ path: x.path, add: x.add, del: x.del })),
|
|
87
|
+
notDone: p ? p.planned.filter((x) => !x.changed).map((x) => x.name || x.box) : [],
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
return r;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ---------- plans ----------
|
|
94
|
+
let plan = (() => { try { return currentPlan(root); } catch { return null; } })();
|
|
95
|
+
function setPlan(p) {
|
|
96
|
+
plan = p;
|
|
97
|
+
if (p) { try { savePlan(root, p); } catch { /* ignore */ } }
|
|
98
|
+
broadcast({ type: 'plan', plan: p && !['done', 'rejected'].includes(p.status) ? p : null });
|
|
99
|
+
}
|
|
100
|
+
async function approvePlan(by) {
|
|
101
|
+
if (!plan || plan.status === 'done') return false;
|
|
102
|
+
setPlan({ ...plan, status: 'approved', approvedAt: new Date().toISOString() });
|
|
103
|
+
await takeSnapshot('plan', { slug: plan.slug });
|
|
104
|
+
if (by === 'user') {
|
|
105
|
+
const msg = { id: `m${Date.now().toString(36)}${(seq++).toString(36)}`, at: new Date().toISOString(), kind: 'approval', from: 'user', page: plan.page, text: `Approved the plan: ${plan.title}`, context: [], plan: plan.slug };
|
|
106
|
+
queue.push(msg); remember({ role: 'user', ...msg }); deliverQueue();
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
function changesFor(base, fresh = false) {
|
|
111
|
+
const hit = changeCache.get(base);
|
|
112
|
+
if (!fresh && hit && Date.now() - hit.at < 3000) return hit.promise;
|
|
113
|
+
const promise = changes(root, base, snapshots).then((r) => {
|
|
114
|
+
// measured from the plan's approval: say how the work compares with the plan
|
|
115
|
+
const p = plan && snapshots.plan?.slug === plan.slug ? plan : snapshots.plan?.slug ? readPlan(root, snapshots.plan.slug) : null;
|
|
116
|
+
if (!r.error && r.base?.kind === 'plan' && p) { r.plan = planCheck(root, p, r); r.plan.steps = stepCheck(root, p, r); r.plan.status = p.status; }
|
|
117
|
+
return r;
|
|
118
|
+
}).catch((e) => ({ error: String(e?.message || e) }));
|
|
119
|
+
changeCache.set(base, { at: Date.now(), promise });
|
|
120
|
+
return promise;
|
|
121
|
+
}
|
|
122
|
+
|
|
47
123
|
function loadHistory() {
|
|
48
124
|
try {
|
|
49
125
|
return readFileSync(historyPath(root), 'utf8').split('\n').filter(Boolean).map((l) => JSON.parse(l)).slice(-200);
|
|
@@ -81,8 +157,8 @@ function touchIdle() {
|
|
|
81
157
|
}, IDLE_MS);
|
|
82
158
|
}
|
|
83
159
|
|
|
84
|
-
function shutdown() {
|
|
85
|
-
broadcast({ type: 'closing' });
|
|
160
|
+
function shutdown(restart = false) {
|
|
161
|
+
broadcast({ type: 'closing', restart });
|
|
86
162
|
try {
|
|
87
163
|
const s = JSON.parse(readFileSync(statePath, 'utf8'));
|
|
88
164
|
if (s.pid === process.pid) rmSync(statePath, { force: true });
|
|
@@ -103,10 +179,13 @@ function deliverQueue() {
|
|
|
103
179
|
if (listener && (listener.res.destroyed || listener.res.writableEnded)) { clearTimeout(listener.timer); listener = null; setAgentState('idle'); }
|
|
104
180
|
if (!listener || queue.length === 0) return;
|
|
105
181
|
const messages = queue.splice(0, queue.length);
|
|
182
|
+
// The code is photographed before the agent hears the message, so its first edit is never part of "before".
|
|
183
|
+
const { res, timer } = listener; clearTimeout(timer); listener = null;
|
|
106
184
|
setAgentState('working');
|
|
107
185
|
lastAgentActivity = Date.now();
|
|
108
186
|
broadcast({ type: 'delivered', ids: messages.map((m) => m.id) });
|
|
109
|
-
|
|
187
|
+
const body = { status: 'message', messages, ...(plan && plan.status === 'review' ? { plan_waiting: plan.title } : {}) };
|
|
188
|
+
takeSnapshot('turn').catch(() => null).finally(() => { if (!res.writableEnded) sendJson(res, 200, body); });
|
|
110
189
|
}
|
|
111
190
|
|
|
112
191
|
function scheduleCloseCheck() {
|
|
@@ -114,7 +193,7 @@ function scheduleCloseCheck() {
|
|
|
114
193
|
if (!listener || clients.size > 0) return;
|
|
115
194
|
closeTimer = setTimeout(() => {
|
|
116
195
|
if (listener && clients.size === 0 && queue.length === 0) {
|
|
117
|
-
finishListen({ status: 'browser_closed' });
|
|
196
|
+
finishListen({ status: everConnected ? 'browser_closed' : 'no_tab' });
|
|
118
197
|
setAgentState('idle');
|
|
119
198
|
}
|
|
120
199
|
}, CLOSE_GRACE_MS);
|
|
@@ -144,6 +223,7 @@ function startWatching() {
|
|
|
144
223
|
watchTimer = setTimeout(() => {
|
|
145
224
|
const files = [...changedPages];
|
|
146
225
|
changedPages.clear();
|
|
226
|
+
changeCache.clear();
|
|
147
227
|
broadcast({ type: 'pages', pages: safePages() });
|
|
148
228
|
broadcast({ type: 'changed', files });
|
|
149
229
|
scheduleChecks();
|
|
@@ -159,6 +239,35 @@ function safePages() {
|
|
|
159
239
|
try { return listPages(root); } catch { return []; }
|
|
160
240
|
}
|
|
161
241
|
|
|
242
|
+
const cleanFocus = (f) => (Array.isArray(f) ? f : []).map((x) => String(x).trim().slice(0, 300)).filter(Boolean).slice(0, 20);
|
|
243
|
+
|
|
244
|
+
// ---------- every truemap project running on this machine (one server each) ----------
|
|
245
|
+
function peek(state) {
|
|
246
|
+
return new Promise((ok) => {
|
|
247
|
+
const r = httpRequest({ host: '127.0.0.1', port: state.port, path: '/api/agent/status', method: 'GET', headers: { 'X-Truemap-Token': state.token } }, (res) => {
|
|
248
|
+
const chunks = []; res.on('data', (c) => chunks.push(c));
|
|
249
|
+
res.on('end', () => { try { ok(JSON.parse(Buffer.concat(chunks).toString('utf8'))); } catch { ok(null); } });
|
|
250
|
+
});
|
|
251
|
+
r.setTimeout(800, () => r.destroy()); r.on('error', () => ok(null)); r.end();
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
async function projects() {
|
|
255
|
+
let files = [];
|
|
256
|
+
try { files = readdirSync(serversDir()).filter((x) => x.endsWith('.json')); } catch { return []; }
|
|
257
|
+
const list = await Promise.all(files.map(async (name) => {
|
|
258
|
+
let st; try { st = JSON.parse(readFileSync(join(serversDir(), name), 'utf8')); } catch { return null; }
|
|
259
|
+
if (!st?.port || !st.token) return null;
|
|
260
|
+
const self = st.pid === process.pid;
|
|
261
|
+
const s = self ? { root, name: basename(root), agent: agentState, agentName, plan: plan && plan.status !== 'done' ? { title: plan.title, status: plan.status } : null, unread: false, asking: false, clients: clients.size } : await peek(st);
|
|
262
|
+
if (!s?.root) return null;
|
|
263
|
+
return { self, name: s.name || basename(s.root), root: s.root, agent: s.agent, agentName: s.agentName || '', plan: s.plan || null, unread: !!s.unread, asking: !!s.asking, url: `http://127.0.0.1:${st.port}/?t=${encodeURIComponent(st.token)}` };
|
|
264
|
+
}));
|
|
265
|
+
// one entry per project, even when two state files (old path spellings) point at the same server
|
|
266
|
+
const seen = new Set();
|
|
267
|
+
return list.filter(Boolean).sort((a, b) => (b.self - a.self) || a.name.localeCompare(b.name))
|
|
268
|
+
.filter((x) => { const k = String(x.root).toLowerCase().replace(/[\\/]+$/, ''); if (seen.has(k)) return false; seen.add(k); return true; });
|
|
269
|
+
}
|
|
270
|
+
|
|
162
271
|
// ---------- http helpers ----------
|
|
163
272
|
function sendJson(res, status, body) {
|
|
164
273
|
res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store' });
|
|
@@ -170,6 +279,12 @@ function sendText(res, status, text) {
|
|
|
170
279
|
res.end(text);
|
|
171
280
|
}
|
|
172
281
|
|
|
282
|
+
// Light or dark, when the user picked one; otherwise the system decides. Shared by every project.
|
|
283
|
+
const prefsFile = () => join(stateHome(), 'prefs.json');
|
|
284
|
+
function readPrefs() { try { return JSON.parse(readFileSync(prefsFile(), 'utf8')) || {}; } catch { return {}; } }
|
|
285
|
+
const theme = () => { const t = readPrefs().theme; return t === 'light' || t === 'dark' ? t : ''; };
|
|
286
|
+
const withTheme = (html) => { const t = theme(); return t ? html.replace(/<html\b/i, `<html data-theme="${t}"`) : html; };
|
|
287
|
+
|
|
173
288
|
function readBody(req, limit = 2 * 1024 * 1024) {
|
|
174
289
|
return new Promise((ok, fail) => {
|
|
175
290
|
let size = 0;
|
|
@@ -227,6 +342,7 @@ function servePage(res, file) {
|
|
|
227
342
|
const inject = `<script src="/__truemap/sdk.js"></script>`;
|
|
228
343
|
let html = body.toString('utf8');
|
|
229
344
|
html = /<\/body>/i.test(html) ? html.replace(/<\/body>(?![\s\S]*<\/body>)/i, `${inject}</body>`) : html + inject;
|
|
345
|
+
html = withTheme(html);
|
|
230
346
|
body = Buffer.from(html, 'utf8');
|
|
231
347
|
}
|
|
232
348
|
res.writeHead(200, {
|
|
@@ -247,7 +363,7 @@ function serveStatic(res, dir, name, cache = 'no-store') {
|
|
|
247
363
|
// The page kit comes from truemap itself, so every repo gets kit updates. A repo may still keep its own
|
|
248
364
|
// assets/truemap.css (older setups, or a hand-tuned copy): that copy wins. assets/theme.css is appended.
|
|
249
365
|
function serveKit(res) {
|
|
250
|
-
const css = kitCss(root);
|
|
366
|
+
const css = themeable(kitCss(root));
|
|
251
367
|
res.writeHead(200, { 'Content-Type': 'text/css; charset=utf-8', 'Cache-Control': 'no-store' });
|
|
252
368
|
res.end(css);
|
|
253
369
|
}
|
|
@@ -257,7 +373,7 @@ function serveUi(res, name) {
|
|
|
257
373
|
if (!file) return sendText(res, 404, 'not found');
|
|
258
374
|
const type = MIME[extname(file).toLowerCase()] || 'application/octet-stream';
|
|
259
375
|
res.writeHead(200, { 'Content-Type': type, 'Cache-Control': 'no-store' });
|
|
260
|
-
res.end(readFileSync(file));
|
|
376
|
+
res.end(name === 'workspace.css' ? themeable(readFileSync(file, 'utf8')) : readFileSync(file));
|
|
261
377
|
}
|
|
262
378
|
|
|
263
379
|
// ---------- routes ----------
|
|
@@ -271,7 +387,7 @@ const server = createServer(async (req, res) => {
|
|
|
271
387
|
// Workspace shell (token required so other local pages can't embed a live session).
|
|
272
388
|
if (p === '/' && req.method === 'GET') {
|
|
273
389
|
if (!tokenOk(req, url)) return sendText(res, 403, 'missing token – open the link printed by `truemap`');
|
|
274
|
-
const html = readFileSync(join(UI_DIR, 'workspace.html'), 'utf8')
|
|
390
|
+
const html = withTheme(readFileSync(join(UI_DIR, 'workspace.html'), 'utf8'))
|
|
275
391
|
.replace('__TRUEMAP_TOKEN__', token)
|
|
276
392
|
.replace('__TRUEMAP_CONTENT__', `http://127.0.0.1:${contentPort}`);
|
|
277
393
|
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store', 'Referrer-Policy': 'no-referrer' });
|
|
@@ -287,16 +403,23 @@ const server = createServer(async (req, res) => {
|
|
|
287
403
|
scheduleChecks();
|
|
288
404
|
res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-store', Connection: 'keep-alive' });
|
|
289
405
|
res.write(`data: ${JSON.stringify({
|
|
290
|
-
type: 'hello', version: VERSION, root, cmd: CMD, agent: agentInfo(), pages: safePages(), check: lastCheck,
|
|
291
|
-
history, current: currentPage, pending: queue.map((m) => m.id), ...(annotateWish !== undefined ? { annotate: annotateWish } : {}),
|
|
406
|
+
type: 'hello', version: VERSION, build: BUILD, root, cmd: CMD, agent: agentInfo(), pages: safePages(), check: lastCheck,
|
|
407
|
+
history, current: currentPage, pending: queue.map((m) => m.id), plan: plan && plan.status !== 'done' ? plan : null, ...(annotateWish !== undefined ? { annotate: annotateWish } : {}),
|
|
292
408
|
})}\n\n`);
|
|
293
409
|
annotateWish = undefined;
|
|
294
|
-
clients.add(res);
|
|
410
|
+
clients.add(res); everConnected = true;
|
|
295
411
|
clearTimeout(closeTimer);
|
|
296
412
|
const ping = setInterval(() => res.write(': ping\n\n'), 20_000);
|
|
297
|
-
req.on('close', () => { clearInterval(ping); clients.delete(res); scheduleCloseCheck(); touchIdle(); });
|
|
413
|
+
req.on('close', () => { clearInterval(ping); clients.delete(res); lastSeenAt = Date.now(); scheduleCloseCheck(); touchIdle(); });
|
|
298
414
|
return;
|
|
299
415
|
}
|
|
416
|
+
if (p === '/api/prefs' && req.method === 'POST') {
|
|
417
|
+
const b = await readBody(req);
|
|
418
|
+
const next = { ...readPrefs() };
|
|
419
|
+
if ('theme' in b) { if (b.theme === 'light' || b.theme === 'dark') next.theme = b.theme; else delete next.theme; }
|
|
420
|
+
writePrivate(prefsFile(), JSON.stringify(next));
|
|
421
|
+
return sendJson(res, 200, { ok: true, theme: theme() });
|
|
422
|
+
}
|
|
300
423
|
if (p === '/api/send' && req.method === 'POST') {
|
|
301
424
|
const b = await readBody(req);
|
|
302
425
|
const text = String(b.text ?? '').slice(0, 20_000);
|
|
@@ -321,6 +444,7 @@ const server = createServer(async (req, res) => {
|
|
|
321
444
|
context,
|
|
322
445
|
...(answer ? { answer } : {}),
|
|
323
446
|
};
|
|
447
|
+
if (b.plan === 'request' && plan && plan.status !== 'done') { msg.kind = 'plan_changes'; msg.plan = plan.slug; setPlan({ ...plan, status: 'revise' }); }
|
|
324
448
|
queue.push(msg);
|
|
325
449
|
endRequested = false; // a new message means the conversation goes on
|
|
326
450
|
remember({ role: 'user', ...msg });
|
|
@@ -342,6 +466,48 @@ const server = createServer(async (req, res) => {
|
|
|
342
466
|
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store', 'Content-Disposition': `attachment; filename="${name.replace(/[^\w.-]/g, '_')}-truemap.html"` });
|
|
343
467
|
return res.end(html);
|
|
344
468
|
}
|
|
469
|
+
if (p === '/api/image' && req.method === 'GET') {
|
|
470
|
+
// A picture the agent put in its reply: a raster image inside this repo, nothing else.
|
|
471
|
+
const asked = String(url.searchParams.get('path') || '');
|
|
472
|
+
const rel = isAbsolute(asked) ? relative(root, asked) : asked;
|
|
473
|
+
const file = rel && !rel.startsWith('..') && !isAbsolute(rel) ? safeFile(root, rel.split(sep).join('/')) : null;
|
|
474
|
+
const type = file && { '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp' }[extname(file).toLowerCase()];
|
|
475
|
+
if (!type) return sendText(res, 404, 'not an image in this repo');
|
|
476
|
+
res.writeHead(200, { 'Content-Type': type, 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff' });
|
|
477
|
+
return res.end(readFileSync(file));
|
|
478
|
+
}
|
|
479
|
+
if (p === '/api/snippet' && req.method === 'GET') {
|
|
480
|
+
try { return sendJson(res, 200, snippet(root, url.searchParams.get('src') || '')); } catch (e) { return sendJson(res, 200, { error: String(e.message || e) }); }
|
|
481
|
+
}
|
|
482
|
+
if (p === '/api/changes' && req.method === 'GET') {
|
|
483
|
+
return sendJson(res, 200, await changesFor(url.searchParams.get('base') || 'auto'));
|
|
484
|
+
}
|
|
485
|
+
if (p === '/api/diff' && req.method === 'GET') {
|
|
486
|
+
return sendJson(res, 200, await fileDiff(root, url.searchParams.get('base') || 'auto', snapshots, String(url.searchParams.get('path') || '')));
|
|
487
|
+
}
|
|
488
|
+
if (p === '/api/plan' && req.method === 'POST') {
|
|
489
|
+
const b = await readBody(req);
|
|
490
|
+
if (b.action === 'approve') return sendJson(res, 200, { ok: await approvePlan('user') });
|
|
491
|
+
if (b.action === 'reject' && plan && plan.status !== 'done') {
|
|
492
|
+
setPlan({ ...plan, status: 'rejected', rejectedAt: new Date().toISOString() });
|
|
493
|
+
const msg = { id: `m${Date.now().toString(36)}${(seq++).toString(36)}`, at: new Date().toISOString(), kind: 'plan_rejected', from: 'user', page: plan.page, text: `Rejected the plan: ${plan.title}`, context: [], plan: plan.slug };
|
|
494
|
+
queue.push(msg); remember({ role: 'user', ...msg }); deliverQueue();
|
|
495
|
+
return sendJson(res, 200, { ok: true });
|
|
496
|
+
}
|
|
497
|
+
return sendJson(res, 400, { error: 'unknown action' });
|
|
498
|
+
}
|
|
499
|
+
if (p === '/api/drift' && req.method === 'GET') {
|
|
500
|
+
const path = String(url.searchParams.get('path') || '').replace(/\\/g, '/');
|
|
501
|
+
const since = String(url.searchParams.get('since') || '');
|
|
502
|
+
if (!path || path.includes('..')) return sendJson(res, 400, { error: 'bad path' });
|
|
503
|
+
const { execFileSync } = await import('node:child_process');
|
|
504
|
+
const g = (args) => { try { return execFileSync('git', args, { cwd: root, encoding: 'utf8', maxBuffer: 16 * 1024 * 1024, windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] }); } catch { return null; } };
|
|
505
|
+
const commit = (since && g(['rev-list', '-1', `--before=${since}`, 'HEAD'])?.trim()) || 'HEAD';
|
|
506
|
+
const text = g(['diff', '--no-color', '-U3', commit, '--', path]);
|
|
507
|
+
if (text === null) return sendJson(res, 200, { error: 'git diff failed' });
|
|
508
|
+
return sendJson(res, 200, { path, from: commit.slice(0, 8), text: text.length > 200_000 ? `${text.slice(0, 200_000)}\n… (cut)` : text });
|
|
509
|
+
}
|
|
510
|
+
if (p === '/api/projects' && req.method === 'GET') return sendJson(res, 200, { projects: await projects() });
|
|
345
511
|
if (p === '/api/end' && req.method === 'POST') {
|
|
346
512
|
// The user is done talking on the page: free the agent to go back to the terminal.
|
|
347
513
|
if (listener) finishListen({ status: 'ended' }); else if (agentState === 'working' || agentState === 'listening') endRequested = true;
|
|
@@ -351,10 +517,16 @@ const server = createServer(async (req, res) => {
|
|
|
351
517
|
|
|
352
518
|
// ----- agent (CLI) side -----
|
|
353
519
|
if (p === '/api/agent/status' && req.method === 'GET') {
|
|
354
|
-
return sendJson(res, 200, {
|
|
520
|
+
return sendJson(res, 200, {
|
|
521
|
+
version: VERSION, pid: process.pid, root, name: basename(root), clients: clients.size, agent: agentState, agentName, queued: queue.length, current: currentPage,
|
|
522
|
+
plan: plan && plan.status !== 'done' ? { title: plan.title, status: plan.status } : null,
|
|
523
|
+
unread: !clients.size && lastReplyAt > lastSeenAt, asking: asking(),
|
|
524
|
+
});
|
|
355
525
|
}
|
|
356
526
|
if (p === '/api/agent/listen' && req.method === 'POST') {
|
|
357
527
|
const b = await readBody(req);
|
|
528
|
+
// the agent listens again: its turn is over, so what it changed goes to the user
|
|
529
|
+
postChanges('turn'); // posts only when something changed since the turn began, and only once
|
|
358
530
|
if (listener) finishListen({ status: 'replaced' });
|
|
359
531
|
lastAgentActivity = Date.now();
|
|
360
532
|
noteAgent(b.agent);
|
|
@@ -377,7 +549,12 @@ const server = createServer(async (req, res) => {
|
|
|
377
549
|
if (!text.trim()) return sendJson(res, 400, { error: 'empty reply' });
|
|
378
550
|
lastAgentActivity = Date.now();
|
|
379
551
|
noteAgent(b.agent);
|
|
380
|
-
|
|
552
|
+
const focus = cleanFocus(b.focus);
|
|
553
|
+
const ask = b.ask && typeof b.ask === 'object' && Array.isArray(b.ask.options) && b.ask.options.length >= 2
|
|
554
|
+
? { question: String(b.ask.question ?? '').slice(0, 500), options: b.ask.options.slice(0, 6).map((o) => ({ label: String(o.label ?? '').slice(0, 200), why: String(o.why ?? '').slice(0, 400) })) }
|
|
555
|
+
: null;
|
|
556
|
+
remember({ role: 'agent', id: `a${Date.now().toString(36)}${(seq++).toString(36)}`, at: new Date().toISOString(), agent: agentName, text, ...(focus.length ? { focus } : {}), ...(ask ? { ask } : {}) });
|
|
557
|
+
lastReplyAt = Date.now(); if (clients.size) lastSeenAt = lastReplyAt;
|
|
381
558
|
if (!listener) setAgentState(b.keepWorking || b.willListen ? 'working' : 'idle');
|
|
382
559
|
return sendJson(res, 200, { ok: true, browsers: clients.size });
|
|
383
560
|
}
|
|
@@ -386,18 +563,84 @@ const server = createServer(async (req, res) => {
|
|
|
386
563
|
const page = String(b.page ?? '');
|
|
387
564
|
noteAgent(b.agent);
|
|
388
565
|
if (typeof b.annotate === 'boolean' && clients.size === 0) annotateWish = b.annotate;
|
|
389
|
-
|
|
566
|
+
const pagesNow = safePages();
|
|
567
|
+
broadcast({ type: 'navigate', page, pages: pagesNow, ...(typeof b.annotate === 'boolean' ? { annotate: b.annotate } : {}) });
|
|
568
|
+
if (/^drafts\//.test(page) && !history.some((h) => h.draft === page)) {
|
|
569
|
+
const t = pagesNow.find((x) => x.page === page)?.title || page;
|
|
570
|
+
const n = parseInt(page.slice(7), 10);
|
|
571
|
+
remember({ role: 'agent', id: `a${Date.now().toString(36)}${(seq++).toString(36)}`, at: new Date().toISOString(), agent: agentName, draft: page, text: `Draft${Number.isFinite(n) ? ` ${n}` : ''}: [${t}](${page})` });
|
|
572
|
+
}
|
|
390
573
|
scheduleChecks();
|
|
391
574
|
return sendJson(res, 200, { ok: true, browsers: clients.size });
|
|
392
575
|
}
|
|
576
|
+
if (p === '/api/agent/show' && req.method === 'POST') {
|
|
577
|
+
// The agent points at the map: light up boxes (by id or data-src), optionally with a note.
|
|
578
|
+
const b = await readBody(req);
|
|
579
|
+
lastAgentActivity = Date.now();
|
|
580
|
+
noteAgent(b.agent);
|
|
581
|
+
broadcast({ type: 'show', page: typeof b.page === 'string' ? b.page : '', focus: cleanFocus(b.focus), note: String(b.note ?? '').slice(0, 2000), clear: !!b.clear, agent: agentName });
|
|
582
|
+
return sendJson(res, 200, { ok: true, browsers: clients.size });
|
|
583
|
+
}
|
|
584
|
+
if (p === '/api/agent/changes' && req.method === 'POST') {
|
|
585
|
+
// `truemap changes`: the summary for the agent, and the Changes view for the user.
|
|
586
|
+
const b = await readBody(req);
|
|
587
|
+
noteAgent(b.agent);
|
|
588
|
+
const base = ['turn', 'plan', 'head', 'main'].includes(b.base) ? b.base : 'auto';
|
|
589
|
+
const r = await changesFor(base, true);
|
|
590
|
+
if (b.open !== false && !r.error && r.files) await postChanges(r.base.kind);
|
|
591
|
+
return sendJson(res, 200, { ...r, browsers: clients.size });
|
|
592
|
+
}
|
|
593
|
+
if (p === '/api/agent/plan' && req.method === 'POST') {
|
|
594
|
+
const b = await readBody(req);
|
|
595
|
+
noteAgent(b.agent);
|
|
596
|
+
lastAgentActivity = Date.now();
|
|
597
|
+
if (b.status) {
|
|
598
|
+
if (!plan) return sendJson(res, 404, { error: 'no plan in play' });
|
|
599
|
+
if (b.status === 'approved') await approvePlan('agent');
|
|
600
|
+
let check = null;
|
|
601
|
+
if (b.status === 'done') {
|
|
602
|
+
// closing a plan takes the agent's account of every step
|
|
603
|
+
let rep2;
|
|
604
|
+
try { rep2 = parseReport(plan, Array.isArray(b.steps) ? b.steps : []); } catch (e) { return sendJson(res, 400, { error: String(e.message || e) }); }
|
|
605
|
+
// the account of steps is optional; when given, every report is kept
|
|
606
|
+
if (rep2.report.some(Boolean)) plan = { ...plan, report: rep2.report, reports: [...(plan.reports || []), { at: new Date().toISOString(), report: rep2.report }] };
|
|
607
|
+
if (snapshots.plan?.slug === plan.slug) { await takeSnapshot('planEnd', { slug: plan.slug }); const r = await changesFor('plan', true); check = r.plan || null; }
|
|
608
|
+
setPlan({ ...plan, status: 'done', doneAt: new Date().toISOString() });
|
|
609
|
+
const sts = check?.steps || [];
|
|
610
|
+
const rpt = plan.report || [];
|
|
611
|
+
const tally = ['done', 'partly', 'not_built', 'dropped'].map((k) => [k, rpt.filter((x) => x?.status === k).length]).filter(([, n]) => n).map(([k, n]) => `${n} ${k.replace('_', ' ')}`).join(', ');
|
|
612
|
+
const conflicts = sts.filter((x) => x.conflict || x.rewritten);
|
|
613
|
+
remember({ role: 'agent', id: `a${Date.now().toString(36)}${(seq++).toString(36)}`, at: new Date().toISOString(), agent: agentName, plan: plan.slug,
|
|
614
|
+
text: [`**Plan closed: ${plan.title}.**${tally ? ` Steps: ${tally}.` : ''}`, ...rpt.map((x, i) => x && x.status !== 'done' ? `- Step ${i + 1} ${x.status.replace('_', ' ')}: ${x.note}` : null).filter(Boolean),
|
|
615
|
+
...(conflicts.length ? ['', '**Check before you trust this report:**', ...conflicts.map((x) => `- Step ${x.n}: ${[x.conflict, x.rewritten].filter(Boolean).join(' ')}`)] : []),
|
|
616
|
+
'', 'Open the Changes tab to see each step beside its diff.'].join('\n') });
|
|
617
|
+
}
|
|
618
|
+
return sendJson(res, 200, { ok: true, plan: plan.slug, status: plan.status, check });
|
|
619
|
+
}
|
|
620
|
+
let p2;
|
|
621
|
+
try { p2 = cleanPlan(b.plan); } catch (e) { return sendJson(res, 400, { error: String(e.message || e) }); }
|
|
622
|
+
const prev = readPlan(root, p2.slug);
|
|
623
|
+
p2.revision = prev && prev.status !== 'done' ? (prev.revision || 1) + 1 : 1;
|
|
624
|
+
p2.status = 'review'; p2.at = new Date().toISOString();
|
|
625
|
+
if (prev && prev.status !== 'done') {
|
|
626
|
+
const was = new Map(prev.touch.map((t) => [t.box, t.what]));
|
|
627
|
+
const wasAdd = new Map(prev.add.map((a) => [a.id, JSON.stringify(a)]));
|
|
628
|
+
p2.revised = [...p2.touch.filter((t) => was.get(t.box) !== t.what).map((t) => t.box), ...p2.add.filter((a) => wasAdd.get(a.id) !== JSON.stringify(a)).map((a) => a.id)];
|
|
629
|
+
}
|
|
630
|
+
setPlan(p2);
|
|
631
|
+
remember({ role: 'agent', id: `a${Date.now().toString(36)}${(seq++).toString(36)}`, at: p2.at, agent: agentName, text: planMessage(p2, p2.revision > 1 ? prev : null, (box) => srcOfBox(root, p2.page, box, 'data-name') || box), plan: p2.slug });
|
|
632
|
+
broadcast({ type: 'navigate', page: p2.page, pages: safePages() });
|
|
633
|
+
return sendJson(res, 200, { ok: true, slug: p2.slug, revision: p2.revision, page: p2.page, browsers: clients.size, warnings: planWarnings(root, p2) });
|
|
634
|
+
}
|
|
393
635
|
if (p === '/api/agent/progress' && req.method === 'POST') {
|
|
394
636
|
const b = await readBody(req);
|
|
395
637
|
broadcast({ type: 'progress', text: String(b.text ?? '').slice(0, 300) });
|
|
396
638
|
return sendJson(res, 200, { ok: true });
|
|
397
639
|
}
|
|
398
640
|
if (p === '/api/agent/shutdown' && req.method === 'POST') {
|
|
641
|
+
const b = await readBody(req).catch(() => ({}));
|
|
399
642
|
sendJson(res, 200, { ok: true });
|
|
400
|
-
setTimeout(shutdown, 50);
|
|
643
|
+
setTimeout(() => shutdown(!!b.restart), 50);
|
|
401
644
|
return;
|
|
402
645
|
}
|
|
403
646
|
return sendText(res, 404, 'not found');
|
|
@@ -413,7 +656,9 @@ const content = createServer((req, res) => {
|
|
|
413
656
|
const p = new URL(req.url, `http://127.0.0.1:${contentPort}`).pathname;
|
|
414
657
|
try {
|
|
415
658
|
if (p === '/__truemap/sdk.js') {
|
|
416
|
-
const
|
|
659
|
+
const themeSrc = readFileSync(new URL('./theme.js', import.meta.url), 'utf8').replace(/^export /m, '');
|
|
660
|
+
const js = `const tmThemeable = (() => {\n${themeSrc}\nreturn themeable;\n})();\n` + [join(UI_DIR, 'wires.js'), join(UI_DIR, 'diagram.js')].map((f) => readFileSync(f, 'utf8')).join('\n;\n')
|
|
661
|
+
+ '\n' + readFileSync(join(UI_DIR, 'sdk.js'), 'utf8').replace('__TRUEMAP_PARENT__', `http://127.0.0.1:${port}`);
|
|
417
662
|
res.writeHead(200, { 'Content-Type': 'text/javascript; charset=utf-8', 'Cache-Control': 'no-store' });
|
|
418
663
|
return res.end(js);
|
|
419
664
|
}
|
|
@@ -430,16 +675,26 @@ const content = createServer((req, res) => {
|
|
|
430
675
|
}
|
|
431
676
|
});
|
|
432
677
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
678
|
+
// The last ports again when they are free, otherwise any.
|
|
679
|
+
function listenOn(srv, want) {
|
|
680
|
+
return new Promise((ok, fail) => {
|
|
681
|
+
const attempt = (p) => {
|
|
682
|
+
const onErr = (e) => { if (p && (e.code === 'EADDRINUSE' || e.code === 'EACCES')) attempt(0); else fail(e); };
|
|
683
|
+
srv.once('error', onErr);
|
|
684
|
+
srv.listen(p, '127.0.0.1', () => { srv.off('error', onErr); ok(srv.address().port); });
|
|
685
|
+
};
|
|
686
|
+
attempt(want);
|
|
441
687
|
});
|
|
442
|
-
}
|
|
688
|
+
}
|
|
689
|
+
(async () => {
|
|
690
|
+
port = await listenOn(server, Number(process.env.TRUEMAP_PORT || lastAddr.port || 0));
|
|
691
|
+
contentPort = await listenOn(content, Number(lastAddr.contentPort || 0));
|
|
692
|
+
writePrivate(addrPath(root), JSON.stringify({ port, contentPort, token }));
|
|
693
|
+
writePrivate(statePath, JSON.stringify({ pid: process.pid, port, contentPort, token, version: VERSION, build: BUILD, root, startedAt: new Date().toISOString() }));
|
|
694
|
+
startWatching();
|
|
695
|
+
scheduleChecks();
|
|
696
|
+
touchIdle();
|
|
697
|
+
})().catch((e) => { console.error(e); process.exit(1); });
|
|
443
698
|
|
|
444
699
|
// Code changes outside .truemap aren't watched (too noisy); re-check while someone is looking.
|
|
445
700
|
setInterval(() => { if (clients.size > 0) scheduleChecks(); }, 30_000).unref();
|
package/src/snippet.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// The real code behind a box: a few lines around `name` in `path`, read from the repo.
|
|
2
|
+
// Only files git tracks (or, without git, files inside the repo that aren't hidden) are served, so a
|
|
3
|
+
// page can never pull out .env or anything ignored.
|
|
4
|
+
import { execFileSync } from 'node:child_process';
|
|
5
|
+
import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs';
|
|
6
|
+
import { join, resolve, sep } from 'node:path';
|
|
7
|
+
import { gitExe } from './common.js';
|
|
8
|
+
import { parseAnchor } from './check.js';
|
|
9
|
+
|
|
10
|
+
const MAX_BYTES = 2 * 1024 * 1024;
|
|
11
|
+
|
|
12
|
+
function git(root, args) {
|
|
13
|
+
try {
|
|
14
|
+
return execFileSync(gitExe(), args, { cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], windowsHide: true, maxBuffer: 8 * 1024 * 1024 });
|
|
15
|
+
} catch { return null; }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function snippet(root, src, around = 10) {
|
|
19
|
+
const { path, name } = parseAnchor(String(src || ''));
|
|
20
|
+
if (!path || path.endsWith('/')) return { path, kind: 'folder' };
|
|
21
|
+
const full = resolve(root, '.' + sep + path.replace(/\//g, sep));
|
|
22
|
+
if (!full.startsWith(resolve(root) + sep) || !existsSync(full)) return { path, error: 'not found' };
|
|
23
|
+
try { if (!realpathSync(full).startsWith(realpathSync(root) + sep)) return { path, error: 'outside the repo' }; } catch { return { path, error: 'not found' }; }
|
|
24
|
+
const tracked = git(root, ['ls-files', '--error-unmatch', '--', path]);
|
|
25
|
+
if (tracked === null) {
|
|
26
|
+
if (git(root, ['rev-parse', '--is-inside-work-tree']) !== null) return { path, error: 'not tracked by git' };
|
|
27
|
+
if (path.split('/').some((p) => p.startsWith('.'))) return { path, error: 'hidden file' };
|
|
28
|
+
}
|
|
29
|
+
const st = statSync(full);
|
|
30
|
+
if (st.isDirectory()) return { path, kind: 'folder' };
|
|
31
|
+
if (st.size > MAX_BYTES) return { path, error: 'file too large to show' };
|
|
32
|
+
const text = readFileSync(join(full), 'utf8');
|
|
33
|
+
if (text.includes('\0')) return { path, error: 'binary file' };
|
|
34
|
+
const lines = text.split(/\r?\n/);
|
|
35
|
+
let line = 0;
|
|
36
|
+
if (name) {
|
|
37
|
+
const esc = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
38
|
+
const re = new RegExp(`(?<![\\p{L}\\p{N}_$])${esc}(?![\\p{L}\\p{N}_$])`, 'u');
|
|
39
|
+
// Prefer the line that defines the name over lines that merely use it:
|
|
40
|
+
// 1. a keyword right before it (function x, class X, def x, const x = …), or x: / x = a function,
|
|
41
|
+
// or a method header at the start of a line (async x(…) {) that is not a call (this.x(…))
|
|
42
|
+
// 2. any line with a defining keyword 3. the first mention
|
|
43
|
+
const strong = [
|
|
44
|
+
new RegExp(`\\b(function\\*?|class|def|fn|func|interface|type|struct|enum|trait|impl|module|const|let|var|val)\\s+${esc}(?![\\p{L}\\p{N}_$])`, 'u'),
|
|
45
|
+
new RegExp(`(?<![.\\p{L}\\p{N}_$])${esc}\\s*[:=]\\s*(async\\s*)?(function|\\(|[\\p{L}_$][\\p{L}\\p{N}_$]*\\s*=>)`, 'u'),
|
|
46
|
+
// a method header: name(…) { or name(…): Type { on one line, or name( with the parameters continuing below
|
|
47
|
+
new RegExp(`^\\s*((public|private|protected|static|async|override|readonly|get|set)\\s+)*${esc}\\s*(<[^>]*>)?\\s*\\((.*\\)\\s*(:[^=]*?)?\\s*\\{\\s*$|[^)]*$)`, 'u'),
|
|
48
|
+
];
|
|
49
|
+
const weak = /\b(function|class|def|fn|func|const|let|var|type|interface|export|struct|enum|router\.\w+|app\.\w+)\b/;
|
|
50
|
+
const hits = lines.map((l, i) => (re.test(l) ? i : -1)).filter((i) => i >= 0);
|
|
51
|
+
if (!hits.length) return { path, name, missing: true, total: lines.length };
|
|
52
|
+
line = hits.find((i) => strong.some((s) => s.test(lines[i]))) ?? hits.find((i) => weak.test(lines[i])) ?? hits[0] ?? 0;
|
|
53
|
+
}
|
|
54
|
+
const start = Math.max(0, line - 3);
|
|
55
|
+
const end = Math.min(lines.length, start + around * 2 + 1);
|
|
56
|
+
const dirty = (git(root, ['status', '--porcelain', '--', path]) || '').trim();
|
|
57
|
+
const changed = dirty ? 'uncommitted changes' : git(root, ['log', '-1', '--format=%cr', '--', path]);
|
|
58
|
+
return {
|
|
59
|
+
path, name, line: line + 1, start: start + 1, total: lines.length,
|
|
60
|
+
lines: lines.slice(start, end).map((l) => (l.length > 220 ? `${l.slice(0, 220)}…` : l)),
|
|
61
|
+
lastChange: changed ? changed.trim() : '',
|
|
62
|
+
};
|
|
63
|
+
}
|
package/src/theme.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Dark mode follows the system unless the user picks one. The stylesheets keep plain
|
|
2
|
+
// `@media (prefers-color-scheme: dark)` blocks; this turns each into rules that also answer to a choice:
|
|
3
|
+
// <html data-theme="dark"> always dark <html data-theme="light"> always light
|
|
4
|
+
// For a shadow root, the host carries the choice as a class instead (tm-dark / tm-light).
|
|
5
|
+
|
|
6
|
+
function blocks(css) {
|
|
7
|
+
// top-level `@media (prefers-color-scheme: dark) { ... }` blocks, with their positions
|
|
8
|
+
const out = [];
|
|
9
|
+
const re = /@media\s*\(\s*prefers-color-scheme\s*:\s*dark\s*\)\s*\{/g;
|
|
10
|
+
let m;
|
|
11
|
+
while ((m = re.exec(css))) {
|
|
12
|
+
let depth = 1; let i = re.lastIndex;
|
|
13
|
+
for (; i < css.length && depth; i++) { if (css[i] === '{') depth++; else if (css[i] === '}') depth--; }
|
|
14
|
+
out.push({ start: m.index, bodyStart: re.lastIndex, end: i });
|
|
15
|
+
re.lastIndex = i;
|
|
16
|
+
}
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function scopeRules(body, pick) {
|
|
21
|
+
// `sel, sel { decls }` pairs (no nested blocks inside a colour-scheme block)
|
|
22
|
+
return body.replace(/([^{}]+)\{([^{}]*)\}/g, (all, sels, decls) => `${sels.split(',').map((x) => pick(x.trim())).filter(Boolean).join(', ')} {${decls}}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** CSS for a document: dark when chosen, or when the system is dark and light was not chosen. */
|
|
26
|
+
export function themeable(css, { host = false } = {}) {
|
|
27
|
+
const found = blocks(css);
|
|
28
|
+
if (!found.length) return css;
|
|
29
|
+
const on = host ? ':host(.tm-dark)' : ':root[data-theme="dark"]';
|
|
30
|
+
const notOff = host ? ':host(:not(.tm-light))' : ':root:not([data-theme="light"])';
|
|
31
|
+
const scope = (prefix) => (sel) => {
|
|
32
|
+
if (!sel) return '';
|
|
33
|
+
if (!host && sel.startsWith(':root')) return prefix + sel.slice(5);
|
|
34
|
+
if (!host && /^html\b/.test(sel)) return prefix.replace(':root', 'html') + sel.slice(4);
|
|
35
|
+
return `${prefix} ${sel}`;
|
|
36
|
+
};
|
|
37
|
+
let out = ''; let at = 0; const forced = [];
|
|
38
|
+
for (const b of found) {
|
|
39
|
+
const body = css.slice(b.bodyStart, b.end - 1);
|
|
40
|
+
out += css.slice(at, b.bodyStart) + scopeRules(body, scope(notOff)) + '}';
|
|
41
|
+
forced.push(scopeRules(body, scope(on)));
|
|
42
|
+
at = b.end;
|
|
43
|
+
}
|
|
44
|
+
const scheme = host ? '' : ':root[data-theme="light"] { color-scheme: light; }\n:root[data-theme="dark"] { color-scheme: dark; }\n';
|
|
45
|
+
return `${out}${css.slice(at)}\n/* the user's choice */\n${scheme}${forced.join('\n')}\n`;
|
|
46
|
+
}
|