formal-ai 0.127.0

Formal symbolic AI implementation 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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
// Shared Links Notation seed loader for the demo.
//
// The browser demo loads its multilingual response phrases, concept summaries,
// project registry, tool registry, language-detection rules, prompt patterns, and agent metadata
// from `src/web/seed/*.lino` instead of hardcoded JavaScript constants. Every
// prompt-handling decision is therefore data-driven: a user who wants to
// retune their own agent can edit a `.lino` file and ship it alongside the
// static site without touching the worker code.
//
// `src/web/seed/` is a deploy artefact synchronised from `data/seed/` (the
// canonical source shared with the Rust solver, CLI, Telegram bot, and HTTP
// server). Run `scripts/sync-seed.sh` to refresh the copy.
//
// The loader is intentionally minimal: indentation-based, untyped, and shared
// between the main thread (`window.FormalAiSeed`) and the worker
// (`self.FormalAiSeed`) without any bundler step.

(function (global) {
  "use strict";

  var DEFAULT_FILES = [
    "seed/agent-info.lino",
    "seed/multilingual-responses.lino",
    "seed/concepts.lino",
    "seed/concept-contexts.lino",
    "seed/facts.lino",
    "seed/projects.lino",
    "seed/brainstorm-seeds.lino",
    "seed/personas.lino",
    "seed/summary-topics.lino",
    "seed/coreference.lino",
    "seed/tools.lino",
    "seed/language-detection.lino",
    "seed/prompt-patterns.lino",
    "seed/intent-routing.lino",
    "seed/greetings.lino",
    "seed/identity.lino",
    "seed/hello-world-programs.lino",
    "seed/demo-dialogs.lino",
    "seed/environments.lino",
  ];

  function isWorker() {
    return (
      typeof global.WorkerGlobalScope !== "undefined" &&
      global instanceof global.WorkerGlobalScope
    );
  }

  function assetVersion() {
    if (typeof global.FORMAL_AI_ASSET_VERSION === "string") {
      return global.FORMAL_AI_ASSET_VERSION;
    }
    try {
      var search = global.location && global.location.search;
      var match = search && /[?&]v=([^&]+)/.exec(search);
      return match ? decodeURIComponent(match[1].replace(/\+/g, " ")) : "";
    } catch (_error) {
      return "";
    }
  }

  function withAssetVersion(url) {
    var version = assetVersion();
    if (!version) return url;
    if (
      url.indexOf("://") !== -1 ||
      url.indexOf("//") === 0 ||
      url.indexOf("data:") === 0
    ) {
      return url;
    }
    return (
      url +
      (url.indexOf("?") === -1 ? "?" : "&") +
      "v=" +
      encodeURIComponent(version)
    );
  }

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

  // Parse an indented Links Notation document into a nested structure:
  //
  //   root_node
  //     child "name"
  //       key "value"
  //
  // -> { name: "root_node", children: [ { name: "child", id: "name",
  //          children: [ { name: "key", id: "value", children: [] } ] } ] }
  function parseLino(text) {
    var lines = String(text || "").split(/\r?\n/);
    var root = { name: "", id: "", value: "", children: [], indent: -1 };
    var stack = [root];
    for (var i = 0; i < lines.length; i += 1) {
      var line = lines[i];
      if (!line || /^\s*$/.test(line)) continue;
      var indentMatch = /^(\s*)/.exec(line);
      var indent = indentMatch ? indentMatch[1].length : 0;
      var content = line.slice(indent);
      // Walk back up to find the parent for this indent.
      while (stack.length > 1 && stack[stack.length - 1].indent >= indent) {
        stack.pop();
      }
      var parent = stack[stack.length - 1];
      var node = parseLinoLine(content, indent);
      parent.children.push(node);
      stack.push(node);
    }
    return root.children.length === 1 ? root.children[0] : root;
  }

  function parseLinoLine(content, indent) {
    var node = {
      name: "",
      id: "",
      value: "",
      children: [],
      indent: indent,
    };
    var first = content.indexOf(' "');
    if (first === -1) {
      node.name = content.trim();
      return node;
    }
    node.name = content.slice(0, first).trim();
    var rest = content.slice(first + 1).trim();
    if (rest.length === 0 || rest[0] !== '"') return node;
    var closing = -1;
    for (var i = 1; i < rest.length; i += 1) {
      if (rest[i] === "\\") {
        i += 1;
        continue;
      }
      if (rest[i] === '"') {
        closing = i;
        break;
      }
    }
    if (closing === -1) return node;
    node.id = unescapeValue(rest.slice(1, closing));
    node.value = node.id;
    return node;
  }

  function findChildren(node, name) {
    if (!node || !Array.isArray(node.children)) return [];
    return node.children.filter(function (child) {
      return child.name === name;
    });
  }

  function findChildValue(node, name) {
    var match = findChildren(node, name)[0];
    return match ? match.id : "";
  }

  function extractMultilingualResponses(node) {
    // The seed stores both a canonical `text` (kept stable so deterministic
    // tests can match it) and zero or more `variant` entries. Issue #27 adds
    // `variant`s to greetings so the demo can randomise its hello messages;
    // issue #160 adds separated courtesy acknowledgement/follow-up fragments.
    var responses = {};
    if (!node) return responses;
    var entries = findChildren(node, "response");
    for (var i = 0; i < entries.length; i += 1) {
      var entry = entries[i];
      var intent = findChildValue(entry, "intent");
      var language = findChildValue(entry, "language");
      var text = findChildValue(entry, "text");
      if (!intent || !language || !text) continue;
      var variantNodes = findChildren(entry, "variant");
      var variants = variantNodes
        .map(function (variant) {
          return variant.id || "";
        })
        .filter(function (value) {
          return value && value.length > 0;
        });
      var acknowledgementNodes = findChildren(entry, "ack_variant");
      var acknowledgements = acknowledgementNodes
        .map(function (variant) {
          return variant.id || "";
        })
        .filter(function (value) {
          return value && value.length > 0;
        });
      var followUpNodes = findChildren(entry, "follow_up_variant");
      var followUps = followUpNodes
        .map(function (variant) {
          return variant.id || "";
        })
        .filter(function (value) {
          return value && value.length > 0;
        });
      if (!responses[intent]) responses[intent] = {};
      responses[intent][language] = {
        text: text,
        variants: variants.length > 0 ? variants : [text],
        acknowledgements: acknowledgements,
        followUps: followUps,
      };
    }
    return responses;
  }

  function extractConcepts(root) {
    if (!root || !Array.isArray(root.children)) return [];
    // Concept files are flat — each top-level child is one record.
    var concepts = [];
    var iterate = function (item) {
      if (!item || !item.name) return;
      if (!item.name.startsWith("concept_")) return;
      var aliases = findChildValue(item, "aliases");
      var contexts = findChildValue(item, "contexts");
      var contextLinks = findChildValue(item, "context_links");
      var localized = findChildren(item, "localized").map(function (loc) {
        var locAliases = findChildValue(loc, "aliases");
        return {
          language: loc.id,
          term: findChildValue(loc, "term"),
          aliases: locAliases ? locAliases.split("|").map(trim).filter(Boolean) : [],
          summary: findChildValue(loc, "summary"),
          source: findChildValue(loc, "source"),
          sourceKind: findChildValue(loc, "source_kind"),
        };
      });
      concepts.push({
        slug: item.name,
        term: findChildValue(item, "term"),
        category: findChildValue(item, "category") || "concept",
        summary: findChildValue(item, "summary"),
        source: findChildValue(item, "source"),
        sourceKind: findChildValue(item, "source_kind") || "project-docs",
        wikidata: findChildValue(item, "wikidata"),
        aliases: aliases ? aliases.split("|").map(trim).filter(Boolean) : [],
        contexts: contexts ? contexts.split("|").map(trim).filter(Boolean) : [],
        contextLinks: contextLinks
          ? contextLinks.split("|").map(trim).filter(Boolean)
          : [],
        localized: localized,
      });
    };
    if (root.name && root.name.startsWith("concept_")) {
      iterate(root);
    } else if (Array.isArray(root.children)) {
      root.children.forEach(iterate);
    }
    return concepts;
  }

  // Extract disambiguating context records (`concept-contexts.lino`).
  // Each context is anchored by a Wikidata Q-ID and carries per-language
  // localized labels plus a `|`-separated alias list (free-text phrases the
  // user might type in the four supported languages).
  function extractConceptContexts(root) {
    if (!root || !Array.isArray(root.children)) return [];
    var out = [];
    var visit = function (parent) {
      var entries = findChildren(parent, "context");
      for (var i = 0; i < entries.length; i += 1) {
        var entry = entries[i];
        if (!entry.id) continue;
        var aliases = findChildValue(entry, "aliases");
        var labels = findChildren(entry, "label").map(function (label) {
          return { language: label.id, text: findChildValue(label, "text") };
        });
        out.push({
          slug: entry.id,
          wikidata: findChildValue(entry, "wikidata"),
          aliases: aliases ? aliases.split("|").map(trim).filter(Boolean) : [],
          labels: labels,
        });
      }
    };
    visit(root);
    for (var i = 0; i < root.children.length; i += 1) {
      visit(root.children[i]);
    }
    return out;
  }

  function extractFacts(root) {
    if (!root || !Array.isArray(root.children)) return [];
    var facts = [];
    var visit = function (item) {
      if (!item || !item.name || item.name.indexOf("fact_") !== 0) return;
      var localized = findChildren(item, "localized").map(function (loc) {
        return {
          language: loc.id,
          subjectLabel: findChildValue(loc, "subject_label"),
          valueLabel: findChildValue(loc, "value_label"),
          summary: findChildValue(loc, "summary"),
          source: findChildValue(loc, "source"),
          sourceKind: findChildValue(loc, "source_kind"),
        };
      });
      facts.push({
        slug: item.name,
        intent: findChildValue(item, "intent") || "fact_lookup",
        category: findChildValue(item, "category"),
        wikidata: splitList(findChildValue(item, "wikidata")),
        relation: findChildValue(item, "relation"),
        subjectQid: findChildValue(item, "subject_qid"),
        valueQid: findChildValue(item, "value_qid"),
        subjectLabel: findChildValue(item, "subject_label"),
        valueLabel: findChildValue(item, "value_label"),
        subjectAliases: splitList(findChildValue(item, "subject_aliases")).map(toLower),
        questionKeywords: splitList(findChildValue(item, "question_keywords")).map(toLower),
        summary: findChildValue(item, "summary"),
        source: findChildValue(item, "source"),
        sourceKind: findChildValue(item, "source_kind"),
        localized: localized,
      });
    };
    if (root.name && root.name.indexOf("fact_") === 0) {
      visit(root);
    } else {
      root.children.forEach(visit);
    }
    return facts;
  }

  function extractProjectStatement(node) {
    if (!node || node.name !== "statement" || !node.id) return null;
    return {
      text: String(node.id || "").trim(),
      kind: findChildValue(node, "kind"),
      weight: parseInt(findChildValue(node, "weight"), 10) || 50,
    };
  }

  function normalizeProjectAlias(value) {
    return String(value || "")
      .toLowerCase()
      .replace(/[-_]+/g, " ")
      .replace(/\s+/g, " ")
      .trim();
  }

  function extractProjects(root) {
    if (!root || !Array.isArray(root.children)) return [];
    var projects = [];
    var visit = function (item) {
      if (!item || !item.name || item.name.indexOf("project_") !== 0) return;
      var localized = findChildren(item, "localized").map(function (loc) {
        return {
          language: loc.id,
          displayName: findChildValue(loc, "display_name"),
          statements: findChildren(loc, "statement")
            .map(extractProjectStatement)
            .filter(Boolean),
        };
      });
      projects.push({
        slug: item.name,
        org: findChildValue(item, "org"),
        name: findChildValue(item, "name"),
        displayName: findChildValue(item, "display_name"),
        url: findChildValue(item, "url"),
        language: findChildValue(item, "language"),
        category: findChildValue(item, "category"),
        aliases: splitList(findChildValue(item, "aliases")).map(normalizeProjectAlias),
        topic: findChildValue(item, "topic"),
        statements: findChildren(item, "statement")
          .map(extractProjectStatement)
          .filter(Boolean),
        localized: localized,
      });
    };
    if (root.name && root.name.indexOf("project_") === 0) {
      visit(root);
    } else {
      root.children.forEach(visit);
    }
    return projects;
  }

  function extractBrainstormSeeds(root) {
    var seeds = { triggers: [], categories: [] };
    if (!root || !Array.isArray(root.children)) return seeds;
    var section = root.name === "brainstorm_seeds" ? root : findChildren(root, "brainstorm_seeds")[0];
    if (!section) return seeds;
    seeds.triggers = splitList(findChildValue(section, "trigger")).map(toLower);
    var categories = findChildren(section, "category");
    for (var i = 0; i < categories.length; i += 1) {
      var category = categories[i];
      var items = findChildren(category, "item")
        .map(function (item) {
          return item.id || "";
        })
        .filter(Boolean);
      if (!items.length) continue;
      seeds.categories.push({
        slug: category.id,
        intent: findChildValue(category, "intent"),
        detectionKeywords: splitList(findChildValue(category, "detection_keywords")).map(toLower),
        items: items,
      });
    }
    return seeds;
  }

  function extractPersonas(root) {
    var seeds = {
      triggers: [],
      defaultPersona: "",
      bodyTemplate: "",
      fallbackBody: "",
      personas: [],
      topics: [],
    };
    if (!root || !Array.isArray(root.children)) return seeds;
    var section = root.name === "personas" ? root : findChildren(root, "personas")[0];
    if (!section) return seeds;
    seeds.triggers = splitList(findChildValue(section, "trigger")).map(toLower);
    seeds.defaultPersona = findChildValue(section, "default_persona");
    seeds.bodyTemplate = findChildValue(section, "body_template");
    seeds.fallbackBody = findChildValue(section, "fallback_body");
    var personaEntries = findChildren(section, "persona");
    for (var i = 0; i < personaEntries.length; i += 1) {
      var persona = personaEntries[i];
      seeds.personas.push({
        displayName: persona.id,
        aliases: splitList(findChildValue(persona, "aliases")).map(toLower),
        wikidata: findChildValue(persona, "wikidata"),
      });
    }
    var topicEntries = findChildren(section, "topic");
    for (var j = 0; j < topicEntries.length; j += 1) {
      var topic = topicEntries[j];
      seeds.topics.push({
        slug: topic.id,
        detectionKeywords: splitList(findChildValue(topic, "detection_keywords")).map(toLower),
        body: findChildValue(topic, "body"),
      });
    }
    return seeds;
  }

  function extractTools(node) {
    var tools = [];
    if (!node) return tools;
    var entries = findChildren(node, "tool");
    for (var i = 0; i < entries.length; i += 1) {
      var entry = entries[i];
      var localized = findChildren(entry, "localized").map(function (loc) {
        return {
          language: loc.id,
          name: findChildValue(loc, "name"),
          description: findChildValue(loc, "description"),
        };
      });
      tools.push({
        id: entry.id,
        name: findChildValue(entry, "name"),
        description: findChildValue(entry, "description"),
        mode: findChildValue(entry, "mode") || "thinking",
        inputs: splitList(findChildValue(entry, "inputs")),
        outputs: splitList(findChildValue(entry, "outputs")),
        isolation: findChildValue(entry, "isolation"),
        sources: splitList(findChildValue(entry, "sources")),
        localized: localized,
      });
    }
    return tools;
  }

  function extractAgentInfo(node) {
    var info = {};
    if (!node) return info;
    var entries = findChildren(node, "field");
    for (var i = 0; i < entries.length; i += 1) {
      var entry = entries[i];
      var key = entry.id;
      var value = findChildValue(entry, "value");
      if (key) info[key] = value;
    }
    return info;
  }

  function extractLanguageRules(node) {
    var rules = [];
    if (!node) return rules;
    var entries = findChildren(node, "rule");
    for (var i = 0; i < entries.length; i += 1) {
      var entry = entries[i];
      rules.push({
        id: entry.id,
        language: findChildValue(entry, "language"),
        label: findChildValue(entry, "label"),
        start: parseCodepoint(findChildValue(entry, "start")),
        end: parseCodepoint(findChildValue(entry, "end")),
        note: findChildValue(entry, "note"),
      });
    }
    return rules;
  }

  function parseCodepoint(value) {
    if (!value) return 0;
    var str = String(value).trim();
    if (str.indexOf("0x") === 0 || str.indexOf("0X") === 0) {
      return parseInt(str.slice(2), 16) || 0;
    }
    return parseInt(str, 10) || 0;
  }

  // Extract the environment directory (`environments.lino`) so the demo can
  // show every supported interface and how to migrate memory between them.
  // Mirrors `src/seed.rs::environment_directory` so the two surfaces always
  // agree on the schema.
  function extractEnvironmentDirectory(root) {
    var directory = {
      environments: [],
      migrationDescription: "",
      flows: [],
    };
    if (!root || !Array.isArray(root.children)) return directory;
    for (var i = 0; i < root.children.length; i += 1) {
      var section = root.children[i];
      if (!section || !section.name) continue;
      if (section.name === "environments") {
        var envEntries = findChildren(section, "environment");
        for (var j = 0; j < envEntries.length; j += 1) {
          var entry = envEntries[j];
          directory.environments.push({
            id: entry.id,
            label: findChildValue(entry, "label"),
            runtime: findChildValue(entry, "runtime"),
            seedPath: findChildValue(entry, "seed_path"),
            memoryStore: findChildValue(entry, "memory_store"),
            memoryExport: findChildValue(entry, "memory_export_command"),
            bundleExport: findChildValue(entry, "bundle_export_command"),
            bundleImport: findChildValue(entry, "bundle_import_command"),
            tools: splitList(findChildValue(entry, "tools")),
          });
        }
      } else if (section.name === "migration") {
        directory.migrationDescription = findChildValue(section, "description");
        var flowEntries = findChildren(section, "flow");
        for (var k = 0; k < flowEntries.length; k += 1) {
          var flow = flowEntries[k];
          directory.flows.push({
            id: flow.id,
            description: findChildValue(flow, "description"),
            fileFormat: findChildValue(flow, "file_format"),
          });
        }
      }
    }
    return directory;
  }

  function extractPromptPatterns(node) {
    var patterns = [];
    if (!node) return patterns;
    var entries = findChildren(node, "pattern");
    for (var i = 0; i < entries.length; i += 1) {
      var entry = entries[i];
      patterns.push({
        id: entry.id,
        intent: findChildValue(entry, "intent"),
        language: findChildValue(entry, "language") || "en",
        kind: findChildValue(entry, "kind"),
        text: findChildValue(entry, "text"),
      });
    }
    return patterns;
  }

  // Extract the intent routing table (`intent-routing.lino`) used by the
  // worker to decide between greeting, identity, hello-world and unknown
  // intents in a fully data-driven way. The schema is mirrored from
  // `src/seed.rs::IntentRoute`.
  function extractIntentRouting(node) {
    var routing = {
      intents: [],
      articlePrefixes: [],
      tracePrefixes: [],
    };
    if (!node || !Array.isArray(node.children)) return routing;
    for (var i = 0; i < node.children.length; i += 1) {
      var child = node.children[i];
      if (!child || !child.name) continue;
      if (child.name === "intent") {
        var route = {
          id: child.id,
          slug: findChildValue(child, "slug"),
          responseLink: findChildValue(child, "response_link"),
          keywords: [],
          phrases: [],
          tokens: [],
          combos: [],
        };
        for (var j = 0; j < child.children.length; j += 1) {
          var entry = child.children[j];
          if (entry.name === "keyword") route.keywords.push(entry.id);
          else if (entry.name === "phrase") route.phrases.push(entry.id);
          else if (entry.name === "token") route.tokens.push(entry.id);
          else if (entry.name === "combo") {
            route.combos.push(
              String(entry.id || "")
                .split("+")
                .map(trim)
                .filter(Boolean),
            );
          }
        }
        routing.intents.push(route);
      } else if (child.name === "article") {
        routing.articlePrefixes.push(child.id);
      } else if (child.name === "trace_prefix") {
        routing.tracePrefixes.push(child.id);
      }
    }
    return routing;
  }

  function splitList(value) {
    if (!value) return [];
    return String(value)
      .split("|")
      .map(trim)
      .filter(Boolean);
  }

  function trim(value) {
    return String(value || "").trim();
  }

  function toLower(value) {
    return trim(value).toLowerCase();
  }

  function fetchText(url) {
    if (typeof global.fetch !== "function") {
      return Promise.resolve("");
    }
    return global.fetch(url).then(function (response) {
      if (!response || !response.ok) return "";
      return response.text();
    }, function () {
      return "";
    });
  }

  function loadAll(files) {
    var target = Array.isArray(files) && files.length ? files : DEFAULT_FILES;
    return Promise.all(
      target.map(function (file) {
        return fetchText(withAssetVersion(file)).then(function (text) {
          return { file: file, text: text };
        });
      }),
    ).then(buildSeed);
  }

  function mergeResponses(a, b) {
    var out = {};
    var keys = Object.keys(a).concat(Object.keys(b || {}));
    for (var i = 0; i < keys.length; i += 1) {
      var key = keys[i];
      out[key] = Object.assign({}, a[key] || {}, (b || {})[key] || {});
    }
    return out;
  }

  // Parse a single merged bundle (the `formal_ai_seed_bundle` document
  // produced by `seed::merged_bundle()` on the Rust side) back into a list
  // of `{ file, text }` pairs. Indentation contract:
  //   formal_ai_seed_bundle             (top-level, indent 0)
  //     file "data/seed/X.lino"         (indent 2)
  //       <line of X.lino>              (indent 4)
  //       <line of X.lino>
  //     file "data/seed/Y.lino"
  //       ...
  function parseBundle(text) {
    var lines = String(text || "").split(/\r?\n/);
    var sections = [];
    var current = null;
    for (var i = 0; i < lines.length; i += 1) {
      var line = lines[i];
      if (line === "") {
        if (current) current.text += "\n";
        continue;
      }
      var indentMatch = /^(\s*)/.exec(line);
      var indent = indentMatch ? indentMatch[1].length : 0;
      var trimmed = line.slice(indent);
      if (indent === 0) {
        // New top-level header — flush any open section.
        if (current) sections.push(current);
        current = null;
        continue;
      }
      if (indent === 2 && trimmed.indexOf("file ") === 0) {
        if (current) sections.push(current);
        current = null;
        var rest = trimmed.slice("file ".length).trim();
        if (rest[0] === '"') {
          var inner = rest.slice(1);
          // Reuse the same closing-quote scan as `parseLinoLine`.
          for (var j = 0; j < inner.length; j += 1) {
            if (inner[j] === "\\") {
              j += 1;
              continue;
            }
            if (inner[j] === '"') {
              current = { file: unescapeValue(inner.slice(0, j)), text: "" };
              break;
            }
          }
        }
        continue;
      }
      if (current) {
        var body = line.indexOf("    ") === 0 ? line.slice(4) : trimmed;
        current.text += body + "\n";
      }
    }
    if (current) sections.push(current);
    return sections;
  }

  // Build a seed object from a pre-fetched merged bundle. Mirrors `loadAll`
  // but skips the network step, so a worker booted offline (or one given a
  // user-uploaded bundle) can hydrate without touching `fetch`.
  function loadFromBundle(text) {
    var sections = parseBundle(text);
    return buildSeed(sections);
  }

  function buildSeed(results) {
    var seed = {
      responses: {},
      concepts: [],
      conceptContexts: [],
      facts: [],
      projects: [],
      brainstormSeeds: { triggers: [], categories: [] },
      personas: {
        triggers: [],
        defaultPersona: "",
        bodyTemplate: "",
        fallbackBody: "",
        personas: [],
        topics: [],
      },
      tools: [],
      agentInfo: {},
      languageRules: [],
      promptPatterns: [],
      intentRouting: { intents: [], articlePrefixes: [], tracePrefixes: [] },
      environments: { environments: [], migrationDescription: "", flows: [] },
      raw: {},
    };
    for (var i = 0; i < results.length; i += 1) {
      var item = results[i];
      seed.raw[item.file] = item.text;
      if (!item.text) continue;
      var root = parseLino(item.text);
      if (item.file.indexOf("multilingual") !== -1) {
        seed.responses = mergeResponses(
          seed.responses,
          extractMultilingualResponses(root),
        );
      } else if (item.file.indexOf("facts") !== -1) {
        seed.facts = seed.facts.concat(extractFacts(root));
      } else if (item.file.indexOf("projects") !== -1) {
        seed.projects = seed.projects.concat(extractProjects(root));
      } else if (item.file.indexOf("brainstorm-seeds") !== -1) {
        seed.brainstormSeeds = extractBrainstormSeeds(root);
      } else if (item.file.indexOf("personas") !== -1) {
        seed.personas = extractPersonas(root);
      } else if (item.file.indexOf("concept-contexts") !== -1) {
        seed.conceptContexts = seed.conceptContexts.concat(
          extractConceptContexts(root),
        );
      } else if (item.file.indexOf("concepts") !== -1) {
        seed.concepts = seed.concepts.concat(extractConcepts(root));
      } else if (item.file.indexOf("tools") !== -1) {
        seed.tools = seed.tools.concat(extractTools(root));
      } else if (item.file.indexOf("agent-info") !== -1) {
        Object.assign(seed.agentInfo, extractAgentInfo(root));
      } else if (item.file.indexOf("language-detection") !== -1) {
        seed.languageRules = seed.languageRules.concat(extractLanguageRules(root));
      } else if (item.file.indexOf("prompt-patterns") !== -1) {
        seed.promptPatterns = seed.promptPatterns.concat(extractPromptPatterns(root));
      } else if (item.file.indexOf("intent-routing") !== -1) {
        seed.intentRouting = extractIntentRouting(root);
      } else if (item.file.indexOf("environments") !== -1) {
        seed.environments = extractEnvironmentDirectory(root);
      }
    }
    return seed;
  }

  global.FormalAiSeed = {
    parse: parseLino,
    loadAll: loadAll,
    loadFromBundle: loadFromBundle,
    parseBundle: parseBundle,
    extractMultilingualResponses: extractMultilingualResponses,
    extractAgentInfo: extractAgentInfo,
    extractLanguageRules: extractLanguageRules,
    extractPromptPatterns: extractPromptPatterns,
    extractConcepts: extractConcepts,
    extractConceptContexts: extractConceptContexts,
    extractFacts: extractFacts,
    extractProjects: extractProjects,
    extractBrainstormSeeds: extractBrainstormSeeds,
    extractPersonas: extractPersonas,
    extractTools: extractTools,
    extractIntentRouting: extractIntentRouting,
    extractEnvironmentDirectory: extractEnvironmentDirectory,
    DEFAULT_FILES: DEFAULT_FILES,
    isWorker: isWorker(),
  };
})(typeof self !== "undefined" ? self : globalThis);