truemap-cli 0.1.0 → 0.3.0
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/bin/truemap.js +3 -0
- package/kit/fonts/OFL.txt +93 -0
- package/kit/fonts/ibm-plex-mono-latin-400-normal.woff2 +0 -0
- package/kit/fonts/ibm-plex-mono-latin-500-normal.woff2 +0 -0
- package/kit/fonts/ibm-plex-sans-latin-400-normal.woff2 +0 -0
- package/kit/fonts/ibm-plex-sans-latin-500-normal.woff2 +0 -0
- package/kit/fonts/ibm-plex-sans-latin-600-normal.woff2 +0 -0
- package/kit/truemap.css +248 -186
- package/package.json +1 -1
- package/skills/truemap/SKILL.md +4 -3
- package/src/check.js +32 -1
- package/src/cli.js +75 -20
- package/src/export.js +62 -0
- package/src/guide.md +67 -32
- package/src/outline.js +71 -0
- package/src/server.js +453 -394
- package/src/ui/sdk.js +350 -58
- package/src/ui/wires.js +237 -0
- package/src/ui/workspace.css +194 -128
- package/src/ui/workspace.html +59 -45
- package/src/ui/workspace.js +277 -104
package/src/ui/workspace.js
CHANGED
|
@@ -2,27 +2,26 @@
|
|
|
2
2
|
const TOKEN = window.TRUEMAP_TOKEN;
|
|
3
3
|
const CONTENT = window.TRUEMAP_CONTENT;
|
|
4
4
|
|
|
5
|
+
const AGENTS = { 'claude-code': 'Claude Code', codex: 'Codex' };
|
|
5
6
|
const S = {
|
|
6
|
-
|
|
7
|
-
placeholder: 'Ask about this project, or select something on the page', placeholderCtx: 'Ask about the selection',
|
|
8
|
-
intro: 'Select a box or some text on the page, then ask about it. The agent receives the code it points to.',
|
|
7
|
+
placeholder: 'Ask about this page', placeholderNotes: 'Add a message (optional)',
|
|
9
8
|
drawPrompt: 'Read this project and draw its blueprint as .truemap/index.html, then open it for me.',
|
|
10
|
-
|
|
11
|
-
['Explain', 'Explain how this works.'],
|
|
12
|
-
['What uses it', 'What depends on this, and what does it depend on?'],
|
|
13
|
-
['Why like this', 'Why is it built this way? What are the trade-offs?'],
|
|
14
|
-
['Change it…', null],
|
|
15
|
-
],
|
|
16
|
-
changePrefix: 'Change it so that ',
|
|
17
|
-
ok: 'Matches the code', changed: (n) => `${n} changed in code`, broken: (n) => `${n} out of date`,
|
|
9
|
+
ok: 'Matches the code', changed: (n) => `${n} changed in code`, broken: (n) => `${n} no longer match`,
|
|
18
10
|
fixPrompt: (p, c, b) => `Check ${p} against the code and update it: ${[b && `${b} anchor(s) no longer match`, c && `${c} anchor(s) whose code changed after the page was written`].filter(Boolean).join(', ')}.`,
|
|
19
|
-
|
|
20
|
-
|
|
11
|
+
chose: 'Chose', fromPage: 'Sent by the page’s own script', waiting: 'Waiting for the agent', you: 'You',
|
|
12
|
+
state: {
|
|
13
|
+
listening: (a) => `${a} is listening`, working: (a) => `${a} is working`, idle: (a) => `${a} stopped listening`, stale: (a) => `No reply from ${a} yet`,
|
|
14
|
+
offline: () => 'No agent listening', closed: () => 'Workspace closed',
|
|
15
|
+
},
|
|
21
16
|
};
|
|
22
17
|
|
|
23
18
|
const $ = (id) => document.getElementById(id);
|
|
24
19
|
const app = $('app'); const frame = $('frame'); const msgsEl = $('msgs'); const input = $('input'); const menu = $('menu');
|
|
25
|
-
const state = {
|
|
20
|
+
const state = {
|
|
21
|
+
pages: [], check: { results: [] }, page: null, history: [], pending: new Set(), scroll: {},
|
|
22
|
+
notes: [], agent: { state: 'offline', name: '', since: 0 }, progress: '', cmd: 'npx -y truemap-cli',
|
|
23
|
+
annotate: false, annotateTouched: false, conn: 'live', frameFocusAt: 0,
|
|
24
|
+
};
|
|
26
25
|
|
|
27
26
|
const store = {
|
|
28
27
|
get: (k) => { try { return localStorage.getItem(k); } catch { return null; } },
|
|
@@ -32,14 +31,21 @@
|
|
|
32
31
|
method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Truemap-Token': TOKEN }, body: JSON.stringify(body || {}),
|
|
33
32
|
}).then((r) => r.json()).catch(() => ({}));
|
|
34
33
|
const toFrame = (msg) => { if (state.page && !frame.hidden) frame.contentWindow?.postMessage({ __truemap: true, ...msg }, CONTENT); };
|
|
34
|
+
const agentName = () => AGENTS[state.agent.name] || 'The agent';
|
|
35
35
|
|
|
36
36
|
// ---------- text helpers ----------
|
|
37
37
|
const esc = (s) => String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
38
|
-
const timeOf = (iso) => {
|
|
38
|
+
const timeOf = (iso) => {
|
|
39
|
+
try {
|
|
40
|
+
const d = new Date(iso); const t = d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
|
41
|
+
return d.toDateString() === new Date().toDateString() ? t : `${d.toLocaleDateString([], { month: 'short', day: 'numeric' })}, ${t}`;
|
|
42
|
+
} catch { return ''; }
|
|
43
|
+
};
|
|
39
44
|
function inline(s) {
|
|
40
45
|
return s
|
|
41
46
|
.replace(/`([^`]+)`/g, '<code>$1</code>')
|
|
42
47
|
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
|
|
48
|
+
.replace(/(^|[^*\w])\*([^*\s][^*]*?)\*(?![*\w])/g, '$1<em>$2</em>')
|
|
43
49
|
.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_, t, href) => {
|
|
44
50
|
if (/^[\w./-]+\.html?(#[\w-]*)?$/i.test(href)) return `<a href="#" data-page="${href}">${t}</a>`;
|
|
45
51
|
if (/^https:\/\/[^\s"'<>]+$/i.test(href)) return `<a href="${href}" target="_blank" rel="noopener noreferrer">${t}</a>`;
|
|
@@ -65,24 +71,46 @@
|
|
|
65
71
|
flush(); close();
|
|
66
72
|
return html;
|
|
67
73
|
}
|
|
74
|
+
const fmtElapsed = (ms) => { const s = Math.max(0, Math.round(ms / 1000)); return s < 60 ? `${s}s` : `${Math.floor(s / 60)}m ${String(s % 60).padStart(2, '0')}s`; };
|
|
68
75
|
|
|
69
|
-
// ----------
|
|
76
|
+
// ---------- conversation panel: one button opens and closes it ----------
|
|
70
77
|
const narrow = () => window.matchMedia('(max-width: 860px)').matches;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
78
|
+
const panelOpen = () => !app.classList.contains('panel-closed');
|
|
79
|
+
function setPanel(open, remember = true) {
|
|
80
|
+
app.classList.toggle('panel-closed', !open);
|
|
81
|
+
const b = $('togglePanel');
|
|
82
|
+
b.setAttribute('aria-pressed', String(open));
|
|
83
|
+
b.title = open ? 'Hide conversation' : 'Show conversation';
|
|
84
|
+
b.setAttribute('aria-label', b.title);
|
|
85
|
+
if (open) state.unread = false;
|
|
86
|
+
renderBadge();
|
|
87
|
+
if (remember && !narrow()) store.set('truemap.panel', open ? 'open' : 'closed');
|
|
88
|
+
}
|
|
89
|
+
// While the panel is closed its button carries what is waiting there: pending comments, or a new answer.
|
|
90
|
+
function renderBadge() {
|
|
91
|
+
const u = $('unread'); const open = panelOpen(); const n = state.notes?.length || 0;
|
|
92
|
+
u.hidden = open || (!n && !state.unread);
|
|
93
|
+
u.textContent = !open && n ? String(n) : '';
|
|
94
|
+
u.classList.toggle('count', !open && n > 0);
|
|
95
|
+
}
|
|
96
|
+
setPanel(narrow() ? false : store.get('truemap.panel') !== 'closed', false);
|
|
97
|
+
$('togglePanel').onclick = () => { const open = !panelOpen(); setPanel(open); if (open) input.focus(); };
|
|
98
|
+
|
|
99
|
+
// ---------- annotate ----------
|
|
100
|
+
function setAnnotate(on, { touched = false } = {}) {
|
|
101
|
+
state.annotate = !!on;
|
|
102
|
+
if (touched) { state.annotateTouched = true; store.set('truemap.annotate', on ? 'on' : 'off'); }
|
|
103
|
+
$('annotate').setAttribute('aria-checked', String(state.annotate));
|
|
104
|
+
toFrame({ type: 'annotate', on: state.annotate });
|
|
76
105
|
}
|
|
77
|
-
|
|
78
|
-
$('
|
|
79
|
-
$('
|
|
80
|
-
const chatOpen = () => !app.classList.contains('chat-closed');
|
|
106
|
+
state.annotate = store.get('truemap.annotate') === 'on';
|
|
107
|
+
$('annotate').setAttribute('aria-checked', String(state.annotate));
|
|
108
|
+
$('annotate').onclick = () => setAnnotate(!state.annotate, { touched: true });
|
|
81
109
|
|
|
82
110
|
// ---------- page switcher ----------
|
|
83
111
|
const resultFor = (page) => state.check.results?.find((r) => r.page === page);
|
|
84
112
|
const stateOf = (r) => (r?.counts?.broken ? 'broken' : r?.counts?.changed ? 'changed' : '');
|
|
85
|
-
const titleOf = (p) => (p.page === 'index.html' && (!p.title || p.title === 'index') ?
|
|
113
|
+
const titleOf = (p) => (p.page === 'index.html' && (!p.title || p.title === 'index') ? 'Blueprint' : p.title || p.page);
|
|
86
114
|
|
|
87
115
|
function renderMenu() {
|
|
88
116
|
const list = $('pages'); list.innerHTML = '';
|
|
@@ -94,11 +122,12 @@
|
|
|
94
122
|
const st = stateOf(resultFor(p.page));
|
|
95
123
|
const b = document.createElement('button');
|
|
96
124
|
b.type = 'button'; b.className = 'page-item' + (p.page === state.page ? ' active' : ''); b.setAttribute('role', 'menuitem');
|
|
97
|
-
b.innerHTML = `<span class="pi-text"><span class="pi-title">${esc(titleOf(p))}</span><span class="pi-file">${esc(p.page)}</span></span>${st ? `<span class="
|
|
125
|
+
b.innerHTML = `<span class="pi-text"><span class="pi-title">${esc(titleOf(p))}</span><span class="pi-file">${esc(p.page)}</span></span>${st ? `<span class="tri ${st}" title="${st === 'broken' ? 'Out of date' : 'Code changed'}"></span>` : ''}`;
|
|
98
126
|
b.onclick = () => { closeMenu(); go(p.page); };
|
|
99
127
|
list.appendChild(b);
|
|
100
128
|
if (i === 0 && p.page === 'index.html' && arr.length > 1) list.insertAdjacentHTML('beforeend', '<div class="menu-gap"></div>');
|
|
101
129
|
});
|
|
130
|
+
if (!state.pages.length) list.innerHTML = '<div class="menu-head">No pages in .truemap/ yet</div>';
|
|
102
131
|
}
|
|
103
132
|
function openMenu() { renderMenu(); menu.hidden = false; $('pageBtn').setAttribute('aria-expanded', 'true'); ($('filter').hidden ? menu.querySelector('.page-item.active, .page-item') : $('filter'))?.focus(); }
|
|
104
133
|
function closeMenu() { menu.hidden = true; $('pageBtn').setAttribute('aria-expanded', 'false'); }
|
|
@@ -107,31 +136,69 @@
|
|
|
107
136
|
document.addEventListener('click', (e) => { if (!menu.hidden && !e.target.closest('.switcher')) closeMenu(); });
|
|
108
137
|
window.addEventListener('blur', () => { if (!menu.hidden) closeMenu(); });
|
|
109
138
|
document.addEventListener('keydown', (e) => {
|
|
139
|
+
const typing = e.target.closest?.('input,textarea,select,[contenteditable]');
|
|
110
140
|
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'k') { e.preventDefault(); menu.hidden ? openMenu() : closeMenu(); }
|
|
111
141
|
else if (e.key === 'Escape' && !menu.hidden) { closeMenu(); $('pageBtn').focus(); }
|
|
142
|
+
else if (e.key === 'Escape' && !typing && state.annotate && $('moreMenu').hidden) setAnnotate(false, { touched: true });
|
|
112
143
|
else if (!menu.hidden && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {
|
|
113
144
|
const items = [...menu.querySelectorAll('.page-item')]; if (!items.length) return;
|
|
114
145
|
e.preventDefault();
|
|
115
146
|
const i = items.indexOf(document.activeElement);
|
|
116
147
|
items[(i + (e.key === 'ArrowDown' ? 1 : -1) + items.length) % items.length].focus();
|
|
148
|
+
} else if (!typing && !e.ctrlKey && !e.metaKey && !e.altKey && e.key.toLowerCase() === 'a' && menu.hidden) {
|
|
149
|
+
setAnnotate(!state.annotate, { touched: true });
|
|
117
150
|
}
|
|
118
151
|
});
|
|
119
152
|
$('recheck').onclick = () => { api('/api/check'); closeMenu(); };
|
|
120
153
|
|
|
154
|
+
// ---------- more menu: the page file, export, end the conversation ----------
|
|
155
|
+
const more = $('moreMenu');
|
|
156
|
+
const filePath = () => {
|
|
157
|
+
if (!state.page) return '';
|
|
158
|
+
const sepChar = String(state.root).includes('\\') ? '\\' : '/';
|
|
159
|
+
return [state.root, '.truemap', ...state.page.split('/')].join(sepChar);
|
|
160
|
+
};
|
|
161
|
+
function renderMore() {
|
|
162
|
+
$('moreFile').textContent = state.page ? `.truemap/${state.page}` : 'No page open';
|
|
163
|
+
$('moreFile').title = filePath();
|
|
164
|
+
$('copyPath').disabled = !state.page;
|
|
165
|
+
$('exportPage').disabled = !state.page || state.conn !== 'live';
|
|
166
|
+
const talking = state.conn === 'live' && (state.agent.state === 'listening' || state.agent.state === 'working');
|
|
167
|
+
$('endConv').disabled = !talking;
|
|
168
|
+
$('endHint').textContent = talking ? `Lets ${agentName()} go back to the terminal` : 'No conversation is running';
|
|
169
|
+
}
|
|
170
|
+
const closeMore = () => { more.hidden = true; $('moreBtn').setAttribute('aria-expanded', 'false'); };
|
|
171
|
+
$('moreBtn').onclick = (e) => {
|
|
172
|
+
e.stopPropagation();
|
|
173
|
+
if (!more.hidden) return closeMore();
|
|
174
|
+
closeMenu(); renderMore(); more.hidden = false; $('moreBtn').setAttribute('aria-expanded', 'true');
|
|
175
|
+
};
|
|
176
|
+
document.addEventListener('click', (e) => { if (!more.hidden && !e.target.closest('.more')) closeMore(); });
|
|
177
|
+
window.addEventListener('blur', () => { if (!more.hidden) closeMore(); });
|
|
178
|
+
document.addEventListener('keydown', (e) => { if (e.key === 'Escape' && !more.hidden) { closeMore(); $('moreBtn').focus(); } });
|
|
179
|
+
$('copyPath').onclick = () => {
|
|
180
|
+
navigator.clipboard?.writeText(filePath()).then(() => { $('copyPath').textContent = 'Copied'; setTimeout(() => { $('copyPath').textContent = 'Copy path'; }, 1400); }).catch(() => {});
|
|
181
|
+
};
|
|
182
|
+
$('exportPage').onclick = () => {
|
|
183
|
+
const a = document.createElement('a');
|
|
184
|
+
a.href = `/api/export?page=${encodeURIComponent(state.page)}&t=${encodeURIComponent(TOKEN)}`;
|
|
185
|
+
a.download = ''; document.body.appendChild(a); a.click(); a.remove(); closeMore();
|
|
186
|
+
};
|
|
187
|
+
$('endConv').onclick = () => { api('/api/end'); closeMore(); };
|
|
188
|
+
|
|
121
189
|
function renderHeader() {
|
|
122
190
|
const p = state.pages.find((x) => x.page === state.page);
|
|
123
|
-
$('pageTitle').textContent = p ? titleOf(p) : '
|
|
191
|
+
$('pageTitle').textContent = p ? titleOf(p) : (state.pages.length ? 'Pages' : 'No pages yet');
|
|
124
192
|
$('pagePath').textContent = state.page ? `.truemap/${state.page}` : '';
|
|
125
193
|
document.title = p ? `${titleOf(p)} – truemap` : 'truemap';
|
|
126
194
|
const r = resultFor(state.page); const st = stateOf(r);
|
|
127
|
-
const dot = $('titleDot'); dot.hidden = !st; dot.className = `state-dot ${st}`;
|
|
128
195
|
const chip = $('checkChip');
|
|
129
|
-
if (!r || r.missing || !r.anchors?.length) { chip.hidden = true; return; }
|
|
196
|
+
if (!r || r.missing || !r.anchors?.length || state.conn === 'closed') { chip.hidden = true; return; }
|
|
130
197
|
chip.hidden = false;
|
|
131
198
|
const { changed, broken } = r.counts;
|
|
132
199
|
chip.dataset.state = st || 'ok';
|
|
133
|
-
|
|
134
|
-
chip.title = st ? 'Ask the agent to update this page' :
|
|
200
|
+
$('checkText').textContent = broken ? S.broken(broken) + (changed ? `, ${S.changed(changed)}` : '') : changed ? S.changed(changed) : S.ok;
|
|
201
|
+
chip.title = st ? 'Ask the agent to update this page' : `${r.counts.ok} boxes checked against the code`;
|
|
135
202
|
}
|
|
136
203
|
function sendMarks() {
|
|
137
204
|
const r = resultFor(state.page);
|
|
@@ -146,42 +213,85 @@
|
|
|
146
213
|
setCurrent(page);
|
|
147
214
|
}
|
|
148
215
|
function setCurrent(page) {
|
|
149
|
-
if (state.page !== page) { state.chips = []; renderChips(); }
|
|
150
216
|
state.page = page;
|
|
151
217
|
history.replaceState(null, '', `${location.pathname}${location.search}#page=${encodeURIComponent(page)}`);
|
|
152
218
|
api('/api/viewing', { page });
|
|
153
|
-
renderHeader();
|
|
219
|
+
renderHeader(); renderNotice(); renderNotes();
|
|
154
220
|
}
|
|
155
221
|
function showEmpty() {
|
|
156
|
-
state.page = null; frame.hidden = true; frame.removeAttribute('src'); $('empty').hidden = false; renderHeader();
|
|
222
|
+
state.page = null; frame.hidden = true; frame.removeAttribute('src'); $('empty').hidden = false; renderHeader(); renderNotice();
|
|
157
223
|
}
|
|
158
224
|
const reloadFrame = () => { if (state.page) frame.src = pageUrl(state.page, true); };
|
|
159
225
|
|
|
160
|
-
// ----------
|
|
226
|
+
// ---------- notes (comments collected in annotate mode) ----------
|
|
227
|
+
const notesForPage = () => state.notes.filter((n) => n.page === state.page);
|
|
228
|
+
function syncFrameNotes() { toFrame({ type: 'notes', items: notesForPage().map(({ id, kind, label, src, text, selector, rect }) => ({ id, kind, label, src, text, selector, rect })) }); }
|
|
229
|
+
// Unsent comments survive a reload of this tab.
|
|
230
|
+
try { state.notes = JSON.parse(sessionStorage.getItem('truemap.notes') || '[]'); } catch { state.notes = []; }
|
|
231
|
+
function saveNotes() { try { sessionStorage.setItem('truemap.notes', JSON.stringify(state.notes)); } catch { /* ignore */ } }
|
|
232
|
+
function renderNotes() {
|
|
233
|
+
saveNotes(); renderBadge();
|
|
234
|
+
const list = $('notes'); list.innerHTML = '';
|
|
235
|
+
list.hidden = state.notes.length === 0;
|
|
236
|
+
state.notes.forEach((n, i) => {
|
|
237
|
+
const li = document.createElement('li'); li.className = 'note';
|
|
238
|
+
li.innerHTML = `<span class="mn-num">${i + 1}</span><span class="mn-what"><b>${esc(n.kind === 'text' ? `“${n.label}”` : n.label)}</b>${n.page !== state.page ? ` on ${esc(n.page)}` : ''}</span><span class="note-text">${esc(n.text)}</span><button class="note-x" type="button" aria-label="Remove comment" title="Remove">×</button>`;
|
|
239
|
+
li.querySelector('.note-x').onclick = (e) => { e.stopPropagation(); state.notes = state.notes.filter((x) => x.id !== n.id); renderNotes(); syncFrameNotes(); };
|
|
240
|
+
li.onclick = () => { if (n.page !== state.page) go(n.page); else toFrame({ type: 'focus-note', id: n.id }); };
|
|
241
|
+
list.appendChild(li);
|
|
242
|
+
});
|
|
243
|
+
const count = state.notes.length;
|
|
244
|
+
$('send').textContent = count ? `Send ${count} comment${count > 1 ? 's' : ''}` : 'Send';
|
|
245
|
+
input.placeholder = count ? S.placeholderNotes : S.placeholder;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// ---------- conversation ----------
|
|
249
|
+
function whoLine(role, at, name) {
|
|
250
|
+
const el = document.createElement('div'); el.className = 'who';
|
|
251
|
+
el.innerHTML = `<b>${esc(role === 'agent' ? (AGENTS[name] || name || agentName()) : S.you)}</b><span>${timeOf(at)}</span>`;
|
|
252
|
+
return el;
|
|
253
|
+
}
|
|
254
|
+
let lastRole = null;
|
|
161
255
|
function renderAll() {
|
|
162
|
-
msgsEl.innerHTML =
|
|
256
|
+
msgsEl.innerHTML = ''; lastRole = null;
|
|
257
|
+
if (!state.history.length) {
|
|
258
|
+
msgsEl.innerHTML = `<div class="intro"><h2>Ask about anything on the page</h2><ol>
|
|
259
|
+
<li>Turn on <b>Annotate</b> (or press <kbd>A</kbd>), then click a box or select text to comment on it.</li>
|
|
260
|
+
<li>Add as many comments as you like. They go out together, each with the code it points to.</li>
|
|
261
|
+
<li>Or just type a question below.</li></ol></div>`;
|
|
262
|
+
}
|
|
163
263
|
for (const e of state.history) appendEntry(e, false);
|
|
264
|
+
renderWorking();
|
|
164
265
|
scrollChat();
|
|
165
266
|
}
|
|
166
|
-
function appendEntry(e, scroll = true) {
|
|
267
|
+
function appendEntry(e, scroll = true, live = false) {
|
|
268
|
+
msgsEl.querySelector('.intro')?.remove();
|
|
269
|
+
if (e.role !== lastRole) { msgsEl.appendChild(whoLine(e.role, e.at, e.agent)); lastRole = e.role; }
|
|
167
270
|
const el = document.createElement('div');
|
|
168
271
|
if (e.role === 'agent') {
|
|
169
272
|
el.className = 'msg agent';
|
|
170
|
-
el.innerHTML =
|
|
171
|
-
|
|
172
|
-
if (!chatOpen()) $('unread').hidden = false;
|
|
273
|
+
el.innerHTML = md(e.text);
|
|
274
|
+
if (live && !panelOpen()) { state.unread = true; renderBadge(); }
|
|
173
275
|
} else {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
276
|
+
el.className = 'msg user'; el.dataset.id = e.id;
|
|
277
|
+
let body = '';
|
|
278
|
+
if (e.kind === 'answer') {
|
|
279
|
+
body = `<span class="answer-q">${esc(e.answer?.question || '')}</span>${esc(S.chose)}: <strong>${esc(e.answer?.choice || '')}</strong>${e.answer?.note ? `<br>${esc(e.answer.note)}` : ''}`;
|
|
280
|
+
} else {
|
|
281
|
+
const notes = (e.context || []).filter((c) => c.comment);
|
|
282
|
+
const sel = (e.context || []).filter((c) => !c.comment);
|
|
283
|
+
if (notes.length) {
|
|
284
|
+
body += `<ol class="msg-notes">${notes.map((c, i) => `<li class="mn"><span class="mn-num">${i + 1}</span><span class="mn-what"><b>${esc(c.kind === 'text' ? `“${c.label}”` : c.label)}</b>${c.src ? ` ${esc(c.src)}` : ''}</span><span>${esc(c.comment)}</span></li>`).join('')}</ol>`;
|
|
285
|
+
}
|
|
286
|
+
if (sel.length) body += `<div class="mn-what">${sel.map((c) => `<b>${esc(c.label)}</b>`).join(', ')}</div>`;
|
|
287
|
+
if (e.text) body += `<div class="msg-text">${esc(e.text).replace(/\n/g, '<br>')}</div>`;
|
|
288
|
+
}
|
|
289
|
+
el.innerHTML = body
|
|
290
|
+
+ (e.from === 'page' ? `<div class="from-page">${S.fromPage}</div>` : '')
|
|
291
|
+
+ (state.pending.has(e.id) ? `<div class="waiting">${S.waiting}</div>` : '');
|
|
183
292
|
}
|
|
184
293
|
msgsEl.appendChild(el);
|
|
294
|
+
renderWorking();
|
|
185
295
|
if (scroll) scrollChat();
|
|
186
296
|
}
|
|
187
297
|
const scrollChat = () => { msgsEl.scrollTop = msgsEl.scrollHeight; };
|
|
@@ -192,49 +302,82 @@
|
|
|
192
302
|
const [page] = a.dataset.page.split('#');
|
|
193
303
|
if (state.pages.some((p) => p.page === page)) go(page);
|
|
194
304
|
});
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
305
|
+
function markPending(id, on) {
|
|
306
|
+
const el = msgsEl.querySelector(`.msg[data-id="${CSS.escape(id)}"]`);
|
|
307
|
+
if (!el) return;
|
|
308
|
+
el.querySelector('.waiting')?.remove();
|
|
309
|
+
if (on) el.insertAdjacentHTML('beforeend', `<div class="waiting">${S.waiting}</div>`);
|
|
200
310
|
}
|
|
201
311
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
312
|
+
// ---------- agent state ----------
|
|
313
|
+
let tick = null;
|
|
314
|
+
function renderWorking() {
|
|
315
|
+
msgsEl.querySelector('.working')?.remove();
|
|
316
|
+
if (state.agent.state !== 'working' || state.conn !== 'live' || isStale()) return;
|
|
317
|
+
const w = document.createElement('div'); w.className = 'working';
|
|
318
|
+
w.innerHTML = `<i></i><span>${esc(state.progress || `${agentName()} is working on it`)}</span>`;
|
|
319
|
+
msgsEl.appendChild(w);
|
|
320
|
+
}
|
|
321
|
+
function renderAgent() {
|
|
322
|
+
const s = state.conn === 'closed' ? 'closed' : state.agent.state;
|
|
323
|
+
const shown = s === 'working' && isStale() ? 'stale' : s;
|
|
324
|
+
$('agent').dataset.state = shown;
|
|
325
|
+
$('agentLabel').textContent = S.state[shown] ? S.state[shown](agentName()) : shown;
|
|
326
|
+
clearInterval(tick);
|
|
327
|
+
const el = $('elapsed');
|
|
328
|
+
if (s === 'working' && state.agent.since) {
|
|
329
|
+
let stale = isStale();
|
|
330
|
+
const upd = () => {
|
|
331
|
+
el.textContent = fmtElapsed(Date.now() - state.agent.since);
|
|
332
|
+
if (isStale() !== stale) { stale = !stale; renderAgent(); }
|
|
210
333
|
};
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
334
|
+
upd(); el.hidden = false; tick = setInterval(upd, 1000);
|
|
335
|
+
} else el.hidden = true;
|
|
336
|
+
renderWorking(); renderNotice();
|
|
337
|
+
}
|
|
338
|
+
function renderNotice() {
|
|
339
|
+
const n = $('notice');
|
|
340
|
+
const s = state.conn === 'closed' ? 'closed' : state.agent.state;
|
|
341
|
+
const page = state.page && state.page !== 'index.html' ? ` ${state.page}` : '';
|
|
342
|
+
// One pattern for every state that needs the user: what is true, what to do, the command to give the agent.
|
|
343
|
+
const listen = cmdBox(`${state.cmd} listen`);
|
|
344
|
+
let html = '';
|
|
345
|
+
if (s === 'closed') {
|
|
346
|
+
html = `<b>This workspace is closed</b>Your pages are still in <code>.truemap/</code>. To reopen this page, ask your agent to run:${cmdBox(state.cmd + page)}`;
|
|
347
|
+
} else if (s === 'offline') {
|
|
348
|
+
html = `<b>Your messages will wait here</b>To get answers, ask your agent in its terminal session to run:${listen}`;
|
|
349
|
+
} else if (s === 'idle') {
|
|
350
|
+
html = `<b>Your messages will wait here</b>To continue, ask ${esc(agentName())} in the terminal to run:${listen}`;
|
|
351
|
+
} else if (s === 'working' && isStale()) {
|
|
352
|
+
html = `<b>No reply for ${Math.round(STALE_MS / 60000)} minutes</b>${esc(agentName())} may have stopped, or be waiting for you in the terminal. Check there, or ask it to run:${listen}<p class="after">If it no longer knows your last comments, send them again.</p>`;
|
|
221
353
|
}
|
|
222
|
-
|
|
354
|
+
n.innerHTML = html; n.hidden = !html;
|
|
355
|
+
n.querySelector('.copy')?.addEventListener('click', (e) => {
|
|
356
|
+
const text = n.querySelector('.cmd code').textContent;
|
|
357
|
+
navigator.clipboard?.writeText(text).then(() => { e.target.textContent = 'Copied'; setTimeout(() => { e.target.textContent = 'Copy'; }, 1400); }).catch(() => {});
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
const STALE_MS = 3 * 60 * 1000;
|
|
361
|
+
const isStale = () => state.agent.state === 'working' && state.conn === 'live'
|
|
362
|
+
&& Date.now() - Math.max(state.agent.since || 0, state.lastAgentEvent || 0) > STALE_MS;
|
|
363
|
+
const cmdBox = (cmd) => `<div class="cmd"><code>${esc(cmd)}</code><button class="copy" type="button">Copy</button></div>`;
|
|
364
|
+
function setAgent(a) {
|
|
365
|
+
if (!a) return;
|
|
366
|
+
state.agent = { state: a.state || 'offline', name: a.name || state.agent.name, since: a.since || 0 };
|
|
367
|
+
if (state.agent.state !== 'working') state.progress = '';
|
|
368
|
+
renderAgent();
|
|
223
369
|
}
|
|
224
370
|
|
|
371
|
+
// ---------- sending ----------
|
|
225
372
|
async function send(text, extra = {}) {
|
|
226
373
|
text = String(text || '').trim();
|
|
227
|
-
const
|
|
374
|
+
const withNotes = !extra.answer && !extra.noNotes;
|
|
375
|
+
const notes = withNotes ? state.notes : [];
|
|
376
|
+
const context = extra.context ?? notes.map((n) => ({ kind: n.kind, label: n.label, src: n.src, excerpt: n.excerpt, comment: n.text, page: n.page }));
|
|
228
377
|
if (!text && !context.length && !extra.answer) return;
|
|
229
378
|
const r = await api('/api/send', { text, context, page: state.page, ...extra });
|
|
230
379
|
if (r.id && !r.delivered) { state.pending.add(r.id); markPending(r.id, true); }
|
|
231
|
-
if (
|
|
232
|
-
}
|
|
233
|
-
function markPending(id, on) {
|
|
234
|
-
const el = msgsEl.querySelector(`.msg[data-id="${CSS.escape(id)}"] .meta`);
|
|
235
|
-
if (!el) return;
|
|
236
|
-
el.querySelector('.pending')?.remove();
|
|
237
|
-
if (on) el.insertAdjacentHTML('afterbegin', `<span class="pending">${S.queued}</span>`);
|
|
380
|
+
if (withNotes && notes.length && r.id) { state.notes = []; renderNotes(); syncFrameNotes(); }
|
|
238
381
|
}
|
|
239
382
|
|
|
240
383
|
const autosize = () => { input.style.height = 'auto'; input.style.height = Math.min(input.scrollHeight, 180) + 'px'; };
|
|
@@ -245,80 +388,105 @@
|
|
|
245
388
|
$('composer').addEventListener('submit', (e) => {
|
|
246
389
|
e.preventDefault();
|
|
247
390
|
const text = input.value;
|
|
248
|
-
if (!text.trim() && !state.
|
|
391
|
+
if (!text.trim() && !state.notes.length) { input.focus(); return; }
|
|
249
392
|
input.value = ''; autosize();
|
|
250
393
|
send(text);
|
|
251
394
|
});
|
|
252
|
-
input.placeholder = S.placeholder;
|
|
253
395
|
|
|
254
|
-
$('drawBlueprint').onclick = () => {
|
|
396
|
+
$('drawBlueprint').onclick = () => { setPanel(true); send(S.drawPrompt, { context: [], noNotes: true }); };
|
|
255
397
|
$('checkChip').onclick = () => {
|
|
256
398
|
const r = resultFor(state.page);
|
|
257
399
|
if (!r || (!r.counts.changed && !r.counts.broken)) return;
|
|
258
|
-
|
|
259
|
-
send(S.fixPrompt(`.truemap/${state.page}`, r.counts.changed, r.counts.broken), { context: [] });
|
|
400
|
+
setPanel(true);
|
|
401
|
+
send(S.fixPrompt(`.truemap/${state.page}`, r.counts.changed, r.counts.broken), { context: [], noNotes: true });
|
|
260
402
|
};
|
|
261
403
|
|
|
262
404
|
// A decision only counts as the user's if the user actually moved focus into the page.
|
|
263
405
|
window.addEventListener('blur', () => { setTimeout(() => { if (document.activeElement === frame) state.frameFocusAt = Date.now(); }, 0); });
|
|
264
406
|
const userIsInFrame = () => document.activeElement === frame && state.frameFocusAt > 0;
|
|
265
407
|
|
|
266
|
-
// ---------- messages from the page
|
|
408
|
+
// ---------- messages from the page frame ----------
|
|
267
409
|
window.addEventListener('message', (e) => {
|
|
268
410
|
if (e.source !== frame.contentWindow || e.origin !== CONTENT || !e.data || !e.data.__truemap) return;
|
|
269
411
|
const d = e.data;
|
|
270
412
|
if (d.type === 'loaded') {
|
|
271
413
|
if (d.page && d.page !== state.page) setCurrent(d.page);
|
|
272
414
|
renderHeader(); sendMarks();
|
|
415
|
+
if (!state.annotateTouched && typeof d.annotate === 'boolean') state.annotate = d.annotate;
|
|
416
|
+
setAnnotate(state.annotate);
|
|
417
|
+
syncFrameNotes();
|
|
273
418
|
const y = state.scroll[d.page]; if (y) toFrame({ type: 'scroll', y });
|
|
274
419
|
} else if (d.type === 'scroll') {
|
|
275
420
|
state.scroll[d.page] = d.y;
|
|
276
|
-
} else if (d.type === '
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
421
|
+
} else if (d.type === 'note' || d.type === 'note-update') {
|
|
422
|
+
const n = d.note || {};
|
|
423
|
+
const note = { id: String(n.id), page: d.page, kind: n.kind === 'text' ? 'text' : 'element', label: String(n.label || '').slice(0, 80), src: String(n.src || ''), excerpt: String(n.excerpt || '').slice(0, 400), text: String(n.text || '').slice(0, 4000), selector: String(n.selector || ''), rect: n.rect && typeof n.rect === 'object' ? { left: +n.rect.left || 0, top: +n.rect.top || 0, right: +n.rect.right || 0, bottom: +n.rect.bottom || 0 } : null };
|
|
424
|
+
const i = state.notes.findIndex((x) => x.id === note.id);
|
|
425
|
+
if (i >= 0) state.notes[i] = note; else state.notes.push(note);
|
|
426
|
+
renderNotes(); syncFrameNotes();
|
|
427
|
+
if (d.send) send(input.value).then(() => { input.value = ''; autosize(); });
|
|
428
|
+
else if (!panelOpen() && !narrow()) setPanel(true, false);
|
|
429
|
+
} else if (d.type === 'note-remove') {
|
|
430
|
+
state.notes = state.notes.filter((x) => x.id !== d.id); renderNotes(); syncFrameNotes();
|
|
431
|
+
} else if (d.type === 'toggle-annotate') {
|
|
432
|
+
setAnnotate(!state.annotate, { touched: true });
|
|
433
|
+
} else if (d.type === 'annotate-off') {
|
|
434
|
+
setAnnotate(false, { touched: true });
|
|
283
435
|
} else if (d.type === 'answer') {
|
|
284
436
|
const fromUser = d.trusted === true && userIsInFrame();
|
|
285
437
|
send('', {
|
|
286
|
-
kind: 'answer', from: fromUser ? 'user' : 'page',
|
|
438
|
+
kind: 'answer', from: fromUser ? 'user' : 'page', noNotes: true,
|
|
287
439
|
context: d.src ? [{ kind: 'element', label: d.question, src: d.src }] : [],
|
|
288
440
|
answer: { question: d.question, choice: d.choice, note: d.note },
|
|
289
441
|
});
|
|
290
442
|
} else if (d.type === 'page-send') {
|
|
291
|
-
if (d.text) send(d.text, { from: 'page', context: [],
|
|
443
|
+
if (d.text) send(d.text, { from: 'page', context: [], noNotes: true });
|
|
292
444
|
}
|
|
293
445
|
});
|
|
294
446
|
|
|
295
447
|
// ---------- live events from the server ----------
|
|
448
|
+
let closedTimer = null;
|
|
449
|
+
function setConn(c) {
|
|
450
|
+
if (state.conn === c) return;
|
|
451
|
+
state.conn = c; app.dataset.conn = c;
|
|
452
|
+
$('annotate').disabled = c === 'closed';
|
|
453
|
+
renderHeader();
|
|
454
|
+
renderAgent();
|
|
455
|
+
}
|
|
296
456
|
function connect() {
|
|
297
457
|
const es = new EventSource(`/api/events?t=${encodeURIComponent(TOKEN)}`);
|
|
298
458
|
let greeted = false;
|
|
299
459
|
const guard = setTimeout(() => { if (!greeted) { es.close(); connect(); } }, 3000);
|
|
460
|
+
es.onerror = () => {
|
|
461
|
+
if (closedTimer) return;
|
|
462
|
+
closedTimer = setTimeout(() => { if (es.readyState !== EventSource.OPEN) setConn('closed'); }, 5000);
|
|
463
|
+
};
|
|
300
464
|
es.onmessage = (ev) => {
|
|
301
465
|
let d; try { d = JSON.parse(ev.data); } catch { return; }
|
|
302
466
|
if (d.type === 'hello') {
|
|
303
|
-
greeted = true; clearTimeout(guard);
|
|
467
|
+
greeted = true; clearTimeout(guard); clearTimeout(closedTimer); closedTimer = null;
|
|
468
|
+
setConn('live');
|
|
304
469
|
state.pages = d.pages || []; state.check = d.check || { results: [] }; state.history = d.history || [];
|
|
305
470
|
state.pending = new Set(d.pending || []);
|
|
471
|
+
if (d.cmd) state.cmd = d.cmd;
|
|
472
|
+
state.root = String(d.root || '');
|
|
473
|
+
if (typeof d.annotate === 'boolean') { state.annotate = d.annotate; state.annotateTouched = true; $('annotate').setAttribute('aria-checked', String(d.annotate)); }
|
|
306
474
|
$('ver').textContent = `truemap ${d.version}`;
|
|
307
475
|
$('project').textContent = String(d.root || '').split(/[\\/]/).filter(Boolean).pop() || '';
|
|
308
|
-
|
|
309
|
-
renderAll();
|
|
476
|
+
state.agent = { state: d.agent?.state || 'offline', name: d.agent?.name || '', since: d.agent?.since || 0 };
|
|
477
|
+
renderAll(); renderAgent(); renderNotes();
|
|
310
478
|
const fromHash = decodeURIComponent((location.hash.match(/page=([^&]+)/) || [])[1] || '');
|
|
311
479
|
const target = [state.page, fromHash, d.current].find((p) => p && state.pages.some((x) => x.page === p)) || state.pages[0]?.page;
|
|
312
480
|
target ? go(target, { force: !frame.getAttribute('src') }) : showEmpty();
|
|
313
481
|
} else if (d.type === 'chat') {
|
|
314
|
-
state.history.push(d.entry); appendEntry(d.entry);
|
|
482
|
+
state.history.push(d.entry); appendEntry(d.entry, true, true);
|
|
483
|
+
if (d.entry.role === 'agent') state.lastAgentEvent = Date.now();
|
|
315
484
|
} else if (d.type === 'agent') {
|
|
316
|
-
setAgent(d.
|
|
317
|
-
if (d.state !== 'working') $('progress').hidden = true;
|
|
485
|
+
setAgent(d.agent);
|
|
318
486
|
} else if (d.type === 'delivered') {
|
|
319
487
|
for (const id of d.ids || []) { state.pending.delete(id); markPending(id, false); }
|
|
320
488
|
} else if (d.type === 'progress') {
|
|
321
|
-
|
|
489
|
+
state.progress = d.text || ''; state.lastAgentEvent = Date.now(); renderWorking(); renderNotice(); scrollChat();
|
|
322
490
|
} else if (d.type === 'pages') {
|
|
323
491
|
state.pages = d.pages || []; renderHeader(); if (!menu.hidden) renderMenu();
|
|
324
492
|
if (!state.page && state.pages[0]) go(state.pages[0].page);
|
|
@@ -330,10 +498,15 @@
|
|
|
330
498
|
state.check = d.check || { results: [] }; renderHeader(); sendMarks(); if (!menu.hidden) renderMenu();
|
|
331
499
|
} else if (d.type === 'navigate') {
|
|
332
500
|
if (d.pages) state.pages = d.pages;
|
|
501
|
+
if (typeof d.annotate === 'boolean') { state.annotate = d.annotate; state.annotateTouched = true; }
|
|
333
502
|
if (d.page && state.pages.some((p) => p.page === d.page)) go(d.page, { force: true });
|
|
503
|
+
else setAnnotate(state.annotate);
|
|
334
504
|
renderHeader();
|
|
505
|
+
} else if (d.type === 'closing') {
|
|
506
|
+
es.close(); setConn('closed');
|
|
335
507
|
}
|
|
336
508
|
};
|
|
337
509
|
}
|
|
510
|
+
renderNotes();
|
|
338
511
|
connect();
|
|
339
512
|
})();
|