@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,101 @@
|
|
|
1
|
+
import { isJsonObject, type JsonObject } from "@sixb/core/models"
|
|
2
|
+
|
|
3
|
+
const KEYS = new Set([
|
|
4
|
+
"type",
|
|
5
|
+
"title",
|
|
6
|
+
"description",
|
|
7
|
+
"properties",
|
|
8
|
+
"required",
|
|
9
|
+
"additionalProperties",
|
|
10
|
+
"items",
|
|
11
|
+
"enum",
|
|
12
|
+
"const",
|
|
13
|
+
"anyOf",
|
|
14
|
+
"format",
|
|
15
|
+
])
|
|
16
|
+
const TYPES = new Set(["object", "array", "string", "number", "integer", "boolean", "null"])
|
|
17
|
+
const FORMATS = new Set([
|
|
18
|
+
"date-time",
|
|
19
|
+
"time",
|
|
20
|
+
"date",
|
|
21
|
+
"duration",
|
|
22
|
+
"email",
|
|
23
|
+
"hostname",
|
|
24
|
+
"uri",
|
|
25
|
+
"ipv4",
|
|
26
|
+
"ipv6",
|
|
27
|
+
"uuid",
|
|
28
|
+
])
|
|
29
|
+
|
|
30
|
+
/** Conservative Claude decoder subset. Preserve schemas exactly; never strip constraints. */
|
|
31
|
+
export function messagesOutputSchema(schema: JsonObject): JsonObject | undefined {
|
|
32
|
+
const active = new Set<JsonObject>()
|
|
33
|
+
const walk = (value: unknown): boolean => {
|
|
34
|
+
if (!isJsonObject(value) || active.has(value)) return false
|
|
35
|
+
if (Object.keys(value).some((key) => !KEYS.has(key))) return false
|
|
36
|
+
active.add(value)
|
|
37
|
+
try {
|
|
38
|
+
for (const key of ["title", "description"])
|
|
39
|
+
if (value[key] !== undefined && typeof value[key] !== "string") return false
|
|
40
|
+
const types = typeof value.type === "string" ? [value.type] : value.type
|
|
41
|
+
if (
|
|
42
|
+
types !== undefined &&
|
|
43
|
+
(!Array.isArray(types) ||
|
|
44
|
+
!types.length ||
|
|
45
|
+
types.some((type) => typeof type !== "string" || !TYPES.has(type)))
|
|
46
|
+
)
|
|
47
|
+
return false
|
|
48
|
+
if (
|
|
49
|
+
value.anyOf !== undefined &&
|
|
50
|
+
(!Array.isArray(value.anyOf) || !value.anyOf.length || !value.anyOf.every(walk))
|
|
51
|
+
)
|
|
52
|
+
return false
|
|
53
|
+
if (
|
|
54
|
+
types === undefined &&
|
|
55
|
+
value.anyOf === undefined &&
|
|
56
|
+
value.const === undefined &&
|
|
57
|
+
value.enum === undefined
|
|
58
|
+
)
|
|
59
|
+
return false
|
|
60
|
+
if (Array.isArray(types) && types.includes("object")) {
|
|
61
|
+
if (!isJsonObject(value.properties) || value.additionalProperties !== false) return false
|
|
62
|
+
if (
|
|
63
|
+
value.required !== undefined &&
|
|
64
|
+
(!Array.isArray(value.required) ||
|
|
65
|
+
value.required.some(
|
|
66
|
+
(key) =>
|
|
67
|
+
typeof key !== "string" || !Object.hasOwn(value.properties as JsonObject, key)
|
|
68
|
+
))
|
|
69
|
+
)
|
|
70
|
+
return false
|
|
71
|
+
if (!Object.values(value.properties).every(walk)) return false
|
|
72
|
+
} else if (
|
|
73
|
+
value.properties !== undefined ||
|
|
74
|
+
value.required !== undefined ||
|
|
75
|
+
value.additionalProperties !== undefined
|
|
76
|
+
)
|
|
77
|
+
return false
|
|
78
|
+
if (Array.isArray(types) && types.includes("array")) {
|
|
79
|
+
if (!walk(value.items)) return false
|
|
80
|
+
} else if (value.items !== undefined) return false
|
|
81
|
+
if (
|
|
82
|
+
value.format !== undefined &&
|
|
83
|
+
(typeof value.format !== "string" || !FORMATS.has(value.format))
|
|
84
|
+
)
|
|
85
|
+
return false
|
|
86
|
+
if (
|
|
87
|
+
value.enum !== undefined &&
|
|
88
|
+
(!Array.isArray(value.enum) ||
|
|
89
|
+
!value.enum.length ||
|
|
90
|
+
value.enum.some((entry) => typeof entry === "object" && entry !== null))
|
|
91
|
+
)
|
|
92
|
+
return false
|
|
93
|
+
if (value.const !== undefined && typeof value.const === "object" && value.const !== null)
|
|
94
|
+
return false
|
|
95
|
+
return true
|
|
96
|
+
} finally {
|
|
97
|
+
active.delete(value)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return schema.type === "object" && walk(schema) ? schema : undefined
|
|
101
|
+
}
|
package/src/provider.ts
ADDED
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertJsonObject,
|
|
3
|
+
type JsonObject,
|
|
4
|
+
type LanguageModel,
|
|
5
|
+
type LanguageModelDefinition,
|
|
6
|
+
type LanguageModelProvider,
|
|
7
|
+
type LanguageModelRateCard,
|
|
8
|
+
type LanguageModelRequest,
|
|
9
|
+
type LanguageModelStream,
|
|
10
|
+
type LanguageModelStreamEvent,
|
|
11
|
+
ModelCatalogUnavailableError,
|
|
12
|
+
type ModelCostEstimator,
|
|
13
|
+
ModelProviderError,
|
|
14
|
+
UnsupportedModelFeatureError,
|
|
15
|
+
} from "@sixb/core/models"
|
|
16
|
+
import { chatEvents } from "@sixb/model-protocols/chat"
|
|
17
|
+
import { messagesEvents } from "@sixb/model-protocols/messages"
|
|
18
|
+
import { responsesEvents } from "@sixb/model-protocols/responses"
|
|
19
|
+
import { foundryEstimator, foundryUsage } from "./accounting"
|
|
20
|
+
import {
|
|
21
|
+
type AzureAIFoundryCatalogOptions,
|
|
22
|
+
type CatalogModel,
|
|
23
|
+
RemoteModelsDevCatalog,
|
|
24
|
+
} from "./catalog"
|
|
25
|
+
import { foundryChatEstimator, foundryChatUsage } from "./chat-accounting"
|
|
26
|
+
import { type ChatRequestOptions, foundryChatRequest, validateChatOptions } from "./chat-request"
|
|
27
|
+
import {
|
|
28
|
+
type AzureAIFoundryCatalog,
|
|
29
|
+
type AzureAIFoundryDeployment,
|
|
30
|
+
type AzureAIFoundryDiscoveryOptions,
|
|
31
|
+
FoundryDeployments,
|
|
32
|
+
type ResolvedDeployment,
|
|
33
|
+
} from "./discovery"
|
|
34
|
+
import { foundryMessagesEstimator, foundryMessagesUsage } from "./messages-accounting"
|
|
35
|
+
import {
|
|
36
|
+
foundryMessagesRequest,
|
|
37
|
+
type MessagesRequestOptions,
|
|
38
|
+
validateMessagesOptions,
|
|
39
|
+
} from "./messages-request"
|
|
40
|
+
import { type RequestOptions, responsesRequest, validateOptions } from "./request"
|
|
41
|
+
import { resolveModel } from "./resolution"
|
|
42
|
+
import {
|
|
43
|
+
abortable,
|
|
44
|
+
type FoundryProtocol,
|
|
45
|
+
FoundryTransport,
|
|
46
|
+
requestId,
|
|
47
|
+
type TransportOptions,
|
|
48
|
+
} from "./transport"
|
|
49
|
+
import { object, PREFIX } from "./util"
|
|
50
|
+
|
|
51
|
+
export interface AzureAIFoundryOptions extends TransportOptions {
|
|
52
|
+
/** Public model capabilities and reference pricing from models.dev. */
|
|
53
|
+
readonly catalog?: AzureAIFoundryCatalogOptions
|
|
54
|
+
/** Namespace bindings from different resources/projects in a single Sixb catalog. */
|
|
55
|
+
readonly providerId?: string
|
|
56
|
+
/** Required project deployment lookup: cache lifetime and request bounds. */
|
|
57
|
+
readonly discovery?: AzureAIFoundryDiscoveryOptions
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface AzureAIFoundryModelMetadata {
|
|
61
|
+
readonly publisher?: string
|
|
62
|
+
readonly modelName?: string
|
|
63
|
+
readonly modelVersion?: string
|
|
64
|
+
readonly sku?: string
|
|
65
|
+
readonly catalog?: {
|
|
66
|
+
readonly provider: "azure" | "fireworks-ai"
|
|
67
|
+
readonly modelId: string
|
|
68
|
+
readonly source: string
|
|
69
|
+
readonly pricing: "reference"
|
|
70
|
+
}
|
|
71
|
+
readonly deployment?: AzureAIFoundryDeployment
|
|
72
|
+
readonly discoveredAt?: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface AzureAIFoundryModelOptions extends RequestOptions {
|
|
76
|
+
readonly definition?: Omit<LanguageModelDefinition, "kind" | "providerId" | "modelId">
|
|
77
|
+
readonly rateCard?: LanguageModelRateCard
|
|
78
|
+
readonly costEstimator?: ModelCostEstimator
|
|
79
|
+
/** Default: preserve positive reports; trust zero only for OpenAI. "unknown" omits all reasoning counts. */
|
|
80
|
+
readonly reasoningUsage?: "reported" | "unknown"
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface AzureAIFoundryMessagesOptions extends MessagesRequestOptions {
|
|
84
|
+
readonly definition?: AzureAIFoundryModelOptions["definition"]
|
|
85
|
+
readonly rateCard?: LanguageModelRateCard
|
|
86
|
+
readonly costEstimator?: ModelCostEstimator
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface AzureAIFoundryChatOptions extends ChatRequestOptions {
|
|
90
|
+
readonly definition?: AzureAIFoundryModelOptions["definition"]
|
|
91
|
+
readonly rateCard?: LanguageModelRateCard
|
|
92
|
+
readonly costEstimator?: ModelCostEstimator
|
|
93
|
+
readonly reasoningUsage?: "reported" | "unknown"
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface AzureAIFoundryModel<Protocol extends FoundryProtocol = FoundryProtocol>
|
|
97
|
+
extends LanguageModel {
|
|
98
|
+
readonly protocol: Protocol
|
|
99
|
+
readonly metadata: AzureAIFoundryModelMetadata
|
|
100
|
+
readonly costEstimator: ModelCostEstimator
|
|
101
|
+
resolve(options?: { readonly offline?: boolean }): Promise<AzureAIFoundryModel<Protocol>>
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface AzureAIFoundryProvider extends LanguageModelProvider {
|
|
105
|
+
(deploymentName: string, options?: AzureAIFoundryModelOptions): AzureAIFoundryModel
|
|
106
|
+
responses(
|
|
107
|
+
deploymentName: string,
|
|
108
|
+
options?: AzureAIFoundryModelOptions
|
|
109
|
+
): AzureAIFoundryModel<"responses">
|
|
110
|
+
messages(
|
|
111
|
+
deploymentName: string,
|
|
112
|
+
options?: AzureAIFoundryMessagesOptions
|
|
113
|
+
): AzureAIFoundryModel<"messages">
|
|
114
|
+
readonly catalog: AzureAIFoundryCatalog
|
|
115
|
+
chat(deploymentName: string, options?: AzureAIFoundryChatOptions): AzureAIFoundryModel<"chat">
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function createAzureAIFoundry(options: AzureAIFoundryOptions): AzureAIFoundryProvider {
|
|
119
|
+
const transport = new FoundryTransport(options)
|
|
120
|
+
const providerId = options.providerId ?? "azure-ai-foundry"
|
|
121
|
+
if (!providerId.trim()) throw new TypeError(`${PREFIX} providerId must not be empty.`)
|
|
122
|
+
const deployments = new FoundryDeployments(providerId, transport, options.discovery)
|
|
123
|
+
const modelCatalog = new RemoteModelsDevCatalog(options.catalog)
|
|
124
|
+
const model = <P extends FoundryProtocol = FoundryProtocol>(
|
|
125
|
+
protocol: P | undefined,
|
|
126
|
+
deploymentName: string,
|
|
127
|
+
modelOptions: AzureAIFoundryModelOptions & MessagesRequestOptions & ChatRequestOptions
|
|
128
|
+
) => {
|
|
129
|
+
if (!deploymentName.trim() || deploymentName !== deploymentName.trim())
|
|
130
|
+
throw new TypeError(
|
|
131
|
+
`${PREFIX} deploymentName must be nonempty without surrounding whitespace.`
|
|
132
|
+
)
|
|
133
|
+
return new FoundryModel(
|
|
134
|
+
protocol,
|
|
135
|
+
transport,
|
|
136
|
+
deployments,
|
|
137
|
+
providerId,
|
|
138
|
+
deploymentName,
|
|
139
|
+
modelOptions,
|
|
140
|
+
modelCatalog
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
const responses = (name: string, options: AzureAIFoundryModelOptions = {}) =>
|
|
144
|
+
model("responses", name, options)
|
|
145
|
+
const messages = (name: string, options: AzureAIFoundryMessagesOptions = {}) =>
|
|
146
|
+
model("messages", name, options)
|
|
147
|
+
const chat = (name: string, options: AzureAIFoundryChatOptions = {}) =>
|
|
148
|
+
model("chat", name, options)
|
|
149
|
+
const list = async (protocol?: FoundryProtocol, refresh = false) => {
|
|
150
|
+
if (refresh) modelCatalog.invalidate()
|
|
151
|
+
const records = await deployments.list(refresh)
|
|
152
|
+
const lookup = records.length ? await modelCatalog.lookup() : undefined
|
|
153
|
+
const results: LanguageModelDefinition[] = []
|
|
154
|
+
for (const deployment of records) {
|
|
155
|
+
const name = deployment.name
|
|
156
|
+
try {
|
|
157
|
+
const profile = resolveModel({
|
|
158
|
+
providerId,
|
|
159
|
+
modelId: name,
|
|
160
|
+
protocol,
|
|
161
|
+
options: {},
|
|
162
|
+
deployment,
|
|
163
|
+
catalogModel: lookup?.(deployment.modelName),
|
|
164
|
+
})
|
|
165
|
+
const flags = deployment.capabilities
|
|
166
|
+
const supported =
|
|
167
|
+
profile.metadata.catalog ||
|
|
168
|
+
(profile.protocol === "responses"
|
|
169
|
+
? flags?.responses === "true"
|
|
170
|
+
: profile.protocol === "chat"
|
|
171
|
+
? flags?.chatCompletion === "true" || flags?.chat_completion === "true"
|
|
172
|
+
: flags?.messages === "true")
|
|
173
|
+
if (supported) results.push(profile.definition)
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (!(error instanceof UnsupportedModelFeatureError)) throw error
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return Object.freeze(results)
|
|
179
|
+
}
|
|
180
|
+
const catalog: AzureAIFoundryCatalog = {
|
|
181
|
+
get: async (name, options) => (await list(options?.protocol)).find((d) => d.modelId === name),
|
|
182
|
+
list: (options) => list(options?.protocol),
|
|
183
|
+
refresh: (options) => list(options?.protocol, true),
|
|
184
|
+
deployments: () => deployments.list(),
|
|
185
|
+
}
|
|
186
|
+
const auto = (name: string, options: AzureAIFoundryModelOptions = {}) =>
|
|
187
|
+
model(undefined, name, options)
|
|
188
|
+
return Object.assign(auto, { providerId, catalog, responses, messages, chat })
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
class FoundryModel<Protocol extends FoundryProtocol> implements AzureAIFoundryModel<Protocol> {
|
|
192
|
+
private readonly profile: ReturnType<typeof resolveModel>
|
|
193
|
+
private readonly estimator: ModelCostEstimator
|
|
194
|
+
#execution?: FoundryModel<Protocol>
|
|
195
|
+
#pendingExecution?: Promise<FoundryModel<Protocol>>
|
|
196
|
+
get protocol(): Protocol {
|
|
197
|
+
return (this.#execution?.profile.protocol ?? this.profile.protocol) as Protocol
|
|
198
|
+
}
|
|
199
|
+
get definition() {
|
|
200
|
+
return this.#execution?.profile.definition ?? this.profile.definition
|
|
201
|
+
}
|
|
202
|
+
get metadata() {
|
|
203
|
+
return this.#execution?.profile.metadata ?? this.profile.metadata
|
|
204
|
+
}
|
|
205
|
+
get costEstimator() {
|
|
206
|
+
return this.#execution?.estimator ?? this.estimator
|
|
207
|
+
}
|
|
208
|
+
private readonly options: AzureAIFoundryModelOptions & MessagesRequestOptions & ChatRequestOptions
|
|
209
|
+
private readonly scope: string
|
|
210
|
+
private readonly reliableReasoning: boolean | undefined
|
|
211
|
+
|
|
212
|
+
constructor(
|
|
213
|
+
private readonly requestedProtocol: Protocol | undefined,
|
|
214
|
+
private readonly transport: FoundryTransport,
|
|
215
|
+
private readonly deployments: FoundryDeployments,
|
|
216
|
+
readonly providerId: string,
|
|
217
|
+
readonly modelId: string,
|
|
218
|
+
options: AzureAIFoundryModelOptions & MessagesRequestOptions & ChatRequestOptions,
|
|
219
|
+
private readonly modelCatalog: RemoteModelsDevCatalog,
|
|
220
|
+
private readonly resolution?: ResolvedDeployment & { readonly catalogModel?: CatalogModel }
|
|
221
|
+
) {
|
|
222
|
+
if (options.request !== undefined)
|
|
223
|
+
assertJsonObject(options.request, `${PREFIX} request options`)
|
|
224
|
+
if (options.rateCard && options.costEstimator)
|
|
225
|
+
throw new TypeError(`${PREFIX} Configure either rateCard or costEstimator, not both.`)
|
|
226
|
+
if (
|
|
227
|
+
options.reasoningUsage !== undefined &&
|
|
228
|
+
!["reported", "unknown"].includes(options.reasoningUsage)
|
|
229
|
+
)
|
|
230
|
+
throw new TypeError(`${PREFIX} Invalid reasoningUsage.`)
|
|
231
|
+
this.profile = resolveModel({
|
|
232
|
+
providerId,
|
|
233
|
+
modelId,
|
|
234
|
+
protocol: requestedProtocol,
|
|
235
|
+
options,
|
|
236
|
+
...this.resolution,
|
|
237
|
+
})
|
|
238
|
+
const protocol = this.profile.protocol
|
|
239
|
+
if (protocol === "messages") validateMessagesOptions(options)
|
|
240
|
+
else if (protocol === "chat") validateChatOptions(options)
|
|
241
|
+
else validateOptions(options)
|
|
242
|
+
const { costEstimator, ...serializable } = options
|
|
243
|
+
this.options = {
|
|
244
|
+
...structuredClone(serializable),
|
|
245
|
+
...(costEstimator ? { costEstimator } : {}),
|
|
246
|
+
}
|
|
247
|
+
this.scope = JSON.stringify([
|
|
248
|
+
protocol === "messages" ? transport.url(protocol) : transport.baseUrl,
|
|
249
|
+
this.protocol,
|
|
250
|
+
modelId,
|
|
251
|
+
])
|
|
252
|
+
this.reliableReasoning =
|
|
253
|
+
options.reasoningUsage === "unknown"
|
|
254
|
+
? false
|
|
255
|
+
: options.reasoningUsage === "reported" ||
|
|
256
|
+
this.metadata.publisher?.toLowerCase() === "openai"
|
|
257
|
+
? true
|
|
258
|
+
: undefined
|
|
259
|
+
this.estimator =
|
|
260
|
+
options.costEstimator ??
|
|
261
|
+
(protocol === "messages"
|
|
262
|
+
? foundryMessagesEstimator
|
|
263
|
+
: protocol === "chat"
|
|
264
|
+
? foundryChatEstimator
|
|
265
|
+
: foundryEstimator)(
|
|
266
|
+
this.profile.rateCard,
|
|
267
|
+
this.options.request,
|
|
268
|
+
this.metadata.modelName,
|
|
269
|
+
this.metadata.modelVersion,
|
|
270
|
+
this.profile.aliases
|
|
271
|
+
)
|
|
272
|
+
Object.freeze(this)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async resolve(options?: { readonly offline?: boolean }): Promise<FoundryModel<Protocol>> {
|
|
276
|
+
if (this.resolution) return this
|
|
277
|
+
const resolution = await this.deployments.resolve(this.modelId, options?.offline === true)
|
|
278
|
+
let catalogModel: CatalogModel | undefined
|
|
279
|
+
try {
|
|
280
|
+
catalogModel = await this.modelCatalog.get(resolution.deployment.modelName, options?.offline)
|
|
281
|
+
} catch (error) {
|
|
282
|
+
if (!(error instanceof ModelCatalogUnavailableError)) throw error
|
|
283
|
+
// Azure identity was verified above. Only public enrichment may fall back to
|
|
284
|
+
// cached facts or an explicit definition; malformed metadata still fails.
|
|
285
|
+
catalogModel = await this.modelCatalog.get(resolution.deployment.modelName, true)
|
|
286
|
+
if (!catalogModel && !this.options.definition) throw error
|
|
287
|
+
}
|
|
288
|
+
return new FoundryModel(
|
|
289
|
+
this.requestedProtocol,
|
|
290
|
+
this.transport,
|
|
291
|
+
this.deployments,
|
|
292
|
+
this.providerId,
|
|
293
|
+
this.modelId,
|
|
294
|
+
this.options,
|
|
295
|
+
this.modelCatalog,
|
|
296
|
+
{
|
|
297
|
+
...resolution,
|
|
298
|
+
catalogModel,
|
|
299
|
+
}
|
|
300
|
+
)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
async stream(request: LanguageModelRequest): Promise<LanguageModelStream> {
|
|
304
|
+
request.signal.throwIfAborted()
|
|
305
|
+
if (!this.resolution) {
|
|
306
|
+
// A direct-stream handle pins its first execution too. Workers use resolve() before admission.
|
|
307
|
+
this.#pendingExecution ??= this.resolve()
|
|
308
|
+
.then((model) => {
|
|
309
|
+
this.#execution = model
|
|
310
|
+
return model
|
|
311
|
+
})
|
|
312
|
+
.catch((error) => {
|
|
313
|
+
this.#pendingExecution = undefined
|
|
314
|
+
throw error
|
|
315
|
+
})
|
|
316
|
+
const model = await abortable(() => this.#pendingExecution!, request.signal)
|
|
317
|
+
return model.stream(request)
|
|
318
|
+
}
|
|
319
|
+
const body =
|
|
320
|
+
this.protocol === "messages"
|
|
321
|
+
? foundryMessagesRequest(request, this.definition, this.options, this.scope)
|
|
322
|
+
: this.protocol === "chat"
|
|
323
|
+
? foundryChatRequest(request, this.definition, this.options, this.scope)
|
|
324
|
+
: responsesRequest(
|
|
325
|
+
request,
|
|
326
|
+
this.definition,
|
|
327
|
+
this.options,
|
|
328
|
+
this.scope,
|
|
329
|
+
this.resolution?.deployment
|
|
330
|
+
)
|
|
331
|
+
const response = await this.transport.post(
|
|
332
|
+
JSON.stringify(body),
|
|
333
|
+
request.signal,
|
|
334
|
+
this.providerId,
|
|
335
|
+
this.modelId,
|
|
336
|
+
this.protocol
|
|
337
|
+
)
|
|
338
|
+
const id = this.transport.redactText(response, requestId(response))
|
|
339
|
+
if (!response.body)
|
|
340
|
+
throw new ModelProviderError(
|
|
341
|
+
`${PREFIX} Provider returned an empty streaming response.`,
|
|
342
|
+
this.providerId,
|
|
343
|
+
this.modelId,
|
|
344
|
+
{ requestId: id, status: response.status }
|
|
345
|
+
)
|
|
346
|
+
const streamOptions = {
|
|
347
|
+
providerId: this.providerId,
|
|
348
|
+
modelId: this.modelId,
|
|
349
|
+
requestId: id,
|
|
350
|
+
errorPrefix: PREFIX,
|
|
351
|
+
}
|
|
352
|
+
return {
|
|
353
|
+
events: this.events(
|
|
354
|
+
this.protocol === "messages"
|
|
355
|
+
? messagesEvents(response.body, request.signal, {
|
|
356
|
+
...streamOptions,
|
|
357
|
+
usage: foundryMessagesUsage,
|
|
358
|
+
})
|
|
359
|
+
: this.protocol === "chat"
|
|
360
|
+
? chatEvents(response.body, request.signal, {
|
|
361
|
+
...streamOptions,
|
|
362
|
+
usage: (raw) => foundryChatUsage(raw, this.reliableReasoning),
|
|
363
|
+
})
|
|
364
|
+
: responsesEvents(response.body, request.signal, {
|
|
365
|
+
...streamOptions,
|
|
366
|
+
usage: (raw) => foundryUsage(raw, this.reliableReasoning),
|
|
367
|
+
finishMetadata: (response) => {
|
|
368
|
+
const metadata: JsonObject = {}
|
|
369
|
+
for (const key of [
|
|
370
|
+
"content_filters",
|
|
371
|
+
"prompt_filter_results",
|
|
372
|
+
"incomplete_details",
|
|
373
|
+
]) {
|
|
374
|
+
if (response[key] !== undefined) metadata[key] = response[key]
|
|
375
|
+
}
|
|
376
|
+
return {
|
|
377
|
+
providerData: { [this.providerId]: metadata },
|
|
378
|
+
...(typeof response.model === "string"
|
|
379
|
+
? { route: { modelId: response.model } }
|
|
380
|
+
: {}),
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
}),
|
|
384
|
+
response
|
|
385
|
+
),
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
private async *events(
|
|
390
|
+
source: AsyncIterable<LanguageModelStreamEvent>,
|
|
391
|
+
response: Response
|
|
392
|
+
): AsyncIterable<LanguageModelStreamEvent> {
|
|
393
|
+
try {
|
|
394
|
+
for await (const event of source) {
|
|
395
|
+
if (event.type === "error" && event.error instanceof ModelProviderError) {
|
|
396
|
+
yield { ...event, error: this.transport.redactError(response, event.error) }
|
|
397
|
+
} else if (event.type === "provider-state" && event.providerId === this.providerId) {
|
|
398
|
+
yield { ...event, data: { ...object(event.data), scope: this.scope } }
|
|
399
|
+
} else if ("providerData" in event && event.providerData?.[this.providerId]) {
|
|
400
|
+
yield {
|
|
401
|
+
...event,
|
|
402
|
+
providerData: {
|
|
403
|
+
...event.providerData,
|
|
404
|
+
[this.providerId]: {
|
|
405
|
+
...object(event.providerData[this.providerId]),
|
|
406
|
+
scope: this.scope,
|
|
407
|
+
},
|
|
408
|
+
},
|
|
409
|
+
}
|
|
410
|
+
} else yield event
|
|
411
|
+
}
|
|
412
|
+
} catch (error) {
|
|
413
|
+
throw this.transport.redactFailure(response, error)
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
package/src/publisher.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { LanguageModelDefinition } from "@sixb/core/models"
|
|
2
|
+
|
|
3
|
+
type Publisher = NonNullable<LanguageModelDefinition["publisher"]>
|
|
4
|
+
|
|
5
|
+
const publishers: Readonly<Record<string, Publisher>> = {
|
|
6
|
+
openai: { id: "openai", name: "OpenAI" },
|
|
7
|
+
anthropic: { id: "anthropic", name: "Anthropic" },
|
|
8
|
+
deepseek: { id: "deepseek", name: "DeepSeek" },
|
|
9
|
+
meta: { id: "meta", name: "Meta" },
|
|
10
|
+
microsoft: { id: "microsoft", name: "Microsoft" },
|
|
11
|
+
mistralai: { id: "mistral", name: "Mistral AI" },
|
|
12
|
+
moonshotai: { id: "moonshotai", name: "Moonshot AI" },
|
|
13
|
+
zai: { id: "zai", name: "Z.ai" },
|
|
14
|
+
xai: { id: "spacexai", name: "xAI" },
|
|
15
|
+
fireworks: { id: "fireworks-ai", name: "Fireworks" },
|
|
16
|
+
fireworksai: { id: "fireworks-ai", name: "Fireworks" },
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function azurePublisher(name: string): Publisher {
|
|
20
|
+
const key = name.toLowerCase().replace(/[^a-z0-9]/g, "")
|
|
21
|
+
return Object.hasOwn(publishers, key)
|
|
22
|
+
? publishers[key]!
|
|
23
|
+
: { id: name.toLowerCase().replace(/\s+/g, "-"), name }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Presentation mapping of catalog families, never deployment names or model capabilities. */
|
|
27
|
+
export function fireworksPublisher(family: unknown): Publisher | undefined {
|
|
28
|
+
if (typeof family !== "string") return undefined
|
|
29
|
+
const group = family.split("-")[0]
|
|
30
|
+
if (group === "glm") return publishers.zai
|
|
31
|
+
if (group === "kimi") return publishers.moonshotai
|
|
32
|
+
if (group === "deepseek") return publishers.deepseek
|
|
33
|
+
return undefined
|
|
34
|
+
}
|