@womp/spark-agent 6.5.0 → 8.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.
Files changed (40) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/README.md +7 -4
  3. package/dist/agent.d.ts +3 -6
  4. package/dist/agent.js +41 -23
  5. package/dist/capabilities/geometry.d.ts +17 -5
  6. package/dist/capabilities/geometry.js +11 -12
  7. package/dist/compaction.d.ts +41 -8
  8. package/dist/compaction.js +241 -77
  9. package/dist/context-window.d.ts +23 -0
  10. package/dist/context-window.js +141 -0
  11. package/dist/generated/geometry-declarations.d.ts +1 -1
  12. package/dist/generated/geometry-declarations.js +1 -1
  13. package/dist/geometry/backendRegistry.d.ts +1 -0
  14. package/dist/geometry/common.d.ts +5 -2
  15. package/dist/geometry/common.js +18 -3
  16. package/dist/geometry/compiler.js +2 -2
  17. package/dist/geometry/diagnostics.d.ts +2 -7
  18. package/dist/geometry/diagnostics.js +3 -51
  19. package/dist/geometry/graph.d.ts +1 -1
  20. package/dist/geometry/graph.js +1 -0
  21. package/dist/geometry/index.d.ts +0 -1
  22. package/dist/geometry/index.js +0 -3
  23. package/dist/geometry/lift.js +105 -113
  24. package/dist/geometry/lower.js +29 -49
  25. package/dist/geometry/schemas/flowir-dialect.json +117 -117
  26. package/dist/geometry/writeParamData.js +4 -4
  27. package/dist/geometry.d.ts +2 -5
  28. package/dist/geometry.js +7 -11
  29. package/dist/index.d.ts +2 -2
  30. package/dist/index.js +2 -1
  31. package/dist/intent-bench.js +1 -1
  32. package/dist/sdk-geometry-host.d.ts +1 -1
  33. package/dist/sdk-geometry-host.js +253 -251
  34. package/dist/ui.d.ts +7 -0
  35. package/dist/ui.js +1 -0
  36. package/dist/womp-scene-api.d.ts +9 -1
  37. package/dist/womp-scene-api.js +41 -32
  38. package/package.json +2 -2
  39. package/dist/context-budget.d.ts +0 -65
  40. package/dist/context-budget.js +0 -364
@@ -133,7 +133,7 @@ function normalizePrimitiveSize(primitive, size) {
133
133
  function scale(value, amount) {
134
134
  return { x: value.x * amount, y: value.y * amount, z: value.z * amount };
135
135
  }
136
- function convertAABB(box, unitMultiplier) {
136
+ export function convertAABB(box, unitMultiplier) {
137
137
  return {
138
138
  min: {
139
139
  x: fromEnginePosition({ x: 0, y: 0, z: box.max.z }, unitMultiplier).x,
@@ -147,6 +147,41 @@ function convertAABB(box, unitMultiplier) {
147
147
  },
148
148
  };
149
149
  }
150
+ function sceneAncestorScale(reader, rootId, id, includeSelf = false) {
151
+ let total = 1;
152
+ let current = includeSelf
153
+ ? reader.requireNode(id)
154
+ : reader.getNode(reader.requireNode(id).parentId ?? rootId);
155
+ const visited = new Set();
156
+ while (current.id !== rootId && !visited.has(current.id)) {
157
+ visited.add(current.id);
158
+ if (current.kind === "union")
159
+ total *= current.transform.scale.x;
160
+ const parentId = current.parentId;
161
+ if (parentId === null || parentId === rootId)
162
+ break;
163
+ current = reader.getNode(parentId);
164
+ }
165
+ return total;
166
+ }
167
+ export function sceneNodePosition(reader, rootId, unitMultiplier, id) {
168
+ return fromEnginePosition(scale(reader.requireNode(id).transform.position, sceneAncestorScale(reader, rootId, id)), unitMultiplier);
169
+ }
170
+ export function sceneNodeRotation(reader, id) {
171
+ return convertRotationBasis(reader.requireNode(id).transform.rotation);
172
+ }
173
+ export function sceneNodeSize(reader, rootId, unitMultiplier, id) {
174
+ const node = reader.requireNode(id);
175
+ const parentScale = sceneAncestorScale(reader, rootId, id);
176
+ if (node.kind === "group")
177
+ return { x: 1, y: 1, z: 1 };
178
+ if (node.kind === "union") {
179
+ const size = node.transform.scale.x * parentScale;
180
+ return { x: size, y: size, z: size };
181
+ }
182
+ const size = fromEngineDimensions(scale(node.transform.scale, parentScale), unitMultiplier);
183
+ return node.kind === "primitive" ? normalizePrimitiveSize(node.primitive, size) : size;
184
+ }
150
185
  /** Align local +Z toward target. Both points are in the same parent-local coordinates. */
151
186
  function lookAt(origin, target) {
152
187
  if (![origin.x, origin.y, origin.z, target.x, target.y, target.z].every(Number.isFinite)) {
@@ -209,25 +244,10 @@ export function createWompSceneApi(context) {
209
244
  throw new Error("Curve V1 and V2 transforms are read-only");
210
245
  return node;
211
246
  };
212
- const ancestorScale = (id, includeSelf = false) => {
213
- let total = 1;
214
- let current = includeSelf
215
- ? requireNode(id)
216
- : scene.getNode(requireNode(id).parentId ?? context.rootId);
217
- const visited = new Set();
218
- while (current.id !== context.rootId && !visited.has(current.id)) {
219
- visited.add(current.id);
220
- if (current.kind === "union")
221
- total *= current.transform.scale.x;
222
- const parentId = current.parentId;
223
- if (parentId === null || parentId === context.rootId)
224
- break;
225
- current = scene.getNode(parentId);
226
- }
227
- return total;
228
- };
229
- const nodePosition = (id) => fromEnginePosition(scale(requireNode(id).transform.position, ancestorScale(id)), unitMultiplier);
230
- const nodeRotation = (id) => convertRotationBasis(requireNode(id).transform.rotation);
247
+ const reader = { requireNode, getNode: (id) => scene.getNode(id) };
248
+ const ancestorScale = (id, includeSelf = false) => sceneAncestorScale(reader, context.rootId, id, includeSelf);
249
+ const nodePosition = (id) => sceneNodePosition(reader, context.rootId, unitMultiplier, id);
250
+ const nodeRotation = (id) => sceneNodeRotation(reader, id);
231
251
  const hasCsgBaseBefore = (parentId, nodeId, index) => {
232
252
  const siblings = scene.getNodeChildren(parentId).filter(({ id }) => id !== nodeId);
233
253
  const targetIndex = index ?? siblings.length;
@@ -237,18 +257,7 @@ export function createWompSceneApi(context) {
237
257
  sibling.kind === "group" ||
238
258
  ("operation" in sibling && sibling.operation === "union"));
239
259
  };
240
- const nodeSize = (id) => {
241
- const node = requireNode(id);
242
- const parentScale = ancestorScale(id);
243
- if (node.kind === "group")
244
- return { x: 1, y: 1, z: 1 };
245
- if (node.kind === "union") {
246
- const size = node.transform.scale.x * parentScale;
247
- return { x: size, y: size, z: size };
248
- }
249
- const size = fromEngineDimensions(scale(node.transform.scale, parentScale), unitMultiplier);
250
- return node.kind === "primitive" ? normalizePrimitiveSize(node.primitive, size) : size;
251
- };
260
+ const nodeSize = (id) => sceneNodeSize(reader, context.rootId, unitMultiplier, id);
252
261
  const update = (id, changes) => {
253
262
  context.call("updateNode", [id, changes], () => scene.updateNode(id, changes));
254
263
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@womp/spark-agent",
3
- "version": "6.5.0",
3
+ "version": "8.0.0",
4
4
  "description": "Portable skills-based AI agent with local and durable chat transports for Womp",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -113,7 +113,7 @@
113
113
  "@ai-sdk/google": "4.0.69",
114
114
  "@ai-sdk/openai": "4.0.63",
115
115
  "@jitl/quickjs-wasmfile-release-sync": "0.32.0",
116
- "@womp/flowir": "0.3.0",
116
+ "@womp/flowir": "0.5.0",
117
117
  "@womp/kakapo-sdk": "0.8.0",
118
118
  "ai": "7.0.95",
119
119
  "eventsource-parser": "^3.0.6",
@@ -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;
@@ -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
- }