@valerypopoff/rivet2-core 2.33.0 → 2.34.1
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 +161 -51
- package/dist/cjs/bundle.cjs.map +3 -3
- package/dist/esm/model/nodes/HttpCallNode.js +25 -1
- package/dist/esm/recording/ExecutionRecorder.js +131 -48
- package/dist/esm/vendor/fnv1a.js +9 -2
- package/dist/types/recording/ExecutionRecorder.d.ts +8 -0
- package/package.json +1 -1
|
@@ -428,6 +428,22 @@ export class HttpCallNodeImpl extends NodeImpl {
|
|
|
428
428
|
}
|
|
429
429
|
async process(inputs, context) {
|
|
430
430
|
const requestAttempts = this.data.retryOnNon200 ? createHttpCallRequestAttempts() : undefined;
|
|
431
|
+
let ownedResponse;
|
|
432
|
+
const discardResponse = () => {
|
|
433
|
+
const response = ownedResponse;
|
|
434
|
+
ownedResponse = undefined;
|
|
435
|
+
try {
|
|
436
|
+
const body = response?.body;
|
|
437
|
+
if (body && !body.locked) {
|
|
438
|
+
// Custom streams can reject or never finish cancellation. Releasing
|
|
439
|
+
// an unused reply must not delay a run or replace its original error.
|
|
440
|
+
void body.cancel().catch(() => undefined);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
catch {
|
|
444
|
+
// A custom Fetch body can also throw synchronously during cleanup.
|
|
445
|
+
}
|
|
446
|
+
};
|
|
431
447
|
try {
|
|
432
448
|
const method = getInputOrData(this.data, inputs, 'method', 'string');
|
|
433
449
|
const url = getInputOrData(this.data, inputs, 'url', 'string');
|
|
@@ -472,13 +488,15 @@ export class HttpCallNodeImpl extends NodeImpl {
|
|
|
472
488
|
}
|
|
473
489
|
const performRequest = async () => {
|
|
474
490
|
try {
|
|
475
|
-
|
|
491
|
+
const response = await fetch(url, {
|
|
476
492
|
method,
|
|
477
493
|
headers,
|
|
478
494
|
body,
|
|
479
495
|
signal: context.signal,
|
|
480
496
|
mode: 'cors',
|
|
481
497
|
});
|
|
498
|
+
ownedResponse = response;
|
|
499
|
+
return response;
|
|
482
500
|
}
|
|
483
501
|
catch (err) {
|
|
484
502
|
if (isAbortError(err, context.signal)) {
|
|
@@ -510,6 +528,7 @@ export class HttpCallNodeImpl extends NodeImpl {
|
|
|
510
528
|
const repeatTimes = normalizeHttpRetryCount(this.data.retryOnNon200RepeatTimes);
|
|
511
529
|
const cooldownMs = normalizeHttpRetryCooldownMs(this.data.retryOnNon200CooldownMs);
|
|
512
530
|
for (let attempt = 0; response.status !== 200 && attempt < repeatTimes; attempt++) {
|
|
531
|
+
discardResponse();
|
|
513
532
|
await waitForRetryCooldown(cooldownMs, context.signal);
|
|
514
533
|
response = await performTrackedRequest();
|
|
515
534
|
}
|
|
@@ -537,6 +556,7 @@ export class HttpCallNodeImpl extends NodeImpl {
|
|
|
537
556
|
const output = responseMetadataOutputs;
|
|
538
557
|
if (this.data.isBinaryOutput) {
|
|
539
558
|
const responseBlob = await response.blob();
|
|
559
|
+
ownedResponse = undefined;
|
|
540
560
|
output['binary'] = {
|
|
541
561
|
type: 'binary',
|
|
542
562
|
value: new Uint8Array(await responseBlob.arrayBuffer()),
|
|
@@ -544,6 +564,7 @@ export class HttpCallNodeImpl extends NodeImpl {
|
|
|
544
564
|
}
|
|
545
565
|
else {
|
|
546
566
|
const responseText = await response.text();
|
|
567
|
+
ownedResponse = undefined;
|
|
547
568
|
output['res_body'] = {
|
|
548
569
|
type: 'string',
|
|
549
570
|
value: responseText,
|
|
@@ -589,6 +610,9 @@ export class HttpCallNodeImpl extends NodeImpl {
|
|
|
589
610
|
}
|
|
590
611
|
throw error;
|
|
591
612
|
}
|
|
613
|
+
finally {
|
|
614
|
+
discardResponse();
|
|
615
|
+
}
|
|
592
616
|
}
|
|
593
617
|
}
|
|
594
618
|
export const httpCallNode = nodeDefinition(HttpCallNodeImpl, 'Http Call');
|
|
@@ -31,14 +31,14 @@ const toRecordedEventMap = {
|
|
|
31
31
|
processId,
|
|
32
32
|
...(resultOrigin === undefined ? {} : { resultOrigin }),
|
|
33
33
|
}, execution),
|
|
34
|
-
nodeFinish: ({ node, outputs, processId, resultOrigin, durationMs, splitRunDurationMs, streamingWatchTerminal, execution }) => withExecution(withDuration({
|
|
34
|
+
nodeFinish: ({ node, outputs, processId, resultOrigin, durationMs, splitRunDurationMs, streamingWatchTerminal, execution, }) => withExecution(withDuration({
|
|
35
35
|
nodeId: node.id,
|
|
36
36
|
outputs,
|
|
37
37
|
processId,
|
|
38
38
|
...(resultOrigin === undefined ? {} : { resultOrigin }),
|
|
39
39
|
...(streamingWatchTerminal ? { streamingWatchTerminal: true } : {}),
|
|
40
40
|
}, durationMs, splitRunDurationMs), execution),
|
|
41
|
-
nodeError: ({ node, error, processId, outputs, splitOutputs, resultOrigin, durationMs, splitRunDurationMs, execution }) => withExecution(withDuration({
|
|
41
|
+
nodeError: ({ node, error, processId, outputs, splitOutputs, resultOrigin, durationMs, splitRunDurationMs, execution, }) => withExecution(withDuration({
|
|
42
42
|
nodeId: node.id,
|
|
43
43
|
error: typeof error === 'string' ? error : error.stack,
|
|
44
44
|
processId,
|
|
@@ -173,16 +173,20 @@ function mapValuesDeep(obj, fn) {
|
|
|
173
173
|
});
|
|
174
174
|
}
|
|
175
175
|
if (isPlainObject(obj)) {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
176
|
+
// Read the properties before recursing, preserving accessor order. Reuse
|
|
177
|
+
// these copied entries instead of allocating a second array of key/value
|
|
178
|
+
// pairs. fromEntries also keeps __proto__ an ordinary own property.
|
|
179
|
+
const entries = Object.entries(obj);
|
|
180
|
+
for (const entry of entries) {
|
|
181
|
+
const value = entry[1];
|
|
182
|
+
entry[1] = isPlainObject(value) || Array.isArray(value) ? mapValuesDeep(value, fn) : fn(value);
|
|
183
|
+
}
|
|
184
|
+
return Object.fromEntries(entries);
|
|
182
185
|
}
|
|
183
186
|
return fn(obj);
|
|
184
187
|
}
|
|
185
188
|
function serializeToObject(recording) {
|
|
189
|
+
const stringHashes = new Map();
|
|
186
190
|
const serialized = {
|
|
187
191
|
version: 1,
|
|
188
192
|
recording,
|
|
@@ -203,9 +207,13 @@ function serializeToObject(recording) {
|
|
|
203
207
|
return `$ASSET:${id}`;
|
|
204
208
|
}
|
|
205
209
|
}
|
|
206
|
-
if (typeof val === 'string' && !val.startsWith('$ASSET:')
|
|
207
|
-
|
|
208
|
-
|
|
210
|
+
if (typeof val === 'string' && val.length > 30 && !val.startsWith('$ASSET:')) {
|
|
211
|
+
let hash = stringHashes.get(val);
|
|
212
|
+
if (hash === undefined) {
|
|
213
|
+
hash = String(fnv1a(val, { size: 32 }));
|
|
214
|
+
stringHashes.set(val, hash);
|
|
215
|
+
}
|
|
216
|
+
serialized.strings[hash] = val;
|
|
209
217
|
return `$STRING:${hash}`;
|
|
210
218
|
}
|
|
211
219
|
return val;
|
|
@@ -243,26 +251,67 @@ export class ExecutionRecorder {
|
|
|
243
251
|
#emitter;
|
|
244
252
|
#includePartialOutputs;
|
|
245
253
|
#includeTrace;
|
|
254
|
+
#capturePolicy;
|
|
255
|
+
#captureDiscarded = false;
|
|
246
256
|
constructor(options = {}) {
|
|
247
257
|
this.#emitter = new Emittery();
|
|
248
258
|
this.#emitter.bindMethods(this, ['on', 'off', 'once']);
|
|
249
259
|
this.#includePartialOutputs = options.includePartialOutputs ?? false;
|
|
250
260
|
this.#includeTrace = options.includeTrace ?? false;
|
|
261
|
+
try {
|
|
262
|
+
this.#capturePolicy = options.createCapturePolicy?.(this);
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
this.#captureDiscarded = true;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
get captureDiscarded() {
|
|
269
|
+
return this.#captureDiscarded;
|
|
270
|
+
}
|
|
271
|
+
/** Release replay data, without cancelling the graph or its terminal listener. */
|
|
272
|
+
discard() {
|
|
273
|
+
this.#captureDiscarded = true;
|
|
274
|
+
this.#events.length = 0;
|
|
275
|
+
const policy = this.#capturePolicy;
|
|
276
|
+
this.#capturePolicy = undefined;
|
|
277
|
+
try {
|
|
278
|
+
policy?.release();
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
/* Recording accounting cannot fail a graph. */
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
#append(event) {
|
|
285
|
+
if (this.#captureDiscarded)
|
|
286
|
+
return;
|
|
287
|
+
try {
|
|
288
|
+
if (this.#capturePolicy && !this.#capturePolicy.retain(event)) {
|
|
289
|
+
this.discard();
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
this.discard();
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
this.#events.push(event);
|
|
251
298
|
}
|
|
252
299
|
on = undefined;
|
|
253
300
|
off = undefined;
|
|
254
301
|
once = undefined;
|
|
255
302
|
recordSocket(channel, options = {}) {
|
|
256
|
-
return new Promise((resolve) => {
|
|
257
|
-
this.recordingId = nanoid();
|
|
303
|
+
return new Promise((resolve, reject) => {
|
|
258
304
|
let settled = false;
|
|
305
|
+
const detach = () => {
|
|
306
|
+
channel.removeEventListener('message', listener);
|
|
307
|
+
channel.removeEventListener('close', onClose);
|
|
308
|
+
options.signal?.removeEventListener('abort', onOwnerDisposed);
|
|
309
|
+
};
|
|
259
310
|
const finish = (emitFinishedRecording) => {
|
|
260
311
|
if (settled)
|
|
261
312
|
return;
|
|
262
313
|
settled = true;
|
|
263
|
-
|
|
264
|
-
channel.removeEventListener('close', onClose);
|
|
265
|
-
options.signal?.removeEventListener('abort', onOwnerDisposed);
|
|
314
|
+
detach();
|
|
266
315
|
if (emitFinishedRecording) {
|
|
267
316
|
emitDetached(this.#emitter, 'finish', {
|
|
268
317
|
recording: this.getRecording(),
|
|
@@ -298,64 +347,94 @@ export class ExecutionRecorder {
|
|
|
298
347
|
if (getEventProjectScope(data) !== undefined) {
|
|
299
348
|
return;
|
|
300
349
|
}
|
|
301
|
-
this.#
|
|
350
|
+
if (!this.#captureDiscarded)
|
|
351
|
+
this.#append(toRecordedEvent(message, data));
|
|
302
352
|
if (isSocketRecordingTerminalEvent(message)) {
|
|
303
353
|
finish(true);
|
|
304
354
|
}
|
|
305
355
|
};
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
356
|
+
try {
|
|
357
|
+
this.recordingId = nanoid();
|
|
358
|
+
if (options.signal?.aborted) {
|
|
359
|
+
onOwnerDisposed();
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
channel.addEventListener('message', listener);
|
|
363
|
+
channel.addEventListener('close', onClose, { once: true });
|
|
364
|
+
options.signal?.addEventListener('abort', onOwnerDisposed, { once: true });
|
|
365
|
+
if (channel.readyState === CLOSED_WEBSOCKET_READY_STATE)
|
|
366
|
+
onClose();
|
|
309
367
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
368
|
+
catch (error) {
|
|
369
|
+
settled = true;
|
|
370
|
+
try {
|
|
371
|
+
detach();
|
|
372
|
+
}
|
|
373
|
+
catch {
|
|
374
|
+
/* Keep the original setup failure and release capture ownership. */
|
|
375
|
+
}
|
|
376
|
+
this.discard();
|
|
377
|
+
reject(error);
|
|
315
378
|
}
|
|
316
379
|
});
|
|
317
380
|
}
|
|
318
381
|
record(processor) {
|
|
319
|
-
|
|
320
|
-
|
|
382
|
+
let unsubscribeAny;
|
|
383
|
+
let unsubscribeFinish;
|
|
321
384
|
let settled = false;
|
|
322
385
|
const finish = () => {
|
|
323
386
|
if (settled)
|
|
324
387
|
return;
|
|
325
388
|
settled = true;
|
|
326
|
-
unsubscribeAny();
|
|
327
|
-
unsubscribeFinish();
|
|
389
|
+
unsubscribeAny?.();
|
|
390
|
+
unsubscribeFinish?.();
|
|
328
391
|
emitDetached(this.#emitter, 'finish', {
|
|
329
392
|
recording: this.getRecording(),
|
|
330
393
|
});
|
|
331
394
|
};
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
395
|
+
try {
|
|
396
|
+
this.recordingId = nanoid();
|
|
397
|
+
const recordingProjectScope = processor.recordingProjectScope;
|
|
398
|
+
unsubscribeAny = processor.onAny((event, data) => {
|
|
399
|
+
if (settled || event === 'finish') {
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
if (this.#includePartialOutputs === false && event === 'partialOutput') {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
if (this.#includeTrace === false && event === 'trace') {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
const eventScope = getEventProjectScope(data);
|
|
409
|
+
if (eventScope !== undefined && eventScope !== recordingProjectScope) {
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
if (!this.#captureDiscarded)
|
|
413
|
+
this.#append(toRecordedEvent(event, data));
|
|
414
|
+
});
|
|
415
|
+
unsubscribeFinish = processor.on('finish', finish);
|
|
416
|
+
}
|
|
417
|
+
catch (error) {
|
|
418
|
+
settled = true;
|
|
419
|
+
try {
|
|
420
|
+
unsubscribeAny?.();
|
|
341
421
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
return;
|
|
422
|
+
catch {
|
|
423
|
+
/* Keep the original setup failure and release capture ownership. */
|
|
345
424
|
}
|
|
346
|
-
this
|
|
347
|
-
|
|
348
|
-
|
|
425
|
+
this.discard();
|
|
426
|
+
throw error;
|
|
427
|
+
}
|
|
349
428
|
// Child processors have graph terminals but no root-level done/finish.
|
|
350
429
|
// Their owner calls this after processGraph settles to make a standalone,
|
|
351
430
|
// replayable recording without changing the live event stream.
|
|
352
431
|
return (terminal) => {
|
|
353
432
|
if (settled)
|
|
354
433
|
return;
|
|
355
|
-
if (terminal?.type === 'done')
|
|
356
|
-
this.#
|
|
357
|
-
if (terminal?.type === 'error')
|
|
358
|
-
this.#
|
|
434
|
+
if (!this.#captureDiscarded && terminal?.type === 'done')
|
|
435
|
+
this.#append(toRecordedEvent('done', { results: terminal.results }));
|
|
436
|
+
if (!this.#captureDiscarded && terminal?.type === 'error')
|
|
437
|
+
this.#append(toRecordedEvent('error', { error: terminal.error }));
|
|
359
438
|
finish();
|
|
360
439
|
};
|
|
361
440
|
}
|
|
@@ -393,9 +472,13 @@ export class ExecutionRecorder {
|
|
|
393
472
|
return recorder;
|
|
394
473
|
}
|
|
395
474
|
serialize() {
|
|
475
|
+
if (this.#captureDiscarded)
|
|
476
|
+
throw new Error('Recording capture is unavailable.');
|
|
396
477
|
return JSON.stringify(serializeToObject(this.getRecording()));
|
|
397
478
|
}
|
|
398
479
|
serializeStream() {
|
|
480
|
+
if (this.#captureDiscarded)
|
|
481
|
+
throw new Error('Recording capture is unavailable.');
|
|
399
482
|
return stringifyJsonStream(serializeToObject(this.getRecording()));
|
|
400
483
|
}
|
|
401
484
|
}
|
package/dist/esm/vendor/fnv1a.js
CHANGED
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
17
17
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
18
18
|
*/
|
|
19
|
-
//
|
|
20
|
-
// with types added
|
|
19
|
+
// Adapted from https://github.com/sindresorhus/fnv1a/blob/main/index.js
|
|
20
|
+
// with types and an equivalent 32-bit arithmetic fast path added.
|
|
21
21
|
const FNV_PRIMES = {
|
|
22
22
|
32: 16777619n,
|
|
23
23
|
64: 1099511628211n,
|
|
@@ -36,6 +36,13 @@ const FNV_OFFSETS = {
|
|
|
36
36
|
};
|
|
37
37
|
const cachedEncoder = new globalThis.TextEncoder();
|
|
38
38
|
function fnv1aUint8Array(uint8Array, size) {
|
|
39
|
+
if (size === 32) {
|
|
40
|
+
let hash = 2_166_136_261;
|
|
41
|
+
for (let index = 0; index < uint8Array.length; index++) {
|
|
42
|
+
hash = Math.imul(hash ^ uint8Array[index], 16_777_619);
|
|
43
|
+
}
|
|
44
|
+
return BigInt(hash >>> 0);
|
|
45
|
+
}
|
|
39
46
|
const fnvPrime = FNV_PRIMES[size];
|
|
40
47
|
let hash = FNV_OFFSETS[size];
|
|
41
48
|
for (let index = 0; index < uint8Array.length; index++) {
|
|
@@ -8,6 +8,11 @@ export type ExecutionRecorderEvents = {
|
|
|
8
8
|
export type ExecutionRecorderOptions = {
|
|
9
9
|
includePartialOutputs?: boolean;
|
|
10
10
|
includeTrace?: boolean;
|
|
11
|
+
/** Optional host accounting. Each recorder owns a separate capture lease. */
|
|
12
|
+
createCapturePolicy?: (recorder: ExecutionRecorder) => {
|
|
13
|
+
retain(event: RecordedEvents): boolean;
|
|
14
|
+
release(): void;
|
|
15
|
+
};
|
|
11
16
|
};
|
|
12
17
|
export type SocketRecordingOptions = {
|
|
13
18
|
/** Record only the protocol messages belonging to one remote graph run. */
|
|
@@ -28,6 +33,9 @@ export declare class ExecutionRecorder {
|
|
|
28
33
|
#private;
|
|
29
34
|
recordingId: RecordingId | undefined;
|
|
30
35
|
constructor(options?: ExecutionRecorderOptions);
|
|
36
|
+
get captureDiscarded(): boolean;
|
|
37
|
+
/** Release replay data, without cancelling the graph or its terminal listener. */
|
|
38
|
+
discard(): void;
|
|
31
39
|
on: Emittery<ExecutionRecorderEvents>['on'];
|
|
32
40
|
off: Emittery<ExecutionRecorderEvents>['off'];
|
|
33
41
|
once: Emittery<ExecutionRecorderEvents>['once'];
|