@benrogmans/lemma-engine 0.6.9 → 0.7.4

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/README.md CHANGED
@@ -46,12 +46,12 @@ const engine = new WasmEngine();
46
46
 
47
47
  ### Core Methods
48
48
 
49
- #### `addLemmaCode(code: string, filename: string): string`
49
+ #### `addLemmaFile(code: string, filename: string): string`
50
50
 
51
51
  Adds a Lemma document to the engine.
52
52
 
53
53
  ```javascript
54
- const result = engine.addLemmaCode(`
54
+ const result = engine.addLemmaFile(`
55
55
  doc employee_contract
56
56
 
57
57
  fact salary = 5000 eur
@@ -78,7 +78,7 @@ Evaluates a document with optional runtime facts.
78
78
  // Evaluate with default facts
79
79
  const result1 = engine.evaluate('employee_contract', '{}');
80
80
 
81
- // Evaluate with runtime fact overrides (as JSON object)
81
+ // Evaluate with runtime fact values (as JSON object)
82
82
  const result2 = engine.evaluate('employee_contract', JSON.stringify({
83
83
  salary: 6000,
84
84
  vacation_days: 30
@@ -162,7 +162,7 @@ async function calculatePricing() {
162
162
 
163
163
  // Load the document
164
164
  const loadResult = JSON.parse(
165
- engine.addLemmaCode(pricingDoc, 'pricing.lemma')
165
+ engine.addLemmaFile(pricingDoc, 'pricing.lemma')
166
166
  );
167
167
 
168
168
  if (!loadResult.success) {
@@ -222,7 +222,7 @@ interface EvaluationResponse {
222
222
  document: string;
223
223
  rules: {
224
224
  [ruleName: string]: {
225
- value: any; // The computed value (e.g., {Number: "100"}, {Unit: "50 EUR"})
225
+ value: any; // The computed value (e.g., {Number: "100"})
226
226
  veto?: string; // Present if rule was vetoed
227
227
  missing_facts?: string[]; // Present if rule couldn't be evaluated
228
228
  operations?: Array<{ // Operation records (always present if rule was evaluated)
@@ -254,8 +254,6 @@ async function typedExample() {
254
254
 
255
255
  if (result.success && result.data) {
256
256
  const price = result.data.rules.final_price?.value;
257
- // price might be {Unit: "100 USD"} or {Number: "100"}
258
- // depending on the rule's result type
259
257
  }
260
258
  }
261
259
  ```
@@ -265,7 +263,7 @@ async function typedExample() {
265
263
  ```javascript
266
264
  try {
267
265
  const result = JSON.parse(
268
- engine.addLemmaCode('invalid syntax', 'bad.lemma')
266
+ engine.addLemmaFile('invalid syntax', 'bad.lemma')
269
267
  );
270
268
 
271
269
  if (!result.success) {
package/lemma.d.ts CHANGED
@@ -1,49 +1,182 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
+
4
+ type ReadableStreamReaderMode = "byob";
5
+
6
+ export class IntoUnderlyingByteSource {
7
+ private constructor();
8
+ free(): void;
9
+ [Symbol.dispose](): void;
10
+ cancel(): void;
11
+ pull(controller: any): Promise<any>;
12
+ start(controller: any): void;
13
+ readonly autoAllocateChunkSize: number;
14
+ readonly type: any;
15
+ }
16
+
17
+ export class IntoUnderlyingSink {
18
+ private constructor();
19
+ free(): void;
20
+ [Symbol.dispose](): void;
21
+ abort(reason: any): Promise<any>;
22
+ close(): Promise<any>;
23
+ write(chunk: any): Promise<any>;
24
+ }
25
+
26
+ export class IntoUnderlyingSource {
27
+ private constructor();
28
+ free(): void;
29
+ [Symbol.dispose](): void;
30
+ cancel(): void;
31
+ pull(controller: any): Promise<any>;
32
+ }
33
+
34
+ /**
35
+ * Raw options for [`pipeTo()`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/pipeTo).
36
+ */
37
+ export class PipeOptions {
38
+ private constructor();
39
+ free(): void;
40
+ [Symbol.dispose](): void;
41
+ readonly preventAbort: boolean;
42
+ readonly preventCancel: boolean;
43
+ readonly preventClose: boolean;
44
+ readonly signal: AbortSignal | undefined;
45
+ }
46
+
47
+ export class QueuingStrategy {
48
+ private constructor();
49
+ free(): void;
50
+ [Symbol.dispose](): void;
51
+ readonly highWaterMark: number;
52
+ }
53
+
54
+ /**
55
+ * Raw options for [`getReader()`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/getReader).
56
+ */
57
+ export class ReadableStreamGetReaderOptions {
58
+ private constructor();
59
+ free(): void;
60
+ [Symbol.dispose](): void;
61
+ readonly mode: ReadableStreamReaderMode;
62
+ }
63
+
64
+ export class ServerConfig {
65
+ free(): void;
66
+ [Symbol.dispose](): void;
67
+ constructor(into_server: AsyncIterator<any>, from_server: WritableStream);
68
+ }
69
+
3
70
  export class WasmEngine {
4
- free(): void;
5
- [Symbol.dispose](): void;
6
- addLemmaCode(code: string, source: string): string;
7
- listDocuments(): string;
8
- constructor();
9
- invert(doc_name: string, rule_name: string, target_json: string, given_facts_json: string): string;
10
- evaluate(doc_name: string, fact_values_json: string): string;
71
+ free(): void;
72
+ [Symbol.dispose](): void;
73
+ /**
74
+ * Add Lemma source (e.g. file contents). Returns a Promise that resolves to a JSON string result.
75
+ */
76
+ addLemmaFile(code: string, source: string): Promise<any>;
77
+ /**
78
+ * Evaluate rules in a document.
79
+ *
80
+ * Pass `rule_names_json` as `"[]"` or `""` to evaluate all rules.
81
+ * Pass a JSON array like `'["total","discount"]'` to evaluate specific rules.
82
+ */
83
+ evaluate(doc_name: string, rule_names_json: string, fact_values_json: string): string;
84
+ /**
85
+ * Format Lemma source code. Returns a JSON string: `{ "success": true, "formatted": "..." }`
86
+ * or `{ "success": false, "error": "..." }`. Only formats if the source parses successfully.
87
+ * Call from JS (e.g. Monaco playground) to implement "Format" without an LSP; there is no on-save in the browser.
88
+ */
89
+ formatSource(code: string, source_attribute: string): string;
90
+ /**
91
+ * Return the full document schema: all facts and rules with their types.
92
+ *
93
+ * Returns the `DocumentSchema` used by all Lemma interfaces, serialized as
94
+ * JSON. Use `getSchema` with specific rule names to get only the facts
95
+ * required by those rules.
96
+ */
97
+ getSchema(doc_name: string, rule_names_json: string): string;
98
+ invert(_doc_name: string, _rule_name: string, _target_json: string, _provided_values_json: string): string;
99
+ /**
100
+ * List all loaded documents with their full schemas.
101
+ *
102
+ * Returns `{ success: true, documents: [DocumentSchema, ...] }` sorted by
103
+ * document name, consistent with the HTTP and MCP interfaces.
104
+ */
105
+ listDocuments(): string;
106
+ constructor();
11
107
  }
12
108
 
109
+ /**
110
+ * Run the Lemma LSP over the given streams. Call from JS after creating
111
+ * an AsyncIterator (client → server messages) and a WritableStream (server → client).
112
+ */
113
+ export function serve(config: ServerConfig): Promise<void>;
114
+
13
115
  export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
14
116
 
15
117
  export interface InitOutput {
16
- readonly memory: WebAssembly.Memory;
17
- readonly __wbg_wasmengine_free: (a: number, b: number) => void;
18
- readonly wasmengine_addLemmaCode: (a: number, b: number, c: number, d: number, e: number) => [number, number];
19
- readonly wasmengine_evaluate: (a: number, b: number, c: number, d: number, e: number) => [number, number];
20
- readonly wasmengine_invert: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => [number, number];
21
- readonly wasmengine_listDocuments: (a: number) => [number, number];
22
- readonly wasmengine_new: () => number;
23
- readonly __wbindgen_free: (a: number, b: number, c: number) => void;
24
- readonly __wbindgen_malloc: (a: number, b: number) => number;
25
- readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
26
- readonly __wbindgen_export_3: WebAssembly.Table;
27
- readonly __wbindgen_start: () => void;
118
+ readonly memory: WebAssembly.Memory;
119
+ readonly __wbg_serverconfig_free: (a: number, b: number) => void;
120
+ readonly serve: (a: number) => number;
121
+ readonly serverconfig_new: (a: number, b: number) => number;
122
+ readonly __wbg_intounderlyingbytesource_free: (a: number, b: number) => void;
123
+ readonly __wbg_intounderlyingsink_free: (a: number, b: number) => void;
124
+ readonly __wbg_intounderlyingsource_free: (a: number, b: number) => void;
125
+ readonly __wbg_pipeoptions_free: (a: number, b: number) => void;
126
+ readonly __wbg_queuingstrategy_free: (a: number, b: number) => void;
127
+ readonly __wbg_readablestreamgetreaderoptions_free: (a: number, b: number) => void;
128
+ readonly intounderlyingbytesource_autoAllocateChunkSize: (a: number) => number;
129
+ readonly intounderlyingbytesource_cancel: (a: number) => void;
130
+ readonly intounderlyingbytesource_pull: (a: number, b: number) => number;
131
+ readonly intounderlyingbytesource_start: (a: number, b: number) => void;
132
+ readonly intounderlyingbytesource_type: (a: number) => number;
133
+ readonly intounderlyingsink_abort: (a: number, b: number) => number;
134
+ readonly intounderlyingsink_close: (a: number) => number;
135
+ readonly intounderlyingsink_write: (a: number, b: number) => number;
136
+ readonly intounderlyingsource_cancel: (a: number) => void;
137
+ readonly intounderlyingsource_pull: (a: number, b: number) => number;
138
+ readonly pipeoptions_preventAbort: (a: number) => number;
139
+ readonly pipeoptions_preventCancel: (a: number) => number;
140
+ readonly pipeoptions_preventClose: (a: number) => number;
141
+ readonly pipeoptions_signal: (a: number) => number;
142
+ readonly queuingstrategy_highWaterMark: (a: number) => number;
143
+ readonly readablestreamgetreaderoptions_mode: (a: number) => number;
144
+ readonly __wbg_wasmengine_free: (a: number, b: number) => void;
145
+ readonly wasmengine_addLemmaFile: (a: number, b: number, c: number, d: number, e: number) => number;
146
+ readonly wasmengine_evaluate: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
147
+ readonly wasmengine_formatSource: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
148
+ readonly wasmengine_getSchema: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
149
+ readonly wasmengine_invert: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => void;
150
+ readonly wasmengine_listDocuments: (a: number, b: number) => void;
151
+ readonly wasmengine_new: () => number;
152
+ readonly __wasm_bindgen_func_elem_8774: (a: number, b: number) => void;
153
+ readonly __wasm_bindgen_func_elem_9930: (a: number, b: number, c: number, d: number) => void;
154
+ readonly __wasm_bindgen_func_elem_8789: (a: number, b: number, c: number) => void;
155
+ readonly __wbindgen_export: (a: number, b: number) => number;
156
+ readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
157
+ readonly __wbindgen_export3: (a: number) => void;
158
+ readonly __wbindgen_export4: (a: number, b: number, c: number) => void;
159
+ readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
28
160
  }
29
161
 
30
162
  export type SyncInitInput = BufferSource | WebAssembly.Module;
163
+
31
164
  /**
32
- * Instantiates the given `module`, which can either be bytes or
33
- * a precompiled `WebAssembly.Module`.
34
- *
35
- * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
36
- *
37
- * @returns {InitOutput}
38
- */
165
+ * Instantiates the given `module`, which can either be bytes or
166
+ * a precompiled `WebAssembly.Module`.
167
+ *
168
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
169
+ *
170
+ * @returns {InitOutput}
171
+ */
39
172
  export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
40
173
 
41
174
  /**
42
- * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
43
- * for everything else, calls `WebAssembly.instantiate` directly.
44
- *
45
- * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
46
- *
47
- * @returns {Promise<InitOutput>}
48
- */
175
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
176
+ * for everything else, calls `WebAssembly.instantiate` directly.
177
+ *
178
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
179
+ *
180
+ * @returns {Promise<InitOutput>}
181
+ */
49
182
  export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;