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