@womp/spark-agent 6.5.0 → 7.0.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/CHANGELOG.md +50 -0
- package/README.md +7 -4
- package/dist/agent.d.ts +3 -6
- package/dist/agent.js +41 -23
- package/dist/compaction.d.ts +41 -8
- package/dist/compaction.js +241 -77
- package/dist/context-window.d.ts +23 -0
- package/dist/context-window.js +141 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -1
- package/dist/ui.d.ts +7 -0
- package/dist/ui.js +1 -0
- package/package.json +1 -1
- package/dist/context-budget.d.ts +0 -65
- package/dist/context-budget.js +0 -364
package/dist/ui.d.ts
CHANGED
|
@@ -106,6 +106,12 @@ export type SparkSelectionData = {
|
|
|
106
106
|
export type SparkCompaction = {
|
|
107
107
|
/** The user message the model reads verbatim from; everything before it is summarized. */
|
|
108
108
|
firstKeptMessageId: string;
|
|
109
|
+
/**
|
|
110
|
+
* Set when the summary was written during its turn: the first step of the
|
|
111
|
+
* recording message the model still reads, counted from 0. Its earlier steps
|
|
112
|
+
* are summarized too.
|
|
113
|
+
*/
|
|
114
|
+
firstKeptStep?: number;
|
|
109
115
|
summary: string;
|
|
110
116
|
/** Skills read in the summarized messages; their current text is sent after the summary. */
|
|
111
117
|
skills: string[];
|
|
@@ -363,6 +369,7 @@ export declare const sparkDataPartSchemas: {
|
|
|
363
369
|
}, z.core.$strip>;
|
|
364
370
|
readonly compaction: z.ZodObject<{
|
|
365
371
|
firstKeptMessageId: z.ZodString;
|
|
372
|
+
firstKeptStep: z.ZodOptional<z.ZodNumber>;
|
|
366
373
|
summary: z.ZodString;
|
|
367
374
|
skills: z.ZodArray<z.ZodString>;
|
|
368
375
|
modelId: z.ZodString;
|
package/dist/ui.js
CHANGED
|
@@ -88,6 +88,7 @@ export const sparkDataPartSchemas = {
|
|
|
88
88
|
selection: z.object({ nodeIds: z.array(z.string().min(1).max(64)).min(1).max(8192) }),
|
|
89
89
|
compaction: z.object({
|
|
90
90
|
firstKeptMessageId: z.string().min(1),
|
|
91
|
+
firstKeptStep: z.number().int().positive().optional(),
|
|
91
92
|
summary: z.string().min(1),
|
|
92
93
|
skills: z.array(z.string().min(1)),
|
|
93
94
|
modelId: z.string(),
|
package/package.json
CHANGED
package/dist/context-budget.d.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import type { ModelMessage } from "ai";
|
|
2
|
-
/** Under prose's ~3.5-4, so the estimate errs high. */
|
|
3
|
-
export declare const CHARS_PER_TOKEN = 3;
|
|
4
|
-
/** About what one image at the providers' 1568 px ceiling costs. */
|
|
5
|
-
export declare const IMAGE_TOKENS = 1600;
|
|
6
|
-
export declare const MESSAGE_OVERHEAD_TOKENS = 4;
|
|
7
|
-
/** Per tool: its name, description and input schema around the counted text. */
|
|
8
|
-
export declare const TOOL_DEFINITION_OVERHEAD_TOKENS = 300;
|
|
9
|
-
/** Start trimming once the estimate passes this share of the budget. */
|
|
10
|
-
export declare const TRIM_TRIGGER_RATIO = 0.9;
|
|
11
|
-
/** Then trim down to this share, so the next steps do not trim again. */
|
|
12
|
-
export declare const TRIM_TARGET_RATIO = 0.75;
|
|
13
|
-
/** A tool result at or below this is left whole: not worth the detail it loses. */
|
|
14
|
-
export declare const SMALL_TOOL_OUTPUT_TOKENS = 500;
|
|
15
|
-
/**
|
|
16
|
-
* Steps of the CURRENT turn kept whole, however long that turn runs.
|
|
17
|
-
*
|
|
18
|
-
* The model is mid-task and reasons from what it just did, so the recent steps
|
|
19
|
-
* have to survive; the ones before them are history like any other and are worth
|
|
20
|
-
* more as window than as detail. Five is small enough that a long run always
|
|
21
|
-
* recovers room and large enough to hold a read-edit-verify cycle.
|
|
22
|
-
*/
|
|
23
|
-
export declare const PROTECTED_CURRENT_STEPS = 5;
|
|
24
|
-
export declare const OMITTED_HISTORY_TEXT = "[Earlier messages in this conversation were left out of this request to fit the model's context window.]";
|
|
25
|
-
export declare const OMITTED_HISTORY_IMAGE_TEXT = "[An earlier image was left out of this request to fit the model's context window.]";
|
|
26
|
-
export declare const OMITTED_TOOL_OUTPUT_TEXT = "[This earlier tool result was left out of this request to fit the model's context window.]";
|
|
27
|
-
/**
|
|
28
|
-
* The model's context window, or undefined when it is not known.
|
|
29
|
-
*
|
|
30
|
-
* Listed models first, then model families whose window is certain. Anything
|
|
31
|
-
* else -- a third-party model routed through OpenRouter -- is undefined, and the
|
|
32
|
-
* conversation is then left exactly as it was: overestimating a window can only
|
|
33
|
-
* repeat today's failure, but underestimating one would silently hide history
|
|
34
|
-
* from a model that could read it.
|
|
35
|
-
*/
|
|
36
|
-
export declare function contextWindowFor(modelId: string): number | undefined;
|
|
37
|
-
export type ContextBudgetOptions = {
|
|
38
|
-
/** Undefined: the conversation is returned untouched, with no report. */
|
|
39
|
-
contextWindow: number | undefined;
|
|
40
|
-
/** Reserved for the response. */
|
|
41
|
-
maxOutputTokens?: number;
|
|
42
|
-
system?: string;
|
|
43
|
-
/** The request's tool set, by name. */
|
|
44
|
-
tools?: Record<string, unknown>;
|
|
45
|
-
};
|
|
46
|
-
export type ContextBudgetReport = {
|
|
47
|
-
/** What the messages may use: the window less output, system and tools. */
|
|
48
|
-
budgetTokens: number;
|
|
49
|
-
originalTokens: number;
|
|
50
|
-
estimatedTokens: number;
|
|
51
|
-
omittedImages: number;
|
|
52
|
-
omittedToolOutputs: number;
|
|
53
|
-
omittedMessages: number;
|
|
54
|
-
/** False when even the current turn alone is over the budget. */
|
|
55
|
-
fits: boolean;
|
|
56
|
-
};
|
|
57
|
-
export type ContextBudgetResult = {
|
|
58
|
-
messages: ModelMessage[];
|
|
59
|
-
report?: ContextBudgetReport;
|
|
60
|
-
};
|
|
61
|
-
export declare function fitMessagesToContext(messages: ModelMessage[], options: ContextBudgetOptions): ContextBudgetResult;
|
|
62
|
-
/** The estimate for a list of messages, as `fitMessagesToContext` counts them. */
|
|
63
|
-
export declare function estimateMessagesTokens(messages: ModelMessage[]): number;
|
|
64
|
-
/** The estimate for a whole request: its messages, system prompt and tool definitions. */
|
|
65
|
-
export declare function estimateRequestTokens(messages: ModelMessage[], options: Pick<ContextBudgetOptions, "system" | "tools">): number;
|
package/dist/context-budget.js
DELETED
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
// Keep one model request inside the model's context window.
|
|
2
|
-
//
|
|
3
|
-
// A Spark turn sends the whole conversation on every step: for a thread that
|
|
4
|
-
// Swan and Crow both write to, that is every earlier turn, every tool result
|
|
5
|
-
// (each up to MODEL_RESULT_BYTE_LIMIT, ./bounded-output.ts) and every image. Nothing
|
|
6
|
-
// bounded the total, so a long enough thread passed the provider's limit and every
|
|
7
|
-
// later turn failed on its first request ("prompt is too long: 1006655 tokens >
|
|
8
|
-
// 1000000 maximum") -- permanently, since each failed attempt only adds rows.
|
|
9
|
-
//
|
|
10
|
-
// This runs on the messages of every step, after the host's own preparation and
|
|
11
|
-
// before the request is built, in both executors: the shared loop (./agent.ts,
|
|
12
|
-
// which Crow runs) and the browser's own loop in Swan.
|
|
13
|
-
//
|
|
14
|
-
// WHAT, in this order, until the estimate is back under the target:
|
|
15
|
-
//
|
|
16
|
-
// 1. Images before the current turn, oldest first -- the most tokens for the
|
|
17
|
-
// least text, and the ones a later step least needs to see again. The
|
|
18
|
-
// agent's own screenshots go before the images the user attached, which
|
|
19
|
-
// are what the work is often built from.
|
|
20
|
-
// 2. Large tool results before the current turn, oldest first. The call and a
|
|
21
|
-
// result part stay, with a note in place of the output, so no provider is
|
|
22
|
-
// handed a call without its result.
|
|
23
|
-
// 3. Whole turns, oldest first, cut at user messages so every remaining call is
|
|
24
|
-
// still paired with its result. A note in their place tells the model that
|
|
25
|
-
// earlier messages were left out.
|
|
26
|
-
//
|
|
27
|
-
// THE CURRENT TURN is the latest user message and everything after it. The
|
|
28
|
-
// message itself -- the question and any image the user just attached -- is never
|
|
29
|
-
// changed, and neither are the last PROTECTED_CURRENT_STEPS steps after it: the
|
|
30
|
-
// model has to be able to see what it just did.
|
|
31
|
-
//
|
|
32
|
-
// Its EARLIER steps are trimmed like any other history, by stages 1 and 2 above.
|
|
33
|
-
// They were exempt until 2026-10-01, on the assumption that a turn is small. An
|
|
34
|
-
// agentic run's turn is not: every step it takes lands after that user message,
|
|
35
|
-
// and `maxSteps` is 100. dev0 thread 142417 reached the window that way -- ten
|
|
36
|
-
// minutes of work, earlier history already trimmed to nothing, and what was left
|
|
37
|
-
// over the limit was the run's own tool results, which nothing was allowed to
|
|
38
|
-
// touch. Stage 3 still stops at the current turn, because dropping whole messages
|
|
39
|
-
// inside it cannot keep calls paired with their results.
|
|
40
|
-
//
|
|
41
|
-
// When the protected part alone is over the window the request still goes out as
|
|
42
|
-
// it is, and the provider's own error says so.
|
|
43
|
-
//
|
|
44
|
-
// ESTIMATES, NOT COUNTS. Counting needs the provider's tokenizer or a request.
|
|
45
|
-
// The estimate is deliberately high (CHARS_PER_TOKEN is under the ~3.5-4 of prose)
|
|
46
|
-
// and trimming starts at TRIM_TRIGGER_RATIO of the budget, so dense JSON that
|
|
47
|
-
// tokenizes worse than prose still lands under the real limit. Images count by
|
|
48
|
-
// number, never by their bytes.
|
|
49
|
-
//
|
|
50
|
-
// STABLE ACROSS STEPS. Once over, the conversation is trimmed to
|
|
51
|
-
// TRIM_TARGET_RATIO rather than to the limit, and always oldest first, so the next
|
|
52
|
-
// steps of the run trim the same prefix again instead of a little more each time --
|
|
53
|
-
// which keeps a provider's prompt cache warm.
|
|
54
|
-
import { SPARK_MODELS } from "./models.js";
|
|
55
|
-
/** Under prose's ~3.5-4, so the estimate errs high. */
|
|
56
|
-
export const CHARS_PER_TOKEN = 3;
|
|
57
|
-
/** About what one image at the providers' 1568 px ceiling costs. */
|
|
58
|
-
export const IMAGE_TOKENS = 1_600;
|
|
59
|
-
export const MESSAGE_OVERHEAD_TOKENS = 4;
|
|
60
|
-
/** Per tool: its name, description and input schema around the counted text. */
|
|
61
|
-
export const TOOL_DEFINITION_OVERHEAD_TOKENS = 300;
|
|
62
|
-
/** Start trimming once the estimate passes this share of the budget. */
|
|
63
|
-
export const TRIM_TRIGGER_RATIO = 0.9;
|
|
64
|
-
/** Then trim down to this share, so the next steps do not trim again. */
|
|
65
|
-
export const TRIM_TARGET_RATIO = 0.75;
|
|
66
|
-
/** A tool result at or below this is left whole: not worth the detail it loses. */
|
|
67
|
-
export const SMALL_TOOL_OUTPUT_TOKENS = 500;
|
|
68
|
-
/**
|
|
69
|
-
* Steps of the CURRENT turn kept whole, however long that turn runs.
|
|
70
|
-
*
|
|
71
|
-
* The model is mid-task and reasons from what it just did, so the recent steps
|
|
72
|
-
* have to survive; the ones before them are history like any other and are worth
|
|
73
|
-
* more as window than as detail. Five is small enough that a long run always
|
|
74
|
-
* recovers room and large enough to hold a read-edit-verify cycle.
|
|
75
|
-
*/
|
|
76
|
-
export const PROTECTED_CURRENT_STEPS = 5;
|
|
77
|
-
export const OMITTED_HISTORY_TEXT = "[Earlier messages in this conversation were left out of this request to fit the model's context window.]";
|
|
78
|
-
export const OMITTED_HISTORY_IMAGE_TEXT = "[An earlier image was left out of this request to fit the model's context window.]";
|
|
79
|
-
export const OMITTED_TOOL_OUTPUT_TEXT = "[This earlier tool result was left out of this request to fit the model's context window.]";
|
|
80
|
-
/**
|
|
81
|
-
* The model's context window, or undefined when it is not known.
|
|
82
|
-
*
|
|
83
|
-
* Listed models first, then model families whose window is certain. Anything
|
|
84
|
-
* else -- a third-party model routed through OpenRouter -- is undefined, and the
|
|
85
|
-
* conversation is then left exactly as it was: overestimating a window can only
|
|
86
|
-
* repeat today's failure, but underestimating one would silently hide history
|
|
87
|
-
* from a model that could read it.
|
|
88
|
-
*/
|
|
89
|
-
export function contextWindowFor(modelId) {
|
|
90
|
-
const listed = SPARK_MODELS.find(({ id }) => id === modelId)?.contextWindow;
|
|
91
|
-
if (listed !== undefined)
|
|
92
|
-
return listed;
|
|
93
|
-
if (/^claude-haiku-/.test(modelId))
|
|
94
|
-
return 200_000;
|
|
95
|
-
if (/^claude-/.test(modelId))
|
|
96
|
-
return 1_000_000;
|
|
97
|
-
if (/^gpt-6-/.test(modelId))
|
|
98
|
-
return 1_050_000;
|
|
99
|
-
if (/^gemini-/.test(modelId))
|
|
100
|
-
return 1_048_576;
|
|
101
|
-
return undefined;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Where the current turn's protected tail begins: the Nth-from-last step.
|
|
105
|
-
*
|
|
106
|
-
* Counted by ASSISTANT messages, because that is what starts a step -- the tool
|
|
107
|
-
* messages that answer it follow and are protected with it, so no call is ever
|
|
108
|
-
* separated from its result. A turn with fewer steps than that protects all of
|
|
109
|
-
* them, which is what this did for every turn before it could trim one.
|
|
110
|
-
*/
|
|
111
|
-
function protectedTailStart(messages, current) {
|
|
112
|
-
let steps = 0;
|
|
113
|
-
for (let i = messages.length - 1; i > current; i -= 1) {
|
|
114
|
-
if (messages[i]?.role !== "assistant")
|
|
115
|
-
continue;
|
|
116
|
-
steps += 1;
|
|
117
|
-
if (steps >= PROTECTED_CURRENT_STEPS)
|
|
118
|
-
return i;
|
|
119
|
-
}
|
|
120
|
-
return current + 1;
|
|
121
|
-
}
|
|
122
|
-
export function fitMessagesToContext(messages, options) {
|
|
123
|
-
if (options.contextWindow === undefined)
|
|
124
|
-
return { messages };
|
|
125
|
-
const budgetTokens = Math.max(0, options.contextWindow -
|
|
126
|
-
(options.maxOutputTokens ?? 0) -
|
|
127
|
-
textTokens(options.system ?? "") -
|
|
128
|
-
toolsTokens(options.tools));
|
|
129
|
-
const sizes = messages.map(messageTokens);
|
|
130
|
-
const originalTokens = sum(sizes);
|
|
131
|
-
const report = {
|
|
132
|
-
budgetTokens,
|
|
133
|
-
originalTokens,
|
|
134
|
-
estimatedTokens: originalTokens,
|
|
135
|
-
omittedImages: 0,
|
|
136
|
-
omittedToolOutputs: 0,
|
|
137
|
-
omittedMessages: 0,
|
|
138
|
-
fits: originalTokens <= budgetTokens,
|
|
139
|
-
};
|
|
140
|
-
if (originalTokens <= budgetTokens * TRIM_TRIGGER_RATIO)
|
|
141
|
-
return { messages, report };
|
|
142
|
-
// The current turn starts at the latest user message. With no user message at
|
|
143
|
-
// all there is no turn to reason about and nothing is touched.
|
|
144
|
-
const current = lastIndex(messages, ({ role }) => role === "user");
|
|
145
|
-
if (current < 0)
|
|
146
|
-
return { messages, report };
|
|
147
|
-
// Stages 1 and 2 rewrite everything before this: all earlier turns, and the
|
|
148
|
-
// current turn's own earlier steps. `current` itself is skipped -- the user's
|
|
149
|
-
// question and the image they just attached are not history.
|
|
150
|
-
const protectedFrom = protectedTailStart(messages, current);
|
|
151
|
-
const target = budgetTokens * TRIM_TARGET_RATIO;
|
|
152
|
-
const out = messages.slice();
|
|
153
|
-
let total = originalTokens;
|
|
154
|
-
/**
|
|
155
|
-
* Rewrites the parts of each earlier message, oldest first, until the estimate
|
|
156
|
-
* is under the target. `rewrite` answers the same part to keep it, or its
|
|
157
|
-
* replacement and roughly what that saves -- used only to stop partway through a
|
|
158
|
-
* message, which is then measured again.
|
|
159
|
-
*/
|
|
160
|
-
const rewriteEarlier = (rewrite, only = () => true) => {
|
|
161
|
-
for (let i = 0; i < protectedFrom && total > target; i += 1) {
|
|
162
|
-
if (i === current)
|
|
163
|
-
continue;
|
|
164
|
-
const message = out[i];
|
|
165
|
-
if (message === undefined || !Array.isArray(message.content) || !only(message))
|
|
166
|
-
continue;
|
|
167
|
-
let projected = total;
|
|
168
|
-
let changed = false;
|
|
169
|
-
const content = message.content.map((part) => {
|
|
170
|
-
if (projected <= target)
|
|
171
|
-
return part;
|
|
172
|
-
const result = rewrite(part, projected);
|
|
173
|
-
if (result.part === part)
|
|
174
|
-
return part;
|
|
175
|
-
changed = true;
|
|
176
|
-
projected -= result.saved;
|
|
177
|
-
return result.part;
|
|
178
|
-
});
|
|
179
|
-
if (!changed)
|
|
180
|
-
continue;
|
|
181
|
-
const next = { ...message, content };
|
|
182
|
-
const size = messageTokens(next);
|
|
183
|
-
total += size - (sizes[i] ?? 0);
|
|
184
|
-
sizes[i] = size;
|
|
185
|
-
out[i] = next;
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
// 1. Earlier images: the agent's own first, then those the user attached.
|
|
189
|
-
const omitImages = (part, projected) => {
|
|
190
|
-
if (isImage(part)) {
|
|
191
|
-
report.omittedImages += 1;
|
|
192
|
-
return { part: omittedImage(), saved: IMAGE_TOKENS };
|
|
193
|
-
}
|
|
194
|
-
if (!isToolResult(part) || part.output.type !== "content" || !Array.isArray(part.output.value)) {
|
|
195
|
-
return { part, saved: 0 };
|
|
196
|
-
}
|
|
197
|
-
let saved = 0;
|
|
198
|
-
const value = part.output.value.map((item) => {
|
|
199
|
-
if (projected - saved <= target || !isImage(item))
|
|
200
|
-
return item;
|
|
201
|
-
report.omittedImages += 1;
|
|
202
|
-
saved += IMAGE_TOKENS;
|
|
203
|
-
return omittedImage();
|
|
204
|
-
});
|
|
205
|
-
return saved === 0 ? { part, saved } : { part: { ...part, output: { ...part.output, value } }, saved };
|
|
206
|
-
};
|
|
207
|
-
rewriteEarlier(omitImages, ({ role }) => role !== "user");
|
|
208
|
-
rewriteEarlier(omitImages, ({ role }) => role === "user");
|
|
209
|
-
// 2. Earlier large tool results.
|
|
210
|
-
rewriteEarlier((part) => {
|
|
211
|
-
if (!isToolResult(part))
|
|
212
|
-
return { part, saved: 0 };
|
|
213
|
-
const saved = outputTokens(part.output);
|
|
214
|
-
if (saved <= SMALL_TOOL_OUTPUT_TOKENS)
|
|
215
|
-
return { part, saved: 0 };
|
|
216
|
-
report.omittedToolOutputs += 1;
|
|
217
|
-
return { part: { ...part, output: { type: "text", value: OMITTED_TOOL_OUTPUT_TEXT } }, saved };
|
|
218
|
-
});
|
|
219
|
-
// 3. Whole earlier turns, cut at user messages.
|
|
220
|
-
if (total > target) {
|
|
221
|
-
const note = { role: "user", content: [{ type: "text", text: OMITTED_HISTORY_TEXT }] };
|
|
222
|
-
const noteTokens = messageTokens(note);
|
|
223
|
-
const ends = [];
|
|
224
|
-
out.forEach(({ role }, i) => {
|
|
225
|
-
if (i > 0 && i < current && role === "user")
|
|
226
|
-
ends.push(i);
|
|
227
|
-
});
|
|
228
|
-
ends.push(current);
|
|
229
|
-
let cut = 0;
|
|
230
|
-
for (const end of ends) {
|
|
231
|
-
if (total + noteTokens <= target)
|
|
232
|
-
break;
|
|
233
|
-
for (let i = cut; i < end; i += 1) {
|
|
234
|
-
const message = out[i];
|
|
235
|
-
if (message === undefined || message.role === "system")
|
|
236
|
-
continue;
|
|
237
|
-
total -= sizes[i] ?? 0;
|
|
238
|
-
report.omittedMessages += 1;
|
|
239
|
-
}
|
|
240
|
-
cut = end;
|
|
241
|
-
}
|
|
242
|
-
if (cut > 0) {
|
|
243
|
-
const kept = out.slice(0, cut).filter(({ role }) => role === "system");
|
|
244
|
-
out.splice(0, cut, ...kept, note);
|
|
245
|
-
total += noteTokens;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
report.estimatedTokens = total;
|
|
249
|
-
report.fits = total <= budgetTokens;
|
|
250
|
-
return { messages: out, report };
|
|
251
|
-
}
|
|
252
|
-
/** The estimate for a list of messages, as `fitMessagesToContext` counts them. */
|
|
253
|
-
export function estimateMessagesTokens(messages) {
|
|
254
|
-
return sum(messages.map(messageTokens));
|
|
255
|
-
}
|
|
256
|
-
/** The estimate for a whole request: its messages, system prompt and tool definitions. */
|
|
257
|
-
export function estimateRequestTokens(messages, options) {
|
|
258
|
-
return estimateMessagesTokens(messages) + textTokens(options.system ?? "") + toolsTokens(options.tools);
|
|
259
|
-
}
|
|
260
|
-
// ── estimation ──────────────────────────────────────────────────────────────
|
|
261
|
-
const estimates = new WeakMap();
|
|
262
|
-
function messageTokens(message) {
|
|
263
|
-
const known = estimates.get(message);
|
|
264
|
-
if (known !== undefined)
|
|
265
|
-
return known;
|
|
266
|
-
const content = message.content;
|
|
267
|
-
const size = MESSAGE_OVERHEAD_TOKENS +
|
|
268
|
-
(typeof content === "string"
|
|
269
|
-
? textTokens(content)
|
|
270
|
-
: Array.isArray(content)
|
|
271
|
-
? sum(content.map(partTokens))
|
|
272
|
-
: 0);
|
|
273
|
-
estimates.set(message, size);
|
|
274
|
-
return size;
|
|
275
|
-
}
|
|
276
|
-
function partTokens(part) {
|
|
277
|
-
if (isImage(part))
|
|
278
|
-
return IMAGE_TOKENS;
|
|
279
|
-
switch (part.type) {
|
|
280
|
-
case "text":
|
|
281
|
-
case "reasoning":
|
|
282
|
-
return textTokens(String(part.text ?? ""));
|
|
283
|
-
case "file":
|
|
284
|
-
return dataTokens(part.data);
|
|
285
|
-
case "tool-call":
|
|
286
|
-
return textTokens(String(part.toolName ?? "")) + textTokens(json(part.input));
|
|
287
|
-
case "tool-result":
|
|
288
|
-
return isToolResult(part) ? outputTokens(part.output) : 0;
|
|
289
|
-
default:
|
|
290
|
-
return textTokens(json(part));
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
function outputTokens(output) {
|
|
294
|
-
switch (output.type) {
|
|
295
|
-
case "text":
|
|
296
|
-
case "error-text":
|
|
297
|
-
return textTokens(String(output.value ?? ""));
|
|
298
|
-
case "content":
|
|
299
|
-
return Array.isArray(output.value)
|
|
300
|
-
? sum(output.value.map((item) => isImage(item) ? IMAGE_TOKENS : item.type === "text" ? textTokens(String(item.text ?? "")) : dataTokens(item.data)))
|
|
301
|
-
: 0;
|
|
302
|
-
default:
|
|
303
|
-
return textTokens(json(output.value));
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
function textTokens(text) {
|
|
307
|
-
return Math.ceil(text.length / CHARS_PER_TOKEN);
|
|
308
|
-
}
|
|
309
|
-
function dataTokens(data) {
|
|
310
|
-
if (typeof data === "string")
|
|
311
|
-
return textTokens(data);
|
|
312
|
-
if (data instanceof Uint8Array || data instanceof ArrayBuffer)
|
|
313
|
-
return Math.ceil(data.byteLength / CHARS_PER_TOKEN);
|
|
314
|
-
return 0;
|
|
315
|
-
}
|
|
316
|
-
function toolsTokens(tools) {
|
|
317
|
-
if (!tools)
|
|
318
|
-
return 0;
|
|
319
|
-
return sum(Object.entries(tools).map(([name, tool]) => {
|
|
320
|
-
const description = tool?.description;
|
|
321
|
-
return (TOOL_DEFINITION_OVERHEAD_TOKENS + textTokens(name) + textTokens(typeof description === "string" ? description : ""));
|
|
322
|
-
}));
|
|
323
|
-
}
|
|
324
|
-
/** JSON text, with binary left out: its size is not text the model reads. */
|
|
325
|
-
function json(value) {
|
|
326
|
-
try {
|
|
327
|
-
return (JSON.stringify(value, (_key, v) => (v instanceof Uint8Array || v instanceof ArrayBuffer ? "" : v)) ?? "");
|
|
328
|
-
}
|
|
329
|
-
catch {
|
|
330
|
-
return "";
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
// ── shapes ──────────────────────────────────────────────────────────────────
|
|
334
|
-
function isImage(part) {
|
|
335
|
-
if (part.type === "image" || part.type === "image-data" || part.type === "image-url")
|
|
336
|
-
return true;
|
|
337
|
-
const mediaType = part.mediaType;
|
|
338
|
-
return ((part.type === "file" || part.type === "media" || part.type === "file-data" || part.type === "file-url") &&
|
|
339
|
-
typeof mediaType === "string" &&
|
|
340
|
-
mediaType.startsWith("image/"));
|
|
341
|
-
}
|
|
342
|
-
function isToolResult(part) {
|
|
343
|
-
return (part.type === "tool-result" &&
|
|
344
|
-
typeof part.output === "object" &&
|
|
345
|
-
part.output !== null &&
|
|
346
|
-
typeof part.output.type === "string");
|
|
347
|
-
}
|
|
348
|
-
function sum(values) {
|
|
349
|
-
let total = 0;
|
|
350
|
-
for (const value of values)
|
|
351
|
-
total += value;
|
|
352
|
-
return total;
|
|
353
|
-
}
|
|
354
|
-
function lastIndex(items, test) {
|
|
355
|
-
for (let i = items.length - 1; i >= 0; i -= 1) {
|
|
356
|
-
const item = items[i];
|
|
357
|
-
if (item !== undefined && test(item))
|
|
358
|
-
return i;
|
|
359
|
-
}
|
|
360
|
-
return -1;
|
|
361
|
-
}
|
|
362
|
-
function omittedImage() {
|
|
363
|
-
return { type: "text", text: OMITTED_HISTORY_IMAGE_TEXT };
|
|
364
|
-
}
|