@polycode-projects/the-mechanical-code-talker 1.10.13 → 1.11.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/src/viz.mjs CHANGED
@@ -113,14 +113,14 @@ export async function readMemoryAskBundle() {
113
113
  }
114
114
 
115
115
  /** Escape untrusted text for safe placement inside HTML content/attributes. */
116
- function escapeHtml(s) {
116
+ export function escapeHtml(s) {
117
117
  return String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c]));
118
118
  }
119
119
 
120
120
  /** JSON-embed graph data into a `<script>` tag safely — escape `</` so a
121
121
  * label/id containing "</script>" can't break out of the tag, and escape
122
122
  * U+2028/U+2029 (valid in JSON strings, invalid unescaped in JS source). */
123
- function embedJson(value) {
123
+ export function embedJson(value) {
124
124
  return JSON.stringify(value)
125
125
  .replace(/</g, "\\u003c")
126
126
  .replace(/\u2028/g, "\\u2028")
@@ -21,6 +21,7 @@ let cached; // undefined = not tried yet; null = unavailable (tried once, honest
21
21
  export function registerWinkModel(factory) {
22
22
  injected = factory;
23
23
  cached = undefined;
24
+ instance = undefined;
24
25
  }
25
26
 
26
27
  /** Load `{ winkNLP, model }` once, or null when wink isn't available. Prefers a
@@ -46,15 +47,20 @@ function nodeRequireWink() {
46
47
  };
47
48
  }
48
49
 
49
- /** Convenience: the constructed `nlp` instance (`winkNLP(model)`) or null. Both
50
- * adapters want exactly this. Not cached here — the adapters cache their own
51
- * higher-level object; constructing `nlp` is cheap next to loading the model. */
50
+ let instance; // undefined = not built yet; null = construction failed once
51
+
52
+ /** Convenience: the constructed `nlp` instance (`winkNLP(model)`) or null.
53
+ * Cached: repeated `winkNLP(model)` construction accumulates module-level
54
+ * state inside the model package until V8 throws "Invalid string length",
55
+ * after which every later construction fails for the life of the process. */
52
56
  export function winkInstance() {
57
+ if (instance !== undefined) return instance;
53
58
  const loaded = loadWinkModel();
54
- if (!loaded) return null;
59
+ if (!loaded) { instance = null; return null; }
55
60
  try {
56
- return loaded.winkNLP(loaded.model);
61
+ instance = loaded.winkNLP(loaded.model);
57
62
  } catch {
58
- return null;
63
+ instance = null;
59
64
  }
65
+ return instance;
60
66
  }