@sixb/azure-ai-foundry 0.1.1
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/LICENSE +21 -0
- package/README.md +218 -0
- package/dist/accounting.d.ts +5 -0
- package/dist/accounting.d.ts.map +1 -0
- package/dist/catalog.d.ts +29 -0
- package/dist/catalog.d.ts.map +1 -0
- package/dist/chat-accounting.d.ts +4 -0
- package/dist/chat-accounting.d.ts.map +1 -0
- package/dist/chat-request.d.ts +18 -0
- package/dist/chat-request.d.ts.map +1 -0
- package/dist/diagnostics.d.ts +10 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/discovery.d.ts +63 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1898 -0
- package/dist/index.js.map +25 -0
- package/dist/messages-accounting.d.ts +5 -0
- package/dist/messages-accounting.d.ts.map +1 -0
- package/dist/messages-request.d.ts +11 -0
- package/dist/messages-request.d.ts.map +1 -0
- package/dist/messages-schema.d.ts +4 -0
- package/dist/messages-schema.d.ts.map +1 -0
- package/dist/provider.d.ts +64 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/publisher.d.ts +7 -0
- package/dist/publisher.d.ts.map +1 -0
- package/dist/request.d.ts +20 -0
- package/dist/request.d.ts.map +1 -0
- package/dist/resolution.d.ts +36 -0
- package/dist/resolution.d.ts.map +1 -0
- package/dist/structured-output.d.ts +4 -0
- package/dist/structured-output.d.ts.map +1 -0
- package/dist/transport.d.ts +30 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/util.d.ts +6 -0
- package/dist/util.d.ts.map +1 -0
- package/package.json +54 -0
- package/src/accounting.ts +117 -0
- package/src/catalog.ts +328 -0
- package/src/chat-accounting.ts +134 -0
- package/src/chat-request.ts +161 -0
- package/src/diagnostics.ts +34 -0
- package/src/discovery.ts +385 -0
- package/src/index.ts +17 -0
- package/src/messages-accounting.ts +105 -0
- package/src/messages-request.ts +164 -0
- package/src/messages-schema.ts +101 -0
- package/src/provider.ts +416 -0
- package/src/publisher.ts +34 -0
- package/src/request.ts +266 -0
- package/src/resolution.ts +179 -0
- package/src/structured-output.ts +135 -0
- package/src/transport.ts +268 -0
- package/src/util.ts +16 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type LanguageModelDefinition } from "@sixb/core/models";
|
|
2
|
+
import type { CatalogModel } from "./catalog";
|
|
3
|
+
import type { ChatRequestOptions } from "./chat-request";
|
|
4
|
+
import type { AzureAIFoundryDeployment } from "./discovery";
|
|
5
|
+
import type { MessagesRequestOptions } from "./messages-request";
|
|
6
|
+
import type { AzureAIFoundryModelOptions } from "./provider";
|
|
7
|
+
import type { FoundryProtocol } from "./transport";
|
|
8
|
+
export declare function resolveModel(input: {
|
|
9
|
+
providerId: string;
|
|
10
|
+
modelId: string;
|
|
11
|
+
protocol?: FoundryProtocol;
|
|
12
|
+
options: AzureAIFoundryModelOptions & ChatRequestOptions & MessagesRequestOptions;
|
|
13
|
+
deployment?: AzureAIFoundryDeployment;
|
|
14
|
+
discoveredAt?: string;
|
|
15
|
+
catalogModel?: CatalogModel;
|
|
16
|
+
}): {
|
|
17
|
+
protocol: FoundryProtocol;
|
|
18
|
+
definition: LanguageModelDefinition;
|
|
19
|
+
metadata: Readonly<{
|
|
20
|
+
catalog?: {
|
|
21
|
+
readonly provider: "azure" | "fireworks-ai";
|
|
22
|
+
readonly modelId: string;
|
|
23
|
+
readonly source: string;
|
|
24
|
+
readonly pricing: "reference";
|
|
25
|
+
};
|
|
26
|
+
publisher?: string;
|
|
27
|
+
modelName?: string;
|
|
28
|
+
modelVersion?: string;
|
|
29
|
+
sku?: string;
|
|
30
|
+
deployment?: AzureAIFoundryDeployment;
|
|
31
|
+
discoveredAt?: string;
|
|
32
|
+
}>;
|
|
33
|
+
rateCard: import("@sixb/core/models").LanguageModelRateCard | undefined;
|
|
34
|
+
aliases: readonly string[];
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=resolution.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolution.d.ts","sourceRoot":"","sources":["../src/resolution.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,uBAAuB,EAE7B,MAAM,mBAAmB,CAAA;AAC1B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AAC7C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AACxD,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAA;AAC3D,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAA;AAChE,OAAO,KAAK,EAA+B,0BAA0B,EAAE,MAAM,YAAY,CAAA;AAEzF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAIlD,wBAAgB,YAAY,CAAC,KAAK,EAAE;IAClC,UAAU,EAAE,MAAM,CAAA;IAClB,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,CAAC,EAAE,eAAe,CAAA;IAC1B,OAAO,EAAE,0BAA0B,GAAG,kBAAkB,GAAG,sBAAsB,CAAA;IACjF,UAAU,CAAC,EAAE,wBAAwB,CAAA;IACrC,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,YAAY,CAAC,EAAE,YAAY,CAAA;CAC5B;;;;;;;;;;;;;;;;;;;EAgGA"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type JsonObject } from "@sixb/core/models";
|
|
2
|
+
/** Azure's documented strict subset. Never weaken the caller's validation contract. */
|
|
3
|
+
export declare function foundryOutputSchema(schema: JsonObject): JsonObject | undefined;
|
|
4
|
+
//# sourceMappingURL=structured-output.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structured-output.d.ts","sourceRoot":"","sources":["../src/structured-output.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAkBjE,uFAAuF;AACvF,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,GAAG,SAAS,CAmH9E"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ModelProviderError } from "@sixb/core/models";
|
|
2
|
+
import { RequestDiagnostics } from "./diagnostics";
|
|
3
|
+
export type ValueSource<T> = T | ((signal: AbortSignal) => T | Promise<T>);
|
|
4
|
+
export type FoundryProtocol = "responses" | "messages" | "chat";
|
|
5
|
+
export interface TransportOptions {
|
|
6
|
+
readonly endpoint: string;
|
|
7
|
+
readonly apiKey: ValueSource<string | undefined>;
|
|
8
|
+
readonly headers?: ValueSource<Readonly<Record<string, string>>>;
|
|
9
|
+
readonly fetch?: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
10
|
+
readonly maxRetries?: number;
|
|
11
|
+
readonly maxRetryDelayMs?: number;
|
|
12
|
+
}
|
|
13
|
+
export declare class FoundryTransport {
|
|
14
|
+
private readonly diagnostics;
|
|
15
|
+
readonly baseUrl: string;
|
|
16
|
+
readonly projectUrl: string;
|
|
17
|
+
private readonly options;
|
|
18
|
+
constructor(options: TransportOptions);
|
|
19
|
+
post(body: string, signal: AbortSignal, providerId: string, modelId: string, protocol?: FoundryProtocol): Promise<Response>;
|
|
20
|
+
redactText(response: Response, value: string | undefined): string | undefined;
|
|
21
|
+
redactError(response: Response, error: ModelProviderError): ModelProviderError;
|
|
22
|
+
redactFailure(response: Response, error: unknown): unknown;
|
|
23
|
+
headers(signal: AbortSignal, diagnostics: RequestDiagnostics, keyHeader?: "api-key" | "x-api-key"): Promise<Headers>;
|
|
24
|
+
fetch(url: string, init: RequestInit): Promise<Response>;
|
|
25
|
+
url(protocol: FoundryProtocol): string;
|
|
26
|
+
}
|
|
27
|
+
export declare function requestId(response: Response): string | undefined;
|
|
28
|
+
/** Bound credential/transport work even when an injected implementation ignores the signal. */
|
|
29
|
+
export declare function abortable<T>(operation: () => T | Promise<T>, signal: AbortSignal): Promise<T>;
|
|
30
|
+
//# sourceMappingURL=transport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAGlD,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,WAAW,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,CAAA;AAC1E,MAAM,MAAM,eAAe,GAAG,WAAW,GAAG,UAAU,GAAG,MAAM,CAAA;AAE/D,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,GAAG,SAAS,CAAC,CAAA;IAChD,QAAQ,CAAC,OAAO,CAAC,EAAE,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAA;IAChE,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;IACzF,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;IAC5B,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAA;CAClC;AAED,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA8C;IAC1E,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAkB;gBAE9B,OAAO,EAAE,gBAAgB;IAwC/B,IAAI,CACR,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,WAAW,EACnB,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EACf,QAAQ,GAAE,eAA6B,GACtC,OAAO,CAAC,QAAQ,CAAC;IAiDpB,UAAU,CAAC,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS;IAI7E,WAAW,CAAC,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,kBAAkB,GAAG,kBAAkB;IAI9E,aAAa,CAAC,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO;IAIpD,OAAO,CACX,MAAM,EAAE,WAAW,EACnB,WAAW,EAAE,kBAAkB,EAC/B,SAAS,GAAE,SAAS,GAAG,WAAuB,GAC7C,OAAO,CAAC,OAAO,CAAC;IAcnB,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC;IAIxD,GAAG,CAAC,QAAQ,EAAE,eAAe,GAAG,MAAM;CAKvC;AAED,wBAAgB,SAAS,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,GAAG,SAAS,CAOhE;AAWD,+FAA+F;AAC/F,wBAAgB,SAAS,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC,CAgB7F"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Written by scripts/build-package.ts, and published on purpose.
|
|
2
|
+
// Bun applies the nearest tsconfig paths map to every import it resolves, node_modules
|
|
3
|
+
// included. An empty map here keeps a consumer's aliases from sending a module in this
|
|
4
|
+
// directory back into a second copy of its own source.
|
|
5
|
+
{ "compilerOptions": { "paths": {} } }
|
package/dist/util.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type JsonObject } from "@sixb/core/models";
|
|
2
|
+
export declare const PREFIX = "[SixbAzureAIFoundry]";
|
|
3
|
+
export declare function object(value: unknown): JsonObject | undefined;
|
|
4
|
+
export declare function positiveInteger(value: number | undefined, field: string): void;
|
|
5
|
+
export declare function counter(value: unknown): number | undefined;
|
|
6
|
+
//# sourceMappingURL=util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAEjE,eAAO,MAAM,MAAM,yBAAyB,CAAA;AAE5C,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,CAE7D;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAI9E;AACD,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAE1D"}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sixb/azure-ai-foundry",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Azure AI Foundry model provider for Sixb",
|
|
5
|
+
"homepage": "https://sixb.ai",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/sixb-ai/sixb/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/sixb-ai/sixb.git",
|
|
12
|
+
"directory": "models/azure-ai-foundry"
|
|
13
|
+
},
|
|
14
|
+
"author": "Sixb contributors",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"bun": "./src/index.ts",
|
|
22
|
+
"import": "./dist/index.js",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "bun ../../scripts/build-package.ts",
|
|
28
|
+
"typecheck": "tsc --noEmit",
|
|
29
|
+
"test": "bun test",
|
|
30
|
+
"test:e2e": "bun test ./tests/*.e2e.ts",
|
|
31
|
+
"prepack": "bun run build"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@sixb/core": "^0.1.11",
|
|
35
|
+
"@sixb/model-protocols": "0.1.1"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/bun": "latest",
|
|
39
|
+
"typescript": "^5.7.0"
|
|
40
|
+
},
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"engines": {
|
|
43
|
+
"bun": ">=1.4.2"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"dist",
|
|
50
|
+
"src",
|
|
51
|
+
"README.md",
|
|
52
|
+
"LICENSE"
|
|
53
|
+
]
|
|
54
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineModelRateCard,
|
|
3
|
+
estimateModelReservation,
|
|
4
|
+
type JsonObject,
|
|
5
|
+
type LanguageModelRateCard,
|
|
6
|
+
type ModelCostEstimator,
|
|
7
|
+
type ModelUsage,
|
|
8
|
+
rateModelCall,
|
|
9
|
+
} from "@sixb/core/models"
|
|
10
|
+
import { responsesUsage } from "@sixb/model-protocols/responses"
|
|
11
|
+
import { object } from "./util"
|
|
12
|
+
|
|
13
|
+
export function foundryUsage(
|
|
14
|
+
raw: JsonObject | undefined,
|
|
15
|
+
reliableReasoning: boolean | undefined
|
|
16
|
+
): ModelUsage {
|
|
17
|
+
const usage = responsesUsage(raw)
|
|
18
|
+
const { reasoningOutputTokens, ...totals } = usage
|
|
19
|
+
const cached = usage.cacheReadInputTokens
|
|
20
|
+
const input = usage.inputTokens
|
|
21
|
+
const output = usage.outputTokens
|
|
22
|
+
const knownReasoning =
|
|
23
|
+
reasoningOutputTokens !== undefined &&
|
|
24
|
+
output !== undefined &&
|
|
25
|
+
reasoningOutputTokens <= output &&
|
|
26
|
+
(reliableReasoning === true || (reliableReasoning === undefined && reasoningOutputTokens > 0))
|
|
27
|
+
return {
|
|
28
|
+
...totals,
|
|
29
|
+
...(object(raw?.input_tokens_details)?.cache_write_tokens === 0
|
|
30
|
+
? { cacheWriteInputTokens: 0 }
|
|
31
|
+
: {}),
|
|
32
|
+
...(input !== undefined && cached !== undefined && cached <= input && onlyTokenMeters(raw)
|
|
33
|
+
? { uncachedInputTokens: input - cached }
|
|
34
|
+
: {}),
|
|
35
|
+
...(knownReasoning ? { reasoningOutputTokens } : {}),
|
|
36
|
+
...(knownReasoning &&
|
|
37
|
+
reasoningOutputTokens !== undefined &&
|
|
38
|
+
output !== undefined &&
|
|
39
|
+
reasoningOutputTokens <= output &&
|
|
40
|
+
onlyTokenMeters(raw)
|
|
41
|
+
? { textOutputTokens: output - reasoningOutputTokens }
|
|
42
|
+
: {}),
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function foundryEstimator(
|
|
47
|
+
card: LanguageModelRateCard | undefined,
|
|
48
|
+
request: JsonObject | undefined,
|
|
49
|
+
modelName?: string,
|
|
50
|
+
modelVersion?: string,
|
|
51
|
+
aliases?: readonly string[]
|
|
52
|
+
): ModelCostEstimator {
|
|
53
|
+
return tokenEstimator(
|
|
54
|
+
card,
|
|
55
|
+
request,
|
|
56
|
+
["temperature", "top_p", "metadata"],
|
|
57
|
+
onlyTokenMeters,
|
|
58
|
+
modelName,
|
|
59
|
+
modelVersion,
|
|
60
|
+
aliases
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function tokenEstimator(
|
|
65
|
+
card: LanguageModelRateCard | undefined,
|
|
66
|
+
request: JsonObject | undefined,
|
|
67
|
+
requestKeys: readonly string[],
|
|
68
|
+
tokenMeters: (raw: JsonObject | undefined) => boolean,
|
|
69
|
+
modelName?: string,
|
|
70
|
+
modelVersion?: string,
|
|
71
|
+
aliases?: readonly string[]
|
|
72
|
+
): ModelCostEstimator {
|
|
73
|
+
const rateCard = card && defineModelRateCard(card)
|
|
74
|
+
const fixed = Object.keys(request ?? {}).every((key) => requestKeys.includes(key))
|
|
75
|
+
return {
|
|
76
|
+
estimateReservation: (tokens) =>
|
|
77
|
+
fixed ? estimateModelReservation({ ...tokens, rateCard }) : undefined,
|
|
78
|
+
estimate: ({ usage, responseModelId, route }) => {
|
|
79
|
+
const actual = route?.modelId ?? responseModelId
|
|
80
|
+
if (
|
|
81
|
+
!fixed ||
|
|
82
|
+
!tokenMeters(usage.raw) ||
|
|
83
|
+
(modelName &&
|
|
84
|
+
actual &&
|
|
85
|
+
!(aliases ?? [modelName, `${modelName}-${modelVersion}`]).includes(actual))
|
|
86
|
+
)
|
|
87
|
+
return { status: "unpriceable", reason: "missing-rate-card" }
|
|
88
|
+
return rateModelCall({ usage, rateCard })
|
|
89
|
+
},
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Unknown/new meters (notably overlapping cache writes) require a custom estimator. */
|
|
94
|
+
function onlyTokenMeters(raw: JsonObject | undefined): boolean {
|
|
95
|
+
if (!raw) return true
|
|
96
|
+
if (
|
|
97
|
+
Object.keys(raw).some(
|
|
98
|
+
(key) =>
|
|
99
|
+
![
|
|
100
|
+
"input_tokens",
|
|
101
|
+
"output_tokens",
|
|
102
|
+
"total_tokens",
|
|
103
|
+
"input_tokens_details",
|
|
104
|
+
"output_tokens_details",
|
|
105
|
+
].includes(key)
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
return false
|
|
109
|
+
// Azure includes this meter with zero on ordinary Responses calls. Nonzero writes have
|
|
110
|
+
// offering-specific billing/overlap semantics and still require a custom estimator.
|
|
111
|
+
return (
|
|
112
|
+
Object.entries(object(raw.input_tokens_details) ?? {}).every(
|
|
113
|
+
([key, value]) => key === "cached_tokens" || (key === "cache_write_tokens" && value === 0)
|
|
114
|
+
) &&
|
|
115
|
+
Object.keys(object(raw.output_tokens_details) ?? {}).every((key) => key === "reasoning_tokens")
|
|
116
|
+
)
|
|
117
|
+
}
|
package/src/catalog.ts
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineLanguageModel,
|
|
3
|
+
defineModelRateCard,
|
|
4
|
+
type LanguageModelRateCard,
|
|
5
|
+
MODEL_REASONING_EFFORTS,
|
|
6
|
+
ModelCatalogUnavailableError,
|
|
7
|
+
type ModelReasoningCapabilities,
|
|
8
|
+
} from "@sixb/core/models"
|
|
9
|
+
import { fireworksPublisher } from "./publisher"
|
|
10
|
+
import type { FoundryProtocol } from "./transport"
|
|
11
|
+
import { abortable } from "./transport"
|
|
12
|
+
import { object, PREFIX } from "./util"
|
|
13
|
+
|
|
14
|
+
const url = "https://models.dev/api.json"
|
|
15
|
+
export interface AzureAIFoundryCatalogOptions {
|
|
16
|
+
readonly ttlMs?: number
|
|
17
|
+
readonly timeoutMs?: number
|
|
18
|
+
readonly fetch?: (input: string | URL | Request, init?: RequestInit) => Promise<Response>
|
|
19
|
+
}
|
|
20
|
+
export interface CatalogModel {
|
|
21
|
+
readonly catalogProvider: "azure" | "fireworks-ai"
|
|
22
|
+
readonly modelName: string
|
|
23
|
+
readonly protocol?: FoundryProtocol
|
|
24
|
+
readonly definition: ReturnType<typeof defineLanguageModel>
|
|
25
|
+
readonly rateCard?: LanguageModelRateCard
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Provider-scoped models.dev snapshot. Construction and offline reads never perform I/O. */
|
|
29
|
+
export class RemoteModelsDevCatalog {
|
|
30
|
+
private snapshot: readonly CatalogModel[] = []
|
|
31
|
+
private loadedAt = 0
|
|
32
|
+
private pending?: Promise<readonly CatalogModel[]>
|
|
33
|
+
private readonly options: AzureAIFoundryCatalogOptions
|
|
34
|
+
constructor(options: AzureAIFoundryCatalogOptions = {}) {
|
|
35
|
+
this.options = { ...options }
|
|
36
|
+
for (const [key, value] of Object.entries({
|
|
37
|
+
ttlMs: this.options.ttlMs,
|
|
38
|
+
timeoutMs: this.options.timeoutMs,
|
|
39
|
+
}))
|
|
40
|
+
if (
|
|
41
|
+
value !== undefined &&
|
|
42
|
+
(!Number.isSafeInteger(value) || value < (key === "ttlMs" ? 0 : 1))
|
|
43
|
+
)
|
|
44
|
+
throw new TypeError(`${PREFIX} Invalid catalog ${key}.`)
|
|
45
|
+
}
|
|
46
|
+
invalidate() {
|
|
47
|
+
this.loadedAt = 0
|
|
48
|
+
}
|
|
49
|
+
async get(name: string, offline = false): Promise<CatalogModel | undefined> {
|
|
50
|
+
return (await this.lookup(offline))(name)
|
|
51
|
+
}
|
|
52
|
+
/** Capture one snapshot so a batch of lookups cannot expire between deployments. */
|
|
53
|
+
async lookup(offline = false): Promise<(name: string) => CatalogModel | undefined> {
|
|
54
|
+
const models = offline ? this.snapshot : await this.load()
|
|
55
|
+
return (name) => {
|
|
56
|
+
// Azure deployment names preserve publisher casing; models.dev IDs are lowercase.
|
|
57
|
+
// Only a unique exact case-insensitive ID match is accepted, never fuzzy name matching.
|
|
58
|
+
const matches = models.filter(
|
|
59
|
+
(m) => m.catalogProvider === "azure" && m.modelName.toLowerCase() === name.toLowerCase()
|
|
60
|
+
)
|
|
61
|
+
if (matches.length) return matches.length === 1 ? matches[0] : undefined
|
|
62
|
+
if (!/^FW-/i.test(name)) return undefined
|
|
63
|
+
const identity = normalizeFireworksVersion(name.slice(3).toLowerCase())
|
|
64
|
+
const fallback = models.filter((m) => {
|
|
65
|
+
if (m.catalogProvider !== "fireworks-ai") return false
|
|
66
|
+
const suffix = /^accounts\/fireworks\/(?:models|routers)\/([^/]+)$/.exec(
|
|
67
|
+
m.modelName.toLowerCase()
|
|
68
|
+
)?.[1]
|
|
69
|
+
return (
|
|
70
|
+
suffix !== undefined &&
|
|
71
|
+
!/(?:^|-)latest$/.test(suffix) &&
|
|
72
|
+
normalizeFireworksVersion(suffix) === identity
|
|
73
|
+
)
|
|
74
|
+
})
|
|
75
|
+
return fallback.length === 1 ? fallback[0] : undefined
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
private load() {
|
|
79
|
+
if (this.loadedAt && Date.now() - this.loadedAt < (this.options.ttlMs ?? 3600000))
|
|
80
|
+
return Promise.resolve(this.snapshot)
|
|
81
|
+
this.pending ??= this.fetchModels()
|
|
82
|
+
.then((models) => {
|
|
83
|
+
this.snapshot = Object.freeze(models)
|
|
84
|
+
this.loadedAt = Date.now()
|
|
85
|
+
return this.snapshot
|
|
86
|
+
})
|
|
87
|
+
.finally(() => {
|
|
88
|
+
this.pending = undefined
|
|
89
|
+
})
|
|
90
|
+
return this.pending
|
|
91
|
+
}
|
|
92
|
+
private async fetchModels() {
|
|
93
|
+
const signal = AbortSignal.timeout(this.options.timeoutMs ?? 10000)
|
|
94
|
+
let text: string
|
|
95
|
+
try {
|
|
96
|
+
const response = await abortable(
|
|
97
|
+
() => (this.options.fetch ?? fetch)(url, { signal, redirect: "error" }),
|
|
98
|
+
signal
|
|
99
|
+
)
|
|
100
|
+
if (!response.ok || !response.body) throw new Error(`HTTP ${response.status}`)
|
|
101
|
+
const reader = response.body.getReader()
|
|
102
|
+
const chunks: Uint8Array[] = []
|
|
103
|
+
let bytes = 0
|
|
104
|
+
try {
|
|
105
|
+
for (;;) {
|
|
106
|
+
const chunk = await abortable(() => reader.read(), signal)
|
|
107
|
+
if (chunk.done) break
|
|
108
|
+
bytes += chunk.value.length
|
|
109
|
+
if (bytes > 32 * 1024 * 1024) throw new Error("Catalog exceeds 32 MiB")
|
|
110
|
+
chunks.push(chunk.value)
|
|
111
|
+
}
|
|
112
|
+
text = Buffer.concat(chunks).toString()
|
|
113
|
+
} finally {
|
|
114
|
+
void reader.cancel().catch(() => {})
|
|
115
|
+
reader.releaseLock()
|
|
116
|
+
}
|
|
117
|
+
} catch (cause) {
|
|
118
|
+
throw new ModelCatalogUnavailableError(`${PREFIX} models.dev catalog unavailable.`, { cause })
|
|
119
|
+
}
|
|
120
|
+
const root = object(JSON.parse(text))
|
|
121
|
+
if (!object(object(root?.azure)?.models))
|
|
122
|
+
throw new TypeError(`${PREFIX} models.dev response is missing azure.models.`)
|
|
123
|
+
return (["azure", "fireworks-ai"] as const).flatMap((catalogProvider) => {
|
|
124
|
+
const providerData = root?.[catalogProvider]
|
|
125
|
+
if (providerData === undefined) return []
|
|
126
|
+
const models = object(object(providerData)?.models)
|
|
127
|
+
if (!models)
|
|
128
|
+
throw new TypeError(`${PREFIX} models.dev response is missing ${catalogProvider}.models.`)
|
|
129
|
+
return Object.entries(models).flatMap(([id, value]) => {
|
|
130
|
+
const raw = object(value)
|
|
131
|
+
if (!raw || raw.id !== id)
|
|
132
|
+
throw new TypeError(`${PREFIX} Invalid models.dev model identity.`)
|
|
133
|
+
const modalities = object(raw.modalities)
|
|
134
|
+
if (!strings(modalities?.output).includes("text")) return []
|
|
135
|
+
const limits = object(raw.limit)
|
|
136
|
+
const provider = object(raw.provider)
|
|
137
|
+
const protocol =
|
|
138
|
+
catalogProvider === "fireworks-ai"
|
|
139
|
+
? "chat"
|
|
140
|
+
: provider?.npm === "@ai-sdk/anthropic"
|
|
141
|
+
? "messages"
|
|
142
|
+
: provider?.shape === "completions" || provider?.npm === "@ai-sdk/openai-compatible"
|
|
143
|
+
? "chat"
|
|
144
|
+
: undefined
|
|
145
|
+
return [
|
|
146
|
+
{
|
|
147
|
+
catalogProvider,
|
|
148
|
+
modelName: id,
|
|
149
|
+
protocol,
|
|
150
|
+
rateCard: rates(object(raw.cost), protocol),
|
|
151
|
+
definition: defineLanguageModel({
|
|
152
|
+
kind: "language",
|
|
153
|
+
providerId: "azure-ai-foundry",
|
|
154
|
+
modelId: id,
|
|
155
|
+
...(typeof raw.name === "string" ? { name: raw.name } : {}),
|
|
156
|
+
...(catalogProvider === "fireworks-ai"
|
|
157
|
+
? { publisher: fireworksPublisher(raw.family) }
|
|
158
|
+
: {}),
|
|
159
|
+
contextWindow: limit(limits?.context),
|
|
160
|
+
maxInputTokens: limit(limits?.input),
|
|
161
|
+
maxOutputTokens: limit(limits?.output),
|
|
162
|
+
capabilities: {
|
|
163
|
+
...(typeof raw.tool_call === "boolean" ? { localTools: raw.tool_call } : {}),
|
|
164
|
+
...(typeof raw.structured_output === "boolean"
|
|
165
|
+
? { nativeStructuredOutput: raw.structured_output }
|
|
166
|
+
: {}),
|
|
167
|
+
reasoning: reasoning(raw.reasoning, raw.reasoning_options),
|
|
168
|
+
...(Array.isArray(modalities?.input)
|
|
169
|
+
? {
|
|
170
|
+
inputMediaTypes: strings(modalities?.input).flatMap((m) =>
|
|
171
|
+
m === "image"
|
|
172
|
+
? ["image/png", "image/jpeg", "image/webp", "image/gif"]
|
|
173
|
+
: m === "pdf"
|
|
174
|
+
? ["application/pdf"]
|
|
175
|
+
: []
|
|
176
|
+
),
|
|
177
|
+
}
|
|
178
|
+
: {}),
|
|
179
|
+
},
|
|
180
|
+
}),
|
|
181
|
+
} satisfies CatalogModel,
|
|
182
|
+
]
|
|
183
|
+
})
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
// Convert numeric version spelling only; keep variant names and dated suffixes intact.
|
|
188
|
+
function normalizeFireworksVersion(id: string): string {
|
|
189
|
+
return id.replace(/(\d)p(?=\d)/g, "$1.")
|
|
190
|
+
}
|
|
191
|
+
function strings(value: unknown): string[] {
|
|
192
|
+
return Array.isArray(value) ? value.filter((s): s is string => typeof s === "string") : []
|
|
193
|
+
}
|
|
194
|
+
function limit(value: unknown) {
|
|
195
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined
|
|
196
|
+
}
|
|
197
|
+
function reasoning(
|
|
198
|
+
supported: unknown,
|
|
199
|
+
value: unknown
|
|
200
|
+
): ModelReasoningCapabilities | false | undefined {
|
|
201
|
+
if (supported === false) return false
|
|
202
|
+
if (supported !== true && !Array.isArray(value)) return undefined
|
|
203
|
+
const options = Array.isArray(value) ? value.map(object).filter((o) => o !== undefined) : []
|
|
204
|
+
const efforts = options.filter((o) => o?.type === "effort").flatMap((o) => strings(o?.values))
|
|
205
|
+
const budget = options.find((o) => o?.type === "budget_tokens")
|
|
206
|
+
return {
|
|
207
|
+
...(options.some((o) => o?.type === "toggle") || efforts.includes("none")
|
|
208
|
+
? { canDisable: true }
|
|
209
|
+
: {}),
|
|
210
|
+
...(efforts.length
|
|
211
|
+
? { efforts: MODEL_REASONING_EFFORTS.filter((e) => efforts.includes(e)) }
|
|
212
|
+
: {}),
|
|
213
|
+
...(budget
|
|
214
|
+
? {
|
|
215
|
+
budgetTokens: {
|
|
216
|
+
...(limit(budget.min) ? { min: limit(budget.min) } : {}),
|
|
217
|
+
...(limit(budget.max) ? { max: limit(budget.max) } : {}),
|
|
218
|
+
},
|
|
219
|
+
}
|
|
220
|
+
: {}),
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
function rates(
|
|
224
|
+
raw: Record<string, unknown> | undefined,
|
|
225
|
+
protocol: FoundryProtocol | undefined
|
|
226
|
+
): LanguageModelRateCard | undefined {
|
|
227
|
+
const card = parseRates(raw)
|
|
228
|
+
if (protocol !== "messages" || card?.cacheWriteInput === undefined) return card
|
|
229
|
+
// models.dev's Messages cache_write is five-minute creation, including in context tiers.
|
|
230
|
+
// One-hour prices are unknown and must not fall back to this rate.
|
|
231
|
+
const { cacheWriteInput, ...rest } = card
|
|
232
|
+
return defineModelRateCard({ ...rest, cacheWriteInput5m: cacheWriteInput })
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function parseRates(raw: Record<string, unknown> | undefined): LanguageModelRateCard | undefined {
|
|
236
|
+
if (!raw || raw.tiers === undefined) return flatRates(raw)
|
|
237
|
+
const { tiers, context_over_200k: legacy, ...base } = raw
|
|
238
|
+
const card = flatRates(base)
|
|
239
|
+
if (!card || !Array.isArray(tiers)) return undefined
|
|
240
|
+
const parsed: { size: number; card: LanguageModelRateCard }[] = []
|
|
241
|
+
for (const value of tiers) {
|
|
242
|
+
const entry = object(value)
|
|
243
|
+
if (!entry) return undefined
|
|
244
|
+
const { tier, ...prices } = entry
|
|
245
|
+
const threshold = object(tier)
|
|
246
|
+
const size = threshold?.size
|
|
247
|
+
const tierCard = flatRates(prices)
|
|
248
|
+
if (
|
|
249
|
+
!tierCard ||
|
|
250
|
+
!threshold ||
|
|
251
|
+
Object.keys(threshold).some((k) => k !== "type" && k !== "size") ||
|
|
252
|
+
(threshold.type !== undefined && threshold.type !== "context") ||
|
|
253
|
+
typeof size !== "number" ||
|
|
254
|
+
!Number.isSafeInteger(size) ||
|
|
255
|
+
size < 0 ||
|
|
256
|
+
size >= Number.MAX_SAFE_INTEGER
|
|
257
|
+
)
|
|
258
|
+
return undefined
|
|
259
|
+
parsed.push({ size, card: tierCard })
|
|
260
|
+
}
|
|
261
|
+
parsed.sort((a, b) => a.size - b.size)
|
|
262
|
+
if (new Set(parsed.map((t) => t.size)).size !== parsed.length) return undefined
|
|
263
|
+
const keys = ["input", "output", "cacheReadInput", "cacheWriteInput"] as const
|
|
264
|
+
if (legacy !== undefined) {
|
|
265
|
+
const mirror = flatRates(object(legacy))
|
|
266
|
+
if (
|
|
267
|
+
!mirror ||
|
|
268
|
+
parsed.length !== 1 ||
|
|
269
|
+
parsed[0]!.size < 200000 ||
|
|
270
|
+
keys.some((k) => mirror[k] !== parsed[0]!.card[k])
|
|
271
|
+
)
|
|
272
|
+
return undefined
|
|
273
|
+
}
|
|
274
|
+
// Missing cache rates are unknown, not inherited or silently treated as zero.
|
|
275
|
+
if (parsed.some((t) => keys.some((k) => (card[k] === undefined) !== (t.card[k] === undefined))))
|
|
276
|
+
return undefined
|
|
277
|
+
const price = (key: (typeof keys)[number]) => {
|
|
278
|
+
const basePrice = card[key]
|
|
279
|
+
if (basePrice === undefined || !parsed.length) return basePrice
|
|
280
|
+
if (typeof basePrice !== "string") return undefined
|
|
281
|
+
return {
|
|
282
|
+
default: basePrice,
|
|
283
|
+
tiers: parsed.map((t, i) => ({
|
|
284
|
+
minTokens: t.size + 1,
|
|
285
|
+
...(parsed[i + 1] ? { maxTokens: parsed[i + 1]!.size + 1 } : {}),
|
|
286
|
+
price: String(t.card[key]),
|
|
287
|
+
})),
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return defineModelRateCard({
|
|
291
|
+
currency: "USD",
|
|
292
|
+
unit: "million-tokens",
|
|
293
|
+
input: price("input")!,
|
|
294
|
+
output: price("output")!,
|
|
295
|
+
...(card.cacheReadInput === undefined ? {} : { cacheReadInput: price("cacheReadInput")! }),
|
|
296
|
+
...(card.cacheWriteInput === undefined ? {} : { cacheWriteInput: price("cacheWriteInput")! }),
|
|
297
|
+
})
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function flatRates(raw: Record<string, unknown> | undefined): LanguageModelRateCard | undefined {
|
|
301
|
+
if (!raw) return undefined
|
|
302
|
+
// These are reference prices per million tokens. Unrepresented tiers/dimensions must not
|
|
303
|
+
// silently become flat prices; callers can supply an estimator for those billing contracts.
|
|
304
|
+
if (Object.keys(raw).some((k) => !["input", "output", "cache_read", "cache_write"].includes(k)))
|
|
305
|
+
return undefined
|
|
306
|
+
const decimal = (v: unknown) =>
|
|
307
|
+
(typeof v === "number" || typeof v === "string") && /^\d+(\.\d+)?$/.test(String(v))
|
|
308
|
+
? String(v)
|
|
309
|
+
: undefined
|
|
310
|
+
const input = decimal(raw.input),
|
|
311
|
+
output = decimal(raw.output)
|
|
312
|
+
if (input === undefined || output === undefined) return undefined
|
|
313
|
+
const read = decimal(raw.cache_read),
|
|
314
|
+
write = decimal(raw.cache_write)
|
|
315
|
+
if (
|
|
316
|
+
(raw.cache_read !== undefined && read === undefined) ||
|
|
317
|
+
(raw.cache_write !== undefined && write === undefined)
|
|
318
|
+
)
|
|
319
|
+
return undefined
|
|
320
|
+
return defineModelRateCard({
|
|
321
|
+
currency: "USD",
|
|
322
|
+
unit: "million-tokens",
|
|
323
|
+
input,
|
|
324
|
+
output,
|
|
325
|
+
...(read === undefined ? {} : { cacheReadInput: read }),
|
|
326
|
+
...(write === undefined ? {} : { cacheWriteInput: write }),
|
|
327
|
+
})
|
|
328
|
+
}
|