@ai-sdk/azure 4.0.89 → 4.0.90
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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +17 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +308 -27
- package/dist/index.js.map +1 -1
- package/docs/04-azure.mdx +93 -13
- package/package.json +1 -1
- package/src/azure-mai-transcription-model-options.ts +22 -0
- package/src/azure-mai-transcription-model.ts +371 -0
- package/src/azure-openai-provider.ts +105 -31
- package/src/azure-speech-model-options.ts +2 -0
- package/src/azure-speech-speech-model.ts +14 -7
- package/src/azure-transcription-model-options.ts +9 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @ai-sdk/azure
|
|
2
2
|
|
|
3
|
+
## 4.0.90
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 8952233: feat(azure): support MAI-Voice-2.1 and MAI-Voice-2.1-Flash speech generation, and MAI-Transcribe-2-Streaming streaming transcription through the MAI realtime API. Adds the `maiBaseURL` and `webSocket` settings and `providerOptions.azure.api: 'mai'`. Unknown MAI voices or styles now fail with a descriptive, non-retryable error.
|
|
8
|
+
|
|
3
9
|
## 4.0.89
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ResponsesProviderMetadata, ResponsesReasoningProviderMetadata, ResponsesSourceDocumentProviderMetadata, ResponsesTextProviderMetadata, codeInterpreter, fileSearch, imageGeneration, webSearch, webSearchPreview } from "@ai-sdk/openai/internal";
|
|
2
2
|
import { EmbeddingModelV4, ImageModelV4, LanguageModelV4, ProviderV4, SpeechModelV4, TranscriptionModelV4 } from "@ai-sdk/provider";
|
|
3
|
-
import { FetchFunction, InferSchema } from "@ai-sdk/provider-utils";
|
|
3
|
+
import { FetchFunction, InferSchema, WebSocketConstructor } from "@ai-sdk/provider-utils";
|
|
4
4
|
import { DeepSeekLanguageModelChatOptions } from "@ai-sdk/deepseek";
|
|
5
5
|
import { OpenAILanguageModelChatOptions, OpenAILanguageModelChatOptions as OpenAIChatLanguageModelOptions, OpenAILanguageModelResponsesOptions, OpenAILanguageModelResponsesOptions as OpenAIResponsesProviderOptions, OpenAIResponsesSystemMessageOptions } from "@ai-sdk/openai";
|
|
6
6
|
//#region src/azure-openai-tools.d.ts
|
|
@@ -61,7 +61,8 @@ interface AzureOpenAIProvider extends ProviderV4 {
|
|
|
61
61
|
imageModel(deploymentId: string): ImageModelV4;
|
|
62
62
|
/**
|
|
63
63
|
* Creates an Azure transcription model. MAI-Transcribe models use the Speech
|
|
64
|
-
* API
|
|
64
|
+
* API and MAI-Transcribe-2-Streaming the MAI realtime API by default; other
|
|
65
|
+
* IDs use OpenAI. Override with providerOptions.azure.api.
|
|
65
66
|
*/
|
|
66
67
|
transcription(deploymentId: string): TranscriptionModelV4;
|
|
67
68
|
/**
|
|
@@ -137,6 +138,18 @@ interface AzureOpenAIProviderSettings {
|
|
|
137
138
|
* Speech requests do not use `baseURL` or `apiVersion`.
|
|
138
139
|
*/
|
|
139
140
|
speechBaseURL?: string;
|
|
141
|
+
/**
|
|
142
|
+
* URL prefix for MAI realtime APIs (MAI-Transcribe-2-Streaming).
|
|
143
|
+
* Defaults to `https://{resourceName}.services.ai.azure.com/mai/v1`.
|
|
144
|
+
*/
|
|
145
|
+
maiBaseURL?: string;
|
|
146
|
+
/**
|
|
147
|
+
* Custom WebSocket implementation for MAI streaming transcription, e.g.
|
|
148
|
+
* `ws` in Node.js. With a custom implementation the API key is sent as a
|
|
149
|
+
* header; otherwise it is sent as the `api-key` query parameter. Entra ID
|
|
150
|
+
* tokens require a custom implementation; streaming rejects them otherwise.
|
|
151
|
+
*/
|
|
152
|
+
webSocket?: WebSocketConstructor;
|
|
140
153
|
}
|
|
141
154
|
/**
|
|
142
155
|
* Create an Azure OpenAI provider instance.
|
|
@@ -174,6 +187,7 @@ type AzureSpeechModelOptions = InferSchema<typeof azureSpeechModelOptions>;
|
|
|
174
187
|
//#endregion
|
|
175
188
|
//#region src/azure-transcription-model-options.d.ts
|
|
176
189
|
declare const azureTranscriptionModelOptions: import("@ai-sdk/provider-utils").LazySchema<{
|
|
190
|
+
language?: string | undefined;
|
|
177
191
|
timestamps?: "word" | "segment" | "none" | undefined;
|
|
178
192
|
transcribeStyle?: "verbatim" | "clean" | undefined;
|
|
179
193
|
locales?: string[] | undefined;
|
|
@@ -183,7 +197,7 @@ declare const azureTranscriptionModelOptions: import("@ai-sdk/provider-utils").L
|
|
|
183
197
|
phraseList?: {
|
|
184
198
|
phrases: string[];
|
|
185
199
|
} | undefined;
|
|
186
|
-
api?: "openai" | "speech" | undefined;
|
|
200
|
+
api?: "openai" | "speech" | "mai" | undefined;
|
|
187
201
|
}>;
|
|
188
202
|
type AzureTranscriptionModelOptions = InferSchema<typeof azureTranscriptionModelOptions>;
|
|
189
203
|
//#endregion
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/azure-openai-tools.ts","../src/azure-openai-provider.ts","../src/azure-openai-provider-metadata.ts","../src/version.ts","../src/azure-speech-model-options.ts","../src/azure-transcription-model-options.ts","../src/azure-transcription-provider-metadata.ts","../src/index.ts"],"mappings":";;;;;;cAQa;EACX,wBAAwB;EACxB,mBAAmB;EACnB,wBAAwB;EACxB,kBAAkB;EAClB,yBAAyB;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/azure-openai-tools.ts","../src/azure-openai-provider.ts","../src/azure-openai-provider-metadata.ts","../src/version.ts","../src/azure-speech-model-options.ts","../src/azure-transcription-model-options.ts","../src/azure-transcription-provider-metadata.ts","../src/index.ts"],"mappings":";;;;;;cAQa;EACX,wBAAwB;EACxB,mBAAmB;EACnB,wBAAwB;EACxB,kBAAkB;EAClB,yBAAyB;;;;UCqCV,4BAA4B;GAC1C,uBAAuB;;;;EAKxB,cAAc,uBAAuB;;;;EAKrC,KAAK,uBAAuB;;;;EAK5B,SAAS,uBAAuB;;;;EAKhC,UAAU,uBAAuB;;;;EAKjC,WAAW,uBAAuB;;;;EAKlC,UAAU,uBAAuB;;;;EAKjC,eAAe,uBAAuB;;;;EAKtC,cAAc,uBAAuB;;;;EAKrC,mBAAmB,uBAAuB;;;;EAK1C,MAAM,uBAAuB;;;;EAK7B,WAAW,uBAAuB;;;;;;EAOlC,cAAc,uBAAuB;;;;EAKrC,mBAAmB,uBAAuB;;;;;EAM1C,OAAO,uBAAuB;;;;EAK9B,YAAY,uBAAuB;;;;EAKnC,cAAc;;UAGC;;;;;;;EAOf;;;;;;;;;EAUA;;;;EAKA;;;;;;EAOA,uBAAuB;;;;EAKvB,UAAU;;;;;EAMV,QAAQ;;;;;EAMR;;;;;;EAOA;;;;;;;;EASA;;;;;EAMA;;;;;;;EAQA,YAAY;;;;;wBAgCE,YACd,UAAS,8BACR;;;;qBAgQU,OAAK;;;KChfN;EACV,OAAO;;KAGG;EACV,OAAO;;KAGG;EACV,OAAO;;KAGG;EACV,OAAO;;;;qBClBI;;;cCMA,0DAAuB;;;;;KAYxB,0BAA0B,mBAC7B;;;cCZI,iEAA8B;;;;;;;;;;;;;KAc/B,iCAAiC,mBACpC;;;KCxBG;EACV;IACE,SAAS;MACP;MACA;MACA;MACA;MACA;MACA;MACA,QAAQ;QACN;QACA;QACA;;;;;;;YCVI,oCAAoC,KAC9C;;YAIU,2BAA2B"}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,238 @@
|
|
|
1
1
|
import { OpenAIChatLanguageModel, OpenAICompletionLanguageModel, OpenAIEmbeddingModel, OpenAIImageModel, OpenAIResponsesLanguageModel, OpenAISpeechModel, OpenAITranscriptionModel, codeInterpreter, fileSearch, imageGeneration, webSearch, webSearchPreview } from "@ai-sdk/openai/internal";
|
|
2
2
|
import { DeepSeekChatLanguageModel } from "@ai-sdk/deepseek/internal";
|
|
3
3
|
import { APICallError, InvalidArgumentError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
|
|
4
|
-
import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertBase64ToUint8Array, createBinaryResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, extractResponseHeaders, isValidHostnamePart, lazySchema, loadApiKey, loadSetting, mediaTypeToExtension, normalizeHeaders, parseProviderOptions, postFormDataToApi, postToApi, safeParseJSON, serializeModelOptions, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
|
|
4
|
+
import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, connectToWebSocket, convertBase64ToUint8Array, convertToBase64, createBinaryResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, extractResponseHeaders, isValidHostnamePart, lazySchema, loadApiKey, loadSetting, mediaTypeToExtension, normalizeHeaders, parseProviderOptions, postFormDataToApi, postToApi, safeParseJSON, serializeModelOptions, toWebSocketUrl, waitForWebSocketBufferDrain, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
|
|
5
5
|
import { z } from "zod/v4";
|
|
6
|
+
//#region src/azure-mai-transcription-model.ts
|
|
7
|
+
const SUPPORTED_SAMPLE_RATES = [16e3, 24e3];
|
|
8
|
+
const DEFAULT_SAMPLE_RATE = 24e3;
|
|
9
|
+
/**
|
|
10
|
+
* MAI streaming transcription over the OpenAI Realtime-compatible
|
|
11
|
+
* `/mai/v1/realtime` WebSocket. The model ID is the Foundry deployment name.
|
|
12
|
+
*/
|
|
13
|
+
var AzureMaiTranscriptionModel = class {
|
|
14
|
+
constructor(modelId, config) {
|
|
15
|
+
this.modelId = modelId;
|
|
16
|
+
this.config = config;
|
|
17
|
+
this.specificationVersion = "v4";
|
|
18
|
+
this.provider = "azure.transcription";
|
|
19
|
+
}
|
|
20
|
+
async doGenerate() {
|
|
21
|
+
throw new UnsupportedFunctionalityError({ functionality: `file transcription with ${this.modelId} (use streaming transcription)` });
|
|
22
|
+
}
|
|
23
|
+
async doStream(options, maiOptions = {}, warnings = []) {
|
|
24
|
+
const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
|
|
25
|
+
const { type, rate = DEFAULT_SAMPLE_RATE } = options.inputAudioFormat;
|
|
26
|
+
if (type !== "audio/pcm" || !SUPPORTED_SAMPLE_RATES.includes(rate)) throw new InvalidArgumentError({
|
|
27
|
+
argument: "inputAudioFormat",
|
|
28
|
+
message: `MAI streaming transcription requires 16-bit mono PCM (audio/pcm) at 16000 or 24000 Hz, got ${type}${options.inputAudioFormat.rate != null ? ` at ${rate} Hz` : ""}.`
|
|
29
|
+
});
|
|
30
|
+
const sessionUpdate = {
|
|
31
|
+
type: "session.update",
|
|
32
|
+
session: {
|
|
33
|
+
type: "transcription",
|
|
34
|
+
audio: { input: {
|
|
35
|
+
format: {
|
|
36
|
+
type: "audio/pcm",
|
|
37
|
+
rate
|
|
38
|
+
},
|
|
39
|
+
transcription: {
|
|
40
|
+
model: this.modelId,
|
|
41
|
+
...maiOptions.language != null ? { language: maiOptions.language } : {}
|
|
42
|
+
},
|
|
43
|
+
turn_detection: null,
|
|
44
|
+
noise_reduction: null
|
|
45
|
+
} }
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const headers = {
|
|
49
|
+
...await this.config.headers(),
|
|
50
|
+
...options.headers
|
|
51
|
+
};
|
|
52
|
+
if (this.config.webSocket == null && Object.entries(headers).some(([key, value]) => key.toLowerCase() === "authorization" && value != null)) throw new InvalidArgumentError({
|
|
53
|
+
argument: "webSocket",
|
|
54
|
+
message: "MAI streaming transcription with Microsoft Entra ID (tokenProvider) or an Authorization header requires a custom webSocket implementation such as `ws`, because the native WebSocket cannot send headers."
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
request: { body: sessionUpdate },
|
|
58
|
+
response: {
|
|
59
|
+
timestamp: currentDate,
|
|
60
|
+
modelId: this.modelId
|
|
61
|
+
},
|
|
62
|
+
stream: createMaiTranscriptionStream({
|
|
63
|
+
...getConnection({
|
|
64
|
+
url: toWebSocketUrl(this.config.url()),
|
|
65
|
+
headers,
|
|
66
|
+
supportsHeaders: this.config.webSocket != null
|
|
67
|
+
}),
|
|
68
|
+
webSocket: this.config.webSocket,
|
|
69
|
+
sessionUpdate,
|
|
70
|
+
bytesPerSecond: rate * 2,
|
|
71
|
+
language: maiOptions.language,
|
|
72
|
+
warnings,
|
|
73
|
+
audio: options.audio,
|
|
74
|
+
abortSignal: options.abortSignal,
|
|
75
|
+
includeRawChunks: options.includeRawChunks
|
|
76
|
+
})
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
function getConnection({ url, headers, supportsHeaders }) {
|
|
81
|
+
if (supportsHeaders) return {
|
|
82
|
+
url,
|
|
83
|
+
headers
|
|
84
|
+
};
|
|
85
|
+
const remaining = {};
|
|
86
|
+
for (const [key, value] of Object.entries(headers)) if (key.toLowerCase() === "api-key") {
|
|
87
|
+
if (value != null) url.searchParams.set("api-key", value);
|
|
88
|
+
} else remaining[key] = value;
|
|
89
|
+
return {
|
|
90
|
+
url,
|
|
91
|
+
headers: remaining
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function createMaiTranscriptionStream({ url, headers, webSocket, sessionUpdate, bytesPerSecond, language, warnings, audio, abortSignal, includeRawChunks }) {
|
|
95
|
+
let finished = false;
|
|
96
|
+
let cleanup = () => {};
|
|
97
|
+
return new ReadableStream({
|
|
98
|
+
start: (controller) => {
|
|
99
|
+
let audioReader;
|
|
100
|
+
let connection;
|
|
101
|
+
let audioStarted = false;
|
|
102
|
+
let committed = false;
|
|
103
|
+
let audioBytes = 0;
|
|
104
|
+
let deltas = "";
|
|
105
|
+
cleanup = (closeCode) => {
|
|
106
|
+
if (audioReader != null) audioReader.cancel().catch(() => {});
|
|
107
|
+
else audio.cancel().catch(() => {});
|
|
108
|
+
connection?.close(closeCode);
|
|
109
|
+
};
|
|
110
|
+
const finishWithError = (error) => {
|
|
111
|
+
if (finished) return;
|
|
112
|
+
finished = true;
|
|
113
|
+
cleanup();
|
|
114
|
+
controller.error(error);
|
|
115
|
+
};
|
|
116
|
+
const finish = (text) => {
|
|
117
|
+
if (finished) return;
|
|
118
|
+
finished = true;
|
|
119
|
+
controller.enqueue({
|
|
120
|
+
type: "finish",
|
|
121
|
+
text,
|
|
122
|
+
segments: [],
|
|
123
|
+
language,
|
|
124
|
+
...audioBytes > 0 ? { durationInSeconds: audioBytes / bytesPerSecond } : {}
|
|
125
|
+
});
|
|
126
|
+
controller.close();
|
|
127
|
+
cleanup(1e3);
|
|
128
|
+
};
|
|
129
|
+
const sendAudio = async (socket) => {
|
|
130
|
+
audioReader = audio.getReader();
|
|
131
|
+
try {
|
|
132
|
+
while (true) {
|
|
133
|
+
const { done, value } = await audioReader.read();
|
|
134
|
+
if (done || finished) break;
|
|
135
|
+
const base64 = convertToBase64(value);
|
|
136
|
+
if (base64.length === 0) continue;
|
|
137
|
+
audioBytes += typeof value === "string" ? base64ByteLength(base64) : value.byteLength;
|
|
138
|
+
socket.send(JSON.stringify({
|
|
139
|
+
type: "input_audio_buffer.append",
|
|
140
|
+
audio: base64
|
|
141
|
+
}));
|
|
142
|
+
await waitForWebSocketBufferDrain(socket);
|
|
143
|
+
}
|
|
144
|
+
} finally {
|
|
145
|
+
audioReader.releaseLock();
|
|
146
|
+
audioReader = void 0;
|
|
147
|
+
}
|
|
148
|
+
if (finished) return;
|
|
149
|
+
if (audioBytes === 0) {
|
|
150
|
+
finish("");
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
committed = true;
|
|
154
|
+
socket.send(JSON.stringify({ type: "input_audio_buffer.commit" }));
|
|
155
|
+
};
|
|
156
|
+
connection = connectToWebSocket({
|
|
157
|
+
url,
|
|
158
|
+
headers,
|
|
159
|
+
webSocket,
|
|
160
|
+
abortSignal,
|
|
161
|
+
onAbort: finishWithError,
|
|
162
|
+
onProcessingError: finishWithError,
|
|
163
|
+
onOpen: () => {
|
|
164
|
+
controller.enqueue({
|
|
165
|
+
type: "stream-start",
|
|
166
|
+
warnings
|
|
167
|
+
});
|
|
168
|
+
},
|
|
169
|
+
onMessageText: async (text) => {
|
|
170
|
+
const parsed = await safeParseJSON({ text });
|
|
171
|
+
if (!parsed.success) return;
|
|
172
|
+
const event = parsed.value;
|
|
173
|
+
const socket = connection?.socket;
|
|
174
|
+
if (includeRawChunks) controller.enqueue({
|
|
175
|
+
type: "raw",
|
|
176
|
+
rawValue: event
|
|
177
|
+
});
|
|
178
|
+
switch (event.type) {
|
|
179
|
+
case "session.created":
|
|
180
|
+
socket?.send(JSON.stringify(sessionUpdate));
|
|
181
|
+
break;
|
|
182
|
+
case "session.updated":
|
|
183
|
+
if (audioStarted || socket == null) break;
|
|
184
|
+
audioStarted = true;
|
|
185
|
+
sendAudio(socket).catch(finishWithError);
|
|
186
|
+
break;
|
|
187
|
+
case "conversation.item.input_audio_transcription.delta":
|
|
188
|
+
deltas += event.delta ?? "";
|
|
189
|
+
controller.enqueue({
|
|
190
|
+
type: "transcript-delta",
|
|
191
|
+
id: event.item_id,
|
|
192
|
+
delta: event.delta ?? ""
|
|
193
|
+
});
|
|
194
|
+
break;
|
|
195
|
+
case "conversation.item.input_audio_transcription.intermediate":
|
|
196
|
+
controller.enqueue({
|
|
197
|
+
type: "transcript-partial",
|
|
198
|
+
id: event.item_id,
|
|
199
|
+
text: event.intermediate ?? ""
|
|
200
|
+
});
|
|
201
|
+
break;
|
|
202
|
+
case "conversation.item.input_audio_transcription.completed": {
|
|
203
|
+
const transcript = event.transcript ?? deltas;
|
|
204
|
+
controller.enqueue({
|
|
205
|
+
type: "transcript-final",
|
|
206
|
+
id: event.item_id,
|
|
207
|
+
text: transcript
|
|
208
|
+
});
|
|
209
|
+
if (committed) finish(transcript);
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
case "conversation.item.input_audio_transcription.failed":
|
|
213
|
+
case "error": finishWithError(/* @__PURE__ */ new Error(`Azure MAI transcription error${event.error?.code != null ? ` (${event.error.code})` : ""}: ${event.error?.message ?? "unknown error"}`));
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
onSocketError: () => {
|
|
217
|
+
finishWithError(/* @__PURE__ */ new Error("Azure MAI transcription WebSocket error"));
|
|
218
|
+
},
|
|
219
|
+
onClose: ({ code, reason }) => {
|
|
220
|
+
finishWithError(/* @__PURE__ */ new Error(`Azure MAI transcription connection closed before the transcript completed${code != null ? ` (code ${code}${reason ? `: ${reason}` : ""})` : ""}`));
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
},
|
|
224
|
+
cancel: () => {
|
|
225
|
+
if (finished) return;
|
|
226
|
+
finished = true;
|
|
227
|
+
cleanup();
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
function base64ByteLength(base64) {
|
|
232
|
+
const padding = base64.endsWith("==") ? 2 : base64.endsWith("=") ? 1 : 0;
|
|
233
|
+
return base64.length * 3 / 4 - padding;
|
|
234
|
+
}
|
|
235
|
+
//#endregion
|
|
6
236
|
//#region src/azure-openai-tools.ts
|
|
7
237
|
const azureOpenaiTools = {
|
|
8
238
|
codeInterpreter,
|
|
@@ -34,7 +264,12 @@ const azureSpeechModelOptions = lazySchema(() => zodSchema(z.strictObject({
|
|
|
34
264
|
api: z.enum(["openai", "speech"]).optional(),
|
|
35
265
|
...azureSpeechSpeechModelOptionsShape()
|
|
36
266
|
})));
|
|
37
|
-
const maiVoiceModels = /* @__PURE__ */ new Map([
|
|
267
|
+
const maiVoiceModels = /* @__PURE__ */ new Map([
|
|
268
|
+
["mai-voice-2.1-flash", "MAI-Voice-2.1-Flash"],
|
|
269
|
+
["mai-voice-2.1", "MAI-Voice-2.1"],
|
|
270
|
+
["mai-voice-2-flash", "MAI-Voice-2-Flash"],
|
|
271
|
+
["mai-voice-2", "MAI-Voice-2"]
|
|
272
|
+
]);
|
|
38
273
|
function getMAIVoiceModel(modelId) {
|
|
39
274
|
return maiVoiceModels.get(modelId.toLowerCase());
|
|
40
275
|
}
|
|
@@ -192,7 +427,8 @@ const failedResponseHandler = async ({ response, url, requestBodyValues }) => {
|
|
|
192
427
|
text: responseBody,
|
|
193
428
|
schema: errorSchema
|
|
194
429
|
});
|
|
195
|
-
const
|
|
430
|
+
const isVoiceReset = response.status === 502 && responseBody.includes("reset reason: protocol error");
|
|
431
|
+
const message = parsed.success ? parsed.value.error.message : isVoiceReset ? "Azure Speech could not synthesize the request. Check that the voice is available for this model and that the style is supported by the voice." : response.status === 400 ? "Azure Speech request failed with status 400. Check the voice name, style, and output format." : `Azure Speech request failed with status ${response.status}.`;
|
|
196
432
|
return {
|
|
197
433
|
responseHeaders,
|
|
198
434
|
value: new APICallError({
|
|
@@ -201,11 +437,21 @@ const failedResponseHandler = async ({ response, url, requestBodyValues }) => {
|
|
|
201
437
|
requestBodyValues,
|
|
202
438
|
statusCode: response.status,
|
|
203
439
|
responseHeaders,
|
|
204
|
-
responseBody
|
|
440
|
+
responseBody,
|
|
441
|
+
isRetryable: isVoiceReset ? false : void 0
|
|
205
442
|
})
|
|
206
443
|
};
|
|
207
444
|
};
|
|
208
445
|
//#endregion
|
|
446
|
+
//#region src/azure-mai-transcription-model-options.ts
|
|
447
|
+
const azureMaiTranscriptionModelOptionsShape = () => ({
|
|
448
|
+
/**
|
|
449
|
+
* Language hint for streaming transcription, e.g. `en`. Omit for automatic
|
|
450
|
+
* language detection.
|
|
451
|
+
*/
|
|
452
|
+
language: z.string().min(1).optional() });
|
|
453
|
+
lazySchema(() => zodSchema(z.strictObject(azureMaiTranscriptionModelOptionsShape())));
|
|
454
|
+
//#endregion
|
|
209
455
|
//#region src/azure-speech-transcription-model-options.ts
|
|
210
456
|
const azureSpeechTranscriptionModelOptionsShape = () => ({
|
|
211
457
|
/**
|
|
@@ -241,10 +487,16 @@ lazySchema(() => zodSchema(z.strictObject(azureSpeechTranscriptionModelOptionsSh
|
|
|
241
487
|
//#region src/azure-transcription-model-options.ts
|
|
242
488
|
const azureTranscriptionModelOptions = lazySchema(() => zodSchema(z.strictObject({
|
|
243
489
|
/**
|
|
244
|
-
* API to use. Defaults to Speech for MAI-Transcribe models,
|
|
490
|
+
* API to use. Defaults to Speech for MAI-Transcribe models, MAI for
|
|
491
|
+
* MAI-Transcribe-2-Streaming, and OpenAI otherwise.
|
|
245
492
|
*/
|
|
246
|
-
api: z.enum([
|
|
247
|
-
|
|
493
|
+
api: z.enum([
|
|
494
|
+
"openai",
|
|
495
|
+
"speech",
|
|
496
|
+
"mai"
|
|
497
|
+
]).optional(),
|
|
498
|
+
...azureSpeechTranscriptionModelOptionsShape(),
|
|
499
|
+
...azureMaiTranscriptionModelOptionsShape()
|
|
248
500
|
})));
|
|
249
501
|
const maiTranscribeModels = /* @__PURE__ */ new Map([["mai-transcribe-2", {
|
|
250
502
|
name: "MAI-Transcribe-2",
|
|
@@ -256,6 +508,9 @@ const maiTranscribeModels = /* @__PURE__ */ new Map([["mai-transcribe-2", {
|
|
|
256
508
|
function getMAITranscribeModel(modelId) {
|
|
257
509
|
return maiTranscribeModels.get(modelId.toLowerCase());
|
|
258
510
|
}
|
|
511
|
+
function isMAITranscribeStreaming(modelId) {
|
|
512
|
+
return modelId.toLowerCase() === "mai-transcribe-2-streaming";
|
|
513
|
+
}
|
|
259
514
|
//#endregion
|
|
260
515
|
//#region src/azure-speech-transcription-model.ts
|
|
261
516
|
var AzureSpeechTranscriptionModel = class {
|
|
@@ -338,7 +593,7 @@ const responseSchema = z.object({
|
|
|
338
593
|
});
|
|
339
594
|
//#endregion
|
|
340
595
|
//#region src/version.ts
|
|
341
|
-
const VERSION = "4.0.
|
|
596
|
+
const VERSION = "4.0.90";
|
|
342
597
|
//#endregion
|
|
343
598
|
//#region src/azure-openai-provider.ts
|
|
344
599
|
function getAzureOpenAIBaseURLInfo(baseURL) {
|
|
@@ -370,7 +625,7 @@ function createAzure(options = {}) {
|
|
|
370
625
|
const authHeaders = tokenProvider ? {} : { [api === "speech" ? "Ocp-Apim-Subscription-Key" : "api-key"]: loadApiKey({
|
|
371
626
|
apiKey: options.apiKey,
|
|
372
627
|
environmentVariableName: "AZURE_API_KEY",
|
|
373
|
-
description: api === "speech" ? "Azure Speech" : "Azure OpenAI"
|
|
628
|
+
description: api === "speech" ? "Azure Speech" : api === "mai" ? "Azure MAI" : "Azure OpenAI"
|
|
374
629
|
}) };
|
|
375
630
|
return withUserAgentSuffix({
|
|
376
631
|
...authHeaders,
|
|
@@ -464,6 +719,13 @@ function createAzure(options = {}) {
|
|
|
464
719
|
url: () => `${speechBaseURL()}/speechtotext/transcriptions:transcribe?api-version=2025-10-15`,
|
|
465
720
|
headers: () => getHeaders("speech"),
|
|
466
721
|
fetch
|
|
722
|
+
}), new AzureMaiTranscriptionModel(modelId, {
|
|
723
|
+
url: () => `${withoutTrailingSlash(options.maiBaseURL) ?? `https://${getResourceName()}.services.ai.azure.com/mai/v1`}/realtime?intent=transcription`,
|
|
724
|
+
headers: async () => tokenProvider ? {
|
|
725
|
+
authorization: `Bearer ${await tokenProvider()}`,
|
|
726
|
+
...getHeaders("mai")
|
|
727
|
+
} : getHeaders("mai"),
|
|
728
|
+
webSocket: options.webSocket
|
|
467
729
|
}));
|
|
468
730
|
const createSpeechModel = (modelId) => new AzureSpeechModel(modelId, options, new OpenAISpeechModel(modelId, {
|
|
469
731
|
provider: "azure.speech",
|
|
@@ -503,11 +765,12 @@ function createAzure(options = {}) {
|
|
|
503
765
|
*/
|
|
504
766
|
const azure = createAzure();
|
|
505
767
|
var AzureTranscriptionModel = class {
|
|
506
|
-
constructor(modelId, config, openai, speech) {
|
|
768
|
+
constructor(modelId, config, openai, speech, mai) {
|
|
507
769
|
this.modelId = modelId;
|
|
508
770
|
this.config = config;
|
|
509
771
|
this.openai = openai;
|
|
510
772
|
this.speech = speech;
|
|
773
|
+
this.mai = mai;
|
|
511
774
|
this.specificationVersion = "v4";
|
|
512
775
|
this.provider = "azure.transcription";
|
|
513
776
|
}
|
|
@@ -521,32 +784,41 @@ var AzureTranscriptionModel = class {
|
|
|
521
784
|
return createAzure(options.config).transcription(options.modelId);
|
|
522
785
|
}
|
|
523
786
|
async doGenerate(options) {
|
|
524
|
-
const { api,
|
|
525
|
-
if (api === "
|
|
787
|
+
const { api, speechOptions, maiOptions } = await this.getOptions(options.providerOptions);
|
|
788
|
+
if (api === "mai") return this.mai.doGenerate();
|
|
789
|
+
if (api === "speech") {
|
|
790
|
+
const result = await this.speech.doGenerate(options, speechOptions);
|
|
791
|
+
return {
|
|
792
|
+
...result,
|
|
793
|
+
warnings: [...result.warnings, ...maiOnlyWarnings(maiOptions)]
|
|
794
|
+
};
|
|
795
|
+
}
|
|
526
796
|
const result = await this.openai.doGenerate(options);
|
|
527
797
|
return {
|
|
528
798
|
...result,
|
|
529
|
-
warnings: [
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
799
|
+
warnings: [
|
|
800
|
+
...result.warnings,
|
|
801
|
+
...speechOnlyWarnings(speechOptions),
|
|
802
|
+
...maiOnlyWarnings(maiOptions)
|
|
803
|
+
]
|
|
534
804
|
};
|
|
535
805
|
}
|
|
536
806
|
async doStream(options) {
|
|
537
|
-
const { api } = await this.getOptions(options.providerOptions);
|
|
807
|
+
const { api, speechOptions, maiOptions } = await this.getOptions(options.providerOptions);
|
|
538
808
|
if (api === "speech") throw new UnsupportedFunctionalityError({ functionality: "streaming transcription with the Azure Speech API" });
|
|
809
|
+
if (api === "mai") return this.mai.doStream(options, maiOptions, speechOnlyWarnings(speechOptions));
|
|
539
810
|
return this.openai.doStream(options);
|
|
540
811
|
}
|
|
541
812
|
async getOptions(providerOptions) {
|
|
542
|
-
const
|
|
813
|
+
const { api, language, ...speechOptions } = await parseProviderOptions({
|
|
543
814
|
provider: "azure",
|
|
544
815
|
providerOptions,
|
|
545
816
|
schema: azureTranscriptionModelOptions
|
|
546
|
-
});
|
|
817
|
+
}) ?? {};
|
|
547
818
|
return {
|
|
548
|
-
|
|
549
|
-
|
|
819
|
+
api: api ?? (isMAITranscribeStreaming(this.modelId) ? "mai" : getMAITranscribeModel(this.modelId) ? "speech" : "openai"),
|
|
820
|
+
speechOptions,
|
|
821
|
+
maiOptions: language != null ? { language } : {}
|
|
550
822
|
};
|
|
551
823
|
}
|
|
552
824
|
};
|
|
@@ -578,14 +850,23 @@ var AzureSpeechModel = class {
|
|
|
578
850
|
const result = await this.openai.doGenerate(options);
|
|
579
851
|
return {
|
|
580
852
|
...result,
|
|
581
|
-
warnings: [...result.warnings, ...
|
|
582
|
-
type: "unsupported",
|
|
583
|
-
feature: `providerOptions.azure.${key}`,
|
|
584
|
-
details: "This option requires the Azure Speech API."
|
|
585
|
-
}))]
|
|
853
|
+
warnings: [...result.warnings, ...speechOnlyWarnings(speechOptions)]
|
|
586
854
|
};
|
|
587
855
|
}
|
|
588
856
|
};
|
|
857
|
+
function speechOnlyWarnings(options) {
|
|
858
|
+
return unsupportedOptionWarnings(options, "This option requires the Azure Speech API.");
|
|
859
|
+
}
|
|
860
|
+
function maiOnlyWarnings(options) {
|
|
861
|
+
return unsupportedOptionWarnings(options, "This option requires MAI streaming transcription.");
|
|
862
|
+
}
|
|
863
|
+
function unsupportedOptionWarnings(options, details) {
|
|
864
|
+
return Object.entries(options).filter(([, value]) => value !== void 0).map(([key]) => ({
|
|
865
|
+
type: "unsupported",
|
|
866
|
+
feature: `providerOptions.azure.${key}`,
|
|
867
|
+
details
|
|
868
|
+
}));
|
|
869
|
+
}
|
|
589
870
|
//#endregion
|
|
590
871
|
export { VERSION, azure, createAzure };
|
|
591
872
|
|