@sixb/azure-ai-foundry 0.1.5 → 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/dist/catalog.d.ts.map +1 -1
- package/dist/index.js +231 -327
- package/dist/index.js.map +6 -6
- package/package.json +3 -3
- package/src/catalog.ts +3 -118
package/dist/catalog.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../src/catalog.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EAInB,KAAK,aAAa,EAEnB,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../src/catalog.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EAInB,KAAK,aAAa,EAEnB,MAAM,mBAAmB,CAAA;AAG1B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAGlD,MAAM,WAAW,4BAA4B;IAC3C,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;CAC1F;AACD,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAA;IACzB,QAAQ,CAAC,eAAe,EAAE,OAAO,GAAG,cAAc,CAAA;IAClD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,QAAQ,CAAC,EAAE,eAAe,CAAA;IACnC,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAA;IAC3D,QAAQ,CAAC,QAAQ,CAAC,EAAE,aAAa,CAAA;CAClC;AAED,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAA;IAC1B,QAAQ,CAAC,eAAe,EAAE,OAAO,GAAG,cAAc,CAAA;IAClD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,QAAQ,CAAC,EAAE,aAAa,CAAA;CAClC;AAID,6FAA6F;AAC7F,qBAAa,sBAAsB;IACjC,OAAO,CAAC,QAAQ,CAA8B;IAC9C,OAAO,CAAC,QAAQ,CAAI;IACpB,OAAO,CAAC,OAAO,CAAC,CAAkC;IAClD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA8B;gBAC1C,OAAO,GAAE,4BAAiC;IAYtD,UAAU;IAGJ,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,UAAQ,GAAG,OAAO,CAAC,YAAY,GAAG,SAAS,CAAC;IAGrE,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,UAAQ,GAAG,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC;IAU7F,oFAAoF;IAC9E,MAAM,CAAC,OAAO,UAAQ,GAAG,OAAO,CAAC,CAAC,IAAI,EAAE,MAAM,KAAK,YAAY,GAAG,SAAS,CAAC;IA2BlF,OAAO,CAAC,IAAI;YAcE,WAAW;CA0F1B"}
|
package/dist/index.js
CHANGED
|
@@ -88,6 +88,7 @@ import {
|
|
|
88
88
|
MODEL_REASONING_EFFORTS,
|
|
89
89
|
ModelCatalogUnavailableError
|
|
90
90
|
} from "@sixb/core/models";
|
|
91
|
+
import { downloadModelsDev, modelsDevRateCard } from "@sixb/model-protocols/models-dev";
|
|
91
92
|
|
|
92
93
|
// src/publisher.ts
|
|
93
94
|
var publishers = {
|
|
@@ -120,236 +121,7 @@ function fireworksPublisher(family) {
|
|
|
120
121
|
return;
|
|
121
122
|
}
|
|
122
123
|
|
|
123
|
-
// src/transport.ts
|
|
124
|
-
import { ModelProviderError as ModelProviderError2 } from "@sixb/core/models";
|
|
125
|
-
|
|
126
|
-
// src/diagnostics.ts
|
|
127
|
-
import { ModelProviderError } from "@sixb/core/models";
|
|
128
|
-
|
|
129
|
-
class RequestDiagnostics {
|
|
130
|
-
secrets = new Set;
|
|
131
|
-
add(value) {
|
|
132
|
-
if (value.trim())
|
|
133
|
-
this.secrets.add(value.trim());
|
|
134
|
-
}
|
|
135
|
-
text(value) {
|
|
136
|
-
for (const secret of [...this.secrets].sort((a, b) => b.length - a.length))
|
|
137
|
-
value = value.replaceAll(secret, "[REDACTED]");
|
|
138
|
-
return value;
|
|
139
|
-
}
|
|
140
|
-
providerError(error) {
|
|
141
|
-
return new ModelProviderError(this.text(error.message), error.providerId, error.modelId, {
|
|
142
|
-
status: error.status,
|
|
143
|
-
retryable: error.retryable,
|
|
144
|
-
retryAfterMs: error.retryAfterMs,
|
|
145
|
-
code: error.code === undefined ? undefined : this.text(error.code),
|
|
146
|
-
requestId: error.requestId === undefined ? undefined : this.text(error.requestId)
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
failure(error) {
|
|
150
|
-
if (error instanceof ModelProviderError)
|
|
151
|
-
return this.providerError(error);
|
|
152
|
-
const sanitized = new Error(this.text(error instanceof Error ? error.message : String(error)));
|
|
153
|
-
sanitized.name = error instanceof Error ? this.text(error.name) : "Error";
|
|
154
|
-
return sanitized;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// src/transport.ts
|
|
159
|
-
class FoundryTransport {
|
|
160
|
-
diagnostics = new WeakMap;
|
|
161
|
-
baseUrl;
|
|
162
|
-
projectUrl;
|
|
163
|
-
options;
|
|
164
|
-
constructor(options, target = "project") {
|
|
165
|
-
if (!URL.canParse(options.endpoint))
|
|
166
|
-
throw new TypeError(`${PREFIX} endpoint must be a valid ${target} URL.`);
|
|
167
|
-
const url = new URL(options.endpoint);
|
|
168
|
-
if (!["https:", "http:"].includes(url.protocol) || url.username || url.password || url.search || url.hash) {
|
|
169
|
-
throw new TypeError(`${PREFIX} endpoint must be an HTTP(S) ${target} URL without credentials, query, or fragment.`);
|
|
170
|
-
}
|
|
171
|
-
const path = url.pathname.replace(/\/+$/, "");
|
|
172
|
-
if (target === "project" && !/^\/api\/projects\/[^/]+$/.test(path))
|
|
173
|
-
throw new TypeError(`${PREFIX} endpoint must be a Foundry project URL: https://<resource>.services.ai.azure.com/api/projects/<project>.`);
|
|
174
|
-
if (target === "resource" && path !== "" && path !== "/openai/v1")
|
|
175
|
-
throw new TypeError(`${PREFIX} embeddings.endpoint must be a resource origin or /openai/v1 URL.`);
|
|
176
|
-
this.projectUrl = `${url.origin}${path}`;
|
|
177
|
-
this.baseUrl = target === "project" ? `${this.projectUrl}/openai/v1` : `${url.origin}/openai/v1`;
|
|
178
|
-
if (typeof options.apiKey !== "function" && (typeof options.apiKey !== "string" || !options.apiKey.trim()))
|
|
179
|
-
throw new TypeError(`${PREFIX} apiKey is required.`);
|
|
180
|
-
if (options.maxRetries !== undefined && (!Number.isSafeInteger(options.maxRetries) || options.maxRetries < 0)) {
|
|
181
|
-
throw new TypeError(`${PREFIX} maxRetries must be a nonnegative safe integer.`);
|
|
182
|
-
}
|
|
183
|
-
positiveInteger(options.maxRetryDelayMs, "maxRetryDelayMs");
|
|
184
|
-
this.options = {
|
|
185
|
-
...options,
|
|
186
|
-
headers: typeof options.headers === "function" ? options.headers : { ...options.headers }
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
async post(body, signal, providerId, modelId, protocol = "responses") {
|
|
190
|
-
const url = this.url(protocol);
|
|
191
|
-
const diagnostics = new RequestDiagnostics;
|
|
192
|
-
for (let attempt = 0;; attempt++) {
|
|
193
|
-
signal.throwIfAborted();
|
|
194
|
-
const headers = await this.headers(signal, diagnostics, protocol === "messages" ? "x-api-key" : "api-key");
|
|
195
|
-
headers.set("content-type", "application/json");
|
|
196
|
-
headers.set("accept", protocol === "embeddings" ? "application/json" : "text/event-stream");
|
|
197
|
-
if (protocol === "messages")
|
|
198
|
-
headers.set("anthropic-version", "2023-06-01");
|
|
199
|
-
signal.throwIfAborted();
|
|
200
|
-
const response = await abortable(async () => {
|
|
201
|
-
const result = await this.fetch(url, {
|
|
202
|
-
method: "POST",
|
|
203
|
-
redirect: "error",
|
|
204
|
-
headers,
|
|
205
|
-
body,
|
|
206
|
-
signal
|
|
207
|
-
});
|
|
208
|
-
if (signal.aborted)
|
|
209
|
-
result.body?.cancel(signal.reason).catch(() => {});
|
|
210
|
-
return result;
|
|
211
|
-
}, signal).catch((error) => {
|
|
212
|
-
if (signal.aborted)
|
|
213
|
-
throw signal.reason;
|
|
214
|
-
throw diagnostics.failure(error);
|
|
215
|
-
});
|
|
216
|
-
this.diagnostics.set(response, diagnostics);
|
|
217
|
-
if (signal.aborted) {
|
|
218
|
-
response.body?.cancel(signal.reason).catch(() => {});
|
|
219
|
-
signal.throwIfAborted();
|
|
220
|
-
}
|
|
221
|
-
if (response.ok)
|
|
222
|
-
return response;
|
|
223
|
-
const error = this.redactError(response, await httpError(response, providerId, modelId, signal));
|
|
224
|
-
if (protocol === "embeddings" || !error.retryable || attempt >= (this.options.maxRetries ?? 2))
|
|
225
|
-
throw error;
|
|
226
|
-
await wait(Math.min(error.retryAfterMs ?? 250 * 2 ** attempt, this.options.maxRetryDelayMs ?? 60000), signal);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
redactText(response, value) {
|
|
230
|
-
return value === undefined ? undefined : this.diagnostics.get(response)?.text(value) ?? value;
|
|
231
|
-
}
|
|
232
|
-
redactError(response, error) {
|
|
233
|
-
return this.diagnostics.get(response).providerError(error);
|
|
234
|
-
}
|
|
235
|
-
redactFailure(response, error) {
|
|
236
|
-
return this.diagnostics.get(response).failure(error);
|
|
237
|
-
}
|
|
238
|
-
async headers(signal, diagnostics, keyHeader = "api-key") {
|
|
239
|
-
const headers = new Headers(await resolve(this.options.headers, signal));
|
|
240
|
-
for (const value of headers.values())
|
|
241
|
-
diagnostics.add(value);
|
|
242
|
-
for (const reserved of ["authorization", "api-key", "x-api-key"])
|
|
243
|
-
if (headers.has(reserved))
|
|
244
|
-
throw new TypeError(`${PREFIX} Use apiKey instead of '${reserved}' headers.`);
|
|
245
|
-
const key = await resolve(this.options.apiKey, signal);
|
|
246
|
-
if (typeof key !== "string" || !key.trim())
|
|
247
|
-
throw new TypeError(`${PREFIX} apiKey must resolve to a nonempty string.`);
|
|
248
|
-
diagnostics.add(key);
|
|
249
|
-
headers.set(keyHeader, key);
|
|
250
|
-
return headers;
|
|
251
|
-
}
|
|
252
|
-
fetch(url, init) {
|
|
253
|
-
return (this.options.fetch ?? fetch)(url, init);
|
|
254
|
-
}
|
|
255
|
-
url(protocol) {
|
|
256
|
-
if (protocol === "embeddings")
|
|
257
|
-
return `${this.baseUrl}/embeddings`;
|
|
258
|
-
if (protocol === "responses")
|
|
259
|
-
return `${this.baseUrl}/responses`;
|
|
260
|
-
if (protocol === "chat")
|
|
261
|
-
return `${this.baseUrl}/chat/completions`;
|
|
262
|
-
return `${new URL(this.baseUrl).origin}/anthropic/v1/messages`;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
function requestId(response) {
|
|
266
|
-
return response.headers.get("apim-request-id") ?? response.headers.get("x-request-id") ?? response.headers.get("request-id") ?? undefined;
|
|
267
|
-
}
|
|
268
|
-
async function resolve(source, signal) {
|
|
269
|
-
signal.throwIfAborted();
|
|
270
|
-
if (typeof source !== "function")
|
|
271
|
-
return source;
|
|
272
|
-
return abortable(() => source(signal), signal);
|
|
273
|
-
}
|
|
274
|
-
function abortable(operation, signal) {
|
|
275
|
-
return new Promise((resolve, reject) => {
|
|
276
|
-
signal.throwIfAborted();
|
|
277
|
-
const abort = () => {
|
|
278
|
-
signal.removeEventListener("abort", abort);
|
|
279
|
-
reject(signal.reason);
|
|
280
|
-
};
|
|
281
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
282
|
-
Promise.resolve().then(() => {
|
|
283
|
-
signal.throwIfAborted();
|
|
284
|
-
return operation();
|
|
285
|
-
}).then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
async function httpError(response, providerId, modelId, signal) {
|
|
289
|
-
let message = `Provider request failed with HTTP ${response.status}.`;
|
|
290
|
-
let code;
|
|
291
|
-
const reader = response.body?.getReader();
|
|
292
|
-
if (reader) {
|
|
293
|
-
const cancel = () => {
|
|
294
|
-
reader.cancel(signal.reason).catch(() => {});
|
|
295
|
-
};
|
|
296
|
-
signal.addEventListener("abort", cancel, { once: true });
|
|
297
|
-
const chunks = [];
|
|
298
|
-
let length = 0;
|
|
299
|
-
try {
|
|
300
|
-
while (length < 8192) {
|
|
301
|
-
signal.throwIfAborted();
|
|
302
|
-
const { value, done } = await abortable(() => reader.read(), signal);
|
|
303
|
-
if (done)
|
|
304
|
-
break;
|
|
305
|
-
const part = value.subarray(0, 8192 - length);
|
|
306
|
-
chunks.push(part);
|
|
307
|
-
length += part.length;
|
|
308
|
-
}
|
|
309
|
-
const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
310
|
-
const error = object(object(parsed)?.error);
|
|
311
|
-
if (typeof error?.message === "string")
|
|
312
|
-
message = error.message;
|
|
313
|
-
if (typeof error?.code === "string")
|
|
314
|
-
code = error.code;
|
|
315
|
-
else if (typeof error?.type === "string")
|
|
316
|
-
code = error.type;
|
|
317
|
-
} catch {} finally {
|
|
318
|
-
signal.removeEventListener("abort", cancel);
|
|
319
|
-
reader.cancel().catch(() => {});
|
|
320
|
-
reader.releaseLock();
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
signal.throwIfAborted();
|
|
324
|
-
const delay = response.headers.get("retry-after-ms") ?? response.headers.get("x-ms-retry-after-ms");
|
|
325
|
-
const retry = response.headers.get("retry-after");
|
|
326
|
-
const ms = delay !== null ? Number(delay) : retry !== null ? Number.isFinite(Number(retry)) ? Number(retry) * 1000 : Date.parse(retry) - Date.now() : undefined;
|
|
327
|
-
return new ModelProviderError2(`${PREFIX} ${message}`, providerId, modelId, {
|
|
328
|
-
status: response.status,
|
|
329
|
-
requestId: requestId(response),
|
|
330
|
-
code,
|
|
331
|
-
retryable: response.status === 408 || response.status === 429 || response.status >= 500,
|
|
332
|
-
...ms !== undefined && Number.isFinite(ms) && ms >= 0 ? { retryAfterMs: Math.ceil(ms) } : {}
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
async function wait(ms, signal) {
|
|
336
|
-
signal.throwIfAborted();
|
|
337
|
-
await new Promise((resolve, reject) => {
|
|
338
|
-
const abort = () => {
|
|
339
|
-
clearTimeout(timer);
|
|
340
|
-
reject(signal.reason);
|
|
341
|
-
};
|
|
342
|
-
const timer = setTimeout(() => {
|
|
343
|
-
signal.removeEventListener("abort", abort);
|
|
344
|
-
resolve();
|
|
345
|
-
}, ms);
|
|
346
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
|
|
350
124
|
// src/catalog.ts
|
|
351
|
-
var url = "https://models.dev/api.json";
|
|
352
|
-
|
|
353
125
|
class RemoteModelsDevCatalog {
|
|
354
126
|
snapshot = [];
|
|
355
127
|
loadedAt = 0;
|
|
@@ -406,30 +178,9 @@ class RemoteModelsDevCatalog {
|
|
|
406
178
|
return this.pending;
|
|
407
179
|
}
|
|
408
180
|
async fetchModels() {
|
|
409
|
-
const signal = AbortSignal.timeout(this.options.timeoutMs ?? 1e4);
|
|
410
181
|
let text;
|
|
411
182
|
try {
|
|
412
|
-
|
|
413
|
-
if (!response.ok || !response.body)
|
|
414
|
-
throw new Error(`HTTP ${response.status}`);
|
|
415
|
-
const reader = response.body.getReader();
|
|
416
|
-
const chunks = [];
|
|
417
|
-
let bytes = 0;
|
|
418
|
-
try {
|
|
419
|
-
for (;; ) {
|
|
420
|
-
const chunk = await abortable(() => reader.read(), signal);
|
|
421
|
-
if (chunk.done)
|
|
422
|
-
break;
|
|
423
|
-
bytes += chunk.value.length;
|
|
424
|
-
if (bytes > 32 * 1024 * 1024)
|
|
425
|
-
throw new Error("Catalog exceeds 32 MiB");
|
|
426
|
-
chunks.push(chunk.value);
|
|
427
|
-
}
|
|
428
|
-
text = Buffer.concat(chunks).toString();
|
|
429
|
-
} finally {
|
|
430
|
-
reader.cancel().catch(() => {});
|
|
431
|
-
reader.releaseLock();
|
|
432
|
-
}
|
|
183
|
+
text = await downloadModelsDev(this.options);
|
|
433
184
|
} catch (cause) {
|
|
434
185
|
throw new ModelCatalogUnavailableError(`${PREFIX} models.dev catalog unavailable.`, { cause });
|
|
435
186
|
}
|
|
@@ -523,86 +274,12 @@ function reasoning(supported, value) {
|
|
|
523
274
|
};
|
|
524
275
|
}
|
|
525
276
|
function rates(raw, protocol) {
|
|
526
|
-
const card =
|
|
277
|
+
const card = modelsDevRateCard(raw);
|
|
527
278
|
if (protocol !== "messages" || card?.cacheWriteInput === undefined)
|
|
528
279
|
return card;
|
|
529
280
|
const { cacheWriteInput, ...rest } = card;
|
|
530
281
|
return defineModelRateCard2({ ...rest, cacheWriteInput5m: cacheWriteInput });
|
|
531
282
|
}
|
|
532
|
-
function parseRates(raw) {
|
|
533
|
-
if (!raw || raw.tiers === undefined)
|
|
534
|
-
return flatRates(raw);
|
|
535
|
-
const { tiers, context_over_200k: legacy, ...base } = raw;
|
|
536
|
-
const card = flatRates(base);
|
|
537
|
-
if (!card || !Array.isArray(tiers))
|
|
538
|
-
return;
|
|
539
|
-
const parsed = [];
|
|
540
|
-
for (const value of tiers) {
|
|
541
|
-
const entry = object(value);
|
|
542
|
-
if (!entry)
|
|
543
|
-
return;
|
|
544
|
-
const { tier, ...prices } = entry;
|
|
545
|
-
const threshold = object(tier);
|
|
546
|
-
const size = threshold?.size;
|
|
547
|
-
const tierCard = flatRates(prices);
|
|
548
|
-
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)
|
|
549
|
-
return;
|
|
550
|
-
parsed.push({ size, card: tierCard });
|
|
551
|
-
}
|
|
552
|
-
parsed.sort((a, b) => a.size - b.size);
|
|
553
|
-
if (new Set(parsed.map((t) => t.size)).size !== parsed.length)
|
|
554
|
-
return;
|
|
555
|
-
const keys = ["input", "output", "cacheReadInput", "cacheWriteInput"];
|
|
556
|
-
if (legacy !== undefined) {
|
|
557
|
-
const mirror = flatRates(object(legacy));
|
|
558
|
-
if (!mirror || parsed.length !== 1 || parsed[0].size < 200000 || keys.some((k) => mirror[k] !== parsed[0].card[k]))
|
|
559
|
-
return;
|
|
560
|
-
}
|
|
561
|
-
if (parsed.some((t) => keys.some((k) => card[k] === undefined !== (t.card[k] === undefined))))
|
|
562
|
-
return;
|
|
563
|
-
const price = (key) => {
|
|
564
|
-
const basePrice = card[key];
|
|
565
|
-
if (basePrice === undefined || !parsed.length)
|
|
566
|
-
return basePrice;
|
|
567
|
-
if (typeof basePrice !== "string")
|
|
568
|
-
return;
|
|
569
|
-
return {
|
|
570
|
-
default: basePrice,
|
|
571
|
-
tiers: parsed.map((t, i) => ({
|
|
572
|
-
minTokens: t.size + 1,
|
|
573
|
-
...parsed[i + 1] ? { maxTokens: parsed[i + 1].size + 1 } : {},
|
|
574
|
-
price: String(t.card[key])
|
|
575
|
-
}))
|
|
576
|
-
};
|
|
577
|
-
};
|
|
578
|
-
return defineModelRateCard2({
|
|
579
|
-
currency: "USD",
|
|
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
|
-
input,
|
|
601
|
-
output,
|
|
602
|
-
...read === undefined ? {} : { cacheReadInput: read },
|
|
603
|
-
...write === undefined ? {} : { cacheWriteInput: write }
|
|
604
|
-
});
|
|
605
|
-
}
|
|
606
283
|
|
|
607
284
|
// src/chat-accounting.ts
|
|
608
285
|
function foundryChatUsage(raw, reliableReasoning) {
|
|
@@ -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;
|
|
@@ -2159,4 +2063,4 @@ export {
|
|
|
2159
2063
|
createAzureAIFoundry
|
|
2160
2064
|
};
|
|
2161
2065
|
|
|
2162
|
-
//# debugId=
|
|
2066
|
+
//# debugId=A039AA13A59ECF2964756E2164756E21
|