@valerypopoff/rivet2-core 2.1.3 → 2.1.5
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/cjs/bundle.cjs +10138 -5679
- package/dist/cjs/bundle.cjs.map +4 -4
- package/dist/cjs/webAppRuntime.cjs +1214 -0
- package/dist/cjs/webAppRuntime.cjs.map +7 -0
- package/dist/esm/api/createProcessor.js +8 -1
- package/dist/esm/api/streaming.js +6 -0
- package/dist/esm/exports.js +8 -0
- package/dist/esm/integrations/mcp/MCPBase.js +92 -34
- package/dist/esm/model/GraphBoundaryEffects.js +13 -0
- package/dist/esm/model/GraphProcessor.js +1138 -86
- package/dist/esm/model/GraphProgress.js +15 -0
- package/dist/esm/model/ManagedAsyncBranches.js +28 -0
- package/dist/esm/model/Nodes.js +12 -0
- package/dist/esm/model/ProcessContextBuilder.js +3 -1
- package/dist/esm/model/RecordingPlayer.js +19 -0
- package/dist/esm/model/StoredValueStore.js +226 -0
- package/dist/esm/model/SubprocessorBridge.js +50 -18
- package/dist/esm/model/ToolCallContinuation.js +1 -0
- package/dist/esm/model/UiGraph.js +177 -4
- package/dist/esm/model/UiGraphActionProtocol.js +131 -0
- package/dist/esm/model/UiGraphBindings.js +530 -0
- package/dist/esm/model/UiGraphBrowserRuntime.js +373 -0
- package/dist/esm/model/UiGraphNormalization.js +148 -0
- package/dist/esm/model/UiGraphRendererStyles.js +1108 -42
- package/dist/esm/model/UiGraphRuntimeModel.js +518 -2
- package/dist/esm/model/UiGraphSchema.js +91 -0
- package/dist/esm/model/UiGraphWebAppStatus.js +19 -0
- package/dist/esm/model/UiGraphWebAppStorage.js +1 -0
- package/dist/esm/model/chat/chatMessages.js +5 -9
- package/dist/esm/model/chat-v2/chatV2Errors.js +36 -3
- package/dist/esm/model/chat-v2/index.js +1 -0
- package/dist/esm/model/chat-v2/toolContinuation.js +6 -8
- package/dist/esm/model/chat-v2/toolContinuationConnection.js +68 -0
- package/dist/esm/model/nodes/ArrayNode.js +2 -13
- package/dist/esm/model/nodes/AssembleMessageNode.js +2 -13
- package/dist/esm/model/nodes/AssemblePromptNode.js +2 -13
- package/dist/esm/model/nodes/CoalesceNode.js +2 -15
- package/dist/esm/model/nodes/DelayNode.js +3 -14
- package/dist/esm/model/nodes/DelegateFunctionCallNode.js +12 -40
- package/dist/esm/model/nodes/DidRunNode.js +2 -13
- package/dist/esm/model/nodes/GetStoredValueNode.js +109 -0
- package/dist/esm/model/nodes/JoinNode.js +2 -13
- package/dist/esm/model/nodes/LLMChatV2Node.js +17 -1
- package/dist/esm/model/nodes/LoopControllerNode.js +3 -14
- package/dist/esm/model/nodes/MCPDiscoveryNode.js +19 -90
- package/dist/esm/model/nodes/MCPGetPromptNode.js +11 -97
- package/dist/esm/model/nodes/MCPToolCallNode.js +11 -97
- package/dist/esm/model/nodes/MatchNode.js +7 -7
- package/dist/esm/model/nodes/PassthroughNode.js +3 -14
- package/dist/esm/model/nodes/RaceInputsNode.js +2 -13
- package/dist/esm/model/nodes/ReportProgressNode.js +57 -0
- package/dist/esm/model/nodes/SetStoredValueNode.js +84 -0
- package/dist/esm/model/nodes/StartBackgroundBranchNode.js +59 -0
- package/dist/esm/model/nodes/StoredValueNodeUtils.js +47 -0
- package/dist/esm/model/nodes/toolCallDelegation.js +44 -1
- package/dist/esm/model/nodes/variadicPortIndex.js +32 -0
- package/dist/esm/plugins/anthropic/anthropic.js +1 -1
- package/dist/esm/recording/ExecutionRecorder.js +6 -0
- package/dist/esm/utils/coerceType.js +3 -0
- package/dist/esm/utils/fetchEventSource.js +16 -17
- package/dist/esm/utils/serialization/serialization.js +4 -0
- package/dist/esm/utils/serialization/serializationUtils.js +8 -27
- package/dist/esm/utils/serialization/serialization_v4.js +3 -3
- package/dist/esm/webAppRuntime.js +4 -2
- package/dist/types/api/createProcessor.d.ts +4 -0
- package/dist/types/api/streaming.d.ts +6 -0
- package/dist/types/exports.d.ts +8 -0
- package/dist/types/integrations/mcp/MCPBase.d.ts +26 -3
- package/dist/types/model/EditorDefinition.d.ts +2 -1
- package/dist/types/model/ExecutorProtocol.d.ts +19 -0
- package/dist/types/model/GraphBoundaryEffects.d.ts +9 -1
- package/dist/types/model/GraphProcessor.d.ts +26 -2
- package/dist/types/model/GraphProgress.d.ts +6 -0
- package/dist/types/model/ManagedAsyncBranches.d.ts +6 -0
- package/dist/types/model/Nodes.d.ts +6 -2
- package/dist/types/model/ProcessContext.d.ts +18 -0
- package/dist/types/model/ProcessContextBuilder.d.ts +3 -1
- package/dist/types/model/StoredValueStore.d.ts +55 -0
- package/dist/types/model/SubprocessorBridge.d.ts +4 -2
- package/dist/types/model/ToolCallContinuation.d.ts +13 -0
- package/dist/types/model/UiGraph.d.ts +63 -9
- package/dist/types/model/UiGraphActionProtocol.d.ts +71 -0
- package/dist/types/model/UiGraphBindings.d.ts +45 -0
- package/dist/types/model/UiGraphBrowserRuntime.d.ts +55 -0
- package/dist/types/model/UiGraphNormalization.d.ts +24 -0
- package/dist/types/model/UiGraphRendererStyles.d.ts +2 -2
- package/dist/types/model/UiGraphRuntimeModel.d.ts +97 -6
- package/dist/types/model/UiGraphSchema.d.ts +113 -0
- package/dist/types/model/UiGraphWebAppStatus.d.ts +5 -0
- package/dist/types/model/UiGraphWebAppStorage.d.ts +3 -0
- package/dist/types/model/chat-v2/index.d.ts +1 -0
- package/dist/types/model/chat-v2/toolContinuation.d.ts +1 -0
- package/dist/types/model/chat-v2/toolContinuationConnection.d.ts +28 -0
- package/dist/types/model/nodes/ArrayNode.d.ts +0 -1
- package/dist/types/model/nodes/AssembleMessageNode.d.ts +0 -1
- package/dist/types/model/nodes/AssemblePromptNode.d.ts +0 -1
- package/dist/types/model/nodes/DelayNode.d.ts +0 -1
- package/dist/types/model/nodes/DidRunNode.d.ts +0 -1
- package/dist/types/model/nodes/GetStoredValueNode.d.ts +25 -0
- package/dist/types/model/nodes/JoinNode.d.ts +0 -1
- package/dist/types/model/nodes/LoopControllerNode.d.ts +0 -1
- package/dist/types/model/nodes/PassthroughNode.d.ts +0 -1
- package/dist/types/model/nodes/RaceInputsNode.d.ts +0 -1
- package/dist/types/model/nodes/ReportProgressNode.d.ts +23 -0
- package/dist/types/model/nodes/SetStoredValueNode.d.ts +23 -0
- package/dist/types/model/nodes/StartBackgroundBranchNode.d.ts +14 -0
- package/dist/types/model/nodes/StoredValueNodeUtils.d.ts +5 -0
- package/dist/types/model/nodes/toolCallDelegation.d.ts +4 -0
- package/dist/types/model/nodes/variadicPortIndex.d.ts +4 -0
- package/dist/types/recording/RecordedEvents.d.ts +10 -0
- package/dist/types/webAppRuntime.d.ts +8 -3
- package/package.json +2 -2
- package/dist/esm/plugins/anthropic/fetchEventSource.js +0 -107
- package/dist/types/plugins/anthropic/fetchEventSource.d.ts +0 -11
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const MAX_PROGRESS_MESSAGE_LENGTH = 500;
|
|
2
|
+
/** Normalizes public graph progress before it crosses executor or web-app transports. */
|
|
3
|
+
export function normalizeGraphProgress(progress) {
|
|
4
|
+
const message = typeof progress.message === 'string' ? progress.message.trim().slice(0, MAX_PROGRESS_MESSAGE_LENGTH) : '';
|
|
5
|
+
const percent = typeof progress.percent === 'number' && Number.isFinite(progress.percent)
|
|
6
|
+
? Math.min(100, Math.max(0, progress.percent))
|
|
7
|
+
: undefined;
|
|
8
|
+
if (!message && percent == null) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
...(message ? { message } : {}),
|
|
13
|
+
...(percent == null ? {} : { percent }),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export class ManagedAsyncBranches {
|
|
2
|
+
#pending = new Set();
|
|
3
|
+
#tailByKey = new Map();
|
|
4
|
+
get hasPending() {
|
|
5
|
+
return this.#pending.size > 0;
|
|
6
|
+
}
|
|
7
|
+
enqueue(key, run, onError) {
|
|
8
|
+
const previous = this.#tailByKey.get(key) ?? Promise.resolve();
|
|
9
|
+
const tracked = previous
|
|
10
|
+
.then(run)
|
|
11
|
+
.catch((error) => {
|
|
12
|
+
onError(error);
|
|
13
|
+
})
|
|
14
|
+
.finally(() => {
|
|
15
|
+
this.#pending.delete(tracked);
|
|
16
|
+
if (this.#tailByKey.get(key) === tracked) {
|
|
17
|
+
this.#tailByKey.delete(key);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
this.#tailByKey.set(key, tracked);
|
|
21
|
+
this.#pending.add(tracked);
|
|
22
|
+
}
|
|
23
|
+
async drain() {
|
|
24
|
+
while (this.#pending.size > 0) {
|
|
25
|
+
await Promise.all([...this.#pending]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
package/dist/esm/model/Nodes.js
CHANGED
|
@@ -54,6 +54,8 @@ import { extractObjectPathNode } from './nodes/ExtractObjectPathNode.js';
|
|
|
54
54
|
export * from './nodes/ExtractObjectPathNode.js';
|
|
55
55
|
import { raiseEventNode } from './nodes/RaiseEventNode.js';
|
|
56
56
|
export * from './nodes/RaiseEventNode.js';
|
|
57
|
+
import { reportProgressNode } from './nodes/ReportProgressNode.js';
|
|
58
|
+
export * from './nodes/ReportProgressNode.js';
|
|
57
59
|
import { contextNode } from './nodes/ContextNode.js';
|
|
58
60
|
export * from './nodes/ContextNode.js';
|
|
59
61
|
import { coalesceNode } from './nodes/CoalesceNode.js';
|
|
@@ -62,12 +64,18 @@ import { didRunNode } from './nodes/DidRunNode.js';
|
|
|
62
64
|
export * from './nodes/DidRunNode.js';
|
|
63
65
|
import { passthroughNode } from './nodes/PassthroughNode.js';
|
|
64
66
|
export * from './nodes/PassthroughNode.js';
|
|
67
|
+
import { startBackgroundBranchNode } from './nodes/StartBackgroundBranchNode.js';
|
|
68
|
+
export * from './nodes/StartBackgroundBranchNode.js';
|
|
65
69
|
import { popNode } from './nodes/PopNode.js';
|
|
66
70
|
export * from './nodes/PopNode.js';
|
|
67
71
|
import { setGlobalNode } from './nodes/SetGlobalNode.js';
|
|
68
72
|
export * from './nodes/SetGlobalNode.js';
|
|
69
73
|
import { getGlobalNode } from './nodes/GetGlobalNode.js';
|
|
70
74
|
export * from './nodes/GetGlobalNode.js';
|
|
75
|
+
import { setStoredValueNode } from './nodes/SetStoredValueNode.js';
|
|
76
|
+
export * from './nodes/SetStoredValueNode.js';
|
|
77
|
+
import { getStoredValueNode } from './nodes/GetStoredValueNode.js';
|
|
78
|
+
export * from './nodes/GetStoredValueNode.js';
|
|
71
79
|
import { waitForEventNode } from './nodes/WaitForEventNode.js';
|
|
72
80
|
export * from './nodes/WaitForEventNode.js';
|
|
73
81
|
import { gptFunctionNode } from './nodes/ToolNode.js';
|
|
@@ -210,13 +218,17 @@ export const registerBuiltInNodes = (registry) => {
|
|
|
210
218
|
.register(externalCallNode)
|
|
211
219
|
.register(extractObjectPathNode)
|
|
212
220
|
.register(raiseEventNode)
|
|
221
|
+
.register(reportProgressNode)
|
|
213
222
|
.register(contextNode)
|
|
214
223
|
.register(coalesceNode)
|
|
215
224
|
.register(didRunNode)
|
|
216
225
|
.register(passthroughNode)
|
|
226
|
+
.register(startBackgroundBranchNode)
|
|
217
227
|
.register(popNode)
|
|
218
228
|
.register(setGlobalNode)
|
|
219
229
|
.register(getGlobalNode)
|
|
230
|
+
.register(setStoredValueNode)
|
|
231
|
+
.register(getStoredValueNode)
|
|
220
232
|
.register(waitForEventNode)
|
|
221
233
|
.register(gptFunctionNode)
|
|
222
234
|
.register(getEmbeddingNode)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export function buildNodeProcessContext(options) {
|
|
2
|
-
const { attachedData, activeOutputPortIds, base, createSubProcessor, execution, externalFunctions, getPluginConfig, isDirectRunTarget, node, nodeAbortController, onPartialOutputs, processId, requestUserInput, setGlobal, waitEvent, } = options;
|
|
2
|
+
const { attachedData, activeOutputPortIds, base, createSubProcessor, execution, externalFunctions, getPluginConfig, isDirectRunTarget, node, nodeAbortController, onPartialOutputs, processId, requestUserInput, reportProgress, setGlobal, toolCallContinuation, waitEvent, } = options;
|
|
3
3
|
return {
|
|
4
4
|
...base,
|
|
5
5
|
node,
|
|
@@ -15,6 +15,8 @@ export function buildNodeProcessContext(options) {
|
|
|
15
15
|
createSubProcessor: createSubProcessor,
|
|
16
16
|
getPluginConfig,
|
|
17
17
|
requestUserInput,
|
|
18
|
+
reportProgress,
|
|
19
|
+
toolCallContinuation,
|
|
18
20
|
execution,
|
|
19
21
|
};
|
|
20
22
|
}
|
|
@@ -136,6 +136,15 @@ export async function replayExecutionRecording(options) {
|
|
|
136
136
|
});
|
|
137
137
|
break;
|
|
138
138
|
}
|
|
139
|
+
case 'graphOutputsReady': {
|
|
140
|
+
const { data } = event;
|
|
141
|
+
emitDetached(emitter, 'graphOutputsReady', {
|
|
142
|
+
graph: getGraph(data.graphId),
|
|
143
|
+
outputs: data.outputs,
|
|
144
|
+
execution: getExecution(data.graphId, data.execution),
|
|
145
|
+
});
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
139
148
|
case 'graphError': {
|
|
140
149
|
const { data } = event;
|
|
141
150
|
emitDetached(emitter, 'graphError', {
|
|
@@ -238,6 +247,16 @@ export async function replayExecutionRecording(options) {
|
|
|
238
247
|
});
|
|
239
248
|
break;
|
|
240
249
|
}
|
|
250
|
+
case 'progress': {
|
|
251
|
+
const { data } = event;
|
|
252
|
+
emitDetached(emitter, 'progress', {
|
|
253
|
+
node: getNode(data.nodeId),
|
|
254
|
+
processId: data.processId,
|
|
255
|
+
progress: data.progress,
|
|
256
|
+
execution: getExecution(data.execution?.graphId ?? getGraphIdForNode(data.nodeId), data.execution),
|
|
257
|
+
});
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
241
260
|
case 'userInput': {
|
|
242
261
|
const { data } = event;
|
|
243
262
|
const node = getNode(data.nodeId);
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run-scoped read-through/write-through cache used by Stored Value nodes.
|
|
3
|
+
* A GraphProcessor creates one controller per root run and shares it with every subprocessor.
|
|
4
|
+
*/
|
|
5
|
+
export class RivetStoredValueController {
|
|
6
|
+
store;
|
|
7
|
+
#cache = new Map();
|
|
8
|
+
#keyOperations = new Map();
|
|
9
|
+
#loadErrors = new Map();
|
|
10
|
+
#waiters = new Map();
|
|
11
|
+
constructor(store) {
|
|
12
|
+
this.store = store;
|
|
13
|
+
}
|
|
14
|
+
async get(key) {
|
|
15
|
+
const normalizedKey = normalizeRivetStoredValueKey(key);
|
|
16
|
+
return await this.#withKeyLock(normalizedKey, async () => cloneReadResult(await this.#getUnlocked(normalizedKey)));
|
|
17
|
+
}
|
|
18
|
+
getCached(key) {
|
|
19
|
+
const normalizedKey = normalizeRivetStoredValueKey(key);
|
|
20
|
+
const cached = this.#cache.get(normalizedKey);
|
|
21
|
+
if (!cached)
|
|
22
|
+
return { loaded: false, found: false };
|
|
23
|
+
return cached.found
|
|
24
|
+
? { loaded: true, found: true, value: cloneRivetStoredValue(cached.value, 'Stored value') }
|
|
25
|
+
: { loaded: true, found: false };
|
|
26
|
+
}
|
|
27
|
+
async set(key, value) {
|
|
28
|
+
const normalizedKey = normalizeRivetStoredValueKey(key);
|
|
29
|
+
const savedValue = cloneRivetStoredValue(value, 'Stored value');
|
|
30
|
+
return await this.#withKeyLock(normalizedKey, async () => {
|
|
31
|
+
const previous = await this.#getUnlocked(normalizedKey);
|
|
32
|
+
await this.store?.set(normalizedKey, cloneRivetStoredValue(savedValue, 'Stored value'));
|
|
33
|
+
this.#cache.set(normalizedKey, { found: true, value: savedValue });
|
|
34
|
+
this.#resolveWaiters(normalizedKey, savedValue);
|
|
35
|
+
return {
|
|
36
|
+
hadPreviousValue: previous.found,
|
|
37
|
+
previousValue: previous.found ? cloneRivetStoredValue(previous.value, 'Stored value') : undefined,
|
|
38
|
+
savedValue: cloneRivetStoredValue(savedValue, 'Stored value'),
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/** Seeds the run cache without writing to the backing store, used for frozen Set-node replay. */
|
|
43
|
+
async seed(key, value) {
|
|
44
|
+
const normalizedKey = normalizeRivetStoredValueKey(key);
|
|
45
|
+
const savedValue = cloneRivetStoredValue(value, 'Stored value');
|
|
46
|
+
await this.#withKeyLock(normalizedKey, () => {
|
|
47
|
+
this.#cache.set(normalizedKey, { found: true, value: savedValue });
|
|
48
|
+
this.#resolveWaiters(normalizedKey, savedValue);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
async waitForSet(key, signal) {
|
|
52
|
+
const normalizedKey = normalizeRivetStoredValueKey(key);
|
|
53
|
+
let immediateValue;
|
|
54
|
+
let waiterPromise;
|
|
55
|
+
signal?.throwIfAborted();
|
|
56
|
+
await this.#withKeyLock(normalizedKey, async () => {
|
|
57
|
+
signal?.throwIfAborted();
|
|
58
|
+
const current = await this.#getUnlocked(normalizedKey);
|
|
59
|
+
signal?.throwIfAborted();
|
|
60
|
+
if (current.found) {
|
|
61
|
+
immediateValue = cloneRivetStoredValue(current.value, 'Stored value');
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
waiterPromise = new Promise((resolve, reject) => {
|
|
65
|
+
const waiter = { resolve, reject };
|
|
66
|
+
const waiters = this.#waiters.get(normalizedKey) ?? new Set();
|
|
67
|
+
waiters.add(waiter);
|
|
68
|
+
this.#waiters.set(normalizedKey, waiters);
|
|
69
|
+
if (!signal)
|
|
70
|
+
return;
|
|
71
|
+
const onAbort = () => {
|
|
72
|
+
waiters.delete(waiter);
|
|
73
|
+
if (waiters.size === 0)
|
|
74
|
+
this.#waiters.delete(normalizedKey);
|
|
75
|
+
reject(signal.reason ?? new Error('Stored value wait was aborted.'));
|
|
76
|
+
};
|
|
77
|
+
if (signal.aborted) {
|
|
78
|
+
onAbort();
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
82
|
+
const originalResolve = waiter.resolve;
|
|
83
|
+
waiter.resolve = (resolvedValue) => {
|
|
84
|
+
signal.removeEventListener('abort', onAbort);
|
|
85
|
+
originalResolve(resolvedValue);
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
signal?.throwIfAborted();
|
|
91
|
+
if (immediateValue !== undefined)
|
|
92
|
+
return immediateValue;
|
|
93
|
+
return await waiterPromise;
|
|
94
|
+
}
|
|
95
|
+
async #getUnlocked(key) {
|
|
96
|
+
const cached = this.#cache.get(key);
|
|
97
|
+
if (cached)
|
|
98
|
+
return cached;
|
|
99
|
+
if (this.#loadErrors.has(key))
|
|
100
|
+
throw this.#loadErrors.get(key);
|
|
101
|
+
try {
|
|
102
|
+
const loaded = await this.store?.get(key);
|
|
103
|
+
const result = loaded === undefined
|
|
104
|
+
? { found: false }
|
|
105
|
+
: { found: true, value: cloneRivetStoredValue(loaded, 'Stored value returned by the store') };
|
|
106
|
+
this.#cache.set(key, result);
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
this.#loadErrors.set(key, error);
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
#resolveWaiters(key, value) {
|
|
115
|
+
const waiters = this.#waiters.get(key);
|
|
116
|
+
if (!waiters)
|
|
117
|
+
return;
|
|
118
|
+
this.#waiters.delete(key);
|
|
119
|
+
for (const waiter of waiters) {
|
|
120
|
+
waiter.resolve(cloneRivetStoredValue(value, 'Stored value'));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async #withKeyLock(key, operation) {
|
|
124
|
+
const previous = this.#keyOperations.get(key) ?? Promise.resolve();
|
|
125
|
+
let release;
|
|
126
|
+
const gate = new Promise((resolve) => {
|
|
127
|
+
release = resolve;
|
|
128
|
+
});
|
|
129
|
+
const current = previous.catch(() => undefined).then(() => gate);
|
|
130
|
+
this.#keyOperations.set(key, current);
|
|
131
|
+
await previous.catch(() => undefined);
|
|
132
|
+
try {
|
|
133
|
+
return await operation();
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
release();
|
|
137
|
+
if (this.#keyOperations.get(key) === current)
|
|
138
|
+
this.#keyOperations.delete(key);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
export function createRivetStoredValueSnapshotStore(initialValues = {}) {
|
|
143
|
+
const currentValues = cloneRivetStoredValueRecord(initialValues, 'Stored value snapshot');
|
|
144
|
+
const patch = {};
|
|
145
|
+
return {
|
|
146
|
+
store: {
|
|
147
|
+
get(key) {
|
|
148
|
+
const normalizedKey = normalizeRivetStoredValueKey(key);
|
|
149
|
+
return Object.prototype.hasOwnProperty.call(currentValues, normalizedKey)
|
|
150
|
+
? cloneRivetStoredValue(currentValues[normalizedKey], 'Stored value')
|
|
151
|
+
: undefined;
|
|
152
|
+
},
|
|
153
|
+
set(key, value) {
|
|
154
|
+
const normalizedKey = normalizeRivetStoredValueKey(key);
|
|
155
|
+
const cloned = cloneRivetStoredValue(value, 'Stored value');
|
|
156
|
+
setStoredValueRecordEntry(currentValues, normalizedKey, cloned);
|
|
157
|
+
setStoredValueRecordEntry(patch, normalizedKey, cloneRivetStoredValue(cloned, 'Stored value'));
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
getPatch: () => cloneRivetStoredValueRecord(patch, 'Stored value patch'),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
export function normalizeRivetStoredValueKey(value) {
|
|
164
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
165
|
+
throw new Error('Stored value key must be a non-empty string.');
|
|
166
|
+
}
|
|
167
|
+
return value;
|
|
168
|
+
}
|
|
169
|
+
function setStoredValueRecordEntry(record, key, value) {
|
|
170
|
+
// Defining an own data property keeps all non-empty JSON string keys safe, including __proto__.
|
|
171
|
+
Object.defineProperty(record, key, { configurable: true, enumerable: true, value, writable: true });
|
|
172
|
+
}
|
|
173
|
+
export function cloneRivetStoredValue(value, label = 'Stored value') {
|
|
174
|
+
return clonePortableValue(value, label, new Set());
|
|
175
|
+
}
|
|
176
|
+
export function cloneRivetStoredValueRecord(value, label = 'Stored value record') {
|
|
177
|
+
const cloned = cloneRivetStoredValue(value, label);
|
|
178
|
+
if (!isPlainRecord(cloned))
|
|
179
|
+
throw new Error(`${label} must be a JSON object.`);
|
|
180
|
+
for (const key of Object.keys(cloned))
|
|
181
|
+
normalizeRivetStoredValueKey(key);
|
|
182
|
+
return cloned;
|
|
183
|
+
}
|
|
184
|
+
function cloneReadResult(result) {
|
|
185
|
+
return result.found ? { found: true, value: cloneRivetStoredValue(result.value, 'Stored value') } : { found: false };
|
|
186
|
+
}
|
|
187
|
+
function clonePortableValue(value, label, ancestors) {
|
|
188
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean')
|
|
189
|
+
return value;
|
|
190
|
+
if (typeof value === 'number') {
|
|
191
|
+
if (!Number.isFinite(value))
|
|
192
|
+
throw new Error(`${label} must contain only finite numbers.`);
|
|
193
|
+
return value;
|
|
194
|
+
}
|
|
195
|
+
if (typeof value !== 'object') {
|
|
196
|
+
throw new Error(`${label} must contain only portable JSON values.`);
|
|
197
|
+
}
|
|
198
|
+
if (ancestors.has(value))
|
|
199
|
+
throw new Error(`${label} must not contain cycles.`);
|
|
200
|
+
ancestors.add(value);
|
|
201
|
+
try {
|
|
202
|
+
if (Array.isArray(value)) {
|
|
203
|
+
const cloned = [];
|
|
204
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
205
|
+
if (!Object.prototype.hasOwnProperty.call(value, index)) {
|
|
206
|
+
throw new Error(`${label} must not contain sparse arrays.`);
|
|
207
|
+
}
|
|
208
|
+
cloned.push(clonePortableValue(value[index], label, ancestors));
|
|
209
|
+
}
|
|
210
|
+
return cloned;
|
|
211
|
+
}
|
|
212
|
+
if (!isPlainRecord(value)) {
|
|
213
|
+
throw new Error(`${label} must contain only plain JSON objects and arrays.`);
|
|
214
|
+
}
|
|
215
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, clonePortableValue(item, label, ancestors)]));
|
|
216
|
+
}
|
|
217
|
+
finally {
|
|
218
|
+
ancestors.delete(value);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function isPlainRecord(value) {
|
|
222
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
223
|
+
return false;
|
|
224
|
+
const prototype = Object.getPrototypeOf(value);
|
|
225
|
+
return prototype === Object.prototype || prototype === null;
|
|
226
|
+
}
|
|
@@ -24,19 +24,22 @@ export function wireSubprocessorEvents(processor, parentEmitter, parentState) {
|
|
|
24
24
|
// graph-level terminal event. Keep passive forwarding alive for the
|
|
25
25
|
// subprocessor object lifetime so remote-debugger/recorder consumers do not
|
|
26
26
|
// miss those node terminals.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
27
|
+
const passiveUnsubscribers = [
|
|
28
|
+
processor.on('nodeError', (event) => parentEmitter.emit('nodeError', event)),
|
|
29
|
+
processor.on('nodeFinish', (event) => parentEmitter.emit('nodeFinish', event)),
|
|
30
|
+
processor.on('partialOutput', (event) => parentEmitter.emit('partialOutput', event)),
|
|
31
|
+
processor.on('progress', (event) => parentEmitter.emit('progress', event)),
|
|
32
|
+
processor.on('nodeExcluded', (event) => parentEmitter.emit('nodeExcluded', event)),
|
|
33
|
+
processor.on('nodeStart', (event) => parentEmitter.emit('nodeStart', event)),
|
|
34
|
+
processor.on('graphAbort', (event) => parentEmitter.emit('graphAbort', event)),
|
|
35
|
+
processor.on('graphError', (event) => parentEmitter.emit('graphError', event)),
|
|
36
|
+
processor.on('userInput', (event) => parentEmitter.emit('userInput', event)),
|
|
37
|
+
processor.on('graphStart', (event) => parentEmitter.emit('graphStart', event)),
|
|
38
|
+
processor.on('graphFinish', (event) => parentEmitter.emit('graphFinish', event)),
|
|
39
|
+
processor.on('nodeOutputsCleared', (event) => parentEmitter.emit('nodeOutputsCleared', event)),
|
|
40
|
+
processor.on('globalSet', (event) => parentEmitter.emit('globalSet', event)),
|
|
41
|
+
processor.on('newAbortController', (event) => parentEmitter.emit('newAbortController', event)),
|
|
42
|
+
];
|
|
40
43
|
const controlUnsubscribers = [
|
|
41
44
|
processor.on('pause', () => {
|
|
42
45
|
if (!parentState.isPaused()) {
|
|
@@ -49,7 +52,7 @@ export function wireSubprocessorEvents(processor, parentEmitter, parentState) {
|
|
|
49
52
|
}
|
|
50
53
|
}),
|
|
51
54
|
];
|
|
52
|
-
processor.onAny((event, data) => {
|
|
55
|
+
const unsubscribeAny = processor.onAny((event, data) => {
|
|
53
56
|
if (event.startsWith('globalSet:')) {
|
|
54
57
|
void parentEmitter.emit(event, data);
|
|
55
58
|
}
|
|
@@ -62,16 +65,35 @@ export function wireSubprocessorEvents(processor, parentEmitter, parentState) {
|
|
|
62
65
|
controlsCleanedUp = true;
|
|
63
66
|
controlUnsubscribers.forEach((unsubscribe) => unsubscribe());
|
|
64
67
|
};
|
|
65
|
-
|
|
68
|
+
if (parentState.autoCleanup !== false) {
|
|
69
|
+
controlUnsubscribers.push(subscribeOwnGraphRunLifecycle(processor, cleanupControls));
|
|
70
|
+
}
|
|
71
|
+
return () => {
|
|
72
|
+
cleanupControls();
|
|
73
|
+
passiveUnsubscribers.forEach((unsubscribe) => unsubscribe());
|
|
74
|
+
unsubscribeAny();
|
|
75
|
+
};
|
|
66
76
|
}
|
|
67
77
|
export function wireSubprocessorLifecycle(processor, options) {
|
|
78
|
+
let unsubscribePendingAbort;
|
|
79
|
+
const abortProcessor = (successful, error) => {
|
|
80
|
+
if (processor.isRunning) {
|
|
81
|
+
void processor.abort(successful, error);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
unsubscribePendingAbort ??= processor.on('graphStart', () => {
|
|
85
|
+
unsubscribePendingAbort?.();
|
|
86
|
+
unsubscribePendingAbort = undefined;
|
|
87
|
+
void processor.abort(successful, error);
|
|
88
|
+
});
|
|
89
|
+
};
|
|
68
90
|
const abortFromSignal = () => {
|
|
69
91
|
const abortReason = getGraphAbortReasonFromSignal(options.signal);
|
|
70
|
-
|
|
92
|
+
abortProcessor(abortReason?.successful ?? false, abortReason?.error);
|
|
71
93
|
};
|
|
72
94
|
const abortFromParent = () => {
|
|
73
95
|
const abortReason = getGraphAbortReasonFromSignal(options.parentAbortSignal);
|
|
74
|
-
|
|
96
|
+
abortProcessor(abortReason?.successful ?? false, abortReason?.error);
|
|
75
97
|
};
|
|
76
98
|
const pauseProcessor = () => {
|
|
77
99
|
void processor.pause();
|
|
@@ -84,6 +106,7 @@ export function wireSubprocessorLifecycle(processor, options) {
|
|
|
84
106
|
const unsubscribers = [
|
|
85
107
|
() => options.signal?.removeEventListener('abort', abortFromSignal),
|
|
86
108
|
() => options.parentAbortSignal.removeEventListener('abort', abortFromParent),
|
|
109
|
+
() => unsubscribePendingAbort?.(),
|
|
87
110
|
options.onParentPause(pauseProcessor),
|
|
88
111
|
options.onParentResume(resumeProcessor),
|
|
89
112
|
];
|
|
@@ -95,5 +118,14 @@ export function wireSubprocessorLifecycle(processor, options) {
|
|
|
95
118
|
cleanedUp = true;
|
|
96
119
|
unsubscribers.forEach((unsubscribe) => unsubscribe());
|
|
97
120
|
};
|
|
98
|
-
|
|
121
|
+
if (options.autoCleanup !== false) {
|
|
122
|
+
unsubscribers.push(subscribeOwnGraphRunLifecycle(processor, cleanup));
|
|
123
|
+
}
|
|
124
|
+
if (options.signal?.aborted) {
|
|
125
|
+
abortFromSignal();
|
|
126
|
+
}
|
|
127
|
+
else if (options.parentAbortSignal.aborted) {
|
|
128
|
+
abortFromParent();
|
|
129
|
+
}
|
|
130
|
+
return cleanup;
|
|
99
131
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|