@sixb/azure-ai-foundry 0.2.4 → 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.
package/dist/index.js CHANGED
@@ -4,8 +4,8 @@ import {
4
4
  assertJsonObject,
5
5
  MODEL_REASONING_LEVELS,
6
6
  ModelCatalogUnavailableError as ModelCatalogUnavailableError4,
7
- ModelProviderError as ModelProviderError4,
8
- UnsupportedModelFeatureError as UnsupportedModelFeatureError6
7
+ ModelProviderError as ModelProviderError7,
8
+ UnsupportedModelFeatureError as UnsupportedModelFeatureError9
9
9
  } from "@sixb/core/models";
10
10
  import { chatEvents } from "@sixb/model-protocols/chat";
11
11
  import { isStrictToolLimitError, messagesEvents } from "@sixb/model-protocols/messages";
@@ -766,6 +766,13 @@ function foundryChatRequest(request, definition, options, scope) {
766
766
  };
767
767
  }
768
768
 
769
+ // src/decision.ts
770
+ import {
771
+ defineModelRateCard as defineModelRateCard3,
772
+ ModelProviderError as ModelProviderError4
773
+ } from "@sixb/core/models";
774
+ import { systemOneRequest, systemOneResult } from "@sixb/model-protocols/systemone";
775
+
769
776
  // src/discovery.ts
770
777
  import {
771
778
  ModelCatalogUnavailableError as ModelCatalogUnavailableError2,
@@ -808,6 +815,18 @@ class RequestDiagnostics {
808
815
  // src/transport.ts
809
816
  import { ModelProviderError as ModelProviderError2 } from "@sixb/core/models";
810
817
  import { contextWindowRejection } from "@sixb/model-protocols/responses";
818
+ var ONE_SHOT = new Set([
819
+ "embeddings",
820
+ "decision",
821
+ "transcription",
822
+ "reranking",
823
+ "speech",
824
+ "image",
825
+ "image-edit"
826
+ ]);
827
+ var AUDIO_API_VERSION = "2024-10-21";
828
+ var IMAGE_API_VERSION = "2025-04-01-preview";
829
+
811
830
  class FoundryTransport {
812
831
  diagnostics = new WeakMap;
813
832
  baseUrl;
@@ -839,13 +858,16 @@ class FoundryTransport {
839
858
  };
840
859
  }
841
860
  async post(body, signal, providerId, modelId, protocol = "responses") {
842
- const url = this.url(protocol);
861
+ const url = this.url(protocol, modelId);
843
862
  const diagnostics = new RequestDiagnostics;
844
863
  for (let attempt = 0;; attempt++) {
845
864
  signal.throwIfAborted();
846
865
  const headers = await this.headers(signal, diagnostics, protocol === "messages" ? "x-api-key" : "api-key");
847
- headers.set("content-type", "application/json");
848
- headers.set("accept", protocol === "embeddings" ? "application/json" : "text/event-stream");
866
+ if (typeof body === "string")
867
+ headers.set("content-type", "application/json");
868
+ else
869
+ headers.delete("content-type");
870
+ headers.set("accept", ONE_SHOT.has(protocol) && protocol !== "speech" ? "application/json" : "text/event-stream");
849
871
  if (protocol === "messages")
850
872
  headers.set("anthropic-version", "2023-06-01");
851
873
  signal.throwIfAborted();
@@ -873,7 +895,7 @@ class FoundryTransport {
873
895
  if (response.ok)
874
896
  return response;
875
897
  const error = this.redactError(response, await httpError(response, providerId, modelId, signal));
876
- if (protocol === "embeddings" || !error.retryable || attempt >= (this.options.maxRetries ?? 2))
898
+ if (ONE_SHOT.has(protocol) || !error.retryable || attempt >= (this.options.maxRetries ?? 2))
877
899
  throw error;
878
900
  await wait(Math.min(error.retryAfterMs ?? 250 * 2 ** attempt, this.options.maxRetryDelayMs ?? 60000), signal);
879
901
  }
@@ -904,9 +926,19 @@ class FoundryTransport {
904
926
  fetch(url, init) {
905
927
  return (this.options.fetch ?? fetch)(url, init);
906
928
  }
907
- url(protocol) {
929
+ url(protocol, deployment) {
908
930
  if (protocol === "embeddings")
909
931
  return `${this.baseUrl}/embeddings`;
932
+ if (protocol === "transcription")
933
+ return `${new URL(this.baseUrl).origin}/openai/deployments/${encodeURIComponent(deployment ?? "")}/audio/transcriptions?api-version=${AUDIO_API_VERSION}`;
934
+ if (protocol === "decision")
935
+ return `${new URL(this.baseUrl).origin}/providers/microsoft/v1/systemone`;
936
+ if (protocol === "image" || protocol === "image-edit")
937
+ return `${new URL(this.baseUrl).origin}/openai/deployments/${encodeURIComponent(deployment ?? "")}/images/${protocol === "image" ? "generations" : "edits"}?api-version=${IMAGE_API_VERSION}`;
938
+ if (protocol === "reranking")
939
+ return `${new URL(this.baseUrl).origin}/providers/cohere/v2/rerank`;
940
+ if (protocol === "speech")
941
+ return `${new URL(this.baseUrl).origin}/openai/v1/audio/speech`;
910
942
  if (protocol === "responses")
911
943
  return `${this.baseUrl}/responses`;
912
944
  if (protocol === "chat")
@@ -914,6 +946,31 @@ class FoundryTransport {
914
946
  return `${new URL(this.baseUrl).origin}/anthropic/v1/messages`;
915
947
  }
916
948
  }
949
+ async function readJson(response, signal, maxBytes) {
950
+ return JSON.parse(await readText(response, signal, maxBytes));
951
+ }
952
+ async function readText(response, signal, maxBytes) {
953
+ const reader = response.body?.getReader();
954
+ if (!reader)
955
+ throw new Error("Empty response");
956
+ const chunks = [];
957
+ let bytes = 0;
958
+ try {
959
+ for (;; ) {
960
+ const chunk = await abortable(() => reader.read(), signal);
961
+ if (chunk.done)
962
+ break;
963
+ bytes += chunk.value.length;
964
+ if (bytes > maxBytes)
965
+ throw new Error(`Response exceeds ${maxBytes} bytes`);
966
+ chunks.push(chunk.value);
967
+ }
968
+ return Buffer.concat(chunks).toString("utf8");
969
+ } finally {
970
+ reader.cancel().catch(() => {});
971
+ reader.releaseLock();
972
+ }
973
+ }
917
974
  function requestId(response) {
918
975
  return response.headers.get("apim-request-id") ?? response.headers.get("x-request-id") ?? response.headers.get("request-id") ?? undefined;
919
976
  }
@@ -960,8 +1017,11 @@ async function httpError(response, providerId, modelId, signal) {
960
1017
  }
961
1018
  const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
962
1019
  const error = object(object(parsed)?.error);
1020
+ const detail = object(parsed)?.detail;
963
1021
  if (typeof error?.message === "string")
964
1022
  message = error.message;
1023
+ else if (typeof detail === "string")
1024
+ message = detail;
965
1025
  if (typeof error?.code === "string")
966
1026
  code = error.code;
967
1027
  else if (typeof error?.type === "string")
@@ -1001,6 +1061,18 @@ async function wait(ms, signal) {
1001
1061
  }
1002
1062
 
1003
1063
  // src/discovery.ts
1064
+ function deploymentMetadata(resolution) {
1065
+ const deployment = resolution?.deployment;
1066
+ return Object.freeze(deployment ? {
1067
+ deployment,
1068
+ modelName: deployment.modelName,
1069
+ modelVersion: deployment.modelVersion,
1070
+ publisher: deployment.modelPublisher,
1071
+ sku: deployment.sku.name,
1072
+ discoveredAt: resolution.discoveredAt
1073
+ } : {});
1074
+ }
1075
+
1004
1076
  class FoundryDeployments {
1005
1077
  providerId;
1006
1078
  transport;
@@ -1258,12 +1330,167 @@ function deploymentRecord(raw, invalid) {
1258
1330
  });
1259
1331
  }
1260
1332
 
1333
+ // src/kinds.ts
1334
+ import { UnsupportedModelFeatureError as UnsupportedModelFeatureError3 } from "@sixb/core/models";
1335
+ var KIND_BINDINGS = {
1336
+ language: "foundry() or foundry.chat()",
1337
+ embedding: "foundry.embedding()",
1338
+ decision: "foundry.decision()",
1339
+ transcription: "foundry.transcription()",
1340
+ reranking: "foundry.reranking()",
1341
+ speech: "foundry.speech()",
1342
+ image: "foundry.image()"
1343
+ };
1344
+ function deploymentKind(deployment) {
1345
+ if (isEmbeddingDeployment(deployment))
1346
+ return "embedding";
1347
+ if (isDecisionDeployment(deployment))
1348
+ return "decision";
1349
+ if (isTranscriptionDeployment(deployment))
1350
+ return "transcription";
1351
+ if (/^cohere-rerank-/i.test(deployment.modelName))
1352
+ return "reranking";
1353
+ if (deployment.capabilities.audioSpeech === "true" || /^(gpt-4o-mini-tts|tts|tts-hd)$/i.test(deployment.modelName))
1354
+ return "speech";
1355
+ if (deployment.capabilities.imageGenerations === "true" || /^(gpt-image-|flux[-.]|dall-e-)/i.test(deployment.modelName))
1356
+ return "image";
1357
+ return "language";
1358
+ }
1359
+ function assertDeploymentKind(deployment, deploymentName, kind) {
1360
+ const actual = deploymentKind(deployment);
1361
+ if (actual !== kind)
1362
+ throw new UnsupportedModelFeatureError3(`${PREFIX} Deployment '${deploymentName}' runs ${deployment.modelName}, not ${article(kind)} ${kind} model. Use ${KIND_BINDINGS[actual]}.`);
1363
+ if (deployment.connectionName)
1364
+ throw new UnsupportedModelFeatureError3(`${PREFIX} ${capitalize(article(kind))} ${kind} deployment from a project connection cannot be routed; use the owning resource's project URL and API key.`);
1365
+ }
1366
+ function article(kind) {
1367
+ return /^[aeiou]/.test(kind) ? "an" : "a";
1368
+ }
1369
+ function capitalize(word) {
1370
+ return word.charAt(0).toUpperCase() + word.slice(1);
1371
+ }
1372
+ function isEmbeddingDeployment(deployment) {
1373
+ return deployment.capabilities.embeddings === "true" || deployment.capabilities.embedding === "true" || /^(text-embedding-|cohere-embed)/i.test(deployment.modelName);
1374
+ }
1375
+ function isDecisionDeployment(deployment) {
1376
+ return deployment.capabilities.decision === "true" || /^microsoft-decision-/i.test(deployment.modelName);
1377
+ }
1378
+ function isTranscriptionDeployment(deployment) {
1379
+ return deployment.capabilities.audioTranscriptions === "true" || /^(whisper|gpt-4o-transcribe|gpt-4o-mini-transcribe|gpt-transcribe)(-|$)/i.test(deployment.modelName);
1380
+ }
1381
+
1382
+ // src/decision.ts
1383
+ var DECISION_1_RATE_CARD = { currency: "USD", input: "0.042", output: "0" };
1384
+ var CAPABILITIES = Object.freeze({
1385
+ questions: Object.freeze(["choice", "score", "probability"]),
1386
+ maxChoices: 255,
1387
+ maxScoreLevels: 10
1388
+ });
1389
+ var MAX_RESPONSE_BYTES = 16 * 1024 * 1024;
1390
+ var MAX_TIMEOUT_MS = 2 ** 31 - 1;
1391
+ function createFoundryDecision(providerId, modelId, options, transport, deployments) {
1392
+ if (!modelId.trim() || modelId !== modelId.trim())
1393
+ throw new TypeError(`${PREFIX} deploymentName must be nonempty without surrounding whitespace.`);
1394
+ positiveInteger(options.timeoutMs, "timeoutMs");
1395
+ if (options.timeoutMs !== undefined && options.timeoutMs > MAX_TIMEOUT_MS)
1396
+ throw new TypeError(`${PREFIX} timeoutMs must be at most ${MAX_TIMEOUT_MS}.`);
1397
+ if (options.rateCard && options.costEstimator)
1398
+ throw new TypeError(`${PREFIX} Configure either rateCard or costEstimator, not both.`);
1399
+ const settings = Object.freeze({
1400
+ timeoutMs: options.timeoutMs ?? 30000,
1401
+ ...options.rateCard ? { rateCard: defineModelRateCard3(options.rateCard) } : {},
1402
+ ...options.costEstimator ? { costEstimator: options.costEstimator } : {}
1403
+ });
1404
+ return decisionBinding(providerId, modelId, settings, transport, deployments);
1405
+ }
1406
+ function decisionBinding(providerId, modelId, settings, transport, deployments, resolution) {
1407
+ const deployment = resolution?.deployment;
1408
+ const definition = Object.freeze({
1409
+ kind: "decision",
1410
+ providerId,
1411
+ modelId,
1412
+ ...deployment ? { publisher: azurePublisher(deployment.modelPublisher) } : {},
1413
+ via: "Azure AI Foundry",
1414
+ capabilities: CAPABILITIES
1415
+ });
1416
+ let pending;
1417
+ const binding = Object.freeze({
1418
+ providerId,
1419
+ modelId,
1420
+ definition,
1421
+ metadata: deploymentMetadata(resolution),
1422
+ costEstimator: resolution?.costEstimator,
1423
+ async resolve(input) {
1424
+ if (resolution)
1425
+ return binding;
1426
+ const resolved = await deployments.resolve(modelId, input?.offline === true);
1427
+ const found = resolved.deployment;
1428
+ assertDeploymentKind(found, modelId, "decision");
1429
+ const models = [modelId, found.modelName, `${found.modelName}-${found.modelVersion}`].map((name) => name.toLowerCase());
1430
+ return decisionBinding(providerId, modelId, settings, transport, deployments, {
1431
+ ...resolved,
1432
+ models,
1433
+ costEstimator: settings.costEstimator ?? decisionEstimator(found, settings.rateCard, models)
1434
+ });
1435
+ },
1436
+ async evaluate(request) {
1437
+ const signal = AbortSignal.any([
1438
+ ...request.signal ? [request.signal] : [],
1439
+ AbortSignal.timeout(settings.timeoutMs)
1440
+ ]);
1441
+ signal.throwIfAborted();
1442
+ if (!resolution) {
1443
+ pending ??= binding.resolve().catch((error) => {
1444
+ pending = undefined;
1445
+ throw error;
1446
+ });
1447
+ const resolved = await abortable(() => pending, signal);
1448
+ return resolved.evaluate({ ...request, signal });
1449
+ }
1450
+ const response = await transport.post(JSON.stringify(systemOneRequest(modelId, request)), signal, providerId, modelId, "decision");
1451
+ const id = transport.redactText(response, requestId(response));
1452
+ let body;
1453
+ try {
1454
+ body = await readJson(response, signal, MAX_RESPONSE_BYTES);
1455
+ } catch {
1456
+ if (signal.aborted)
1457
+ throw signal.reason;
1458
+ throw new ModelProviderError4(`${PREFIX} Decision model returned invalid JSON.`, providerId, modelId, { code: "invalid_json", requestId: id, status: response.status });
1459
+ }
1460
+ return systemOneResult(body, request.questions, {
1461
+ providerId,
1462
+ modelId,
1463
+ errorPrefix: PREFIX,
1464
+ requestId: id,
1465
+ assertModel: (model) => {
1466
+ if (typeof model !== "string" || !resolution.models.includes(model.toLowerCase()))
1467
+ throw new Error("Unexpected response model");
1468
+ },
1469
+ redact: (value) => transport.redactText(response, value) ?? value
1470
+ });
1471
+ }
1472
+ });
1473
+ return binding;
1474
+ }
1475
+ function decisionEstimator(deployment, rateCard, models) {
1476
+ const { modelName, modelVersion } = deployment;
1477
+ const published = modelName.toLowerCase() === "microsoft-decision-1" && modelVersion === "1" ? DECISION_1_RATE_CARD : undefined;
1478
+ const estimator = tokenEstimator(rateCard ?? published, undefined, [], () => true);
1479
+ return {
1480
+ estimateReservation: estimator.estimateReservation,
1481
+ estimate: (input) => {
1482
+ const actual = input.route?.modelId ?? input.responseModelId;
1483
+ return actual === undefined || models.includes(actual.toLowerCase()) ? estimator.estimate(input) : { status: "unpriceable", reason: "missing-rate-card" };
1484
+ }
1485
+ };
1486
+ }
1487
+
1261
1488
  // src/embedding.ts
1262
1489
  import {
1263
- defineModelRateCard as defineModelRateCard3,
1490
+ defineModelRateCard as defineModelRateCard4,
1264
1491
  EmbeddingModelResponseError,
1265
1492
  ModelCatalogUnavailableError as ModelCatalogUnavailableError3,
1266
- UnsupportedModelFeatureError as UnsupportedModelFeatureError3
1493
+ UnsupportedModelFeatureError as UnsupportedModelFeatureError4
1267
1494
  } from "@sixb/core/models";
1268
1495
  function createFoundryEmbedding(providerId, modelId, options, transport, deployments, catalog, resolution) {
1269
1496
  const dimensions = options.dimensions;
@@ -1286,18 +1513,10 @@ function createFoundryEmbedding(providerId, modelId, options, transport, deploym
1286
1513
  const settings = Object.freeze({
1287
1514
  model: representation,
1288
1515
  dimensions,
1289
- rateCard: options.rateCard && defineModelRateCard3(options.rateCard),
1516
+ rateCard: options.rateCard && defineModelRateCard4(options.rateCard),
1290
1517
  costEstimator: options.costEstimator
1291
1518
  });
1292
- const deployment = resolution?.deployment;
1293
- const metadata = Object.freeze(deployment ? {
1294
- deployment,
1295
- modelName: deployment.modelName,
1296
- modelVersion: deployment.modelVersion,
1297
- publisher: deployment.modelPublisher,
1298
- sku: deployment.sku.name,
1299
- discoveredAt: resolution.discoveredAt
1300
- } : {});
1519
+ const metadata = deploymentMetadata(resolution);
1301
1520
  let pending;
1302
1521
  const binding = Object.freeze({
1303
1522
  providerId,
@@ -1370,7 +1589,7 @@ function validateDeployment(resolved, dimensions) {
1370
1589
  const name = deployment.modelName.toLowerCase();
1371
1590
  const maxDimensions = name === "text-embedding-3-small" ? 1536 : name === "text-embedding-3-large" ? 3072 : name === "text-embedding-ada-002" ? 1536 : undefined;
1372
1591
  if (deployment.modelPublisher.toLowerCase() !== "openai" || !maxDimensions || deployment.capabilities.embeddings === "false" || deployment.capabilities.embedding === "false")
1373
- throw new UnsupportedModelFeatureError3(`${PREFIX} This embedding deployment is not supported. Use an OpenAI text-embedding-3-small, text-embedding-3-large, or text-embedding-ada-002 deployment.`);
1592
+ throw new UnsupportedModelFeatureError4(`${PREFIX} This embedding deployment is not supported. Use an OpenAI text-embedding-3-small, text-embedding-3-large, or text-embedding-ada-002 deployment.`);
1374
1593
  const configurable = name !== "text-embedding-ada-002";
1375
1594
  if (dimensions > maxDimensions || !configurable && dimensions !== maxDimensions)
1376
1595
  throw new TypeError(`${PREFIX} ${name} requires ${configurable ? `1\u2013${maxDimensions}` : maxDimensions} dimensions.`);
@@ -1401,28 +1620,6 @@ function parseVectors(data, count, dimensions, invalid) {
1401
1620
  function invalidResponse(providerId, modelId, metadata) {
1402
1621
  return new EmbeddingModelResponseError(`${PREFIX} Invalid embedding response: expected the resolved model and one finite, nonzero vector of the configured dimension per input, with unique indices.`, providerId, modelId, metadata);
1403
1622
  }
1404
- async function readResponse(response, signal) {
1405
- const reader = response.body?.getReader();
1406
- if (!reader)
1407
- throw new Error("Empty embedding response");
1408
- const chunks = [];
1409
- let bytes = 0;
1410
- try {
1411
- for (;; ) {
1412
- const chunk = await abortable(() => reader.read(), signal);
1413
- if (chunk.done)
1414
- break;
1415
- bytes += chunk.value.length;
1416
- if (bytes > 128 * 1024 * 1024)
1417
- throw new Error("Embedding response exceeds 128 MiB");
1418
- chunks.push(chunk.value);
1419
- }
1420
- return JSON.parse(Buffer.concat(chunks).toString("utf8"));
1421
- } finally {
1422
- reader.cancel().catch(() => {});
1423
- reader.releaseLock();
1424
- }
1425
- }
1426
1623
  function embeddingUsage(raw) {
1427
1624
  const inputTokens = counter(raw?.prompt_tokens);
1428
1625
  const totalTokens = counter(raw?.total_tokens);
@@ -1450,7 +1647,7 @@ async function readEmbeddingResult(response, transport, signal, expected) {
1450
1647
  };
1451
1648
  let body;
1452
1649
  try {
1453
- body = record(await readResponse(response, signal));
1650
+ body = record(await readJson(response, signal, 128 * 1024 * 1024));
1454
1651
  } catch {
1455
1652
  if (signal.aborted)
1456
1653
  throw signal.reason;
@@ -1470,6 +1667,293 @@ async function readEmbeddingResult(response, transport, signal, expected) {
1470
1667
  };
1471
1668
  }
1472
1669
 
1670
+ // src/image.ts
1671
+ import {
1672
+ defineModelRateCard as defineModelRateCard5,
1673
+ estimateModelReservation as estimateModelReservation2,
1674
+ ImageModelResponseError,
1675
+ rateModelCall as rateModelCall2,
1676
+ UnsupportedModelFeatureError as UnsupportedModelFeatureError5
1677
+ } from "@sixb/core/models";
1678
+ var MAX_RESPONSE_BYTES2 = 512 * 1024 * 1024;
1679
+ var MAX_TIMEOUT_MS2 = 2 ** 31 - 1;
1680
+ var INPUT_MEDIA_TYPES = Object.freeze(["image/png", "image/jpeg", "image/webp"]);
1681
+ var EXTENSIONS = {
1682
+ "image/png": "png",
1683
+ "image/jpeg": "jpg",
1684
+ "image/webp": "webp"
1685
+ };
1686
+ var GPT_SIZES = Object.freeze(["auto", "1024x1024", "1536x1024", "1024x1536"]);
1687
+ var GPT_QUALITIES = Object.freeze(["auto", "low", "medium", "high"]);
1688
+ var GPT_INPUT = Object.freeze({
1689
+ maxCount: 16,
1690
+ maxBytes: 50 * 1024 * 1024,
1691
+ mediaTypes: INPUT_MEDIA_TYPES
1692
+ });
1693
+ function family(deployment) {
1694
+ const name = deployment.modelName.toLowerCase();
1695
+ if (name.startsWith("gpt-image-"))
1696
+ return {
1697
+ gpt: true,
1698
+ definition: {
1699
+ maxImagesPerCall: 10,
1700
+ .../^gpt-image-1(\.5|-mini)?$/.test(name) ? { sizes: GPT_SIZES } : {},
1701
+ qualities: GPT_QUALITIES,
1702
+ inputImages: GPT_INPUT
1703
+ }
1704
+ };
1705
+ if (name === "flux.1-kontext-pro")
1706
+ return {
1707
+ gpt: false,
1708
+ definition: {
1709
+ maxImagesPerCall: 1,
1710
+ inputImages: { maxCount: 1, maxBytes: 20 * 1024 * 1024, mediaTypes: INPUT_MEDIA_TYPES }
1711
+ }
1712
+ };
1713
+ if (name === "flux-1.1-pro")
1714
+ return { gpt: false, definition: { maxImagesPerCall: 1 } };
1715
+ throw new UnsupportedModelFeatureError5(`${PREFIX} ${deployment.modelName} is not supported: use a GPT Image, FLUX.1 Kontext [pro] or FLUX1.1 [pro] deployment.`);
1716
+ }
1717
+ var gpt = (input, imageOutput, textOutput) => ({
1718
+ card: textOutput === undefined ? { currency: "USD", input, output: imageOutput } : { currency: "USD", input, output: textOutput, imageOutput },
1719
+ textOutput: textOutput !== undefined
1720
+ });
1721
+ var perImage = (images) => ({
1722
+ card: { currency: "USD", images },
1723
+ textOutput: false
1724
+ });
1725
+ var PUBLISHED = {
1726
+ "gpt-image-1": {
1727
+ version: "2025-04-15",
1728
+ skus: { GlobalStandard: gpt("5", "40"), DataZoneStandard: gpt("5.5", "44") }
1729
+ },
1730
+ "gpt-image-1-mini": {
1731
+ version: "2025-10-06",
1732
+ skus: { GlobalStandard: gpt("2", "8"), DataZoneStandard: gpt("2.2", "8.8") }
1733
+ },
1734
+ "gpt-image-1.5": {
1735
+ version: "2025-12-16",
1736
+ skus: {
1737
+ GlobalStandard: gpt("5", "32", "10"),
1738
+ DataZoneStandard: gpt("5.5", "35.2", "11")
1739
+ }
1740
+ },
1741
+ "gpt-image-2": {
1742
+ version: "2026-04-21",
1743
+ skus: { GlobalStandard: gpt("5", "30"), DataZoneStandard: gpt("5.5", "33") }
1744
+ },
1745
+ "flux.1-kontext-pro": {
1746
+ version: "1",
1747
+ skus: {
1748
+ GlobalStandard: perImage("0.04"),
1749
+ DataZoneStandard: perImage("0.044"),
1750
+ Standard: perImage("0.044")
1751
+ }
1752
+ },
1753
+ "flux-1.1-pro": {
1754
+ version: "1",
1755
+ skus: {
1756
+ GlobalStandard: perImage("0.04"),
1757
+ DataZoneStandard: perImage("0.044"),
1758
+ Standard: perImage("0.044")
1759
+ }
1760
+ }
1761
+ };
1762
+ function createFoundryImage(providerId, modelId, options, transport, deployments) {
1763
+ if (!modelId.trim() || modelId !== modelId.trim())
1764
+ throw new TypeError(`${PREFIX} deploymentName must be nonempty without surrounding whitespace.`);
1765
+ positiveInteger(options.timeoutMs, "timeoutMs");
1766
+ if (options.timeoutMs !== undefined && options.timeoutMs > MAX_TIMEOUT_MS2)
1767
+ throw new TypeError(`${PREFIX} timeoutMs must be at most ${MAX_TIMEOUT_MS2}.`);
1768
+ if (options.description !== undefined && (typeof options.description !== "string" || !options.description.trim()))
1769
+ throw new TypeError(`${PREFIX} description must be a nonblank string.`);
1770
+ if (options.rateCard && options.costEstimator)
1771
+ throw new TypeError(`${PREFIX} Configure either rateCard or costEstimator, not both.`);
1772
+ const settings = Object.freeze({
1773
+ timeoutMs: options.timeoutMs ?? 180000,
1774
+ ...options.description ? { description: options.description } : {},
1775
+ ...options.rateCard ? { rateCard: defineModelRateCard5(options.rateCard) } : {},
1776
+ ...options.costEstimator ? { costEstimator: options.costEstimator } : {}
1777
+ });
1778
+ return imageBinding(providerId, modelId, settings, transport, deployments);
1779
+ }
1780
+ function imageBinding(providerId, modelId, settings, transport, deployments, resolution) {
1781
+ const deployment = resolution?.deployment;
1782
+ const definition = Object.freeze({
1783
+ kind: "image",
1784
+ providerId,
1785
+ modelId,
1786
+ ...deployment ? { publisher: azurePublisher(deployment.modelPublisher) } : {},
1787
+ via: "Azure AI Foundry",
1788
+ ...settings.description ? { description: settings.description } : {},
1789
+ ...resolution ? resolution.family.definition : { inputImages: GPT_INPUT }
1790
+ });
1791
+ let pending;
1792
+ const binding = Object.freeze({
1793
+ providerId,
1794
+ modelId,
1795
+ definition,
1796
+ metadata: deploymentMetadata(resolution),
1797
+ ...resolution?.costEstimator ? { costEstimator: resolution.costEstimator } : {},
1798
+ ...resolution?.rateCard ? { rateCard: resolution.rateCard } : {},
1799
+ async resolve(input) {
1800
+ if (resolution)
1801
+ return binding;
1802
+ const resolved = await deployments.resolve(modelId, input?.offline === true);
1803
+ assertDeploymentKind(resolved.deployment, modelId, "image");
1804
+ const found = family(resolved.deployment);
1805
+ const published = settings.costEstimator ? undefined : publishedPrice(resolved.deployment);
1806
+ const rateCard = settings.costEstimator ? undefined : settings.rateCard ?? published?.card;
1807
+ return imageBinding(providerId, modelId, settings, transport, deployments, {
1808
+ ...resolved,
1809
+ family: found,
1810
+ ...rateCard ? { rateCard } : {},
1811
+ ...settings.costEstimator || rateCard ? {
1812
+ costEstimator: settings.costEstimator ?? imageEstimator(rateCard, settings.rateCard ? true : published.textOutput)
1813
+ } : {}
1814
+ });
1815
+ },
1816
+ async generate(request) {
1817
+ const signal = AbortSignal.any([
1818
+ ...request.signal ? [request.signal] : [],
1819
+ AbortSignal.timeout(settings.timeoutMs)
1820
+ ]);
1821
+ signal.throwIfAborted();
1822
+ if (typeof request.prompt !== "string" || !request.prompt.trim())
1823
+ throw new TypeError(`${PREFIX} Image prompt must be a nonblank string.`);
1824
+ if (!Number.isSafeInteger(request.n) || request.n < 1)
1825
+ throw new TypeError(`${PREFIX} Image count must be a positive integer.`);
1826
+ if (!resolution) {
1827
+ pending ??= binding.resolve().catch((error) => {
1828
+ pending = undefined;
1829
+ throw error;
1830
+ });
1831
+ const resolved = await abortable(() => pending, signal);
1832
+ return resolved.generate({ ...request, signal });
1833
+ }
1834
+ const { gpt } = resolution.family;
1835
+ const { maxImagesPerCall, inputImages } = resolution.family.definition;
1836
+ if (maxImagesPerCall !== undefined && request.n > maxImagesPerCall)
1837
+ throw new TypeError(`${PREFIX} ${modelId} returns at most ${maxImagesPerCall} per call.`);
1838
+ const images = request.images ?? [];
1839
+ if (images.length > (inputImages?.maxCount ?? 0))
1840
+ throw new TypeError(`${PREFIX} ${modelId} takes at most ${inputImages?.maxCount ?? 0} reference images.`);
1841
+ for (const image of images)
1842
+ if (!EXTENSIONS[image.mediaType] || image.data.byteLength > inputImages.maxBytes)
1843
+ throw new TypeError(`${PREFIX} Reference images must be PNG, JPEG or WebP within maxBytes.`);
1844
+ const warnings = [
1845
+ ...request.seed === undefined ? [] : ["seed: not supported"],
1846
+ ...request.style === undefined ? [] : ["style: not supported"],
1847
+ ...request.aspectRatio === undefined ? [] : ["aspectRatio: not supported; use size"],
1848
+ ...request.quality === undefined || gpt ? [] : ["quality: not supported"]
1849
+ ];
1850
+ const fields = {
1851
+ prompt: request.prompt,
1852
+ ...request.n === 1 ? {} : { n: request.n },
1853
+ ...request.size === undefined ? {} : { size: request.size },
1854
+ ...request.quality === undefined || !gpt ? {} : { quality: request.quality }
1855
+ };
1856
+ const edit = images.length > 0;
1857
+ const response = await transport.post(edit ? editForm(fields, images, gpt) : JSON.stringify(fields), signal, providerId, modelId, edit ? "image-edit" : "image");
1858
+ const id = transport.redactText(response, requestId(response));
1859
+ const providerIds = id ? { providerIds: { requestId: id } } : {};
1860
+ let body;
1861
+ try {
1862
+ body = await readJson(response, signal, MAX_RESPONSE_BYTES2);
1863
+ } catch {
1864
+ if (signal.aborted)
1865
+ throw signal.reason;
1866
+ throw new ImageModelResponseError(`${PREFIX} Image generation returned invalid or oversized JSON.`, providerId, modelId, providerIds);
1867
+ }
1868
+ const payload = object(body);
1869
+ const decoded = decodeImages(payload?.data);
1870
+ const metadata = {
1871
+ usage: imageUsage(payload, Array.isArray(payload?.data) ? payload.data.length : undefined),
1872
+ ...providerIds
1873
+ };
1874
+ if (!decoded || decoded.length === 0 || decoded.length > request.n)
1875
+ throw new ImageModelResponseError(`${PREFIX} Image generation returned no readable images.`, providerId, modelId, metadata);
1876
+ return {
1877
+ ...metadata,
1878
+ output: { images: decoded },
1879
+ ...warnings.length ? { warnings } : {}
1880
+ };
1881
+ }
1882
+ });
1883
+ return binding;
1884
+ }
1885
+ function editForm(fields, images, gpt) {
1886
+ const form = new FormData;
1887
+ for (const [key, value] of Object.entries(fields))
1888
+ form.set(key, String(value));
1889
+ images.forEach((image, index) => {
1890
+ const file = new Blob([new Uint8Array(image.data)], { type: image.mediaType });
1891
+ form.append(gpt ? "image[]" : "image", file, `image-${index}.${EXTENSIONS[image.mediaType]}`);
1892
+ });
1893
+ return form;
1894
+ }
1895
+ function decodeImages(value) {
1896
+ if (!Array.isArray(value))
1897
+ return;
1898
+ const images = [];
1899
+ for (const entry of value) {
1900
+ const encoded = object(entry)?.b64_json;
1901
+ if (typeof encoded !== "string" || !encoded)
1902
+ return;
1903
+ const data = new Uint8Array(Buffer.from(encoded, "base64"));
1904
+ const mediaType = imageMediaType(data);
1905
+ if (!mediaType)
1906
+ return;
1907
+ images.push({ data, mediaType });
1908
+ }
1909
+ return images;
1910
+ }
1911
+ function imageMediaType(bytes) {
1912
+ const head = Buffer.from(bytes.subarray(0, 12));
1913
+ if (head.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10])))
1914
+ return "image/png";
1915
+ if (head[0] === 255 && head[1] === 216 && head[2] === 255)
1916
+ return "image/jpeg";
1917
+ if (head.toString("latin1", 0, 4) === "RIFF" && head.toString("latin1", 8, 12) === "WEBP")
1918
+ return "image/webp";
1919
+ return;
1920
+ }
1921
+ function imageUsage(payload, images) {
1922
+ const raw = object(payload?.usage);
1923
+ const output = object(raw?.output_tokens_details);
1924
+ const inputTokens = counter(raw?.input_tokens);
1925
+ const outputTokens = counter(raw?.output_tokens);
1926
+ const imageOutputTokens = counter(output?.image_tokens);
1927
+ const textOutputTokens = counter(output?.text_tokens);
1928
+ return {
1929
+ ...images === undefined ? {} : { outputImages: images },
1930
+ ...inputTokens === undefined ? {} : { inputTokens },
1931
+ ...outputTokens === undefined ? {} : { outputTokens },
1932
+ ...imageOutputTokens === undefined ? {} : { imageOutputTokens },
1933
+ ...textOutputTokens === undefined ? {} : { textOutputTokens },
1934
+ ...raw ? { raw } : {}
1935
+ };
1936
+ }
1937
+ function publishedPrice(deployment) {
1938
+ const name = deployment.modelName.toLowerCase();
1939
+ const entry = Object.hasOwn(PUBLISHED, name) ? PUBLISHED[name] : undefined;
1940
+ if (!entry || entry.version !== deployment.modelVersion)
1941
+ return;
1942
+ const price = Object.hasOwn(entry.skus, deployment.sku.name) ? entry.skus[deployment.sku.name] : undefined;
1943
+ return price && { card: defineModelRateCard5(price.card), textOutput: price.textOutput };
1944
+ }
1945
+ function imageEstimator(rateCard, textOutput) {
1946
+ return {
1947
+ estimateReservation: (input) => rateCard.images === undefined ? undefined : estimateModelReservation2({ outputImages: input.n, rateCard }),
1948
+ estimate: ({ usage, dimensions }) => {
1949
+ const details = object(usage.raw?.input_tokens_details);
1950
+ if (details?.image_tokens !== undefined && details.image_tokens !== 0 || !textOutput && (usage.textOutputTokens ?? 0) > 0)
1951
+ return { status: "unpriceable", reason: "missing-rate-card" };
1952
+ return rateModelCall2({ usage, rateCard, dimensions });
1953
+ }
1954
+ };
1955
+ }
1956
+
1473
1957
  // src/messages-accounting.ts
1474
1958
  function foundryMessagesUsage(raw) {
1475
1959
  const uncached = counter(raw.input_tokens);
@@ -1538,7 +2022,7 @@ function tokenMeters2(raw) {
1538
2022
  // src/messages-request.ts
1539
2023
  import {
1540
2024
  modelReasoningSupportIssue as modelReasoningSupportIssue3,
1541
- UnsupportedModelFeatureError as UnsupportedModelFeatureError4
2025
+ UnsupportedModelFeatureError as UnsupportedModelFeatureError6
1542
2026
  } from "@sixb/core/models";
1543
2027
  import {
1544
2028
  messagesInput,
@@ -1660,7 +2144,7 @@ function foundryMessagesRequest(request, definition, options, scope, unconstrain
1660
2144
  let conversation = false;
1661
2145
  for (const message of request.messages) {
1662
2146
  if (message.role === "system" && conversation)
1663
- throw new UnsupportedModelFeatureError4(`${PREFIX} Messages system instructions must precede the conversation.`);
2147
+ throw new UnsupportedModelFeatureError6(`${PREFIX} Messages system instructions must precede the conversation.`);
1664
2148
  if (message.role !== "system")
1665
2149
  conversation = true;
1666
2150
  }
@@ -1668,13 +2152,13 @@ function foundryMessagesRequest(request, definition, options, scope, unconstrain
1668
2152
  throw new TypeError(`${PREFIX} Native Messages requires maxOutputTokens in the definition, binding, or request.`);
1669
2153
  const caps = definition.capabilities;
1670
2154
  if (request.tools.length && caps.localTools !== true)
1671
- throw new UnsupportedModelFeatureError4(`${PREFIX} Configure localTools capability before using Messages tools.`);
2155
+ throw new UnsupportedModelFeatureError6(`${PREFIX} Configure localTools capability before using Messages tools.`);
1672
2156
  const schema = request.responseFormat && messagesOutputSchema(request.responseFormat.schema);
1673
2157
  if (request.responseFormat && (caps.nativeStructuredOutput !== true || !schema))
1674
- throw new UnsupportedModelFeatureError4(`${PREFIX} Messages structured output requires a declared capability and a supported closed-object Claude schema.`);
2158
+ throw new UnsupportedModelFeatureError6(`${PREFIX} Messages structured output requires a declared capability and a supported closed-object Claude schema.`);
1675
2159
  const thinking = reasoningRequest(request, definition, options, max);
1676
2160
  if (thinking.thinking && object(thinking.thinking)?.type !== "disabled" && ["temperature", "top_p", "top_k"].some((key) => options.request?.[key] !== undefined))
1677
- throw new UnsupportedModelFeatureError4(`${PREFIX} Explicit sampling controls cannot be combined with Messages thinking.`);
2161
+ throw new UnsupportedModelFeatureError6(`${PREFIX} Explicit sampling controls cannot be combined with Messages thinking.`);
1678
2162
  const mapped = messagesInput(request.messages, definition.providerId, PREFIX);
1679
2163
  const output = {
1680
2164
  ...thinking.effort === undefined ? {} : { effort: thinking.effort },
@@ -1718,7 +2202,7 @@ function reasoningRequest(request, definition, options, max) {
1718
2202
  const caps = definition.capabilities.reasoning;
1719
2203
  const issue = modelReasoningSupportIssue3(caps, reasoning);
1720
2204
  if (caps === undefined || issue)
1721
- throw new UnsupportedModelFeatureError4(`${PREFIX} ${issue ?? "Configure reasoning capabilities for this deployment"}.`);
2205
+ throw new UnsupportedModelFeatureError6(`${PREFIX} ${issue ?? "Configure reasoning capabilities for this deployment"}.`);
1722
2206
  if (reasoning === "none") {
1723
2207
  return {
1724
2208
  thinking: { type: "disabled" }
@@ -1726,22 +2210,173 @@ function reasoningRequest(request, definition, options, max) {
1726
2210
  }
1727
2211
  if (typeof reasoning === "string") {
1728
2212
  if (reasoning === "minimal" || options.thinkingMode === "manual")
1729
- throw new UnsupportedModelFeatureError4(`${PREFIX} Named efforts require an adaptive-thinking Claude offering.`);
2213
+ throw new UnsupportedModelFeatureError6(`${PREFIX} Named efforts require an adaptive-thinking Claude offering.`);
1730
2214
  return { thinking: { type: "adaptive" }, effort: reasoning };
1731
2215
  }
1732
2216
  if (options.thinkingMode === "adaptive")
1733
- throw new UnsupportedModelFeatureError4(`${PREFIX} This Claude offering only supports adaptive thinking, not manual budgets.`);
2217
+ throw new UnsupportedModelFeatureError6(`${PREFIX} This Claude offering only supports adaptive thinking, not manual budgets.`);
1734
2218
  if (reasoning.budgetTokens < 1024 || reasoning.budgetTokens >= max)
1735
- throw new UnsupportedModelFeatureError4(`${PREFIX} Messages reasoning budget must be at least 1024 and below maxOutputTokens (${max}).`);
2219
+ throw new UnsupportedModelFeatureError6(`${PREFIX} Messages reasoning budget must be at least 1024 and below maxOutputTokens (${max}).`);
1736
2220
  return { thinking: { type: "enabled", budget_tokens: reasoning.budgetTokens } };
1737
2221
  }
1738
2222
 
2223
+ // src/reranking.ts
2224
+ import {
2225
+ defineModelRateCard as defineModelRateCard6,
2226
+ estimateModelReservation as estimateModelReservation3,
2227
+ ModelProviderError as ModelProviderError5,
2228
+ RerankingModelResponseError,
2229
+ rateModelCall as rateModelCall3
2230
+ } from "@sixb/core/models";
2231
+ var MAX_RESPONSE_BYTES3 = 4 * 1024 * 1024;
2232
+ var MAX_TIMEOUT_MS3 = 2 ** 31 - 1;
2233
+ var PUBLISHED2 = {
2234
+ "cohere-rerank-v5-fast": {
2235
+ GlobalStandard: { currency: "USD", input: "0.02", output: "0" },
2236
+ DataZoneStandard: { currency: "USD", input: "0.022", output: "0" }
2237
+ },
2238
+ "cohere-rerank-v5-pro": {
2239
+ GlobalStandard: { currency: "USD", input: "0.05", output: "0" },
2240
+ DataZoneStandard: { currency: "USD", input: "0.055", output: "0" }
2241
+ }
2242
+ };
2243
+ function createFoundryReranking(providerId, modelId, options, transport, deployments) {
2244
+ if (!modelId.trim() || modelId !== modelId.trim())
2245
+ throw new TypeError(`${PREFIX} deploymentName must be nonempty without surrounding whitespace.`);
2246
+ positiveInteger(options.timeoutMs, "timeoutMs");
2247
+ if (options.timeoutMs !== undefined && options.timeoutMs > MAX_TIMEOUT_MS3)
2248
+ throw new TypeError(`${PREFIX} timeoutMs must be at most ${MAX_TIMEOUT_MS3}.`);
2249
+ if (options.rateCard && options.costEstimator)
2250
+ throw new TypeError(`${PREFIX} Configure either rateCard or costEstimator, not both.`);
2251
+ const settings = Object.freeze({
2252
+ timeoutMs: options.timeoutMs ?? 30000,
2253
+ ...options.rateCard ? { rateCard: defineModelRateCard6(options.rateCard) } : {},
2254
+ ...options.costEstimator ? { costEstimator: options.costEstimator } : {}
2255
+ });
2256
+ return rerankingBinding(providerId, modelId, settings, transport, deployments);
2257
+ }
2258
+ function rerankingBinding(providerId, modelId, settings, transport, deployments, resolution) {
2259
+ const deployment = resolution?.deployment;
2260
+ const definition = Object.freeze({
2261
+ kind: "reranking",
2262
+ providerId,
2263
+ modelId,
2264
+ ...deployment ? { publisher: azurePublisher(deployment.modelPublisher) } : {},
2265
+ via: "Azure AI Foundry"
2266
+ });
2267
+ let pending;
2268
+ const binding = Object.freeze({
2269
+ providerId,
2270
+ modelId,
2271
+ definition,
2272
+ metadata: deploymentMetadata(resolution),
2273
+ ...resolution?.costEstimator ? { costEstimator: resolution.costEstimator } : {},
2274
+ async resolve(input) {
2275
+ if (resolution)
2276
+ return binding;
2277
+ const resolved = await deployments.resolve(modelId, input?.offline === true);
2278
+ assertDeploymentKind(resolved.deployment, modelId, "reranking");
2279
+ const rateCard = settings.rateCard ?? publishedRateCard(resolved.deployment);
2280
+ const costEstimator = settings.costEstimator ?? (rateCard ? rerankingEstimator(rateCard) : undefined);
2281
+ return rerankingBinding(providerId, modelId, settings, transport, deployments, {
2282
+ ...resolved,
2283
+ ...costEstimator ? { costEstimator } : {}
2284
+ });
2285
+ },
2286
+ async rerank(request) {
2287
+ const signal = AbortSignal.any([
2288
+ ...request.signal ? [request.signal] : [],
2289
+ AbortSignal.timeout(settings.timeoutMs)
2290
+ ]);
2291
+ signal.throwIfAborted();
2292
+ if (typeof request.query !== "string" || !request.query.trim() || !Array.isArray(request.documents) || request.documents.some((text) => typeof text !== "string" || !text.trim()))
2293
+ throw new TypeError(`${PREFIX} Reranking requires a query and nonempty document strings.`);
2294
+ const documents = [...request.documents];
2295
+ if (!documents.length)
2296
+ return { results: [] };
2297
+ if (!resolution) {
2298
+ pending ??= binding.resolve().catch((error) => {
2299
+ pending = undefined;
2300
+ throw error;
2301
+ });
2302
+ const resolved = await abortable(() => pending, signal);
2303
+ return resolved.rerank({ query: request.query, documents, signal });
2304
+ }
2305
+ const response = await transport.post(JSON.stringify({ model: modelId, query: request.query, documents }), signal, providerId, modelId, "reranking");
2306
+ const id = transport.redactText(response, requestId(response));
2307
+ let body;
2308
+ try {
2309
+ body = await readJson(response, signal, MAX_RESPONSE_BYTES3);
2310
+ } catch {
2311
+ if (signal.aborted)
2312
+ throw signal.reason;
2313
+ throw new ModelProviderError5(`${PREFIX} Reranking returned invalid JSON.`, providerId, modelId, { code: "invalid_json", requestId: id, status: response.status });
2314
+ }
2315
+ const payload = object(body);
2316
+ const responseId = typeof payload?.id === "string" && payload.id ? payload.id : undefined;
2317
+ const metadata = {
2318
+ usage: rerankingUsage(payload),
2319
+ ...id || responseId ? {
2320
+ providerIds: {
2321
+ ...id ? { requestId: id } : {},
2322
+ ...responseId ? { responseId } : {}
2323
+ }
2324
+ } : {}
2325
+ };
2326
+ const results = rerankingResults(payload?.results, documents.length);
2327
+ if (!results)
2328
+ throw new RerankingModelResponseError(`${PREFIX} Invalid reranking response: expected one finite score per document with unique indices.`, providerId, modelId, metadata);
2329
+ return { ...metadata, results };
2330
+ }
2331
+ });
2332
+ return binding;
2333
+ }
2334
+ function rerankingResults(value, count) {
2335
+ if (!Array.isArray(value) || value.length !== count)
2336
+ return;
2337
+ const seen = new Set;
2338
+ const results = [];
2339
+ for (const entry of value) {
2340
+ const index = object(entry)?.index;
2341
+ const score = object(entry)?.relevance_score;
2342
+ if (typeof index !== "number" || !Number.isSafeInteger(index) || index < 0 || index >= count || seen.has(index) || typeof score !== "number" || !Number.isFinite(score))
2343
+ return;
2344
+ seen.add(index);
2345
+ results.push({ index, score });
2346
+ }
2347
+ return results;
2348
+ }
2349
+ function rerankingUsage(payload) {
2350
+ const billed = object(object(payload?.meta)?.billed_units);
2351
+ const inputTokens = counter(billed?.input_tokens);
2352
+ return {
2353
+ ...inputTokens === undefined ? {} : { inputTokens, outputTokens: 0 },
2354
+ ...billed ? { raw: billed } : {}
2355
+ };
2356
+ }
2357
+ function publishedRateCard(deployment) {
2358
+ const skus = PUBLISHED2[deployment.modelName.toLowerCase()];
2359
+ const card = skus && deployment.modelVersion === "1" && Object.hasOwn(skus, deployment.sku.name) ? skus[deployment.sku.name] : undefined;
2360
+ return card ? defineModelRateCard6(card) : undefined;
2361
+ }
2362
+ function rerankingEstimator(rateCard) {
2363
+ return {
2364
+ estimateReservation: (tokens) => estimateModelReservation3({ ...tokens, rateCard }),
2365
+ estimate: ({ usage }) => {
2366
+ const raw = usage.raw ?? {};
2367
+ if (Object.entries(raw).some(([key, count]) => key !== "input_tokens" && count !== 0))
2368
+ return { status: "unpriceable", reason: "missing-rate-card" };
2369
+ return rateModelCall3({ usage, rateCard });
2370
+ }
2371
+ };
2372
+ }
2373
+
1739
2374
  // src/resolution.ts
1740
2375
  import {
1741
2376
  defineLanguageModel as defineLanguageModel2,
1742
2377
  defineLanguageModelLabel,
1743
- defineModelRateCard as defineModelRateCard4,
1744
- UnsupportedModelFeatureError as UnsupportedModelFeatureError5
2378
+ defineModelRateCard as defineModelRateCard7,
2379
+ UnsupportedModelFeatureError as UnsupportedModelFeatureError7
1745
2380
  } from "@sixb/core/models";
1746
2381
  function resolveModel(input) {
1747
2382
  const { deployment, options } = input;
@@ -1758,10 +2393,10 @@ function resolveModel(input) {
1758
2393
  const publisher = deploymentPublisher?.id === "fireworks-ai" ? entry?.definition.publisher ?? deploymentPublisher : deploymentPublisher ?? entry?.definition.publisher;
1759
2394
  const protocol = input.protocol ?? entry?.protocol ?? (deployment?.capabilities.responses === "true" ? "responses" : deployment?.capabilities.chatCompletion === "true" || deployment?.capabilities.chat_completion === "true" ? "chat" : deployment?.capabilities.messages === "true" ? "messages" : "responses");
1760
2395
  if (protocol === "messages" && deployment?.connectionName)
1761
- throw new UnsupportedModelFeatureError5(`${PREFIX} Native Messages cannot route a deployment from a project connection; use the owning resource's project URL and API key.`);
2396
+ throw new UnsupportedModelFeatureError7(`${PREFIX} Native Messages cannot route a deployment from a project connection; use the owning resource's project URL and API key.`);
1762
2397
  const flag = deployment?.capabilities[protocol === "chat" ? "chatCompletion" : protocol] ?? (protocol === "chat" ? deployment?.capabilities.chat_completion : undefined);
1763
2398
  if (flag === "false")
1764
- throw new UnsupportedModelFeatureError5(`${PREFIX} Deployment '${input.modelId}' reports that ${protocol === "chat" ? "Chat" : protocol === "responses" ? "Responses" : "Messages"} is unsupported.`);
2399
+ throw new UnsupportedModelFeatureError7(`${PREFIX} Deployment '${input.modelId}' reports that ${protocol === "chat" ? "Chat" : protocol === "responses" ? "Responses" : "Messages"} is unsupported.`);
1765
2400
  const definition = defineLanguageModel2({
1766
2401
  ...entry?.definition,
1767
2402
  publisher,
@@ -1799,7 +2434,7 @@ function resolveModel(input) {
1799
2434
  })
1800
2435
  } : {}
1801
2436
  }),
1802
- rateCard: options.rateCard ? defineModelRateCard4(options.rateCard) : entry?.rateCard,
2437
+ rateCard: options.rateCard ? defineModelRateCard7(options.rateCard) : entry?.rateCard,
1803
2438
  aliases: Object.freeze([
1804
2439
  ...new Set([
1805
2440
  input.modelId,
@@ -1848,6 +2483,462 @@ function effectiveDefinition(definition, protocol, options) {
1848
2483
  });
1849
2484
  }
1850
2485
 
2486
+ // src/speech.ts
2487
+ import {
2488
+ defineModelRateCard as defineModelRateCard8,
2489
+ rateModelCall as rateModelCall4,
2490
+ SpeechModelResponseError,
2491
+ UnsupportedModelFeatureError as UnsupportedModelFeatureError8
2492
+ } from "@sixb/core/models";
2493
+ var FORMATS2 = {
2494
+ "audio/mpeg": "mp3",
2495
+ "audio/ogg": "opus",
2496
+ "audio/aac": "aac",
2497
+ "audio/flac": "flac",
2498
+ "audio/wav": "wav"
2499
+ };
2500
+ var OUTPUT_MEDIA_TYPES = Object.freeze(Object.keys(FORMATS2));
2501
+ var MAX_INPUT_CHARACTERS = 4096;
2502
+ var MAX_AUDIO_BYTES = 128 * 1024 * 1024;
2503
+ var MAX_EVENT_BYTES = 16 * 1024 * 1024;
2504
+ var MAX_TIMEOUT_MS4 = 2 ** 31 - 1;
2505
+ var PUBLISHED3 = {
2506
+ "2025-03-20": {
2507
+ GlobalStandard: { currency: "USD", input: "0.6", output: "12" },
2508
+ DataZoneStandard: { currency: "USD", input: "0.66", output: "13.2" },
2509
+ Standard: { currency: "USD", input: "0.66", output: "13.2" }
2510
+ },
2511
+ "2025-12-15": {
2512
+ GlobalStandard: { currency: "USD", input: "0.6", output: "12" },
2513
+ DataZoneStandard: { currency: "USD", input: "0.66", output: "13.2" }
2514
+ }
2515
+ };
2516
+ function createFoundrySpeech(providerId, modelId, options, transport, deployments) {
2517
+ if (!modelId.trim() || modelId !== modelId.trim())
2518
+ throw new TypeError(`${PREFIX} deploymentName must be nonempty without surrounding whitespace.`);
2519
+ positiveInteger(options.timeoutMs, "timeoutMs");
2520
+ if (options.timeoutMs !== undefined && options.timeoutMs > MAX_TIMEOUT_MS4)
2521
+ throw new TypeError(`${PREFIX} timeoutMs must be at most ${MAX_TIMEOUT_MS4}.`);
2522
+ if (options.voice !== undefined && (typeof options.voice !== "string" || !options.voice.trim()))
2523
+ throw new TypeError(`${PREFIX} voice must be a nonblank string.`);
2524
+ if (options.rateCard && options.costEstimator)
2525
+ throw new TypeError(`${PREFIX} Configure either rateCard or costEstimator, not both.`);
2526
+ const settings = Object.freeze({
2527
+ voice: options.voice ?? "alloy",
2528
+ timeoutMs: options.timeoutMs ?? 120000,
2529
+ ...options.rateCard ? { rateCard: defineModelRateCard8(options.rateCard) } : {},
2530
+ ...options.costEstimator ? { costEstimator: options.costEstimator } : {}
2531
+ });
2532
+ return speechBinding(providerId, modelId, settings, transport, deployments);
2533
+ }
2534
+ function speechBinding(providerId, modelId, settings, transport, deployments, resolution) {
2535
+ const deployment = resolution?.deployment;
2536
+ const definition = Object.freeze({
2537
+ kind: "speech",
2538
+ providerId,
2539
+ modelId,
2540
+ ...deployment ? { publisher: azurePublisher(deployment.modelPublisher) } : {},
2541
+ via: "Azure AI Foundry",
2542
+ maxInputCharacters: MAX_INPUT_CHARACTERS,
2543
+ outputMediaTypes: OUTPUT_MEDIA_TYPES,
2544
+ instructions: true
2545
+ });
2546
+ let pending;
2547
+ const binding = Object.freeze({
2548
+ providerId,
2549
+ modelId,
2550
+ definition,
2551
+ defaultVoice: settings.voice,
2552
+ metadata: deploymentMetadata(resolution),
2553
+ ...resolution?.costEstimator ? { costEstimator: resolution.costEstimator } : {},
2554
+ ...resolution?.rateCard ? { rateCard: resolution.rateCard } : {},
2555
+ async resolve(input) {
2556
+ if (resolution)
2557
+ return binding;
2558
+ const resolved = await deployments.resolve(modelId, input?.offline === true);
2559
+ assertDeploymentKind(resolved.deployment, modelId, "speech");
2560
+ if (!/^gpt-4o-mini-tts$/i.test(resolved.deployment.modelName))
2561
+ throw new UnsupportedModelFeatureError8(`${PREFIX} ${resolved.deployment.modelName} is not supported: deploy gpt-4o-mini-tts, which reports its usage.`);
2562
+ const rateCard = settings.costEstimator ? undefined : settings.rateCard ?? publishedRateCard2(resolved.deployment);
2563
+ return speechBinding(providerId, modelId, settings, transport, deployments, {
2564
+ ...resolved,
2565
+ ...rateCard ? { rateCard } : {},
2566
+ ...settings.costEstimator || rateCard ? { costEstimator: settings.costEstimator ?? speechEstimator(rateCard) } : {}
2567
+ });
2568
+ },
2569
+ async speak(request) {
2570
+ const signal = AbortSignal.any([
2571
+ ...request.signal ? [request.signal] : [],
2572
+ AbortSignal.timeout(settings.timeoutMs)
2573
+ ]);
2574
+ signal.throwIfAborted();
2575
+ const mediaType = request.mediaType ?? "audio/mpeg";
2576
+ const format = FORMATS2[mediaType];
2577
+ if (!format)
2578
+ throw new TypeError(`${PREFIX} Speech media type '${mediaType}' cannot be requested.`);
2579
+ if (typeof request.text !== "string" || !request.text.trim() || request.text.length > MAX_INPUT_CHARACTERS)
2580
+ throw new TypeError(`${PREFIX} Speech text must be 1 to ${MAX_INPUT_CHARACTERS} characters.`);
2581
+ if (!resolution) {
2582
+ pending ??= binding.resolve().catch((error) => {
2583
+ pending = undefined;
2584
+ throw error;
2585
+ });
2586
+ const resolved = await abortable(() => pending, signal);
2587
+ return resolved.speak({ ...request, signal });
2588
+ }
2589
+ const response = await transport.post(JSON.stringify({
2590
+ model: modelId,
2591
+ input: request.text,
2592
+ voice: request.voice ?? settings.voice,
2593
+ response_format: format,
2594
+ stream_format: "sse",
2595
+ ...request.instructions === undefined ? {} : { instructions: request.instructions },
2596
+ ...request.speed === undefined ? {} : { speed: request.speed }
2597
+ }), signal, providerId, modelId, "speech");
2598
+ const id = transport.redactText(response, requestId(response));
2599
+ const stream = await readSpeechStream(response, signal);
2600
+ const metadata = {
2601
+ usage: speechUsage(stream.usage),
2602
+ ...id ? { providerIds: { requestId: id } } : {}
2603
+ };
2604
+ const failure = (reason) => new SpeechModelResponseError(`${PREFIX} ${reason}`, providerId, modelId, metadata);
2605
+ if (stream.error !== undefined)
2606
+ throw failure(`Speech generation failed: ${transport.redactText(response, stream.error) ?? stream.error}`);
2607
+ if (stream.problem)
2608
+ throw failure(`Speech generation ${stream.problem}.`);
2609
+ const audio = wavLengths(stream.audio, format);
2610
+ if (!hasSignature(audio, format))
2611
+ throw failure(`Speech generation returned no ${format} audio.`);
2612
+ const warnings = request.language === undefined ? [] : ["language: not supported; the model reads the text in its own language"];
2613
+ return {
2614
+ ...metadata,
2615
+ output: { audio: { data: audio, mediaType } },
2616
+ ...warnings.length ? { warnings } : {}
2617
+ };
2618
+ }
2619
+ });
2620
+ return binding;
2621
+ }
2622
+ async function readSpeechStream(response, signal) {
2623
+ const parts = [];
2624
+ let bytes = 0;
2625
+ let usage;
2626
+ let done = false;
2627
+ let error;
2628
+ let problem;
2629
+ let data = [];
2630
+ let pending = "";
2631
+ const dispatch = () => {
2632
+ const text = data.join(`
2633
+ `);
2634
+ data = [];
2635
+ if (!text || text === "[DONE]")
2636
+ return;
2637
+ let event;
2638
+ try {
2639
+ event = object(JSON.parse(text));
2640
+ } catch {
2641
+ problem = "returned a malformed event";
2642
+ return;
2643
+ }
2644
+ if (event?.type === "speech.audio.delta" && typeof event.audio === "string" && !done) {
2645
+ const chunk = Buffer.from(event.audio, "base64");
2646
+ bytes += chunk.length;
2647
+ if (bytes > MAX_AUDIO_BYTES)
2648
+ problem = `exceeded ${MAX_AUDIO_BYTES} bytes of audio`;
2649
+ else
2650
+ parts.push(chunk);
2651
+ } else if (event?.type === "speech.audio.done") {
2652
+ done = true;
2653
+ usage = object(event.usage);
2654
+ } else if (event?.type === "error") {
2655
+ const detail = object(event.error);
2656
+ error = typeof detail?.message === "string" ? detail.message : "error event";
2657
+ }
2658
+ };
2659
+ const reader = response.body?.getReader();
2660
+ const decoder = new TextDecoder;
2661
+ try {
2662
+ while (reader && !problem && error === undefined) {
2663
+ const chunk = await abortable(() => reader.read(), signal).catch(() => {
2664
+ return;
2665
+ });
2666
+ if (!chunk) {
2667
+ if (signal.aborted)
2668
+ throw signal.reason;
2669
+ problem = "stream failed before it finished";
2670
+ break;
2671
+ }
2672
+ pending += decoder.decode(chunk.value, { stream: !chunk.done });
2673
+ const lines = pending.split(/\r?\n/);
2674
+ pending = chunk.done ? "" : lines.pop();
2675
+ if (pending.length > MAX_EVENT_BYTES)
2676
+ problem = "returned an oversized event";
2677
+ for (const line of lines) {
2678
+ if (line === "")
2679
+ dispatch();
2680
+ else if (line.startsWith("data:"))
2681
+ data.push(line.slice(line.startsWith("data: ") ? 6 : 5));
2682
+ if (problem || error !== undefined)
2683
+ break;
2684
+ }
2685
+ if (chunk.done) {
2686
+ dispatch();
2687
+ break;
2688
+ }
2689
+ }
2690
+ } finally {
2691
+ reader?.cancel().catch(() => {});
2692
+ reader?.releaseLock();
2693
+ }
2694
+ return {
2695
+ audio: new Uint8Array(Buffer.concat(parts)),
2696
+ ...usage ? { usage } : {},
2697
+ ...error === undefined ? {} : { error },
2698
+ ...problem ? { problem } : error === undefined && !done ? { problem: "stream ended before speech.audio.done" } : {}
2699
+ };
2700
+ }
2701
+ function speechUsage(raw) {
2702
+ const inputTokens = counter(raw?.input_tokens);
2703
+ const outputTokens = counter(raw?.output_tokens);
2704
+ return {
2705
+ ...inputTokens === undefined ? {} : { inputTokens },
2706
+ ...outputTokens === undefined ? {} : { outputTokens },
2707
+ ...raw ? { raw } : {}
2708
+ };
2709
+ }
2710
+ function wavLengths(audio, format) {
2711
+ if (format !== "wav" || audio.length < 44 || ascii(audio, 36) !== "data")
2712
+ return audio;
2713
+ const view = new DataView(audio.buffer, audio.byteOffset, audio.byteLength);
2714
+ if (view.getUint32(4, true) === 4294967295)
2715
+ view.setUint32(4, audio.length - 8, true);
2716
+ if (view.getUint32(40, true) === 4294967295)
2717
+ view.setUint32(40, audio.length - 44, true);
2718
+ return audio;
2719
+ }
2720
+ function hasSignature(audio, format) {
2721
+ if (format === "wav")
2722
+ return ascii(audio, 0) === "RIFF" && ascii(audio, 8) === "WAVE";
2723
+ if (format === "opus")
2724
+ return ascii(audio, 0) === "OggS";
2725
+ if (format === "flac")
2726
+ return ascii(audio, 0) === "fLaC";
2727
+ const sync = audio.length >= 2 && audio[0] === 255 && (audio[1] & 224) === 224;
2728
+ const layer = audio.length >= 2 ? audio[1] & 6 : -1;
2729
+ if (format === "aac")
2730
+ return sync && layer === 0;
2731
+ return ascii(audio, 0).startsWith("ID3") || sync && layer !== 0;
2732
+ }
2733
+ function ascii(bytes, offset) {
2734
+ return String.fromCharCode(...bytes.subarray(offset, offset + 4));
2735
+ }
2736
+ function publishedRateCard2(deployment) {
2737
+ const skus = /^gpt-4o-mini-tts$/i.test(deployment.modelName) && Object.hasOwn(PUBLISHED3, deployment.modelVersion) ? PUBLISHED3[deployment.modelVersion] : undefined;
2738
+ const card = skus && Object.hasOwn(skus, deployment.sku.name) ? skus[deployment.sku.name] : undefined;
2739
+ return card ? defineModelRateCard8(card) : undefined;
2740
+ }
2741
+ function speechEstimator(rateCard) {
2742
+ return { estimate: ({ usage }) => rateModelCall4({ usage, rateCard }) };
2743
+ }
2744
+
2745
+ // src/transcription.ts
2746
+ import {
2747
+ defineModelRateCard as defineModelRateCard9,
2748
+ ModelProviderError as ModelProviderError6,
2749
+ rateModelCall as rateModelCall5,
2750
+ TranscriptionModelResponseError
2751
+ } from "@sixb/core/models";
2752
+ var MAX_INPUT_BYTES = 25 * 1024 * 1024;
2753
+ var EXTENSIONS2 = {
2754
+ "audio/flac": "flac",
2755
+ "audio/x-flac": "flac",
2756
+ "audio/mpeg": "mp3",
2757
+ "audio/mp3": "mp3",
2758
+ "audio/mp4": "m4a",
2759
+ "audio/m4a": "m4a",
2760
+ "audio/x-m4a": "m4a",
2761
+ "audio/ogg": "ogg",
2762
+ "audio/wav": "wav",
2763
+ "audio/wave": "wav",
2764
+ "audio/x-wav": "wav",
2765
+ "audio/webm": "webm"
2766
+ };
2767
+ var MEDIA_TYPES = Object.freeze(Object.keys(EXTENSIONS2));
2768
+ var MAX_RESPONSE_BYTES4 = 8 * 1024 * 1024;
2769
+ var MAX_TIMEOUT_MS5 = 2 ** 31 - 1;
2770
+ var PUBLISHED4 = [
2771
+ {
2772
+ model: "gpt-4o-transcribe",
2773
+ versions: ["2025-03-20"],
2774
+ skus: {
2775
+ GlobalStandard: { currency: "USD", input: "6", output: "10" },
2776
+ DataZoneStandard: { currency: "USD", input: "6.6", output: "11" },
2777
+ Standard: { currency: "USD", input: "6.6", output: "11" }
2778
+ }
2779
+ },
2780
+ {
2781
+ model: "gpt-4o-mini-transcribe",
2782
+ versions: ["2025-03-20", "2025-12-15"],
2783
+ skus: {
2784
+ GlobalStandard: { currency: "USD", input: "3", output: "5" },
2785
+ DataZoneStandard: { currency: "USD", input: "3.3", output: "5.5" },
2786
+ Standard: { currency: "USD", input: "3.3", output: "5.5" }
2787
+ }
2788
+ },
2789
+ {
2790
+ model: "gpt-transcribe",
2791
+ versions: ["2026-07-28"],
2792
+ skus: {
2793
+ GlobalStandard: { currency: "USD", audioInputSeconds: "0.000075" },
2794
+ DataZoneStandard: { currency: "USD", audioInputSeconds: "0.0000825" }
2795
+ }
2796
+ },
2797
+ {
2798
+ model: "whisper",
2799
+ versions: ["001"],
2800
+ skus: {
2801
+ GlobalStandard: { currency: "USD", audioInputSeconds: "0.0001" },
2802
+ Standard: { currency: "USD", audioInputSeconds: "0.00011" }
2803
+ }
2804
+ }
2805
+ ];
2806
+ function createFoundryTranscription(providerId, modelId, options, transport, deployments) {
2807
+ if (!modelId.trim() || modelId !== modelId.trim())
2808
+ throw new TypeError(`${PREFIX} deploymentName must be nonempty without surrounding whitespace.`);
2809
+ positiveInteger(options.timeoutMs, "timeoutMs");
2810
+ if (options.timeoutMs !== undefined && options.timeoutMs > MAX_TIMEOUT_MS5)
2811
+ throw new TypeError(`${PREFIX} timeoutMs must be at most ${MAX_TIMEOUT_MS5}.`);
2812
+ if (options.language !== undefined && !/^[a-z]{2,3}$/.test(options.language))
2813
+ throw new TypeError(`${PREFIX} language must be an ISO-639 code, such as 'en'.`);
2814
+ if (options.rateCard && options.costEstimator)
2815
+ throw new TypeError(`${PREFIX} Configure either rateCard or costEstimator, not both.`);
2816
+ const settings = Object.freeze({
2817
+ timeoutMs: options.timeoutMs ?? 120000,
2818
+ ...options.language ? { language: options.language } : {},
2819
+ ...options.rateCard ? { rateCard: defineModelRateCard9(options.rateCard) } : {},
2820
+ ...options.costEstimator ? { costEstimator: options.costEstimator } : {}
2821
+ });
2822
+ return transcriptionBinding(providerId, modelId, settings, transport, deployments);
2823
+ }
2824
+ function transcriptionBinding(providerId, modelId, settings, transport, deployments, resolution) {
2825
+ const deployment = resolution?.deployment;
2826
+ const definition = Object.freeze({
2827
+ kind: "transcription",
2828
+ providerId,
2829
+ modelId,
2830
+ ...deployment ? { publisher: azurePublisher(deployment.modelPublisher) } : {},
2831
+ via: "Azure AI Foundry",
2832
+ maxInputBytes: MAX_INPUT_BYTES,
2833
+ mediaTypes: MEDIA_TYPES
2834
+ });
2835
+ let pending;
2836
+ const binding = Object.freeze({
2837
+ providerId,
2838
+ modelId,
2839
+ definition,
2840
+ metadata: deploymentMetadata(resolution),
2841
+ ...resolution?.costEstimator ? { costEstimator: resolution.costEstimator } : {},
2842
+ ...resolution?.rateCard ? { rateCard: resolution.rateCard } : {},
2843
+ async resolve(input) {
2844
+ if (resolution)
2845
+ return binding;
2846
+ const resolved = await deployments.resolve(modelId, input?.offline === true);
2847
+ const found = resolved.deployment;
2848
+ assertDeploymentKind(found, modelId, "transcription");
2849
+ const rateCard = settings.costEstimator ? undefined : settings.rateCard ?? publishedRateCard3(found);
2850
+ return transcriptionBinding(providerId, modelId, settings, transport, deployments, {
2851
+ ...resolved,
2852
+ verbose: /^whisper$/i.test(found.modelName),
2853
+ chunking: /-diarize$/i.test(found.modelName),
2854
+ ...rateCard ? { rateCard } : {},
2855
+ ...settings.costEstimator || rateCard ? { costEstimator: settings.costEstimator ?? transcriptionEstimator(rateCard) } : {}
2856
+ });
2857
+ },
2858
+ async transcribe(request) {
2859
+ const signal = AbortSignal.any([
2860
+ ...request.signal ? [request.signal] : [],
2861
+ AbortSignal.timeout(settings.timeoutMs)
2862
+ ]);
2863
+ signal.throwIfAborted();
2864
+ const extension = EXTENSIONS2[request.mediaType];
2865
+ if (!(request.audio instanceof Uint8Array) || request.audio.byteLength === 0 || request.audio.byteLength > MAX_INPUT_BYTES)
2866
+ throw new TypeError(`${PREFIX} Transcription audio must be 1 byte to 25 MiB.`);
2867
+ if (!extension)
2868
+ throw new TypeError(`${PREFIX} Transcription media type '${request.mediaType}' is not supported.`);
2869
+ if (!resolution) {
2870
+ pending ??= binding.resolve().catch((error) => {
2871
+ pending = undefined;
2872
+ throw error;
2873
+ });
2874
+ const resolved = await abortable(() => pending, signal);
2875
+ return resolved.transcribe({ ...request, signal });
2876
+ }
2877
+ const form = new FormData;
2878
+ const file = new Blob([new Uint8Array(request.audio)], { type: request.mediaType });
2879
+ form.set("file", file, `audio.${extension}`);
2880
+ form.set("response_format", resolution.verbose ? "verbose_json" : "json");
2881
+ if (resolution.chunking)
2882
+ form.set("chunking_strategy", "auto");
2883
+ if (settings.language)
2884
+ form.set("language", settings.language);
2885
+ const response = await transport.post(form, signal, providerId, modelId, "transcription");
2886
+ const id = transport.redactText(response, requestId(response));
2887
+ let body;
2888
+ try {
2889
+ body = await readJson(response, signal, MAX_RESPONSE_BYTES4);
2890
+ } catch {
2891
+ if (signal.aborted)
2892
+ throw signal.reason;
2893
+ throw new ModelProviderError6(`${PREFIX} Transcription returned invalid JSON.`, providerId, modelId, { code: "invalid_json", requestId: id, status: response.status });
2894
+ }
2895
+ const payload = object(body);
2896
+ const metadata = {
2897
+ usage: transcriptionUsage(payload),
2898
+ ...id ? { providerIds: { requestId: id } } : {}
2899
+ };
2900
+ if (typeof payload?.text !== "string")
2901
+ throw new TranscriptionModelResponseError(`${PREFIX} Transcription response is missing its text.`, providerId, modelId, metadata);
2902
+ return { ...metadata, output: { text: payload.text } };
2903
+ }
2904
+ });
2905
+ return binding;
2906
+ }
2907
+ function transcriptionUsage(payload) {
2908
+ const usage = object(payload?.usage);
2909
+ if (usage?.type === "tokens") {
2910
+ const inputTokens = counter(usage.input_tokens);
2911
+ const outputTokens = counter(usage.output_tokens);
2912
+ return {
2913
+ ...inputTokens === undefined ? {} : { inputTokens },
2914
+ ...outputTokens === undefined ? {} : { outputTokens },
2915
+ raw: usage
2916
+ };
2917
+ }
2918
+ const seconds = usage?.type === "duration" ? usage.seconds : payload?.duration;
2919
+ const audioDurationMs = typeof seconds === "number" && Number.isFinite(seconds) && seconds >= 0 ? Math.round(seconds * 1000) : undefined;
2920
+ return {
2921
+ ...audioDurationMs === undefined ? {} : { audioDurationMs },
2922
+ ...usage ? { raw: usage } : {}
2923
+ };
2924
+ }
2925
+ function publishedRateCard3(deployment) {
2926
+ const name = deployment.modelName.toLowerCase();
2927
+ const entry = PUBLISHED4.find((price) => price.model === name && price.versions.includes(deployment.modelVersion));
2928
+ const card = entry && Object.hasOwn(entry.skus, deployment.sku.name) && entry.skus[deployment.sku.name];
2929
+ return card ? defineModelRateCard9(card) : undefined;
2930
+ }
2931
+ function transcriptionEstimator(rateCard) {
2932
+ return {
2933
+ estimate: ({ usage }) => {
2934
+ const details = object(usage.raw?.input_token_details);
2935
+ if (details?.text_tokens !== undefined && details.text_tokens !== 0)
2936
+ return { status: "unpriceable", reason: "missing-rate-card" };
2937
+ return rateModelCall5({ usage, rateCard });
2938
+ }
2939
+ };
2940
+ }
2941
+
1851
2942
  // src/provider.ts
1852
2943
  function createAzureAIFoundry(options) {
1853
2944
  const transport = new FoundryTransport(options);
@@ -1870,6 +2961,11 @@ function createAzureAIFoundry(options) {
1870
2961
  const responses = (name, options = {}) => model("responses", name, options);
1871
2962
  const messages = (name, options = {}) => model("messages", name, options);
1872
2963
  const chat = (name, options = {}) => model("chat", name, options);
2964
+ const decision = (name, options = {}) => createFoundryDecision(providerId, name, options, transport, deployments);
2965
+ const transcription = (name, options = {}) => createFoundryTranscription(providerId, name, options, transport, deployments);
2966
+ const reranking = (name, options = {}) => createFoundryReranking(providerId, name, options, transport, deployments);
2967
+ const image = (name, options = {}) => createFoundryImage(providerId, name, options, transport, deployments);
2968
+ const speech = (name, options = {}) => createFoundrySpeech(providerId, name, options, transport, deployments);
1873
2969
  const list = async (protocol, refresh = false) => {
1874
2970
  if (refresh)
1875
2971
  modelCatalog.invalidate();
@@ -1878,7 +2974,7 @@ function createAzureAIFoundry(options) {
1878
2974
  const results = [];
1879
2975
  for (const deployment of records) {
1880
2976
  const name = deployment.name;
1881
- if (isEmbeddingDeployment(deployment) || await modelCatalog.getEmbedding(deployment.modelName, true))
2977
+ if (deploymentKind(deployment) !== "language" || await modelCatalog.getEmbedding(deployment.modelName, true))
1882
2978
  continue;
1883
2979
  try {
1884
2980
  const profile = resolveModel({
@@ -1894,7 +2990,7 @@ function createAzureAIFoundry(options) {
1894
2990
  if (supported)
1895
2991
  results.push(profile.definition);
1896
2992
  } catch (error) {
1897
- if (!(error instanceof UnsupportedModelFeatureError6))
2993
+ if (!(error instanceof UnsupportedModelFeatureError9))
1898
2994
  throw error;
1899
2995
  }
1900
2996
  }
@@ -1907,7 +3003,19 @@ function createAzureAIFoundry(options) {
1907
3003
  deployments: () => deployments.list()
1908
3004
  };
1909
3005
  const auto = (name, options = {}) => model(undefined, name, options);
1910
- return Object.assign(auto, { providerId, catalog, responses, messages, chat, embedding });
3006
+ return Object.assign(auto, {
3007
+ providerId,
3008
+ catalog,
3009
+ responses,
3010
+ messages,
3011
+ chat,
3012
+ embedding,
3013
+ decision,
3014
+ transcription,
3015
+ reranking,
3016
+ speech,
3017
+ image
3018
+ });
1911
3019
  }
1912
3020
 
1913
3021
  class FoundryModel {
@@ -1995,8 +3103,9 @@ class FoundryModel {
1995
3103
  if (this.resolution)
1996
3104
  return this;
1997
3105
  const resolution = await this.deployments.resolve(this.modelId, options?.offline === true);
1998
- if (isEmbeddingDeployment(resolution.deployment))
1999
- throw new UnsupportedModelFeatureError6(`${PREFIX} Use foundry.embedding() for an embedding deployment.`);
3106
+ const kind = deploymentKind(resolution.deployment);
3107
+ if (kind !== "language")
3108
+ throw new UnsupportedModelFeatureError9(`${PREFIX} Use ${KIND_BINDINGS[kind]} for ${article(kind)} ${kind} deployment.`);
2000
3109
  let catalogModel;
2001
3110
  try {
2002
3111
  catalogModel = await this.modelCatalog.get(resolution.deployment.modelName, options?.offline);
@@ -2008,7 +3117,7 @@ class FoundryModel {
2008
3117
  throw error;
2009
3118
  }
2010
3119
  if (await this.modelCatalog.getEmbedding(resolution.deployment.modelName, true))
2011
- throw new UnsupportedModelFeatureError6(`${PREFIX} Use foundry.embedding() for an embedding deployment.`);
3120
+ throw new UnsupportedModelFeatureError9(`${PREFIX} Use foundry.embedding() for an embedding deployment.`);
2012
3121
  return new FoundryModel(this.requestedProtocol, this.transport, this.deployments, this.providerId, this.modelId, this.options, this.modelCatalog, {
2013
3122
  ...resolution,
2014
3123
  catalogModel
@@ -2030,7 +3139,7 @@ class FoundryModel {
2030
3139
  const response = this.protocol === "messages" ? await this.postMessages(request) : await this.transport.post(JSON.stringify(this.protocol === "chat" ? foundryChatRequest(request, this.definition, this.options, this.scope) : responsesRequest(request, this.definition, this.options, this.scope, this.resolution?.deployment)), request.signal, this.providerId, this.modelId, this.protocol);
2031
3140
  const id = this.transport.redactText(response, requestId(response));
2032
3141
  if (!response.body)
2033
- throw new ModelProviderError4(`${PREFIX} Provider returned an empty streaming response.`, this.providerId, this.modelId, { requestId: id, status: response.status });
3142
+ throw new ModelProviderError7(`${PREFIX} Provider returned an empty streaming response.`, this.providerId, this.modelId, { requestId: id, status: response.status });
2034
3143
  const streamOptions = {
2035
3144
  providerId: this.providerId,
2036
3145
  modelId: this.modelId,
@@ -2071,7 +3180,7 @@ class FoundryModel {
2071
3180
  try {
2072
3181
  return await post(prepared.body);
2073
3182
  } catch (error) {
2074
- if (prepared.strictTools === undefined || !(error instanceof ModelProviderError4) || !isStrictToolLimitError(error))
3183
+ if (prepared.strictTools === undefined || !(error instanceof ModelProviderError7) || !isStrictToolLimitError(error))
2075
3184
  throw error;
2076
3185
  strictToolRefusals.add(prepared.strictTools);
2077
3186
  return post(foundryMessagesRequest(request, this.definition, this.options, this.scope, true).body);
@@ -2080,7 +3189,7 @@ class FoundryModel {
2080
3189
  async* events(source, response) {
2081
3190
  try {
2082
3191
  for await (const event of source) {
2083
- if (event.type === "error" && event.error instanceof ModelProviderError4) {
3192
+ if (event.type === "error" && event.error instanceof ModelProviderError7) {
2084
3193
  yield { ...event, error: this.transport.redactError(response, event.error) };
2085
3194
  } else if (event.type === "provider-state" && event.providerId === this.providerId) {
2086
3195
  yield { ...event, data: { ...object(event.data), scope: this.scope } };
@@ -2103,11 +3212,8 @@ class FoundryModel {
2103
3212
  }
2104
3213
  }
2105
3214
  }
2106
- function isEmbeddingDeployment(deployment) {
2107
- return deployment.capabilities.embeddings === "true" || deployment.capabilities.embedding === "true" || /^(text-embedding-|cohere-embed)/i.test(deployment.modelName);
2108
- }
2109
3215
  export {
2110
3216
  createAzureAIFoundry
2111
3217
  };
2112
3218
 
2113
- //# debugId=8BAD6E2F429F859E64756E2164756E21
3219
+ //# debugId=9D607CB81AD6E4F564756E2164756E21