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.
@@ -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,7 +1587,13 @@ 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") {
1594
+ if (!text) {
1595
+ continue;
1596
+ }
1582
1597
  return text || "Task completed.";
1583
1598
  }
1584
1599
  if (finishReason === "length") {
@@ -1610,6 +1625,9 @@ async function loop(context, options) {
1610
1625
  }
1611
1626
  continue;
1612
1627
  }
1628
+ if (!text) {
1629
+ continue;
1630
+ }
1613
1631
  return text || "Task completed.";
1614
1632
  } catch (error) {
1615
1633
  if (options?.abortSignal?.aborted || error?.name === "AbortError") {
@@ -1621,7 +1639,7 @@ async function loop(context, options) {
1621
1639
  }
1622
1640
  if (isRetryableError(error)) {
1623
1641
  const delay = Math.min(2e3 * Math.pow(2, errorCount - 1), 3e4);
1624
- await sleep(delay);
1642
+ await sleep(delay, options?.abortSignal);
1625
1643
  continue;
1626
1644
  }
1627
1645
  return `Error: ${error?.message ?? String(error)}`;
@@ -1632,8 +1650,34 @@ function isRetryableError(error) {
1632
1650
  const status = error?.status ?? error?.statusCode;
1633
1651
  return status === 429 || status === 500 || status === 502 || status === 503;
1634
1652
  }
1635
- function sleep(ms) {
1636
- 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
+ });
1637
1681
  }
1638
1682
 
1639
1683
  // src/tools/read.ts