agentkernel 0.19.1 → 0.20.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/index.js CHANGED
@@ -361,8 +361,344 @@ var SandboxSession = class {
361
361
  }
362
362
  };
363
363
 
364
+ // node_modules/eventsource-parser/dist/errors.js
365
+ var ParseError = class extends Error {
366
+ constructor(message, options) {
367
+ super(message);
368
+ this.name = "ParseError";
369
+ this.type = options.type;
370
+ this.field = options.field;
371
+ this.value = options.value;
372
+ this.line = options.line;
373
+ }
374
+ };
375
+
376
+ // node_modules/eventsource-parser/dist/parse.js
377
+ var LF = 10;
378
+ var CR = 13;
379
+ var SPACE = 32;
380
+ var MAX_FIELD_PREFIX_LENGTH = 6;
381
+ function createParser(config) {
382
+ if (typeof config === "function") {
383
+ throw new TypeError("`config` must be an object, got a function instead. Did you mean `createParser({onEvent: fn})`?");
384
+ }
385
+ const { maxBufferSize, onComment, onError, onEvent, onId, onRetry } = config;
386
+ const pendingFragments = [];
387
+ let pendingFragmentsLength = 0;
388
+ let isFirstChunk = true;
389
+ let id;
390
+ let data = "";
391
+ let dataLines = 0;
392
+ let eventType;
393
+ let terminated = false;
394
+ let skippingLine = false;
395
+ let skipNextLineFeed = false;
396
+ function feed(chunk) {
397
+ if (terminated) {
398
+ throw new Error("Cannot feed parser: it was terminated after exceeding the configured max buffer size. Call `reset()` to resume parsing.");
399
+ }
400
+ if (isFirstChunk) {
401
+ isFirstChunk = false;
402
+ if (chunk.charCodeAt(0) === 65279) {
403
+ chunk = chunk.slice(1);
404
+ } else if (chunk.charCodeAt(0) === 239 && chunk.charCodeAt(1) === 187 && chunk.charCodeAt(2) === 191) {
405
+ chunk = chunk.slice(3);
406
+ }
407
+ }
408
+ if (skippingLine || skipNextLineFeed) {
409
+ chunk = resumeAfterSkip(chunk);
410
+ if (!chunk) {
411
+ return;
412
+ }
413
+ }
414
+ if (!pendingFragments.length) {
415
+ const trailing = processLines(chunk);
416
+ if (trailing !== "") {
417
+ storeTrailing(trailing);
418
+ }
419
+ checkBufferSize();
420
+ return;
421
+ }
422
+ if (chunk.indexOf("\n") === -1 && chunk.indexOf("\r") === -1) {
423
+ if (pendingFragmentsLength < MAX_FIELD_PREFIX_LENGTH) {
424
+ const head = pendingFragments.join("") + chunk.slice(0, MAX_FIELD_PREFIX_LENGTH - pendingFragmentsLength);
425
+ if (!shouldBufferTrailing(head)) {
426
+ pendingFragments.length = 0;
427
+ pendingFragmentsLength = 0;
428
+ skippingLine = true;
429
+ return;
430
+ }
431
+ }
432
+ pendingFragments.push(chunk);
433
+ pendingFragmentsLength += chunk.length;
434
+ checkBufferSize();
435
+ return;
436
+ }
437
+ pendingFragments.push(chunk);
438
+ const input = pendingFragments.join("");
439
+ pendingFragments.length = 0;
440
+ pendingFragmentsLength = 0;
441
+ storeTrailing(processLines(input));
442
+ checkBufferSize();
443
+ }
444
+ function resumeAfterSkip(chunk) {
445
+ if (chunk.length === 0) {
446
+ return chunk;
447
+ }
448
+ if (skipNextLineFeed) {
449
+ skipNextLineFeed = false;
450
+ return chunk.charCodeAt(0) === LF ? chunk.slice(1) : chunk;
451
+ }
452
+ const crIndex = chunk.indexOf("\r");
453
+ const lfIndex = chunk.indexOf("\n");
454
+ const lineEnd = crIndex === -1 ? lfIndex : lfIndex === -1 ? crIndex : crIndex < lfIndex ? crIndex : lfIndex;
455
+ if (lineEnd === -1) {
456
+ return "";
457
+ }
458
+ if (lineEnd === chunk.length - 1 && chunk.charCodeAt(lineEnd) === CR) {
459
+ skippingLine = false;
460
+ skipNextLineFeed = true;
461
+ return "";
462
+ }
463
+ skippingLine = false;
464
+ return chunk.slice(lineEnd + (chunk.charCodeAt(lineEnd) === CR && chunk.charCodeAt(lineEnd + 1) === LF ? 2 : 1));
465
+ }
466
+ function storeTrailing(trailing) {
467
+ if (!trailing)
468
+ return;
469
+ if (trailing.charCodeAt(trailing.length - 1) === CR) {
470
+ parseLine(trailing, 0, trailing.length - 1);
471
+ skipNextLineFeed = true;
472
+ return;
473
+ }
474
+ if (shouldBufferTrailing(trailing)) {
475
+ pendingFragments.push(trailing);
476
+ pendingFragmentsLength = trailing.length;
477
+ return;
478
+ }
479
+ skippingLine = true;
480
+ }
481
+ function shouldBufferTrailing(trailing) {
482
+ const firstCharCode = trailing.charCodeAt(0);
483
+ return firstCharCode === 58 && !!onComment || firstCharCode === 100 && isPotentialField(trailing, "data") || firstCharCode === 101 && isPotentialField(trailing, "event") || firstCharCode === 105 && isPotentialField(trailing, "id") || firstCharCode === 114 && isPotentialField(trailing, "retry");
484
+ }
485
+ function checkBufferSize() {
486
+ if (maxBufferSize === void 0)
487
+ return;
488
+ if (pendingFragmentsLength + data.length <= maxBufferSize)
489
+ return;
490
+ terminated = true;
491
+ pendingFragments.length = 0;
492
+ pendingFragmentsLength = 0;
493
+ id = void 0;
494
+ data = "";
495
+ dataLines = 0;
496
+ eventType = void 0;
497
+ skippingLine = false;
498
+ skipNextLineFeed = false;
499
+ onError === null || onError === void 0 ? void 0 : onError(new ParseError(`Buffered data exceeded max buffer size of ${maxBufferSize} characters`, {
500
+ type: "max-buffer-size-exceeded"
501
+ }));
502
+ }
503
+ function processLines(chunk) {
504
+ let searchIndex = 0;
505
+ if (chunk.indexOf("\r") === -1) {
506
+ let lfIndex = chunk.indexOf("\n", searchIndex);
507
+ while (lfIndex !== -1) {
508
+ if (searchIndex === lfIndex) {
509
+ if (id !== void 0) {
510
+ onId === null || onId === void 0 ? void 0 : onId(id);
511
+ }
512
+ if (dataLines > 0) {
513
+ onEvent === null || onEvent === void 0 ? void 0 : onEvent({ id, event: eventType, data });
514
+ }
515
+ id = void 0;
516
+ data = "";
517
+ dataLines = 0;
518
+ eventType = void 0;
519
+ searchIndex = lfIndex + 1;
520
+ lfIndex = chunk.indexOf("\n", searchIndex);
521
+ continue;
522
+ }
523
+ const firstCharCode = chunk.charCodeAt(searchIndex);
524
+ if (isDataPrefix(chunk, searchIndex, firstCharCode)) {
525
+ const valueStart = chunk.charCodeAt(searchIndex + 5) === SPACE ? searchIndex + 6 : searchIndex + 5;
526
+ const value = chunk.slice(valueStart, lfIndex);
527
+ if (dataLines === 0 && chunk.charCodeAt(lfIndex + 1) === LF) {
528
+ if (id !== void 0) {
529
+ onId === null || onId === void 0 ? void 0 : onId(id);
530
+ }
531
+ onEvent === null || onEvent === void 0 ? void 0 : onEvent({ id, event: eventType, data: value });
532
+ id = void 0;
533
+ data = "";
534
+ eventType = void 0;
535
+ searchIndex = lfIndex + 2;
536
+ lfIndex = chunk.indexOf("\n", searchIndex);
537
+ continue;
538
+ }
539
+ data = dataLines === 0 ? value : `${data}
540
+ ${value}`;
541
+ dataLines++;
542
+ } else if (isEventPrefix(chunk, searchIndex, firstCharCode)) {
543
+ eventType = chunk.slice(chunk.charCodeAt(searchIndex + 6) === SPACE ? searchIndex + 7 : searchIndex + 6, lfIndex) || void 0;
544
+ } else {
545
+ parseLine(chunk, searchIndex, lfIndex);
546
+ }
547
+ searchIndex = lfIndex + 1;
548
+ lfIndex = chunk.indexOf("\n", searchIndex);
549
+ }
550
+ return chunk.slice(searchIndex);
551
+ }
552
+ while (searchIndex < chunk.length) {
553
+ const crIndex = chunk.indexOf("\r", searchIndex);
554
+ const lfIndex = chunk.indexOf("\n", searchIndex);
555
+ let lineEnd = -1;
556
+ if (crIndex !== -1 && lfIndex !== -1) {
557
+ lineEnd = crIndex < lfIndex ? crIndex : lfIndex;
558
+ } else if (crIndex !== -1) {
559
+ if (crIndex === chunk.length - 1) {
560
+ lineEnd = -1;
561
+ } else {
562
+ lineEnd = crIndex;
563
+ }
564
+ } else if (lfIndex !== -1) {
565
+ lineEnd = lfIndex;
566
+ }
567
+ if (lineEnd === -1) {
568
+ break;
569
+ }
570
+ parseLine(chunk, searchIndex, lineEnd);
571
+ searchIndex = lineEnd + 1;
572
+ if (chunk.charCodeAt(searchIndex - 1) === CR && chunk.charCodeAt(searchIndex) === LF) {
573
+ searchIndex++;
574
+ }
575
+ }
576
+ return chunk.slice(searchIndex);
577
+ }
578
+ function parseLine(chunk, start, end) {
579
+ if (start === end) {
580
+ dispatchEvent();
581
+ return;
582
+ }
583
+ const firstCharCode = chunk.charCodeAt(start);
584
+ if (isDataPrefix(chunk, start, firstCharCode)) {
585
+ const valueStart = chunk.charCodeAt(start + 5) === SPACE ? start + 6 : start + 5;
586
+ const value2 = chunk.slice(valueStart, end);
587
+ data = dataLines === 0 ? value2 : `${data}
588
+ ${value2}`;
589
+ dataLines++;
590
+ return;
591
+ }
592
+ if (isEventPrefix(chunk, start, firstCharCode)) {
593
+ eventType = chunk.slice(chunk.charCodeAt(start + 6) === SPACE ? start + 7 : start + 6, end) || void 0;
594
+ return;
595
+ }
596
+ if (firstCharCode === 105 && chunk.charCodeAt(start + 1) === 100 && chunk.charCodeAt(start + 2) === 58) {
597
+ const value2 = chunk.slice(chunk.charCodeAt(start + 3) === SPACE ? start + 4 : start + 3, end);
598
+ if (!value2.includes("\0"))
599
+ id = value2;
600
+ return;
601
+ }
602
+ if (firstCharCode === 58) {
603
+ if (onComment) {
604
+ const line2 = chunk.slice(start, end);
605
+ onComment(line2.slice(chunk.charCodeAt(start + 1) === SPACE ? 2 : 1));
606
+ }
607
+ return;
608
+ }
609
+ const line = chunk.slice(start, end);
610
+ const fieldSeparatorIndex = line.indexOf(":");
611
+ if (fieldSeparatorIndex === -1) {
612
+ processField(line, "", line);
613
+ return;
614
+ }
615
+ const field = line.slice(0, fieldSeparatorIndex);
616
+ const offset = line.charCodeAt(fieldSeparatorIndex + 1) === SPACE ? 2 : 1;
617
+ const value = line.slice(fieldSeparatorIndex + offset);
618
+ processField(field, value, line);
619
+ }
620
+ function processField(field, value, line) {
621
+ switch (field) {
622
+ case "event":
623
+ eventType = value || void 0;
624
+ break;
625
+ case "data":
626
+ data = dataLines === 0 ? value : `${data}
627
+ ${value}`;
628
+ dataLines++;
629
+ break;
630
+ case "id":
631
+ if (!value.includes("\0"))
632
+ id = value;
633
+ break;
634
+ case "retry":
635
+ if (/^\d+$/.test(value)) {
636
+ onRetry === null || onRetry === void 0 ? void 0 : onRetry(parseInt(value, 10));
637
+ } else {
638
+ onError === null || onError === void 0 ? void 0 : onError(new ParseError(`Invalid \`retry\` value: "${value}"`, {
639
+ type: "invalid-retry",
640
+ value,
641
+ line
642
+ }));
643
+ }
644
+ break;
645
+ default:
646
+ onError === null || onError === void 0 ? void 0 : onError(new ParseError(`Unknown field "${field.length > 20 ? `${field.slice(0, 20)}\u2026` : field}"`, { type: "unknown-field", field, value, line }));
647
+ break;
648
+ }
649
+ }
650
+ function dispatchEvent() {
651
+ if (id !== void 0) {
652
+ onId === null || onId === void 0 ? void 0 : onId(id);
653
+ }
654
+ if (dataLines > 0) {
655
+ onEvent === null || onEvent === void 0 ? void 0 : onEvent({
656
+ id,
657
+ event: eventType,
658
+ data
659
+ });
660
+ }
661
+ id = void 0;
662
+ data = "";
663
+ dataLines = 0;
664
+ eventType = void 0;
665
+ }
666
+ function reset(options = {}) {
667
+ if (options.consume && pendingFragments.length > 0) {
668
+ const incompleteLine = pendingFragments.join("");
669
+ parseLine(incompleteLine, 0, incompleteLine.length);
670
+ }
671
+ isFirstChunk = true;
672
+ id = void 0;
673
+ data = "";
674
+ dataLines = 0;
675
+ eventType = void 0;
676
+ pendingFragments.length = 0;
677
+ pendingFragmentsLength = 0;
678
+ terminated = false;
679
+ skippingLine = false;
680
+ skipNextLineFeed = false;
681
+ }
682
+ return { feed, reset };
683
+ }
684
+ function isDataPrefix(chunk, i, firstCharCode) {
685
+ return firstCharCode === 100 && chunk.charCodeAt(i + 1) === 97 && chunk.charCodeAt(i + 2) === 116 && chunk.charCodeAt(i + 3) === 97 && chunk.charCodeAt(i + 4) === 58;
686
+ }
687
+ function isEventPrefix(chunk, i, firstCharCode) {
688
+ return firstCharCode === 101 && chunk.charCodeAt(i + 1) === 118 && chunk.charCodeAt(i + 2) === 101 && chunk.charCodeAt(i + 3) === 110 && chunk.charCodeAt(i + 4) === 116 && chunk.charCodeAt(i + 5) === 58;
689
+ }
690
+ function isPotentialField(line, field) {
691
+ let i = 1;
692
+ while (i < line.length && i < field.length) {
693
+ if (line.charCodeAt(i) !== field.charCodeAt(i)) {
694
+ return false;
695
+ }
696
+ i++;
697
+ }
698
+ return line.length <= field.length || line.charCodeAt(field.length) === 58;
699
+ }
700
+
364
701
  // src/sse.ts
365
- import { createParser } from "eventsource-parser";
366
702
  var KNOWN_EVENTS = /* @__PURE__ */ new Set(["started", "progress", "output", "done", "error"]);
367
703
  function pushEvent(state, event) {
368
704
  state.events.push(event);