@dxos/brain 0.0.1 → 0.13.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.
Files changed (71) hide show
  1. package/LICENSE +105 -0
  2. package/README.md +32 -1
  3. package/dist/lib/Builtins.mjs +184 -0
  4. package/dist/lib/Builtins.mjs.map +1 -0
  5. package/dist/lib/CompilePrompt.mjs +305 -0
  6. package/dist/lib/CompilePrompt.mjs.map +1 -0
  7. package/dist/lib/Compiler.mjs +212 -0
  8. package/dist/lib/Compiler.mjs.map +1 -0
  9. package/dist/lib/Encoding.mjs +116 -0
  10. package/dist/lib/Encoding.mjs.map +1 -0
  11. package/dist/lib/GoalRules.mjs +245 -0
  12. package/dist/lib/GoalRules.mjs.map +1 -0
  13. package/dist/lib/Vocabulary.mjs +92 -0
  14. package/dist/lib/Vocabulary.mjs.map +1 -0
  15. package/dist/lib/chunk-rolldown-runtime.mjs +13 -0
  16. package/dist/lib/index.mjs +7 -0
  17. package/dist/lib/testing.mjs +831 -0
  18. package/dist/lib/testing.mjs.map +1 -0
  19. package/dist/types/src/Builtins.d.ts +61 -0
  20. package/dist/types/src/Builtins.d.ts.map +1 -0
  21. package/dist/types/src/Builtins.test.d.ts +2 -0
  22. package/dist/types/src/Builtins.test.d.ts.map +1 -0
  23. package/dist/types/src/CompilePrompt.d.ts +107 -0
  24. package/dist/types/src/CompilePrompt.d.ts.map +1 -0
  25. package/dist/types/src/CompilePrompt.test.d.ts +2 -0
  26. package/dist/types/src/CompilePrompt.test.d.ts.map +1 -0
  27. package/dist/types/src/Compiler.d.ts +76 -0
  28. package/dist/types/src/Compiler.d.ts.map +1 -0
  29. package/dist/types/src/Compiler.test.d.ts +2 -0
  30. package/dist/types/src/Compiler.test.d.ts.map +1 -0
  31. package/dist/types/src/Encoding.d.ts +42 -0
  32. package/dist/types/src/Encoding.d.ts.map +1 -0
  33. package/dist/types/src/Encoding.test.d.ts +2 -0
  34. package/dist/types/src/Encoding.test.d.ts.map +1 -0
  35. package/dist/types/src/GoalRules.d.ts +125 -0
  36. package/dist/types/src/GoalRules.d.ts.map +1 -0
  37. package/dist/types/src/GoalRules.test.d.ts +2 -0
  38. package/dist/types/src/GoalRules.test.d.ts.map +1 -0
  39. package/dist/types/src/Vocabulary.d.ts +31 -0
  40. package/dist/types/src/Vocabulary.d.ts.map +1 -0
  41. package/dist/types/src/Vocabulary.test.d.ts +2 -0
  42. package/dist/types/src/Vocabulary.test.d.ts.map +1 -0
  43. package/dist/types/src/index.d.ts +7 -0
  44. package/dist/types/src/index.d.ts.map +1 -0
  45. package/dist/types/src/testing/index.d.ts +4 -0
  46. package/dist/types/src/testing/index.d.ts.map +1 -0
  47. package/dist/types/src/testing/reference.d.ts +9 -0
  48. package/dist/types/src/testing/reference.d.ts.map +1 -0
  49. package/dist/types/src/testing/scenarios.d.ts +73 -0
  50. package/dist/types/src/testing/scenarios.d.ts.map +1 -0
  51. package/dist/types/src/testing/simulate.d.ts +31 -0
  52. package/dist/types/src/testing/simulate.d.ts.map +1 -0
  53. package/dist/types/tsconfig.tsbuildinfo +1 -0
  54. package/package.json +74 -5
  55. package/src/Builtins.test.ts +45 -0
  56. package/src/Builtins.ts +284 -0
  57. package/src/CompilePrompt.test.ts +117 -0
  58. package/src/CompilePrompt.ts +376 -0
  59. package/src/Compiler.test.ts +85 -0
  60. package/src/Compiler.ts +234 -0
  61. package/src/Encoding.test.ts +79 -0
  62. package/src/Encoding.ts +99 -0
  63. package/src/GoalRules.test.ts +186 -0
  64. package/src/GoalRules.ts +306 -0
  65. package/src/Vocabulary.test.ts +48 -0
  66. package/src/Vocabulary.ts +88 -0
  67. package/src/index.ts +10 -0
  68. package/src/testing/index.ts +7 -0
  69. package/src/testing/reference.ts +91 -0
  70. package/src/testing/scenarios.ts +604 -0
  71. package/src/testing/simulate.ts +127 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GoalRules.mjs","names":[],"sources":["../../src/GoalRules.ts"],"sourcesContent":["//\n// Copyright 2026 DXOS.org\n//\n\n// @import-as-namespace\n\nimport type * as Ast from '@dxos/datalog/Ast';\nimport * as Engine from '@dxos/datalog/Engine';\nimport { BaseError } from '@dxos/errors';\nimport { RDF } from '@dxos/pipeline-rdf';\n\nimport * as Builtins from './Builtins.ts';\nimport * as Compiler from './Compiler.ts';\nimport * as Encoding from './Encoding.ts';\nimport type * as Vocabulary from './Vocabulary.ts';\n\n/**\n * Size of the evaluator's working memory: past it the oldest facts retire, so a goal that runs\n * indefinitely keeps a bounded engine, text and entity index (and bounded queries over them).\n */\nexport const MAX_FACTS = 10_000;\n\n/** Most sub-goals one evaluator tracks; sub-goals are structural, so exceeding it is an error. */\nexport const MAX_SUBGOALS = 256;\n\n/** Thrown by `GoalRules.update` when an input would take the evaluator past {@link MAX_SUBGOALS}. */\nexport class CapacityError extends BaseError.extend('CapacityError', 'Goal evaluator capacity exceeded') {\n constructor(context: { readonly max: number; readonly requested: number }) {\n super({ message: `Goal evaluator would hold ${context.requested} sub-goals; the cap is ${context.max}.`, context });\n }\n}\n\n/** A reason to run judgment on the goal. */\nexport type Wake = {\n /** The wake rule's label, `achieved`, or the sub-goal whose status changed. */\n readonly label: string;\n readonly cause: 'rule' | 'achieved' | 'subgoal';\n /** Ids of the facts the waking derivation rests on. */\n readonly facts: ReadonlyArray<string>;\n};\n\nexport type Evaluation = {\n readonly at: number;\n readonly wakes: ReadonlyArray<Wake>;\n readonly achieved: boolean;\n readonly holds: boolean;\n};\n\n/** An action the agent proposes, checked against the goal's `blocks` rules before it runs. */\nexport type Action = {\n readonly id: string;\n readonly kind: string;\n readonly args: Readonly<Record<string, string>>;\n};\n\nexport type ActionCheck = {\n readonly blocked: boolean;\n /** Ids of the facts that contributed to the block (empty when only the action did). */\n readonly facts: ReadonlyArray<string>;\n};\n\nexport type Options = {\n readonly source: string;\n /** When the goal was created (epoch ms); also the clock's starting time. */\n readonly createdAt: number;\n readonly vocabulary?: Vocabulary.Vocabulary;\n readonly text?: Builtins.TextIndex;\n readonly entities?: Builtins.EntityIndex;\n /** Entities a fact concerns; defaults to its subject. */\n readonly concerns?: (fact: RDF.Fact) => ReadonlyArray<string>;\n /**\n * Working-memory size; defaults to {@link MAX_FACTS}. Each `update` first retires facts whose\n * `assertion.validTo` is before the clock, then the oldest (by `generatedAtTime`, then arrival)\n * until at most `maxFacts` remain. Retiring drops a fact from the evaluator only; the feed and any\n * external index keep it, and a re-sent fact with a retired id is admitted again as new.\n * Retirement never wakes the goal, and the facts behind the current `achieved` derivation are\n * never retired, so a goal stays achieved once it is; `holds` and wake bindings may lapse as\n * their facts retire, and a lapsed binding wakes again when new facts restore it.\n */\n readonly maxFacts?: number;\n /** Overrides {@link MAX_SUBGOALS}. */\n readonly maxSubgoals?: number;\n};\n\nexport type Input = {\n /** Evaluation time (epoch ms); a change re-evaluates time built-ins. */\n readonly at: number;\n readonly facts?: ReadonlyArray<RDF.Fact>;\n /** Current status of each sub-goal; a new id adds the sub-goal. */\n readonly subgoals?: Readonly<Record<string, string>>;\n};\n\n/**\n * Evaluates one goal's compiled rules over a growing fact stream and a clock. A goal wakes when a\n * wake rule gains a new binding, when `achieved` first becomes true, and when a sub-goal's status\n * changes.\n */\nexport class GoalRules {\n readonly compilation: Compiler.Compilation;\n readonly #engine: Engine.Engine;\n readonly #vocabulary: Vocabulary.Vocabulary;\n readonly #text: Builtins.TextIndex;\n readonly #entities: Builtins.EntityIndex;\n readonly #concerns: (fact: RDF.Fact) => ReadonlyArray<string>;\n /** Working memory in arrival order. */\n readonly #facts = new Map<string, Admitted>();\n readonly #subgoals = new Map<string, string>();\n readonly #maxFacts: number;\n readonly #maxSubgoals: number;\n #now: number;\n #previous: number;\n #achieved = false;\n #sequence = 0;\n\n /** @throws Compiler.CompileError if the rules do not compile. */\n constructor(options: Options) {\n this.#now = options.createdAt;\n this.#previous = options.createdAt;\n this.#maxFacts = options.maxFacts ?? MAX_FACTS;\n this.#maxSubgoals = options.maxSubgoals ?? MAX_SUBGOALS;\n this.#vocabulary = options.vocabulary ?? Compiler.defaultVocabulary();\n this.#text = options.text ?? new Builtins.KeywordIndex();\n this.#entities = options.entities ?? new Builtins.MemoryEntityIndex();\n this.#concerns = options.concerns ?? ((fact) => [RDF.termValue(fact.assertion.subject)]);\n const builtins = Builtins.make({\n clock: {\n now: () => this.#now,\n previous: () => this.#previous,\n createdAt: options.createdAt,\n saidAt: (factId) => this.#facts.get(factId)?.saidAt,\n },\n text: this.#text,\n entities: this.#entities,\n });\n this.compilation = Compiler.compileOrThrow(options.source, { vocabulary: this.#vocabulary, builtins });\n this.#engine = Engine.make({ program: this.compilation.program, builtins, relations: Compiler.RELATIONS });\n this.#achieved = this.achieved;\n }\n\n get achieved(): boolean {\n return this.#engine.has('achieved', ['goal']);\n }\n\n get holds(): boolean {\n return this.#engine.has('holds', ['goal']);\n }\n\n /**\n * Adds facts and sub-goal status at time `at`, retiring facts past the working-memory window\n * (see {@link Options.maxFacts}); returns the wakes and the goal's state.\n * @throws CapacityError if the input would exceed the sub-goal cap; nothing is applied.\n */\n update({ at, facts = [], subgoals = {} }: Input): Evaluation {\n this.#checkSubgoals(subgoals);\n this.#previous = this.#now;\n this.#now = at;\n\n const arrived = new Map<string, { fact: RDF.Fact; admitted: Admitted }>();\n for (const fact of facts) {\n if (!this.#facts.has(fact.id) && !arrived.has(fact.id)) {\n arrived.set(fact.id, { fact, admitted: this.#admit(fact) });\n }\n }\n const retired = this.#retire([...arrived.values()].map(({ admitted }) => admitted));\n const retiring: Engine.Entry[] = [];\n for (const id of retired) {\n const admitted = this.#facts.get(id);\n if (admitted) {\n retiring.push(...admitted.entries);\n this.#facts.delete(id);\n this.#text.remove(id);\n this.#entities.remove(id);\n }\n }\n if (retiring.length > 0) {\n // A separate pass whose changes are discarded, so retirement never wakes the goal.\n this.#engine.update({ retract: retiring });\n }\n\n const insert: Engine.Entry[] = [];\n const retract: Engine.Entry[] = [];\n for (const { fact, admitted } of arrived.values()) {\n if (!retired.has(fact.id)) {\n this.#facts.set(fact.id, admitted);\n this.#text.add(fact.id, factText(fact));\n this.#entities.add(fact.id, this.#concerns(fact));\n insert.push(...admitted.entries);\n }\n }\n\n const wakes: Wake[] = [];\n for (const [subgoal, status] of Object.entries(subgoals)) {\n const previous = this.#subgoals.get(subgoal);\n if (previous === status) {\n continue;\n }\n if (previous === undefined) {\n insert.push({ relation: 'subgoal', tuple: ['goal', subgoal] });\n } else {\n retract.push({ relation: 'status', tuple: [subgoal, previous] });\n wakes.push({ label: subgoal, cause: 'subgoal', facts: [] });\n }\n insert.push({ relation: 'status', tuple: [subgoal, status] });\n this.#subgoals.set(subgoal, status);\n }\n\n const changes = this.#engine.update({ insert, retract, refresh: this.#now !== this.#previous });\n for (const [relation, label] of this.compilation.wakes) {\n for (const tuple of changes.added.get(relation) ?? []) {\n wakes.push({ label, cause: 'rule', facts: Encoding.factIds(this.#engine.provenance(relation, tuple)) });\n }\n }\n\n const achieved = this.achieved;\n if (achieved && !this.#achieved) {\n wakes.push({\n label: 'achieved',\n cause: 'achieved',\n facts: Encoding.factIds(this.#engine.provenance('achieved', ['goal'])),\n });\n }\n this.#achieved = achieved;\n return { at, wakes, achieved, holds: this.holds };\n }\n\n #checkSubgoals(subgoals: Readonly<Record<string, string>>): void {\n const requested = this.#subgoals.size + Object.keys(subgoals).filter((id) => !this.#subgoals.has(id)).length;\n if (requested > this.#maxSubgoals) {\n throw new CapacityError({ max: this.#maxSubgoals, requested });\n }\n }\n\n #admit(fact: RDF.Fact): Admitted {\n const validTo = fact.assertion.validTo === undefined ? undefined : Date.parse(fact.assertion.validTo);\n return {\n id: fact.id,\n sequence: this.#sequence++,\n saidAt: Date.parse(fact.attribution.generatedAtTime),\n validTo: validTo === undefined || Number.isNaN(validTo) ? undefined : validTo,\n entries: Encoding.encode(fact, this.#vocabulary),\n };\n }\n\n /** Ids to retire from working memory plus the arriving facts: expired first, then oldest past the cap. */\n #retire(arriving: ReadonlyArray<Admitted>): Set<string> {\n const pinned = new Set(this.achieved ? Encoding.factIds(this.#engine.provenance('achieved', ['goal'])) : []);\n const retired = new Set<string>();\n const kept: Admitted[] = [];\n for (const admitted of [...this.#facts.values(), ...arriving]) {\n if (!pinned.has(admitted.id) && admitted.validTo !== undefined && admitted.validTo < this.#now) {\n retired.add(admitted.id);\n } else {\n kept.push(admitted);\n }\n }\n let excess = kept.length - this.#maxFacts;\n if (excess > 0) {\n const oldest = kept\n .filter(({ id }) => !pinned.has(id))\n .sort((a, b) => age(a.saidAt) - age(b.saidAt) || a.sequence - b.sequence);\n for (const { id } of oldest) {\n if (excess-- <= 0) {\n break;\n }\n retired.add(id);\n }\n }\n return retired;\n }\n\n /** Evaluates the `blocks` rules for a proposed action without keeping it. */\n checkAction(action: Action): ActionCheck {\n const entries: Engine.Entry[] = [\n { relation: 'action', tuple: [action.id, action.kind] },\n ...Object.entries(action.args).map(([key, value]) => ({ relation: 'actionArg', tuple: [action.id, key, value] })),\n ];\n this.#engine.update({ insert: entries });\n const tuple: Ast.Tuple = [action.id];\n const blocked = this.#engine.has('blocks', tuple);\n const facts = blocked ? Encoding.factIds(this.#engine.provenance('blocks', tuple)) : [];\n this.#engine.update({ retract: entries });\n return { blocked, facts };\n }\n}\n\n/** A fact in working memory, with what retirement needs to order and retract it. */\ntype Admitted = {\n readonly id: string;\n /** Arrival order, the tie-break between facts said at the same time. */\n readonly sequence: number;\n readonly saidAt: number;\n readonly validTo?: number;\n readonly entries: ReadonlyArray<Engine.Entry>;\n};\n\n/** An unparseable `generatedAtTime` sorts as oldest. */\nconst age = (saidAt: number): number => (Number.isNaN(saidAt) ? Number.NEGATIVE_INFINITY : saidAt);\n\n/** Creates a goal evaluator. */\nexport const make = (options: Options): GoalRules => new GoalRules(options);\n\n/** The text `about` matches: what was said plus the triple. */\nconst factText = ({ assertion }: RDF.Fact): string =>\n [assertion.quote ?? '', RDF.termValue(assertion.subject), assertion.predicate, RDF.termValue(assertion.object)].join(\n ' ',\n );\n"],"mappings":";;;;;;;;;;;;;;;;;;;A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AAC,OAAO,IAAI,OAAO,IAAI;EAAE,GACtD,GAAG,OAAO,QAAQ,OAAO,IAAI,CAAC,CAAC,KAAK,CAAC,KAAK,YAAY;GAAE,UAAU;GAAa,OAAO;IAAC,OAAO;IAAI;IAAK;GAAK;EAAE,EAAE,CAClH;EACA,KAAK,QAAQ,OAAO,EAAE,QAAQ,QAAQ,CAAC;EACvC,MAAM,QAAmB,CAAC,OAAO,EAAE;EACnC,MAAM,UAAU,KAAK,QAAQ,IAAI,UAAU,KAAK;EAChD,MAAM,QAAQ,UAAU,QAAiB,KAAK,QAAQ,WAAW,UAAU,KAAK,CAAC,IAAI,CAAC;EACtF,KAAK,QAAQ,OAAO,EAAE,SAAS,QAAQ,CAAC;EACxC,OAAO;GAAE;GAAS;EAAM;CAC1B;AACF;;AAaA,IAAM,OAAO,WAA4B,OAAO,MAAM,MAAM,IAAI,OAAO,oBAAoB;;AAG3F,IAAa,QAAQ,YAAgC,IAAI,UAAU,OAAO;;AAG1E,IAAM,YAAY,EAAE,gBAClB;CAAC,UAAU,SAAS;CAAI,IAAI,UAAU,UAAU,OAAO;CAAG,UAAU;CAAW,IAAI,UAAU,UAAU,MAAM;AAAC,CAAC,CAAC,KAC9G,GACF"}
@@ -0,0 +1,92 @@
1
+ import { t as __exportAll } from "./chunk-rolldown-runtime.mjs";
2
+ import { Predicate } from "@dxos/pipeline-rdf/types";
3
+ //#region src/Vocabulary.ts
4
+ var Vocabulary_exports = /* @__PURE__ */ __exportAll({
5
+ DEFAULT_ENTRIES: () => DEFAULT_ENTRIES,
6
+ Vocabulary: () => Vocabulary,
7
+ make: () => make,
8
+ normalize: () => normalize
9
+ });
10
+ /**
11
+ * The lookup key for a surface predicate: pipeline-rdf's relation key (case, auxiliaries and head-verb
12
+ * inflection collapse, as the fact store matches them) with words joined by `_`, so `Will-Work-On`,
13
+ * `working on` and `works_on` compare equal.
14
+ */
15
+ var normalize = (surface) => Predicate.normalize(surface.replace(/[_-]+/g, " ")).replace(/\s+/g, "_");
16
+ /** The canonical predicates rules may use as shorthand, and the synonyms that map onto them. */
17
+ var Vocabulary = class {
18
+ entries;
19
+ #canonical = /* @__PURE__ */ new Map();
20
+ #predicates = /* @__PURE__ */ new Set();
21
+ /** @throws Error on an invalid predicate name, a reserved name, or a synonym claimed twice. */
22
+ constructor(entries, reserved = []) {
23
+ this.entries = entries;
24
+ for (const { predicate, synonyms = [] } of entries) {
25
+ if (!/^[a-z][a-z0-9_]*$/.test(predicate)) throw new Error(`Canonical predicate must be a lowercase identifier: ${predicate}`);
26
+ if (reserved.includes(predicate)) throw new Error(`Canonical predicate collides with a reserved name: ${predicate}`);
27
+ this.#predicates.add(predicate);
28
+ for (const surface of [predicate, ...synonyms]) {
29
+ const key = normalize(surface);
30
+ const existing = this.#canonical.get(key);
31
+ if (existing !== void 0 && existing !== predicate) throw new Error(`Synonym ${surface} maps to both ${existing} and ${predicate}`);
32
+ this.#canonical.set(key, predicate);
33
+ }
34
+ }
35
+ }
36
+ /** True if `name` is a canonical predicate. */
37
+ isCanonical(name) {
38
+ return this.#predicates.has(name);
39
+ }
40
+ /** The canonical predicate for a surface form, if the vocabulary knows it. */
41
+ canonical(surface) {
42
+ return this.#canonical.get(normalize(surface));
43
+ }
44
+ /** The stored predicate for a surface form: canonical if known, otherwise the form unchanged. */
45
+ resolve(surface) {
46
+ return this.canonical(surface) ?? surface;
47
+ }
48
+ };
49
+ /** Creates a vocabulary. */
50
+ var make = (entries, reserved = []) => new Vocabulary(entries, reserved);
51
+ /** A starter vocabulary covering the example goals; extraction grows it. */
52
+ var DEFAULT_ENTRIES = [
53
+ {
54
+ predicate: "helps_with",
55
+ synonyms: ["will-help-with", "helping-with"]
56
+ },
57
+ {
58
+ predicate: "works_on",
59
+ synonyms: [
60
+ "working-on",
61
+ "will-work-on",
62
+ "working_on"
63
+ ]
64
+ },
65
+ {
66
+ predicate: "completed",
67
+ synonyms: ["finished", "done"]
68
+ },
69
+ {
70
+ predicate: "shipped",
71
+ synonyms: ["ships", "released"]
72
+ },
73
+ {
74
+ predicate: "proposes",
75
+ synonyms: ["suggests"]
76
+ },
77
+ { predicate: "received" },
78
+ { predicate: "archived" },
79
+ { predicate: "deleted" },
80
+ { predicate: "filed" },
81
+ { predicate: "drafted" },
82
+ { predicate: "notified" },
83
+ { predicate: "relayed" },
84
+ {
85
+ predicate: "awaiting",
86
+ synonyms: ["waiting-for"]
87
+ }
88
+ ];
89
+ //#endregion
90
+ export { DEFAULT_ENTRIES, Vocabulary, make, normalize, Vocabulary_exports as t };
91
+
92
+ //# sourceMappingURL=Vocabulary.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Vocabulary.mjs","names":[],"sources":["../../src/Vocabulary.ts"],"sourcesContent":["//\n// Copyright 2026 DXOS.org\n//\n\n// @import-as-namespace\n\nimport { Predicate } from '@dxos/pipeline-rdf/types';\n\n/** A canonical predicate and the surface forms extraction maps onto it. */\nexport type Entry = {\n /** Identifier usable as rule shorthand (`helps_with(dima, X)`). */\n readonly predicate: string;\n readonly synonyms?: ReadonlyArray<string>;\n readonly description?: string;\n};\n\n/**\n * The lookup key for a surface predicate: pipeline-rdf's relation key (case, auxiliaries and head-verb\n * inflection collapse, as the fact store matches them) with words joined by `_`, so `Will-Work-On`,\n * `working on` and `works_on` compare equal.\n */\nexport const normalize = (surface: string): string =>\n Predicate.normalize(surface.replace(/[_-]+/g, ' ')).replace(/\\s+/g, '_');\n\n/** The canonical predicates rules may use as shorthand, and the synonyms that map onto them. */\nexport class Vocabulary {\n readonly entries: ReadonlyArray<Entry>;\n readonly #canonical = new Map<string, string>();\n readonly #predicates = new Set<string>();\n\n /** @throws Error on an invalid predicate name, a reserved name, or a synonym claimed twice. */\n constructor(entries: ReadonlyArray<Entry>, reserved: ReadonlyArray<string> = []) {\n this.entries = entries;\n for (const { predicate, synonyms = [] } of entries) {\n if (!/^[a-z][a-z0-9_]*$/.test(predicate)) {\n throw new Error(`Canonical predicate must be a lowercase identifier: ${predicate}`);\n }\n if (reserved.includes(predicate)) {\n throw new Error(`Canonical predicate collides with a reserved name: ${predicate}`);\n }\n this.#predicates.add(predicate);\n for (const surface of [predicate, ...synonyms]) {\n const key = normalize(surface);\n const existing = this.#canonical.get(key);\n if (existing !== undefined && existing !== predicate) {\n throw new Error(`Synonym ${surface} maps to both ${existing} and ${predicate}`);\n }\n this.#canonical.set(key, predicate);\n }\n }\n }\n\n /** True if `name` is a canonical predicate. */\n isCanonical(name: string): boolean {\n return this.#predicates.has(name);\n }\n\n /** The canonical predicate for a surface form, if the vocabulary knows it. */\n canonical(surface: string): string | undefined {\n return this.#canonical.get(normalize(surface));\n }\n\n /** The stored predicate for a surface form: canonical if known, otherwise the form unchanged. */\n resolve(surface: string): string {\n return this.canonical(surface) ?? surface;\n }\n}\n\n/** Creates a vocabulary. */\nexport const make = (entries: ReadonlyArray<Entry>, reserved: ReadonlyArray<string> = []): Vocabulary =>\n new Vocabulary(entries, reserved);\n\n/** A starter vocabulary covering the example goals; extraction grows it. */\nexport const DEFAULT_ENTRIES: ReadonlyArray<Entry> = [\n { predicate: 'helps_with', synonyms: ['will-help-with', 'helping-with'] },\n { predicate: 'works_on', synonyms: ['working-on', 'will-work-on', 'working_on'] },\n { predicate: 'completed', synonyms: ['finished', 'done'] },\n { predicate: 'shipped', synonyms: ['ships', 'released'] },\n { predicate: 'proposes', synonyms: ['suggests'] },\n { predicate: 'received' },\n { predicate: 'archived' },\n { predicate: 'deleted' },\n { predicate: 'filed' },\n { predicate: 'drafted' },\n { predicate: 'notified' },\n { predicate: 'relayed' },\n { predicate: 'awaiting', synonyms: ['waiting-for'] },\n];\n"],"mappings":";;;;;;;;;;;;;;AAqBA,IAAa,aAAa,YACxB,UAAU,UAAU,QAAQ,QAAQ,UAAU,GAAG,CAAC,CAAC,CAAC,QAAQ,QAAQ,GAAG;;AAGzE,IAAa,aAAb,MAAwB;CACtB;CACA,6BAAsB,IAAI,IAAoB;CAC9C,8BAAuB,IAAI,IAAY;;CAGvC,YAAY,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AAQ;CACrB,EAAE,WAAW,UAAU;CACvB,EAAE,WAAW,WAAW;CACxB,EAAE,WAAW,UAAU;CACvB;EAAE,WAAW;EAAY,UAAU,CAAC,aAAa;CAAE;AACrD"}
@@ -0,0 +1,13 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __defProp = Object.defineProperty;
3
+ var __exportAll = (all, no_symbols) => {
4
+ let target = {};
5
+ for (var name in all) __defProp(target, name, {
6
+ get: all[name],
7
+ enumerable: true
8
+ });
9
+ if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
10
+ return target;
11
+ };
12
+ //#endregion
13
+ export { __exportAll as t };
@@ -0,0 +1,7 @@
1
+ import { t as Builtins_exports } from "./Builtins.mjs";
2
+ import { t as Encoding_exports } from "./Encoding.mjs";
3
+ import { t as Vocabulary_exports } from "./Vocabulary.mjs";
4
+ import { t as Compiler_exports } from "./Compiler.mjs";
5
+ import { t as CompilePrompt_exports } from "./CompilePrompt.mjs";
6
+ import { t as GoalRules_exports } from "./GoalRules.mjs";
7
+ export { Builtins_exports as Builtins, CompilePrompt_exports as CompilePrompt, Compiler_exports as Compiler, Encoding_exports as Encoding, GoalRules_exports as GoalRules, Vocabulary_exports as Vocabulary };