@opencode/ai 2.2.5 → 2.2.6

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 (264) hide show
  1. package/README.md +133 -69
  2. package/dist/cache-policy.d.ts +1 -1
  3. package/dist/cache-policy.js +16 -4
  4. package/dist/decision-client.d.ts +3 -3
  5. package/dist/decision.d.ts +20 -125
  6. package/dist/decision.js +25 -15
  7. package/dist/effect.d.ts +4 -15
  8. package/dist/effect.js +2 -6
  9. package/dist/effort-updates.d.ts +3 -3
  10. package/dist/effort-updates.js +2 -2
  11. package/dist/experimental/cloudflare-run-extraction.d.ts +7 -3
  12. package/dist/experimental/cloudflare-run-extraction.js +4 -1
  13. package/dist/experimental/extraction.d.ts +10 -40
  14. package/dist/experimental/extraction.js +14 -13
  15. package/dist/experimental.d.ts +8 -8
  16. package/dist/experimental.js +4 -4
  17. package/dist/generation.d.ts +0 -2
  18. package/dist/generation.js +2 -2
  19. package/dist/image-client.d.ts +13 -7
  20. package/dist/image-client.js +1 -1
  21. package/dist/image.d.ts +357 -464
  22. package/dist/image.js +17 -15
  23. package/dist/index.d.ts +28 -465
  24. package/dist/index.js +18 -17
  25. package/dist/llm.d.ts +65 -266
  26. package/dist/llm.js +101 -92
  27. package/dist/loop.d.ts +38 -0
  28. package/dist/loop.js +136 -0
  29. package/dist/media-model.d.ts +1 -1
  30. package/dist/media-model.js +8 -6
  31. package/dist/media.d.ts +9 -0
  32. package/dist/media.js +37 -2
  33. package/dist/protocols/alibaba-chat.d.ts +5 -1
  34. package/dist/protocols/alibaba-chat.js +3 -3
  35. package/dist/protocols/alibaba-images.d.ts +2 -2
  36. package/dist/protocols/alibaba-messages.js +3 -3
  37. package/dist/protocols/alibaba-responses.js +8 -20
  38. package/dist/protocols/anthropic-messages.d.ts +1 -1
  39. package/dist/protocols/anthropic-messages.js +54 -173
  40. package/dist/protocols/assemblyai-transcription.d.ts +2 -2
  41. package/dist/protocols/bedrock-converse.js +10 -16
  42. package/dist/protocols/cartesia-speech.d.ts +2 -2
  43. package/dist/protocols/cloudflare-workers-ai-images.d.ts +2 -2
  44. package/dist/protocols/cohere-chat.js +9 -12
  45. package/dist/protocols/deepgram-speech.d.ts +2 -2
  46. package/dist/protocols/deepgram-transcription.d.ts +2 -2
  47. package/dist/protocols/elevenlabs-speech.d.ts +2 -2
  48. package/dist/protocols/elevenlabs-transcription.d.ts +2 -2
  49. package/dist/protocols/gemini.d.ts +3 -0
  50. package/dist/protocols/gemini.js +54 -72
  51. package/dist/protocols/google-images.d.ts +2 -2
  52. package/dist/protocols/google-interactions.d.ts +3 -4
  53. package/dist/protocols/google-interactions.js +44 -30
  54. package/dist/protocols/google-speech.d.ts +2 -2
  55. package/dist/protocols/google-transcription.d.ts +2 -2
  56. package/dist/protocols/google-video.d.ts +2 -2
  57. package/dist/protocols/meta-images.d.ts +2 -2
  58. package/dist/protocols/meta-responses.js +26 -23
  59. package/dist/protocols/minimax-images.d.ts +2 -2
  60. package/dist/protocols/mistral-chat.js +7 -10
  61. package/dist/protocols/open-responses-channel.js +6 -19
  62. package/dist/protocols/open-responses-continuation.d.ts +1 -5
  63. package/dist/protocols/open-responses-continuation.js +1 -3
  64. package/dist/protocols/open-responses.d.ts +1 -21
  65. package/dist/protocols/open-responses.js +10 -15
  66. package/dist/protocols/openai-chat.d.ts +3 -23
  67. package/dist/protocols/openai-chat.js +26 -47
  68. package/dist/protocols/openai-compatible-chat.d.ts +1 -1
  69. package/dist/protocols/openai-compatible-responses.d.ts +2 -2
  70. package/dist/protocols/openai-compatible-responses.js +1 -1
  71. package/dist/protocols/openai-decisions.js +1 -1
  72. package/dist/protocols/openai-images.d.ts +2 -2
  73. package/dist/protocols/openai-responses.js +10 -29
  74. package/dist/protocols/openai-speech.d.ts +2 -2
  75. package/dist/protocols/openai-transcription.d.ts +2 -2
  76. package/dist/protocols/runway-video.d.ts +2 -2
  77. package/dist/protocols/shared.d.ts +1 -4
  78. package/dist/protocols/shared.js +13 -11
  79. package/dist/protocols/system-one.d.ts +4 -4
  80. package/dist/protocols/utils/bedrock-cache.js +2 -5
  81. package/dist/protocols/utils/bedrock-media.d.ts +0 -3
  82. package/dist/protocols/utils/cache.d.ts +3 -0
  83. package/dist/protocols/utils/cache.js +15 -2
  84. package/dist/protocols/utils/image-sizing.d.ts +1 -1
  85. package/dist/protocols/utils/responses-checkpoint.js +1 -1
  86. package/dist/protocols/utils/responses-compaction.d.ts +1 -1
  87. package/dist/protocols/utils/responses-compaction.js +6 -5
  88. package/dist/protocols/utils/responses-hosted-tools.d.ts +97 -1
  89. package/dist/protocols/utils/responses-hosted-tools.js +31 -0
  90. package/dist/protocols/utils/tool-stream.d.ts +5 -271
  91. package/dist/protocols/utils/tool-stream.js +3 -13
  92. package/dist/protocols/venice-chat.d.ts +3 -1
  93. package/dist/protocols/venice-chat.js +4 -4
  94. package/dist/protocols/xai-images.d.ts +2 -2
  95. package/dist/protocols/xai-responses.js +5 -25
  96. package/dist/protocols/xai-video.d.ts +2 -2
  97. package/dist/protocols/zai-chat.d.ts +5 -1
  98. package/dist/protocols/zai-chat.js +1 -1
  99. package/dist/protocols/zai-images.d.ts +2 -2
  100. package/dist/protocols/zai-messages.js +3 -3
  101. package/dist/provider-error.d.ts +0 -1
  102. package/dist/provider-error.js +7 -80
  103. package/dist/providers/alibaba.d.ts +67 -4
  104. package/dist/providers/alibaba.js +6 -1
  105. package/dist/providers/amazon-bedrock-mantle.d.ts +6 -3
  106. package/dist/providers/amazon-bedrock-mantle.js +6 -3
  107. package/dist/providers/amazon-bedrock.d.ts +6 -7
  108. package/dist/providers/amazon-bedrock.js +1 -1
  109. package/dist/providers/anthropic-compatible.d.ts +7 -8
  110. package/dist/providers/anthropic-compatible.js +2 -5
  111. package/dist/providers/anthropic.d.ts +8 -9
  112. package/dist/providers/anthropic.js +2 -7
  113. package/dist/providers/assemblyai.d.ts +2 -2
  114. package/dist/providers/assemblyai.js +2 -7
  115. package/dist/providers/azure/messages.d.ts +2 -0
  116. package/dist/providers/azure/messages.js +1 -0
  117. package/dist/providers/azure.d.ts +322 -7
  118. package/dist/providers/azure.js +25 -9
  119. package/dist/providers/baseten.d.ts +5 -5
  120. package/dist/providers/baseten.js +7 -21
  121. package/dist/providers/cartesia.d.ts +1 -1
  122. package/dist/providers/cerebras.d.ts +5 -5
  123. package/dist/providers/cerebras.js +4 -15
  124. package/dist/providers/cloudflare-ai-gateway.d.ts +21 -13
  125. package/dist/providers/cloudflare-ai-gateway.js +28 -40
  126. package/dist/providers/cloudflare-shared.d.ts +9 -0
  127. package/dist/providers/cloudflare-shared.js +25 -0
  128. package/dist/providers/cloudflare-workers-ai.d.ts +4 -4
  129. package/dist/providers/cloudflare-workers-ai.js +4 -20
  130. package/dist/providers/cohere/chat.js +2 -7
  131. package/dist/providers/cohere.d.ts +6 -5
  132. package/dist/providers/cohere.js +7 -13
  133. package/dist/providers/deepgram.d.ts +2 -2
  134. package/dist/providers/deepgram.js +2 -7
  135. package/dist/providers/deepinfra.d.ts +5 -5
  136. package/dist/providers/deepinfra.js +5 -13
  137. package/dist/providers/deepseek.d.ts +5 -5
  138. package/dist/providers/deepseek.js +3 -14
  139. package/dist/providers/digitalocean.d.ts +8 -5
  140. package/dist/providers/digitalocean.js +13 -14
  141. package/dist/providers/elevenlabs.d.ts +2 -2
  142. package/dist/providers/elevenlabs.js +2 -7
  143. package/dist/providers/fireworks.d.ts +5 -5
  144. package/dist/providers/fireworks.js +4 -15
  145. package/dist/providers/google/interactions.d.ts +1 -1
  146. package/dist/providers/google/interactions.js +2 -7
  147. package/dist/providers/google-vertex-chat.d.ts +3 -3
  148. package/dist/providers/google-vertex-chat.js +3 -29
  149. package/dist/providers/google-vertex-interactions.d.ts +3 -3
  150. package/dist/providers/google-vertex-messages.d.ts +3 -3
  151. package/dist/providers/google-vertex-messages.js +3 -29
  152. package/dist/providers/google-vertex-mistral.d.ts +3 -3
  153. package/dist/providers/google-vertex-mistral.js +3 -29
  154. package/dist/providers/google-vertex-responses.d.ts +3 -3
  155. package/dist/providers/google-vertex-responses.js +4 -30
  156. package/dist/providers/google-vertex-shared.d.ts +31 -0
  157. package/dist/providers/google-vertex-shared.js +25 -0
  158. package/dist/providers/google-vertex.d.ts +3 -3
  159. package/dist/providers/google.d.ts +15 -13
  160. package/dist/providers/google.js +9 -19
  161. package/dist/providers/groq.d.ts +5 -5
  162. package/dist/providers/groq.js +4 -15
  163. package/dist/providers/meta.d.ts +5 -5
  164. package/dist/providers/meta.js +10 -23
  165. package/dist/providers/minimax/chat.js +2 -7
  166. package/dist/providers/minimax/responses.js +2 -7
  167. package/dist/providers/minimax.d.ts +5 -5
  168. package/dist/providers/minimax.js +7 -22
  169. package/dist/providers/mistral.d.ts +10 -11
  170. package/dist/providers/mistral.js +3 -14
  171. package/dist/providers/moonshot/messages.js +2 -7
  172. package/dist/providers/moonshot/responses.js +2 -7
  173. package/dist/providers/moonshot.d.ts +5 -5
  174. package/dist/providers/moonshot.js +4 -16
  175. package/dist/providers/openai-compatible-responses.d.ts +6 -7
  176. package/dist/providers/openai-compatible-responses.js +2 -8
  177. package/dist/providers/openai-compatible.d.ts +6 -7
  178. package/dist/providers/openai-compatible.js +4 -10
  179. package/dist/providers/openai.d.ts +4 -4
  180. package/dist/providers/openai.js +3 -3
  181. package/dist/providers/opencode.d.ts +4 -4
  182. package/dist/providers/opencode.js +6 -2
  183. package/dist/providers/openrouter.d.ts +8 -5
  184. package/dist/providers/openrouter.js +16 -24
  185. package/dist/providers/runway.d.ts +1 -1
  186. package/dist/providers/settings.d.ts +40 -0
  187. package/dist/providers/settings.js +19 -0
  188. package/dist/providers/togetherai.d.ts +5 -5
  189. package/dist/providers/togetherai.js +5 -15
  190. package/dist/providers/typesafe-ai.d.ts +4 -4
  191. package/dist/providers/typesafe-ai.js +6 -2
  192. package/dist/providers/venice.d.ts +4 -4
  193. package/dist/providers/venice.js +6 -2
  194. package/dist/providers/vercel-ai-gateway.d.ts +5 -5
  195. package/dist/providers/vercel-ai-gateway.js +15 -28
  196. package/dist/providers/xai.d.ts +4 -4
  197. package/dist/providers/xai.js +4 -12
  198. package/dist/providers/zai-coding-plan/messages.js +2 -7
  199. package/dist/providers/zai-coding-plan/responses.js +2 -7
  200. package/dist/providers/zai-coding-plan.d.ts +5 -5
  201. package/dist/providers/zai-coding-plan.js +8 -15
  202. package/dist/providers/zai.d.ts +5 -5
  203. package/dist/providers/zai.js +10 -22
  204. package/dist/public.d.ts +12 -0
  205. package/dist/public.js +5 -0
  206. package/dist/route/auth-options.d.ts +9 -5
  207. package/dist/route/auth-options.js +10 -7
  208. package/dist/route/client.d.ts +50 -37
  209. package/dist/route/client.js +42 -53
  210. package/dist/route/decision.d.ts +8 -8
  211. package/dist/route/decision.js +3 -3
  212. package/dist/route/executor.d.ts +5 -0
  213. package/dist/route/executor.js +10 -0
  214. package/dist/route/index.d.ts +5 -7
  215. package/dist/route/index.js +1 -1
  216. package/dist/route/media.d.ts +7 -11
  217. package/dist/route/media.js +22 -26
  218. package/dist/route/transport/http.js +6 -5
  219. package/dist/route/transport/websocket.js +7 -1
  220. package/dist/schema/errors.d.ts +16 -17
  221. package/dist/schema/errors.js +63 -12
  222. package/dist/schema/events.d.ts +2563 -2954
  223. package/dist/schema/events.js +54 -109
  224. package/dist/schema/messages.d.ts +194 -137
  225. package/dist/schema/messages.js +66 -83
  226. package/dist/schema/options.d.ts +112 -65
  227. package/dist/schema/options.js +52 -101
  228. package/dist/speech-client.d.ts +7 -7
  229. package/dist/speech.d.ts +306 -316
  230. package/dist/speech.js +16 -14
  231. package/dist/testing.d.ts +31 -175
  232. package/dist/testing.js +60 -5
  233. package/dist/tool-history.d.ts +1 -1
  234. package/dist/tool-history.js +3 -10
  235. package/dist/tool-runtime.d.ts +9 -2
  236. package/dist/tool-runtime.js +17 -31
  237. package/dist/tool.d.ts +39 -43
  238. package/dist/tool.js +16 -26
  239. package/dist/transcription-client.d.ts +7 -7
  240. package/dist/transcription.d.ts +314 -265
  241. package/dist/transcription.js +16 -14
  242. package/dist/utils/provider-codes.d.ts +3 -0
  243. package/dist/utils/provider-codes.js +41 -0
  244. package/dist/video-client.d.ts +7 -7
  245. package/dist/video-client.js +1 -1
  246. package/dist/video.d.ts +269 -287
  247. package/dist/video.js +16 -14
  248. package/package.json +9 -9
  249. package/dist/experimental/testing.d.ts +0 -3
  250. package/dist/experimental/testing.js +0 -3
  251. package/dist/testing/decision.d.ts +0 -16
  252. package/dist/testing/decision.js +0 -20
  253. package/dist/testing/image.d.ts +0 -14
  254. package/dist/testing/image.js +0 -12
  255. package/dist/testing/media.d.ts +0 -18
  256. package/dist/testing/media.js +0 -36
  257. package/dist/testing/script.d.ts +0 -28
  258. package/dist/testing/script.js +0 -60
  259. package/dist/testing/speech.d.ts +0 -14
  260. package/dist/testing/speech.js +0 -12
  261. package/dist/testing/transcription.d.ts +0 -14
  262. package/dist/testing/transcription.js +0 -12
  263. package/dist/testing/video.d.ts +0 -14
  264. package/dist/testing/video.js +0 -12
@@ -57,17 +57,6 @@ declare const ProviderConfigurationError_base: Schema.Class<ProviderConfiguratio
57
57
  */
58
58
  export declare class ProviderConfigurationError extends ProviderConfigurationError_base {
59
59
  }
60
- declare const NoRouteError_base: Schema.Class<NoRouteError, Schema.TaggedStruct<"NoRoute", {
61
- readonly message: Schema.String;
62
- readonly body: Schema.optional<Schema.String>;
63
- readonly http: Schema.optional<typeof HttpContext>;
64
- readonly cause: Schema.optional<Schema.Defect>;
65
- readonly route: Schema.String;
66
- readonly provider: Schema.brand<Schema.String, "AI.ProviderID">;
67
- readonly model: Schema.brand<Schema.String, "AI.ModelID">;
68
- }>, import("effect/Cause").YieldableError>;
69
- export declare class NoRouteError extends NoRouteError_base {
70
- }
71
60
  declare const AuthenticationError_base: Schema.Class<AuthenticationError, Schema.TaggedStruct<"Authentication", {
72
61
  message: Schema.String;
73
62
  body: Schema.optional<Schema.String>;
@@ -159,25 +148,35 @@ declare const TimeoutError_base: Schema.Class<TimeoutError, Schema.TaggedStruct<
159
148
  /** A caller-supplied deadline elapsed, such as `Generation.await` polling past its `Poll.timeout`. */
160
149
  export declare class TimeoutError extends TimeoutError_base {
161
150
  }
162
- export declare const AIErrorReason: Schema.toTaggedUnion<"_tag", readonly [typeof InvalidRequestError, typeof UnsupportedOperationError, typeof NoRouteError, typeof AuthenticationError, typeof RateLimitError, typeof QuotaExceededError, typeof ContentPolicyError, typeof ProviderInternalError, typeof TransportError, typeof InvalidProviderOutputError, typeof UnknownProviderError, typeof TimeoutError]>;
151
+ export declare const AIErrorReason: Schema.toTaggedUnion<"_tag", readonly [typeof InvalidRequestError, typeof UnsupportedOperationError, typeof AuthenticationError, typeof RateLimitError, typeof QuotaExceededError, typeof ContentPolicyError, typeof ProviderInternalError, typeof TransportError, typeof InvalidProviderOutputError, typeof UnknownProviderError, typeof TimeoutError]>;
163
152
  export type AIErrorReason = Schema.Schema.Type<typeof AIErrorReason>;
164
153
  declare const AIError_base: Schema.Class<AIError, Schema.TaggedStruct<"AI.Error", {
165
- readonly reason: Schema.toTaggedUnion<"_tag", readonly [typeof InvalidRequestError, typeof UnsupportedOperationError, typeof NoRouteError, typeof AuthenticationError, typeof RateLimitError, typeof QuotaExceededError, typeof ContentPolicyError, typeof ProviderInternalError, typeof TransportError, typeof InvalidProviderOutputError, typeof UnknownProviderError, typeof TimeoutError]>;
154
+ readonly reason: Schema.toTaggedUnion<"_tag", readonly [typeof InvalidRequestError, typeof UnsupportedOperationError, typeof AuthenticationError, typeof RateLimitError, typeof QuotaExceededError, typeof ContentPolicyError, typeof ProviderInternalError, typeof TransportError, typeof InvalidProviderOutputError, typeof UnknownProviderError, typeof TimeoutError]>;
166
155
  }>, import("effect/Cause").YieldableError>;
167
156
  export declare class AIError extends AIError_base {
168
- readonly cause: AuthenticationError | ContentPolicyError | InvalidProviderOutputError | InvalidRequestError | NoRouteError | ProviderInternalError | QuotaExceededError | RateLimitError | TimeoutError | TransportError | UnknownProviderError | UnsupportedOperationError;
169
- /** Output that arrived before `generate`, `generateObject` or a `loop` step failed. Not in the schema, so encoding omits it. */
157
+ readonly cause: AuthenticationError | ContentPolicyError | InvalidProviderOutputError | InvalidRequestError | ProviderInternalError | QuotaExceededError | RateLimitError | TimeoutError | TransportError | UnknownProviderError | UnsupportedOperationError;
158
+ /** Output that arrived before `generate` (including its `schema` decode) or a `loop` step failed. Not in the schema, so encoding omits it. */
170
159
  readonly partial?: LLMResponse;
171
160
  constructor(props: Pick<AIError, "reason" | "partial">);
172
161
  get message(): string;
162
+ /** HTTP status of the failed response, when the failure came from one. */
163
+ get status(): number | undefined;
164
+ /** The provider's error code from the response body or `x-amzn-errortype`, or the transport's system error code. */
165
+ get code(): string | undefined;
166
+ /** Delay the provider asked for before retrying, from `retry-after` style headers. */
167
+ get retryAfterMs(): number | undefined;
168
+ /**
169
+ * Whether a failed call may succeed when sent again: rate limits, provider-side failures, transport failures that did
170
+ * not deliver an accepted write, and unrecognized failures. Callers decide which calls are safe to repeat.
171
+ */
172
+ get isRetryable(): boolean;
173
173
  }
174
174
  /** Return `error` with `partial` attached, or unchanged when there is no partial output. */
175
175
  export declare const withPartial: (error: AIError, partial: LLMResponse | undefined) => AIError;
176
176
  /**
177
177
  * Failure type for tool execute handlers. Handlers must map their internal
178
178
  * errors to this shape; the runtime catches `ToolFailure`s and surfaces them
179
- * as `tool-error` events plus a `tool-result` of `type: "error"` so the model
180
- * can self-correct.
179
+ * as a `tool-result` of `type: "error"` so the model can self-correct.
181
180
  *
182
181
  * Anything thrown or yielded by a handler that is not a `ToolFailure` is
183
182
  * treated as a defect and fails the stream.
@@ -1,7 +1,8 @@
1
1
  import { Schema } from "effect";
2
2
  import { NonNegativeInt } from "@opencode/schema";
3
3
  import { Tool } from "@opencode/schema/tool";
4
- import { ModelID, ProviderID, RouteID } from "./ids.js";
4
+ import { ProviderID, RouteID } from "./ids.js";
5
+ import { awsErrorType, providerCodes } from "../utils/provider-codes.js";
5
6
  export const ProviderFailureClassification = Schema.Literals(["context-overflow", "payload-too-large"]);
6
7
  export class HttpContext extends Schema.Class("AI.HttpContext")({
7
8
  url: Schema.String,
@@ -53,13 +54,6 @@ export class ProviderConfigurationError extends Schema.TaggedError("AI.Error.Pro
53
54
  message: Schema.String,
54
55
  }) {
55
56
  }
56
- export class NoRouteError extends Schema.TaggedError("AI.Error.NoRoute")("NoRoute", {
57
- ...ReasonFields,
58
- route: RouteID,
59
- provider: ProviderID,
60
- model: ModelID,
61
- }) {
62
- }
63
57
  export class AuthenticationError extends Schema.TaggedError("AI.Error.Authentication")("Authentication", ReasonFields) {
64
58
  }
65
59
  export class RateLimitError extends Schema.TaggedError("AI.Error.RateLimit")("RateLimit", {
@@ -110,7 +104,6 @@ export class TimeoutError extends Schema.TaggedError("AI.Error.Timeout")("Timeou
110
104
  export const AIErrorReason = Schema.Union([
111
105
  InvalidRequestError,
112
106
  UnsupportedOperationError,
113
- NoRouteError,
114
107
  AuthenticationError,
115
108
  RateLimitError,
116
109
  QuotaExceededError,
@@ -125,7 +118,7 @@ export class AIError extends Schema.TaggedError()("AI.Error", {
125
118
  reason: AIErrorReason,
126
119
  }) {
127
120
  cause = this.reason;
128
- /** Output that arrived before `generate`, `generateObject` or a `loop` step failed. Not in the schema, so encoding omits it. */
121
+ /** Output that arrived before `generate` (including its `schema` decode) or a `loop` step failed. Not in the schema, so encoding omits it. */
129
122
  partial;
130
123
  constructor(props) {
131
124
  super({ reason: props.reason });
@@ -134,14 +127,72 @@ export class AIError extends Schema.TaggedError()("AI.Error", {
134
127
  get message() {
135
128
  return this.reason.message;
136
129
  }
130
+ /** HTTP status of the failed response, when the failure came from one. */
131
+ get status() {
132
+ return this.reason.http?.status;
133
+ }
134
+ /** The provider's error code from the response body or `x-amzn-errortype`, or the transport's system error code. */
135
+ get code() {
136
+ if (this.reason._tag === "Transport" && this.reason.code !== undefined)
137
+ return this.reason.code;
138
+ return [
139
+ ...(this.reason.body === undefined ? [] : providerCodes(this.reason.body)),
140
+ ...awsErrorType(this.reason.http?.headers["x-amzn-errortype"]),
141
+ ][0];
142
+ }
143
+ /** Delay the provider asked for before retrying, from `retry-after` style headers. */
144
+ get retryAfterMs() {
145
+ if (this.reason._tag === "RateLimit")
146
+ return this.reason.retryAfterMs ?? this.reason.rateLimit?.retryAfterMs;
147
+ if (this.reason._tag === "QuotaExceeded" || this.reason._tag === "ProviderInternal")
148
+ return this.reason.retryAfterMs;
149
+ return undefined;
150
+ }
151
+ /**
152
+ * Whether a failed call may succeed when sent again: rate limits, provider-side failures, transport failures that did
153
+ * not deliver an accepted write, and unrecognized failures. Callers decide which calls are safe to repeat.
154
+ */
155
+ get isRetryable() {
156
+ const override = this.reason.http?.headers["x-should-retry"];
157
+ if (override === "true")
158
+ return true;
159
+ if (override === "false")
160
+ return false;
161
+ switch (this.reason._tag) {
162
+ case "RateLimit":
163
+ case "ProviderInternal":
164
+ return true;
165
+ // A WebSocket acknowledgment marks delivery accepted before model output may exist.
166
+ // Read failures can still recover; the caller chooses retry versus continuation from durable output.
167
+ case "Transport":
168
+ return (this.reason.delivery !== "rejected" && (this.reason.delivery !== "accepted" || this.reason.operation === "read"));
169
+ case "InvalidProviderOutput":
170
+ return this.reason.classification === "incomplete-stream";
171
+ // Unrecognized failures retry: classification records affirmative
172
+ // deterministic evidence, and transient failures are exactly the ones
173
+ // that arrive in shapes no classifier anticipates.
174
+ case "UnknownProvider":
175
+ return true;
176
+ case "Authentication":
177
+ case "QuotaExceeded":
178
+ case "ContentPolicy":
179
+ case "InvalidRequest":
180
+ case "UnsupportedOperation":
181
+ case "Timeout":
182
+ return false;
183
+ default: {
184
+ const exhaustive = this.reason;
185
+ return exhaustive;
186
+ }
187
+ }
188
+ }
137
189
  }
138
190
  /** Return `error` with `partial` attached, or unchanged when there is no partial output. */
139
191
  export const withPartial = (error, partial) => partial === undefined ? error : new AIError({ reason: error.reason, partial });
140
192
  /**
141
193
  * Failure type for tool execute handlers. Handlers must map their internal
142
194
  * errors to this shape; the runtime catches `ToolFailure`s and surfaces them
143
- * as `tool-error` events plus a `tool-result` of `type: "error"` so the model
144
- * can self-correct.
195
+ * as a `tool-result` of `type: "error"` so the model can self-correct.
145
196
  *
146
197
  * Anything thrown or yielded by a handler that is not a `ToolFailure` is
147
198
  * treated as a defect and fails the stream.