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

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.
@@ -1503,6 +1503,9 @@ async function loop(context, options) {
1503
1503
  const loopHooks = options?.hooks ?? {};
1504
1504
  while (true) {
1505
1505
  try {
1506
+ if (options?.abortSignal?.aborted) {
1507
+ return "Request aborted.";
1508
+ }
1506
1509
  if (compactionAttempts < MAX_COMPACTION_ATTEMPTS) {
1507
1510
  const { didCompact, newMessages } = await maybeCompactContext(context, {
1508
1511
  provider: options?.provider,
@@ -1516,6 +1519,9 @@ async function loop(context, options) {
1516
1519
  continue;
1517
1520
  }
1518
1521
  }
1522
+ if (options?.abortSignal?.aborted) {
1523
+ return "Request aborted.";
1524
+ }
1519
1525
  let tools = options?.tools || {};
1520
1526
  let systemPrompt = options?.systemPrompt;
1521
1527
  if (loopHooks.beforeLLMCall?.length) {
@@ -1540,6 +1546,9 @@ async function loop(context, options) {
1540
1546
  onClarificationRequest: options?.onClarificationRequest,
1541
1547
  abortSignal: options?.abortSignal
1542
1548
  };
1549
+ if (options?.abortSignal?.aborted) {
1550
+ return "Request aborted.";
1551
+ }
1543
1552
  const result = streamTextAI(context.messages, tools, {
1544
1553
  abortSignal: options?.abortSignal,
1545
1554
  toolExecutionContext,
@@ -1578,6 +1587,9 @@ async function loop(context, options) {
1578
1587
  await hook({ context, finishReason, text });
1579
1588
  }
1580
1589
  }
1590
+ if (options?.abortSignal?.aborted) {
1591
+ return "Request aborted.";
1592
+ }
1581
1593
  if (finishReason === "stop") {
1582
1594
  if (!text) {
1583
1595
  continue;
@@ -1627,7 +1639,7 @@ async function loop(context, options) {
1627
1639
  }
1628
1640
  if (isRetryableError(error)) {
1629
1641
  const delay = Math.min(2e3 * Math.pow(2, errorCount - 1), 3e4);
1630
- await sleep(delay);
1642
+ await sleep(delay, options?.abortSignal);
1631
1643
  continue;
1632
1644
  }
1633
1645
  return `Error: ${error?.message ?? String(error)}`;
@@ -1638,8 +1650,34 @@ function isRetryableError(error) {
1638
1650
  const status = error?.status ?? error?.statusCode;
1639
1651
  return status === 429 || status === 500 || status === 502 || status === 503;
1640
1652
  }
1641
- function sleep(ms) {
1642
- return new Promise((resolve) => setTimeout(resolve, ms));
1653
+ function sleep(ms, abortSignal) {
1654
+ return new Promise((resolve, reject) => {
1655
+ if (abortSignal?.aborted) {
1656
+ const abortError = new Error("Aborted");
1657
+ abortError.name = "AbortError";
1658
+ reject(abortError);
1659
+ return;
1660
+ }
1661
+ let timeoutId;
1662
+ const onAbort = () => {
1663
+ clearTimeout(timeoutId);
1664
+ if (abortSignal) {
1665
+ abortSignal.removeEventListener("abort", onAbort);
1666
+ }
1667
+ const abortError = new Error("Aborted");
1668
+ abortError.name = "AbortError";
1669
+ reject(abortError);
1670
+ };
1671
+ timeoutId = setTimeout(() => {
1672
+ if (abortSignal) {
1673
+ abortSignal.removeEventListener("abort", onAbort);
1674
+ }
1675
+ resolve();
1676
+ }, ms);
1677
+ if (abortSignal) {
1678
+ abortSignal.addEventListener("abort", onAbort, { once: true });
1679
+ }
1680
+ });
1643
1681
  }
1644
1682
 
1645
1683
  // src/tools/read.ts