@polka-codes/core 0.9.29 → 0.9.30

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.
@@ -343,6 +343,14 @@ export { commandStepSpecHandler as commandStepSpecHandler_alias_1 }
343
343
  export { commandStepSpecHandler as commandStepSpecHandler_alias_2 }
344
344
  export { commandStepSpecHandler as commandStepSpecHandler_alias_3 }
345
345
 
346
+ /**
347
+ * Utility to compute exponential backoff delays for rate-limit handling. generated by polka.codes
348
+ *
349
+ * The backoff starts at baseSeconds and doubles each time, capped at capSeconds.
350
+ * Example with base=2, cap=60: 2, 4, 8, 16, 32, 60, 60, ...
351
+ */
352
+ export declare function computeRateLimitBackoffSeconds(count: number, baseSeconds?: number, capSeconds?: number): number;
353
+
346
354
  declare type Config = z.infer<typeof configSchema>;
347
355
  export { Config }
348
356
  export { Config as Config_alias_1 }
package/dist/index.js CHANGED
@@ -1433,6 +1433,21 @@ function toToolInfoV1(tool) {
1433
1433
  return v1Tool;
1434
1434
  }
1435
1435
 
1436
+ // src/Agent/backoff.ts
1437
+ function computeRateLimitBackoffSeconds(count, baseSeconds = 2, capSeconds = 60) {
1438
+ if (!Number.isFinite(count) || count <= 0) {
1439
+ count = 1;
1440
+ }
1441
+ if (!Number.isFinite(baseSeconds) || baseSeconds <= 0) {
1442
+ baseSeconds = 2;
1443
+ }
1444
+ if (!Number.isFinite(capSeconds) || capSeconds <= 0) {
1445
+ capSeconds = 60;
1446
+ }
1447
+ const delay = baseSeconds * 2 ** (count - 1);
1448
+ return Math.min(delay, capSeconds);
1449
+ }
1450
+
1436
1451
  // src/Agent/parseAssistantMessage.ts
1437
1452
  function parseNestedParameters(content, parameterPrefix, childrenParams) {
1438
1453
  const result = {};
@@ -2011,6 +2026,7 @@ ${instance.prompt}`;
2011
2026
  const retryCount = this.config.retryCount ?? 5;
2012
2027
  const requestTimeoutSeconds = this.config.requestTimeoutSeconds ?? 90;
2013
2028
  let respMessages = [];
2029
+ let rateLimitErrorCount = 0;
2014
2030
  for (let i = 0; i < retryCount; i++) {
2015
2031
  if (this.#aborted) {
2016
2032
  break;
@@ -2026,7 +2042,7 @@ ${instance.prompt}`;
2026
2042
  }
2027
2043
  if (requestTimeoutSeconds > 0 && requestAbortController) {
2028
2044
  timeout = setTimeout(() => {
2029
- console.debug(
2045
+ console.error(
2030
2046
  `
2031
2047
  Request timeout after ${requestTimeoutSeconds} seconds. Canceling current request attempt ${i + 1}/${retryCount}.`
2032
2048
  );
@@ -2071,6 +2087,7 @@ Request timeout after ${requestTimeoutSeconds} seconds. Canceling current reques
2071
2087
  });
2072
2088
  const resp = await stream.response;
2073
2089
  respMessages = resp.messages;
2090
+ rateLimitErrorCount = 0;
2074
2091
  if (timeout) {
2075
2092
  clearTimeout(timeout);
2076
2093
  timeout = void 0;
@@ -2080,8 +2097,19 @@ Request timeout after ${requestTimeoutSeconds} seconds. Canceling current reques
2080
2097
  if (this.#aborted) {
2081
2098
  break;
2082
2099
  }
2083
- console.debug(`Request attempt ${i + 1} timed out, will retry`);
2100
+ console.error(`Request attempt ${i + 1} timed out, will retry`);
2101
+ } else if (error?.error?.error?.code === "rate_limit_exceeded" || error?.error?.code === "rate_limit_exceeded" || error?.code === "rate_limit_exceeded" || error?.status === 429 || error?.error?.status === 429) {
2102
+ rateLimitErrorCount++;
2103
+ const waitSeconds = computeRateLimitBackoffSeconds(rateLimitErrorCount);
2104
+ console.error(`Rate limit exceeded. Waiting ${waitSeconds}s before retrying...`);
2105
+ if (timeout) {
2106
+ clearTimeout(timeout);
2107
+ timeout = void 0;
2108
+ }
2109
+ await new Promise((resolve) => setTimeout(resolve, waitSeconds * 1e3));
2110
+ console.error("Retrying request...");
2084
2111
  } else {
2112
+ rateLimitErrorCount = 0;
2085
2113
  console.error("Error in stream:", error);
2086
2114
  }
2087
2115
  } finally {
@@ -2110,7 +2138,7 @@ Request timeout after ${requestTimeoutSeconds} seconds. Canceling current reques
2110
2138
  break;
2111
2139
  }
2112
2140
  if (i < retryCount - 1) {
2113
- console.debug(`
2141
+ console.error(`
2114
2142
  Retrying request ${i + 2} of ${retryCount}`);
2115
2143
  }
2116
2144
  }
@@ -3786,7 +3814,7 @@ var makeAgentStepSpecHandler = (getModelFn) => ({
3786
3814
  if (context.verbose && context.verbose >= 1) {
3787
3815
  logger.log(`[agent-step] Using agent: ${agentName}`);
3788
3816
  }
3789
- const agentOptions = {
3817
+ return new AgentClass({
3790
3818
  ai: model,
3791
3819
  os: parameters.os ?? "linux",
3792
3820
  provider: context.provider,
@@ -3796,9 +3824,9 @@ var makeAgentStepSpecHandler = (getModelFn) => ({
3796
3824
  usageMeter,
3797
3825
  parameters: modelParameters,
3798
3826
  scripts: parameters.scripts,
3799
- callback: context.agentCallback
3800
- };
3801
- return new AgentClass(agentOptions);
3827
+ callback: context.agentCallback,
3828
+ requireToolUse: false
3829
+ });
3802
3830
  } else {
3803
3831
  if (!step2.systemPrompt) {
3804
3832
  throw new Error("No system prompt specified for the agent step.");
@@ -3819,7 +3847,7 @@ var makeAgentStepSpecHandler = (getModelFn) => ({
3819
3847
  toolFormat,
3820
3848
  parameters: modelParameters,
3821
3849
  usageMeter,
3822
- requireToolUse: true
3850
+ requireToolUse: false
3823
3851
  });
3824
3852
  }
3825
3853
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polka-codes/core",
3
- "version": "0.9.29",
3
+ "version": "0.9.30",
4
4
  "license": "AGPL-3.0",
5
5
  "author": "github@polka.codes",
6
6
  "type": "module",