pulse-coder-engine 0.0.1-alpha.13 → 0.0.1-alpha.14

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
@@ -451,6 +451,9 @@ async function loop(context, options) {
451
451
  const loopHooks = options?.hooks ?? {};
452
452
  while (true) {
453
453
  try {
454
+ if (options?.abortSignal?.aborted) {
455
+ return "Request aborted.";
456
+ }
454
457
  if (compactionAttempts < MAX_COMPACTION_ATTEMPTS) {
455
458
  const { didCompact, newMessages } = await maybeCompactContext(context, {
456
459
  provider: options?.provider,
@@ -464,6 +467,9 @@ async function loop(context, options) {
464
467
  continue;
465
468
  }
466
469
  }
470
+ if (options?.abortSignal?.aborted) {
471
+ return "Request aborted.";
472
+ }
467
473
  let tools = options?.tools || {};
468
474
  let systemPrompt = options?.systemPrompt;
469
475
  if (loopHooks.beforeLLMCall?.length) {
@@ -488,6 +494,9 @@ async function loop(context, options) {
488
494
  onClarificationRequest: options?.onClarificationRequest,
489
495
  abortSignal: options?.abortSignal
490
496
  };
497
+ if (options?.abortSignal?.aborted) {
498
+ return "Request aborted.";
499
+ }
491
500
  const result = streamTextAI(context.messages, tools, {
492
501
  abortSignal: options?.abortSignal,
493
502
  toolExecutionContext,
@@ -526,6 +535,9 @@ async function loop(context, options) {
526
535
  await hook({ context, finishReason, text });
527
536
  }
528
537
  }
538
+ if (options?.abortSignal?.aborted) {
539
+ return "Request aborted.";
540
+ }
529
541
  if (finishReason === "stop") {
530
542
  if (!text) {
531
543
  continue;
@@ -575,7 +587,7 @@ async function loop(context, options) {
575
587
  }
576
588
  if (isRetryableError(error)) {
577
589
  const delay = Math.min(2e3 * Math.pow(2, errorCount - 1), 3e4);
578
- await sleep(delay);
590
+ await sleep(delay, options?.abortSignal);
579
591
  continue;
580
592
  }
581
593
  return `Error: ${error?.message ?? String(error)}`;
@@ -586,8 +598,34 @@ function isRetryableError(error) {
586
598
  const status = error?.status ?? error?.statusCode;
587
599
  return status === 429 || status === 500 || status === 502 || status === 503;
588
600
  }
589
- function sleep(ms) {
590
- return new Promise((resolve) => setTimeout(resolve, ms));
601
+ function sleep(ms, abortSignal) {
602
+ return new Promise((resolve, reject) => {
603
+ if (abortSignal?.aborted) {
604
+ const abortError = new Error("Aborted");
605
+ abortError.name = "AbortError";
606
+ reject(abortError);
607
+ return;
608
+ }
609
+ let timeoutId;
610
+ const onAbort = () => {
611
+ clearTimeout(timeoutId);
612
+ if (abortSignal) {
613
+ abortSignal.removeEventListener("abort", onAbort);
614
+ }
615
+ const abortError = new Error("Aborted");
616
+ abortError.name = "AbortError";
617
+ reject(abortError);
618
+ };
619
+ timeoutId = setTimeout(() => {
620
+ if (abortSignal) {
621
+ abortSignal.removeEventListener("abort", onAbort);
622
+ }
623
+ resolve();
624
+ }, ms);
625
+ if (abortSignal) {
626
+ abortSignal.addEventListener("abort", onAbort, { once: true });
627
+ }
628
+ });
591
629
  }
592
630
 
593
631
  // src/tools/read.ts