depa-actor 0.2.0 → 0.2.2

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.
@@ -2,38 +2,123 @@
2
2
  /**
3
3
  * depa-actor — Dispatch Bridge
4
4
  *
5
- * Optional bridge to depa-processor DispatchEngine.
6
- * Only this file imports from depa-processor concepts.
7
- * No hard dependencyuses structural typing.
5
+ * Drives the depa-processor `DispatchEngine` (all 7 strategies) from the actor
6
+ * side. The envelope DispatchRequest adaptation and the actor `tag` overlay
7
+ * live HERE, in depa-actor the generic `DispatchEngine` / `DispatchStrategyConfig`
8
+ * stay free of any actor (`envelope` / `tag` / `ActorSelf`) knowledge.
9
+ *
10
+ * Two orthogonal axes:
11
+ * 1. `tag` — actor's first-level route (which mailbox). Selecting a handler by
12
+ * tag is the actor system's job (`ActorDef.handlers[tag]`); a dispatch handler
13
+ * built here is slotted UNDER a tag, so tag selection happens upstream.
14
+ * `tag` is NOT a DispatchStrategyType — it is an orthogonal overlay dimension.
15
+ * 2. `strategy` — one of the 7 `DispatchStrategyType` values, used as the
16
+ * second-level resolution within the selected tag.
8
17
  */
9
18
  Object.defineProperty(exports, "__esModule", { value: true });
10
19
  exports.createDispatchHandler = createDispatchHandler;
11
- // ─── createDispatchHandler ───────────────────────────────────────────
20
+ const depa_processor_1 = require("depa-processor");
21
+ // ─── Internal helpers ────────────────────────────────────────────────
22
+ function isStrategyConfig(routes) {
23
+ return routes instanceof depa_processor_1.DispatchStrategyConfig;
24
+ }
12
25
  /**
13
- * Creates an ActorHandler that routes envelopes through dispatch routes.
14
- * Falls back to `defaultHandler` for tags not covered by any route.
26
+ * Build a `DispatchStrategyConfig<void>` from a plain key handler map for the
27
+ * supported key-based strategies. The map values are pre-bound to `(self, envelope)`
28
+ * by capturing the current dispatch's `self` / `envelope` via a thunk.
15
29
  */
16
- function createDispatchHandler(routes, defaultHandler) {
17
- // Build tag route index for O(1) lookup
18
- const tagIndex = new Map();
19
- for (const route of routes) {
20
- for (const tag of route.tags) {
21
- tagIndex.set(tag, route);
22
- }
30
+ function buildKeyBasedConfig(strategy, routes, self, envelope) {
31
+ const handlerMap = new Map();
32
+ for (const [key, handler] of Object.entries(routes)) {
33
+ handlerMap.set(key, () => handler(self, envelope));
34
+ }
35
+ switch (strategy) {
36
+ case depa_processor_1.DispatchStrategyType.ROUTE_KEY:
37
+ return depa_processor_1.DispatchStrategyConfig.forRouteKeyStrategy({
38
+ handlerMap: handlerMap,
39
+ });
40
+ case depa_processor_1.DispatchStrategyType.ENUM:
41
+ return depa_processor_1.DispatchStrategyConfig.forEnumStrategy({
42
+ handlerMap,
43
+ });
44
+ case depa_processor_1.DispatchStrategyType.COMMAND_TABLE:
45
+ return depa_processor_1.DispatchStrategyConfig.forCommandStrategy({
46
+ commandConverter: (command) => handlerMap.has(command) ? command : null,
47
+ handlerExtractor: (commandEnum) => handlerMap.get(commandEnum) ?? null,
48
+ });
49
+ default:
50
+ throw new Error(`createDispatchHandler: strategy ${strategy} requires a prebuilt ` +
51
+ `DispatchStrategyConfig in 'routes' (plain key→handler maps support ` +
52
+ `ROUTE_KEY / ENUM / COMMAND_TABLE only).`);
53
+ }
54
+ }
55
+ /** Build the strategy-appropriate DispatchRequest from the envelope. */
56
+ function buildRequest(strategy, envelope, extractors) {
57
+ const input = extractors?.inputOf ? extractors.inputOf(envelope) : envelope.payload;
58
+ const routeKey = extractors?.routeKeyOf
59
+ ? extractors.routeKeyOf(envelope)
60
+ : envelope.tag;
61
+ const path = extractors?.pathOf ? extractors.pathOf(envelope) : envelope.tag;
62
+ switch (strategy) {
63
+ case depa_processor_1.DispatchStrategyType.CLASS:
64
+ return (0, depa_processor_1.createClassDispatchRequest)(input);
65
+ case depa_processor_1.DispatchStrategyType.ROUTE_KEY:
66
+ return (0, depa_processor_1.createRouteKeyDispatchRequest)(routeKey, input, false);
67
+ case depa_processor_1.DispatchStrategyType.ENUM:
68
+ return (0, depa_processor_1.createEnumDispatchRequest)(extractors?.enumOf ? extractors.enumOf(envelope) : envelope.tag, input);
69
+ case depa_processor_1.DispatchStrategyType.ROUTE_KEY_TO_ENUM:
70
+ return (0, depa_processor_1.createRouteKeyToEnumDispatchRequest)(routeKey, input);
71
+ case depa_processor_1.DispatchStrategyType.COMMAND_TABLE:
72
+ return (0, depa_processor_1.createCommandDispatchRequest)(routeKey, input);
73
+ case depa_processor_1.DispatchStrategyType.PATH:
74
+ return (0, depa_processor_1.createPathDispatchRequest)({
75
+ runtime: undefined,
76
+ request: input,
77
+ path,
78
+ });
79
+ case depa_processor_1.DispatchStrategyType.ACTION_PATH:
80
+ return (0, depa_processor_1.createActionPathDispatchRequest)({
81
+ runtime: undefined,
82
+ request: input,
83
+ action: extractors?.actionOf ? extractors.actionOf(envelope) : undefined,
84
+ path,
85
+ });
86
+ default:
87
+ throw new Error(`createDispatchHandler: unknown strategy ${String(strategy)}`);
23
88
  }
89
+ }
90
+ // ─── createDispatchHandler ───────────────────────────────────────────
91
+ /**
92
+ * Creates an ActorHandler that resolves an envelope to a sub-handler via one of
93
+ * the 7 depa-processor dispatch strategies (opt-in, per-handler — DX form C / B2).
94
+ *
95
+ * Object-param signature: `{ strategy, routes, defaultHandler?, extractors? }`.
96
+ *
97
+ * Composability:
98
+ * - slot the returned handler under a `tag` in `ActorDef.handlers` → `tag` overlay
99
+ * (first-level) + `strategy` (second-level), the two axes are orthogonal.
100
+ * - the resolved sub-handler may itself be a `createPipelineHandler(...)` result,
101
+ * so dispatch and pipeline nest freely.
102
+ *
103
+ * No declarative `ActorDef.dispatch` field and no global feature flag / enableXxx
104
+ * API exist — enabling rich dispatch is expressed purely by import + composition.
105
+ */
106
+ function createDispatchHandler(params) {
107
+ const { strategy, routes, defaultHandler, extractors } = params;
24
108
  return async (self, envelope) => {
25
- const route = tagIndex.get(envelope.tag);
26
- if (route) {
27
- const key = route.resolveKey(envelope);
28
- const handler = route.routes[key];
29
- if (handler) {
30
- await handler(self, envelope);
31
- }
32
- else if (route.fallback) {
33
- await route.fallback(self, envelope, key);
34
- }
109
+ const config = isStrategyConfig(routes)
110
+ ? routes
111
+ : buildKeyBasedConfig(strategy, routes, self, envelope);
112
+ const engine = new depa_processor_1.DispatchEngine();
113
+ engine.registerStrategy(config);
114
+ const request = buildRequest(strategy, envelope, extractors);
115
+ const result = await engine.dispatch(request);
116
+ if (result.isHandled()) {
117
+ // Await the sub-handler's (possibly async) side effect.
118
+ await result.getResult();
119
+ return;
35
120
  }
36
- else if (defaultHandler) {
121
+ if (defaultHandler) {
37
122
  await defaultHandler(self, envelope);
38
123
  }
39
124
  };
@@ -1,8 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dispatchEffects = exports.createAiAgentSchedulerHooks = exports.scheduleOne = exports.selectNextFiberId = exports.computeEffectivePriority = exports.applyFailure = exports.reduceOrchestrator = exports.createOrchestratorState = exports.DEFAULT_ORCHESTRATOR_OPTIONS = exports.createDispatchHandler = exports.createPipelineHandler = exports.dispatchInstructions = exports.takeNextCommandFromGroup = exports.pushFrontCommandToGroup = exports.pushFrontCommand = exports.pushBackCommandToGroup = exports.pushBackCommand = exports.popSelectedCommandFromGroup = exports.popNextCommand = exports.popFrontCommand = exports.popBackCommand = exports.drainWhereCommandDequeFromGroup = exports.drainCommandDequeFromGroup = exports.defaultCommandDequeSelector = exports.createCommandDequeGroup = exports.createCommandDeque = exports.createOperandStack = exports.createInstructionStack = exports.createStackMachine = exports.createRuntimeIndexHook = exports.RuntimeIndexHook = exports.createPersistenceEffectPort = exports.createRecoveryHooks = exports.createSnapshotCodec = exports.createCompletionBindingRegistry = exports.createCompletionSignalRegistry = exports.CompletionBindingRegistry = exports.CompletionSignalRegistry = exports.ActorRuntime = exports.ActorSystem = void 0;
3
+ exports.DEFAULT_ORCHESTRATOR_OPTIONS = exports.DispatchStrategyConfig = exports.DispatchStrategyType = exports.createDispatchHandler = exports.createPipelineHandler = exports.dispatchInstructions = exports.takeNextCommandFromGroup = exports.pushFrontCommandToGroup = exports.pushFrontCommand = exports.pushBackCommandToGroup = exports.pushBackCommand = exports.popSelectedCommandFromGroup = exports.popNextCommand = exports.popFrontCommand = exports.popBackCommand = exports.drainWhereCommandDequeFromGroup = exports.drainCommandDequeFromGroup = exports.defaultCommandDequeSelector = exports.createCommandDequeGroup = exports.createCommandDeque = exports.createOperandStack = exports.createInstructionStack = exports.createStackMachine = exports.createRuntimeIndexHook = exports.RuntimeIndexHook = exports.createPersistenceEffectPort = exports.createRecoveryHooks = exports.createSnapshotCodec = exports.createCompletionBindingRegistry = exports.createCompletionSignalRegistry = exports.CompletionBindingRegistry = exports.CompletionSignalRegistry = exports.ActorRuntime = exports.createActorRefEndpoint = exports.rebuildActorRegistrations = exports.unregisterActor = exports.dispatchActor = exports.resolveActor = exports.registerActor = exports.createLocalActorAddressingRuntime = exports.parseActorRegistrationReceipt = exports.parseActorOwnerSnapshot = exports.parseActorSelector = exports.parseActorAddress = exports.actorAddressKey = exports.ActorAddressingError = exports.ACTOR_REGISTRATION_SCHEMA_VERSION = exports.ACTOR_OWNER_SNAPSHOT_SCHEMA_VERSION = exports.ACTOR_ADDRESS_SCHEMA_VERSION = exports.ActorSystem = void 0;
4
+ exports.dispatchEffects = exports.createAiAgentSchedulerHooks = exports.scheduleOne = exports.selectNextFiberId = exports.computeEffectivePriority = exports.applyFailure = exports.reduceOrchestrator = exports.createOrchestratorState = void 0;
4
5
  var ActorSystem_js_1 = require("./core/ActorSystem.cjs");
5
6
  Object.defineProperty(exports, "ActorSystem", { enumerable: true, get: function () { return ActorSystem_js_1.ActorSystem; } });
7
+ var addressing_js_1 = require("./addressing.cjs");
8
+ Object.defineProperty(exports, "ACTOR_ADDRESS_SCHEMA_VERSION", { enumerable: true, get: function () { return addressing_js_1.ACTOR_ADDRESS_SCHEMA_VERSION; } });
9
+ Object.defineProperty(exports, "ACTOR_OWNER_SNAPSHOT_SCHEMA_VERSION", { enumerable: true, get: function () { return addressing_js_1.ACTOR_OWNER_SNAPSHOT_SCHEMA_VERSION; } });
10
+ Object.defineProperty(exports, "ACTOR_REGISTRATION_SCHEMA_VERSION", { enumerable: true, get: function () { return addressing_js_1.ACTOR_REGISTRATION_SCHEMA_VERSION; } });
11
+ Object.defineProperty(exports, "ActorAddressingError", { enumerable: true, get: function () { return addressing_js_1.ActorAddressingError; } });
12
+ Object.defineProperty(exports, "actorAddressKey", { enumerable: true, get: function () { return addressing_js_1.actorAddressKey; } });
13
+ Object.defineProperty(exports, "parseActorAddress", { enumerable: true, get: function () { return addressing_js_1.parseActorAddress; } });
14
+ Object.defineProperty(exports, "parseActorSelector", { enumerable: true, get: function () { return addressing_js_1.parseActorSelector; } });
15
+ Object.defineProperty(exports, "parseActorOwnerSnapshot", { enumerable: true, get: function () { return addressing_js_1.parseActorOwnerSnapshot; } });
16
+ Object.defineProperty(exports, "parseActorRegistrationReceipt", { enumerable: true, get: function () { return addressing_js_1.parseActorRegistrationReceipt; } });
17
+ Object.defineProperty(exports, "createLocalActorAddressingRuntime", { enumerable: true, get: function () { return addressing_js_1.createLocalActorAddressingRuntime; } });
18
+ Object.defineProperty(exports, "registerActor", { enumerable: true, get: function () { return addressing_js_1.registerActor; } });
19
+ Object.defineProperty(exports, "resolveActor", { enumerable: true, get: function () { return addressing_js_1.resolveActor; } });
20
+ Object.defineProperty(exports, "dispatchActor", { enumerable: true, get: function () { return addressing_js_1.dispatchActor; } });
21
+ Object.defineProperty(exports, "unregisterActor", { enumerable: true, get: function () { return addressing_js_1.unregisterActor; } });
22
+ Object.defineProperty(exports, "rebuildActorRegistrations", { enumerable: true, get: function () { return addressing_js_1.rebuildActorRegistrations; } });
23
+ Object.defineProperty(exports, "createActorRefEndpoint", { enumerable: true, get: function () { return addressing_js_1.createActorRefEndpoint; } });
6
24
  var ActorRuntime_js_1 = require("./runtime/ActorRuntime.cjs");
7
25
  Object.defineProperty(exports, "ActorRuntime", { enumerable: true, get: function () { return ActorRuntime_js_1.ActorRuntime; } });
8
26
  var completion_js_1 = require("./runtime/completion.cjs");
@@ -40,6 +58,13 @@ var ActorPipeline_js_1 = require("./pipeline/ActorPipeline.cjs");
40
58
  Object.defineProperty(exports, "createPipelineHandler", { enumerable: true, get: function () { return ActorPipeline_js_1.createPipelineHandler; } });
41
59
  var ActorDispatchAdapter_js_1 = require("./dispatch/ActorDispatchAdapter.cjs");
42
60
  Object.defineProperty(exports, "createDispatchHandler", { enumerable: true, get: function () { return ActorDispatchAdapter_js_1.createDispatchHandler; } });
61
+ // Dispatch strategy enum + the common-tier strategy config factories
62
+ // (ROUTE_KEY / ENUM / COMMAND_TABLE). Advanced strategies (PATH / ACTION_PATH +
63
+ // AntPathMatcher / PathActionMatchRule), manifest-* and router/ stay in
64
+ // depa-processor and are imported directly by users who need them.
65
+ var depa_processor_1 = require("depa-processor");
66
+ Object.defineProperty(exports, "DispatchStrategyType", { enumerable: true, get: function () { return depa_processor_1.DispatchStrategyType; } });
67
+ Object.defineProperty(exports, "DispatchStrategyConfig", { enumerable: true, get: function () { return depa_processor_1.DispatchStrategyConfig; } });
43
68
  var index_js_2 = require("./orchestration/index.cjs");
44
69
  Object.defineProperty(exports, "DEFAULT_ORCHESTRATOR_OPTIONS", { enumerable: true, get: function () { return index_js_2.DEFAULT_ORCHESTRATOR_OPTIONS; } });
45
70
  Object.defineProperty(exports, "createOrchestratorState", { enumerable: true, get: function () { return index_js_2.createOrchestratorState; } });
@@ -2,33 +2,43 @@
2
2
  /**
3
3
  * depa-actor — ActorPipeline
4
4
  *
5
- * Adapts the DOP 6-step pipeline (from depa-processor) to actor context.
5
+ * Adapts the DOP 6-step pipeline to actor context by REUSING the depa-processor
6
+ * `component` primitives (DEPA dimension chain Processor → Actor): the 6-step
7
+ * orchestration is delegated to `runByFuncStyleAdapter`, identity/null seams come
8
+ * from `stdMake*`, and the core-logic seam is `StdInnerLogic` verbatim — this module
9
+ * no longer re-implements a parallel copy of `component/types.ts`.
6
10
  *
7
- * Mapping:
11
+ * Mapping (outer dimension):
8
12
  * OuterRuntime = ActorSelf
9
13
  * OuterInput = envelope payload
10
14
  * OuterConfig = actor state
11
15
  *
16
+ * Step 6 (output) is adapted with `TOuterOutput = void`: actor output is a side
17
+ * effect (write `self.state` / `self.send`), not a returned value. This thinly
18
+ * adapts depa-processor's pure-function `StdOuterOutputAdapter` without leaking
19
+ * actor semantics back into the generic primitive.
20
+ *
12
21
  * createPipelineHandler() wraps a pipeline definition into an ActorHandler.
13
22
  */
14
23
  Object.defineProperty(exports, "__esModule", { value: true });
15
24
  exports.createPipelineHandler = createPipelineHandler;
25
+ const depa_processor_1 = require("depa-processor");
16
26
  // ─── createPipelineHandler ───────────────────────────────────────────
27
+ /**
28
+ * Wrap a pipeline definition into an ActorHandler by delegating the 6-step
29
+ * orchestration to depa-processor `runByFuncStyleAdapter`.
30
+ *
31
+ * Outer dimension: runtime = self, input = payload, config = state.
32
+ * Step 6 produces `void` — `runByFuncStyleAdapter` runs the output adapter for its
33
+ * side effects and the (void) return value is discarded.
34
+ */
17
35
  function createPipelineHandler(pipeline) {
18
36
  return async (self, envelope) => {
19
- const payload = envelope.payload;
20
- const state = self.state;
21
- // Step 1: Compute derived
22
- const derived = pipeline.computeDerived(self, payload, state);
23
- // Step 2: Transform runtime
24
- const innerRuntime = pipeline.innerRuntime(self, payload, state, derived);
25
- // Step 3: Transform input
26
- const innerInput = pipeline.innerInput(self, payload, state, derived);
27
- // Step 4: Transform config
28
- const innerConfig = pipeline.innerConfig(self, payload, state, derived);
29
- // Step 5: Core logic
30
- const innerOutput = await pipeline.coreLogic(innerRuntime, innerInput, innerConfig);
31
- // Step 6: Output (side effects on actor state / send messages)
32
- await pipeline.output(self, payload, state, derived, innerOutput);
37
+ // `runByFuncStyleAdapter` returns the output adapter's value verbatim (without
38
+ // awaiting it). Capture it and await so an async step-6 side effect (write
39
+ // state / send messages) is fully settled before the handler resolves —
40
+ // preserving the original `await pipeline.output(...)` semantics.
41
+ const outputResult = await (0, depa_processor_1.runByFuncStyleAdapter)(self, envelope.payload, self.state, pipeline.computeDerived, pipeline.innerRuntime, pipeline.innerInput, pipeline.innerConfig, pipeline.coreLogic, pipeline.output);
42
+ await outputResult;
33
43
  };
34
44
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "depa-actor",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "main": "./dist-cjs/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -17,6 +17,9 @@
17
17
  "dist-cjs",
18
18
  "src"
19
19
  ],
20
+ "dependencies": {
21
+ "depa-processor": "0.1.1"
22
+ },
20
23
  "scripts": {
21
24
  "build": "tsc && tsc -p tsconfig.cjs.json && node scripts/build-cjs.mjs",
22
25
  "dev": "tsc --watch",