@typeonce/effect-machine 0.35.0 → 0.36.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.
- package/README.md +8 -7
- package/dist/Machine.d.ts +113 -136
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/command.d.ts +0 -1
- package/dist/internal/machine/command.d.ts.map +1 -1
- package/dist/internal/machine/command.js +0 -14
- package/dist/internal/machine/command.js.map +1 -1
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +15 -2
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/construction.d.ts +9 -0
- package/dist/internal/machine/construction.d.ts.map +1 -0
- package/dist/internal/machine/construction.js +132 -0
- package/dist/internal/machine/construction.js.map +1 -0
- package/dist/internal/machine/declaration.d.ts +1 -0
- package/dist/internal/machine/declaration.d.ts.map +1 -1
- package/dist/internal/machine/declaration.js +3 -8
- package/dist/internal/machine/declaration.js.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +9 -7
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +2 -3
- package/dist/internal/machine/implementation.d.ts.map +1 -1
- package/dist/internal/machine/implementation.js.map +1 -1
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +68 -168
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +63 -17
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/topology.d.ts +6 -0
- package/dist/internal/machine/topology.d.ts.map +1 -1
- package/dist/internal/machine/topology.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +36 -76
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/docs/agent-guide.md +37 -34
- package/docs/root-api.md +143 -190
- package/package.json +1 -1
- package/src/Machine.ts +176 -144
- package/src/internal/machine/command.ts +0 -16
- package/src/internal/machine/configuration.ts +13 -10
- package/src/internal/machine/construction.ts +147 -0
- package/src/internal/machine/declaration.ts +4 -7
- package/src/internal/machine/executionPlan.ts +9 -7
- package/src/internal/machine/implementation.ts +2 -3
- package/src/internal/machine/machine.ts +86 -223
- package/src/internal/machine/planner.ts +69 -18
- package/src/internal/machine/topology.ts +4 -0
- package/src/internal/testing/machine/finiteModel.ts +44 -122
- package/dist/internal/machine/targetBuilder.d.ts +0 -16
- package/dist/internal/machine/targetBuilder.d.ts.map +0 -1
- package/dist/internal/machine/targetBuilder.js +0 -333
- package/dist/internal/machine/targetBuilder.js.map +0 -1
- package/src/internal/machine/targetBuilder.ts +0 -525
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/** Converts public construction objects into owned planner instructions. */
|
|
2
|
+
import type { Machine } from "../../Machine.js"
|
|
3
|
+
import * as Topology from "./topology.js"
|
|
4
|
+
|
|
5
|
+
export const record = (value: unknown): Readonly<Record<string, unknown>> => {
|
|
6
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
7
|
+
throw new Error("Machine construction must be an object")
|
|
8
|
+
}
|
|
9
|
+
return value as Readonly<Record<string, unknown>>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const nodeValue = (node: Machine.StateNode, config: Readonly<Record<string, unknown>>): unknown => {
|
|
13
|
+
if (config.decoded !== undefined && typeof config.decoded !== "boolean") {
|
|
14
|
+
throw new Error("Machine decoded construction flag must be a boolean")
|
|
15
|
+
}
|
|
16
|
+
if (node.schema === undefined) {
|
|
17
|
+
if (Object.hasOwn(config, "data") || Object.hasOwn(config, "decoded")) {
|
|
18
|
+
throw new Error(`Machine structural state "${node.path}" cannot construct data`)
|
|
19
|
+
}
|
|
20
|
+
return undefined
|
|
21
|
+
}
|
|
22
|
+
if (config.decoded === true) {
|
|
23
|
+
if (!Object.hasOwn(config, "data")) throw new Error("Machine decoded construction requires data")
|
|
24
|
+
return config.data
|
|
25
|
+
}
|
|
26
|
+
return Topology.makeStateInput(Object.hasOwn(config, "data") ? config.data : {})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const target = (
|
|
30
|
+
nodes: Machine.StateNodes,
|
|
31
|
+
path: string,
|
|
32
|
+
raw: unknown = {},
|
|
33
|
+
source?: string
|
|
34
|
+
): unknown => {
|
|
35
|
+
const selectedNode = nodes.byPath.get(path)
|
|
36
|
+
if (selectedNode?.type === "atomic" || selectedNode?.type === "final") {
|
|
37
|
+
const config = record(raw)
|
|
38
|
+
if (Reflect.ownKeys(config).some((key) => key !== "data" && key !== "decoded")) {
|
|
39
|
+
throw new Error("Machine leaf construction accepts only data and decoded")
|
|
40
|
+
}
|
|
41
|
+
return Topology.makeTarget(path, nodeValue(selectedNode, config) as { readonly _tag: PropertyKey })
|
|
42
|
+
}
|
|
43
|
+
const children = new Map<string, unknown>()
|
|
44
|
+
const visit = (path: string, raw: unknown): unknown => {
|
|
45
|
+
const config = record(raw)
|
|
46
|
+
if (Reflect.ownKeys(config).some((key) => key !== "data" && key !== "decoded" && key !== "states")) {
|
|
47
|
+
throw new Error("Machine state construction accepts only data, decoded, and states")
|
|
48
|
+
}
|
|
49
|
+
const node = nodes.byPath.get(path)
|
|
50
|
+
if (node === undefined || node.type === "history") throw new Error(`Machine invalid construction state "${path}"`)
|
|
51
|
+
const value = nodeValue(node, config)
|
|
52
|
+
if (Object.hasOwn(config, "states")) {
|
|
53
|
+
if (node.type !== "compound" && node.type !== "parallel") {
|
|
54
|
+
throw new Error(`Machine state "${path}" has no child construction`)
|
|
55
|
+
}
|
|
56
|
+
const states = record(config.states)
|
|
57
|
+
const keys = Reflect.ownKeys(states)
|
|
58
|
+
const active = node.children.filter((child) => nodes.byPath.get(child)?.type !== "history")
|
|
59
|
+
if (
|
|
60
|
+
node.type === "compound"
|
|
61
|
+
? keys.length !== 1
|
|
62
|
+
: keys.length !== active.length &&
|
|
63
|
+
!(keys.length === 1 && source !== undefined && (source === path || source.startsWith(`${path}.`)))
|
|
64
|
+
) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`Machine explicit states for "${path}" must select ${node.type === "compound" ? "one child" : "every region"}`
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
for (const key of keys) {
|
|
70
|
+
const child = path === "" ? String(key) : `${path}.${String(key)}`
|
|
71
|
+
if (
|
|
72
|
+
typeof key !== "string" ||
|
|
73
|
+
!(nodes.byPath.get(child)?.parent === path && nodes.byPath.get(child)?.type !== "history")
|
|
74
|
+
) throw new Error(`Machine unknown child "${String(key)}"`)
|
|
75
|
+
children.set(child, visit(child, states[key]))
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return value
|
|
79
|
+
}
|
|
80
|
+
const value = visit(path, raw)
|
|
81
|
+
const values = new Map(children)
|
|
82
|
+
values.set(path, value)
|
|
83
|
+
let selected = path
|
|
84
|
+
let config = record(raw)
|
|
85
|
+
while (config.states !== undefined) {
|
|
86
|
+
const node = nodes.byPath.get(selected)!
|
|
87
|
+
const states = record(config.states)
|
|
88
|
+
const keys = Object.keys(states)
|
|
89
|
+
if (
|
|
90
|
+
keys.length !== 1 ||
|
|
91
|
+
(node.type === "parallel" &&
|
|
92
|
+
!(source !== undefined && (source === selected || source.startsWith(`${selected}.`))))
|
|
93
|
+
) break
|
|
94
|
+
const key = keys[0]!
|
|
95
|
+
selected = selected === "" ? key : `${selected}.${key}`
|
|
96
|
+
config = record(states[key])
|
|
97
|
+
}
|
|
98
|
+
const node = nodes.byPath.get(selected)!
|
|
99
|
+
const selectedValue = values.get(selected)
|
|
100
|
+
const ancestors = Object.fromEntries(
|
|
101
|
+
[...values].filter(([p, v]) => v !== undefined && p !== selected && !p.startsWith(`${selected}.`))
|
|
102
|
+
)
|
|
103
|
+
if (node.type === "choice") return Topology.makeChoiceTarget(selected, node.parent ?? "", ancestors)
|
|
104
|
+
if (node.type === "atomic" || node.type === "final") {
|
|
105
|
+
return Topology.makeTarget(selected, selectedValue as { readonly _tag: PropertyKey }, {
|
|
106
|
+
values: ancestors as Readonly<Record<string, { readonly _tag: PropertyKey }>>
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
const descendants = new Map([...children].filter(([p]) => p.startsWith(`${selected}.`)))
|
|
110
|
+
return {
|
|
111
|
+
...Topology.makeInitialTarget(selected, selectedValue, ancestors),
|
|
112
|
+
...(descendants.size === 0 ? {} : { children: descendants })
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export const update = (nodes: Machine.StateNodes, path: string, raw: unknown): Topology.StateUpdate => {
|
|
117
|
+
const config = record(raw)
|
|
118
|
+
if (Reflect.ownKeys(config).some((key) => key !== "data" && key !== "decoded")) {
|
|
119
|
+
throw new Error("Machine update construction accepts only data and decoded")
|
|
120
|
+
}
|
|
121
|
+
const node = nodes.byPath.get(path)
|
|
122
|
+
if (node?.schema === undefined) throw new Error("Machine update requires a valued state")
|
|
123
|
+
return Topology.makeStateUpdate(path, nodeValue(node, config))
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** History fallbacks construct a complete tree, including the history owner. */
|
|
127
|
+
export const snapshot = (nodes: Machine.StateNodes, raw: unknown, path = ""): unknown => {
|
|
128
|
+
const config = record(raw)
|
|
129
|
+
const node = nodes.byPath.get(path)!
|
|
130
|
+
target(nodes, path, config)
|
|
131
|
+
const value = nodeValue(node, config)
|
|
132
|
+
if (node.type === "choice") return Topology.makeChoiceTarget(path, node.parent!)
|
|
133
|
+
if (node.type === "atomic" || node.type === "final") return { path, value }
|
|
134
|
+
if (config.states === undefined) {
|
|
135
|
+
throw new Error("Machine history fallback requires a complete explicit tree")
|
|
136
|
+
}
|
|
137
|
+
const states = record(config.states)
|
|
138
|
+
const children = Object.fromEntries(
|
|
139
|
+
Object.entries(states).map(([key, child]) => [
|
|
140
|
+
key,
|
|
141
|
+
snapshot(nodes, child, path === "" ? key : `${path}.${key}`)
|
|
142
|
+
])
|
|
143
|
+
)
|
|
144
|
+
return node.type === "parallel" ?
|
|
145
|
+
{ path, value, states: children }
|
|
146
|
+
: { path, value, state: Object.values(children)[0] }
|
|
147
|
+
}
|
|
@@ -12,6 +12,7 @@ interface Source {
|
|
|
12
12
|
readonly value: unknown
|
|
13
13
|
}
|
|
14
14
|
export interface Declaration {
|
|
15
|
+
readonly initialize?: (input: unknown) => unknown
|
|
15
16
|
readonly root: State<Machine.StateNodeConfig>
|
|
16
17
|
readonly sources: ReadonlyMap<string, Source>
|
|
17
18
|
readonly branches: ReadonlyMap<string, Readonly<Record<string, Readonly<Record<string, unknown>>>>>
|
|
@@ -50,7 +51,7 @@ export const capture = (
|
|
|
50
51
|
const spec = { ...record(value, `Machine branch "${name}.${key}" must be a declaration`) }
|
|
51
52
|
for (const field of Reflect.ownKeys(spec)) {
|
|
52
53
|
if (
|
|
53
|
-
typeof field !== "string" || !["target", "update", "
|
|
54
|
+
typeof field !== "string" || !["target", "update", "history", "none", "title"].includes(field)
|
|
54
55
|
) {
|
|
55
56
|
throw new Error(`Machine branch "${name}.${key}" contains an unknown declaration field`)
|
|
56
57
|
}
|
|
@@ -76,7 +77,7 @@ export const selection = (
|
|
|
76
77
|
declaration: Declaration,
|
|
77
78
|
config: Readonly<Record<string, unknown>>
|
|
78
79
|
): Topology.TargetSelection => {
|
|
79
|
-
const keys = ["target", "
|
|
80
|
+
const keys = ["target", "history", "none"].filter((key) => config[key] !== undefined)
|
|
80
81
|
if (keys.length === 0 && config.update !== undefined) {
|
|
81
82
|
const owner = reference(config.update, declaration.root)
|
|
82
83
|
if (owner.kind !== "state") throw new Error("Machine update requires an active state reference")
|
|
@@ -91,17 +92,13 @@ export const selection = (
|
|
|
91
92
|
return Topology.noneTargetSelection
|
|
92
93
|
}
|
|
93
94
|
const ref = reference(config[key], declaration.root)
|
|
94
|
-
if (ref.path === "") throw new Error("Machine root data changes use update; transitions select a descendant")
|
|
95
95
|
if ((key === "history") !== (ref.kind === "history")) {
|
|
96
96
|
throw new Error("Machine history references require a history transition")
|
|
97
97
|
}
|
|
98
|
-
if (key === "initial" && ref.kind !== "state") {
|
|
99
|
-
throw new Error("Machine initial entry requires an active state reference")
|
|
100
|
-
}
|
|
101
98
|
const update = config.update === undefined ? undefined : reference(config.update, declaration.root).path
|
|
102
99
|
if (update !== undefined && key !== "target") throw new Error("Machine owner updates require an ordinary destination")
|
|
103
100
|
return Topology.makeTargetSelection(
|
|
104
|
-
|
|
101
|
+
ref.kind,
|
|
105
102
|
ref.path,
|
|
106
103
|
key === "history" ? "full" : "branch",
|
|
107
104
|
update
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import * as Effect from "effect/Effect"
|
|
8
8
|
import type { Machine, MachineTarget } from "../../Machine.js"
|
|
9
|
-
import {
|
|
9
|
+
import { type RuntimeCommand } from "./command.js"
|
|
10
10
|
import {
|
|
11
11
|
type ActiveConfiguration,
|
|
12
12
|
compareDocumentOrder,
|
|
@@ -391,7 +391,6 @@ const makeIndexedTransitionContext = (
|
|
|
391
391
|
event: any,
|
|
392
392
|
machineReferences?: PlanningMachineReferences
|
|
393
393
|
): any => {
|
|
394
|
-
const source = descriptor.nodes[sourceIndex]!
|
|
395
394
|
const parentIndex = descriptor.parentIndices[sourceIndex]!
|
|
396
395
|
const ancestors: Record<string, unknown> = {}
|
|
397
396
|
for (const ancestorIndex of descriptor.ancestorIndices[sourceIndex]!) {
|
|
@@ -405,8 +404,7 @@ const makeIndexedTransitionContext = (
|
|
|
405
404
|
containingState: parentIndex < 0 ? undefined : configuration.values[parentIndex],
|
|
406
405
|
ancestors,
|
|
407
406
|
event,
|
|
408
|
-
snapshot: snapshotFromIndexedState(descriptor, configuration)
|
|
409
|
-
target: getTargetBuilder(machine, source.path)
|
|
407
|
+
snapshot: snapshotFromIndexedState(descriptor, configuration)
|
|
410
408
|
}
|
|
411
409
|
}
|
|
412
410
|
|
|
@@ -601,7 +599,12 @@ const collectIndexedEvaluatedTransition = (
|
|
|
601
599
|
const initialResolution = unresolvedTarget !== undefined && isInitialTarget(unresolvedTarget)
|
|
602
600
|
? resolveInitialTarget(
|
|
603
601
|
machine,
|
|
604
|
-
|
|
602
|
+
(() => {
|
|
603
|
+
const current = activeConfigurationFromIndexedState(descriptor, state)
|
|
604
|
+
return update === undefined
|
|
605
|
+
? current
|
|
606
|
+
: { ...current, values: new Map(current.values).set(update.path, update.value) }
|
|
607
|
+
})(),
|
|
605
608
|
unresolvedTarget,
|
|
606
609
|
(selection.context as any).event
|
|
607
610
|
)
|
|
@@ -830,8 +833,7 @@ const planIndexedFlatState = (
|
|
|
830
833
|
containingState: undefined,
|
|
831
834
|
ancestors: {},
|
|
832
835
|
event,
|
|
833
|
-
snapshot: snapshotFromIndexedState(descriptor, current)
|
|
834
|
-
target: getTargetBuilder(machine, sourcePath)
|
|
836
|
+
snapshot: snapshotFromIndexedState(descriptor, current)
|
|
835
837
|
},
|
|
836
838
|
transition.evaluate
|
|
837
839
|
)
|
|
@@ -28,13 +28,12 @@ export interface CapturedStateConfig {
|
|
|
28
28
|
/**
|
|
29
29
|
* Captured machine implementation consumed by semantic layers.
|
|
30
30
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
31
|
+
* Internal code enters through this view so handler lookup cannot silently
|
|
32
|
+
* produce `any`.
|
|
33
33
|
* Construction captures these containers before handing the machine to a planner.
|
|
34
34
|
*/
|
|
35
35
|
export interface MachineInternal extends Machine.Any {
|
|
36
36
|
readonly handlers: Readonly<Record<string, CapturedStateConfig | undefined>>
|
|
37
|
-
readonly makeTargetBuilder: (source: string) => Machine.TargetBuilder<any, any>
|
|
38
37
|
}
|
|
39
38
|
|
|
40
39
|
/** The single conversion from an erased public machine to its captured implementation. */
|