@anthropic-ai/sdk 0.21.1 → 0.23.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 +31 -0
- package/README.md +1 -1
- package/core.d.ts +4 -2
- package/core.d.ts.map +1 -1
- package/core.js +17 -4
- package/core.js.map +1 -1
- package/core.mjs +18 -5
- package/core.mjs.map +1 -1
- package/index.d.mts +12 -2
- package/index.d.ts +12 -2
- package/index.d.ts.map +1 -1
- package/index.js +0 -2
- package/index.js.map +1 -1
- package/index.mjs +0 -2
- package/index.mjs.map +1 -1
- package/lib/MessageStream.d.ts +3 -2
- package/lib/MessageStream.d.ts.map +1 -1
- package/lib/MessageStream.js +25 -2
- package/lib/MessageStream.js.map +1 -1
- package/lib/MessageStream.mjs +25 -2
- package/lib/MessageStream.mjs.map +1 -1
- package/package.json +1 -1
- package/resources/index.d.ts +1 -2
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js +1 -3
- package/resources/index.js.map +1 -1
- package/resources/index.mjs +0 -1
- package/resources/index.mjs.map +1 -1
- package/resources/messages.d.ts +205 -211
- package/resources/messages.d.ts.map +1 -1
- package/resources/messages.js.map +1 -1
- package/resources/messages.mjs.map +1 -1
- package/src/core.ts +27 -8
- package/src/index.ts +12 -3
- package/src/lib/MessageStream.ts +31 -5
- package/src/resources/index.ts +12 -1
- package/src/resources/messages.ts +258 -245
- package/src/version.ts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
- package/lib/ToolsBetaMessageStream.d.ts +0 -95
- package/lib/ToolsBetaMessageStream.d.ts.map +0 -1
- package/lib/ToolsBetaMessageStream.js +0 -459
- package/lib/ToolsBetaMessageStream.js.map +0 -1
- package/lib/ToolsBetaMessageStream.mjs +0 -455
- package/lib/ToolsBetaMessageStream.mjs.map +0 -1
- package/resources/beta/beta.d.ts +0 -9
- package/resources/beta/beta.d.ts.map +0 -1
- package/resources/beta/beta.js +0 -40
- package/resources/beta/beta.js.map +0 -1
- package/resources/beta/beta.mjs +0 -13
- package/resources/beta/beta.mjs.map +0 -1
- package/resources/beta/index.d.ts +0 -3
- package/resources/beta/index.d.ts.map +0 -1
- package/resources/beta/index.js +0 -9
- package/resources/beta/index.js.map +0 -1
- package/resources/beta/index.mjs +0 -4
- package/resources/beta/index.mjs.map +0 -1
- package/resources/beta/tools/index.d.ts +0 -3
- package/resources/beta/tools/index.d.ts.map +0 -1
- package/resources/beta/tools/index.js +0 -9
- package/resources/beta/tools/index.js.map +0 -1
- package/resources/beta/tools/index.mjs +0 -4
- package/resources/beta/tools/index.mjs.map +0 -1
- package/resources/beta/tools/messages.d.ts +0 -793
- package/resources/beta/tools/messages.d.ts.map +0 -1
- package/resources/beta/tools/messages.js +0 -29
- package/resources/beta/tools/messages.js.map +0 -1
- package/resources/beta/tools/messages.mjs +0 -24
- package/resources/beta/tools/messages.mjs.map +0 -1
- package/resources/beta/tools/tools.d.ts +0 -24
- package/resources/beta/tools/tools.d.ts.map +0 -1
- package/resources/beta/tools/tools.js +0 -40
- package/resources/beta/tools/tools.js.map +0 -1
- package/resources/beta/tools/tools.mjs +0 -13
- package/resources/beta/tools/tools.mjs.map +0 -1
- package/src/lib/ToolsBetaMessageStream.ts +0 -561
- package/src/resources/beta/beta.ts +0 -12
- package/src/resources/beta/index.ts +0 -4
- package/src/resources/beta/tools/index.ts +0 -21
- package/src/resources/beta/tools/messages.ts +0 -907
- package/src/resources/beta/tools/tools.ts +0 -27
package/src/core.ts
CHANGED
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
type HeadersInit,
|
|
20
20
|
} from "./_shims/index.js";
|
|
21
21
|
export { type Response };
|
|
22
|
-
import { isMultipartBody } from "./uploads.js";
|
|
22
|
+
import { BlobLike, isBlobLike, isMultipartBody } from "./uploads.js";
|
|
23
23
|
export {
|
|
24
24
|
maybeMultipartFormRequestOptions,
|
|
25
25
|
multipartFormRequestOptions,
|
|
@@ -249,7 +249,17 @@ export abstract class APIClient {
|
|
|
249
249
|
path: string,
|
|
250
250
|
opts?: PromiseOrValue<RequestOptions<Req>>,
|
|
251
251
|
): APIPromise<Rsp> {
|
|
252
|
-
return this.request(
|
|
252
|
+
return this.request(
|
|
253
|
+
Promise.resolve(opts).then(async (opts) => {
|
|
254
|
+
const body =
|
|
255
|
+
opts && isBlobLike(opts?.body) ? new DataView(await opts.body.arrayBuffer())
|
|
256
|
+
: opts?.body instanceof DataView ? opts.body
|
|
257
|
+
: opts?.body instanceof ArrayBuffer ? new DataView(opts.body)
|
|
258
|
+
: opts && ArrayBuffer.isView(opts?.body) ? new DataView(opts.body.buffer)
|
|
259
|
+
: opts?.body;
|
|
260
|
+
return { method, path, ...opts, body };
|
|
261
|
+
}),
|
|
262
|
+
);
|
|
253
263
|
}
|
|
254
264
|
|
|
255
265
|
getAPIList<Item, PageClass extends AbstractPage<Item> = AbstractPage<Item>>(
|
|
@@ -271,6 +281,8 @@ export abstract class APIClient {
|
|
|
271
281
|
const encoded = encoder.encode(body);
|
|
272
282
|
return encoded.length.toString();
|
|
273
283
|
}
|
|
284
|
+
} else if (ArrayBuffer.isView(body)) {
|
|
285
|
+
return body.byteLength.toString();
|
|
274
286
|
}
|
|
275
287
|
|
|
276
288
|
return null;
|
|
@@ -280,7 +292,9 @@ export abstract class APIClient {
|
|
|
280
292
|
const { method, path, query, headers: headers = {} } = options;
|
|
281
293
|
|
|
282
294
|
const body =
|
|
283
|
-
|
|
295
|
+
ArrayBuffer.isView(options.body) || (options.__binaryRequest && typeof options.body === 'string') ?
|
|
296
|
+
options.body
|
|
297
|
+
: isMultipartBody(options.body) ? options.body.body
|
|
284
298
|
: options.body ? JSON.stringify(options.body, null, 2)
|
|
285
299
|
: null;
|
|
286
300
|
const contentLength = this.calculateContentLength(body);
|
|
@@ -735,7 +749,9 @@ export type Headers = Record<string, string | null | undefined>;
|
|
|
735
749
|
export type DefaultQuery = Record<string, string | undefined>;
|
|
736
750
|
export type KeysEnum<T> = { [P in keyof Required<T>]: true };
|
|
737
751
|
|
|
738
|
-
export type RequestOptions<
|
|
752
|
+
export type RequestOptions<
|
|
753
|
+
Req = unknown | Record<string, unknown> | Readable | BlobLike | ArrayBufferView | ArrayBuffer,
|
|
754
|
+
> = {
|
|
739
755
|
method?: HTTPMethod;
|
|
740
756
|
path?: string;
|
|
741
757
|
query?: Req | undefined;
|
|
@@ -749,6 +765,7 @@ export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> =
|
|
|
749
765
|
signal?: AbortSignal | undefined | null;
|
|
750
766
|
idempotencyKey?: string;
|
|
751
767
|
|
|
768
|
+
__binaryRequest?: boolean | undefined;
|
|
752
769
|
__binaryResponse?: boolean | undefined;
|
|
753
770
|
__streamClass?: typeof Stream;
|
|
754
771
|
};
|
|
@@ -770,6 +787,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
|
|
|
770
787
|
signal: true,
|
|
771
788
|
idempotencyKey: true,
|
|
772
789
|
|
|
790
|
+
__binaryRequest: true,
|
|
773
791
|
__binaryResponse: true,
|
|
774
792
|
__streamClass: true,
|
|
775
793
|
};
|
|
@@ -783,10 +801,11 @@ export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
|
|
|
783
801
|
);
|
|
784
802
|
};
|
|
785
803
|
|
|
786
|
-
export type FinalRequestOptions<Req = unknown | Record<string, unknown> | Readable
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
804
|
+
export type FinalRequestOptions<Req = unknown | Record<string, unknown> | Readable | DataView> =
|
|
805
|
+
RequestOptions<Req> & {
|
|
806
|
+
method: HTTPMethod;
|
|
807
|
+
path: string;
|
|
808
|
+
};
|
|
790
809
|
|
|
791
810
|
declare const Deno: any;
|
|
792
811
|
declare const EdgeRuntime: any;
|
package/src/index.ts
CHANGED
|
@@ -122,7 +122,6 @@ export class Anthropic extends Core.APIClient {
|
|
|
122
122
|
|
|
123
123
|
completions: API.Completions = new API.Completions(this);
|
|
124
124
|
messages: API.Messages = new API.Messages(this);
|
|
125
|
-
beta: API.Beta = new API.Beta(this);
|
|
126
125
|
|
|
127
126
|
protected override defaultQuery(): Core.DefaultQuery | undefined {
|
|
128
127
|
return this._options.defaultQuery;
|
|
@@ -242,6 +241,7 @@ export namespace Anthropic {
|
|
|
242
241
|
export import ContentBlockStartEvent = API.ContentBlockStartEvent;
|
|
243
242
|
export import ContentBlockStopEvent = API.ContentBlockStopEvent;
|
|
244
243
|
export import ImageBlockParam = API.ImageBlockParam;
|
|
244
|
+
export import InputJsonDelta = API.InputJsonDelta;
|
|
245
245
|
export import Message = API.Message;
|
|
246
246
|
export import MessageDeltaEvent = API.MessageDeltaEvent;
|
|
247
247
|
export import MessageDeltaUsage = API.MessageDeltaUsage;
|
|
@@ -249,16 +249,25 @@ export namespace Anthropic {
|
|
|
249
249
|
export import MessageStartEvent = API.MessageStartEvent;
|
|
250
250
|
export import MessageStopEvent = API.MessageStopEvent;
|
|
251
251
|
export import MessageStreamEvent = API.MessageStreamEvent;
|
|
252
|
+
export import RawContentBlockDeltaEvent = API.RawContentBlockDeltaEvent;
|
|
253
|
+
export import RawContentBlockStartEvent = API.RawContentBlockStartEvent;
|
|
254
|
+
export import RawContentBlockStopEvent = API.RawContentBlockStopEvent;
|
|
255
|
+
export import RawMessageDeltaEvent = API.RawMessageDeltaEvent;
|
|
256
|
+
export import RawMessageStartEvent = API.RawMessageStartEvent;
|
|
257
|
+
export import RawMessageStopEvent = API.RawMessageStopEvent;
|
|
258
|
+
export import RawMessageStreamEvent = API.RawMessageStreamEvent;
|
|
252
259
|
export import TextBlock = API.TextBlock;
|
|
253
260
|
export import TextBlockParam = API.TextBlockParam;
|
|
254
261
|
export import TextDelta = API.TextDelta;
|
|
262
|
+
export import Tool = API.Tool;
|
|
263
|
+
export import ToolResultBlockParam = API.ToolResultBlockParam;
|
|
264
|
+
export import ToolUseBlock = API.ToolUseBlock;
|
|
265
|
+
export import ToolUseBlockParam = API.ToolUseBlockParam;
|
|
255
266
|
export import Usage = API.Usage;
|
|
256
267
|
export import MessageCreateParams = API.MessageCreateParams;
|
|
257
268
|
export import MessageCreateParamsNonStreaming = API.MessageCreateParamsNonStreaming;
|
|
258
269
|
export import MessageCreateParamsStreaming = API.MessageCreateParamsStreaming;
|
|
259
270
|
export import MessageStreamParams = API.MessageStreamParams;
|
|
260
|
-
|
|
261
|
-
export import Beta = API.Beta;
|
|
262
271
|
}
|
|
263
272
|
|
|
264
273
|
export default Anthropic;
|
package/src/lib/MessageStream.ts
CHANGED
|
@@ -7,15 +7,18 @@ import {
|
|
|
7
7
|
MessageStreamEvent,
|
|
8
8
|
MessageParam,
|
|
9
9
|
MessageCreateParams,
|
|
10
|
-
|
|
10
|
+
MessageCreateParamsBase,
|
|
11
11
|
} from "../resources/messages.js";
|
|
12
12
|
import { type ReadableStream } from "../_shims/index.js";
|
|
13
13
|
import { Stream } from "../streaming.js";
|
|
14
|
+
import { TextBlock } from "../resources.js";
|
|
15
|
+
import { partialParse } from "../_vendor/partial-json-parser/parser.js";
|
|
14
16
|
|
|
15
17
|
export interface MessageStreamEvents {
|
|
16
18
|
connect: () => void;
|
|
17
19
|
streamEvent: (event: MessageStreamEvent, snapshot: Message) => void;
|
|
18
20
|
text: (textDelta: string, textSnapshot: string) => void;
|
|
21
|
+
inputJson: (partialJson: string, jsonSnapshot: unknown) => void;
|
|
19
22
|
message: (message: Message) => void;
|
|
20
23
|
contentBlock: (content: ContentBlock) => void;
|
|
21
24
|
finalMessage: (message: Message) => void;
|
|
@@ -29,6 +32,8 @@ type MessageStreamEventListeners<Event extends keyof MessageStreamEvents> = {
|
|
|
29
32
|
once?: boolean;
|
|
30
33
|
}[];
|
|
31
34
|
|
|
35
|
+
const JSON_BUF_PROPERTY = '__json_buf';
|
|
36
|
+
|
|
32
37
|
export class MessageStream implements AsyncIterable<MessageStreamEvent> {
|
|
33
38
|
messages: MessageParam[] = [];
|
|
34
39
|
receivedMessages: Message[] = [];
|
|
@@ -85,7 +90,7 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
|
|
|
85
90
|
|
|
86
91
|
static createMessage(
|
|
87
92
|
messages: Messages,
|
|
88
|
-
params:
|
|
93
|
+
params: MessageCreateParamsBase,
|
|
89
94
|
options?: Core.RequestOptions,
|
|
90
95
|
): MessageStream {
|
|
91
96
|
const runner = new MessageStream();
|
|
@@ -264,7 +269,7 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
|
|
|
264
269
|
}
|
|
265
270
|
const textBlocks = this.receivedMessages
|
|
266
271
|
.at(-1)!
|
|
267
|
-
.content.filter((block) => block.type === 'text')
|
|
272
|
+
.content.filter((block): block is TextBlock => block.type === 'text')
|
|
268
273
|
.map((block) => block.text);
|
|
269
274
|
if (textBlocks.length === 0) {
|
|
270
275
|
throw new AnthropicError('stream ended without producing a content block with type=text');
|
|
@@ -369,8 +374,13 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
|
|
|
369
374
|
|
|
370
375
|
switch (event.type) {
|
|
371
376
|
case 'content_block_delta': {
|
|
372
|
-
|
|
373
|
-
|
|
377
|
+
const content = messageSnapshot.content.at(-1)!;
|
|
378
|
+
if (event.delta.type === 'text_delta' && content.type === 'text') {
|
|
379
|
+
this._emit('text', event.delta.text, content.text || '');
|
|
380
|
+
} else if (event.delta.type === 'input_json_delta' && content.type === 'tool_use') {
|
|
381
|
+
if (content.input) {
|
|
382
|
+
this._emit('inputJson', event.delta.partial_json, content.input);
|
|
383
|
+
}
|
|
374
384
|
}
|
|
375
385
|
break;
|
|
376
386
|
}
|
|
@@ -459,6 +469,22 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
|
|
|
459
469
|
const snapshotContent = snapshot.content.at(event.index);
|
|
460
470
|
if (snapshotContent?.type === 'text' && event.delta.type === 'text_delta') {
|
|
461
471
|
snapshotContent.text += event.delta.text;
|
|
472
|
+
} else if (snapshotContent?.type === 'tool_use' && event.delta.type === 'input_json_delta') {
|
|
473
|
+
// we need to keep track of the raw JSON string as well so that we can
|
|
474
|
+
// re-parse it for each delta, for now we just store it as an untyped
|
|
475
|
+
// non-enumerable property on the snapshot
|
|
476
|
+
let jsonBuf = (snapshotContent as any)[JSON_BUF_PROPERTY] || '';
|
|
477
|
+
jsonBuf += event.delta.partial_json;
|
|
478
|
+
|
|
479
|
+
Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY, {
|
|
480
|
+
value: jsonBuf,
|
|
481
|
+
enumerable: false,
|
|
482
|
+
writable: true,
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
if (jsonBuf) {
|
|
486
|
+
snapshotContent.input = partialParse(jsonBuf);
|
|
487
|
+
}
|
|
462
488
|
}
|
|
463
489
|
return snapshot;
|
|
464
490
|
}
|
package/src/resources/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
|
-
export { Beta } from "./beta/beta.js";
|
|
4
3
|
export {
|
|
5
4
|
Completion,
|
|
6
5
|
CompletionCreateParams,
|
|
@@ -14,6 +13,7 @@ export {
|
|
|
14
13
|
ContentBlockStartEvent,
|
|
15
14
|
ContentBlockStopEvent,
|
|
16
15
|
ImageBlockParam,
|
|
16
|
+
InputJsonDelta,
|
|
17
17
|
Message,
|
|
18
18
|
MessageDeltaEvent,
|
|
19
19
|
MessageDeltaUsage,
|
|
@@ -21,9 +21,20 @@ export {
|
|
|
21
21
|
MessageStartEvent,
|
|
22
22
|
MessageStopEvent,
|
|
23
23
|
MessageStreamEvent,
|
|
24
|
+
RawContentBlockDeltaEvent,
|
|
25
|
+
RawContentBlockStartEvent,
|
|
26
|
+
RawContentBlockStopEvent,
|
|
27
|
+
RawMessageDeltaEvent,
|
|
28
|
+
RawMessageStartEvent,
|
|
29
|
+
RawMessageStopEvent,
|
|
30
|
+
RawMessageStreamEvent,
|
|
24
31
|
TextBlock,
|
|
25
32
|
TextBlockParam,
|
|
26
33
|
TextDelta,
|
|
34
|
+
Tool,
|
|
35
|
+
ToolResultBlockParam,
|
|
36
|
+
ToolUseBlock,
|
|
37
|
+
ToolUseBlockParam,
|
|
27
38
|
Usage,
|
|
28
39
|
MessageCreateParams,
|
|
29
40
|
MessageCreateParamsNonStreaming,
|