@typeonce/effect-machine 0.11.0 → 0.12.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.
Files changed (39) hide show
  1. package/README.md +33 -9
  2. package/dist/Machine.d.ts +43 -43
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +9 -9
  5. package/dist/internal/machine/cluster.js +2 -2
  6. package/dist/internal/machine/cluster.js.map +1 -1
  7. package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
  8. package/dist/internal/machine/commandRuntime.js +2 -2
  9. package/dist/internal/machine/commandRuntime.js.map +1 -1
  10. package/dist/internal/machine/configuration.d.ts +7 -7
  11. package/dist/internal/machine/configuration.d.ts.map +1 -1
  12. package/dist/internal/machine/configuration.js +3 -3
  13. package/dist/internal/machine/configuration.js.map +1 -1
  14. package/dist/internal/machine/executionPlan.d.ts +3 -3
  15. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  16. package/dist/internal/machine/executionPlan.js +25 -25
  17. package/dist/internal/machine/executionPlan.js.map +1 -1
  18. package/dist/internal/machine/invocation.js +1 -1
  19. package/dist/internal/machine/invocation.js.map +1 -1
  20. package/dist/internal/machine/planner.d.ts +2 -2
  21. package/dist/internal/machine/planner.d.ts.map +1 -1
  22. package/dist/internal/machine/planner.js +20 -20
  23. package/dist/internal/machine/planner.js.map +1 -1
  24. package/dist/internal/machine/process.js +3 -3
  25. package/dist/internal/machine/process.js.map +1 -1
  26. package/dist/internal/machine/runtime.d.ts +3 -3
  27. package/dist/internal/machine/runtime.d.ts.map +1 -1
  28. package/dist/internal/machine/runtime.js.map +1 -1
  29. package/docs/agent-guide.md +28 -11
  30. package/package.json +1 -1
  31. package/src/Machine.ts +43 -43
  32. package/src/internal/machine/cluster.ts +2 -2
  33. package/src/internal/machine/commandRuntime.ts +4 -2
  34. package/src/internal/machine/configuration.ts +10 -10
  35. package/src/internal/machine/executionPlan.ts +35 -35
  36. package/src/internal/machine/invocation.ts +1 -1
  37. package/src/internal/machine/planner.ts +30 -24
  38. package/src/internal/machine/process.ts +3 -3
  39. package/src/internal/machine/runtime.ts +4 -4
@@ -7,7 +7,13 @@
7
7
  import * as Cause from "effect/Cause"
8
8
  import * as Effect from "effect/Effect"
9
9
  import type * as Schema from "effect/Schema"
10
- import type { ActorContext, ActorRef, Enqueue, InitialEvent as MachineInitialEvent, Machine } from "../../Machine.js"
10
+ import type {
11
+ Enqueue,
12
+ InitialEvent as MachineInitialEvent,
13
+ Machine,
14
+ MachineReferences,
15
+ MachineTarget
16
+ } from "../../Machine.js"
11
17
  import { getTargetBuilder, makeCollector, type RuntimeCommand } from "./command.js"
12
18
  import {
13
19
  type ActiveConfiguration,
@@ -94,35 +100,35 @@ export type TransitionHandler<States extends Machine.StateSchemas, E, R, Context
94
100
  enqueue: Enqueue<any, any>
95
101
  ) => Machine.HandlerResult<States, E, R>
96
102
 
97
- const rootActorScopes = new WeakMap<Machine.Any, ActorContext<any, any>>()
98
- const machineActorScopes = new WeakMap<Machine.Any, ActorContext<any, any>>()
103
+ const rootMachineReferences = new WeakMap<Machine.Any, MachineReferences<any, any>>()
104
+ const scopedMachineReferences = new WeakMap<Machine.Any, MachineReferences<any, any>>()
99
105
 
100
- export const withActorScope = (
106
+ export const withMachineReferences = (
101
107
  machine: Machine.Any,
102
- actorScope: ActorContext<any, any>
108
+ machineReferences: MachineReferences<any, any>
103
109
  ): Machine.Any => {
104
110
  const scoped = Object.create(machine) as Machine.Any
105
- machineActorScopes.set(scoped, { self: actorScope.self, parent: actorScope.parent })
111
+ scopedMachineReferences.set(scoped, { self: machineReferences.self, parent: machineReferences.parent })
106
112
  return scoped
107
113
  }
108
114
 
109
- const getActorContext = (
115
+ const resolveMachineReferences = (
110
116
  machine: Machine.Any,
111
117
  configuration: ActiveConfiguration
112
- ): ActorContext<any, any> => {
113
- const scope = configuration.actorScope
118
+ ): MachineReferences<any, any> => {
119
+ const scope = configuration.machineReferences
114
120
  if (scope !== undefined) return scope
115
- const machineScope = machineActorScopes.get(machine)
121
+ const machineScope = scopedMachineReferences.get(machine)
116
122
  if (machineScope !== undefined) return machineScope
117
- const cached = rootActorScopes.get(machine)
123
+ const cached = rootMachineReferences.get(machine)
118
124
  if (cached !== undefined) return cached
119
- const self: ActorRef<unknown> = {
125
+ const self: MachineTarget<unknown> = {
120
126
  id: machine.id ?? "Machine",
121
127
  sessionId: "Machine.plan",
122
128
  send: () => Effect.fail(new StoppedError())
123
129
  }
124
130
  const root = { self, parent: undefined }
125
- rootActorScopes.set(machine, root)
131
+ rootMachineReferences.set(machine, root)
126
132
  return root
127
133
  }
128
134
 
@@ -240,7 +246,7 @@ const completeHistoryConfiguration = (
240
246
  values,
241
247
  outputs: configuration.outputs,
242
248
  history: configuration.history,
243
- actorScope: configuration.actorScope
249
+ machineReferences: configuration.machineReferences
244
250
  } as ActiveConfiguration
245
251
  active.add(child.path)
246
252
  if (child.schema !== undefined) {
@@ -249,7 +255,7 @@ const completeHistoryConfiguration = (
249
255
  throw new Error(`Machine shallow history requires an initial value implementation for state "${path}"`)
250
256
  }
251
257
  const initialized = collectStateInitializer(machine, initializer, {
252
- ...getActorContext(machine, current),
258
+ ...resolveMachineReferences(machine, current),
253
259
  state: current.values.get(path),
254
260
  containingState: getParentValue(machine, current, path),
255
261
  ancestors: getParentValues(machine, current, path),
@@ -271,14 +277,14 @@ const completeHistoryConfiguration = (
271
277
  values,
272
278
  outputs: configuration.outputs,
273
279
  history: configuration.history,
274
- actorScope: configuration.actorScope
280
+ machineReferences: configuration.machineReferences
275
281
  } as ActiveConfiguration
276
282
  const initializer = valuedMissing.length === 0 ? undefined : machine.handlers[path]?.initial
277
283
  if (valuedMissing.length > 0 && initializer === undefined) {
278
284
  throw new Error(`Machine shallow history requires an initial value implementation for state "${path}"`)
279
285
  }
280
286
  const initialized = initializer === undefined ? undefined : collectStateInitializer(machine, initializer, {
281
- ...getActorContext(machine, current),
287
+ ...resolveMachineReferences(machine, current),
282
288
  state: current.values.get(path),
283
289
  containingState: getParentValue(machine, current, path),
284
290
  ancestors: getParentValues(machine, current, path),
@@ -542,7 +548,7 @@ const makeStateActionContext = <
542
548
  path: string,
543
549
  event: Machine.LifecycleEvent<Events>
544
550
  ): Machine.StateActionContext<States, Events, Emits, StateId> => ({
545
- ...getActorContext(machine, configuration),
551
+ ...resolveMachineReferences(machine, configuration),
546
552
  state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
547
553
  containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
548
554
  ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
@@ -562,7 +568,7 @@ const makeTransitionContext = <
562
568
  event: Machine.EventByTag<Events, EventTag>,
563
569
  snapshot: Machine.Snapshot<States>
564
570
  ): Machine.HandlerContext<States, Events, Emits, StateId, EventTag, any, any> => ({
565
- ...getActorContext(machine, configuration),
571
+ ...resolveMachineReferences(machine, configuration),
566
572
  state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
567
573
  containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
568
574
  ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
@@ -584,7 +590,7 @@ const makeDoneContext = <
584
590
  output: unknown,
585
591
  snapshot: Machine.Snapshot<States>
586
592
  ): Machine.DoneContext<States, Events, Emits, StateId> => ({
587
- ...getActorContext(machine, configuration),
593
+ ...resolveMachineReferences(machine, configuration),
588
594
  state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
589
595
  containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
590
596
  ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
@@ -679,7 +685,7 @@ const selectAlwaysTransitions = <
679
685
  Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>
680
686
  >,
681
687
  context: {
682
- ...getActorContext(machine, configuration),
688
+ ...resolveMachineReferences(machine, configuration),
683
689
  state: configuration.values.get(path) as Machine.StateByIdentifier<
684
690
  States,
685
691
  Machine.StateIdentifier<States>
@@ -867,7 +873,7 @@ const selectInvocationTransition = <
867
873
  if (transition === undefined) return []
868
874
  const snapshot = snapshotFromConfiguration<States>(machine, configuration)
869
875
  const context = {
870
- ...getActorContext(machine, configuration),
876
+ ...resolveMachineReferences(machine, configuration),
871
877
  state: configuration.values.get(event.path),
872
878
  containingState: getParentValue(machine, configuration, event.path),
873
879
  ancestors: getParentValues(machine, configuration, event.path),
@@ -1107,10 +1113,10 @@ const resolveChoiceTarget = (
1107
1113
  ]),
1108
1114
  outputs: configuration.outputs,
1109
1115
  history: configuration.history,
1110
- ...(configuration.actorScope === undefined ? {} : { actorScope: configuration.actorScope })
1116
+ ...(configuration.machineReferences === undefined ? {} : { machineReferences: configuration.machineReferences })
1111
1117
  }
1112
1118
  const collected = collectTransition(machine, choice.transition, {
1113
- ...getActorContext(machine, provisional),
1119
+ ...resolveMachineReferences(machine, provisional),
1114
1120
  containingState: getParentValue(machine, provisional, node.path),
1115
1121
  ancestors: getParentValues(machine, provisional, node.path),
1116
1122
  event,
@@ -459,7 +459,7 @@ const makeProcessLogic: <
459
459
  ) =>
460
460
  compiledInitial === undefined
461
461
  ? internalRuntime.provideMachineRuntime(
462
- internalPlanner.planInitial(internalPlanner.withActorScope(machine, scope), ...initialArgs).pipe(
462
+ internalPlanner.planInitial(internalPlanner.withMachineReferences(machine, scope), ...initialArgs).pipe(
463
463
  Effect.flatMap((planned) => {
464
464
  const commands = planned.commands.length === 0
465
465
  ? undefined
@@ -540,7 +540,7 @@ const makeProcessLogic: <
540
540
  try {
541
541
  planned = internalPlanner.planConfiguration(
542
542
  machine,
543
- Configuration.withActorScope(
543
+ Configuration.withMachineReferences(
544
544
  configuration ?? Configuration.normalizeConfigurationSync(machine, current),
545
545
  context
546
546
  ),
@@ -651,7 +651,7 @@ const makeProcessLogic: <
651
651
  try {
652
652
  planned = internalPlanner.planConfiguration(
653
653
  machine,
654
- Configuration.withActorScope(
654
+ Configuration.withMachineReferences(
655
655
  configuration ?? Configuration.normalizeConfigurationSync(machine, current),
656
656
  context
657
657
  ),
@@ -18,7 +18,7 @@ import * as Scope from "effect/Scope"
18
18
  import * as Stream from "effect/Stream"
19
19
  import * as SynchronizedRef from "effect/SynchronizedRef"
20
20
  import type * as Take from "effect/Take"
21
- import type { ActorRef } from "../../Machine.js"
21
+ import type { MachineTarget } from "../../Machine.js"
22
22
  import { ChildAlreadyExistsError, StoppedError } from "./errors.js"
23
23
 
24
24
  type ChildDescriptor = {
@@ -325,7 +325,7 @@ interface ProcessAddress<in Event> {
325
325
  readonly send: (event: Event) => Effect.Effect<void, StoppedError>
326
326
  }
327
327
 
328
- const isProcessAddress = (value: unknown): value is ActorRef<unknown> =>
328
+ const isProcessAddress = (value: unknown): value is MachineTarget<unknown> =>
329
329
  typeof value === "object" && value !== null && "send" in value && typeof value.send === "function"
330
330
 
331
331
  export interface ProcessScope<Event> {
@@ -335,7 +335,7 @@ export interface ProcessScope<Event> {
335
335
  readonly sendParent: (event: unknown) => Effect.Effect<void, StoppedError>
336
336
  readonly emit: (event: unknown) => Effect.Effect<void>
337
337
  readonly sendTo: {
338
- <TargetEvent>(target: ActorRef<TargetEvent>, event: TargetEvent): Effect.Effect<void, StoppedError>
338
+ <TargetEvent>(target: MachineTarget<TargetEvent>, event: TargetEvent): Effect.Effect<void, StoppedError>
339
339
  (child: ChildSelector, event: unknown): Effect.Effect<void, StoppedError>
340
340
  }
341
341
  readonly stopChild: (child: ChildSelector) => Effect.Effect<void>
@@ -367,7 +367,7 @@ export interface ProcessContext<State, Event> extends ProcessScope<Event> {
367
367
  *
368
368
  * Unlike `ProcessContext`, synchronous mailbox and state operations do not
369
369
  * introduce an Effect boundary. The compiled drain still returns an Effect so
370
- * actor commands, invokes, observation callbacks, interruption, and the Effect
370
+ * machine commands, invokes, observation callbacks, interruption, and the Effect
371
371
  * scheduler remain explicit at their actual boundaries.
372
372
  *
373
373
  * @internal