@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
package/src/request.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isJsonObject,
|
|
3
|
+
type JsonObject,
|
|
4
|
+
type LanguageModelDefinition,
|
|
5
|
+
type LanguageModelRequest,
|
|
6
|
+
type ModelMessage,
|
|
7
|
+
modelReasoningSupportIssue,
|
|
8
|
+
UnsupportedModelFeatureError,
|
|
9
|
+
} from "@sixb/core/models"
|
|
10
|
+
import { responsesInput } from "@sixb/model-protocols/responses"
|
|
11
|
+
import type { AzureAIFoundryDeployment } from "./discovery"
|
|
12
|
+
import { foundryOutputSchema } from "./structured-output"
|
|
13
|
+
import { object, PREFIX, positiveInteger } from "./util"
|
|
14
|
+
|
|
15
|
+
export interface RequestOptions {
|
|
16
|
+
readonly maxOutputTokens?: number
|
|
17
|
+
readonly request?: JsonObject
|
|
18
|
+
/** Defaults to auto for known OpenAI reasoning models on Responses; false disables summaries. */
|
|
19
|
+
readonly reasoningSummary?: false | "auto" | "concise" | "detailed"
|
|
20
|
+
/** Defaults to true. Disable only for deployments that do not support encrypted replay. */
|
|
21
|
+
readonly encryptedReasoning?: boolean
|
|
22
|
+
/** Aggregate decoded inline image/PDF bytes; defaults to 20 MiB. */
|
|
23
|
+
readonly maxInputFileBytes?: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Summary support is separate from reasoning effort. Keep unknown model identities opt-in.
|
|
27
|
+
// Azure reports the base model name separately from its deployment name and version.
|
|
28
|
+
const SUMMARY_MODELS = new Set(["gpt-5", "gpt-5-mini", "gpt-5-nano", "o3", "o4-mini"])
|
|
29
|
+
|
|
30
|
+
const RESERVED = new Set([
|
|
31
|
+
"model",
|
|
32
|
+
"input",
|
|
33
|
+
"instructions",
|
|
34
|
+
"tools",
|
|
35
|
+
"tool_choice",
|
|
36
|
+
"parallel_tool_calls",
|
|
37
|
+
"stream",
|
|
38
|
+
"store",
|
|
39
|
+
"background",
|
|
40
|
+
"conversation",
|
|
41
|
+
"previous_response_id",
|
|
42
|
+
"include",
|
|
43
|
+
"reasoning",
|
|
44
|
+
"text",
|
|
45
|
+
"max_output_tokens",
|
|
46
|
+
"truncation",
|
|
47
|
+
])
|
|
48
|
+
|
|
49
|
+
export function validateOptions(options: RequestOptions): void {
|
|
50
|
+
positiveInteger(options.maxOutputTokens, "maxOutputTokens")
|
|
51
|
+
positiveInteger(options.maxInputFileBytes, "maxInputFileBytes")
|
|
52
|
+
for (const key of Object.keys(options.request ?? {})) {
|
|
53
|
+
if (RESERVED.has(key))
|
|
54
|
+
throw new TypeError(`${PREFIX} Request option '${key}' is owned by the adapter.`)
|
|
55
|
+
}
|
|
56
|
+
if (options.encryptedReasoning !== undefined && typeof options.encryptedReasoning !== "boolean")
|
|
57
|
+
throw new TypeError(`${PREFIX} encryptedReasoning must be boolean.`)
|
|
58
|
+
if (
|
|
59
|
+
options.reasoningSummary !== undefined &&
|
|
60
|
+
options.reasoningSummary !== false &&
|
|
61
|
+
!["auto", "concise", "detailed"].includes(options.reasoningSummary)
|
|
62
|
+
)
|
|
63
|
+
throw new TypeError(`${PREFIX} Invalid reasoningSummary.`)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function responsesRequest(
|
|
67
|
+
request: LanguageModelRequest,
|
|
68
|
+
definition: LanguageModelDefinition,
|
|
69
|
+
options: RequestOptions,
|
|
70
|
+
scope: string,
|
|
71
|
+
deployment?: AzureAIFoundryDeployment
|
|
72
|
+
): JsonObject {
|
|
73
|
+
const max = prepareRequest(request, definition, options, scope)
|
|
74
|
+
const input = responsesInput(request.messages, definition.providerId).map((item) => {
|
|
75
|
+
if (!isJsonObject(item)) return item
|
|
76
|
+
const content = Array.isArray(item.content)
|
|
77
|
+
? item.content.map((part) => {
|
|
78
|
+
if (isJsonObject(part) && part.type === "input_file" && part.file_data && !part.filename)
|
|
79
|
+
return { ...part, filename: "document.pdf" }
|
|
80
|
+
return part
|
|
81
|
+
})
|
|
82
|
+
: undefined
|
|
83
|
+
return {
|
|
84
|
+
...item,
|
|
85
|
+
...(content ? { content } : {}),
|
|
86
|
+
...(item.role !== undefined ? { type: "message" } : {}),
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
const caps = definition.capabilities
|
|
90
|
+
if (request.tools.length && caps.localTools !== true)
|
|
91
|
+
throw new UnsupportedModelFeatureError(
|
|
92
|
+
`${PREFIX} Configure localTools capability for this deployment before using tools.`
|
|
93
|
+
)
|
|
94
|
+
const schema = request.responseFormat && foundryOutputSchema(request.responseFormat.schema)
|
|
95
|
+
if (request.responseFormat && (caps.nativeStructuredOutput !== true || !schema)) {
|
|
96
|
+
throw new UnsupportedModelFeatureError(
|
|
97
|
+
`${PREFIX} Structured output requires a declared capability and an Azure-compatible strict schema (100 properties, five levels, closed objects, all properties required).`
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
const reasoning = request.reasoning
|
|
101
|
+
const summary =
|
|
102
|
+
options.reasoningSummary ??
|
|
103
|
+
(reasoning !== "none" &&
|
|
104
|
+
caps.reasoning &&
|
|
105
|
+
deployment?.modelPublisher.toLowerCase() === "openai" &&
|
|
106
|
+
SUMMARY_MODELS.has(deployment.modelName.toLowerCase())
|
|
107
|
+
? "auto"
|
|
108
|
+
: undefined)
|
|
109
|
+
if (reasoning !== undefined && reasoning !== "provider-default") {
|
|
110
|
+
const issue = modelReasoningSupportIssue(caps.reasoning, reasoning)
|
|
111
|
+
if (typeof reasoning !== "string" || caps.reasoning === undefined || issue) {
|
|
112
|
+
throw new UnsupportedModelFeatureError(
|
|
113
|
+
`${PREFIX} ${issue ?? "Responses reasoning requires declared named efforts; exact token budgets are unsupported"}.`
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (summary && (!caps.reasoning || typeof caps.reasoning !== "object"))
|
|
118
|
+
throw new UnsupportedModelFeatureError(
|
|
119
|
+
`${PREFIX} Reasoning summaries require a declared reasoning capability.`
|
|
120
|
+
)
|
|
121
|
+
const tools = request.tools.map((tool) => ({
|
|
122
|
+
type: "function",
|
|
123
|
+
name: tool.name,
|
|
124
|
+
description: tool.description,
|
|
125
|
+
parameters: tool.inputSchema,
|
|
126
|
+
strict:
|
|
127
|
+
caps.nativeStructuredOutput === true && foundryOutputSchema(tool.inputSchema) !== undefined,
|
|
128
|
+
}))
|
|
129
|
+
return {
|
|
130
|
+
...options.request,
|
|
131
|
+
model: definition.modelId,
|
|
132
|
+
input,
|
|
133
|
+
stream: true,
|
|
134
|
+
store: false,
|
|
135
|
+
...(options.encryptedReasoning === false ? {} : { include: ["reasoning.encrypted_content"] }),
|
|
136
|
+
...(max === undefined ? {} : { max_output_tokens: max }),
|
|
137
|
+
...(tools.length
|
|
138
|
+
? {
|
|
139
|
+
tools,
|
|
140
|
+
tool_choice: "auto",
|
|
141
|
+
parallel_tool_calls:
|
|
142
|
+
!schema && !tools.some((t) => t.strict) && caps.parallelToolCalls === true,
|
|
143
|
+
}
|
|
144
|
+
: {}),
|
|
145
|
+
...((reasoning !== undefined && reasoning !== "provider-default") || summary
|
|
146
|
+
? {
|
|
147
|
+
reasoning: {
|
|
148
|
+
...(reasoning === undefined || reasoning === "provider-default"
|
|
149
|
+
? {}
|
|
150
|
+
: { effort: reasoning }),
|
|
151
|
+
...(summary ? { summary } : {}),
|
|
152
|
+
},
|
|
153
|
+
}
|
|
154
|
+
: {}),
|
|
155
|
+
...(schema && request.responseFormat
|
|
156
|
+
? {
|
|
157
|
+
text: {
|
|
158
|
+
format: {
|
|
159
|
+
type: "json_schema",
|
|
160
|
+
name: request.responseFormat.name,
|
|
161
|
+
schema,
|
|
162
|
+
strict: true,
|
|
163
|
+
...(request.responseFormat.description === undefined
|
|
164
|
+
? {}
|
|
165
|
+
: { description: request.responseFormat.description }),
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
}
|
|
169
|
+
: {}),
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function prepareRequest(
|
|
174
|
+
request: LanguageModelRequest,
|
|
175
|
+
definition: LanguageModelDefinition,
|
|
176
|
+
options: { readonly maxOutputTokens?: number; readonly maxInputFileBytes?: number },
|
|
177
|
+
scope: string,
|
|
178
|
+
remotePdf = false
|
|
179
|
+
): number | undefined {
|
|
180
|
+
positiveInteger(request.maxOutputTokens, "maxOutputTokens")
|
|
181
|
+
validateMessages(
|
|
182
|
+
request.messages,
|
|
183
|
+
definition,
|
|
184
|
+
options.maxInputFileBytes ?? 20 * 1024 * 1024,
|
|
185
|
+
scope,
|
|
186
|
+
remotePdf
|
|
187
|
+
)
|
|
188
|
+
const limits = [
|
|
189
|
+
definition.maxOutputTokens,
|
|
190
|
+
options.maxOutputTokens,
|
|
191
|
+
request.maxOutputTokens,
|
|
192
|
+
].filter((n): n is number => n !== undefined)
|
|
193
|
+
return limits.length ? Math.min(...limits) : undefined
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function validateMessages(
|
|
197
|
+
messages: readonly ModelMessage[],
|
|
198
|
+
definition: LanguageModelDefinition,
|
|
199
|
+
budget: number,
|
|
200
|
+
scope: string,
|
|
201
|
+
remotePdf = false
|
|
202
|
+
): void {
|
|
203
|
+
let bytes = 0
|
|
204
|
+
for (const message of messages) {
|
|
205
|
+
if (message.role === "system") {
|
|
206
|
+
checkScope(message.providerData?.[definition.providerId], scope)
|
|
207
|
+
continue
|
|
208
|
+
}
|
|
209
|
+
for (const part of message.content) {
|
|
210
|
+
if (part.type === "provider-state") {
|
|
211
|
+
if (part.providerId === definition.providerId) checkScope(part.data, scope)
|
|
212
|
+
continue
|
|
213
|
+
}
|
|
214
|
+
checkScope(part.providerData?.[definition.providerId], scope)
|
|
215
|
+
if (part.type !== "file") continue
|
|
216
|
+
const allowed = definition.capabilities.inputMediaTypes
|
|
217
|
+
const media = part.mediaType
|
|
218
|
+
if (
|
|
219
|
+
!["application/pdf", "image/png", "image/jpeg", "image/webp", "image/gif"].includes(
|
|
220
|
+
media
|
|
221
|
+
) ||
|
|
222
|
+
!(
|
|
223
|
+
allowed === "any" ||
|
|
224
|
+
allowed?.some(
|
|
225
|
+
(type) => type === media || (type === "image/*" && media.startsWith("image/"))
|
|
226
|
+
)
|
|
227
|
+
)
|
|
228
|
+
) {
|
|
229
|
+
throw new UnsupportedModelFeatureError(
|
|
230
|
+
`${PREFIX} Input media '${media}' is not supported by this deployment's configured capabilities.`
|
|
231
|
+
)
|
|
232
|
+
}
|
|
233
|
+
if (part.data.username || part.data.password)
|
|
234
|
+
throw new TypeError(`${PREFIX} File URLs must not contain credentials.`)
|
|
235
|
+
if (part.data.protocol !== "data:") {
|
|
236
|
+
if (
|
|
237
|
+
(!remotePdf && media === "application/pdf") ||
|
|
238
|
+
!["https:", "http:"].includes(part.data.protocol)
|
|
239
|
+
)
|
|
240
|
+
throw new UnsupportedModelFeatureError(
|
|
241
|
+
`${PREFIX} ${remotePdf ? "Use inline base64 or HTTP(S) images/PDFs." : "Use an inline base64 PDF or an HTTP(S)/base64 image."}`
|
|
242
|
+
)
|
|
243
|
+
continue
|
|
244
|
+
}
|
|
245
|
+
const match = /^data:([^;,]+);base64,([A-Za-z0-9+/]*={0,2})$/.exec(part.data.href)
|
|
246
|
+
if (!match || match[1] !== media || !match[2] || match[2].length % 4 !== 0)
|
|
247
|
+
throw new TypeError(
|
|
248
|
+
`${PREFIX} File data must be canonical base64 with a matching media type.`
|
|
249
|
+
)
|
|
250
|
+
const data = match[2]
|
|
251
|
+
bytes += (data.length / 4) * 3 - (data.endsWith("==") ? 2 : data.endsWith("=") ? 1 : 0)
|
|
252
|
+
if (bytes > budget)
|
|
253
|
+
throw new RangeError(`${PREFIX} Inline files exceed maxInputFileBytes (${budget}).`)
|
|
254
|
+
if (Buffer.from(data, "base64").toString("base64") !== data)
|
|
255
|
+
throw new TypeError(`${PREFIX} File data must use canonical base64.`)
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function checkScope(data: unknown, scope: string): void {
|
|
261
|
+
if (data !== undefined && object(data)?.scope !== scope) {
|
|
262
|
+
throw new UnsupportedModelFeatureError(
|
|
263
|
+
`${PREFIX} Replay state belongs to a different endpoint, deployment, or protocol.`
|
|
264
|
+
)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineLanguageModel,
|
|
3
|
+
defineModelRateCard,
|
|
4
|
+
type LanguageModelDefinition,
|
|
5
|
+
UnsupportedModelFeatureError,
|
|
6
|
+
} from "@sixb/core/models"
|
|
7
|
+
import type { CatalogModel } from "./catalog"
|
|
8
|
+
import type { ChatRequestOptions } from "./chat-request"
|
|
9
|
+
import type { AzureAIFoundryDeployment } from "./discovery"
|
|
10
|
+
import type { MessagesRequestOptions } from "./messages-request"
|
|
11
|
+
import type { AzureAIFoundryModelMetadata, AzureAIFoundryModelOptions } from "./provider"
|
|
12
|
+
import { azurePublisher } from "./publisher"
|
|
13
|
+
import type { FoundryProtocol } from "./transport"
|
|
14
|
+
import { PREFIX } from "./util"
|
|
15
|
+
|
|
16
|
+
// One composition path for catalog listing, worker resolution, and direct execution.
|
|
17
|
+
export function resolveModel(input: {
|
|
18
|
+
providerId: string
|
|
19
|
+
modelId: string
|
|
20
|
+
protocol?: FoundryProtocol
|
|
21
|
+
options: AzureAIFoundryModelOptions & ChatRequestOptions & MessagesRequestOptions
|
|
22
|
+
deployment?: AzureAIFoundryDeployment
|
|
23
|
+
discoveredAt?: string
|
|
24
|
+
catalogModel?: CatalogModel
|
|
25
|
+
}) {
|
|
26
|
+
const { deployment, options } = input
|
|
27
|
+
const metadata: AzureAIFoundryModelMetadata = deployment
|
|
28
|
+
? {
|
|
29
|
+
publisher: deployment.modelPublisher,
|
|
30
|
+
modelName: deployment.modelName,
|
|
31
|
+
modelVersion: deployment.modelVersion,
|
|
32
|
+
sku: deployment.sku.name,
|
|
33
|
+
deployment,
|
|
34
|
+
discoveredAt: input.discoveredAt,
|
|
35
|
+
}
|
|
36
|
+
: {}
|
|
37
|
+
const entry = input.catalogModel
|
|
38
|
+
const deploymentPublisher = deployment && azurePublisher(deployment.modelPublisher)
|
|
39
|
+
const publisher =
|
|
40
|
+
deploymentPublisher?.id === "fireworks-ai"
|
|
41
|
+
? (entry?.definition.publisher ?? deploymentPublisher)
|
|
42
|
+
: (deploymentPublisher ?? entry?.definition.publisher)
|
|
43
|
+
const protocol =
|
|
44
|
+
input.protocol ??
|
|
45
|
+
entry?.protocol ??
|
|
46
|
+
(deployment?.capabilities.responses === "true"
|
|
47
|
+
? "responses"
|
|
48
|
+
: deployment?.capabilities.chatCompletion === "true" ||
|
|
49
|
+
deployment?.capabilities.chat_completion === "true"
|
|
50
|
+
? "chat"
|
|
51
|
+
: deployment?.capabilities.messages === "true"
|
|
52
|
+
? "messages"
|
|
53
|
+
: "responses")
|
|
54
|
+
if (protocol === "messages" && deployment?.connectionName)
|
|
55
|
+
throw new UnsupportedModelFeatureError(
|
|
56
|
+
`${PREFIX} Native Messages cannot route a deployment from a project connection; use the owning resource's project URL and API key.`
|
|
57
|
+
)
|
|
58
|
+
const flag =
|
|
59
|
+
deployment?.capabilities[protocol === "chat" ? "chatCompletion" : protocol] ??
|
|
60
|
+
(protocol === "chat" ? deployment?.capabilities.chat_completion : undefined)
|
|
61
|
+
if (flag === "false")
|
|
62
|
+
throw new UnsupportedModelFeatureError(
|
|
63
|
+
`${PREFIX} Deployment '${input.modelId}' reports that ${protocol === "chat" ? "Chat" : protocol === "responses" ? "Responses" : "Messages"} is unsupported.`
|
|
64
|
+
)
|
|
65
|
+
const definition = defineLanguageModel({
|
|
66
|
+
...entry?.definition,
|
|
67
|
+
publisher,
|
|
68
|
+
via: "Azure AI Foundry",
|
|
69
|
+
...options.definition,
|
|
70
|
+
kind: "language",
|
|
71
|
+
providerId: input.providerId,
|
|
72
|
+
modelId: input.modelId,
|
|
73
|
+
capabilities: {
|
|
74
|
+
...entry?.definition.capabilities,
|
|
75
|
+
...options.definition?.capabilities,
|
|
76
|
+
},
|
|
77
|
+
})
|
|
78
|
+
return {
|
|
79
|
+
protocol,
|
|
80
|
+
// Until Azure verifies the deployment, limits are not executable metadata.
|
|
81
|
+
// In particular, Sixb treats advertised context limits as ready for offline resolution.
|
|
82
|
+
definition: deployment
|
|
83
|
+
? effectiveDefinition(definition, protocol, options)
|
|
84
|
+
: defineLanguageModel({
|
|
85
|
+
kind: "language",
|
|
86
|
+
providerId: input.providerId,
|
|
87
|
+
modelId: input.modelId,
|
|
88
|
+
publisher: definition.publisher,
|
|
89
|
+
via: definition.via,
|
|
90
|
+
capabilities: {},
|
|
91
|
+
}),
|
|
92
|
+
metadata: Object.freeze({
|
|
93
|
+
...metadata,
|
|
94
|
+
...(entry
|
|
95
|
+
? {
|
|
96
|
+
catalog: Object.freeze({
|
|
97
|
+
provider: entry.catalogProvider,
|
|
98
|
+
modelId: entry.modelName,
|
|
99
|
+
source: "https://models.dev/api.json",
|
|
100
|
+
pricing: "reference" as const,
|
|
101
|
+
}),
|
|
102
|
+
}
|
|
103
|
+
: {}),
|
|
104
|
+
}),
|
|
105
|
+
rateCard: options.rateCard ? defineModelRateCard(options.rateCard) : entry?.rateCard,
|
|
106
|
+
aliases: Object.freeze([
|
|
107
|
+
...new Set([
|
|
108
|
+
input.modelId,
|
|
109
|
+
...(entry ? [entry.modelName] : []),
|
|
110
|
+
...(metadata.modelName
|
|
111
|
+
? [
|
|
112
|
+
metadata.modelName,
|
|
113
|
+
...(metadata.modelVersion && protocol !== "messages"
|
|
114
|
+
? [`${metadata.modelName}-${metadata.modelVersion}`]
|
|
115
|
+
: []),
|
|
116
|
+
]
|
|
117
|
+
: []),
|
|
118
|
+
]),
|
|
119
|
+
]),
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Catalog facts and explicit overrides still have to fit the selected wire adapter.
|
|
124
|
+
function effectiveDefinition(
|
|
125
|
+
definition: LanguageModelDefinition,
|
|
126
|
+
protocol: FoundryProtocol,
|
|
127
|
+
options: AzureAIFoundryModelOptions & ChatRequestOptions & MessagesRequestOptions
|
|
128
|
+
): LanguageModelDefinition {
|
|
129
|
+
const caps = definition.capabilities
|
|
130
|
+
const media = caps.inputMediaTypes
|
|
131
|
+
const supported = [
|
|
132
|
+
"image/png",
|
|
133
|
+
"image/jpeg",
|
|
134
|
+
"image/webp",
|
|
135
|
+
"image/gif",
|
|
136
|
+
...(protocol === "chat" ? [] : ["application/pdf"]),
|
|
137
|
+
]
|
|
138
|
+
let reasoning = caps.reasoning
|
|
139
|
+
if (reasoning) {
|
|
140
|
+
const { budgetTokens, efforts, ...rest } = reasoning
|
|
141
|
+
const max =
|
|
142
|
+
Math.min(definition.maxOutputTokens ?? Infinity, options.maxOutputTokens ?? Infinity) - 1
|
|
143
|
+
const min = Math.max(1024, budgetTokens?.min ?? 0)
|
|
144
|
+
const budgetMax = Math.min(max, budgetTokens?.max ?? Infinity)
|
|
145
|
+
reasoning = {
|
|
146
|
+
...rest,
|
|
147
|
+
...(efforts && !(protocol === "messages" && options.thinkingMode === "manual")
|
|
148
|
+
? { efforts: efforts.filter((effort) => protocol !== "messages" || effort !== "minimal") }
|
|
149
|
+
: {}),
|
|
150
|
+
...(budgetTokens &&
|
|
151
|
+
protocol === "messages" &&
|
|
152
|
+
options.thinkingMode !== "adaptive" &&
|
|
153
|
+
budgetMax >= min
|
|
154
|
+
? { budgetTokens: { min, ...(Number.isFinite(budgetMax) ? { max: budgetMax } : {}) } }
|
|
155
|
+
: {}),
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return defineLanguageModel({
|
|
159
|
+
...definition,
|
|
160
|
+
capabilities: {
|
|
161
|
+
...caps,
|
|
162
|
+
...(media === undefined
|
|
163
|
+
? {}
|
|
164
|
+
: {
|
|
165
|
+
inputMediaTypes: supported.filter(
|
|
166
|
+
(type) =>
|
|
167
|
+
media === "any" ||
|
|
168
|
+
media.includes(type) ||
|
|
169
|
+
(type.startsWith("image/") && media.includes("image/*"))
|
|
170
|
+
),
|
|
171
|
+
}),
|
|
172
|
+
...(reasoning === undefined ? {} : { reasoning }),
|
|
173
|
+
...(protocol === "chat" && options.profile === "deepseek"
|
|
174
|
+
? { nativeStructuredOutput: false }
|
|
175
|
+
: {}),
|
|
176
|
+
...(caps.providerExecutedTools ? { providerExecutedTools: false } : {}),
|
|
177
|
+
},
|
|
178
|
+
})
|
|
179
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { isJsonObject, type JsonObject } from "@sixb/core/models"
|
|
2
|
+
|
|
3
|
+
const KEYWORDS = new Set([
|
|
4
|
+
"type",
|
|
5
|
+
"description",
|
|
6
|
+
"title",
|
|
7
|
+
"properties",
|
|
8
|
+
"required",
|
|
9
|
+
"additionalProperties",
|
|
10
|
+
"items",
|
|
11
|
+
"enum",
|
|
12
|
+
"anyOf",
|
|
13
|
+
"$ref",
|
|
14
|
+
"$defs",
|
|
15
|
+
"definitions",
|
|
16
|
+
])
|
|
17
|
+
const TYPES = new Set(["object", "array", "string", "number", "integer", "boolean", "null"])
|
|
18
|
+
|
|
19
|
+
/** Azure's documented strict subset. Never weaken the caller's validation contract. */
|
|
20
|
+
export function foundryOutputSchema(schema: JsonObject): JsonObject | undefined {
|
|
21
|
+
// Count the actual wire tree: reusing a JS object at two property locations still sends it twice.
|
|
22
|
+
let root: unknown
|
|
23
|
+
try {
|
|
24
|
+
root = JSON.parse(JSON.stringify(schema))
|
|
25
|
+
} catch {
|
|
26
|
+
return undefined
|
|
27
|
+
}
|
|
28
|
+
if (!isJsonObject(root)) return undefined
|
|
29
|
+
const wire = root
|
|
30
|
+
const counted = new Set<JsonObject>()
|
|
31
|
+
const validated = new Map<JsonObject, Set<number>>()
|
|
32
|
+
let properties = 0
|
|
33
|
+
const reference = (ref: string): JsonObject | undefined => {
|
|
34
|
+
if (ref === "#") return wire
|
|
35
|
+
if (!ref.startsWith("#/")) return undefined
|
|
36
|
+
let value: unknown = wire
|
|
37
|
+
for (const segment of ref.slice(2).split("/")) {
|
|
38
|
+
const key = segment.replaceAll("~1", "/").replaceAll("~0", "~")
|
|
39
|
+
if (!isJsonObject(value) || !Object.hasOwn(value, key)) return undefined
|
|
40
|
+
value = value[key]
|
|
41
|
+
}
|
|
42
|
+
return isJsonObject(value) ? value : undefined
|
|
43
|
+
}
|
|
44
|
+
const walk = (value: unknown, depth: number, active: ReadonlySet<JsonObject>): boolean => {
|
|
45
|
+
if (!isJsonObject(value)) return false
|
|
46
|
+
if (active.has(value)) return value.type === "object" || value.type === "array"
|
|
47
|
+
if (validated.get(value)?.has(depth)) return true
|
|
48
|
+
if (Object.keys(value).some((key) => !KEYWORDS.has(key))) return false
|
|
49
|
+
for (const key of ["description", "title"]) {
|
|
50
|
+
if (value[key] !== undefined && typeof value[key] !== "string") return false
|
|
51
|
+
}
|
|
52
|
+
const next = new Set(active).add(value)
|
|
53
|
+
if (value.$ref !== undefined) {
|
|
54
|
+
if (typeof value.$ref !== "string" || !walk(reference(value.$ref), depth, next)) return false
|
|
55
|
+
}
|
|
56
|
+
const types = typeof value.type === "string" ? [value.type] : value.type
|
|
57
|
+
if (
|
|
58
|
+
types !== undefined &&
|
|
59
|
+
(!Array.isArray(types) ||
|
|
60
|
+
types.length === 0 ||
|
|
61
|
+
types.some((t) => typeof t !== "string" || !TYPES.has(t)) ||
|
|
62
|
+
(Array.isArray(value.type) &&
|
|
63
|
+
(types.length !== 2 || !types.includes("null") || new Set(types).size !== 2)))
|
|
64
|
+
)
|
|
65
|
+
return false
|
|
66
|
+
const objectType = Array.isArray(types) && types.includes("object")
|
|
67
|
+
const arrayType = Array.isArray(types) && types.includes("array")
|
|
68
|
+
const level = depth + (objectType || arrayType ? 1 : 0)
|
|
69
|
+
if (level > 5) return false
|
|
70
|
+
if (objectType) {
|
|
71
|
+
if (
|
|
72
|
+
!isJsonObject(value.properties) ||
|
|
73
|
+
value.additionalProperties !== false ||
|
|
74
|
+
!Array.isArray(value.required)
|
|
75
|
+
)
|
|
76
|
+
return false
|
|
77
|
+
const keys = Object.keys(value.properties)
|
|
78
|
+
const required = value.required
|
|
79
|
+
if (
|
|
80
|
+
required.length !== keys.length ||
|
|
81
|
+
new Set(required).size !== keys.length ||
|
|
82
|
+
keys.some((key) => !required.includes(key))
|
|
83
|
+
)
|
|
84
|
+
return false
|
|
85
|
+
if (!counted.has(value)) {
|
|
86
|
+
counted.add(value)
|
|
87
|
+
properties += keys.length
|
|
88
|
+
}
|
|
89
|
+
if (
|
|
90
|
+
properties > 100 ||
|
|
91
|
+
!Object.values(value.properties).every((child) => walk(child, level, next))
|
|
92
|
+
)
|
|
93
|
+
return false
|
|
94
|
+
} else if (
|
|
95
|
+
value.properties !== undefined ||
|
|
96
|
+
value.required !== undefined ||
|
|
97
|
+
value.additionalProperties !== undefined
|
|
98
|
+
)
|
|
99
|
+
return false
|
|
100
|
+
if (arrayType && !walk(value.items, level, next)) return false
|
|
101
|
+
if (!arrayType && value.items !== undefined) return false
|
|
102
|
+
if (
|
|
103
|
+
value.anyOf !== undefined &&
|
|
104
|
+
(!Array.isArray(value.anyOf) ||
|
|
105
|
+
value.anyOf.length === 0 ||
|
|
106
|
+
!value.anyOf.every((child) => walk(child, level, next)))
|
|
107
|
+
)
|
|
108
|
+
return false
|
|
109
|
+
if (value.type === undefined && value.$ref === undefined && value.anyOf === undefined)
|
|
110
|
+
return false
|
|
111
|
+
if (
|
|
112
|
+
value.enum !== undefined &&
|
|
113
|
+
(!Array.isArray(value.enum) ||
|
|
114
|
+
value.enum.length === 0 ||
|
|
115
|
+
value.enum.some((entry) => typeof entry === "object" && entry !== null))
|
|
116
|
+
)
|
|
117
|
+
return false
|
|
118
|
+
for (const key of ["$defs", "definitions"]) {
|
|
119
|
+
const definitions = value[key]
|
|
120
|
+
if (
|
|
121
|
+
definitions !== undefined &&
|
|
122
|
+
(!isJsonObject(definitions) ||
|
|
123
|
+
!Object.values(definitions).every((child) => walk(child, 0, next)))
|
|
124
|
+
)
|
|
125
|
+
return false
|
|
126
|
+
}
|
|
127
|
+
const depths = validated.get(value) ?? new Set<number>()
|
|
128
|
+
depths.add(depth)
|
|
129
|
+
validated.set(value, depths)
|
|
130
|
+
return true
|
|
131
|
+
}
|
|
132
|
+
return wire.type === "object" && wire.anyOf === undefined && walk(wire, 0, new Set())
|
|
133
|
+
? schema
|
|
134
|
+
: undefined
|
|
135
|
+
}
|