agentkernel 0.19.0 → 0.20.0

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