@anthropic-ai/sdk 0.36.2 → 0.37.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 +39 -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 +4 -1
- package/lib/BetaMessageStream.d.ts.map +1 -1
- package/lib/BetaMessageStream.js +79 -22
- package/lib/BetaMessageStream.js.map +1 -1
- package/lib/BetaMessageStream.mjs +79 -22
- package/lib/BetaMessageStream.mjs.map +1 -1
- package/lib/MessageStream.d.ts +4 -1
- package/lib/MessageStream.d.ts.map +1 -1
- package/lib/MessageStream.js +79 -22
- package/lib/MessageStream.js.map +1 -1
- package/lib/MessageStream.mjs +79 -22
- 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 +22 -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 +148 -13
- 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 +22 -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 +129 -14
- 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 +26 -0
- package/src/internal/decoders/line.ts +100 -38
- package/src/lib/BetaMessageStream.ts +87 -22
- package/src/lib/MessageStream.ts +88 -22
- package/src/pagination.ts +8 -0
- package/src/resources/beta/beta.ts +28 -1
- package/src/resources/beta/index.ts +12 -0
- package/src/resources/beta/messages/batches.ts +22 -0
- package/src/resources/beta/messages/index.ts +12 -0
- package/src/resources/beta/messages/messages.ts +212 -14
- package/src/resources/index.ts +13 -0
- package/src/resources/messages/batches.ts +22 -0
- package/src/resources/messages/index.ts +13 -0
- package/src/resources/messages/messages.ts +177 -15
- 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
package/src/index.ts
CHANGED
|
@@ -53,6 +53,7 @@ import {
|
|
|
53
53
|
InputJSONDelta,
|
|
54
54
|
Message,
|
|
55
55
|
MessageCountTokensParams,
|
|
56
|
+
MessageCountTokensTool,
|
|
56
57
|
MessageCreateParams,
|
|
57
58
|
MessageCreateParamsNonStreaming,
|
|
58
59
|
MessageCreateParamsStreaming,
|
|
@@ -75,17 +76,29 @@ import {
|
|
|
75
76
|
RawMessageStartEvent,
|
|
76
77
|
RawMessageStopEvent,
|
|
77
78
|
RawMessageStreamEvent,
|
|
79
|
+
RedactedThinkingBlock,
|
|
80
|
+
RedactedThinkingBlockParam,
|
|
81
|
+
SignatureDelta,
|
|
78
82
|
TextBlock,
|
|
79
83
|
TextBlockParam,
|
|
80
84
|
TextCitation,
|
|
81
85
|
TextCitationParam,
|
|
82
86
|
TextDelta,
|
|
87
|
+
ThinkingBlock,
|
|
88
|
+
ThinkingBlockParam,
|
|
89
|
+
ThinkingConfigDisabled,
|
|
90
|
+
ThinkingConfigEnabled,
|
|
91
|
+
ThinkingConfigParam,
|
|
92
|
+
ThinkingDelta,
|
|
83
93
|
Tool,
|
|
94
|
+
ToolBash20250124,
|
|
84
95
|
ToolChoice,
|
|
85
96
|
ToolChoiceAny,
|
|
86
97
|
ToolChoiceAuto,
|
|
87
98
|
ToolChoiceTool,
|
|
88
99
|
ToolResultBlockParam,
|
|
100
|
+
ToolTextEditor20250124,
|
|
101
|
+
ToolUnion,
|
|
89
102
|
ToolUseBlock,
|
|
90
103
|
ToolUseBlockParam,
|
|
91
104
|
Usage,
|
|
@@ -354,6 +367,7 @@ export declare namespace Anthropic {
|
|
|
354
367
|
type ImageBlockParam as ImageBlockParam,
|
|
355
368
|
type InputJSONDelta as InputJSONDelta,
|
|
356
369
|
type Message as Message,
|
|
370
|
+
type MessageCountTokensTool as MessageCountTokensTool,
|
|
357
371
|
type MessageDeltaEvent as MessageDeltaEvent,
|
|
358
372
|
type MessageDeltaUsage as MessageDeltaUsage,
|
|
359
373
|
type MessageParam as MessageParam,
|
|
@@ -371,17 +385,29 @@ export declare namespace Anthropic {
|
|
|
371
385
|
type RawMessageStartEvent as RawMessageStartEvent,
|
|
372
386
|
type RawMessageStopEvent as RawMessageStopEvent,
|
|
373
387
|
type RawMessageStreamEvent as RawMessageStreamEvent,
|
|
388
|
+
type RedactedThinkingBlock as RedactedThinkingBlock,
|
|
389
|
+
type RedactedThinkingBlockParam as RedactedThinkingBlockParam,
|
|
390
|
+
type SignatureDelta as SignatureDelta,
|
|
374
391
|
type TextBlock as TextBlock,
|
|
375
392
|
type TextBlockParam as TextBlockParam,
|
|
376
393
|
type TextCitation as TextCitation,
|
|
377
394
|
type TextCitationParam as TextCitationParam,
|
|
378
395
|
type TextDelta as TextDelta,
|
|
396
|
+
type ThinkingBlock as ThinkingBlock,
|
|
397
|
+
type ThinkingBlockParam as ThinkingBlockParam,
|
|
398
|
+
type ThinkingConfigDisabled as ThinkingConfigDisabled,
|
|
399
|
+
type ThinkingConfigEnabled as ThinkingConfigEnabled,
|
|
400
|
+
type ThinkingConfigParam as ThinkingConfigParam,
|
|
401
|
+
type ThinkingDelta as ThinkingDelta,
|
|
379
402
|
type Tool as Tool,
|
|
403
|
+
type ToolBash20250124 as ToolBash20250124,
|
|
380
404
|
type ToolChoice as ToolChoice,
|
|
381
405
|
type ToolChoiceAny as ToolChoiceAny,
|
|
382
406
|
type ToolChoiceAuto as ToolChoiceAuto,
|
|
383
407
|
type ToolChoiceTool as ToolChoiceTool,
|
|
384
408
|
type ToolResultBlockParam as ToolResultBlockParam,
|
|
409
|
+
type ToolTextEditor20250124 as ToolTextEditor20250124,
|
|
410
|
+
type ToolUnion as ToolUnion,
|
|
385
411
|
type ToolUseBlock as ToolUseBlock,
|
|
386
412
|
type ToolUseBlockParam as ToolUseBlockParam,
|
|
387
413
|
type Usage as Usage,
|
|
@@ -13,52 +13,58 @@ export class LineDecoder {
|
|
|
13
13
|
static NEWLINE_CHARS = new Set(['\n', '\r']);
|
|
14
14
|
static NEWLINE_REGEXP = /\r\n|[\n\r]/g;
|
|
15
15
|
|
|
16
|
-
buffer:
|
|
17
|
-
|
|
16
|
+
buffer: Uint8Array;
|
|
17
|
+
#carriageReturnIndex: number | null;
|
|
18
18
|
textDecoder: any; // TextDecoder found in browsers; not typed to avoid pulling in either "dom" or "node" types.
|
|
19
19
|
|
|
20
20
|
constructor() {
|
|
21
|
-
this.buffer =
|
|
22
|
-
this
|
|
21
|
+
this.buffer = new Uint8Array();
|
|
22
|
+
this.#carriageReturnIndex = null;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
decode(chunk: Bytes): string[] {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (this.trailingCR) {
|
|
29
|
-
text = '\r' + text;
|
|
30
|
-
this.trailingCR = false;
|
|
31
|
-
}
|
|
32
|
-
if (text.endsWith('\r')) {
|
|
33
|
-
this.trailingCR = true;
|
|
34
|
-
text = text.slice(0, -1);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (!text) {
|
|
26
|
+
if (chunk == null) {
|
|
38
27
|
return [];
|
|
39
28
|
}
|
|
40
29
|
|
|
41
|
-
const
|
|
42
|
-
|
|
30
|
+
const binaryChunk =
|
|
31
|
+
chunk instanceof ArrayBuffer ? new Uint8Array(chunk)
|
|
32
|
+
: typeof chunk === 'string' ? new TextEncoder().encode(chunk)
|
|
33
|
+
: chunk;
|
|
34
|
+
|
|
35
|
+
let newData = new Uint8Array(this.buffer.length + binaryChunk.length);
|
|
36
|
+
newData.set(this.buffer);
|
|
37
|
+
newData.set(binaryChunk, this.buffer.length);
|
|
38
|
+
this.buffer = newData;
|
|
39
|
+
|
|
40
|
+
const lines: string[] = [];
|
|
41
|
+
let patternIndex;
|
|
42
|
+
while ((patternIndex = findNewlineIndex(this.buffer, this.#carriageReturnIndex)) != null) {
|
|
43
|
+
if (patternIndex.carriage && this.#carriageReturnIndex == null) {
|
|
44
|
+
// skip until we either get a corresponding `\n`, a new `\r` or nothing
|
|
45
|
+
this.#carriageReturnIndex = patternIndex.index;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
43
48
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
// we got double \r or \rtext\n
|
|
50
|
+
if (
|
|
51
|
+
this.#carriageReturnIndex != null &&
|
|
52
|
+
(patternIndex.index !== this.#carriageReturnIndex + 1 || patternIndex.carriage)
|
|
53
|
+
) {
|
|
54
|
+
lines.push(this.decodeText(this.buffer.slice(0, this.#carriageReturnIndex - 1)));
|
|
55
|
+
this.buffer = this.buffer.slice(this.#carriageReturnIndex);
|
|
56
|
+
this.#carriageReturnIndex = null;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
49
59
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return [];
|
|
53
|
-
}
|
|
60
|
+
const endIndex =
|
|
61
|
+
this.#carriageReturnIndex !== null ? patternIndex.preceding - 1 : patternIndex.preceding;
|
|
54
62
|
|
|
55
|
-
|
|
56
|
-
lines
|
|
57
|
-
this.buffer = [];
|
|
58
|
-
}
|
|
63
|
+
const line = this.decodeText(this.buffer.slice(0, endIndex));
|
|
64
|
+
lines.push(line);
|
|
59
65
|
|
|
60
|
-
|
|
61
|
-
this
|
|
66
|
+
this.buffer = this.buffer.slice(patternIndex.index);
|
|
67
|
+
this.#carriageReturnIndex = null;
|
|
62
68
|
}
|
|
63
69
|
|
|
64
70
|
return lines;
|
|
@@ -102,13 +108,69 @@ export class LineDecoder {
|
|
|
102
108
|
}
|
|
103
109
|
|
|
104
110
|
flush(): string[] {
|
|
105
|
-
if (!this.buffer.length
|
|
111
|
+
if (!this.buffer.length) {
|
|
106
112
|
return [];
|
|
107
113
|
}
|
|
114
|
+
return this.decode('\n');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
108
117
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
118
|
+
/**
|
|
119
|
+
* This function searches the buffer for the end patterns, (\r or \n)
|
|
120
|
+
* and returns an object with the index preceding the matched newline and the
|
|
121
|
+
* index after the newline char. `null` is returned if no new line is found.
|
|
122
|
+
*
|
|
123
|
+
* ```ts
|
|
124
|
+
* findNewLineIndex('abc\ndef') -> { preceding: 2, index: 3 }
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
function findNewlineIndex(
|
|
128
|
+
buffer: Uint8Array,
|
|
129
|
+
startIndex: number | null,
|
|
130
|
+
): { preceding: number; index: number; carriage: boolean } | null {
|
|
131
|
+
const newline = 0x0a; // \n
|
|
132
|
+
const carriage = 0x0d; // \r
|
|
133
|
+
|
|
134
|
+
for (let i = startIndex ?? 0; i < buffer.length; i++) {
|
|
135
|
+
if (buffer[i] === newline) {
|
|
136
|
+
return { preceding: i, index: i + 1, carriage: false };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (buffer[i] === carriage) {
|
|
140
|
+
return { preceding: i, index: i + 1, carriage: true };
|
|
141
|
+
}
|
|
113
142
|
}
|
|
143
|
+
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function findDoubleNewlineIndex(buffer: Uint8Array): number {
|
|
148
|
+
// This function searches the buffer for the end patterns (\r\r, \n\n, \r\n\r\n)
|
|
149
|
+
// and returns the index right after the first occurrence of any pattern,
|
|
150
|
+
// or -1 if none of the patterns are found.
|
|
151
|
+
const newline = 0x0a; // \n
|
|
152
|
+
const carriage = 0x0d; // \r
|
|
153
|
+
|
|
154
|
+
for (let i = 0; i < buffer.length - 1; i++) {
|
|
155
|
+
if (buffer[i] === newline && buffer[i + 1] === newline) {
|
|
156
|
+
// \n\n
|
|
157
|
+
return i + 2;
|
|
158
|
+
}
|
|
159
|
+
if (buffer[i] === carriage && buffer[i + 1] === carriage) {
|
|
160
|
+
// \r\r
|
|
161
|
+
return i + 2;
|
|
162
|
+
}
|
|
163
|
+
if (
|
|
164
|
+
buffer[i] === carriage &&
|
|
165
|
+
buffer[i + 1] === newline &&
|
|
166
|
+
i + 3 < buffer.length &&
|
|
167
|
+
buffer[i + 2] === carriage &&
|
|
168
|
+
buffer[i + 3] === newline
|
|
169
|
+
) {
|
|
170
|
+
// \r\n\r\n
|
|
171
|
+
return i + 4;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return -1;
|
|
114
176
|
}
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
type MessageCreateParams as BetaMessageCreateParams,
|
|
10
10
|
type MessageCreateParamsBase as BetaMessageCreateParamsBase,
|
|
11
11
|
type BetaTextBlock,
|
|
12
|
+
type BetaTextCitation,
|
|
12
13
|
} from "../resources/beta/messages/messages.js";
|
|
13
14
|
import { type ReadableStream, type Response } from "../_shims/index.js";
|
|
14
15
|
import { Stream } from "../streaming.js";
|
|
@@ -18,7 +19,10 @@ export interface MessageStreamEvents {
|
|
|
18
19
|
connect: () => void;
|
|
19
20
|
streamEvent: (event: BetaMessageStreamEvent, snapshot: BetaMessage) => void;
|
|
20
21
|
text: (textDelta: string, textSnapshot: string) => void;
|
|
22
|
+
citation: (citation: BetaTextCitation, citationsSnapshot: BetaTextCitation[]) => void;
|
|
21
23
|
inputJson: (partialJson: string, jsonSnapshot: unknown) => void;
|
|
24
|
+
thinking: (thinkingDelta: string, thinkingSnapshot: string) => void;
|
|
25
|
+
signature: (signature: string) => void;
|
|
22
26
|
message: (message: BetaMessage) => void;
|
|
23
27
|
contentBlock: (content: BetaContentBlock) => void;
|
|
24
28
|
finalMessage: (message: BetaMessage) => void;
|
|
@@ -413,12 +417,39 @@ export class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent>
|
|
|
413
417
|
switch (event.type) {
|
|
414
418
|
case 'content_block_delta': {
|
|
415
419
|
const content = messageSnapshot.content.at(-1)!;
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
420
|
+
switch (event.delta.type) {
|
|
421
|
+
case 'text_delta': {
|
|
422
|
+
if (content.type === 'text') {
|
|
423
|
+
this._emit('text', event.delta.text, content.text || '');
|
|
424
|
+
}
|
|
425
|
+
break;
|
|
421
426
|
}
|
|
427
|
+
case 'citations_delta': {
|
|
428
|
+
if (content.type === 'text') {
|
|
429
|
+
this._emit('citation', event.delta.citation, content.citations ?? []);
|
|
430
|
+
}
|
|
431
|
+
break;
|
|
432
|
+
}
|
|
433
|
+
case 'input_json_delta': {
|
|
434
|
+
if (content.type === 'tool_use' && content.input) {
|
|
435
|
+
this._emit('inputJson', event.delta.partial_json, content.input);
|
|
436
|
+
}
|
|
437
|
+
break;
|
|
438
|
+
}
|
|
439
|
+
case 'thinking_delta': {
|
|
440
|
+
if (content.type === 'thinking') {
|
|
441
|
+
this._emit('thinking', event.delta.thinking, content.thinking);
|
|
442
|
+
}
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
case 'signature_delta': {
|
|
446
|
+
if (content.type === 'thinking') {
|
|
447
|
+
this._emit('signature', content.signature);
|
|
448
|
+
}
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
default:
|
|
452
|
+
checkNever(event.delta);
|
|
422
453
|
}
|
|
423
454
|
break;
|
|
424
455
|
}
|
|
@@ -505,24 +536,55 @@ export class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent>
|
|
|
505
536
|
return snapshot;
|
|
506
537
|
case 'content_block_delta': {
|
|
507
538
|
const snapshotContent = snapshot.content.at(event.index);
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
jsonBuf += event.delta.partial_json;
|
|
516
|
-
|
|
517
|
-
Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY, {
|
|
518
|
-
value: jsonBuf,
|
|
519
|
-
enumerable: false,
|
|
520
|
-
writable: true,
|
|
521
|
-
});
|
|
522
|
-
|
|
523
|
-
if (jsonBuf) {
|
|
524
|
-
snapshotContent.input = partialParse(jsonBuf);
|
|
539
|
+
|
|
540
|
+
switch (event.delta.type) {
|
|
541
|
+
case 'text_delta': {
|
|
542
|
+
if (snapshotContent?.type === 'text') {
|
|
543
|
+
snapshotContent.text += event.delta.text;
|
|
544
|
+
}
|
|
545
|
+
break;
|
|
525
546
|
}
|
|
547
|
+
case 'citations_delta': {
|
|
548
|
+
if (snapshotContent?.type === 'text') {
|
|
549
|
+
snapshotContent.citations ??= [];
|
|
550
|
+
snapshotContent.citations.push(event.delta.citation);
|
|
551
|
+
}
|
|
552
|
+
break;
|
|
553
|
+
}
|
|
554
|
+
case 'input_json_delta': {
|
|
555
|
+
if (snapshotContent?.type === 'tool_use') {
|
|
556
|
+
// we need to keep track of the raw JSON string as well so that we can
|
|
557
|
+
// re-parse it for each delta, for now we just store it as an untyped
|
|
558
|
+
// non-enumerable property on the snapshot
|
|
559
|
+
let jsonBuf = (snapshotContent as any)[JSON_BUF_PROPERTY] || '';
|
|
560
|
+
jsonBuf += event.delta.partial_json;
|
|
561
|
+
|
|
562
|
+
Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY, {
|
|
563
|
+
value: jsonBuf,
|
|
564
|
+
enumerable: false,
|
|
565
|
+
writable: true,
|
|
566
|
+
});
|
|
567
|
+
|
|
568
|
+
if (jsonBuf) {
|
|
569
|
+
snapshotContent.input = partialParse(jsonBuf);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
break;
|
|
573
|
+
}
|
|
574
|
+
case 'thinking_delta': {
|
|
575
|
+
if (snapshotContent?.type === 'thinking') {
|
|
576
|
+
snapshotContent.thinking += event.delta.thinking;
|
|
577
|
+
}
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
case 'signature_delta': {
|
|
581
|
+
if (snapshotContent?.type === 'thinking') {
|
|
582
|
+
snapshotContent.signature = event.delta.signature;
|
|
583
|
+
}
|
|
584
|
+
break;
|
|
585
|
+
}
|
|
586
|
+
default:
|
|
587
|
+
checkNever(event.delta);
|
|
526
588
|
}
|
|
527
589
|
return snapshot;
|
|
528
590
|
}
|
|
@@ -597,3 +659,6 @@ export class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent>
|
|
|
597
659
|
return stream.toReadableStream();
|
|
598
660
|
}
|
|
599
661
|
}
|
|
662
|
+
|
|
663
|
+
// used to ensure exhaustive case matching without throwing a runtime error
|
|
664
|
+
function checkNever(x: never) {}
|
package/src/lib/MessageStream.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
type MessageCreateParams,
|
|
10
10
|
type MessageCreateParamsBase,
|
|
11
11
|
type TextBlock,
|
|
12
|
+
type TextCitation,
|
|
12
13
|
} from "../resources/messages.js";
|
|
13
14
|
import { type ReadableStream, type Response } from "../_shims/index.js";
|
|
14
15
|
import { Stream } from "../streaming.js";
|
|
@@ -18,7 +19,10 @@ export interface MessageStreamEvents {
|
|
|
18
19
|
connect: () => void;
|
|
19
20
|
streamEvent: (event: MessageStreamEvent, snapshot: Message) => void;
|
|
20
21
|
text: (textDelta: string, textSnapshot: string) => void;
|
|
22
|
+
citation: (citation: TextCitation, citationsSnapshot: TextCitation[]) => void;
|
|
21
23
|
inputJson: (partialJson: string, jsonSnapshot: unknown) => void;
|
|
24
|
+
thinking: (thinkingDelta: string, thinkingSnapshot: string) => void;
|
|
25
|
+
signature: (signature: string) => void;
|
|
22
26
|
message: (message: Message) => void;
|
|
23
27
|
contentBlock: (content: ContentBlock) => void;
|
|
24
28
|
finalMessage: (message: Message) => void;
|
|
@@ -413,12 +417,39 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
|
|
|
413
417
|
switch (event.type) {
|
|
414
418
|
case 'content_block_delta': {
|
|
415
419
|
const content = messageSnapshot.content.at(-1)!;
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
420
|
+
switch (event.delta.type) {
|
|
421
|
+
case 'text_delta': {
|
|
422
|
+
if (content.type === 'text') {
|
|
423
|
+
this._emit('text', event.delta.text, content.text || '');
|
|
424
|
+
}
|
|
425
|
+
break;
|
|
421
426
|
}
|
|
427
|
+
case 'citations_delta': {
|
|
428
|
+
if (content.type === 'text') {
|
|
429
|
+
this._emit('citation', event.delta.citation, content.citations ?? []);
|
|
430
|
+
}
|
|
431
|
+
break;
|
|
432
|
+
}
|
|
433
|
+
case 'input_json_delta': {
|
|
434
|
+
if (content.type === 'tool_use' && content.input) {
|
|
435
|
+
this._emit('inputJson', event.delta.partial_json, content.input);
|
|
436
|
+
}
|
|
437
|
+
break;
|
|
438
|
+
}
|
|
439
|
+
case 'thinking_delta': {
|
|
440
|
+
if (content.type === 'thinking') {
|
|
441
|
+
this._emit('thinking', event.delta.thinking, content.thinking);
|
|
442
|
+
}
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
case 'signature_delta': {
|
|
446
|
+
if (content.type === 'thinking') {
|
|
447
|
+
this._emit('signature', content.signature);
|
|
448
|
+
}
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
default:
|
|
452
|
+
checkNever(event.delta);
|
|
422
453
|
}
|
|
423
454
|
break;
|
|
424
455
|
}
|
|
@@ -505,25 +536,57 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
|
|
|
505
536
|
return snapshot;
|
|
506
537
|
case 'content_block_delta': {
|
|
507
538
|
const snapshotContent = snapshot.content.at(event.index);
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
if (jsonBuf) {
|
|
524
|
-
snapshotContent.input = partialParse(jsonBuf);
|
|
539
|
+
|
|
540
|
+
switch (event.delta.type) {
|
|
541
|
+
case 'text_delta': {
|
|
542
|
+
if (snapshotContent?.type === 'text') {
|
|
543
|
+
snapshotContent.text += event.delta.text;
|
|
544
|
+
}
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
547
|
+
case 'citations_delta': {
|
|
548
|
+
if (snapshotContent?.type === 'text') {
|
|
549
|
+
snapshotContent.citations ??= [];
|
|
550
|
+
snapshotContent.citations.push(event.delta.citation);
|
|
551
|
+
}
|
|
552
|
+
break;
|
|
525
553
|
}
|
|
554
|
+
case 'input_json_delta': {
|
|
555
|
+
if (snapshotContent?.type === 'tool_use') {
|
|
556
|
+
// we need to keep track of the raw JSON string as well so that we can
|
|
557
|
+
// re-parse it for each delta, for now we just store it as an untyped
|
|
558
|
+
// non-enumerable property on the snapshot
|
|
559
|
+
let jsonBuf = (snapshotContent as any)[JSON_BUF_PROPERTY] || '';
|
|
560
|
+
jsonBuf += event.delta.partial_json;
|
|
561
|
+
|
|
562
|
+
Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY, {
|
|
563
|
+
value: jsonBuf,
|
|
564
|
+
enumerable: false,
|
|
565
|
+
writable: true,
|
|
566
|
+
});
|
|
567
|
+
|
|
568
|
+
if (jsonBuf) {
|
|
569
|
+
snapshotContent.input = partialParse(jsonBuf);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
break;
|
|
573
|
+
}
|
|
574
|
+
case 'thinking_delta': {
|
|
575
|
+
if (snapshotContent?.type === 'thinking') {
|
|
576
|
+
snapshotContent.thinking += event.delta.thinking;
|
|
577
|
+
}
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
case 'signature_delta': {
|
|
581
|
+
if (snapshotContent?.type === 'thinking') {
|
|
582
|
+
snapshotContent.signature = event.delta.signature;
|
|
583
|
+
}
|
|
584
|
+
break;
|
|
585
|
+
}
|
|
586
|
+
default:
|
|
587
|
+
checkNever(event.delta);
|
|
526
588
|
}
|
|
589
|
+
|
|
527
590
|
return snapshot;
|
|
528
591
|
}
|
|
529
592
|
case 'content_block_stop':
|
|
@@ -597,3 +660,6 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
|
|
|
597
660
|
return stream.toReadableStream();
|
|
598
661
|
}
|
|
599
662
|
}
|
|
663
|
+
|
|
664
|
+
// used to ensure exhaustive case matching without throwing a runtime error
|
|
665
|
+
function checkNever(x: never) {}
|
package/src/pagination.ts
CHANGED
|
@@ -45,6 +45,14 @@ export class Page<Item> extends AbstractPage<Item> implements PageResponse<Item>
|
|
|
45
45
|
return this.data ?? [];
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
override hasNextPage(): boolean {
|
|
49
|
+
if (this.has_more === false) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return super.hasNextPage();
|
|
54
|
+
}
|
|
55
|
+
|
|
48
56
|
// @deprecated Please use `nextPageInfo()` instead
|
|
49
57
|
nextPageParams(): Partial<PageParams> | null {
|
|
50
58
|
const info = this.nextPageInfo();
|
|
@@ -35,20 +35,32 @@ import {
|
|
|
35
35
|
BetaRawMessageStartEvent,
|
|
36
36
|
BetaRawMessageStopEvent,
|
|
37
37
|
BetaRawMessageStreamEvent,
|
|
38
|
+
BetaRedactedThinkingBlock,
|
|
39
|
+
BetaRedactedThinkingBlockParam,
|
|
40
|
+
BetaSignatureDelta,
|
|
38
41
|
BetaTextBlock,
|
|
39
42
|
BetaTextBlockParam,
|
|
40
43
|
BetaTextCitation,
|
|
41
44
|
BetaTextCitationParam,
|
|
42
45
|
BetaTextDelta,
|
|
46
|
+
BetaThinkingBlock,
|
|
47
|
+
BetaThinkingBlockParam,
|
|
48
|
+
BetaThinkingConfigDisabled,
|
|
49
|
+
BetaThinkingConfigEnabled,
|
|
50
|
+
BetaThinkingConfigParam,
|
|
51
|
+
BetaThinkingDelta,
|
|
43
52
|
BetaTool,
|
|
44
53
|
BetaToolBash20241022,
|
|
54
|
+
BetaToolBash20250124,
|
|
45
55
|
BetaToolChoice,
|
|
46
56
|
BetaToolChoiceAny,
|
|
47
57
|
BetaToolChoiceAuto,
|
|
48
58
|
BetaToolChoiceTool,
|
|
49
59
|
BetaToolComputerUse20241022,
|
|
60
|
+
BetaToolComputerUse20250124,
|
|
50
61
|
BetaToolResultBlockParam,
|
|
51
62
|
BetaToolTextEditor20241022,
|
|
63
|
+
BetaToolTextEditor20250124,
|
|
52
64
|
BetaToolUnion,
|
|
53
65
|
BetaToolUseBlock,
|
|
54
66
|
BetaToolUseBlockParam,
|
|
@@ -70,8 +82,11 @@ export type AnthropicBeta =
|
|
|
70
82
|
| 'message-batches-2024-09-24'
|
|
71
83
|
| 'prompt-caching-2024-07-31'
|
|
72
84
|
| 'computer-use-2024-10-22'
|
|
85
|
+
| 'computer-use-2025-01-24'
|
|
73
86
|
| 'pdfs-2024-09-25'
|
|
74
|
-
| 'token-counting-2024-11-01'
|
|
87
|
+
| 'token-counting-2024-11-01'
|
|
88
|
+
| 'token-efficient-tools-2025-02-19'
|
|
89
|
+
| 'output-128k-2025-02-19';
|
|
75
90
|
|
|
76
91
|
export interface BetaAPIError {
|
|
77
92
|
message: string;
|
|
@@ -203,20 +218,32 @@ export declare namespace Beta {
|
|
|
203
218
|
type BetaRawMessageStartEvent as BetaRawMessageStartEvent,
|
|
204
219
|
type BetaRawMessageStopEvent as BetaRawMessageStopEvent,
|
|
205
220
|
type BetaRawMessageStreamEvent as BetaRawMessageStreamEvent,
|
|
221
|
+
type BetaRedactedThinkingBlock as BetaRedactedThinkingBlock,
|
|
222
|
+
type BetaRedactedThinkingBlockParam as BetaRedactedThinkingBlockParam,
|
|
223
|
+
type BetaSignatureDelta as BetaSignatureDelta,
|
|
206
224
|
type BetaTextBlock as BetaTextBlock,
|
|
207
225
|
type BetaTextBlockParam as BetaTextBlockParam,
|
|
208
226
|
type BetaTextCitation as BetaTextCitation,
|
|
209
227
|
type BetaTextCitationParam as BetaTextCitationParam,
|
|
210
228
|
type BetaTextDelta as BetaTextDelta,
|
|
229
|
+
type BetaThinkingBlock as BetaThinkingBlock,
|
|
230
|
+
type BetaThinkingBlockParam as BetaThinkingBlockParam,
|
|
231
|
+
type BetaThinkingConfigDisabled as BetaThinkingConfigDisabled,
|
|
232
|
+
type BetaThinkingConfigEnabled as BetaThinkingConfigEnabled,
|
|
233
|
+
type BetaThinkingConfigParam as BetaThinkingConfigParam,
|
|
234
|
+
type BetaThinkingDelta as BetaThinkingDelta,
|
|
211
235
|
type BetaTool as BetaTool,
|
|
212
236
|
type BetaToolBash20241022 as BetaToolBash20241022,
|
|
237
|
+
type BetaToolBash20250124 as BetaToolBash20250124,
|
|
213
238
|
type BetaToolChoice as BetaToolChoice,
|
|
214
239
|
type BetaToolChoiceAny as BetaToolChoiceAny,
|
|
215
240
|
type BetaToolChoiceAuto as BetaToolChoiceAuto,
|
|
216
241
|
type BetaToolChoiceTool as BetaToolChoiceTool,
|
|
217
242
|
type BetaToolComputerUse20241022 as BetaToolComputerUse20241022,
|
|
243
|
+
type BetaToolComputerUse20250124 as BetaToolComputerUse20250124,
|
|
218
244
|
type BetaToolResultBlockParam as BetaToolResultBlockParam,
|
|
219
245
|
type BetaToolTextEditor20241022 as BetaToolTextEditor20241022,
|
|
246
|
+
type BetaToolTextEditor20250124 as BetaToolTextEditor20250124,
|
|
220
247
|
type BetaToolUnion as BetaToolUnion,
|
|
221
248
|
type BetaToolUseBlock as BetaToolUseBlock,
|
|
222
249
|
type BetaToolUseBlockParam as BetaToolUseBlockParam,
|
|
@@ -48,20 +48,32 @@ export {
|
|
|
48
48
|
type BetaRawMessageStartEvent,
|
|
49
49
|
type BetaRawMessageStopEvent,
|
|
50
50
|
type BetaRawMessageStreamEvent,
|
|
51
|
+
type BetaRedactedThinkingBlock,
|
|
52
|
+
type BetaRedactedThinkingBlockParam,
|
|
53
|
+
type BetaSignatureDelta,
|
|
51
54
|
type BetaTextBlock,
|
|
52
55
|
type BetaTextBlockParam,
|
|
53
56
|
type BetaTextCitation,
|
|
54
57
|
type BetaTextCitationParam,
|
|
55
58
|
type BetaTextDelta,
|
|
59
|
+
type BetaThinkingBlock,
|
|
60
|
+
type BetaThinkingBlockParam,
|
|
61
|
+
type BetaThinkingConfigDisabled,
|
|
62
|
+
type BetaThinkingConfigEnabled,
|
|
63
|
+
type BetaThinkingConfigParam,
|
|
64
|
+
type BetaThinkingDelta,
|
|
56
65
|
type BetaTool,
|
|
57
66
|
type BetaToolBash20241022,
|
|
67
|
+
type BetaToolBash20250124,
|
|
58
68
|
type BetaToolChoice,
|
|
59
69
|
type BetaToolChoiceAny,
|
|
60
70
|
type BetaToolChoiceAuto,
|
|
61
71
|
type BetaToolChoiceTool,
|
|
62
72
|
type BetaToolComputerUse20241022,
|
|
73
|
+
type BetaToolComputerUse20250124,
|
|
63
74
|
type BetaToolResultBlockParam,
|
|
64
75
|
type BetaToolTextEditor20241022,
|
|
76
|
+
type BetaToolTextEditor20250124,
|
|
65
77
|
type BetaToolUnion,
|
|
66
78
|
type BetaToolUseBlock,
|
|
67
79
|
type BetaToolUseBlockParam,
|