@sixb/azure-ai-foundry 0.1.5 → 0.2.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.
@@ -1 +1 @@
1
- {"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../src/catalog.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EAInB,KAAK,aAAa,EAEnB,MAAM,mBAAmB,CAAA;AAE1B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAKlD,MAAM,WAAW,4BAA4B;IAC3C,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;CAC1F;AACD,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAA;IACzB,QAAQ,CAAC,eAAe,EAAE,OAAO,GAAG,cAAc,CAAA;IAClD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,QAAQ,CAAC,EAAE,eAAe,CAAA;IACnC,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAA;IAC3D,QAAQ,CAAC,QAAQ,CAAC,EAAE,aAAa,CAAA;CAClC;AAED,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAA;IAC1B,QAAQ,CAAC,eAAe,EAAE,OAAO,GAAG,cAAc,CAAA;IAClD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,QAAQ,CAAC,EAAE,aAAa,CAAA;CAClC;AAID,6FAA6F;AAC7F,qBAAa,sBAAsB;IACjC,OA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AG,IAAI;IAIxB,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAM3B,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,kBAAkB;IAU5D,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK;CAO/B"}
1
+ {"version":3,"file":"diagnostics.d.ts","sourceRoot":"","sources":["../src/diagnostics.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAEtD,gGAAgG;AAChG,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAoB;IAE5C,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAIxB,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAM3B,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,kBAAkB;IAW5D,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK;CAO/B"}
package/dist/index.js CHANGED
@@ -88,6 +88,7 @@ import {
88
88
  MODEL_REASONING_EFFORTS,
89
89
  ModelCatalogUnavailableError
90
90
  } from "@sixb/core/models";
91
+ import { downloadModelsDev, modelsDevRateCard } from "@sixb/model-protocols/models-dev";
91
92
 
92
93
  // src/publisher.ts
93
94
  var publishers = {
@@ -120,236 +121,7 @@ function fireworksPublisher(family) {
120
121
  return;
121
122
  }
122
123
 
123
- // src/transport.ts
124
- import { ModelProviderError as ModelProviderError2 } from "@sixb/core/models";
125
-
126
- // src/diagnostics.ts
127
- import { ModelProviderError } from "@sixb/core/models";
128
-
129
- class RequestDiagnostics {
130
- secrets = new Set;
131
- add(value) {
132
- if (value.trim())
133
- this.secrets.add(value.trim());
134
- }
135
- text(value) {
136
- for (const secret of [...this.secrets].sort((a, b) => b.length - a.length))
137
- value = value.replaceAll(secret, "[REDACTED]");
138
- return value;
139
- }
140
- providerError(error) {
141
- return new ModelProviderError(this.text(error.message), error.providerId, error.modelId, {
142
- status: error.status,
143
- retryable: error.retryable,
144
- retryAfterMs: error.retryAfterMs,
145
- code: error.code === undefined ? undefined : this.text(error.code),
146
- requestId: error.requestId === undefined ? undefined : this.text(error.requestId)
147
- });
148
- }
149
- failure(error) {
150
- if (error instanceof ModelProviderError)
151
- return this.providerError(error);
152
- const sanitized = new Error(this.text(error instanceof Error ? error.message : String(error)));
153
- sanitized.name = error instanceof Error ? this.text(error.name) : "Error";
154
- return sanitized;
155
- }
156
- }
157
-
158
- // src/transport.ts
159
- class FoundryTransport {
160
- diagnostics = new WeakMap;
161
- baseUrl;
162
- projectUrl;
163
- options;
164
- constructor(options, target = "project") {
165
- if (!URL.canParse(options.endpoint))
166
- throw new TypeError(`${PREFIX} endpoint must be a valid ${target} URL.`);
167
- const url = new URL(options.endpoint);
168
- if (!["https:", "http:"].includes(url.protocol) || url.username || url.password || url.search || url.hash) {
169
- throw new TypeError(`${PREFIX} endpoint must be an HTTP(S) ${target} URL without credentials, query, or fragment.`);
170
- }
171
- const path = url.pathname.replace(/\/+$/, "");
172
- if (target === "project" && !/^\/api\/projects\/[^/]+$/.test(path))
173
- throw new TypeError(`${PREFIX} endpoint must be a Foundry project URL: https://<resource>.services.ai.azure.com/api/projects/<project>.`);
174
- if (target === "resource" && path !== "" && path !== "/openai/v1")
175
- throw new TypeError(`${PREFIX} embeddings.endpoint must be a resource origin or /openai/v1 URL.`);
176
- this.projectUrl = `${url.origin}${path}`;
177
- this.baseUrl = target === "project" ? `${this.projectUrl}/openai/v1` : `${url.origin}/openai/v1`;
178
- if (typeof options.apiKey !== "function" && (typeof options.apiKey !== "string" || !options.apiKey.trim()))
179
- throw new TypeError(`${PREFIX} apiKey is required.`);
180
- if (options.maxRetries !== undefined && (!Number.isSafeInteger(options.maxRetries) || options.maxRetries < 0)) {
181
- throw new TypeError(`${PREFIX} maxRetries must be a nonnegative safe integer.`);
182
- }
183
- positiveInteger(options.maxRetryDelayMs, "maxRetryDelayMs");
184
- this.options = {
185
- ...options,
186
- headers: typeof options.headers === "function" ? options.headers : { ...options.headers }
187
- };
188
- }
189
- async post(body, signal, providerId, modelId, protocol = "responses") {
190
- const url = this.url(protocol);
191
- const diagnostics = new RequestDiagnostics;
192
- for (let attempt = 0;; attempt++) {
193
- signal.throwIfAborted();
194
- const headers = await this.headers(signal, diagnostics, protocol === "messages" ? "x-api-key" : "api-key");
195
- headers.set("content-type", "application/json");
196
- headers.set("accept", protocol === "embeddings" ? "application/json" : "text/event-stream");
197
- if (protocol === "messages")
198
- headers.set("anthropic-version", "2023-06-01");
199
- signal.throwIfAborted();
200
- const response = await abortable(async () => {
201
- const result = await this.fetch(url, {
202
- method: "POST",
203
- redirect: "error",
204
- headers,
205
- body,
206
- signal
207
- });
208
- if (signal.aborted)
209
- result.body?.cancel(signal.reason).catch(() => {});
210
- return result;
211
- }, signal).catch((error) => {
212
- if (signal.aborted)
213
- throw signal.reason;
214
- throw diagnostics.failure(error);
215
- });
216
- this.diagnostics.set(response, diagnostics);
217
- if (signal.aborted) {
218
- response.body?.cancel(signal.reason).catch(() => {});
219
- signal.throwIfAborted();
220
- }
221
- if (response.ok)
222
- return response;
223
- const error = this.redactError(response, await httpError(response, providerId, modelId, signal));
224
- if (protocol === "embeddings" || !error.retryable || attempt >= (this.options.maxRetries ?? 2))
225
- throw error;
226
- await wait(Math.min(error.retryAfterMs ?? 250 * 2 ** attempt, this.options.maxRetryDelayMs ?? 60000), signal);
227
- }
228
- }
229
- redactText(response, value) {
230
- return value === undefined ? undefined : this.diagnostics.get(response)?.text(value) ?? value;
231
- }
232
- redactError(response, error) {
233
- return this.diagnostics.get(response).providerError(error);
234
- }
235
- redactFailure(response, error) {
236
- return this.diagnostics.get(response).failure(error);
237
- }
238
- async headers(signal, diagnostics, keyHeader = "api-key") {
239
- const headers = new Headers(await resolve(this.options.headers, signal));
240
- for (const value of headers.values())
241
- diagnostics.add(value);
242
- for (const reserved of ["authorization", "api-key", "x-api-key"])
243
- if (headers.has(reserved))
244
- throw new TypeError(`${PREFIX} Use apiKey instead of '${reserved}' headers.`);
245
- const key = await resolve(this.options.apiKey, signal);
246
- if (typeof key !== "string" || !key.trim())
247
- throw new TypeError(`${PREFIX} apiKey must resolve to a nonempty string.`);
248
- diagnostics.add(key);
249
- headers.set(keyHeader, key);
250
- return headers;
251
- }
252
- fetch(url, init) {
253
- return (this.options.fetch ?? fetch)(url, init);
254
- }
255
- url(protocol) {
256
- if (protocol === "embeddings")
257
- return `${this.baseUrl}/embeddings`;
258
- if (protocol === "responses")
259
- return `${this.baseUrl}/responses`;
260
- if (protocol === "chat")
261
- return `${this.baseUrl}/chat/completions`;
262
- return `${new URL(this.baseUrl).origin}/anthropic/v1/messages`;
263
- }
264
- }
265
- function requestId(response) {
266
- return response.headers.get("apim-request-id") ?? response.headers.get("x-request-id") ?? response.headers.get("request-id") ?? undefined;
267
- }
268
- async function resolve(source, signal) {
269
- signal.throwIfAborted();
270
- if (typeof source !== "function")
271
- return source;
272
- return abortable(() => source(signal), signal);
273
- }
274
- function abortable(operation, signal) {
275
- return new Promise((resolve, reject) => {
276
- signal.throwIfAborted();
277
- const abort = () => {
278
- signal.removeEventListener("abort", abort);
279
- reject(signal.reason);
280
- };
281
- signal.addEventListener("abort", abort, { once: true });
282
- Promise.resolve().then(() => {
283
- signal.throwIfAborted();
284
- return operation();
285
- }).then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
286
- });
287
- }
288
- async function httpError(response, providerId, modelId, signal) {
289
- let message = `Provider request failed with HTTP ${response.status}.`;
290
- let code;
291
- const reader = response.body?.getReader();
292
- if (reader) {
293
- const cancel = () => {
294
- reader.cancel(signal.reason).catch(() => {});
295
- };
296
- signal.addEventListener("abort", cancel, { once: true });
297
- const chunks = [];
298
- let length = 0;
299
- try {
300
- while (length < 8192) {
301
- signal.throwIfAborted();
302
- const { value, done } = await abortable(() => reader.read(), signal);
303
- if (done)
304
- break;
305
- const part = value.subarray(0, 8192 - length);
306
- chunks.push(part);
307
- length += part.length;
308
- }
309
- const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
310
- const error = object(object(parsed)?.error);
311
- if (typeof error?.message === "string")
312
- message = error.message;
313
- if (typeof error?.code === "string")
314
- code = error.code;
315
- else if (typeof error?.type === "string")
316
- code = error.type;
317
- } catch {} finally {
318
- signal.removeEventListener("abort", cancel);
319
- reader.cancel().catch(() => {});
320
- reader.releaseLock();
321
- }
322
- }
323
- signal.throwIfAborted();
324
- const delay = response.headers.get("retry-after-ms") ?? response.headers.get("x-ms-retry-after-ms");
325
- const retry = response.headers.get("retry-after");
326
- const ms = delay !== null ? Number(delay) : retry !== null ? Number.isFinite(Number(retry)) ? Number(retry) * 1000 : Date.parse(retry) - Date.now() : undefined;
327
- return new ModelProviderError2(`${PREFIX} ${message}`, providerId, modelId, {
328
- status: response.status,
329
- requestId: requestId(response),
330
- code,
331
- retryable: response.status === 408 || response.status === 429 || response.status >= 500,
332
- ...ms !== undefined && Number.isFinite(ms) && ms >= 0 ? { retryAfterMs: Math.ceil(ms) } : {}
333
- });
334
- }
335
- async function wait(ms, signal) {
336
- signal.throwIfAborted();
337
- await new Promise((resolve, reject) => {
338
- const abort = () => {
339
- clearTimeout(timer);
340
- reject(signal.reason);
341
- };
342
- const timer = setTimeout(() => {
343
- signal.removeEventListener("abort", abort);
344
- resolve();
345
- }, ms);
346
- signal.addEventListener("abort", abort, { once: true });
347
- });
348
- }
349
-
350
124
  // src/catalog.ts
351
- var url = "https://models.dev/api.json";
352
-
353
125
  class RemoteModelsDevCatalog {
354
126
  snapshot = [];
355
127
  loadedAt = 0;
@@ -406,30 +178,9 @@ class RemoteModelsDevCatalog {
406
178
  return this.pending;
407
179
  }
408
180
  async fetchModels() {
409
- const signal = AbortSignal.timeout(this.options.timeoutMs ?? 1e4);
410
181
  let text;
411
182
  try {
412
- const response = await abortable(() => (this.options.fetch ?? fetch)(url, { signal, redirect: "error" }), signal);
413
- if (!response.ok || !response.body)
414
- throw new Error(`HTTP ${response.status}`);
415
- const reader = response.body.getReader();
416
- const chunks = [];
417
- let bytes = 0;
418
- try {
419
- for (;; ) {
420
- const chunk = await abortable(() => reader.read(), signal);
421
- if (chunk.done)
422
- break;
423
- bytes += chunk.value.length;
424
- if (bytes > 32 * 1024 * 1024)
425
- throw new Error("Catalog exceeds 32 MiB");
426
- chunks.push(chunk.value);
427
- }
428
- text = Buffer.concat(chunks).toString();
429
- } finally {
430
- reader.cancel().catch(() => {});
431
- reader.releaseLock();
432
- }
183
+ text = await downloadModelsDev(this.options);
433
184
  } catch (cause) {
434
185
  throw new ModelCatalogUnavailableError(`${PREFIX} models.dev catalog unavailable.`, { cause });
435
186
  }
@@ -523,86 +274,12 @@ function reasoning(supported, value) {
523
274
  };
524
275
  }
525
276
  function rates(raw, protocol) {
526
- const card = parseRates(raw);
277
+ const card = modelsDevRateCard(raw);
527
278
  if (protocol !== "messages" || card?.cacheWriteInput === undefined)
528
279
  return card;
529
280
  const { cacheWriteInput, ...rest } = card;
530
281
  return defineModelRateCard2({ ...rest, cacheWriteInput5m: cacheWriteInput });
531
282
  }
532
- function parseRates(raw) {
533
- if (!raw || raw.tiers === undefined)
534
- return flatRates(raw);
535
- const { tiers, context_over_200k: legacy, ...base } = raw;
536
- const card = flatRates(base);
537
- if (!card || !Array.isArray(tiers))
538
- return;
539
- const parsed = [];
540
- for (const value of tiers) {
541
- const entry = object(value);
542
- if (!entry)
543
- return;
544
- const { tier, ...prices } = entry;
545
- const threshold = object(tier);
546
- const size = threshold?.size;
547
- const tierCard = flatRates(prices);
548
- if (!tierCard || !threshold || Object.keys(threshold).some((k) => k !== "type" && k !== "size") || threshold.type !== undefined && threshold.type !== "context" || typeof size !== "number" || !Number.isSafeInteger(size) || size < 0 || size >= Number.MAX_SAFE_INTEGER)
549
- return;
550
- parsed.push({ size, card: tierCard });
551
- }
552
- parsed.sort((a, b) => a.size - b.size);
553
- if (new Set(parsed.map((t) => t.size)).size !== parsed.length)
554
- return;
555
- const keys = ["input", "output", "cacheReadInput", "cacheWriteInput"];
556
- if (legacy !== undefined) {
557
- const mirror = flatRates(object(legacy));
558
- if (!mirror || parsed.length !== 1 || parsed[0].size < 200000 || keys.some((k) => mirror[k] !== parsed[0].card[k]))
559
- return;
560
- }
561
- if (parsed.some((t) => keys.some((k) => card[k] === undefined !== (t.card[k] === undefined))))
562
- return;
563
- const price = (key) => {
564
- const basePrice = card[key];
565
- if (basePrice === undefined || !parsed.length)
566
- return basePrice;
567
- if (typeof basePrice !== "string")
568
- return;
569
- return {
570
- default: basePrice,
571
- tiers: parsed.map((t, i) => ({
572
- minTokens: t.size + 1,
573
- ...parsed[i + 1] ? { maxTokens: parsed[i + 1].size + 1 } : {},
574
- price: String(t.card[key])
575
- }))
576
- };
577
- };
578
- return defineModelRateCard2({
579
- currency: "USD",
580
- input: price("input"),
581
- output: price("output"),
582
- ...card.cacheReadInput === undefined ? {} : { cacheReadInput: price("cacheReadInput") },
583
- ...card.cacheWriteInput === undefined ? {} : { cacheWriteInput: price("cacheWriteInput") }
584
- });
585
- }
586
- function flatRates(raw) {
587
- if (!raw)
588
- return;
589
- if (Object.keys(raw).some((k) => !["input", "output", "cache_read", "cache_write"].includes(k)))
590
- return;
591
- const decimal = (v) => (typeof v === "number" || typeof v === "string") && /^\d+(\.\d+)?$/.test(String(v)) ? String(v) : undefined;
592
- const input = decimal(raw.input), output = decimal(raw.output);
593
- if (input === undefined || output === undefined)
594
- return;
595
- const read = decimal(raw.cache_read), write = decimal(raw.cache_write);
596
- if (raw.cache_read !== undefined && read === undefined || raw.cache_write !== undefined && write === undefined)
597
- return;
598
- return defineModelRateCard2({
599
- currency: "USD",
600
- input,
601
- output,
602
- ...read === undefined ? {} : { cacheReadInput: read },
603
- ...write === undefined ? {} : { cacheWriteInput: write }
604
- });
605
- }
606
283
 
607
284
  // src/chat-accounting.ts
608
285
  function foundryChatUsage(raw, reliableReasoning) {
@@ -1078,6 +755,236 @@ import {
1078
755
  ModelCatalogUnavailableError as ModelCatalogUnavailableError2,
1079
756
  ModelProviderError as ModelProviderError3
1080
757
  } from "@sixb/core/models";
758
+
759
+ // src/diagnostics.ts
760
+ import { ModelProviderError } from "@sixb/core/models";
761
+
762
+ class RequestDiagnostics {
763
+ secrets = new Set;
764
+ add(value) {
765
+ if (value.trim())
766
+ this.secrets.add(value.trim());
767
+ }
768
+ text(value) {
769
+ for (const secret of [...this.secrets].sort((a, b) => b.length - a.length))
770
+ value = value.replaceAll(secret, "[REDACTED]");
771
+ return value;
772
+ }
773
+ providerError(error) {
774
+ return new ModelProviderError(this.text(error.message), error.providerId, error.modelId, {
775
+ status: error.status,
776
+ retryable: error.retryable,
777
+ retryAfterMs: error.retryAfterMs,
778
+ code: error.code === undefined ? undefined : this.text(error.code),
779
+ requestId: error.requestId === undefined ? undefined : this.text(error.requestId),
780
+ reason: error.reason
781
+ });
782
+ }
783
+ failure(error) {
784
+ if (error instanceof ModelProviderError)
785
+ return this.providerError(error);
786
+ const sanitized = new Error(this.text(error instanceof Error ? error.message : String(error)));
787
+ sanitized.name = error instanceof Error ? this.text(error.name) : "Error";
788
+ return sanitized;
789
+ }
790
+ }
791
+
792
+ // src/transport.ts
793
+ import { ModelProviderError as ModelProviderError2 } from "@sixb/core/models";
794
+ import { contextWindowRejection } from "@sixb/model-protocols/responses";
795
+ class FoundryTransport {
796
+ diagnostics = new WeakMap;
797
+ baseUrl;
798
+ projectUrl;
799
+ options;
800
+ constructor(options, target = "project") {
801
+ if (!URL.canParse(options.endpoint))
802
+ throw new TypeError(`${PREFIX} endpoint must be a valid ${target} URL.`);
803
+ const url = new URL(options.endpoint);
804
+ if (!["https:", "http:"].includes(url.protocol) || url.username || url.password || url.search || url.hash) {
805
+ throw new TypeError(`${PREFIX} endpoint must be an HTTP(S) ${target} URL without credentials, query, or fragment.`);
806
+ }
807
+ const path = url.pathname.replace(/\/+$/, "");
808
+ if (target === "project" && !/^\/api\/projects\/[^/]+$/.test(path))
809
+ throw new TypeError(`${PREFIX} endpoint must be a Foundry project URL: https://<resource>.services.ai.azure.com/api/projects/<project>.`);
810
+ if (target === "resource" && path !== "" && path !== "/openai/v1")
811
+ throw new TypeError(`${PREFIX} embeddings.endpoint must be a resource origin or /openai/v1 URL.`);
812
+ this.projectUrl = `${url.origin}${path}`;
813
+ this.baseUrl = target === "project" ? `${this.projectUrl}/openai/v1` : `${url.origin}/openai/v1`;
814
+ if (typeof options.apiKey !== "function" && (typeof options.apiKey !== "string" || !options.apiKey.trim()))
815
+ throw new TypeError(`${PREFIX} apiKey is required.`);
816
+ if (options.maxRetries !== undefined && (!Number.isSafeInteger(options.maxRetries) || options.maxRetries < 0)) {
817
+ throw new TypeError(`${PREFIX} maxRetries must be a nonnegative safe integer.`);
818
+ }
819
+ positiveInteger(options.maxRetryDelayMs, "maxRetryDelayMs");
820
+ this.options = {
821
+ ...options,
822
+ headers: typeof options.headers === "function" ? options.headers : { ...options.headers }
823
+ };
824
+ }
825
+ async post(body, signal, providerId, modelId, protocol = "responses") {
826
+ const url = this.url(protocol);
827
+ const diagnostics = new RequestDiagnostics;
828
+ for (let attempt = 0;; attempt++) {
829
+ signal.throwIfAborted();
830
+ const headers = await this.headers(signal, diagnostics, protocol === "messages" ? "x-api-key" : "api-key");
831
+ headers.set("content-type", "application/json");
832
+ headers.set("accept", protocol === "embeddings" ? "application/json" : "text/event-stream");
833
+ if (protocol === "messages")
834
+ headers.set("anthropic-version", "2023-06-01");
835
+ signal.throwIfAborted();
836
+ const response = await abortable(async () => {
837
+ const result = await this.fetch(url, {
838
+ method: "POST",
839
+ redirect: "error",
840
+ headers,
841
+ body,
842
+ signal
843
+ });
844
+ if (signal.aborted)
845
+ result.body?.cancel(signal.reason).catch(() => {});
846
+ return result;
847
+ }, signal).catch((error) => {
848
+ if (signal.aborted)
849
+ throw signal.reason;
850
+ throw diagnostics.failure(error);
851
+ });
852
+ this.diagnostics.set(response, diagnostics);
853
+ if (signal.aborted) {
854
+ response.body?.cancel(signal.reason).catch(() => {});
855
+ signal.throwIfAborted();
856
+ }
857
+ if (response.ok)
858
+ return response;
859
+ const error = this.redactError(response, await httpError(response, providerId, modelId, signal));
860
+ if (protocol === "embeddings" || !error.retryable || attempt >= (this.options.maxRetries ?? 2))
861
+ throw error;
862
+ await wait(Math.min(error.retryAfterMs ?? 250 * 2 ** attempt, this.options.maxRetryDelayMs ?? 60000), signal);
863
+ }
864
+ }
865
+ redactText(response, value) {
866
+ return value === undefined ? undefined : this.diagnostics.get(response)?.text(value) ?? value;
867
+ }
868
+ redactError(response, error) {
869
+ return this.diagnostics.get(response).providerError(error);
870
+ }
871
+ redactFailure(response, error) {
872
+ return this.diagnostics.get(response).failure(error);
873
+ }
874
+ async headers(signal, diagnostics, keyHeader = "api-key") {
875
+ const headers = new Headers(await resolve(this.options.headers, signal));
876
+ for (const value of headers.values())
877
+ diagnostics.add(value);
878
+ for (const reserved of ["authorization", "api-key", "x-api-key"])
879
+ if (headers.has(reserved))
880
+ throw new TypeError(`${PREFIX} Use apiKey instead of '${reserved}' headers.`);
881
+ const key = await resolve(this.options.apiKey, signal);
882
+ if (typeof key !== "string" || !key.trim())
883
+ throw new TypeError(`${PREFIX} apiKey must resolve to a nonempty string.`);
884
+ diagnostics.add(key);
885
+ headers.set(keyHeader, key);
886
+ return headers;
887
+ }
888
+ fetch(url, init) {
889
+ return (this.options.fetch ?? fetch)(url, init);
890
+ }
891
+ url(protocol) {
892
+ if (protocol === "embeddings")
893
+ return `${this.baseUrl}/embeddings`;
894
+ if (protocol === "responses")
895
+ return `${this.baseUrl}/responses`;
896
+ if (protocol === "chat")
897
+ return `${this.baseUrl}/chat/completions`;
898
+ return `${new URL(this.baseUrl).origin}/anthropic/v1/messages`;
899
+ }
900
+ }
901
+ function requestId(response) {
902
+ return response.headers.get("apim-request-id") ?? response.headers.get("x-request-id") ?? response.headers.get("request-id") ?? undefined;
903
+ }
904
+ async function resolve(source, signal) {
905
+ signal.throwIfAborted();
906
+ if (typeof source !== "function")
907
+ return source;
908
+ return abortable(() => source(signal), signal);
909
+ }
910
+ function abortable(operation, signal) {
911
+ return new Promise((resolve, reject) => {
912
+ signal.throwIfAborted();
913
+ const abort = () => {
914
+ signal.removeEventListener("abort", abort);
915
+ reject(signal.reason);
916
+ };
917
+ signal.addEventListener("abort", abort, { once: true });
918
+ Promise.resolve().then(() => {
919
+ signal.throwIfAborted();
920
+ return operation();
921
+ }).then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
922
+ });
923
+ }
924
+ async function httpError(response, providerId, modelId, signal) {
925
+ let message = `Provider request failed with HTTP ${response.status}.`;
926
+ let code;
927
+ const reader = response.body?.getReader();
928
+ if (reader) {
929
+ const cancel = () => {
930
+ reader.cancel(signal.reason).catch(() => {});
931
+ };
932
+ signal.addEventListener("abort", cancel, { once: true });
933
+ const chunks = [];
934
+ let length = 0;
935
+ try {
936
+ while (length < 8192) {
937
+ signal.throwIfAborted();
938
+ const { value, done } = await abortable(() => reader.read(), signal);
939
+ if (done)
940
+ break;
941
+ const part = value.subarray(0, 8192 - length);
942
+ chunks.push(part);
943
+ length += part.length;
944
+ }
945
+ const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
946
+ const error = object(object(parsed)?.error);
947
+ if (typeof error?.message === "string")
948
+ message = error.message;
949
+ if (typeof error?.code === "string")
950
+ code = error.code;
951
+ else if (typeof error?.type === "string")
952
+ code = error.type;
953
+ } catch {} finally {
954
+ signal.removeEventListener("abort", cancel);
955
+ reader.cancel().catch(() => {});
956
+ reader.releaseLock();
957
+ }
958
+ }
959
+ signal.throwIfAborted();
960
+ const delay = response.headers.get("retry-after-ms") ?? response.headers.get("x-ms-retry-after-ms");
961
+ const retry = response.headers.get("retry-after");
962
+ const ms = delay !== null ? Number(delay) : retry !== null ? Number.isFinite(Number(retry)) ? Number(retry) * 1000 : Date.parse(retry) - Date.now() : undefined;
963
+ return new ModelProviderError2(`${PREFIX} ${message}`, providerId, modelId, {
964
+ status: response.status,
965
+ requestId: requestId(response),
966
+ code,
967
+ ...contextWindowRejection({ status: response.status, code, message }),
968
+ retryable: response.status === 408 || response.status === 429 || response.status >= 500,
969
+ ...ms !== undefined && Number.isFinite(ms) && ms >= 0 ? { retryAfterMs: Math.ceil(ms) } : {}
970
+ });
971
+ }
972
+ async function wait(ms, signal) {
973
+ signal.throwIfAborted();
974
+ await new Promise((resolve, reject) => {
975
+ const abort = () => {
976
+ clearTimeout(timer);
977
+ reject(signal.reason);
978
+ };
979
+ const timer = setTimeout(() => {
980
+ signal.removeEventListener("abort", abort);
981
+ resolve();
982
+ }, ms);
983
+ signal.addEventListener("abort", abort, { once: true });
984
+ });
985
+ }
986
+
987
+ // src/discovery.ts
1081
988
  class FoundryDeployments {
1082
989
  providerId;
1083
990
  transport;
@@ -2159,4 +2066,4 @@ export {
2159
2066
  createAzureAIFoundry
2160
2067
  };
2161
2068
 
2162
- //# debugId=22700C04D55CEB4464756E2164756E21
2069
+ //# debugId=96BCAAA89374958164756E2164756E21