@cubicecho/agent-core 2.24.0 → 2.25.0
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/README.md +10 -7
- package/dist/agent-loop.d.ts +84 -205
- package/dist/agent-loop.js +288 -597
- package/dist/calibration.d.ts +14 -10
- package/dist/calibration.js +30 -23
- package/dist/capabilities.d.ts +111 -42
- package/dist/capabilities.js +281 -138
- package/dist/client.d.ts +104 -35
- package/dist/client.js +205 -74
- package/dist/compaction.d.ts +103 -60
- package/dist/compaction.js +160 -86
- package/dist/config.d.ts +48 -12
- package/dist/config.js +20 -1
- package/dist/continuation.d.ts +24 -16
- package/dist/continuation.js +70 -44
- package/dist/digest.d.ts +7 -0
- package/dist/digest.js +18 -0
- package/dist/errors.d.ts +24 -15
- package/dist/errors.js +23 -15
- package/dist/events.d.ts +100 -63
- package/dist/events.js +250 -120
- package/dist/guards.d.ts +35 -4
- package/dist/guards.js +38 -5
- package/dist/hook-events.d.ts +21 -5
- package/dist/hook-events.js +23 -9
- package/dist/hooks.d.ts +96 -73
- package/dist/hooks.js +202 -116
- package/dist/index.d.ts +30 -26
- package/dist/index.js +29 -24
- package/dist/ledger.d.ts +62 -30
- package/dist/ledger.js +163 -56
- package/dist/platform.d.ts +13 -0
- package/dist/platform.js +13 -0
- package/dist/preselect.d.ts +222 -0
- package/dist/preselect.js +290 -0
- package/dist/request-body.d.ts +32 -0
- package/dist/request-body.js +47 -0
- package/dist/reset.d.ts +1 -0
- package/dist/reset.js +7 -6
- package/dist/retry.d.ts +29 -138
- package/dist/retry.js +69 -282
- package/dist/run-calls.d.ts +123 -0
- package/dist/run-calls.js +351 -0
- package/dist/run-turn.d.ts +36 -15
- package/dist/run-turn.js +74 -37
- package/dist/runtime.d.ts +23 -17
- package/dist/runtime.js +34 -24
- package/dist/schema-compat.d.ts +35 -18
- package/dist/schema-compat.js +340 -175
- package/dist/scope.d.ts +28 -5
- package/dist/scope.js +37 -10
- package/dist/side-task.d.ts +123 -40
- package/dist/side-task.js +181 -63
- package/dist/snapshot.d.ts +10 -5
- package/dist/snapshot.js +73 -30
- package/dist/spec.d.ts +56 -25
- package/dist/spec.js +494 -215
- package/dist/stream.d.ts +66 -16
- package/dist/stream.js +220 -97
- package/dist/thinking.d.ts +23 -8
- package/dist/thinking.js +92 -44
- package/dist/tokens.d.ts +164 -6
- package/dist/tokens.js +319 -6
- package/dist/tool-calls.d.ts +82 -17
- package/dist/tool-calls.js +198 -83
- package/dist/tool-loading.d.ts +101 -189
- package/dist/tool-loading.js +277 -371
- package/dist/wire.d.ts +81 -0
- package/dist/wire.js +73 -0
- package/llms.txt +76 -39
- package/package.json +4 -4
package/dist/retry.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import OpenAI from "openai";
|
|
2
1
|
/**
|
|
3
2
|
* Everything about a request failing that is not about what the request said.
|
|
4
3
|
*
|
|
@@ -15,6 +14,7 @@ export declare class EndpointSilent extends Error {
|
|
|
15
14
|
* The request was bigger than the model will read. Its own class so nothing retries it: sending
|
|
16
15
|
* the same too-large request again is the same refusal, one round trip later.
|
|
17
16
|
*
|
|
17
|
+
* @remarks
|
|
18
18
|
* `runTurn` raises it from either side of the round trip — its own pre-flight guard, or the
|
|
19
19
|
* endpoint's refusal read back through `isOverflow` — so a caller has one thing to catch whether
|
|
20
20
|
* or not it gave a `contextLimit`. The second carries the endpoint's own message, and the error
|
|
@@ -23,143 +23,22 @@ export declare class EndpointSilent extends Error {
|
|
|
23
23
|
export declare class ContextOverflow extends Error {
|
|
24
24
|
readonly name = "ContextOverflow";
|
|
25
25
|
}
|
|
26
|
-
/**
|
|
27
|
-
* 1234 → "1.2k". The numbers in an overflow message are large and nobody reads the units digit.
|
|
28
|
-
*
|
|
29
|
-
* @param tokens The count to render.
|
|
30
|
-
*/
|
|
31
|
-
export declare const compact: (tokens: number) => string;
|
|
32
|
-
/** What the two token estimates below take besides what they measure. */
|
|
33
|
-
export interface TokenEstimateOptions {
|
|
34
|
-
/**
|
|
35
|
-
* The divisor, `CHARS_PER_TOKEN` unless given — `charsPerTokenFor` for a model whose reported
|
|
36
|
-
* usage has calibrated it. A value that is not a number above zero is ignored.
|
|
37
|
-
*/
|
|
38
|
-
charsPerToken?: number;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* How many characters one message is worth: its keys, and its content in whichever shape.
|
|
42
|
-
*
|
|
43
|
-
* @param message The message as it will be sent.
|
|
44
|
-
*/
|
|
45
|
-
export declare function messageChars(message: OpenAI.ChatCompletionMessageParam): number;
|
|
46
|
-
/**
|
|
47
|
-
* How many characters a tool array is worth, measured once per array.
|
|
48
|
-
*
|
|
49
|
-
* @param tools The tool definitions as they will be sent. An empty array is worth nothing.
|
|
50
|
-
*/
|
|
51
|
-
export declare function toolsChars(tools: OpenAI.ChatCompletionTool[]): number;
|
|
52
|
-
/**
|
|
53
|
-
* How many characters a request is worth: the walk `requestTokens` divides, without the division.
|
|
54
|
-
*
|
|
55
|
-
* What calibration reads a reported prompt count against, since a ratio is only as good as the
|
|
56
|
-
* character count it was taken over agreeing with the one it is later applied to.
|
|
57
|
-
*
|
|
58
|
-
* @param body The request as it was sent, tools included.
|
|
59
|
-
*/
|
|
60
|
-
export declare function requestChars(body: OpenAI.ChatCompletionCreateParamsStreaming): number;
|
|
61
|
-
/**
|
|
62
|
-
* What this request will cost the window, in tokens, near enough.
|
|
63
|
-
*
|
|
64
|
-
* See `estimateTokens` for why it is characters over four and which way it is wrong on purpose,
|
|
65
|
-
* and `charsPerTokenFor` for the divisor a model's own reported usage has measured instead.
|
|
66
|
-
*
|
|
67
|
-
* Summed by walking the body rather than by serialising it. `JSON.stringify` on the messages
|
|
68
|
-
* built the entire transcript into a string on every call and threw it away having read nothing
|
|
69
|
-
* but its `.length` — against a transcript that grows by a turn each turn, and one the SDK is
|
|
70
|
-
* about to serialise again to send. What the walk misses is JSON's own punctuation and the keys,
|
|
71
|
-
* which the envelope constants put back — one per key and one per content part, rather than one
|
|
72
|
-
* per message, since the keys only some shapes carry and the parts a client appends block by
|
|
73
|
-
* block are most of what a tool-using transcript is made of. What is left is a message's
|
|
74
|
-
* escaping, which is not a constant and is small against an estimate that is already characters
|
|
75
|
-
* over four.
|
|
76
|
-
*
|
|
77
|
-
* @param body The request as it will be sent, tools included.
|
|
78
|
-
* @param options The divisor, `CHARS_PER_TOKEN` when none is given.
|
|
79
|
-
*/
|
|
80
|
-
export declare const requestTokens: (body: OpenAI.ChatCompletionCreateParamsStreaming, { charsPerToken }?: TokenEstimateOptions) => number;
|
|
81
|
-
/**
|
|
82
|
-
* What a request is made of, by the part of it a consumer can actually do something about.
|
|
83
|
-
*
|
|
84
|
-
* The question an operator asks is not how big the request is — the total already answers that —
|
|
85
|
-
* but what is filling the window, and the only useful answer names a lever: a system prompt to
|
|
86
|
-
* shorten, a tool list to load on demand instead of declaring whole, a transcript to compact,
|
|
87
|
-
* results to prune. So the cut follows the levers rather than the roles: `toolResults` is exactly
|
|
88
|
-
* what `pruneToolResults` can shrink, and `history` is everything `planCompaction` folds, the
|
|
89
|
-
* arguments of the calls in it included.
|
|
90
|
-
*/
|
|
91
|
-
export interface ContextBreakdown {
|
|
92
|
-
/** Every system and developer message, wherever it sits in the transcript. */
|
|
93
|
-
system: number;
|
|
94
|
-
/** The declared tool schemas, which a chat template renders ahead of the system prompt. */
|
|
95
|
-
tools: number;
|
|
96
|
-
/** What was said: the user and assistant messages, and the calls the assistant asked for. */
|
|
97
|
-
history: number;
|
|
98
|
-
/** What the tools handed back — the `tool` messages, and nothing else. */
|
|
99
|
-
toolResults: number;
|
|
100
|
-
/** The four above, summed. */
|
|
101
|
-
total: number;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* What each part of a request is worth in characters, by the same walk `requestTokens` divides.
|
|
105
|
-
*
|
|
106
|
-
* Exact and additive: the parts sum to `total`, which is `requestChars` plus `toolsChars`. The
|
|
107
|
-
* conversion to tokens is `contextTokens`' business, because that is where an estimate and a
|
|
108
|
-
* reported count have to be told apart.
|
|
109
|
-
*
|
|
110
|
-
* @param body The request as it will be sent, tools included.
|
|
111
|
-
*/
|
|
112
|
-
export declare function contextChars(body: OpenAI.ChatCompletionCreateParamsStreaming): ContextBreakdown;
|
|
113
|
-
/** What `contextTokens` takes besides the request. */
|
|
114
|
-
export interface ContextBreakdownOptions extends TokenEstimateOptions {
|
|
115
|
-
/**
|
|
116
|
-
* The prompt count the endpoint reported for this request, if it has answered. Given one, the
|
|
117
|
-
* parts are shares of it and the breakdown sums to what was actually charged rather than to an
|
|
118
|
-
* estimate; left out, they are shares of `requestTokens`.
|
|
119
|
-
*/
|
|
120
|
-
promptTokens?: number;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* What each part of a request costs the window, in tokens, adding up to the whole.
|
|
124
|
-
*
|
|
125
|
-
* Shares rather than four independent estimates, because a readout whose parts do not add up to
|
|
126
|
-
* the total beside them is a readout nobody trusts. Nothing in the round trip reports anything
|
|
127
|
-
* finer than a prompt count — a completion says how many tokens it read and not a word about
|
|
128
|
-
* where they came from — so the proportions are an estimate whatever the total is.
|
|
129
|
-
*
|
|
130
|
-
* Without a reported count the total is `requestTokens`, and the tools are counted the way it and
|
|
131
|
-
* `TurnMetrics.toolSchemaTokens` count them rather than shared out, so the two agree by
|
|
132
|
-
* construction and an operator does not read one number for the tool block in the metrics and a
|
|
133
|
-
* different one here. With a reported count every part is a share of it, the tools included:
|
|
134
|
-
* that number is the server's, and the point of using it is that the parts sum to what was
|
|
135
|
-
* charged.
|
|
136
|
-
*
|
|
137
|
-
* @param body The request as it will be sent, tools included.
|
|
138
|
-
* @param options The divisor, and the reported prompt count when there is one.
|
|
139
|
-
*/
|
|
140
|
-
export declare function contextTokens(body: OpenAI.ChatCompletionCreateParamsStreaming, { charsPerToken, promptTokens }?: ContextBreakdownOptions): ContextBreakdown;
|
|
141
|
-
/**
|
|
142
|
-
* One message's estimated tokens, by the same count `requestTokens` sums for a whole request.
|
|
143
|
-
*
|
|
144
|
-
* For the arithmetic that weighs part of a transcript against a window — `planCompaction`'s kept
|
|
145
|
-
* tail — where `estimateTokens` on the text alone would leave out the calls and the envelope.
|
|
146
|
-
*
|
|
147
|
-
* @param message The message as it will be sent.
|
|
148
|
-
* @param options The divisor, `CHARS_PER_TOKEN` when none is given.
|
|
149
|
-
*/
|
|
150
|
-
export declare const messageTokens: (message: OpenAI.ChatCompletionMessageParam, { charsPerToken }?: TokenEstimateOptions) => number;
|
|
151
26
|
/**
|
|
152
27
|
* Whether a refusal means the request was too big, rather than merely refused.
|
|
153
28
|
*
|
|
29
|
+
* @param detail - The endpoint's own message.
|
|
30
|
+
* @returns `true` for an overflow: one of the over-long wordings, beside `token` or `context`.
|
|
31
|
+
* `false` for anything that reads as a rate limit, whatever else it says.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
154
34
|
* Rate limits are ruled out first: they borrow the same words and mean the opposite, being worth
|
|
155
35
|
* another attempt where an overflow never is.
|
|
156
|
-
*
|
|
157
|
-
* @param detail The endpoint's own message.
|
|
158
36
|
*/
|
|
159
37
|
export declare const isOverflow: (detail: string) => boolean;
|
|
160
38
|
/**
|
|
161
39
|
* The smallest window worth believing in, and the floor under `runTurn`'s guard.
|
|
162
40
|
*
|
|
41
|
+
* @remarks
|
|
163
42
|
* `runTurn`'s `contextLimit` reads it as a sanity check on a number it was handed: below this,
|
|
164
43
|
* the limit is taken for a placeholder — an unset column, a listing that said nothing — rather
|
|
165
44
|
* than a window worth refusing a run over. That is the whole of what reads it; `contextLimitFor`
|
|
@@ -176,37 +55,45 @@ export declare const SMALLEST_LIKELY_WINDOW = 8192;
|
|
|
176
55
|
/**
|
|
177
56
|
* Whether a failed request is worth trying again.
|
|
178
57
|
*
|
|
58
|
+
* @param error - The rejection, as caught. What is not an SDK error is not transient.
|
|
59
|
+
* @returns `true` for an `EndpointSilent`, a connection error, a 408, 409 or 429, and any status
|
|
60
|
+
* of 500 or above; `false` for any other SDK error, one with no status included.
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
179
63
|
* The question is whether the request was *refused or lost*, rather than answered with a
|
|
180
64
|
* complaint about its contents: a connection that never landed, a server too busy or too broken
|
|
181
65
|
* to answer, an endpoint that went quiet. A 400 for a malformed tool schema would fail exactly
|
|
182
66
|
* the same way on every attempt, and the two capability cases below are negotiated rather than
|
|
183
67
|
* retried blindly.
|
|
184
|
-
*
|
|
185
|
-
* @param error The rejection, as caught. What is not an SDK error is not transient.
|
|
186
68
|
*/
|
|
187
69
|
export declare function isTransient(error: unknown): boolean;
|
|
188
70
|
/**
|
|
189
71
|
* Whether a failure is the endpoint refusing the request as written, rather than losing it.
|
|
190
72
|
*
|
|
73
|
+
* @param error - The rejection, as caught. What is not an SDK error refuses nothing.
|
|
74
|
+
* @returns `true` for an SDK error whose status is 400 or 422, and for nothing else.
|
|
75
|
+
*
|
|
76
|
+
* @remarks
|
|
191
77
|
* 400 and 422 are what a server says when it read the body and disliked it, which makes them the
|
|
192
78
|
* only statuses worth answering by sending something different. Any 4xx is too wide: 401, 404 and
|
|
193
79
|
* 429 are 4xx and none of them is about the fields, and a 429 is one `isTransient` accepts — a
|
|
194
80
|
* caller that re-sent on it at once doubled the rate against a server that had just asked for less.
|
|
195
81
|
* Catching everything is wider still, since an abort or a connection that never landed then
|
|
196
82
|
* latches off whatever the re-send left out.
|
|
197
|
-
*
|
|
198
|
-
* @param error The rejection, as caught. What is not an SDK error refuses nothing.
|
|
199
83
|
*/
|
|
200
84
|
export declare const refusesRequest: (error: unknown) => boolean;
|
|
201
85
|
/**
|
|
202
86
|
* Whether a failure is a local server still loading the model, rather than one failing to serve.
|
|
203
87
|
*
|
|
88
|
+
* @param error - The rejection, as caught.
|
|
89
|
+
* @returns `true` for a 503 whose body is of type `unavailable_error`, or whose message says the
|
|
90
|
+
* model is loading; `false` for any other status and for what is not an SDK error.
|
|
91
|
+
*
|
|
92
|
+
* @remarks
|
|
204
93
|
* llama.cpp answers 503 `Loading model` with type `unavailable_error` from the moment it starts
|
|
205
94
|
* until the weights are mapped, and a router build says the same while it swaps models. That is
|
|
206
95
|
* thirty to ninety seconds for a large model from a cold page cache, and `backoffMs` gives up
|
|
207
96
|
* inside fifteen: sized for a busy host, not for one reading a file. A plain 503 is not this.
|
|
208
|
-
*
|
|
209
|
-
* @param error The rejection, as caught.
|
|
210
97
|
*/
|
|
211
98
|
export declare function isModelLoading(error: unknown): boolean;
|
|
212
99
|
/** How long to wait between asking a loading server again. */
|
|
@@ -214,15 +101,19 @@ export declare const LOADING_POLL_MS = 3000;
|
|
|
214
101
|
/** How long `runTurn` waits for a model to load unless told otherwise. */
|
|
215
102
|
export declare const LOADING_TIMEOUT_MS = 120000;
|
|
216
103
|
/**
|
|
217
|
-
*
|
|
104
|
+
* How long to wait before a retry: exponential, with jitter so several tasks failing at once do
|
|
105
|
+
* not return in lockstep.
|
|
218
106
|
*
|
|
219
|
-
* @param attempt Zero-based. Doubles from 500ms to a ceiling of eight seconds, before jitter.
|
|
107
|
+
* @param attempt - Zero-based. Doubles from 500ms to a ceiling of eight seconds, before jitter.
|
|
108
|
+
* @returns Milliseconds, somewhere between half of that wait and all of it, and not a whole number.
|
|
220
109
|
*/
|
|
221
110
|
export declare const backoffMs: (attempt: number) => number;
|
|
222
111
|
/**
|
|
223
112
|
* A delay an abort cuts short, rejecting rather than resolving early.
|
|
224
113
|
*
|
|
225
|
-
* @param ms How long to wait.
|
|
226
|
-
* @param signal Abandons the wait. One already aborted rejects without waiting at all.
|
|
114
|
+
* @param ms - How long to wait.
|
|
115
|
+
* @param [signal] - Abandons the wait. One already aborted rejects without waiting at all.
|
|
116
|
+
* @returns A promise that resolves once `ms` has passed, or rejects with the signal's reason — an
|
|
117
|
+
* `Error` of its own where the signal carries none.
|
|
227
118
|
*/
|
|
228
119
|
export declare const sleep: (ms: number, signal?: AbortSignal) => Promise<void>;
|