@valerypopoff/rivet2-core 2.1.3 → 2.1.4
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 +2707 -1012
- package/dist/cjs/bundle.cjs.map +4 -4
- package/dist/cjs/webAppRuntime.cjs +625 -0
- package/dist/cjs/webAppRuntime.cjs.map +7 -0
- package/dist/esm/api/createProcessor.js +3 -0
- package/dist/esm/api/streaming.js +6 -0
- package/dist/esm/exports.js +5 -0
- package/dist/esm/integrations/mcp/MCPBase.js +92 -34
- package/dist/esm/model/GraphProcessor.js +11 -4
- package/dist/esm/model/GraphProgress.js +15 -0
- package/dist/esm/model/Nodes.js +3 -0
- package/dist/esm/model/ProcessContextBuilder.js +2 -1
- package/dist/esm/model/RecordingPlayer.js +10 -0
- package/dist/esm/model/SubprocessorBridge.js +1 -0
- package/dist/esm/model/UiGraph.js +127 -4
- package/dist/esm/model/UiGraphActionProtocol.js +122 -0
- package/dist/esm/model/UiGraphBindings.js +530 -0
- package/dist/esm/model/UiGraphBrowserRuntime.js +40 -0
- package/dist/esm/model/UiGraphNormalization.js +298 -0
- package/dist/esm/model/UiGraphRendererStyles.js +374 -6
- package/dist/esm/model/UiGraphRuntimeModel.js +300 -0
- package/dist/esm/model/UiGraphWebAppStatus.js +19 -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/DidRunNode.js +2 -13
- package/dist/esm/model/nodes/JoinNode.js +2 -13
- 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/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/variadicPortIndex.js +32 -0
- package/dist/esm/plugins/anthropic/anthropic.js +1 -1
- package/dist/esm/recording/ExecutionRecorder.js +5 -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/streaming.d.ts +6 -0
- package/dist/types/exports.d.ts +5 -0
- package/dist/types/integrations/mcp/MCPBase.d.ts +26 -3
- package/dist/types/model/ExecutorProtocol.d.ts +7 -0
- package/dist/types/model/GraphProcessor.d.ts +7 -0
- package/dist/types/model/GraphProgress.d.ts +6 -0
- package/dist/types/model/Nodes.d.ts +3 -2
- package/dist/types/model/ProcessContext.d.ts +3 -0
- package/dist/types/model/ProcessContextBuilder.d.ts +2 -1
- package/dist/types/model/UiGraph.d.ts +42 -9
- package/dist/types/model/UiGraphActionProtocol.d.ts +69 -0
- package/dist/types/model/UiGraphBindings.d.ts +45 -0
- package/dist/types/model/UiGraphBrowserRuntime.d.ts +4 -0
- package/dist/types/model/UiGraphNormalization.d.ts +24 -0
- package/dist/types/model/UiGraphRendererStyles.d.ts +1 -1
- package/dist/types/model/UiGraphRuntimeModel.d.ts +77 -5
- package/dist/types/model/UiGraphWebAppStatus.d.ts +5 -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/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/variadicPortIndex.d.ts +4 -0
- package/dist/types/recording/RecordedEvents.d.ts +5 -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,625 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/webAppRuntime.ts
|
|
21
|
+
var webAppRuntime_exports = {};
|
|
22
|
+
__export(webAppRuntime_exports, {
|
|
23
|
+
RIVET_WEB_APP_ACTION_PROTOCOL_VERSION: () => RIVET_WEB_APP_ACTION_PROTOCOL_VERSION,
|
|
24
|
+
applyUiGraphStatePatch: () => applyUiGraphStatePatch,
|
|
25
|
+
copyUiGraphText: () => copyUiGraphText,
|
|
26
|
+
createUiGraphActionExecutionController: () => createUiGraphActionExecutionController,
|
|
27
|
+
createUiGraphChatSubmissionStatePatch: () => createUiGraphChatSubmissionStatePatch,
|
|
28
|
+
createUiGraphInteractionController: () => createUiGraphInteractionController,
|
|
29
|
+
downloadUiGraphJsonOutput: () => downloadUiGraphJsonOutput,
|
|
30
|
+
getUiGraphActionState: () => getUiGraphActionState,
|
|
31
|
+
getUiGraphChatDraftStateKey: () => getUiGraphChatDraftStateKey,
|
|
32
|
+
getUiGraphComponentRenderModel: () => getUiGraphComponentRenderModel,
|
|
33
|
+
getUiGraphJsonOutputFilename: () => getUiGraphJsonOutputFilename,
|
|
34
|
+
parseRivetWebAppClientMessage: () => parseRivetWebAppClientMessage,
|
|
35
|
+
parseRivetWebAppServerMessage: () => parseRivetWebAppServerMessage
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(webAppRuntime_exports);
|
|
38
|
+
|
|
39
|
+
// src/utils/newId.ts
|
|
40
|
+
var import_non_secure = require("nanoid/non-secure");
|
|
41
|
+
|
|
42
|
+
// src/model/UiGraph.ts
|
|
43
|
+
function getUiGraphInitialState(uiGraph) {
|
|
44
|
+
const state = {};
|
|
45
|
+
for (const component of uiGraph.components) {
|
|
46
|
+
if (component.type === "input" || component.type === "textarea") {
|
|
47
|
+
state[component.stateKey] = component.defaultValue ?? "";
|
|
48
|
+
} else if (component.type === "chat") {
|
|
49
|
+
state[getUiGraphChatDraftStateKey(component.id)] = "";
|
|
50
|
+
state[getUiGraphChatMessagesStateKey(component.id)] = [];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return state;
|
|
54
|
+
}
|
|
55
|
+
function getUiGraphChatDraftStateKey(componentId) {
|
|
56
|
+
return `__rivet_chat_${componentId}_draft`;
|
|
57
|
+
}
|
|
58
|
+
function getUiGraphChatMessagesStateKey(componentId) {
|
|
59
|
+
return `__rivet_chat_${componentId}_messages`;
|
|
60
|
+
}
|
|
61
|
+
function getUiGraphChatMessages(componentId, state) {
|
|
62
|
+
const messages = state[getUiGraphChatMessagesStateKey(componentId)];
|
|
63
|
+
return Array.isArray(messages) ? messages.filter(isUiGraphChatMessage) : [];
|
|
64
|
+
}
|
|
65
|
+
function createUiGraphChatSubmissionStatePatch(componentId, state) {
|
|
66
|
+
const draftStateKey = getUiGraphChatDraftStateKey(componentId);
|
|
67
|
+
const content = `${state[draftStateKey] ?? ""}`.trim();
|
|
68
|
+
if (!content) {
|
|
69
|
+
return void 0;
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
[draftStateKey]: "",
|
|
73
|
+
[getUiGraphChatMessagesStateKey(componentId)]: [
|
|
74
|
+
...getUiGraphChatMessages(componentId, state),
|
|
75
|
+
{ role: "user", content }
|
|
76
|
+
]
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function getUiGraphComponentActionState(component, state) {
|
|
80
|
+
if (component.type === "button") {
|
|
81
|
+
return getUiGraphActionState(component.action, state);
|
|
82
|
+
}
|
|
83
|
+
const messagesStateKey = getUiGraphChatMessagesStateKey(component.id);
|
|
84
|
+
return {
|
|
85
|
+
...getUiGraphActionState(component.action, state),
|
|
86
|
+
[messagesStateKey]: getUiGraphChatMessages(component.id, state)
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function getUiGraphComponentActionOutputStateKeys(component) {
|
|
90
|
+
return component.type === "button" ? getUiGraphActionOutputBindings(component.action).map((binding) => binding.stateKey.trim()).filter(Boolean) : [getUiGraphChatMessagesStateKey(component.id)];
|
|
91
|
+
}
|
|
92
|
+
function getUiGraphActionState(action, state) {
|
|
93
|
+
return Object.fromEntries(
|
|
94
|
+
[...new Set(getUiGraphActionInputBindings(action).map((binding) => binding.stateKey))].filter(Boolean).filter((stateKey) => Object.prototype.hasOwnProperty.call(state, stateKey)).map((stateKey) => [stateKey, state[stateKey]])
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
function getUiGraphActionOutputBindings(action) {
|
|
98
|
+
if (action.outputs) {
|
|
99
|
+
return action.outputs;
|
|
100
|
+
}
|
|
101
|
+
return action.outputStateKey ? [{ outputKey: action.outputKey, stateKey: action.outputStateKey }] : [];
|
|
102
|
+
}
|
|
103
|
+
function getUiGraphActionInputBindings(action) {
|
|
104
|
+
if (action.inputMappings) {
|
|
105
|
+
return action.inputMappings;
|
|
106
|
+
}
|
|
107
|
+
return Object.entries(action.inputs ?? {}).map(([inputKey, binding]) => ({
|
|
108
|
+
inputKey,
|
|
109
|
+
stateKey: binding.type === "state" ? binding.key : ""
|
|
110
|
+
}));
|
|
111
|
+
}
|
|
112
|
+
function isUiGraphChatMessage(value) {
|
|
113
|
+
return value != null && typeof value === "object" && (value.role === "user" || value.role === "assistant") && typeof value.content === "string";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/model/GraphProgress.ts
|
|
117
|
+
var MAX_PROGRESS_MESSAGE_LENGTH = 500;
|
|
118
|
+
function normalizeGraphProgress(progress) {
|
|
119
|
+
const message = typeof progress.message === "string" ? progress.message.trim().slice(0, MAX_PROGRESS_MESSAGE_LENGTH) : "";
|
|
120
|
+
const percent = typeof progress.percent === "number" && Number.isFinite(progress.percent) ? Math.min(100, Math.max(0, progress.percent)) : void 0;
|
|
121
|
+
if (!message && percent == null) {
|
|
122
|
+
return void 0;
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
...message ? { message } : {},
|
|
126
|
+
...percent == null ? {} : { percent }
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/model/UiGraphRuntimeModel.ts
|
|
131
|
+
var IMAGE_DATA_URL_PATTERN = /^data:(image\/(?:avif|bmp|gif|jpe?g|png|webp));base64,([\s\S]+)$/i;
|
|
132
|
+
var IMAGE_URL_PATTERN = /^(?:https?:\/\/|blob:|\/\/|\/|\.\.?\/)/i;
|
|
133
|
+
var RELATIVE_IMAGE_PATH_PATTERN = /\.(?:avif|bmp|gif|jpe?g|png|webp)(?:[?#].*)?$/i;
|
|
134
|
+
var BASE64_PATTERN = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
135
|
+
function createUiGraphActionExecutionController() {
|
|
136
|
+
let nextVersion = 0;
|
|
137
|
+
const activeExecutionByComponent = /* @__PURE__ */ new Map();
|
|
138
|
+
const latestWriterByStateKey = /* @__PURE__ */ new Map();
|
|
139
|
+
return {
|
|
140
|
+
begin(component) {
|
|
141
|
+
if (activeExecutionByComponent.has(component.id)) {
|
|
142
|
+
return void 0;
|
|
143
|
+
}
|
|
144
|
+
const execution = { componentId: component.id, id: ++nextVersion };
|
|
145
|
+
activeExecutionByComponent.set(component.id, execution.id);
|
|
146
|
+
for (const stateKey of getUiGraphComponentActionOutputStateKeys(component)) {
|
|
147
|
+
latestWriterByStateKey.set(stateKey, execution.id);
|
|
148
|
+
}
|
|
149
|
+
return execution;
|
|
150
|
+
},
|
|
151
|
+
finish(execution) {
|
|
152
|
+
if (activeExecutionByComponent.get(execution.componentId) !== execution.id) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
activeExecutionByComponent.delete(execution.componentId);
|
|
156
|
+
return true;
|
|
157
|
+
},
|
|
158
|
+
isCurrent(execution) {
|
|
159
|
+
return activeExecutionByComponent.get(execution.componentId) === execution.id;
|
|
160
|
+
},
|
|
161
|
+
isRunning(componentId) {
|
|
162
|
+
return activeExecutionByComponent.has(componentId);
|
|
163
|
+
},
|
|
164
|
+
noteStateWrite(stateKey) {
|
|
165
|
+
const normalizedStateKey = stateKey.trim();
|
|
166
|
+
if (normalizedStateKey) {
|
|
167
|
+
latestWriterByStateKey.set(normalizedStateKey, ++nextVersion);
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
resolveStatePatch(execution, statePatch) {
|
|
171
|
+
if (!statePatch) {
|
|
172
|
+
return void 0;
|
|
173
|
+
}
|
|
174
|
+
const applicableEntries = Object.entries(statePatch).filter(
|
|
175
|
+
([stateKey]) => latestWriterByStateKey.get(stateKey) === execution.id
|
|
176
|
+
);
|
|
177
|
+
return applicableEntries.length > 0 ? Object.fromEntries(applicableEntries) : void 0;
|
|
178
|
+
},
|
|
179
|
+
reset() {
|
|
180
|
+
activeExecutionByComponent.clear();
|
|
181
|
+
latestWriterByStateKey.clear();
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function createUiGraphInteractionController(initialUiGraph, options = {}) {
|
|
186
|
+
let uiGraphId = initialUiGraph.id;
|
|
187
|
+
let state = options.initialState ? { ...options.initialState } : getUiGraphInitialState(initialUiGraph);
|
|
188
|
+
let actionErrors = {};
|
|
189
|
+
let actionProgress = {};
|
|
190
|
+
let snapshot;
|
|
191
|
+
const actionController = createUiGraphActionExecutionController();
|
|
192
|
+
const activeActions = /* @__PURE__ */ new Map();
|
|
193
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
194
|
+
const updateSnapshot = () => {
|
|
195
|
+
snapshot = {
|
|
196
|
+
actionErrors,
|
|
197
|
+
actionProgress,
|
|
198
|
+
runningComponentIds: new Set([...activeActions.values()].map(({ execution }) => execution.componentId)),
|
|
199
|
+
state
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
const publish = (change) => {
|
|
203
|
+
updateSnapshot();
|
|
204
|
+
for (const listener of listeners) {
|
|
205
|
+
listener(change);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
const abortMatchingActions = (predicate, notify) => {
|
|
209
|
+
let changed = false;
|
|
210
|
+
for (const [executionId, activeAction] of activeActions) {
|
|
211
|
+
if (!predicate(activeAction, executionId)) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
activeAction.abortController.abort();
|
|
215
|
+
activeActions.delete(executionId);
|
|
216
|
+
actionController.finish(activeAction.execution);
|
|
217
|
+
changed = true;
|
|
218
|
+
}
|
|
219
|
+
if (changed && notify) {
|
|
220
|
+
publish("action");
|
|
221
|
+
}
|
|
222
|
+
return changed;
|
|
223
|
+
};
|
|
224
|
+
const abortAllActions = (notify) => {
|
|
225
|
+
let changed = abortMatchingActions(() => true, false);
|
|
226
|
+
actionController.reset();
|
|
227
|
+
if (Object.keys(actionProgress).length > 0) {
|
|
228
|
+
actionProgress = {};
|
|
229
|
+
changed = true;
|
|
230
|
+
}
|
|
231
|
+
if (changed && notify) {
|
|
232
|
+
publish("action");
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
const removeActionPresentation = (componentId) => {
|
|
236
|
+
if (Object.prototype.hasOwnProperty.call(actionErrors, componentId)) {
|
|
237
|
+
actionErrors = { ...actionErrors };
|
|
238
|
+
delete actionErrors[componentId];
|
|
239
|
+
}
|
|
240
|
+
if (Object.prototype.hasOwnProperty.call(actionProgress, componentId)) {
|
|
241
|
+
actionProgress = { ...actionProgress };
|
|
242
|
+
delete actionProgress[componentId];
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
updateSnapshot();
|
|
246
|
+
return {
|
|
247
|
+
abortActions() {
|
|
248
|
+
abortAllActions(true);
|
|
249
|
+
},
|
|
250
|
+
cancelAction(componentId) {
|
|
251
|
+
if (abortMatchingActions(({ execution }) => execution.componentId === componentId, false)) {
|
|
252
|
+
removeActionPresentation(componentId);
|
|
253
|
+
publish("action");
|
|
254
|
+
}
|
|
255
|
+
},
|
|
256
|
+
detachActions() {
|
|
257
|
+
activeActions.clear();
|
|
258
|
+
actionController.reset();
|
|
259
|
+
actionProgress = {};
|
|
260
|
+
updateSnapshot();
|
|
261
|
+
},
|
|
262
|
+
getSnapshot() {
|
|
263
|
+
return snapshot;
|
|
264
|
+
},
|
|
265
|
+
async runAction(component, runner) {
|
|
266
|
+
const execution = actionController.begin(component);
|
|
267
|
+
if (!execution) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const abortController = new AbortController();
|
|
271
|
+
activeActions.set(execution.id, { abortController, execution });
|
|
272
|
+
removeActionPresentation(component.id);
|
|
273
|
+
publish("action");
|
|
274
|
+
try {
|
|
275
|
+
const result = await runner({
|
|
276
|
+
abortOtherActions: () => abortMatchingActions((_activeAction, executionId) => executionId !== execution.id, true),
|
|
277
|
+
componentId: component.id,
|
|
278
|
+
reportProgress: (progress) => {
|
|
279
|
+
if (!actionController.isCurrent(execution)) return;
|
|
280
|
+
const normalized = normalizeGraphProgress(progress);
|
|
281
|
+
if (!normalized) return;
|
|
282
|
+
actionProgress = { ...actionProgress, [component.id]: normalized };
|
|
283
|
+
publish("action");
|
|
284
|
+
},
|
|
285
|
+
signal: abortController.signal,
|
|
286
|
+
state: getUiGraphComponentActionState(component, state)
|
|
287
|
+
});
|
|
288
|
+
if (!actionController.isCurrent(execution)) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
const statePatch = actionController.resolveStatePatch(execution, result.statePatch);
|
|
292
|
+
if (statePatch) {
|
|
293
|
+
state = applyUiGraphStatePatch(state, statePatch);
|
|
294
|
+
}
|
|
295
|
+
} catch (error) {
|
|
296
|
+
if (!abortController.signal.aborted && actionController.isCurrent(execution)) {
|
|
297
|
+
actionErrors = {
|
|
298
|
+
...actionErrors,
|
|
299
|
+
[component.id]: error instanceof Error ? error.message : String(error)
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
} finally {
|
|
303
|
+
if (activeActions.delete(execution.id)) {
|
|
304
|
+
actionController.finish(execution);
|
|
305
|
+
if (Object.prototype.hasOwnProperty.call(actionProgress, component.id)) {
|
|
306
|
+
actionProgress = { ...actionProgress };
|
|
307
|
+
delete actionProgress[component.id];
|
|
308
|
+
}
|
|
309
|
+
publish("action");
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
setUiGraph(nextUiGraph) {
|
|
314
|
+
if (nextUiGraph.id !== uiGraphId) {
|
|
315
|
+
abortAllActions(false);
|
|
316
|
+
uiGraphId = nextUiGraph.id;
|
|
317
|
+
state = getUiGraphInitialState(nextUiGraph);
|
|
318
|
+
actionErrors = {};
|
|
319
|
+
actionProgress = {};
|
|
320
|
+
publish("graph");
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const actionComponentIds = new Set(
|
|
324
|
+
nextUiGraph.components.filter((component) => component.type === "button" || component.type === "chat").map((component) => component.id)
|
|
325
|
+
);
|
|
326
|
+
let changed = abortMatchingActions(({ execution }) => !actionComponentIds.has(execution.componentId), false);
|
|
327
|
+
const remainingErrors = Object.fromEntries(
|
|
328
|
+
Object.entries(actionErrors).filter(([componentId]) => actionComponentIds.has(componentId))
|
|
329
|
+
);
|
|
330
|
+
const remainingProgress = Object.fromEntries(
|
|
331
|
+
Object.entries(actionProgress).filter(([componentId]) => actionComponentIds.has(componentId))
|
|
332
|
+
);
|
|
333
|
+
if (Object.keys(remainingErrors).length !== Object.keys(actionErrors).length) {
|
|
334
|
+
actionErrors = remainingErrors;
|
|
335
|
+
changed = true;
|
|
336
|
+
}
|
|
337
|
+
if (Object.keys(remainingProgress).length !== Object.keys(actionProgress).length) {
|
|
338
|
+
actionProgress = remainingProgress;
|
|
339
|
+
changed = true;
|
|
340
|
+
}
|
|
341
|
+
if (changed) {
|
|
342
|
+
publish("graph");
|
|
343
|
+
}
|
|
344
|
+
},
|
|
345
|
+
subscribe(listener) {
|
|
346
|
+
listeners.add(listener);
|
|
347
|
+
return () => listeners.delete(listener);
|
|
348
|
+
},
|
|
349
|
+
updateState(stateKey, value) {
|
|
350
|
+
actionController.noteStateWrite(stateKey);
|
|
351
|
+
state = { ...state, [stateKey]: value };
|
|
352
|
+
publish("state");
|
|
353
|
+
},
|
|
354
|
+
updateStatePatch(statePatch) {
|
|
355
|
+
for (const stateKey of Object.keys(statePatch)) {
|
|
356
|
+
actionController.noteStateWrite(stateKey);
|
|
357
|
+
}
|
|
358
|
+
state = applyUiGraphStatePatch(state, statePatch);
|
|
359
|
+
publish("state");
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
function getUiGraphComponentRenderModel(component, state) {
|
|
364
|
+
switch (component.type) {
|
|
365
|
+
case "text":
|
|
366
|
+
return { component, text: component.text, type: "text" };
|
|
367
|
+
case "markdown":
|
|
368
|
+
return { component, markdown: component.markdown, type: "markdown" };
|
|
369
|
+
case "gap":
|
|
370
|
+
return { component, size: component.size, type: "gap" };
|
|
371
|
+
case "input":
|
|
372
|
+
case "textarea":
|
|
373
|
+
return {
|
|
374
|
+
component,
|
|
375
|
+
label: getUiGraphComponentLabel(component),
|
|
376
|
+
type: component.type,
|
|
377
|
+
value: `${state[component.stateKey] ?? component.defaultValue ?? ""}`
|
|
378
|
+
};
|
|
379
|
+
case "button":
|
|
380
|
+
return { component, label: component.label, type: "button" };
|
|
381
|
+
case "chat":
|
|
382
|
+
return {
|
|
383
|
+
component,
|
|
384
|
+
draft: `${state[getUiGraphChatDraftStateKey(component.id)] ?? ""}`,
|
|
385
|
+
messages: getUiGraphChatMessages(component.id, state),
|
|
386
|
+
type: "chat"
|
|
387
|
+
};
|
|
388
|
+
case "output":
|
|
389
|
+
return {
|
|
390
|
+
component,
|
|
391
|
+
label: getUiGraphComponentLabel(component),
|
|
392
|
+
output: getUiGraphOutputRenderModel(state, component.stateKey, component.renderAs ?? "text"),
|
|
393
|
+
type: "output"
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
function getUiGraphComponentLabel(component) {
|
|
398
|
+
return component.label || component.stateKey;
|
|
399
|
+
}
|
|
400
|
+
function getUiGraphOutputRenderModel(state, stateKey, renderAs) {
|
|
401
|
+
const value = state[stateKey];
|
|
402
|
+
const hasValue = hasUiGraphStateValue(state, stateKey);
|
|
403
|
+
const renderedValue = renderUiGraphOutputValue(value, renderAs);
|
|
404
|
+
const imageSource = hasValue && renderAs === "image" ? getUiGraphImageSource(value) : void 0;
|
|
405
|
+
const imageErrorMessage = hasValue && renderAs === "image" && !imageSource ? "Expected an image URL or base64 image." : void 0;
|
|
406
|
+
return {
|
|
407
|
+
hasValue,
|
|
408
|
+
...imageErrorMessage ? { imageErrorMessage } : {},
|
|
409
|
+
...imageSource ? { imageSource } : {},
|
|
410
|
+
...hasValue && renderAs === "json" ? { jsonDownloadValue: stringifyUiGraphValue(value) } : {},
|
|
411
|
+
renderedValue,
|
|
412
|
+
renderAs
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
function hasUiGraphStateValue(state, key) {
|
|
416
|
+
return Object.prototype.hasOwnProperty.call(state, key) && state[key] !== void 0;
|
|
417
|
+
}
|
|
418
|
+
function renderUiGraphOutputValue(value, renderAs) {
|
|
419
|
+
if (renderAs === "json") {
|
|
420
|
+
return stringifyUiGraphValue(value) ?? "";
|
|
421
|
+
}
|
|
422
|
+
return typeof value === "string" ? value : value == null ? "" : stringifyUiGraphValue(value) ?? "";
|
|
423
|
+
}
|
|
424
|
+
function getUiGraphImageSource(value) {
|
|
425
|
+
if (typeof value !== "string") {
|
|
426
|
+
return void 0;
|
|
427
|
+
}
|
|
428
|
+
const source = value.trim();
|
|
429
|
+
if (!source) {
|
|
430
|
+
return void 0;
|
|
431
|
+
}
|
|
432
|
+
const dataUrl = IMAGE_DATA_URL_PATTERN.exec(source);
|
|
433
|
+
if (dataUrl) {
|
|
434
|
+
const base642 = normalizeBase64(dataUrl[2]);
|
|
435
|
+
return base642 ? `data:${normalizeImageMediaType(dataUrl[1])};base64,${base642}` : void 0;
|
|
436
|
+
}
|
|
437
|
+
const base64 = normalizeBase64(source);
|
|
438
|
+
const inferredMediaType = base64 ? inferBase64ImageMediaType(base64) : void 0;
|
|
439
|
+
if (base64 && inferredMediaType) {
|
|
440
|
+
return `data:${inferredMediaType};base64,${base64}`;
|
|
441
|
+
}
|
|
442
|
+
if (!IMAGE_URL_PATTERN.test(source) && !RELATIVE_IMAGE_PATH_PATTERN.test(source)) {
|
|
443
|
+
return void 0;
|
|
444
|
+
}
|
|
445
|
+
try {
|
|
446
|
+
const protocol = new URL(source, "https://rivet.invalid").protocol;
|
|
447
|
+
return protocol === "http:" || protocol === "https:" || protocol === "blob:" ? source : void 0;
|
|
448
|
+
} catch {
|
|
449
|
+
return void 0;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
function normalizeBase64(value) {
|
|
453
|
+
const compact = value.replace(/\s/g, "");
|
|
454
|
+
return compact.length >= 8 && compact.length % 4 !== 1 && BASE64_PATTERN.test(compact) ? compact : void 0;
|
|
455
|
+
}
|
|
456
|
+
function normalizeImageMediaType(mediaType) {
|
|
457
|
+
const normalized = mediaType.toLowerCase();
|
|
458
|
+
return normalized === "image/jpg" ? "image/jpeg" : normalized;
|
|
459
|
+
}
|
|
460
|
+
function inferBase64ImageMediaType(value) {
|
|
461
|
+
if (value.startsWith("iVBORw0KGgo")) return "image/png";
|
|
462
|
+
if (value.startsWith("/9j/")) return "image/jpeg";
|
|
463
|
+
if (value.startsWith("R0lGODdh") || value.startsWith("R0lGODlh")) return "image/gif";
|
|
464
|
+
return void 0;
|
|
465
|
+
}
|
|
466
|
+
function stringifyUiGraphValue(value) {
|
|
467
|
+
try {
|
|
468
|
+
return JSON.stringify(value, null, 2);
|
|
469
|
+
} catch {
|
|
470
|
+
return "[Unserializable value]";
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
function getUiGraphJsonOutputFilename(appName, date = /* @__PURE__ */ new Date()) {
|
|
474
|
+
const safeName = appName.trim().replace(/[<>:"/\\|?*\u0000-\u001F]/g, "-").replace(/\s+/g, " ").slice(0, 80);
|
|
475
|
+
return `${safeName || "Rivet web app"} ${formatUiGraphDownloadDateTime(date)}.json`;
|
|
476
|
+
}
|
|
477
|
+
function formatUiGraphDownloadDateTime(date) {
|
|
478
|
+
const pad = (value) => `${value}`.padStart(2, "0");
|
|
479
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}-${pad(
|
|
480
|
+
date.getMinutes()
|
|
481
|
+
)}-${pad(date.getSeconds())}`;
|
|
482
|
+
}
|
|
483
|
+
function applyUiGraphStatePatch(state, statePatch) {
|
|
484
|
+
return statePatch ? { ...state, ...statePatch } : state;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// src/model/UiGraphActionProtocol.ts
|
|
488
|
+
var RIVET_WEB_APP_ACTION_PROTOCOL_VERSION = 1;
|
|
489
|
+
function parseRivetWebAppClientMessage(value) {
|
|
490
|
+
if (!isRecord(value) || typeof value.type !== "string") return void 0;
|
|
491
|
+
switch (value.type) {
|
|
492
|
+
case "client.hello":
|
|
493
|
+
return value.protocolVersion === RIVET_WEB_APP_ACTION_PROTOCOL_VERSION ? { type: value.type, protocolVersion: value.protocolVersion } : void 0;
|
|
494
|
+
case "action.start":
|
|
495
|
+
return isNonEmptyString(value.requestId) && isNonEmptyString(value.componentId) && isRecord(value.state) && (value.revisionKey == null || typeof value.revisionKey === "string") ? {
|
|
496
|
+
type: value.type,
|
|
497
|
+
requestId: value.requestId,
|
|
498
|
+
componentId: value.componentId,
|
|
499
|
+
state: value.state,
|
|
500
|
+
...typeof value.revisionKey === "string" ? { revisionKey: value.revisionKey } : {}
|
|
501
|
+
} : void 0;
|
|
502
|
+
case "action.cancel":
|
|
503
|
+
return isNonEmptyString(value.runId) ? { type: value.type, runId: value.runId } : void 0;
|
|
504
|
+
case "run.resume":
|
|
505
|
+
return isNonEmptyString(value.runId) && typeof value.lastSequence === "number" && Number.isSafeInteger(value.lastSequence) && value.lastSequence >= 0 ? { type: value.type, runId: value.runId, lastSequence: value.lastSequence } : void 0;
|
|
506
|
+
default:
|
|
507
|
+
return void 0;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
function parseRivetWebAppServerMessage(value) {
|
|
511
|
+
if (!isRecord(value) || typeof value.type !== "string") return void 0;
|
|
512
|
+
if (value.type === "server.ready") {
|
|
513
|
+
return value.protocolVersion === RIVET_WEB_APP_ACTION_PROTOCOL_VERSION ? { type: value.type, protocolVersion: value.protocolVersion } : void 0;
|
|
514
|
+
}
|
|
515
|
+
if (value.type === "server.draining") return { type: value.type };
|
|
516
|
+
if (value.type === "action.rejected") {
|
|
517
|
+
return isNonEmptyString(value.requestId) && isNonEmptyString(value.error) ? {
|
|
518
|
+
type: value.type,
|
|
519
|
+
requestId: value.requestId,
|
|
520
|
+
error: value.error,
|
|
521
|
+
...typeof value.code === "string" ? { code: value.code } : {}
|
|
522
|
+
} : void 0;
|
|
523
|
+
}
|
|
524
|
+
if (value.type === "run.rejected") {
|
|
525
|
+
return isNonEmptyString(value.runId) && isNonEmptyString(value.error) ? {
|
|
526
|
+
type: value.type,
|
|
527
|
+
runId: value.runId,
|
|
528
|
+
error: value.error,
|
|
529
|
+
...typeof value.code === "string" ? { code: value.code } : {}
|
|
530
|
+
} : void 0;
|
|
531
|
+
}
|
|
532
|
+
if (!hasRunEventBase(value)) return void 0;
|
|
533
|
+
const base = {
|
|
534
|
+
requestId: value.requestId,
|
|
535
|
+
runId: value.runId,
|
|
536
|
+
sequence: value.sequence
|
|
537
|
+
};
|
|
538
|
+
switch (value.type) {
|
|
539
|
+
case "action.accepted":
|
|
540
|
+
case "action.cancelled":
|
|
541
|
+
return { type: value.type, ...base };
|
|
542
|
+
case "action.progress": {
|
|
543
|
+
if (!isRecord(value.progress)) return void 0;
|
|
544
|
+
const progress = normalizeGraphProgress({
|
|
545
|
+
message: typeof value.progress.message === "string" ? value.progress.message : void 0,
|
|
546
|
+
percent: typeof value.progress.percent === "number" ? value.progress.percent : void 0
|
|
547
|
+
});
|
|
548
|
+
return progress ? { type: value.type, ...base, progress } : void 0;
|
|
549
|
+
}
|
|
550
|
+
case "action.completed":
|
|
551
|
+
return isRecord(value.statePatch) ? { type: value.type, ...base, statePatch: value.statePatch } : void 0;
|
|
552
|
+
case "action.failed":
|
|
553
|
+
return isNonEmptyString(value.error) ? {
|
|
554
|
+
type: value.type,
|
|
555
|
+
...base,
|
|
556
|
+
error: value.error,
|
|
557
|
+
...typeof value.code === "string" ? { code: value.code } : {}
|
|
558
|
+
} : void 0;
|
|
559
|
+
case "action.interrupted":
|
|
560
|
+
return isNonEmptyString(value.error) ? { type: value.type, ...base, error: value.error } : void 0;
|
|
561
|
+
default:
|
|
562
|
+
return void 0;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
function hasRunEventBase(value) {
|
|
566
|
+
return isNonEmptyString(value.requestId) && isNonEmptyString(value.runId) && typeof value.sequence === "number" && Number.isSafeInteger(value.sequence) && value.sequence > 0;
|
|
567
|
+
}
|
|
568
|
+
function isRecord(value) {
|
|
569
|
+
return typeof value === "object" && value != null && !Array.isArray(value);
|
|
570
|
+
}
|
|
571
|
+
function isNonEmptyString(value) {
|
|
572
|
+
return typeof value === "string" && value.length > 0;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// src/model/UiGraphBrowserRuntime.ts
|
|
576
|
+
async function copyUiGraphText(value) {
|
|
577
|
+
var _a;
|
|
578
|
+
try {
|
|
579
|
+
await navigator.clipboard.writeText(value);
|
|
580
|
+
return true;
|
|
581
|
+
} catch {
|
|
582
|
+
}
|
|
583
|
+
const textArea = document.createElement("textarea");
|
|
584
|
+
textArea.className = "rivet-web-app-clipboard-fallback";
|
|
585
|
+
textArea.value = value;
|
|
586
|
+
try {
|
|
587
|
+
document.body.append(textArea);
|
|
588
|
+
textArea.select();
|
|
589
|
+
return ((_a = document.execCommand) == null ? void 0 : _a.call(document, "copy")) ?? false;
|
|
590
|
+
} catch {
|
|
591
|
+
return false;
|
|
592
|
+
} finally {
|
|
593
|
+
textArea.remove();
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
function downloadUiGraphJsonOutput(value, appName) {
|
|
597
|
+
const url = URL.createObjectURL(new Blob([value], { type: "application/json" }));
|
|
598
|
+
try {
|
|
599
|
+
const link = document.createElement("a");
|
|
600
|
+
link.href = url;
|
|
601
|
+
link.download = getUiGraphJsonOutputFilename(appName);
|
|
602
|
+
document.body.append(link);
|
|
603
|
+
link.click();
|
|
604
|
+
link.remove();
|
|
605
|
+
} finally {
|
|
606
|
+
globalThis.setTimeout(() => URL.revokeObjectURL(url), 0);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
610
|
+
0 && (module.exports = {
|
|
611
|
+
RIVET_WEB_APP_ACTION_PROTOCOL_VERSION,
|
|
612
|
+
applyUiGraphStatePatch,
|
|
613
|
+
copyUiGraphText,
|
|
614
|
+
createUiGraphActionExecutionController,
|
|
615
|
+
createUiGraphChatSubmissionStatePatch,
|
|
616
|
+
createUiGraphInteractionController,
|
|
617
|
+
downloadUiGraphJsonOutput,
|
|
618
|
+
getUiGraphActionState,
|
|
619
|
+
getUiGraphChatDraftStateKey,
|
|
620
|
+
getUiGraphComponentRenderModel,
|
|
621
|
+
getUiGraphJsonOutputFilename,
|
|
622
|
+
parseRivetWebAppClientMessage,
|
|
623
|
+
parseRivetWebAppServerMessage
|
|
624
|
+
});
|
|
625
|
+
//# sourceMappingURL=webAppRuntime.cjs.map
|