@almadar/evaluator 2.41.0 → 2.43.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.
@@ -1,4 +1,4 @@
1
- import { UserContext, TraitConfig, NavItem, AgentContext, LlmContext, WorkspaceContext, SessionContext, MemoryContext, TraceContext, IntegrationContext, SExpr } from '@almadar/core';
1
+ import { EntityRow, EventPayload, UserContext, RuntimeValue, TraitConfig, NavItem, AgentContext, LlmContext, WorkspaceContext, SessionContext, MemoryContext, TraceContext, IntegrationContext, SExpr } from '@almadar/core';
2
2
 
3
3
  /**
4
4
  * Evaluation Context
@@ -15,9 +15,9 @@ import { UserContext, TraitConfig, NavItem, AgentContext, LlmContext, WorkspaceC
15
15
  */
16
16
  interface EvaluationContext {
17
17
  /** Entity data for @entity bindings */
18
- entity: Record<string, unknown>;
18
+ entity: EntityRow;
19
19
  /** Payload data for @payload bindings */
20
- payload: Record<string, unknown>;
20
+ payload: EventPayload;
21
21
  /** Current state for @state binding */
22
22
  state: string;
23
23
  /** Current timestamp for @now binding (defaults to Date.now()) */
@@ -25,9 +25,9 @@ interface EvaluationContext {
25
25
  /** User data for @user bindings (role-based UI) */
26
26
  user?: UserContext;
27
27
  /** Singleton entities for @EntityName bindings */
28
- singletons: Map<string, Record<string, unknown>>;
28
+ singletons: Map<string, EntityRow>;
29
29
  /** Local variables from 'let' bindings */
30
- locals?: Map<string, unknown>;
30
+ locals?: Map<string, RuntimeValue>;
31
31
  /**
32
32
  * Call-site trait config for @config bindings. Populated by
33
33
  * `OrbitalServerRuntime.executeEffects` from `RegisteredOrbital.configByTrait`.
@@ -80,40 +80,46 @@ interface EvaluationContext {
80
80
  trace?: TraceContext;
81
81
  integration?: IntegrationContext;
82
82
  /** Mutate entity fields */
83
- mutateEntity?: (changes: Record<string, unknown>) => void;
83
+ mutateEntity?: (changes: Record<string, RuntimeValue>) => void;
84
84
  /** Emit an event */
85
- emit?: (event: string, payload?: unknown) => void;
85
+ emit?: (event: string, payload?: RuntimeValue) => void;
86
86
  /** Navigate to a route */
87
- navigate?: (route: string, params?: Record<string, unknown>) => void;
87
+ navigate?: (route: string, params?: Record<string, RuntimeValue>) => void;
88
88
  /** Persist data (create/update/delete/batch) */
89
- persist?: (action: 'create' | 'update' | 'delete' | 'batch', data?: Record<string, unknown>) => Promise<void>;
89
+ persist?: (action: 'create' | 'update' | 'delete' | 'batch', data?: Record<string, RuntimeValue>) => Promise<void>;
90
90
  /** Show a notification */
91
91
  notify?: (message: string, type?: 'success' | 'error' | 'warning' | 'info') => void;
92
92
  /** Spawn a new entity instance */
93
- spawn?: (entityType: string, props?: Record<string, unknown>) => void;
93
+ spawn?: (entityType: string, props?: Record<string, RuntimeValue>) => void;
94
94
  /** Despawn an entity instance */
95
95
  despawn?: (entityId?: string) => void;
96
96
  /** Call an external service */
97
- callService?: (service: string, method: string, params?: Record<string, unknown>) => Promise<unknown>;
97
+ callService?: (service: string, method: string, params?: Record<string, RuntimeValue>) => Promise<RuntimeValue>;
98
98
  /** Render UI to a slot */
99
- renderUI?: (slot: string, pattern: unknown, props?: Record<string, unknown>, priority?: number) => void;
99
+ renderUI?: (slot: string, pattern: RuntimeValue, props?: Record<string, RuntimeValue>, priority?: number) => void;
100
100
  /** Register an OS trigger (server-side only) */
101
- registerOsTrigger?: (type: string, config: Record<string, unknown>) => void;
101
+ registerOsTrigger?: (type: string, config: Record<string, RuntimeValue>) => void;
102
102
  /** Effect handlers for resource operators (grouped to avoid top-level pollution) */
103
103
  effectHandlers?: {
104
- ref?: (entityType: string, options?: unknown) => unknown;
105
- deref?: (entityType: string, id?: unknown) => unknown;
106
- swap?: (entityType: string, id: unknown, transformExpr: unknown, evaluate: unknown, ctx: unknown) => unknown;
107
- watch?: (entityType: string, effects: unknown[], evaluate: unknown, ctx: unknown) => void;
108
- atomic?: (effects: unknown[], evaluate: unknown, ctx: unknown) => unknown;
109
- fetch?: (entityType: string, options?: unknown) => unknown;
104
+ ref?: (entityType: string, options?: RuntimeValue) => RuntimeValue;
105
+ deref?: (entityType: string, id?: RuntimeValue) => RuntimeValue;
106
+ swap?: (entityType: string, id: RuntimeValue, transformExpr: SExpr, evaluate: Evaluator$2, ctx: EvaluationContext) => RuntimeValue;
107
+ watch?: (entityType: string, effects: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext) => void;
108
+ atomic?: (effects: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext) => RuntimeValue;
109
+ fetch?: (entityType: string, options?: RuntimeValue) => RuntimeValue;
110
110
  };
111
111
  }
112
+ /**
113
+ * The function operator implementations receive to evaluate child
114
+ * expressions — the interpreter's `evaluate` or a compiled tree's
115
+ * child-dispatch; both share this contract.
116
+ */
117
+ type Evaluator$2 = (expr: SExpr, ctx: EvaluationContext) => RuntimeValue;
112
118
  /**
113
119
  * Create a minimal evaluation context for testing/guards.
114
120
  * Only includes bindings, no effect handlers.
115
121
  */
116
- declare function createMinimalContext(entity?: Record<string, unknown>, payload?: Record<string, unknown>, state?: string): EvaluationContext;
122
+ declare function createMinimalContext(entity?: EntityRow, payload?: EventPayload, state?: string): EvaluationContext;
117
123
  /**
118
124
  * Create a context with effect handlers.
119
125
  * Used for runtime evaluation where effects need to execute.
@@ -123,8 +129,8 @@ declare function createEffectContext(base: EvaluationContext, handlers: Partial<
123
129
  * Create a child context with additional local bindings.
124
130
  * Used for 'let' expressions.
125
131
  */
126
- declare function createChildContext(parent: EvaluationContext, locals: Map<string, unknown>): EvaluationContext;
127
- declare function resolveBinding(binding: string, ctx: EvaluationContext): unknown;
132
+ declare function createChildContext(parent: EvaluationContext, locals: Map<string, RuntimeValue>): EvaluationContext;
133
+ declare function resolveBinding(binding: string, ctx: EvaluationContext): RuntimeValue;
128
134
 
129
135
  /**
130
136
  * Arithmetic Operator Implementations
@@ -132,55 +138,55 @@ declare function resolveBinding(binding: string, ctx: EvaluationContext): unknow
132
138
  * Implements: +, -, *, /, %, abs, min, max, floor, ceil, round, clamp
133
139
  */
134
140
 
135
- type Evaluator$5 = (expr: SExpr, ctx: EvaluationContext) => unknown;
141
+ type Evaluator$1 = (expr: SExpr, ctx: EvaluationContext) => unknown;
136
142
  /**
137
143
  * Evaluate addition: ["+", a, b, ...]
138
144
  */
139
- declare function evalAdd(args: SExpr[], evaluate: Evaluator$5, ctx: EvaluationContext): number;
145
+ declare function evalAdd(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
140
146
  /**
141
147
  * Evaluate subtraction: ["-", a] (negate) or ["-", a, b] (subtract)
142
148
  */
143
- declare function evalSubtract(args: SExpr[], evaluate: Evaluator$5, ctx: EvaluationContext): number;
149
+ declare function evalSubtract(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
144
150
  /**
145
151
  * Evaluate multiplication: ["*", a, b, ...]
146
152
  */
147
- declare function evalMultiply(args: SExpr[], evaluate: Evaluator$5, ctx: EvaluationContext): number;
153
+ declare function evalMultiply(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
148
154
  /**
149
155
  * Evaluate division: ["/", a, b]
150
156
  */
151
- declare function evalDivide(args: SExpr[], evaluate: Evaluator$5, ctx: EvaluationContext): number;
157
+ declare function evalDivide(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
152
158
  /**
153
159
  * Evaluate modulo: ["%", a, b]
154
160
  */
155
- declare function evalModulo(args: SExpr[], evaluate: Evaluator$5, ctx: EvaluationContext): number;
161
+ declare function evalModulo(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
156
162
  /**
157
163
  * Evaluate absolute value: ["abs", a]
158
164
  */
159
- declare function evalAbs(args: SExpr[], evaluate: Evaluator$5, ctx: EvaluationContext): number;
165
+ declare function evalAbs(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
160
166
  /**
161
167
  * Evaluate minimum: ["min", a, b, ...]
162
168
  */
163
- declare function evalMin(args: SExpr[], evaluate: Evaluator$5, ctx: EvaluationContext): number;
169
+ declare function evalMin(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
164
170
  /**
165
171
  * Evaluate maximum: ["max", a, b, ...]
166
172
  */
167
- declare function evalMax(args: SExpr[], evaluate: Evaluator$5, ctx: EvaluationContext): number;
173
+ declare function evalMax(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
168
174
  /**
169
175
  * Evaluate floor: ["floor", a]
170
176
  */
171
- declare function evalFloor(args: SExpr[], evaluate: Evaluator$5, ctx: EvaluationContext): number;
177
+ declare function evalFloor(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
172
178
  /**
173
179
  * Evaluate ceiling: ["ceil", a]
174
180
  */
175
- declare function evalCeil(args: SExpr[], evaluate: Evaluator$5, ctx: EvaluationContext): number;
181
+ declare function evalCeil(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
176
182
  /**
177
183
  * Evaluate round: ["round", a]
178
184
  */
179
- declare function evalRound(args: SExpr[], evaluate: Evaluator$5, ctx: EvaluationContext): number;
185
+ declare function evalRound(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
180
186
  /**
181
187
  * Evaluate clamp: ["clamp", value, min, max]
182
188
  */
183
- declare function evalClamp(args: SExpr[], evaluate: Evaluator$5, ctx: EvaluationContext): number;
189
+ declare function evalClamp(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
184
190
 
185
191
  /**
186
192
  * Comparison Operator Implementations
@@ -188,37 +194,37 @@ declare function evalClamp(args: SExpr[], evaluate: Evaluator$5, ctx: Evaluation
188
194
  * Implements: =, !=, <, >, <=, >=
189
195
  */
190
196
 
191
- type Evaluator$4 = (expr: SExpr, ctx: EvaluationContext) => unknown;
197
+ type Evaluator = (expr: SExpr, ctx: EvaluationContext) => unknown;
192
198
  /**
193
199
  * Evaluate equality: ["=", a, b]
194
200
  * Uses strict equality (===) for type safety.
195
201
  */
196
- declare function evalEqual(args: SExpr[], evaluate: Evaluator$4, ctx: EvaluationContext): boolean;
202
+ declare function evalEqual(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): boolean;
197
203
  /**
198
204
  * Evaluate not-equal: ["!=", a, b]
199
205
  */
200
- declare function evalNotEqual(args: SExpr[], evaluate: Evaluator$4, ctx: EvaluationContext): boolean;
206
+ declare function evalNotEqual(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): boolean;
201
207
  /**
202
208
  * Evaluate less-than: ["<", a, b]
203
209
  */
204
- declare function evalLessThan(args: SExpr[], evaluate: Evaluator$4, ctx: EvaluationContext): boolean;
210
+ declare function evalLessThan(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): boolean;
205
211
  /**
206
212
  * Evaluate greater-than: [">", a, b]
207
213
  */
208
- declare function evalGreaterThan(args: SExpr[], evaluate: Evaluator$4, ctx: EvaluationContext): boolean;
214
+ declare function evalGreaterThan(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): boolean;
209
215
  /**
210
216
  * Evaluate less-than-or-equal: ["<=", a, b]
211
217
  */
212
- declare function evalLessThanOrEqual(args: SExpr[], evaluate: Evaluator$4, ctx: EvaluationContext): boolean;
218
+ declare function evalLessThanOrEqual(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): boolean;
213
219
  /**
214
220
  * Evaluate greater-than-or-equal: [">=", a, b]
215
221
  */
216
- declare function evalGreaterThanOrEqual(args: SExpr[], evaluate: Evaluator$4, ctx: EvaluationContext): boolean;
222
+ declare function evalGreaterThanOrEqual(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): boolean;
217
223
  /**
218
224
  * Evaluate regex match: ["matches", subject, pattern]
219
225
  * Returns true if subject matches the regex pattern.
220
226
  */
221
- declare function evalMatches(args: SExpr[], evaluate: Evaluator$4, ctx: EvaluationContext): boolean;
227
+ declare function evalMatches(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): boolean;
222
228
 
223
229
  /**
224
230
  * Logic Operator Implementations
@@ -227,7 +233,6 @@ declare function evalMatches(args: SExpr[], evaluate: Evaluator$4, ctx: Evaluati
227
233
  * All logic operators support short-circuit evaluation.
228
234
  */
229
235
 
230
- type Evaluator$3 = (expr: SExpr, ctx: EvaluationContext) => unknown;
231
236
  /**
232
237
  * Evaluate logical AND: ["and", a, b, ...]
233
238
  * Operand semantics (matches JS `&&` and the compiled TS path — Phase 5,
@@ -237,7 +242,7 @@ type Evaluator$3 = (expr: SExpr, ctx: EvaluationContext) => unknown;
237
242
  * shipping behaviors — this is observationally identical to returning a
238
243
  * boolean.)
239
244
  */
240
- declare function evalAnd(args: SExpr[], evaluate: Evaluator$3, ctx: EvaluationContext): unknown;
245
+ declare function evalAnd(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
241
246
  /**
242
247
  * Evaluate logical OR: ["or", a, b, ...]
243
248
  * Operand semantics (matches JS `||` and the compiled TS path — Phase 5,
@@ -245,16 +250,16 @@ declare function evalAnd(args: SExpr[], evaluate: Evaluator$3, ctx: EvaluationCo
245
250
  * last argument's value if all are falsy. Short-circuits — does not evaluate
246
251
  * past the first truthy.
247
252
  */
248
- declare function evalOr(args: SExpr[], evaluate: Evaluator$3, ctx: EvaluationContext): unknown;
253
+ declare function evalOr(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
249
254
  /**
250
255
  * Evaluate logical NOT: ["not", a]
251
256
  */
252
- declare function evalNot(args: SExpr[], evaluate: Evaluator$3, ctx: EvaluationContext): boolean;
257
+ declare function evalNot(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): boolean;
253
258
  /**
254
259
  * Evaluate conditional: ["if", condition, then, else]
255
260
  * Only evaluates the branch that matches the condition.
256
261
  */
257
- declare function evalIf(args: SExpr[], evaluate: Evaluator$3, ctx: EvaluationContext): unknown;
262
+ declare function evalIf(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
258
263
 
259
264
  /**
260
265
  * Control Operator Implementations
@@ -262,7 +267,6 @@ declare function evalIf(args: SExpr[], evaluate: Evaluator$3, ctx: EvaluationCon
262
267
  * Implements: let, do, when, fn
263
268
  */
264
269
 
265
- type Evaluator$2 = (expr: SExpr, ctx: EvaluationContext) => unknown;
266
270
  /**
267
271
  * Evaluate let binding.
268
272
  * Supports two formats:
@@ -280,22 +284,22 @@ type Evaluator$2 = (expr: SExpr, ctx: EvaluationContext) => unknown;
280
284
  * reference unresolved (`undefined`), silently corrupting any `let` with
281
285
  * dependent bindings.
282
286
  */
283
- declare function evalLet(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): unknown;
287
+ declare function evalLet(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
284
288
  /**
285
289
  * Evaluate do block: ["do", expr1, expr2, ...]
286
290
  * Executes expressions in sequence, returns last result.
287
291
  */
288
- declare function evalDo(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): unknown;
292
+ declare function evalDo(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
289
293
  /**
290
294
  * Evaluate when: ["when", condition, effect]
291
295
  * Executes effect only when condition is truthy.
292
296
  */
293
- declare function evalWhen(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
297
+ declare function evalWhen(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
294
298
  /**
295
299
  * Evaluate lambda: ["fn", varName, body] or ["fn", [vars], body]
296
300
  * Creates a function that can be passed to collection operators.
297
301
  */
298
- declare function evalFn(args: SExpr[], _evaluate: Evaluator$2, _ctx: EvaluationContext): (item: unknown, evaluate: Evaluator$2, ctx: EvaluationContext) => unknown;
302
+ declare function evalFn(args: SExpr[], _evaluate: Evaluator$2, _ctx: EvaluationContext): (item: RuntimeValue, evaluate: Evaluator$2, ctx: EvaluationContext) => RuntimeValue;
299
303
 
300
304
  /**
301
305
  * Collection Operator Implementations
@@ -306,51 +310,50 @@ declare function evalFn(args: SExpr[], _evaluate: Evaluator$2, _ctx: EvaluationC
306
310
  * matching the Rust evaluator's approach.
307
311
  */
308
312
 
309
- type Evaluator$1 = (expr: SExpr, ctx: EvaluationContext) => unknown;
310
313
  /**
311
314
  * Evaluate map: ["map", collection, expr_using_@item]
312
315
  */
313
- declare function evalMap(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): unknown[];
316
+ declare function evalMap(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue[];
314
317
  /**
315
318
  * Evaluate filter: ["filter", collection, expr_using_@item]
316
319
  */
317
- declare function evalFilter(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): unknown[];
320
+ declare function evalFilter(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue[];
318
321
  /**
319
322
  * Evaluate find: ["find", collection, expr_using_@item]
320
323
  */
321
- declare function evalFind(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): unknown;
324
+ declare function evalFind(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
322
325
  /**
323
326
  * Evaluate count: ["count", collection]
324
327
  */
325
- declare function evalCount(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
328
+ declare function evalCount(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): number;
326
329
  /**
327
330
  * Evaluate sum: ["sum", collection] or ["sum", collection, mapExpr_using_@item]
328
331
  */
329
- declare function evalSum(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): number;
332
+ declare function evalSum(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): number;
330
333
  /**
331
334
  * Evaluate first: ["first", collection]
332
335
  */
333
- declare function evalFirst(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): unknown;
336
+ declare function evalFirst(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
334
337
  /**
335
338
  * Evaluate last: ["last", collection]
336
339
  */
337
- declare function evalLast(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): unknown;
340
+ declare function evalLast(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
338
341
  /**
339
342
  * Evaluate nth: ["nth", collection, index]
340
343
  */
341
- declare function evalNth(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): unknown;
344
+ declare function evalNth(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
342
345
  /**
343
346
  * Evaluate concat: ["concat", collection1, collection2, ...]
344
347
  */
345
- declare function evalConcat(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): unknown[];
348
+ declare function evalConcat(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue[];
346
349
  /**
347
350
  * Evaluate includes: ["includes", collection, element]
348
351
  */
349
- declare function evalIncludes(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): boolean;
352
+ declare function evalIncludes(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): boolean;
350
353
  /**
351
354
  * Evaluate empty: ["empty", collection]
352
355
  */
353
- declare function evalEmpty(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): boolean;
356
+ declare function evalEmpty(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): boolean;
354
357
  /**
355
358
  * `(list a b c …)` — literal array constructor. Evaluates each argument and
356
359
  * returns them as a plain array. Lowering wraps a literal list bound for a
@@ -359,7 +362,7 @@ declare function evalEmpty(args: SExpr[], evaluate: Evaluator$1, ctx: Evaluation
359
362
  * operator name (e.g. `[path method …]` would otherwise be read as the `path`
360
363
  * operator call and collapse to a string).
361
364
  */
362
- declare function evalList(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationContext): unknown[];
365
+ declare function evalList(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue[];
363
366
 
364
367
  /**
365
368
  * Effect Operator Implementations
@@ -369,15 +372,14 @@ declare function evalList(args: SExpr[], evaluate: Evaluator$1, ctx: EvaluationC
369
372
  * Effect operators have side effects and require effect handlers in the context.
370
373
  */
371
374
 
372
- type Evaluator = (expr: SExpr, ctx: EvaluationContext) => unknown;
373
375
  /**
374
376
  * Evaluate set: ["set", "@entity.field", value] or ["set", "@entity.field", value, operation]
375
377
  */
376
- declare function evalSet(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
378
+ declare function evalSet(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
377
379
  /**
378
380
  * Evaluate emit: ["emit", eventKey] or ["emit", eventKey, payload]
379
381
  */
380
- declare function evalEmit(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
382
+ declare function evalEmit(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
381
383
  /**
382
384
  * Evaluate persist:
383
385
  * - ["persist", action] or ["persist", action, data]
@@ -387,27 +389,27 @@ declare function evalEmit(args: SExpr[], evaluate: Evaluator, ctx: EvaluationCon
387
389
  * Each operation is an S-expression: ["create", "collection", {...data}],
388
390
  * ["update", "collection", "id", {...data}], or ["delete", "collection", "id"].
389
391
  */
390
- declare function evalPersist(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
392
+ declare function evalPersist(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
391
393
  /**
392
394
  * Evaluate navigate: ["navigate", route] or ["navigate", route, params]
393
395
  */
394
- declare function evalNavigate(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
396
+ declare function evalNavigate(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
395
397
  /**
396
398
  * Evaluate notify: ["notify", message] or ["notify", message, type]
397
399
  */
398
- declare function evalNotify(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
400
+ declare function evalNotify(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
399
401
  /**
400
402
  * Evaluate spawn: ["spawn", entityType] or ["spawn", entityType, props]
401
403
  */
402
- declare function evalSpawn(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
404
+ declare function evalSpawn(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
403
405
  /**
404
406
  * Evaluate despawn: ["despawn"] or ["despawn", entityId]
405
407
  */
406
- declare function evalDespawn(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
408
+ declare function evalDespawn(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
407
409
  /**
408
410
  * Evaluate call-service: ["call-service", service, method] or ["call-service", service, method, params]
409
411
  */
410
- declare function evalCallService(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
412
+ declare function evalCallService(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
411
413
  /**
412
414
  * Evaluate render-ui:
413
415
  * - ["render-ui", slot, pattern]
@@ -415,45 +417,45 @@ declare function evalCallService(args: SExpr[], evaluate: Evaluator, ctx: Evalua
415
417
  * - ["render-ui", slot, pattern, props, priority]
416
418
  * - ["render-ui", slot, null] - clears the slot
417
419
  */
418
- declare function evalRenderUI(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
420
+ declare function evalRenderUI(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
419
421
  /**
420
422
  * Evaluate set-dynamic: ["set-dynamic", pathExpr, value]
421
423
  * Used for dynamic field paths computed at runtime.
422
424
  * The pathExpr should evaluate to a dot-separated path string.
423
425
  */
424
- declare function evalSetDynamic(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
426
+ declare function evalSetDynamic(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
425
427
  /**
426
428
  * Evaluate increment: ["increment", "@entity.field"] or ["increment", "@entity.field", amount]
427
429
  */
428
- declare function evalIncrement(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
430
+ declare function evalIncrement(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
429
431
  /**
430
432
  * Evaluate decrement: ["decrement", "@entity.field"] or ["decrement", "@entity.field", amount]
431
433
  */
432
- declare function evalDecrement(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
434
+ declare function evalDecrement(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
433
435
  /**
434
436
  * Evaluate ref: ["ref", "EntityType"] or ["ref", "EntityType", { filter, include }]
435
437
  * Server-side: queries entity data. Client-side: subscribes to EntityStore.
436
438
  */
437
- declare function evalRef(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): unknown;
439
+ declare function evalRef(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
438
440
  /**
439
441
  * Evaluate deref: ["deref", "EntityType"] or ["deref", "EntityType", idExpr]
440
442
  * Pure snapshot read. Returns current entity data from store.
441
443
  */
442
- declare function evalDeref(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): unknown;
444
+ declare function evalDeref(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
443
445
  /**
444
446
  * Evaluate swap!: ["swap!", "EntityType", idExpr, transformExpr]
445
447
  * Atomic read-modify-write with CAS retry.
446
448
  */
447
- declare function evalSwap(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): unknown;
449
+ declare function evalSwap(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
448
450
  /**
449
451
  * Evaluate watch: ["watch", "EntityType", [effect1, effect2, ...]]
450
452
  * Client-only. Registers a callback on the EntityStore.
451
453
  */
452
- declare function evalWatch(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
454
+ declare function evalWatch(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): void;
453
455
  /**
454
456
  * Evaluate atomic: ["atomic", [effect1, effect2, ...]]
455
457
  * Groups effects into a transaction. All succeed or all roll back.
456
458
  */
457
- declare function evalAtomic(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): unknown;
459
+ declare function evalAtomic(args: SExpr[], evaluate: Evaluator$2, ctx: EvaluationContext): RuntimeValue;
458
460
 
459
461
  export { evalSetDynamic as $, evalGreaterThanOrEqual as A, evalIf as B, evalIncludes as C, evalIncrement as D, type EvaluationContext as E, evalLast as F, evalLessThan as G, evalLessThanOrEqual as H, evalLet as I, evalList as J, evalMap as K, evalMatches as L, evalMax as M, evalMin as N, evalModulo as O, evalMultiply as P, evalNavigate as Q, evalNot as R, evalNotEqual as S, evalNotify as T, evalNth as U, evalOr as V, evalPersist as W, evalRef as X, evalRenderUI as Y, evalRound as Z, evalSet as _, createEffectContext as a, evalSpawn as a0, evalSubtract as a1, evalSum as a2, evalSwap as a3, evalWatch as a4, evalWhen as a5, resolveBinding as a6, createMinimalContext as b, createChildContext as c, evalAdd as d, evalAbs as e, evalAnd as f, evalAtomic as g, evalCallService as h, evalCeil as i, evalClamp as j, evalConcat as k, evalCount as l, evalDecrement as m, evalDeref as n, evalDespawn as o, evalDivide as p, evalDo as q, evalEmit as r, evalEmpty as s, evalEqual as t, evalFilter as u, evalFind as v, evalFirst as w, evalFloor as x, evalFn as y, evalGreaterThan as z };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { SExpr, ListenPayloadEvaluator } from '@almadar/core';
1
+ import { SExpr, RuntimeValue, ListenPayloadEvaluator } from '@almadar/core';
2
2
  export { CORE_BINDINGS, CoreBinding, Expression, ExpressionSchema, ParsedBinding, SExpr, SExprAtom, SExprSchema, UserContext, collectBindings, getArgs, getOperator, isBinding, isSExpr, isSExprAtom, isSExprCall, isValidBinding, parseBinding, sexpr, walkSExpr } from '@almadar/core';
3
- import { E as EvaluationContext } from './index-BIg56YY8.js';
4
- export { c as createChildContext, a as createEffectContext, b as createMinimalContext, e as evalAbs, d as evalAdd, f as evalAnd, g as evalAtomic, h as evalCallService, i as evalCeil, j as evalClamp, k as evalConcat, l as evalCount, m as evalDecrement, n as evalDeref, o as evalDespawn, p as evalDivide, q as evalDo, r as evalEmit, s as evalEmpty, t as evalEqual, u as evalFilter, v as evalFind, w as evalFirst, x as evalFloor, y as evalFn, z as evalGreaterThan, A as evalGreaterThanOrEqual, B as evalIf, C as evalIncludes, D as evalIncrement, F as evalLast, G as evalLessThan, H as evalLessThanOrEqual, I as evalLet, J as evalList, K as evalMap, L as evalMatches, M as evalMax, N as evalMin, O as evalModulo, P as evalMultiply, Q as evalNavigate, R as evalNot, S as evalNotEqual, T as evalNotify, U as evalNth, V as evalOr, W as evalPersist, X as evalRef, Y as evalRenderUI, Z as evalRound, _ as evalSet, $ as evalSetDynamic, a0 as evalSpawn, a1 as evalSubtract, a2 as evalSum, a3 as evalSwap, a4 as evalWatch, a5 as evalWhen, a6 as resolveBinding } from './index-BIg56YY8.js';
3
+ import { E as EvaluationContext } from './index-DY9A18IY.js';
4
+ export { c as createChildContext, a as createEffectContext, b as createMinimalContext, e as evalAbs, d as evalAdd, f as evalAnd, g as evalAtomic, h as evalCallService, i as evalCeil, j as evalClamp, k as evalConcat, l as evalCount, m as evalDecrement, n as evalDeref, o as evalDespawn, p as evalDivide, q as evalDo, r as evalEmit, s as evalEmpty, t as evalEqual, u as evalFilter, v as evalFind, w as evalFirst, x as evalFloor, y as evalFn, z as evalGreaterThan, A as evalGreaterThanOrEqual, B as evalIf, C as evalIncludes, D as evalIncrement, F as evalLast, G as evalLessThan, H as evalLessThanOrEqual, I as evalLet, J as evalList, K as evalMap, L as evalMatches, M as evalMax, N as evalMin, O as evalModulo, P as evalMultiply, Q as evalNavigate, R as evalNot, S as evalNotEqual, T as evalNotify, U as evalNth, V as evalOr, W as evalPersist, X as evalRef, Y as evalRenderUI, Z as evalRound, _ as evalSet, $ as evalSetDynamic, a0 as evalSpawn, a1 as evalSubtract, a2 as evalSum, a3 as evalSwap, a4 as evalWatch, a5 as evalWhen, a6 as resolveBinding } from './index-DY9A18IY.js';
5
5
 
6
6
  /**
7
7
  * S-Expression Evaluator
@@ -13,19 +13,45 @@ export { c as createChildContext, a as createEffectContext, b as createMinimalCo
13
13
  */
14
14
 
15
15
  /**
16
- * S-Expression Evaluator class.
17
- *
18
- * Provides runtime interpretation of S-expressions for guards, effects, and computed values.
16
+ * A compiled S-expression node: the operator impl and child closures are
17
+ * resolved once at compile time, so a firing pays zero dispatch, zero arity
18
+ * checks, and zero binding-path parsing.
19
19
  */
20
+ type CompiledFn = (ctx: EvaluationContext) => RuntimeValue;
20
21
  declare class SExpressionEvaluator {
22
+ /**
23
+ * Tier-up compilation cache, keyed by node IDENTITY: schema trees are
24
+ * long-lived parsed objects, so a WeakMap costs no key serialization and
25
+ * is collected with the schema. First sight of a node interprets it and
26
+ * marks it seen; the second sight compiles it — one-off dynamically built
27
+ * expressions never pay compile cost.
28
+ */
29
+ private compileCache;
30
+ /** Single bound interpreter handed to operator impls — was allocated per dispatch. */
31
+ private readonly boundInterpret;
21
32
  /**
22
33
  * Evaluate an S-expression in the given context.
34
+ * Hot trees promote to compiled closures on second use; everything else
35
+ * runs through the interpreter.
23
36
  *
24
37
  * @param expr - S-expression to evaluate
25
38
  * @param ctx - Evaluation context with bindings and effect handlers
26
39
  * @returns Result of evaluation
27
40
  */
28
- evaluate(expr: SExpr, ctx: EvaluationContext): unknown;
41
+ evaluate(expr: SExpr, ctx: EvaluationContext): RuntimeValue;
42
+ /**
43
+ * The tree-walking interpreter of record — first-use path and the
44
+ * compiler's fallback for foreign subtrees.
45
+ */
46
+ private interpret;
47
+ /**
48
+ * Compile one tree into composed closures. Every node of the tree is
49
+ * registered in `into` so the impls' `evaluate` callback (childDispatch)
50
+ * resolves in-tree children by identity and falls back to interpretation
51
+ * only for foreign subtrees. Arity is asserted HERE, once — a tree that
52
+ * compiles never re-validates.
53
+ */
54
+ private compileNode;
29
55
  private isPlainObject;
30
56
  /**
31
57
  * Evaluate an S-expression as a guard (returns boolean).
@@ -51,23 +77,27 @@ declare class SExpressionEvaluator {
51
77
  executeEffects(effects: SExpr[], ctx: EvaluationContext): void;
52
78
  /**
53
79
  * Compile an S-expression to a function for faster repeated evaluation.
54
- * Uses a cache to avoid recompilation.
80
+ * Same machinery as the automatic tier-up in `evaluate`; explicit callers
81
+ * promote immediately instead of on second use.
55
82
  *
56
83
  * @param expr - S-expression to compile
57
84
  * @returns Function that evaluates the expression given a context
58
85
  */
59
- compile(expr: SExpr): (ctx: EvaluationContext) => unknown;
86
+ compile(expr: SExpr): CompiledFn;
60
87
  /**
61
- * Clear the JIT compilation cache.
88
+ * Clear the compilation cache.
62
89
  */
63
90
  clearCache(): void;
91
+ /**
92
+ * Dispatch to the appropriate operator implementation.
93
+ */
64
94
  /**
65
95
  * Dispatch to the appropriate operator implementation.
66
96
  */
67
97
  private dispatchOperator;
68
98
  }
69
99
  declare const evaluator: SExpressionEvaluator;
70
- declare function evaluate(expr: SExpr, ctx: EvaluationContext): unknown;
100
+ declare function evaluate(expr: SExpr, ctx: EvaluationContext): RuntimeValue;
71
101
  declare function evaluateGuard(expr: SExpr, ctx: EvaluationContext): boolean;
72
102
  declare function executeEffect(expr: SExpr, ctx: EvaluationContext): void;
73
103
  declare function executeEffects(effects: SExpr[], ctx: EvaluationContext): void;