@sixb/azure-ai-foundry 0.2.0 → 0.2.5

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.
Files changed (47) hide show
  1. package/README.md +187 -2
  2. package/dist/chat-request.d.ts.map +1 -1
  3. package/dist/decision.d.ts +18 -0
  4. package/dist/decision.d.ts.map +1 -0
  5. package/dist/discovery.d.ts +3 -0
  6. package/dist/discovery.d.ts.map +1 -1
  7. package/dist/embedding.d.ts +1 -1
  8. package/dist/embedding.d.ts.map +1 -1
  9. package/dist/image.d.ts +20 -0
  10. package/dist/image.d.ts.map +1 -0
  11. package/dist/index.d.ts +5 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +1249 -99
  14. package/dist/index.js.map +18 -12
  15. package/dist/kinds.d.ts +13 -0
  16. package/dist/kinds.d.ts.map +1 -0
  17. package/dist/messages-request.d.ts +14 -1
  18. package/dist/messages-request.d.ts.map +1 -1
  19. package/dist/provider.d.ts +15 -0
  20. package/dist/provider.d.ts.map +1 -1
  21. package/dist/request.d.ts +6 -1
  22. package/dist/request.d.ts.map +1 -1
  23. package/dist/reranking.d.ts +18 -0
  24. package/dist/reranking.d.ts.map +1 -0
  25. package/dist/speech.d.ts +20 -0
  26. package/dist/speech.d.ts.map +1 -0
  27. package/dist/structured-output.d.ts.map +1 -1
  28. package/dist/transcription.d.ts +20 -0
  29. package/dist/transcription.d.ts.map +1 -0
  30. package/dist/transport.d.ts +10 -2
  31. package/dist/transport.d.ts.map +1 -1
  32. package/package.json +3 -3
  33. package/src/chat-request.ts +18 -20
  34. package/src/decision.ts +211 -0
  35. package/src/discovery.ts +18 -0
  36. package/src/embedding.ts +15 -36
  37. package/src/image.ts +457 -0
  38. package/src/index.ts +8 -0
  39. package/src/kinds.ts +100 -0
  40. package/src/messages-request.ts +39 -8
  41. package/src/provider.ts +121 -31
  42. package/src/request.ts +24 -11
  43. package/src/reranking.ts +264 -0
  44. package/src/speech.ts +390 -0
  45. package/src/structured-output.ts +8 -0
  46. package/src/transcription.ts +322 -0
  47. package/src/transport.ts +90 -11
@@ -5,7 +5,12 @@ import {
5
5
  modelReasoningSupportIssue,
6
6
  UnsupportedModelFeatureError,
7
7
  } from "@sixb/core/models"
8
- import { messagesInput } from "@sixb/model-protocols/messages"
8
+ import {
9
+ messagesInput,
10
+ messagesStrictTools,
11
+ StrictToolRefusals,
12
+ strictToolsKey,
13
+ } from "@sixb/model-protocols/messages"
9
14
  import { messagesOutputSchema } from "./messages-schema"
10
15
  import { prepareRequest } from "./request"
11
16
  import { object, PREFIX, positiveInteger } from "./util"
@@ -18,6 +23,15 @@ export interface MessagesRequestOptions {
18
23
  readonly thinkingMode?: "adaptive" | "manual"
19
24
  }
20
25
 
26
+ /** Strict tool sets Claude on Foundry refused: sent unconstrained from the start next time. */
27
+ export const strictToolRefusals = new StrictToolRefusals()
28
+
29
+ export interface FoundryMessagesRequest {
30
+ readonly body: JsonObject
31
+ /** The key of the strict tools it sends, if any, for {@link strictToolRefusals}. */
32
+ readonly strictTools?: string
33
+ }
34
+
21
35
  const RESERVED = new Set([
22
36
  "model",
23
37
  "messages",
@@ -58,12 +72,18 @@ export function validateMessagesOptions(options: MessagesRequestOptions): void {
58
72
  }
59
73
  }
60
74
 
75
+ /**
76
+ * `unconstrained`: no strict tools, as after Claude refused this request's. Otherwise a tool is
77
+ * strict when it asks, the deployment declares native structured output, and it fits Anthropic's
78
+ * limits, in order (`messagesStrictTools`).
79
+ */
61
80
  export function foundryMessagesRequest(
62
81
  request: LanguageModelRequest,
63
82
  definition: LanguageModelDefinition,
64
83
  options: MessagesRequestOptions,
65
- scope: string
66
- ): JsonObject {
84
+ scope: string,
85
+ unconstrained = false
86
+ ): FoundryMessagesRequest {
67
87
  const max = prepareRequest(request, definition, options, scope, true)
68
88
  let conversation = false
69
89
  for (const message of request.messages) {
@@ -101,7 +121,19 @@ export function foundryMessagesRequest(
101
121
  ...(thinking.effort === undefined ? {} : { effort: thinking.effort }),
102
122
  ...(schema ? { format: { type: "json_schema", schema } } : {}),
103
123
  }
104
- return {
124
+ const candidates = request.tools.map((tool) => ({
125
+ strict: tool.strict === true && caps.nativeStructuredOutput === true,
126
+ schema: messagesOutputSchema(tool.inputSchema),
127
+ }))
128
+ const chosen = messagesStrictTools(candidates, schema)
129
+ const constrained = request.tools.flatMap((tool, index) =>
130
+ chosen[index] ? [{ name: tool.name, input_schema: tool.inputSchema }] : []
131
+ )
132
+ const key = constrained.length === 0 ? undefined : strictToolsKey(constrained)
133
+ const refused = unconstrained || (key !== undefined && strictToolRefusals.has(key))
134
+ const strict = chosen.map((choice) => choice && !refused)
135
+ const strictTools = refused ? undefined : key
136
+ const body: JsonObject = {
105
137
  ...options.request,
106
138
  model: definition.modelId,
107
139
  messages: mapped.messages,
@@ -112,18 +144,17 @@ export function foundryMessagesRequest(
112
144
  ...(Object.keys(output).length ? { output_config: output } : {}),
113
145
  ...(request.tools.length
114
146
  ? {
115
- tools: request.tools.map((tool) => ({
147
+ tools: request.tools.map((tool, index) => ({
116
148
  name: tool.name,
117
149
  description: tool.description,
118
150
  input_schema: tool.inputSchema,
119
- ...(caps.nativeStructuredOutput === true && messagesOutputSchema(tool.inputSchema)
120
- ? { strict: true }
121
- : {}),
151
+ ...(strict[index] ? { strict: true } : {}),
122
152
  })),
123
153
  tool_choice: { type: "auto", disable_parallel_tool_use: caps.parallelToolCalls !== true },
124
154
  }
125
155
  : {}),
126
156
  }
157
+ return { body, ...(strictTools === undefined ? {} : { strictTools }) }
127
158
  }
128
159
 
129
160
  function reasoningRequest(
package/src/provider.ts CHANGED
@@ -17,7 +17,7 @@ import {
17
17
  UnsupportedModelFeatureError,
18
18
  } from "@sixb/core/models"
19
19
  import { chatEvents } from "@sixb/model-protocols/chat"
20
- import { messagesEvents } from "@sixb/model-protocols/messages"
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
23
  import {
@@ -27,6 +27,11 @@ import {
27
27
  } from "./catalog"
28
28
  import { foundryChatEstimator, foundryChatUsage } from "./chat-accounting"
29
29
  import { type ChatRequestOptions, foundryChatRequest, validateChatOptions } from "./chat-request"
30
+ import {
31
+ type AzureAIFoundryDecisionModel,
32
+ type AzureAIFoundryDecisionOptions,
33
+ createFoundryDecision,
34
+ } from "./decision"
30
35
  import {
31
36
  type AzureAIFoundryCatalog,
32
37
  type AzureAIFoundryDeployment,
@@ -40,14 +45,36 @@ import {
40
45
  type AzureAIFoundryEmbeddingTransportOptions,
41
46
  createFoundryEmbedding,
42
47
  } from "./embedding"
48
+ import {
49
+ type AzureAIFoundryImageModel,
50
+ type AzureAIFoundryImageOptions,
51
+ createFoundryImage,
52
+ } from "./image"
53
+ import { article, deploymentKind, KIND_BINDINGS } from "./kinds"
43
54
  import { foundryMessagesEstimator, foundryMessagesUsage } from "./messages-accounting"
44
55
  import {
45
56
  foundryMessagesRequest,
46
57
  type MessagesRequestOptions,
58
+ strictToolRefusals,
47
59
  validateMessagesOptions,
48
60
  } from "./messages-request"
49
61
  import { type RequestOptions, responsesRequest, validateOptions } from "./request"
62
+ import {
63
+ type AzureAIFoundryRerankingModel,
64
+ type AzureAIFoundryRerankingOptions,
65
+ createFoundryReranking,
66
+ } from "./reranking"
50
67
  import { resolveModel } from "./resolution"
68
+ import {
69
+ type AzureAIFoundrySpeechModel,
70
+ type AzureAIFoundrySpeechOptions,
71
+ createFoundrySpeech,
72
+ } from "./speech"
73
+ import {
74
+ type AzureAIFoundryTranscriptionModel,
75
+ type AzureAIFoundryTranscriptionOptions,
76
+ createFoundryTranscription,
77
+ } from "./transcription"
51
78
  import {
52
79
  abortable,
53
80
  type FoundryProtocol,
@@ -130,6 +157,25 @@ export interface AzureAIFoundryProvider extends LanguageModelProvider {
130
157
  deploymentName: string,
131
158
  options: AzureAIFoundryEmbeddingOptions
132
159
  ): AzureAIFoundryEmbeddingModel
160
+ /** A deployment of a decision model, such as Microsoft-Decision-1, for `models.decision`. */
161
+ decision(
162
+ deploymentName: string,
163
+ options?: AzureAIFoundryDecisionOptions
164
+ ): AzureAIFoundryDecisionModel
165
+ /** A transcription deployment, such as gpt-4o-transcribe, for `models.audio.transcription`. */
166
+ transcription(
167
+ deploymentName: string,
168
+ options?: AzureAIFoundryTranscriptionOptions
169
+ ): AzureAIFoundryTranscriptionModel
170
+ /** A Cohere rerank deployment, such as Cohere-rerank-v5-fast, for `models.reranking`. */
171
+ reranking(
172
+ deploymentName: string,
173
+ options?: AzureAIFoundryRerankingOptions
174
+ ): AzureAIFoundryRerankingModel
175
+ /** A speech deployment, such as gpt-4o-mini-tts, for `models.audio.speech`. */
176
+ speech(deploymentName: string, options?: AzureAIFoundrySpeechOptions): AzureAIFoundrySpeechModel
177
+ /** An image deployment, such as gpt-image-1.5 or FLUX.1-Kontext-pro, for `models.image`. */
178
+ image(deploymentName: string, options?: AzureAIFoundryImageOptions): AzureAIFoundryImageModel
133
179
  readonly catalog: AzureAIFoundryCatalog
134
180
  chat(deploymentName: string, options?: AzureAIFoundryChatOptions): AzureAIFoundryModel<"chat">
135
181
  }
@@ -182,6 +228,16 @@ export function createAzureAIFoundry(options: AzureAIFoundryOptions): AzureAIFou
182
228
  model("messages", name, options)
183
229
  const chat = (name: string, options: AzureAIFoundryChatOptions = {}) =>
184
230
  model("chat", name, options)
231
+ const decision = (name: string, options: AzureAIFoundryDecisionOptions = {}) =>
232
+ createFoundryDecision(providerId, name, options, transport, deployments)
233
+ const transcription = (name: string, options: AzureAIFoundryTranscriptionOptions = {}) =>
234
+ createFoundryTranscription(providerId, name, options, transport, deployments)
235
+ const reranking = (name: string, options: AzureAIFoundryRerankingOptions = {}) =>
236
+ createFoundryReranking(providerId, name, options, transport, deployments)
237
+ const image = (name: string, options: AzureAIFoundryImageOptions = {}) =>
238
+ createFoundryImage(providerId, name, options, transport, deployments)
239
+ const speech = (name: string, options: AzureAIFoundrySpeechOptions = {}) =>
240
+ createFoundrySpeech(providerId, name, options, transport, deployments)
185
241
  const list = async (protocol?: FoundryProtocol, refresh = false) => {
186
242
  if (refresh) modelCatalog.invalidate()
187
243
  const records = await deployments.list(refresh)
@@ -189,8 +245,9 @@ export function createAzureAIFoundry(options: AzureAIFoundryOptions): AzureAIFou
189
245
  const results: LanguageModelDefinition[] = []
190
246
  for (const deployment of records) {
191
247
  const name = deployment.name
248
+ // Listings hold language models; every other kind has its own binding.
192
249
  if (
193
- isEmbeddingDeployment(deployment) ||
250
+ deploymentKind(deployment) !== "language" ||
194
251
  (await modelCatalog.getEmbedding(deployment.modelName, true))
195
252
  )
196
253
  continue
@@ -226,7 +283,19 @@ export function createAzureAIFoundry(options: AzureAIFoundryOptions): AzureAIFou
226
283
  }
227
284
  const auto = (name: string, options: AzureAIFoundryModelOptions = {}) =>
228
285
  model(undefined, name, options)
229
- return Object.assign(auto, { providerId, catalog, responses, messages, chat, embedding })
286
+ return Object.assign(auto, {
287
+ providerId,
288
+ catalog,
289
+ responses,
290
+ messages,
291
+ chat,
292
+ embedding,
293
+ decision,
294
+ transcription,
295
+ reranking,
296
+ speech,
297
+ image,
298
+ })
230
299
  }
231
300
 
232
301
  class FoundryModel<Protocol extends FoundryProtocol> implements AzureAIFoundryModel<Protocol> {
@@ -330,9 +399,10 @@ class FoundryModel<Protocol extends FoundryProtocol> implements AzureAIFoundryMo
330
399
  async resolve(options?: { readonly offline?: boolean }): Promise<FoundryModel<Protocol>> {
331
400
  if (this.resolution) return this
332
401
  const resolution = await this.deployments.resolve(this.modelId, options?.offline === true)
333
- if (isEmbeddingDeployment(resolution.deployment))
402
+ const kind = deploymentKind(resolution.deployment)
403
+ if (kind !== "language")
334
404
  throw new UnsupportedModelFeatureError(
335
- `${PREFIX} Use foundry.embedding() for an embedding deployment.`
405
+ `${PREFIX} Use ${KIND_BINDINGS[kind]} for ${article(kind)} ${kind} deployment.`
336
406
  )
337
407
  let catalogModel: CatalogModel | undefined
338
408
  try {
@@ -379,25 +449,26 @@ class FoundryModel<Protocol extends FoundryProtocol> implements AzureAIFoundryMo
379
449
  const model = await abortable(() => this.#pendingExecution!, request.signal)
380
450
  return model.stream(request)
381
451
  }
382
- const body =
452
+ const response =
383
453
  this.protocol === "messages"
384
- ? foundryMessagesRequest(request, this.definition, this.options, this.scope)
385
- : this.protocol === "chat"
386
- ? foundryChatRequest(request, this.definition, this.options, this.scope)
387
- : responsesRequest(
388
- request,
389
- this.definition,
390
- this.options,
391
- this.scope,
392
- this.resolution?.deployment
393
- )
394
- const response = await this.transport.post(
395
- JSON.stringify(body),
396
- request.signal,
397
- this.providerId,
398
- this.modelId,
399
- this.protocol
400
- )
454
+ ? 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
+ )
401
472
  const id = this.transport.redactText(response, requestId(response))
402
473
  if (!response.body)
403
474
  throw new ModelProviderError(
@@ -449,6 +520,33 @@ class FoundryModel<Protocol extends FoundryProtocol> implements AzureAIFoundryMo
449
520
  }
450
521
  }
451
522
 
523
+ /** A limit no count predicts, such as the grammar's compiled size: once more, unconstrained. */
524
+ private async postMessages(request: LanguageModelRequest): Promise<Response> {
525
+ const prepared = foundryMessagesRequest(request, this.definition, this.options, this.scope)
526
+ const post = (body: JsonObject) =>
527
+ this.transport.post(
528
+ JSON.stringify(body),
529
+ request.signal,
530
+ this.providerId,
531
+ this.modelId,
532
+ "messages"
533
+ )
534
+ try {
535
+ return await post(prepared.body)
536
+ } catch (error) {
537
+ if (
538
+ prepared.strictTools === undefined ||
539
+ !(error instanceof ModelProviderError) ||
540
+ !isStrictToolLimitError(error)
541
+ )
542
+ throw error
543
+ strictToolRefusals.add(prepared.strictTools)
544
+ return post(
545
+ foundryMessagesRequest(request, this.definition, this.options, this.scope, true).body
546
+ )
547
+ }
548
+ }
549
+
452
550
  private async *events(
453
551
  source: AsyncIterable<LanguageModelStreamEvent>,
454
552
  response: Response
@@ -477,11 +575,3 @@ class FoundryModel<Protocol extends FoundryProtocol> implements AzureAIFoundryMo
477
575
  }
478
576
  }
479
577
  }
480
-
481
- function isEmbeddingDeployment(deployment: AzureAIFoundryDeployment): boolean {
482
- return (
483
- deployment.capabilities.embeddings === "true" ||
484
- deployment.capabilities.embedding === "true" ||
485
- /^(text-embedding-|cohere-embed)/i.test(deployment.modelName)
486
- )
487
- }
package/src/request.ts CHANGED
@@ -4,10 +4,11 @@ import {
4
4
  type LanguageModelDefinition,
5
5
  type LanguageModelRequest,
6
6
  type ModelMessage,
7
+ type ModelToolSpecification,
7
8
  modelReasoningSupportIssue,
8
9
  UnsupportedModelFeatureError,
9
10
  } from "@sixb/core/models"
10
- import { responsesInput } from "@sixb/model-protocols/responses"
11
+ import { requiredNullableSchema, responsesInput } from "@sixb/model-protocols/responses"
11
12
  import type { AzureAIFoundryDeployment } from "./discovery"
12
13
  import { foundryOutputSchema } from "./structured-output"
13
14
  import { object, PREFIX, positiveInteger } from "./util"
@@ -118,14 +119,16 @@ export function responsesRequest(
118
119
  throw new UnsupportedModelFeatureError(
119
120
  `${PREFIX} Reasoning summaries require a declared reasoning capability.`
120
121
  )
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
- }))
122
+ const tools = request.tools.map((tool) => {
123
+ const schema = caps.nativeStructuredOutput === true ? foundryStrictTool(tool) : undefined
124
+ return {
125
+ type: "function",
126
+ name: tool.name,
127
+ description: tool.description,
128
+ parameters: schema ?? tool.inputSchema,
129
+ strict: schema !== undefined,
130
+ }
131
+ })
129
132
  return {
130
133
  ...options.request,
131
134
  model: definition.modelId,
@@ -138,8 +141,8 @@ export function responsesRequest(
138
141
  ? {
139
142
  tools,
140
143
  tool_choice: "auto",
141
- parallel_tool_calls:
142
- !schema && !tools.some((t) => t.strict) && caps.parallelToolCalls === true,
144
+ // Strict tools keep parallel calls (measured on Azure, November 2026).
145
+ parallel_tool_calls: !schema && caps.parallelToolCalls === true,
143
146
  }
144
147
  : {}),
145
148
  ...((reasoning !== undefined && reasoning !== "provider-default") || summary
@@ -264,3 +267,13 @@ function checkScope(data: unknown, scope: string): void {
264
267
  )
265
268
  }
266
269
  }
270
+
271
+ /**
272
+ * A tool that asked for strict arguments, in the form OpenAI's strict mode takes (every property
273
+ * required, an optional one nullable), when it fits Azure's strict subset.
274
+ */
275
+ export function foundryStrictTool(tool: ModelToolSpecification): JsonObject | undefined {
276
+ if (!tool.strict) return undefined
277
+ const schema = requiredNullableSchema(tool.inputSchema)
278
+ return foundryOutputSchema(schema) === undefined ? undefined : schema
279
+ }
@@ -0,0 +1,264 @@
1
+ import {
2
+ defineModelRateCard,
3
+ estimateModelReservation,
4
+ type ModelCostEstimator,
5
+ ModelProviderError,
6
+ type ModelRateCard,
7
+ type ModelUsage,
8
+ type RerankingModel,
9
+ type RerankingModelDefinition,
10
+ type RerankingModelRequest,
11
+ RerankingModelResponseError,
12
+ type RerankingModelResponseMetadata,
13
+ type RerankingModelResult,
14
+ type RerankingResult,
15
+ rateModelCall,
16
+ } from "@sixb/core/models"
17
+ import {
18
+ type AzureAIFoundryDeployment,
19
+ deploymentMetadata,
20
+ type FoundryDeployments,
21
+ type ResolvedDeployment,
22
+ } from "./discovery"
23
+ import { assertDeploymentKind } from "./kinds"
24
+ import type { AzureAIFoundryModelMetadata } from "./provider"
25
+ import { azurePublisher } from "./publisher"
26
+ import { abortable, type FoundryTransport, readJson, requestId } from "./transport"
27
+ import { counter, object, PREFIX, positiveInteger } from "./util"
28
+
29
+ export interface AzureAIFoundryRerankingOptions {
30
+ /** Deadline for one reranking, response included. Default: 30,000 ms. */
31
+ readonly timeoutMs?: number
32
+ readonly rateCard?: ModelRateCard
33
+ readonly costEstimator?: ModelCostEstimator
34
+ }
35
+
36
+ export interface AzureAIFoundryRerankingModel extends RerankingModel {
37
+ readonly metadata: AzureAIFoundryModelMetadata
38
+ resolve(options?: { readonly offline?: boolean }): Promise<AzureAIFoundryRerankingModel>
39
+ }
40
+
41
+ /** 10,000 scores answer in well under 1 MiB. */
42
+ const MAX_RESPONSE_BYTES = 4 * 1024 * 1024
43
+
44
+ /** AbortSignal.timeout's largest delay. */
45
+ const MAX_TIMEOUT_MS = 2 ** 31 - 1
46
+
47
+ /**
48
+ * Azure Retail Prices (verified 2026-11-13), by deployment SKU: Rerank v5 bills input tokens,
49
+ * USD per million. Rerank v4 bills USD 2 (fast) and 2.50 (pro) per thousand searches, a unit
50
+ * Sixb's rate cards cannot hold yet, so its cost stays unknown.
51
+ */
52
+ const PUBLISHED: Readonly<Record<string, Readonly<Record<string, ModelRateCard>>>> = {
53
+ // Rerank V5 Fast {Glbl,DZ} 1M Tokens
54
+ "cohere-rerank-v5-fast": {
55
+ GlobalStandard: { currency: "USD", input: "0.02", output: "0" },
56
+ DataZoneStandard: { currency: "USD", input: "0.022", output: "0" },
57
+ },
58
+ // Rerank V5 Pro {Glbl,DZ} 1M Tokens
59
+ "cohere-rerank-v5-pro": {
60
+ GlobalStandard: { currency: "USD", input: "0.05", output: "0" },
61
+ DataZoneStandard: { currency: "USD", input: "0.055", output: "0" },
62
+ },
63
+ }
64
+
65
+ interface RerankingSettings {
66
+ readonly timeoutMs: number
67
+ readonly rateCard?: ModelRateCard
68
+ readonly costEstimator?: ModelCostEstimator
69
+ }
70
+
71
+ interface RerankingResolution extends ResolvedDeployment {
72
+ readonly costEstimator?: ModelCostEstimator
73
+ }
74
+
75
+ export function createFoundryReranking(
76
+ providerId: string,
77
+ modelId: string,
78
+ options: AzureAIFoundryRerankingOptions,
79
+ transport: FoundryTransport,
80
+ deployments: FoundryDeployments
81
+ ): AzureAIFoundryRerankingModel {
82
+ if (!modelId.trim() || modelId !== modelId.trim())
83
+ throw new TypeError(`${PREFIX} deploymentName must be nonempty without surrounding whitespace.`)
84
+ positiveInteger(options.timeoutMs, "timeoutMs")
85
+ if (options.timeoutMs !== undefined && options.timeoutMs > MAX_TIMEOUT_MS)
86
+ throw new TypeError(`${PREFIX} timeoutMs must be at most ${MAX_TIMEOUT_MS}.`)
87
+ if (options.rateCard && options.costEstimator)
88
+ throw new TypeError(`${PREFIX} Configure either rateCard or costEstimator, not both.`)
89
+ // Validate and snapshot now: later edits to the caller's object change nothing.
90
+ const settings: RerankingSettings = Object.freeze({
91
+ timeoutMs: options.timeoutMs ?? 30_000,
92
+ ...(options.rateCard ? { rateCard: defineModelRateCard(options.rateCard) } : {}),
93
+ ...(options.costEstimator ? { costEstimator: options.costEstimator } : {}),
94
+ })
95
+ return rerankingBinding(providerId, modelId, settings, transport, deployments)
96
+ }
97
+
98
+ /** Discovery pins the deployed model for pricing; inference posts Cohere's v2 rerank request. */
99
+ function rerankingBinding(
100
+ providerId: string,
101
+ modelId: string,
102
+ settings: RerankingSettings,
103
+ transport: FoundryTransport,
104
+ deployments: FoundryDeployments,
105
+ resolution?: RerankingResolution
106
+ ): AzureAIFoundryRerankingModel {
107
+ const deployment = resolution?.deployment
108
+ const definition: RerankingModelDefinition = Object.freeze({
109
+ kind: "reranking",
110
+ providerId,
111
+ modelId,
112
+ ...(deployment ? { publisher: azurePublisher(deployment.modelPublisher) } : {}),
113
+ via: "Azure AI Foundry",
114
+ })
115
+
116
+ let pending: Promise<AzureAIFoundryRerankingModel> | undefined
117
+ const binding: AzureAIFoundryRerankingModel = Object.freeze({
118
+ providerId,
119
+ modelId,
120
+ definition,
121
+ metadata: deploymentMetadata(resolution),
122
+ ...(resolution?.costEstimator ? { costEstimator: resolution.costEstimator } : {}),
123
+
124
+ async resolve(input?: { readonly offline?: boolean }) {
125
+ if (resolution) return binding
126
+ const resolved = await deployments.resolve(modelId, input?.offline === true)
127
+ assertDeploymentKind(resolved.deployment, modelId, "reranking")
128
+ const rateCard = settings.rateCard ?? publishedRateCard(resolved.deployment)
129
+ const costEstimator =
130
+ settings.costEstimator ?? (rateCard ? rerankingEstimator(rateCard) : undefined)
131
+ return rerankingBinding(providerId, modelId, settings, transport, deployments, {
132
+ ...resolved,
133
+ ...(costEstimator ? { costEstimator } : {}),
134
+ })
135
+ },
136
+
137
+ async rerank(request: RerankingModelRequest): Promise<RerankingModelResult> {
138
+ const signal = AbortSignal.any([
139
+ ...(request.signal ? [request.signal] : []),
140
+ AbortSignal.timeout(settings.timeoutMs),
141
+ ])
142
+ signal.throwIfAborted()
143
+ if (
144
+ typeof request.query !== "string" ||
145
+ !request.query.trim() ||
146
+ !Array.isArray(request.documents) ||
147
+ request.documents.some((text) => typeof text !== "string" || !text.trim())
148
+ )
149
+ throw new TypeError(`${PREFIX} Reranking requires a query and nonempty document strings.`)
150
+ const documents = [...request.documents]
151
+ if (!documents.length) return { results: [] }
152
+ if (!resolution) {
153
+ // A direct call pins the deployment once, as the runtime does before admission.
154
+ pending ??= binding.resolve().catch((error) => {
155
+ pending = undefined
156
+ throw error
157
+ })
158
+ const resolved = await abortable(() => pending!, signal)
159
+ return resolved.rerank({ query: request.query, documents, signal })
160
+ }
161
+
162
+ // Without top_n, Cohere scores every document, which Sixb requires.
163
+ const response = await transport.post(
164
+ JSON.stringify({ model: modelId, query: request.query, documents }),
165
+ signal,
166
+ providerId,
167
+ modelId,
168
+ "reranking"
169
+ )
170
+ const id = transport.redactText(response, requestId(response))
171
+ let body: unknown
172
+ try {
173
+ body = await readJson(response, signal, MAX_RESPONSE_BYTES)
174
+ } catch {
175
+ // The parse error can quote the body; only the status and request ID travel.
176
+ if (signal.aborted) throw signal.reason
177
+ throw new ModelProviderError(
178
+ `${PREFIX} Reranking returned invalid JSON.`,
179
+ providerId,
180
+ modelId,
181
+ { code: "invalid_json", requestId: id, status: response.status }
182
+ )
183
+ }
184
+ const payload = object(body)
185
+ const responseId = typeof payload?.id === "string" && payload.id ? payload.id : undefined
186
+ const metadata: RerankingModelResponseMetadata = {
187
+ usage: rerankingUsage(payload),
188
+ ...(id || responseId
189
+ ? {
190
+ providerIds: {
191
+ ...(id ? { requestId: id } : {}),
192
+ ...(responseId ? { responseId } : {}),
193
+ },
194
+ }
195
+ : {}),
196
+ }
197
+ const results = rerankingResults(payload?.results, documents.length)
198
+ if (!results)
199
+ throw new RerankingModelResponseError(
200
+ `${PREFIX} Invalid reranking response: expected one finite score per document with unique indices.`,
201
+ providerId,
202
+ modelId,
203
+ metadata
204
+ )
205
+ return { ...metadata, results }
206
+ },
207
+ })
208
+ return binding
209
+ }
210
+
211
+ function rerankingResults(value: unknown, count: number): RerankingResult[] | undefined {
212
+ if (!Array.isArray(value) || value.length !== count) return undefined
213
+ const seen = new Set<number>()
214
+ const results: RerankingResult[] = []
215
+ for (const entry of value) {
216
+ const index = object(entry)?.index
217
+ const score = object(entry)?.relevance_score
218
+ if (
219
+ typeof index !== "number" ||
220
+ !Number.isSafeInteger(index) ||
221
+ index < 0 ||
222
+ index >= count ||
223
+ seen.has(index) ||
224
+ typeof score !== "number" ||
225
+ !Number.isFinite(score)
226
+ )
227
+ return undefined
228
+ seen.add(index)
229
+ results.push({ index, score })
230
+ }
231
+ return results
232
+ }
233
+
234
+ /** Rerank v5 bills `input_tokens`; v4 bills `search_units`, kept in `raw` only. */
235
+ function rerankingUsage(payload: Record<string, unknown> | undefined): ModelUsage {
236
+ const billed = object(object(payload?.meta)?.billed_units)
237
+ const inputTokens = counter(billed?.input_tokens)
238
+ return {
239
+ ...(inputTokens === undefined ? {} : { inputTokens, outputTokens: 0 }),
240
+ ...(billed ? { raw: billed } : {}),
241
+ }
242
+ }
243
+
244
+ function publishedRateCard(deployment: AzureAIFoundryDeployment): ModelRateCard | undefined {
245
+ const skus = PUBLISHED[deployment.modelName.toLowerCase()]
246
+ const card =
247
+ skus && deployment.modelVersion === "1" && Object.hasOwn(skus, deployment.sku.name)
248
+ ? skus[deployment.sku.name]
249
+ : undefined
250
+ return card ? defineModelRateCard(card) : undefined
251
+ }
252
+
253
+ function rerankingEstimator(rateCard: ModelRateCard): ModelCostEstimator {
254
+ return {
255
+ estimateReservation: (tokens) => estimateModelReservation({ ...tokens, rateCard }),
256
+ estimate: ({ usage }) => {
257
+ // A search-unit bill is not a token bill, whatever the card; a zero count bills nothing.
258
+ const raw = usage.raw ?? {}
259
+ if (Object.entries(raw).some(([key, count]) => key !== "input_tokens" && count !== 0))
260
+ return { status: "unpriceable", reason: "missing-rate-card" }
261
+ return rateModelCall({ usage, rateCard })
262
+ },
263
+ }
264
+ }