@cubicecho/agent-core 2.23.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 +14 -9
- package/dist/agent-loop.d.ts +84 -204
- package/dist/agent-loop.js +416 -643
- package/dist/calibration.d.ts +14 -10
- package/dist/calibration.js +30 -22
- package/dist/capabilities.d.ts +140 -35
- package/dist/capabilities.js +345 -152
- package/dist/client.d.ts +104 -35
- package/dist/client.js +210 -78
- package/dist/compaction.d.ts +103 -60
- package/dist/compaction.js +163 -93
- package/dist/config.d.ts +48 -12
- package/dist/config.js +20 -1
- package/dist/continuation.d.ts +25 -17
- package/dist/continuation.js +70 -47
- 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 +108 -58
- package/dist/events.js +265 -128
- package/dist/guards.d.ts +53 -0
- package/dist/guards.js +55 -0
- package/dist/hook-events.d.ts +38 -0
- package/dist/hook-events.js +36 -0
- package/dist/hooks.d.ts +105 -83
- package/dist/hooks.js +224 -139
- package/dist/index.d.ts +30 -25
- package/dist/index.js +29 -23
- 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 +40 -143
- package/dist/retry.js +79 -285
- package/dist/run-calls.d.ts +123 -0
- package/dist/run-calls.js +351 -0
- package/dist/run-turn.d.ts +44 -16
- 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 +51 -11
- package/dist/schema-compat.js +375 -193
- package/dist/scope.d.ts +28 -5
- package/dist/scope.js +37 -10
- package/dist/side-task.d.ts +127 -39
- package/dist/side-task.js +189 -100
- package/dist/snapshot.d.ts +12 -7
- package/dist/snapshot.js +87 -65
- package/dist/spec.d.ts +66 -36
- package/dist/spec.js +514 -265
- package/dist/stream.d.ts +73 -16
- package/dist/stream.js +228 -98
- package/dist/thinking.d.ts +23 -8
- package/dist/thinking.js +96 -46
- package/dist/tokens.d.ts +171 -6
- package/dist/tokens.js +326 -6
- package/dist/tool-calls.d.ts +89 -15
- package/dist/tool-calls.js +205 -85
- package/dist/tool-loading.d.ts +109 -187
- package/dist/tool-loading.js +286 -372
- package/dist/wire.d.ts +81 -0
- package/dist/wire.js +73 -0
- package/llms.txt +87 -43
- package/package.json +4 -4
package/dist/compaction.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type OpenAI from
|
|
2
|
-
import type { Endpoint } from
|
|
3
|
-
import { type HookContext, type
|
|
4
|
-
import { type SideTaskOptions } from
|
|
1
|
+
import type OpenAI from 'openai';
|
|
2
|
+
import type { Endpoint } from './config.ts';
|
|
3
|
+
import { type HookContext, type HookRunner, type OnNote } from './hooks.ts';
|
|
4
|
+
import { type SideTaskOptions } from './side-task.ts';
|
|
5
5
|
/**
|
|
6
6
|
* Keeping a long run inside its window: stale tool results cleared, and the oldest stretch folded
|
|
7
7
|
* into a summary the model writes itself.
|
|
@@ -20,6 +20,7 @@ export declare const KEEP_RATIO = 0.35;
|
|
|
20
20
|
/**
|
|
21
21
|
* The summariser's instruction when the caller gives none.
|
|
22
22
|
*
|
|
23
|
+
* @remarks
|
|
23
24
|
* Asks for notes rather than a retelling, because what the summary replaces is the model's only
|
|
24
25
|
* record of what was decided, and a narrative spends its words on the order things happened in.
|
|
25
26
|
*/
|
|
@@ -30,14 +31,28 @@ export declare const SUMMARY_PROMPT: string;
|
|
|
30
31
|
export declare const SUMMARY_LEAD = "Summary of the earlier part of this conversation, which is no longer shown in full:\n\n";
|
|
31
32
|
/** What `pruneToolResults` takes. */
|
|
32
33
|
export interface PruneOptions {
|
|
33
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* How many of the latest tool results are left whole.
|
|
36
|
+
*
|
|
37
|
+
* @defaultValue `5`
|
|
38
|
+
*/
|
|
34
39
|
keepLast?: number;
|
|
35
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* A result this long or shorter, in characters, is left whole wherever it is.
|
|
42
|
+
*
|
|
43
|
+
* @defaultValue `256`
|
|
44
|
+
*/
|
|
36
45
|
maxChars?: number;
|
|
37
46
|
}
|
|
38
47
|
/**
|
|
39
48
|
* The transcript with every tool result but the latest few replaced by a one-line stub.
|
|
40
49
|
*
|
|
50
|
+
* @param messages - The transcript. Not written to.
|
|
51
|
+
* @param [options] - How many results to keep and how long one must be to clear.
|
|
52
|
+
* @returns `messages` itself when nothing was cleared, otherwise a new array in which each cleared
|
|
53
|
+
* result is a new message and every other one the same object.
|
|
54
|
+
*
|
|
55
|
+
* @remarks
|
|
41
56
|
* The cheap half of compaction. A `read_file` of a 40k-character file is 10k tokens on every turn
|
|
42
57
|
* after it, and by then the model has usually taken what it wanted from it; the stub keeps the
|
|
43
58
|
* call answered — a call with no result is a malformed transcript — and says how much was there,
|
|
@@ -46,9 +61,6 @@ export interface PruneOptions {
|
|
|
46
61
|
* only copy of them the model has (`holdsDefinitions`); either still counts as one of the latest
|
|
47
62
|
* few. Returns the same array when there was nothing to clear. Rewrites the prefix;
|
|
48
63
|
* see the module comment on when to run it.
|
|
49
|
-
*
|
|
50
|
-
* @param messages The transcript. Not written to.
|
|
51
|
-
* @param options How many results to keep and how long one must be to clear.
|
|
52
64
|
*/
|
|
53
65
|
export declare function pruneToolResults(messages: Message[], { keepLast, maxChars }?: PruneOptions): Message[];
|
|
54
66
|
/** What `planCompaction` takes. */
|
|
@@ -60,9 +72,17 @@ export interface CompactionOptions {
|
|
|
60
72
|
* absent is the estimate of the whole transcript.
|
|
61
73
|
*/
|
|
62
74
|
used?: number;
|
|
63
|
-
/**
|
|
75
|
+
/**
|
|
76
|
+
* The fraction of `limit` in use before compacting.
|
|
77
|
+
*
|
|
78
|
+
* @defaultValue `COMPACT_AT`
|
|
79
|
+
*/
|
|
64
80
|
compactAt?: number;
|
|
65
|
-
/**
|
|
81
|
+
/**
|
|
82
|
+
* The fraction of `limit` the kept tail may fill.
|
|
83
|
+
*
|
|
84
|
+
* @defaultValue `KEEP_RATIO`
|
|
85
|
+
*/
|
|
66
86
|
keepRatio?: number;
|
|
67
87
|
/**
|
|
68
88
|
* The fraction of `limit` the whole request should come down to, in place of `keepRatio`: the
|
|
@@ -72,15 +92,22 @@ export interface CompactionOptions {
|
|
|
72
92
|
target?: number;
|
|
73
93
|
/**
|
|
74
94
|
* What the summary is assumed to cost when planning to a `target` — the ceiling its writer is
|
|
75
|
-
* held to, since the plan is made before it is written.
|
|
95
|
+
* held to, since the plan is made before it is written.
|
|
96
|
+
*
|
|
97
|
+
* @defaultValue `1024`, which is `summariser`'s ceiling
|
|
76
98
|
*/
|
|
77
99
|
summaryTokens?: number;
|
|
78
|
-
/**
|
|
100
|
+
/**
|
|
101
|
+
* One message's tokens.
|
|
102
|
+
*
|
|
103
|
+
* @defaultValue `messageTokens` divided by `charsPerToken`
|
|
104
|
+
*/
|
|
79
105
|
estimate?: (message: Message) => number;
|
|
80
106
|
/**
|
|
81
107
|
* The divisor the default `estimate` uses — `charsPerTokenFor` the model, for a transcript
|
|
82
|
-
* weighed the way `runTurn` sizes its requests. `
|
|
83
|
-
*
|
|
108
|
+
* weighed the way `runTurn` sizes its requests. Ignored beside an `estimate` of the caller's own.
|
|
109
|
+
*
|
|
110
|
+
* @defaultValue `CHARS_PER_TOKEN`
|
|
84
111
|
*/
|
|
85
112
|
charsPerToken?: number;
|
|
86
113
|
/**
|
|
@@ -114,6 +141,13 @@ export interface CompactionPlan {
|
|
|
114
141
|
/**
|
|
115
142
|
* Where to fold a transcript that has grown into its window, or `undefined` when it should not be.
|
|
116
143
|
*
|
|
144
|
+
* @param messages - The transcript, system prompts included if the caller keeps them in it.
|
|
145
|
+
* @param options - The window, what is in use, the ratios, and where the last fold ended. See
|
|
146
|
+
* `CompactionOptions`.
|
|
147
|
+
* @returns The plan, its `toSummarise` a new array of the same message objects, or `undefined` when
|
|
148
|
+
* there is nothing to fold. `messages` is not written to.
|
|
149
|
+
*
|
|
150
|
+
* @remarks
|
|
117
151
|
* The kept tail is walked back from the end until it fills `keepRatio` of the window, then moved
|
|
118
152
|
* forward onto a user message: a transcript resuming mid-exchange — a tool result with no call
|
|
119
153
|
* before it, a reply with no question — is malformed and servers refuse it. The system prompts at
|
|
@@ -134,26 +168,26 @@ export interface CompactionPlan {
|
|
|
134
168
|
* comes, and `after` says how far short it fell. An earlier summary the new one replaces is not
|
|
135
169
|
* credited, so `after` errs high by at most that much. It is only as good as `estimate` — see
|
|
136
170
|
* `estimateFrom` for one that is measured.
|
|
137
|
-
*
|
|
138
|
-
* @param messages The transcript, system prompts included if the caller keeps them in it.
|
|
139
|
-
* @param options The window, what is in use, the ratios, and where the last fold ended. See
|
|
140
|
-
* `CompactionOptions`.
|
|
141
171
|
*/
|
|
142
172
|
export declare function planCompaction(messages: Message[], { limit, used, compactAt, keepRatio, target, summaryTokens, charsPerToken, estimate, from: givenFrom, previous: givenPrevious, }: CompactionOptions): CompactionPlan | undefined;
|
|
143
173
|
/**
|
|
144
174
|
* What the summariser is handed for a plan: the earlier summary if there was one, then each
|
|
145
175
|
* message as its role and at most 4000 characters of its text.
|
|
146
176
|
*
|
|
147
|
-
* @param plan What `planCompaction` returned.
|
|
177
|
+
* @param plan - What `planCompaction` returned.
|
|
178
|
+
* @returns The text for the summariser, messages a blank line apart. A message with neither text
|
|
179
|
+
* nor calls is left out, and a plan with a `previous` opens with it under `Notes so far:`.
|
|
148
180
|
*/
|
|
149
181
|
export declare function summaryInput(plan: CompactionPlan): string;
|
|
150
182
|
/**
|
|
151
183
|
* A summariser that asks `model` with `SUMMARY_PROMPT`, for `compactTranscript`.
|
|
152
184
|
*
|
|
153
|
-
* @param config The endpoint the summary is written through.
|
|
154
|
-
* @param model The model to write it, which may be a smaller one than the run's.
|
|
155
|
-
* @param options Cancellation and notices
|
|
156
|
-
*
|
|
185
|
+
* @param config - The endpoint the summary is written through.
|
|
186
|
+
* @param model - The model to write it, which may be a smaller one than the run's.
|
|
187
|
+
* @param [options] - Cancellation and notices, and the instruction and ceiling the summary is
|
|
188
|
+
* written under.
|
|
189
|
+
* @returns A function from `summaryInput`'s text to the summary. It rejects as `ask` does, and
|
|
190
|
+
* resolves with an empty string where the model wrote nothing.
|
|
157
191
|
*/
|
|
158
192
|
export declare const summariser: (config: Endpoint, model: string, { system, maxTokens, ...options }?: SideTaskOptions & {
|
|
159
193
|
system?: string;
|
|
@@ -161,6 +195,7 @@ export declare const summariser: (config: Endpoint, model: string, { system, max
|
|
|
161
195
|
/**
|
|
162
196
|
* One fold, as a host that keeps its transcript append-only stores it.
|
|
163
197
|
*
|
|
198
|
+
* @remarks
|
|
164
199
|
* The other half of `compactTranscript`: the same work, recorded rather than applied. A host that
|
|
165
200
|
* persists this beside an untouched transcript still shows the user every message, can undo a fold
|
|
166
201
|
* by dropping one row, and rebuilds the request with `applyCompaction` — where a host that keeps
|
|
@@ -179,12 +214,12 @@ export interface CompactionRunOptions {
|
|
|
179
214
|
/**
|
|
180
215
|
* Hooks to tell. `context` is extended with `compacting` and `range`, whose indexes are the
|
|
181
216
|
* plan's — and so the host's own, for a plan made over a stored transcript. `honourVeto` waits
|
|
182
|
-
* for the hooks and lets one stop the compaction;
|
|
217
|
+
* for the hooks and lets one stop the compaction; without it they add no latency.
|
|
183
218
|
*/
|
|
184
219
|
hooks?: {
|
|
185
220
|
run: HookRunner;
|
|
186
221
|
context: HookContext;
|
|
187
|
-
onNote?:
|
|
222
|
+
onNote?: OnNote;
|
|
188
223
|
honourVeto?: boolean;
|
|
189
224
|
};
|
|
190
225
|
/**
|
|
@@ -196,58 +231,75 @@ export interface CompactionRunOptions {
|
|
|
196
231
|
/**
|
|
197
232
|
* The hooks and the summariser for a plan, as a record to store rather than a transcript to send.
|
|
198
233
|
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* @param messages The transcript the plan was made for. Read only, and only for the hooks.
|
|
204
|
-
* @param plan What `planCompaction` returned for it.
|
|
205
|
-
* @param summarise Writes the summary from `summaryInput`'s text. See `summariser`. Not called
|
|
234
|
+
* @param messages - The transcript the plan was made for. Read only, and only for the hooks.
|
|
235
|
+
* @param plan - What `planCompaction` returned for it.
|
|
236
|
+
* @param summarise - Writes the summary from `summaryInput`'s text. See `summariser`. Not called
|
|
206
237
|
* when a hook vetoes.
|
|
207
|
-
* @param options Hooks to tell and whether the window is already past. See `CompactionRunOptions`.
|
|
238
|
+
* @param [options] - Hooks to tell and whether the window is already past. See `CompactionRunOptions`.
|
|
208
239
|
* @returns `undefined` when nothing was folded — a hook vetoed, or the summary came back empty —
|
|
209
240
|
* so the caller stores nothing and the transcript is still whole.
|
|
241
|
+
*
|
|
242
|
+
* @remarks
|
|
243
|
+
* What `compactTranscript` does before it rewrites anything, which is all a host needs when the
|
|
244
|
+
* fold lives on the session row and the messages stay where they are. Nothing here is persisted or
|
|
245
|
+
* logged — that is the host's, and so is deciding what to do with a fold that did not happen.
|
|
210
246
|
*/
|
|
211
247
|
export declare function runCompaction(messages: Message[], plan: CompactionPlan, summarise: (text: string) => Promise<string>, { hooks, forced }?: CompactionRunOptions): Promise<CompactionRecord | undefined>;
|
|
212
248
|
/**
|
|
213
249
|
* The transcript as the server should see it: the folded head replaced by its summary.
|
|
214
250
|
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
* continues these notes rather than summarising them a second time. Any earlier summary message in
|
|
218
|
-
* the kept head is dropped, since the record's already contains it.
|
|
219
|
-
*
|
|
220
|
-
* @param messages The stored transcript, whole. Not written to.
|
|
221
|
-
* @param record The fold, or `undefined` for a session that has not been compacted, which hands
|
|
251
|
+
* @param messages - The stored transcript, whole. Not written to.
|
|
252
|
+
* @param [record] - The fold, or `undefined` for a session that has not been compacted, which hands
|
|
222
253
|
* back `messages` itself.
|
|
223
|
-
* @param options `from` is the first message the fold was allowed to take — the plan's, for a host
|
|
254
|
+
* @param [options] - `from` is the first message the fold was allowed to take — the plan's, for a host
|
|
224
255
|
* that keeps its system prompts in the array; everything before it is kept ahead of the summary.
|
|
225
256
|
* Absent, the leading `system` messages are found by scanning, and zero of them is the ordinary
|
|
226
257
|
* case for a host whose system prompt is a separate argument.
|
|
258
|
+
* @returns `messages` itself when there is no record or its summary is blank, otherwise a new
|
|
259
|
+
* array: the kept head, one summary message, then everything from the record's `through` on.
|
|
260
|
+
*
|
|
261
|
+
* @remarks
|
|
262
|
+
* The inverse of storing a `CompactionRecord`, and the shape `planCompaction` expects to meet
|
|
263
|
+
* again — the same `SUMMARY_LEAD`, in a `system` message at the same place — so the next fold
|
|
264
|
+
* continues these notes rather than summarising them a second time. Any earlier summary message in
|
|
265
|
+
* the kept head is dropped, since the record's already contains it.
|
|
227
266
|
*/
|
|
228
|
-
export declare function applyCompaction(messages: Message[], record?: Pick<CompactionRecord,
|
|
267
|
+
export declare function applyCompaction(messages: Message[], record?: Pick<CompactionRecord, 'summary' | 'through'>, { from }?: {
|
|
229
268
|
from?: number;
|
|
230
269
|
}): Message[];
|
|
231
270
|
/**
|
|
232
271
|
* Where a stored index sits in the request `applyCompaction` builds, once a fold has shifted
|
|
233
272
|
* everything after it.
|
|
234
273
|
*
|
|
274
|
+
* @param index - The position in the stored transcript.
|
|
275
|
+
* @param [record] - The fold in force, or `undefined` for a session that has none, which hands the
|
|
276
|
+
* index straight back.
|
|
277
|
+
* @param [head] - How many messages the request keeps ahead of the summary — the leading system
|
|
278
|
+
* prompts, when the host keeps them in the array. Zero is the stored-fold case,
|
|
279
|
+
* where the summary is the request's first message.
|
|
280
|
+
* @returns The index in the request. One before the record's `through` gives `head`, where the
|
|
281
|
+
* summary sits — an index among the kept head messages themselves included.
|
|
282
|
+
*
|
|
283
|
+
* @remarks
|
|
235
284
|
* A transcript that stays append-only and a request that does not are two numberings of the same
|
|
236
285
|
* conversation, and anything that names a position — `withContext`'s index, a range handed to a
|
|
237
286
|
* hook — has to say which it is in. An index inside the folded stretch answers with the summary
|
|
238
287
|
* message that now stands for it.
|
|
239
|
-
*
|
|
240
|
-
* @param index The position in the stored transcript.
|
|
241
|
-
* @param record The fold in force, or `undefined` for a session that has none, which hands the
|
|
242
|
-
* index straight back.
|
|
243
|
-
* @param head How many messages the request keeps ahead of the summary — the leading system
|
|
244
|
-
* prompts, when the host keeps them in the array. Zero, the default, is the stored-fold case,
|
|
245
|
-
* where the summary is the request's first message.
|
|
246
288
|
*/
|
|
247
|
-
export declare const requestIndex: (index: number, record?: Pick<CompactionRecord,
|
|
289
|
+
export declare const requestIndex: (index: number, record?: Pick<CompactionRecord, 'through'>, head?: number) => number;
|
|
248
290
|
/**
|
|
249
291
|
* The transcript with the plan's stretch replaced by one system message holding its summary.
|
|
250
292
|
*
|
|
293
|
+
* @param messages - The transcript the plan was made for. Not written to.
|
|
294
|
+
* @param plan - What `planCompaction` returned for it.
|
|
295
|
+
* @param summarise - Writes the summary from `summaryInput`'s text. See `summariser`. Not called
|
|
296
|
+
* when a hook vetoes.
|
|
297
|
+
* @param [options] - Hooks to tell and whether the window is already past. See
|
|
298
|
+
* `CompactionRunOptions`.
|
|
299
|
+
* @returns `messages` itself when nothing was folded — a veto or an empty summary — otherwise a
|
|
300
|
+
* new array.
|
|
301
|
+
*
|
|
302
|
+
* @remarks
|
|
251
303
|
* `beforeCompact` is told what is being folded while the summary is written, beside it rather
|
|
252
304
|
* than ahead of it — a memory server filing it is not a rescue worth making the run wait for, and
|
|
253
305
|
* `notify` never rejects. A host that wants its hooks able to stop a compaction sets
|
|
@@ -261,15 +313,6 @@ export declare const requestIndex: (index: number, record?: Pick<CompactionRecor
|
|
|
261
313
|
* `runCompaction` and `applyCompaction` are its two halves, and it is nothing but the two in
|
|
262
314
|
* order, so a host that stores the fold instead of the array gets the same summary at the same
|
|
263
315
|
* cut rather than a second implementation that drifts from this one.
|
|
264
|
-
*
|
|
265
|
-
* @param messages The transcript the plan was made for. Not written to.
|
|
266
|
-
* @param plan What `planCompaction` returned for it.
|
|
267
|
-
* @param summarise Writes the summary from `summaryInput`'s text. See `summariser`. Not called
|
|
268
|
-
* when a hook vetoes.
|
|
269
|
-
* @param options Hooks to tell and whether the window is already past. See
|
|
270
|
-
* `CompactionRunOptions`.
|
|
271
|
-
* @returns `messages` itself when nothing was folded — a veto or an empty summary — otherwise a
|
|
272
|
-
* new array.
|
|
273
316
|
*/
|
|
274
317
|
export declare function compactTranscript(messages: Message[], plan: CompactionPlan, summarise: (text: string) => Promise<string>, options?: CompactionRunOptions): Promise<Message[]>;
|
|
275
318
|
export {};
|