@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.
@@ -1,180 +0,0 @@
1
- // code-explorer-hints.mjs — suggested next queries drawn from what a loaded
2
- // CODE graph actually holds. Pure over the entities payload (individuals +
3
- // objectProperties), so the desktop shell and any future plain page share one
4
- // tested generator and neither hand-writes prompt strings.
5
- //
6
- // The shapes are exactly the ones ask.mjs already answers, phrased from its own
7
- // closed vocabulary (ask-vocab.mjs's RELATIONS/EDGE_NOUN_TO_METRIC): a relation
8
- // only appears in a hint when the graph carries an edge of that kind, and a
9
- // focus symbol only fills a slot when it actually sits at that end of an edge —
10
- // so every suggestion resolves to a real answer, never a guess.
11
-
12
- import { RELATIONS, EDGE_NOUN_TO_METRIC } from "./ask-vocab.mjs";
13
-
14
- // Node classes this explorer reads as code, with the singular/plural nouns
15
- // ask.mjs's own list and count lanes accept (ENTITY_TO_TYPE's inverse). Schema
16
- // documentation individuals (SchemaClass/SchemaPredicate) and anything else are
17
- // left out — they are not what a reader explores.
18
- const CODE_CLASS_NOUN = Object.freeze({
19
- Module: ["module", "modules"],
20
- Class: ["class", "classes"],
21
- Function: ["function", "functions"],
22
- Method: ["method", "methods"],
23
- Attribute: ["attribute", "attributes"],
24
- GlobalVariable: ["variable", "variables"],
25
- Commit: ["commit", "commits"],
26
- });
27
-
28
- // The classes a focus symbol is drawn from — the things a reader centres on.
29
- const FOCUSABLE_CLASSES = new Set(["Module", "Class", "Function", "Method"]);
30
-
31
- // Fine, symbol-grain predicates fold onto their coarse sibling for phrasing:
32
- // "what does X call" covers callsSymbol too, exactly as ask.mjs's
33
- // SYMBOL_GRAIN_SIBLING pairs them.
34
- const GRAIN_BASE = Object.freeze({ callsSymbol: "calls", touchesSymbol: "touches" });
35
- const baseKind = (predicate) => GRAIN_BASE[predicate] || predicate;
36
-
37
- // Forward reading (focus is the subject): "what does <focus> <bare>". The bare
38
- // verb form is ask-vocab's own, so the phrasing tracks the vocabulary.
39
- const FORWARD_TEMPLATE = Object.freeze({
40
- imports: (f) => `what does ${f} import`,
41
- calls: (f) => `what does ${f} call`,
42
- contains: (f) => `what is in ${f}`,
43
- defines: (f) => `what does ${f} define`,
44
- inherits: (f) => `what does ${f} inherit from`,
45
- serves: (f) => `what does ${f} serve`,
46
- denotes: (f) => `what does ${f} denote`,
47
- });
48
-
49
- // Reverse reading (focus is the object): "what <kind> <focus>".
50
- const REVERSE_TEMPLATE = Object.freeze({
51
- imports: (f) => `what imports ${f}`,
52
- calls: (f) => `what calls ${f}`,
53
- tests: (f) => `what tests ${f}`,
54
- inherits: (f) => `what inherits from ${f}`,
55
- contains: (f) => `what contains ${f}`,
56
- serves: (f) => `what serves ${f}`,
57
- denotes: (f) => `what denotes ${f}`,
58
- });
59
-
60
- /** Index a payload into the counts and adjacency the hints read: class →
61
- * count, base relation kind → { count, subjects, objects }, and a label →
62
- * { class, degree } term table over every example edge. */
63
- function indexGraph(payload) {
64
- const individuals = Array.isArray(payload?.individuals) ? payload.individuals : [];
65
- const classOfLabel = new Map();
66
- const classCounts = new Map();
67
- for (const ind of individuals) {
68
- if (!ind || !ind.label) continue;
69
- if (CODE_CLASS_NOUN[ind.class]) classCounts.set(ind.class, (classCounts.get(ind.class) || 0) + 1);
70
- if (!classOfLabel.has(ind.label)) classOfLabel.set(ind.label, ind.class);
71
- }
72
-
73
- const groups = Array.isArray(payload?.objectProperties) ? payload.objectProperties : [];
74
- const kinds = new Map(); // base kind -> { count, subjects:Set, objects:Set }
75
- const degree = new Map(); // label -> degree over example edges
76
- const bumpKind = (k) => kinds.get(k) || kinds.set(k, { count: 0, subjects: new Set(), objects: new Set() }).get(k);
77
- for (const g of groups) {
78
- if (!g || !g.predicate) continue;
79
- const k = baseKind(String(g.predicate));
80
- const entry = bumpKind(k);
81
- entry.count += Number(g.count) || (Array.isArray(g.examples) ? g.examples.length : 0);
82
- for (const e of Array.isArray(g.examples) ? g.examples : []) {
83
- const s = e?.subjectLabel || e?.subject;
84
- const o = e?.objectLabel || e?.object;
85
- if (s) { entry.subjects.add(s); degree.set(s, (degree.get(s) || 0) + 1); }
86
- if (o) { entry.objects.add(o); degree.set(o, (degree.get(o) || 0) + 1); }
87
- }
88
- }
89
- return { classCounts, classOfLabel, kinds, degree };
90
- }
91
-
92
- /** Pick the focus symbol: the caller's choice when it is a real term, else the
93
- * highest-degree focusable-class label, else the highest-degree label at all,
94
- * else null. Ties break on label for determinism. */
95
- function pickFocus(index, requested) {
96
- if (requested && index.degree.has(requested)) return requested;
97
- const ranked = [...index.degree.entries()]
98
- .sort((a, b) => b[1] - a[1] || String(a[0]).localeCompare(String(b[0])));
99
- const focusable = ranked.find(([label]) => FOCUSABLE_CLASSES.has(index.classOfLabel.get(label)));
100
- if (focusable) return focusable[0];
101
- return ranked.length ? ranked[0][0] : null;
102
- }
103
-
104
- /**
105
- * Suggested queries for a loaded code graph.
106
- *
107
- * @param {object} payload the entities payload (individuals + objectProperties).
108
- * @param {object} [opts]
109
- * @param {string} [opts.focus] centre the neighbourhood on this symbol.
110
- * @param {number} [opts.limit] cap the returned hints (default 12).
111
- * @returns {{ focus: (string|null), hints: Array<{text,group,rationale}> }}
112
- * `hints` is ordered neighbourhood → compositional → explore; empty
113
- * when the graph holds nothing to explore.
114
- */
115
- export function generateCodeHints(payload, { focus: requestedFocus = null, limit = 12 } = {}) {
116
- const index = indexGraph(payload);
117
- const focus = pickFocus(index, requestedFocus);
118
- const hints = [];
119
- const add = (text, group, rationale) => hints.push({ text, group, rationale });
120
- const kindPresent = (k) => (index.kinds.get(k)?.count || 0) > 0;
121
-
122
- if (focus) {
123
- for (const [kind, template] of Object.entries(FORWARD_TEMPLATE)) {
124
- if (index.kinds.get(kind)?.subjects.has(focus)) {
125
- add(template(focus), "neighbourhood", `${focus} ${verbBare(kind)} other symbols in this graph`);
126
- }
127
- }
128
- for (const [kind, template] of Object.entries(REVERSE_TEMPLATE)) {
129
- if (index.kinds.get(kind)?.objects.has(focus)) {
130
- add(template(focus), "neighbourhood", `other symbols ${kind} ${focus}`);
131
- }
132
- }
133
- }
134
-
135
- // Compositional shapes over the graph's own edges and classes.
136
- const calls = index.kinds.get("calls");
137
- if (calls && index.classCounts.has("Function") && calls.objects.size) {
138
- const target = [...calls.objects].sort()[0];
139
- add(`which functions call ${target}`, "compositional", `${target} is called somewhere in this graph`);
140
- }
141
- const contains = index.kinds.get("contains");
142
- if (contains && index.classCounts.has("Method")) {
143
- const owner = [...contains.subjects].sort().find((s) => index.classOfLabel.get(s) === "Class");
144
- if (owner) add(`public methods of ${owner}`, "compositional", `${owner} contains members`);
145
- }
146
-
147
- // Explore-the-whole-graph shapes. The rankings and the coverage filter come
148
- // first — they are the more useful whole-graph questions — then a list and a
149
- // count per present class. EDGE_NOUN_TO_METRIC names the nouns ask.mjs's
150
- // superlative lane accepts, so a hint only offers a ranking the graph can
151
- // actually compute.
152
- const superlatives = [
153
- { noun: "imports", entity: "module", filter: "Module" },
154
- { noun: "callers", entity: "function", filter: "Function" },
155
- ];
156
- for (const s of superlatives) {
157
- const metric = EDGE_NOUN_TO_METRIC[s.noun];
158
- if (metric && kindPresent(metric.kind) && index.classCounts.has(s.filter)) {
159
- add(`which ${s.entity} has the most ${s.noun}`, "explore", `rank ${s.entity}s by ${s.noun}`);
160
- }
161
- }
162
- if (kindPresent("tests")) {
163
- add("which of those are tested", "explore", "filter a listing by test coverage");
164
- }
165
- const classesByCount = [...index.classCounts.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
166
- for (const [cls, count] of classesByCount) {
167
- const [, plural] = CODE_CLASS_NOUN[cls];
168
- add(`list ${plural}`, "explore", `${count} ${plural} in this graph`);
169
- add(`how many ${plural}`, "explore", `count the ${plural}`);
170
- }
171
-
172
- return { focus, hints: hints.slice(0, limit) };
173
- }
174
-
175
- // The infinitive ask-vocab curates for each relation, used in rationales.
176
- function verbBare(kind) {
177
- return RELATIONS[kind]?.bare || kind;
178
- }
179
-
180
- export { CODE_CLASS_NOUN, indexGraph };