@sixb/agent 0.1.0 → 0.2.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 (102) hide show
  1. package/dist/agent-prompt.d.ts +34 -28
  2. package/dist/agent-prompt.d.ts.map +1 -1
  3. package/dist/artifacts/publish-tool.d.ts +1 -1
  4. package/dist/attachments.d.ts +42 -1
  5. package/dist/attachments.d.ts.map +1 -1
  6. package/dist/child-files.d.ts +0 -5
  7. package/dist/child-files.d.ts.map +1 -1
  8. package/dist/computer-runner.d.ts +16 -7
  9. package/dist/computer-runner.d.ts.map +1 -1
  10. package/dist/context-budget.d.ts +23 -17
  11. package/dist/context-budget.d.ts.map +1 -1
  12. package/dist/context-compaction.d.ts +118 -18
  13. package/dist/context-compaction.d.ts.map +1 -1
  14. package/dist/context-guard.d.ts +69 -0
  15. package/dist/context-guard.d.ts.map +1 -0
  16. package/dist/conversation-messages.d.ts +33 -0
  17. package/dist/conversation-messages.d.ts.map +1 -0
  18. package/dist/execution-plan.d.ts +5 -1
  19. package/dist/execution-plan.d.ts.map +1 -1
  20. package/dist/failure.d.ts.map +1 -1
  21. package/dist/index.js +4916 -2697
  22. package/dist/index.js.map +41 -34
  23. package/dist/inline-images.d.ts +28 -0
  24. package/dist/inline-images.d.ts.map +1 -0
  25. package/dist/message-authors.d.ts +4 -4
  26. package/dist/message-authors.d.ts.map +1 -1
  27. package/dist/message-times.d.ts +4 -3
  28. package/dist/message-times.d.ts.map +1 -1
  29. package/dist/model-adapters.d.ts +6 -2
  30. package/dist/model-adapters.d.ts.map +1 -1
  31. package/dist/prompt-cache.d.ts +28 -0
  32. package/dist/prompt-cache.d.ts.map +1 -0
  33. package/dist/run-agent-turn.d.ts +33 -3
  34. package/dist/run-agent-turn.d.ts.map +1 -1
  35. package/dist/run-child.d.ts.map +1 -1
  36. package/dist/run-conversation-turn.d.ts.map +1 -1
  37. package/dist/run-environment.d.ts +35 -5
  38. package/dist/run-environment.d.ts.map +1 -1
  39. package/dist/run-files-lock.d.ts +16 -0
  40. package/dist/run-files-lock.d.ts.map +1 -0
  41. package/dist/run-subagent.d.ts +15 -1
  42. package/dist/run-subagent.d.ts.map +1 -1
  43. package/dist/run-workflow-agent-node.d.ts +5 -0
  44. package/dist/run-workflow-agent-node.d.ts.map +1 -1
  45. package/dist/sandbox-api-context.d.ts.map +1 -1
  46. package/dist/subagent-context.d.ts +50 -0
  47. package/dist/subagent-context.d.ts.map +1 -0
  48. package/dist/subagent-continuation.d.ts +10 -4
  49. package/dist/subagent-continuation.d.ts.map +1 -1
  50. package/dist/subagent-tools.d.ts +11 -0
  51. package/dist/subagent-tools.d.ts.map +1 -1
  52. package/dist/thread-context.d.ts +7 -2
  53. package/dist/thread-context.d.ts.map +1 -1
  54. package/dist/tools/bash.d.ts +8 -0
  55. package/dist/tools/bash.d.ts.map +1 -1
  56. package/dist/tools/model-spec.d.ts +7 -2
  57. package/dist/tools/model-spec.d.ts.map +1 -1
  58. package/dist/tools/result-media.d.ts +53 -10
  59. package/dist/tools/result-media.d.ts.map +1 -1
  60. package/dist/tools/result-output.d.ts +1 -1
  61. package/dist/tools/result-output.d.ts.map +1 -1
  62. package/dist/tools/tool-outputs.d.ts +63 -0
  63. package/dist/tools/tool-outputs.d.ts.map +1 -0
  64. package/dist/types.d.ts +9 -2
  65. package/dist/types.d.ts.map +1 -1
  66. package/package.json +4 -4
  67. package/src/agent-prompt.ts +178 -76
  68. package/src/attachments.ts +275 -84
  69. package/src/child-files.ts +1 -6
  70. package/src/computer-runner.ts +75 -23
  71. package/src/context-budget.ts +110 -94
  72. package/src/context-compaction.ts +1007 -187
  73. package/src/context-guard.ts +826 -0
  74. package/src/conversation-messages.ts +86 -0
  75. package/src/execution-plan.ts +22 -2
  76. package/src/failure.ts +11 -0
  77. package/src/inline-images.ts +69 -0
  78. package/src/message-authors.ts +5 -5
  79. package/src/message-times.ts +8 -5
  80. package/src/model-adapters.ts +59 -13
  81. package/src/prompt-cache.ts +40 -0
  82. package/src/run-agent-turn.ts +234 -72
  83. package/src/run-child.ts +30 -5
  84. package/src/run-conversation-turn.ts +16 -1
  85. package/src/run-environment.ts +553 -106
  86. package/src/run-files-lock.ts +118 -0
  87. package/src/run-subagent.ts +132 -46
  88. package/src/run-workflow-agent-node.ts +84 -10
  89. package/src/sandbox-api-context.ts +3 -0
  90. package/src/skills/sixb-artifacts/canvas.md +1 -1
  91. package/src/skills/sixb-artifacts/page.md +1 -1
  92. package/src/skills/sixb-artifacts/slides.md +1 -1
  93. package/src/subagent-context.ts +182 -0
  94. package/src/subagent-continuation.ts +11 -6
  95. package/src/subagent-tools.ts +73 -11
  96. package/src/thread-context.ts +9 -3
  97. package/src/tools/bash.ts +17 -1
  98. package/src/tools/model-spec.ts +9 -4
  99. package/src/tools/result-media.ts +286 -82
  100. package/src/tools/result-output.ts +4 -2
  101. package/src/tools/tool-outputs.ts +464 -0
  102. package/src/types.ts +14 -2
@@ -15,7 +15,8 @@ import { ChildRunner } from "./run-child"
15
15
  import { runConversationTurn } from "./run-conversation-turn"
16
16
  import { runApiCredential, runnerTokenFor, runTokenFor } from "./run-credential"
17
17
  import type { OpenRunSandbox } from "./run-environment"
18
- import type { RunSandbox } from "./run-sandbox"
18
+ import { holdRunFiles } from "./run-files-lock"
19
+ import { type RunSandbox, SANDBOX_CONTEXT_DIRECTORY } from "./run-sandbox"
19
20
  import type { AgentRunSettings } from "./types"
20
21
 
21
22
  // A turn on its owner's computer: claimed over HTTP as them, run in their folder, its children
@@ -38,13 +39,17 @@ export interface ComputerRunnerOptions {
38
39
  /** The user's own credential, as an `Authorization` header value, read for each claim. */
39
40
  authorization(): Promise<string>
40
41
  /**
41
- * This computer's id, which each claim names: a thread runs on one of its owner's computers, and
42
- * only that one claims its turns.
42
+ * This computer, as each claim names it with the folder: a thread runs on one of its owner's
43
+ * computers, in one folder there, and only `sixb` open there claims its turns.
43
44
  */
44
- computerId(): Promise<string>
45
- /** The folder the user's turns work in. */
45
+ computer(): Promise<{ readonly id: string; readonly name: string }>
46
+ /** The folder the user's turns work in: its real path. */
46
47
  readonly folder: string
47
- /** Where each run's own files go, a directory per run, removed when the run ends. */
48
+ /**
49
+ * Where each run's own files go: a directory per thread, so every turn's prompt names the same
50
+ * paths, emptied when a run starts and removed when it ends. Shared by every runner on the
51
+ * computer, which hold a thread's directory one run at a time.
52
+ */
48
53
  readonly runFilesRoot: string
49
54
  readonly openSandbox: OpenComputerSandbox
50
55
  /** How many children of one turn run at once. Defaults to 4. */
@@ -71,20 +76,25 @@ export class ComputerRunner {
71
76
  * ended already, or another runner holds it. `signal` stops the runner: the turn ends
72
77
  * interrupted, its end recorded.
73
78
  */
74
- async runTurn(runId: string, signal: AbortSignal): Promise<boolean> {
79
+ async runTurn(
80
+ runId: string,
81
+ signal: AbortSignal,
82
+ onClaimed?: (run: ConversationAgentRunRecord) => void
83
+ ): Promise<boolean> {
75
84
  const { apiUrl } = this.options
76
85
  const fetch = this.options.fetch
77
86
  const session = await claimAgentRunOverHttp({
78
87
  apiUrl,
79
88
  runId,
80
89
  authorization: await this.options.authorization(),
81
- computerId: await this.options.computerId(),
90
+ computer: { ...(await this.options.computer()), folder: this.options.folder },
82
91
  leaseExpiresAt: lease(),
83
92
  ...(fetch === undefined ? {} : { fetch }),
84
93
  })
85
94
  if (!session) return false
86
95
 
87
96
  const { run } = session
97
+ onClaimed?.(run)
88
98
  const turn: HttpAgentRunSessionInput<ConversationAgentRunRecord> = {
89
99
  apiUrl,
90
100
  runnerToken: runnerTokenFor({
@@ -112,6 +122,7 @@ export class ComputerRunner {
112
122
  openSandbox: () =>
113
123
  this.sandbox({
114
124
  runId: run.id,
125
+ threadId: run.threadId,
115
126
  executionToken: session.executionToken,
116
127
  projectId: run.projectId,
117
128
  folder: this.options.folder,
@@ -212,6 +223,7 @@ export class ComputerRunner {
212
223
  openSandbox: ({ session }) =>
213
224
  this.sandbox({
214
225
  runId: session.run.id,
226
+ threadId: session.run.threadId,
215
227
  executionToken: session.executionToken,
216
228
  projectId: session.run.projectId,
217
229
  }),
@@ -222,10 +234,12 @@ export class ComputerRunner {
222
234
 
223
235
  /**
224
236
  * A run's sandbox: the user's folder for a turn, which the prompt describes, or a blank folder
225
- * of its own for a child. Its own files go in a directory of their own, outside the folder.
237
+ * of its own for a child. Its own files go in its thread's directory, outside the folder: the
238
+ * prompt names them, so each run of the thread names the same paths.
226
239
  */
227
240
  private sandbox(input: {
228
241
  readonly runId: string
242
+ readonly threadId: string
229
243
  readonly executionToken: string
230
244
  readonly projectId: string
231
245
  readonly folder?: string
@@ -233,27 +247,52 @@ export class ComputerRunner {
233
247
  const { apiUrl } = this.options
234
248
  const credential = runApiCredential(apiUrl, runTokenFor(input))
235
249
  return async ({ signal }) => {
236
- await mkdir(join(this.options.runFilesRoot, input.runId), { recursive: true })
237
250
  // Real paths: the OS sandbox compares them, and the agent names files by them.
238
- const runFiles = await realpath(join(this.options.runFilesRoot, input.runId))
251
+ await mkdir(this.options.runFilesRoot, { recursive: true })
252
+ const runDirectory = join(await realpath(this.options.runFilesRoot), threadDirectory(input))
253
+ // Named as a cloud sandbox names them, so a path reads as the run's files wherever it ran.
254
+ const runFiles = join(runDirectory, SANDBOX_CONTEXT_DIRECTORY)
239
255
  const folder = input.folder === undefined ? undefined : await realpath(input.folder)
256
+ let held: (() => Promise<void>) | undefined
257
+
240
258
  const sandbox: RunSandbox = {
241
259
  waitBeforeModel: folder !== undefined,
242
260
  contextDirectory: runFiles,
243
- acquire: async () => ({
244
- sandbox: await this.options.openSandbox({
245
- ...(folder === undefined ? {} : { workingDirectory: folder }),
246
- runFiles,
247
- credential,
261
+ acquire: async () => {
262
+ const giveBack = await holdRunFiles({
263
+ directory: runDirectory,
264
+ runId: input.runId,
248
265
  signal,
249
- }),
250
- ...(folder === undefined
251
- ? {}
252
- : { promptContext: { workingDirectory: folder, retention: "computer" as const } }),
253
- }),
266
+ })
267
+ held = giveBack
268
+ try {
269
+ // Each run starts empty, as with a directory of its own: nothing an earlier run, or
270
+ // one that crashed, left there.
271
+ await rm(runDirectory, { recursive: true, force: true })
272
+ await mkdir(runFiles, { recursive: true })
273
+ return {
274
+ sandbox: await this.options.openSandbox({
275
+ ...(folder === undefined ? {} : { workingDirectory: folder }),
276
+ runFiles,
277
+ credential,
278
+ signal,
279
+ }),
280
+ ...(folder === undefined
281
+ ? {}
282
+ : { promptContext: { workingDirectory: folder, retention: "computer" as const } }),
283
+ }
284
+ } catch (error) {
285
+ await giveBack()
286
+ throw error
287
+ }
288
+ },
254
289
  release: async ({ sandbox }) => {
255
- await sandbox.destroy()
256
- await rm(runFiles, { recursive: true, force: true })
290
+ try {
291
+ await sandbox.destroy()
292
+ await rm(runDirectory, { recursive: true, force: true })
293
+ } finally {
294
+ await held?.()
295
+ }
257
296
  },
258
297
  }
259
298
  return { sandbox }
@@ -266,6 +305,19 @@ export class ComputerRunner {
266
305
  }
267
306
  }
268
307
 
308
+ /**
309
+ * A thread's files directory, by its id: one thread runs one run at a time, and its children have
310
+ * threads of their own.
311
+ */
312
+ function threadDirectory(input: { readonly runId: string; readonly threadId: string }): string {
313
+ if (!/^[A-Za-z0-9_-]+$/.test(input.threadId)) {
314
+ throw new Error(
315
+ `[SixbAgent] Run '${input.runId}' names thread '${input.threadId}', which cannot name a directory.`
316
+ )
317
+ }
318
+ return input.threadId
319
+ }
320
+
269
321
  function lease(): string {
270
322
  return new Date(Date.now() + LEASE_MS).toISOString()
271
323
  }
@@ -1,27 +1,26 @@
1
1
  import { AgentDefinitionError } from "@sixb/core"
2
+ import {
3
+ AGENT_TOKEN_RATE_SAMPLES,
4
+ type AgentContextBudget,
5
+ type AgentContextTokenRateSample,
6
+ type AgentModelSelection,
7
+ agentContextSizeTokens,
8
+ agentOutputCapTokens,
9
+ calibrateAgentContextBytesPerToken,
10
+ contextSummaryOutputTokens,
11
+ DEFAULT_AGENT_CONTEXT_WINDOW_TOKENS,
12
+ learnedAgentContextBytesPerToken,
13
+ measureAgentContextTokenRate,
14
+ measureAgentModelRequest,
15
+ resolveAgentContextBudget,
16
+ } from "@sixb/core/internal/agents"
2
17
  import {
3
18
  defineLanguageModel,
4
19
  type LanguageModel,
5
- type LanguageModelDefinition,
6
20
  type LanguageModelRequest,
7
- type ModelReasoning,
21
+ type LanguageModelStreamEvent,
8
22
  } from "@sixb/core/models"
9
23
 
10
- const DEFAULT_AGENT_CONTEXT_WINDOW_TOKENS = 128_000
11
-
12
- export interface AgentContextBudget {
13
- readonly windowTokens: number
14
- readonly inputBudgetTokens: number
15
- readonly reserveTokens: number
16
- readonly keepRecentTokens: number
17
- readonly source: "model" | "fallback"
18
- }
19
-
20
- interface AgentModelSelection {
21
- readonly model: LanguageModel
22
- readonly reasoning?: ModelReasoning
23
- }
24
-
25
24
  /**
26
25
  * Derive the model's input and output budgets from its definition, and cap each call's output to
27
26
  * them. The model comes pinned: whoever hands it over resolved it once for the run.
@@ -29,6 +28,12 @@ interface AgentModelSelection {
29
28
  export function prepareAgentModel(selection: AgentModelSelection): {
30
29
  readonly model: LanguageModel
31
30
  readonly budget: AgentContextBudget
31
+ /**
32
+ * The bytes per token the run's latest counted call measured, bounded around the run's rate (the
33
+ * project's measurements and its own calls'), as calibration and the output ceiling read it.
34
+ * Undefined until a call is counted.
35
+ */
36
+ readonly measuredBytesPerToken: () => number | undefined
32
37
  } {
33
38
  const model = selection.model
34
39
  const definition = defineLanguageModel(model.definition)
@@ -38,15 +43,27 @@ export function prepareAgentModel(selection: AgentModelSelection): {
38
43
  `[SixbAgent] No context limit is available for '${model.providerId}/${model.modelId}'; using the ${DEFAULT_AGENT_CONTEXT_WINDOW_TOKENS.toLocaleString("en-US")}-token fallback.`
39
44
  )
40
45
  }
41
- const maxOutputTokens = outputCapTokens(selection.model, definition) ?? budget.reserveTokens
46
+ const maxOutputTokens = agentOutputCapTokens(selection.model, definition) ?? budget.reserveTokens
42
47
  if (
43
48
  typeof selection.reasoning === "object" &&
44
49
  selection.reasoning.budgetTokens >= maxOutputTokens
45
50
  ) {
46
- throw invalidBudget("output allowance must exceed the reasoning token budget.")
51
+ throw new AgentDefinitionError(
52
+ "[SixbAgent] Agent context output allowance must exceed the reasoning token budget."
53
+ )
47
54
  }
55
+
56
+ // The run's rate, from the project's measurements and, newest first, its own calls'.
57
+ const tokenRates = selection.tokenRates ?? []
58
+ const runTokenRates: AgentContextTokenRateSample[] = []
59
+ let runBytesPerToken = budget.bytesPerToken
60
+ // The rate the run's latest counted call measured, bounded around the run's; else the run's.
61
+ let counted = false
62
+ let measuredBytesPerToken = budget.bytesPerToken
63
+
48
64
  return {
49
65
  budget,
66
+ measuredBytesPerToken: () => (counted ? measuredBytesPerToken : undefined),
50
67
  model: Object.freeze({
51
68
  providerId: model.providerId,
52
69
  modelId: model.modelId,
@@ -55,107 +72,106 @@ export function prepareAgentModel(selection: AgentModelSelection): {
55
72
  ...(model.validateResponseFormat === undefined
56
73
  ? {}
57
74
  : { validateResponseFormat: model.validateResponseFormat.bind(model) }),
58
- stream: (request: LanguageModelRequest) =>
59
- model.stream({
75
+ stream: async (request: LanguageModelRequest) => {
76
+ const size = measureAgentModelRequest(request)
77
+ const stream = await model.stream({
60
78
  ...request,
61
79
  maxOutputTokens: Math.min(
62
80
  request.maxOutputTokens ?? maxOutputTokens,
63
- outputCeilingTokens(request, budget, maxOutputTokens)
81
+ outputCeilingTokens(
82
+ agentContextSizeTokens(size, Math.min(runBytesPerToken, measuredBytesPerToken)),
83
+ budget,
84
+ maxOutputTokens
85
+ )
64
86
  ),
65
- }),
87
+ })
88
+
89
+ return {
90
+ events: withCountedInput(stream.events, (inputTokens) => {
91
+ const sample = measureAgentContextTokenRate({ size, inputTokens })
92
+ if (sample) {
93
+ // The newest 20 are all the statistic reads.
94
+ runTokenRates.unshift(sample)
95
+ runTokenRates.splice(AGENT_TOKEN_RATE_SAMPLES)
96
+ runBytesPerToken = learnedAgentContextBytesPerToken([...runTokenRates, ...tokenRates])
97
+ }
98
+
99
+ measuredBytesPerToken = calibrateAgentContextBytesPerToken({
100
+ bytesPerToken: runBytesPerToken,
101
+ size,
102
+ inputTokens,
103
+ })
104
+ counted = true
105
+ }),
106
+ }
107
+ },
66
108
  }),
67
109
  }
68
110
  }
69
111
 
70
112
  /**
71
- * A call may write whatever its input leaves of the window, up to the cap. Compaction keeps the
72
- * input within its budget, so the reserve is always left, and it is all a call gets when its input
73
- * cannot be estimated.
113
+ * A call may write whatever its input leaves of the window, up to the cap, and the reserve at
114
+ * least. The input is weighed at the denser of the run's rate and the one its latest call
115
+ * measured, so it errs high whichever way this request's content tokenizes: a call right after a
116
+ * large tool result can read denser than the calls it was calibrated on, and with no turn to ask
117
+ * it again, an allowance past the window's room is a refused call.
118
+ *
119
+ * Below the reserve, only the guard decides (see createAgentContextGuard): it weighs the request
120
+ * from what the provider counted, and caps the request it lets past the limit at its own room.
74
121
  */
75
122
  function outputCeilingTokens(
76
- request: LanguageModelRequest,
123
+ inputTokens: number,
77
124
  budget: AgentContextBudget,
78
125
  capTokens: number
79
126
  ): number {
80
- const inputTokens = estimateRequestInputTokens(request)
81
- if (inputTokens === undefined) return budget.reserveTokens
82
- return Math.min(capTokens, Math.max(budget.reserveTokens, budget.windowTokens - inputTokens))
127
+ return Math.max(
128
+ Math.min(capTokens, budget.reserveTokens),
129
+ agentOutputRoomTokens(inputTokens, budget, capTokens)
130
+ )
83
131
  }
84
132
 
85
133
  /**
86
- * Serialized bytes at two per token. Gateway tokenizers measured from 2.8 (Hindi on GLM) to 12
87
- * bytes per token, and an underestimate is the costly side: whenever the window bounds the ceiling,
88
- * any input it misses overflows providers that reject input plus output beyond the window. Attached
89
- * files have no byte-to-token rate.
134
+ * What a call may write for what its input weighs: the room it leaves of the window, up to
135
+ * `capTokens`. The one measure the output ceiling and the guard size a call's answer by.
90
136
  */
91
- function estimateRequestInputTokens(request: LanguageModelRequest): number | undefined {
92
- const hasFile = request.messages.some(
93
- (message) => message.role === "user" && message.content.some((part) => part.type === "file")
94
- )
95
- if (hasFile) return undefined
96
- const serialized = JSON.stringify([request.messages, request.tools, request.responseFormat])
97
- return Math.ceil(new TextEncoder().encode(serialized).byteLength / 2)
137
+ export function agentOutputRoomTokens(
138
+ inputTokens: number,
139
+ budget: AgentContextBudget,
140
+ capTokens: number
141
+ ): number {
142
+ return Math.min(capTokens, budget.windowTokens - inputTokens)
98
143
  }
99
144
 
100
- /** The most one call may write: the binding's limit, capped at the model maximum. */
101
- function outputCapTokens(
102
- binding: LanguageModel,
103
- definition: LanguageModelDefinition
104
- ): number | undefined {
105
- if (binding.maxOutputTokens === undefined) return definition.maxOutputTokens
106
- return Math.min(binding.maxOutputTokens, definition.maxOutputTokens ?? binding.maxOutputTokens)
145
+ /**
146
+ * The least room a request past its model's limit is still sent with: what a compaction summary
147
+ * may write (see contextSummaryOutputTokens), the answer the framework already sizes as enough to
148
+ * carry a conversation on. With less, the turn ends with the context error rather than send a
149
+ * call whose answer would be cut off almost at once.
150
+ */
151
+ export function agentMinimumAnswerTokens(budget: AgentContextBudget): number {
152
+ return contextSummaryOutputTokens(budget.reserveTokens)
107
153
  }
108
154
 
109
155
  /**
110
- * Input-only limits are a conservative window: leave the same reserve without inventing a total.
111
- * The reserve is the output every call is guaranteed, not its ceiling, so it stays small and a
112
- * larger output limit never shrinks the input budget.
156
+ * A call's request as the conversation estimator weighs it (see measureAgentModelRequest), at
157
+ * `bytesPerToken`.
113
158
  */
114
- export function resolveAgentContextBudget(
115
- selection: AgentModelSelection,
116
- definition: LanguageModelDefinition = selection.model.definition
117
- ): AgentContextBudget {
118
- const modelWindow = definition.contextWindow ?? definition.maxInputTokens
119
- const windowTokens = modelWindow ?? DEFAULT_AGENT_CONTEXT_WINDOW_TOKENS
120
- const reserveTokens = Math.max(
121
- Math.min(
122
- 16_384,
123
- Math.floor(windowTokens * 0.25),
124
- outputCapTokens(selection.model, definition) ?? Number.POSITIVE_INFINITY
125
- ),
126
- typeof selection.reasoning === "object" ? selection.reasoning.budgetTokens + 1 : 0
127
- )
128
- assertPositiveSafeInteger(windowTokens, "windowTokens")
129
- assertPositiveSafeInteger(reserveTokens, "reserveTokens")
130
- if (reserveTokens >= windowTokens) {
131
- throw invalidBudget("reserveTokens must be less than the resolved context window.")
132
- }
133
- const inputBudgetTokens = Math.min(
134
- definition.maxInputTokens ?? Number.POSITIVE_INFINITY,
135
- windowTokens - reserveTokens
136
- )
137
- assertPositiveSafeInteger(inputBudgetTokens, "inputBudgetTokens")
138
- const keepRecentTokens = Math.min(20_000, Math.floor(inputBudgetTokens * 0.5))
139
- assertPositiveSafeInteger(keepRecentTokens, "keepRecentTokens")
140
- if (keepRecentTokens >= inputBudgetTokens) {
141
- throw invalidBudget("keepRecentTokens must be less than the resolved input budget.")
142
- }
143
-
144
- return Object.freeze({
145
- windowTokens,
146
- inputBudgetTokens,
147
- reserveTokens,
148
- keepRecentTokens,
149
- source: modelWindow === undefined ? "fallback" : "model",
150
- })
159
+ export function estimateAgentRequestTokens(
160
+ request: Pick<LanguageModelRequest, "messages" | "tools" | "responseFormat">,
161
+ bytesPerToken: number
162
+ ): number {
163
+ return agentContextSizeTokens(measureAgentModelRequest(request), bytesPerToken)
151
164
  }
152
165
 
153
- function invalidBudget(message: string): AgentDefinitionError {
154
- return new AgentDefinitionError(`[SixbAgent] Agent context ${message}`)
155
- }
166
+ /** The events as they come, handing over the input tokens the provider counted once it says. */
167
+ async function* withCountedInput(
168
+ events: AsyncIterable<LanguageModelStreamEvent>,
169
+ counted: (inputTokens: number) => void
170
+ ): AsyncIterable<LanguageModelStreamEvent> {
171
+ for await (const event of events) {
172
+ const inputTokens = event.type === "finish" ? event.usage.inputTokens : undefined
173
+ if (inputTokens !== undefined && inputTokens > 0) counted(inputTokens)
156
174
 
157
- function assertPositiveSafeInteger(value: number, field: string): void {
158
- if (!Number.isSafeInteger(value) || value <= 0) {
159
- throw invalidBudget(`${field} must be a positive safe integer.`)
175
+ yield event
160
176
  }
161
177
  }