@anthropic-ai/sdk 0.21.0 → 0.22.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 +27 -0
- package/README.md +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 +2 -2
- 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 +207 -213
- package/resources/messages.d.ts.map +1 -1
- package/resources/messages.js.map +1 -1
- package/resources/messages.mjs.map +1 -1
- package/src/_shims/auto/runtime-bun.ts +1 -1
- package/src/_shims/auto/runtime-node.ts +1 -1
- package/src/_shims/auto/runtime.ts +1 -1
- package/src/_shims/auto/types-node.ts +1 -1
- package/src/_shims/bun-runtime.ts +2 -2
- package/src/_shims/index.d.ts +3 -3
- package/src/_shims/index.mjs +1 -1
- package/src/_shims/node-runtime.ts +3 -3
- package/src/_shims/registry.ts +1 -1
- package/src/_shims/web-runtime.ts +3 -3
- package/src/core.ts +6 -6
- package/src/error.ts +1 -1
- package/src/index.ts +17 -8
- package/src/lib/MessageStream.ts +36 -10
- package/src/resource.ts +1 -1
- package/src/resources/completions.ts +5 -5
- package/src/resources/index.ts +14 -3
- package/src/resources/messages.ts +267 -254
- package/src/shims/node.ts +3 -3
- package/src/shims/web.ts +3 -3
- package/src/streaming.ts +4 -4
- package/src/uploads.ts +4 -4
- 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/_shims/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
3
|
*/
|
|
4
|
-
import { manual } from
|
|
5
|
-
import * as auto from "./auto/types";
|
|
6
|
-
import { type RequestOptions } from
|
|
4
|
+
import { manual } from "./manual-types.js";
|
|
5
|
+
import * as auto from "./auto/types.js";
|
|
6
|
+
import { type RequestOptions } from "../core.js";
|
|
7
7
|
|
|
8
8
|
type SelectType<Manual, Auto> = unknown extends Manual ? Auto : Manual;
|
|
9
9
|
|
package/src/_shims/index.mjs
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
3
|
*/
|
|
4
4
|
import * as shims from './registry.mjs';
|
|
5
|
-
import * as auto from "./auto/runtime";
|
|
5
|
+
import * as auto from "./auto/runtime.mjs";
|
|
6
6
|
if (!shims.kind) shims.setShims(auto.getRuntime(), { auto: true });
|
|
7
7
|
export * from './registry.mjs';
|
|
@@ -10,9 +10,9 @@ import { ReadStream as FsReadStream } from 'node:fs';
|
|
|
10
10
|
import { type Agent } from 'node:http';
|
|
11
11
|
import { FormDataEncoder } from 'form-data-encoder';
|
|
12
12
|
import { Readable } from 'node:stream';
|
|
13
|
-
import { type RequestOptions } from
|
|
14
|
-
import { MultipartBody } from
|
|
15
|
-
import { type Shims } from
|
|
13
|
+
import { type RequestOptions } from "../core.js";
|
|
14
|
+
import { MultipartBody } from "./MultipartBody.js";
|
|
15
|
+
import { type Shims } from "./registry.js";
|
|
16
16
|
|
|
17
17
|
// @ts-ignore (this package does not have proper export maps for this export)
|
|
18
18
|
import { ReadableStream } from 'web-streams-polyfill/dist/ponyfill.es2018.js';
|
package/src/_shims/registry.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
|
|
3
3
|
*/
|
|
4
|
-
import { MultipartBody } from
|
|
5
|
-
import { type RequestOptions } from
|
|
6
|
-
import { type Shims } from
|
|
4
|
+
import { MultipartBody } from "./MultipartBody.js";
|
|
5
|
+
import { type RequestOptions } from "../core.js";
|
|
6
|
+
import { type Shims } from "./registry.js";
|
|
7
7
|
|
|
8
8
|
export function getRuntime({ manuallyImported }: { manuallyImported?: boolean } = {}): Shims {
|
|
9
9
|
const recommendation =
|
package/src/core.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { VERSION } from
|
|
2
|
-
import { Stream } from
|
|
1
|
+
import { VERSION } from "./version.js";
|
|
2
|
+
import { Stream } from "./streaming.js";
|
|
3
3
|
import {
|
|
4
4
|
AnthropicError,
|
|
5
5
|
APIError,
|
|
6
6
|
APIConnectionError,
|
|
7
7
|
APIConnectionTimeoutError,
|
|
8
8
|
APIUserAbortError,
|
|
9
|
-
} from
|
|
9
|
+
} from "./error.js";
|
|
10
10
|
import {
|
|
11
11
|
kind as shimsKind,
|
|
12
12
|
type Readable,
|
|
@@ -17,15 +17,15 @@ import {
|
|
|
17
17
|
type RequestInit,
|
|
18
18
|
type Response,
|
|
19
19
|
type HeadersInit,
|
|
20
|
-
} from
|
|
20
|
+
} from "./_shims/index.js";
|
|
21
21
|
export { type Response };
|
|
22
|
-
import { isMultipartBody } from
|
|
22
|
+
import { isMultipartBody } from "./uploads.js";
|
|
23
23
|
export {
|
|
24
24
|
maybeMultipartFormRequestOptions,
|
|
25
25
|
multipartFormRequestOptions,
|
|
26
26
|
createForm,
|
|
27
27
|
type Uploadable,
|
|
28
|
-
} from
|
|
28
|
+
} from "./uploads.js";
|
|
29
29
|
|
|
30
30
|
export type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>;
|
|
31
31
|
|
package/src/error.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
|
-
import * as Core from
|
|
4
|
-
import * as Errors from
|
|
5
|
-
import { type Agent } from
|
|
6
|
-
import * as Uploads from
|
|
7
|
-
import * as API from "./resources/index";
|
|
3
|
+
import * as Core from "./core.js";
|
|
4
|
+
import * as Errors from "./error.js";
|
|
5
|
+
import { type Agent } from "./_shims/index.js";
|
|
6
|
+
import * as Uploads from "./uploads.js";
|
|
7
|
+
import * as API from "./resources/index.js";
|
|
8
8
|
|
|
9
9
|
export interface ClientOptions {
|
|
10
10
|
/**
|
|
@@ -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
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as Core from "../core";
|
|
2
|
-
import { AnthropicError, APIUserAbortError } from "../error";
|
|
1
|
+
import * as Core from "../core.js";
|
|
2
|
+
import { AnthropicError, APIUserAbortError } from "../error.js";
|
|
3
3
|
import {
|
|
4
4
|
ContentBlock,
|
|
5
5
|
Messages,
|
|
@@ -7,15 +7,18 @@ import {
|
|
|
7
7
|
MessageStreamEvent,
|
|
8
8
|
MessageParam,
|
|
9
9
|
MessageCreateParams,
|
|
10
|
-
|
|
11
|
-
} from "../resources/messages";
|
|
12
|
-
import { type ReadableStream } from "../_shims/index";
|
|
13
|
-
import { Stream } from "../streaming";
|
|
10
|
+
MessageCreateParamsBase,
|
|
11
|
+
} from "../resources/messages.js";
|
|
12
|
+
import { type ReadableStream } from "../_shims/index.js";
|
|
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/resource.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
|
-
import * as Core from "../core";
|
|
4
|
-
import { APIPromise } from "../core";
|
|
5
|
-
import { APIResource } from "../resource";
|
|
6
|
-
import * as CompletionsAPI from "./completions";
|
|
7
|
-
import { Stream } from "../streaming";
|
|
3
|
+
import * as Core from "../core.js";
|
|
4
|
+
import { APIPromise } from "../core.js";
|
|
5
|
+
import { APIResource } from "../resource.js";
|
|
6
|
+
import * as CompletionsAPI from "./completions.js";
|
|
7
|
+
import { Stream } from "../streaming.js";
|
|
8
8
|
|
|
9
9
|
export class Completions extends APIResource {
|
|
10
10
|
/**
|
package/src/resources/index.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
|
-
export { Beta } from './beta/beta';
|
|
4
3
|
export {
|
|
5
4
|
Completion,
|
|
6
5
|
CompletionCreateParams,
|
|
7
6
|
CompletionCreateParamsNonStreaming,
|
|
8
7
|
CompletionCreateParamsStreaming,
|
|
9
8
|
Completions,
|
|
10
|
-
} from
|
|
9
|
+
} from "./completions.js";
|
|
11
10
|
export {
|
|
12
11
|
ContentBlock,
|
|
13
12
|
ContentBlockDeltaEvent,
|
|
14
13
|
ContentBlockStartEvent,
|
|
15
14
|
ContentBlockStopEvent,
|
|
16
15
|
ImageBlockParam,
|
|
16
|
+
InputJsonDelta,
|
|
17
17
|
Message,
|
|
18
18
|
MessageDeltaEvent,
|
|
19
19
|
MessageDeltaUsage,
|
|
@@ -21,13 +21,24 @@ 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,
|
|
30
41
|
MessageCreateParamsStreaming,
|
|
31
42
|
MessageStreamParams,
|
|
32
43
|
Messages,
|
|
33
|
-
} from
|
|
44
|
+
} from "./messages.js";
|