@sixb/azure-ai-foundry 0.1.4 → 0.1.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 +3 -2
- package/dist/accounting.d.ts +7 -3
- package/dist/accounting.d.ts.map +1 -1
- package/dist/catalog.d.ts +3 -3
- package/dist/catalog.d.ts.map +1 -1
- package/dist/chat-accounting.d.ts +3 -2
- package/dist/chat-accounting.d.ts.map +1 -1
- package/dist/embedding.d.ts +2 -2
- package/dist/embedding.d.ts.map +1 -1
- package/dist/index.js +243 -329
- package/dist/index.js.map +12 -12
- package/dist/messages-accounting.d.ts +3 -2
- package/dist/messages-accounting.d.ts.map +1 -1
- package/dist/provider.d.ts +7 -7
- package/dist/provider.d.ts.map +1 -1
- package/dist/resolution.d.ts +1 -1
- package/dist/resolution.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/accounting.ts +9 -5
- package/src/catalog.ts +7 -124
- package/src/chat-accounting.ts +5 -9
- package/src/embedding.ts +2 -2
- package/src/messages-accounting.ts +4 -9
- package/src/provider.ts +16 -9
- package/src/resolution.ts +4 -0
package/dist/index.js
CHANGED
|
@@ -57,6 +57,7 @@ function tokenEstimator(card, request, requestKeys, tokenMeters, modelName, mode
|
|
|
57
57
|
const rateCard = card && defineModelRateCard(card);
|
|
58
58
|
const fixed = Object.keys(request ?? {}).every((key) => requestKeys.includes(key));
|
|
59
59
|
return {
|
|
60
|
+
...fixed && rateCard ? { rateCard } : {},
|
|
60
61
|
estimateReservation: (tokens) => fixed ? estimateModelReservation({ ...tokens, rateCard }) : undefined,
|
|
61
62
|
estimate: ({ usage, responseModelId, route }) => {
|
|
62
63
|
const actual = route?.modelId ?? responseModelId;
|
|
@@ -87,6 +88,7 @@ import {
|
|
|
87
88
|
MODEL_REASONING_EFFORTS,
|
|
88
89
|
ModelCatalogUnavailableError
|
|
89
90
|
} from "@sixb/core/models";
|
|
91
|
+
import { downloadModelsDev, modelsDevRateCard } from "@sixb/model-protocols/models-dev";
|
|
90
92
|
|
|
91
93
|
// src/publisher.ts
|
|
92
94
|
var publishers = {
|
|
@@ -119,236 +121,7 @@ function fireworksPublisher(family) {
|
|
|
119
121
|
return;
|
|
120
122
|
}
|
|
121
123
|
|
|
122
|
-
// src/transport.ts
|
|
123
|
-
import { ModelProviderError as ModelProviderError2 } from "@sixb/core/models";
|
|
124
|
-
|
|
125
|
-
// src/diagnostics.ts
|
|
126
|
-
import { ModelProviderError } from "@sixb/core/models";
|
|
127
|
-
|
|
128
|
-
class RequestDiagnostics {
|
|
129
|
-
secrets = new Set;
|
|
130
|
-
add(value) {
|
|
131
|
-
if (value.trim())
|
|
132
|
-
this.secrets.add(value.trim());
|
|
133
|
-
}
|
|
134
|
-
text(value) {
|
|
135
|
-
for (const secret of [...this.secrets].sort((a, b) => b.length - a.length))
|
|
136
|
-
value = value.replaceAll(secret, "[REDACTED]");
|
|
137
|
-
return value;
|
|
138
|
-
}
|
|
139
|
-
providerError(error) {
|
|
140
|
-
return new ModelProviderError(this.text(error.message), error.providerId, error.modelId, {
|
|
141
|
-
status: error.status,
|
|
142
|
-
retryable: error.retryable,
|
|
143
|
-
retryAfterMs: error.retryAfterMs,
|
|
144
|
-
code: error.code === undefined ? undefined : this.text(error.code),
|
|
145
|
-
requestId: error.requestId === undefined ? undefined : this.text(error.requestId)
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
failure(error) {
|
|
149
|
-
if (error instanceof ModelProviderError)
|
|
150
|
-
return this.providerError(error);
|
|
151
|
-
const sanitized = new Error(this.text(error instanceof Error ? error.message : String(error)));
|
|
152
|
-
sanitized.name = error instanceof Error ? this.text(error.name) : "Error";
|
|
153
|
-
return sanitized;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// src/transport.ts
|
|
158
|
-
class FoundryTransport {
|
|
159
|
-
diagnostics = new WeakMap;
|
|
160
|
-
baseUrl;
|
|
161
|
-
projectUrl;
|
|
162
|
-
options;
|
|
163
|
-
constructor(options, target = "project") {
|
|
164
|
-
if (!URL.canParse(options.endpoint))
|
|
165
|
-
throw new TypeError(`${PREFIX} endpoint must be a valid ${target} URL.`);
|
|
166
|
-
const url = new URL(options.endpoint);
|
|
167
|
-
if (!["https:", "http:"].includes(url.protocol) || url.username || url.password || url.search || url.hash) {
|
|
168
|
-
throw new TypeError(`${PREFIX} endpoint must be an HTTP(S) ${target} URL without credentials, query, or fragment.`);
|
|
169
|
-
}
|
|
170
|
-
const path = url.pathname.replace(/\/+$/, "");
|
|
171
|
-
if (target === "project" && !/^\/api\/projects\/[^/]+$/.test(path))
|
|
172
|
-
throw new TypeError(`${PREFIX} endpoint must be a Foundry project URL: https://<resource>.services.ai.azure.com/api/projects/<project>.`);
|
|
173
|
-
if (target === "resource" && path !== "" && path !== "/openai/v1")
|
|
174
|
-
throw new TypeError(`${PREFIX} embeddings.endpoint must be a resource origin or /openai/v1 URL.`);
|
|
175
|
-
this.projectUrl = `${url.origin}${path}`;
|
|
176
|
-
this.baseUrl = target === "project" ? `${this.projectUrl}/openai/v1` : `${url.origin}/openai/v1`;
|
|
177
|
-
if (typeof options.apiKey !== "function" && (typeof options.apiKey !== "string" || !options.apiKey.trim()))
|
|
178
|
-
throw new TypeError(`${PREFIX} apiKey is required.`);
|
|
179
|
-
if (options.maxRetries !== undefined && (!Number.isSafeInteger(options.maxRetries) || options.maxRetries < 0)) {
|
|
180
|
-
throw new TypeError(`${PREFIX} maxRetries must be a nonnegative safe integer.`);
|
|
181
|
-
}
|
|
182
|
-
positiveInteger(options.maxRetryDelayMs, "maxRetryDelayMs");
|
|
183
|
-
this.options = {
|
|
184
|
-
...options,
|
|
185
|
-
headers: typeof options.headers === "function" ? options.headers : { ...options.headers }
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
async post(body, signal, providerId, modelId, protocol = "responses") {
|
|
189
|
-
const url = this.url(protocol);
|
|
190
|
-
const diagnostics = new RequestDiagnostics;
|
|
191
|
-
for (let attempt = 0;; attempt++) {
|
|
192
|
-
signal.throwIfAborted();
|
|
193
|
-
const headers = await this.headers(signal, diagnostics, protocol === "messages" ? "x-api-key" : "api-key");
|
|
194
|
-
headers.set("content-type", "application/json");
|
|
195
|
-
headers.set("accept", protocol === "embeddings" ? "application/json" : "text/event-stream");
|
|
196
|
-
if (protocol === "messages")
|
|
197
|
-
headers.set("anthropic-version", "2023-06-01");
|
|
198
|
-
signal.throwIfAborted();
|
|
199
|
-
const response = await abortable(async () => {
|
|
200
|
-
const result = await this.fetch(url, {
|
|
201
|
-
method: "POST",
|
|
202
|
-
redirect: "error",
|
|
203
|
-
headers,
|
|
204
|
-
body,
|
|
205
|
-
signal
|
|
206
|
-
});
|
|
207
|
-
if (signal.aborted)
|
|
208
|
-
result.body?.cancel(signal.reason).catch(() => {});
|
|
209
|
-
return result;
|
|
210
|
-
}, signal).catch((error) => {
|
|
211
|
-
if (signal.aborted)
|
|
212
|
-
throw signal.reason;
|
|
213
|
-
throw diagnostics.failure(error);
|
|
214
|
-
});
|
|
215
|
-
this.diagnostics.set(response, diagnostics);
|
|
216
|
-
if (signal.aborted) {
|
|
217
|
-
response.body?.cancel(signal.reason).catch(() => {});
|
|
218
|
-
signal.throwIfAborted();
|
|
219
|
-
}
|
|
220
|
-
if (response.ok)
|
|
221
|
-
return response;
|
|
222
|
-
const error = this.redactError(response, await httpError(response, providerId, modelId, signal));
|
|
223
|
-
if (protocol === "embeddings" || !error.retryable || attempt >= (this.options.maxRetries ?? 2))
|
|
224
|
-
throw error;
|
|
225
|
-
await wait(Math.min(error.retryAfterMs ?? 250 * 2 ** attempt, this.options.maxRetryDelayMs ?? 60000), signal);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
redactText(response, value) {
|
|
229
|
-
return value === undefined ? undefined : this.diagnostics.get(response)?.text(value) ?? value;
|
|
230
|
-
}
|
|
231
|
-
redactError(response, error) {
|
|
232
|
-
return this.diagnostics.get(response).providerError(error);
|
|
233
|
-
}
|
|
234
|
-
redactFailure(response, error) {
|
|
235
|
-
return this.diagnostics.get(response).failure(error);
|
|
236
|
-
}
|
|
237
|
-
async headers(signal, diagnostics, keyHeader = "api-key") {
|
|
238
|
-
const headers = new Headers(await resolve(this.options.headers, signal));
|
|
239
|
-
for (const value of headers.values())
|
|
240
|
-
diagnostics.add(value);
|
|
241
|
-
for (const reserved of ["authorization", "api-key", "x-api-key"])
|
|
242
|
-
if (headers.has(reserved))
|
|
243
|
-
throw new TypeError(`${PREFIX} Use apiKey instead of '${reserved}' headers.`);
|
|
244
|
-
const key = await resolve(this.options.apiKey, signal);
|
|
245
|
-
if (typeof key !== "string" || !key.trim())
|
|
246
|
-
throw new TypeError(`${PREFIX} apiKey must resolve to a nonempty string.`);
|
|
247
|
-
diagnostics.add(key);
|
|
248
|
-
headers.set(keyHeader, key);
|
|
249
|
-
return headers;
|
|
250
|
-
}
|
|
251
|
-
fetch(url, init) {
|
|
252
|
-
return (this.options.fetch ?? fetch)(url, init);
|
|
253
|
-
}
|
|
254
|
-
url(protocol) {
|
|
255
|
-
if (protocol === "embeddings")
|
|
256
|
-
return `${this.baseUrl}/embeddings`;
|
|
257
|
-
if (protocol === "responses")
|
|
258
|
-
return `${this.baseUrl}/responses`;
|
|
259
|
-
if (protocol === "chat")
|
|
260
|
-
return `${this.baseUrl}/chat/completions`;
|
|
261
|
-
return `${new URL(this.baseUrl).origin}/anthropic/v1/messages`;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
function requestId(response) {
|
|
265
|
-
return response.headers.get("apim-request-id") ?? response.headers.get("x-request-id") ?? response.headers.get("request-id") ?? undefined;
|
|
266
|
-
}
|
|
267
|
-
async function resolve(source, signal) {
|
|
268
|
-
signal.throwIfAborted();
|
|
269
|
-
if (typeof source !== "function")
|
|
270
|
-
return source;
|
|
271
|
-
return abortable(() => source(signal), signal);
|
|
272
|
-
}
|
|
273
|
-
function abortable(operation, signal) {
|
|
274
|
-
return new Promise((resolve, reject) => {
|
|
275
|
-
signal.throwIfAborted();
|
|
276
|
-
const abort = () => {
|
|
277
|
-
signal.removeEventListener("abort", abort);
|
|
278
|
-
reject(signal.reason);
|
|
279
|
-
};
|
|
280
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
281
|
-
Promise.resolve().then(() => {
|
|
282
|
-
signal.throwIfAborted();
|
|
283
|
-
return operation();
|
|
284
|
-
}).then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
async function httpError(response, providerId, modelId, signal) {
|
|
288
|
-
let message = `Provider request failed with HTTP ${response.status}.`;
|
|
289
|
-
let code;
|
|
290
|
-
const reader = response.body?.getReader();
|
|
291
|
-
if (reader) {
|
|
292
|
-
const cancel = () => {
|
|
293
|
-
reader.cancel(signal.reason).catch(() => {});
|
|
294
|
-
};
|
|
295
|
-
signal.addEventListener("abort", cancel, { once: true });
|
|
296
|
-
const chunks = [];
|
|
297
|
-
let length = 0;
|
|
298
|
-
try {
|
|
299
|
-
while (length < 8192) {
|
|
300
|
-
signal.throwIfAborted();
|
|
301
|
-
const { value, done } = await abortable(() => reader.read(), signal);
|
|
302
|
-
if (done)
|
|
303
|
-
break;
|
|
304
|
-
const part = value.subarray(0, 8192 - length);
|
|
305
|
-
chunks.push(part);
|
|
306
|
-
length += part.length;
|
|
307
|
-
}
|
|
308
|
-
const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
309
|
-
const error = object(object(parsed)?.error);
|
|
310
|
-
if (typeof error?.message === "string")
|
|
311
|
-
message = error.message;
|
|
312
|
-
if (typeof error?.code === "string")
|
|
313
|
-
code = error.code;
|
|
314
|
-
else if (typeof error?.type === "string")
|
|
315
|
-
code = error.type;
|
|
316
|
-
} catch {} finally {
|
|
317
|
-
signal.removeEventListener("abort", cancel);
|
|
318
|
-
reader.cancel().catch(() => {});
|
|
319
|
-
reader.releaseLock();
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
signal.throwIfAborted();
|
|
323
|
-
const delay = response.headers.get("retry-after-ms") ?? response.headers.get("x-ms-retry-after-ms");
|
|
324
|
-
const retry = response.headers.get("retry-after");
|
|
325
|
-
const ms = delay !== null ? Number(delay) : retry !== null ? Number.isFinite(Number(retry)) ? Number(retry) * 1000 : Date.parse(retry) - Date.now() : undefined;
|
|
326
|
-
return new ModelProviderError2(`${PREFIX} ${message}`, providerId, modelId, {
|
|
327
|
-
status: response.status,
|
|
328
|
-
requestId: requestId(response),
|
|
329
|
-
code,
|
|
330
|
-
retryable: response.status === 408 || response.status === 429 || response.status >= 500,
|
|
331
|
-
...ms !== undefined && Number.isFinite(ms) && ms >= 0 ? { retryAfterMs: Math.ceil(ms) } : {}
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
async function wait(ms, signal) {
|
|
335
|
-
signal.throwIfAborted();
|
|
336
|
-
await new Promise((resolve, reject) => {
|
|
337
|
-
const abort = () => {
|
|
338
|
-
clearTimeout(timer);
|
|
339
|
-
reject(signal.reason);
|
|
340
|
-
};
|
|
341
|
-
const timer = setTimeout(() => {
|
|
342
|
-
signal.removeEventListener("abort", abort);
|
|
343
|
-
resolve();
|
|
344
|
-
}, ms);
|
|
345
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
|
|
349
124
|
// src/catalog.ts
|
|
350
|
-
var url = "https://models.dev/api.json";
|
|
351
|
-
|
|
352
125
|
class RemoteModelsDevCatalog {
|
|
353
126
|
snapshot = [];
|
|
354
127
|
loadedAt = 0;
|
|
@@ -405,30 +178,9 @@ class RemoteModelsDevCatalog {
|
|
|
405
178
|
return this.pending;
|
|
406
179
|
}
|
|
407
180
|
async fetchModels() {
|
|
408
|
-
const signal = AbortSignal.timeout(this.options.timeoutMs ?? 1e4);
|
|
409
181
|
let text;
|
|
410
182
|
try {
|
|
411
|
-
|
|
412
|
-
if (!response.ok || !response.body)
|
|
413
|
-
throw new Error(`HTTP ${response.status}`);
|
|
414
|
-
const reader = response.body.getReader();
|
|
415
|
-
const chunks = [];
|
|
416
|
-
let bytes = 0;
|
|
417
|
-
try {
|
|
418
|
-
for (;; ) {
|
|
419
|
-
const chunk = await abortable(() => reader.read(), signal);
|
|
420
|
-
if (chunk.done)
|
|
421
|
-
break;
|
|
422
|
-
bytes += chunk.value.length;
|
|
423
|
-
if (bytes > 32 * 1024 * 1024)
|
|
424
|
-
throw new Error("Catalog exceeds 32 MiB");
|
|
425
|
-
chunks.push(chunk.value);
|
|
426
|
-
}
|
|
427
|
-
text = Buffer.concat(chunks).toString();
|
|
428
|
-
} finally {
|
|
429
|
-
reader.cancel().catch(() => {});
|
|
430
|
-
reader.releaseLock();
|
|
431
|
-
}
|
|
183
|
+
text = await downloadModelsDev(this.options);
|
|
432
184
|
} catch (cause) {
|
|
433
185
|
throw new ModelCatalogUnavailableError(`${PREFIX} models.dev catalog unavailable.`, { cause });
|
|
434
186
|
}
|
|
@@ -522,88 +274,12 @@ function reasoning(supported, value) {
|
|
|
522
274
|
};
|
|
523
275
|
}
|
|
524
276
|
function rates(raw, protocol) {
|
|
525
|
-
const card =
|
|
277
|
+
const card = modelsDevRateCard(raw);
|
|
526
278
|
if (protocol !== "messages" || card?.cacheWriteInput === undefined)
|
|
527
279
|
return card;
|
|
528
280
|
const { cacheWriteInput, ...rest } = card;
|
|
529
281
|
return defineModelRateCard2({ ...rest, cacheWriteInput5m: cacheWriteInput });
|
|
530
282
|
}
|
|
531
|
-
function parseRates(raw) {
|
|
532
|
-
if (!raw || raw.tiers === undefined)
|
|
533
|
-
return flatRates(raw);
|
|
534
|
-
const { tiers, context_over_200k: legacy, ...base } = raw;
|
|
535
|
-
const card = flatRates(base);
|
|
536
|
-
if (!card || !Array.isArray(tiers))
|
|
537
|
-
return;
|
|
538
|
-
const parsed = [];
|
|
539
|
-
for (const value of tiers) {
|
|
540
|
-
const entry = object(value);
|
|
541
|
-
if (!entry)
|
|
542
|
-
return;
|
|
543
|
-
const { tier, ...prices } = entry;
|
|
544
|
-
const threshold = object(tier);
|
|
545
|
-
const size = threshold?.size;
|
|
546
|
-
const tierCard = flatRates(prices);
|
|
547
|
-
if (!tierCard || !threshold || Object.keys(threshold).some((k) => k !== "type" && k !== "size") || threshold.type !== undefined && threshold.type !== "context" || typeof size !== "number" || !Number.isSafeInteger(size) || size < 0 || size >= Number.MAX_SAFE_INTEGER)
|
|
548
|
-
return;
|
|
549
|
-
parsed.push({ size, card: tierCard });
|
|
550
|
-
}
|
|
551
|
-
parsed.sort((a, b) => a.size - b.size);
|
|
552
|
-
if (new Set(parsed.map((t) => t.size)).size !== parsed.length)
|
|
553
|
-
return;
|
|
554
|
-
const keys = ["input", "output", "cacheReadInput", "cacheWriteInput"];
|
|
555
|
-
if (legacy !== undefined) {
|
|
556
|
-
const mirror = flatRates(object(legacy));
|
|
557
|
-
if (!mirror || parsed.length !== 1 || parsed[0].size < 200000 || keys.some((k) => mirror[k] !== parsed[0].card[k]))
|
|
558
|
-
return;
|
|
559
|
-
}
|
|
560
|
-
if (parsed.some((t) => keys.some((k) => card[k] === undefined !== (t.card[k] === undefined))))
|
|
561
|
-
return;
|
|
562
|
-
const price = (key) => {
|
|
563
|
-
const basePrice = card[key];
|
|
564
|
-
if (basePrice === undefined || !parsed.length)
|
|
565
|
-
return basePrice;
|
|
566
|
-
if (typeof basePrice !== "string")
|
|
567
|
-
return;
|
|
568
|
-
return {
|
|
569
|
-
default: basePrice,
|
|
570
|
-
tiers: parsed.map((t, i) => ({
|
|
571
|
-
minTokens: t.size + 1,
|
|
572
|
-
...parsed[i + 1] ? { maxTokens: parsed[i + 1].size + 1 } : {},
|
|
573
|
-
price: String(t.card[key])
|
|
574
|
-
}))
|
|
575
|
-
};
|
|
576
|
-
};
|
|
577
|
-
return defineModelRateCard2({
|
|
578
|
-
currency: "USD",
|
|
579
|
-
unit: "million-tokens",
|
|
580
|
-
input: price("input"),
|
|
581
|
-
output: price("output"),
|
|
582
|
-
...card.cacheReadInput === undefined ? {} : { cacheReadInput: price("cacheReadInput") },
|
|
583
|
-
...card.cacheWriteInput === undefined ? {} : { cacheWriteInput: price("cacheWriteInput") }
|
|
584
|
-
});
|
|
585
|
-
}
|
|
586
|
-
function flatRates(raw) {
|
|
587
|
-
if (!raw)
|
|
588
|
-
return;
|
|
589
|
-
if (Object.keys(raw).some((k) => !["input", "output", "cache_read", "cache_write"].includes(k)))
|
|
590
|
-
return;
|
|
591
|
-
const decimal = (v) => (typeof v === "number" || typeof v === "string") && /^\d+(\.\d+)?$/.test(String(v)) ? String(v) : undefined;
|
|
592
|
-
const input = decimal(raw.input), output = decimal(raw.output);
|
|
593
|
-
if (input === undefined || output === undefined)
|
|
594
|
-
return;
|
|
595
|
-
const read = decimal(raw.cache_read), write = decimal(raw.cache_write);
|
|
596
|
-
if (raw.cache_read !== undefined && read === undefined || raw.cache_write !== undefined && write === undefined)
|
|
597
|
-
return;
|
|
598
|
-
return defineModelRateCard2({
|
|
599
|
-
currency: "USD",
|
|
600
|
-
unit: "million-tokens",
|
|
601
|
-
input,
|
|
602
|
-
output,
|
|
603
|
-
...read === undefined ? {} : { cacheReadInput: read },
|
|
604
|
-
...write === undefined ? {} : { cacheWriteInput: write }
|
|
605
|
-
});
|
|
606
|
-
}
|
|
607
283
|
|
|
608
284
|
// src/chat-accounting.ts
|
|
609
285
|
function foundryChatUsage(raw, reliableReasoning) {
|
|
@@ -627,6 +303,7 @@ function foundryChatUsage(raw, reliableReasoning) {
|
|
|
627
303
|
function foundryChatEstimator(card, request, modelName, modelVersion, aliases) {
|
|
628
304
|
const estimator = tokenEstimator(card, request, ["temperature", "top_p", "metadata"], tokenMeters, modelName, modelVersion, aliases);
|
|
629
305
|
return {
|
|
306
|
+
...estimator.rateCard ? { rateCard: estimator.rateCard } : {},
|
|
630
307
|
estimateReservation: estimator.estimateReservation,
|
|
631
308
|
estimate: (input) => estimator.estimate({
|
|
632
309
|
...input,
|
|
@@ -1078,6 +755,233 @@ import {
|
|
|
1078
755
|
ModelCatalogUnavailableError as ModelCatalogUnavailableError2,
|
|
1079
756
|
ModelProviderError as ModelProviderError3
|
|
1080
757
|
} from "@sixb/core/models";
|
|
758
|
+
|
|
759
|
+
// src/diagnostics.ts
|
|
760
|
+
import { ModelProviderError } from "@sixb/core/models";
|
|
761
|
+
|
|
762
|
+
class RequestDiagnostics {
|
|
763
|
+
secrets = new Set;
|
|
764
|
+
add(value) {
|
|
765
|
+
if (value.trim())
|
|
766
|
+
this.secrets.add(value.trim());
|
|
767
|
+
}
|
|
768
|
+
text(value) {
|
|
769
|
+
for (const secret of [...this.secrets].sort((a, b) => b.length - a.length))
|
|
770
|
+
value = value.replaceAll(secret, "[REDACTED]");
|
|
771
|
+
return value;
|
|
772
|
+
}
|
|
773
|
+
providerError(error) {
|
|
774
|
+
return new ModelProviderError(this.text(error.message), error.providerId, error.modelId, {
|
|
775
|
+
status: error.status,
|
|
776
|
+
retryable: error.retryable,
|
|
777
|
+
retryAfterMs: error.retryAfterMs,
|
|
778
|
+
code: error.code === undefined ? undefined : this.text(error.code),
|
|
779
|
+
requestId: error.requestId === undefined ? undefined : this.text(error.requestId)
|
|
780
|
+
});
|
|
781
|
+
}
|
|
782
|
+
failure(error) {
|
|
783
|
+
if (error instanceof ModelProviderError)
|
|
784
|
+
return this.providerError(error);
|
|
785
|
+
const sanitized = new Error(this.text(error instanceof Error ? error.message : String(error)));
|
|
786
|
+
sanitized.name = error instanceof Error ? this.text(error.name) : "Error";
|
|
787
|
+
return sanitized;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
// src/transport.ts
|
|
792
|
+
import { ModelProviderError as ModelProviderError2 } from "@sixb/core/models";
|
|
793
|
+
class FoundryTransport {
|
|
794
|
+
diagnostics = new WeakMap;
|
|
795
|
+
baseUrl;
|
|
796
|
+
projectUrl;
|
|
797
|
+
options;
|
|
798
|
+
constructor(options, target = "project") {
|
|
799
|
+
if (!URL.canParse(options.endpoint))
|
|
800
|
+
throw new TypeError(`${PREFIX} endpoint must be a valid ${target} URL.`);
|
|
801
|
+
const url = new URL(options.endpoint);
|
|
802
|
+
if (!["https:", "http:"].includes(url.protocol) || url.username || url.password || url.search || url.hash) {
|
|
803
|
+
throw new TypeError(`${PREFIX} endpoint must be an HTTP(S) ${target} URL without credentials, query, or fragment.`);
|
|
804
|
+
}
|
|
805
|
+
const path = url.pathname.replace(/\/+$/, "");
|
|
806
|
+
if (target === "project" && !/^\/api\/projects\/[^/]+$/.test(path))
|
|
807
|
+
throw new TypeError(`${PREFIX} endpoint must be a Foundry project URL: https://<resource>.services.ai.azure.com/api/projects/<project>.`);
|
|
808
|
+
if (target === "resource" && path !== "" && path !== "/openai/v1")
|
|
809
|
+
throw new TypeError(`${PREFIX} embeddings.endpoint must be a resource origin or /openai/v1 URL.`);
|
|
810
|
+
this.projectUrl = `${url.origin}${path}`;
|
|
811
|
+
this.baseUrl = target === "project" ? `${this.projectUrl}/openai/v1` : `${url.origin}/openai/v1`;
|
|
812
|
+
if (typeof options.apiKey !== "function" && (typeof options.apiKey !== "string" || !options.apiKey.trim()))
|
|
813
|
+
throw new TypeError(`${PREFIX} apiKey is required.`);
|
|
814
|
+
if (options.maxRetries !== undefined && (!Number.isSafeInteger(options.maxRetries) || options.maxRetries < 0)) {
|
|
815
|
+
throw new TypeError(`${PREFIX} maxRetries must be a nonnegative safe integer.`);
|
|
816
|
+
}
|
|
817
|
+
positiveInteger(options.maxRetryDelayMs, "maxRetryDelayMs");
|
|
818
|
+
this.options = {
|
|
819
|
+
...options,
|
|
820
|
+
headers: typeof options.headers === "function" ? options.headers : { ...options.headers }
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
async post(body, signal, providerId, modelId, protocol = "responses") {
|
|
824
|
+
const url = this.url(protocol);
|
|
825
|
+
const diagnostics = new RequestDiagnostics;
|
|
826
|
+
for (let attempt = 0;; attempt++) {
|
|
827
|
+
signal.throwIfAborted();
|
|
828
|
+
const headers = await this.headers(signal, diagnostics, protocol === "messages" ? "x-api-key" : "api-key");
|
|
829
|
+
headers.set("content-type", "application/json");
|
|
830
|
+
headers.set("accept", protocol === "embeddings" ? "application/json" : "text/event-stream");
|
|
831
|
+
if (protocol === "messages")
|
|
832
|
+
headers.set("anthropic-version", "2023-06-01");
|
|
833
|
+
signal.throwIfAborted();
|
|
834
|
+
const response = await abortable(async () => {
|
|
835
|
+
const result = await this.fetch(url, {
|
|
836
|
+
method: "POST",
|
|
837
|
+
redirect: "error",
|
|
838
|
+
headers,
|
|
839
|
+
body,
|
|
840
|
+
signal
|
|
841
|
+
});
|
|
842
|
+
if (signal.aborted)
|
|
843
|
+
result.body?.cancel(signal.reason).catch(() => {});
|
|
844
|
+
return result;
|
|
845
|
+
}, signal).catch((error) => {
|
|
846
|
+
if (signal.aborted)
|
|
847
|
+
throw signal.reason;
|
|
848
|
+
throw diagnostics.failure(error);
|
|
849
|
+
});
|
|
850
|
+
this.diagnostics.set(response, diagnostics);
|
|
851
|
+
if (signal.aborted) {
|
|
852
|
+
response.body?.cancel(signal.reason).catch(() => {});
|
|
853
|
+
signal.throwIfAborted();
|
|
854
|
+
}
|
|
855
|
+
if (response.ok)
|
|
856
|
+
return response;
|
|
857
|
+
const error = this.redactError(response, await httpError(response, providerId, modelId, signal));
|
|
858
|
+
if (protocol === "embeddings" || !error.retryable || attempt >= (this.options.maxRetries ?? 2))
|
|
859
|
+
throw error;
|
|
860
|
+
await wait(Math.min(error.retryAfterMs ?? 250 * 2 ** attempt, this.options.maxRetryDelayMs ?? 60000), signal);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
redactText(response, value) {
|
|
864
|
+
return value === undefined ? undefined : this.diagnostics.get(response)?.text(value) ?? value;
|
|
865
|
+
}
|
|
866
|
+
redactError(response, error) {
|
|
867
|
+
return this.diagnostics.get(response).providerError(error);
|
|
868
|
+
}
|
|
869
|
+
redactFailure(response, error) {
|
|
870
|
+
return this.diagnostics.get(response).failure(error);
|
|
871
|
+
}
|
|
872
|
+
async headers(signal, diagnostics, keyHeader = "api-key") {
|
|
873
|
+
const headers = new Headers(await resolve(this.options.headers, signal));
|
|
874
|
+
for (const value of headers.values())
|
|
875
|
+
diagnostics.add(value);
|
|
876
|
+
for (const reserved of ["authorization", "api-key", "x-api-key"])
|
|
877
|
+
if (headers.has(reserved))
|
|
878
|
+
throw new TypeError(`${PREFIX} Use apiKey instead of '${reserved}' headers.`);
|
|
879
|
+
const key = await resolve(this.options.apiKey, signal);
|
|
880
|
+
if (typeof key !== "string" || !key.trim())
|
|
881
|
+
throw new TypeError(`${PREFIX} apiKey must resolve to a nonempty string.`);
|
|
882
|
+
diagnostics.add(key);
|
|
883
|
+
headers.set(keyHeader, key);
|
|
884
|
+
return headers;
|
|
885
|
+
}
|
|
886
|
+
fetch(url, init) {
|
|
887
|
+
return (this.options.fetch ?? fetch)(url, init);
|
|
888
|
+
}
|
|
889
|
+
url(protocol) {
|
|
890
|
+
if (protocol === "embeddings")
|
|
891
|
+
return `${this.baseUrl}/embeddings`;
|
|
892
|
+
if (protocol === "responses")
|
|
893
|
+
return `${this.baseUrl}/responses`;
|
|
894
|
+
if (protocol === "chat")
|
|
895
|
+
return `${this.baseUrl}/chat/completions`;
|
|
896
|
+
return `${new URL(this.baseUrl).origin}/anthropic/v1/messages`;
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
function requestId(response) {
|
|
900
|
+
return response.headers.get("apim-request-id") ?? response.headers.get("x-request-id") ?? response.headers.get("request-id") ?? undefined;
|
|
901
|
+
}
|
|
902
|
+
async function resolve(source, signal) {
|
|
903
|
+
signal.throwIfAborted();
|
|
904
|
+
if (typeof source !== "function")
|
|
905
|
+
return source;
|
|
906
|
+
return abortable(() => source(signal), signal);
|
|
907
|
+
}
|
|
908
|
+
function abortable(operation, signal) {
|
|
909
|
+
return new Promise((resolve, reject) => {
|
|
910
|
+
signal.throwIfAborted();
|
|
911
|
+
const abort = () => {
|
|
912
|
+
signal.removeEventListener("abort", abort);
|
|
913
|
+
reject(signal.reason);
|
|
914
|
+
};
|
|
915
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
916
|
+
Promise.resolve().then(() => {
|
|
917
|
+
signal.throwIfAborted();
|
|
918
|
+
return operation();
|
|
919
|
+
}).then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
async function httpError(response, providerId, modelId, signal) {
|
|
923
|
+
let message = `Provider request failed with HTTP ${response.status}.`;
|
|
924
|
+
let code;
|
|
925
|
+
const reader = response.body?.getReader();
|
|
926
|
+
if (reader) {
|
|
927
|
+
const cancel = () => {
|
|
928
|
+
reader.cancel(signal.reason).catch(() => {});
|
|
929
|
+
};
|
|
930
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
931
|
+
const chunks = [];
|
|
932
|
+
let length = 0;
|
|
933
|
+
try {
|
|
934
|
+
while (length < 8192) {
|
|
935
|
+
signal.throwIfAborted();
|
|
936
|
+
const { value, done } = await abortable(() => reader.read(), signal);
|
|
937
|
+
if (done)
|
|
938
|
+
break;
|
|
939
|
+
const part = value.subarray(0, 8192 - length);
|
|
940
|
+
chunks.push(part);
|
|
941
|
+
length += part.length;
|
|
942
|
+
}
|
|
943
|
+
const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
944
|
+
const error = object(object(parsed)?.error);
|
|
945
|
+
if (typeof error?.message === "string")
|
|
946
|
+
message = error.message;
|
|
947
|
+
if (typeof error?.code === "string")
|
|
948
|
+
code = error.code;
|
|
949
|
+
else if (typeof error?.type === "string")
|
|
950
|
+
code = error.type;
|
|
951
|
+
} catch {} finally {
|
|
952
|
+
signal.removeEventListener("abort", cancel);
|
|
953
|
+
reader.cancel().catch(() => {});
|
|
954
|
+
reader.releaseLock();
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
signal.throwIfAborted();
|
|
958
|
+
const delay = response.headers.get("retry-after-ms") ?? response.headers.get("x-ms-retry-after-ms");
|
|
959
|
+
const retry = response.headers.get("retry-after");
|
|
960
|
+
const ms = delay !== null ? Number(delay) : retry !== null ? Number.isFinite(Number(retry)) ? Number(retry) * 1000 : Date.parse(retry) - Date.now() : undefined;
|
|
961
|
+
return new ModelProviderError2(`${PREFIX} ${message}`, providerId, modelId, {
|
|
962
|
+
status: response.status,
|
|
963
|
+
requestId: requestId(response),
|
|
964
|
+
code,
|
|
965
|
+
retryable: response.status === 408 || response.status === 429 || response.status >= 500,
|
|
966
|
+
...ms !== undefined && Number.isFinite(ms) && ms >= 0 ? { retryAfterMs: Math.ceil(ms) } : {}
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
async function wait(ms, signal) {
|
|
970
|
+
signal.throwIfAborted();
|
|
971
|
+
await new Promise((resolve, reject) => {
|
|
972
|
+
const abort = () => {
|
|
973
|
+
clearTimeout(timer);
|
|
974
|
+
reject(signal.reason);
|
|
975
|
+
};
|
|
976
|
+
const timer = setTimeout(() => {
|
|
977
|
+
signal.removeEventListener("abort", abort);
|
|
978
|
+
resolve();
|
|
979
|
+
}, ms);
|
|
980
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
// src/discovery.ts
|
|
1081
985
|
class FoundryDeployments {
|
|
1082
986
|
providerId;
|
|
1083
987
|
transport;
|
|
@@ -1799,6 +1703,7 @@ function reasoningRequest(request, definition, options, max) {
|
|
|
1799
1703
|
// src/resolution.ts
|
|
1800
1704
|
import {
|
|
1801
1705
|
defineLanguageModel as defineLanguageModel2,
|
|
1706
|
+
defineLanguageModelLabel,
|
|
1802
1707
|
defineModelRateCard as defineModelRateCard4,
|
|
1803
1708
|
UnsupportedModelFeatureError as UnsupportedModelFeatureError5
|
|
1804
1709
|
} from "@sixb/core/models";
|
|
@@ -1826,6 +1731,7 @@ function resolveModel(input) {
|
|
|
1826
1731
|
publisher,
|
|
1827
1732
|
via: "Azure AI Foundry",
|
|
1828
1733
|
...options.definition,
|
|
1734
|
+
...defineLanguageModelLabel(options, PREFIX),
|
|
1829
1735
|
kind: "language",
|
|
1830
1736
|
providerId: input.providerId,
|
|
1831
1737
|
modelId: input.modelId,
|
|
@@ -1840,6 +1746,8 @@ function resolveModel(input) {
|
|
|
1840
1746
|
kind: "language",
|
|
1841
1747
|
providerId: input.providerId,
|
|
1842
1748
|
modelId: input.modelId,
|
|
1749
|
+
name: definition.name,
|
|
1750
|
+
description: definition.description,
|
|
1843
1751
|
publisher: definition.publisher,
|
|
1844
1752
|
via: definition.via,
|
|
1845
1753
|
capabilities: {}
|
|
@@ -1990,10 +1898,14 @@ class FoundryModel {
|
|
|
1990
1898
|
get costEstimator() {
|
|
1991
1899
|
return this.#execution?.estimator ?? this.estimator;
|
|
1992
1900
|
}
|
|
1901
|
+
get rateCard() {
|
|
1902
|
+
return (this.#execution?.estimator ?? this.estimator).rateCard;
|
|
1903
|
+
}
|
|
1993
1904
|
options;
|
|
1994
1905
|
scope;
|
|
1995
1906
|
reliableReasoning;
|
|
1996
1907
|
defaultReasoning;
|
|
1908
|
+
maxOutputTokens;
|
|
1997
1909
|
constructor(requestedProtocol, transport, deployments, providerId, modelId, options, modelCatalog, resolution) {
|
|
1998
1910
|
this.requestedProtocol = requestedProtocol;
|
|
1999
1911
|
this.transport = transport;
|
|
@@ -2027,6 +1939,8 @@ class FoundryModel {
|
|
|
2027
1939
|
validateChatOptions(options);
|
|
2028
1940
|
else
|
|
2029
1941
|
validateOptions(options);
|
|
1942
|
+
if (options.maxOutputTokens !== undefined)
|
|
1943
|
+
this.maxOutputTokens = options.maxOutputTokens;
|
|
2030
1944
|
const { costEstimator, ...serializable } = options;
|
|
2031
1945
|
this.options = {
|
|
2032
1946
|
...structuredClone(serializable),
|
|
@@ -2149,4 +2063,4 @@ export {
|
|
|
2149
2063
|
createAzureAIFoundry
|
|
2150
2064
|
};
|
|
2151
2065
|
|
|
2152
|
-
//# debugId=
|
|
2066
|
+
//# debugId=A039AA13A59ECF2964756E2164756E21
|