@almadar/runtime 6.29.0 → 6.30.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,5 +1,5 @@
1
- import { createTickScheduler, EventBus, createUnifiedLoader, MockPersistenceAdapter, InMemoryPersistence, preprocessSchema, normalizeCallSiteConfigToValues, StateMachineManager, parseDurationString, createContextFromBindings, validateEventPayload, formatPayloadValidationError, collectDeclaredConfigDefaults, collectDeclaredEntityDefaults, EffectExecutor } from './chunk-6YDQGA6X.js';
2
- export { InMemoryPersistence, collectDeclaredConfigDefaults } from './chunk-6YDQGA6X.js';
1
+ import { createTickScheduler, EventBus, createUnifiedLoader, MockPersistenceAdapter, InMemoryPersistence, preprocessSchema, normalizeCallSiteConfigToValues, StateMachineManager, parseDurationString, createContextFromBindings, validateEventPayload, formatPayloadValidationError, collectDeclaredConfigDefaults, collectDeclaredEntityDefaults, EffectExecutor } from './chunk-TGL6XC6E.js';
2
+ export { InMemoryPersistence, collectDeclaredConfigDefaults } from './chunk-TGL6XC6E.js';
3
3
  import { isValidCronExpression } from './chunk-U4PL237A.js';
4
4
  import './chunk-PZ5AY32C.js';
5
5
  import { createLogger } from '@almadar/logger';
@@ -138,7 +138,7 @@ var OrbitalServerRuntime = class {
138
138
  if (!isNodeEnv()) return;
139
139
  if (!this.substrateHandlersPromise) {
140
140
  this.substrateHandlersPromise = (async () => {
141
- const { createAgentSubstrateHandlers } = await import('./createAgentSubstrateHandlers-WZCHKGU6.js');
141
+ const { createAgentSubstrateHandlers } = await import('./createAgentSubstrateHandlers-RUX4RFC6.js');
142
142
  this.substrateHandlers = createAgentSubstrateHandlers({
143
143
  emitEvent: (type, payload) => this.eventBus.emit(type, payload),
144
144
  services: this.config.substrateServices ?? {}
@@ -1008,6 +1008,7 @@ var HANDLER_MANIFEST = {
1008
1008
  };
1009
1009
  var effectLog = createLogger("almadar:runtime:effects");
1010
1010
  setNamespaceLevel("almadar:runtime:effects", "WARN");
1011
+ var persistLog = createLogger("almadar:runtime:persist");
1011
1012
  function parseEffect(effect) {
1012
1013
  if (!Array.isArray(effect) || effect.length === 0) {
1013
1014
  return null;
@@ -1312,6 +1313,35 @@ var EffectExecutor = class _EffectExecutor {
1312
1313
  failure: emit.failure
1313
1314
  };
1314
1315
  }
1316
+ /**
1317
+ * Run a substrate (async) effect and dispatch the author's `emit.success`
1318
+ * / `emit.failure` bus event when configured. Mirrors the compiled-path
1319
+ * generic substrate dispatch (server.rs): uniform `{ result }` payload on
1320
+ * success, `{ error }` on failure.
1321
+ *
1322
+ * When no `emit` config is present (fire-and-forget call) the op still runs
1323
+ * but no event is dispatched and errors propagate (legacy behavior).
1324
+ */
1325
+ async runSubstrate(invoke, emitCfg) {
1326
+ if (!emitCfg) {
1327
+ await invoke();
1328
+ return;
1329
+ }
1330
+ try {
1331
+ const result = await invoke();
1332
+ this.emitSuccess(emitCfg, "success", { result: result ?? null });
1333
+ } catch (err) {
1334
+ this.emitFailure(emitCfg, err);
1335
+ }
1336
+ }
1337
+ /**
1338
+ * Separate a trailing `{ emit: {...} }` options object from positional
1339
+ * substrate-op args. Returns `[positionalArgs, emitConfig]`.
1340
+ */
1341
+ splitSubstrateEmit(args) {
1342
+ const emitCfg = args.length > 0 ? this.extractEmitConfig(args[args.length - 1]) : void 0;
1343
+ return emitCfg ? [args.slice(0, -1), emitCfg] : [args, void 0];
1344
+ }
1315
1345
  // ==========================================================================
1316
1346
  // Effect Dispatch
1317
1347
  // ==========================================================================
@@ -1375,14 +1405,14 @@ var EffectExecutor = class _EffectExecutor {
1375
1405
  const action = args[0];
1376
1406
  const last = args[args.length - 1];
1377
1407
  const emitCfg = last && typeof last === "object" && !Array.isArray(last) && "emit" in last ? this.extractEmitConfig(last) : void 0;
1378
- effectLog.debug("persist:dispatch", {
1408
+ persistLog.debug("persist:dispatch", {
1379
1409
  action,
1380
1410
  argCount: args.length,
1381
1411
  argTypes: args.map((a) => Array.isArray(a) ? "array" : a === null ? "null" : typeof a).join(","),
1382
1412
  traitName: this.context.traitName,
1383
1413
  transition: this.context.transition
1384
1414
  });
1385
- effectLog.debug("persist:emit-config", {
1415
+ persistLog.debug("persist:emit-config", {
1386
1416
  action,
1387
1417
  hasEmitCfg: emitCfg !== void 0,
1388
1418
  success: emitCfg?.success,
@@ -1392,30 +1422,30 @@ var EffectExecutor = class _EffectExecutor {
1392
1422
  if (action === "batch") {
1393
1423
  const operations = args[1];
1394
1424
  await this.handlers.persist("batch", "", { operations });
1395
- effectLog.debug("persist:success", {
1425
+ persistLog.debug("persist:success", {
1396
1426
  action,
1397
1427
  entityType: "batch",
1398
1428
  opCount: operations.length,
1399
1429
  willEmit: emitCfg?.success
1400
1430
  });
1401
1431
  this.emitSuccess(emitCfg, "success", operations);
1402
- effectLog.debug("persist:emit-fired", { action, eventName: emitCfg?.success });
1432
+ persistLog.debug("persist:emit-fired", { action, eventName: emitCfg?.success });
1403
1433
  } else {
1404
1434
  const entityType = args[1];
1405
1435
  const data = args[2];
1406
1436
  await this.handlers.persist(action, entityType, data);
1407
1437
  const dataId = typeof data === "string" ? data : data && typeof data === "object" ? data.id : void 0;
1408
- effectLog.debug("persist:success", {
1438
+ persistLog.debug("persist:success", {
1409
1439
  action,
1410
1440
  entityType,
1411
1441
  dataId,
1412
1442
  willEmit: emitCfg?.success
1413
1443
  });
1414
1444
  this.emitSuccess(emitCfg, "success", data);
1415
- effectLog.debug("persist:emit-fired", { action, eventName: emitCfg?.success });
1445
+ persistLog.debug("persist:emit-fired", { action, eventName: emitCfg?.success });
1416
1446
  }
1417
1447
  } catch (err) {
1418
- effectLog.error("persist:error", {
1448
+ persistLog.error("persist:error", {
1419
1449
  action,
1420
1450
  entityType: action === "batch" ? "batch" : args[1],
1421
1451
  error: err instanceof Error ? err.message : String(err)
@@ -1823,80 +1853,106 @@ var EffectExecutor = class _EffectExecutor {
1823
1853
  break;
1824
1854
  }
1825
1855
  // === Agent substrate operators (server-side, effect-position) ===
1856
+ //
1857
+ // Each honours a trailing `{ emit: { success, failure } }` options
1858
+ // object (mirrors fetch/persist/call-service): on success the
1859
+ // author's `emit.success` event fires with a uniform `{ result }`
1860
+ // payload so `?result` captures the return value; on failure the
1861
+ // `emit.failure` event fires with `{ error }`. Without an emit
1862
+ // config the call is fire-and-forget (legacy).
1826
1863
  case "compose/compose-all": {
1827
- if (this.handlers.substrateComposeAll) {
1828
- const config = args[0];
1829
- await this.handlers.substrateComposeAll(config);
1830
- } else {
1831
- this.logUnsupported("compose/compose-all");
1832
- }
1864
+ const [positional, emitCfg] = this.splitSubstrateEmit(args);
1865
+ const config = positional[0];
1866
+ await this.runSubstrate(async () => {
1867
+ if (!this.handlers.substrateComposeAll) {
1868
+ this.logUnsupported("compose/compose-all");
1869
+ return null;
1870
+ }
1871
+ return this.handlers.substrateComposeAll(config);
1872
+ }, emitCfg);
1833
1873
  break;
1834
1874
  }
1835
1875
  case "compose/compose-children": {
1836
- if (this.handlers.substrateComposeChildren) {
1837
- const parentName = args[0];
1838
- const children = args[1];
1839
- await this.handlers.substrateComposeChildren(parentName, children);
1840
- } else {
1841
- this.logUnsupported("compose/compose-children");
1842
- }
1876
+ const [positional, emitCfg] = this.splitSubstrateEmit(args);
1877
+ const parentName = positional[0];
1878
+ const children = positional[1];
1879
+ await this.runSubstrate(async () => {
1880
+ if (!this.handlers.substrateComposeChildren) {
1881
+ this.logUnsupported("compose/compose-children");
1882
+ return null;
1883
+ }
1884
+ return this.handlers.substrateComposeChildren(parentName, children);
1885
+ }, emitCfg);
1843
1886
  break;
1844
1887
  }
1845
1888
  case "behavior/instantiate": {
1846
- if (this.handlers.substrateInstantiate) {
1847
- const parentName = args[0];
1848
- const behavior = args[1];
1849
- const params = args.length > 2 ? args[2] : void 0;
1850
- await this.handlers.substrateInstantiate(parentName, behavior, params);
1851
- } else {
1852
- this.logUnsupported("behavior/instantiate");
1853
- }
1889
+ const [positional, emitCfg] = this.splitSubstrateEmit(args);
1890
+ const parentName = positional[0];
1891
+ const behavior = positional[1];
1892
+ const params = positional.length > 2 ? positional[2] : void 0;
1893
+ await this.runSubstrate(async () => {
1894
+ if (!this.handlers.substrateInstantiate) {
1895
+ this.logUnsupported("behavior/instantiate");
1896
+ return null;
1897
+ }
1898
+ return this.handlers.substrateInstantiate(parentName, behavior, params);
1899
+ }, emitCfg);
1854
1900
  break;
1855
1901
  }
1856
1902
  case "behavior/call": {
1857
- if (this.handlers.substrateCall) {
1858
- const behavior = args[0];
1859
- const method = args[1];
1860
- const params = args.length > 2 ? args[2] : void 0;
1861
- await this.handlers.substrateCall(behavior, method, params);
1862
- } else {
1863
- this.logUnsupported("behavior/call");
1864
- }
1903
+ const [positional, emitCfg] = this.splitSubstrateEmit(args);
1904
+ const behavior = positional[0];
1905
+ const method = positional[1];
1906
+ const params = positional.length > 2 ? positional[2] : void 0;
1907
+ await this.runSubstrate(async () => {
1908
+ if (!this.handlers.substrateCall) {
1909
+ this.logUnsupported("behavior/call");
1910
+ return null;
1911
+ }
1912
+ return this.handlers.substrateCall(behavior, method, params);
1913
+ }, emitCfg);
1865
1914
  break;
1866
1915
  }
1867
1916
  case "validate/validate": {
1868
- if (this.handlers.substrateValidate) {
1869
- const orbitalName = args[0];
1870
- await this.handlers.substrateValidate(orbitalName);
1871
- } else {
1872
- this.logUnsupported("validate/validate");
1873
- }
1917
+ const [positional, emitCfg] = this.splitSubstrateEmit(args);
1918
+ const orbitalName = positional[0];
1919
+ await this.runSubstrate(async () => {
1920
+ if (!this.handlers.substrateValidate) {
1921
+ this.logUnsupported("validate/validate");
1922
+ return null;
1923
+ }
1924
+ return this.handlers.substrateValidate(orbitalName);
1925
+ }, emitCfg);
1874
1926
  break;
1875
1927
  }
1876
1928
  case "lolo/emit-body": {
1877
- if (this.handlers.substrateEmitBody) {
1878
- const orbitalName = args[0];
1879
- const loloSource = args[1];
1880
- await this.handlers.substrateEmitBody(orbitalName, loloSource);
1881
- } else {
1882
- this.logUnsupported("lolo/emit-body");
1883
- }
1929
+ const [positional, emitCfg] = this.splitSubstrateEmit(args);
1930
+ const orbitalName = positional[0];
1931
+ const loloSource = positional[1];
1932
+ await this.runSubstrate(async () => {
1933
+ if (!this.handlers.substrateEmitBody) {
1934
+ this.logUnsupported("lolo/emit-body");
1935
+ return null;
1936
+ }
1937
+ return this.handlers.substrateEmitBody(orbitalName, loloSource);
1938
+ }, emitCfg);
1884
1939
  break;
1885
1940
  }
1886
1941
  default: {
1887
1942
  if (operator.includes("/")) {
1888
- const ctx = createContextFromBindings(
1889
- this.bindings,
1890
- this.strictBindings,
1891
- this.contextExtensions
1892
- );
1893
- const result = this.getEvaluator().evaluate(
1894
- [operator, ...args],
1895
- ctx
1896
- );
1897
- if (result instanceof Promise) {
1898
- await result;
1899
- }
1943
+ const [positional, emitCfg] = this.splitSubstrateEmit(args);
1944
+ await this.runSubstrate(async () => {
1945
+ const ctx = createContextFromBindings(
1946
+ this.bindings,
1947
+ this.strictBindings,
1948
+ this.contextExtensions
1949
+ );
1950
+ const result = this.getEvaluator().evaluate(
1951
+ [operator, ...positional],
1952
+ ctx
1953
+ );
1954
+ return result instanceof Promise ? await result : result;
1955
+ }, emitCfg);
1900
1956
  } else if (this.debug) {
1901
1957
  effectLog.warn("unknown-operator", { operator });
1902
1958
  }
@@ -3997,5 +4053,5 @@ var InMemoryPersistence = class {
3997
4053
  };
3998
4054
 
3999
4055
  export { EffectExecutor, EventBus, HANDLER_MANIFEST, InMemoryPersistence, MockPersistenceAdapter, StateMachineManager, TickScheduler, buildEmitsFromTraits, collectDeclaredConfigDefaults, collectDeclaredEntityDefaults, containsBindings, createContextFromBindings, createInitialTraitState, createMockPersistence, createTestExecutor, createTickScheduler, createUnifiedLoader, extractBindings, findInitialState, findTransition, formatPayloadValidationError, getIsolatedCollectionName, getNamespacedEvent, interpolateProps, interpolateValue, isBrowser, isElectron, isNamespacedEvent, isNode, isValidDurationString, normalizeCallSiteConfigToValues, normalizeEventKey, parseDurationString, parseNamespacedEvent, preprocessSchema, processEvent, validateEventPayload, validatePayloadShapes };
4000
- //# sourceMappingURL=chunk-6YDQGA6X.js.map
4001
- //# sourceMappingURL=chunk-6YDQGA6X.js.map
4056
+ //# sourceMappingURL=chunk-TGL6XC6E.js.map
4057
+ //# sourceMappingURL=chunk-TGL6XC6E.js.map