@anthropic-ai/sdk 0.27.3 → 0.29.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 (111) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/README.md +80 -0
  3. package/_shims/node-types.d.ts +1 -1
  4. package/core.d.ts +6 -3
  5. package/core.d.ts.map +1 -1
  6. package/core.js +30 -10
  7. package/core.js.map +1 -1
  8. package/core.mjs +27 -8
  9. package/core.mjs.map +1 -1
  10. package/index.d.mts +19 -0
  11. package/index.d.ts +19 -0
  12. package/index.d.ts.map +1 -1
  13. package/index.js +2 -0
  14. package/index.js.map +1 -1
  15. package/index.mjs +2 -0
  16. package/index.mjs.map +1 -1
  17. package/internal/decoders/jsonl.d.ts +12 -0
  18. package/internal/decoders/jsonl.d.ts.map +1 -0
  19. package/internal/decoders/jsonl.js +35 -0
  20. package/internal/decoders/jsonl.js.map +1 -0
  21. package/internal/decoders/jsonl.mjs +31 -0
  22. package/internal/decoders/jsonl.mjs.map +1 -0
  23. package/internal/decoders/line.d.ts +20 -0
  24. package/internal/decoders/line.d.ts.map +1 -0
  25. package/internal/decoders/line.js +88 -0
  26. package/internal/decoders/line.js.map +1 -0
  27. package/internal/decoders/line.mjs +84 -0
  28. package/internal/decoders/line.mjs.map +1 -0
  29. package/package.json +1 -1
  30. package/pagination.d.ts +26 -0
  31. package/pagination.d.ts.map +1 -0
  32. package/pagination.js +54 -0
  33. package/pagination.js.map +1 -0
  34. package/pagination.mjs +50 -0
  35. package/pagination.mjs.map +1 -0
  36. package/resources/beta/beta.d.ts +79 -0
  37. package/resources/beta/beta.d.ts.map +1 -1
  38. package/resources/beta/beta.js +3 -0
  39. package/resources/beta/beta.js.map +1 -1
  40. package/resources/beta/beta.mjs +3 -0
  41. package/resources/beta/beta.mjs.map +1 -1
  42. package/resources/beta/index.d.ts +2 -1
  43. package/resources/beta/index.d.ts.map +1 -1
  44. package/resources/beta/index.js +5 -3
  45. package/resources/beta/index.js.map +1 -1
  46. package/resources/beta/index.mjs +2 -1
  47. package/resources/beta/index.mjs.map +1 -1
  48. package/resources/beta/messages/batches.d.ts +242 -0
  49. package/resources/beta/messages/batches.d.ts.map +1 -0
  50. package/resources/beta/messages/batches.js +120 -0
  51. package/resources/beta/messages/batches.js.map +1 -0
  52. package/resources/beta/messages/batches.mjs +92 -0
  53. package/resources/beta/messages/batches.mjs.map +1 -0
  54. package/resources/beta/messages/index.d.ts +3 -0
  55. package/resources/beta/messages/index.d.ts.map +1 -0
  56. package/resources/beta/messages/index.js +10 -0
  57. package/resources/beta/messages/index.js.map +1 -0
  58. package/resources/beta/messages/index.mjs +4 -0
  59. package/resources/beta/messages/index.mjs.map +1 -0
  60. package/resources/beta/messages/messages.d.ts +663 -0
  61. package/resources/beta/messages/messages.d.ts.map +1 -0
  62. package/resources/beta/messages/messages.js +54 -0
  63. package/resources/beta/messages/messages.js.map +1 -0
  64. package/resources/beta/messages/messages.mjs +27 -0
  65. package/resources/beta/messages/messages.mjs.map +1 -0
  66. package/resources/beta/prompt-caching/messages.d.ts +37 -50
  67. package/resources/beta/prompt-caching/messages.d.ts.map +1 -1
  68. package/resources/beta/prompt-caching/messages.js +7 -3
  69. package/resources/beta/prompt-caching/messages.js.map +1 -1
  70. package/resources/beta/prompt-caching/messages.mjs +7 -3
  71. package/resources/beta/prompt-caching/messages.mjs.map +1 -1
  72. package/resources/completions.d.ts +3 -12
  73. package/resources/completions.d.ts.map +1 -1
  74. package/resources/completions.js.map +1 -1
  75. package/resources/completions.mjs.map +1 -1
  76. package/resources/index.d.ts +2 -2
  77. package/resources/index.d.ts.map +1 -1
  78. package/resources/index.js.map +1 -1
  79. package/resources/index.mjs +1 -1
  80. package/resources/index.mjs.map +1 -1
  81. package/resources/messages.d.ts +73 -31
  82. package/resources/messages.d.ts.map +1 -1
  83. package/resources/messages.js.map +1 -1
  84. package/resources/messages.mjs.map +1 -1
  85. package/shims/node.d.ts +1 -0
  86. package/shims/node.d.ts.map +1 -1
  87. package/src/_shims/node-types.d.ts +1 -1
  88. package/src/core.ts +36 -9
  89. package/src/index.ts +20 -0
  90. package/src/internal/decoders/jsonl.ts +41 -0
  91. package/src/internal/decoders/line.ts +114 -0
  92. package/src/pagination.ts +84 -0
  93. package/src/resources/beta/beta.ts +104 -0
  94. package/src/resources/beta/index.ts +47 -1
  95. package/src/resources/beta/messages/batches.ts +393 -0
  96. package/src/resources/beta/messages/index.ts +53 -0
  97. package/src/resources/beta/messages/messages.ts +795 -0
  98. package/src/resources/beta/prompt-caching/messages.ts +45 -57
  99. package/src/resources/completions.ts +3 -12
  100. package/src/resources/index.ts +18 -1
  101. package/src/resources/messages.ts +82 -35
  102. package/src/streaming.ts +1 -111
  103. package/src/version.ts +1 -1
  104. package/streaming.d.ts.map +1 -1
  105. package/streaming.js +4 -85
  106. package/streaming.js.map +1 -1
  107. package/streaming.mjs +1 -82
  108. package/streaming.mjs.map +1 -1
  109. package/version.d.ts +1 -1
  110. package/version.js +1 -1
  111. package/version.mjs +1 -1
@@ -5,13 +5,12 @@ import { APIPromise } from "../../../core.js";
5
5
  import * as Core from "../../../core.js";
6
6
  import * as PromptCachingMessagesAPI from "./messages.js";
7
7
  import * as MessagesAPI from "../../messages.js";
8
+ import * as BetaAPI from "../beta.js";
8
9
  import { Stream } from "../../../streaming.js";
9
10
  import { PromptCachingBetaMessageStream } from "../../../lib/PromptCachingBetaMessageStream.js";
10
11
 
11
12
  export class Messages extends APIResource {
12
13
  /**
13
- * Create a Message.
14
- *
15
14
  * Send a structured list of input messages with text and/or image content, and the
16
15
  * model will generate the next message in the conversation.
17
16
  *
@@ -19,27 +18,31 @@ export class Messages extends APIResource {
19
18
  * conversations.
20
19
  */
21
20
  create(
22
- body: MessageCreateParamsNonStreaming,
21
+ params: MessageCreateParamsNonStreaming,
23
22
  options?: Core.RequestOptions,
24
23
  ): APIPromise<PromptCachingBetaMessage>;
25
24
  create(
26
- body: MessageCreateParamsStreaming,
25
+ params: MessageCreateParamsStreaming,
27
26
  options?: Core.RequestOptions,
28
27
  ): APIPromise<Stream<RawPromptCachingBetaMessageStreamEvent>>;
29
28
  create(
30
- body: MessageCreateParamsBase,
29
+ params: MessageCreateParamsBase,
31
30
  options?: Core.RequestOptions,
32
31
  ): APIPromise<Stream<RawPromptCachingBetaMessageStreamEvent> | PromptCachingBetaMessage>;
33
32
  create(
34
- body: MessageCreateParams,
33
+ params: MessageCreateParams,
35
34
  options?: Core.RequestOptions,
36
35
  ): APIPromise<PromptCachingBetaMessage> | APIPromise<Stream<RawPromptCachingBetaMessageStreamEvent>> {
36
+ const { betas, ...body } = params;
37
37
  return this._client.post('/v1/messages?beta=prompt_caching', {
38
38
  body,
39
39
  timeout: (this._client as any)._options.timeout ?? 600000,
40
40
  ...options,
41
- headers: { 'anthropic-beta': 'prompt-caching-2024-07-31', ...options?.headers },
42
- stream: body.stream ?? false,
41
+ headers: {
42
+ 'anthropic-beta': betas != null ? betas.toString() : 'prompt-caching-2024-07-31',
43
+ ...options?.headers,
44
+ },
45
+ stream: params.stream ?? false,
43
46
  }) as APIPromise<PromptCachingBetaMessage> | APIPromise<Stream<RawPromptCachingBetaMessageStreamEvent>>;
44
47
  }
45
48
 
@@ -304,7 +307,7 @@ export type MessageCreateParams = MessageCreateParamsNonStreaming | MessageCreat
304
307
 
305
308
  export interface MessageCreateParamsBase {
306
309
  /**
307
- * The maximum number of tokens to generate before stopping.
310
+ * Body param: The maximum number of tokens to generate before stopping.
308
311
  *
309
312
  * Note that our models may stop _before_ reaching this maximum. This parameter
310
313
  * only specifies the absolute maximum number of tokens to generate.
@@ -315,7 +318,7 @@ export interface MessageCreateParamsBase {
315
318
  max_tokens: number;
316
319
 
317
320
  /**
318
- * Input messages.
321
+ * Body param: Input messages.
319
322
  *
320
323
  * Our models are trained to operate on alternating `user` and `assistant`
321
324
  * conversational turns. When creating a new `Message`, you specify the prior
@@ -404,19 +407,19 @@ export interface MessageCreateParamsBase {
404
407
  messages: Array<PromptCachingBetaMessageParam>;
405
408
 
406
409
  /**
407
- * The model that will complete your prompt.\n\nSee
410
+ * Body param: The model that will complete your prompt.\n\nSee
408
411
  * [models](https://docs.anthropic.com/en/docs/models-overview) for additional
409
412
  * details and options.
410
413
  */
411
414
  model: MessagesAPI.Model;
412
415
 
413
416
  /**
414
- * An object describing metadata about the request.
417
+ * Body param: An object describing metadata about the request.
415
418
  */
416
- metadata?: MessageCreateParams.Metadata;
419
+ metadata?: MessagesAPI.Metadata;
417
420
 
418
421
  /**
419
- * Custom text sequences that will cause the model to stop generating.
422
+ * Body param: Custom text sequences that will cause the model to stop generating.
420
423
  *
421
424
  * Our models will normally stop when they have naturally completed their turn,
422
425
  * which will result in a response `stop_reason` of `"end_turn"`.
@@ -429,7 +432,8 @@ export interface MessageCreateParamsBase {
429
432
  stop_sequences?: Array<string>;
430
433
 
431
434
  /**
432
- * Whether to incrementally stream the response using server-sent events.
435
+ * Body param: Whether to incrementally stream the response using server-sent
436
+ * events.
433
437
  *
434
438
  * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for
435
439
  * details.
@@ -437,7 +441,7 @@ export interface MessageCreateParamsBase {
437
441
  stream?: boolean;
438
442
 
439
443
  /**
440
- * System prompt.
444
+ * Body param: System prompt.
441
445
  *
442
446
  * A system prompt is a way of providing context and instructions to Claude, such
443
447
  * as specifying a particular goal or role. See our
@@ -446,7 +450,7 @@ export interface MessageCreateParamsBase {
446
450
  system?: string | Array<PromptCachingBetaTextBlockParam>;
447
451
 
448
452
  /**
449
- * Amount of randomness injected into the response.
453
+ * Body param: Amount of randomness injected into the response.
450
454
  *
451
455
  * Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0`
452
456
  * for analytical / multiple choice, and closer to `1.0` for creative and
@@ -458,16 +462,13 @@ export interface MessageCreateParamsBase {
458
462
  temperature?: number;
459
463
 
460
464
  /**
461
- * How the model should use the provided tools. The model can use a specific tool,
462
- * any available tool, or decide by itself.
465
+ * Body param: How the model should use the provided tools. The model can use a
466
+ * specific tool, any available tool, or decide by itself.
463
467
  */
464
- tool_choice?:
465
- | MessageCreateParams.ToolChoiceAuto
466
- | MessageCreateParams.ToolChoiceAny
467
- | MessageCreateParams.ToolChoiceTool;
468
+ tool_choice?: MessagesAPI.ToolChoice;
468
469
 
469
470
  /**
470
- * Definitions of tools that the model may use.
471
+ * Body param: Definitions of tools that the model may use.
471
472
  *
472
473
  * If you include `tools` in your API request, the model may return `tool_use`
473
474
  * content blocks that represent the model's use of those tools. You can then run
@@ -539,7 +540,7 @@ export interface MessageCreateParamsBase {
539
540
  tools?: Array<PromptCachingBetaTool>;
540
541
 
541
542
  /**
542
- * Only sample from the top K options for each subsequent token.
543
+ * Body param: Only sample from the top K options for each subsequent token.
543
544
  *
544
545
  * Used to remove "long tail" low probability responses.
545
546
  * [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277).
@@ -550,7 +551,7 @@ export interface MessageCreateParamsBase {
550
551
  top_k?: number;
551
552
 
552
553
  /**
553
- * Use nucleus sampling.
554
+ * Body param: Use nucleus sampling.
554
555
  *
555
556
  * In nucleus sampling, we compute the cumulative distribution over all the options
556
557
  * for each subsequent token in decreasing probability order and cut it off once it
@@ -561,48 +562,33 @@ export interface MessageCreateParamsBase {
561
562
  * `temperature`.
562
563
  */
563
564
  top_p?: number;
565
+
566
+ /**
567
+ * Header param: Optional header to specify the beta version(s) you want to use.
568
+ */
569
+ betas?: Array<BetaAPI.AnthropicBeta>;
564
570
  }
565
571
 
566
572
  export namespace MessageCreateParams {
567
573
  /**
568
- * An object describing metadata about the request.
569
- */
570
- export interface Metadata {
571
- /**
572
- * An external identifier for the user who is associated with the request.
573
- *
574
- * This should be a uuid, hash value, or other opaque identifier. Anthropic may use
575
- * this id to help detect abuse. Do not include any identifying information such as
576
- * name, email address, or phone number.
577
- */
578
- user_id?: string | null;
579
- }
574
+ * @deprecated use `Anthropic.Messages.Metadata` instead
575
+ */
576
+ export type Metadata = MessagesAPI.Metadata;
580
577
 
581
578
  /**
582
- * The model will automatically decide whether to use tools.
579
+ * @deprecated use `Anthropic.Messages.ToolChoiceAuto` instead
583
580
  */
584
- export interface ToolChoiceAuto {
585
- type: 'auto';
586
- }
581
+ export type ToolChoiceAuto = MessagesAPI.ToolChoiceAuto;
587
582
 
588
583
  /**
589
- * The model will use any available tools.
584
+ * @deprecated use `Anthropic.Messages.ToolChoiceAny` instead
590
585
  */
591
- export interface ToolChoiceAny {
592
- type: 'any';
593
- }
586
+ export type ToolChoiceAny = MessagesAPI.ToolChoiceAny;
594
587
 
595
588
  /**
596
- * The model will use the specified tool with `tool_choice.name`.
589
+ * @deprecated use `Anthropic.Messages.ToolChoiceTool` instead
597
590
  */
598
- export interface ToolChoiceTool {
599
- /**
600
- * The name of the tool to use.
601
- */
602
- name: string;
603
-
604
- type: 'tool';
605
- }
591
+ export type ToolChoiceTool = MessagesAPI.ToolChoiceTool;
606
592
 
607
593
  export type MessageCreateParamsNonStreaming = PromptCachingMessagesAPI.MessageCreateParamsNonStreaming;
608
594
  export type MessageCreateParamsStreaming = PromptCachingMessagesAPI.MessageCreateParamsStreaming;
@@ -610,7 +596,8 @@ export namespace MessageCreateParams {
610
596
 
611
597
  export interface MessageCreateParamsNonStreaming extends MessageCreateParamsBase {
612
598
  /**
613
- * Whether to incrementally stream the response using server-sent events.
599
+ * Body param: Whether to incrementally stream the response using server-sent
600
+ * events.
614
601
  *
615
602
  * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for
616
603
  * details.
@@ -620,7 +607,8 @@ export interface MessageCreateParamsNonStreaming extends MessageCreateParamsBase
620
607
 
621
608
  export interface MessageCreateParamsStreaming extends MessageCreateParamsBase {
622
609
  /**
623
- * Whether to incrementally stream the response using server-sent events.
610
+ * Body param: Whether to incrementally stream the response using server-sent
611
+ * events.
624
612
  *
625
613
  * See [streaming](https://docs.anthropic.com/en/api/messages-streaming) for
626
614
  * details.
@@ -117,7 +117,7 @@ export interface CompletionCreateParamsBase {
117
117
  /**
118
118
  * An object describing metadata about the request.
119
119
  */
120
- metadata?: CompletionCreateParams.Metadata;
120
+ metadata?: MessagesAPI.Metadata;
121
121
 
122
122
  /**
123
123
  * Sequences that will cause the model to stop generating.
@@ -174,18 +174,9 @@ export interface CompletionCreateParamsBase {
174
174
 
175
175
  export namespace CompletionCreateParams {
176
176
  /**
177
- * An object describing metadata about the request.
177
+ * @deprecated use `Anthropic.Messages.Metadata` instead
178
178
  */
179
- export interface Metadata {
180
- /**
181
- * An external identifier for the user who is associated with the request.
182
- *
183
- * This should be a uuid, hash value, or other opaque identifier. Anthropic may use
184
- * this id to help detect abuse. Do not include any identifying information such as
185
- * name, email address, or phone number.
186
- */
187
- user_id?: string | null;
188
- }
179
+ export type Metadata = MessagesAPI.Metadata;
189
180
 
190
181
  export type CompletionCreateParamsNonStreaming = CompletionsAPI.CompletionCreateParamsNonStreaming;
191
182
  export type CompletionCreateParamsStreaming = CompletionsAPI.CompletionCreateParamsStreaming;
@@ -1,6 +1,18 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
- export { Beta } from "./beta/beta.js";
3
+ export {
4
+ AnthropicBeta,
5
+ BetaAPIError,
6
+ BetaAuthenticationError,
7
+ BetaError,
8
+ BetaErrorResponse,
9
+ BetaInvalidRequestError,
10
+ BetaNotFoundError,
11
+ BetaOverloadedError,
12
+ BetaPermissionError,
13
+ BetaRateLimitError,
14
+ Beta,
15
+ } from "./beta/beta.js";
4
16
  export {
5
17
  Completion,
6
18
  CompletionCreateParams,
@@ -23,6 +35,7 @@ export {
23
35
  MessageStartEvent,
24
36
  MessageStopEvent,
25
37
  MessageStreamEvent,
38
+ Metadata,
26
39
  Model,
27
40
  RawContentBlockDeltaEvent,
28
41
  RawContentBlockStartEvent,
@@ -35,6 +48,10 @@ export {
35
48
  TextBlockParam,
36
49
  TextDelta,
37
50
  Tool,
51
+ ToolChoice,
52
+ ToolChoiceAny,
53
+ ToolChoiceAuto,
54
+ ToolChoiceTool,
38
55
  ToolResultBlockParam,
39
56
  ToolUseBlock,
40
57
  ToolUseBlockParam,
@@ -11,8 +11,6 @@ export { MessageStream } from "../lib/MessageStream.js";
11
11
 
12
12
  export class Messages extends APIResource {
13
13
  /**
14
- * Create a Message.
15
- *
16
14
  * Send a structured list of input messages with text and/or image content, and the
17
15
  * model will generate the next message in the conversation.
18
16
  *
@@ -213,6 +211,17 @@ export type MessageStopEvent = RawMessageStopEvent;
213
211
 
214
212
  export type MessageStreamEvent = RawMessageStreamEvent;
215
213
 
214
+ export interface Metadata {
215
+ /**
216
+ * An external identifier for the user who is associated with the request.
217
+ *
218
+ * This should be a uuid, hash value, or other opaque identifier. Anthropic may use
219
+ * this id to help detect abuse. Do not include any identifying information such as
220
+ * name, email address, or phone number.
221
+ */
222
+ user_id?: string | null;
223
+ }
224
+
216
225
  /**
217
226
  * The model that will complete your prompt.\n\nSee
218
227
  * [models](https://docs.anthropic.com/en/docs/models-overview) for additional
@@ -365,6 +374,62 @@ export namespace Tool {
365
374
  }
366
375
  }
367
376
 
377
+ /**
378
+ * How the model should use the provided tools. The model can use a specific tool,
379
+ * any available tool, or decide by itself.
380
+ */
381
+ export type ToolChoice = ToolChoiceAuto | ToolChoiceAny | ToolChoiceTool;
382
+
383
+ /**
384
+ * The model will use any available tools.
385
+ */
386
+ export interface ToolChoiceAny {
387
+ type: 'any';
388
+
389
+ /**
390
+ * Whether to disable parallel tool use.
391
+ *
392
+ * Defaults to `false`. If set to `true`, the model will output exactly one tool
393
+ * use.
394
+ */
395
+ disable_parallel_tool_use?: boolean;
396
+ }
397
+
398
+ /**
399
+ * The model will automatically decide whether to use tools.
400
+ */
401
+ export interface ToolChoiceAuto {
402
+ type: 'auto';
403
+
404
+ /**
405
+ * Whether to disable parallel tool use.
406
+ *
407
+ * Defaults to `false`. If set to `true`, the model will output at most one tool
408
+ * use.
409
+ */
410
+ disable_parallel_tool_use?: boolean;
411
+ }
412
+
413
+ /**
414
+ * The model will use the specified tool with `tool_choice.name`.
415
+ */
416
+ export interface ToolChoiceTool {
417
+ /**
418
+ * The name of the tool to use.
419
+ */
420
+ name: string;
421
+
422
+ type: 'tool';
423
+
424
+ /**
425
+ * Whether to disable parallel tool use.
426
+ *
427
+ * Defaults to `false`. If set to `true`, the model will output exactly one tool
428
+ * use.
429
+ */
430
+ disable_parallel_tool_use?: boolean;
431
+ }
432
+
368
433
  export interface ToolResultBlockParam {
369
434
  tool_use_id: string;
370
435
 
@@ -520,7 +585,7 @@ export interface MessageCreateParamsBase {
520
585
  /**
521
586
  * An object describing metadata about the request.
522
587
  */
523
- metadata?: MessageCreateParams.Metadata;
588
+ metadata?: Metadata;
524
589
 
525
590
  /**
526
591
  * Custom text sequences that will cause the model to stop generating.
@@ -568,10 +633,7 @@ export interface MessageCreateParamsBase {
568
633
  * How the model should use the provided tools. The model can use a specific tool,
569
634
  * any available tool, or decide by itself.
570
635
  */
571
- tool_choice?:
572
- | MessageCreateParams.ToolChoiceAuto
573
- | MessageCreateParams.ToolChoiceAny
574
- | MessageCreateParams.ToolChoiceTool;
636
+ tool_choice?: ToolChoice;
575
637
 
576
638
  /**
577
639
  * Definitions of tools that the model may use.
@@ -672,44 +734,24 @@ export interface MessageCreateParamsBase {
672
734
 
673
735
  export namespace MessageCreateParams {
674
736
  /**
675
- * An object describing metadata about the request.
737
+ * @deprecated use `Anthropic.Messages.ToolChoiceAuto` instead
676
738
  */
677
- export interface Metadata {
678
- /**
679
- * An external identifier for the user who is associated with the request.
680
- *
681
- * This should be a uuid, hash value, or other opaque identifier. Anthropic may use
682
- * this id to help detect abuse. Do not include any identifying information such as
683
- * name, email address, or phone number.
684
- */
685
- user_id?: string | null;
686
- }
739
+ export type Metadata = MessagesAPI.Metadata;
687
740
 
688
741
  /**
689
- * The model will automatically decide whether to use tools.
742
+ * @deprecated use `Anthropic.Messages.ToolChoiceAuto` instead
690
743
  */
691
- export interface ToolChoiceAuto {
692
- type: 'auto';
693
- }
744
+ export type ToolChoiceAuto = MessagesAPI.ToolChoiceAuto;
694
745
 
695
746
  /**
696
- * The model will use any available tools.
747
+ * @deprecated use `Anthropic.Messages.ToolChoiceAny` instead
697
748
  */
698
- export interface ToolChoiceAny {
699
- type: 'any';
700
- }
749
+ export type ToolChoiceAny = MessagesAPI.ToolChoiceAny;
701
750
 
702
751
  /**
703
- * The model will use the specified tool with `tool_choice.name`.
752
+ * @deprecated use `Anthropic.Messages.ToolChoiceTool` instead
704
753
  */
705
- export interface ToolChoiceTool {
706
- /**
707
- * The name of the tool to use.
708
- */
709
- name: string;
710
-
711
- type: 'tool';
712
- }
754
+ export type ToolChoiceTool = MessagesAPI.ToolChoiceTool;
713
755
 
714
756
  export type MessageCreateParamsNonStreaming = MessagesAPI.MessageCreateParamsNonStreaming;
715
757
  export type MessageCreateParamsStreaming = MessagesAPI.MessageCreateParamsStreaming;
@@ -752,6 +794,7 @@ export namespace Messages {
752
794
  export import MessageStartEvent = MessagesAPI.MessageStartEvent;
753
795
  export import MessageStopEvent = MessagesAPI.MessageStopEvent;
754
796
  export import MessageStreamEvent = MessagesAPI.MessageStreamEvent;
797
+ export import Metadata = MessagesAPI.Metadata;
755
798
  export import Model = MessagesAPI.Model;
756
799
  export import RawContentBlockDeltaEvent = MessagesAPI.RawContentBlockDeltaEvent;
757
800
  export import RawContentBlockStartEvent = MessagesAPI.RawContentBlockStartEvent;
@@ -764,6 +807,10 @@ export namespace Messages {
764
807
  export import TextBlockParam = MessagesAPI.TextBlockParam;
765
808
  export import TextDelta = MessagesAPI.TextDelta;
766
809
  export import Tool = MessagesAPI.Tool;
810
+ export import ToolChoice = MessagesAPI.ToolChoice;
811
+ export import ToolChoiceAny = MessagesAPI.ToolChoiceAny;
812
+ export import ToolChoiceAuto = MessagesAPI.ToolChoiceAuto;
813
+ export import ToolChoiceTool = MessagesAPI.ToolChoiceTool;
767
814
  export import ToolResultBlockParam = MessagesAPI.ToolResultBlockParam;
768
815
  export import ToolUseBlock = MessagesAPI.ToolUseBlock;
769
816
  export import ToolUseBlockParam = MessagesAPI.ToolUseBlockParam;
package/src/streaming.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { ReadableStream, type Response } from "./_shims/index.js";
2
2
  import { AnthropicError } from "./error.js";
3
+ import { LineDecoder } from "./internal/decoders/line.js";
3
4
 
4
5
  import { createResponseHeaders } from "./core.js";
5
6
  import { APIError } from "./error.js";
@@ -345,117 +346,6 @@ class SSEDecoder {
345
346
  }
346
347
  }
347
348
 
348
- /**
349
- * A re-implementation of httpx's `LineDecoder` in Python that handles incrementally
350
- * reading lines from text.
351
- *
352
- * https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258
353
- */
354
- class LineDecoder {
355
- // prettier-ignore
356
- static NEWLINE_CHARS = new Set(['\n', '\r']);
357
- static NEWLINE_REGEXP = /\r\n|[\n\r]/g;
358
-
359
- buffer: string[];
360
- trailingCR: boolean;
361
- textDecoder: any; // TextDecoder found in browsers; not typed to avoid pulling in either "dom" or "node" types.
362
-
363
- constructor() {
364
- this.buffer = [];
365
- this.trailingCR = false;
366
- }
367
-
368
- decode(chunk: Bytes): string[] {
369
- let text = this.decodeText(chunk);
370
-
371
- if (this.trailingCR) {
372
- text = '\r' + text;
373
- this.trailingCR = false;
374
- }
375
- if (text.endsWith('\r')) {
376
- this.trailingCR = true;
377
- text = text.slice(0, -1);
378
- }
379
-
380
- if (!text) {
381
- return [];
382
- }
383
-
384
- const trailingNewline = LineDecoder.NEWLINE_CHARS.has(text[text.length - 1] || '');
385
- let lines = text.split(LineDecoder.NEWLINE_REGEXP);
386
-
387
- // if there is a trailing new line then the last entry will be an empty
388
- // string which we don't care about
389
- if (trailingNewline) {
390
- lines.pop();
391
- }
392
-
393
- if (lines.length === 1 && !trailingNewline) {
394
- this.buffer.push(lines[0]!);
395
- return [];
396
- }
397
-
398
- if (this.buffer.length > 0) {
399
- lines = [this.buffer.join('') + lines[0], ...lines.slice(1)];
400
- this.buffer = [];
401
- }
402
-
403
- if (!trailingNewline) {
404
- this.buffer = [lines.pop() || ''];
405
- }
406
-
407
- return lines;
408
- }
409
-
410
- decodeText(bytes: Bytes): string {
411
- if (bytes == null) return '';
412
- if (typeof bytes === 'string') return bytes;
413
-
414
- // Node:
415
- if (typeof Buffer !== 'undefined') {
416
- if (bytes instanceof Buffer) {
417
- return bytes.toString();
418
- }
419
- if (bytes instanceof Uint8Array) {
420
- return Buffer.from(bytes).toString();
421
- }
422
-
423
- throw new AnthropicError(
424
- `Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.`,
425
- );
426
- }
427
-
428
- // Browser
429
- if (typeof TextDecoder !== 'undefined') {
430
- if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
431
- this.textDecoder ??= new TextDecoder('utf8');
432
- return this.textDecoder.decode(bytes);
433
- }
434
-
435
- throw new AnthropicError(
436
- `Unexpected: received non-Uint8Array/ArrayBuffer (${
437
- (bytes as any).constructor.name
438
- }) in a web platform. Please report this error.`,
439
- );
440
- }
441
-
442
- throw new AnthropicError(
443
- `Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.`,
444
- );
445
- }
446
-
447
- flush(): string[] {
448
- if (!this.buffer.length && !this.trailingCR) {
449
- return [];
450
- }
451
-
452
- const lines = [this.buffer.join('')];
453
- this.buffer = [];
454
- this.trailingCR = false;
455
- return lines;
456
- }
457
- }
458
-
459
349
  /** This is an internal helper function that's just used for testing */
460
350
  export function _decodeChunks(chunks: string[]): string[] {
461
351
  const decoder = new LineDecoder();
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '0.27.3'; // x-release-please-version
1
+ export const VERSION = '0.29.0'; // x-release-please-version
@@ -1 +1 @@
1
- {"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,QAAQ,EAAE,MAA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