@sixb/azure-ai-foundry 0.2.5 → 0.2.7
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 +66 -33
- package/dist/accounting.d.ts +5 -0
- package/dist/accounting.d.ts.map +1 -1
- package/dist/caching.d.ts +34 -0
- package/dist/caching.d.ts.map +1 -0
- package/dist/catalog.d.ts.map +1 -1
- package/dist/chat-accounting.d.ts.map +1 -1
- package/dist/chat-request.d.ts +2 -1
- package/dist/chat-request.d.ts.map +1 -1
- package/dist/discovery.d.ts.map +1 -1
- package/dist/embedding.d.ts +1 -3
- package/dist/embedding.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +255 -113
- package/dist/index.js.map +17 -16
- package/dist/kinds.d.ts.map +1 -1
- package/dist/messages-request.d.ts.map +1 -1
- package/dist/provider.d.ts +10 -5
- package/dist/provider.d.ts.map +1 -1
- package/dist/request.d.ts +6 -2
- package/dist/request.d.ts.map +1 -1
- package/dist/resolution.d.ts.map +1 -1
- package/dist/transport.d.ts +30 -3
- package/dist/transport.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/accounting.ts +33 -11
- package/src/caching.ts +80 -0
- package/src/catalog.ts +15 -4
- package/src/chat-accounting.ts +12 -9
- package/src/chat-request.ts +14 -3
- package/src/discovery.ts +6 -0
- package/src/embedding.ts +1 -13
- package/src/index.ts +2 -6
- package/src/kinds.ts +0 -9
- package/src/messages-accounting.ts +6 -1
- package/src/messages-request.ts +32 -24
- package/src/provider.ts +65 -50
- package/src/request.ts +56 -23
- package/src/resolution.ts +13 -14
- package/src/transport.ts +130 -31
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
MODEL_REASONING_LEVELS,
|
|
6
6
|
ModelCatalogUnavailableError as ModelCatalogUnavailableError4,
|
|
7
7
|
ModelProviderError as ModelProviderError7,
|
|
8
|
-
UnsupportedModelFeatureError as
|
|
8
|
+
UnsupportedModelFeatureError as UnsupportedModelFeatureError10
|
|
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";
|
|
@@ -39,19 +39,20 @@ function foundryUsage(raw, reliableReasoning) {
|
|
|
39
39
|
const usage = responsesUsage(raw);
|
|
40
40
|
const { reasoningOutputTokens, ...totals } = usage;
|
|
41
41
|
const cached = usage.cacheReadInputTokens;
|
|
42
|
+
const written = counter(object(raw?.input_tokens_details)?.cache_write_tokens);
|
|
42
43
|
const input = usage.inputTokens;
|
|
43
44
|
const output = usage.outputTokens;
|
|
44
45
|
const knownReasoning = reasoningOutputTokens !== undefined && output !== undefined && reasoningOutputTokens <= output && (reliableReasoning === true || reliableReasoning === undefined && reasoningOutputTokens > 0);
|
|
45
46
|
return {
|
|
46
47
|
...totals,
|
|
47
|
-
...
|
|
48
|
-
...input !== undefined && cached !== undefined && cached <= input && onlyTokenMeters(raw) ? { uncachedInputTokens: input - cached } : {},
|
|
48
|
+
...written === undefined ? {} : { cacheWriteInputTokens: written },
|
|
49
|
+
...input !== undefined && cached !== undefined && cached + (written ?? 0) <= input && onlyTokenMeters(raw) ? { uncachedInputTokens: input - cached - (written ?? 0) } : {},
|
|
49
50
|
...knownReasoning ? { reasoningOutputTokens } : {},
|
|
50
51
|
...knownReasoning && reasoningOutputTokens !== undefined && output !== undefined && reasoningOutputTokens <= output && onlyTokenMeters(raw) ? { textOutputTokens: output - reasoningOutputTokens } : {}
|
|
51
52
|
};
|
|
52
53
|
}
|
|
53
54
|
function foundryEstimator(card, request, modelName, modelVersion, aliases) {
|
|
54
|
-
return tokenEstimator(card, request, ["temperature", "top_p", "metadata"], onlyTokenMeters, modelName, modelVersion, aliases);
|
|
55
|
+
return tokenEstimator(card, request, ["temperature", "top_p", "metadata", "prompt_cache_key", "prompt_cache_retention"], onlyTokenMeters, modelName, modelVersion, aliases);
|
|
55
56
|
}
|
|
56
57
|
function tokenEstimator(card, request, requestKeys, tokenMeters, modelName, modelVersion, aliases) {
|
|
57
58
|
const rateCard = card && defineModelRateCard(card);
|
|
@@ -78,7 +79,38 @@ function onlyTokenMeters(raw) {
|
|
|
78
79
|
"output_tokens_details"
|
|
79
80
|
].includes(key)))
|
|
80
81
|
return false;
|
|
81
|
-
return Object.entries(object(raw.input_tokens_details) ?? {}).every(([key, value]) => key === "cached_tokens" || key === "cache_write_tokens"
|
|
82
|
+
return Object.entries(object(raw.input_tokens_details) ?? {}).every(([key, value]) => key === "cached_tokens" || key === "cache_write_tokens" || key === "cache_write_tokens_details" && defaultLifetimeWrites(value)) && Object.keys(object(raw.output_tokens_details) ?? {}).every((key) => key === "reasoning_tokens");
|
|
83
|
+
}
|
|
84
|
+
function defaultLifetimeWrites(details) {
|
|
85
|
+
const lifetimes = object(details);
|
|
86
|
+
return lifetimes !== undefined && Object.entries(lifetimes).every(([key, value]) => key === "ttl_30m_tokens" && counter(value) !== undefined || key === "ttl_12h_tokens" && value === 0);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/caching.ts
|
|
90
|
+
import { billsPromptCacheWrites } from "@sixb/model-protocols/responses";
|
|
91
|
+
var CACHING_FIELDS = ["prompt_cache_key", "prompt_cache_retention", "prompt_cache_options"];
|
|
92
|
+
var refusals = new Set;
|
|
93
|
+
function promptCaching(input) {
|
|
94
|
+
const { caching, deployment } = input;
|
|
95
|
+
const openai = deployment?.modelPublisher.toLowerCase() === "openai";
|
|
96
|
+
const billsWrites = openai && billsPromptCacheWrites(deployment.modelName);
|
|
97
|
+
const reusable = caching === "auto" || caching !== "off" && (input.localTools > 0 || input.messages.some((message) => message.role === "assistant"));
|
|
98
|
+
const intent = typeof caching === "object" ? caching : undefined;
|
|
99
|
+
const settings = {
|
|
100
|
+
...intent?.key ? { prompt_cache_key: intent.key } : {},
|
|
101
|
+
...!reusable && billsWrites ? { prompt_cache_options: { mode: "explicit" } } : {},
|
|
102
|
+
...reusable && openai && !billsWrites && intent?.lifetime === "long" ? { prompt_cache_retention: "24h" } : {}
|
|
103
|
+
};
|
|
104
|
+
return Object.fromEntries(Object.entries(settings).filter(([field]) => !Object.hasOwn(input.request ?? {}, field) && !refusals.has(`${input.scope}\x00${field}`)));
|
|
105
|
+
}
|
|
106
|
+
function refusedCaching(error, body, request, scope) {
|
|
107
|
+
if (error.status !== 400)
|
|
108
|
+
return false;
|
|
109
|
+
const field = CACHING_FIELDS.find((name) => Object.hasOwn(body, name) && !Object.hasOwn(request ?? {}, name) && error.message.includes(name));
|
|
110
|
+
if (!field)
|
|
111
|
+
return false;
|
|
112
|
+
refusals.add(`${scope}\x00${field}`);
|
|
113
|
+
return true;
|
|
82
114
|
}
|
|
83
115
|
|
|
84
116
|
// src/catalog.ts
|
|
@@ -88,7 +120,11 @@ import {
|
|
|
88
120
|
MODEL_REASONING_EFFORTS,
|
|
89
121
|
ModelCatalogUnavailableError
|
|
90
122
|
} from "@sixb/core/models";
|
|
91
|
-
import {
|
|
123
|
+
import {
|
|
124
|
+
downloadModelsDev,
|
|
125
|
+
modelsDevRateCard,
|
|
126
|
+
scaleTokenPrice
|
|
127
|
+
} from "@sixb/model-protocols/models-dev";
|
|
92
128
|
|
|
93
129
|
// src/publisher.ts
|
|
94
130
|
var publishers = {
|
|
@@ -278,7 +314,11 @@ function rates(raw, protocol) {
|
|
|
278
314
|
if (protocol !== "messages" || card?.cacheWriteInput === undefined)
|
|
279
315
|
return card;
|
|
280
316
|
const { cacheWriteInput, ...rest } = card;
|
|
281
|
-
return defineModelRateCard2({
|
|
317
|
+
return defineModelRateCard2({
|
|
318
|
+
...rest,
|
|
319
|
+
cacheWriteInput5m: cacheWriteInput,
|
|
320
|
+
...rest.input === undefined ? {} : { cacheWriteInput1h: scaleTokenPrice(rest.input, [2n, 1n]) }
|
|
321
|
+
});
|
|
282
322
|
}
|
|
283
323
|
|
|
284
324
|
// src/chat-accounting.ts
|
|
@@ -286,6 +326,7 @@ function foundryChatUsage(raw, reliableReasoning) {
|
|
|
286
326
|
const input = counter(raw?.prompt_tokens);
|
|
287
327
|
const output = counter(raw?.completion_tokens);
|
|
288
328
|
const read = counter(object(raw?.prompt_tokens_details)?.cached_tokens);
|
|
329
|
+
const written = counter(object(raw?.prompt_tokens_details)?.cache_write_tokens);
|
|
289
330
|
const reported = reasoningTokens(raw);
|
|
290
331
|
const reasoning = reported !== undefined && reported !== false && (reliableReasoning === true || reliableReasoning === undefined && reported > 0) ? reported : undefined;
|
|
291
332
|
const known = tokenMeters(raw);
|
|
@@ -293,15 +334,15 @@ function foundryChatUsage(raw, reliableReasoning) {
|
|
|
293
334
|
...input === undefined ? {} : { inputTokens: input },
|
|
294
335
|
...output === undefined ? {} : { outputTokens: output },
|
|
295
336
|
...read === undefined ? {} : { cacheReadInputTokens: read },
|
|
296
|
-
...
|
|
297
|
-
...input !== undefined && read !== undefined && read <= input && known ? { uncachedInputTokens: input - read } : {},
|
|
337
|
+
...written === undefined ? {} : { cacheWriteInputTokens: written },
|
|
338
|
+
...input !== undefined && read !== undefined && read + (written ?? 0) <= input && known ? { uncachedInputTokens: input - read - (written ?? 0) } : {},
|
|
298
339
|
...reasoning === undefined ? {} : { reasoningOutputTokens: reasoning },
|
|
299
340
|
...output !== undefined && reasoning !== undefined && reasoning <= output && known ? { textOutputTokens: output - reasoning } : {},
|
|
300
341
|
...raw ? { raw } : {}
|
|
301
342
|
};
|
|
302
343
|
}
|
|
303
344
|
function foundryChatEstimator(card, request, modelName, modelVersion, aliases) {
|
|
304
|
-
const estimator = tokenEstimator(card, request, ["temperature", "top_p", "metadata"], tokenMeters, modelName, modelVersion, aliases);
|
|
345
|
+
const estimator = tokenEstimator(card, request, ["temperature", "top_p", "metadata", "prompt_cache_key", "prompt_cache_retention"], tokenMeters, modelName, modelVersion, aliases);
|
|
305
346
|
return {
|
|
306
347
|
...estimator.rateCard ? { rateCard: estimator.rateCard } : {},
|
|
307
348
|
estimateReservation: estimator.estimateReservation,
|
|
@@ -327,9 +368,12 @@ function tokenMeters(raw) {
|
|
|
327
368
|
if (!details)
|
|
328
369
|
return false;
|
|
329
370
|
for (const [meter, count] of Object.entries(details)) {
|
|
330
|
-
if (key === "prompt_tokens_details" && meter === "cached_tokens" || key !== "prompt_tokens_details" && meter === "reasoning_tokens") {
|
|
371
|
+
if (key === "prompt_tokens_details" && (meter === "cached_tokens" || meter === "cache_write_tokens") || key !== "prompt_tokens_details" && meter === "reasoning_tokens") {
|
|
331
372
|
if (counter(count) === undefined)
|
|
332
373
|
return false;
|
|
374
|
+
} else if (key === "prompt_tokens_details" && meter === "cache_write_tokens_details") {
|
|
375
|
+
if (!defaultLifetimeWrites(count))
|
|
376
|
+
return false;
|
|
333
377
|
} else if (key === "prompt_tokens_details" && meter === "image_tokens") {
|
|
334
378
|
const image = counter(count);
|
|
335
379
|
const input = counter(raw.prompt_tokens);
|
|
@@ -337,7 +381,7 @@ function tokenMeters(raw) {
|
|
|
337
381
|
return false;
|
|
338
382
|
} else if (![
|
|
339
383
|
"audio_tokens",
|
|
340
|
-
...key === "prompt_tokens_details" ? [
|
|
384
|
+
...key === "prompt_tokens_details" ? [] : ["accepted_prediction_tokens", "rejected_prediction_tokens"]
|
|
341
385
|
].includes(meter) || count !== 0)
|
|
342
386
|
return false;
|
|
343
387
|
}
|
|
@@ -526,8 +570,9 @@ function validateOptions(options) {
|
|
|
526
570
|
throw new TypeError(`${PREFIX} Invalid reasoningSummary.`);
|
|
527
571
|
}
|
|
528
572
|
function responsesRequest(request, definition, options, scope, deployment) {
|
|
529
|
-
const max = prepareRequest(request, definition, options, scope);
|
|
530
|
-
const
|
|
573
|
+
const { max, messages } = prepareRequest(request, definition, options, scope);
|
|
574
|
+
const replayed = responsesInput(messages, definition.providerId, definition.modelId);
|
|
575
|
+
const input = replayed.map((item) => {
|
|
531
576
|
if (!isJsonObject3(item))
|
|
532
577
|
return item;
|
|
533
578
|
const content = Array.isArray(item.content) ? item.content.map((part) => {
|
|
@@ -575,6 +620,14 @@ function responsesRequest(request, definition, options, scope, deployment) {
|
|
|
575
620
|
stream: true,
|
|
576
621
|
store: false,
|
|
577
622
|
...options.encryptedReasoning === false ? {} : { include: ["reasoning.encrypted_content"] },
|
|
623
|
+
...promptCaching({
|
|
624
|
+
caching: request.caching,
|
|
625
|
+
messages: request.messages,
|
|
626
|
+
localTools: request.tools.length,
|
|
627
|
+
deployment,
|
|
628
|
+
request: options.request,
|
|
629
|
+
scope
|
|
630
|
+
}),
|
|
578
631
|
...max === undefined ? {} : { max_output_tokens: max },
|
|
579
632
|
...tools.length ? {
|
|
580
633
|
tools,
|
|
@@ -602,28 +655,44 @@ function responsesRequest(request, definition, options, scope, deployment) {
|
|
|
602
655
|
}
|
|
603
656
|
function prepareRequest(request, definition, options, scope, remotePdf = false) {
|
|
604
657
|
positiveInteger(request.maxOutputTokens, "maxOutputTokens");
|
|
605
|
-
validateMessages(request.messages, definition, options.maxInputFileBytes ?? 20 * 1024 * 1024,
|
|
658
|
+
validateMessages(request.messages, definition, options.maxInputFileBytes ?? 20 * 1024 * 1024, remotePdf);
|
|
606
659
|
const limits = [
|
|
607
660
|
definition.maxOutputTokens,
|
|
608
661
|
options.maxOutputTokens,
|
|
609
662
|
request.maxOutputTokens
|
|
610
663
|
].filter((n) => n !== undefined);
|
|
611
|
-
return
|
|
664
|
+
return {
|
|
665
|
+
max: limits.length ? Math.min(...limits) : undefined,
|
|
666
|
+
messages: ownReplayState(request.messages, definition.providerId, scope)
|
|
667
|
+
};
|
|
612
668
|
}
|
|
613
|
-
function
|
|
669
|
+
function ownReplayState(messages, providerId, scope) {
|
|
670
|
+
const foreign = (data) => data !== undefined && object(data)?.scope !== scope;
|
|
671
|
+
const own = (part) => {
|
|
672
|
+
if (!part.providerData || !foreign(part.providerData[providerId]))
|
|
673
|
+
return part;
|
|
674
|
+
const { [providerId]: _foreign, ...others } = part.providerData;
|
|
675
|
+
return { ...part, providerData: others };
|
|
676
|
+
};
|
|
677
|
+
return messages.map((message) => {
|
|
678
|
+
if (message.role === "system")
|
|
679
|
+
return own(message);
|
|
680
|
+
if (message.role === "user")
|
|
681
|
+
return { ...message, content: message.content.map(own) };
|
|
682
|
+
if (message.role === "tool")
|
|
683
|
+
return { ...message, content: message.content.map(own) };
|
|
684
|
+
return {
|
|
685
|
+
...message,
|
|
686
|
+
content: message.content.flatMap((part) => part.type !== "provider-state" ? [own(part)] : part.providerId === providerId && foreign(part.data) ? [] : [part])
|
|
687
|
+
};
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
function validateMessages(messages, definition, budget, remotePdf = false) {
|
|
614
691
|
let bytes = 0;
|
|
615
692
|
for (const message of messages) {
|
|
616
|
-
if (message.role === "system")
|
|
617
|
-
checkScope(message.providerData?.[definition.providerId], scope);
|
|
693
|
+
if (message.role === "system")
|
|
618
694
|
continue;
|
|
619
|
-
}
|
|
620
695
|
for (const part of message.content) {
|
|
621
|
-
if (part.type === "provider-state") {
|
|
622
|
-
if (part.providerId === definition.providerId)
|
|
623
|
-
checkScope(part.data, scope);
|
|
624
|
-
continue;
|
|
625
|
-
}
|
|
626
|
-
checkScope(part.providerData?.[definition.providerId], scope);
|
|
627
696
|
if (part.type !== "file")
|
|
628
697
|
continue;
|
|
629
698
|
const allowed = definition.capabilities.inputMediaTypes;
|
|
@@ -650,11 +719,6 @@ function validateMessages(messages, definition, budget, scope, remotePdf = false
|
|
|
650
719
|
}
|
|
651
720
|
}
|
|
652
721
|
}
|
|
653
|
-
function checkScope(data, scope) {
|
|
654
|
-
if (data !== undefined && object(data)?.scope !== scope) {
|
|
655
|
-
throw new UnsupportedModelFeatureError(`${PREFIX} Replay state belongs to a different endpoint, deployment, or protocol.`);
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
722
|
function foundryStrictTool(tool) {
|
|
659
723
|
if (!tool.strict)
|
|
660
724
|
return;
|
|
@@ -702,8 +766,8 @@ function validateChatOptions(options) {
|
|
|
702
766
|
if (options.includeUsage !== undefined && typeof options.includeUsage !== "boolean")
|
|
703
767
|
throw new TypeError(`${PREFIX} includeUsage must be boolean.`);
|
|
704
768
|
}
|
|
705
|
-
function foundryChatRequest(request, definition, options, scope) {
|
|
706
|
-
const max = prepareRequest(request, definition, options, scope);
|
|
769
|
+
function foundryChatRequest(request, definition, options, scope, deployment) {
|
|
770
|
+
const { max, messages } = prepareRequest(request, definition, options, scope);
|
|
707
771
|
const profile = options.profile ?? "openai";
|
|
708
772
|
if (profile !== "deepseek" && options.reasoningReplay === "tool-continuation")
|
|
709
773
|
throw new UnsupportedModelFeatureError2(`${PREFIX} Reasoning-content replay requires the DeepSeek Chat profile.`);
|
|
@@ -735,7 +799,7 @@ function foundryChatRequest(request, definition, options, scope) {
|
|
|
735
799
|
return {
|
|
736
800
|
...options.request,
|
|
737
801
|
model: definition.modelId,
|
|
738
|
-
messages: chatInput(
|
|
802
|
+
messages: chatInput(messages, {
|
|
739
803
|
providerId: definition.providerId,
|
|
740
804
|
errorPrefix: PREFIX,
|
|
741
805
|
systemRole: options.systemRole,
|
|
@@ -743,6 +807,14 @@ function foundryChatRequest(request, definition, options, scope) {
|
|
|
743
807
|
}),
|
|
744
808
|
stream: true,
|
|
745
809
|
...options.includeUsage === false ? {} : { stream_options: { include_usage: true } },
|
|
810
|
+
...promptCaching({
|
|
811
|
+
caching: request.caching,
|
|
812
|
+
messages: request.messages,
|
|
813
|
+
localTools: request.tools.length,
|
|
814
|
+
deployment,
|
|
815
|
+
request: options.request,
|
|
816
|
+
scope
|
|
817
|
+
}),
|
|
746
818
|
...max === undefined ? {} : { [options.maxTokensParameter ?? "max_completion_tokens"]: max },
|
|
747
819
|
...explicitReasoning && typeof reasoning === "string" ? { reasoning_effort: reasoning } : {},
|
|
748
820
|
...tools.length ? {
|
|
@@ -776,7 +848,8 @@ import { systemOneRequest, systemOneResult } from "@sixb/model-protocols/systemo
|
|
|
776
848
|
// src/discovery.ts
|
|
777
849
|
import {
|
|
778
850
|
ModelCatalogUnavailableError as ModelCatalogUnavailableError2,
|
|
779
|
-
ModelProviderError as ModelProviderError3
|
|
851
|
+
ModelProviderError as ModelProviderError3,
|
|
852
|
+
UnsupportedModelFeatureError as UnsupportedModelFeatureError3
|
|
780
853
|
} from "@sixb/core/models";
|
|
781
854
|
|
|
782
855
|
// src/diagnostics.ts
|
|
@@ -826,28 +899,38 @@ var ONE_SHOT = new Set([
|
|
|
826
899
|
]);
|
|
827
900
|
var AUDIO_API_VERSION = "2024-10-21";
|
|
828
901
|
var IMAGE_API_VERSION = "2025-04-01-preview";
|
|
902
|
+
var ENTRA_SCOPE = "https://ai.azure.com/.default";
|
|
903
|
+
var RENEW_BEFORE_EXPIRY_MS = 2 * 60000;
|
|
904
|
+
var EXPIRING_MS = 1000;
|
|
905
|
+
var RENEW_RETRY_MS = 3000;
|
|
906
|
+
var TOKEN_TIMEOUT_MS = 30000;
|
|
829
907
|
|
|
830
908
|
class FoundryTransport {
|
|
831
909
|
diagnostics = new WeakMap;
|
|
832
|
-
baseUrl;
|
|
833
910
|
projectUrl;
|
|
911
|
+
resourceUrl;
|
|
834
912
|
options;
|
|
835
|
-
|
|
913
|
+
token;
|
|
914
|
+
renewal;
|
|
915
|
+
constructor(options) {
|
|
836
916
|
if (!URL.canParse(options.endpoint))
|
|
837
|
-
throw new TypeError(`${PREFIX} endpoint must be a valid
|
|
917
|
+
throw new TypeError(`${PREFIX} endpoint must be a valid project URL.`);
|
|
838
918
|
const url = new URL(options.endpoint);
|
|
839
919
|
if (!["https:", "http:"].includes(url.protocol) || url.username || url.password || url.search || url.hash) {
|
|
840
|
-
throw new TypeError(`${PREFIX} endpoint must be an HTTP(S)
|
|
920
|
+
throw new TypeError(`${PREFIX} endpoint must be an HTTP(S) project URL without credentials, query, or fragment.`);
|
|
841
921
|
}
|
|
842
922
|
const path = url.pathname.replace(/\/+$/, "");
|
|
843
|
-
if (
|
|
923
|
+
if (!/^\/api\/projects\/[^/]+$/.test(path))
|
|
844
924
|
throw new TypeError(`${PREFIX} endpoint must be a Foundry project URL: https://<resource>.services.ai.azure.com/api/projects/<project>.`);
|
|
845
|
-
if (target === "resource" && path !== "" && path !== "/openai/v1")
|
|
846
|
-
throw new TypeError(`${PREFIX} embeddings.endpoint must be a resource origin or /openai/v1 URL.`);
|
|
847
925
|
this.projectUrl = `${url.origin}${path}`;
|
|
848
|
-
this.
|
|
849
|
-
if (
|
|
850
|
-
|
|
926
|
+
this.resourceUrl = url.origin;
|
|
927
|
+
if (options.credential !== undefined) {
|
|
928
|
+
if (options.apiKey !== undefined)
|
|
929
|
+
throw new TypeError(`${PREFIX} Configure either apiKey or credential, not both.`);
|
|
930
|
+
if (typeof options.credential?.getToken !== "function")
|
|
931
|
+
throw new TypeError(`${PREFIX} credential must have a getToken method, such as DefaultAzureCredential from @azure/identity.`);
|
|
932
|
+
} else if (typeof options.apiKey !== "function" && (typeof options.apiKey !== "string" || !options.apiKey.trim()))
|
|
933
|
+
throw new TypeError(`${PREFIX} apiKey or credential is required.`);
|
|
851
934
|
if (options.maxRetries !== undefined && (!Number.isSafeInteger(options.maxRetries) || options.maxRetries < 0)) {
|
|
852
935
|
throw new TypeError(`${PREFIX} maxRetries must be a nonnegative safe integer.`);
|
|
853
936
|
}
|
|
@@ -915,35 +998,78 @@ class FoundryTransport {
|
|
|
915
998
|
diagnostics.add(value);
|
|
916
999
|
for (const reserved of ["authorization", "api-key", "x-api-key"])
|
|
917
1000
|
if (headers.has(reserved))
|
|
918
|
-
throw new TypeError(`${PREFIX} Use apiKey instead of '${reserved}' headers.`);
|
|
1001
|
+
throw new TypeError(`${PREFIX} Use apiKey or credential instead of '${reserved}' headers.`);
|
|
1002
|
+
if (this.options.credential) {
|
|
1003
|
+
const token = await this.bearerToken(signal);
|
|
1004
|
+
diagnostics.add(token);
|
|
1005
|
+
headers.set("authorization", `Bearer ${token}`);
|
|
1006
|
+
return headers;
|
|
1007
|
+
}
|
|
919
1008
|
const key = await resolve(this.options.apiKey, signal);
|
|
920
1009
|
if (typeof key !== "string" || !key.trim())
|
|
921
|
-
throw new TypeError(`${PREFIX} apiKey must resolve to a nonempty string.`);
|
|
1010
|
+
throw new TypeError(`${PREFIX} apiKey must resolve to a nonempty string: pass apiKey or credential, or set AZURE_AI_FOUNDRY_API_KEY.`);
|
|
922
1011
|
diagnostics.add(key);
|
|
923
1012
|
headers.set(keyHeader, key);
|
|
924
1013
|
return headers;
|
|
925
1014
|
}
|
|
1015
|
+
bearerToken(signal) {
|
|
1016
|
+
const token = this.token;
|
|
1017
|
+
const now = Date.now();
|
|
1018
|
+
if (!token || now >= token.expiresAt - EXPIRING_MS)
|
|
1019
|
+
return abortable(() => this.renewToken(), signal);
|
|
1020
|
+
if (now >= token.renewAt)
|
|
1021
|
+
this.renewToken().catch(() => {
|
|
1022
|
+
if (this.token === token)
|
|
1023
|
+
this.token = { ...token, renewAt: Date.now() + RENEW_RETRY_MS };
|
|
1024
|
+
});
|
|
1025
|
+
return Promise.resolve(token.value);
|
|
1026
|
+
}
|
|
1027
|
+
renewToken() {
|
|
1028
|
+
this.renewal ??= (async () => {
|
|
1029
|
+
const controller = new AbortController;
|
|
1030
|
+
const timer = setTimeout(() => controller.abort(new Error(`${PREFIX} credential.getToken timed out.`)), TOKEN_TIMEOUT_MS);
|
|
1031
|
+
try {
|
|
1032
|
+
const signal = controller.signal;
|
|
1033
|
+
const token = await abortable(() => this.options.credential.getToken(ENTRA_SCOPE, { abortSignal: signal }), signal);
|
|
1034
|
+
if (typeof token?.token !== "string" || !token.token.trim())
|
|
1035
|
+
throw new TypeError(`${PREFIX} credential returned no token for ${ENTRA_SCOPE}.`);
|
|
1036
|
+
const expiresAt = token.expiresOnTimestamp ?? 0;
|
|
1037
|
+
this.token = {
|
|
1038
|
+
value: token.token,
|
|
1039
|
+
expiresAt,
|
|
1040
|
+
renewAt: Math.min(token.refreshAfterTimestamp ?? expiresAt, expiresAt - RENEW_BEFORE_EXPIRY_MS)
|
|
1041
|
+
};
|
|
1042
|
+
return token.token;
|
|
1043
|
+
} finally {
|
|
1044
|
+
clearTimeout(timer);
|
|
1045
|
+
this.renewal = undefined;
|
|
1046
|
+
}
|
|
1047
|
+
})();
|
|
1048
|
+
return this.renewal;
|
|
1049
|
+
}
|
|
926
1050
|
fetch(url, init) {
|
|
927
1051
|
return (this.options.fetch ?? fetch)(url, init);
|
|
928
1052
|
}
|
|
929
1053
|
url(protocol, deployment) {
|
|
930
|
-
|
|
931
|
-
|
|
1054
|
+
const resource = this.resourceUrl;
|
|
1055
|
+
const path = encodeURIComponent(deployment ?? "");
|
|
932
1056
|
if (protocol === "transcription")
|
|
933
|
-
return `${
|
|
934
|
-
if (protocol === "decision")
|
|
935
|
-
return `${new URL(this.baseUrl).origin}/providers/microsoft/v1/systemone`;
|
|
1057
|
+
return `${resource}/openai/deployments/${path}/audio/transcriptions?api-version=${AUDIO_API_VERSION}`;
|
|
936
1058
|
if (protocol === "image" || protocol === "image-edit")
|
|
937
|
-
return `${
|
|
1059
|
+
return `${resource}/openai/deployments/${path}/images/${protocol === "image" ? "generations" : "edits"}?api-version=${IMAGE_API_VERSION}`;
|
|
1060
|
+
if (protocol === "decision")
|
|
1061
|
+
return `${resource}/providers/microsoft/v1/systemone`;
|
|
938
1062
|
if (protocol === "reranking")
|
|
939
|
-
return `${
|
|
1063
|
+
return `${resource}/providers/cohere/v2/rerank`;
|
|
1064
|
+
if (protocol === "messages")
|
|
1065
|
+
return `${resource}/anthropic/v1/messages`;
|
|
1066
|
+
if (protocol === "embeddings")
|
|
1067
|
+
return `${resource}/openai/v1/embeddings`;
|
|
940
1068
|
if (protocol === "speech")
|
|
941
|
-
return `${
|
|
942
|
-
if (protocol === "responses")
|
|
943
|
-
return `${this.baseUrl}/responses`;
|
|
1069
|
+
return `${resource}/openai/v1/audio/speech`;
|
|
944
1070
|
if (protocol === "chat")
|
|
945
|
-
return `${
|
|
946
|
-
return `${
|
|
1071
|
+
return `${resource}/openai/v1/chat/completions`;
|
|
1072
|
+
return `${resource}/openai/v1/responses`;
|
|
947
1073
|
}
|
|
948
1074
|
}
|
|
949
1075
|
async function readJson(response, signal, maxBytes) {
|
|
@@ -1108,6 +1234,8 @@ class FoundryDeployments {
|
|
|
1108
1234
|
const deployment = snapshot?.byName.get(name);
|
|
1109
1235
|
if (!snapshot || !deployment)
|
|
1110
1236
|
throw this.invalid(`Deployment '${name}' was not found in ${offline ? "cached project deployments; resolve online first" : "this project"}.`);
|
|
1237
|
+
if (deployment.connectionName)
|
|
1238
|
+
throw new UnsupportedModelFeatureError3(`${PREFIX} Deployment '${name}' comes from project connection '${deployment.connectionName}', on another resource. Configure a provider with that resource's project URL.`);
|
|
1111
1239
|
return {
|
|
1112
1240
|
deployment,
|
|
1113
1241
|
discoveredAt: snapshot.fetchedAt
|
|
@@ -1331,7 +1459,7 @@ function deploymentRecord(raw, invalid) {
|
|
|
1331
1459
|
}
|
|
1332
1460
|
|
|
1333
1461
|
// src/kinds.ts
|
|
1334
|
-
import { UnsupportedModelFeatureError as
|
|
1462
|
+
import { UnsupportedModelFeatureError as UnsupportedModelFeatureError4 } from "@sixb/core/models";
|
|
1335
1463
|
var KIND_BINDINGS = {
|
|
1336
1464
|
language: "foundry() or foundry.chat()",
|
|
1337
1465
|
embedding: "foundry.embedding()",
|
|
@@ -1359,16 +1487,11 @@ function deploymentKind(deployment) {
|
|
|
1359
1487
|
function assertDeploymentKind(deployment, deploymentName, kind) {
|
|
1360
1488
|
const actual = deploymentKind(deployment);
|
|
1361
1489
|
if (actual !== kind)
|
|
1362
|
-
throw new
|
|
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.`);
|
|
1490
|
+
throw new UnsupportedModelFeatureError4(`${PREFIX} Deployment '${deploymentName}' runs ${deployment.modelName}, not ${article(kind)} ${kind} model. Use ${KIND_BINDINGS[actual]}.`);
|
|
1365
1491
|
}
|
|
1366
1492
|
function article(kind) {
|
|
1367
1493
|
return /^[aeiou]/.test(kind) ? "an" : "a";
|
|
1368
1494
|
}
|
|
1369
|
-
function capitalize(word) {
|
|
1370
|
-
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
1371
|
-
}
|
|
1372
1495
|
function isEmbeddingDeployment(deployment) {
|
|
1373
1496
|
return deployment.capabilities.embeddings === "true" || deployment.capabilities.embedding === "true" || /^(text-embedding-|cohere-embed)/i.test(deployment.modelName);
|
|
1374
1497
|
}
|
|
@@ -1490,7 +1613,7 @@ import {
|
|
|
1490
1613
|
defineModelRateCard as defineModelRateCard4,
|
|
1491
1614
|
EmbeddingModelResponseError,
|
|
1492
1615
|
ModelCatalogUnavailableError as ModelCatalogUnavailableError3,
|
|
1493
|
-
UnsupportedModelFeatureError as
|
|
1616
|
+
UnsupportedModelFeatureError as UnsupportedModelFeatureError5
|
|
1494
1617
|
} from "@sixb/core/models";
|
|
1495
1618
|
function createFoundryEmbedding(providerId, modelId, options, transport, deployments, catalog, resolution) {
|
|
1496
1619
|
const dimensions = options.dimensions;
|
|
@@ -1589,7 +1712,7 @@ function validateDeployment(resolved, dimensions) {
|
|
|
1589
1712
|
const name = deployment.modelName.toLowerCase();
|
|
1590
1713
|
const maxDimensions = name === "text-embedding-3-small" ? 1536 : name === "text-embedding-3-large" ? 3072 : name === "text-embedding-ada-002" ? 1536 : undefined;
|
|
1591
1714
|
if (deployment.modelPublisher.toLowerCase() !== "openai" || !maxDimensions || deployment.capabilities.embeddings === "false" || deployment.capabilities.embedding === "false")
|
|
1592
|
-
throw new
|
|
1715
|
+
throw new UnsupportedModelFeatureError5(`${PREFIX} This embedding deployment is not supported. Use an OpenAI text-embedding-3-small, text-embedding-3-large, or text-embedding-ada-002 deployment.`);
|
|
1593
1716
|
const configurable = name !== "text-embedding-ada-002";
|
|
1594
1717
|
if (dimensions > maxDimensions || !configurable && dimensions !== maxDimensions)
|
|
1595
1718
|
throw new TypeError(`${PREFIX} ${name} requires ${configurable ? `1\u2013${maxDimensions}` : maxDimensions} dimensions.`);
|
|
@@ -1673,7 +1796,7 @@ import {
|
|
|
1673
1796
|
estimateModelReservation as estimateModelReservation2,
|
|
1674
1797
|
ImageModelResponseError,
|
|
1675
1798
|
rateModelCall as rateModelCall2,
|
|
1676
|
-
UnsupportedModelFeatureError as
|
|
1799
|
+
UnsupportedModelFeatureError as UnsupportedModelFeatureError6
|
|
1677
1800
|
} from "@sixb/core/models";
|
|
1678
1801
|
var MAX_RESPONSE_BYTES2 = 512 * 1024 * 1024;
|
|
1679
1802
|
var MAX_TIMEOUT_MS2 = 2 ** 31 - 1;
|
|
@@ -1712,7 +1835,7 @@ function family(deployment) {
|
|
|
1712
1835
|
};
|
|
1713
1836
|
if (name === "flux-1.1-pro")
|
|
1714
1837
|
return { gpt: false, definition: { maxImagesPerCall: 1 } };
|
|
1715
|
-
throw new
|
|
1838
|
+
throw new UnsupportedModelFeatureError6(`${PREFIX} ${deployment.modelName} is not supported: use a GPT Image, FLUX.1 Kontext [pro] or FLUX1.1 [pro] deployment.`);
|
|
1716
1839
|
}
|
|
1717
1840
|
var gpt = (input, imageOutput, textOutput) => ({
|
|
1718
1841
|
card: textOutput === undefined ? { currency: "USD", input, output: imageOutput } : { currency: "USD", input, output: textOutput, imageOutput },
|
|
@@ -2013,7 +2136,7 @@ function tokenMeters2(raw) {
|
|
|
2013
2136
|
const tools = object(value);
|
|
2014
2137
|
if (!tools || Object.entries(tools).some(([k, v]) => !["web_search_requests", "web_fetch_requests"].includes(k) || v !== 0))
|
|
2015
2138
|
return false;
|
|
2016
|
-
} else if (key === "service_tier" && value === "standard") {} else
|
|
2139
|
+
} else if (key === "service_tier" && value === "standard" || key === "speed" && value === "standard" || key === "inference_geo" && value === "global") {} else
|
|
2017
2140
|
return false;
|
|
2018
2141
|
}
|
|
2019
2142
|
return true;
|
|
@@ -2022,10 +2145,11 @@ function tokenMeters2(raw) {
|
|
|
2022
2145
|
// src/messages-request.ts
|
|
2023
2146
|
import {
|
|
2024
2147
|
modelReasoningSupportIssue as modelReasoningSupportIssue3,
|
|
2025
|
-
UnsupportedModelFeatureError as
|
|
2148
|
+
UnsupportedModelFeatureError as UnsupportedModelFeatureError7
|
|
2026
2149
|
} from "@sixb/core/models";
|
|
2027
2150
|
import {
|
|
2028
2151
|
messagesInput,
|
|
2152
|
+
messagesPromptCaching,
|
|
2029
2153
|
messagesStrictTools,
|
|
2030
2154
|
StrictToolRefusals,
|
|
2031
2155
|
strictToolsKey
|
|
@@ -2140,11 +2264,11 @@ function validateMessagesOptions(options) {
|
|
|
2140
2264
|
}
|
|
2141
2265
|
}
|
|
2142
2266
|
function foundryMessagesRequest(request, definition, options, scope, unconstrained = false) {
|
|
2143
|
-
const max = prepareRequest(request, definition, options, scope, true);
|
|
2267
|
+
const { max, messages } = prepareRequest(request, definition, options, scope, true);
|
|
2144
2268
|
let conversation = false;
|
|
2145
|
-
for (const message of
|
|
2269
|
+
for (const message of messages) {
|
|
2146
2270
|
if (message.role === "system" && conversation)
|
|
2147
|
-
throw new
|
|
2271
|
+
throw new UnsupportedModelFeatureError7(`${PREFIX} Messages system instructions must precede the conversation.`);
|
|
2148
2272
|
if (message.role !== "system")
|
|
2149
2273
|
conversation = true;
|
|
2150
2274
|
}
|
|
@@ -2152,14 +2276,14 @@ function foundryMessagesRequest(request, definition, options, scope, unconstrain
|
|
|
2152
2276
|
throw new TypeError(`${PREFIX} Native Messages requires maxOutputTokens in the definition, binding, or request.`);
|
|
2153
2277
|
const caps = definition.capabilities;
|
|
2154
2278
|
if (request.tools.length && caps.localTools !== true)
|
|
2155
|
-
throw new
|
|
2279
|
+
throw new UnsupportedModelFeatureError7(`${PREFIX} Configure localTools capability before using Messages tools.`);
|
|
2156
2280
|
const schema = request.responseFormat && messagesOutputSchema(request.responseFormat.schema);
|
|
2157
2281
|
if (request.responseFormat && (caps.nativeStructuredOutput !== true || !schema))
|
|
2158
|
-
throw new
|
|
2282
|
+
throw new UnsupportedModelFeatureError7(`${PREFIX} Messages structured output requires a declared capability and a supported closed-object Claude schema.`);
|
|
2159
2283
|
const thinking = reasoningRequest(request, definition, options, max);
|
|
2160
2284
|
if (thinking.thinking && object(thinking.thinking)?.type !== "disabled" && ["temperature", "top_p", "top_k"].some((key) => options.request?.[key] !== undefined))
|
|
2161
|
-
throw new
|
|
2162
|
-
const mapped = messagesInput(
|
|
2285
|
+
throw new UnsupportedModelFeatureError7(`${PREFIX} Explicit sampling controls cannot be combined with Messages thinking.`);
|
|
2286
|
+
const mapped = messagesInput(messages, definition.providerId, PREFIX);
|
|
2163
2287
|
const output = {
|
|
2164
2288
|
...thinking.effort === undefined ? {} : { effort: thinking.effort },
|
|
2165
2289
|
...schema ? { format: { type: "json_schema", schema } } : {}
|
|
@@ -2174,7 +2298,7 @@ function foundryMessagesRequest(request, definition, options, scope, unconstrain
|
|
|
2174
2298
|
const refused = unconstrained || key !== undefined && strictToolRefusals.has(key);
|
|
2175
2299
|
const strict = chosen.map((choice) => choice && !refused);
|
|
2176
2300
|
const strictTools = refused ? undefined : key;
|
|
2177
|
-
const body = {
|
|
2301
|
+
const body = messagesPromptCaching({
|
|
2178
2302
|
...options.request,
|
|
2179
2303
|
model: definition.modelId,
|
|
2180
2304
|
messages: mapped.messages,
|
|
@@ -2190,9 +2314,12 @@ function foundryMessagesRequest(request, definition, options, scope, unconstrain
|
|
|
2190
2314
|
input_schema: tool.inputSchema,
|
|
2191
2315
|
...strict[index] ? { strict: true } : {}
|
|
2192
2316
|
})),
|
|
2193
|
-
tool_choice: {
|
|
2317
|
+
tool_choice: {
|
|
2318
|
+
type: "auto",
|
|
2319
|
+
disable_parallel_tool_use: caps.parallelToolCalls !== true
|
|
2320
|
+
}
|
|
2194
2321
|
} : {}
|
|
2195
|
-
};
|
|
2322
|
+
}, request.caching, request.tools.length);
|
|
2196
2323
|
return { body, ...strictTools === undefined ? {} : { strictTools } };
|
|
2197
2324
|
}
|
|
2198
2325
|
function reasoningRequest(request, definition, options, max) {
|
|
@@ -2202,7 +2329,7 @@ function reasoningRequest(request, definition, options, max) {
|
|
|
2202
2329
|
const caps = definition.capabilities.reasoning;
|
|
2203
2330
|
const issue = modelReasoningSupportIssue3(caps, reasoning);
|
|
2204
2331
|
if (caps === undefined || issue)
|
|
2205
|
-
throw new
|
|
2332
|
+
throw new UnsupportedModelFeatureError7(`${PREFIX} ${issue ?? "Configure reasoning capabilities for this deployment"}.`);
|
|
2206
2333
|
if (reasoning === "none") {
|
|
2207
2334
|
return {
|
|
2208
2335
|
thinking: { type: "disabled" }
|
|
@@ -2210,13 +2337,13 @@ function reasoningRequest(request, definition, options, max) {
|
|
|
2210
2337
|
}
|
|
2211
2338
|
if (typeof reasoning === "string") {
|
|
2212
2339
|
if (reasoning === "minimal" || options.thinkingMode === "manual")
|
|
2213
|
-
throw new
|
|
2340
|
+
throw new UnsupportedModelFeatureError7(`${PREFIX} Named efforts require an adaptive-thinking Claude offering.`);
|
|
2214
2341
|
return { thinking: { type: "adaptive" }, effort: reasoning };
|
|
2215
2342
|
}
|
|
2216
2343
|
if (options.thinkingMode === "adaptive")
|
|
2217
|
-
throw new
|
|
2344
|
+
throw new UnsupportedModelFeatureError7(`${PREFIX} This Claude offering only supports adaptive thinking, not manual budgets.`);
|
|
2218
2345
|
if (reasoning.budgetTokens < 1024 || reasoning.budgetTokens >= max)
|
|
2219
|
-
throw new
|
|
2346
|
+
throw new UnsupportedModelFeatureError7(`${PREFIX} Messages reasoning budget must be at least 1024 and below maxOutputTokens (${max}).`);
|
|
2220
2347
|
return { thinking: { type: "enabled", budget_tokens: reasoning.budgetTokens } };
|
|
2221
2348
|
}
|
|
2222
2349
|
|
|
@@ -2376,7 +2503,7 @@ import {
|
|
|
2376
2503
|
defineLanguageModel as defineLanguageModel2,
|
|
2377
2504
|
defineLanguageModelLabel,
|
|
2378
2505
|
defineModelRateCard as defineModelRateCard7,
|
|
2379
|
-
UnsupportedModelFeatureError as
|
|
2506
|
+
UnsupportedModelFeatureError as UnsupportedModelFeatureError8
|
|
2380
2507
|
} from "@sixb/core/models";
|
|
2381
2508
|
function resolveModel(input) {
|
|
2382
2509
|
const { deployment, options } = input;
|
|
@@ -2391,12 +2518,10 @@ function resolveModel(input) {
|
|
|
2391
2518
|
const entry = input.catalogModel;
|
|
2392
2519
|
const deploymentPublisher = deployment && azurePublisher(deployment.modelPublisher);
|
|
2393
2520
|
const publisher = deploymentPublisher?.id === "fireworks-ai" ? entry?.definition.publisher ?? deploymentPublisher : deploymentPublisher ?? entry?.definition.publisher;
|
|
2394
|
-
const protocol = input.protocol ??
|
|
2395
|
-
if (protocol === "messages" && deployment?.connectionName)
|
|
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.`);
|
|
2521
|
+
const protocol = input.protocol ?? (deployment ? publisherProtocol(deployment.modelPublisher) : "responses");
|
|
2397
2522
|
const flag = deployment?.capabilities[protocol === "chat" ? "chatCompletion" : protocol] ?? (protocol === "chat" ? deployment?.capabilities.chat_completion : undefined);
|
|
2398
2523
|
if (flag === "false")
|
|
2399
|
-
throw new
|
|
2524
|
+
throw new UnsupportedModelFeatureError8(`${PREFIX} Deployment '${input.modelId}' reports that ${protocol === "chat" ? "Chat" : protocol === "responses" ? "Responses" : "Messages"} is unsupported.`);
|
|
2400
2525
|
const definition = defineLanguageModel2({
|
|
2401
2526
|
...entry?.definition,
|
|
2402
2527
|
publisher,
|
|
@@ -2447,6 +2572,10 @@ function resolveModel(input) {
|
|
|
2447
2572
|
])
|
|
2448
2573
|
};
|
|
2449
2574
|
}
|
|
2575
|
+
function publisherProtocol(publisher) {
|
|
2576
|
+
const name = publisher.toLowerCase();
|
|
2577
|
+
return name === "openai" ? "responses" : name === "anthropic" ? "messages" : "chat";
|
|
2578
|
+
}
|
|
2450
2579
|
function effectiveDefinition(definition, protocol, options) {
|
|
2451
2580
|
const caps = definition.capabilities;
|
|
2452
2581
|
const media = caps.inputMediaTypes;
|
|
@@ -2488,7 +2617,7 @@ import {
|
|
|
2488
2617
|
defineModelRateCard as defineModelRateCard8,
|
|
2489
2618
|
rateModelCall as rateModelCall4,
|
|
2490
2619
|
SpeechModelResponseError,
|
|
2491
|
-
UnsupportedModelFeatureError as
|
|
2620
|
+
UnsupportedModelFeatureError as UnsupportedModelFeatureError9
|
|
2492
2621
|
} from "@sixb/core/models";
|
|
2493
2622
|
var FORMATS2 = {
|
|
2494
2623
|
"audio/mpeg": "mp3",
|
|
@@ -2558,7 +2687,7 @@ function speechBinding(providerId, modelId, settings, transport, deployments, re
|
|
|
2558
2687
|
const resolved = await deployments.resolve(modelId, input?.offline === true);
|
|
2559
2688
|
assertDeploymentKind(resolved.deployment, modelId, "speech");
|
|
2560
2689
|
if (!/^gpt-4o-mini-tts$/i.test(resolved.deployment.modelName))
|
|
2561
|
-
throw new
|
|
2690
|
+
throw new UnsupportedModelFeatureError9(`${PREFIX} ${resolved.deployment.modelName} is not supported: deploy gpt-4o-mini-tts, which reports its usage.`);
|
|
2562
2691
|
const rateCard = settings.costEstimator ? undefined : settings.rateCard ?? publishedRateCard2(resolved.deployment);
|
|
2563
2692
|
return speechBinding(providerId, modelId, settings, transport, deployments, {
|
|
2564
2693
|
...resolved,
|
|
@@ -2940,19 +3069,23 @@ function transcriptionEstimator(rateCard) {
|
|
|
2940
3069
|
}
|
|
2941
3070
|
|
|
2942
3071
|
// src/provider.ts
|
|
2943
|
-
function createAzureAIFoundry(options) {
|
|
2944
|
-
|
|
3072
|
+
function createAzureAIFoundry(options = {}) {
|
|
3073
|
+
if ("embeddings" in options)
|
|
3074
|
+
throw new TypeError(`${PREFIX} Remove the embeddings option: embedding deployments are called on the project's resource with its credentials.`);
|
|
3075
|
+
const endpoint = options.endpoint ?? process.env.AZURE_AI_FOUNDRY_ENDPOINT;
|
|
3076
|
+
if (!endpoint)
|
|
3077
|
+
throw new TypeError(`${PREFIX} endpoint is required: pass the project endpoint or set AZURE_AI_FOUNDRY_ENDPOINT.`);
|
|
3078
|
+
const transport = new FoundryTransport({
|
|
3079
|
+
...options,
|
|
3080
|
+
endpoint,
|
|
3081
|
+
...options.credential || options.apiKey !== undefined ? {} : { apiKey: () => process.env.AZURE_AI_FOUNDRY_API_KEY }
|
|
3082
|
+
});
|
|
2945
3083
|
const providerId = options.providerId ?? "azure-ai-foundry";
|
|
2946
3084
|
if (!providerId.trim())
|
|
2947
3085
|
throw new TypeError(`${PREFIX} providerId must not be empty.`);
|
|
2948
3086
|
const deployments = new FoundryDeployments(providerId, transport, options.discovery);
|
|
2949
3087
|
const modelCatalog = new RemoteModelsDevCatalog(options.catalog);
|
|
2950
|
-
const
|
|
2951
|
-
const embedding = (name, input) => {
|
|
2952
|
-
if (!embeddingTransport)
|
|
2953
|
-
throw new TypeError(`${PREFIX} Configure embeddings.endpoint and embeddings.apiKey before declaring an embedding model.`);
|
|
2954
|
-
return createFoundryEmbedding(providerId, name, input, embeddingTransport, deployments, modelCatalog);
|
|
2955
|
-
};
|
|
3088
|
+
const embedding = (name, input) => createFoundryEmbedding(providerId, name, input, transport, deployments, modelCatalog);
|
|
2956
3089
|
const model = (protocol, deploymentName, modelOptions) => {
|
|
2957
3090
|
if (!deploymentName.trim() || deploymentName !== deploymentName.trim())
|
|
2958
3091
|
throw new TypeError(`${PREFIX} deploymentName must be nonempty without surrounding whitespace.`);
|
|
@@ -2974,7 +3107,7 @@ function createAzureAIFoundry(options) {
|
|
|
2974
3107
|
const results = [];
|
|
2975
3108
|
for (const deployment of records) {
|
|
2976
3109
|
const name = deployment.name;
|
|
2977
|
-
if (deploymentKind(deployment) !== "language" || await modelCatalog.getEmbedding(deployment.modelName, true))
|
|
3110
|
+
if (deployment.connectionName || deploymentKind(deployment) !== "language" || await modelCatalog.getEmbedding(deployment.modelName, true))
|
|
2978
3111
|
continue;
|
|
2979
3112
|
try {
|
|
2980
3113
|
const profile = resolveModel({
|
|
@@ -2986,11 +3119,12 @@ function createAzureAIFoundry(options) {
|
|
|
2986
3119
|
catalogModel: lookup?.(deployment.modelName)
|
|
2987
3120
|
});
|
|
2988
3121
|
const flags = deployment.capabilities;
|
|
2989
|
-
const
|
|
3122
|
+
const offers = (offered) => offered === "chat" ? flags.chatCompletion === "true" || flags.chat_completion === "true" : flags[offered] === "true";
|
|
3123
|
+
const supported = profile.metadata.catalog || (protocol ? offers(protocol) : ["responses", "chat", "messages"].some(offers));
|
|
2990
3124
|
if (supported)
|
|
2991
3125
|
results.push(profile.definition);
|
|
2992
3126
|
} catch (error) {
|
|
2993
|
-
if (!(error instanceof
|
|
3127
|
+
if (!(error instanceof UnsupportedModelFeatureError10))
|
|
2994
3128
|
throw error;
|
|
2995
3129
|
}
|
|
2996
3130
|
}
|
|
@@ -3090,11 +3224,7 @@ class FoundryModel {
|
|
|
3090
3224
|
...structuredClone(serializable),
|
|
3091
3225
|
...costEstimator ? { costEstimator } : {}
|
|
3092
3226
|
};
|
|
3093
|
-
this.scope = JSON.stringify([
|
|
3094
|
-
protocol === "messages" ? transport.url(protocol) : transport.baseUrl,
|
|
3095
|
-
this.protocol,
|
|
3096
|
-
modelId
|
|
3097
|
-
]);
|
|
3227
|
+
this.scope = JSON.stringify([transport.url(protocol), protocol, modelId]);
|
|
3098
3228
|
this.reliableReasoning = options.reasoningUsage === "unknown" ? false : options.reasoningUsage === "reported" || this.metadata.publisher?.toLowerCase() === "openai" ? true : undefined;
|
|
3099
3229
|
this.estimator = options.costEstimator ?? (protocol === "messages" ? foundryMessagesEstimator : protocol === "chat" ? foundryChatEstimator : foundryEstimator)(this.profile.rateCard, this.options.request, this.metadata.modelName, this.metadata.modelVersion, this.profile.aliases);
|
|
3100
3230
|
Object.freeze(this);
|
|
@@ -3105,7 +3235,7 @@ class FoundryModel {
|
|
|
3105
3235
|
const resolution = await this.deployments.resolve(this.modelId, options?.offline === true);
|
|
3106
3236
|
const kind = deploymentKind(resolution.deployment);
|
|
3107
3237
|
if (kind !== "language")
|
|
3108
|
-
throw new
|
|
3238
|
+
throw new UnsupportedModelFeatureError10(`${PREFIX} Use ${KIND_BINDINGS[kind]} for ${article(kind)} ${kind} deployment.`);
|
|
3109
3239
|
let catalogModel;
|
|
3110
3240
|
try {
|
|
3111
3241
|
catalogModel = await this.modelCatalog.get(resolution.deployment.modelName, options?.offline);
|
|
@@ -3117,7 +3247,7 @@ class FoundryModel {
|
|
|
3117
3247
|
throw error;
|
|
3118
3248
|
}
|
|
3119
3249
|
if (await this.modelCatalog.getEmbedding(resolution.deployment.modelName, true))
|
|
3120
|
-
throw new
|
|
3250
|
+
throw new UnsupportedModelFeatureError10(`${PREFIX} Use foundry.embedding() for an embedding deployment.`);
|
|
3121
3251
|
return new FoundryModel(this.requestedProtocol, this.transport, this.deployments, this.providerId, this.modelId, this.options, this.modelCatalog, {
|
|
3122
3252
|
...resolution,
|
|
3123
3253
|
catalogModel
|
|
@@ -3136,7 +3266,7 @@ class FoundryModel {
|
|
|
3136
3266
|
const model = await abortable(() => this.#pendingExecution, request.signal);
|
|
3137
3267
|
return model.stream(request);
|
|
3138
3268
|
}
|
|
3139
|
-
const response = this.protocol === "messages" ? await this.postMessages(request) : await this.
|
|
3269
|
+
const response = this.protocol === "messages" ? await this.postMessages(request) : await this.postOpenAICompatible(request);
|
|
3140
3270
|
const id = this.transport.redactText(response, requestId(response));
|
|
3141
3271
|
if (!response.body)
|
|
3142
3272
|
throw new ModelProviderError7(`${PREFIX} Provider returned an empty streaming response.`, this.providerId, this.modelId, { requestId: id, status: response.status });
|
|
@@ -3174,6 +3304,18 @@ class FoundryModel {
|
|
|
3174
3304
|
}), response)
|
|
3175
3305
|
};
|
|
3176
3306
|
}
|
|
3307
|
+
async postOpenAICompatible(request) {
|
|
3308
|
+
const deployment = this.resolution?.deployment;
|
|
3309
|
+
for (let attempt = 0;; attempt++) {
|
|
3310
|
+
const body = this.protocol === "chat" ? foundryChatRequest(request, this.definition, this.options, this.scope, deployment) : responsesRequest(request, this.definition, this.options, this.scope, deployment);
|
|
3311
|
+
try {
|
|
3312
|
+
return await this.transport.post(JSON.stringify(body), request.signal, this.providerId, this.modelId, this.protocol);
|
|
3313
|
+
} catch (error) {
|
|
3314
|
+
if (attempt >= 3 || !(error instanceof ModelProviderError7) || !refusedCaching(error, body, this.options.request, this.scope))
|
|
3315
|
+
throw error;
|
|
3316
|
+
}
|
|
3317
|
+
}
|
|
3318
|
+
}
|
|
3177
3319
|
async postMessages(request) {
|
|
3178
3320
|
const prepared = foundryMessagesRequest(request, this.definition, this.options, this.scope);
|
|
3179
3321
|
const post = (body) => this.transport.post(JSON.stringify(body), request.signal, this.providerId, this.modelId, "messages");
|
|
@@ -3216,4 +3358,4 @@ export {
|
|
|
3216
3358
|
createAzureAIFoundry
|
|
3217
3359
|
};
|
|
3218
3360
|
|
|
3219
|
-
//# debugId=
|
|
3361
|
+
//# debugId=31DEE47D5A3829A864756E2164756E21
|