@sixb/azure-ai-foundry 0.2.0 → 0.2.5

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 (47) hide show
  1. package/README.md +187 -2
  2. package/dist/chat-request.d.ts.map +1 -1
  3. package/dist/decision.d.ts +18 -0
  4. package/dist/decision.d.ts.map +1 -0
  5. package/dist/discovery.d.ts +3 -0
  6. package/dist/discovery.d.ts.map +1 -1
  7. package/dist/embedding.d.ts +1 -1
  8. package/dist/embedding.d.ts.map +1 -1
  9. package/dist/image.d.ts +20 -0
  10. package/dist/image.d.ts.map +1 -0
  11. package/dist/index.d.ts +5 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +1249 -99
  14. package/dist/index.js.map +18 -12
  15. package/dist/kinds.d.ts +13 -0
  16. package/dist/kinds.d.ts.map +1 -0
  17. package/dist/messages-request.d.ts +14 -1
  18. package/dist/messages-request.d.ts.map +1 -1
  19. package/dist/provider.d.ts +15 -0
  20. package/dist/provider.d.ts.map +1 -1
  21. package/dist/request.d.ts +6 -1
  22. package/dist/request.d.ts.map +1 -1
  23. package/dist/reranking.d.ts +18 -0
  24. package/dist/reranking.d.ts.map +1 -0
  25. package/dist/speech.d.ts +20 -0
  26. package/dist/speech.d.ts.map +1 -0
  27. package/dist/structured-output.d.ts.map +1 -1
  28. package/dist/transcription.d.ts +20 -0
  29. package/dist/transcription.d.ts.map +1 -0
  30. package/dist/transport.d.ts +10 -2
  31. package/dist/transport.d.ts.map +1 -1
  32. package/package.json +3 -3
  33. package/src/chat-request.ts +18 -20
  34. package/src/decision.ts +211 -0
  35. package/src/discovery.ts +18 -0
  36. package/src/embedding.ts +15 -36
  37. package/src/image.ts +457 -0
  38. package/src/index.ts +8 -0
  39. package/src/kinds.ts +100 -0
  40. package/src/messages-request.ts +39 -8
  41. package/src/provider.ts +121 -31
  42. package/src/request.ts +24 -11
  43. package/src/reranking.ts +264 -0
  44. package/src/speech.ts +390 -0
  45. package/src/structured-output.ts +8 -0
  46. package/src/transcription.ts +322 -0
  47. package/src/transport.ts +90 -11
@@ -0,0 +1,322 @@
1
+ import {
2
+ defineModelRateCard,
3
+ type ModelCostEstimate,
4
+ ModelProviderError,
5
+ type ModelRateCard,
6
+ type ModelUsage,
7
+ rateModelCall,
8
+ type TranscriptionCostEstimator,
9
+ type TranscriptionModel,
10
+ type TranscriptionModelDefinition,
11
+ type TranscriptionModelRequest,
12
+ TranscriptionModelResponseError,
13
+ type TranscriptionModelResult,
14
+ } from "@sixb/core/models"
15
+ import {
16
+ type AzureAIFoundryDeployment,
17
+ deploymentMetadata,
18
+ type FoundryDeployments,
19
+ type ResolvedDeployment,
20
+ } from "./discovery"
21
+ import { assertDeploymentKind } from "./kinds"
22
+ import type { AzureAIFoundryModelMetadata } from "./provider"
23
+ import { azurePublisher } from "./publisher"
24
+ import { abortable, type FoundryTransport, readJson, requestId } from "./transport"
25
+ import { counter, object, PREFIX, positiveInteger } from "./util"
26
+
27
+ export interface AzureAIFoundryTranscriptionOptions {
28
+ /** The spoken language as an ISO-639 code, such as `en`. Default: detected by the model. */
29
+ readonly language?: string
30
+ /** Deadline for one transcription, upload and response included. Default: 120,000 ms. */
31
+ readonly timeoutMs?: number
32
+ readonly rateCard?: ModelRateCard
33
+ readonly costEstimator?: TranscriptionCostEstimator
34
+ }
35
+
36
+ export interface AzureAIFoundryTranscriptionModel extends TranscriptionModel {
37
+ readonly metadata: AzureAIFoundryModelMetadata
38
+ resolve(options?: { readonly offline?: boolean }): Promise<AzureAIFoundryTranscriptionModel>
39
+ }
40
+
41
+ /** Azure's upload limit for one file. */
42
+ const MAX_INPUT_BYTES = 25 * 1024 * 1024
43
+
44
+ /** Azure picks the decoder from the file name's extension; the bytes are never transcoded. */
45
+ const EXTENSIONS: Readonly<Record<string, string>> = {
46
+ "audio/flac": "flac",
47
+ "audio/x-flac": "flac",
48
+ "audio/mpeg": "mp3",
49
+ "audio/mp3": "mp3",
50
+ "audio/mp4": "m4a",
51
+ "audio/m4a": "m4a",
52
+ "audio/x-m4a": "m4a",
53
+ "audio/ogg": "ogg",
54
+ "audio/wav": "wav",
55
+ "audio/wave": "wav",
56
+ "audio/x-wav": "wav",
57
+ "audio/webm": "webm",
58
+ }
59
+ const MEDIA_TYPES = Object.freeze(Object.keys(EXTENSIONS))
60
+
61
+ /** A transcript of 25 MiB of audio is far smaller; Whisper's segments add a few times the text. */
62
+ const MAX_RESPONSE_BYTES = 8 * 1024 * 1024
63
+
64
+ /** AbortSignal.timeout's largest delay. */
65
+ const MAX_TIMEOUT_MS = 2 ** 31 - 1
66
+
67
+ /**
68
+ * Azure Retail Prices (verified 2026-11-13), by deployment SKU. Token prices are per million audio
69
+ * input and text output tokens; the binding sends no prompt, so every input token is audio.
70
+ * Hourly prices are per second of audio. gpt-4o-transcribe-diarize has no retail meter.
71
+ */
72
+ const PUBLISHED: readonly {
73
+ readonly model: string
74
+ readonly versions: readonly string[]
75
+ readonly skus: Readonly<Record<string, ModelRateCard>>
76
+ }[] = [
77
+ {
78
+ // gpt-4o-transcribe-{aud-inp,txt-out}-{glbl,dz,rgnl}
79
+ model: "gpt-4o-transcribe",
80
+ versions: ["2025-03-20"],
81
+ skus: {
82
+ GlobalStandard: { currency: "USD", input: "6", output: "10" },
83
+ DataZoneStandard: { currency: "USD", input: "6.6", output: "11" },
84
+ Standard: { currency: "USD", input: "6.6", output: "11" },
85
+ },
86
+ },
87
+ {
88
+ // gpt-4o-mini-transcribe-{aud-inp,txt-out}-{glbl,dz,rgnl}
89
+ model: "gpt-4o-mini-transcribe",
90
+ versions: ["2025-03-20", "2025-12-15"],
91
+ skus: {
92
+ GlobalStandard: { currency: "USD", input: "3", output: "5" },
93
+ DataZoneStandard: { currency: "USD", input: "3.3", output: "5.5" },
94
+ Standard: { currency: "USD", input: "3.3", output: "5.5" },
95
+ },
96
+ },
97
+ {
98
+ // "gpt-transcribe Gl/Dz Unit": USD 0.27 and 0.297 per hour.
99
+ model: "gpt-transcribe",
100
+ versions: ["2026-07-28"],
101
+ skus: {
102
+ GlobalStandard: { currency: "USD", audioInputSeconds: "0.000075" },
103
+ DataZoneStandard: { currency: "USD", audioInputSeconds: "0.0000825" },
104
+ },
105
+ },
106
+ {
107
+ // "Speech-to-Text-Batch-Whisper-glbl/regnl": USD 0.36 and 0.396 per hour.
108
+ model: "whisper",
109
+ versions: ["001"],
110
+ skus: {
111
+ GlobalStandard: { currency: "USD", audioInputSeconds: "0.0001" },
112
+ Standard: { currency: "USD", audioInputSeconds: "0.00011" },
113
+ },
114
+ },
115
+ ]
116
+
117
+ interface TranscriptionSettings {
118
+ readonly language?: string
119
+ readonly timeoutMs: number
120
+ readonly rateCard?: ModelRateCard
121
+ readonly costEstimator?: TranscriptionCostEstimator
122
+ }
123
+
124
+ interface TranscriptionResolution extends ResolvedDeployment {
125
+ readonly costEstimator?: TranscriptionCostEstimator
126
+ readonly rateCard?: ModelRateCard
127
+ /** Whisper reports no usage in JSON; its verbose format carries the audio duration. */
128
+ readonly verbose: boolean
129
+ /** gpt-4o-transcribe-diarize needs a chunking strategy for audio over 30 seconds. */
130
+ readonly chunking: boolean
131
+ }
132
+
133
+ export function createFoundryTranscription(
134
+ providerId: string,
135
+ modelId: string,
136
+ options: AzureAIFoundryTranscriptionOptions,
137
+ transport: FoundryTransport,
138
+ deployments: FoundryDeployments
139
+ ): AzureAIFoundryTranscriptionModel {
140
+ if (!modelId.trim() || modelId !== modelId.trim())
141
+ throw new TypeError(`${PREFIX} deploymentName must be nonempty without surrounding whitespace.`)
142
+ positiveInteger(options.timeoutMs, "timeoutMs")
143
+ if (options.timeoutMs !== undefined && options.timeoutMs > MAX_TIMEOUT_MS)
144
+ throw new TypeError(`${PREFIX} timeoutMs must be at most ${MAX_TIMEOUT_MS}.`)
145
+ if (options.language !== undefined && !/^[a-z]{2,3}$/.test(options.language))
146
+ throw new TypeError(`${PREFIX} language must be an ISO-639 code, such as 'en'.`)
147
+ if (options.rateCard && options.costEstimator)
148
+ throw new TypeError(`${PREFIX} Configure either rateCard or costEstimator, not both.`)
149
+ // Validate and snapshot now: later edits to the caller's object change nothing.
150
+ const settings: TranscriptionSettings = Object.freeze({
151
+ timeoutMs: options.timeoutMs ?? 120_000,
152
+ ...(options.language ? { language: options.language } : {}),
153
+ ...(options.rateCard ? { rateCard: defineModelRateCard(options.rateCard) } : {}),
154
+ ...(options.costEstimator ? { costEstimator: options.costEstimator } : {}),
155
+ })
156
+ return transcriptionBinding(providerId, modelId, settings, transport, deployments)
157
+ }
158
+
159
+ /** Discovery pins the deployed model for pricing; inference uploads the file to the resource. */
160
+ function transcriptionBinding(
161
+ providerId: string,
162
+ modelId: string,
163
+ settings: TranscriptionSettings,
164
+ transport: FoundryTransport,
165
+ deployments: FoundryDeployments,
166
+ resolution?: TranscriptionResolution
167
+ ): AzureAIFoundryTranscriptionModel {
168
+ const deployment = resolution?.deployment
169
+ const definition: TranscriptionModelDefinition = Object.freeze({
170
+ kind: "transcription",
171
+ providerId,
172
+ modelId,
173
+ ...(deployment ? { publisher: azurePublisher(deployment.modelPublisher) } : {}),
174
+ via: "Azure AI Foundry",
175
+ maxInputBytes: MAX_INPUT_BYTES,
176
+ mediaTypes: MEDIA_TYPES,
177
+ })
178
+
179
+ let pending: Promise<AzureAIFoundryTranscriptionModel> | undefined
180
+ const binding: AzureAIFoundryTranscriptionModel = Object.freeze({
181
+ providerId,
182
+ modelId,
183
+ definition,
184
+ metadata: deploymentMetadata(resolution),
185
+ ...(resolution?.costEstimator ? { costEstimator: resolution.costEstimator } : {}),
186
+ ...(resolution?.rateCard ? { rateCard: resolution.rateCard } : {}),
187
+
188
+ async resolve(input?: { readonly offline?: boolean }) {
189
+ if (resolution) return binding
190
+ const resolved = await deployments.resolve(modelId, input?.offline === true)
191
+ const found = resolved.deployment
192
+ assertDeploymentKind(found, modelId, "transcription")
193
+ const rateCard = settings.costEstimator
194
+ ? undefined
195
+ : (settings.rateCard ?? publishedRateCard(found))
196
+ return transcriptionBinding(providerId, modelId, settings, transport, deployments, {
197
+ ...resolved,
198
+ verbose: /^whisper$/i.test(found.modelName),
199
+ chunking: /-diarize$/i.test(found.modelName),
200
+ ...(rateCard ? { rateCard } : {}),
201
+ ...(settings.costEstimator || rateCard
202
+ ? { costEstimator: settings.costEstimator ?? transcriptionEstimator(rateCard!) }
203
+ : {}),
204
+ })
205
+ },
206
+
207
+ async transcribe(request: TranscriptionModelRequest): Promise<TranscriptionModelResult> {
208
+ const signal = AbortSignal.any([
209
+ ...(request.signal ? [request.signal] : []),
210
+ AbortSignal.timeout(settings.timeoutMs),
211
+ ])
212
+ signal.throwIfAborted()
213
+ const extension = EXTENSIONS[request.mediaType]
214
+ if (
215
+ !(request.audio instanceof Uint8Array) ||
216
+ request.audio.byteLength === 0 ||
217
+ request.audio.byteLength > MAX_INPUT_BYTES
218
+ )
219
+ throw new TypeError(`${PREFIX} Transcription audio must be 1 byte to 25 MiB.`)
220
+ if (!extension)
221
+ throw new TypeError(
222
+ `${PREFIX} Transcription media type '${request.mediaType}' is not supported.`
223
+ )
224
+ if (!resolution) {
225
+ // A direct call pins the deployment once, as the runtime does before admission.
226
+ pending ??= binding.resolve().catch((error) => {
227
+ pending = undefined
228
+ throw error
229
+ })
230
+ const resolved = await abortable(() => pending!, signal)
231
+ return resolved.transcribe({ ...request, signal })
232
+ }
233
+
234
+ const form = new FormData()
235
+ // Copied: the upload must not change if the caller reuses its buffer.
236
+ const file = new Blob([new Uint8Array(request.audio)], { type: request.mediaType })
237
+ form.set("file", file, `audio.${extension}`)
238
+ form.set("response_format", resolution.verbose ? "verbose_json" : "json")
239
+ if (resolution.chunking) form.set("chunking_strategy", "auto")
240
+ if (settings.language) form.set("language", settings.language)
241
+ const response = await transport.post(form, signal, providerId, modelId, "transcription")
242
+ const id = transport.redactText(response, requestId(response))
243
+ let body: unknown
244
+ try {
245
+ body = await readJson(response, signal, MAX_RESPONSE_BYTES)
246
+ } catch {
247
+ // The parse error can quote the body; only the status and request ID travel.
248
+ if (signal.aborted) throw signal.reason
249
+ throw new ModelProviderError(
250
+ `${PREFIX} Transcription returned invalid JSON.`,
251
+ providerId,
252
+ modelId,
253
+ { code: "invalid_json", requestId: id, status: response.status }
254
+ )
255
+ }
256
+ const payload = object(body)
257
+ const metadata = {
258
+ usage: transcriptionUsage(payload),
259
+ ...(id ? { providerIds: { requestId: id } } : {}),
260
+ }
261
+ if (typeof payload?.text !== "string")
262
+ throw new TranscriptionModelResponseError(
263
+ `${PREFIX} Transcription response is missing its text.`,
264
+ providerId,
265
+ modelId,
266
+ metadata
267
+ )
268
+ return { ...metadata, output: { text: payload.text } }
269
+ },
270
+ })
271
+ return binding
272
+ }
273
+
274
+ /**
275
+ * Token models report `usage.type: "tokens"`, gpt-transcribe `usage.type: "duration"` in whole
276
+ * seconds, and Whisper's verbose format a top-level `duration`. Anything else stays unknown.
277
+ */
278
+ function transcriptionUsage(payload: Record<string, unknown> | undefined): ModelUsage {
279
+ const usage = object(payload?.usage)
280
+ if (usage?.type === "tokens") {
281
+ const inputTokens = counter(usage.input_tokens)
282
+ const outputTokens = counter(usage.output_tokens)
283
+ return {
284
+ ...(inputTokens === undefined ? {} : { inputTokens }),
285
+ ...(outputTokens === undefined ? {} : { outputTokens }),
286
+ raw: usage,
287
+ }
288
+ }
289
+ const seconds = usage?.type === "duration" ? usage.seconds : payload?.duration
290
+ const audioDurationMs =
291
+ typeof seconds === "number" && Number.isFinite(seconds) && seconds >= 0
292
+ ? Math.round(seconds * 1000)
293
+ : undefined
294
+ return {
295
+ ...(audioDurationMs === undefined ? {} : { audioDurationMs }),
296
+ ...(usage ? { raw: usage } : {}),
297
+ }
298
+ }
299
+
300
+ /** The deployment's retail price, when Azure publishes one for its model, version and SKU. */
301
+ function publishedRateCard(deployment: AzureAIFoundryDeployment): ModelRateCard | undefined {
302
+ const name = deployment.modelName.toLowerCase()
303
+ const entry = PUBLISHED.find(
304
+ (price) => price.model === name && price.versions.includes(deployment.modelVersion)
305
+ )
306
+ const card =
307
+ entry && Object.hasOwn(entry.skus, deployment.sku.name) && entry.skus[deployment.sku.name]
308
+ return card ? defineModelRateCard(card) : undefined
309
+ }
310
+
311
+ function transcriptionEstimator(rateCard: ModelRateCard): TranscriptionCostEstimator {
312
+ return {
313
+ // Neither tokens nor duration can be read from the file before Azure decodes it.
314
+ estimate: ({ usage }): ModelCostEstimate => {
315
+ // Text input tokens bill at another rate; they come only from a prompt, which is never sent.
316
+ const details = object(usage.raw?.input_token_details)
317
+ if (details?.text_tokens !== undefined && details.text_tokens !== 0)
318
+ return { status: "unpriceable", reason: "missing-rate-card" }
319
+ return rateModelCall({ usage, rateCard })
320
+ },
321
+ }
322
+ }
package/src/transport.ts CHANGED
@@ -5,6 +5,31 @@ import { object, PREFIX, positiveInteger } from "./util"
5
5
 
6
6
  export type ValueSource<T> = T | ((signal: AbortSignal) => T | Promise<T>)
7
7
  export type FoundryProtocol = "responses" | "messages" | "chat"
8
+ type FoundryRoute = FoundryProtocol | OneShotRoute
9
+ /** Routes that answer in one body. A failure is never replayed: the call may have been billed. */
10
+ type OneShotRoute =
11
+ | "embeddings"
12
+ | "decision"
13
+ | "transcription"
14
+ | "reranking"
15
+ | "speech"
16
+ | "image"
17
+ | "image-edit"
18
+ const ONE_SHOT: ReadonlySet<FoundryRoute> = new Set<OneShotRoute>([
19
+ "embeddings",
20
+ "decision",
21
+ "transcription",
22
+ "reranking",
23
+ "speech",
24
+ "image",
25
+ "image-edit",
26
+ ])
27
+
28
+ /** The GA data-plane version: the v1 API does not route audio deployments. */
29
+ const AUDIO_API_VERSION = "2024-10-21"
30
+
31
+ /** The first version with GPT Image; v1 does not route FLUX.1 Kontext edits. */
32
+ const IMAGE_API_VERSION = "2025-04-01-preview"
8
33
 
9
34
  export interface TransportOptions {
10
35
  readonly endpoint: string
@@ -66,13 +91,13 @@ export class FoundryTransport {
66
91
  }
67
92
 
68
93
  async post(
69
- body: string,
94
+ body: string | FormData,
70
95
  signal: AbortSignal,
71
96
  providerId: string,
72
97
  modelId: string,
73
- protocol: FoundryProtocol | "embeddings" = "responses"
98
+ protocol: FoundryRoute = "responses"
74
99
  ): Promise<Response> {
75
- const url = this.url(protocol)
100
+ const url = this.url(protocol, modelId)
76
101
  const diagnostics = new RequestDiagnostics()
77
102
  for (let attempt = 0; ; attempt++) {
78
103
  signal.throwIfAborted()
@@ -81,8 +106,14 @@ export class FoundryTransport {
81
106
  diagnostics,
82
107
  protocol === "messages" ? "x-api-key" : "api-key"
83
108
  )
84
- headers.set("content-type", "application/json")
85
- headers.set("accept", protocol === "embeddings" ? "application/json" : "text/event-stream")
109
+ // fetch writes a multipart body's content type, boundary included.
110
+ if (typeof body === "string") headers.set("content-type", "application/json")
111
+ else headers.delete("content-type")
112
+ // Speech streams its audio as events: the only stream that reports usage.
113
+ headers.set(
114
+ "accept",
115
+ ONE_SHOT.has(protocol) && protocol !== "speech" ? "application/json" : "text/event-stream"
116
+ )
86
117
  if (protocol === "messages") headers.set("anthropic-version", "2023-06-01")
87
118
  signal.throwIfAborted()
88
119
  // Ambiguous network failures and accepted streams are never automatically replayed.
@@ -112,11 +143,8 @@ export class FoundryTransport {
112
143
  response,
113
144
  await httpError(response, providerId, modelId, signal)
114
145
  )
115
- if (
116
- protocol === "embeddings" ||
117
- !error.retryable ||
118
- attempt >= (this.options.maxRetries ?? 2)
119
- )
146
+ // One-shot routes: the caller decides, from `retryable`, whether to replay.
147
+ if (ONE_SHOT.has(protocol) || !error.retryable || attempt >= (this.options.maxRetries ?? 2))
120
148
  throw error
121
149
  await wait(
122
150
  Math.min(error.retryAfterMs ?? 250 * 2 ** attempt, this.options.maxRetryDelayMs ?? 60_000),
@@ -159,14 +187,62 @@ export class FoundryTransport {
159
187
  return (this.options.fetch ?? fetch)(url, init)
160
188
  }
161
189
 
162
- url(protocol: FoundryProtocol | "embeddings"): string {
190
+ url(protocol: FoundryRoute, deployment?: string): string {
163
191
  if (protocol === "embeddings") return `${this.baseUrl}/embeddings`
192
+ // Audio deployments answer on the resource origin, by deployment path.
193
+ if (protocol === "transcription")
194
+ return `${new URL(this.baseUrl).origin}/openai/deployments/${encodeURIComponent(deployment ?? "")}/audio/transcriptions?api-version=${AUDIO_API_VERSION}`
195
+ // Decision models answer on the resource origin; the project path does not route them.
196
+ if (protocol === "decision")
197
+ return `${new URL(this.baseUrl).origin}/providers/microsoft/v1/systemone`
198
+ // Image deployments answer on the resource origin, by deployment path.
199
+ if (protocol === "image" || protocol === "image-edit")
200
+ return `${new URL(this.baseUrl).origin}/openai/deployments/${encodeURIComponent(deployment ?? "")}/images/${protocol === "image" ? "generations" : "edits"}?api-version=${IMAGE_API_VERSION}`
201
+ // Cohere rerankers answer Cohere's v2 API on the resource origin.
202
+ if (protocol === "reranking")
203
+ return `${new URL(this.baseUrl).origin}/providers/cohere/v2/rerank`
204
+ // Speech answers on the resource's v1 API; the project path does not route it.
205
+ if (protocol === "speech") return `${new URL(this.baseUrl).origin}/openai/v1/audio/speech`
164
206
  if (protocol === "responses") return `${this.baseUrl}/responses`
165
207
  if (protocol === "chat") return `${this.baseUrl}/chat/completions`
166
208
  return `${new URL(this.baseUrl).origin}/anthropic/v1/messages`
167
209
  }
168
210
  }
169
211
 
212
+ /** Read a JSON body up to `maxBytes`; the reader is released on success, failure or abort. */
213
+ export async function readJson(
214
+ response: Response,
215
+ signal: AbortSignal,
216
+ maxBytes: number
217
+ ): Promise<unknown> {
218
+ return JSON.parse(await readText(response, signal, maxBytes))
219
+ }
220
+
221
+ /** Read a body as UTF-8 up to `maxBytes`; the reader is released on success, failure or abort. */
222
+ export async function readText(
223
+ response: Response,
224
+ signal: AbortSignal,
225
+ maxBytes: number
226
+ ): Promise<string> {
227
+ const reader = response.body?.getReader()
228
+ if (!reader) throw new Error("Empty response")
229
+ const chunks: Uint8Array[] = []
230
+ let bytes = 0
231
+ try {
232
+ for (;;) {
233
+ const chunk = await abortable(() => reader.read(), signal)
234
+ if (chunk.done) break
235
+ bytes += chunk.value.length
236
+ if (bytes > maxBytes) throw new Error(`Response exceeds ${maxBytes} bytes`)
237
+ chunks.push(chunk.value)
238
+ }
239
+ return Buffer.concat(chunks).toString("utf8")
240
+ } finally {
241
+ void reader.cancel().catch(() => {})
242
+ reader.releaseLock()
243
+ }
244
+ }
245
+
170
246
  export function requestId(response: Response): string | undefined {
171
247
  return (
172
248
  response.headers.get("apim-request-id") ??
@@ -231,7 +307,10 @@ async function httpError(
231
307
  }
232
308
  const parsed: unknown = JSON.parse(Buffer.concat(chunks).toString("utf8"))
233
309
  const error = object(object(parsed)?.error)
310
+ // Decision models reject some requests with `{ "detail": "..." }` instead of `error`.
311
+ const detail = object(parsed)?.detail
234
312
  if (typeof error?.message === "string") message = error.message
313
+ else if (typeof detail === "string") message = detail
235
314
  if (typeof error?.code === "string") code = error.code
236
315
  else if (typeof error?.type === "string") code = error.type
237
316
  } catch {