@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.
Files changed (94) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +1 -1
  3. package/index.d.mts +12 -2
  4. package/index.d.ts +12 -2
  5. package/index.d.ts.map +1 -1
  6. package/index.js +0 -2
  7. package/index.js.map +1 -1
  8. package/index.mjs +0 -2
  9. package/index.mjs.map +1 -1
  10. package/lib/MessageStream.d.ts +3 -2
  11. package/lib/MessageStream.d.ts.map +1 -1
  12. package/lib/MessageStream.js +25 -2
  13. package/lib/MessageStream.js.map +1 -1
  14. package/lib/MessageStream.mjs +25 -2
  15. package/lib/MessageStream.mjs.map +1 -1
  16. package/package.json +2 -2
  17. package/resources/index.d.ts +1 -2
  18. package/resources/index.d.ts.map +1 -1
  19. package/resources/index.js +1 -3
  20. package/resources/index.js.map +1 -1
  21. package/resources/index.mjs +0 -1
  22. package/resources/index.mjs.map +1 -1
  23. package/resources/messages.d.ts +207 -213
  24. package/resources/messages.d.ts.map +1 -1
  25. package/resources/messages.js.map +1 -1
  26. package/resources/messages.mjs.map +1 -1
  27. package/src/_shims/auto/runtime-bun.ts +1 -1
  28. package/src/_shims/auto/runtime-node.ts +1 -1
  29. package/src/_shims/auto/runtime.ts +1 -1
  30. package/src/_shims/auto/types-node.ts +1 -1
  31. package/src/_shims/bun-runtime.ts +2 -2
  32. package/src/_shims/index.d.ts +3 -3
  33. package/src/_shims/index.mjs +1 -1
  34. package/src/_shims/node-runtime.ts +3 -3
  35. package/src/_shims/registry.ts +1 -1
  36. package/src/_shims/web-runtime.ts +3 -3
  37. package/src/core.ts +6 -6
  38. package/src/error.ts +1 -1
  39. package/src/index.ts +17 -8
  40. package/src/lib/MessageStream.ts +36 -10
  41. package/src/resource.ts +1 -1
  42. package/src/resources/completions.ts +5 -5
  43. package/src/resources/index.ts +14 -3
  44. package/src/resources/messages.ts +267 -254
  45. package/src/shims/node.ts +3 -3
  46. package/src/shims/web.ts +3 -3
  47. package/src/streaming.ts +4 -4
  48. package/src/uploads.ts +4 -4
  49. package/src/version.ts +1 -1
  50. package/version.d.ts +1 -1
  51. package/version.js +1 -1
  52. package/version.mjs +1 -1
  53. package/lib/ToolsBetaMessageStream.d.ts +0 -95
  54. package/lib/ToolsBetaMessageStream.d.ts.map +0 -1
  55. package/lib/ToolsBetaMessageStream.js +0 -459
  56. package/lib/ToolsBetaMessageStream.js.map +0 -1
  57. package/lib/ToolsBetaMessageStream.mjs +0 -455
  58. package/lib/ToolsBetaMessageStream.mjs.map +0 -1
  59. package/resources/beta/beta.d.ts +0 -9
  60. package/resources/beta/beta.d.ts.map +0 -1
  61. package/resources/beta/beta.js +0 -40
  62. package/resources/beta/beta.js.map +0 -1
  63. package/resources/beta/beta.mjs +0 -13
  64. package/resources/beta/beta.mjs.map +0 -1
  65. package/resources/beta/index.d.ts +0 -3
  66. package/resources/beta/index.d.ts.map +0 -1
  67. package/resources/beta/index.js +0 -9
  68. package/resources/beta/index.js.map +0 -1
  69. package/resources/beta/index.mjs +0 -4
  70. package/resources/beta/index.mjs.map +0 -1
  71. package/resources/beta/tools/index.d.ts +0 -3
  72. package/resources/beta/tools/index.d.ts.map +0 -1
  73. package/resources/beta/tools/index.js +0 -9
  74. package/resources/beta/tools/index.js.map +0 -1
  75. package/resources/beta/tools/index.mjs +0 -4
  76. package/resources/beta/tools/index.mjs.map +0 -1
  77. package/resources/beta/tools/messages.d.ts +0 -793
  78. package/resources/beta/tools/messages.d.ts.map +0 -1
  79. package/resources/beta/tools/messages.js +0 -29
  80. package/resources/beta/tools/messages.js.map +0 -1
  81. package/resources/beta/tools/messages.mjs +0 -24
  82. package/resources/beta/tools/messages.mjs.map +0 -1
  83. package/resources/beta/tools/tools.d.ts +0 -24
  84. package/resources/beta/tools/tools.d.ts.map +0 -1
  85. package/resources/beta/tools/tools.js +0 -40
  86. package/resources/beta/tools/tools.js.map +0 -1
  87. package/resources/beta/tools/tools.mjs +0 -13
  88. package/resources/beta/tools/tools.mjs.map +0 -1
  89. package/src/lib/ToolsBetaMessageStream.ts +0 -561
  90. package/src/resources/beta/beta.ts +0 -12
  91. package/src/resources/beta/index.ts +0 -4
  92. package/src/resources/beta/tools/index.ts +0 -21
  93. package/src/resources/beta/tools/messages.ts +0 -907
  94. package/src/resources/beta/tools/tools.ts +0 -27
@@ -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 './manual-types';
5
- import * as auto from "./auto/types";
6
- import { type RequestOptions } from '../core';
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
 
@@ -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 '../core';
14
- import { MultipartBody } from './MultipartBody';
15
- import { type Shims } from './registry';
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';
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3
3
  */
4
- import { type RequestOptions } from '../core';
4
+ import { type RequestOptions } from "../core.js";
5
5
 
6
6
  export interface Shims {
7
7
  kind: string;
@@ -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 './MultipartBody';
5
- import { type RequestOptions } from '../core';
6
- import { type Shims } from './registry';
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 './version';
2
- import { Stream } from './streaming';
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 './error';
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 './_shims/index';
20
+ } from "./_shims/index.js";
21
21
  export { type Response };
22
- import { isMultipartBody } from './uploads';
22
+ import { isMultipartBody } from "./uploads.js";
23
23
  export {
24
24
  maybeMultipartFormRequestOptions,
25
25
  multipartFormRequestOptions,
26
26
  createForm,
27
27
  type Uploadable,
28
- } from './uploads';
28
+ } from "./uploads.js";
29
29
 
30
30
  export type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>;
31
31
 
package/src/error.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
- import { castToError, Headers } from './core';
3
+ import { castToError, Headers } from "./core.js";
4
4
 
5
5
  export class AnthropicError extends Error {}
6
6
 
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 './core';
4
- import * as Errors from './error';
5
- import { type Agent } from './_shims/index';
6
- import * as Uploads from './uploads';
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;
@@ -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
- MessageStreamParams,
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: MessageStreamParams,
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
- if (event.delta.type === 'text_delta') {
373
- this._emit('text', event.delta.text, messageSnapshot.content.at(-1)!.text || '');
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,6 +1,6 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
- import * as Core from "./core";
3
+ import * as Core from "./core.js";
4
4
 
5
5
  export class APIResource {
6
6
  protected _client: Core.APIClient;
@@ -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
  /**
@@ -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 './completions';
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 './messages';
44
+ } from "./messages.js";