@ai-sdk/openai 3.0.14 → 3.0.15
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.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +6 -5
- package/src/chat/__fixtures__/azure-model-router.1.chunks.txt +8 -0
- package/src/chat/__snapshots__/openai-chat-language-model.test.ts.snap +88 -0
- package/src/chat/convert-openai-chat-usage.ts +57 -0
- package/src/chat/convert-to-openai-chat-messages.test.ts +516 -0
- package/src/chat/convert-to-openai-chat-messages.ts +225 -0
- package/src/chat/get-response-metadata.ts +15 -0
- package/src/chat/map-openai-finish-reason.ts +19 -0
- package/src/chat/openai-chat-api.ts +198 -0
- package/src/chat/openai-chat-language-model.test.ts +3496 -0
- package/src/chat/openai-chat-language-model.ts +700 -0
- package/src/chat/openai-chat-options.ts +186 -0
- package/src/chat/openai-chat-prepare-tools.test.ts +322 -0
- package/src/chat/openai-chat-prepare-tools.ts +84 -0
- package/src/chat/openai-chat-prompt.ts +70 -0
- package/src/completion/convert-openai-completion-usage.ts +46 -0
- package/src/completion/convert-to-openai-completion-prompt.ts +93 -0
- package/src/completion/get-response-metadata.ts +15 -0
- package/src/completion/map-openai-finish-reason.ts +19 -0
- package/src/completion/openai-completion-api.ts +81 -0
- package/src/completion/openai-completion-language-model.test.ts +752 -0
- package/src/completion/openai-completion-language-model.ts +336 -0
- package/src/completion/openai-completion-options.ts +58 -0
- package/src/embedding/__snapshots__/openai-embedding-model.test.ts.snap +43 -0
- package/src/embedding/openai-embedding-api.ts +13 -0
- package/src/embedding/openai-embedding-model.test.ts +146 -0
- package/src/embedding/openai-embedding-model.ts +95 -0
- package/src/embedding/openai-embedding-options.ts +30 -0
- package/src/image/openai-image-api.ts +35 -0
- package/src/image/openai-image-model.test.ts +722 -0
- package/src/image/openai-image-model.ts +305 -0
- package/src/image/openai-image-options.ts +28 -0
- package/src/index.ts +9 -0
- package/src/internal/index.ts +19 -0
- package/src/openai-config.ts +18 -0
- package/src/openai-error.test.ts +34 -0
- package/src/openai-error.ts +22 -0
- package/src/openai-language-model-capabilities.test.ts +93 -0
- package/src/openai-language-model-capabilities.ts +54 -0
- package/src/openai-provider.test.ts +98 -0
- package/src/openai-provider.ts +270 -0
- package/src/openai-tools.ts +114 -0
- package/src/responses/__fixtures__/openai-apply-patch-tool-delete.1.chunks.txt +5 -0
- package/src/responses/__fixtures__/openai-apply-patch-tool.1.chunks.txt +38 -0
- package/src/responses/__fixtures__/openai-apply-patch-tool.1.json +69 -0
- package/src/responses/__fixtures__/openai-code-interpreter-tool.1.chunks.txt +393 -0
- package/src/responses/__fixtures__/openai-code-interpreter-tool.1.json +137 -0
- package/src/responses/__fixtures__/openai-error.1.chunks.txt +4 -0
- package/src/responses/__fixtures__/openai-error.1.json +8 -0
- package/src/responses/__fixtures__/openai-file-search-tool.1.chunks.txt +94 -0
- package/src/responses/__fixtures__/openai-file-search-tool.1.json +89 -0
- package/src/responses/__fixtures__/openai-file-search-tool.2.chunks.txt +93 -0
- package/src/responses/__fixtures__/openai-file-search-tool.2.json +112 -0
- package/src/responses/__fixtures__/openai-image-generation-tool.1.chunks.txt +16 -0
- package/src/responses/__fixtures__/openai-image-generation-tool.1.json +96 -0
- package/src/responses/__fixtures__/openai-local-shell-tool.1.chunks.txt +7 -0
- package/src/responses/__fixtures__/openai-local-shell-tool.1.json +70 -0
- package/src/responses/__fixtures__/openai-mcp-tool-approval.1.chunks.txt +11 -0
- package/src/responses/__fixtures__/openai-mcp-tool-approval.1.json +169 -0
- package/src/responses/__fixtures__/openai-mcp-tool-approval.2.chunks.txt +123 -0
- package/src/responses/__fixtures__/openai-mcp-tool-approval.2.json +176 -0
- package/src/responses/__fixtures__/openai-mcp-tool-approval.3.chunks.txt +11 -0
- package/src/responses/__fixtures__/openai-mcp-tool-approval.3.json +169 -0
- package/src/responses/__fixtures__/openai-mcp-tool-approval.4.chunks.txt +84 -0
- package/src/responses/__fixtures__/openai-mcp-tool-approval.4.json +182 -0
- package/src/responses/__fixtures__/openai-mcp-tool.1.chunks.txt +373 -0
- package/src/responses/__fixtures__/openai-mcp-tool.1.json +159 -0
- package/src/responses/__fixtures__/openai-reasoning-encrypted-content.1.chunks.txt +110 -0
- package/src/responses/__fixtures__/openai-reasoning-encrypted-content.1.json +117 -0
- package/src/responses/__fixtures__/openai-shell-tool.1.chunks.txt +182 -0
- package/src/responses/__fixtures__/openai-shell-tool.1.json +73 -0
- package/src/responses/__fixtures__/openai-web-search-tool.1.chunks.txt +185 -0
- package/src/responses/__fixtures__/openai-web-search-tool.1.json +266 -0
- package/src/responses/__snapshots__/openai-responses-language-model.test.ts.snap +10955 -0
- package/src/responses/convert-openai-responses-usage.ts +53 -0
- package/src/responses/convert-to-openai-responses-input.test.ts +2976 -0
- package/src/responses/convert-to-openai-responses-input.ts +578 -0
- package/src/responses/map-openai-responses-finish-reason.ts +22 -0
- package/src/responses/openai-responses-api.test.ts +89 -0
- package/src/responses/openai-responses-api.ts +1086 -0
- package/src/responses/openai-responses-language-model.test.ts +6927 -0
- package/src/responses/openai-responses-language-model.ts +1932 -0
- package/src/responses/openai-responses-options.ts +312 -0
- package/src/responses/openai-responses-prepare-tools.test.ts +924 -0
- package/src/responses/openai-responses-prepare-tools.ts +264 -0
- package/src/responses/openai-responses-provider-metadata.ts +39 -0
- package/src/speech/openai-speech-api.ts +38 -0
- package/src/speech/openai-speech-model.test.ts +202 -0
- package/src/speech/openai-speech-model.ts +137 -0
- package/src/speech/openai-speech-options.ts +22 -0
- package/src/tool/apply-patch.ts +141 -0
- package/src/tool/code-interpreter.ts +104 -0
- package/src/tool/file-search.ts +145 -0
- package/src/tool/image-generation.ts +126 -0
- package/src/tool/local-shell.test-d.ts +20 -0
- package/src/tool/local-shell.ts +72 -0
- package/src/tool/mcp.ts +125 -0
- package/src/tool/shell.ts +85 -0
- package/src/tool/web-search-preview.ts +139 -0
- package/src/tool/web-search.test-d.ts +13 -0
- package/src/tool/web-search.ts +179 -0
- package/src/transcription/openai-transcription-api.ts +37 -0
- package/src/transcription/openai-transcription-model.test.ts +507 -0
- package/src/transcription/openai-transcription-model.ts +232 -0
- package/src/transcription/openai-transcription-options.ts +50 -0
- package/src/transcription/transcription-test.mp3 +0 -0
- package/src/version.ts +6 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LanguageModelV3CallOptions,
|
|
3
|
+
SharedV3Warning,
|
|
4
|
+
UnsupportedFunctionalityError,
|
|
5
|
+
} from '@ai-sdk/provider';
|
|
6
|
+
import { validateTypes } from '@ai-sdk/provider-utils';
|
|
7
|
+
import { codeInterpreterArgsSchema } from '../tool/code-interpreter';
|
|
8
|
+
import { fileSearchArgsSchema } from '../tool/file-search';
|
|
9
|
+
import { imageGenerationArgsSchema } from '../tool/image-generation';
|
|
10
|
+
import { mcpArgsSchema } from '../tool/mcp';
|
|
11
|
+
import { webSearchArgsSchema } from '../tool/web-search';
|
|
12
|
+
import { webSearchPreviewArgsSchema } from '../tool/web-search-preview';
|
|
13
|
+
import { OpenAIResponsesTool } from './openai-responses-api';
|
|
14
|
+
|
|
15
|
+
export async function prepareResponsesTools({
|
|
16
|
+
tools,
|
|
17
|
+
toolChoice,
|
|
18
|
+
}: {
|
|
19
|
+
tools: LanguageModelV3CallOptions['tools'];
|
|
20
|
+
toolChoice: LanguageModelV3CallOptions['toolChoice'] | undefined;
|
|
21
|
+
}): Promise<{
|
|
22
|
+
tools?: Array<OpenAIResponsesTool>;
|
|
23
|
+
toolChoice?:
|
|
24
|
+
| 'auto'
|
|
25
|
+
| 'none'
|
|
26
|
+
| 'required'
|
|
27
|
+
| { type: 'file_search' }
|
|
28
|
+
| { type: 'web_search_preview' }
|
|
29
|
+
| { type: 'web_search' }
|
|
30
|
+
| { type: 'function'; name: string }
|
|
31
|
+
| { type: 'code_interpreter' }
|
|
32
|
+
| { type: 'mcp' }
|
|
33
|
+
| { type: 'image_generation' }
|
|
34
|
+
| { type: 'apply_patch' };
|
|
35
|
+
toolWarnings: SharedV3Warning[];
|
|
36
|
+
}> {
|
|
37
|
+
// when the tools array is empty, change it to undefined to prevent errors:
|
|
38
|
+
tools = tools?.length ? tools : undefined;
|
|
39
|
+
|
|
40
|
+
const toolWarnings: SharedV3Warning[] = [];
|
|
41
|
+
|
|
42
|
+
if (tools == null) {
|
|
43
|
+
return { tools: undefined, toolChoice: undefined, toolWarnings };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const openaiTools: Array<OpenAIResponsesTool> = [];
|
|
47
|
+
|
|
48
|
+
for (const tool of tools) {
|
|
49
|
+
switch (tool.type) {
|
|
50
|
+
case 'function':
|
|
51
|
+
openaiTools.push({
|
|
52
|
+
type: 'function',
|
|
53
|
+
name: tool.name,
|
|
54
|
+
description: tool.description,
|
|
55
|
+
parameters: tool.inputSchema,
|
|
56
|
+
...(tool.strict != null ? { strict: tool.strict } : {}),
|
|
57
|
+
});
|
|
58
|
+
break;
|
|
59
|
+
case 'provider': {
|
|
60
|
+
switch (tool.id) {
|
|
61
|
+
case 'openai.file_search': {
|
|
62
|
+
const args = await validateTypes({
|
|
63
|
+
value: tool.args,
|
|
64
|
+
schema: fileSearchArgsSchema,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
openaiTools.push({
|
|
68
|
+
type: 'file_search',
|
|
69
|
+
vector_store_ids: args.vectorStoreIds,
|
|
70
|
+
max_num_results: args.maxNumResults,
|
|
71
|
+
ranking_options: args.ranking
|
|
72
|
+
? {
|
|
73
|
+
ranker: args.ranking.ranker,
|
|
74
|
+
score_threshold: args.ranking.scoreThreshold,
|
|
75
|
+
}
|
|
76
|
+
: undefined,
|
|
77
|
+
filters: args.filters,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
case 'openai.local_shell': {
|
|
83
|
+
openaiTools.push({
|
|
84
|
+
type: 'local_shell',
|
|
85
|
+
});
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
case 'openai.shell': {
|
|
89
|
+
openaiTools.push({
|
|
90
|
+
type: 'shell',
|
|
91
|
+
});
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case 'openai.apply_patch': {
|
|
95
|
+
openaiTools.push({
|
|
96
|
+
type: 'apply_patch',
|
|
97
|
+
});
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
case 'openai.web_search_preview': {
|
|
101
|
+
const args = await validateTypes({
|
|
102
|
+
value: tool.args,
|
|
103
|
+
schema: webSearchPreviewArgsSchema,
|
|
104
|
+
});
|
|
105
|
+
openaiTools.push({
|
|
106
|
+
type: 'web_search_preview',
|
|
107
|
+
search_context_size: args.searchContextSize,
|
|
108
|
+
user_location: args.userLocation,
|
|
109
|
+
});
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
case 'openai.web_search': {
|
|
113
|
+
const args = await validateTypes({
|
|
114
|
+
value: tool.args,
|
|
115
|
+
schema: webSearchArgsSchema,
|
|
116
|
+
});
|
|
117
|
+
openaiTools.push({
|
|
118
|
+
type: 'web_search',
|
|
119
|
+
filters:
|
|
120
|
+
args.filters != null
|
|
121
|
+
? { allowed_domains: args.filters.allowedDomains }
|
|
122
|
+
: undefined,
|
|
123
|
+
external_web_access: args.externalWebAccess,
|
|
124
|
+
search_context_size: args.searchContextSize,
|
|
125
|
+
user_location: args.userLocation,
|
|
126
|
+
});
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
case 'openai.code_interpreter': {
|
|
130
|
+
const args = await validateTypes({
|
|
131
|
+
value: tool.args,
|
|
132
|
+
schema: codeInterpreterArgsSchema,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
openaiTools.push({
|
|
136
|
+
type: 'code_interpreter',
|
|
137
|
+
container:
|
|
138
|
+
args.container == null
|
|
139
|
+
? { type: 'auto', file_ids: undefined }
|
|
140
|
+
: typeof args.container === 'string'
|
|
141
|
+
? args.container
|
|
142
|
+
: { type: 'auto', file_ids: args.container.fileIds },
|
|
143
|
+
});
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
case 'openai.image_generation': {
|
|
147
|
+
const args = await validateTypes({
|
|
148
|
+
value: tool.args,
|
|
149
|
+
schema: imageGenerationArgsSchema,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
openaiTools.push({
|
|
153
|
+
type: 'image_generation',
|
|
154
|
+
background: args.background,
|
|
155
|
+
input_fidelity: args.inputFidelity,
|
|
156
|
+
input_image_mask: args.inputImageMask
|
|
157
|
+
? {
|
|
158
|
+
file_id: args.inputImageMask.fileId,
|
|
159
|
+
image_url: args.inputImageMask.imageUrl,
|
|
160
|
+
}
|
|
161
|
+
: undefined,
|
|
162
|
+
model: args.model,
|
|
163
|
+
moderation: args.moderation,
|
|
164
|
+
partial_images: args.partialImages,
|
|
165
|
+
quality: args.quality,
|
|
166
|
+
output_compression: args.outputCompression,
|
|
167
|
+
output_format: args.outputFormat,
|
|
168
|
+
size: args.size,
|
|
169
|
+
});
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
case 'openai.mcp': {
|
|
173
|
+
const args = await validateTypes({
|
|
174
|
+
value: tool.args,
|
|
175
|
+
schema: mcpArgsSchema,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const mapApprovalFilter = (filter: { toolNames?: string[] }) => ({
|
|
179
|
+
tool_names: filter.toolNames,
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
const requireApproval = args.requireApproval;
|
|
183
|
+
const requireApprovalParam:
|
|
184
|
+
| 'always'
|
|
185
|
+
| 'never'
|
|
186
|
+
| {
|
|
187
|
+
never?: { tool_names?: string[] };
|
|
188
|
+
}
|
|
189
|
+
| undefined =
|
|
190
|
+
requireApproval == null
|
|
191
|
+
? undefined
|
|
192
|
+
: typeof requireApproval === 'string'
|
|
193
|
+
? requireApproval
|
|
194
|
+
: requireApproval.never != null
|
|
195
|
+
? { never: mapApprovalFilter(requireApproval.never) }
|
|
196
|
+
: undefined;
|
|
197
|
+
|
|
198
|
+
openaiTools.push({
|
|
199
|
+
type: 'mcp',
|
|
200
|
+
server_label: args.serverLabel,
|
|
201
|
+
allowed_tools: Array.isArray(args.allowedTools)
|
|
202
|
+
? args.allowedTools
|
|
203
|
+
: args.allowedTools
|
|
204
|
+
? {
|
|
205
|
+
read_only: args.allowedTools.readOnly,
|
|
206
|
+
tool_names: args.allowedTools.toolNames,
|
|
207
|
+
}
|
|
208
|
+
: undefined,
|
|
209
|
+
authorization: args.authorization,
|
|
210
|
+
connector_id: args.connectorId,
|
|
211
|
+
headers: args.headers,
|
|
212
|
+
require_approval: requireApprovalParam ?? 'never',
|
|
213
|
+
server_description: args.serverDescription,
|
|
214
|
+
server_url: args.serverUrl,
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
default:
|
|
223
|
+
toolWarnings.push({
|
|
224
|
+
type: 'unsupported',
|
|
225
|
+
feature: `function tool ${tool}`,
|
|
226
|
+
});
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (toolChoice == null) {
|
|
232
|
+
return { tools: openaiTools, toolChoice: undefined, toolWarnings };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const type = toolChoice.type;
|
|
236
|
+
|
|
237
|
+
switch (type) {
|
|
238
|
+
case 'auto':
|
|
239
|
+
case 'none':
|
|
240
|
+
case 'required':
|
|
241
|
+
return { tools: openaiTools, toolChoice: type, toolWarnings };
|
|
242
|
+
case 'tool':
|
|
243
|
+
return {
|
|
244
|
+
tools: openaiTools,
|
|
245
|
+
toolChoice:
|
|
246
|
+
toolChoice.toolName === 'code_interpreter' ||
|
|
247
|
+
toolChoice.toolName === 'file_search' ||
|
|
248
|
+
toolChoice.toolName === 'image_generation' ||
|
|
249
|
+
toolChoice.toolName === 'web_search_preview' ||
|
|
250
|
+
toolChoice.toolName === 'web_search' ||
|
|
251
|
+
toolChoice.toolName === 'mcp' ||
|
|
252
|
+
toolChoice.toolName === 'apply_patch'
|
|
253
|
+
? { type: toolChoice.toolName }
|
|
254
|
+
: { type: 'function', name: toolChoice.toolName },
|
|
255
|
+
toolWarnings,
|
|
256
|
+
};
|
|
257
|
+
default: {
|
|
258
|
+
const _exhaustiveCheck: never = type;
|
|
259
|
+
throw new UnsupportedFunctionalityError({
|
|
260
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { openaiResponsesChunkSchema } from './openai-responses-api';
|
|
2
|
+
import { InferSchema } from '@ai-sdk/provider-utils';
|
|
3
|
+
|
|
4
|
+
type OpenaiResponsesChunk = InferSchema<typeof openaiResponsesChunkSchema>;
|
|
5
|
+
|
|
6
|
+
type ResponsesOutputTextAnnotationProviderMetadata = Extract<
|
|
7
|
+
OpenaiResponsesChunk,
|
|
8
|
+
{ type: 'response.output_text.annotation.added' }
|
|
9
|
+
>['annotation'];
|
|
10
|
+
|
|
11
|
+
export type ResponsesTextProviderMetadata = {
|
|
12
|
+
itemId: string;
|
|
13
|
+
annotations?: Array<ResponsesOutputTextAnnotationProviderMetadata>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type OpenaiResponsesTextProviderMetadata = {
|
|
17
|
+
openai: ResponsesTextProviderMetadata;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type ResponsesSourceDocumentProviderMetadata =
|
|
21
|
+
| {
|
|
22
|
+
type: 'file_citation';
|
|
23
|
+
fileId: string;
|
|
24
|
+
index: number;
|
|
25
|
+
}
|
|
26
|
+
| {
|
|
27
|
+
type: 'container_file_citation';
|
|
28
|
+
fileId: string;
|
|
29
|
+
containerId: string;
|
|
30
|
+
}
|
|
31
|
+
| {
|
|
32
|
+
type: 'file_path';
|
|
33
|
+
fileId: string;
|
|
34
|
+
index: number;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type OpenaiResponsesSourceDocumentProviderMetadata = {
|
|
38
|
+
openai: ResponsesSourceDocumentProviderMetadata;
|
|
39
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type OpenAISpeechAPITypes = {
|
|
2
|
+
/**
|
|
3
|
+
* The voice to use when generating the audio.
|
|
4
|
+
* Supported voices are alloy, ash, ballad, coral, echo, fable, onyx, nova, sage, shimmer, and verse.
|
|
5
|
+
* @default 'alloy'
|
|
6
|
+
*/
|
|
7
|
+
voice?:
|
|
8
|
+
| 'alloy'
|
|
9
|
+
| 'ash'
|
|
10
|
+
| 'ballad'
|
|
11
|
+
| 'coral'
|
|
12
|
+
| 'echo'
|
|
13
|
+
| 'fable'
|
|
14
|
+
| 'onyx'
|
|
15
|
+
| 'nova'
|
|
16
|
+
| 'sage'
|
|
17
|
+
| 'shimmer'
|
|
18
|
+
| 'verse';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The speed of the generated audio.
|
|
22
|
+
* Select a value from 0.25 to 4.0.
|
|
23
|
+
* @default 1.0
|
|
24
|
+
*/
|
|
25
|
+
speed?: number;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The format of the generated audio.
|
|
29
|
+
* @default 'mp3'
|
|
30
|
+
*/
|
|
31
|
+
response_format?: 'mp3' | 'opus' | 'aac' | 'flac' | 'wav' | 'pcm';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Instructions for the speech generation e.g. "Speak in a slow and steady tone".
|
|
35
|
+
* Does not work with tts-1 or tts-1-hd.
|
|
36
|
+
*/
|
|
37
|
+
instructions?: string;
|
|
38
|
+
};
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { createTestServer } from '@ai-sdk/test-server/with-vitest';
|
|
2
|
+
import { createOpenAI } from '../openai-provider';
|
|
3
|
+
import { OpenAISpeechModel } from './openai-speech-model';
|
|
4
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
5
|
+
|
|
6
|
+
vi.mock('../version', () => ({
|
|
7
|
+
VERSION: '0.0.0-test',
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
const provider = createOpenAI({ apiKey: 'test-api-key' });
|
|
11
|
+
const model = provider.speech('tts-1');
|
|
12
|
+
|
|
13
|
+
const server = createTestServer({
|
|
14
|
+
'https://api.openai.com/v1/audio/speech': {},
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('doGenerate', () => {
|
|
18
|
+
function prepareAudioResponse({
|
|
19
|
+
headers,
|
|
20
|
+
format = 'mp3',
|
|
21
|
+
}: {
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
format?: 'mp3' | 'opus' | 'aac' | 'flac' | 'wav' | 'pcm';
|
|
24
|
+
} = {}) {
|
|
25
|
+
const audioBuffer = new Uint8Array(100); // Mock audio data
|
|
26
|
+
server.urls['https://api.openai.com/v1/audio/speech'].response = {
|
|
27
|
+
type: 'binary',
|
|
28
|
+
headers: {
|
|
29
|
+
'content-type': `audio/${format}`,
|
|
30
|
+
...headers,
|
|
31
|
+
},
|
|
32
|
+
body: Buffer.from(audioBuffer),
|
|
33
|
+
};
|
|
34
|
+
return audioBuffer;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
it('should pass the model and text', async () => {
|
|
38
|
+
prepareAudioResponse();
|
|
39
|
+
|
|
40
|
+
await model.doGenerate({
|
|
41
|
+
text: 'Hello from the AI SDK!',
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
expect(await server.calls[0].requestBodyJson).toMatchObject({
|
|
45
|
+
model: 'tts-1',
|
|
46
|
+
input: 'Hello from the AI SDK!',
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should pass headers', async () => {
|
|
51
|
+
prepareAudioResponse();
|
|
52
|
+
|
|
53
|
+
const provider = createOpenAI({
|
|
54
|
+
apiKey: 'test-api-key',
|
|
55
|
+
organization: 'test-organization',
|
|
56
|
+
project: 'test-project',
|
|
57
|
+
headers: {
|
|
58
|
+
'Custom-Provider-Header': 'provider-header-value',
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
await provider.speech('tts-1').doGenerate({
|
|
63
|
+
text: 'Hello from the AI SDK!',
|
|
64
|
+
headers: {
|
|
65
|
+
'Custom-Request-Header': 'request-header-value',
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
expect(server.calls[0].requestHeaders).toMatchObject({
|
|
70
|
+
authorization: 'Bearer test-api-key',
|
|
71
|
+
'content-type': 'application/json',
|
|
72
|
+
'custom-provider-header': 'provider-header-value',
|
|
73
|
+
'custom-request-header': 'request-header-value',
|
|
74
|
+
'openai-organization': 'test-organization',
|
|
75
|
+
'openai-project': 'test-project',
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
expect(server.calls[0].requestUserAgent).toContain(
|
|
79
|
+
`ai-sdk/openai/0.0.0-test`,
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should pass options', async () => {
|
|
84
|
+
prepareAudioResponse();
|
|
85
|
+
|
|
86
|
+
await model.doGenerate({
|
|
87
|
+
text: 'Hello from the AI SDK!',
|
|
88
|
+
voice: 'nova',
|
|
89
|
+
outputFormat: 'opus',
|
|
90
|
+
speed: 1.5,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
expect(await server.calls[0].requestBodyJson).toMatchObject({
|
|
94
|
+
model: 'tts-1',
|
|
95
|
+
input: 'Hello from the AI SDK!',
|
|
96
|
+
voice: 'nova',
|
|
97
|
+
speed: 1.5,
|
|
98
|
+
response_format: 'opus',
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should return audio data with correct content type', async () => {
|
|
103
|
+
const audio = new Uint8Array(100); // Mock audio data
|
|
104
|
+
prepareAudioResponse({
|
|
105
|
+
format: 'opus',
|
|
106
|
+
headers: {
|
|
107
|
+
'x-request-id': 'test-request-id',
|
|
108
|
+
'x-ratelimit-remaining': '123',
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const result = await model.doGenerate({
|
|
113
|
+
text: 'Hello from the AI SDK!',
|
|
114
|
+
outputFormat: 'opus',
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
expect(result.audio).toStrictEqual(audio);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should include response data with timestamp, modelId and headers', async () => {
|
|
121
|
+
prepareAudioResponse({
|
|
122
|
+
headers: {
|
|
123
|
+
'x-request-id': 'test-request-id',
|
|
124
|
+
'x-ratelimit-remaining': '123',
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const testDate = new Date(0);
|
|
129
|
+
const customModel = new OpenAISpeechModel('tts-1', {
|
|
130
|
+
provider: 'test-provider',
|
|
131
|
+
url: () => 'https://api.openai.com/v1/audio/speech',
|
|
132
|
+
headers: () => ({}),
|
|
133
|
+
_internal: {
|
|
134
|
+
currentDate: () => testDate,
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const result = await customModel.doGenerate({
|
|
139
|
+
text: 'Hello from the AI SDK!',
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
expect(result.response).toMatchObject({
|
|
143
|
+
timestamp: testDate,
|
|
144
|
+
modelId: 'tts-1',
|
|
145
|
+
headers: {
|
|
146
|
+
'content-type': 'audio/mp3',
|
|
147
|
+
'x-request-id': 'test-request-id',
|
|
148
|
+
'x-ratelimit-remaining': '123',
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('should use real date when no custom date provider is specified', async () => {
|
|
154
|
+
prepareAudioResponse();
|
|
155
|
+
|
|
156
|
+
const testDate = new Date(0);
|
|
157
|
+
const customModel = new OpenAISpeechModel('tts-1', {
|
|
158
|
+
provider: 'test-provider',
|
|
159
|
+
url: () => 'https://api.openai.com/v1/audio/speech',
|
|
160
|
+
headers: () => ({}),
|
|
161
|
+
_internal: {
|
|
162
|
+
currentDate: () => testDate,
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
const result = await customModel.doGenerate({
|
|
167
|
+
text: 'Hello from the AI SDK!',
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
expect(result.response.timestamp.getTime()).toEqual(testDate.getTime());
|
|
171
|
+
expect(result.response.modelId).toBe('tts-1');
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('should handle different audio formats', async () => {
|
|
175
|
+
const formats = ['mp3', 'opus', 'aac', 'flac', 'wav', 'pcm'] as const;
|
|
176
|
+
|
|
177
|
+
for (const format of formats) {
|
|
178
|
+
const audio = prepareAudioResponse({ format });
|
|
179
|
+
|
|
180
|
+
const result = await model.doGenerate({
|
|
181
|
+
text: 'Hello from the AI SDK!',
|
|
182
|
+
providerOptions: {
|
|
183
|
+
openai: {
|
|
184
|
+
response_format: format,
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
expect(result.audio).toStrictEqual(audio);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('should include warnings if any are generated', async () => {
|
|
194
|
+
prepareAudioResponse();
|
|
195
|
+
|
|
196
|
+
const result = await model.doGenerate({
|
|
197
|
+
text: 'Hello from the AI SDK!',
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
expect(result.warnings).toEqual([]);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { SpeechModelV3, SharedV3Warning } from '@ai-sdk/provider';
|
|
2
|
+
import {
|
|
3
|
+
combineHeaders,
|
|
4
|
+
createBinaryResponseHandler,
|
|
5
|
+
parseProviderOptions,
|
|
6
|
+
postJsonToApi,
|
|
7
|
+
} from '@ai-sdk/provider-utils';
|
|
8
|
+
import { OpenAIConfig } from '../openai-config';
|
|
9
|
+
import { openaiFailedResponseHandler } from '../openai-error';
|
|
10
|
+
import { OpenAISpeechAPITypes } from './openai-speech-api';
|
|
11
|
+
import {
|
|
12
|
+
openaiSpeechProviderOptionsSchema,
|
|
13
|
+
OpenAISpeechModelId,
|
|
14
|
+
} from './openai-speech-options';
|
|
15
|
+
|
|
16
|
+
interface OpenAISpeechModelConfig extends OpenAIConfig {
|
|
17
|
+
_internal?: {
|
|
18
|
+
currentDate?: () => Date;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class OpenAISpeechModel implements SpeechModelV3 {
|
|
23
|
+
readonly specificationVersion = 'v3';
|
|
24
|
+
|
|
25
|
+
get provider(): string {
|
|
26
|
+
return this.config.provider;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
readonly modelId: OpenAISpeechModelId,
|
|
31
|
+
private readonly config: OpenAISpeechModelConfig,
|
|
32
|
+
) {}
|
|
33
|
+
|
|
34
|
+
private async getArgs({
|
|
35
|
+
text,
|
|
36
|
+
voice = 'alloy',
|
|
37
|
+
outputFormat = 'mp3',
|
|
38
|
+
speed,
|
|
39
|
+
instructions,
|
|
40
|
+
language,
|
|
41
|
+
providerOptions,
|
|
42
|
+
}: Parameters<SpeechModelV3['doGenerate']>[0]) {
|
|
43
|
+
const warnings: SharedV3Warning[] = [];
|
|
44
|
+
|
|
45
|
+
// Parse provider options
|
|
46
|
+
const openAIOptions = await parseProviderOptions({
|
|
47
|
+
provider: 'openai',
|
|
48
|
+
providerOptions,
|
|
49
|
+
schema: openaiSpeechProviderOptionsSchema,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Create request body
|
|
53
|
+
const requestBody: Record<string, unknown> = {
|
|
54
|
+
model: this.modelId,
|
|
55
|
+
input: text,
|
|
56
|
+
voice,
|
|
57
|
+
response_format: 'mp3',
|
|
58
|
+
speed,
|
|
59
|
+
instructions,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
if (outputFormat) {
|
|
63
|
+
if (['mp3', 'opus', 'aac', 'flac', 'wav', 'pcm'].includes(outputFormat)) {
|
|
64
|
+
requestBody.response_format = outputFormat;
|
|
65
|
+
} else {
|
|
66
|
+
warnings.push({
|
|
67
|
+
type: 'unsupported',
|
|
68
|
+
feature: 'outputFormat',
|
|
69
|
+
details: `Unsupported output format: ${outputFormat}. Using mp3 instead.`,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Add provider-specific options
|
|
75
|
+
if (openAIOptions) {
|
|
76
|
+
const speechModelOptions: OpenAISpeechAPITypes = {};
|
|
77
|
+
|
|
78
|
+
for (const key in speechModelOptions) {
|
|
79
|
+
const value = speechModelOptions[key as keyof OpenAISpeechAPITypes];
|
|
80
|
+
if (value !== undefined) {
|
|
81
|
+
requestBody[key] = value;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (language) {
|
|
87
|
+
warnings.push({
|
|
88
|
+
type: 'unsupported',
|
|
89
|
+
feature: 'language',
|
|
90
|
+
details: `OpenAI speech models do not support language selection. Language parameter "${language}" was ignored.`,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
requestBody,
|
|
96
|
+
warnings,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async doGenerate(
|
|
101
|
+
options: Parameters<SpeechModelV3['doGenerate']>[0],
|
|
102
|
+
): Promise<Awaited<ReturnType<SpeechModelV3['doGenerate']>>> {
|
|
103
|
+
const currentDate = this.config._internal?.currentDate?.() ?? new Date();
|
|
104
|
+
const { requestBody, warnings } = await this.getArgs(options);
|
|
105
|
+
|
|
106
|
+
const {
|
|
107
|
+
value: audio,
|
|
108
|
+
responseHeaders,
|
|
109
|
+
rawValue: rawResponse,
|
|
110
|
+
} = await postJsonToApi({
|
|
111
|
+
url: this.config.url({
|
|
112
|
+
path: '/audio/speech',
|
|
113
|
+
modelId: this.modelId,
|
|
114
|
+
}),
|
|
115
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
116
|
+
body: requestBody,
|
|
117
|
+
failedResponseHandler: openaiFailedResponseHandler,
|
|
118
|
+
successfulResponseHandler: createBinaryResponseHandler(),
|
|
119
|
+
abortSignal: options.abortSignal,
|
|
120
|
+
fetch: this.config.fetch,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
audio,
|
|
125
|
+
warnings,
|
|
126
|
+
request: {
|
|
127
|
+
body: JSON.stringify(requestBody),
|
|
128
|
+
},
|
|
129
|
+
response: {
|
|
130
|
+
timestamp: currentDate,
|
|
131
|
+
modelId: this.modelId,
|
|
132
|
+
headers: responseHeaders,
|
|
133
|
+
body: rawResponse,
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { InferSchema, lazySchema, zodSchema } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { z } from 'zod/v4';
|
|
3
|
+
|
|
4
|
+
export type OpenAISpeechModelId =
|
|
5
|
+
| 'tts-1'
|
|
6
|
+
| 'tts-1-hd'
|
|
7
|
+
| 'gpt-4o-mini-tts'
|
|
8
|
+
| (string & {});
|
|
9
|
+
|
|
10
|
+
// https://platform.openai.com/docs/api-reference/audio/createSpeech
|
|
11
|
+
export const openaiSpeechProviderOptionsSchema = lazySchema(() =>
|
|
12
|
+
zodSchema(
|
|
13
|
+
z.object({
|
|
14
|
+
instructions: z.string().nullish(),
|
|
15
|
+
speed: z.number().min(0.25).max(4.0).default(1.0).nullish(),
|
|
16
|
+
}),
|
|
17
|
+
),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
export type OpenAISpeechCallOptions = InferSchema<
|
|
21
|
+
typeof openaiSpeechProviderOptionsSchema
|
|
22
|
+
>;
|