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.
@@ -1458,6 +1458,9 @@ async function loop(context, options) {
1458
1458
  const loopHooks = options?.hooks ?? {};
1459
1459
  while (true) {
1460
1460
  try {
1461
+ if (options?.abortSignal?.aborted) {
1462
+ return "Request aborted.";
1463
+ }
1461
1464
  if (compactionAttempts < MAX_COMPACTION_ATTEMPTS) {
1462
1465
  const { didCompact, newMessages } = await maybeCompactContext(context, {
1463
1466
  provider: options?.provider,
@@ -1471,6 +1474,9 @@ async function loop(context, options) {
1471
1474
  continue;
1472
1475
  }
1473
1476
  }
1477
+ if (options?.abortSignal?.aborted) {
1478
+ return "Request aborted.";
1479
+ }
1474
1480
  let tools = options?.tools || {};
1475
1481
  let systemPrompt = options?.systemPrompt;
1476
1482
  if (loopHooks.beforeLLMCall?.length) {
@@ -1495,6 +1501,9 @@ async function loop(context, options) {
1495
1501
  onClarificationRequest: options?.onClarificationRequest,
1496
1502
  abortSignal: options?.abortSignal
1497
1503
  };
1504
+ if (options?.abortSignal?.aborted) {
1505
+ return "Request aborted.";
1506
+ }
1498
1507
  const result = streamTextAI(context.messages, tools, {
1499
1508
  abortSignal: options?.abortSignal,
1500
1509
  toolExecutionContext,
@@ -1533,7 +1542,13 @@ async function loop(context, options) {
1533
1542
  await hook({ context, finishReason, text });
1534
1543
  }
1535
1544
  }
1545
+ if (options?.abortSignal?.aborted) {
1546
+ return "Request aborted.";
1547
+ }
1536
1548
  if (finishReason === "stop") {
1549
+ if (!text) {
1550
+ continue;
1551
+ }
1537
1552
  return text || "Task completed.";
1538
1553
  }
1539
1554
  if (finishReason === "length") {
@@ -1565,6 +1580,9 @@ async function loop(context, options) {
1565
1580
  }
1566
1581
  continue;
1567
1582
  }
1583
+ if (!text) {
1584
+ continue;
1585
+ }
1568
1586
  return text || "Task completed.";
1569
1587
  } catch (error) {
1570
1588
  if (options?.abortSignal?.aborted || error?.name === "AbortError") {
@@ -1576,7 +1594,7 @@ async function loop(context, options) {
1576
1594
  }
1577
1595
  if (isRetryableError(error)) {
1578
1596
  const delay = Math.min(2e3 * Math.pow(2, errorCount - 1), 3e4);
1579
- await sleep(delay);
1597
+ await sleep(delay, options?.abortSignal);
1580
1598
  continue;
1581
1599
  }
1582
1600
  return `Error: ${error?.message ?? String(error)}`;
@@ -1587,8 +1605,34 @@ function isRetryableError(error) {
1587
1605
  const status = error?.status ?? error?.statusCode;
1588
1606
  return status === 429 || status === 500 || status === 502 || status === 503;
1589
1607
  }
1590
- function sleep(ms) {
1591
- return new Promise((resolve) => setTimeout(resolve, ms));
1608
+ function sleep(ms, abortSignal) {
1609
+ return new Promise((resolve, reject) => {
1610
+ if (abortSignal?.aborted) {
1611
+ const abortError = new Error("Aborted");
1612
+ abortError.name = "AbortError";
1613
+ reject(abortError);
1614
+ return;
1615
+ }
1616
+ let timeoutId;
1617
+ const onAbort = () => {
1618
+ clearTimeout(timeoutId);
1619
+ if (abortSignal) {
1620
+ abortSignal.removeEventListener("abort", onAbort);
1621
+ }
1622
+ const abortError = new Error("Aborted");
1623
+ abortError.name = "AbortError";
1624
+ reject(abortError);
1625
+ };
1626
+ timeoutId = setTimeout(() => {
1627
+ if (abortSignal) {
1628
+ abortSignal.removeEventListener("abort", onAbort);
1629
+ }
1630
+ resolve();
1631
+ }, ms);
1632
+ if (abortSignal) {
1633
+ abortSignal.addEventListener("abort", onAbort, { once: true });
1634
+ }
1635
+ });
1592
1636
  }
1593
1637
 
1594
1638
  // src/tools/read.ts