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.js CHANGED
@@ -391,6 +391,9 @@ async function loop(context, options) {
391
391
  const loopHooks = options?.hooks ?? {};
392
392
  while (true) {
393
393
  try {
394
+ if (options?.abortSignal?.aborted) {
395
+ return "Request aborted.";
396
+ }
394
397
  if (compactionAttempts < MAX_COMPACTION_ATTEMPTS) {
395
398
  const { didCompact, newMessages } = await maybeCompactContext(context, {
396
399
  provider: options?.provider,
@@ -404,6 +407,9 @@ async function loop(context, options) {
404
407
  continue;
405
408
  }
406
409
  }
410
+ if (options?.abortSignal?.aborted) {
411
+ return "Request aborted.";
412
+ }
407
413
  let tools = options?.tools || {};
408
414
  let systemPrompt = options?.systemPrompt;
409
415
  if (loopHooks.beforeLLMCall?.length) {
@@ -428,6 +434,9 @@ async function loop(context, options) {
428
434
  onClarificationRequest: options?.onClarificationRequest,
429
435
  abortSignal: options?.abortSignal
430
436
  };
437
+ if (options?.abortSignal?.aborted) {
438
+ return "Request aborted.";
439
+ }
431
440
  const result = streamTextAI(context.messages, tools, {
432
441
  abortSignal: options?.abortSignal,
433
442
  toolExecutionContext,
@@ -466,6 +475,9 @@ async function loop(context, options) {
466
475
  await hook({ context, finishReason, text });
467
476
  }
468
477
  }
478
+ if (options?.abortSignal?.aborted) {
479
+ return "Request aborted.";
480
+ }
469
481
  if (finishReason === "stop") {
470
482
  if (!text) {
471
483
  continue;
@@ -515,7 +527,7 @@ async function loop(context, options) {
515
527
  }
516
528
  if (isRetryableError(error)) {
517
529
  const delay = Math.min(2e3 * Math.pow(2, errorCount - 1), 3e4);
518
- await sleep(delay);
530
+ await sleep(delay, options?.abortSignal);
519
531
  continue;
520
532
  }
521
533
  return `Error: ${error?.message ?? String(error)}`;
@@ -526,8 +538,34 @@ function isRetryableError(error) {
526
538
  const status = error?.status ?? error?.statusCode;
527
539
  return status === 429 || status === 500 || status === 502 || status === 503;
528
540
  }
529
- function sleep(ms) {
530
- return new Promise((resolve) => setTimeout(resolve, ms));
541
+ function sleep(ms, abortSignal) {
542
+ return new Promise((resolve, reject) => {
543
+ if (abortSignal?.aborted) {
544
+ const abortError = new Error("Aborted");
545
+ abortError.name = "AbortError";
546
+ reject(abortError);
547
+ return;
548
+ }
549
+ let timeoutId;
550
+ const onAbort = () => {
551
+ clearTimeout(timeoutId);
552
+ if (abortSignal) {
553
+ abortSignal.removeEventListener("abort", onAbort);
554
+ }
555
+ const abortError = new Error("Aborted");
556
+ abortError.name = "AbortError";
557
+ reject(abortError);
558
+ };
559
+ timeoutId = setTimeout(() => {
560
+ if (abortSignal) {
561
+ abortSignal.removeEventListener("abort", onAbort);
562
+ }
563
+ resolve();
564
+ }, ms);
565
+ if (abortSignal) {
566
+ abortSignal.addEventListener("abort", onAbort, { once: true });
567
+ }
568
+ });
531
569
  }
532
570
 
533
571
  // src/tools/read.ts