@polycode-projects/the-mechanical-code-talker 5.0.20 → 5.0.22
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 +4 -27
- package/package.json +1 -6
- package/src/adapters/toml-config.mjs +5 -0
- package/src/domain/domain.mjs +8 -0
- package/src/domain/interpret/normalize.mjs +86 -1
- package/src/domain/router/recognize.mjs +0 -0
- package/src/domain/world-snapshot.mjs +25 -0
- package/src/services/adventure-autoplay.mjs +1 -3
- package/src/services/adventure.mjs +17 -29
- package/src/services/chat.mjs +325 -13
- package/src/services/viz-boot.mjs +8 -8
- package/src/surfaces/web/memory-ask-browser.bundle.js +91 -91
- package/src/domain/code-explorer-hints.mjs +0 -180
- package/src/services/code-explorer-viz.mjs +0 -596
- package/src/surfaces/web/code-explorer-browser-entry.mjs +0 -253
|
@@ -1,596 +0,0 @@
|
|
|
1
|
-
// code-explorer-viz.mjs — the code explorer as a full-viewport IDE shell:
|
|
2
|
-
// a title bar (graph source, Open graph…/Open repo…), an explorer sidebar
|
|
3
|
-
// reading each import/call/contains edge back as a plain sentence, a chat
|
|
4
|
-
// centre over the same graph with a rail of suggested questions, and a status
|
|
5
|
-
// bar carrying the graph's own counts. Clicking a term asks the engine what
|
|
6
|
-
// relates to it (askRelatedFacts, over tmct_ask) and renders the answer, so the
|
|
7
|
-
// sidebar and the chat put the same question to the same place.
|
|
8
|
-
//
|
|
9
|
-
// The chat session seeds BOTH the loaded code graph and chat.html's
|
|
10
|
-
// general-knowledge bands (./chat-seed.json, fetched lazily at runtime), so one
|
|
11
|
-
// conversation answers "what is a queue" and "what imports src/core/model.mjs"
|
|
12
|
-
// alike — and degrades to graph-only when the seed asset is unavailable.
|
|
13
|
-
//
|
|
14
|
-
// The derivations are pure so the shell, the packaging scripts, and the unit
|
|
15
|
-
// tests all share one code path; renderCodeExplorerHtml builds one
|
|
16
|
-
// self-contained document that both packagers ship unchanged — the site build
|
|
17
|
-
// (scripts/build-demo-site.mjs) and the Electron desktop shell
|
|
18
|
-
// (scripts/build-electron-app.mjs). Panels are plain sections inside the
|
|
19
|
-
// sidebar/centre grid, so a later panel is one more <section class="panel">,
|
|
20
|
-
// not a re-architecture.
|
|
21
|
-
|
|
22
|
-
import { THEME_TOKENS_CSS, SERIF_STACK, MONO_STACK, escapeHtml, embedJson, embedScriptText, demoEyebrowHtml, EYEBROW_LINKS_CSS } from "./viz-theme.mjs";
|
|
23
|
-
import { generateCodeHints } from "../domain/code-explorer-hints.mjs";
|
|
24
|
-
import { phraseForRelation } from "../domain/ask-vocab.mjs";
|
|
25
|
-
import { fetchWithProgress } from "./memory-panel-viz.mjs";
|
|
26
|
-
|
|
27
|
-
// Third-person verb for each stored relation kind, symbol grain folded onto
|
|
28
|
-
// its coarse sibling — derived from ask-vocab.mjs's own RELATIONS table
|
|
29
|
-
// rather than a second hand-curated relation-verb table.
|
|
30
|
-
export function edgePhrase(kind) {
|
|
31
|
-
return phraseForRelation(kind);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Both packagers (build-electron-app.mjs, build-demo-site.mjs) place
|
|
35
|
-
// vendor/wink.js one level below the page they render, so the same loader
|
|
36
|
-
// script inlines into renderCodeExplorerHtml's `winkLoaderInline` slot for
|
|
37
|
-
// either channel.
|
|
38
|
-
export const VENDOR_WINK_LOADER_JS = `window.__WINK_LOADER__ = async function () {
|
|
39
|
-
var m = await import("./vendor/wink.js");
|
|
40
|
-
return { winkNLP: m.winkNLP, model: m.model };
|
|
41
|
-
};`;
|
|
42
|
-
|
|
43
|
-
export const DESKTOP_APP_URL = "https://gitlab.com/polycode-projects/the-mechanical-code-talker#the-code-explorer-desktop";
|
|
44
|
-
|
|
45
|
-
const LEDGER_ROW_LIMIT_DEFAULT = 4000;
|
|
46
|
-
|
|
47
|
-
/** Pure derivation over an entities payload (individuals + objectProperties).
|
|
48
|
-
* Returns { rows, terms, focus, stats, meta } — rows are one readable
|
|
49
|
-
* sentence per example edge, terms is the degree-ranked label index, and the
|
|
50
|
-
* neighbourhood of `focus` survives a row cap first so a huge graph degrades
|
|
51
|
-
* to "local + rest" rather than truncating the centre. */
|
|
52
|
-
export function computeCodeLedger(payload, { focus = null, rowLimit = LEDGER_ROW_LIMIT_DEFAULT } = {}) {
|
|
53
|
-
const individuals = Array.isArray(payload?.individuals) ? payload.individuals : [];
|
|
54
|
-
const classOf = new Map();
|
|
55
|
-
for (const ind of individuals) if (ind?.label && !classOf.has(ind.label)) classOf.set(ind.label, ind.class || "");
|
|
56
|
-
|
|
57
|
-
const groups = Array.isArray(payload?.objectProperties) ? payload.objectProperties : [];
|
|
58
|
-
const rows = [];
|
|
59
|
-
const degree = new Map();
|
|
60
|
-
const kindCounts = new Map();
|
|
61
|
-
const bumpTerm = (t) => { if (t) degree.set(t, (degree.get(t) || 0) + 1); };
|
|
62
|
-
for (const g of groups) {
|
|
63
|
-
if (!g?.predicate) continue;
|
|
64
|
-
const kind = String(g.predicate);
|
|
65
|
-
kindCounts.set(kind, (kindCounts.get(kind) || 0) + (Number(g.count) || 0));
|
|
66
|
-
for (const e of Array.isArray(g.examples) ? g.examples : []) {
|
|
67
|
-
const s = e?.subjectLabel || e?.subject;
|
|
68
|
-
const o = e?.objectLabel || e?.object;
|
|
69
|
-
if (!s || !o) continue;
|
|
70
|
-
rows.push({ s, kind, phrase: edgePhrase(kind), o, sClass: classOf.get(s) || "", oClass: classOf.get(o) || "" });
|
|
71
|
-
bumpTerm(s); bumpTerm(o);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const terms = [...degree.entries()]
|
|
76
|
-
.map(([term, deg]) => ({ term, degree: deg, class: classOf.get(term) || "" }))
|
|
77
|
-
.sort((a, b) => b.degree - a.degree || a.term.localeCompare(b.term));
|
|
78
|
-
|
|
79
|
-
let focusTerm = focus && degree.has(focus) ? focus : null;
|
|
80
|
-
if (!focusTerm && terms.length) focusTerm = terms[0].term;
|
|
81
|
-
|
|
82
|
-
const classCounts = new Map();
|
|
83
|
-
for (const ind of individuals) if (ind?.class) classCounts.set(ind.class, (classCounts.get(ind.class) || 0) + 1);
|
|
84
|
-
const stats = {
|
|
85
|
-
individuals: individuals.length,
|
|
86
|
-
edges: rows.length,
|
|
87
|
-
classes: [...classCounts.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])),
|
|
88
|
-
kinds: [...kindCounts.entries()].filter(([, c]) => c > 0).sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])),
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const total = rows.length;
|
|
92
|
-
let shown = rows;
|
|
93
|
-
if (total > rowLimit) {
|
|
94
|
-
const near = new Set([focusTerm]);
|
|
95
|
-
for (const r of rows) { if (r.s === focusTerm) near.add(r.o); if (r.o === focusTerm) near.add(r.s); }
|
|
96
|
-
const inHood = (r) => near.has(r.s) || near.has(r.o);
|
|
97
|
-
shown = [...rows.filter(inHood), ...rows.filter((r) => !inHood(r))].slice(0, rowLimit);
|
|
98
|
-
}
|
|
99
|
-
return { rows: shown, terms, focus: focusTerm, stats, meta: { shown: shown.length, total, truncated: shown.length < total } };
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/** Everything the page embeds, derived once from a payload: the ledger, the
|
|
103
|
-
* degree-ranked terms, the suggested queries, and the focus symbol. */
|
|
104
|
-
export function computeCodeExplorerData(payload, opts = {}) {
|
|
105
|
-
const ledger = computeCodeLedger(payload, opts);
|
|
106
|
-
const { focus, hints } = generateCodeHints(payload, { focus: ledger.focus });
|
|
107
|
-
return { payload, ledger, hints, focus: ledger.focus || focus, meta: { title: opts.title || "code graph" } };
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// ---- client script pieces, spliced into CLIENT_JS below -------------------
|
|
111
|
-
// Named module functions so the page's own source reads as a list of calls
|
|
112
|
-
// into real, unit-tested code rather than an inlined block. Each is written
|
|
113
|
-
// to survive a `.toString()` round trip into a browser `<script>` tag: no
|
|
114
|
-
// closure over this module's own scope, only its parameters and whichever
|
|
115
|
-
// sibling below it is also spliced into the same page script.
|
|
116
|
-
|
|
117
|
-
/** One line naming the loaded graph's own size: individual/edge counts, then
|
|
118
|
-
* up to four of its most common classes. */
|
|
119
|
-
export function statsSummaryLine(ledgerStats) {
|
|
120
|
-
var parts = [ledgerStats.individuals + " individuals", ledgerStats.edges + " edges"];
|
|
121
|
-
var cls = ledgerStats.classes.slice(0, 4).map(function (c) { return c[1] + " " + c[0]; });
|
|
122
|
-
return parts.concat(cls).join(" · ");
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/** Every fact the chat can draw on: the graph's own edges plus whatever the
|
|
126
|
-
* general-knowledge seed has counted so far. */
|
|
127
|
-
export function factTotalText(edges, seedFacts) {
|
|
128
|
-
return (edges + seedFacts).toLocaleString();
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export function rowKey(row) { return JSON.stringify([row.s, row.kind, row.o]); }
|
|
132
|
-
|
|
133
|
-
/** The sidebar's rows: the engine's own answer about the focus when it
|
|
134
|
-
* grounded one, the row list's plain filter otherwise, then whatever
|
|
135
|
-
* neither named — a fold over the rest of the graph. */
|
|
136
|
-
export function focusRowsHtml(data, related) {
|
|
137
|
-
var focus = data.focus;
|
|
138
|
-
var rows = data.ledger.rows;
|
|
139
|
-
var grounded = Boolean(related && related.grounded);
|
|
140
|
-
var near = grounded ? related.rows : rows.filter(function (r) { return r.s === focus || r.o === focus; });
|
|
141
|
-
var nearKeys = {};
|
|
142
|
-
near.forEach(function (r) { nearKeys[rowKey(r)] = true; });
|
|
143
|
-
var rest = rows.filter(function (r) { return !nearKeys[rowKey(r)]; });
|
|
144
|
-
var ordered = near.concat(rest);
|
|
145
|
-
return ordered.map(function (r) {
|
|
146
|
-
var hot = (r.s === focus || r.o === focus) ? " row-focus" : "";
|
|
147
|
-
return '<li class="row' + hot + '">'
|
|
148
|
-
+ '<button class="term" data-term="' + escapeHtml(r.s) + '">' + escapeHtml(r.s) + '</button> '
|
|
149
|
-
+ '<span class="verb">' + escapeHtml(r.phrase) + '</span> '
|
|
150
|
-
+ '<button class="term" data-term="' + escapeHtml(r.o) + '">' + escapeHtml(r.o) + '</button>'
|
|
151
|
-
+ '</li>';
|
|
152
|
-
}).join("") || (grounded
|
|
153
|
-
? '<li class="row muted">nothing in this graph relates to ' + escapeHtml(focus) + '.</li>'
|
|
154
|
-
: '<li class="row muted">no edges in this graph.</li>');
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export function hintsHtml(hints) {
|
|
158
|
-
return hints.map(function (h) {
|
|
159
|
-
return '<button class="hint" data-q="' + escapeHtml(h.text) + '" title="' + escapeHtml(h.rationale) + '">'
|
|
160
|
-
+ escapeHtml(h.text) + '</button>';
|
|
161
|
-
}).join("") || '<span class="muted">nothing to suggest for this graph.</span>';
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export function turnHtml(role, text) {
|
|
165
|
-
return '<span class="who">' + (role === "you" ? "you" : "tmct") + '</span><span class="said">' + escapeHtml(text) + '</span>';
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export function chatEmptyStateHtml(hasEngine) {
|
|
169
|
-
return hasEngine
|
|
170
|
-
? '<p class="chat-empty-head">Ask the graph, or ask it anything</p>'
|
|
171
|
-
+ '<p>Ask about the code graph on the left, or about anything its general knowledge covers, like “what is a queue”.</p>'
|
|
172
|
-
+ '<p class="chat-empty-hint">Try one of the questions below, or type your own.</p>'
|
|
173
|
-
: '<p class="chat-empty-head">Static view</p>'
|
|
174
|
-
+ '<p>This page shows a fixed snapshot of the graph. The live chat is not available here.</p>';
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export function mbText(n) { return (n / 1048576).toFixed(1); }
|
|
178
|
-
|
|
179
|
-
/** How many of the seed's own individuals are facts — the general-knowledge
|
|
180
|
-
* half of the fact-total pill. */
|
|
181
|
-
export function seedFactsFromPayload(payload) {
|
|
182
|
-
return (payload.individuals || []).filter(function (i) { return i.class === "Fact"; }).length;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export function seedLoadingNote(loadedBytes, totalBytes) {
|
|
186
|
-
return "loading general knowledge… " + mbText(loadedBytes) + (totalBytes ? " of " + mbText(totalBytes) : "") + " MB";
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/** One retry with a cache-busting query param, fetch side only: a CDN edge
|
|
190
|
-
* can serve a corrupted or truncated precompressed response (a transient
|
|
191
|
-
* bad cache entry, not a code defect — real bytes decompress fine, and the
|
|
192
|
-
* same URL fetched moments later is clean), and JSON.parse throwing is the
|
|
193
|
-
* only signal of that. `fetcher` is `fetchWithProgress` (or a stand-in for
|
|
194
|
-
* a test); `baseUrl` already carries the build's own content-hash query
|
|
195
|
-
* string, if any, so the bust param joins with `&` or `?` accordingly. */
|
|
196
|
-
export async function fetchSeedPayloadWithRetry(fetcher, baseUrl, seedQuery, onProgress) {
|
|
197
|
-
var lastErr = null;
|
|
198
|
-
for (var attempt = 1; attempt <= 2; attempt++) {
|
|
199
|
-
try {
|
|
200
|
-
var bust = attempt === 1 ? "" : (seedQuery ? "&" : "?") + "retry=1";
|
|
201
|
-
var seedBlob = await fetcher(baseUrl + seedQuery + bust, onProgress);
|
|
202
|
-
var text = await seedBlob.text();
|
|
203
|
-
return JSON.parse(text); // throwing here is the corrupted/truncated-response retry signal
|
|
204
|
-
} catch (e) {
|
|
205
|
-
lastErr = e;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
throw lastErr;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const CLIENT_JS = String.raw`
|
|
212
|
-
(function () {
|
|
213
|
-
var DATA = window.__CODE_EXPLORER__;
|
|
214
|
-
var api = window.tmct ? window.tmct.page : null;
|
|
215
|
-
var els = {
|
|
216
|
-
focus: document.getElementById("focus-name"),
|
|
217
|
-
ledger: document.getElementById("ledger"),
|
|
218
|
-
hints: document.getElementById("hints"),
|
|
219
|
-
stats: document.getElementById("stats"),
|
|
220
|
-
log: document.getElementById("chat-log"),
|
|
221
|
-
form: document.getElementById("chat-form"),
|
|
222
|
-
input: document.getElementById("chat-input"),
|
|
223
|
-
dockNote: document.getElementById("dock-note"),
|
|
224
|
-
source: document.getElementById("source-name"),
|
|
225
|
-
seedStatus: document.getElementById("seed-status"),
|
|
226
|
-
factTotal: document.getElementById("fact-total-value"),
|
|
227
|
-
};
|
|
228
|
-
var session = null;
|
|
229
|
-
|
|
230
|
-
var escapeHtml = ${escapeHtml.toString()};
|
|
231
|
-
var rowKey = ${rowKey.toString()};
|
|
232
|
-
var statsSummaryLine = ${statsSummaryLine.toString()};
|
|
233
|
-
var factTotalText = ${factTotalText.toString()};
|
|
234
|
-
var focusRowsHtml = ${focusRowsHtml.toString()};
|
|
235
|
-
var hintsHtml = ${hintsHtml.toString()};
|
|
236
|
-
var turnHtml = ${turnHtml.toString()};
|
|
237
|
-
var chatEmptyStateHtml = ${chatEmptyStateHtml.toString()};
|
|
238
|
-
|
|
239
|
-
function renderStats(data) {
|
|
240
|
-
els.stats.textContent = statsSummaryLine(data.ledger.stats);
|
|
241
|
-
renderFactTotal(data);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// Everything the chat can answer from: the graph's own edges plus the
|
|
245
|
-
// general-knowledge seed, which arrives later and re-renders this.
|
|
246
|
-
function renderFactTotal(data) {
|
|
247
|
-
if (!els.factTotal) return;
|
|
248
|
-
var edges = (data && data.ledger && data.ledger.stats && data.ledger.stats.edges) || 0;
|
|
249
|
-
els.factTotal.textContent = factTotalText(edges, seedState.facts);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// "What relates to the focus", put to the engine. askRelatedFacts asks
|
|
253
|
-
// tmct_ask one question per relation kind the loaded graph carries, in both
|
|
254
|
-
// directions, and hands back the answers' own typed rows — the same ask()
|
|
255
|
-
// this page's chat turns reach, so the panel and the conversation answer one
|
|
256
|
-
// question one way. Null on the static page, which has no engine to ask.
|
|
257
|
-
function askRelated(focus) {
|
|
258
|
-
if (!api || !api.askRelatedFacts || !focus) return null;
|
|
259
|
-
try {
|
|
260
|
-
return api.askRelatedFacts(DATA.payload, focus);
|
|
261
|
-
} catch (e) {
|
|
262
|
-
console.warn("tmct code explorer: the related-facts ask failed, splitting the row list instead", e);
|
|
263
|
-
return null;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// The engine's answer whenever it grounded one. The row list's own split is
|
|
268
|
-
// what is left when there is no engine (the static page) or when every
|
|
269
|
-
// question came back parsed as something else — an identifier that is
|
|
270
|
-
// itself a relation verb reads as a question about the verb. Whatever the
|
|
271
|
-
// neighbourhood did not already name follows, in degree order: a bulk view
|
|
272
|
-
// of the rest of the graph, which is a fold and not a question.
|
|
273
|
-
function renderFocusRows(data, related) {
|
|
274
|
-
els.ledger.innerHTML = focusRowsHtml(data, related);
|
|
275
|
-
els.focus.textContent = data.focus || "—";
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
function renderHints(data) {
|
|
279
|
-
els.hints.innerHTML = hintsHtml(data.hints);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
function focusOn(term) {
|
|
283
|
-
if (!api || !api.computeCodeExplorerData) return;
|
|
284
|
-
DATA = api.computeCodeExplorerData(DATA.payload, { focus: term, title: DATA.meta.title });
|
|
285
|
-
window.__CODE_EXPLORER__ = DATA;
|
|
286
|
-
mountView(DATA);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
function mountView(data) {
|
|
290
|
-
renderStats(data);
|
|
291
|
-
renderFocusRows(data, askRelated(data.focus));
|
|
292
|
-
renderHints(data);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
function clearEmptyState() {
|
|
296
|
-
var e = document.getElementById("chat-empty");
|
|
297
|
-
if (e) e.remove();
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
function appendTurn(role, text) {
|
|
301
|
-
clearEmptyState();
|
|
302
|
-
var div = document.createElement("div");
|
|
303
|
-
div.className = "turn turn-" + role;
|
|
304
|
-
div.innerHTML = turnHtml(role, text);
|
|
305
|
-
els.log.appendChild(div);
|
|
306
|
-
els.log.scrollTop = els.log.scrollHeight;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
function renderEmptyState() {
|
|
310
|
-
var div = document.createElement("div");
|
|
311
|
-
div.className = "chat-empty";
|
|
312
|
-
div.id = "chat-empty";
|
|
313
|
-
div.innerHTML = chatEmptyStateHtml(Boolean(api));
|
|
314
|
-
els.log.appendChild(div);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
// ---- the general-knowledge seed --------------------------------------
|
|
318
|
-
// The same chat-seed.json chat.html boots from, loaded lazily so the shell
|
|
319
|
-
// paints instantly: same-origin fetch on the site, the preload bridge's
|
|
320
|
-
// readSeed under the desktop shell (a file:// page cannot fetch). The
|
|
321
|
-
// status bar narrates the load; a missing or failing seed leaves the chat
|
|
322
|
-
// graph-only and says so, never broken.
|
|
323
|
-
// The build's own content hash for the seed rides in the URL, so the service
|
|
324
|
-
// worker's cache-first read can only return the copy this page asked for.
|
|
325
|
-
// Empty under the desktop shell, which reads the seed off disk instead.
|
|
326
|
-
var SEED_QUERY = DATA.seedStamp ? "?b=" + DATA.seedStamp : "";
|
|
327
|
-
var seedState = { status: api ? "loading" : "absent", payload: null, facts: 0 };
|
|
328
|
-
function seedNote(text) { if (els.seedStatus) els.seedStatus.textContent = text; }
|
|
329
|
-
|
|
330
|
-
var mbText = ${mbText.toString()};
|
|
331
|
-
var fetchWithProgress = ${fetchWithProgress.toString()};
|
|
332
|
-
var seedFactsFromPayload = ${seedFactsFromPayload.toString()};
|
|
333
|
-
var seedLoadingNote = ${seedLoadingNote.toString()};
|
|
334
|
-
var fetchSeedPayloadWithRetry = ${fetchSeedPayloadWithRetry.toString()};
|
|
335
|
-
|
|
336
|
-
// The desktop shell's readSeed() reads off local disk, not a CDN, so it
|
|
337
|
-
// keeps its single attempt in loadSeed() below; only this fetch path retries.
|
|
338
|
-
function fetchSeedPayload() {
|
|
339
|
-
return fetchSeedPayloadWithRetry(fetchWithProgress, "./chat-seed.json", SEED_QUERY, function (loaded, total) {
|
|
340
|
-
seedNote(seedLoadingNote(loaded, total));
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
async function loadSeed() {
|
|
345
|
-
try {
|
|
346
|
-
var payload = null;
|
|
347
|
-
if (window.tmctDesktop && typeof window.tmctDesktop.readSeed === "function") {
|
|
348
|
-
seedNote("loading general knowledge…");
|
|
349
|
-
var text = await window.tmctDesktop.readSeed();
|
|
350
|
-
if (text) payload = JSON.parse(text);
|
|
351
|
-
} else {
|
|
352
|
-
payload = await fetchSeedPayload();
|
|
353
|
-
}
|
|
354
|
-
if (payload) {
|
|
355
|
-
seedState.payload = payload;
|
|
356
|
-
seedState.facts = seedFactsFromPayload(seedState.payload);
|
|
357
|
-
seedState.status = "ready";
|
|
358
|
-
seedNote("general knowledge: " + seedState.facts + " facts");
|
|
359
|
-
renderFactTotal(DATA);
|
|
360
|
-
return;
|
|
361
|
-
}
|
|
362
|
-
} catch (e) {
|
|
363
|
-
console.warn("tmct code explorer: chat-seed unavailable, continuing graph-only", e);
|
|
364
|
-
}
|
|
365
|
-
seedState.status = "absent";
|
|
366
|
-
seedNote("graph-only, general knowledge unavailable");
|
|
367
|
-
}
|
|
368
|
-
var seedPromise = api ? loadSeed() : Promise.resolve();
|
|
369
|
-
|
|
370
|
-
function cloneSeed() {
|
|
371
|
-
if (!seedState.payload) return null;
|
|
372
|
-
try { return structuredClone(seedState.payload); } catch (e) { return JSON.parse(JSON.stringify(seedState.payload)); }
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
async function ensureSession() {
|
|
376
|
-
if (session || !api || !api.createCodeExplorerSession) return session;
|
|
377
|
-
if (api.registerWinkModel && window.__WINK_LOADER__) {
|
|
378
|
-
try { var mod = await window.__WINK_LOADER__(); api.registerWinkModel(function () { return mod; }); }
|
|
379
|
-
catch (e) { /* the lemma/POS tier is optional */ }
|
|
380
|
-
}
|
|
381
|
-
await seedPromise;
|
|
382
|
-
session = api.createCodeExplorerSession({
|
|
383
|
-
graphPayload: DATA.payload,
|
|
384
|
-
seedPayload: cloneSeed(),
|
|
385
|
-
vocabSeeded: seedState.status === "ready",
|
|
386
|
-
});
|
|
387
|
-
return session;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
async function ask(q) {
|
|
391
|
-
appendTurn("you", q);
|
|
392
|
-
var s = await ensureSession();
|
|
393
|
-
if (!s) { appendTurn("tmct", "the live chat is not loaded on this page."); return; }
|
|
394
|
-
var res = await s.turn(q);
|
|
395
|
-
appendTurn("tmct", res.answer);
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
// Delegate term + hint clicks.
|
|
399
|
-
document.addEventListener("click", function (ev) {
|
|
400
|
-
var t = ev.target.closest ? ev.target.closest("[data-term]") : null;
|
|
401
|
-
if (t) { focusOn(t.getAttribute("data-term")); return; }
|
|
402
|
-
var h = ev.target.closest ? ev.target.closest("[data-q]") : null;
|
|
403
|
-
if (h) { els.input.value = h.getAttribute("data-q"); els.input.focus(); if (api) ask(h.getAttribute("data-q")); return; }
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
if (els.form) {
|
|
407
|
-
els.form.addEventListener("submit", function (ev) {
|
|
408
|
-
ev.preventDefault();
|
|
409
|
-
var q = els.input.value.trim();
|
|
410
|
-
if (!q) return;
|
|
411
|
-
els.input.value = "";
|
|
412
|
-
ask(q);
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
// Desktop pickers, present only under the Electron shell. A swapped graph
|
|
417
|
-
// starts a fresh session; the general-knowledge seed carries over.
|
|
418
|
-
function wirePicker(id, method, updateSource) {
|
|
419
|
-
var btn = document.getElementById(id);
|
|
420
|
-
if (!btn) return;
|
|
421
|
-
if (!window.tmctDesktop || typeof window.tmctDesktop[method] !== "function") { btn.disabled = true; return; }
|
|
422
|
-
btn.addEventListener("click", async function () {
|
|
423
|
-
btn.disabled = true;
|
|
424
|
-
try {
|
|
425
|
-
var picked = await window.tmctDesktop[method]();
|
|
426
|
-
if (picked && picked.payload) {
|
|
427
|
-
session = null;
|
|
428
|
-
DATA = (api && api.computeCodeExplorerData)
|
|
429
|
-
? api.computeCodeExplorerData(picked.payload, { title: picked.name || "code graph" })
|
|
430
|
-
: DATA;
|
|
431
|
-
window.__CODE_EXPLORER__ = DATA;
|
|
432
|
-
if (updateSource && els.source) els.source.textContent = picked.name || "(loaded graph)";
|
|
433
|
-
els.log.innerHTML = "";
|
|
434
|
-
renderEmptyState();
|
|
435
|
-
mountView(DATA);
|
|
436
|
-
}
|
|
437
|
-
} finally { btn.disabled = false; }
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
wirePicker("open-graph", "openGraph", true);
|
|
441
|
-
wirePicker("open-repo", "openRepo", true);
|
|
442
|
-
|
|
443
|
-
if (!api) {
|
|
444
|
-
if (els.dockNote) els.dockNote.textContent = "static view, live chat unavailable here.";
|
|
445
|
-
if (els.input) els.input.disabled = true;
|
|
446
|
-
seedNote("static view");
|
|
447
|
-
}
|
|
448
|
-
renderEmptyState();
|
|
449
|
-
|
|
450
|
-
mountView(DATA);
|
|
451
|
-
})();
|
|
452
|
-
`;
|
|
453
|
-
|
|
454
|
-
/**
|
|
455
|
-
* One self-contained HTML document: the full-viewport IDE shell over
|
|
456
|
-
* computeCodeExplorerData's output. `bundleInline` inlines the chat engine
|
|
457
|
-
* (for a single-file page / a data: URL); otherwise `bundleAvailable` links
|
|
458
|
-
* `./code-explorer.bundle.js`. `winkLoaderInline` optionally inlines a wink
|
|
459
|
-
* model loader as `window.__WINK_LOADER__`. `showDesktopLink` adds a line
|
|
460
|
-
* pointing at the desktop app's README section — the desktop shell itself
|
|
461
|
-
* renders this page too and passes `false`, since it has nothing to point at.
|
|
462
|
-
*/
|
|
463
|
-
export function renderCodeExplorerHtml(data, { bundleInline = "", bundleAvailable = false, winkLoaderInline = "", sourceName = "demo code graph", showDesktopLink = false, seedStamp = "" } = {}) {
|
|
464
|
-
const payloadJson = embedJson(data.payload);
|
|
465
|
-
const dataJson = embedJson({ ledger: data.ledger, hints: data.hints, focus: data.focus, meta: data.meta });
|
|
466
|
-
|
|
467
|
-
return `<!doctype html>
|
|
468
|
-
<html lang="en">
|
|
469
|
-
<head>
|
|
470
|
-
<meta charset="utf-8">
|
|
471
|
-
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
472
|
-
<title>tmct code explorer</title>
|
|
473
|
-
<link rel="icon" href="./favicon.svg" type="image/svg+xml">
|
|
474
|
-
<link rel="icon" href="./favicon.ico" sizes="any">
|
|
475
|
-
<link rel="apple-touch-icon" href="./apple-touch-icon.png">
|
|
476
|
-
<style>
|
|
477
|
-
${THEME_TOKENS_CSS}
|
|
478
|
-
* { box-sizing: border-box; }
|
|
479
|
-
html, body { height: 100%; }
|
|
480
|
-
body { margin: 0; overflow: hidden; background: var(--bg); color: var(--ink); font-family: ${SERIF_STACK}; }
|
|
481
|
-
.shell { height: 100%; display: grid; grid-template-rows: auto minmax(0, 1fr) auto; }
|
|
482
|
-
|
|
483
|
-
.titlebar { display: flex; align-items: center; gap: 0.7rem; flex-wrap: wrap; padding: 0.5rem 0.9rem; background: var(--card); border-bottom: 1px solid var(--line); box-shadow: inset 0 -2px 0 var(--entail-soft); }
|
|
484
|
-
.titlebar .mark { width: 15px; height: 15px; color: var(--entail); flex: none; }
|
|
485
|
-
.titlebar h1 { font-size: 0.95rem; margin: 0; font-weight: 600; letter-spacing: 0.02em; }
|
|
486
|
-
${EYEBROW_LINKS_CSS}
|
|
487
|
-
.titlebar .sub { color: var(--muted); font-size: 0.76rem; font-family: ${MONO_STACK}; }
|
|
488
|
-
.titlebar .sub a { color: var(--corpus); }
|
|
489
|
-
/* the fact count sits in the titlebar, not the footer status bar: it is what
|
|
490
|
-
the chat can actually answer from, and the footer is where a wrong number
|
|
491
|
-
goes unread. */
|
|
492
|
-
.titlebar .factpill { display: inline-flex; align-items: baseline; gap: 0.34rem; font-family: ${MONO_STACK}; font-size: 0.62rem; letter-spacing: 0.07em; text-transform: uppercase; color: var(--muted); border: 1px solid var(--entail); border-radius: 99px; padding: 0.14rem 0.7rem; background: var(--entail-soft); white-space: nowrap; }
|
|
493
|
-
.titlebar .factpill-value { font-size: 0.9rem; letter-spacing: 0; font-variant-numeric: tabular-nums; font-weight: 600; color: var(--ink); }
|
|
494
|
-
.titlebar .pickers { margin-left: auto; display: flex; gap: 0.45rem; }
|
|
495
|
-
button { font: inherit; cursor: pointer; }
|
|
496
|
-
button:disabled { cursor: default; opacity: 0.5; }
|
|
497
|
-
button:focus-visible, input:focus-visible { outline: 2px solid var(--entail); outline-offset: 1px; }
|
|
498
|
-
.pickers button { background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-radius: 4px; padding: 0.3rem 0.65rem; font-size: 0.8rem; }
|
|
499
|
-
.pickers button:hover { border-color: var(--entail); }
|
|
500
|
-
|
|
501
|
-
.workbench { display: grid; grid-template-columns: minmax(230px, 320px) minmax(0, 1fr); min-height: 0; }
|
|
502
|
-
.sidebar { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); }
|
|
503
|
-
.panel { display: flex; flex-direction: column; min-height: 0; flex: 1; }
|
|
504
|
-
.panel-head { margin: 0; padding: 0.45rem 0.9rem; flex: none; display: flex; align-items: baseline; gap: 0.45rem; font-family: ${MONO_STACK}; font-size: 0.66rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.09em; color: var(--muted); border-bottom: 1px solid var(--line); }
|
|
505
|
-
.panel-body { flex: 1; min-height: 0; overflow-y: auto; padding: 0.5rem 0.9rem 1rem; }
|
|
506
|
-
.focus-line { font-family: ${MONO_STACK}; font-size: 0.72rem; color: var(--corpus); text-transform: none; letter-spacing: 0; overflow-wrap: anywhere; }
|
|
507
|
-
ul.rows { list-style: none; margin: 0; padding: 0; }
|
|
508
|
-
.row { padding: 0.26rem 0.35rem; border-radius: 4px; font-size: 0.88rem; line-height: 1.45; }
|
|
509
|
-
.row-focus { background: var(--corpus-soft); }
|
|
510
|
-
.row.muted, .muted { color: var(--muted); }
|
|
511
|
-
.term { background: none; border: none; padding: 0; color: var(--corpus); font-family: ${MONO_STACK}; font-size: 0.8rem; text-align: left; text-decoration: underline; text-decoration-color: var(--line); overflow-wrap: anywhere; }
|
|
512
|
-
.term:hover { text-decoration-color: var(--corpus); }
|
|
513
|
-
.verb { color: var(--muted); }
|
|
514
|
-
|
|
515
|
-
.editor { display: flex; flex-direction: column; min-height: 0; background: var(--card); }
|
|
516
|
-
#chat-log { flex: 1; min-height: 0; overflow-y: auto; padding: 0.9rem 1.1rem; display: flex; flex-direction: column; gap: 0.7rem; }
|
|
517
|
-
.turn { max-width: 46rem; font-size: 0.95rem; line-height: 1.5; }
|
|
518
|
-
.turn .who { display: block; font-family: ${MONO_STACK}; font-size: 0.62rem; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); margin-bottom: 0.15rem; }
|
|
519
|
-
.turn-you .said { font-family: ${MONO_STACK}; font-size: 0.84rem; }
|
|
520
|
-
.turn-tmct { border-left: 2px solid var(--entail); padding-left: 0.7rem; }
|
|
521
|
-
.turn-tmct .said { white-space: pre-wrap; }
|
|
522
|
-
.chat-empty { margin: auto; max-width: 26rem; text-align: center; color: var(--muted); }
|
|
523
|
-
.chat-empty-head { color: var(--ink); font-weight: 600; font-size: 1.05rem; margin: 0 0 0.5rem; }
|
|
524
|
-
.chat-empty p { margin: 0.3rem 0; line-height: 1.55; font-size: 0.92rem; }
|
|
525
|
-
.chat-empty-hint { font-size: 0.82rem; }
|
|
526
|
-
.suggest { flex: none; border-top: 1px solid var(--line); padding: 0.65rem 1.1rem 0.4rem; }
|
|
527
|
-
.suggest-label { font-family: ${MONO_STACK}; font-size: 0.66rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.09em; color: var(--muted); }
|
|
528
|
-
.hints { display: flex; flex-wrap: wrap; gap: 0.4rem; max-height: 5.6rem; overflow-y: auto; padding: 0.45rem 0 0.55rem; }
|
|
529
|
-
.hint { background: var(--bg); border: 1px solid var(--line); border-radius: 999px; padding: 0.24rem 0.7rem; font-size: 0.8rem; color: var(--ink); text-align: left; }
|
|
530
|
-
.hint:hover { border-color: var(--entail); }
|
|
531
|
-
#chat-form { flex: none; display: flex; gap: 0.5rem; padding: 0.75rem 1.1rem 0.9rem; border-top: 1px solid var(--line); }
|
|
532
|
-
#chat-input { flex: 1; min-width: 0; font: inherit; font-size: 0.92rem; padding: 0.5rem 0.65rem; border: 1px solid var(--line); border-radius: 4px; background: var(--bg); color: var(--ink); }
|
|
533
|
-
#chat-form button { background: var(--entail); color: var(--card); font-weight: 600; border: none; border-radius: 4px; padding: 0.5rem 1rem; font-size: 0.9rem; }
|
|
534
|
-
|
|
535
|
-
.statusbar { display: flex; align-items: baseline; gap: 1rem; flex-wrap: wrap; padding: 0.32rem 0.9rem; font-family: ${MONO_STACK}; font-size: 0.7rem; color: var(--muted); background: var(--entail-soft); border-top: 1px solid var(--entail); }
|
|
536
|
-
#seed-status { margin-left: auto; text-align: right; }
|
|
537
|
-
|
|
538
|
-
@media (max-width: 760px) {
|
|
539
|
-
.workbench { grid-template-columns: 1fr; grid-template-rows: minmax(0, 34%) minmax(0, 1fr); }
|
|
540
|
-
.sidebar { border-right: none; border-bottom: 1px solid var(--line); }
|
|
541
|
-
}
|
|
542
|
-
</style>
|
|
543
|
-
</head>
|
|
544
|
-
<body>
|
|
545
|
-
<div class="shell">
|
|
546
|
-
<header class="titlebar">
|
|
547
|
-
<svg class="mark" viewBox="0 0 16 16" aria-hidden="true"><path fill="currentColor" d="M8 0l1 2.3a5.8 5.8 0 0 1 1.9.8L13.3 2l.7.7-1.1 2.4c.4.6.6 1.2.8 1.9L16 8l-2.3 1a5.8 5.8 0 0 1-.8 1.9l1.1 2.4-.7.7-2.4-1.1a5.8 5.8 0 0 1-1.9.8L8 16l-1-2.3a5.8 5.8 0 0 1-1.9-.8L2.7 14l-.7-.7 1.1-2.4a5.8 5.8 0 0 1-.8-1.9L0 8l2.3-1c.2-.7.4-1.3.8-1.9L2 2.7l.7-.7 2.4 1.1A5.8 5.8 0 0 1 7 2.3L8 0zm0 5.2A2.8 2.8 0 1 0 8 10.8 2.8 2.8 0 0 0 8 5.2z"/></svg>
|
|
548
|
-
<h1>${demoEyebrowHtml("code", "code explorer")}</h1>
|
|
549
|
-
<span class="sub">source: <span id="source-name">${escapeHtml(sourceName)}</span></span>
|
|
550
|
-
<span class="factpill" id="fact-total" aria-live="polite" title="every fact this page's chat can draw on — the code graph's own edges plus the general-knowledge seed once it lands">
|
|
551
|
-
<span class="factpill-value" id="fact-total-value">—</span> facts loaded
|
|
552
|
-
</span>
|
|
553
|
-
${showDesktopLink ? `<span class="sub">Also available as a <a href="${DESKTOP_APP_URL}">desktop app</a>.</span>` : ""}
|
|
554
|
-
<div class="pickers">
|
|
555
|
-
<button id="open-graph">Open graph…</button>
|
|
556
|
-
<button id="open-repo">Open repo…</button>
|
|
557
|
-
</div>
|
|
558
|
-
</header>
|
|
559
|
-
<div class="workbench">
|
|
560
|
-
<aside class="sidebar">
|
|
561
|
-
<section class="panel" data-panel="explorer">
|
|
562
|
-
<h2 class="panel-head">Facts around <span class="focus-line" id="focus-name">—</span></h2>
|
|
563
|
-
<div class="panel-body">
|
|
564
|
-
<ul class="rows" id="ledger"></ul>
|
|
565
|
-
</div>
|
|
566
|
-
</section>
|
|
567
|
-
</aside>
|
|
568
|
-
<section class="editor" data-panel="conversation">
|
|
569
|
-
<h2 class="panel-head">Conversation</h2>
|
|
570
|
-
<div id="chat-log"></div>
|
|
571
|
-
<div class="suggest">
|
|
572
|
-
<span class="suggest-label">Try asking</span>
|
|
573
|
-
<div class="hints" id="hints"></div>
|
|
574
|
-
</div>
|
|
575
|
-
<form id="chat-form">
|
|
576
|
-
<input id="chat-input" type="text" autocomplete="off" placeholder="ask about this graph, or anything it knows…">
|
|
577
|
-
<button type="submit">Ask</button>
|
|
578
|
-
</form>
|
|
579
|
-
</section>
|
|
580
|
-
</div>
|
|
581
|
-
<footer class="statusbar">
|
|
582
|
-
<span id="stats"></span>
|
|
583
|
-
<span id="dock-note"></span>
|
|
584
|
-
<span id="seed-status"></span>
|
|
585
|
-
</footer>
|
|
586
|
-
</div>
|
|
587
|
-
<script>window.__CODE_EXPLORER__ = Object.assign({ payload: ${payloadJson}, seedStamp: ${JSON.stringify(seedStamp)} }, ${dataJson});</script>
|
|
588
|
-
${winkLoaderInline ? `<script>\n${embedScriptText(winkLoaderInline)}\n</script>` : ""}
|
|
589
|
-
${bundleInline ? `<script>\n${embedScriptText(bundleInline)}\n</script>` : ""}
|
|
590
|
-
${bundleAvailable && !bundleInline ? `<script src="./code-explorer.bundle.js"></script>` : ""}
|
|
591
|
-
<script>
|
|
592
|
-
${embedScriptText(CLIENT_JS)}
|
|
593
|
-
</script>
|
|
594
|
-
</body>
|
|
595
|
-
</html>`;
|
|
596
|
-
}
|