formal-ai 0.36.1

Formal symbolic AI proof of concept with OpenAI-compatible APIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// Append-only Links Notation event log for the demo.
//
// The browser demo records every user/assistant turn as an Event in an
// IndexedDB object store. The store is treated as append-only: writes can only
// add new records, and the public API does not expose a "forget" operation.
// Users can export the full log as Links Notation text and import a previous
// log into a new browser session — Links Notation is the portable format
// shared with the Rust solver and the seed data under `data/`.
//
// Storage layout:
//
//   demo_memory
//     event "id1"
//       role "user"
//       content "Hi"
//       sentAt "2026-05-15T12:00:00.000Z"
//     event "id2"
//       role "assistant"
//       intent "greeting"
//       content "Hi, how may I help you?"
//       sentAt "2026-05-15T12:00:01.000Z"
//
// Only existing entries are encoded; absent fields are omitted so the format
// stays human-readable in DevTools and on disk.

(function (global) {
  "use strict";

  var DB_NAME = "formal-ai-demo";
  var DB_VERSION = 1;
  var STORE_NAME = "events";
  var ROOT_HEADER = "demo_memory";
  var BUNDLE_HEADER = "formal_ai_bundle";
  // Schema is intentionally additive. Older logs without "kind" still parse
  // as plain user/assistant turns. New "kind" values record reasoning steps,
  // tool invocations, decisions, and other internal events so the log is a
  // complete projection of what the agent did.
  var EXPORT_FIELDS = [
    "kind",
    "role",
    "intent",
    "tool",
    "inputs",
    "outputs",
    "content",
    "sentAt",
    "demoLabel",
    "evidence",
    // Issue #27: conversation grouping. Events keep their global ordering in
    // the append-only log; conversationId/conversationTitle just attribute each
    // event to a specific chat thread so the UI can filter on read.
    "conversationId",
    "conversationTitle",
  ];

  var dbPromise = null;

  function hasIndexedDb() {
    try {
      return typeof global.indexedDB !== "undefined";
    } catch (_error) {
      return false;
    }
  }

  function openDatabase() {
    if (dbPromise) return dbPromise;
    if (!hasIndexedDb()) {
      dbPromise = Promise.resolve(null);
      return dbPromise;
    }
    dbPromise = new Promise(function (resolve, reject) {
      var request = global.indexedDB.open(DB_NAME, DB_VERSION);
      request.onupgradeneeded = function () {
        var db = request.result;
        if (!db.objectStoreNames.contains(STORE_NAME)) {
          db.createObjectStore(STORE_NAME, {
            keyPath: "id",
            autoIncrement: true,
          });
        }
      };
      request.onsuccess = function () {
        resolve(request.result);
      };
      request.onerror = function () {
        reject(request.error);
      };
    });
    return dbPromise;
  }

  function escapeValue(value) {
    return String(value).replace(/\\/g, "\\\\").replace(/"/g, '\\"');
  }

  function unescapeValue(value) {
    return value.replace(/\\"/g, '"').replace(/\\\\/g, "\\");
  }

  function serializeEvidence(evidence) {
    if (!Array.isArray(evidence)) return "";
    return evidence.filter(function (item) {
      return typeof item === "string" && item.length > 0;
    }).join("|");
  }

  function parseEvidence(value) {
    if (!value) return [];
    return value.split("|").filter(Boolean);
  }

  function formatEvent(event) {
    var lines = [];
    var id = event && event.id != null ? String(event.id) : "";
    lines.push('  event "' + escapeValue(id) + '"');
    for (var index = 0; index < EXPORT_FIELDS.length; index += 1) {
      var key = EXPORT_FIELDS[index];
      var raw = event ? event[key] : undefined;
      if (raw === undefined || raw === null) continue;
      var value = key === "evidence" ? serializeEvidence(raw) : raw;
      if (typeof value !== "string" || value.length === 0) {
        if (key !== "evidence") {
          value = String(raw);
        } else {
          continue;
        }
      }
      lines.push('    ' + key + ' "' + escapeValue(value) + '"');
    }
    return lines.join("\n");
  }

  function exportLinksNotation(events) {
    var safe = Array.isArray(events) ? events : [];
    if (safe.length === 0) return ROOT_HEADER + "\n";
    var parts = [ROOT_HEADER];
    for (var index = 0; index < safe.length; index += 1) {
      parts.push(formatEvent(safe[index]));
    }
    return parts.join("\n") + "\n";
  }

  function parseLinksNotation(text) {
    if (typeof text !== "string" || !text.trim()) return [];
    var lines = text.split(/\r?\n/);
    var header = (lines.shift() || "").trim();
    if (header !== ROOT_HEADER) return [];
    var events = [];
    var current = null;
    var eventPattern = /^\s{2}event\s+"((?:[^"\\]|\\.)*)"\s*$/;
    var fieldPattern = /^\s{4}([a-zA-Z0-9_]+)\s+"((?:[^"\\]|\\.)*)"\s*$/;
    for (var index = 0; index < lines.length; index += 1) {
      var line = lines[index];
      if (!line || !line.trim()) continue;
      var eventMatch = eventPattern.exec(line);
      if (eventMatch) {
        if (current) events.push(current);
        current = { id: unescapeValue(eventMatch[1]) };
        continue;
      }
      var fieldMatch = fieldPattern.exec(line);
      if (fieldMatch && current) {
        var key = fieldMatch[1];
        var raw = unescapeValue(fieldMatch[2]);
        current[key] = key === "evidence" ? parseEvidence(raw) : raw;
      }
    }
    if (current) events.push(current);
    return events;
  }

  function withStore(mode, action) {
    return openDatabase().then(function (db) {
      if (!db) return null;
      return new Promise(function (resolve, reject) {
        var transaction = db.transaction(STORE_NAME, mode);
        transaction.onerror = function () {
          reject(transaction.error);
        };
        transaction.oncomplete = function () {
          resolve(result);
        };
        var store = transaction.objectStore(STORE_NAME);
        var result = null;
        action(store, function (value) {
          result = value;
        }, reject);
      });
    });
  }

  function appendEvent(event) {
    if (!event || typeof event !== "object") {
      return Promise.resolve(null);
    }
    var record = {
      role: String(event.role || ""),
      content: String(event.content || ""),
      sentAt: String(event.sentAt || new Date().toISOString()),
    };
    if (event.kind) record.kind = String(event.kind);
    if (event.intent) record.intent = String(event.intent);
    if (event.tool) record.tool = String(event.tool);
    if (event.inputs !== undefined && event.inputs !== null) {
      record.inputs =
        typeof event.inputs === "string"
          ? event.inputs
          : JSON.stringify(event.inputs);
    }
    if (event.outputs !== undefined && event.outputs !== null) {
      record.outputs =
        typeof event.outputs === "string"
          ? event.outputs
          : JSON.stringify(event.outputs);
    }
    if (event.demoLabel) record.demoLabel = String(event.demoLabel);
    if (Array.isArray(event.evidence)) record.evidence = event.evidence.slice();
    if (event.conversationId) record.conversationId = String(event.conversationId);
    if (event.conversationTitle)
      record.conversationTitle = String(event.conversationTitle);
    return withStore("readwrite", function (store, setResult) {
      var request = store.add(record);
      request.onsuccess = function () {
        record.id = request.result;
        setResult(record);
      };
    });
  }

  function listEvents() {
    return withStore("readonly", function (store, setResult) {
      var request = store.getAll();
      request.onsuccess = function () {
        var items = Array.isArray(request.result) ? request.result : [];
        items.sort(function (left, right) {
          return Number(left.id) - Number(right.id);
        });
        setResult(items);
      };
    }).then(function (value) {
      return Array.isArray(value) ? value : [];
    });
  }

  function importEvents(events) {
    var safe = Array.isArray(events) ? events : [];
    if (safe.length === 0) return Promise.resolve(0);
    return withStore("readwrite", function (store, setResult) {
      var inserted = 0;
      var index = 0;

      function insertNext() {
        if (index >= safe.length) {
          setResult(inserted);
          return;
        }
        var raw = safe[index];
        index += 1;
        if (!raw || typeof raw !== "object") {
          insertNext();
          return;
        }
        var record = {
          role: String(raw.role || ""),
          content: String(raw.content || ""),
          sentAt: String(raw.sentAt || new Date().toISOString()),
        };
        if (raw.kind) record.kind = String(raw.kind);
        if (raw.intent) record.intent = String(raw.intent);
        if (raw.tool) record.tool = String(raw.tool);
        if (raw.inputs !== undefined && raw.inputs !== null) {
          record.inputs =
            typeof raw.inputs === "string"
              ? raw.inputs
              : JSON.stringify(raw.inputs);
        }
        if (raw.outputs !== undefined && raw.outputs !== null) {
          record.outputs =
            typeof raw.outputs === "string"
              ? raw.outputs
              : JSON.stringify(raw.outputs);
        }
        if (raw.demoLabel) record.demoLabel = String(raw.demoLabel);
        if (Array.isArray(raw.evidence)) record.evidence = raw.evidence.slice();
        if (raw.conversationId) record.conversationId = String(raw.conversationId);
        if (raw.conversationTitle)
          record.conversationTitle = String(raw.conversationTitle);
        var request = store.add(record);
        request.onsuccess = function () {
          inserted += 1;
          insertNext();
        };
        request.onerror = function () {
          insertNext();
        };
      }

      insertNext();
    }).then(function (value) {
      return typeof value === "number" ? value : 0;
    });
  }

  function indentBlock(text, indent) {
    var prefix = indent || "  ";
    return String(text || "")
      .split(/\r?\n/)
      .filter(function (line) { return line.length > 0; })
      .map(function (line) { return prefix + line; })
      .join("\n");
  }

  // Combine app metadata + every seed file + the entire append-only event log
  // into a single Links Notation document. The output is the canonical
  // "report-ready" debug snapshot — paste it into a GitHub issue and the
  // maintainer can reconstruct the agent's full state.
  function exportBundle(options) {
    var settings = options || {};
    var seed = settings.seed || {};
    var events = Array.isArray(settings.events) ? settings.events : [];
    var info = settings.info || {};
    var preferences = settings.preferences || null;
    var lines = ["formal_ai_bundle"];
    lines.push('  exported_at "' + escapeValue(new Date().toISOString()) + '"');
    if (info.version) {
      lines.push('  version "' + escapeValue(info.version) + '"');
    }
    if (info.url) {
      lines.push('  url "' + escapeValue(info.url) + '"');
    }
    if (info.userAgent) {
      lines.push('  user_agent "' + escapeValue(info.userAgent) + '"');
    }
    if (info.workerState) {
      lines.push('  worker_state "' + escapeValue(info.workerState) + '"');
    }
    if (info.mode) {
      lines.push('  mode "' + escapeValue(info.mode) + '"');
    }
    var seedFiles = seed && seed.raw ? Object.keys(seed.raw) : [];
    if (seedFiles.length > 0) {
      lines.push("  seed_files");
      seedFiles.forEach(function (filename) {
        lines.push('    file "' + escapeValue(filename) + '"');
        lines.push(indentBlock(seed.raw[filename], "      "));
      });
    }
    if (preferences && typeof preferences === "object") {
      var prefKeys = Object.keys(preferences);
      if (prefKeys.length > 0) {
        lines.push("  preferences");
        prefKeys.forEach(function (key) {
          var raw = preferences[key];
          if (raw === undefined || raw === null) return;
          var value =
            typeof raw === "boolean" ? (raw ? "on" : "off") : String(raw);
          lines.push('    ' + key + ' "' + escapeValue(value) + '"');
        });
      }
    }
    lines.push("  " + ROOT_HEADER);
    events.forEach(function (event) {
      lines.push("  " + formatEvent(event));
    });
    return lines.join("\n") + "\n";
  }

  // Default export entry point. Always emits the full self-contained
  // `formal_ai_bundle` (seed + events + preferences + metadata) so a single
  // file is enough for a maintainer to replay the agent's state. Older code
  // paths and tests can still call `exportBundle`/`exportLinksNotation`
  // directly — `exportFullMemory` is the new canonical name.
  function exportFullMemory(options) {
    return exportBundle(options || {});
  }

  // Parse either a `formal_ai_bundle` document (the new full-memory format)
  // or the legacy `demo_memory` event log. Returns a small descriptor object
  // describing what was found so the caller can act on the structure:
  //
  //   { kind: "bundle", events, seedFiles, preferences, info, agentInfo }
  //   { kind: "memory", events, seedFiles: {}, preferences: null, info: {}, agentInfo: {} }
  //
  // `seedFiles` is a plain `{ filename: contents }` object; `info` carries
  // the bundle metadata (`exported_at`, `version`, `url`, `user_agent`,
  // `worker_state`, `mode`); `agentInfo` mirrors the `agent_info` map parsed
  // out of `seed/agent-info.lino` when present (used by
  // `suggestMigrations`).
  function importFullMemory(text) {
    var safe = typeof text === "string" ? text : "";
    var firstLine = safe.split(/\r?\n/, 1)[0] || "";
    if (firstLine.trim() === BUNDLE_HEADER) {
      return parseBundleDocument(safe);
    }
    return {
      kind: "memory",
      events: parseLinksNotation(safe),
      seedFiles: {},
      preferences: null,
      info: {},
      agentInfo: {},
    };
  }

  // Parser for the bundle document. Walks the indentation manually so it
  // does not pull in `FormalAiSeed.parseBundle` (which only returns the seed
  // files). The parser is forgiving: unknown sub-sections are skipped, and a
  // truncated document still yields whatever events were recoverable.
  function parseBundleDocument(text) {
    var lines = text.split(/\r?\n/);
    var info = {};
    var seedFiles = {};
    var preferences = null;
    var agentInfo = {};
    var memoryLines = [];
    var section = null; // null | "seed_files" | "preferences" | "memory"
    var currentSeedFile = null;
    var index = 0;
    while (index < lines.length) {
      var line = lines[index];
      index += 1;
      if (!line) continue;
      var indentMatch = /^( *)/.exec(line);
      var indent = indentMatch ? indentMatch[1].length : 0;
      var content = line.slice(indent);
      if (indent === 0) {
        section = null;
        continue;
      }
      if (indent === 2) {
        // Top-level subsection inside the bundle.
        if (content === "seed_files") {
          section = "seed_files";
          currentSeedFile = null;
          continue;
        }
        if (content === "preferences") {
          section = "preferences";
          preferences = {};
          continue;
        }
        if (content === ROOT_HEADER) {
          section = "memory";
          memoryLines = [ROOT_HEADER];
          continue;
        }
        var infoMatch = /^([a-zA-Z0-9_]+)\s+"((?:[^"\\]|\\.)*)"\s*$/.exec(content);
        if (infoMatch) {
          info[infoMatch[1]] = unescapeValue(infoMatch[2]);
        }
        section = null;
        continue;
      }
      if (section === "seed_files") {
        if (indent === 4) {
          var fileMatch = /^file\s+"((?:[^"\\]|\\.)*)"\s*$/.exec(content);
          if (fileMatch) {
            currentSeedFile = unescapeValue(fileMatch[1]);
            seedFiles[currentSeedFile] = "";
          }
          continue;
        }
        if (currentSeedFile && indent >= 6) {
          // Body lines for the current seed file. Strip the 6-space prefix.
          var body = line.length >= 6 ? line.slice(6) : "";
          if (seedFiles[currentSeedFile].length > 0) {
            seedFiles[currentSeedFile] += "\n";
          }
          seedFiles[currentSeedFile] += body;
        }
        continue;
      }
      if (section === "preferences") {
        if (indent === 4) {
          var prefMatch = /^([a-zA-Z0-9_]+)\s+"((?:[^"\\]|\\.)*)"\s*$/.exec(content);
          if (prefMatch) {
            var prefValue = unescapeValue(prefMatch[2]);
            preferences[prefMatch[1]] =
              prefValue === "on" ? true : prefValue === "off" ? false : prefValue;
          }
        }
        continue;
      }
      if (section === "memory") {
        // Strip the leading 2 spaces of bundle indentation so the captured
        // block matches the standalone `demo_memory` shape parsed by
        // `parseLinksNotation`.
        var stripped = line.length >= 2 ? line.slice(2) : line;
        memoryLines.push(stripped);
        continue;
      }
    }
    var events = memoryLines.length > 0
      ? parseLinksNotation(memoryLines.join("\n"))
      : [];
    if (seedFiles["seed/agent-info.lino"]) {
      agentInfo = parseAgentInfo(seedFiles["seed/agent-info.lino"]);
    } else if (seedFiles["data/seed/agent-info.lino"]) {
      // Tolerate the canonical path too — bundles produced by the CLI emit
      // `data/seed/...` while the browser emits `seed/...`.
      agentInfo = parseAgentInfo(seedFiles["data/seed/agent-info.lino"]);
    }
    return {
      kind: "bundle",
      events: events,
      seedFiles: seedFiles,
      preferences: preferences,
      info: info,
      agentInfo: agentInfo,
    };
  }

  // Tiny parser for the `agent_info` block — extracts `field "<key>" \n
  // value "<value>"` pairs so callers can look up the embedded seed
  // version, supported languages, etc. without pulling in the larger seed
  // loader.
  function parseAgentInfo(text) {
    var out = {};
    var lines = String(text || "").split(/\r?\n/);
    var currentField = null;
    var fieldPattern = /^\s{2}field\s+"((?:[^"\\]|\\.)*)"\s*$/;
    var valuePattern = /^\s{4}value\s+"((?:[^"\\]|\\.)*)"\s*$/;
    for (var i = 0; i < lines.length; i += 1) {
      var line = lines[i];
      var fieldMatch = fieldPattern.exec(line);
      if (fieldMatch) {
        currentField = unescapeValue(fieldMatch[1]);
        continue;
      }
      var valueMatch = valuePattern.exec(line);
      if (valueMatch && currentField) {
        out[currentField] = unescapeValue(valueMatch[1]);
        currentField = null;
      }
    }
    return out;
  }

  // Suggest known data migrations between an imported document and the
  // currently running app. The first migration check covers the seed
  // version baked into `agent-info.lino`. Returns an array of human-readable
  // strings — empty when no migration is needed. The function is pure so
  // tests can call it without a browser environment.
  function suggestMigrations(options) {
    var settings = options || {};
    var imported = settings.imported || {};
    var current = settings.current || {};
    var suggestions = [];
    var importedAgent = imported.agentInfo || {};
    var currentAgent = current.agentInfo || {};
    var importedVersion = importedAgent.version || (imported.info && imported.info.version);
    var currentVersion = currentAgent.version || (current.info && current.info.version);
    if (importedVersion && currentVersion && importedVersion !== currentVersion) {
      suggestions.push(
        "Seed version " + importedVersion + "" + currentVersion +
          ": review the new entries in data/seed/ (multilingual responses, concepts, tools) — your imported memory was authored against an older seed.",
      );
    } else if (importedVersion && !currentVersion) {
      suggestions.push(
        "Imported bundle was authored against seed version " + importedVersion +
          " but the running app does not expose a seed version. Update the app to compare.",
      );
    }
    if (imported.kind === "memory") {
      suggestions.push(
        "Imported file is a legacy demo_memory log (no seed). The events were imported, but the seed at the time of capture is unknown — export from this session to upgrade to a full bundle.",
      );
    }
    return suggestions;
  }

  global.FormalAiMemory = {
    appendEvent: appendEvent,
    listEvents: listEvents,
    importEvents: importEvents,
    exportLinksNotation: exportLinksNotation,
    exportBundle: exportBundle,
    exportFullMemory: exportFullMemory,
    importFullMemory: importFullMemory,
    suggestMigrations: suggestMigrations,
    parseLinksNotation: parseLinksNotation,
    parseBundleDocument: parseBundleDocument,
    parseAgentInfo: parseAgentInfo,
    formatEvent: formatEvent,
    DB_NAME: DB_NAME,
    STORE_NAME: STORE_NAME,
    ROOT: ROOT_HEADER,
    BUNDLE_ROOT: BUNDLE_HEADER,
  };
})(typeof window !== "undefined" ? window : globalThis);