@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
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { newId } from '../utils/newId.js';
|
|
2
|
+
export const UI_GRAPH_GAP_SIZES = ['small', 'medium', 'large'];
|
|
3
|
+
export const UI_GRAPH_OUTPUT_RENDER_MODES = ['text', 'json', 'markdown', 'image'];
|
|
2
4
|
export function hasValidUiGraphComponentIds(uiGraph) {
|
|
3
5
|
const componentIds = new Set();
|
|
4
6
|
for (const component of uiGraph.components) {
|
|
@@ -11,9 +13,8 @@ export function hasValidUiGraphComponentIds(uiGraph) {
|
|
|
11
13
|
return true;
|
|
12
14
|
}
|
|
13
15
|
/**
|
|
14
|
-
* Returns an immutable repair for legacy or externally assembled
|
|
15
|
-
*
|
|
16
|
-
* without mutating a host-owned project snapshot.
|
|
16
|
+
* Returns an immutable component-ID repair for legacy or externally assembled
|
|
17
|
+
* UI graphs. Full untrusted-data validation belongs to normalizeUiGraph().
|
|
17
18
|
*/
|
|
18
19
|
export function normalizeUiGraphComponentIds(uiGraph) {
|
|
19
20
|
if (hasValidUiGraphComponentIds(uiGraph)) {
|
|
@@ -92,11 +93,144 @@ export function getUiGraphInitialState(uiGraph) {
|
|
|
92
93
|
if (component.type === 'input' || component.type === 'textarea') {
|
|
93
94
|
state[component.stateKey] = component.defaultValue ?? '';
|
|
94
95
|
}
|
|
96
|
+
else if (component.type === 'dropdown') {
|
|
97
|
+
state[component.stateKey] = '';
|
|
98
|
+
}
|
|
99
|
+
else if (component.type === 'chat') {
|
|
100
|
+
state[getUiGraphChatDraftStateKey(component.id)] = '';
|
|
101
|
+
state[getUiGraphChatMessagesStateKey(component.id)] = [];
|
|
102
|
+
state[getUiGraphChatPinsStateKey(component.id)] = [];
|
|
103
|
+
}
|
|
95
104
|
}
|
|
96
105
|
return state;
|
|
97
106
|
}
|
|
98
107
|
export function getUiGraphActionComponent(uiGraph, componentId) {
|
|
99
|
-
return uiGraph.components.find((component) => component.id === componentId && component.type === 'button');
|
|
108
|
+
return uiGraph.components.find((component) => component.id === componentId && (component.type === 'button' || component.type === 'chat'));
|
|
109
|
+
}
|
|
110
|
+
export function getUiGraphChatDraftStateKey(componentId) {
|
|
111
|
+
return `__rivet_chat_${componentId}_draft`;
|
|
112
|
+
}
|
|
113
|
+
export function getUiGraphChatMessagesStateKey(componentId) {
|
|
114
|
+
return `__rivet_chat_${componentId}_messages`;
|
|
115
|
+
}
|
|
116
|
+
export function getUiGraphChatPinsStateKey(componentId) {
|
|
117
|
+
return `__rivet_chat_${componentId}_pins`;
|
|
118
|
+
}
|
|
119
|
+
export function getUiGraphChatMessages(componentId, state) {
|
|
120
|
+
const messages = state[getUiGraphChatMessagesStateKey(componentId)];
|
|
121
|
+
return Array.isArray(messages) ? messages.filter(isUiGraphChatMessage) : [];
|
|
122
|
+
}
|
|
123
|
+
export function getUiGraphChatPins(componentId, state) {
|
|
124
|
+
const messages = getUiGraphChatMessages(componentId, state);
|
|
125
|
+
const storedPins = state[getUiGraphChatPinsStateKey(componentId)];
|
|
126
|
+
const messageIndexes = Array.isArray(storedPins)
|
|
127
|
+
? [
|
|
128
|
+
...new Set(storedPins.filter((value) => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0)),
|
|
129
|
+
].sort((left, right) => left - right)
|
|
130
|
+
: [];
|
|
131
|
+
const pinnedIndexes = new Set(messageIndexes);
|
|
132
|
+
const pins = [];
|
|
133
|
+
let prompt;
|
|
134
|
+
let promptMessageIndex;
|
|
135
|
+
messages.forEach((message, messageIndex) => {
|
|
136
|
+
if (message.role === 'user') {
|
|
137
|
+
prompt = message;
|
|
138
|
+
promptMessageIndex = messageIndex;
|
|
139
|
+
}
|
|
140
|
+
else if (pinnedIndexes.has(messageIndex)) {
|
|
141
|
+
pins.push({ messageIndex, prompt, promptMessageIndex, response: message });
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
return pins;
|
|
145
|
+
}
|
|
146
|
+
export function createUiGraphChatPinStatePatch(componentId, state, messageIndex) {
|
|
147
|
+
const messages = getUiGraphChatMessages(componentId, state);
|
|
148
|
+
if (messages[messageIndex]?.role !== 'assistant') {
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
const pinsStateKey = getUiGraphChatPinsStateKey(componentId);
|
|
152
|
+
const pinnedIndexes = getUiGraphChatPins(componentId, state).map((pin) => pin.messageIndex);
|
|
153
|
+
return {
|
|
154
|
+
[pinsStateKey]: pinnedIndexes.includes(messageIndex)
|
|
155
|
+
? pinnedIndexes.filter((index) => index !== messageIndex)
|
|
156
|
+
: [...pinnedIndexes, messageIndex].sort((left, right) => left - right),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/** Clears a Chat's conversation and pins without discarding an unsent draft. */
|
|
160
|
+
export function createUiGraphChatHistoryFlushStatePatch(componentId) {
|
|
161
|
+
return {
|
|
162
|
+
[getUiGraphChatMessagesStateKey(componentId)]: [],
|
|
163
|
+
[getUiGraphChatPinsStateKey(componentId)]: [],
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
export function createUiGraphChatSubmissionStatePatch(componentId, state) {
|
|
167
|
+
const draftStateKey = getUiGraphChatDraftStateKey(componentId);
|
|
168
|
+
const content = `${state[draftStateKey] ?? ''}`.trim();
|
|
169
|
+
if (!content) {
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
[draftStateKey]: '',
|
|
174
|
+
[getUiGraphChatMessagesStateKey(componentId)]: [
|
|
175
|
+
...getUiGraphChatMessages(componentId, state),
|
|
176
|
+
{ role: 'user', content },
|
|
177
|
+
],
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
export function getUiGraphComponentActionState(component, state) {
|
|
181
|
+
if (component.type === 'button') {
|
|
182
|
+
return getUiGraphActionState(component.action, state);
|
|
183
|
+
}
|
|
184
|
+
const messagesStateKey = getUiGraphChatMessagesStateKey(component.id);
|
|
185
|
+
return {
|
|
186
|
+
...getUiGraphActionState(component.action, state),
|
|
187
|
+
[messagesStateKey]: getUiGraphChatMessages(component.id, state),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
export function resolveUiGraphComponentActionInputs(component, state) {
|
|
191
|
+
if (component.type === 'button') {
|
|
192
|
+
return resolveUiGraphActionInputs(component.action, state);
|
|
193
|
+
}
|
|
194
|
+
const messages = getUiGraphChatMessages(component.id, state);
|
|
195
|
+
const latestMessage = messages[messages.length - 1];
|
|
196
|
+
const latestUserMessage = latestMessage?.role === 'user' ? latestMessage : undefined;
|
|
197
|
+
const historyMessages = latestUserMessage ? messages.slice(0, -1) : messages;
|
|
198
|
+
const inputs = resolveUiGraphActionInputs(component.action, state);
|
|
199
|
+
if (component.action.userInputId) {
|
|
200
|
+
inputs[component.action.userInputId] = latestUserMessage?.content ?? '';
|
|
201
|
+
}
|
|
202
|
+
if (component.action.historyInputId) {
|
|
203
|
+
inputs[component.action.historyInputId] = {
|
|
204
|
+
type: 'chat-message[]',
|
|
205
|
+
value: historyMessages.map(toRivetChatMessage),
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
return inputs;
|
|
209
|
+
}
|
|
210
|
+
export function resolveUiGraphComponentActionOutputStatePatch(component, outputs, state) {
|
|
211
|
+
if (component.type === 'button') {
|
|
212
|
+
return resolveUiGraphActionOutputStatePatch(component.action, outputs);
|
|
213
|
+
}
|
|
214
|
+
const outputId = component.action.responseOutputId;
|
|
215
|
+
if (!outputId || !Object.prototype.hasOwnProperty.call(outputs, outputId)) {
|
|
216
|
+
throw new Error(outputId
|
|
217
|
+
? `Graph output "${outputId}" was not returned by the target graph.`
|
|
218
|
+
: 'The Chat component has no response graph output.');
|
|
219
|
+
}
|
|
220
|
+
const messagesStateKey = getUiGraphChatMessagesStateKey(component.id);
|
|
221
|
+
return {
|
|
222
|
+
[messagesStateKey]: [
|
|
223
|
+
...getUiGraphChatMessages(component.id, state),
|
|
224
|
+
{ role: 'assistant', content: getUiGraphChatResponseText(outputs[outputId]) },
|
|
225
|
+
],
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
export function getUiGraphComponentActionOutputStateKeys(component) {
|
|
229
|
+
return component.type === 'button'
|
|
230
|
+
? getUiGraphActionOutputBindings(component.action)
|
|
231
|
+
.map((binding) => binding.stateKey.trim())
|
|
232
|
+
.filter(Boolean)
|
|
233
|
+
: [getUiGraphChatMessagesStateKey(component.id)];
|
|
100
234
|
}
|
|
101
235
|
export function resolveUiGraphActionInputs(action, state) {
|
|
102
236
|
const inputs = {};
|
|
@@ -168,3 +302,42 @@ function resolveUiGraphOutputBindingValue(binding, outputs) {
|
|
|
168
302
|
}
|
|
169
303
|
return outputs[binding.outputKey]?.value;
|
|
170
304
|
}
|
|
305
|
+
function isUiGraphChatMessage(value) {
|
|
306
|
+
return (value != null &&
|
|
307
|
+
typeof value === 'object' &&
|
|
308
|
+
(value.role === 'user' || value.role === 'assistant') &&
|
|
309
|
+
typeof value.content === 'string');
|
|
310
|
+
}
|
|
311
|
+
function toRivetChatMessage(message) {
|
|
312
|
+
return message.role === 'user'
|
|
313
|
+
? { type: 'user', message: message.content }
|
|
314
|
+
: {
|
|
315
|
+
type: 'assistant',
|
|
316
|
+
message: message.content,
|
|
317
|
+
function_call: undefined,
|
|
318
|
+
function_calls: undefined,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
function getUiGraphChatResponseText(output) {
|
|
322
|
+
if (output.type === 'chat-message') {
|
|
323
|
+
const message = output.value.message;
|
|
324
|
+
if (typeof message === 'string') {
|
|
325
|
+
return message;
|
|
326
|
+
}
|
|
327
|
+
return (Array.isArray(message) ? message : [message])
|
|
328
|
+
.flatMap((part) => (typeof part === 'string' ? [part] : []))
|
|
329
|
+
.join('\n');
|
|
330
|
+
}
|
|
331
|
+
if (typeof output.value === 'string') {
|
|
332
|
+
return output.value;
|
|
333
|
+
}
|
|
334
|
+
if (output.value == null) {
|
|
335
|
+
return '';
|
|
336
|
+
}
|
|
337
|
+
try {
|
|
338
|
+
return JSON.stringify(output.value, null, 2);
|
|
339
|
+
}
|
|
340
|
+
catch {
|
|
341
|
+
return String(output.value);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { normalizeGraphProgress } from './GraphProgress.js';
|
|
2
|
+
export const RIVET_WEB_APP_ACTION_PROTOCOL_VERSION = 1;
|
|
3
|
+
export function parseRivetWebAppClientMessage(value) {
|
|
4
|
+
if (!isRecord(value) || typeof value.type !== 'string')
|
|
5
|
+
return undefined;
|
|
6
|
+
switch (value.type) {
|
|
7
|
+
case 'client.hello':
|
|
8
|
+
return value.protocolVersion === RIVET_WEB_APP_ACTION_PROTOCOL_VERSION
|
|
9
|
+
? { type: value.type, protocolVersion: value.protocolVersion }
|
|
10
|
+
: undefined;
|
|
11
|
+
case 'action.start':
|
|
12
|
+
return isNonEmptyString(value.requestId) &&
|
|
13
|
+
isNonEmptyString(value.componentId) &&
|
|
14
|
+
isRecord(value.state) &&
|
|
15
|
+
(value.storage == null || isRecord(value.storage)) &&
|
|
16
|
+
(value.revisionKey == null || typeof value.revisionKey === 'string')
|
|
17
|
+
? {
|
|
18
|
+
type: value.type,
|
|
19
|
+
requestId: value.requestId,
|
|
20
|
+
componentId: value.componentId,
|
|
21
|
+
state: value.state,
|
|
22
|
+
...(isRecord(value.storage) ? { storage: value.storage } : {}),
|
|
23
|
+
...(typeof value.revisionKey === 'string' ? { revisionKey: value.revisionKey } : {}),
|
|
24
|
+
}
|
|
25
|
+
: undefined;
|
|
26
|
+
case 'action.cancel':
|
|
27
|
+
return isNonEmptyString(value.runId) ? { type: value.type, runId: value.runId } : undefined;
|
|
28
|
+
case 'run.resume':
|
|
29
|
+
return isNonEmptyString(value.runId) &&
|
|
30
|
+
typeof value.lastSequence === 'number' &&
|
|
31
|
+
Number.isSafeInteger(value.lastSequence) &&
|
|
32
|
+
value.lastSequence >= 0
|
|
33
|
+
? { type: value.type, runId: value.runId, lastSequence: value.lastSequence }
|
|
34
|
+
: undefined;
|
|
35
|
+
default:
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export function parseRivetWebAppServerMessage(value) {
|
|
40
|
+
if (!isRecord(value) || typeof value.type !== 'string')
|
|
41
|
+
return undefined;
|
|
42
|
+
if (value.type === 'server.ready') {
|
|
43
|
+
return value.protocolVersion === RIVET_WEB_APP_ACTION_PROTOCOL_VERSION
|
|
44
|
+
? { type: value.type, protocolVersion: value.protocolVersion }
|
|
45
|
+
: undefined;
|
|
46
|
+
}
|
|
47
|
+
if (value.type === 'server.draining')
|
|
48
|
+
return { type: value.type };
|
|
49
|
+
if (value.type === 'action.rejected') {
|
|
50
|
+
return isNonEmptyString(value.requestId) && isNonEmptyString(value.error)
|
|
51
|
+
? {
|
|
52
|
+
type: value.type,
|
|
53
|
+
requestId: value.requestId,
|
|
54
|
+
error: value.error,
|
|
55
|
+
...(typeof value.code === 'string' ? { code: value.code } : {}),
|
|
56
|
+
}
|
|
57
|
+
: undefined;
|
|
58
|
+
}
|
|
59
|
+
if (value.type === 'run.rejected') {
|
|
60
|
+
return isNonEmptyString(value.runId) && isNonEmptyString(value.error)
|
|
61
|
+
? {
|
|
62
|
+
type: value.type,
|
|
63
|
+
runId: value.runId,
|
|
64
|
+
error: value.error,
|
|
65
|
+
...(typeof value.code === 'string' ? { code: value.code } : {}),
|
|
66
|
+
}
|
|
67
|
+
: undefined;
|
|
68
|
+
}
|
|
69
|
+
if (!hasRunEventBase(value))
|
|
70
|
+
return undefined;
|
|
71
|
+
const base = {
|
|
72
|
+
requestId: value.requestId,
|
|
73
|
+
runId: value.runId,
|
|
74
|
+
sequence: value.sequence,
|
|
75
|
+
};
|
|
76
|
+
switch (value.type) {
|
|
77
|
+
case 'action.accepted':
|
|
78
|
+
case 'action.cancelled':
|
|
79
|
+
return { type: value.type, ...base };
|
|
80
|
+
case 'action.progress': {
|
|
81
|
+
if (!isRecord(value.progress))
|
|
82
|
+
return undefined;
|
|
83
|
+
const progress = normalizeGraphProgress({
|
|
84
|
+
message: typeof value.progress.message === 'string' ? value.progress.message : undefined,
|
|
85
|
+
percent: typeof value.progress.percent === 'number' ? value.progress.percent : undefined,
|
|
86
|
+
});
|
|
87
|
+
return progress ? { type: value.type, ...base, progress } : undefined;
|
|
88
|
+
}
|
|
89
|
+
case 'action.completed':
|
|
90
|
+
return isRecord(value.statePatch) && (value.storagePatch == null || isRecord(value.storagePatch))
|
|
91
|
+
? {
|
|
92
|
+
type: value.type,
|
|
93
|
+
...base,
|
|
94
|
+
statePatch: value.statePatch,
|
|
95
|
+
...(isRecord(value.storagePatch) ? { storagePatch: value.storagePatch } : {}),
|
|
96
|
+
}
|
|
97
|
+
: undefined;
|
|
98
|
+
case 'action.failed':
|
|
99
|
+
return isNonEmptyString(value.error)
|
|
100
|
+
? {
|
|
101
|
+
type: value.type,
|
|
102
|
+
...base,
|
|
103
|
+
error: value.error,
|
|
104
|
+
...(typeof value.code === 'string' ? { code: value.code } : {}),
|
|
105
|
+
}
|
|
106
|
+
: undefined;
|
|
107
|
+
case 'action.interrupted':
|
|
108
|
+
return isNonEmptyString(value.error) ? { type: value.type, ...base, error: value.error } : undefined;
|
|
109
|
+
default:
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
export function isRivetWebAppRunTerminalEvent(event) {
|
|
114
|
+
return (event.type === 'action.completed' ||
|
|
115
|
+
event.type === 'action.failed' ||
|
|
116
|
+
event.type === 'action.cancelled' ||
|
|
117
|
+
event.type === 'action.interrupted');
|
|
118
|
+
}
|
|
119
|
+
function hasRunEventBase(value) {
|
|
120
|
+
return (isNonEmptyString(value.requestId) &&
|
|
121
|
+
isNonEmptyString(value.runId) &&
|
|
122
|
+
typeof value.sequence === 'number' &&
|
|
123
|
+
Number.isSafeInteger(value.sequence) &&
|
|
124
|
+
value.sequence > 0);
|
|
125
|
+
}
|
|
126
|
+
function isRecord(value) {
|
|
127
|
+
return typeof value === 'object' && value != null && !Array.isArray(value);
|
|
128
|
+
}
|
|
129
|
+
function isNonEmptyString(value) {
|
|
130
|
+
return typeof value === 'string' && value.length > 0;
|
|
131
|
+
}
|