@anthropic-ai/sdk 0.36.3 → 0.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -0
- package/README.md +30 -3
- package/core.d.ts +1 -0
- package/core.d.ts.map +1 -1
- package/core.js +33 -6
- package/core.js.map +1 -1
- package/core.mjs +33 -6
- package/core.mjs.map +1 -1
- package/index.d.mts +2 -2
- package/index.d.ts +2 -2
- package/index.d.ts.map +1 -1
- package/index.js.map +1 -1
- package/index.mjs.map +1 -1
- package/internal/decoders/line.d.ts +3 -2
- package/internal/decoders/line.d.ts.map +1 -1
- package/internal/decoders/line.js +97 -35
- package/internal/decoders/line.js.map +1 -1
- package/internal/decoders/line.mjs +95 -34
- package/internal/decoders/line.mjs.map +1 -1
- package/lib/BetaMessageStream.d.ts +2 -0
- package/lib/BetaMessageStream.d.ts.map +1 -1
- package/lib/BetaMessageStream.js +24 -0
- package/lib/BetaMessageStream.js.map +1 -1
- package/lib/BetaMessageStream.mjs +24 -0
- package/lib/BetaMessageStream.mjs.map +1 -1
- package/lib/MessageStream.d.ts +2 -0
- package/lib/MessageStream.d.ts.map +1 -1
- package/lib/MessageStream.js +24 -0
- package/lib/MessageStream.js.map +1 -1
- package/lib/MessageStream.mjs +24 -0
- package/lib/MessageStream.mjs.map +1 -1
- package/package.json +29 -8
- package/pagination.d.ts +1 -0
- package/pagination.d.ts.map +1 -1
- package/pagination.js +6 -0
- package/pagination.js.map +1 -1
- package/pagination.mjs +6 -0
- package/pagination.mjs.map +1 -1
- package/resources/beta/beta.d.ts +3 -3
- package/resources/beta/beta.d.ts.map +1 -1
- package/resources/beta/beta.js.map +1 -1
- package/resources/beta/beta.mjs.map +1 -1
- package/resources/beta/index.d.ts +1 -1
- package/resources/beta/index.d.ts.map +1 -1
- package/resources/beta/index.js.map +1 -1
- package/resources/beta/index.mjs.map +1 -1
- package/resources/beta/messages/batches.d.ts +18 -0
- package/resources/beta/messages/batches.d.ts.map +1 -1
- package/resources/beta/messages/batches.js +3 -0
- package/resources/beta/messages/batches.js.map +1 -1
- package/resources/beta/messages/batches.mjs +3 -0
- package/resources/beta/messages/batches.mjs.map +1 -1
- package/resources/beta/messages/index.d.ts +1 -1
- package/resources/beta/messages/index.d.ts.map +1 -1
- package/resources/beta/messages/index.js.map +1 -1
- package/resources/beta/messages/index.mjs.map +1 -1
- package/resources/beta/messages/messages.d.ts +163 -22
- package/resources/beta/messages/messages.d.ts.map +1 -1
- package/resources/beta/messages/messages.js +5 -1
- package/resources/beta/messages/messages.js.map +1 -1
- package/resources/beta/messages/messages.mjs +5 -1
- package/resources/beta/messages/messages.mjs.map +1 -1
- package/resources/index.d.ts +1 -1
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js.map +1 -1
- package/resources/index.mjs.map +1 -1
- package/resources/messages/batches.d.ts +18 -0
- package/resources/messages/batches.d.ts.map +1 -1
- package/resources/messages/batches.js +15 -0
- package/resources/messages/batches.js.map +1 -1
- package/resources/messages/batches.mjs +15 -0
- package/resources/messages/batches.mjs.map +1 -1
- package/resources/messages/index.d.ts +1 -1
- package/resources/messages/index.d.ts.map +1 -1
- package/resources/messages/index.js.map +1 -1
- package/resources/messages/index.mjs.map +1 -1
- package/resources/messages/messages.d.ts +144 -23
- package/resources/messages/messages.d.ts.map +1 -1
- package/resources/messages/messages.js +5 -1
- package/resources/messages/messages.js.map +1 -1
- package/resources/messages/messages.mjs +5 -1
- package/resources/messages/messages.mjs.map +1 -1
- package/src/core.ts +39 -6
- package/src/index.ts +32 -0
- package/src/internal/decoders/line.ts +100 -38
- package/src/lib/BetaMessageStream.ts +26 -0
- package/src/lib/MessageStream.ts +26 -0
- package/src/pagination.ts +8 -0
- package/src/resources/beta/beta.ts +34 -1
- package/src/resources/beta/index.ts +15 -0
- package/src/resources/beta/messages/batches.ts +18 -0
- package/src/resources/beta/messages/index.ts +15 -0
- package/src/resources/beta/messages/messages.ts +236 -25
- package/src/resources/index.ts +16 -0
- package/src/resources/messages/batches.ts +18 -0
- package/src/resources/messages/index.ts +16 -0
- package/src/resources/messages/messages.ts +202 -27
- package/src/streaming.ts +1 -43
- package/src/version.ts +1 -1
- package/streaming.d.ts +0 -2
- package/streaming.d.ts.map +1 -1
- package/streaming.js +2 -38
- package/streaming.js.map +1 -1
- package/streaming.mjs +1 -36
- package/streaming.mjs.map +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
|
@@ -34,6 +34,8 @@ export class Messages extends APIResource {
|
|
|
34
34
|
*
|
|
35
35
|
* The Messages API can be used for either single queries or stateless multi-turn
|
|
36
36
|
* conversations.
|
|
37
|
+
*
|
|
38
|
+
* Learn more about the Messages API in our [user guide](/en/docs/initial-setup)
|
|
37
39
|
*/
|
|
38
40
|
create(body: MessageCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<Message>;
|
|
39
41
|
create(
|
|
@@ -57,7 +59,9 @@ export class Messages extends APIResource {
|
|
|
57
59
|
}
|
|
58
60
|
return this._client.post('/v1/messages', {
|
|
59
61
|
body,
|
|
60
|
-
timeout:
|
|
62
|
+
timeout:
|
|
63
|
+
(this._client as any)._options.timeout ??
|
|
64
|
+
(body.stream ? 600000 : this._client._calculateNonstreamingTimeout(body.max_tokens)),
|
|
61
65
|
...options,
|
|
62
66
|
stream: body.stream ?? false,
|
|
63
67
|
}) as APIPromise<Message> | APIPromise<Stream<RawMessageStreamEvent>>;
|
|
@@ -75,6 +79,9 @@ export class Messages extends APIResource {
|
|
|
75
79
|
*
|
|
76
80
|
* The Token Count API can be used to count the number of tokens in a Message,
|
|
77
81
|
* including tools, images, and documents, without creating it.
|
|
82
|
+
*
|
|
83
|
+
* Learn more about token counting in our
|
|
84
|
+
* [user guide](/en/docs/build-with-claude/token-counting)
|
|
78
85
|
*/
|
|
79
86
|
countTokens(
|
|
80
87
|
body: MessageCountTokensParams,
|
|
@@ -84,6 +91,14 @@ export class Messages extends APIResource {
|
|
|
84
91
|
}
|
|
85
92
|
}
|
|
86
93
|
|
|
94
|
+
export interface Base64ImageSource {
|
|
95
|
+
data: string;
|
|
96
|
+
|
|
97
|
+
media_type: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp';
|
|
98
|
+
|
|
99
|
+
type: 'base64';
|
|
100
|
+
}
|
|
101
|
+
|
|
87
102
|
export interface Base64PDFSource {
|
|
88
103
|
data: string;
|
|
89
104
|
|
|
@@ -190,7 +205,7 @@ export interface CitationsDelta {
|
|
|
190
205
|
type: 'citations_delta';
|
|
191
206
|
}
|
|
192
207
|
|
|
193
|
-
export type ContentBlock = TextBlock | ToolUseBlock;
|
|
208
|
+
export type ContentBlock = TextBlock | ToolUseBlock | ThinkingBlock | RedactedThinkingBlock;
|
|
194
209
|
|
|
195
210
|
export type ContentBlockDeltaEvent = RawContentBlockDeltaEvent;
|
|
196
211
|
|
|
@@ -199,7 +214,9 @@ export type ContentBlockParam =
|
|
|
199
214
|
| ImageBlockParam
|
|
200
215
|
| ToolUseBlockParam
|
|
201
216
|
| ToolResultBlockParam
|
|
202
|
-
| DocumentBlockParam
|
|
217
|
+
| DocumentBlockParam
|
|
218
|
+
| ThinkingBlockParam
|
|
219
|
+
| RedactedThinkingBlockParam;
|
|
203
220
|
|
|
204
221
|
export interface ContentBlockSource {
|
|
205
222
|
content: string | Array<ContentBlockSourceContent>;
|
|
@@ -214,7 +231,7 @@ export type ContentBlockStartEvent = RawContentBlockStartEvent;
|
|
|
214
231
|
export type ContentBlockStopEvent = RawContentBlockStopEvent;
|
|
215
232
|
|
|
216
233
|
export interface DocumentBlockParam {
|
|
217
|
-
source: Base64PDFSource | PlainTextSource | ContentBlockSource;
|
|
234
|
+
source: Base64PDFSource | PlainTextSource | ContentBlockSource | URLPDFSource;
|
|
218
235
|
|
|
219
236
|
type: 'document';
|
|
220
237
|
|
|
@@ -228,23 +245,13 @@ export interface DocumentBlockParam {
|
|
|
228
245
|
}
|
|
229
246
|
|
|
230
247
|
export interface ImageBlockParam {
|
|
231
|
-
source:
|
|
248
|
+
source: Base64ImageSource | URLImageSource;
|
|
232
249
|
|
|
233
250
|
type: 'image';
|
|
234
251
|
|
|
235
252
|
cache_control?: CacheControlEphemeral | null;
|
|
236
253
|
}
|
|
237
254
|
|
|
238
|
-
export namespace ImageBlockParam {
|
|
239
|
-
export interface Source {
|
|
240
|
-
data: string;
|
|
241
|
-
|
|
242
|
-
media_type: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp';
|
|
243
|
-
|
|
244
|
-
type: 'base64';
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
255
|
export type InputJsonDelta = InputJSONDelta;
|
|
249
256
|
|
|
250
257
|
export interface InputJSONDelta {
|
|
@@ -354,10 +361,15 @@ export interface Message {
|
|
|
354
361
|
*
|
|
355
362
|
* For example, `output_tokens` will be non-zero, even for an empty string response
|
|
356
363
|
* from Claude.
|
|
364
|
+
*
|
|
365
|
+
* Total input tokens in a request is the summation of `input_tokens`,
|
|
366
|
+
* `cache_creation_input_tokens`, and `cache_read_input_tokens`.
|
|
357
367
|
*/
|
|
358
368
|
usage: Usage;
|
|
359
369
|
}
|
|
360
370
|
|
|
371
|
+
export type MessageCountTokensTool = Tool | ToolBash20250124 | ToolTextEditor20250124;
|
|
372
|
+
|
|
361
373
|
export type MessageDeltaEvent = RawMessageDeltaEvent;
|
|
362
374
|
|
|
363
375
|
export interface MessageDeltaUsage {
|
|
@@ -404,7 +416,8 @@ export interface Metadata {
|
|
|
404
416
|
* details and options.
|
|
405
417
|
*/
|
|
406
418
|
export type Model =
|
|
407
|
-
|
|
|
419
|
+
| 'claude-3-7-sonnet-latest'
|
|
420
|
+
| 'claude-3-7-sonnet-20250219'
|
|
408
421
|
| 'claude-3-5-haiku-latest'
|
|
409
422
|
| 'claude-3-5-haiku-20241022'
|
|
410
423
|
| 'claude-3-5-sonnet-latest'
|
|
@@ -415,7 +428,8 @@ export type Model =
|
|
|
415
428
|
| 'claude-3-sonnet-20240229'
|
|
416
429
|
| 'claude-3-haiku-20240307'
|
|
417
430
|
| 'claude-2.1'
|
|
418
|
-
| 'claude-2.0'
|
|
431
|
+
| 'claude-2.0'
|
|
432
|
+
| (string & {});
|
|
419
433
|
|
|
420
434
|
const DEPRECATED_MODELS: {
|
|
421
435
|
[K in Model]?: string;
|
|
@@ -439,7 +453,7 @@ export interface PlainTextSource {
|
|
|
439
453
|
}
|
|
440
454
|
|
|
441
455
|
export interface RawContentBlockDeltaEvent {
|
|
442
|
-
delta: TextDelta | InputJSONDelta | CitationsDelta;
|
|
456
|
+
delta: TextDelta | InputJSONDelta | CitationsDelta | ThinkingDelta | SignatureDelta;
|
|
443
457
|
|
|
444
458
|
index: number;
|
|
445
459
|
|
|
@@ -447,7 +461,7 @@ export interface RawContentBlockDeltaEvent {
|
|
|
447
461
|
}
|
|
448
462
|
|
|
449
463
|
export interface RawContentBlockStartEvent {
|
|
450
|
-
content_block: TextBlock | ToolUseBlock;
|
|
464
|
+
content_block: TextBlock | ToolUseBlock | ThinkingBlock | RedactedThinkingBlock;
|
|
451
465
|
|
|
452
466
|
index: number;
|
|
453
467
|
|
|
@@ -478,6 +492,9 @@ export interface RawMessageDeltaEvent {
|
|
|
478
492
|
*
|
|
479
493
|
* For example, `output_tokens` will be non-zero, even for an empty string response
|
|
480
494
|
* from Claude.
|
|
495
|
+
*
|
|
496
|
+
* Total input tokens in a request is the summation of `input_tokens`,
|
|
497
|
+
* `cache_creation_input_tokens`, and `cache_read_input_tokens`.
|
|
481
498
|
*/
|
|
482
499
|
usage: MessageDeltaUsage;
|
|
483
500
|
}
|
|
@@ -508,6 +525,24 @@ export type RawMessageStreamEvent =
|
|
|
508
525
|
| RawContentBlockDeltaEvent
|
|
509
526
|
| RawContentBlockStopEvent;
|
|
510
527
|
|
|
528
|
+
export interface RedactedThinkingBlock {
|
|
529
|
+
data: string;
|
|
530
|
+
|
|
531
|
+
type: 'redacted_thinking';
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
export interface RedactedThinkingBlockParam {
|
|
535
|
+
data: string;
|
|
536
|
+
|
|
537
|
+
type: 'redacted_thinking';
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
export interface SignatureDelta {
|
|
541
|
+
signature: string;
|
|
542
|
+
|
|
543
|
+
type: 'signature_delta';
|
|
544
|
+
}
|
|
545
|
+
|
|
511
546
|
export interface TextBlock {
|
|
512
547
|
/**
|
|
513
548
|
* Citations supporting the text block.
|
|
@@ -546,9 +581,65 @@ export interface TextDelta {
|
|
|
546
581
|
type: 'text_delta';
|
|
547
582
|
}
|
|
548
583
|
|
|
584
|
+
export interface ThinkingBlock {
|
|
585
|
+
signature: string;
|
|
586
|
+
|
|
587
|
+
thinking: string;
|
|
588
|
+
|
|
589
|
+
type: 'thinking';
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
export interface ThinkingBlockParam {
|
|
593
|
+
signature: string;
|
|
594
|
+
|
|
595
|
+
thinking: string;
|
|
596
|
+
|
|
597
|
+
type: 'thinking';
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
export interface ThinkingConfigDisabled {
|
|
601
|
+
type: 'disabled';
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
export interface ThinkingConfigEnabled {
|
|
605
|
+
/**
|
|
606
|
+
* Determines how many tokens Claude can use for its internal reasoning process.
|
|
607
|
+
* Larger budgets can enable more thorough analysis for complex problems, improving
|
|
608
|
+
* response quality.
|
|
609
|
+
*
|
|
610
|
+
* Must be ≥1024 and less than `max_tokens`.
|
|
611
|
+
*
|
|
612
|
+
* See
|
|
613
|
+
* [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)
|
|
614
|
+
* for details.
|
|
615
|
+
*/
|
|
616
|
+
budget_tokens: number;
|
|
617
|
+
|
|
618
|
+
type: 'enabled';
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Configuration for enabling Claude's extended thinking.
|
|
623
|
+
*
|
|
624
|
+
* When enabled, responses include `thinking` content blocks showing Claude's
|
|
625
|
+
* thinking process before the final answer. Requires a minimum budget of 1,024
|
|
626
|
+
* tokens and counts towards your `max_tokens` limit.
|
|
627
|
+
*
|
|
628
|
+
* See
|
|
629
|
+
* [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)
|
|
630
|
+
* for details.
|
|
631
|
+
*/
|
|
632
|
+
export type ThinkingConfigParam = ThinkingConfigEnabled | ThinkingConfigDisabled;
|
|
633
|
+
|
|
634
|
+
export interface ThinkingDelta {
|
|
635
|
+
thinking: string;
|
|
636
|
+
|
|
637
|
+
type: 'thinking_delta';
|
|
638
|
+
}
|
|
639
|
+
|
|
549
640
|
export interface Tool {
|
|
550
641
|
/**
|
|
551
|
-
* [JSON schema](https://json-schema.org/) for this tool's input.
|
|
642
|
+
* [JSON schema](https://json-schema.org/draft/2020-12) for this tool's input.
|
|
552
643
|
*
|
|
553
644
|
* This defines the shape of the `input` that your tool accepts and that the model
|
|
554
645
|
* will produce.
|
|
@@ -577,7 +668,7 @@ export interface Tool {
|
|
|
577
668
|
|
|
578
669
|
export namespace Tool {
|
|
579
670
|
/**
|
|
580
|
-
* [JSON schema](https://json-schema.org/) for this tool's input.
|
|
671
|
+
* [JSON schema](https://json-schema.org/draft/2020-12) for this tool's input.
|
|
581
672
|
*
|
|
582
673
|
* This defines the shape of the `input` that your tool accepts and that the model
|
|
583
674
|
* will produce.
|
|
@@ -590,6 +681,19 @@ export namespace Tool {
|
|
|
590
681
|
}
|
|
591
682
|
}
|
|
592
683
|
|
|
684
|
+
export interface ToolBash20250124 {
|
|
685
|
+
/**
|
|
686
|
+
* Name of the tool.
|
|
687
|
+
*
|
|
688
|
+
* This is how the tool will be called by the model and in tool_use blocks.
|
|
689
|
+
*/
|
|
690
|
+
name: 'bash';
|
|
691
|
+
|
|
692
|
+
type: 'bash_20250124';
|
|
693
|
+
|
|
694
|
+
cache_control?: CacheControlEphemeral | null;
|
|
695
|
+
}
|
|
696
|
+
|
|
593
697
|
/**
|
|
594
698
|
* How the model should use the provided tools. The model can use a specific tool,
|
|
595
699
|
* any available tool, or decide by itself.
|
|
@@ -658,6 +762,21 @@ export interface ToolResultBlockParam {
|
|
|
658
762
|
is_error?: boolean;
|
|
659
763
|
}
|
|
660
764
|
|
|
765
|
+
export interface ToolTextEditor20250124 {
|
|
766
|
+
/**
|
|
767
|
+
* Name of the tool.
|
|
768
|
+
*
|
|
769
|
+
* This is how the tool will be called by the model and in tool_use blocks.
|
|
770
|
+
*/
|
|
771
|
+
name: 'str_replace_editor';
|
|
772
|
+
|
|
773
|
+
type: 'text_editor_20250124';
|
|
774
|
+
|
|
775
|
+
cache_control?: CacheControlEphemeral | null;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
export type ToolUnion = Tool | ToolBash20250124 | ToolTextEditor20250124;
|
|
779
|
+
|
|
661
780
|
export interface ToolUseBlock {
|
|
662
781
|
id: string;
|
|
663
782
|
|
|
@@ -680,6 +799,18 @@ export interface ToolUseBlockParam {
|
|
|
680
799
|
cache_control?: CacheControlEphemeral | null;
|
|
681
800
|
}
|
|
682
801
|
|
|
802
|
+
export interface URLImageSource {
|
|
803
|
+
type: 'url';
|
|
804
|
+
|
|
805
|
+
url: string;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
export interface URLPDFSource {
|
|
809
|
+
type: 'url';
|
|
810
|
+
|
|
811
|
+
url: string;
|
|
812
|
+
}
|
|
813
|
+
|
|
683
814
|
export interface Usage {
|
|
684
815
|
/**
|
|
685
816
|
* The number of input tokens used to create the cache entry.
|
|
@@ -860,6 +991,19 @@ export interface MessageCreateParamsBase {
|
|
|
860
991
|
*/
|
|
861
992
|
temperature?: number;
|
|
862
993
|
|
|
994
|
+
/**
|
|
995
|
+
* Configuration for enabling Claude's extended thinking.
|
|
996
|
+
*
|
|
997
|
+
* When enabled, responses include `thinking` content blocks showing Claude's
|
|
998
|
+
* thinking process before the final answer. Requires a minimum budget of 1,024
|
|
999
|
+
* tokens and counts towards your `max_tokens` limit.
|
|
1000
|
+
*
|
|
1001
|
+
* See
|
|
1002
|
+
* [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)
|
|
1003
|
+
* for details.
|
|
1004
|
+
*/
|
|
1005
|
+
thinking?: ThinkingConfigParam;
|
|
1006
|
+
|
|
863
1007
|
/**
|
|
864
1008
|
* How the model should use the provided tools. The model can use a specific tool,
|
|
865
1009
|
* any available tool, or decide by itself.
|
|
@@ -878,8 +1022,9 @@ export interface MessageCreateParamsBase {
|
|
|
878
1022
|
*
|
|
879
1023
|
* - `name`: Name of the tool.
|
|
880
1024
|
* - `description`: Optional, but strongly-recommended description of the tool.
|
|
881
|
-
* - `input_schema`: [JSON schema](https://json-schema.org/) for the
|
|
882
|
-
* shape that the model will produce in `tool_use` output content
|
|
1025
|
+
* - `input_schema`: [JSON schema](https://json-schema.org/draft/2020-12) for the
|
|
1026
|
+
* tool `input` shape that the model will produce in `tool_use` output content
|
|
1027
|
+
* blocks.
|
|
883
1028
|
*
|
|
884
1029
|
* For example, if you defined `tools` as:
|
|
885
1030
|
*
|
|
@@ -936,7 +1081,7 @@ export interface MessageCreateParamsBase {
|
|
|
936
1081
|
*
|
|
937
1082
|
* See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details.
|
|
938
1083
|
*/
|
|
939
|
-
tools?: Array<
|
|
1084
|
+
tools?: Array<ToolUnion>;
|
|
940
1085
|
|
|
941
1086
|
/**
|
|
942
1087
|
* Only sample from the top K options for each subsequent token.
|
|
@@ -1117,6 +1262,19 @@ export interface MessageCountTokensParams {
|
|
|
1117
1262
|
*/
|
|
1118
1263
|
system?: string | Array<TextBlockParam>;
|
|
1119
1264
|
|
|
1265
|
+
/**
|
|
1266
|
+
* Configuration for enabling Claude's extended thinking.
|
|
1267
|
+
*
|
|
1268
|
+
* When enabled, responses include `thinking` content blocks showing Claude's
|
|
1269
|
+
* thinking process before the final answer. Requires a minimum budget of 1,024
|
|
1270
|
+
* tokens and counts towards your `max_tokens` limit.
|
|
1271
|
+
*
|
|
1272
|
+
* See
|
|
1273
|
+
* [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)
|
|
1274
|
+
* for details.
|
|
1275
|
+
*/
|
|
1276
|
+
thinking?: ThinkingConfigParam;
|
|
1277
|
+
|
|
1120
1278
|
/**
|
|
1121
1279
|
* How the model should use the provided tools. The model can use a specific tool,
|
|
1122
1280
|
* any available tool, or decide by itself.
|
|
@@ -1135,8 +1293,9 @@ export interface MessageCountTokensParams {
|
|
|
1135
1293
|
*
|
|
1136
1294
|
* - `name`: Name of the tool.
|
|
1137
1295
|
* - `description`: Optional, but strongly-recommended description of the tool.
|
|
1138
|
-
* - `input_schema`: [JSON schema](https://json-schema.org/) for the
|
|
1139
|
-
* shape that the model will produce in `tool_use` output content
|
|
1296
|
+
* - `input_schema`: [JSON schema](https://json-schema.org/draft/2020-12) for the
|
|
1297
|
+
* tool `input` shape that the model will produce in `tool_use` output content
|
|
1298
|
+
* blocks.
|
|
1140
1299
|
*
|
|
1141
1300
|
* For example, if you defined `tools` as:
|
|
1142
1301
|
*
|
|
@@ -1193,7 +1352,7 @@ export interface MessageCountTokensParams {
|
|
|
1193
1352
|
*
|
|
1194
1353
|
* See our [guide](https://docs.anthropic.com/en/docs/tool-use) for more details.
|
|
1195
1354
|
*/
|
|
1196
|
-
tools?: Array<
|
|
1355
|
+
tools?: Array<MessageCountTokensTool>;
|
|
1197
1356
|
}
|
|
1198
1357
|
|
|
1199
1358
|
Messages.Batches = Batches;
|
|
@@ -1201,6 +1360,7 @@ Messages.MessageBatchesPage = MessageBatchesPage;
|
|
|
1201
1360
|
|
|
1202
1361
|
export declare namespace Messages {
|
|
1203
1362
|
export {
|
|
1363
|
+
type Base64ImageSource as Base64ImageSource,
|
|
1204
1364
|
type Base64PDFSource as Base64PDFSource,
|
|
1205
1365
|
type CacheControlEphemeral as CacheControlEphemeral,
|
|
1206
1366
|
type CitationCharLocation as CitationCharLocation,
|
|
@@ -1223,6 +1383,7 @@ export declare namespace Messages {
|
|
|
1223
1383
|
type InputJsonDelta as InputJsonDelta,
|
|
1224
1384
|
type InputJSONDelta as InputJSONDelta,
|
|
1225
1385
|
type Message as Message,
|
|
1386
|
+
type MessageCountTokensTool as MessageCountTokensTool,
|
|
1226
1387
|
type MessageDeltaEvent as MessageDeltaEvent,
|
|
1227
1388
|
type MessageDeltaUsage as MessageDeltaUsage,
|
|
1228
1389
|
type MessageParam as MessageParam,
|
|
@@ -1240,19 +1401,33 @@ export declare namespace Messages {
|
|
|
1240
1401
|
type RawMessageStartEvent as RawMessageStartEvent,
|
|
1241
1402
|
type RawMessageStopEvent as RawMessageStopEvent,
|
|
1242
1403
|
type RawMessageStreamEvent as RawMessageStreamEvent,
|
|
1404
|
+
type RedactedThinkingBlock as RedactedThinkingBlock,
|
|
1405
|
+
type RedactedThinkingBlockParam as RedactedThinkingBlockParam,
|
|
1406
|
+
type SignatureDelta as SignatureDelta,
|
|
1243
1407
|
type TextBlock as TextBlock,
|
|
1244
1408
|
type TextBlockParam as TextBlockParam,
|
|
1245
1409
|
type TextCitation as TextCitation,
|
|
1246
1410
|
type TextCitationParam as TextCitationParam,
|
|
1247
1411
|
type TextDelta as TextDelta,
|
|
1412
|
+
type ThinkingBlock as ThinkingBlock,
|
|
1413
|
+
type ThinkingBlockParam as ThinkingBlockParam,
|
|
1414
|
+
type ThinkingConfigDisabled as ThinkingConfigDisabled,
|
|
1415
|
+
type ThinkingConfigEnabled as ThinkingConfigEnabled,
|
|
1416
|
+
type ThinkingConfigParam as ThinkingConfigParam,
|
|
1417
|
+
type ThinkingDelta as ThinkingDelta,
|
|
1248
1418
|
type Tool as Tool,
|
|
1419
|
+
type ToolBash20250124 as ToolBash20250124,
|
|
1249
1420
|
type ToolChoice as ToolChoice,
|
|
1250
1421
|
type ToolChoiceAny as ToolChoiceAny,
|
|
1251
1422
|
type ToolChoiceAuto as ToolChoiceAuto,
|
|
1252
1423
|
type ToolChoiceTool as ToolChoiceTool,
|
|
1253
1424
|
type ToolResultBlockParam as ToolResultBlockParam,
|
|
1425
|
+
type ToolTextEditor20250124 as ToolTextEditor20250124,
|
|
1426
|
+
type ToolUnion as ToolUnion,
|
|
1254
1427
|
type ToolUseBlock as ToolUseBlock,
|
|
1255
1428
|
type ToolUseBlockParam as ToolUseBlockParam,
|
|
1429
|
+
type URLImageSource as URLImageSource,
|
|
1430
|
+
type URLPDFSource as URLPDFSource,
|
|
1256
1431
|
type Usage as Usage,
|
|
1257
1432
|
type MessageCreateParams as MessageCreateParams,
|
|
1258
1433
|
type MessageCreateParamsNonStreaming as MessageCreateParamsNonStreaming,
|
package/src/streaming.ts
CHANGED
|
@@ -1,6 +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
|
+
import { findDoubleNewlineIndex, LineDecoder } from "./internal/decoders/line.js";
|
|
4
4
|
import { ReadableStreamToAsyncIterable } from "./internal/stream-utils.js";
|
|
5
5
|
|
|
6
6
|
import { createResponseHeaders } from "./core.js";
|
|
@@ -261,37 +261,6 @@ async function* iterSSEChunks(iterator: AsyncIterableIterator<Bytes>): AsyncGene
|
|
|
261
261
|
}
|
|
262
262
|
}
|
|
263
263
|
|
|
264
|
-
function findDoubleNewlineIndex(buffer: Uint8Array): number {
|
|
265
|
-
// This function searches the buffer for the end patterns (\r\r, \n\n, \r\n\r\n)
|
|
266
|
-
// and returns the index right after the first occurrence of any pattern,
|
|
267
|
-
// or -1 if none of the patterns are found.
|
|
268
|
-
const newline = 0x0a; // \n
|
|
269
|
-
const carriage = 0x0d; // \r
|
|
270
|
-
|
|
271
|
-
for (let i = 0; i < buffer.length - 2; i++) {
|
|
272
|
-
if (buffer[i] === newline && buffer[i + 1] === newline) {
|
|
273
|
-
// \n\n
|
|
274
|
-
return i + 2;
|
|
275
|
-
}
|
|
276
|
-
if (buffer[i] === carriage && buffer[i + 1] === carriage) {
|
|
277
|
-
// \r\r
|
|
278
|
-
return i + 2;
|
|
279
|
-
}
|
|
280
|
-
if (
|
|
281
|
-
buffer[i] === carriage &&
|
|
282
|
-
buffer[i + 1] === newline &&
|
|
283
|
-
i + 3 < buffer.length &&
|
|
284
|
-
buffer[i + 2] === carriage &&
|
|
285
|
-
buffer[i + 3] === newline
|
|
286
|
-
) {
|
|
287
|
-
// \r\n\r\n
|
|
288
|
-
return i + 4;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
return -1;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
264
|
class SSEDecoder {
|
|
296
265
|
private data: string[];
|
|
297
266
|
private event: string | null;
|
|
@@ -347,17 +316,6 @@ class SSEDecoder {
|
|
|
347
316
|
}
|
|
348
317
|
}
|
|
349
318
|
|
|
350
|
-
/** This is an internal helper function that's just used for testing */
|
|
351
|
-
export function _decodeChunks(chunks: string[]): string[] {
|
|
352
|
-
const decoder = new LineDecoder();
|
|
353
|
-
const lines: string[] = [];
|
|
354
|
-
for (const chunk of chunks) {
|
|
355
|
-
lines.push(...decoder.decode(chunk));
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
return lines;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
319
|
function partition(str: string, delimiter: string): [string, string, string] {
|
|
362
320
|
const index = str.indexOf(delimiter);
|
|
363
321
|
if (index !== -1) {
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '0.
|
|
1
|
+
export const VERSION = '0.38.0'; // x-release-please-version
|
package/streaming.d.ts
CHANGED
|
@@ -29,6 +29,4 @@ export declare class Stream<Item> implements AsyncIterable<Item> {
|
|
|
29
29
|
toReadableStream(): ReadableStream;
|
|
30
30
|
}
|
|
31
31
|
export declare function _iterSSEMessages(response: Response, controller: AbortController): AsyncGenerator<ServerSentEvent, void, unknown>;
|
|
32
|
-
/** This is an internal helper function that's just used for testing */
|
|
33
|
-
export declare function _decodeChunks(chunks: string[]): string[];
|
|
34
32
|
//# sourceMappingURL=streaming.d.ts.map
|
package/streaming.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAU/D,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,EAAE,CAAC;CACf,CAAC;AAEF,qBAAa,MAAM,CAAC,IAAI,CAAE,YAAW,aAAa,CAAC,IAAI,CAAC;IAIpD,OAAO,CAAC,QAAQ;IAHlB,UAAU,EAAE,eAAe,CAAC;gBAGlB,QAAQ,EAAE,MAAM,aAAa,CAAC,IAAI,CAAC,EAC3C,UAAU,EAAE,eAAe;IAK7B,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC;IAiE3F;;;OAGG;IACH,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE,cAAc,EAAE,cAAc,EAAE,UAAU,EAAE,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC;IA2C1G,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC;IAI7C;;;OAGG;IACH,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IAwBnC;;;;OAIG;IACH,gBAAgB,IAAI,cAAc;CA0BnC;AAED,wBAAuB,gBAAgB,CACrC,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,eAAe,GAC1B,cAAc,CAAC,eAAe,EAAE,IAAI,EAAE,OAAO,CAAC,CAqBhD
|
|
1
|
+
{"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAU/D,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,EAAE,CAAC;CACf,CAAC;AAEF,qBAAa,MAAM,CAAC,IAAI,CAAE,YAAW,aAAa,CAAC,IAAI,CAAC;IAIpD,OAAO,CAAC,QAAQ;IAHlB,UAAU,EAAE,eAAe,CAAC;gBAGlB,QAAQ,EAAE,MAAM,aAAa,CAAC,IAAI,CAAC,EAC3C,UAAU,EAAE,eAAe;IAK7B,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC;IAiE3F;;;OAGG;IACH,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAAE,cAAc,EAAE,cAAc,EAAE,UAAU,EAAE,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC;IA2C1G,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC;IAI7C;;;OAGG;IACH,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IAwBnC;;;;OAIG;IACH,gBAAgB,IAAI,cAAc;CA0BnC;AAED,wBAAuB,gBAAgB,CACrC,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,eAAe,GAC1B,cAAc,CAAC,eAAe,EAAE,IAAI,EAAE,OAAO,CAAC,CAqBhD"}
|
package/streaming.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports._iterSSEMessages = exports.Stream = void 0;
|
|
4
4
|
const index_1 = require("./_shims/index.js");
|
|
5
5
|
const error_1 = require("./error.js");
|
|
6
6
|
const line_1 = require("./internal/decoders/line.js");
|
|
@@ -217,7 +217,7 @@ async function* iterSSEChunks(iterator) {
|
|
|
217
217
|
newData.set(binaryChunk, data.length);
|
|
218
218
|
data = newData;
|
|
219
219
|
let patternIndex;
|
|
220
|
-
while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
|
|
220
|
+
while ((patternIndex = (0, line_1.findDoubleNewlineIndex)(data)) !== -1) {
|
|
221
221
|
yield data.slice(0, patternIndex);
|
|
222
222
|
data = data.slice(patternIndex);
|
|
223
223
|
}
|
|
@@ -226,32 +226,6 @@ async function* iterSSEChunks(iterator) {
|
|
|
226
226
|
yield data;
|
|
227
227
|
}
|
|
228
228
|
}
|
|
229
|
-
function findDoubleNewlineIndex(buffer) {
|
|
230
|
-
// This function searches the buffer for the end patterns (\r\r, \n\n, \r\n\r\n)
|
|
231
|
-
// and returns the index right after the first occurrence of any pattern,
|
|
232
|
-
// or -1 if none of the patterns are found.
|
|
233
|
-
const newline = 0x0a; // \n
|
|
234
|
-
const carriage = 0x0d; // \r
|
|
235
|
-
for (let i = 0; i < buffer.length - 2; i++) {
|
|
236
|
-
if (buffer[i] === newline && buffer[i + 1] === newline) {
|
|
237
|
-
// \n\n
|
|
238
|
-
return i + 2;
|
|
239
|
-
}
|
|
240
|
-
if (buffer[i] === carriage && buffer[i + 1] === carriage) {
|
|
241
|
-
// \r\r
|
|
242
|
-
return i + 2;
|
|
243
|
-
}
|
|
244
|
-
if (buffer[i] === carriage &&
|
|
245
|
-
buffer[i + 1] === newline &&
|
|
246
|
-
i + 3 < buffer.length &&
|
|
247
|
-
buffer[i + 2] === carriage &&
|
|
248
|
-
buffer[i + 3] === newline) {
|
|
249
|
-
// \r\n\r\n
|
|
250
|
-
return i + 4;
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
return -1;
|
|
254
|
-
}
|
|
255
229
|
class SSEDecoder {
|
|
256
230
|
constructor() {
|
|
257
231
|
this.event = null;
|
|
@@ -293,16 +267,6 @@ class SSEDecoder {
|
|
|
293
267
|
return null;
|
|
294
268
|
}
|
|
295
269
|
}
|
|
296
|
-
/** This is an internal helper function that's just used for testing */
|
|
297
|
-
function _decodeChunks(chunks) {
|
|
298
|
-
const decoder = new line_1.LineDecoder();
|
|
299
|
-
const lines = [];
|
|
300
|
-
for (const chunk of chunks) {
|
|
301
|
-
lines.push(...decoder.decode(chunk));
|
|
302
|
-
}
|
|
303
|
-
return lines;
|
|
304
|
-
}
|
|
305
|
-
exports._decodeChunks = _decodeChunks;
|
|
306
270
|
function partition(str, delimiter) {
|
|
307
271
|
const index = str.indexOf(delimiter);
|
|
308
272
|
if (index !== -1) {
|
package/streaming.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streaming.js","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":";;;AAAA,6CAA+D;AAC/D,sCAAyC;AACzC,
|
|
1
|
+
{"version":3,"file":"streaming.js","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":";;;AAAA,6CAA+D;AAC/D,sCAAyC;AACzC,sDAA+E;AAC/E,6DAAwE;AAExE,oCAA+C;AAC/C,sCAAmC;AAUnC,MAAa,MAAM;IAGjB,YACU,QAAmC,EAC3C,UAA2B;QADnB,aAAQ,GAAR,QAAQ,CAA2B;QAG3C,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;IAED,MAAM,CAAC,eAAe,CAAO,QAAkB,EAAE,UAA2B;QAC1E,IAAI,QAAQ,GAAG,KAAK,CAAC;QAErB,KAAK,SAAS,CAAC,CAAC,QAAQ;YACtB,IAAI,QAAQ,EAAE;gBACZ,MAAM,IAAI,KAAK,CAAC,0EAA0E,CAAC,CAAC;aAC7F;YACD,QAAQ,GAAG,IAAI,CAAC;YAChB,IAAI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IAAI,CAAC,IAAI,CAAC,CAAC;QAEvB,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;YACxB,OAAO,IAAI,CAAC;SACb;QAED,IAAI,CAAC,SAAS,EAAE,CAAC,EAAE,KAAK,CAAC,GAAG,SAAS,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAEjD,IAAI,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;YACzB,KAAK,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;SAC5B;QAED,IAAI,SAAS,KAAK,OAAO,EAAE;YACzB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;SACpB;aAAM,IAAI,SAAS,KAAK,MAAM,EAAE;YAC/B,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;SACvB;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAED,SAAS,SAAS,CAAC,GAAW,EAAE,SAAiB;IAC/C,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACrC,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE;QAChB,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,SAAS,EAAE,GAAG,CAAC,SAAS,CAAC,KAAK,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;KACtF;IAED,OAAO,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;AACvB,CAAC"}
|
package/streaming.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ReadableStream } from "./_shims/index.mjs";
|
|
2
2
|
import { AnthropicError } from "./error.mjs";
|
|
3
|
-
import { LineDecoder } from "./internal/decoders/line.mjs";
|
|
3
|
+
import { findDoubleNewlineIndex, LineDecoder } from "./internal/decoders/line.mjs";
|
|
4
4
|
import { ReadableStreamToAsyncIterable } from "./internal/stream-utils.mjs";
|
|
5
5
|
import { createResponseHeaders } from "./core.mjs";
|
|
6
6
|
import { APIError } from "./error.mjs";
|
|
@@ -221,32 +221,6 @@ async function* iterSSEChunks(iterator) {
|
|
|
221
221
|
yield data;
|
|
222
222
|
}
|
|
223
223
|
}
|
|
224
|
-
function findDoubleNewlineIndex(buffer) {
|
|
225
|
-
// This function searches the buffer for the end patterns (\r\r, \n\n, \r\n\r\n)
|
|
226
|
-
// and returns the index right after the first occurrence of any pattern,
|
|
227
|
-
// or -1 if none of the patterns are found.
|
|
228
|
-
const newline = 0x0a; // \n
|
|
229
|
-
const carriage = 0x0d; // \r
|
|
230
|
-
for (let i = 0; i < buffer.length - 2; i++) {
|
|
231
|
-
if (buffer[i] === newline && buffer[i + 1] === newline) {
|
|
232
|
-
// \n\n
|
|
233
|
-
return i + 2;
|
|
234
|
-
}
|
|
235
|
-
if (buffer[i] === carriage && buffer[i + 1] === carriage) {
|
|
236
|
-
// \r\r
|
|
237
|
-
return i + 2;
|
|
238
|
-
}
|
|
239
|
-
if (buffer[i] === carriage &&
|
|
240
|
-
buffer[i + 1] === newline &&
|
|
241
|
-
i + 3 < buffer.length &&
|
|
242
|
-
buffer[i + 2] === carriage &&
|
|
243
|
-
buffer[i + 3] === newline) {
|
|
244
|
-
// \r\n\r\n
|
|
245
|
-
return i + 4;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
return -1;
|
|
249
|
-
}
|
|
250
224
|
class SSEDecoder {
|
|
251
225
|
constructor() {
|
|
252
226
|
this.event = null;
|
|
@@ -288,15 +262,6 @@ class SSEDecoder {
|
|
|
288
262
|
return null;
|
|
289
263
|
}
|
|
290
264
|
}
|
|
291
|
-
/** This is an internal helper function that's just used for testing */
|
|
292
|
-
export function _decodeChunks(chunks) {
|
|
293
|
-
const decoder = new LineDecoder();
|
|
294
|
-
const lines = [];
|
|
295
|
-
for (const chunk of chunks) {
|
|
296
|
-
lines.push(...decoder.decode(chunk));
|
|
297
|
-
}
|
|
298
|
-
return lines;
|
|
299
|
-
}
|
|
300
265
|
function partition(str, delimiter) {
|
|
301
266
|
const index = str.indexOf(delimiter);
|
|
302
267
|
if (index !== -1) {
|