@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.
Files changed (114) hide show
  1. package/dist/cjs/bundle.cjs +10138 -5679
  2. package/dist/cjs/bundle.cjs.map +4 -4
  3. package/dist/cjs/webAppRuntime.cjs +1214 -0
  4. package/dist/cjs/webAppRuntime.cjs.map +7 -0
  5. package/dist/esm/api/createProcessor.js +8 -1
  6. package/dist/esm/api/streaming.js +6 -0
  7. package/dist/esm/exports.js +8 -0
  8. package/dist/esm/integrations/mcp/MCPBase.js +92 -34
  9. package/dist/esm/model/GraphBoundaryEffects.js +13 -0
  10. package/dist/esm/model/GraphProcessor.js +1138 -86
  11. package/dist/esm/model/GraphProgress.js +15 -0
  12. package/dist/esm/model/ManagedAsyncBranches.js +28 -0
  13. package/dist/esm/model/Nodes.js +12 -0
  14. package/dist/esm/model/ProcessContextBuilder.js +3 -1
  15. package/dist/esm/model/RecordingPlayer.js +19 -0
  16. package/dist/esm/model/StoredValueStore.js +226 -0
  17. package/dist/esm/model/SubprocessorBridge.js +50 -18
  18. package/dist/esm/model/ToolCallContinuation.js +1 -0
  19. package/dist/esm/model/UiGraph.js +177 -4
  20. package/dist/esm/model/UiGraphActionProtocol.js +131 -0
  21. package/dist/esm/model/UiGraphBindings.js +530 -0
  22. package/dist/esm/model/UiGraphBrowserRuntime.js +373 -0
  23. package/dist/esm/model/UiGraphNormalization.js +148 -0
  24. package/dist/esm/model/UiGraphRendererStyles.js +1108 -42
  25. package/dist/esm/model/UiGraphRuntimeModel.js +518 -2
  26. package/dist/esm/model/UiGraphSchema.js +91 -0
  27. package/dist/esm/model/UiGraphWebAppStatus.js +19 -0
  28. package/dist/esm/model/UiGraphWebAppStorage.js +1 -0
  29. package/dist/esm/model/chat/chatMessages.js +5 -9
  30. package/dist/esm/model/chat-v2/chatV2Errors.js +36 -3
  31. package/dist/esm/model/chat-v2/index.js +1 -0
  32. package/dist/esm/model/chat-v2/toolContinuation.js +6 -8
  33. package/dist/esm/model/chat-v2/toolContinuationConnection.js +68 -0
  34. package/dist/esm/model/nodes/ArrayNode.js +2 -13
  35. package/dist/esm/model/nodes/AssembleMessageNode.js +2 -13
  36. package/dist/esm/model/nodes/AssemblePromptNode.js +2 -13
  37. package/dist/esm/model/nodes/CoalesceNode.js +2 -15
  38. package/dist/esm/model/nodes/DelayNode.js +3 -14
  39. package/dist/esm/model/nodes/DelegateFunctionCallNode.js +12 -40
  40. package/dist/esm/model/nodes/DidRunNode.js +2 -13
  41. package/dist/esm/model/nodes/GetStoredValueNode.js +109 -0
  42. package/dist/esm/model/nodes/JoinNode.js +2 -13
  43. package/dist/esm/model/nodes/LLMChatV2Node.js +17 -1
  44. package/dist/esm/model/nodes/LoopControllerNode.js +3 -14
  45. package/dist/esm/model/nodes/MCPDiscoveryNode.js +19 -90
  46. package/dist/esm/model/nodes/MCPGetPromptNode.js +11 -97
  47. package/dist/esm/model/nodes/MCPToolCallNode.js +11 -97
  48. package/dist/esm/model/nodes/MatchNode.js +7 -7
  49. package/dist/esm/model/nodes/PassthroughNode.js +3 -14
  50. package/dist/esm/model/nodes/RaceInputsNode.js +2 -13
  51. package/dist/esm/model/nodes/ReportProgressNode.js +57 -0
  52. package/dist/esm/model/nodes/SetStoredValueNode.js +84 -0
  53. package/dist/esm/model/nodes/StartBackgroundBranchNode.js +59 -0
  54. package/dist/esm/model/nodes/StoredValueNodeUtils.js +47 -0
  55. package/dist/esm/model/nodes/toolCallDelegation.js +44 -1
  56. package/dist/esm/model/nodes/variadicPortIndex.js +32 -0
  57. package/dist/esm/plugins/anthropic/anthropic.js +1 -1
  58. package/dist/esm/recording/ExecutionRecorder.js +6 -0
  59. package/dist/esm/utils/coerceType.js +3 -0
  60. package/dist/esm/utils/fetchEventSource.js +16 -17
  61. package/dist/esm/utils/serialization/serialization.js +4 -0
  62. package/dist/esm/utils/serialization/serializationUtils.js +8 -27
  63. package/dist/esm/utils/serialization/serialization_v4.js +3 -3
  64. package/dist/esm/webAppRuntime.js +4 -2
  65. package/dist/types/api/createProcessor.d.ts +4 -0
  66. package/dist/types/api/streaming.d.ts +6 -0
  67. package/dist/types/exports.d.ts +8 -0
  68. package/dist/types/integrations/mcp/MCPBase.d.ts +26 -3
  69. package/dist/types/model/EditorDefinition.d.ts +2 -1
  70. package/dist/types/model/ExecutorProtocol.d.ts +19 -0
  71. package/dist/types/model/GraphBoundaryEffects.d.ts +9 -1
  72. package/dist/types/model/GraphProcessor.d.ts +26 -2
  73. package/dist/types/model/GraphProgress.d.ts +6 -0
  74. package/dist/types/model/ManagedAsyncBranches.d.ts +6 -0
  75. package/dist/types/model/Nodes.d.ts +6 -2
  76. package/dist/types/model/ProcessContext.d.ts +18 -0
  77. package/dist/types/model/ProcessContextBuilder.d.ts +3 -1
  78. package/dist/types/model/StoredValueStore.d.ts +55 -0
  79. package/dist/types/model/SubprocessorBridge.d.ts +4 -2
  80. package/dist/types/model/ToolCallContinuation.d.ts +13 -0
  81. package/dist/types/model/UiGraph.d.ts +63 -9
  82. package/dist/types/model/UiGraphActionProtocol.d.ts +71 -0
  83. package/dist/types/model/UiGraphBindings.d.ts +45 -0
  84. package/dist/types/model/UiGraphBrowserRuntime.d.ts +55 -0
  85. package/dist/types/model/UiGraphNormalization.d.ts +24 -0
  86. package/dist/types/model/UiGraphRendererStyles.d.ts +2 -2
  87. package/dist/types/model/UiGraphRuntimeModel.d.ts +97 -6
  88. package/dist/types/model/UiGraphSchema.d.ts +113 -0
  89. package/dist/types/model/UiGraphWebAppStatus.d.ts +5 -0
  90. package/dist/types/model/UiGraphWebAppStorage.d.ts +3 -0
  91. package/dist/types/model/chat-v2/index.d.ts +1 -0
  92. package/dist/types/model/chat-v2/toolContinuation.d.ts +1 -0
  93. package/dist/types/model/chat-v2/toolContinuationConnection.d.ts +28 -0
  94. package/dist/types/model/nodes/ArrayNode.d.ts +0 -1
  95. package/dist/types/model/nodes/AssembleMessageNode.d.ts +0 -1
  96. package/dist/types/model/nodes/AssemblePromptNode.d.ts +0 -1
  97. package/dist/types/model/nodes/DelayNode.d.ts +0 -1
  98. package/dist/types/model/nodes/DidRunNode.d.ts +0 -1
  99. package/dist/types/model/nodes/GetStoredValueNode.d.ts +25 -0
  100. package/dist/types/model/nodes/JoinNode.d.ts +0 -1
  101. package/dist/types/model/nodes/LoopControllerNode.d.ts +0 -1
  102. package/dist/types/model/nodes/PassthroughNode.d.ts +0 -1
  103. package/dist/types/model/nodes/RaceInputsNode.d.ts +0 -1
  104. package/dist/types/model/nodes/ReportProgressNode.d.ts +23 -0
  105. package/dist/types/model/nodes/SetStoredValueNode.d.ts +23 -0
  106. package/dist/types/model/nodes/StartBackgroundBranchNode.d.ts +14 -0
  107. package/dist/types/model/nodes/StoredValueNodeUtils.d.ts +5 -0
  108. package/dist/types/model/nodes/toolCallDelegation.d.ts +4 -0
  109. package/dist/types/model/nodes/variadicPortIndex.d.ts +4 -0
  110. package/dist/types/recording/RecordedEvents.d.ts +10 -0
  111. package/dist/types/webAppRuntime.d.ts +8 -3
  112. package/package.json +2 -2
  113. package/dist/esm/plugins/anthropic/fetchEventSource.js +0 -107
  114. 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 UI graphs.
15
- * Repaired IDs are deterministic so separately rendered HTML and action calls agree
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
+ }