@anthropic-ai/sdk 0.126.0 → 0.127.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 (166) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/client.d.mts +10 -9
  3. package/client.d.mts.map +1 -1
  4. package/client.d.ts +10 -9
  5. package/client.d.ts.map +1 -1
  6. package/client.js +33 -19
  7. package/client.js.map +1 -1
  8. package/client.mjs +34 -20
  9. package/client.mjs.map +1 -1
  10. package/internal/parse.d.mts.map +1 -1
  11. package/internal/parse.d.ts.map +1 -1
  12. package/internal/parse.js +2 -2
  13. package/internal/parse.js.map +1 -1
  14. package/internal/parse.mjs +3 -3
  15. package/internal/parse.mjs.map +1 -1
  16. package/internal/shims.d.mts +3 -3
  17. package/internal/shims.d.mts.map +1 -1
  18. package/internal/shims.d.ts +3 -3
  19. package/internal/shims.d.ts.map +1 -1
  20. package/internal/shims.js.map +1 -1
  21. package/internal/shims.mjs.map +1 -1
  22. package/internal/utils/log.d.mts +7 -12
  23. package/internal/utils/log.d.mts.map +1 -1
  24. package/internal/utils/log.d.ts +7 -12
  25. package/internal/utils/log.d.ts.map +1 -1
  26. package/internal/utils/log.js +12 -0
  27. package/internal/utils/log.js.map +1 -1
  28. package/internal/utils/log.mjs +11 -0
  29. package/internal/utils/log.mjs.map +1 -1
  30. package/lib/BetaMessageStream.d.mts.map +1 -1
  31. package/lib/BetaMessageStream.d.ts.map +1 -1
  32. package/lib/BetaMessageStream.js +7 -5
  33. package/lib/BetaMessageStream.js.map +1 -1
  34. package/lib/BetaMessageStream.mjs +7 -5
  35. package/lib/BetaMessageStream.mjs.map +1 -1
  36. package/lib/beta-parser.d.mts +1 -3
  37. package/lib/beta-parser.d.mts.map +1 -1
  38. package/lib/beta-parser.d.ts +1 -3
  39. package/lib/beta-parser.d.ts.map +1 -1
  40. package/lib/beta-parser.js.map +1 -1
  41. package/lib/beta-parser.mjs.map +1 -1
  42. package/lib/internal/stub.d.mts +2 -0
  43. package/lib/internal/stub.d.mts.map +1 -0
  44. package/lib/internal/stub.d.ts +2 -0
  45. package/lib/internal/stub.d.ts.map +1 -0
  46. package/lib/internal/stub.js +6 -0
  47. package/lib/internal/stub.js.map +1 -0
  48. package/lib/internal/stub.mjs +3 -0
  49. package/lib/internal/stub.mjs.map +1 -0
  50. package/lib/internal/types.d.mts +5 -0
  51. package/lib/internal/types.d.mts.map +1 -0
  52. package/lib/internal/types.d.ts +5 -0
  53. package/lib/internal/types.d.ts.map +1 -0
  54. package/lib/internal/types.js +3 -0
  55. package/lib/internal/types.js.map +1 -0
  56. package/lib/internal/types.mjs +2 -0
  57. package/lib/internal/types.mjs.map +1 -0
  58. package/lib/middleware.js +6 -1
  59. package/lib/middleware.js.map +1 -1
  60. package/lib/middleware.mjs +6 -1
  61. package/lib/middleware.mjs.map +1 -1
  62. package/lib/parser.d.mts +1 -3
  63. package/lib/parser.d.mts.map +1 -1
  64. package/lib/parser.d.ts +1 -3
  65. package/lib/parser.d.ts.map +1 -1
  66. package/lib/parser.js.map +1 -1
  67. package/lib/parser.mjs.map +1 -1
  68. package/lib/tools/BetaToolRunner.d.mts +21 -6
  69. package/lib/tools/BetaToolRunner.d.mts.map +1 -1
  70. package/lib/tools/BetaToolRunner.d.ts +21 -6
  71. package/lib/tools/BetaToolRunner.d.ts.map +1 -1
  72. package/lib/tools/BetaToolRunner.js +125 -23
  73. package/lib/tools/BetaToolRunner.js.map +1 -1
  74. package/lib/tools/BetaToolRunner.mjs +125 -23
  75. package/lib/tools/BetaToolRunner.mjs.map +1 -1
  76. package/lib/transform-json-schema.js +7 -5
  77. package/lib/transform-json-schema.js.map +1 -1
  78. package/lib/transform-json-schema.mjs +7 -5
  79. package/lib/transform-json-schema.mjs.map +1 -1
  80. package/package.json +2 -1
  81. package/resources/beta/deployment-runs.d.mts +4 -0
  82. package/resources/beta/deployment-runs.d.mts.map +1 -1
  83. package/resources/beta/deployment-runs.d.ts +4 -0
  84. package/resources/beta/deployment-runs.d.ts.map +1 -1
  85. package/resources/beta/deployments.d.mts +5 -0
  86. package/resources/beta/deployments.d.mts.map +1 -1
  87. package/resources/beta/deployments.d.ts +5 -0
  88. package/resources/beta/deployments.d.ts.map +1 -1
  89. package/resources/beta/dreams.d.mts +17 -0
  90. package/resources/beta/dreams.d.mts.map +1 -1
  91. package/resources/beta/dreams.d.ts +17 -0
  92. package/resources/beta/dreams.d.ts.map +1 -1
  93. package/resources/beta/memory-stores/memories.d.mts +6 -0
  94. package/resources/beta/memory-stores/memories.d.mts.map +1 -1
  95. package/resources/beta/memory-stores/memories.d.ts +6 -0
  96. package/resources/beta/memory-stores/memories.d.ts.map +1 -1
  97. package/resources/beta/memory-stores/memory-versions.d.mts +7 -0
  98. package/resources/beta/memory-stores/memory-versions.d.mts.map +1 -1
  99. package/resources/beta/memory-stores/memory-versions.d.ts +7 -0
  100. package/resources/beta/memory-stores/memory-versions.d.ts.map +1 -1
  101. package/resources/beta/messages/messages.d.mts +1 -1
  102. package/resources/beta/messages/messages.d.ts +1 -1
  103. package/resources/beta/organization/index.d.mts +1 -1
  104. package/resources/beta/organization/index.d.mts.map +1 -1
  105. package/resources/beta/organization/index.d.ts +1 -1
  106. package/resources/beta/organization/index.d.ts.map +1 -1
  107. package/resources/beta/organization/index.js.map +1 -1
  108. package/resources/beta/organization/index.mjs.map +1 -1
  109. package/resources/beta/organization/organization.d.mts +2 -2
  110. package/resources/beta/organization/organization.d.mts.map +1 -1
  111. package/resources/beta/organization/organization.d.ts +2 -2
  112. package/resources/beta/organization/organization.d.ts.map +1 -1
  113. package/resources/beta/organization/organization.js.map +1 -1
  114. package/resources/beta/organization/organization.mjs.map +1 -1
  115. package/resources/beta/organization/rate-limits.d.mts +87 -5
  116. package/resources/beta/organization/rate-limits.d.mts.map +1 -1
  117. package/resources/beta/organization/rate-limits.d.ts +87 -5
  118. package/resources/beta/organization/rate-limits.d.ts.map +1 -1
  119. package/resources/beta/organization/workspaces/rate-limits.d.mts +9 -4
  120. package/resources/beta/organization/workspaces/rate-limits.d.mts.map +1 -1
  121. package/resources/beta/organization/workspaces/rate-limits.d.ts +9 -4
  122. package/resources/beta/organization/workspaces/rate-limits.d.ts.map +1 -1
  123. package/resources/beta/organization/workspaces/rate-limits.js.map +1 -1
  124. package/resources/beta/organization/workspaces/rate-limits.mjs.map +1 -1
  125. package/resources/beta/sessions/sessions.d.mts +6 -0
  126. package/resources/beta/sessions/sessions.d.mts.map +1 -1
  127. package/resources/beta/sessions/sessions.d.ts +6 -0
  128. package/resources/beta/sessions/sessions.d.ts.map +1 -1
  129. package/resources/beta/sessions/sessions.js.map +1 -1
  130. package/resources/beta/sessions/sessions.mjs.map +1 -1
  131. package/resources/beta/tunnels/certificates.d.mts +2 -2
  132. package/resources/beta/tunnels/certificates.d.ts +2 -2
  133. package/resources/beta/vaults/credentials.d.mts +19 -5
  134. package/resources/beta/vaults/credentials.d.mts.map +1 -1
  135. package/resources/beta/vaults/credentials.d.ts +19 -5
  136. package/resources/beta/vaults/credentials.d.ts.map +1 -1
  137. package/src/client.ts +82 -71
  138. package/src/internal/parse.ts +8 -11
  139. package/src/internal/shims.ts +4 -3
  140. package/src/internal/utils/log.ts +16 -2
  141. package/src/lib/BetaMessageStream.ts +7 -5
  142. package/src/lib/beta-parser.ts +1 -3
  143. package/src/lib/internal/stub.ts +2 -0
  144. package/src/lib/internal/types.ts +2 -0
  145. package/src/lib/middleware.ts +6 -1
  146. package/src/lib/parser.ts +1 -3
  147. package/src/lib/tools/BetaToolRunner.ts +157 -24
  148. package/src/lib/transform-json-schema.ts +8 -6
  149. package/src/resources/beta/deployment-runs.ts +4 -0
  150. package/src/resources/beta/deployments.ts +5 -0
  151. package/src/resources/beta/dreams.ts +17 -0
  152. package/src/resources/beta/memory-stores/memories.ts +6 -0
  153. package/src/resources/beta/memory-stores/memory-versions.ts +7 -0
  154. package/src/resources/beta/messages/messages.ts +1 -1
  155. package/src/resources/beta/organization/index.ts +6 -0
  156. package/src/resources/beta/organization/organization.ts +12 -0
  157. package/src/resources/beta/organization/rate-limits.ts +112 -4
  158. package/src/resources/beta/organization/workspaces/rate-limits.ts +16 -4
  159. package/src/resources/beta/sessions/sessions.ts +6 -0
  160. package/src/resources/beta/tunnels/certificates.ts +2 -2
  161. package/src/resources/beta/vaults/credentials.ts +19 -5
  162. package/src/version.ts +1 -1
  163. package/version.d.mts +1 -1
  164. package/version.d.ts +1 -1
  165. package/version.js +1 -1
  166. package/version.mjs +1 -1
@@ -3,6 +3,7 @@ import { ToolError } from './ToolError';
3
3
  import { Anthropic } from '../..';
4
4
  import { AnthropicError } from '../../core/error';
5
5
  import {
6
+ BetaCompactionConfig,
6
7
  BetaContentBlockParam,
7
8
  BetaMessage,
8
9
  BetaMessageParam,
@@ -20,12 +21,14 @@ import { RequestOptions } from '../../internal/request-options';
20
21
  import { buildHeaders } from '../../internal/headers';
21
22
  import { promiseWithResolvers } from '../../internal/utils/promise';
22
23
  import { checkNever } from '../../internal/utils/values';
24
+ import { loggerFor } from '../../internal/utils/log';
23
25
  import { CompactionControl, DEFAULT_SUMMARY_PROMPT, DEFAULT_TOKEN_THRESHOLD } from './CompactionControl';
24
26
  import {
25
27
  collectStainlessHelpers,
26
28
  helperHeader,
27
29
  STAINLESS_HELPER_HEADER,
28
30
  } from '../../internal/stainless-helper-header';
31
+ import type { Simplify } from '../internal/types';
29
32
 
30
33
  /**
31
34
  * A ToolRunner handles the automatic conversation loop between the assistant and tools.
@@ -43,6 +46,8 @@ export class BetaToolRunner<Stream extends boolean> {
43
46
  #options: BetaToolRunnerRequestOptions;
44
47
  /** Promise for the last message received from the assistant */
45
48
  #message?: Promise<BetaMessage> | undefined;
49
+ /** The stream of the request in progress, when streaming */
50
+ #stream?: BetaMessageStream | undefined;
46
51
  /** Cached tool response to avoid redundant executions */
47
52
  #toolResponse?: Promise<BetaMessageParam | null> | undefined;
48
53
  /** Promise resolvers for waiting on completion */
@@ -53,12 +58,17 @@ export class BetaToolRunner<Stream extends boolean> {
53
58
  };
54
59
  /** Number of iterations (API requests) made so far */
55
60
  #iterationCount = 0;
61
+ /** A compaction scheduled with `compactBeforeNextTurn()`, in flight until its response has been handled */
62
+ #compaction: Compaction = { status: 'idle' };
63
+ /** Whether the last turn was paused; a scheduled compaction waits for it to be resumed */
64
+ #turnPaused = false;
56
65
 
57
66
  constructor(
58
67
  private client: Anthropic,
59
68
  params: BetaToolRunnerParams,
60
69
  options?: BetaToolRunnerRequestOptions,
61
70
  ) {
71
+ rejectCompactionParam(params);
62
72
  this.#state = {
63
73
  params: {
64
74
  // You can't clone the entire params since there are functions as handlers.
@@ -190,7 +200,6 @@ export class BetaToolRunner<Stream extends boolean> {
190
200
 
191
201
  try {
192
202
  while (true) {
193
- let stream;
194
203
  try {
195
204
  if (
196
205
  this.#state.params.max_iterations &&
@@ -199,6 +208,12 @@ export class BetaToolRunner<Stream extends boolean> {
199
208
  break;
200
209
  }
201
210
 
211
+ // The API can't compact a conversation that ends mid-turn, so a paused turn is resumed first.
212
+ if (this.#compaction.status === 'scheduled' && !this.#turnPaused) {
213
+ yield* this.#compact(this.#compaction.config);
214
+ continue;
215
+ }
216
+
202
217
  this.#mutated = false;
203
218
  this.#toolResponse = undefined;
204
219
  this.#iterationCount++;
@@ -206,23 +221,14 @@ export class BetaToolRunner<Stream extends boolean> {
206
221
 
207
222
  const { max_iterations, compactionControl, ...params } = this.#state.params;
208
223
 
209
- if (params.stream) {
210
- stream = this.client.beta.messages.stream({ ...params }, this.#options);
211
- this.#message = stream.finalMessage();
212
- // Make sure that this promise doesn't throw before we get the option to do something about it.
213
- // Error will be caught when we call await this.#message ultimately
214
- this.#message.catch(() => {});
215
- yield stream as any;
216
- } else {
217
- this.#message = this.client.beta.messages.create({ ...params, stream: false }, this.#options);
218
- yield this.#message as any;
219
- }
224
+ yield* this.#send(params);
220
225
 
221
226
  const isCompacted = await this.#checkAndCompact();
222
227
  if (!isCompacted) {
223
228
  if (!this.#mutated) {
224
- const message = await this.#message;
229
+ const message = await this.#message!;
225
230
  const nextStep = determineNextStepFromStopReason(message.stop_reason);
231
+ this.#turnPaused = nextStep === 'resume';
226
232
  this.#state.params.messages.push({ role: message.role, content: message.content });
227
233
 
228
234
  // Container-bound server tools reject a follow-up request that omits the container the
@@ -237,24 +243,28 @@ export class BetaToolRunner<Stream extends boolean> {
237
243
  }
238
244
 
239
245
  if (nextStep === 'stop') {
246
+ yield* this.#compactAfterFinalTurn();
240
247
  break;
241
248
  }
242
249
  if (nextStep === 'resume') {
243
250
  continue;
244
251
  }
252
+ } else {
253
+ // The caller has taken over the history, so the last response no longer says how it ends.
254
+ this.#turnPaused = false;
245
255
  }
246
256
 
247
257
  const toolMessage = await this.#generateToolResponse(this.#state.params.messages.at(-1)!);
248
258
  if (toolMessage) {
249
259
  this.#state.params.messages.push(toolMessage);
250
260
  } else if (!this.#mutated) {
261
+ yield* this.#compactAfterFinalTurn();
251
262
  break;
252
263
  }
253
264
  }
254
265
  } finally {
255
- if (stream) {
256
- stream.abort();
257
- }
266
+ this.#stream?.abort();
267
+ this.#stream = undefined;
258
268
  }
259
269
  }
260
270
 
@@ -273,6 +283,69 @@ export class BetaToolRunner<Stream extends boolean> {
273
283
  }
274
284
  }
275
285
 
286
+ /**
287
+ * Sends one request and yields its message, or its stream when streaming. `#message` and `#stream` are set
288
+ * before the yield, so they are there while the caller handles the item; the loop aborts the stream at the
289
+ * end of the iteration.
290
+ */
291
+ async *#send(params: MessageCreateParams): AsyncGenerator<BetaToolRunnerItem<Stream>, void, undefined> {
292
+ if (params.stream) {
293
+ this.#stream = this.client.beta.messages.stream({ ...params }, this.#options);
294
+ this.#message = this.#stream.finalMessage();
295
+ // Make sure that this promise doesn't throw before we get the option to do something about it.
296
+ // Error will be caught when we call await this.#message ultimately
297
+ this.#message.catch(() => {});
298
+ yield this.#stream as any;
299
+ } else {
300
+ this.#message = this.client.beta.messages.create({ ...params, stream: false }, this.#options);
301
+ yield this.#message as any;
302
+ }
303
+ }
304
+
305
+ async *#compact(
306
+ compaction: BetaCompactionConfig,
307
+ ): AsyncGenerator<BetaToolRunnerItem<Stream>, void, undefined> {
308
+ rejectCompactionEdit(this.#state.params);
309
+ // The API refuses `compaction` alongside `context_management`; later requests keep it.
310
+ const { max_iterations, compactionControl, context_management, ...params } = this.#state.params;
311
+ this.#compaction = { status: 'in_flight' };
312
+ this.#toolResponse = undefined;
313
+ const lastMessage = this.#message;
314
+
315
+ try {
316
+ yield* this.#send({ ...params, compaction });
317
+ const message = await this.#message!;
318
+ if (message.content.some((block) => block.type === 'compaction' && block.content)) {
319
+ // The response has to be sent back as it came, first, replacing the messages it summarizes.
320
+ this.#state.params.messages = [{ role: message.role, content: message.content }];
321
+ } else {
322
+ loggerFor(this.client).warn('Compaction produced no summary; keeping the conversation as it is.');
323
+ // If the run ends here, `done()` resolves to the last real message rather than this response.
324
+ this.#message = lastMessage;
325
+ }
326
+ } finally {
327
+ this.#compaction = { status: 'idle' };
328
+ }
329
+ }
330
+
331
+ async *#compactAfterFinalTurn(): AsyncGenerator<BetaToolRunnerItem<Stream>, void, undefined> {
332
+ if (this.#compaction.status !== 'scheduled') {
333
+ return;
334
+ }
335
+ const lastContent = this.#state.params.messages.at(-1)?.content;
336
+ if (Array.isArray(lastContent) && lastContent.some((block) => block.type === 'tool_use')) {
337
+ // A turn that was cut short can end with tool calls that are never run, and the API can't
338
+ // compact a conversation whose last turn has an unanswered tool call.
339
+ loggerFor(this.client).warn(
340
+ 'The pending compaction was skipped because the last turn ended with tool calls that were not run. ' +
341
+ 'Call `compactBeforeNextTurn()` again if you continue the conversation.',
342
+ );
343
+ this.#compaction = { status: 'idle' };
344
+ return;
345
+ }
346
+ yield* this.#compact(this.#compaction.config);
347
+ }
348
+
276
349
  /**
277
350
  * Update the parameters for the next API call. This invalidates any cached tool responses.
278
351
  *
@@ -297,11 +370,19 @@ export class BetaToolRunner<Stream extends boolean> {
297
370
  setMessagesParams(
298
371
  paramsOrMutator: BetaToolRunnerParams | ((prevParams: BetaToolRunnerParams) => BetaToolRunnerParams),
299
372
  ) {
300
- if (typeof paramsOrMutator === 'function') {
301
- this.#state.params = paramsOrMutator(this.#state.params);
302
- } else {
303
- this.#state.params = paramsOrMutator;
373
+ const params =
374
+ typeof paramsOrMutator === 'function' ? paramsOrMutator(this.#state.params) : paramsOrMutator;
375
+ rejectCompactionParam(params);
376
+ if (this.#compaction.status !== 'idle') {
377
+ rejectCompactionEdit(params);
304
378
  }
379
+ if (this.#compaction.status === 'in_flight' && params.messages !== this.#state.params.messages) {
380
+ throw new AnthropicError(
381
+ "Message params can't be changed while the conversation is being compacted, because the compaction " +
382
+ 'response is about to replace them. Change them after this iteration instead.',
383
+ );
384
+ }
385
+ this.#state.params = params;
305
386
  this.#mutated = true;
306
387
  // Invalidate cached tool response since parameters changed
307
388
  this.#toolResponse = undefined;
@@ -458,6 +539,28 @@ export class BetaToolRunner<Stream extends boolean> {
458
539
  }));
459
540
  }
460
541
 
542
+ /**
543
+ * Schedule a compaction of the conversation. Once the current turn has finished, including any tool
544
+ * calls, the runner requests a summary and replaces the message history with the compaction response,
545
+ * which is yielded like any other message. Requires the `compact-2026-09-04` beta.
546
+ *
547
+ * @param compaction - The config to send, as `messages.create()` takes it. Defaults to `{ type: 'summarize' }`
548
+ *
549
+ * @example
550
+ * for await (const message of runner) {
551
+ * if (message.usage.input_tokens > 100_000) {
552
+ * runner.compactBeforeNextTurn();
553
+ * }
554
+ * }
555
+ */
556
+ compactBeforeNextTurn(compaction?: BetaCompactionConfig): void {
557
+ if (this.#compaction.status === 'in_flight') {
558
+ return;
559
+ }
560
+ rejectCompactionEdit(this.#state.params);
561
+ this.#compaction = { status: 'scheduled', config: compaction ?? { type: 'summarize' } };
562
+ }
563
+
461
564
  /**
462
565
  * Makes the ToolRunner directly awaitable, equivalent to calling .runUntilDone()
463
566
  * This allows using `await runner` instead of `await runner.runUntilDone()`
@@ -470,6 +573,26 @@ export class BetaToolRunner<Stream extends boolean> {
470
573
  }
471
574
  }
472
575
 
576
+ function rejectCompactionParam(params: BetaToolRunnerParams): void {
577
+ if ('compaction' in params && params.compaction != null) {
578
+ throw new AnthropicError(
579
+ '`compaction` cannot be set on a tool runner: every request in the loop would compact again. ' +
580
+ 'Call `runner.compactBeforeNextTurn()` when the conversation should be compacted instead.',
581
+ );
582
+ }
583
+ }
584
+
585
+ function rejectCompactionEdit(params: BetaToolRunnerParams): void {
586
+ // The compaction request is sent without `context_management`, so the API can't refuse the pair there:
587
+ // it would run and bill the compaction, then refuse the next request.
588
+ if (params.context_management?.edits?.some((edit) => edit.type.startsWith('compact_'))) {
589
+ throw new AnthropicError(
590
+ "`compactBeforeNextTurn()` can't be used while `context_management` has a compaction edit, " +
591
+ "because the API doesn't accept a compaction block together with one. Remove the edit first.",
592
+ );
593
+ }
594
+ }
595
+
473
596
  async function generateToolResponse(
474
597
  params: BetaToolRunnerParams,
475
598
  lastMessage = params.messages.at(-1),
@@ -644,14 +767,14 @@ function determineNextStepFromStopReason(stopReason: BetaStopReason | null): Nex
644
767
  }
645
768
  }
646
769
 
647
- // vendored from typefest just to make things look a bit nicer on hover
648
- type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};
649
-
650
770
  /**
651
771
  * Parameters for creating a ToolRunner, extending MessageCreateParams with runnable tools.
772
+ *
773
+ * `compaction` is left out because every request in the loop would compact again; call
774
+ * `BetaToolRunner.compactBeforeNextTurn()` instead.
652
775
  */
653
776
  export type BetaToolRunnerParams = Simplify<
654
- Omit<MessageCreateParams, 'tools'> & {
777
+ Omit<MessageCreateParams, 'tools' | 'compaction'> & {
655
778
  tools: (BetaToolUnion | BetaRunnableTool<any>)[];
656
779
  /**
657
780
  * Maximum number of iterations (API requests) to make in the tool execution loop.
@@ -669,3 +792,13 @@ export type BetaToolRunnerParams = Simplify<
669
792
  >;
670
793
 
671
794
  export type BetaToolRunnerRequestOptions = Pick<RequestOptions, 'headers' | 'signal' | 'fallbackState'>;
795
+
796
+ type Compaction =
797
+ | { status: 'idle' }
798
+ | { status: 'scheduled'; config: BetaCompactionConfig }
799
+ | { status: 'in_flight' };
800
+
801
+ type BetaToolRunnerItem<Stream extends boolean> =
802
+ Stream extends true ? BetaMessageStream
803
+ : Stream extends false ? BetaMessage
804
+ : BetaMessage | BetaMessageStream;
@@ -28,12 +28,8 @@ export function transformJSONSchema(jsonSchema: JSONSchema): JSONSchema {
28
28
  function _transformJSONSchema(jsonSchema: JSONSchema): JSONSchema {
29
29
  const strictSchema: JSONSchema = {};
30
30
 
31
- const ref = pop(jsonSchema, '$ref');
32
- if (ref !== undefined) {
33
- strictSchema['$ref'] = ref;
34
- return strictSchema;
35
- }
36
-
31
+ // `$defs` must be handled before the `$ref` early-return so that a root-level
32
+ // `$ref` with sibling `$defs` keeps its (transformed) definitions.
37
33
  const defs = pop(jsonSchema, '$defs');
38
34
  if (defs !== undefined) {
39
35
  const strictDefs: Record<string, any> = {};
@@ -43,6 +39,12 @@ function _transformJSONSchema(jsonSchema: JSONSchema): JSONSchema {
43
39
  }
44
40
  }
45
41
 
42
+ const ref = pop(jsonSchema, '$ref');
43
+ if (ref !== undefined) {
44
+ strictSchema['$ref'] = ref;
45
+ return strictSchema;
46
+ }
47
+
46
48
  const type = pop(jsonSchema, 'type');
47
49
  const anyOf = pop(jsonSchema, 'anyOf');
48
50
  const oneOf = pop(jsonSchema, 'oneOf');
@@ -311,6 +311,10 @@ export type BetaManagedAgentsTriggerContext =
311
311
 
312
312
  /**
313
313
  * What triggered a deployment run.
314
+ *
315
+ * - `schedule` - The run was fired by the deployment's cron schedule.
316
+ * - `manual` - The run was started manually by creating a session directly against
317
+ * the deployment.
314
318
  */
315
319
  export type BetaManagedAgentsTriggerType = 'schedule' | 'manual';
316
320
 
@@ -460,6 +460,11 @@ export type BetaManagedAgentsDeploymentPausedReasonError =
460
460
 
461
461
  /**
462
462
  * Lifecycle status of a deployment.
463
+ *
464
+ * - `active` - The deployment is active and can run sessions. Archived deployments
465
+ * also report this status; check `archived_at` to distinguish them.
466
+ * - `paused` - The deployment is paused. Autonomous triggers are suppressed;
467
+ * manual runs are still permitted.
463
468
  */
464
469
  export type BetaManagedAgentsDeploymentStatus = 'active' | 'paused';
465
470
 
@@ -295,6 +295,23 @@ export interface BetaDreamSessionsInput {
295
295
 
296
296
  /**
297
297
  * Lifecycle status of a Dream.
298
+ *
299
+ * - `pending` - The dream is waiting to start and hasn't read its inputs yet.
300
+ *
301
+ * `outputs` is empty and every `usage` count is zero.
302
+ *
303
+ * - `running` - The dream is reading its inputs and writing its result.
304
+ *
305
+ * `usage` updates while the dream has this status.
306
+ *
307
+ * - `completed` - The dream finished and its output memory store holds the
308
+ * complete result.
309
+ * - `failed` - The dream stopped with an error, which `error` describes.
310
+ *
311
+ * If `outputs` references a memory store, that memory store keeps what the dream
312
+ * wrote before it stopped.
313
+ *
314
+ * - `canceled` - The caller canceled the dream before it completed.
298
315
  */
299
316
  export type BetaDreamStatus = 'pending' | 'running' | 'completed' | 'failed' | 'canceled';
300
317
 
@@ -346,6 +346,12 @@ export interface BetaManagedAgentsMemoryPrefix {
346
346
  * `content`. When omitted, the default is endpoint-specific: retrieve operations
347
347
  * default to `full`; list, create, and update operations default to `basic`.
348
348
  * Listing with `view=full` caps `limit` at 20.
349
+ *
350
+ * - `basic` - Return the object with `content` set to `null`. The
351
+ * `content_size_bytes` and `content_sha256` fields remain populated, so sync
352
+ * clients can diff without fetching content.
353
+ * - `full` - Return the object with `content` populated. On list endpoints,
354
+ * `view=full` caps `limit` at 20.
349
355
  */
350
356
  export type BetaManagedAgentsMemoryView = 'basic' | 'full';
351
357
 
@@ -236,6 +236,13 @@ export interface BetaManagedAgentsMemoryVersion {
236
236
  /**
237
237
  * The kind of mutation a `memory_version` records. Every non-no-op mutation to a
238
238
  * memory appends exactly one version row with one of these values.
239
+ *
240
+ * - `created` - The memory was created. The first version in any memory's lineage.
241
+ * - `modified` - The memory's `content`, `path`, or both were changed via update.
242
+ * Writes the agent makes through the filesystem mount also appear as `modified`.
243
+ * - `deleted` - The memory was deleted. The `content`, `content_size_bytes`, and
244
+ * `content_sha256` fields are `null` on this version. The preceding version,
245
+ * while it is retained, records the deleted content's size and hash.
239
246
  */
240
247
  export type BetaManagedAgentsMemoryVersionOperation = 'created' | 'modified' | 'deleted';
241
248
 
@@ -5000,7 +5000,7 @@ export interface BetaToolBash20250124 {
5000
5000
  }
5001
5001
 
5002
5002
  /**
5003
- * Reference to a single MCP tool by its server and remote name the same
5003
+ * Reference to a single MCP tool by its server and remote name; the same
5004
5004
  * `server_name`/`name` pair `mcp_tool_use` carries.
5005
5005
  */
5006
5006
  export interface BetaToolChangeMCPToolReference {
@@ -50,7 +50,13 @@ export { Organization, type BetaOrganization, type BetaOrganizationRole } from '
50
50
  export {
51
51
  RateLimits,
52
52
  type BetaOrganizationRateLimit,
53
+ type BetaOrganizationRateLimitBatchGroup,
54
+ type BetaOrganizationRateLimitFilesGroup,
55
+ type BetaOrganizationRateLimitModelGroup,
56
+ type BetaOrganizationRateLimitSkillsGroup,
57
+ type BetaOrganizationRateLimitTokenCountGroup,
53
58
  type BetaOrganizationRateLimitValue,
59
+ type BetaOrganizationRateLimitWebSearchGroup,
54
60
  type RateLimitListParams,
55
61
  type BetaOrganizationRateLimitsPageCursor,
56
62
  } from './rate-limits';
@@ -53,7 +53,13 @@ import {
53
53
  import * as RateLimitsAPI from './rate-limits';
54
54
  import {
55
55
  BetaOrganizationRateLimit,
56
+ BetaOrganizationRateLimitBatchGroup,
57
+ BetaOrganizationRateLimitFilesGroup,
58
+ BetaOrganizationRateLimitModelGroup,
59
+ BetaOrganizationRateLimitSkillsGroup,
60
+ BetaOrganizationRateLimitTokenCountGroup,
56
61
  BetaOrganizationRateLimitValue,
62
+ BetaOrganizationRateLimitWebSearchGroup,
57
63
  BetaOrganizationRateLimitsPageCursor,
58
64
  RateLimitListParams,
59
65
  RateLimits,
@@ -252,7 +258,13 @@ export declare namespace Organization {
252
258
  export {
253
259
  RateLimits as RateLimits,
254
260
  type BetaOrganizationRateLimit as BetaOrganizationRateLimit,
261
+ type BetaOrganizationRateLimitBatchGroup as BetaOrganizationRateLimitBatchGroup,
262
+ type BetaOrganizationRateLimitFilesGroup as BetaOrganizationRateLimitFilesGroup,
263
+ type BetaOrganizationRateLimitModelGroup as BetaOrganizationRateLimitModelGroup,
264
+ type BetaOrganizationRateLimitSkillsGroup as BetaOrganizationRateLimitSkillsGroup,
265
+ type BetaOrganizationRateLimitTokenCountGroup as BetaOrganizationRateLimitTokenCountGroup,
255
266
  type BetaOrganizationRateLimitValue as BetaOrganizationRateLimitValue,
267
+ type BetaOrganizationRateLimitWebSearchGroup as BetaOrganizationRateLimitWebSearchGroup,
256
268
  type BetaOrganizationRateLimitsPageCursor as BetaOrganizationRateLimitsPageCursor,
257
269
  type RateLimitListParams as RateLimitListParams,
258
270
  };
@@ -37,14 +37,26 @@ export type BetaOrganizationRateLimitsPageCursor = PageCursor<BetaOrganizationRa
37
37
 
38
38
  export interface BetaOrganizationRateLimit {
39
39
  /**
40
- * Stable identifier for this rate-limit group within the organization.
40
+ * Identifier of this rate-limit entry. It is stable within the organization and
41
+ * differs between organizations; the group's own identifier is `group.id`.
41
42
  */
42
43
  id: string;
43
44
 
44
45
  /**
45
- * The kind of rate-limit group this entry represents. `model_group` entries apply
46
- * to a family of models (listed in `models`); other values apply to an API-surface
47
- * category and have `models` set to `null`.
46
+ * The rate-limit group this entry's limits apply to. Its `type` equals
47
+ * `group_type`.
48
+ */
49
+ group:
50
+ | BetaOrganizationRateLimitModelGroup
51
+ | BetaOrganizationRateLimitBatchGroup
52
+ | BetaOrganizationRateLimitTokenCountGroup
53
+ | BetaOrganizationRateLimitFilesGroup
54
+ | BetaOrganizationRateLimitSkillsGroup
55
+ | BetaOrganizationRateLimitWebSearchGroup;
56
+
57
+ /**
58
+ * @deprecated Use `group.type` instead. `group_type` is still returned and always
59
+ * equals `group.type`.
48
60
  */
49
61
  group_type: 'batch' | 'files' | 'model_group' | 'skills' | 'token_count' | 'web_search';
50
62
 
@@ -65,6 +77,82 @@ export interface BetaOrganizationRateLimit {
65
77
  type: 'rate_limit';
66
78
  }
67
79
 
80
+ export interface BetaOrganizationRateLimitBatchGroup {
81
+ /**
82
+ * Opaque identifier of the rate-limit group (for example,
83
+ * `rlg_01VPTCmyiu5ZLsWkcxYG2pY8`). It is the same in every organization and never
84
+ * changes, unlike the entry's own identifier, which differs per organization.
85
+ */
86
+ id: string;
87
+
88
+ /**
89
+ * Always `batch`: the Message Batches API.
90
+ */
91
+ type: 'batch';
92
+ }
93
+
94
+ export interface BetaOrganizationRateLimitFilesGroup {
95
+ /**
96
+ * Opaque identifier of the rate-limit group (for example,
97
+ * `rlg_01VPTCmyiu5ZLsWkcxYG2pY8`). It is the same in every organization and never
98
+ * changes, unlike the entry's own identifier, which differs per organization.
99
+ */
100
+ id: string;
101
+
102
+ /**
103
+ * Always `files`: the Files API.
104
+ */
105
+ type: 'files';
106
+ }
107
+
108
+ export interface BetaOrganizationRateLimitModelGroup {
109
+ /**
110
+ * Opaque identifier of the rate-limit group (for example,
111
+ * `rlg_01VPTCmyiu5ZLsWkcxYG2pY8`). It is the same in every organization and never
112
+ * changes, unlike the entry's own identifier, which differs per organization.
113
+ */
114
+ id: string;
115
+
116
+ /**
117
+ * Human-readable name of the model group (for example, `Claude Sonnet 4.x`). For
118
+ * display only; it may change.
119
+ */
120
+ display_name: string;
121
+
122
+ /**
123
+ * Always `model_group`: a family of models.
124
+ */
125
+ type: 'model_group';
126
+ }
127
+
128
+ export interface BetaOrganizationRateLimitSkillsGroup {
129
+ /**
130
+ * Opaque identifier of the rate-limit group (for example,
131
+ * `rlg_01VPTCmyiu5ZLsWkcxYG2pY8`). It is the same in every organization and never
132
+ * changes, unlike the entry's own identifier, which differs per organization.
133
+ */
134
+ id: string;
135
+
136
+ /**
137
+ * Always `skills`: the Skills API.
138
+ */
139
+ type: 'skills';
140
+ }
141
+
142
+ export interface BetaOrganizationRateLimitTokenCountGroup {
143
+ /**
144
+ * Opaque identifier of the rate-limit group (for example,
145
+ * `rlg_01VPTCmyiu5ZLsWkcxYG2pY8`). It is the same in every organization and never
146
+ * changes, unlike the entry's own identifier, which differs per organization.
147
+ */
148
+ id: string;
149
+
150
+ /**
151
+ * Always `token_count`: the Token Count API.
152
+ */
153
+ type: 'token_count';
154
+ }
155
+
68
156
  export interface BetaOrganizationRateLimitValue {
69
157
  /**
70
158
  * The limiter type (for example, `requests_per_minute` or
@@ -78,6 +166,20 @@ export interface BetaOrganizationRateLimitValue {
78
166
  value: number;
79
167
  }
80
168
 
169
+ export interface BetaOrganizationRateLimitWebSearchGroup {
170
+ /**
171
+ * Opaque identifier of the rate-limit group (for example,
172
+ * `rlg_01VPTCmyiu5ZLsWkcxYG2pY8`). It is the same in every organization and never
173
+ * changes, unlike the entry's own identifier, which differs per organization.
174
+ */
175
+ id: string;
176
+
177
+ /**
178
+ * Always `web_search`: the Messages API web search tool.
179
+ */
180
+ type: 'web_search';
181
+ }
182
+
81
183
  export interface RateLimitListParams extends PageCursorParams {
82
184
  /**
83
185
  * Filter by group type.
@@ -95,7 +197,13 @@ export interface RateLimitListParams extends PageCursorParams {
95
197
  export declare namespace RateLimits {
96
198
  export {
97
199
  type BetaOrganizationRateLimit as BetaOrganizationRateLimit,
200
+ type BetaOrganizationRateLimitBatchGroup as BetaOrganizationRateLimitBatchGroup,
201
+ type BetaOrganizationRateLimitFilesGroup as BetaOrganizationRateLimitFilesGroup,
202
+ type BetaOrganizationRateLimitModelGroup as BetaOrganizationRateLimitModelGroup,
203
+ type BetaOrganizationRateLimitSkillsGroup as BetaOrganizationRateLimitSkillsGroup,
204
+ type BetaOrganizationRateLimitTokenCountGroup as BetaOrganizationRateLimitTokenCountGroup,
98
205
  type BetaOrganizationRateLimitValue as BetaOrganizationRateLimitValue,
206
+ type BetaOrganizationRateLimitWebSearchGroup as BetaOrganizationRateLimitWebSearchGroup,
99
207
  type BetaOrganizationRateLimitsPageCursor as BetaOrganizationRateLimitsPageCursor,
100
208
  type RateLimitListParams as RateLimitListParams,
101
209
  };
@@ -1,4 +1,5 @@
1
1
  import { APIResource } from '../../../../core/resource';
2
+ import * as RateLimitsAPI from '../rate-limits';
2
3
  import { PageCursor, type PageCursorParams, PagePromise } from '../../../../core/pagination';
3
4
  import { RequestOptions } from '../../../../internal/request-options';
4
5
  import { path } from '../../../../internal/utils/path';
@@ -41,9 +42,20 @@ export type BetaWorkspaceRateLimitsPageCursor = PageCursor<BetaWorkspaceRateLimi
41
42
 
42
43
  export interface BetaWorkspaceRateLimit {
43
44
  /**
44
- * The kind of rate-limit group this entry represents. `model_group` entries apply
45
- * to a family of models (listed in `models`); other values apply to an API-surface
46
- * category and have `models` set to `null`.
45
+ * The rate-limit group this entry's limits apply to. Its `type` equals
46
+ * `group_type`.
47
+ */
48
+ group:
49
+ | RateLimitsAPI.BetaOrganizationRateLimitModelGroup
50
+ | RateLimitsAPI.BetaOrganizationRateLimitBatchGroup
51
+ | RateLimitsAPI.BetaOrganizationRateLimitTokenCountGroup
52
+ | RateLimitsAPI.BetaOrganizationRateLimitFilesGroup
53
+ | RateLimitsAPI.BetaOrganizationRateLimitSkillsGroup
54
+ | RateLimitsAPI.BetaOrganizationRateLimitWebSearchGroup;
55
+
56
+ /**
57
+ * @deprecated Use `group.type` instead. `group_type` is still returned and always
58
+ * equals `group.type`.
47
59
  */
48
60
  group_type: 'batch' | 'files' | 'model_group' | 'skills' | 'token_count' | 'web_search';
49
61
 
@@ -60,7 +72,7 @@ export interface BetaWorkspaceRateLimit {
60
72
  models: Array<string> | null;
61
73
 
62
74
  /**
63
- * The `id` of the RateLimit group this override applies to.
75
+ * The `id` of the organization's RateLimit entry this override applies to.
64
76
  */
65
77
  rate_limit_id: string;
66
78
 
@@ -741,6 +741,12 @@ export interface BetaManagedAgentsSession {
741
741
 
742
742
  /**
743
743
  * SessionStatus enum
744
+ *
745
+ * - `rescheduling` - Transient error occurred, retrying automatically.
746
+ * - `running` - Agent is actively executing.
747
+ * - `idle` - Agent is waiting for input, including user messages or tool
748
+ * confirmations. Sessions start in idle.
749
+ * - `terminated` - Session has ended, either due to an error or completion.
744
750
  */
745
751
  status: 'rescheduling' | 'running' | 'idle' | 'terminated';
746
752