@sixb/azure-ai-foundry 0.2.0 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +187 -2
  2. package/dist/chat-request.d.ts.map +1 -1
  3. package/dist/decision.d.ts +18 -0
  4. package/dist/decision.d.ts.map +1 -0
  5. package/dist/discovery.d.ts +3 -0
  6. package/dist/discovery.d.ts.map +1 -1
  7. package/dist/embedding.d.ts +1 -1
  8. package/dist/embedding.d.ts.map +1 -1
  9. package/dist/image.d.ts +20 -0
  10. package/dist/image.d.ts.map +1 -0
  11. package/dist/index.d.ts +5 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +1249 -99
  14. package/dist/index.js.map +18 -12
  15. package/dist/kinds.d.ts +13 -0
  16. package/dist/kinds.d.ts.map +1 -0
  17. package/dist/messages-request.d.ts +14 -1
  18. package/dist/messages-request.d.ts.map +1 -1
  19. package/dist/provider.d.ts +15 -0
  20. package/dist/provider.d.ts.map +1 -1
  21. package/dist/request.d.ts +6 -1
  22. package/dist/request.d.ts.map +1 -1
  23. package/dist/reranking.d.ts +18 -0
  24. package/dist/reranking.d.ts.map +1 -0
  25. package/dist/speech.d.ts +20 -0
  26. package/dist/speech.d.ts.map +1 -0
  27. package/dist/structured-output.d.ts.map +1 -1
  28. package/dist/transcription.d.ts +20 -0
  29. package/dist/transcription.d.ts.map +1 -0
  30. package/dist/transport.d.ts +10 -2
  31. package/dist/transport.d.ts.map +1 -1
  32. package/package.json +3 -3
  33. package/src/chat-request.ts +18 -20
  34. package/src/decision.ts +211 -0
  35. package/src/discovery.ts +18 -0
  36. package/src/embedding.ts +15 -36
  37. package/src/image.ts +457 -0
  38. package/src/index.ts +8 -0
  39. package/src/kinds.ts +100 -0
  40. package/src/messages-request.ts +39 -8
  41. package/src/provider.ts +121 -31
  42. package/src/request.ts +24 -11
  43. package/src/reranking.ts +264 -0
  44. package/src/speech.ts +390 -0
  45. package/src/structured-output.ts +8 -0
  46. package/src/transcription.ts +322 -0
  47. package/src/transport.ts +90 -11
package/dist/index.js CHANGED
@@ -4,11 +4,11 @@ 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
- import { messagesEvents } from "@sixb/model-protocols/messages";
11
+ import { isStrictToolLimitError, messagesEvents } from "@sixb/model-protocols/messages";
12
12
  import { responsesEvents } from "@sixb/model-protocols/responses";
13
13
 
14
14
  // src/accounting.ts
@@ -377,7 +377,7 @@ import {
377
377
  modelReasoningSupportIssue,
378
378
  UnsupportedModelFeatureError
379
379
  } from "@sixb/core/models";
380
- import { responsesInput } from "@sixb/model-protocols/responses";
380
+ import { requiredNullableSchema, responsesInput } from "@sixb/model-protocols/responses";
381
381
 
382
382
  // src/structured-output.ts
383
383
  import { isJsonObject as isJsonObject2 } from "@sixb/core/models";
@@ -393,7 +393,13 @@ var KEYWORDS = new Set([
393
393
  "anyOf",
394
394
  "$ref",
395
395
  "$defs",
396
- "definitions"
396
+ "definitions",
397
+ "minimum",
398
+ "maximum",
399
+ "minLength",
400
+ "maxLength",
401
+ "minItems",
402
+ "maxItems"
397
403
  ]);
398
404
  var TYPES = new Set(["object", "array", "string", "number", "integer", "boolean", "null"]);
399
405
  function foundryOutputSchema(schema) {
@@ -552,13 +558,16 @@ function responsesRequest(request, definition, options, scope, deployment) {
552
558
  }
553
559
  if (summary && (!caps.reasoning || typeof caps.reasoning !== "object"))
554
560
  throw new UnsupportedModelFeatureError(`${PREFIX} Reasoning summaries require a declared reasoning capability.`);
555
- const tools = request.tools.map((tool) => ({
556
- type: "function",
557
- name: tool.name,
558
- description: tool.description,
559
- parameters: tool.inputSchema,
560
- strict: caps.nativeStructuredOutput === true && foundryOutputSchema(tool.inputSchema) !== undefined
561
- }));
561
+ const tools = request.tools.map((tool) => {
562
+ const schema = caps.nativeStructuredOutput === true ? foundryStrictTool(tool) : undefined;
563
+ return {
564
+ type: "function",
565
+ name: tool.name,
566
+ description: tool.description,
567
+ parameters: schema ?? tool.inputSchema,
568
+ strict: schema !== undefined
569
+ };
570
+ });
562
571
  return {
563
572
  ...options.request,
564
573
  model: definition.modelId,
@@ -570,7 +579,7 @@ function responsesRequest(request, definition, options, scope, deployment) {
570
579
  ...tools.length ? {
571
580
  tools,
572
581
  tool_choice: "auto",
573
- parallel_tool_calls: !schema && !tools.some((t) => t.strict) && caps.parallelToolCalls === true
582
+ parallel_tool_calls: !schema && caps.parallelToolCalls === true
574
583
  } : {},
575
584
  ...reasoning !== undefined && reasoning !== "provider-default" || summary ? {
576
585
  reasoning: {
@@ -646,6 +655,12 @@ function checkScope(data, scope) {
646
655
  throw new UnsupportedModelFeatureError(`${PREFIX} Replay state belongs to a different endpoint, deployment, or protocol.`);
647
656
  }
648
657
  }
658
+ function foundryStrictTool(tool) {
659
+ if (!tool.strict)
660
+ return;
661
+ const schema = requiredNullableSchema(tool.inputSchema);
662
+ return foundryOutputSchema(schema) === undefined ? undefined : schema;
663
+ }
649
664
 
650
665
  // src/chat-request.ts
651
666
  var RESERVED2 = new Set([
@@ -705,17 +720,18 @@ function foundryChatRequest(request, definition, options, scope) {
705
720
  const schema = request.responseFormat && foundryOutputSchema(request.responseFormat.schema);
706
721
  if (request.responseFormat && (profile === "deepseek" || caps.nativeStructuredOutput !== true || !schema))
707
722
  throw new UnsupportedModelFeatureError2(`${PREFIX} Chat structured output requires verified OpenAI-compatible strict-schema support. DeepSeek JSON mode is not strict schema decoding.`);
708
- const tools = request.tools.map((tool) => ({
709
- type: "function",
710
- function: {
711
- name: tool.name,
712
- description: tool.description,
713
- parameters: tool.inputSchema,
714
- ...profile === "openai" ? {
715
- strict: caps.nativeStructuredOutput === true && foundryOutputSchema(tool.inputSchema) !== undefined
716
- } : {}
717
- }
718
- }));
723
+ const tools = request.tools.map((tool) => {
724
+ const strict = profile === "openai" && caps.nativeStructuredOutput === true ? foundryStrictTool(tool) : undefined;
725
+ return {
726
+ type: "function",
727
+ function: {
728
+ name: tool.name,
729
+ description: tool.description,
730
+ parameters: strict ?? tool.inputSchema,
731
+ ...profile === "openai" ? { strict: strict !== undefined } : {}
732
+ }
733
+ };
734
+ });
719
735
  return {
720
736
  ...options.request,
721
737
  model: definition.modelId,
@@ -733,7 +749,7 @@ function foundryChatRequest(request, definition, options, scope) {
733
749
  tools,
734
750
  tool_choice: "auto",
735
751
  ...profile === "openai" ? {
736
- parallel_tool_calls: !schema && !tools.some((tool) => tool.function.strict) && caps.parallelToolCalls === true
752
+ parallel_tool_calls: !schema && caps.parallelToolCalls === true
737
753
  } : {}
738
754
  } : {},
739
755
  ...schema && request.responseFormat ? {
@@ -750,6 +766,13 @@ function foundryChatRequest(request, definition, options, scope) {
750
766
  };
751
767
  }
752
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
+
753
776
  // src/discovery.ts
754
777
  import {
755
778
  ModelCatalogUnavailableError as ModelCatalogUnavailableError2,
@@ -792,6 +815,18 @@ class RequestDiagnostics {
792
815
  // src/transport.ts
793
816
  import { ModelProviderError as ModelProviderError2 } from "@sixb/core/models";
794
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
+
795
830
  class FoundryTransport {
796
831
  diagnostics = new WeakMap;
797
832
  baseUrl;
@@ -823,13 +858,16 @@ class FoundryTransport {
823
858
  };
824
859
  }
825
860
  async post(body, signal, providerId, modelId, protocol = "responses") {
826
- const url = this.url(protocol);
861
+ const url = this.url(protocol, modelId);
827
862
  const diagnostics = new RequestDiagnostics;
828
863
  for (let attempt = 0;; attempt++) {
829
864
  signal.throwIfAborted();
830
865
  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");
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");
833
871
  if (protocol === "messages")
834
872
  headers.set("anthropic-version", "2023-06-01");
835
873
  signal.throwIfAborted();
@@ -857,7 +895,7 @@ class FoundryTransport {
857
895
  if (response.ok)
858
896
  return response;
859
897
  const error = this.redactError(response, await httpError(response, providerId, modelId, signal));
860
- if (protocol === "embeddings" || !error.retryable || attempt >= (this.options.maxRetries ?? 2))
898
+ if (ONE_SHOT.has(protocol) || !error.retryable || attempt >= (this.options.maxRetries ?? 2))
861
899
  throw error;
862
900
  await wait(Math.min(error.retryAfterMs ?? 250 * 2 ** attempt, this.options.maxRetryDelayMs ?? 60000), signal);
863
901
  }
@@ -888,9 +926,19 @@ class FoundryTransport {
888
926
  fetch(url, init) {
889
927
  return (this.options.fetch ?? fetch)(url, init);
890
928
  }
891
- url(protocol) {
929
+ url(protocol, deployment) {
892
930
  if (protocol === "embeddings")
893
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`;
894
942
  if (protocol === "responses")
895
943
  return `${this.baseUrl}/responses`;
896
944
  if (protocol === "chat")
@@ -898,6 +946,31 @@ class FoundryTransport {
898
946
  return `${new URL(this.baseUrl).origin}/anthropic/v1/messages`;
899
947
  }
900
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
+ }
901
974
  function requestId(response) {
902
975
  return response.headers.get("apim-request-id") ?? response.headers.get("x-request-id") ?? response.headers.get("request-id") ?? undefined;
903
976
  }
@@ -944,8 +1017,11 @@ async function httpError(response, providerId, modelId, signal) {
944
1017
  }
945
1018
  const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
946
1019
  const error = object(object(parsed)?.error);
1020
+ const detail = object(parsed)?.detail;
947
1021
  if (typeof error?.message === "string")
948
1022
  message = error.message;
1023
+ else if (typeof detail === "string")
1024
+ message = detail;
949
1025
  if (typeof error?.code === "string")
950
1026
  code = error.code;
951
1027
  else if (typeof error?.type === "string")
@@ -985,6 +1061,18 @@ async function wait(ms, signal) {
985
1061
  }
986
1062
 
987
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
+
988
1076
  class FoundryDeployments {
989
1077
  providerId;
990
1078
  transport;
@@ -1242,12 +1330,167 @@ function deploymentRecord(raw, invalid) {
1242
1330
  });
1243
1331
  }
1244
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
+
1245
1488
  // src/embedding.ts
1246
1489
  import {
1247
- defineModelRateCard as defineModelRateCard3,
1490
+ defineModelRateCard as defineModelRateCard4,
1248
1491
  EmbeddingModelResponseError,
1249
1492
  ModelCatalogUnavailableError as ModelCatalogUnavailableError3,
1250
- UnsupportedModelFeatureError as UnsupportedModelFeatureError3
1493
+ UnsupportedModelFeatureError as UnsupportedModelFeatureError4
1251
1494
  } from "@sixb/core/models";
1252
1495
  function createFoundryEmbedding(providerId, modelId, options, transport, deployments, catalog, resolution) {
1253
1496
  const dimensions = options.dimensions;
@@ -1270,18 +1513,10 @@ function createFoundryEmbedding(providerId, modelId, options, transport, deploym
1270
1513
  const settings = Object.freeze({
1271
1514
  model: representation,
1272
1515
  dimensions,
1273
- rateCard: options.rateCard && defineModelRateCard3(options.rateCard),
1516
+ rateCard: options.rateCard && defineModelRateCard4(options.rateCard),
1274
1517
  costEstimator: options.costEstimator
1275
1518
  });
1276
- const deployment = resolution?.deployment;
1277
- const metadata = Object.freeze(deployment ? {
1278
- deployment,
1279
- modelName: deployment.modelName,
1280
- modelVersion: deployment.modelVersion,
1281
- publisher: deployment.modelPublisher,
1282
- sku: deployment.sku.name,
1283
- discoveredAt: resolution.discoveredAt
1284
- } : {});
1519
+ const metadata = deploymentMetadata(resolution);
1285
1520
  let pending;
1286
1521
  const binding = Object.freeze({
1287
1522
  providerId,
@@ -1354,7 +1589,7 @@ function validateDeployment(resolved, dimensions) {
1354
1589
  const name = deployment.modelName.toLowerCase();
1355
1590
  const maxDimensions = name === "text-embedding-3-small" ? 1536 : name === "text-embedding-3-large" ? 3072 : name === "text-embedding-ada-002" ? 1536 : undefined;
1356
1591
  if (deployment.modelPublisher.toLowerCase() !== "openai" || !maxDimensions || deployment.capabilities.embeddings === "false" || deployment.capabilities.embedding === "false")
1357
- 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.`);
1358
1593
  const configurable = name !== "text-embedding-ada-002";
1359
1594
  if (dimensions > maxDimensions || !configurable && dimensions !== maxDimensions)
1360
1595
  throw new TypeError(`${PREFIX} ${name} requires ${configurable ? `1\u2013${maxDimensions}` : maxDimensions} dimensions.`);
@@ -1385,28 +1620,6 @@ function parseVectors(data, count, dimensions, invalid) {
1385
1620
  function invalidResponse(providerId, modelId, metadata) {
1386
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);
1387
1622
  }
1388
- async function readResponse(response, signal) {
1389
- const reader = response.body?.getReader();
1390
- if (!reader)
1391
- throw new Error("Empty embedding response");
1392
- const chunks = [];
1393
- let bytes = 0;
1394
- try {
1395
- for (;; ) {
1396
- const chunk = await abortable(() => reader.read(), signal);
1397
- if (chunk.done)
1398
- break;
1399
- bytes += chunk.value.length;
1400
- if (bytes > 128 * 1024 * 1024)
1401
- throw new Error("Embedding response exceeds 128 MiB");
1402
- chunks.push(chunk.value);
1403
- }
1404
- return JSON.parse(Buffer.concat(chunks).toString("utf8"));
1405
- } finally {
1406
- reader.cancel().catch(() => {});
1407
- reader.releaseLock();
1408
- }
1409
- }
1410
1623
  function embeddingUsage(raw) {
1411
1624
  const inputTokens = counter(raw?.prompt_tokens);
1412
1625
  const totalTokens = counter(raw?.total_tokens);
@@ -1434,7 +1647,7 @@ async function readEmbeddingResult(response, transport, signal, expected) {
1434
1647
  };
1435
1648
  let body;
1436
1649
  try {
1437
- body = record(await readResponse(response, signal));
1650
+ body = record(await readJson(response, signal, 128 * 1024 * 1024));
1438
1651
  } catch {
1439
1652
  if (signal.aborted)
1440
1653
  throw signal.reason;
@@ -1454,6 +1667,293 @@ async function readEmbeddingResult(response, transport, signal, expected) {
1454
1667
  };
1455
1668
  }
1456
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
+
1457
1957
  // src/messages-accounting.ts
1458
1958
  function foundryMessagesUsage(raw) {
1459
1959
  const uncached = counter(raw.input_tokens);
@@ -1522,9 +2022,14 @@ function tokenMeters2(raw) {
1522
2022
  // src/messages-request.ts
1523
2023
  import {
1524
2024
  modelReasoningSupportIssue as modelReasoningSupportIssue3,
1525
- UnsupportedModelFeatureError as UnsupportedModelFeatureError4
2025
+ UnsupportedModelFeatureError as UnsupportedModelFeatureError6
1526
2026
  } from "@sixb/core/models";
1527
- import { messagesInput } from "@sixb/model-protocols/messages";
2027
+ import {
2028
+ messagesInput,
2029
+ messagesStrictTools,
2030
+ StrictToolRefusals,
2031
+ strictToolsKey
2032
+ } from "@sixb/model-protocols/messages";
1528
2033
 
1529
2034
  // src/messages-schema.ts
1530
2035
  import { isJsonObject as isJsonObject4 } from "@sixb/core/models";
@@ -1602,6 +2107,7 @@ function messagesOutputSchema(schema) {
1602
2107
  }
1603
2108
 
1604
2109
  // src/messages-request.ts
2110
+ var strictToolRefusals = new StrictToolRefusals;
1605
2111
  var RESERVED3 = new Set([
1606
2112
  "model",
1607
2113
  "messages",
@@ -1633,12 +2139,12 @@ function validateMessagesOptions(options) {
1633
2139
  throw new TypeError(`${PREFIX} cache_control must be ephemeral with an optional 5m or 1h ttl.`);
1634
2140
  }
1635
2141
  }
1636
- function foundryMessagesRequest(request, definition, options, scope) {
2142
+ function foundryMessagesRequest(request, definition, options, scope, unconstrained = false) {
1637
2143
  const max = prepareRequest(request, definition, options, scope, true);
1638
2144
  let conversation = false;
1639
2145
  for (const message of request.messages) {
1640
2146
  if (message.role === "system" && conversation)
1641
- throw new UnsupportedModelFeatureError4(`${PREFIX} Messages system instructions must precede the conversation.`);
2147
+ throw new UnsupportedModelFeatureError6(`${PREFIX} Messages system instructions must precede the conversation.`);
1642
2148
  if (message.role !== "system")
1643
2149
  conversation = true;
1644
2150
  }
@@ -1646,19 +2152,29 @@ function foundryMessagesRequest(request, definition, options, scope) {
1646
2152
  throw new TypeError(`${PREFIX} Native Messages requires maxOutputTokens in the definition, binding, or request.`);
1647
2153
  const caps = definition.capabilities;
1648
2154
  if (request.tools.length && caps.localTools !== true)
1649
- throw new UnsupportedModelFeatureError4(`${PREFIX} Configure localTools capability before using Messages tools.`);
2155
+ throw new UnsupportedModelFeatureError6(`${PREFIX} Configure localTools capability before using Messages tools.`);
1650
2156
  const schema = request.responseFormat && messagesOutputSchema(request.responseFormat.schema);
1651
2157
  if (request.responseFormat && (caps.nativeStructuredOutput !== true || !schema))
1652
- 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.`);
1653
2159
  const thinking = reasoningRequest(request, definition, options, max);
1654
2160
  if (thinking.thinking && object(thinking.thinking)?.type !== "disabled" && ["temperature", "top_p", "top_k"].some((key) => options.request?.[key] !== undefined))
1655
- 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.`);
1656
2162
  const mapped = messagesInput(request.messages, definition.providerId, PREFIX);
1657
2163
  const output = {
1658
2164
  ...thinking.effort === undefined ? {} : { effort: thinking.effort },
1659
2165
  ...schema ? { format: { type: "json_schema", schema } } : {}
1660
2166
  };
1661
- return {
2167
+ const candidates = request.tools.map((tool) => ({
2168
+ strict: tool.strict === true && caps.nativeStructuredOutput === true,
2169
+ schema: messagesOutputSchema(tool.inputSchema)
2170
+ }));
2171
+ const chosen = messagesStrictTools(candidates, schema);
2172
+ const constrained = request.tools.flatMap((tool, index) => chosen[index] ? [{ name: tool.name, input_schema: tool.inputSchema }] : []);
2173
+ const key = constrained.length === 0 ? undefined : strictToolsKey(constrained);
2174
+ const refused = unconstrained || key !== undefined && strictToolRefusals.has(key);
2175
+ const strict = chosen.map((choice) => choice && !refused);
2176
+ const strictTools = refused ? undefined : key;
2177
+ const body = {
1662
2178
  ...options.request,
1663
2179
  model: definition.modelId,
1664
2180
  messages: mapped.messages,
@@ -1668,15 +2184,16 @@ function foundryMessagesRequest(request, definition, options, scope) {
1668
2184
  ...thinking.thinking ? { thinking: thinking.thinking } : {},
1669
2185
  ...Object.keys(output).length ? { output_config: output } : {},
1670
2186
  ...request.tools.length ? {
1671
- tools: request.tools.map((tool) => ({
2187
+ tools: request.tools.map((tool, index) => ({
1672
2188
  name: tool.name,
1673
2189
  description: tool.description,
1674
2190
  input_schema: tool.inputSchema,
1675
- ...caps.nativeStructuredOutput === true && messagesOutputSchema(tool.inputSchema) ? { strict: true } : {}
2191
+ ...strict[index] ? { strict: true } : {}
1676
2192
  })),
1677
2193
  tool_choice: { type: "auto", disable_parallel_tool_use: caps.parallelToolCalls !== true }
1678
2194
  } : {}
1679
2195
  };
2196
+ return { body, ...strictTools === undefined ? {} : { strictTools } };
1680
2197
  }
1681
2198
  function reasoningRequest(request, definition, options, max) {
1682
2199
  const reasoning = request.reasoning;
@@ -1685,7 +2202,7 @@ function reasoningRequest(request, definition, options, max) {
1685
2202
  const caps = definition.capabilities.reasoning;
1686
2203
  const issue = modelReasoningSupportIssue3(caps, reasoning);
1687
2204
  if (caps === undefined || issue)
1688
- throw new UnsupportedModelFeatureError4(`${PREFIX} ${issue ?? "Configure reasoning capabilities for this deployment"}.`);
2205
+ throw new UnsupportedModelFeatureError6(`${PREFIX} ${issue ?? "Configure reasoning capabilities for this deployment"}.`);
1689
2206
  if (reasoning === "none") {
1690
2207
  return {
1691
2208
  thinking: { type: "disabled" }
@@ -1693,22 +2210,173 @@ function reasoningRequest(request, definition, options, max) {
1693
2210
  }
1694
2211
  if (typeof reasoning === "string") {
1695
2212
  if (reasoning === "minimal" || options.thinkingMode === "manual")
1696
- 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.`);
1697
2214
  return { thinking: { type: "adaptive" }, effort: reasoning };
1698
2215
  }
1699
2216
  if (options.thinkingMode === "adaptive")
1700
- 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.`);
1701
2218
  if (reasoning.budgetTokens < 1024 || reasoning.budgetTokens >= max)
1702
- 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}).`);
1703
2220
  return { thinking: { type: "enabled", budget_tokens: reasoning.budgetTokens } };
1704
2221
  }
1705
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
+
1706
2374
  // src/resolution.ts
1707
2375
  import {
1708
2376
  defineLanguageModel as defineLanguageModel2,
1709
2377
  defineLanguageModelLabel,
1710
- defineModelRateCard as defineModelRateCard4,
1711
- UnsupportedModelFeatureError as UnsupportedModelFeatureError5
2378
+ defineModelRateCard as defineModelRateCard7,
2379
+ UnsupportedModelFeatureError as UnsupportedModelFeatureError7
1712
2380
  } from "@sixb/core/models";
1713
2381
  function resolveModel(input) {
1714
2382
  const { deployment, options } = input;
@@ -1725,10 +2393,10 @@ function resolveModel(input) {
1725
2393
  const publisher = deploymentPublisher?.id === "fireworks-ai" ? entry?.definition.publisher ?? deploymentPublisher : deploymentPublisher ?? entry?.definition.publisher;
1726
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");
1727
2395
  if (protocol === "messages" && deployment?.connectionName)
1728
- 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.`);
1729
2397
  const flag = deployment?.capabilities[protocol === "chat" ? "chatCompletion" : protocol] ?? (protocol === "chat" ? deployment?.capabilities.chat_completion : undefined);
1730
2398
  if (flag === "false")
1731
- 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.`);
1732
2400
  const definition = defineLanguageModel2({
1733
2401
  ...entry?.definition,
1734
2402
  publisher,
@@ -1766,7 +2434,7 @@ function resolveModel(input) {
1766
2434
  })
1767
2435
  } : {}
1768
2436
  }),
1769
- rateCard: options.rateCard ? defineModelRateCard4(options.rateCard) : entry?.rateCard,
2437
+ rateCard: options.rateCard ? defineModelRateCard7(options.rateCard) : entry?.rateCard,
1770
2438
  aliases: Object.freeze([
1771
2439
  ...new Set([
1772
2440
  input.modelId,
@@ -1815,6 +2483,462 @@ function effectiveDefinition(definition, protocol, options) {
1815
2483
  });
1816
2484
  }
1817
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
+
1818
2942
  // src/provider.ts
1819
2943
  function createAzureAIFoundry(options) {
1820
2944
  const transport = new FoundryTransport(options);
@@ -1837,6 +2961,11 @@ function createAzureAIFoundry(options) {
1837
2961
  const responses = (name, options = {}) => model("responses", name, options);
1838
2962
  const messages = (name, options = {}) => model("messages", name, options);
1839
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);
1840
2969
  const list = async (protocol, refresh = false) => {
1841
2970
  if (refresh)
1842
2971
  modelCatalog.invalidate();
@@ -1845,7 +2974,7 @@ function createAzureAIFoundry(options) {
1845
2974
  const results = [];
1846
2975
  for (const deployment of records) {
1847
2976
  const name = deployment.name;
1848
- if (isEmbeddingDeployment(deployment) || await modelCatalog.getEmbedding(deployment.modelName, true))
2977
+ if (deploymentKind(deployment) !== "language" || await modelCatalog.getEmbedding(deployment.modelName, true))
1849
2978
  continue;
1850
2979
  try {
1851
2980
  const profile = resolveModel({
@@ -1861,7 +2990,7 @@ function createAzureAIFoundry(options) {
1861
2990
  if (supported)
1862
2991
  results.push(profile.definition);
1863
2992
  } catch (error) {
1864
- if (!(error instanceof UnsupportedModelFeatureError6))
2993
+ if (!(error instanceof UnsupportedModelFeatureError9))
1865
2994
  throw error;
1866
2995
  }
1867
2996
  }
@@ -1874,7 +3003,19 @@ function createAzureAIFoundry(options) {
1874
3003
  deployments: () => deployments.list()
1875
3004
  };
1876
3005
  const auto = (name, options = {}) => model(undefined, name, options);
1877
- 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
+ });
1878
3019
  }
1879
3020
 
1880
3021
  class FoundryModel {
@@ -1962,8 +3103,9 @@ class FoundryModel {
1962
3103
  if (this.resolution)
1963
3104
  return this;
1964
3105
  const resolution = await this.deployments.resolve(this.modelId, options?.offline === true);
1965
- if (isEmbeddingDeployment(resolution.deployment))
1966
- 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.`);
1967
3109
  let catalogModel;
1968
3110
  try {
1969
3111
  catalogModel = await this.modelCatalog.get(resolution.deployment.modelName, options?.offline);
@@ -1975,7 +3117,7 @@ class FoundryModel {
1975
3117
  throw error;
1976
3118
  }
1977
3119
  if (await this.modelCatalog.getEmbedding(resolution.deployment.modelName, true))
1978
- throw new UnsupportedModelFeatureError6(`${PREFIX} Use foundry.embedding() for an embedding deployment.`);
3120
+ throw new UnsupportedModelFeatureError9(`${PREFIX} Use foundry.embedding() for an embedding deployment.`);
1979
3121
  return new FoundryModel(this.requestedProtocol, this.transport, this.deployments, this.providerId, this.modelId, this.options, this.modelCatalog, {
1980
3122
  ...resolution,
1981
3123
  catalogModel
@@ -1994,11 +3136,10 @@ class FoundryModel {
1994
3136
  const model = await abortable(() => this.#pendingExecution, request.signal);
1995
3137
  return model.stream(request);
1996
3138
  }
1997
- const body = this.protocol === "messages" ? foundryMessagesRequest(request, this.definition, this.options, this.scope) : this.protocol === "chat" ? foundryChatRequest(request, this.definition, this.options, this.scope) : responsesRequest(request, this.definition, this.options, this.scope, this.resolution?.deployment);
1998
- const response = await this.transport.post(JSON.stringify(body), request.signal, this.providerId, this.modelId, this.protocol);
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);
1999
3140
  const id = this.transport.redactText(response, requestId(response));
2000
3141
  if (!response.body)
2001
- 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 });
2002
3143
  const streamOptions = {
2003
3144
  providerId: this.providerId,
2004
3145
  modelId: this.modelId,
@@ -2033,10 +3174,22 @@ class FoundryModel {
2033
3174
  }), response)
2034
3175
  };
2035
3176
  }
3177
+ async postMessages(request) {
3178
+ const prepared = foundryMessagesRequest(request, this.definition, this.options, this.scope);
3179
+ const post = (body) => this.transport.post(JSON.stringify(body), request.signal, this.providerId, this.modelId, "messages");
3180
+ try {
3181
+ return await post(prepared.body);
3182
+ } catch (error) {
3183
+ if (prepared.strictTools === undefined || !(error instanceof ModelProviderError7) || !isStrictToolLimitError(error))
3184
+ throw error;
3185
+ strictToolRefusals.add(prepared.strictTools);
3186
+ return post(foundryMessagesRequest(request, this.definition, this.options, this.scope, true).body);
3187
+ }
3188
+ }
2036
3189
  async* events(source, response) {
2037
3190
  try {
2038
3191
  for await (const event of source) {
2039
- if (event.type === "error" && event.error instanceof ModelProviderError4) {
3192
+ if (event.type === "error" && event.error instanceof ModelProviderError7) {
2040
3193
  yield { ...event, error: this.transport.redactError(response, event.error) };
2041
3194
  } else if (event.type === "provider-state" && event.providerId === this.providerId) {
2042
3195
  yield { ...event, data: { ...object(event.data), scope: this.scope } };
@@ -2059,11 +3212,8 @@ class FoundryModel {
2059
3212
  }
2060
3213
  }
2061
3214
  }
2062
- function isEmbeddingDeployment(deployment) {
2063
- return deployment.capabilities.embeddings === "true" || deployment.capabilities.embedding === "true" || /^(text-embedding-|cohere-embed)/i.test(deployment.modelName);
2064
- }
2065
3215
  export {
2066
3216
  createAzureAIFoundry
2067
3217
  };
2068
3218
 
2069
- //# debugId=96BCAAA89374958164756E2164756E21
3219
+ //# debugId=9D607CB81AD6E4F564756E2164756E21