@anthropic-ai/sdk 0.55.0 → 0.55.1

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 (43) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/internal/types.d.mts +8 -6
  3. package/internal/types.d.mts.map +1 -1
  4. package/internal/types.d.ts +8 -6
  5. package/internal/types.d.ts.map +1 -1
  6. package/internal/uploads.js +1 -1
  7. package/internal/uploads.js.map +1 -1
  8. package/internal/uploads.mjs +1 -1
  9. package/internal/uploads.mjs.map +1 -1
  10. package/internal/utils/log.js +1 -1
  11. package/internal/utils/log.js.map +1 -1
  12. package/internal/utils/log.mjs +1 -1
  13. package/internal/utils/log.mjs.map +1 -1
  14. package/internal/utils/path.d.mts.map +1 -1
  15. package/internal/utils/path.d.ts.map +1 -1
  16. package/internal/utils/path.js +26 -5
  17. package/internal/utils/path.js.map +1 -1
  18. package/internal/utils/path.mjs +26 -5
  19. package/internal/utils/path.mjs.map +1 -1
  20. package/lib/BetaMessageStream.d.mts.map +1 -1
  21. package/lib/BetaMessageStream.d.ts.map +1 -1
  22. package/lib/BetaMessageStream.js +20 -7
  23. package/lib/BetaMessageStream.js.map +1 -1
  24. package/lib/BetaMessageStream.mjs +20 -7
  25. package/lib/BetaMessageStream.mjs.map +1 -1
  26. package/lib/MessageStream.d.mts.map +1 -1
  27. package/lib/MessageStream.d.ts.map +1 -1
  28. package/lib/MessageStream.js +21 -8
  29. package/lib/MessageStream.js.map +1 -1
  30. package/lib/MessageStream.mjs +21 -8
  31. package/lib/MessageStream.mjs.map +1 -1
  32. package/package.json +1 -1
  33. package/src/internal/types.ts +9 -6
  34. package/src/internal/uploads.ts +1 -1
  35. package/src/internal/utils/log.ts +1 -1
  36. package/src/internal/utils/path.ts +30 -7
  37. package/src/lib/BetaMessageStream.ts +20 -7
  38. package/src/lib/MessageStream.ts +21 -8
  39. package/src/version.ts +1 -1
  40. package/version.d.mts +1 -1
  41. package/version.d.ts +1 -1
  42. package/version.js +1 -1
  43. package/version.mjs +1 -1
@@ -7,7 +7,7 @@ export type KeysEnum<T> = { [P in keyof Required<T>]: true };
7
7
 
8
8
  export type FinalizedRequestInit = RequestInit & { headers: Headers };
9
9
 
10
- type NotAny<T> = [unknown] extends [T] ? never : T;
10
+ type NotAny<T> = [0] extends [1 & T] ? never : T;
11
11
 
12
12
  /**
13
13
  * Some environments overload the global fetch function, and Parameters<T> only gets the last signature.
@@ -64,13 +64,15 @@ type OverloadedParameters<T> =
64
64
  * [1]: https://www.typescriptlang.org/tsconfig/#typeAcquisition
65
65
  */
66
66
  /** @ts-ignore For users with \@types/node */
67
- type UndiciTypesRequestInit = NotAny<import('../node_modules/undici-types').RequestInit> | NotAny<import('../../node_modules/undici-types').RequestInit> | NotAny<import('../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici-types').RequestInit>;
67
+ type UndiciTypesRequestInit = NotAny<import('../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit>;
68
68
  /** @ts-ignore For users with undici */
69
- type UndiciRequestInit = NotAny<import('../node_modules/undici').RequestInit> | NotAny<import('../../node_modules/undici').RequestInit> | NotAny<import('../../../node_modules/undici').RequestInit> | NotAny<import('../../../../node_modules/undici').RequestInit> | NotAny<import('../../../../../node_modules/undici').RequestInit> | NotAny<import('../../../../../../node_modules/undici').RequestInit> | NotAny<import('../../../../../../../node_modules/undici').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici').RequestInit>;
69
+ type UndiciRequestInit = NotAny<import('../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici/index.d.ts').RequestInit>;
70
70
  /** @ts-ignore For users with \@types/bun */
71
71
  type BunRequestInit = globalThis.FetchRequestInit;
72
- /** @ts-ignore For users with node-fetch */
73
- type NodeFetchRequestInit = NotAny<import('../node_modules/node-fetch').RequestInit> | NotAny<import('../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/node-fetch').RequestInit>;
72
+ /** @ts-ignore For users with node-fetch@2 */
73
+ type NodeFetch2RequestInit = NotAny<import('../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit>;
74
+ /** @ts-ignore For users with node-fetch@3, doesn't need file extension because types are at ./@types/index.d.ts */
75
+ type NodeFetch3RequestInit = NotAny<import('../node_modules/node-fetch').RequestInit> | NotAny<import('../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/node-fetch').RequestInit>;
74
76
  /** @ts-ignore For users who use Deno */
75
77
  type FetchRequestInit = NonNullable<OverloadedParameters<typeof fetch>[1]>;
76
78
  /* eslint-enable */
@@ -79,7 +81,8 @@ type RequestInits =
79
81
  | NotAny<UndiciTypesRequestInit>
80
82
  | NotAny<UndiciRequestInit>
81
83
  | NotAny<BunRequestInit>
82
- | NotAny<NodeFetchRequestInit>
84
+ | NotAny<NodeFetch2RequestInit>
85
+ | NotAny<NodeFetch3RequestInit>
83
86
  | NotAny<RequestInit>
84
87
  | NotAny<FetchRequestInit>;
85
88
 
@@ -90,7 +90,7 @@ export const multipartFormRequestOptions = async (
90
90
  return { ...opts, body: await createForm(opts.body, fetch) };
91
91
  };
92
92
 
93
- const supportsFormDataMap = /** @__PURE__ */ new WeakMap<Fetch, Promise<boolean>>();
93
+ const supportsFormDataMap = /* @__PURE__ */ new WeakMap<Fetch, Promise<boolean>>();
94
94
 
95
95
  /**
96
96
  * node-fetch doesn't support the global FormData object in recent node versions. Instead of sending
@@ -58,7 +58,7 @@ const noopLogger = {
58
58
  debug: noop,
59
59
  };
60
60
 
61
- let cachedLoggers = /** @__PURE__ */ new WeakMap<Logger, [LogLevel, Logger]>();
61
+ let cachedLoggers = /* @__PURE__ */ new WeakMap<Logger, [LogLevel, Logger]>();
62
62
 
63
63
  export function loggerFor(client: BaseAnthropic): Logger {
64
64
  const logger = client.logger;
@@ -12,25 +12,43 @@ export function encodeURIPath(str: string) {
12
12
  return str.replace(/[^A-Za-z0-9\-._~!$&'()*+,;=:@]+/g, encodeURIComponent);
13
13
  }
14
14
 
15
+ const EMPTY = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.create(null));
16
+
15
17
  export const createPathTagFunction = (pathEncoder = encodeURIPath) =>
16
18
  function path(statics: readonly string[], ...params: readonly unknown[]): string {
17
19
  // If there are no params, no processing is needed.
18
20
  if (statics.length === 1) return statics[0]!;
19
21
 
20
22
  let postPath = false;
23
+ const invalidSegments = [];
21
24
  const path = statics.reduce((previousValue, currentValue, index) => {
22
25
  if (/[?#]/.test(currentValue)) {
23
26
  postPath = true;
24
27
  }
25
- return (
26
- previousValue +
27
- currentValue +
28
- (index === params.length ? '' : (postPath ? encodeURIComponent : pathEncoder)(String(params[index])))
29
- );
28
+ const value = params[index];
29
+ let encoded = (postPath ? encodeURIComponent : pathEncoder)('' + value);
30
+ if (
31
+ index !== params.length &&
32
+ (value == null ||
33
+ (typeof value === 'object' &&
34
+ // handle values from other realms
35
+ value.toString ===
36
+ Object.getPrototypeOf(Object.getPrototypeOf((value as any).hasOwnProperty ?? EMPTY) ?? EMPTY)
37
+ ?.toString))
38
+ ) {
39
+ encoded = value + '';
40
+ invalidSegments.push({
41
+ start: previousValue.length + currentValue.length,
42
+ length: encoded.length,
43
+ error: `Value of type ${Object.prototype.toString
44
+ .call(value)
45
+ .slice(8, -1)} is not a valid path parameter`,
46
+ });
47
+ }
48
+ return previousValue + currentValue + (index === params.length ? '' : encoded);
30
49
  }, '');
31
50
 
32
51
  const pathOnly = path.split(/[?#]/, 1)[0]!;
33
- const invalidSegments = [];
34
52
  const invalidSegmentPattern = /(?<=^|\/)(?:\.|%2e){1,2}(?=\/|$)/gi;
35
53
  let match;
36
54
 
@@ -39,9 +57,12 @@ export const createPathTagFunction = (pathEncoder = encodeURIPath) =>
39
57
  invalidSegments.push({
40
58
  start: match.index,
41
59
  length: match[0].length,
60
+ error: `Value "${match[0]}" can\'t be safely passed as a path parameter`,
42
61
  });
43
62
  }
44
63
 
64
+ invalidSegments.sort((a, b) => a.start - b.start);
65
+
45
66
  if (invalidSegments.length > 0) {
46
67
  let lastEnd = 0;
47
68
  const underline = invalidSegments.reduce((acc, segment) => {
@@ -52,7 +73,9 @@ export const createPathTagFunction = (pathEncoder = encodeURIPath) =>
52
73
  }, '');
53
74
 
54
75
  throw new AnthropicError(
55
- `Path parameters result in path with invalid segments:\n${path}\n${underline}`,
76
+ `Path parameters result in path with invalid segments:\n${invalidSegments
77
+ .map((e) => e.error)
78
+ .join('\n')}\n${path}\n${underline}`,
56
79
  );
57
80
  }
58
81
 
@@ -583,14 +583,19 @@ export class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent>
583
583
  switch (event.delta.type) {
584
584
  case 'text_delta': {
585
585
  if (snapshotContent?.type === 'text') {
586
- snapshotContent.text += event.delta.text;
586
+ snapshot.content[event.index] = {
587
+ ...snapshotContent,
588
+ text: (snapshotContent.text || '') + event.delta.text,
589
+ };
587
590
  }
588
591
  break;
589
592
  }
590
593
  case 'citations_delta': {
591
594
  if (snapshotContent?.type === 'text') {
592
- snapshotContent.citations ??= [];
593
- snapshotContent.citations.push(event.delta.citation);
595
+ snapshot.content[event.index] = {
596
+ ...snapshotContent,
597
+ citations: [...(snapshotContent.citations ?? []), event.delta.citation],
598
+ };
594
599
  }
595
600
  break;
596
601
  }
@@ -602,7 +607,8 @@ export class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent>
602
607
  let jsonBuf = (snapshotContent as any)[JSON_BUF_PROPERTY] || '';
603
608
  jsonBuf += event.delta.partial_json;
604
609
 
605
- Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY, {
610
+ const newContent = { ...snapshotContent };
611
+ Object.defineProperty(newContent, JSON_BUF_PROPERTY, {
606
612
  value: jsonBuf,
607
613
  enumerable: false,
608
614
  writable: true,
@@ -610,7 +616,7 @@ export class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent>
610
616
 
611
617
  if (jsonBuf) {
612
618
  try {
613
- snapshotContent.input = partialParse(jsonBuf);
619
+ newContent.input = partialParse(jsonBuf);
614
620
  } catch (err) {
615
621
  const error = new AnthropicError(
616
622
  `Unable to parse tool parameter JSON from model. Please retry your request or adjust your prompt. Error: ${err}. JSON: ${jsonBuf}`,
@@ -618,18 +624,25 @@ export class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent>
618
624
  this.#handleError(error);
619
625
  }
620
626
  }
627
+ snapshot.content[event.index] = newContent;
621
628
  }
622
629
  break;
623
630
  }
624
631
  case 'thinking_delta': {
625
632
  if (snapshotContent?.type === 'thinking') {
626
- snapshotContent.thinking += event.delta.thinking;
633
+ snapshot.content[event.index] = {
634
+ ...snapshotContent,
635
+ thinking: snapshotContent.thinking + event.delta.thinking,
636
+ };
627
637
  }
628
638
  break;
629
639
  }
630
640
  case 'signature_delta': {
631
641
  if (snapshotContent?.type === 'thinking') {
632
- snapshotContent.signature = event.delta.signature;
642
+ snapshot.content[event.index] = {
643
+ ...snapshotContent,
644
+ signature: event.delta.signature,
645
+ };
633
646
  }
634
647
  break;
635
648
  }
@@ -574,7 +574,7 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
574
574
 
575
575
  return snapshot;
576
576
  case 'content_block_start':
577
- snapshot.content.push(event.content_block);
577
+ snapshot.content.push({ ...event.content_block });
578
578
  return snapshot;
579
579
  case 'content_block_delta': {
580
580
  const snapshotContent = snapshot.content.at(event.index);
@@ -582,14 +582,19 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
582
582
  switch (event.delta.type) {
583
583
  case 'text_delta': {
584
584
  if (snapshotContent?.type === 'text') {
585
- snapshotContent.text += event.delta.text;
585
+ snapshot.content[event.index] = {
586
+ ...snapshotContent,
587
+ text: (snapshotContent.text || '') + event.delta.text,
588
+ };
586
589
  }
587
590
  break;
588
591
  }
589
592
  case 'citations_delta': {
590
593
  if (snapshotContent?.type === 'text') {
591
- snapshotContent.citations ??= [];
592
- snapshotContent.citations.push(event.delta.citation);
594
+ snapshot.content[event.index] = {
595
+ ...snapshotContent,
596
+ citations: [...(snapshotContent.citations ?? []), event.delta.citation],
597
+ };
593
598
  }
594
599
  break;
595
600
  }
@@ -601,27 +606,35 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
601
606
  let jsonBuf = (snapshotContent as any)[JSON_BUF_PROPERTY] || '';
602
607
  jsonBuf += event.delta.partial_json;
603
608
 
604
- Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY, {
609
+ const newContent = { ...snapshotContent };
610
+ Object.defineProperty(newContent, JSON_BUF_PROPERTY, {
605
611
  value: jsonBuf,
606
612
  enumerable: false,
607
613
  writable: true,
608
614
  });
609
615
 
610
616
  if (jsonBuf) {
611
- snapshotContent.input = partialParse(jsonBuf);
617
+ newContent.input = partialParse(jsonBuf);
612
618
  }
619
+ snapshot.content[event.index] = newContent;
613
620
  }
614
621
  break;
615
622
  }
616
623
  case 'thinking_delta': {
617
624
  if (snapshotContent?.type === 'thinking') {
618
- snapshotContent.thinking += event.delta.thinking;
625
+ snapshot.content[event.index] = {
626
+ ...snapshotContent,
627
+ thinking: snapshotContent.thinking + event.delta.thinking,
628
+ };
619
629
  }
620
630
  break;
621
631
  }
622
632
  case 'signature_delta': {
623
633
  if (snapshotContent?.type === 'thinking') {
624
- snapshotContent.signature = event.delta.signature;
634
+ snapshot.content[event.index] = {
635
+ ...snapshotContent,
636
+ signature: event.delta.signature,
637
+ };
625
638
  }
626
639
  break;
627
640
  }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '0.55.0'; // x-release-please-version
1
+ export const VERSION = '0.55.1'; // x-release-please-version
package/version.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "0.55.0";
1
+ export declare const VERSION = "0.55.1";
2
2
  //# sourceMappingURL=version.d.mts.map
package/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "0.55.0";
1
+ export declare const VERSION = "0.55.1";
2
2
  //# sourceMappingURL=version.d.ts.map
package/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = '0.55.0'; // x-release-please-version
4
+ exports.VERSION = '0.55.1'; // x-release-please-version
5
5
  //# sourceMappingURL=version.js.map
package/version.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export const VERSION = '0.55.0'; // x-release-please-version
1
+ export const VERSION = '0.55.1'; // x-release-please-version
2
2
  //# sourceMappingURL=version.mjs.map