@dxos/brain 0.0.1 → 0.12.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/LICENSE +105 -0
- package/README.md +32 -1
- package/dist/lib/Builtins.mjs +178 -0
- package/dist/lib/Builtins.mjs.map +1 -0
- package/dist/lib/CompilePrompt.mjs +305 -0
- package/dist/lib/CompilePrompt.mjs.map +1 -0
- package/dist/lib/Compiler.mjs +212 -0
- package/dist/lib/Compiler.mjs.map +1 -0
- package/dist/lib/Encoding.mjs +116 -0
- package/dist/lib/Encoding.mjs.map +1 -0
- package/dist/lib/GoalRules.mjs +161 -0
- package/dist/lib/GoalRules.mjs.map +1 -0
- package/dist/lib/Vocabulary.mjs +92 -0
- package/dist/lib/Vocabulary.mjs.map +1 -0
- package/dist/lib/chunk-rolldown-runtime.mjs +13 -0
- package/dist/lib/index.mjs +7 -0
- package/dist/lib/testing.mjs +831 -0
- package/dist/lib/testing.mjs.map +1 -0
- package/dist/types/src/Builtins.d.ts +57 -0
- package/dist/types/src/Builtins.d.ts.map +1 -0
- package/dist/types/src/Builtins.test.d.ts +2 -0
- package/dist/types/src/Builtins.test.d.ts.map +1 -0
- package/dist/types/src/CompilePrompt.d.ts +107 -0
- package/dist/types/src/CompilePrompt.d.ts.map +1 -0
- package/dist/types/src/CompilePrompt.test.d.ts +2 -0
- package/dist/types/src/CompilePrompt.test.d.ts.map +1 -0
- package/dist/types/src/Compiler.d.ts +76 -0
- package/dist/types/src/Compiler.d.ts.map +1 -0
- package/dist/types/src/Compiler.test.d.ts +2 -0
- package/dist/types/src/Compiler.test.d.ts.map +1 -0
- package/dist/types/src/Encoding.d.ts +42 -0
- package/dist/types/src/Encoding.d.ts.map +1 -0
- package/dist/types/src/GoalRules.d.ts +66 -0
- package/dist/types/src/GoalRules.d.ts.map +1 -0
- package/dist/types/src/GoalRules.test.d.ts +2 -0
- package/dist/types/src/GoalRules.test.d.ts.map +1 -0
- package/dist/types/src/Vocabulary.d.ts +31 -0
- package/dist/types/src/Vocabulary.d.ts.map +1 -0
- package/dist/types/src/Vocabulary.test.d.ts +2 -0
- package/dist/types/src/Vocabulary.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +7 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +4 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/reference.d.ts +9 -0
- package/dist/types/src/testing/reference.d.ts.map +1 -0
- package/dist/types/src/testing/scenarios.d.ts +73 -0
- package/dist/types/src/testing/scenarios.d.ts.map +1 -0
- package/dist/types/src/testing/simulate.d.ts +31 -0
- package/dist/types/src/testing/simulate.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +74 -5
- package/src/Builtins.test.ts +45 -0
- package/src/Builtins.ts +274 -0
- package/src/CompilePrompt.test.ts +117 -0
- package/src/CompilePrompt.ts +376 -0
- package/src/Compiler.test.ts +85 -0
- package/src/Compiler.ts +234 -0
- package/src/Encoding.ts +99 -0
- package/src/GoalRules.test.ts +96 -0
- package/src/GoalRules.ts +181 -0
- package/src/Vocabulary.test.ts +48 -0
- package/src/Vocabulary.ts +88 -0
- package/src/index.ts +10 -0
- package/src/testing/index.ts +7 -0
- package/src/testing/reference.ts +91 -0
- package/src/testing/scenarios.ts +604 -0
- package/src/testing/simulate.ts +127 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
// @import-as-namespace
|
|
6
|
+
|
|
7
|
+
import { Predicate } from '@dxos/pipeline-rdf/types';
|
|
8
|
+
|
|
9
|
+
/** A canonical predicate and the surface forms extraction maps onto it. */
|
|
10
|
+
export type Entry = {
|
|
11
|
+
/** Identifier usable as rule shorthand (`helps_with(dima, X)`). */
|
|
12
|
+
readonly predicate: string;
|
|
13
|
+
readonly synonyms?: ReadonlyArray<string>;
|
|
14
|
+
readonly description?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The lookup key for a surface predicate: pipeline-rdf's relation key (case, auxiliaries and head-verb
|
|
19
|
+
* inflection collapse, as the fact store matches them) with words joined by `_`, so `Will-Work-On`,
|
|
20
|
+
* `working on` and `works_on` compare equal.
|
|
21
|
+
*/
|
|
22
|
+
export const normalize = (surface: string): string =>
|
|
23
|
+
Predicate.normalize(surface.replace(/[_-]+/g, ' ')).replace(/\s+/g, '_');
|
|
24
|
+
|
|
25
|
+
/** The canonical predicates rules may use as shorthand, and the synonyms that map onto them. */
|
|
26
|
+
export class Vocabulary {
|
|
27
|
+
readonly entries: ReadonlyArray<Entry>;
|
|
28
|
+
readonly #canonical = new Map<string, string>();
|
|
29
|
+
readonly #predicates = new Set<string>();
|
|
30
|
+
|
|
31
|
+
/** @throws Error on an invalid predicate name, a reserved name, or a synonym claimed twice. */
|
|
32
|
+
constructor(entries: ReadonlyArray<Entry>, reserved: ReadonlyArray<string> = []) {
|
|
33
|
+
this.entries = entries;
|
|
34
|
+
for (const { predicate, synonyms = [] } of entries) {
|
|
35
|
+
if (!/^[a-z][a-z0-9_]*$/.test(predicate)) {
|
|
36
|
+
throw new Error(`Canonical predicate must be a lowercase identifier: ${predicate}`);
|
|
37
|
+
}
|
|
38
|
+
if (reserved.includes(predicate)) {
|
|
39
|
+
throw new Error(`Canonical predicate collides with a reserved name: ${predicate}`);
|
|
40
|
+
}
|
|
41
|
+
this.#predicates.add(predicate);
|
|
42
|
+
for (const surface of [predicate, ...synonyms]) {
|
|
43
|
+
const key = normalize(surface);
|
|
44
|
+
const existing = this.#canonical.get(key);
|
|
45
|
+
if (existing !== undefined && existing !== predicate) {
|
|
46
|
+
throw new Error(`Synonym ${surface} maps to both ${existing} and ${predicate}`);
|
|
47
|
+
}
|
|
48
|
+
this.#canonical.set(key, predicate);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** True if `name` is a canonical predicate. */
|
|
54
|
+
isCanonical(name: string): boolean {
|
|
55
|
+
return this.#predicates.has(name);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The canonical predicate for a surface form, if the vocabulary knows it. */
|
|
59
|
+
canonical(surface: string): string | undefined {
|
|
60
|
+
return this.#canonical.get(normalize(surface));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The stored predicate for a surface form: canonical if known, otherwise the form unchanged. */
|
|
64
|
+
resolve(surface: string): string {
|
|
65
|
+
return this.canonical(surface) ?? surface;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Creates a vocabulary. */
|
|
70
|
+
export const make = (entries: ReadonlyArray<Entry>, reserved: ReadonlyArray<string> = []): Vocabulary =>
|
|
71
|
+
new Vocabulary(entries, reserved);
|
|
72
|
+
|
|
73
|
+
/** A starter vocabulary covering the example goals; extraction grows it. */
|
|
74
|
+
export const DEFAULT_ENTRIES: ReadonlyArray<Entry> = [
|
|
75
|
+
{ predicate: 'helps_with', synonyms: ['will-help-with', 'helping-with'] },
|
|
76
|
+
{ predicate: 'works_on', synonyms: ['working-on', 'will-work-on', 'working_on'] },
|
|
77
|
+
{ predicate: 'completed', synonyms: ['finished', 'done'] },
|
|
78
|
+
{ predicate: 'shipped', synonyms: ['ships', 'released'] },
|
|
79
|
+
{ predicate: 'proposes', synonyms: ['suggests'] },
|
|
80
|
+
{ predicate: 'received' },
|
|
81
|
+
{ predicate: 'archived' },
|
|
82
|
+
{ predicate: 'deleted' },
|
|
83
|
+
{ predicate: 'filed' },
|
|
84
|
+
{ predicate: 'drafted' },
|
|
85
|
+
{ predicate: 'notified' },
|
|
86
|
+
{ predicate: 'relayed' },
|
|
87
|
+
{ predicate: 'awaiting', synonyms: ['waiting-for'] },
|
|
88
|
+
];
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export * as Builtins from './Builtins.ts';
|
|
6
|
+
export * as CompilePrompt from './CompilePrompt.ts';
|
|
7
|
+
export * as Compiler from './Compiler.ts';
|
|
8
|
+
export * as Encoding from './Encoding.ts';
|
|
9
|
+
export * as GoalRules from './GoalRules.ts';
|
|
10
|
+
export * as Vocabulary from './Vocabulary.ts';
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { trim } from '@dxos/util';
|
|
6
|
+
|
|
7
|
+
/** Hand-written reference compilations of the eight example goals, keyed by scenario number. */
|
|
8
|
+
export const REFERENCE: Readonly<Record<number, string>> = {
|
|
9
|
+
1: trim`
|
|
10
|
+
wake(dima_work) :- speaker(F, dima), not force(F, expressive).
|
|
11
|
+
wake(dima_work) :- fact(F, dima, _, _), not speaker(F, dima).
|
|
12
|
+
`,
|
|
13
|
+
2: trim`
|
|
14
|
+
shipped(F) :- about(F, "release"), about(F, "shipped"), polarity(F, "+"), not mood(F, interrogative), factuality(F, "CT+").
|
|
15
|
+
candidate(F) :- fact(F, _, _, _), about(F, "release").
|
|
16
|
+
wake(release) :- candidate(F), not achieved(goal).
|
|
17
|
+
achieved(goal) :- shipped(F), fact(N, goal, notified, _), saidAt(F, T1), saidAt(N, T2), T2 >= T1.
|
|
18
|
+
`,
|
|
19
|
+
3: trim`
|
|
20
|
+
dima(F) :- speaker(F, dima), force(F, commissive), about(F, "agent plugin").
|
|
21
|
+
wake(reply) :- dima(F), not achieved(goal).
|
|
22
|
+
wake(reply) :- dima(F), achieved(goal).
|
|
23
|
+
wake(followup) :- elapsed(goal, 2d), not achieved(goal).
|
|
24
|
+
achieved(goal) :- dima(F), polarity(F, "+").
|
|
25
|
+
`,
|
|
26
|
+
4: trim`
|
|
27
|
+
gone(M) :- fact(_, M, archived, inbox).
|
|
28
|
+
gone(M) :- fact(_, M, deleted, inbox).
|
|
29
|
+
waiting(M) :- fact(_, M, received, inbox), not gone(M).
|
|
30
|
+
wake(email) :- fact(F, M, received, inbox).
|
|
31
|
+
holds(goal) :- not waiting(_).
|
|
32
|
+
`,
|
|
33
|
+
5: trim`
|
|
34
|
+
filed(F) :- about(F, "tax return"), about(F, "filed"), polarity(F, "+"), not speaker(F, alice).
|
|
35
|
+
achieved(goal) :- filed(F).
|
|
36
|
+
wake(remind) :- due("2027-04-15T00:00:00Z", 30d), not achieved(goal).
|
|
37
|
+
wake(remind) :- due("2027-04-15T00:00:00Z", 7d), not achieved(goal).
|
|
38
|
+
wake(overdue) :- due("2027-04-15T23:59:59Z", 0d), not achieved(goal).
|
|
39
|
+
wake(progress) :- subgoal(goal, G), status(G, achieved).
|
|
40
|
+
wake(progress) :- speaker(F, rich), about(F, "tax").
|
|
41
|
+
`,
|
|
42
|
+
6: trim`
|
|
43
|
+
wake(practice) :- every(1d), not achieved(goal).
|
|
44
|
+
`,
|
|
45
|
+
7: trim`
|
|
46
|
+
meeting(A) :- action(A, book_meeting).
|
|
47
|
+
meeting(A) :- action(A, update_meeting).
|
|
48
|
+
blocks(A) :- meeting(A), actionArg(A, start, T), weekday(T, friday).
|
|
49
|
+
`,
|
|
50
|
+
8: trim`
|
|
51
|
+
mine(F) :- source(F, "session:s42"), speaker(F, rich).
|
|
52
|
+
wake(turn) :- mine(F), not achieved(goal).
|
|
53
|
+
achieved(goal) :- mine(F), force(F, commissive), polarity(F, "+"), fact(D, goal, drafted, _).
|
|
54
|
+
`,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** Plausible but wrong compilations; replaying the scenario must reject each one. */
|
|
58
|
+
export const WRONG: ReadonlyArray<{ readonly scenario: number; readonly note: string; readonly source: string }> = [
|
|
59
|
+
{
|
|
60
|
+
scenario: 3,
|
|
61
|
+
note: 'achievement ignores polarity, so a refusal achieves the goal',
|
|
62
|
+
source: trim`
|
|
63
|
+
achieved(goal) :- speaker(F, dima), force(F, commissive), about(F, "agent plugin").
|
|
64
|
+
wake(followup) :- elapsed(goal, 2d).
|
|
65
|
+
wake(reply) :- speaker(F, dima).
|
|
66
|
+
`,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
scenario: 1,
|
|
70
|
+
note: "only Dima's own messages wake, missing third-party reports",
|
|
71
|
+
source: 'wake(x) :- speaker(F, dima).',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
scenario: 7,
|
|
75
|
+
note: 'any action with any Friday argument is blocked',
|
|
76
|
+
source: 'blocks(A) :- action(A, _), actionArg(A, _, T), weekday(T, friday).',
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
scenario: 4,
|
|
80
|
+
note: 'holds on any archived email instead of on an empty inbox',
|
|
81
|
+
source: trim`
|
|
82
|
+
holds(goal) :- fact(_, M, archived, inbox).
|
|
83
|
+
wake(e) :- fact(F, _, received, inbox).
|
|
84
|
+
`,
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
scenario: 6,
|
|
88
|
+
note: 'elapsed fires once, not daily',
|
|
89
|
+
source: 'wake(p) :- elapsed(goal, 1d).',
|
|
90
|
+
},
|
|
91
|
+
];
|