@anthropic-ai/sdk 0.102.0 → 0.103.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 (149) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/client.d.mts +30 -7
  3. package/client.d.mts.map +1 -1
  4. package/client.d.ts +30 -7
  5. package/client.d.ts.map +1 -1
  6. package/client.js +48 -25
  7. package/client.js.map +1 -1
  8. package/client.mjs +47 -24
  9. package/client.mjs.map +1 -1
  10. package/core/middleware.d.mts +42 -5
  11. package/core/middleware.d.mts.map +1 -1
  12. package/core/middleware.d.ts +42 -5
  13. package/core/middleware.d.ts.map +1 -1
  14. package/core/middleware.js +14 -7
  15. package/core/middleware.js.map +1 -1
  16. package/core/middleware.mjs +14 -7
  17. package/core/middleware.mjs.map +1 -1
  18. package/core/streaming.d.mts +9 -0
  19. package/core/streaming.d.mts.map +1 -1
  20. package/core/streaming.d.ts +9 -0
  21. package/core/streaming.d.ts.map +1 -1
  22. package/core/streaming.js +11 -0
  23. package/core/streaming.js.map +1 -1
  24. package/core/streaming.mjs +11 -0
  25. package/core/streaming.mjs.map +1 -1
  26. package/index.d.mts +1 -0
  27. package/index.d.mts.map +1 -1
  28. package/index.d.ts +1 -0
  29. package/index.d.ts.map +1 -1
  30. package/index.js +4 -1
  31. package/index.js.map +1 -1
  32. package/index.mjs +1 -0
  33. package/index.mjs.map +1 -1
  34. package/internal/parse.d.mts.map +1 -1
  35. package/internal/parse.d.ts.map +1 -1
  36. package/internal/parse.js +0 -3
  37. package/internal/parse.js.map +1 -1
  38. package/internal/parse.mjs +0 -3
  39. package/internal/parse.mjs.map +1 -1
  40. package/internal/request-options.d.mts +46 -3
  41. package/internal/request-options.d.mts.map +1 -1
  42. package/internal/request-options.d.ts +46 -3
  43. package/internal/request-options.d.ts.map +1 -1
  44. package/internal/request-options.js +12 -1
  45. package/internal/request-options.js.map +1 -1
  46. package/internal/request-options.mjs +10 -0
  47. package/internal/request-options.mjs.map +1 -1
  48. package/internal/utils/log.d.mts +11 -1
  49. package/internal/utils/log.d.mts.map +1 -1
  50. package/internal/utils/log.d.ts +11 -1
  51. package/internal/utils/log.d.ts.map +1 -1
  52. package/internal/utils/log.js +34 -9
  53. package/internal/utils/log.js.map +1 -1
  54. package/internal/utils/log.mjs +32 -8
  55. package/internal/utils/log.mjs.map +1 -1
  56. package/lib/BetaMessageStream.d.mts.map +1 -1
  57. package/lib/BetaMessageStream.d.ts.map +1 -1
  58. package/lib/BetaMessageStream.js +5 -0
  59. package/lib/BetaMessageStream.js.map +1 -1
  60. package/lib/BetaMessageStream.mjs +5 -0
  61. package/lib/BetaMessageStream.mjs.map +1 -1
  62. package/lib/middleware.d.mts +96 -0
  63. package/lib/middleware.d.mts.map +1 -0
  64. package/lib/middleware.d.ts +96 -0
  65. package/lib/middleware.d.ts.map +1 -0
  66. package/lib/middleware.js +703 -0
  67. package/lib/middleware.js.map +1 -0
  68. package/lib/middleware.mjs +698 -0
  69. package/lib/middleware.mjs.map +1 -0
  70. package/lib/tools/BetaToolRunner.d.mts +1 -1
  71. package/lib/tools/BetaToolRunner.d.mts.map +1 -1
  72. package/lib/tools/BetaToolRunner.d.ts +1 -1
  73. package/lib/tools/BetaToolRunner.d.ts.map +1 -1
  74. package/lib/tools/BetaToolRunner.js +10 -2
  75. package/lib/tools/BetaToolRunner.js.map +1 -1
  76. package/lib/tools/BetaToolRunner.mjs +10 -2
  77. package/lib/tools/BetaToolRunner.mjs.map +1 -1
  78. package/package.json +1 -1
  79. package/resources/beta/agents/agents.d.mts +1 -1
  80. package/resources/beta/agents/agents.d.mts.map +1 -1
  81. package/resources/beta/agents/agents.d.ts +1 -1
  82. package/resources/beta/agents/agents.d.ts.map +1 -1
  83. package/resources/beta/agents/agents.js.map +1 -1
  84. package/resources/beta/agents/agents.mjs.map +1 -1
  85. package/resources/beta/beta.d.mts +3 -3
  86. package/resources/beta/beta.d.mts.map +1 -1
  87. package/resources/beta/beta.d.ts +3 -3
  88. package/resources/beta/beta.d.ts.map +1 -1
  89. package/resources/beta/beta.js.map +1 -1
  90. package/resources/beta/beta.mjs.map +1 -1
  91. package/resources/beta/index.d.mts +1 -1
  92. package/resources/beta/index.d.mts.map +1 -1
  93. package/resources/beta/index.d.ts +1 -1
  94. package/resources/beta/index.d.ts.map +1 -1
  95. package/resources/beta/index.js.map +1 -1
  96. package/resources/beta/index.mjs.map +1 -1
  97. package/resources/beta/messages/batches.d.mts +28 -0
  98. package/resources/beta/messages/batches.d.mts.map +1 -1
  99. package/resources/beta/messages/batches.d.ts +28 -0
  100. package/resources/beta/messages/batches.d.ts.map +1 -1
  101. package/resources/beta/messages/index.d.mts +1 -1
  102. package/resources/beta/messages/index.d.mts.map +1 -1
  103. package/resources/beta/messages/index.d.ts +1 -1
  104. package/resources/beta/messages/index.d.ts.map +1 -1
  105. package/resources/beta/messages/index.js.map +1 -1
  106. package/resources/beta/messages/index.mjs.map +1 -1
  107. package/resources/beta/messages/messages.d.mts +234 -6
  108. package/resources/beta/messages/messages.d.mts.map +1 -1
  109. package/resources/beta/messages/messages.d.ts +234 -6
  110. package/resources/beta/messages/messages.d.ts.map +1 -1
  111. package/resources/beta/messages/messages.js +7 -0
  112. package/resources/beta/messages/messages.js.map +1 -1
  113. package/resources/beta/messages/messages.mjs +7 -0
  114. package/resources/beta/messages/messages.mjs.map +1 -1
  115. package/resources/beta/models.d.mts +6 -0
  116. package/resources/beta/models.d.mts.map +1 -1
  117. package/resources/beta/models.d.ts +6 -0
  118. package/resources/beta/models.d.ts.map +1 -1
  119. package/resources/messages/messages.d.mts +2 -2
  120. package/resources/messages/messages.d.mts.map +1 -1
  121. package/resources/messages/messages.d.ts +2 -2
  122. package/resources/messages/messages.d.ts.map +1 -1
  123. package/resources/messages/messages.js +1 -0
  124. package/resources/messages/messages.js.map +1 -1
  125. package/resources/messages/messages.mjs +1 -0
  126. package/resources/messages/messages.mjs.map +1 -1
  127. package/src/client.ts +55 -24
  128. package/src/core/middleware.ts +53 -7
  129. package/src/core/streaming.ts +15 -0
  130. package/src/index.ts +6 -0
  131. package/src/internal/parse.ts +0 -4
  132. package/src/internal/request-options.ts +48 -3
  133. package/src/internal/utils/log.ts +39 -9
  134. package/src/lib/BetaMessageStream.ts +5 -0
  135. package/src/lib/middleware.ts +937 -0
  136. package/src/lib/tools/BetaToolRunner.ts +12 -3
  137. package/src/resources/beta/agents/agents.ts +1 -0
  138. package/src/resources/beta/beta.ts +15 -1
  139. package/src/resources/beta/index.ts +6 -0
  140. package/src/resources/beta/messages/batches.ts +30 -0
  141. package/src/resources/beta/messages/index.ts +6 -0
  142. package/src/resources/beta/messages/messages.ts +279 -5
  143. package/src/resources/beta/models.ts +7 -0
  144. package/src/resources/messages/messages.ts +4 -1
  145. package/src/version.ts +1 -1
  146. package/version.d.mts +1 -1
  147. package/version.d.ts +1 -1
  148. package/version.js +1 -1
  149. package/version.mjs +1 -1
@@ -0,0 +1,96 @@
1
+ import type { Middleware } from "../core/middleware.mjs";
2
+ import type { AnthropicBeta } from "../resources/beta/beta.mjs";
3
+ import type { BetaFallbackParam, BetaRawMessageDeltaEvent } from "../resources/beta/messages/messages.mjs";
4
+ export { BetaFallbackState } from "../internal/request-options.mjs";
5
+ /** Why {@link BetaRefusalFallbackOptions.onError} fired. */
6
+ export type BetaRefusalFallbackError = {
7
+ /** The refusal carries no `fallback_credit_token`, so it can't be retried. */
8
+ kind: 'no_credit_token';
9
+ message: string;
10
+ /** The refusal `message_delta` event, verbatim. */
11
+ event: BetaRawMessageDeltaEvent;
12
+ } | {
13
+ /** The stream refused but every fallback entry has been used up. */
14
+ kind: 'chain_exhausted';
15
+ message: string;
16
+ /** The refusal `message_delta` event, verbatim. */
17
+ event: BetaRawMessageDeltaEvent;
18
+ } | {
19
+ /** A streaming fallback request failed; the hop was skipped. */
20
+ kind: 'request_failed';
21
+ message: string;
22
+ /** The fallback model whose request failed. */
23
+ model: string;
24
+ /** The HTTP status, or `null` when the request threw instead of resolving. */
25
+ status: number | null;
26
+ /** The parsed error body, or the thrown error when `status` is `null`. */
27
+ detail: unknown;
28
+ };
29
+ export interface BetaRefusalFallbackOptions {
30
+ /**
31
+ * Betas added to the `anthropic-beta` header of every `/v1/messages`
32
+ * request this middleware handles — the original request included, since
33
+ * refusals only carry a `fallback_credit_token` when the beta is enabled.
34
+ * Defaults to `['fallback-credit-2026-06-01']`; pass `[]` to send none.
35
+ */
36
+ betas?: readonly AnthropicBeta[] | undefined;
37
+ /**
38
+ * Called when a refusal is surfaced to the client rather than retried —
39
+ * it carries no `fallback_credit_token`, no fallback entries remain, or a
40
+ * streaming fallback request failed. Discriminate on `error.kind`.
41
+ * Defaults to logging through the client logger.
42
+ */
43
+ onError?: ((error: BetaRefusalFallbackError) => void) | undefined;
44
+ }
45
+ /**
46
+ * Middleware that retries refused `/v1/messages` requests down a fallback chain.
47
+ *
48
+ * Non-streaming: when a response comes back with `stop_reason: 'refusal'`, the
49
+ * request is retried with each entry of `fallbacks` merged over the original
50
+ * params — passing along the refusal's `fallback_credit_token` — until a model
51
+ * accepts or the chain is exhausted. A message served by a fallback carries a
52
+ * `fallback` content block prepended at each model boundary — the same seam
53
+ * block shape the server-side `fallbacks` param places in `content`, though
54
+ * the rest of the envelope is the serving hop's as returned (see the
55
+ * known-divergences note below); an exhausted chain surfaces the final
56
+ * refusal verbatim.
57
+ *
58
+ * Streaming: when the stream ends in `stop_reason: 'refusal'`, a second
59
+ * request is issued to the fallback model — carrying the refused model's
60
+ * partial output as a trailing assistant prefill when the refusal grants one
61
+ * (`fallback_has_prefill_claim`), plus the refusal's `fallback_credit_token`
62
+ * — and the fallback's events are spliced onto the
63
+ * still-open stream, so the client sees one continuous message in the
64
+ * server-side `fallbacks` wire shape: a `fallback` content block at each model
65
+ * boundary, monotonic block indices, and per-hop `usage.iterations` on the
66
+ * final `message_delta`. Only `model` is honored from each entry on this path:
67
+ * the credit token is redeemable only against the refused request's body, so
68
+ * the other per-entry overrides (`max_tokens`, `thinking`, ...) would be
69
+ * rejected.
70
+ *
71
+ * The fallback-credit beta the credit tokens require is sent by default on
72
+ * every request the middleware handles; the `betas` option controls this.
73
+ *
74
+ * In both modes a fallback that itself refuses with a fresh credit token
75
+ * continues down the chain. A streaming fallback whose prefill the server
76
+ * rejects (HTTP 400) is retried once without it; a fallback whose request
77
+ * fails outright is skipped — its token was never redeemed, so it carries to
78
+ * the next entry.
79
+ *
80
+ * To keep later requests on the model that accepted, pass a
81
+ * {@link BetaFallbackState} via the `fallbackState` request option; requests
82
+ * sharing that state start directly at the pinned fallback. Reuse one state
83
+ * across whatever scope the pin should apply to — typically a conversation.
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * const client = new Anthropic({
88
+ * middleware: [betaRefusalFallbackMiddleware([{ model: 'claude-opus-4-8' }])],
89
+ * });
90
+ *
91
+ * const fallbackState = new BetaFallbackState();
92
+ * const message = await client.beta.messages.create(params, { fallbackState });
93
+ * ```
94
+ */
95
+ export declare function betaRefusalFallbackMiddleware(fallbacks: readonly BetaFallbackParam[], options?: BetaRefusalFallbackOptions): Middleware;
96
+ //# sourceMappingURL=middleware.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.d.mts","sourceRoot":"","sources":["../src/lib/middleware.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAqC,+BAA2B;AAIxF,OAAO,KAAK,EAAE,aAAa,EAAE,mCAA+B;AAC5D,OAAO,KAAK,EAIV,iBAAiB,EAOjB,wBAAwB,EAMzB,gDAA4C;AAE7C,OAAO,EAAE,iBAAiB,EAAE,wCAAoC;AAwBhE,4DAA4D;AAC5D,MAAM,MAAM,wBAAwB,GAChC;IACE,8EAA8E;IAC9E,IAAI,EAAE,iBAAiB,CAAC;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,mDAAmD;IACnD,KAAK,EAAE,wBAAwB,CAAC;CACjC,GACD;IACE,oEAAoE;IACpE,IAAI,EAAE,iBAAiB,CAAC;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,mDAAmD;IACnD,KAAK,EAAE,wBAAwB,CAAC;CACjC,GACD;IACE,gEAAgE;IAChE,IAAI,EAAE,gBAAgB,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,+CAA+C;IAC/C,KAAK,EAAE,MAAM,CAAC;IACd,8EAA8E;IAC9E,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,0EAA0E;IAC1E,MAAM,EAAE,OAAO,CAAC;CACjB,CAAC;AAEN,MAAM,WAAW,0BAA0B;IACzC;;;;;OAKG;IACH,KAAK,CAAC,EAAE,SAAS,aAAa,EAAE,GAAG,SAAS,CAAC;IAE7C;;;;;OAKG;IACH,OAAO,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,wBAAwB,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC;CACnE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,wBAAgB,6BAA6B,CAC3C,SAAS,EAAE,SAAS,iBAAiB,EAAE,EACvC,OAAO,GAAE,0BAA+B,GACvC,UAAU,CA0JZ"}
@@ -0,0 +1,96 @@
1
+ import type { Middleware } from "../core/middleware.js";
2
+ import type { AnthropicBeta } from "../resources/beta/beta.js";
3
+ import type { BetaFallbackParam, BetaRawMessageDeltaEvent } from "../resources/beta/messages/messages.js";
4
+ export { BetaFallbackState } from "../internal/request-options.js";
5
+ /** Why {@link BetaRefusalFallbackOptions.onError} fired. */
6
+ export type BetaRefusalFallbackError = {
7
+ /** The refusal carries no `fallback_credit_token`, so it can't be retried. */
8
+ kind: 'no_credit_token';
9
+ message: string;
10
+ /** The refusal `message_delta` event, verbatim. */
11
+ event: BetaRawMessageDeltaEvent;
12
+ } | {
13
+ /** The stream refused but every fallback entry has been used up. */
14
+ kind: 'chain_exhausted';
15
+ message: string;
16
+ /** The refusal `message_delta` event, verbatim. */
17
+ event: BetaRawMessageDeltaEvent;
18
+ } | {
19
+ /** A streaming fallback request failed; the hop was skipped. */
20
+ kind: 'request_failed';
21
+ message: string;
22
+ /** The fallback model whose request failed. */
23
+ model: string;
24
+ /** The HTTP status, or `null` when the request threw instead of resolving. */
25
+ status: number | null;
26
+ /** The parsed error body, or the thrown error when `status` is `null`. */
27
+ detail: unknown;
28
+ };
29
+ export interface BetaRefusalFallbackOptions {
30
+ /**
31
+ * Betas added to the `anthropic-beta` header of every `/v1/messages`
32
+ * request this middleware handles — the original request included, since
33
+ * refusals only carry a `fallback_credit_token` when the beta is enabled.
34
+ * Defaults to `['fallback-credit-2026-06-01']`; pass `[]` to send none.
35
+ */
36
+ betas?: readonly AnthropicBeta[] | undefined;
37
+ /**
38
+ * Called when a refusal is surfaced to the client rather than retried —
39
+ * it carries no `fallback_credit_token`, no fallback entries remain, or a
40
+ * streaming fallback request failed. Discriminate on `error.kind`.
41
+ * Defaults to logging through the client logger.
42
+ */
43
+ onError?: ((error: BetaRefusalFallbackError) => void) | undefined;
44
+ }
45
+ /**
46
+ * Middleware that retries refused `/v1/messages` requests down a fallback chain.
47
+ *
48
+ * Non-streaming: when a response comes back with `stop_reason: 'refusal'`, the
49
+ * request is retried with each entry of `fallbacks` merged over the original
50
+ * params — passing along the refusal's `fallback_credit_token` — until a model
51
+ * accepts or the chain is exhausted. A message served by a fallback carries a
52
+ * `fallback` content block prepended at each model boundary — the same seam
53
+ * block shape the server-side `fallbacks` param places in `content`, though
54
+ * the rest of the envelope is the serving hop's as returned (see the
55
+ * known-divergences note below); an exhausted chain surfaces the final
56
+ * refusal verbatim.
57
+ *
58
+ * Streaming: when the stream ends in `stop_reason: 'refusal'`, a second
59
+ * request is issued to the fallback model — carrying the refused model's
60
+ * partial output as a trailing assistant prefill when the refusal grants one
61
+ * (`fallback_has_prefill_claim`), plus the refusal's `fallback_credit_token`
62
+ * — and the fallback's events are spliced onto the
63
+ * still-open stream, so the client sees one continuous message in the
64
+ * server-side `fallbacks` wire shape: a `fallback` content block at each model
65
+ * boundary, monotonic block indices, and per-hop `usage.iterations` on the
66
+ * final `message_delta`. Only `model` is honored from each entry on this path:
67
+ * the credit token is redeemable only against the refused request's body, so
68
+ * the other per-entry overrides (`max_tokens`, `thinking`, ...) would be
69
+ * rejected.
70
+ *
71
+ * The fallback-credit beta the credit tokens require is sent by default on
72
+ * every request the middleware handles; the `betas` option controls this.
73
+ *
74
+ * In both modes a fallback that itself refuses with a fresh credit token
75
+ * continues down the chain. A streaming fallback whose prefill the server
76
+ * rejects (HTTP 400) is retried once without it; a fallback whose request
77
+ * fails outright is skipped — its token was never redeemed, so it carries to
78
+ * the next entry.
79
+ *
80
+ * To keep later requests on the model that accepted, pass a
81
+ * {@link BetaFallbackState} via the `fallbackState` request option; requests
82
+ * sharing that state start directly at the pinned fallback. Reuse one state
83
+ * across whatever scope the pin should apply to — typically a conversation.
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * const client = new Anthropic({
88
+ * middleware: [betaRefusalFallbackMiddleware([{ model: 'claude-opus-4-8' }])],
89
+ * });
90
+ *
91
+ * const fallbackState = new BetaFallbackState();
92
+ * const message = await client.beta.messages.create(params, { fallbackState });
93
+ * ```
94
+ */
95
+ export declare function betaRefusalFallbackMiddleware(fallbacks: readonly BetaFallbackParam[], options?: BetaRefusalFallbackOptions): Middleware;
96
+ //# sourceMappingURL=middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/lib/middleware.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAqC,8BAA2B;AAIxF,OAAO,KAAK,EAAE,aAAa,EAAE,kCAA+B;AAC5D,OAAO,KAAK,EAIV,iBAAiB,EAOjB,wBAAwB,EAMzB,+CAA4C;AAE7C,OAAO,EAAE,iBAAiB,EAAE,uCAAoC;AAwBhE,4DAA4D;AAC5D,MAAM,MAAM,wBAAwB,GAChC;IACE,8EAA8E;IAC9E,IAAI,EAAE,iBAAiB,CAAC;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,mDAAmD;IACnD,KAAK,EAAE,wBAAwB,CAAC;CACjC,GACD;IACE,oEAAoE;IACpE,IAAI,EAAE,iBAAiB,CAAC;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,mDAAmD;IACnD,KAAK,EAAE,wBAAwB,CAAC;CACjC,GACD;IACE,gEAAgE;IAChE,IAAI,EAAE,gBAAgB,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,+CAA+C;IAC/C,KAAK,EAAE,MAAM,CAAC;IACd,8EAA8E;IAC9E,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,0EAA0E;IAC1E,MAAM,EAAE,OAAO,CAAC;CACjB,CAAC;AAEN,MAAM,WAAW,0BAA0B;IACzC;;;;;OAKG;IACH,KAAK,CAAC,EAAE,SAAS,aAAa,EAAE,GAAG,SAAS,CAAC;IAE7C;;;;;OAKG;IACH,OAAO,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,wBAAwB,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC;CACnE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,wBAAgB,6BAA6B,CAC3C,SAAS,EAAE,SAAS,iBAAiB,EAAE,EACvC,OAAO,GAAE,0BAA+B,GACvC,UAAU,CA0JZ"}