@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.
- package/LICENSE +105 -0
- package/README.md +32 -1
- package/dist/lib/Builtins.mjs +184 -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 +245 -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 +61 -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/Encoding.test.d.ts +2 -0
- package/dist/types/src/Encoding.test.d.ts.map +1 -0
- package/dist/types/src/GoalRules.d.ts +125 -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 +284 -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.test.ts +79 -0
- package/src/Encoding.ts +99 -0
- package/src/GoalRules.test.ts +186 -0
- package/src/GoalRules.ts +306 -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,127 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type { RDF } from '@dxos/pipeline-rdf';
|
|
6
|
+
|
|
7
|
+
import * as Compiler from '../Compiler.ts';
|
|
8
|
+
import * as GoalRules from '../GoalRules.ts';
|
|
9
|
+
import type * as Vocabulary from '../Vocabulary.ts';
|
|
10
|
+
import { type Scenario, type ScenarioFact, type Step } from './scenarios.ts';
|
|
11
|
+
|
|
12
|
+
export type StepResult = {
|
|
13
|
+
readonly id: string;
|
|
14
|
+
readonly wake: boolean;
|
|
15
|
+
readonly labels: ReadonlyArray<string>;
|
|
16
|
+
/** The wakes, with the fact ids behind each. */
|
|
17
|
+
readonly wakes: ReadonlyArray<GoalRules.Wake>;
|
|
18
|
+
readonly achieved: boolean;
|
|
19
|
+
readonly holds: boolean;
|
|
20
|
+
readonly blocks: boolean;
|
|
21
|
+
/** Expectations of this step the result violates. */
|
|
22
|
+
readonly failures: ReadonlyArray<string>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type SimulationResult = {
|
|
26
|
+
readonly ok: boolean;
|
|
27
|
+
readonly steps: ReadonlyArray<StepResult>;
|
|
28
|
+
readonly failures: ReadonlyArray<string>;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** Converts a scripted fact to an `RDF.Fact` said (and recorded) at `saidAt`. */
|
|
32
|
+
export const toFact = (fact: ScenarioFact, saidAt: string): RDF.Fact => ({
|
|
33
|
+
id: fact.id,
|
|
34
|
+
assertion: {
|
|
35
|
+
subject: { kind: 'entity', entity: fact.s },
|
|
36
|
+
predicate: fact.p,
|
|
37
|
+
object: { kind: 'entity', entity: fact.o },
|
|
38
|
+
quote: fact.quote,
|
|
39
|
+
},
|
|
40
|
+
factuality: { value: fact.factuality, polarity: fact.polarity },
|
|
41
|
+
illocution: { force: fact.force, mood: fact.mood },
|
|
42
|
+
attribution: { agent: fact.speaker, source: fact.source, generatedAtTime: saidAt },
|
|
43
|
+
recordedAt: saidAt,
|
|
44
|
+
extractor: { id: 'scenario', model: 'none', version: '1' },
|
|
45
|
+
sourceHash: fact.id,
|
|
46
|
+
pass: 'scenario',
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Replays a scenario against compiled goal rules and checks every step's expectations; a
|
|
51
|
+
* compile error or a runtime error is reported as a failure.
|
|
52
|
+
*/
|
|
53
|
+
export const simulate = (
|
|
54
|
+
scenario: Scenario,
|
|
55
|
+
source: string,
|
|
56
|
+
options: { vocabulary?: Vocabulary.Vocabulary } = {},
|
|
57
|
+
): SimulationResult => {
|
|
58
|
+
const concerns = new Map<string, ReadonlyArray<string>>();
|
|
59
|
+
for (const step of scenario.steps) {
|
|
60
|
+
for (const fact of step.facts ?? []) {
|
|
61
|
+
concerns.set(fact.id, fact.concerns ?? [fact.s]);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let rules: GoalRules.GoalRules;
|
|
66
|
+
try {
|
|
67
|
+
rules = GoalRules.make({
|
|
68
|
+
source,
|
|
69
|
+
createdAt: Date.parse(scenario.createdAt),
|
|
70
|
+
vocabulary: options.vocabulary,
|
|
71
|
+
concerns: (fact) => concerns.get(fact.id) ?? [],
|
|
72
|
+
});
|
|
73
|
+
} catch (error) {
|
|
74
|
+
if (error instanceof Compiler.CompileError) {
|
|
75
|
+
return { ok: false, steps: [], failures: error.diagnostics.map(Compiler.formatDiagnostic) };
|
|
76
|
+
}
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const statuses: Record<string, string> = {};
|
|
81
|
+
const steps = scenario.steps.map((step): StepResult => {
|
|
82
|
+
for (const subgoal of step.subgoals ?? []) {
|
|
83
|
+
statuses[subgoal] = 'active';
|
|
84
|
+
}
|
|
85
|
+
Object.assign(statuses, step.status ?? {});
|
|
86
|
+
const evaluation = rules.update({
|
|
87
|
+
at: Date.parse(step.at),
|
|
88
|
+
facts: (step.facts ?? []).map((fact) => toFact(fact, step.at)),
|
|
89
|
+
subgoals: { ...statuses },
|
|
90
|
+
});
|
|
91
|
+
const blocks = (step.actions ?? []).some((action) => rules.checkAction(action).blocked);
|
|
92
|
+
const observed = {
|
|
93
|
+
wake: evaluation.wakes.length > 0,
|
|
94
|
+
labels: evaluation.wakes.map(({ label }) => label),
|
|
95
|
+
achieved: evaluation.achieved,
|
|
96
|
+
holds: evaluation.holds,
|
|
97
|
+
blocks,
|
|
98
|
+
};
|
|
99
|
+
return { id: step.id, ...observed, wakes: evaluation.wakes, failures: checkStep(step, observed) };
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const failures = [...steps.flatMap((step) => step.failures), ...checkGroups(scenario, steps)];
|
|
103
|
+
return { ok: failures.length === 0, steps, failures };
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const EXPECTATIONS = ['wake', 'achieved', 'holds', 'blocks'] as const;
|
|
107
|
+
|
|
108
|
+
/** The expectations of `step` that `result` violates. */
|
|
109
|
+
const checkStep = (step: Step, result: Pick<StepResult, (typeof EXPECTATIONS)[number] | 'labels'>): string[] =>
|
|
110
|
+
EXPECTATIONS.flatMap((key) => {
|
|
111
|
+
const expected = step.expect[key];
|
|
112
|
+
if (expected === undefined || expected === null || result[key] === expected) {
|
|
113
|
+
return [];
|
|
114
|
+
}
|
|
115
|
+
const labels = key === 'wake' && result.labels.length > 0 ? ` [${result.labels.join(', ')}]` : '';
|
|
116
|
+
return [`${step.id} (${step.note}): ${key}=${result[key]} expected ${expected}${labels}`];
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const checkGroups = (scenario: Scenario, results: ReadonlyArray<StepResult>): string[] => {
|
|
120
|
+
const failures: string[] = [];
|
|
121
|
+
for (const group of scenario.wakeOneOf ?? []) {
|
|
122
|
+
if (!group.some((id) => results[scenario.steps.findIndex((step) => step.id === id)]?.wake)) {
|
|
123
|
+
failures.push(`no wake in any of ${group.join(', ')} (time-driven wake expected)`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return failures;
|
|
127
|
+
};
|