@sphereon/ssi-sdk.xstate-machine-persistence 0.33.1-next.3 → 0.33.1-next.73
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/dist/index.cjs +1014 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{ssi-sdk.xstate-machine-persistence.d.ts → index.d.cts} +426 -467
- package/dist/index.d.ts +425 -5
- package/dist/index.js +982 -21
- package/dist/index.js.map +1 -1
- package/package.json +25 -14
- package/plugin.schema.json +80 -320
- package/src/__tests__/localAgent.test.ts +1 -0
- package/src/__tests__/restAgent.test.ts +4 -1
- package/src/__tests__/shared/MachineStatePersistenceAgentLogic.ts +1 -0
- package/dist/agent/MachineStatePersistence.d.ts +0 -26
- package/dist/agent/MachineStatePersistence.d.ts.map +0 -1
- package/dist/agent/MachineStatePersistence.js +0 -197
- package/dist/agent/MachineStatePersistence.js.map +0 -1
- package/dist/functions/index.d.ts +0 -4
- package/dist/functions/index.d.ts.map +0 -1
- package/dist/functions/index.js +0 -20
- package/dist/functions/index.js.map +0 -1
- package/dist/functions/machineRegistration.d.ts +0 -130
- package/dist/functions/machineRegistration.d.ts.map +0 -1
- package/dist/functions/machineRegistration.js +0 -303
- package/dist/functions/machineRegistration.js.map +0 -1
- package/dist/functions/stateEventEmitter.d.ts +0 -10
- package/dist/functions/stateEventEmitter.d.ts.map +0 -1
- package/dist/functions/stateEventEmitter.js +0 -21
- package/dist/functions/stateEventEmitter.js.map +0 -1
- package/dist/functions/stateMapper.d.ts +0 -34
- package/dist/functions/stateMapper.d.ts.map +0 -1
- package/dist/functions/stateMapper.js +0 -83
- package/dist/functions/stateMapper.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/tsdoc-metadata.json +0 -11
- package/dist/types/IMachineStatePersistence.d.ts +0 -62
- package/dist/types/IMachineStatePersistence.d.ts.map +0 -1
- package/dist/types/IMachineStatePersistence.js +0 -3
- package/dist/types/IMachineStatePersistence.js.map +0 -1
- package/dist/types/index.d.ts +0 -3
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -19
- package/dist/types/index.js.map +0 -1
- package/dist/types/types.d.ts +0 -173
- package/dist/types/types.d.ts.map +0 -1
- package/dist/types/types.js +0 -13
- package/dist/types/types.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,23 +1,984 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
5
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// plugin.schema.json
|
|
9
|
+
var require_plugin_schema = __commonJS({
|
|
10
|
+
"plugin.schema.json"(exports, module) {
|
|
11
|
+
module.exports = {
|
|
12
|
+
IMachineStatePersistence: {
|
|
13
|
+
components: {
|
|
14
|
+
schemas: {
|
|
15
|
+
MachineStateDeleteArgs: {
|
|
16
|
+
$ref: '#/components/schemas/Pick<StoreMachineStateInfo,("instanceId"|"tenantId")>'
|
|
17
|
+
},
|
|
18
|
+
'Pick<StoreMachineStateInfo,("instanceId"|"tenantId")>': {
|
|
19
|
+
type: "object",
|
|
20
|
+
properties: {
|
|
21
|
+
instanceId: {
|
|
22
|
+
type: "string",
|
|
23
|
+
description: "Unique instance ID of the machine"
|
|
24
|
+
},
|
|
25
|
+
tenantId: {
|
|
26
|
+
type: "string"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
required: ["instanceId"],
|
|
30
|
+
additionalProperties: false
|
|
31
|
+
},
|
|
32
|
+
MachineStateGetArgs: {
|
|
33
|
+
$ref: '#/components/schemas/Pick<StoreMachineStateInfo,("instanceId"|"tenantId")>'
|
|
34
|
+
},
|
|
35
|
+
MachineStateInfo: {
|
|
36
|
+
type: "object",
|
|
37
|
+
additionalProperties: false,
|
|
38
|
+
properties: {
|
|
39
|
+
state: {
|
|
40
|
+
$ref: "#/components/schemas/SerializableState"
|
|
41
|
+
},
|
|
42
|
+
instanceId: {
|
|
43
|
+
type: "string",
|
|
44
|
+
description: "Unique instance ID of the machine"
|
|
45
|
+
},
|
|
46
|
+
sessionId: {
|
|
47
|
+
type: "string",
|
|
48
|
+
description: "Session Id of the machine. Not necessarily unique"
|
|
49
|
+
},
|
|
50
|
+
machineName: {
|
|
51
|
+
type: "string",
|
|
52
|
+
description: "Machine name"
|
|
53
|
+
},
|
|
54
|
+
latestStateName: {
|
|
55
|
+
type: "string",
|
|
56
|
+
description: "The latest state name. Can be empty for a newly initialize machine"
|
|
57
|
+
},
|
|
58
|
+
latestEventType: {
|
|
59
|
+
type: "string",
|
|
60
|
+
description: "event types like SET_TOC, SET_FIRSTNAME, .... Will be xstate.init on a newly initialized machine"
|
|
61
|
+
},
|
|
62
|
+
createdAt: {
|
|
63
|
+
type: "string",
|
|
64
|
+
description: "Represents the creation date"
|
|
65
|
+
},
|
|
66
|
+
expiresAt: {
|
|
67
|
+
type: "string",
|
|
68
|
+
description: "Represents the expiration date"
|
|
69
|
+
},
|
|
70
|
+
updatedAt: {
|
|
71
|
+
type: "string",
|
|
72
|
+
description: "Represents the update date"
|
|
73
|
+
},
|
|
74
|
+
updatedCount: {
|
|
75
|
+
type: "number",
|
|
76
|
+
description: "Represents a counter for tracking updates."
|
|
77
|
+
},
|
|
78
|
+
completedAt: {
|
|
79
|
+
type: "string",
|
|
80
|
+
format: "date-time"
|
|
81
|
+
},
|
|
82
|
+
tenantId: {
|
|
83
|
+
type: "string"
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
required: ["createdAt", "instanceId", "latestEventType", "machineName", "state", "updatedAt", "updatedCount"]
|
|
87
|
+
},
|
|
88
|
+
SerializableState: {
|
|
89
|
+
$ref: "#/components/schemas/XStateConfig<any,AnyEventObject>"
|
|
90
|
+
},
|
|
91
|
+
"XStateConfig<any,AnyEventObject>": {
|
|
92
|
+
type: "object",
|
|
93
|
+
properties: {
|
|
94
|
+
value: {
|
|
95
|
+
$ref: "#/components/schemas/StateValue"
|
|
96
|
+
},
|
|
97
|
+
context: {},
|
|
98
|
+
_event: {
|
|
99
|
+
$ref: "#/components/schemas/SCXML.Event<AnyEventObject>"
|
|
100
|
+
},
|
|
101
|
+
_sessionid: {
|
|
102
|
+
type: ["string", "null"]
|
|
103
|
+
},
|
|
104
|
+
historyValue: {
|
|
105
|
+
$ref: "#/components/schemas/HistoryValue"
|
|
106
|
+
},
|
|
107
|
+
history: {},
|
|
108
|
+
actions: {
|
|
109
|
+
type: "array",
|
|
110
|
+
items: {}
|
|
111
|
+
},
|
|
112
|
+
activities: {},
|
|
113
|
+
meta: {},
|
|
114
|
+
events: {
|
|
115
|
+
type: "array",
|
|
116
|
+
items: {
|
|
117
|
+
$ref: "#/components/schemas/AnyEventObject"
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
configuration: {
|
|
121
|
+
type: "array",
|
|
122
|
+
items: {}
|
|
123
|
+
},
|
|
124
|
+
transitions: {
|
|
125
|
+
type: "array",
|
|
126
|
+
items: {}
|
|
127
|
+
},
|
|
128
|
+
children: {
|
|
129
|
+
$ref: "#/components/schemas/Record<string,any>"
|
|
130
|
+
},
|
|
131
|
+
done: {
|
|
132
|
+
type: "boolean"
|
|
133
|
+
},
|
|
134
|
+
tags: {
|
|
135
|
+
$ref: "#/components/schemas/Set<string>"
|
|
136
|
+
},
|
|
137
|
+
machine: {}
|
|
138
|
+
},
|
|
139
|
+
required: ["value", "context", "_event", "_sessionid", "configuration", "transitions", "children"],
|
|
140
|
+
additionalProperties: false,
|
|
141
|
+
description: "The configuration for the XState machine state. Simplified StateConfig object from XState so we have a minimal typed structure"
|
|
142
|
+
},
|
|
143
|
+
StateValue: {
|
|
144
|
+
anyOf: [
|
|
145
|
+
{
|
|
146
|
+
type: "string"
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
$ref: "#/components/schemas/StateValueMap"
|
|
150
|
+
}
|
|
151
|
+
],
|
|
152
|
+
description: 'The string or object representing the state value relative to the parent state node.\n\n- For a child atomic state node, this is a string, e.g., `"pending"`.\n- For complex state nodes, this is an object, e.g., `{ success: "someChildState" }`.'
|
|
153
|
+
},
|
|
154
|
+
StateValueMap: {
|
|
155
|
+
type: "object",
|
|
156
|
+
additionalProperties: {
|
|
157
|
+
$ref: "#/components/schemas/StateValue"
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
"SCXML.Event<AnyEventObject>": {
|
|
161
|
+
type: "object",
|
|
162
|
+
properties: {
|
|
163
|
+
name: {
|
|
164
|
+
type: "string",
|
|
165
|
+
description: "This is a character string giving the name of the event. The SCXML Processor must set the name field to the name of this event. It is what is matched against the 'event' attribute of <transition>. Note that transitions can do additional tests by using the value of this field inside boolean expressions in the 'cond' attribute."
|
|
166
|
+
},
|
|
167
|
+
type: {
|
|
168
|
+
type: "string",
|
|
169
|
+
enum: ["platform", "internal", "external"],
|
|
170
|
+
description: `This field describes the event type. The SCXML Processor must set it to: "platform" (for events raised by the platform itself, such as error events), "internal" (for events raised by <raise> and <send> with target '_internal') or "external" (for all other events).`
|
|
171
|
+
},
|
|
172
|
+
sendid: {
|
|
173
|
+
type: "string",
|
|
174
|
+
description: "If the sending entity has specified a value for this, the Processor must set this field to that value (see C Event I/O Processors for details). Otherwise, in the case of error events triggered by a failed attempt to send an event, the Processor must set this field to the send id of the triggering <send> element. Otherwise it must leave it blank."
|
|
175
|
+
},
|
|
176
|
+
origin: {
|
|
177
|
+
type: "string",
|
|
178
|
+
description: "This is a URI, equivalent to the 'target' attribute on the <send> element. For external events, the SCXML Processor should set this field to a value which, when used as the value of 'target', will allow the receiver of the event to <send> a response back to the originating entity via the Event I/O Processor specified in 'origintype'. For internal and platform events, the Processor must leave this field blank."
|
|
179
|
+
},
|
|
180
|
+
origintype: {
|
|
181
|
+
type: "string",
|
|
182
|
+
description: "This is equivalent to the 'type' field on the <send> element. For external events, the SCXML Processor should set this field to a value which, when used as the value of 'type', will allow the receiver of the event to <send> a response back to the originating entity at the URI specified by 'origin'. For internal and platform events, the Processor must leave this field blank."
|
|
183
|
+
},
|
|
184
|
+
invokeid: {
|
|
185
|
+
type: "string",
|
|
186
|
+
description: "If this event is generated from an invoked child process, the SCXML Processor must set this field to the invoke id of the invocation that triggered the child process. Otherwise it must leave it blank."
|
|
187
|
+
},
|
|
188
|
+
data: {
|
|
189
|
+
$ref: "#/components/schemas/AnyEventObject",
|
|
190
|
+
description: "This field contains whatever data the sending entity chose to include in this event. The receiving SCXML Processor should reformat this data to match its data model, but must not otherwise modify it.\n\nIf the conversion is not possible, the Processor must leave the field blank and must place an error 'error.execution' in the internal event queue."
|
|
191
|
+
},
|
|
192
|
+
$$type: {
|
|
193
|
+
type: "string",
|
|
194
|
+
const: "scxml"
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
required: ["name", "type", "data", "$$type"],
|
|
198
|
+
additionalProperties: false
|
|
199
|
+
},
|
|
200
|
+
AnyEventObject: {
|
|
201
|
+
type: "object",
|
|
202
|
+
properties: {
|
|
203
|
+
type: {
|
|
204
|
+
type: "string",
|
|
205
|
+
description: "The type of event that is sent."
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
required: ["type"]
|
|
209
|
+
},
|
|
210
|
+
HistoryValue: {
|
|
211
|
+
type: "object",
|
|
212
|
+
properties: {
|
|
213
|
+
states: {
|
|
214
|
+
$ref: "#/components/schemas/Record<string,(interface-434048446-6094-6223-434048446-0-57488|undefined)>"
|
|
215
|
+
},
|
|
216
|
+
current: {
|
|
217
|
+
$ref: "#/components/schemas/StateValue"
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
required: ["states"],
|
|
221
|
+
additionalProperties: false
|
|
222
|
+
},
|
|
223
|
+
"Record<string,(interface-434048446-6094-6223-434048446-0-57488|undefined)>": {
|
|
224
|
+
type: "object",
|
|
225
|
+
additionalProperties: {
|
|
226
|
+
anyOf: [
|
|
227
|
+
{
|
|
228
|
+
$ref: "#/components/schemas/HistoryValue"
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
not: {}
|
|
232
|
+
}
|
|
233
|
+
]
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
"Record<string,any>": {
|
|
237
|
+
type: "object"
|
|
238
|
+
},
|
|
239
|
+
"Set<string>": {
|
|
240
|
+
type: "object",
|
|
241
|
+
properties: {
|
|
242
|
+
size: {
|
|
243
|
+
type: "number"
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
required: ["size"],
|
|
247
|
+
additionalProperties: false
|
|
248
|
+
},
|
|
249
|
+
InitMachineStateArgs: {
|
|
250
|
+
type: "object",
|
|
251
|
+
additionalProperties: false,
|
|
252
|
+
properties: {
|
|
253
|
+
cleanupAllOtherInstances: {
|
|
254
|
+
type: "boolean"
|
|
255
|
+
},
|
|
256
|
+
customInstanceId: {
|
|
257
|
+
type: "string"
|
|
258
|
+
},
|
|
259
|
+
existingInstanceId: {
|
|
260
|
+
type: "string"
|
|
261
|
+
},
|
|
262
|
+
machineName: {
|
|
263
|
+
type: "string",
|
|
264
|
+
description: "Machine name"
|
|
265
|
+
},
|
|
266
|
+
tenantId: {
|
|
267
|
+
type: "string"
|
|
268
|
+
},
|
|
269
|
+
createdAt: {
|
|
270
|
+
type: "string",
|
|
271
|
+
description: "Represents the creation date"
|
|
272
|
+
},
|
|
273
|
+
expiresAt: {
|
|
274
|
+
type: "string",
|
|
275
|
+
description: "Represents the expiration date"
|
|
276
|
+
},
|
|
277
|
+
stateType: {
|
|
278
|
+
$ref: "#/components/schemas/MachineStateInitType"
|
|
279
|
+
},
|
|
280
|
+
machineState: {
|
|
281
|
+
$ref: "#/components/schemas/MachineStateInfo"
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
required: ["machineName"]
|
|
285
|
+
},
|
|
286
|
+
MachineStateInitType: {
|
|
287
|
+
type: "string",
|
|
288
|
+
enum: ["new", "existing"]
|
|
289
|
+
},
|
|
290
|
+
MachineStateInit: {
|
|
291
|
+
type: "object",
|
|
292
|
+
additionalProperties: false,
|
|
293
|
+
properties: {
|
|
294
|
+
stateType: {
|
|
295
|
+
$ref: "#/components/schemas/MachineStateInitType"
|
|
296
|
+
},
|
|
297
|
+
machineState: {
|
|
298
|
+
$ref: "#/components/schemas/MachineStateInfo"
|
|
299
|
+
},
|
|
300
|
+
instanceId: {
|
|
301
|
+
type: "string",
|
|
302
|
+
description: "Unique instance ID of the machine"
|
|
303
|
+
},
|
|
304
|
+
machineName: {
|
|
305
|
+
type: "string",
|
|
306
|
+
description: "Machine name"
|
|
307
|
+
},
|
|
308
|
+
tenantId: {
|
|
309
|
+
type: "string"
|
|
310
|
+
},
|
|
311
|
+
createdAt: {
|
|
312
|
+
type: "string",
|
|
313
|
+
description: "Represents the creation date"
|
|
314
|
+
},
|
|
315
|
+
expiresAt: {
|
|
316
|
+
type: "string",
|
|
317
|
+
description: "Represents the expiration date"
|
|
318
|
+
}
|
|
319
|
+
},
|
|
320
|
+
required: ["createdAt", "instanceId", "machineName", "stateType"]
|
|
321
|
+
},
|
|
322
|
+
MachineStatePersistArgs: {
|
|
323
|
+
type: "object",
|
|
324
|
+
additionalProperties: false,
|
|
325
|
+
properties: {
|
|
326
|
+
cleanupOnFinalState: {
|
|
327
|
+
type: "boolean"
|
|
328
|
+
},
|
|
329
|
+
updatedCount: {
|
|
330
|
+
type: "number",
|
|
331
|
+
description: "Represents a counter for tracking updates."
|
|
332
|
+
},
|
|
333
|
+
state: {
|
|
334
|
+
$ref: "#/components/schemas/SerializableState"
|
|
335
|
+
},
|
|
336
|
+
instanceId: {
|
|
337
|
+
type: "string",
|
|
338
|
+
description: "Unique instance ID of the machine"
|
|
339
|
+
},
|
|
340
|
+
machineName: {
|
|
341
|
+
type: "string",
|
|
342
|
+
description: "Machine name"
|
|
343
|
+
},
|
|
344
|
+
tenantId: {
|
|
345
|
+
type: "string"
|
|
346
|
+
},
|
|
347
|
+
expiresAt: {
|
|
348
|
+
type: "string",
|
|
349
|
+
description: "Represents the expiration date"
|
|
350
|
+
},
|
|
351
|
+
stateType: {
|
|
352
|
+
$ref: "#/components/schemas/MachineStateInitType"
|
|
353
|
+
},
|
|
354
|
+
machineState: {
|
|
355
|
+
$ref: "#/components/schemas/MachineStateInfo"
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
required: ["instanceId", "machineName", "state", "stateType"],
|
|
359
|
+
description: "Represents the arguments required to persist the machine state."
|
|
360
|
+
},
|
|
361
|
+
DeleteExpiredStatesArgs: {
|
|
362
|
+
$ref: '#/components/schemas/Pick<StoreMachineStateDeleteExpiredArgs,("deleteDoneStates"|"machineName"|"tenantId")>',
|
|
363
|
+
description: "Represents the arguments for deleting expired states from a machine."
|
|
364
|
+
},
|
|
365
|
+
'Pick<StoreMachineStateDeleteExpiredArgs,("deleteDoneStates"|"machineName"|"tenantId")>': {
|
|
366
|
+
type: "object",
|
|
367
|
+
properties: {
|
|
368
|
+
deleteDoneStates: {
|
|
369
|
+
type: "boolean"
|
|
370
|
+
},
|
|
371
|
+
machineName: {
|
|
372
|
+
type: "string"
|
|
373
|
+
},
|
|
374
|
+
tenantId: {
|
|
375
|
+
type: "string"
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
additionalProperties: false
|
|
379
|
+
},
|
|
380
|
+
DeleteStateResult: {
|
|
381
|
+
type: "number"
|
|
382
|
+
},
|
|
383
|
+
FindActiveStatesArgs: {
|
|
384
|
+
$ref: "#/components/schemas/StoreMachineStatesFindActiveArgs",
|
|
385
|
+
description: "Represents the arguments for finding active states of a store machine."
|
|
386
|
+
},
|
|
387
|
+
StoreMachineStatesFindActiveArgs: {
|
|
388
|
+
$ref: '#/components/schemas/Partial<Pick<StoreMachineStateInfo,("machineName"|"tenantId"|"instanceId")>>'
|
|
389
|
+
},
|
|
390
|
+
'Partial<Pick<StoreMachineStateInfo,("machineName"|"tenantId"|"instanceId")>>': {
|
|
391
|
+
type: "object",
|
|
392
|
+
properties: {
|
|
393
|
+
machineName: {
|
|
394
|
+
type: "string",
|
|
395
|
+
description: "Machine name"
|
|
396
|
+
},
|
|
397
|
+
tenantId: {
|
|
398
|
+
type: "string"
|
|
399
|
+
},
|
|
400
|
+
instanceId: {
|
|
401
|
+
type: "string",
|
|
402
|
+
description: "Unique instance ID of the machine"
|
|
403
|
+
}
|
|
404
|
+
},
|
|
405
|
+
additionalProperties: false
|
|
406
|
+
}
|
|
407
|
+
},
|
|
408
|
+
methods: {
|
|
409
|
+
machineStateDelete: {
|
|
410
|
+
description: "Delete a particular machine state by instance id and tenant id",
|
|
411
|
+
arguments: {
|
|
412
|
+
$ref: "#/components/schemas/MachineStateDeleteArgs"
|
|
413
|
+
},
|
|
414
|
+
returnType: {
|
|
415
|
+
type: "boolean"
|
|
416
|
+
}
|
|
417
|
+
},
|
|
418
|
+
machineStateGet: {
|
|
419
|
+
description: "Get a particular machine state by instance id and tenant id",
|
|
420
|
+
arguments: {
|
|
421
|
+
$ref: "#/components/schemas/MachineStateGetArgs"
|
|
422
|
+
},
|
|
423
|
+
returnType: {
|
|
424
|
+
$ref: "#/components/schemas/MachineStateInfo"
|
|
425
|
+
}
|
|
426
|
+
},
|
|
427
|
+
machineStateInit: {
|
|
428
|
+
description: "Initializes a state object for a new machine. Does not persist anything",
|
|
429
|
+
arguments: {
|
|
430
|
+
$ref: "#/components/schemas/InitMachineStateArgs"
|
|
431
|
+
},
|
|
432
|
+
returnType: {
|
|
433
|
+
$ref: "#/components/schemas/MachineStateInit"
|
|
434
|
+
}
|
|
435
|
+
},
|
|
436
|
+
machineStatePersist: {
|
|
437
|
+
description: "Persists the state",
|
|
438
|
+
arguments: {
|
|
439
|
+
$ref: "#/components/schemas/MachineStatePersistArgs"
|
|
440
|
+
},
|
|
441
|
+
returnType: {
|
|
442
|
+
$ref: "#/components/schemas/MachineStateInfo"
|
|
443
|
+
}
|
|
444
|
+
},
|
|
445
|
+
machineStatesDeleteExpired: {
|
|
446
|
+
description: "Deletes the state of an xstate machine in the database.",
|
|
447
|
+
arguments: {
|
|
448
|
+
$ref: "#/components/schemas/DeleteExpiredStatesArgs"
|
|
449
|
+
},
|
|
450
|
+
returnType: {
|
|
451
|
+
$ref: "#/components/schemas/DeleteStateResult"
|
|
452
|
+
}
|
|
453
|
+
},
|
|
454
|
+
machineStatesFindActive: {
|
|
455
|
+
description: "Loads the states of active xstate machines from the database.",
|
|
456
|
+
arguments: {
|
|
457
|
+
$ref: "#/components/schemas/FindActiveStatesArgs"
|
|
458
|
+
},
|
|
459
|
+
returnType: {
|
|
460
|
+
type: "array",
|
|
461
|
+
items: {
|
|
462
|
+
$ref: "#/components/schemas/MachineStateInfo"
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
// src/agent/MachineStatePersistence.ts
|
|
474
|
+
import Debug2 from "debug";
|
|
475
|
+
import { v4 as uuidv4 } from "uuid";
|
|
476
|
+
|
|
477
|
+
// src/functions/machineRegistration.ts
|
|
478
|
+
import { State as State2 } from "xstate";
|
|
479
|
+
import { waitFor } from "xstate/lib/waitFor";
|
|
480
|
+
|
|
481
|
+
// src/types/types.ts
|
|
482
|
+
var MachineStatePersistEventType = /* @__PURE__ */ function(MachineStatePersistEventType2) {
|
|
483
|
+
MachineStatePersistEventType2["INIT"] = "INIT";
|
|
484
|
+
MachineStatePersistEventType2["EVERY"] = "EVERY";
|
|
485
|
+
return MachineStatePersistEventType2;
|
|
486
|
+
}({});
|
|
487
|
+
|
|
488
|
+
// src/functions/stateEventEmitter.ts
|
|
489
|
+
import Debug from "debug";
|
|
490
|
+
var debug = Debug("sphereon:ssi-sdk:machine-state:xstate-persistence");
|
|
491
|
+
var emitMachineStatePersistEvent = /* @__PURE__ */ __name((event, context) => {
|
|
492
|
+
debug(`Emitting machine state persistence event '${event.type}' with counter: ${event.data._eventCounter} and state ${JSON.stringify(event.data.state.value)}`);
|
|
493
|
+
void context.agent.emit(event.type, event.data);
|
|
494
|
+
}, "emitMachineStatePersistEvent");
|
|
495
|
+
|
|
496
|
+
// src/functions/stateMapper.ts
|
|
497
|
+
import { State } from "xstate";
|
|
498
|
+
var machineStateToStoreInfo = /* @__PURE__ */ __name((machineInfo, existingState) => {
|
|
499
|
+
const { state, machineName, tenantId, expiresAt, instanceId, updatedCount } = machineInfo;
|
|
500
|
+
const existing = existingState ?? {
|
|
501
|
+
machineName,
|
|
502
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
503
|
+
expiresAt
|
|
504
|
+
};
|
|
505
|
+
const stateInstance = State.create(machineInfo.state);
|
|
506
|
+
let latestStateName = void 0;
|
|
507
|
+
if (stateInstance.value) {
|
|
508
|
+
latestStateName = typeof stateInstance.value === "string" ? stateInstance.value : JSON.stringify(stateInstance.value);
|
|
509
|
+
}
|
|
510
|
+
if (latestStateName === "{}") {
|
|
511
|
+
latestStateName = void 0;
|
|
512
|
+
}
|
|
513
|
+
return {
|
|
514
|
+
instanceId,
|
|
515
|
+
updatedCount: updatedCount ?? (existing?.updatedCount ? existing.updatedCount++ : 0),
|
|
516
|
+
sessionId: stateInstance._sessionid ?? void 0,
|
|
517
|
+
machineName,
|
|
518
|
+
state: serializeMachineState(state),
|
|
519
|
+
tenantId,
|
|
520
|
+
latestStateName,
|
|
521
|
+
latestEventType: stateInstance.event.type,
|
|
522
|
+
updatedAt: /* @__PURE__ */ new Date(),
|
|
523
|
+
expiresAt,
|
|
524
|
+
createdAt: existing.createdAt ?? /* @__PURE__ */ new Date(),
|
|
525
|
+
completedAt: existing.completedAt ?? (stateInstance.done ? /* @__PURE__ */ new Date() : void 0)
|
|
526
|
+
};
|
|
527
|
+
}, "machineStateToStoreInfo");
|
|
528
|
+
var storeInfoToMachineInit = /* @__PURE__ */ __name((args) => {
|
|
529
|
+
const { instanceId, machineName, tenantId, expiresAt, createdAt, stateType, machineState } = args;
|
|
530
|
+
return {
|
|
531
|
+
stateType,
|
|
532
|
+
machineName,
|
|
533
|
+
tenantId,
|
|
534
|
+
expiresAt,
|
|
535
|
+
instanceId,
|
|
536
|
+
createdAt,
|
|
537
|
+
machineState
|
|
538
|
+
};
|
|
539
|
+
}, "storeInfoToMachineInit");
|
|
540
|
+
var machineStateToMachineInit = /* @__PURE__ */ __name((machineInfo, existingState) => {
|
|
541
|
+
return storeInfoToMachineInit({
|
|
542
|
+
...machineStateToStoreInfo(machineInfo, existingState),
|
|
543
|
+
stateType: "existing",
|
|
544
|
+
machineState: machineInfo.machineState
|
|
545
|
+
});
|
|
546
|
+
}, "machineStateToMachineInit");
|
|
547
|
+
var serializeMachineState = /* @__PURE__ */ __name((state) => {
|
|
548
|
+
if (typeof state === "string") {
|
|
549
|
+
return state;
|
|
550
|
+
}
|
|
551
|
+
const jsonState = "toJSON" in state ? state.toJSON() : state;
|
|
552
|
+
return JSON.stringify(jsonState);
|
|
553
|
+
}, "serializeMachineState");
|
|
554
|
+
var deserializeMachineState = /* @__PURE__ */ __name((state) => {
|
|
555
|
+
return State.create(JSON.parse(state));
|
|
556
|
+
}, "deserializeMachineState");
|
|
557
|
+
|
|
558
|
+
// src/functions/machineRegistration.ts
|
|
559
|
+
var machineStatePersistInit = /* @__PURE__ */ __name(async (opts) => {
|
|
560
|
+
const { context, ...args } = opts;
|
|
561
|
+
if (!(context.agent.availableMethods().includes("machineStateInit") && "machineStateInit" in context.agent)) {
|
|
562
|
+
console.log(`IMachineStatePersistence was not exposed in the current agent. Not initializing new persistence object`);
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
return await context.agent.machineStateInit(args);
|
|
566
|
+
}, "machineStatePersistInit");
|
|
567
|
+
var machineStatePersistOnTransition = /* @__PURE__ */ __name(async (opts) => {
|
|
568
|
+
const { cleanupOnFinalState, context, init, interpreter } = opts;
|
|
569
|
+
const { machineState, ...initEventData } = init;
|
|
570
|
+
if (!(context.agent.availableMethods().includes("machineStatePersist") && "machineStatePersist" in context.agent)) {
|
|
571
|
+
console.log(`IMachineStatePersistence was not exposed in the current agent. Disabling machine state persistence events`);
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
let _eventCounter = init.machineState?.updatedCount ?? 0;
|
|
575
|
+
interpreter.onChange(async (_machineContext) => {
|
|
576
|
+
emitMachineStatePersistEvent({
|
|
577
|
+
type: MachineStatePersistEventType.EVERY,
|
|
578
|
+
data: {
|
|
579
|
+
...initEventData,
|
|
580
|
+
state: interpreter.getSnapshot(),
|
|
581
|
+
_eventCounter: ++_eventCounter,
|
|
582
|
+
_eventDate: /* @__PURE__ */ new Date(),
|
|
583
|
+
_cleanupOnFinalState: cleanupOnFinalState !== false
|
|
584
|
+
}
|
|
585
|
+
}, context);
|
|
586
|
+
});
|
|
587
|
+
if (cleanupOnFinalState && context.agent.availableMethods().includes("machineStateDelete")) {
|
|
588
|
+
interpreter.onDone((doneEvent) => {
|
|
589
|
+
;
|
|
590
|
+
context.agent.machineStateDelete({
|
|
591
|
+
tenantId: initEventData.tenantId,
|
|
592
|
+
instanceId: initEventData.instanceId
|
|
593
|
+
});
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
}, "machineStatePersistOnTransition");
|
|
597
|
+
var machineStatePersistRegistration = /* @__PURE__ */ __name(async (args) => {
|
|
598
|
+
const { disablePersistence } = args;
|
|
599
|
+
if (disablePersistence === true) {
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
const expiresAt = args.expireInMS ? new Date(Date.now() + args.expireInMS) : args.expiresAt;
|
|
603
|
+
const machineName = args.machineName ?? args.interpreter.machine.id ?? args.interpreter.id;
|
|
604
|
+
const init = await machineStatePersistInit({
|
|
605
|
+
...args,
|
|
606
|
+
machineName,
|
|
607
|
+
expiresAt
|
|
608
|
+
});
|
|
609
|
+
if (init) {
|
|
610
|
+
await machineStatePersistOnTransition({
|
|
611
|
+
...args,
|
|
612
|
+
init
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
return init;
|
|
616
|
+
}, "machineStatePersistRegistration");
|
|
617
|
+
var assertNonExpired = /* @__PURE__ */ __name((args) => {
|
|
618
|
+
const { expiresAt, machineName } = args;
|
|
619
|
+
if (expiresAt && expiresAt.getTime() < Date.now()) {
|
|
620
|
+
throw new Error(`Cannot resume ${machineName}. It expired at ${expiresAt.toLocaleString()}`);
|
|
621
|
+
}
|
|
622
|
+
}, "assertNonExpired");
|
|
623
|
+
var interpreterResumeFromState = /* @__PURE__ */ __name(async (args) => {
|
|
624
|
+
const { interpreter, machineState, context, noRegistration, cleanupAllOtherInstances, cleanupOnFinalState } = args;
|
|
625
|
+
const { machineName, instanceId, tenantId } = machineState;
|
|
626
|
+
assertNonExpired(machineState);
|
|
627
|
+
if (noRegistration !== true) {
|
|
628
|
+
await machineStatePersistRegistration({
|
|
629
|
+
stateType: "existing",
|
|
630
|
+
machineName,
|
|
631
|
+
tenantId,
|
|
632
|
+
existingInstanceId: instanceId,
|
|
633
|
+
cleanupAllOtherInstances,
|
|
634
|
+
cleanupOnFinalState,
|
|
635
|
+
context,
|
|
636
|
+
interpreter
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
const state = State2.from(machineState.state.value, machineState.state.context);
|
|
640
|
+
interpreter.start(state);
|
|
641
|
+
await waitFor(interpreter, (awaitState) => awaitState.matches(state.value));
|
|
642
|
+
return {
|
|
643
|
+
machineState,
|
|
644
|
+
init: machineStateToMachineInit({
|
|
645
|
+
...machineState,
|
|
646
|
+
stateType: "existing"
|
|
647
|
+
}, machineStateToStoreInfo({
|
|
648
|
+
...machineState,
|
|
649
|
+
stateType: "existing"
|
|
650
|
+
})),
|
|
651
|
+
interpreter
|
|
652
|
+
};
|
|
653
|
+
}, "interpreterResumeFromState");
|
|
654
|
+
var interpreterStartOrResumeFromInit = /* @__PURE__ */ __name(async (args) => {
|
|
655
|
+
const { init, noRegistration, interpreter, cleanupOnFinalState, cleanupAllOtherInstances, context } = args;
|
|
656
|
+
const { stateType, instanceId, machineName, tenantId, expiresAt } = init;
|
|
657
|
+
if (init.machineName !== interpreter.id) {
|
|
658
|
+
throw new Error(`Machine state init machine name ${init.machineName} does not match name from state machine interpreter ${interpreter.id}`);
|
|
659
|
+
}
|
|
660
|
+
assertNonExpired({
|
|
661
|
+
machineName,
|
|
662
|
+
expiresAt
|
|
663
|
+
});
|
|
664
|
+
if (noRegistration !== true) {
|
|
665
|
+
await machineStatePersistRegistration({
|
|
666
|
+
stateType: stateType ?? "existing",
|
|
667
|
+
machineName,
|
|
668
|
+
tenantId,
|
|
669
|
+
...stateType === "existing" && {
|
|
670
|
+
existingInstanceId: instanceId
|
|
671
|
+
},
|
|
672
|
+
...stateType === "new" && {
|
|
673
|
+
customInstanceId: instanceId
|
|
674
|
+
},
|
|
675
|
+
cleanupAllOtherInstances,
|
|
676
|
+
cleanupOnFinalState,
|
|
677
|
+
context,
|
|
678
|
+
interpreter
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
let machineState;
|
|
682
|
+
if (stateType === "new") {
|
|
683
|
+
interpreter.start();
|
|
684
|
+
} else {
|
|
685
|
+
machineState = await context.agent.machineStateGet({
|
|
686
|
+
tenantId,
|
|
687
|
+
instanceId
|
|
688
|
+
});
|
|
689
|
+
interpreter.start(machineState.state);
|
|
690
|
+
}
|
|
691
|
+
await new Promise((res) => setTimeout(res, 50));
|
|
692
|
+
return {
|
|
693
|
+
interpreter,
|
|
694
|
+
machineState,
|
|
695
|
+
init
|
|
696
|
+
};
|
|
697
|
+
}, "interpreterStartOrResumeFromInit");
|
|
698
|
+
var interpreterStartOrResume = /* @__PURE__ */ __name(async (args) => {
|
|
699
|
+
const { stateType, singletonCheck, instanceId, tenantId, noRegistration, context, interpreter, cleanupAllOtherInstances, cleanupOnFinalState } = args;
|
|
700
|
+
const machineName = args.machineName ?? interpreter.id;
|
|
701
|
+
let activeStates = await context.agent.machineStatesFindActive({
|
|
702
|
+
machineName,
|
|
703
|
+
tenantId,
|
|
704
|
+
instanceId
|
|
705
|
+
});
|
|
706
|
+
if (stateType === "new" && activeStates.length > 0 && cleanupAllOtherInstances) {
|
|
707
|
+
await Promise.all(activeStates.map((state) => context.agent.machineStateDelete({
|
|
708
|
+
tenantId: args.tenantId,
|
|
709
|
+
instanceId: state.instanceId
|
|
710
|
+
})));
|
|
711
|
+
activeStates = await context.agent.machineStatesFindActive({
|
|
712
|
+
machineName,
|
|
713
|
+
tenantId,
|
|
714
|
+
instanceId
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
if (singletonCheck && activeStates.length > 0) {
|
|
718
|
+
if (stateType === "new" || stateType === "existing" && (!instanceId && activeStates.length > 1 || instanceId && activeStates.every((state) => state.instanceId !== instanceId))) {
|
|
719
|
+
return Promise.reject(new Error(`Found ${activeStates.length} active '${machineName}' instances, but only one is allows at the same time`));
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
if (stateType === "new") {
|
|
723
|
+
if (instanceId && activeStates.length > 0) {
|
|
724
|
+
return Promise.reject(new Error(`Found an active '${machineName}' instance with id ${instanceId}, but a new instance was requested with the same id`));
|
|
725
|
+
}
|
|
726
|
+
const init = await context.agent.machineStateInit({
|
|
727
|
+
stateType: "new",
|
|
728
|
+
customInstanceId: instanceId,
|
|
729
|
+
machineName: machineName ?? interpreter.id,
|
|
730
|
+
tenantId,
|
|
731
|
+
cleanupAllOtherInstances
|
|
732
|
+
});
|
|
733
|
+
return await interpreterStartOrResumeFromInit({
|
|
734
|
+
init,
|
|
735
|
+
noRegistration,
|
|
736
|
+
interpreter,
|
|
737
|
+
context,
|
|
738
|
+
cleanupOnFinalState,
|
|
739
|
+
cleanupAllOtherInstances
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
if (activeStates.length === 0) {
|
|
743
|
+
if (stateType === "existing") {
|
|
744
|
+
return Promise.reject(new Error(`Could not find existing state machine ${machineName}, instanceId ${instanceId}`));
|
|
745
|
+
}
|
|
746
|
+
const init = await context.agent.machineStateInit({
|
|
747
|
+
stateType: "new",
|
|
748
|
+
customInstanceId: instanceId,
|
|
749
|
+
machineName: machineName ?? interpreter.id,
|
|
750
|
+
tenantId,
|
|
751
|
+
cleanupAllOtherInstances
|
|
752
|
+
});
|
|
753
|
+
return await interpreterStartOrResumeFromInit({
|
|
754
|
+
init,
|
|
755
|
+
noRegistration,
|
|
756
|
+
interpreter,
|
|
757
|
+
context,
|
|
758
|
+
cleanupOnFinalState,
|
|
759
|
+
cleanupAllOtherInstances
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
const activeState = activeStates[0];
|
|
763
|
+
return interpreterResumeFromState({
|
|
764
|
+
machineState: activeState,
|
|
765
|
+
noRegistration,
|
|
766
|
+
interpreter,
|
|
767
|
+
context,
|
|
768
|
+
cleanupOnFinalState,
|
|
769
|
+
cleanupAllOtherInstances
|
|
770
|
+
});
|
|
771
|
+
}, "interpreterStartOrResume");
|
|
772
|
+
|
|
773
|
+
// src/agent/MachineStatePersistence.ts
|
|
774
|
+
var debug2 = Debug2("sphereon:ssi-sdk:machine-state:xstate-persistence");
|
|
775
|
+
var MachineStatePersistence = class {
|
|
776
|
+
static {
|
|
777
|
+
__name(this, "MachineStatePersistence");
|
|
778
|
+
}
|
|
779
|
+
schema = schema.IMachineStatePersistence;
|
|
780
|
+
methods;
|
|
781
|
+
eventTypes;
|
|
782
|
+
_store;
|
|
783
|
+
get store() {
|
|
784
|
+
if (!this._store) {
|
|
785
|
+
throw Error("No store available in options");
|
|
786
|
+
}
|
|
787
|
+
return this._store;
|
|
788
|
+
}
|
|
789
|
+
constructor(opts) {
|
|
790
|
+
const { store, eventTypes, isRESTClient } = opts;
|
|
791
|
+
this.eventTypes = eventTypes;
|
|
792
|
+
this.methods = {
|
|
793
|
+
machineStatesFindActive: this.machineStatesFindActive.bind(this),
|
|
794
|
+
machineStatesDeleteExpired: this.machineStatesDeleteExpired.bind(this),
|
|
795
|
+
machineStateInit: this.machineStateInit.bind(this),
|
|
796
|
+
machineStatePersist: this.machineStatePersist.bind(this),
|
|
797
|
+
machineStateGet: this.machineStateGet.bind(this),
|
|
798
|
+
machineStateDelete: this.machineStateDelete.bind(this)
|
|
799
|
+
};
|
|
800
|
+
this._store = store;
|
|
801
|
+
if (isRESTClient) {
|
|
802
|
+
return;
|
|
803
|
+
} else if (!store) {
|
|
804
|
+
throw Error("No store available in options");
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
async onEvent(event, context) {
|
|
808
|
+
debug2(`Received machine state persistence event '${event.type}' counter: ${event.data._eventCounter}}`);
|
|
809
|
+
if (!this.eventTypes.includes(event.type)) {
|
|
810
|
+
console.log(`event type ${event.type} not registered for agent. Registered: ${JSON.stringify(this.eventTypes)}`);
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
switch (event.type) {
|
|
814
|
+
case MachineStatePersistEventType.INIT:
|
|
815
|
+
await context.agent.machineStateInit({
|
|
816
|
+
...event.data
|
|
817
|
+
});
|
|
818
|
+
break;
|
|
819
|
+
case MachineStatePersistEventType.EVERY:
|
|
820
|
+
await context.agent.machineStatePersist({
|
|
821
|
+
...event.data,
|
|
822
|
+
cleanupOnFinalState: event.data.cleanupOnFinalState ?? event.data._cleanupOnFinalState,
|
|
823
|
+
updatedCount: event.data._eventCounter ?? event.data.updatedCount
|
|
824
|
+
});
|
|
825
|
+
break;
|
|
826
|
+
default:
|
|
827
|
+
return Promise.reject(Error(`Event type ${event.type} not supported`));
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
async machineStateInit(args, context) {
|
|
831
|
+
const { tenantId, machineName, expiresAt, customInstanceId, existingInstanceId, cleanupAllOtherInstances } = args;
|
|
832
|
+
debug2(`machineStateInit for machine name ${machineName}, tenant ${tenantId}, custom instance ${customInstanceId}, existing id ${existingInstanceId}`);
|
|
833
|
+
if (customInstanceId && existingInstanceId) {
|
|
834
|
+
return Promise.reject(new Error(`Cannot have both a custom and existing instance id at the same time`));
|
|
835
|
+
}
|
|
836
|
+
if (cleanupAllOtherInstances) {
|
|
837
|
+
await context.agent.machineStatesDeleteExpired({
|
|
838
|
+
machineName,
|
|
839
|
+
tenantId,
|
|
840
|
+
deleteDoneStates: true
|
|
841
|
+
});
|
|
842
|
+
await context.agent.machineStatesDeleteExpired({
|
|
843
|
+
machineName,
|
|
844
|
+
tenantId,
|
|
845
|
+
deleteDoneStates: false
|
|
846
|
+
});
|
|
847
|
+
const activeMachineStates = (await context.agent.machineStatesFindActive({
|
|
848
|
+
machineName,
|
|
849
|
+
tenantId
|
|
850
|
+
})).filter((state) => !existingInstanceId || state.instanceId !== existingInstanceId);
|
|
851
|
+
await Promise.all(activeMachineStates.map((state) => context.agent.machineStateDelete({
|
|
852
|
+
instanceId: state.instanceId,
|
|
853
|
+
tenantId
|
|
854
|
+
})));
|
|
855
|
+
}
|
|
856
|
+
let machineInit = void 0;
|
|
857
|
+
let machineState;
|
|
858
|
+
if (existingInstanceId) {
|
|
859
|
+
debug2(`machineStateInit is using a previously persisted instance id (${existingInstanceId})`);
|
|
860
|
+
machineState = await context.agent.machineStateGet({
|
|
861
|
+
tenantId,
|
|
862
|
+
instanceId: existingInstanceId
|
|
863
|
+
});
|
|
864
|
+
machineInit = machineStateToMachineInit({
|
|
865
|
+
...machineState,
|
|
866
|
+
machineState,
|
|
867
|
+
stateType: "existing"
|
|
868
|
+
}, {
|
|
869
|
+
...machineState,
|
|
870
|
+
state: serializeMachineState(machineState.state)
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
if (customInstanceId) {
|
|
874
|
+
debug2(`machineStateInit is using a custom instance id (${customInstanceId})`);
|
|
875
|
+
}
|
|
876
|
+
if (!machineInit) {
|
|
877
|
+
machineInit = {
|
|
878
|
+
machineName,
|
|
879
|
+
tenantId,
|
|
880
|
+
expiresAt,
|
|
881
|
+
instanceId: customInstanceId ?? uuidv4(),
|
|
882
|
+
createdAt: args.createdAt ?? /* @__PURE__ */ new Date(),
|
|
883
|
+
stateType: "new"
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
return machineInit;
|
|
887
|
+
}
|
|
888
|
+
async machineStatePersist(args, context) {
|
|
889
|
+
const { instanceId, tenantId, machineName, updatedCount } = args;
|
|
890
|
+
const cleanupOnFinalState = args.cleanupOnFinalState !== false;
|
|
891
|
+
debug2(`machineStatePersist for machine name ${machineName}, updateCount: ${updatedCount}, instance ${instanceId} and tenant ${tenantId}...`);
|
|
892
|
+
try {
|
|
893
|
+
const queriedStates = await this.store.findMachineStates({
|
|
894
|
+
filter: [
|
|
895
|
+
{
|
|
896
|
+
instanceId,
|
|
897
|
+
tenantId
|
|
898
|
+
}
|
|
899
|
+
]
|
|
900
|
+
});
|
|
901
|
+
const existingState = queriedStates.length === 1 ? queriedStates[0] : void 0;
|
|
902
|
+
const storeInfoArgs = machineStateToStoreInfo(args, existingState);
|
|
903
|
+
let storedState;
|
|
904
|
+
if (updatedCount !== void 0 && updatedCount > 1 && storeInfoArgs.latestEventType === "xstate.init") {
|
|
905
|
+
console.log(`Not persisting machine state for resumed init event for machine ${machineName}, tenant ${tenantId} and state with id ${instanceId}`);
|
|
906
|
+
storedState = storeInfoArgs;
|
|
907
|
+
} else {
|
|
908
|
+
storedState = await this.store.persistMachineState(storeInfoArgs);
|
|
909
|
+
}
|
|
910
|
+
const machineStateInfo = {
|
|
911
|
+
...storedState,
|
|
912
|
+
state: deserializeMachineState(storedState.state)
|
|
913
|
+
};
|
|
914
|
+
debug2(`machineStatePersist success for machine name ${machineName}, instance ${instanceId}, update count ${machineStateInfo.updatedCount}, tenant ${tenantId}, last event: ${machineStateInfo.latestEventType}, last state: ${machineStateInfo.latestStateName}`);
|
|
915
|
+
if (cleanupOnFinalState && machineStateInfo.state.done) {
|
|
916
|
+
debug2(`reached final state for machine ${machineName} instance ${instanceId} and auto cleanup was enabled. Deleting machine state`);
|
|
917
|
+
await context.agent.machineStateDelete(machineStateInfo);
|
|
918
|
+
}
|
|
919
|
+
return machineStateInfo;
|
|
920
|
+
} catch (error) {
|
|
921
|
+
console.log(error);
|
|
922
|
+
return Promise.reject(error);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
async machineStatesFindActive(args) {
|
|
926
|
+
const { machineName, tenantId } = args;
|
|
927
|
+
debug2(`machineStateFindActive for machine name ${machineName} and tenant ${tenantId}...`);
|
|
928
|
+
const storedStates = await this.store.findActiveMachineStates(args);
|
|
929
|
+
const machineStateInfos = storedStates.map((storedState) => {
|
|
930
|
+
return {
|
|
931
|
+
...storedState,
|
|
932
|
+
state: deserializeMachineState(storedState.state)
|
|
933
|
+
};
|
|
934
|
+
});
|
|
935
|
+
debug2(`machineStateFindActive returned ${machineStateInfos.length} results for machine name ${machineName} and tenant ${tenantId}`);
|
|
936
|
+
return machineStateInfos;
|
|
937
|
+
}
|
|
938
|
+
async machineStatesDeleteExpired(args) {
|
|
939
|
+
const { machineName, tenantId } = args;
|
|
940
|
+
debug2(`machineStatesDeleteExpired for machine name ${machineName} and tenant ${tenantId}...`);
|
|
941
|
+
const deleteResult = await this.store.deleteExpiredMachineStates(args);
|
|
942
|
+
debug2(`machineStatesDeleteExpired result for machine name ${machineName} and tenant ${tenantId}: ${deleteResult}`);
|
|
943
|
+
return deleteResult;
|
|
944
|
+
}
|
|
945
|
+
async machineStateGet(args, context) {
|
|
946
|
+
const { instanceId, tenantId } = args;
|
|
947
|
+
debug2(`machineStateGet for machine instance ${instanceId} and tenant ${tenantId}...`);
|
|
948
|
+
const storedState = await this.store.getMachineState(args);
|
|
949
|
+
const machineInfo = {
|
|
950
|
+
...storedState,
|
|
951
|
+
state: deserializeMachineState(storedState.state)
|
|
952
|
+
};
|
|
953
|
+
debug2(`machineStateGet success for machine instance ${instanceId} and tenant ${tenantId}`);
|
|
954
|
+
return machineInfo;
|
|
955
|
+
}
|
|
956
|
+
async machineStateDelete(args, context) {
|
|
957
|
+
const { instanceId, tenantId } = args;
|
|
958
|
+
debug2(`machineStateDelete for machine instance ${instanceId} and tenant ${tenantId}...`);
|
|
959
|
+
const deleteResult = await this.store.deleteMachineState(args);
|
|
960
|
+
debug2(`machineStateDelete result for machine instance ${instanceId} and tenant ${tenantId}: ${deleteResult}`);
|
|
961
|
+
return deleteResult;
|
|
962
|
+
}
|
|
963
|
+
};
|
|
964
|
+
|
|
965
|
+
// src/index.ts
|
|
966
|
+
var schema = require_plugin_schema();
|
|
967
|
+
export {
|
|
968
|
+
MachineStatePersistEventType,
|
|
969
|
+
MachineStatePersistence,
|
|
970
|
+
deserializeMachineState,
|
|
971
|
+
emitMachineStatePersistEvent,
|
|
972
|
+
interpreterResumeFromState,
|
|
973
|
+
interpreterStartOrResume,
|
|
974
|
+
interpreterStartOrResumeFromInit,
|
|
975
|
+
machineStatePersistInit,
|
|
976
|
+
machineStatePersistOnTransition,
|
|
977
|
+
machineStatePersistRegistration,
|
|
978
|
+
machineStateToMachineInit,
|
|
979
|
+
machineStateToStoreInfo,
|
|
980
|
+
schema,
|
|
981
|
+
serializeMachineState,
|
|
982
|
+
storeInfoToMachineInit
|
|
15
983
|
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.schema = void 0;
|
|
18
|
-
const schema = require('../plugin.schema.json');
|
|
19
|
-
exports.schema = schema;
|
|
20
|
-
__exportStar(require("./agent/MachineStatePersistence"), exports);
|
|
21
|
-
__exportStar(require("./types"), exports);
|
|
22
|
-
__exportStar(require("./functions"), exports);
|
|
23
984
|
//# sourceMappingURL=index.js.map
|