@sixb/azure-ai-foundry 0.2.6 → 0.2.7

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.
@@ -7,6 +7,7 @@ import {
7
7
  } from "@sixb/core/models"
8
8
  import {
9
9
  messagesInput,
10
+ messagesPromptCaching,
10
11
  messagesStrictTools,
11
12
  StrictToolRefusals,
12
13
  strictToolsKey,
@@ -84,9 +85,9 @@ export function foundryMessagesRequest(
84
85
  scope: string,
85
86
  unconstrained = false
86
87
  ): FoundryMessagesRequest {
87
- const max = prepareRequest(request, definition, options, scope, true)
88
+ const { max, messages } = prepareRequest(request, definition, options, scope, true)
88
89
  let conversation = false
89
- for (const message of request.messages) {
90
+ for (const message of messages) {
90
91
  if (message.role === "system" && conversation)
91
92
  throw new UnsupportedModelFeatureError(
92
93
  `${PREFIX} Messages system instructions must precede the conversation.`
@@ -116,7 +117,7 @@ export function foundryMessagesRequest(
116
117
  throw new UnsupportedModelFeatureError(
117
118
  `${PREFIX} Explicit sampling controls cannot be combined with Messages thinking.`
118
119
  )
119
- const mapped = messagesInput(request.messages, definition.providerId, PREFIX)
120
+ const mapped = messagesInput(messages, definition.providerId, PREFIX)
120
121
  const output: JsonObject = {
121
122
  ...(thinking.effort === undefined ? {} : { effort: thinking.effort }),
122
123
  ...(schema ? { format: { type: "json_schema", schema } } : {}),
@@ -133,27 +134,34 @@ export function foundryMessagesRequest(
133
134
  const refused = unconstrained || (key !== undefined && strictToolRefusals.has(key))
134
135
  const strict = chosen.map((choice) => choice && !refused)
135
136
  const strictTools = refused ? undefined : key
136
- const body: JsonObject = {
137
- ...options.request,
138
- model: definition.modelId,
139
- messages: mapped.messages,
140
- ...(mapped.system.length ? { system: mapped.system } : {}),
141
- stream: true,
142
- max_tokens: max,
143
- ...(thinking.thinking ? { thinking: thinking.thinking } : {}),
144
- ...(Object.keys(output).length ? { output_config: output } : {}),
145
- ...(request.tools.length
146
- ? {
147
- tools: request.tools.map((tool, index) => ({
148
- name: tool.name,
149
- description: tool.description,
150
- input_schema: tool.inputSchema,
151
- ...(strict[index] ? { strict: true } : {}),
152
- })),
153
- tool_choice: { type: "auto", disable_parallel_tool_use: caps.parallelToolCalls !== true },
154
- }
155
- : {}),
156
- }
137
+ const body = messagesPromptCaching(
138
+ {
139
+ ...options.request,
140
+ model: definition.modelId,
141
+ messages: mapped.messages,
142
+ ...(mapped.system.length ? { system: mapped.system } : {}),
143
+ stream: true,
144
+ max_tokens: max,
145
+ ...(thinking.thinking ? { thinking: thinking.thinking } : {}),
146
+ ...(Object.keys(output).length ? { output_config: output } : {}),
147
+ ...(request.tools.length
148
+ ? {
149
+ tools: request.tools.map((tool, index) => ({
150
+ name: tool.name,
151
+ description: tool.description,
152
+ input_schema: tool.inputSchema,
153
+ ...(strict[index] ? { strict: true } : {}),
154
+ })),
155
+ tool_choice: {
156
+ type: "auto",
157
+ disable_parallel_tool_use: caps.parallelToolCalls !== true,
158
+ },
159
+ }
160
+ : {}),
161
+ },
162
+ request.caching,
163
+ request.tools.length
164
+ )
157
165
  return { body, ...(strictTools === undefined ? {} : { strictTools }) }
158
166
  }
159
167
 
package/src/provider.ts CHANGED
@@ -20,6 +20,7 @@ import { chatEvents } from "@sixb/model-protocols/chat"
20
20
  import { isStrictToolLimitError, messagesEvents } from "@sixb/model-protocols/messages"
21
21
  import { responsesEvents } from "@sixb/model-protocols/responses"
22
22
  import { foundryEstimator, foundryUsage, type PricedEstimator } from "./accounting"
23
+ import { refusedCaching } from "./caching"
23
24
  import {
24
25
  type AzureAIFoundryCatalogOptions,
25
26
  type CatalogModel,
@@ -42,7 +43,6 @@ import {
42
43
  import {
43
44
  type AzureAIFoundryEmbeddingModel,
44
45
  type AzureAIFoundryEmbeddingOptions,
45
- type AzureAIFoundryEmbeddingTransportOptions,
46
46
  createFoundryEmbedding,
47
47
  } from "./embedding"
48
48
  import {
@@ -84,9 +84,14 @@ import {
84
84
  } from "./transport"
85
85
  import { object, PREFIX } from "./util"
86
86
 
87
- export interface AzureAIFoundryOptions extends TransportOptions {
88
- /** Explicit resource transport: project endpoints do not route embeddings. */
89
- readonly embeddings?: AzureAIFoundryEmbeddingTransportOptions
87
+ export interface AzureAIFoundryOptions extends Omit<TransportOptions, "endpoint"> {
88
+ /**
89
+ * The project endpoint from the Foundry portal,
90
+ * `https://<resource>.services.ai.azure.com/api/projects/<project>`.
91
+ * Default: `AZURE_AI_FOUNDRY_ENDPOINT`. Without `credential`, `apiKey` defaults to
92
+ * `AZURE_AI_FOUNDRY_API_KEY`, read for each call.
93
+ */
94
+ readonly endpoint?: string
90
95
  /** Public model capabilities and reference pricing from models.dev. */
91
96
  readonly catalog?: AzureAIFoundryCatalogOptions
92
97
  /** Namespace bindings from different resources/projects in a single Sixb catalog. */
@@ -180,29 +185,29 @@ export interface AzureAIFoundryProvider extends LanguageModelProvider {
180
185
  chat(deploymentName: string, options?: AzureAIFoundryChatOptions): AzureAIFoundryModel<"chat">
181
186
  }
182
187
 
183
- export function createAzureAIFoundry(options: AzureAIFoundryOptions): AzureAIFoundryProvider {
184
- const transport = new FoundryTransport(options)
188
+ export function createAzureAIFoundry(options: AzureAIFoundryOptions = {}): AzureAIFoundryProvider {
189
+ if ("embeddings" in options)
190
+ throw new TypeError(
191
+ `${PREFIX} Remove the embeddings option: embedding deployments are called on the project's resource with its credentials.`
192
+ )
193
+ const endpoint = options.endpoint ?? process.env.AZURE_AI_FOUNDRY_ENDPOINT
194
+ if (!endpoint)
195
+ throw new TypeError(
196
+ `${PREFIX} endpoint is required: pass the project endpoint or set AZURE_AI_FOUNDRY_ENDPOINT.`
197
+ )
198
+ const transport = new FoundryTransport({
199
+ ...options,
200
+ endpoint,
201
+ ...(options.credential || options.apiKey !== undefined
202
+ ? {}
203
+ : { apiKey: () => process.env.AZURE_AI_FOUNDRY_API_KEY }),
204
+ })
185
205
  const providerId = options.providerId ?? "azure-ai-foundry"
186
206
  if (!providerId.trim()) throw new TypeError(`${PREFIX} providerId must not be empty.`)
187
207
  const deployments = new FoundryDeployments(providerId, transport, options.discovery)
188
208
  const modelCatalog = new RemoteModelsDevCatalog(options.catalog)
189
- const embeddingTransport = options.embeddings
190
- ? new FoundryTransport(options.embeddings, "resource")
191
- : undefined
192
- const embedding = (name: string, input: AzureAIFoundryEmbeddingOptions) => {
193
- if (!embeddingTransport)
194
- throw new TypeError(
195
- `${PREFIX} Configure embeddings.endpoint and embeddings.apiKey before declaring an embedding model.`
196
- )
197
- return createFoundryEmbedding(
198
- providerId,
199
- name,
200
- input,
201
- embeddingTransport,
202
- deployments,
203
- modelCatalog
204
- )
205
- }
209
+ const embedding = (name: string, input: AzureAIFoundryEmbeddingOptions) =>
210
+ createFoundryEmbedding(providerId, name, input, transport, deployments, modelCatalog)
206
211
  const model = <P extends FoundryProtocol = FoundryProtocol>(
207
212
  protocol: P | undefined,
208
213
  deploymentName: string,
@@ -247,6 +252,7 @@ export function createAzureAIFoundry(options: AzureAIFoundryOptions): AzureAIFou
247
252
  const name = deployment.name
248
253
  // Listings hold language models; every other kind has its own binding.
249
254
  if (
255
+ deployment.connectionName ||
250
256
  deploymentKind(deployment) !== "language" ||
251
257
  (await modelCatalog.getEmbedding(deployment.modelName, true))
252
258
  )
@@ -260,14 +266,16 @@ export function createAzureAIFoundry(options: AzureAIFoundryOptions): AzureAIFou
260
266
  deployment,
261
267
  catalogModel: lookup?.(deployment.modelName),
262
268
  })
269
+ // Without catalog facts, Azure's flags say whether the deployment chats at all; they
270
+ // name no protocol reliably, so an automatic listing accepts any of them.
263
271
  const flags = deployment.capabilities
272
+ const offers = (offered: FoundryProtocol) =>
273
+ offered === "chat"
274
+ ? flags.chatCompletion === "true" || flags.chat_completion === "true"
275
+ : flags[offered] === "true"
264
276
  const supported =
265
277
  profile.metadata.catalog ||
266
- (profile.protocol === "responses"
267
- ? flags?.responses === "true"
268
- : profile.protocol === "chat"
269
- ? flags?.chatCompletion === "true" || flags?.chat_completion === "true"
270
- : flags?.messages === "true")
278
+ (protocol ? offers(protocol) : (["responses", "chat", "messages"] as const).some(offers))
271
279
  if (supported) results.push(profile.definition)
272
280
  } catch (error) {
273
281
  if (!(error instanceof UnsupportedModelFeatureError)) throw error
@@ -368,11 +376,7 @@ class FoundryModel<Protocol extends FoundryProtocol> implements AzureAIFoundryMo
368
376
  ...structuredClone(serializable),
369
377
  ...(costEstimator ? { costEstimator } : {}),
370
378
  }
371
- this.scope = JSON.stringify([
372
- protocol === "messages" ? transport.url(protocol) : transport.baseUrl,
373
- this.protocol,
374
- modelId,
375
- ])
379
+ this.scope = JSON.stringify([transport.url(protocol), protocol, modelId])
376
380
  this.reliableReasoning =
377
381
  options.reasoningUsage === "unknown"
378
382
  ? false
@@ -452,23 +456,7 @@ class FoundryModel<Protocol extends FoundryProtocol> implements AzureAIFoundryMo
452
456
  const response =
453
457
  this.protocol === "messages"
454
458
  ? await this.postMessages(request)
455
- : await this.transport.post(
456
- JSON.stringify(
457
- this.protocol === "chat"
458
- ? foundryChatRequest(request, this.definition, this.options, this.scope)
459
- : responsesRequest(
460
- request,
461
- this.definition,
462
- this.options,
463
- this.scope,
464
- this.resolution?.deployment
465
- )
466
- ),
467
- request.signal,
468
- this.providerId,
469
- this.modelId,
470
- this.protocol
471
- )
459
+ : await this.postOpenAICompatible(request)
472
460
  const id = this.transport.redactText(response, requestId(response))
473
461
  if (!response.body)
474
462
  throw new ModelProviderError(
@@ -520,6 +508,33 @@ class FoundryModel<Protocol extends FoundryProtocol> implements AzureAIFoundryMo
520
508
  }
521
509
  }
522
510
 
511
+ /** A caching setting Azure refuses for this deployment: once more, without it. */
512
+ private async postOpenAICompatible(request: LanguageModelRequest): Promise<Response> {
513
+ const deployment = this.resolution?.deployment
514
+ for (let attempt = 0; ; attempt++) {
515
+ const body =
516
+ this.protocol === "chat"
517
+ ? foundryChatRequest(request, this.definition, this.options, this.scope, deployment)
518
+ : responsesRequest(request, this.definition, this.options, this.scope, deployment)
519
+ try {
520
+ return await this.transport.post(
521
+ JSON.stringify(body),
522
+ request.signal,
523
+ this.providerId,
524
+ this.modelId,
525
+ this.protocol
526
+ )
527
+ } catch (error) {
528
+ if (
529
+ attempt >= 3 ||
530
+ !(error instanceof ModelProviderError) ||
531
+ !refusedCaching(error, body, this.options.request, this.scope)
532
+ )
533
+ throw error
534
+ }
535
+ }
536
+ }
537
+
523
538
  /** A limit no count predicts, such as the grammar's compiled size: once more, unconstrained. */
524
539
  private async postMessages(request: LanguageModelRequest): Promise<Response> {
525
540
  const prepared = foundryMessagesRequest(request, this.definition, this.options, this.scope)
package/src/request.ts CHANGED
@@ -1,14 +1,18 @@
1
1
  import {
2
2
  isJsonObject,
3
3
  type JsonObject,
4
+ type JsonValue,
4
5
  type LanguageModelDefinition,
5
6
  type LanguageModelRequest,
7
+ type ModelAssistantPart,
6
8
  type ModelMessage,
7
9
  type ModelToolSpecification,
8
10
  modelReasoningSupportIssue,
11
+ type ProviderData,
9
12
  UnsupportedModelFeatureError,
10
13
  } from "@sixb/core/models"
11
14
  import { requiredNullableSchema, responsesInput } from "@sixb/model-protocols/responses"
15
+ import { promptCaching } from "./caching"
12
16
  import type { AzureAIFoundryDeployment } from "./discovery"
13
17
  import { foundryOutputSchema } from "./structured-output"
14
18
  import { object, PREFIX, positiveInteger } from "./util"
@@ -71,8 +75,8 @@ export function responsesRequest(
71
75
  scope: string,
72
76
  deployment?: AzureAIFoundryDeployment
73
77
  ): JsonObject {
74
- const max = prepareRequest(request, definition, options, scope)
75
- const replayed = responsesInput(request.messages, definition.providerId, definition.modelId)
78
+ const { max, messages } = prepareRequest(request, definition, options, scope)
79
+ const replayed = responsesInput(messages, definition.providerId, definition.modelId)
76
80
  const input = replayed.map((item) => {
77
81
  if (!isJsonObject(item)) return item
78
82
  const content = Array.isArray(item.content)
@@ -137,6 +141,14 @@ export function responsesRequest(
137
141
  stream: true,
138
142
  store: false,
139
143
  ...(options.encryptedReasoning === false ? {} : { include: ["reasoning.encrypted_content"] }),
144
+ ...promptCaching({
145
+ caching: request.caching,
146
+ messages: request.messages,
147
+ localTools: request.tools.length,
148
+ deployment,
149
+ request: options.request,
150
+ scope,
151
+ }),
140
152
  ...(max === undefined ? {} : { max_output_tokens: max }),
141
153
  ...(tools.length
142
154
  ? {
@@ -174,19 +186,19 @@ export function responsesRequest(
174
186
  }
175
187
  }
176
188
 
189
+ /** The output ceiling, and the messages to send: validated, with only this scope's replay state. */
177
190
  export function prepareRequest(
178
191
  request: LanguageModelRequest,
179
192
  definition: LanguageModelDefinition,
180
193
  options: { readonly maxOutputTokens?: number; readonly maxInputFileBytes?: number },
181
194
  scope: string,
182
195
  remotePdf = false
183
- ): number | undefined {
196
+ ): { readonly max: number | undefined; readonly messages: readonly ModelMessage[] } {
184
197
  positiveInteger(request.maxOutputTokens, "maxOutputTokens")
185
198
  validateMessages(
186
199
  request.messages,
187
200
  definition,
188
201
  options.maxInputFileBytes ?? 20 * 1024 * 1024,
189
- scope,
190
202
  remotePdf
191
203
  )
192
204
  const limits = [
@@ -194,28 +206,56 @@ export function prepareRequest(
194
206
  options.maxOutputTokens,
195
207
  request.maxOutputTokens,
196
208
  ].filter((n): n is number => n !== undefined)
197
- return limits.length ? Math.min(...limits) : undefined
209
+ return {
210
+ max: limits.length ? Math.min(...limits) : undefined,
211
+ messages: ownReplayState(request.messages, definition.providerId, scope),
212
+ }
213
+ }
214
+
215
+ /**
216
+ * Replay state goes back only where it was recorded: the address, deployment and protocol that
217
+ * `scope` names. State recorded anywhere else, such as another deployment's encrypted reasoning
218
+ * after a conversation switches models, is left out; its visible text and tool calls still replay.
219
+ */
220
+ function ownReplayState(
221
+ messages: readonly ModelMessage[],
222
+ providerId: string,
223
+ scope: string
224
+ ): ModelMessage[] {
225
+ const foreign = (data: JsonValue | undefined) =>
226
+ data !== undefined && object(data)?.scope !== scope
227
+ const own = <T extends { readonly providerData?: ProviderData }>(part: T): T => {
228
+ if (!part.providerData || !foreign(part.providerData[providerId])) return part
229
+ const { [providerId]: _foreign, ...others } = part.providerData
230
+ return { ...part, providerData: others }
231
+ }
232
+ return messages.map((message): ModelMessage => {
233
+ if (message.role === "system") return own(message)
234
+ if (message.role === "user") return { ...message, content: message.content.map(own) }
235
+ if (message.role === "tool") return { ...message, content: message.content.map(own) }
236
+ return {
237
+ ...message,
238
+ content: message.content.flatMap((part): ModelAssistantPart[] =>
239
+ part.type !== "provider-state"
240
+ ? [own(part)]
241
+ : part.providerId === providerId && foreign(part.data)
242
+ ? []
243
+ : [part]
244
+ ),
245
+ }
246
+ })
198
247
  }
199
248
 
200
249
  export function validateMessages(
201
250
  messages: readonly ModelMessage[],
202
251
  definition: LanguageModelDefinition,
203
252
  budget: number,
204
- scope: string,
205
253
  remotePdf = false
206
254
  ): void {
207
255
  let bytes = 0
208
256
  for (const message of messages) {
209
- if (message.role === "system") {
210
- checkScope(message.providerData?.[definition.providerId], scope)
211
- continue
212
- }
257
+ if (message.role === "system") continue
213
258
  for (const part of message.content) {
214
- if (part.type === "provider-state") {
215
- if (part.providerId === definition.providerId) checkScope(part.data, scope)
216
- continue
217
- }
218
- checkScope(part.providerData?.[definition.providerId], scope)
219
259
  if (part.type !== "file") continue
220
260
  const allowed = definition.capabilities.inputMediaTypes
221
261
  const media = part.mediaType
@@ -261,14 +301,6 @@ export function validateMessages(
261
301
  }
262
302
  }
263
303
 
264
- function checkScope(data: unknown, scope: string): void {
265
- if (data !== undefined && object(data)?.scope !== scope) {
266
- throw new UnsupportedModelFeatureError(
267
- `${PREFIX} Replay state belongs to a different endpoint, deployment, or protocol.`
268
- )
269
- }
270
- }
271
-
272
304
  /**
273
305
  * A tool that asked for strict arguments, in the form OpenAI's strict mode takes (every property
274
306
  * required, an optional one nullable), when it fits Azure's strict subset.
package/src/resolution.ts CHANGED
@@ -42,20 +42,7 @@ export function resolveModel(input: {
42
42
  ? (entry?.definition.publisher ?? deploymentPublisher)
43
43
  : (deploymentPublisher ?? entry?.definition.publisher)
44
44
  const protocol =
45
- input.protocol ??
46
- entry?.protocol ??
47
- (deployment?.capabilities.responses === "true"
48
- ? "responses"
49
- : deployment?.capabilities.chatCompletion === "true" ||
50
- deployment?.capabilities.chat_completion === "true"
51
- ? "chat"
52
- : deployment?.capabilities.messages === "true"
53
- ? "messages"
54
- : "responses")
55
- if (protocol === "messages" && deployment?.connectionName)
56
- throw new UnsupportedModelFeatureError(
57
- `${PREFIX} Native Messages cannot route a deployment from a project connection; use the owning resource's project URL and API key.`
58
- )
45
+ input.protocol ?? (deployment ? publisherProtocol(deployment.modelPublisher) : "responses")
59
46
  const flag =
60
47
  deployment?.capabilities[protocol === "chat" ? "chatCompletion" : protocol] ??
61
48
  (protocol === "chat" ? deployment?.capabilities.chat_completion : undefined)
@@ -124,6 +111,18 @@ export function resolveModel(input: {
124
111
  }
125
112
  }
126
113
 
114
+ /**
115
+ * The protocol that serves a publisher's models fully. Azure's capability flags cannot choose:
116
+ * every chat deployment reports `chat_completion` alone, Claude included. OpenAI models reason
117
+ * with tools only on Responses (gpt-6.2-sol refuses both on Chat); Claude answers Messages, not
118
+ * Chat; other publishers take reasoning efforts on Chat, which Kimi and DeepSeek refuse on
119
+ * Responses (measured November 2026).
120
+ */
121
+ function publisherProtocol(publisher: string): FoundryProtocol {
122
+ const name = publisher.toLowerCase()
123
+ return name === "openai" ? "responses" : name === "anthropic" ? "messages" : "chat"
124
+ }
125
+
127
126
  // Catalog facts and explicit overrides still have to fit the selected wire adapter.
128
127
  function effectiveDefinition(
129
128
  definition: LanguageModelDefinition,