@polka-codes/core 0.9.29 → 0.9.31
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/_tsup-dts-rollup.d.ts +9 -1
- package/dist/index.js +38 -15
- package/package.json +1 -1
|
@@ -24,7 +24,7 @@ declare abstract class AgentBase {
|
|
|
24
24
|
get messages(): Readonly<ModelMessage[]>;
|
|
25
25
|
setMessages(messages: Readonly<ModelMessage[]>): void;
|
|
26
26
|
start(prompt: UserContent): Promise<ExitReason>;
|
|
27
|
-
step(
|
|
27
|
+
step(message: (UserModelMessage | ToolModelMessage)[]): Promise<AssistantMessageContent[]>;
|
|
28
28
|
handleStepResponse(response: AssistantMessageContent[]): Promise<{
|
|
29
29
|
type: "reply";
|
|
30
30
|
message: (UserModelMessage | ToolModelMessage)[];
|
|
@@ -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 = {};
|
|
@@ -1947,16 +1962,11 @@ ${instance.prompt}`;
|
|
|
1947
1962
|
this.#callback({ kind: "StartTask" /* StartTask */, agent: this, systemPrompt: this.config.systemPrompt });
|
|
1948
1963
|
return await this.#processLoop(prompt5);
|
|
1949
1964
|
}
|
|
1950
|
-
async step(
|
|
1965
|
+
async step(message) {
|
|
1951
1966
|
if (this.#messages.length === 0) {
|
|
1952
1967
|
this.#callback({ kind: "StartTask" /* StartTask */, agent: this, systemPrompt: this.config.systemPrompt });
|
|
1953
1968
|
}
|
|
1954
|
-
return await this.#request(
|
|
1955
|
-
{
|
|
1956
|
-
role: "user",
|
|
1957
|
-
content: prompt5
|
|
1958
|
-
}
|
|
1959
|
-
]);
|
|
1969
|
+
return await this.#request(message);
|
|
1960
1970
|
}
|
|
1961
1971
|
async handleStepResponse(response) {
|
|
1962
1972
|
return this.#handleResponse(response);
|
|
@@ -2011,6 +2021,7 @@ ${instance.prompt}`;
|
|
|
2011
2021
|
const retryCount = this.config.retryCount ?? 5;
|
|
2012
2022
|
const requestTimeoutSeconds = this.config.requestTimeoutSeconds ?? 90;
|
|
2013
2023
|
let respMessages = [];
|
|
2024
|
+
let rateLimitErrorCount = 0;
|
|
2014
2025
|
for (let i = 0; i < retryCount; i++) {
|
|
2015
2026
|
if (this.#aborted) {
|
|
2016
2027
|
break;
|
|
@@ -2026,7 +2037,7 @@ ${instance.prompt}`;
|
|
|
2026
2037
|
}
|
|
2027
2038
|
if (requestTimeoutSeconds > 0 && requestAbortController) {
|
|
2028
2039
|
timeout = setTimeout(() => {
|
|
2029
|
-
console.
|
|
2040
|
+
console.error(
|
|
2030
2041
|
`
|
|
2031
2042
|
Request timeout after ${requestTimeoutSeconds} seconds. Canceling current request attempt ${i + 1}/${retryCount}.`
|
|
2032
2043
|
);
|
|
@@ -2071,6 +2082,7 @@ Request timeout after ${requestTimeoutSeconds} seconds. Canceling current reques
|
|
|
2071
2082
|
});
|
|
2072
2083
|
const resp = await stream.response;
|
|
2073
2084
|
respMessages = resp.messages;
|
|
2085
|
+
rateLimitErrorCount = 0;
|
|
2074
2086
|
if (timeout) {
|
|
2075
2087
|
clearTimeout(timeout);
|
|
2076
2088
|
timeout = void 0;
|
|
@@ -2080,8 +2092,19 @@ Request timeout after ${requestTimeoutSeconds} seconds. Canceling current reques
|
|
|
2080
2092
|
if (this.#aborted) {
|
|
2081
2093
|
break;
|
|
2082
2094
|
}
|
|
2083
|
-
console.
|
|
2095
|
+
console.error(`Request attempt ${i + 1} timed out, will retry`);
|
|
2096
|
+
} 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) {
|
|
2097
|
+
rateLimitErrorCount++;
|
|
2098
|
+
const waitSeconds = computeRateLimitBackoffSeconds(rateLimitErrorCount);
|
|
2099
|
+
console.error(`Rate limit exceeded. Waiting ${waitSeconds}s before retrying...`);
|
|
2100
|
+
if (timeout) {
|
|
2101
|
+
clearTimeout(timeout);
|
|
2102
|
+
timeout = void 0;
|
|
2103
|
+
}
|
|
2104
|
+
await new Promise((resolve) => setTimeout(resolve, waitSeconds * 1e3));
|
|
2105
|
+
console.error("Retrying request...");
|
|
2084
2106
|
} else {
|
|
2107
|
+
rateLimitErrorCount = 0;
|
|
2085
2108
|
console.error("Error in stream:", error);
|
|
2086
2109
|
}
|
|
2087
2110
|
} finally {
|
|
@@ -2110,7 +2133,7 @@ Request timeout after ${requestTimeoutSeconds} seconds. Canceling current reques
|
|
|
2110
2133
|
break;
|
|
2111
2134
|
}
|
|
2112
2135
|
if (i < retryCount - 1) {
|
|
2113
|
-
console.
|
|
2136
|
+
console.error(`
|
|
2114
2137
|
Retrying request ${i + 2} of ${retryCount}`);
|
|
2115
2138
|
}
|
|
2116
2139
|
}
|
|
@@ -3786,7 +3809,7 @@ var makeAgentStepSpecHandler = (getModelFn) => ({
|
|
|
3786
3809
|
if (context.verbose && context.verbose >= 1) {
|
|
3787
3810
|
logger.log(`[agent-step] Using agent: ${agentName}`);
|
|
3788
3811
|
}
|
|
3789
|
-
|
|
3812
|
+
return new AgentClass({
|
|
3790
3813
|
ai: model,
|
|
3791
3814
|
os: parameters.os ?? "linux",
|
|
3792
3815
|
provider: context.provider,
|
|
@@ -3796,9 +3819,9 @@ var makeAgentStepSpecHandler = (getModelFn) => ({
|
|
|
3796
3819
|
usageMeter,
|
|
3797
3820
|
parameters: modelParameters,
|
|
3798
3821
|
scripts: parameters.scripts,
|
|
3799
|
-
callback: context.agentCallback
|
|
3800
|
-
|
|
3801
|
-
|
|
3822
|
+
callback: context.agentCallback,
|
|
3823
|
+
requireToolUse: false
|
|
3824
|
+
});
|
|
3802
3825
|
} else {
|
|
3803
3826
|
if (!step2.systemPrompt) {
|
|
3804
3827
|
throw new Error("No system prompt specified for the agent step.");
|
|
@@ -3819,7 +3842,7 @@ var makeAgentStepSpecHandler = (getModelFn) => ({
|
|
|
3819
3842
|
toolFormat,
|
|
3820
3843
|
parameters: modelParameters,
|
|
3821
3844
|
usageMeter,
|
|
3822
|
-
requireToolUse:
|
|
3845
|
+
requireToolUse: false
|
|
3823
3846
|
});
|
|
3824
3847
|
}
|
|
3825
3848
|
};
|