pulse-coder-engine 0.0.1-alpha.12 → 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,7 +475,13 @@ 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") {
482
+ if (!text) {
483
+ continue;
484
+ }
470
485
  return text || "Task completed.";
471
486
  }
472
487
  if (finishReason === "length") {
@@ -498,6 +513,9 @@ async function loop(context, options) {
498
513
  }
499
514
  continue;
500
515
  }
516
+ if (!text) {
517
+ continue;
518
+ }
501
519
  return text || "Task completed.";
502
520
  } catch (error) {
503
521
  if (options?.abortSignal?.aborted || error?.name === "AbortError") {
@@ -509,7 +527,7 @@ async function loop(context, options) {
509
527
  }
510
528
  if (isRetryableError(error)) {
511
529
  const delay = Math.min(2e3 * Math.pow(2, errorCount - 1), 3e4);
512
- await sleep(delay);
530
+ await sleep(delay, options?.abortSignal);
513
531
  continue;
514
532
  }
515
533
  return `Error: ${error?.message ?? String(error)}`;
@@ -520,8 +538,34 @@ function isRetryableError(error) {
520
538
  const status = error?.status ?? error?.statusCode;
521
539
  return status === 429 || status === 500 || status === 502 || status === 503;
522
540
  }
523
- function sleep(ms) {
524
- 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
+ });
525
569
  }
526
570
 
527
571
  // src/tools/read.ts