@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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sixb/azure-ai-foundry",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Azure AI Foundry model provider for Sixb",
|
|
5
5
|
"homepage": "https://sixb.ai",
|
|
6
6
|
"bugs": {
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"prepack": "bun run build"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@sixb/core": "^0.1.
|
|
35
|
-
"@sixb/model-protocols": "0.1.
|
|
34
|
+
"@sixb/core": "^0.1.26",
|
|
35
|
+
"@sixb/model-protocols": "0.1.3"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/bun": "latest",
|
package/src/catalog.ts
CHANGED
|
@@ -6,12 +6,11 @@ import {
|
|
|
6
6
|
type ModelRateCard,
|
|
7
7
|
type ModelReasoningCapabilities,
|
|
8
8
|
} from "@sixb/core/models"
|
|
9
|
+
import { downloadModelsDev, modelsDevRateCard } from "@sixb/model-protocols/models-dev"
|
|
9
10
|
import { fireworksPublisher } from "./publisher"
|
|
10
11
|
import type { FoundryProtocol } from "./transport"
|
|
11
|
-
import { abortable } from "./transport"
|
|
12
12
|
import { object, PREFIX } from "./util"
|
|
13
13
|
|
|
14
|
-
const url = "https://models.dev/api.json"
|
|
15
14
|
export interface AzureAIFoundryCatalogOptions {
|
|
16
15
|
readonly ttlMs?: number
|
|
17
16
|
readonly timeoutMs?: number
|
|
@@ -112,30 +111,9 @@ export class RemoteModelsDevCatalog {
|
|
|
112
111
|
return this.pending
|
|
113
112
|
}
|
|
114
113
|
private async fetchModels() {
|
|
115
|
-
const signal = AbortSignal.timeout(this.options.timeoutMs ?? 10000)
|
|
116
114
|
let text: string
|
|
117
115
|
try {
|
|
118
|
-
|
|
119
|
-
() => (this.options.fetch ?? fetch)(url, { signal, redirect: "error" }),
|
|
120
|
-
signal
|
|
121
|
-
)
|
|
122
|
-
if (!response.ok || !response.body) throw new Error(`HTTP ${response.status}`)
|
|
123
|
-
const reader = response.body.getReader()
|
|
124
|
-
const chunks: Uint8Array[] = []
|
|
125
|
-
let bytes = 0
|
|
126
|
-
try {
|
|
127
|
-
for (;;) {
|
|
128
|
-
const chunk = await abortable(() => reader.read(), signal)
|
|
129
|
-
if (chunk.done) break
|
|
130
|
-
bytes += chunk.value.length
|
|
131
|
-
if (bytes > 32 * 1024 * 1024) throw new Error("Catalog exceeds 32 MiB")
|
|
132
|
-
chunks.push(chunk.value)
|
|
133
|
-
}
|
|
134
|
-
text = Buffer.concat(chunks).toString()
|
|
135
|
-
} finally {
|
|
136
|
-
void reader.cancel().catch(() => {})
|
|
137
|
-
reader.releaseLock()
|
|
138
|
-
}
|
|
116
|
+
text = await downloadModelsDev(this.options)
|
|
139
117
|
} catch (cause) {
|
|
140
118
|
throw new ModelCatalogUnavailableError(`${PREFIX} models.dev catalog unavailable.`, { cause })
|
|
141
119
|
}
|
|
@@ -263,103 +241,10 @@ function rates(
|
|
|
263
241
|
raw: Record<string, unknown> | undefined,
|
|
264
242
|
protocol: FoundryProtocol | undefined
|
|
265
243
|
): ModelRateCard | undefined {
|
|
266
|
-
const card =
|
|
244
|
+
const card = modelsDevRateCard(raw)
|
|
267
245
|
if (protocol !== "messages" || card?.cacheWriteInput === undefined) return card
|
|
268
246
|
// models.dev's Messages cache_write is five-minute creation, including in context tiers.
|
|
269
247
|
// One-hour prices are unknown and must not fall back to this rate.
|
|
270
248
|
const { cacheWriteInput, ...rest } = card
|
|
271
249
|
return defineModelRateCard({ ...rest, cacheWriteInput5m: cacheWriteInput })
|
|
272
250
|
}
|
|
273
|
-
|
|
274
|
-
function parseRates(raw: Record<string, unknown> | undefined): ModelRateCard | undefined {
|
|
275
|
-
if (!raw || raw.tiers === undefined) return flatRates(raw)
|
|
276
|
-
const { tiers, context_over_200k: legacy, ...base } = raw
|
|
277
|
-
const card = flatRates(base)
|
|
278
|
-
if (!card || !Array.isArray(tiers)) return undefined
|
|
279
|
-
const parsed: { size: number; card: ModelRateCard }[] = []
|
|
280
|
-
for (const value of tiers) {
|
|
281
|
-
const entry = object(value)
|
|
282
|
-
if (!entry) return undefined
|
|
283
|
-
const { tier, ...prices } = entry
|
|
284
|
-
const threshold = object(tier)
|
|
285
|
-
const size = threshold?.size
|
|
286
|
-
const tierCard = flatRates(prices)
|
|
287
|
-
if (
|
|
288
|
-
!tierCard ||
|
|
289
|
-
!threshold ||
|
|
290
|
-
Object.keys(threshold).some((k) => k !== "type" && k !== "size") ||
|
|
291
|
-
(threshold.type !== undefined && threshold.type !== "context") ||
|
|
292
|
-
typeof size !== "number" ||
|
|
293
|
-
!Number.isSafeInteger(size) ||
|
|
294
|
-
size < 0 ||
|
|
295
|
-
size >= Number.MAX_SAFE_INTEGER
|
|
296
|
-
)
|
|
297
|
-
return undefined
|
|
298
|
-
parsed.push({ size, card: tierCard })
|
|
299
|
-
}
|
|
300
|
-
parsed.sort((a, b) => a.size - b.size)
|
|
301
|
-
if (new Set(parsed.map((t) => t.size)).size !== parsed.length) return undefined
|
|
302
|
-
const keys = ["input", "output", "cacheReadInput", "cacheWriteInput"] as const
|
|
303
|
-
if (legacy !== undefined) {
|
|
304
|
-
const mirror = flatRates(object(legacy))
|
|
305
|
-
if (
|
|
306
|
-
!mirror ||
|
|
307
|
-
parsed.length !== 1 ||
|
|
308
|
-
parsed[0]!.size < 200000 ||
|
|
309
|
-
keys.some((k) => mirror[k] !== parsed[0]!.card[k])
|
|
310
|
-
)
|
|
311
|
-
return undefined
|
|
312
|
-
}
|
|
313
|
-
// Missing cache rates are unknown, not inherited or silently treated as zero.
|
|
314
|
-
if (parsed.some((t) => keys.some((k) => (card[k] === undefined) !== (t.card[k] === undefined))))
|
|
315
|
-
return undefined
|
|
316
|
-
const price = (key: (typeof keys)[number]) => {
|
|
317
|
-
const basePrice = card[key]
|
|
318
|
-
if (basePrice === undefined || !parsed.length) return basePrice
|
|
319
|
-
if (typeof basePrice !== "string") return undefined
|
|
320
|
-
return {
|
|
321
|
-
default: basePrice,
|
|
322
|
-
tiers: parsed.map((t, i) => ({
|
|
323
|
-
minTokens: t.size + 1,
|
|
324
|
-
...(parsed[i + 1] ? { maxTokens: parsed[i + 1]!.size + 1 } : {}),
|
|
325
|
-
price: String(t.card[key]),
|
|
326
|
-
})),
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
return defineModelRateCard({
|
|
330
|
-
currency: "USD",
|
|
331
|
-
input: price("input")!,
|
|
332
|
-
output: price("output")!,
|
|
333
|
-
...(card.cacheReadInput === undefined ? {} : { cacheReadInput: price("cacheReadInput")! }),
|
|
334
|
-
...(card.cacheWriteInput === undefined ? {} : { cacheWriteInput: price("cacheWriteInput")! }),
|
|
335
|
-
})
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
function flatRates(raw: Record<string, unknown> | undefined): ModelRateCard | undefined {
|
|
339
|
-
if (!raw) return undefined
|
|
340
|
-
// These are reference prices per million tokens. Unrepresented tiers/dimensions must not
|
|
341
|
-
// silently become flat prices; callers can supply an estimator for those billing contracts.
|
|
342
|
-
if (Object.keys(raw).some((k) => !["input", "output", "cache_read", "cache_write"].includes(k)))
|
|
343
|
-
return undefined
|
|
344
|
-
const decimal = (v: unknown) =>
|
|
345
|
-
(typeof v === "number" || typeof v === "string") && /^\d+(\.\d+)?$/.test(String(v))
|
|
346
|
-
? String(v)
|
|
347
|
-
: undefined
|
|
348
|
-
const input = decimal(raw.input),
|
|
349
|
-
output = decimal(raw.output)
|
|
350
|
-
if (input === undefined || output === undefined) return undefined
|
|
351
|
-
const read = decimal(raw.cache_read),
|
|
352
|
-
write = decimal(raw.cache_write)
|
|
353
|
-
if (
|
|
354
|
-
(raw.cache_read !== undefined && read === undefined) ||
|
|
355
|
-
(raw.cache_write !== undefined && write === undefined)
|
|
356
|
-
)
|
|
357
|
-
return undefined
|
|
358
|
-
return defineModelRateCard({
|
|
359
|
-
currency: "USD",
|
|
360
|
-
input,
|
|
361
|
-
output,
|
|
362
|
-
...(read === undefined ? {} : { cacheReadInput: read }),
|
|
363
|
-
...(write === undefined ? {} : { cacheWriteInput: write }),
|
|
364
|
-
})
|
|
365
|
-
}
|