@almadar/evaluator 2.5.6 → 2.7.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 { SExpr } from '@almadar/core';
1
+ import { AgentContext, SExpr } from '@almadar/core';
2
2
 
3
3
  /**
4
4
  * Evaluation Context
@@ -8,6 +8,7 @@ import { SExpr } from '@almadar/core';
8
8
  *
9
9
  * @packageDocumentation
10
10
  */
11
+
11
12
  /**
12
13
  * User context for @user bindings (role-based UI).
13
14
  */
@@ -62,6 +63,8 @@ interface EvaluationContext {
62
63
  _probSeed?: {
63
64
  state: number;
64
65
  };
66
+ /** Agent context for agent/* operators (memory, LLM, tools, session) */
67
+ agent?: AgentContext;
65
68
  /** Mutate entity fields */
66
69
  mutateEntity?: (changes: Record<string, unknown>) => void;
67
70
  /** Emit an event */
@@ -82,6 +85,15 @@ interface EvaluationContext {
82
85
  renderUI?: (slot: string, pattern: unknown, props?: Record<string, unknown>, priority?: number) => void;
83
86
  /** Register an OS trigger (server-side only) */
84
87
  registerOsTrigger?: (type: string, config: Record<string, unknown>) => void;
88
+ /** Effect handlers for resource operators (grouped to avoid top-level pollution) */
89
+ effectHandlers?: {
90
+ ref?: (entityType: string, options?: unknown) => unknown;
91
+ deref?: (entityType: string, id?: unknown) => unknown;
92
+ swap?: (entityType: string, id: unknown, transformExpr: unknown, evaluate: unknown, ctx: unknown) => unknown;
93
+ watch?: (entityType: string, effects: unknown[], evaluate: unknown, ctx: unknown) => void;
94
+ atomic?: (effects: unknown[], evaluate: unknown, ctx: unknown) => unknown;
95
+ fetch?: (entityType: string, options?: unknown) => unknown;
96
+ };
85
97
  }
86
98
  /**
87
99
  * Create a minimal evaluation context for testing/guards.
@@ -383,5 +395,30 @@ declare function evalIncrement(args: SExpr[], evaluate: Evaluator, ctx: Evaluati
383
395
  * Evaluate decrement: ["decrement", "@entity.field"] or ["decrement", "@entity.field", amount]
384
396
  */
385
397
  declare function evalDecrement(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
398
+ /**
399
+ * Evaluate ref: ["ref", "EntityType"] or ["ref", "EntityType", { filter, include }]
400
+ * Server-side: queries entity data. Client-side: subscribes to EntityStore.
401
+ */
402
+ declare function evalRef(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): unknown;
403
+ /**
404
+ * Evaluate deref: ["deref", "EntityType"] or ["deref", "EntityType", idExpr]
405
+ * Pure snapshot read. Returns current entity data from store.
406
+ */
407
+ declare function evalDeref(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): unknown;
408
+ /**
409
+ * Evaluate swap!: ["swap!", "EntityType", idExpr, transformExpr]
410
+ * Atomic read-modify-write with CAS retry.
411
+ */
412
+ declare function evalSwap(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): unknown;
413
+ /**
414
+ * Evaluate watch: ["watch", "EntityType", [effect1, effect2, ...]]
415
+ * Client-only. Registers a callback on the EntityStore.
416
+ */
417
+ declare function evalWatch(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): void;
418
+ /**
419
+ * Evaluate atomic: ["atomic", [effect1, effect2, ...]]
420
+ * Groups effects into a transaction. All succeed or all roll back.
421
+ */
422
+ declare function evalAtomic(args: SExpr[], evaluate: Evaluator, ctx: EvaluationContext): unknown;
386
423
 
387
- export { evalWhen as $, evalIncludes as A, evalIncrement as B, evalLast as C, evalLessThan as D, type EvaluationContext as E, evalLessThanOrEqual as F, evalLet as G, evalMap as H, evalMatches as I, evalMax as J, evalMin as K, evalModulo as L, evalMultiply as M, evalNavigate as N, evalNot as O, evalNotEqual as P, evalNotify as Q, evalNth as R, evalOr as S, evalPersist as T, evalRenderUI as U, evalRound as V, evalSet as W, evalSetDynamic as X, evalSpawn as Y, evalSubtract as Z, evalSum as _, createEffectContext as a, resolveBinding as a0, createMinimalContext as b, createChildContext as c, evalAdd as d, evalAbs as e, evalAnd as f, evalCallService as g, evalCeil as h, evalClamp as i, evalConcat as j, evalCount as k, evalDecrement as l, evalDespawn as m, evalDivide as n, evalDo as o, evalEmit as p, evalEmpty as q, evalEqual as r, evalFilter as s, evalFind as t, evalFirst as u, evalFloor as v, evalFn as w, evalGreaterThan as x, evalGreaterThanOrEqual as y, evalIf as z };
424
+ export { evalSpawn 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, evalMap as J, evalMatches as K, evalMax as L, evalMin as M, evalModulo as N, evalMultiply as O, evalNavigate as P, evalNot as Q, evalNotEqual as R, evalNotify as S, evalNth as T, evalOr as U, evalPersist as V, evalRef as W, evalRenderUI as X, evalRound as Y, evalSet as Z, evalSetDynamic as _, createEffectContext as a, evalSubtract as a0, evalSum as a1, evalSwap as a2, evalWatch as a3, evalWhen as a4, resolveBinding as a5, 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
1
  import { SExpr } from '@almadar/core';
2
2
  export { CORE_BINDINGS, CoreBinding, Expression, ExpressionSchema, ParsedBinding, SExpr, SExprAtom, SExprSchema, collectBindings, getArgs, getOperator, isBinding, isSExpr, isSExprAtom, isSExprCall, isValidBinding, parseBinding, sexpr, walkSExpr } from '@almadar/core';
3
- import { E as EvaluationContext } from './index-Cwku8aP4.js';
4
- export { c as createChildContext, a as createEffectContext, b as createMinimalContext, e as evalAbs, d as evalAdd, f as evalAnd, g as evalCallService, h as evalCeil, i as evalClamp, j as evalConcat, k as evalCount, l as evalDecrement, m as evalDespawn, n as evalDivide, o as evalDo, p as evalEmit, q as evalEmpty, r as evalEqual, s as evalFilter, t as evalFind, u as evalFirst, v as evalFloor, w as evalFn, x as evalGreaterThan, y as evalGreaterThanOrEqual, z as evalIf, A as evalIncludes, B as evalIncrement, C as evalLast, D as evalLessThan, F as evalLessThanOrEqual, G as evalLet, H as evalMap, I as evalMatches, J as evalMax, K as evalMin, L as evalModulo, M as evalMultiply, N as evalNavigate, O as evalNot, P as evalNotEqual, Q as evalNotify, R as evalNth, S as evalOr, T as evalPersist, U as evalRenderUI, V as evalRound, W as evalSet, X as evalSetDynamic, Y as evalSpawn, Z as evalSubtract, _ as evalSum, $ as evalWhen, a0 as resolveBinding } from './index-Cwku8aP4.js';
3
+ import { E as EvaluationContext } from './index-CvAGW0K_.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 evalMap, K as evalMatches, L as evalMax, M as evalMin, N as evalModulo, O as evalMultiply, P as evalNavigate, Q as evalNot, R as evalNotEqual, S as evalNotify, T as evalNth, U as evalOr, V as evalPersist, W as evalRef, X as evalRenderUI, Y as evalRound, Z as evalSet, _ as evalSetDynamic, $ as evalSpawn, a0 as evalSubtract, a1 as evalSum, a2 as evalSwap, a3 as evalWatch, a4 as evalWhen, a5 as resolveBinding } from './index-CvAGW0K_.js';
5
5
 
6
6
  /**
7
7
  * S-Expression Evaluator
package/dist/index.js CHANGED
@@ -574,6 +574,56 @@ function evalDecrement(args, evaluate2, ctx) {
574
574
  const newValue = toNumber3(currentValue) - amount;
575
575
  ctx.mutateEntity({ [fieldPath]: newValue });
576
576
  }
577
+ function evalRef(args, evaluate2, ctx) {
578
+ const entityType = args[0];
579
+ if (ctx.effectHandlers?.ref) {
580
+ return ctx.effectHandlers.ref(entityType, args.length > 1 ? evaluate2(args[1], ctx) : void 0);
581
+ }
582
+ if (ctx.effectHandlers?.fetch) {
583
+ return ctx.effectHandlers.fetch(entityType, args.length > 1 ? evaluate2(args[1], ctx) : void 0);
584
+ }
585
+ return void 0;
586
+ }
587
+ function evalDeref(args, evaluate2, ctx) {
588
+ const entityType = args[0];
589
+ const id = args.length > 1 ? evaluate2(args[1], ctx) : void 0;
590
+ if (ctx.effectHandlers?.deref) {
591
+ return ctx.effectHandlers.deref(entityType, id);
592
+ }
593
+ if (id && ctx.effectHandlers?.fetch) {
594
+ return ctx.effectHandlers.fetch(entityType, id);
595
+ }
596
+ return ctx.entity?.[entityType] ?? [];
597
+ }
598
+ function evalSwap(args, evaluate2, ctx) {
599
+ const entityType = args[0];
600
+ const id = evaluate2(args[1], ctx);
601
+ const transformExpr = args[2];
602
+ if (ctx.effectHandlers?.swap) {
603
+ return ctx.effectHandlers.swap(entityType, id, transformExpr, evaluate2, ctx);
604
+ }
605
+ console.warn("No swap handler in context");
606
+ return void 0;
607
+ }
608
+ function evalWatch(args, evaluate2, ctx) {
609
+ const entityType = args[0];
610
+ const effects = args[1];
611
+ if (ctx.effectHandlers?.watch) {
612
+ ctx.effectHandlers.watch(entityType, effects, evaluate2, ctx);
613
+ }
614
+ }
615
+ function evalAtomic(args, evaluate2, ctx) {
616
+ const effects = args[0];
617
+ if (ctx.effectHandlers?.atomic) {
618
+ return ctx.effectHandlers.atomic(effects, evaluate2, ctx);
619
+ }
620
+ if (Array.isArray(effects)) {
621
+ for (const eff of effects) {
622
+ evaluate2(eff, ctx);
623
+ }
624
+ }
625
+ return void 0;
626
+ }
577
627
  function toNumber3(value) {
578
628
  if (typeof value === "number") return value;
579
629
  if (typeof value === "string") {
@@ -2386,6 +2436,120 @@ function evalOsDebounce(args, evaluate2, ctx) {
2386
2436
  registerTrigger(ctx, "debounce", { ms, eventType });
2387
2437
  }
2388
2438
 
2439
+ // std/agent.ts
2440
+ function evalAgentRecall(args, evaluate2, ctx) {
2441
+ if (!ctx.agent) return [];
2442
+ const query = evaluate2(args[0], ctx);
2443
+ const limit = args.length > 1 ? evaluate2(args[1], ctx) : void 0;
2444
+ return ctx.agent.recall(query, limit);
2445
+ }
2446
+ function evalAgentMemories(args, evaluate2, ctx) {
2447
+ if (!ctx.agent) return [];
2448
+ const category = args.length > 0 ? evaluate2(args[0], ctx) : void 0;
2449
+ return ctx.agent.memories(category);
2450
+ }
2451
+ function evalAgentMemoryStrength(args, evaluate2, ctx) {
2452
+ if (!ctx.agent) return 0;
2453
+ const id = evaluate2(args[0], ctx);
2454
+ return ctx.agent.memoryStrength(id);
2455
+ }
2456
+ function evalAgentIsPinned(args, evaluate2, ctx) {
2457
+ if (!ctx.agent) return false;
2458
+ const id = evaluate2(args[0], ctx);
2459
+ return ctx.agent.isPinned(id);
2460
+ }
2461
+ function evalAgentProvider(_args, _evaluate, ctx) {
2462
+ if (!ctx.agent) return "";
2463
+ return ctx.agent.provider();
2464
+ }
2465
+ function evalAgentModel(_args, _evaluate, ctx) {
2466
+ if (!ctx.agent) return "";
2467
+ return ctx.agent.model();
2468
+ }
2469
+ function evalAgentTools(_args, _evaluate, ctx) {
2470
+ if (!ctx.agent) return [];
2471
+ return ctx.agent.tools();
2472
+ }
2473
+ function evalAgentTokenCount(_args, _evaluate, ctx) {
2474
+ if (!ctx.agent) return 0;
2475
+ return ctx.agent.tokenCount();
2476
+ }
2477
+ function evalAgentContextUsage(_args, _evaluate, ctx) {
2478
+ if (!ctx.agent) return 0;
2479
+ return ctx.agent.contextUsage();
2480
+ }
2481
+ function evalAgentSessionId(_args, _evaluate, ctx) {
2482
+ if (!ctx.agent) return "";
2483
+ return ctx.agent.sessionId();
2484
+ }
2485
+ function evalAgentMemorize(args, evaluate2, ctx) {
2486
+ if (!ctx.agent) return null;
2487
+ const content = evaluate2(args[0], ctx);
2488
+ const category = evaluate2(args[1], ctx);
2489
+ const scope = args.length > 2 ? evaluate2(args[2], ctx) : void 0;
2490
+ return ctx.agent.memorize(content, category, scope);
2491
+ }
2492
+ function evalAgentForget(args, evaluate2, ctx) {
2493
+ if (!ctx.agent) return null;
2494
+ const id = evaluate2(args[0], ctx);
2495
+ return ctx.agent.forget(id);
2496
+ }
2497
+ function evalAgentPin(args, evaluate2, ctx) {
2498
+ if (!ctx.agent) return null;
2499
+ const id = evaluate2(args[0], ctx);
2500
+ return ctx.agent.pin(id);
2501
+ }
2502
+ function evalAgentReinforce(args, evaluate2, ctx) {
2503
+ if (!ctx.agent) return null;
2504
+ const id = evaluate2(args[0], ctx);
2505
+ return ctx.agent.reinforce(id);
2506
+ }
2507
+ function evalAgentDecay(_args, _evaluate, ctx) {
2508
+ if (!ctx.agent) return null;
2509
+ return ctx.agent.decay();
2510
+ }
2511
+ function evalAgentGenerate(args, evaluate2, ctx) {
2512
+ if (!ctx.agent) return null;
2513
+ const prompt = evaluate2(args[0], ctx);
2514
+ const options = args.length > 1 ? evaluate2(args[1], ctx) : void 0;
2515
+ return ctx.agent.generate(prompt, options);
2516
+ }
2517
+ function evalAgentSwitchProvider(args, evaluate2, ctx) {
2518
+ if (!ctx.agent) return null;
2519
+ const provider = evaluate2(args[0], ctx);
2520
+ const model = args.length > 1 ? evaluate2(args[1], ctx) : void 0;
2521
+ ctx.agent.switchProvider(provider, model);
2522
+ return null;
2523
+ }
2524
+ function evalAgentInvoke(args, evaluate2, ctx) {
2525
+ if (!ctx.agent) return null;
2526
+ const toolName = evaluate2(args[0], ctx);
2527
+ const toolArgs = evaluate2(args[1], ctx);
2528
+ return ctx.agent.invoke(toolName, toolArgs);
2529
+ }
2530
+ function evalAgentCompact(args, evaluate2, ctx) {
2531
+ if (!ctx.agent) return null;
2532
+ const strategy = args.length > 0 ? evaluate2(args[0], ctx) : void 0;
2533
+ return ctx.agent.compact(strategy);
2534
+ }
2535
+ function evalAgentFork(args, evaluate2, ctx) {
2536
+ if (!ctx.agent) return null;
2537
+ const label = args.length > 0 ? evaluate2(args[0], ctx) : void 0;
2538
+ return ctx.agent.fork(label);
2539
+ }
2540
+ function evalAgentLabel(args, evaluate2, ctx) {
2541
+ if (!ctx.agent) return null;
2542
+ const text = evaluate2(args[0], ctx);
2543
+ ctx.agent.label(text);
2544
+ return null;
2545
+ }
2546
+ function evalAgentSearchCode(args, evaluate2, ctx) {
2547
+ if (!ctx.agent) return null;
2548
+ const query = evaluate2(args[0], ctx);
2549
+ const language = args.length > 1 ? evaluate2(args[1], ctx) : void 0;
2550
+ return ctx.agent.searchCode(query, language);
2551
+ }
2552
+
2389
2553
  // SExpressionEvaluator.ts
2390
2554
  var jitCache = /* @__PURE__ */ new Map();
2391
2555
  var MAX_JIT_CACHE_SIZE = 1e3;
@@ -2593,6 +2757,18 @@ var SExpressionEvaluator = class {
2593
2757
  case "render-ui":
2594
2758
  evalRenderUI(args, evaluate2, ctx);
2595
2759
  return void 0;
2760
+ // Resource operators
2761
+ case "ref":
2762
+ return evalRef(args, evaluate2, ctx);
2763
+ case "deref":
2764
+ return evalDeref(args, evaluate2, ctx);
2765
+ case "swap!":
2766
+ return evalSwap(args, evaluate2, ctx);
2767
+ case "watch":
2768
+ evalWatch(args, evaluate2, ctx);
2769
+ return void 0;
2770
+ case "atomic":
2771
+ return evalAtomic(args, evaluate2, ctx);
2596
2772
  // ===============================
2597
2773
  // Standard Library: math/*
2598
2774
  // ===============================
@@ -3020,6 +3196,64 @@ var SExpressionEvaluator = class {
3020
3196
  case "os/debounce":
3021
3197
  evalOsDebounce(args, evaluate2, ctx);
3022
3198
  return void 0;
3199
+ // ===============================
3200
+ // Standard Library: agent/*
3201
+ // ===============================
3202
+ // Pure (memory)
3203
+ case "agent/recall":
3204
+ return evalAgentRecall(args, evaluate2, ctx);
3205
+ case "agent/memories":
3206
+ return evalAgentMemories(args, evaluate2, ctx);
3207
+ case "agent/memory-strength":
3208
+ return evalAgentMemoryStrength(args, evaluate2, ctx);
3209
+ case "agent/is-pinned":
3210
+ return evalAgentIsPinned(args, evaluate2, ctx);
3211
+ // Pure (LLM)
3212
+ case "agent/provider":
3213
+ return evalAgentProvider(args, evaluate2, ctx);
3214
+ case "agent/model":
3215
+ return evalAgentModel(args, evaluate2, ctx);
3216
+ // Pure (tools)
3217
+ case "agent/tools":
3218
+ return evalAgentTools(args, evaluate2, ctx);
3219
+ // Pure (context)
3220
+ case "agent/token-count":
3221
+ return evalAgentTokenCount(args, evaluate2, ctx);
3222
+ case "agent/context-usage":
3223
+ return evalAgentContextUsage(args, evaluate2, ctx);
3224
+ // Pure (session)
3225
+ case "agent/session-id":
3226
+ return evalAgentSessionId(args, evaluate2, ctx);
3227
+ // Effects (memory)
3228
+ case "agent/memorize":
3229
+ return evalAgentMemorize(args, evaluate2, ctx);
3230
+ case "agent/forget":
3231
+ return evalAgentForget(args, evaluate2, ctx);
3232
+ case "agent/pin":
3233
+ return evalAgentPin(args, evaluate2, ctx);
3234
+ case "agent/reinforce":
3235
+ return evalAgentReinforce(args, evaluate2, ctx);
3236
+ case "agent/decay":
3237
+ return evalAgentDecay(args, evaluate2, ctx);
3238
+ // Effects (LLM)
3239
+ case "agent/generate":
3240
+ return evalAgentGenerate(args, evaluate2, ctx);
3241
+ case "agent/switch-provider":
3242
+ return evalAgentSwitchProvider(args, evaluate2, ctx);
3243
+ // Effects (tools)
3244
+ case "agent/invoke":
3245
+ return evalAgentInvoke(args, evaluate2, ctx);
3246
+ // Effects (context)
3247
+ case "agent/compact":
3248
+ return evalAgentCompact(args, evaluate2, ctx);
3249
+ // Effects (session)
3250
+ case "agent/fork":
3251
+ return evalAgentFork(args, evaluate2, ctx);
3252
+ case "agent/label":
3253
+ return evalAgentLabel(args, evaluate2, ctx);
3254
+ // Effects (search)
3255
+ case "agent/search-code":
3256
+ return evalAgentSearchCode(args, evaluate2, ctx);
3023
3257
  default:
3024
3258
  console.warn(`Unknown operator: ${op}`);
3025
3259
  return void 0;
@@ -3040,6 +3274,6 @@ function executeEffects(effects, ctx) {
3040
3274
  evaluator.executeEffects(effects, ctx);
3041
3275
  }
3042
3276
 
3043
- export { SExpressionEvaluator, createChildContext, createEffectContext, createMinimalContext, evalAbs, evalAdd, evalAnd, evalCallService, evalCeil, evalClamp, evalConcat, evalCount, evalDecrement, evalDespawn, evalDivide, evalDo, evalEmit, evalEmpty, evalEqual, evalFilter, evalFind, evalFirst, evalFloor, evalFn, evalGreaterThan, evalGreaterThanOrEqual, evalIf, evalIncludes, evalIncrement, evalLast, evalLessThan, evalLessThanOrEqual, evalLet, evalMap, evalMatches, evalMax, evalMin, evalModulo, evalMultiply, evalNavigate, evalNot, evalNotEqual, evalNotify, evalNth, evalOr, evalPersist, evalRenderUI, evalRound, evalSet, evalSetDynamic, evalSpawn, evalSubtract, evalSum, evalWhen, evaluate, evaluateGuard, evaluator, executeEffect, executeEffects, resolveBinding };
3277
+ export { SExpressionEvaluator, createChildContext, createEffectContext, createMinimalContext, evalAbs, evalAdd, evalAnd, evalAtomic, evalCallService, evalCeil, evalClamp, evalConcat, evalCount, evalDecrement, evalDeref, evalDespawn, evalDivide, evalDo, evalEmit, evalEmpty, evalEqual, evalFilter, evalFind, evalFirst, evalFloor, evalFn, evalGreaterThan, evalGreaterThanOrEqual, evalIf, evalIncludes, evalIncrement, evalLast, evalLessThan, evalLessThanOrEqual, evalLet, evalMap, evalMatches, evalMax, evalMin, evalModulo, evalMultiply, evalNavigate, evalNot, evalNotEqual, evalNotify, evalNth, evalOr, evalPersist, evalRef, evalRenderUI, evalRound, evalSet, evalSetDynamic, evalSpawn, evalSubtract, evalSum, evalSwap, evalWatch, evalWhen, evaluate, evaluateGuard, evaluator, executeEffect, executeEffects, resolveBinding };
3044
3278
  //# sourceMappingURL=index.js.map
3045
3279
  //# sourceMappingURL=index.js.map