@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,134 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
JsonObject,
|
|
3
|
+
LanguageModelRateCard,
|
|
4
|
+
ModelCostEstimator,
|
|
5
|
+
ModelUsage,
|
|
6
|
+
} from "@sixb/core/models"
|
|
7
|
+
import { tokenEstimator } from "./accounting"
|
|
8
|
+
import { counter, object } from "./util"
|
|
9
|
+
|
|
10
|
+
// Interpret Chat counters directly. Cache reads overlap prompt_tokens; reasoning overlaps
|
|
11
|
+
// completion_tokens. A missing partition is unknown, even when a rate card supplies its price.
|
|
12
|
+
export function foundryChatUsage(
|
|
13
|
+
raw: JsonObject | undefined,
|
|
14
|
+
reliableReasoning: boolean | undefined
|
|
15
|
+
): ModelUsage {
|
|
16
|
+
const input = counter(raw?.prompt_tokens)
|
|
17
|
+
const output = counter(raw?.completion_tokens)
|
|
18
|
+
const read = counter(object(raw?.prompt_tokens_details)?.cached_tokens)
|
|
19
|
+
const reported = reasoningTokens(raw)
|
|
20
|
+
const reasoning =
|
|
21
|
+
reported !== undefined &&
|
|
22
|
+
reported !== false &&
|
|
23
|
+
(reliableReasoning === true || (reliableReasoning === undefined && reported > 0))
|
|
24
|
+
? reported
|
|
25
|
+
: undefined
|
|
26
|
+
const known = tokenMeters(raw)
|
|
27
|
+
return {
|
|
28
|
+
...(input === undefined ? {} : { inputTokens: input }),
|
|
29
|
+
...(output === undefined ? {} : { outputTokens: output }),
|
|
30
|
+
...(read === undefined ? {} : { cacheReadInputTokens: read }),
|
|
31
|
+
...(object(raw?.prompt_tokens_details)?.cache_write_tokens === 0
|
|
32
|
+
? { cacheWriteInputTokens: 0 }
|
|
33
|
+
: {}),
|
|
34
|
+
...(input !== undefined && read !== undefined && read <= input && known
|
|
35
|
+
? { uncachedInputTokens: input - read }
|
|
36
|
+
: {}),
|
|
37
|
+
...(reasoning === undefined ? {} : { reasoningOutputTokens: reasoning }),
|
|
38
|
+
...(output !== undefined && reasoning !== undefined && reasoning <= output && known
|
|
39
|
+
? { textOutputTokens: output - reasoning }
|
|
40
|
+
: {}),
|
|
41
|
+
...(raw ? { raw } : {}),
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function foundryChatEstimator(
|
|
46
|
+
card: LanguageModelRateCard | undefined,
|
|
47
|
+
request: JsonObject | undefined,
|
|
48
|
+
modelName?: string,
|
|
49
|
+
modelVersion?: string,
|
|
50
|
+
aliases?: readonly string[]
|
|
51
|
+
): ModelCostEstimator {
|
|
52
|
+
const estimator = tokenEstimator(
|
|
53
|
+
card,
|
|
54
|
+
request,
|
|
55
|
+
["temperature", "top_p", "metadata"],
|
|
56
|
+
tokenMeters,
|
|
57
|
+
modelName,
|
|
58
|
+
modelVersion,
|
|
59
|
+
aliases
|
|
60
|
+
)
|
|
61
|
+
return {
|
|
62
|
+
estimateReservation: estimator.estimateReservation,
|
|
63
|
+
estimate: (input) =>
|
|
64
|
+
estimator.estimate({
|
|
65
|
+
...input,
|
|
66
|
+
usage: { ...foundryChatUsage(input.usage.raw, false), ...input.usage },
|
|
67
|
+
}),
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function tokenMeters(raw: JsonObject | undefined): boolean {
|
|
72
|
+
if (!raw) return true
|
|
73
|
+
if (reasoningTokens(raw) === false) return false
|
|
74
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
75
|
+
if (["prompt_tokens", "completion_tokens", "total_tokens"].includes(key)) {
|
|
76
|
+
if (counter(value) === undefined) return false
|
|
77
|
+
} else if (
|
|
78
|
+
key === "prompt_tokens_details" ||
|
|
79
|
+
key === "completion_tokens_details" ||
|
|
80
|
+
key === "output_tokens_details"
|
|
81
|
+
) {
|
|
82
|
+
if (value === null) continue
|
|
83
|
+
const details = object(value)
|
|
84
|
+
if (!details) return false
|
|
85
|
+
for (const [meter, count] of Object.entries(details)) {
|
|
86
|
+
if (
|
|
87
|
+
(key === "prompt_tokens_details" && meter === "cached_tokens") ||
|
|
88
|
+
(key !== "prompt_tokens_details" && meter === "reasoning_tokens")
|
|
89
|
+
) {
|
|
90
|
+
if (counter(count) === undefined) return false
|
|
91
|
+
} else if (key === "prompt_tokens_details" && meter === "image_tokens") {
|
|
92
|
+
// Image tokens are already included in prompt_tokens, at the input token rate.
|
|
93
|
+
const image = counter(count)
|
|
94
|
+
const input = counter(raw.prompt_tokens)
|
|
95
|
+
if (image === undefined || input === undefined || image > input) return false
|
|
96
|
+
} else if (
|
|
97
|
+
![
|
|
98
|
+
"audio_tokens",
|
|
99
|
+
...(key === "prompt_tokens_details"
|
|
100
|
+
? ["cache_write_tokens"]
|
|
101
|
+
: ["accepted_prediction_tokens", "rejected_prediction_tokens"]),
|
|
102
|
+
].includes(meter) ||
|
|
103
|
+
count !== 0
|
|
104
|
+
)
|
|
105
|
+
return false
|
|
106
|
+
}
|
|
107
|
+
} else if (key !== "reasoning_tokens") return false
|
|
108
|
+
}
|
|
109
|
+
return true
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Azure Chat backends may repeat the same reasoning partition in Responses-style
|
|
113
|
+
// details or at the top level. Accept only valid, consistent aliases within the total.
|
|
114
|
+
function reasoningTokens(raw: JsonObject | undefined): number | undefined | false {
|
|
115
|
+
let result: number | undefined
|
|
116
|
+
for (const value of [
|
|
117
|
+
object(raw?.completion_tokens_details)?.reasoning_tokens,
|
|
118
|
+
object(raw?.output_tokens_details)?.reasoning_tokens,
|
|
119
|
+
raw?.reasoning_tokens,
|
|
120
|
+
]) {
|
|
121
|
+
if (value === undefined) continue
|
|
122
|
+
const count = counter(value)
|
|
123
|
+
const output = counter(raw?.completion_tokens)
|
|
124
|
+
if (
|
|
125
|
+
count === undefined ||
|
|
126
|
+
output === undefined ||
|
|
127
|
+
count > output ||
|
|
128
|
+
(result !== undefined && result !== count)
|
|
129
|
+
)
|
|
130
|
+
return false
|
|
131
|
+
result = count
|
|
132
|
+
}
|
|
133
|
+
return result
|
|
134
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type JsonObject,
|
|
3
|
+
type LanguageModelDefinition,
|
|
4
|
+
type LanguageModelRequest,
|
|
5
|
+
modelReasoningSupportIssue,
|
|
6
|
+
UnsupportedModelFeatureError,
|
|
7
|
+
} from "@sixb/core/models"
|
|
8
|
+
import { chatInput } from "@sixb/model-protocols/chat"
|
|
9
|
+
import { prepareRequest } from "./request"
|
|
10
|
+
import { foundryOutputSchema } from "./structured-output"
|
|
11
|
+
import { PREFIX, positiveInteger } from "./util"
|
|
12
|
+
|
|
13
|
+
export interface ChatRequestOptions {
|
|
14
|
+
readonly maxOutputTokens?: number
|
|
15
|
+
readonly maxInputFileBytes?: number
|
|
16
|
+
readonly request?: JsonObject
|
|
17
|
+
/** Default: OpenAI-compatible. Select DeepSeek compatibility explicitly when required. */
|
|
18
|
+
readonly profile?: "openai" | "deepseek"
|
|
19
|
+
readonly systemRole?: "system" | "developer"
|
|
20
|
+
/** Defaults to max_completion_tokens. Use max_tokens only for deployments requiring the older field. */
|
|
21
|
+
readonly maxTokensParameter?: "max_completion_tokens" | "max_tokens"
|
|
22
|
+
/** Request a final usage chunk (default true). Disable for offerings that reject stream_options. */
|
|
23
|
+
readonly includeUsage?: boolean
|
|
24
|
+
/** Default: omit. Opt in only when the deployed model requires reasoning for tool continuation. */
|
|
25
|
+
readonly reasoningReplay?: "omit" | "tool-continuation"
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const RESERVED = new Set([
|
|
29
|
+
"model",
|
|
30
|
+
"messages",
|
|
31
|
+
"stream",
|
|
32
|
+
"stream_options",
|
|
33
|
+
"n",
|
|
34
|
+
"tools",
|
|
35
|
+
"tool_choice",
|
|
36
|
+
"parallel_tool_calls",
|
|
37
|
+
"functions",
|
|
38
|
+
"function_call",
|
|
39
|
+
"response_format",
|
|
40
|
+
"max_tokens",
|
|
41
|
+
"max_completion_tokens",
|
|
42
|
+
"reasoning_effort",
|
|
43
|
+
"thinking",
|
|
44
|
+
"reasoning",
|
|
45
|
+
"modalities",
|
|
46
|
+
"audio",
|
|
47
|
+
"store",
|
|
48
|
+
])
|
|
49
|
+
|
|
50
|
+
export function validateChatOptions(options: ChatRequestOptions): void {
|
|
51
|
+
positiveInteger(options.maxOutputTokens, "maxOutputTokens")
|
|
52
|
+
positiveInteger(options.maxInputFileBytes, "maxInputFileBytes")
|
|
53
|
+
for (const key of Object.keys(options.request ?? {}))
|
|
54
|
+
if (RESERVED.has(key))
|
|
55
|
+
throw new TypeError(`${PREFIX} Chat request option '${key}' is owned by the adapter.`)
|
|
56
|
+
for (const [key, allowed] of [
|
|
57
|
+
["profile", ["openai", "deepseek"]],
|
|
58
|
+
["systemRole", ["system", "developer"]],
|
|
59
|
+
["maxTokensParameter", ["max_completion_tokens", "max_tokens"]],
|
|
60
|
+
["reasoningReplay", ["omit", "tool-continuation"]],
|
|
61
|
+
] as const) {
|
|
62
|
+
if (options[key] !== undefined && !allowed.some((value) => value === options[key]))
|
|
63
|
+
throw new TypeError(`${PREFIX} Invalid Chat ${key}.`)
|
|
64
|
+
}
|
|
65
|
+
if (options.includeUsage !== undefined && typeof options.includeUsage !== "boolean")
|
|
66
|
+
throw new TypeError(`${PREFIX} includeUsage must be boolean.`)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function foundryChatRequest(
|
|
70
|
+
request: LanguageModelRequest,
|
|
71
|
+
definition: LanguageModelDefinition,
|
|
72
|
+
options: ChatRequestOptions,
|
|
73
|
+
scope: string
|
|
74
|
+
): JsonObject {
|
|
75
|
+
const max = prepareRequest(request, definition, options, scope)
|
|
76
|
+
const profile = options.profile ?? "openai"
|
|
77
|
+
if (profile !== "deepseek" && options.reasoningReplay === "tool-continuation")
|
|
78
|
+
throw new UnsupportedModelFeatureError(
|
|
79
|
+
`${PREFIX} Reasoning-content replay requires the DeepSeek Chat profile.`
|
|
80
|
+
)
|
|
81
|
+
const caps = definition.capabilities
|
|
82
|
+
if (request.tools.length && caps.localTools !== true)
|
|
83
|
+
throw new UnsupportedModelFeatureError(
|
|
84
|
+
`${PREFIX} Configure localTools capability before using Chat tools.`
|
|
85
|
+
)
|
|
86
|
+
const reasoning = request.reasoning
|
|
87
|
+
const explicitReasoning = reasoning !== undefined && reasoning !== "provider-default"
|
|
88
|
+
if (explicitReasoning) {
|
|
89
|
+
const issue = modelReasoningSupportIssue(caps.reasoning, reasoning)
|
|
90
|
+
if (issue || caps.reasoning === undefined || typeof reasoning !== "string")
|
|
91
|
+
throw new UnsupportedModelFeatureError(
|
|
92
|
+
`${PREFIX} ${issue ?? "Chat reasoning requires declared named efforts; exact budgets are unsupported"}.`
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
const schema = request.responseFormat && foundryOutputSchema(request.responseFormat.schema)
|
|
96
|
+
if (
|
|
97
|
+
request.responseFormat &&
|
|
98
|
+
(profile === "deepseek" || caps.nativeStructuredOutput !== true || !schema)
|
|
99
|
+
)
|
|
100
|
+
throw new UnsupportedModelFeatureError(
|
|
101
|
+
`${PREFIX} Chat structured output requires verified OpenAI-compatible strict-schema support. DeepSeek JSON mode is not strict schema decoding.`
|
|
102
|
+
)
|
|
103
|
+
const tools = request.tools.map((tool) => ({
|
|
104
|
+
type: "function",
|
|
105
|
+
function: {
|
|
106
|
+
name: tool.name,
|
|
107
|
+
description: tool.description,
|
|
108
|
+
parameters: tool.inputSchema,
|
|
109
|
+
...(profile === "openai"
|
|
110
|
+
? {
|
|
111
|
+
strict:
|
|
112
|
+
caps.nativeStructuredOutput === true &&
|
|
113
|
+
foundryOutputSchema(tool.inputSchema) !== undefined,
|
|
114
|
+
}
|
|
115
|
+
: {}),
|
|
116
|
+
},
|
|
117
|
+
}))
|
|
118
|
+
return {
|
|
119
|
+
...options.request,
|
|
120
|
+
model: definition.modelId,
|
|
121
|
+
messages: chatInput(request.messages, {
|
|
122
|
+
providerId: definition.providerId,
|
|
123
|
+
errorPrefix: PREFIX,
|
|
124
|
+
systemRole: options.systemRole,
|
|
125
|
+
reasoningReplay: options.reasoningReplay,
|
|
126
|
+
}),
|
|
127
|
+
stream: true,
|
|
128
|
+
...(options.includeUsage === false ? {} : { stream_options: { include_usage: true } }),
|
|
129
|
+
...(max === undefined ? {} : { [options.maxTokensParameter ?? "max_completion_tokens"]: max }),
|
|
130
|
+
...(explicitReasoning && typeof reasoning === "string" ? { reasoning_effort: reasoning } : {}),
|
|
131
|
+
...(tools.length
|
|
132
|
+
? {
|
|
133
|
+
tools,
|
|
134
|
+
tool_choice: "auto",
|
|
135
|
+
...(profile === "openai"
|
|
136
|
+
? {
|
|
137
|
+
parallel_tool_calls:
|
|
138
|
+
!schema &&
|
|
139
|
+
!tools.some((tool) => tool.function.strict) &&
|
|
140
|
+
caps.parallelToolCalls === true,
|
|
141
|
+
}
|
|
142
|
+
: {}),
|
|
143
|
+
}
|
|
144
|
+
: {}),
|
|
145
|
+
...(schema && request.responseFormat
|
|
146
|
+
? {
|
|
147
|
+
response_format: {
|
|
148
|
+
type: "json_schema",
|
|
149
|
+
json_schema: {
|
|
150
|
+
name: request.responseFormat.name,
|
|
151
|
+
schema,
|
|
152
|
+
strict: true,
|
|
153
|
+
...(request.responseFormat.description
|
|
154
|
+
? { description: request.responseFormat.description }
|
|
155
|
+
: {}),
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
}
|
|
159
|
+
: {}),
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ModelProviderError } from "@sixb/core/models"
|
|
2
|
+
|
|
3
|
+
/** Credentials belong to one request, including its retries, never to the provider instance. */
|
|
4
|
+
export class RequestDiagnostics {
|
|
5
|
+
private readonly secrets = new Set<string>()
|
|
6
|
+
|
|
7
|
+
add(value: string): void {
|
|
8
|
+
if (value.trim()) this.secrets.add(value.trim())
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
text(value: string): string {
|
|
12
|
+
for (const secret of [...this.secrets].sort((a, b) => b.length - a.length))
|
|
13
|
+
value = value.replaceAll(secret, "[REDACTED]")
|
|
14
|
+
return value
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
providerError(error: ModelProviderError): ModelProviderError {
|
|
18
|
+
return new ModelProviderError(this.text(error.message), error.providerId, error.modelId, {
|
|
19
|
+
status: error.status,
|
|
20
|
+
retryable: error.retryable,
|
|
21
|
+
retryAfterMs: error.retryAfterMs,
|
|
22
|
+
code: error.code === undefined ? undefined : this.text(error.code),
|
|
23
|
+
requestId: error.requestId === undefined ? undefined : this.text(error.requestId),
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
failure(error: unknown): Error {
|
|
28
|
+
if (error instanceof ModelProviderError) return this.providerError(error)
|
|
29
|
+
// Rebuild rather than attaching an unsanitized cause, stack or custom properties.
|
|
30
|
+
const sanitized = new Error(this.text(error instanceof Error ? error.message : String(error)))
|
|
31
|
+
sanitized.name = error instanceof Error ? this.text(error.name) : "Error"
|
|
32
|
+
return sanitized
|
|
33
|
+
}
|
|
34
|
+
}
|