depa-actor 0.1.2 → 0.2.1

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.
Files changed (96) hide show
  1. package/dist/addressing.d.ts +100 -0
  2. package/dist/addressing.d.ts.map +1 -0
  3. package/dist/addressing.js +409 -0
  4. package/dist/addressing.js.map +1 -0
  5. package/dist/core/ActorSystem.d.ts +1 -1
  6. package/dist/core/ActorSystem.d.ts.map +1 -1
  7. package/dist/dispatch/ActorDispatchAdapter.d.ts +75 -18
  8. package/dist/dispatch/ActorDispatchAdapter.d.ts.map +1 -1
  9. package/dist/dispatch/ActorDispatchAdapter.js +109 -24
  10. package/dist/dispatch/ActorDispatchAdapter.js.map +1 -1
  11. package/dist/execution/commandDeque.d.ts +121 -0
  12. package/dist/execution/commandDeque.d.ts.map +1 -0
  13. package/dist/execution/commandDeque.js +415 -0
  14. package/dist/execution/commandDeque.js.map +1 -0
  15. package/dist/execution/dispatcher.d.ts +72 -0
  16. package/dist/execution/dispatcher.d.ts.map +1 -0
  17. package/dist/execution/dispatcher.js +77 -0
  18. package/dist/execution/dispatcher.js.map +1 -0
  19. package/dist/execution/index.d.ts +7 -0
  20. package/dist/execution/index.d.ts.map +1 -0
  21. package/dist/execution/index.js +4 -0
  22. package/dist/execution/index.js.map +1 -0
  23. package/dist/execution/stack.d.ts +24 -0
  24. package/dist/execution/stack.d.ts.map +1 -0
  25. package/dist/execution/stack.js +84 -0
  26. package/dist/execution/stack.js.map +1 -0
  27. package/dist/index.d.ts +21 -15
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +15 -8
  30. package/dist/index.js.map +1 -1
  31. package/dist/orchestration/index.d.ts +8 -8
  32. package/dist/orchestration/index.d.ts.map +1 -1
  33. package/dist/orchestration/index.js +6 -6
  34. package/dist/orchestration/index.js.map +1 -1
  35. package/dist/orchestration/presets/aiAgent.d.ts +2 -2
  36. package/dist/orchestration/presets/aiAgent.d.ts.map +1 -1
  37. package/dist/orchestration/recovery.d.ts +2 -2
  38. package/dist/orchestration/recovery.d.ts.map +1 -1
  39. package/dist/orchestration/reducer.d.ts +2 -2
  40. package/dist/orchestration/reducer.d.ts.map +1 -1
  41. package/dist/orchestration/reducer.js +2 -2
  42. package/dist/orchestration/reducer.js.map +1 -1
  43. package/dist/orchestration/runtimeAdapter.d.ts +3 -3
  44. package/dist/orchestration/runtimeAdapter.d.ts.map +1 -1
  45. package/dist/orchestration/scheduler.d.ts +2 -2
  46. package/dist/orchestration/scheduler.d.ts.map +1 -1
  47. package/dist/orchestration/types.d.ts +1 -1
  48. package/dist/orchestration/types.d.ts.map +1 -1
  49. package/dist/pipeline/ActorPipeline.d.ts +36 -9
  50. package/dist/pipeline/ActorPipeline.d.ts.map +1 -1
  51. package/dist/pipeline/ActorPipeline.js +26 -16
  52. package/dist/pipeline/ActorPipeline.js.map +1 -1
  53. package/dist/runtime/ActorRuntime.d.ts +2 -2
  54. package/dist/runtime/ActorRuntime.d.ts.map +1 -1
  55. package/dist/runtime/ActorRuntime.js +1 -1
  56. package/dist/runtime/ActorRuntime.js.map +1 -1
  57. package/dist-cjs/addressing.cjs +424 -0
  58. package/dist-cjs/core/ActorSystem.cjs +172 -0
  59. package/dist-cjs/core/types.cjs +10 -0
  60. package/dist-cjs/dispatch/ActorDispatchAdapter.cjs +125 -0
  61. package/dist-cjs/execution/commandDeque.cjs +430 -0
  62. package/dist-cjs/execution/dispatcher.cjs +79 -0
  63. package/dist-cjs/execution/index.cjs +24 -0
  64. package/dist-cjs/execution/stack.cjs +88 -0
  65. package/dist-cjs/index.cjs +77 -0
  66. package/dist-cjs/orchestration/index.cjs +19 -0
  67. package/dist-cjs/orchestration/presets/aiAgent.cjs +40 -0
  68. package/dist-cjs/orchestration/recovery.cjs +112 -0
  69. package/dist-cjs/orchestration/reducer.cjs +276 -0
  70. package/dist-cjs/orchestration/runtimeAdapter.cjs +14 -0
  71. package/dist-cjs/orchestration/scheduler.cjs +86 -0
  72. package/dist-cjs/orchestration/types.cjs +14 -0
  73. package/dist-cjs/package.json +3 -0
  74. package/dist-cjs/pipeline/ActorPipeline.cjs +44 -0
  75. package/dist-cjs/runtime/ActorRuntime.cjs +77 -0
  76. package/dist-cjs/runtime/completion.cjs +75 -0
  77. package/dist-cjs/runtime/indexing.cjs +34 -0
  78. package/dist-cjs/runtime/snapshot.cjs +14 -0
  79. package/package.json +9 -4
  80. package/src/addressing.ts +756 -0
  81. package/src/core/ActorSystem.ts +1 -1
  82. package/src/dispatch/ActorDispatchAdapter.ts +219 -48
  83. package/src/execution/commandDeque.ts +656 -0
  84. package/src/execution/dispatcher.ts +188 -0
  85. package/src/execution/index.ts +58 -0
  86. package/src/execution/stack.ts +130 -0
  87. package/src/index.ts +121 -16
  88. package/src/orchestration/index.ts +8 -8
  89. package/src/orchestration/presets/aiAgent.ts +2 -2
  90. package/src/orchestration/recovery.ts +2 -2
  91. package/src/orchestration/reducer.ts +3 -3
  92. package/src/orchestration/runtimeAdapter.ts +3 -3
  93. package/src/orchestration/scheduler.ts +2 -2
  94. package/src/orchestration/types.ts +1 -1
  95. package/src/pipeline/ActorPipeline.ts +104 -58
  96. package/src/runtime/ActorRuntime.ts +2 -2
@@ -14,7 +14,7 @@ import type {
14
14
  ActorSelf,
15
15
  TaggedEnvelope,
16
16
  TagHandler,
17
- } from './types';
17
+ } from './types.js';
18
18
 
19
19
  // ─── ActorCell (internal) ────────────────────────────────────────────
20
20
 
@@ -1,83 +1,254 @@
1
1
  /**
2
2
  * depa-actor — Dispatch Bridge
3
3
  *
4
- * Optional bridge to depa-processor DispatchEngine.
5
- * Only this file imports from depa-processor concepts.
6
- * No hard dependencyuses structural typing.
4
+ * Drives the depa-processor `DispatchEngine` (all 7 strategies) from the actor
5
+ * side. The envelope DispatchRequest adaptation and the actor `tag` overlay
6
+ * live HERE, in depa-actor the generic `DispatchEngine` / `DispatchStrategyConfig`
7
+ * stay free of any actor (`envelope` / `tag` / `ActorSelf`) knowledge.
8
+ *
9
+ * Two orthogonal axes:
10
+ * 1. `tag` — actor's first-level route (which mailbox). Selecting a handler by
11
+ * tag is the actor system's job (`ActorDef.handlers[tag]`); a dispatch handler
12
+ * built here is slotted UNDER a tag, so tag selection happens upstream.
13
+ * `tag` is NOT a DispatchStrategyType — it is an orthogonal overlay dimension.
14
+ * 2. `strategy` — one of the 7 `DispatchStrategyType` values, used as the
15
+ * second-level resolution within the selected tag.
7
16
  */
8
17
 
18
+ import {
19
+ DispatchEngine,
20
+ DispatchStrategyConfig,
21
+ DispatchStrategyType,
22
+ createClassDispatchRequest,
23
+ createRouteKeyDispatchRequest,
24
+ createEnumDispatchRequest,
25
+ createRouteKeyToEnumDispatchRequest,
26
+ createCommandDispatchRequest,
27
+ createPathDispatchRequest,
28
+ createActionPathDispatchRequest,
29
+ } from 'depa-processor';
9
30
  import type {
10
31
  MailboxSchema,
11
32
  ActorSelf,
12
33
  ActorEnvelope,
13
34
  ActorHandler,
14
- } from '../core/types';
35
+ } from '../core/types.js';
15
36
 
16
- // ─── DispatchRoute (structural, no import) ───────────────────────────
37
+ // ─── Actor sub-handler ───────────────────────────────────────────────
17
38
 
18
39
  /**
19
- * A dispatch route maps a tag to a dispatch key and handler.
20
- * This is a structural interface no dependency on depa-processor.
40
+ * A resolved actor sub-handler: receives `(self, envelope)` and performs side
41
+ * effects (write state / send messages). This is the `TResult = void | Promise<void>`
42
+ * shape the underlying DispatchEngine resolves to.
21
43
  */
22
- export interface DispatchRoute<
44
+ export type ActorRouteHandler<
45
+ TRuntime,
46
+ TSchema extends MailboxSchema,
47
+ TState,
48
+ > = (
49
+ self: ActorSelf<TRuntime, TSchema, TState>,
50
+ envelope: ActorEnvelope<TSchema>,
51
+ ) => void | Promise<void>;
52
+
53
+ // ─── Key extraction (envelope → dispatch coordinate) ─────────────────
54
+
55
+ /**
56
+ * Maps an envelope to the dispatch coordinate consumed by the selected strategy.
57
+ * Defaults are provided per strategy; override to route by payload fields, etc.
58
+ */
59
+ export interface DispatchKeyExtractors<TSchema extends MailboxSchema> {
60
+ /** ROUTE_KEY / ROUTE_KEY_TO_ENUM / COMMAND_TABLE — defaults to `envelope.tag`. */
61
+ routeKeyOf?: (envelope: ActorEnvelope<TSchema>) => string;
62
+ /** ENUM — defaults to `envelope.tag`. */
63
+ enumOf?: (envelope: ActorEnvelope<TSchema>) => string | number;
64
+ /** CLASS / key-based strategies' input — defaults to `envelope.payload`. */
65
+ inputOf?: (envelope: ActorEnvelope<TSchema>) => unknown;
66
+ /** PATH / ACTION_PATH — defaults to `envelope.tag`. */
67
+ pathOf?: (envelope: ActorEnvelope<TSchema>) => string;
68
+ /** ACTION_PATH action — defaults to `undefined`. */
69
+ actionOf?: (envelope: ActorEnvelope<TSchema>) => unknown;
70
+ }
71
+
72
+ // ─── createDispatchHandler params (object-param, B2 composable) ───────
73
+
74
+ /**
75
+ * Parameters for {@link createDispatchHandler}.
76
+ *
77
+ * `routes` is either:
78
+ * - a plain key → actor-sub-handler map (for the common key-based strategies
79
+ * CLASS-by-key is not applicable; use ROUTE_KEY / ENUM / COMMAND_TABLE), or
80
+ * - a fully-built `DispatchStrategyConfig<void | Promise<void>>` for advanced
81
+ * strategies (PATH / ACTION_PATH / CLASS / ROUTE_KEY_TO_ENUM) where the
82
+ * handler wiring is expressed in depa-processor terms.
83
+ *
84
+ * When a `DispatchStrategyConfig` is supplied, its handlers receive the raw
85
+ * dispatch `input` / `context` (already adapted from the envelope) and may
86
+ * close over `self` via the surrounding scope.
87
+ */
88
+ export interface CreateDispatchHandlerParams<
23
89
  TRuntime,
24
90
  TSchema extends MailboxSchema,
25
91
  TState,
26
92
  > {
27
- /** Which tags this route handles */
28
- tags: (keyof TSchema & string)[];
29
-
30
- /** Resolve dispatch key from envelope */
31
- resolveKey: (envelope: ActorEnvelope<TSchema>) => string;
32
-
33
- /** Route table: dispatch key handler */
34
- routes: Record<string, (
35
- self: ActorSelf<TRuntime, TSchema, TState>,
36
- envelope: ActorEnvelope<TSchema>,
37
- ) => void | Promise<void>>;
38
-
39
- /** Fallback if no route matches */
40
- fallback?: (
41
- self: ActorSelf<TRuntime, TSchema, TState>,
42
- envelope: ActorEnvelope<TSchema>,
43
- key: string,
44
- ) => void | Promise<void>;
93
+ /** Which of the 7 strategies to use for second-level resolution. */
94
+ strategy: DispatchStrategyType;
95
+
96
+ /**
97
+ * Route table. For key-based strategies (ROUTE_KEY / ENUM / COMMAND_TABLE)
98
+ * pass a `Record<key, ActorRouteHandler>`. For advanced strategies, pass a
99
+ * prebuilt `DispatchStrategyConfig` whose `TResult = void | Promise<void>`.
100
+ */
101
+ routes:
102
+ | Record<string, ActorRouteHandler<TRuntime, TSchema, TState>>
103
+ | DispatchStrategyConfig<void | Promise<void>>;
104
+
105
+ /** Optional fallback when the strategy does not resolve a handler. */
106
+ defaultHandler?: ActorHandler<TRuntime, TSchema, TState>;
107
+
108
+ /** Optional envelope → dispatch-coordinate extractors. */
109
+ extractors?: DispatchKeyExtractors<TSchema>;
110
+ }
111
+
112
+ // ─── Internal helpers ────────────────────────────────────────────────
113
+
114
+ function isStrategyConfig(
115
+ routes: unknown,
116
+ ): routes is DispatchStrategyConfig<void | Promise<void>> {
117
+ return routes instanceof DispatchStrategyConfig;
118
+ }
119
+
120
+ /**
121
+ * Build a `DispatchStrategyConfig<void>` from a plain key → handler map for the
122
+ * supported key-based strategies. The map values are pre-bound to `(self, envelope)`
123
+ * by capturing the current dispatch's `self` / `envelope` via a thunk.
124
+ */
125
+ function buildKeyBasedConfig<TRuntime, TSchema extends MailboxSchema, TState>(
126
+ strategy: DispatchStrategyType,
127
+ routes: Record<string, ActorRouteHandler<TRuntime, TSchema, TState>>,
128
+ self: ActorSelf<TRuntime, TSchema, TState>,
129
+ envelope: ActorEnvelope<TSchema>,
130
+ ): DispatchStrategyConfig<void | Promise<void>> {
131
+ const handlerMap = new Map<
132
+ string | number,
133
+ (input: unknown) => void | Promise<void>
134
+ >();
135
+ for (const [key, handler] of Object.entries(routes)) {
136
+ handlerMap.set(key, () => handler(self, envelope));
137
+ }
138
+
139
+ switch (strategy) {
140
+ case DispatchStrategyType.ROUTE_KEY:
141
+ return DispatchStrategyConfig.forRouteKeyStrategy<void | Promise<void>>({
142
+ handlerMap: handlerMap as Map<string, (input: unknown) => void | Promise<void>>,
143
+ });
144
+ case DispatchStrategyType.ENUM:
145
+ return DispatchStrategyConfig.forEnumStrategy<void | Promise<void>>({
146
+ handlerMap,
147
+ });
148
+ case DispatchStrategyType.COMMAND_TABLE:
149
+ return DispatchStrategyConfig.forCommandStrategy<void | Promise<void>>({
150
+ commandConverter: (command: string) =>
151
+ handlerMap.has(command) ? command : null,
152
+ handlerExtractor: (commandEnum: string | number) =>
153
+ handlerMap.get(commandEnum) ?? null,
154
+ });
155
+ default:
156
+ throw new Error(
157
+ `createDispatchHandler: strategy ${strategy} requires a prebuilt ` +
158
+ `DispatchStrategyConfig in 'routes' (plain key→handler maps support ` +
159
+ `ROUTE_KEY / ENUM / COMMAND_TABLE only).`,
160
+ );
161
+ }
162
+ }
163
+
164
+ /** Build the strategy-appropriate DispatchRequest from the envelope. */
165
+ function buildRequest<TSchema extends MailboxSchema>(
166
+ strategy: DispatchStrategyType,
167
+ envelope: ActorEnvelope<TSchema>,
168
+ extractors: DispatchKeyExtractors<TSchema> | undefined,
169
+ ) {
170
+ const input = extractors?.inputOf ? extractors.inputOf(envelope) : envelope.payload;
171
+ const routeKey = extractors?.routeKeyOf
172
+ ? extractors.routeKeyOf(envelope)
173
+ : envelope.tag;
174
+ const path = extractors?.pathOf ? extractors.pathOf(envelope) : envelope.tag;
175
+
176
+ switch (strategy) {
177
+ case DispatchStrategyType.CLASS:
178
+ return createClassDispatchRequest<void | Promise<void>>(input);
179
+ case DispatchStrategyType.ROUTE_KEY:
180
+ return createRouteKeyDispatchRequest<void | Promise<void>>(routeKey, input, false);
181
+ case DispatchStrategyType.ENUM:
182
+ return createEnumDispatchRequest<void | Promise<void>>(
183
+ extractors?.enumOf ? extractors.enumOf(envelope) : envelope.tag,
184
+ input,
185
+ );
186
+ case DispatchStrategyType.ROUTE_KEY_TO_ENUM:
187
+ return createRouteKeyToEnumDispatchRequest<void | Promise<void>>(routeKey, input);
188
+ case DispatchStrategyType.COMMAND_TABLE:
189
+ return createCommandDispatchRequest<void | Promise<void>>(routeKey, input);
190
+ case DispatchStrategyType.PATH:
191
+ return createPathDispatchRequest<void | Promise<void>, unknown, unknown>({
192
+ runtime: undefined,
193
+ request: input,
194
+ path,
195
+ });
196
+ case DispatchStrategyType.ACTION_PATH:
197
+ return createActionPathDispatchRequest<void | Promise<void>, unknown, unknown, unknown>({
198
+ runtime: undefined,
199
+ request: input,
200
+ action: extractors?.actionOf ? extractors.actionOf(envelope) : undefined,
201
+ path,
202
+ });
203
+ default:
204
+ throw new Error(`createDispatchHandler: unknown strategy ${String(strategy)}`);
205
+ }
45
206
  }
46
207
 
47
208
  // ─── createDispatchHandler ───────────────────────────────────────────
48
209
 
49
210
  /**
50
- * Creates an ActorHandler that routes envelopes through dispatch routes.
51
- * Falls back to `defaultHandler` for tags not covered by any route.
211
+ * Creates an ActorHandler that resolves an envelope to a sub-handler via one of
212
+ * the 7 depa-processor dispatch strategies (opt-in, per-handler DX form C / B2).
213
+ *
214
+ * Object-param signature: `{ strategy, routes, defaultHandler?, extractors? }`.
215
+ *
216
+ * Composability:
217
+ * - slot the returned handler under a `tag` in `ActorDef.handlers` → `tag` overlay
218
+ * (first-level) + `strategy` (second-level), the two axes are orthogonal.
219
+ * - the resolved sub-handler may itself be a `createPipelineHandler(...)` result,
220
+ * so dispatch and pipeline nest freely.
221
+ *
222
+ * No declarative `ActorDef.dispatch` field and no global feature flag / enableXxx
223
+ * API exist — enabling rich dispatch is expressed purely by import + composition.
52
224
  */
53
225
  export function createDispatchHandler<
54
226
  TRuntime,
55
227
  TSchema extends MailboxSchema,
56
228
  TState,
57
229
  >(
58
- routes: DispatchRoute<TRuntime, TSchema, TState>[],
59
- defaultHandler?: ActorHandler<TRuntime, TSchema, TState>,
230
+ params: CreateDispatchHandlerParams<TRuntime, TSchema, TState>,
60
231
  ): ActorHandler<TRuntime, TSchema, TState> {
61
- // Build tag route index for O(1) lookup
62
- const tagIndex = new Map<string, DispatchRoute<TRuntime, TSchema, TState>>();
63
- for (const route of routes) {
64
- for (const tag of route.tags) {
65
- tagIndex.set(tag, route);
66
- }
67
- }
232
+ const { strategy, routes, defaultHandler, extractors } = params;
68
233
 
69
234
  return async (self, envelope) => {
70
- const route = tagIndex.get(envelope.tag);
71
-
72
- if (route) {
73
- const key = route.resolveKey(envelope);
74
- const handler = route.routes[key];
75
- if (handler) {
76
- await handler(self, envelope);
77
- } else if (route.fallback) {
78
- await route.fallback(self, envelope, key);
79
- }
80
- } else if (defaultHandler) {
235
+ const config = isStrategyConfig(routes)
236
+ ? routes
237
+ : buildKeyBasedConfig(strategy, routes, self, envelope);
238
+
239
+ const engine = new DispatchEngine<void | Promise<void>>();
240
+ engine.registerStrategy(config);
241
+
242
+ const request = buildRequest(strategy, envelope, extractors);
243
+ const result = await engine.dispatch(request);
244
+
245
+ if (result.isHandled()) {
246
+ // Await the sub-handler's (possibly async) side effect.
247
+ await result.getResult();
248
+ return;
249
+ }
250
+
251
+ if (defaultHandler) {
81
252
  await defaultHandler(self, envelope);
82
253
  }
83
254
  };