@ai-sdk/openai 4.0.0-beta.7 → 4.0.0-beta.74
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 +636 -24
- package/README.md +2 -0
- package/dist/index.d.ts +240 -44
- package/dist/index.js +3345 -1683
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +390 -36
- package/dist/internal/index.js +2707 -1706
- package/dist/internal/index.js.map +1 -1
- package/docs/03-openai.mdx +413 -39
- package/package.json +16 -17
- package/src/chat/convert-openai-chat-usage.ts +1 -1
- package/src/chat/convert-to-openai-chat-messages.ts +96 -68
- package/src/chat/map-openai-finish-reason.ts +1 -1
- package/src/chat/openai-chat-api.ts +6 -2
- package/src/chat/{openai-chat-options.ts → openai-chat-language-model-options.ts} +11 -1
- package/src/chat/openai-chat-language-model.ts +82 -148
- package/src/chat/openai-chat-prepare-tools.ts +3 -3
- package/src/completion/convert-openai-completion-usage.ts +1 -1
- package/src/completion/convert-to-openai-completion-prompt.ts +1 -2
- package/src/completion/map-openai-finish-reason.ts +1 -1
- package/src/completion/openai-completion-api.ts +5 -2
- package/src/completion/{openai-completion-options.ts → openai-completion-language-model-options.ts} +5 -1
- package/src/completion/openai-completion-language-model.ts +53 -17
- package/src/embedding/{openai-embedding-options.ts → openai-embedding-model-options.ts} +5 -1
- package/src/embedding/openai-embedding-model.ts +22 -5
- package/src/files/openai-files-api.ts +17 -0
- package/src/files/openai-files-options.ts +22 -0
- package/src/files/openai-files.ts +100 -0
- package/src/image/openai-image-model-options.ts +123 -0
- package/src/image/openai-image-model.ts +62 -83
- package/src/index.ts +15 -6
- package/src/internal/index.ts +7 -6
- package/src/openai-config.ts +7 -7
- package/src/openai-language-model-capabilities.ts +5 -4
- package/src/openai-provider.ts +80 -9
- package/src/openai-stream-error.ts +181 -0
- package/src/openai-tools.ts +12 -1
- package/src/realtime/index.ts +2 -0
- package/src/realtime/openai-realtime-event-mapper.ts +436 -0
- package/src/realtime/openai-realtime-model-options.ts +3 -0
- package/src/realtime/openai-realtime-model.ts +111 -0
- package/src/responses/convert-openai-responses-usage.ts +1 -1
- package/src/responses/convert-to-openai-responses-input.ts +345 -90
- package/src/responses/map-openai-responses-finish-reason.ts +1 -1
- package/src/responses/openai-responses-api.ts +186 -17
- package/src/responses/{openai-responses-options.ts → openai-responses-language-model-options.ts} +55 -1
- package/src/responses/openai-responses-language-model.ts +330 -52
- package/src/responses/openai-responses-prepare-tools.ts +129 -18
- package/src/responses/openai-responses-provider-metadata.ts +12 -2
- package/src/skills/openai-skills-api.ts +31 -0
- package/src/skills/openai-skills.ts +83 -0
- package/src/speech/{openai-speech-options.ts → openai-speech-model-options.ts} +5 -1
- package/src/speech/openai-speech-model.ts +23 -7
- package/src/tool/apply-patch.ts +33 -32
- package/src/tool/code-interpreter.ts +40 -41
- package/src/tool/custom.ts +2 -8
- package/src/tool/file-search.ts +3 -3
- package/src/tool/image-generation.ts +2 -2
- package/src/tool/local-shell.ts +2 -2
- package/src/tool/mcp.ts +3 -3
- package/src/tool/shell.ts +9 -4
- package/src/tool/tool-search.ts +98 -0
- package/src/tool/web-search-preview.ts +2 -2
- package/src/tool/web-search.ts +10 -2
- package/src/transcription/{openai-transcription-options.ts → openai-transcription-model-options.ts} +5 -1
- package/src/transcription/openai-transcription-model.ts +35 -13
- package/dist/index.d.mts +0 -1107
- package/dist/index.mjs +0 -6509
- package/dist/index.mjs.map +0 -1
- package/dist/internal/index.d.mts +0 -1137
- package/dist/internal/index.mjs +0 -6322
- package/dist/internal/index.mjs.map +0 -1
- package/src/image/openai-image-options.ts +0 -31
package/src/tool/local-shell.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
createProviderDefinedToolFactoryWithOutputSchema,
|
|
3
3
|
lazySchema,
|
|
4
4
|
zodSchema,
|
|
5
5
|
} from '@ai-sdk/provider-utils';
|
|
@@ -24,7 +24,7 @@ export const localShellOutputSchema = lazySchema(() =>
|
|
|
24
24
|
zodSchema(z.object({ output: z.string() })),
|
|
25
25
|
);
|
|
26
26
|
|
|
27
|
-
export const localShell =
|
|
27
|
+
export const localShell = createProviderDefinedToolFactoryWithOutputSchema<
|
|
28
28
|
{
|
|
29
29
|
/**
|
|
30
30
|
* Execute a shell command on the server.
|
package/src/tool/mcp.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
createProviderExecutedToolFactory,
|
|
3
3
|
lazySchema,
|
|
4
4
|
zodSchema,
|
|
5
5
|
} from '@ai-sdk/provider-utils';
|
|
6
|
-
import { JSONValue } from '@ai-sdk/provider';
|
|
6
|
+
import type { JSONValue } from '@ai-sdk/provider';
|
|
7
7
|
import { z } from 'zod/v4';
|
|
8
8
|
|
|
9
9
|
const jsonValueSchema: z.ZodType<JSONValue> = z.lazy(() =>
|
|
@@ -105,7 +105,7 @@ type McpArgs = {
|
|
|
105
105
|
serverUrl?: string;
|
|
106
106
|
};
|
|
107
107
|
|
|
108
|
-
export const mcpToolFactory =
|
|
108
|
+
export const mcpToolFactory = createProviderExecutedToolFactory<
|
|
109
109
|
{},
|
|
110
110
|
{
|
|
111
111
|
type: 'call';
|
package/src/tool/shell.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
createProviderDefinedToolFactoryWithOutputSchema,
|
|
3
3
|
lazySchema,
|
|
4
4
|
zodSchema,
|
|
5
5
|
} from '@ai-sdk/provider-utils';
|
|
6
|
+
import type { SharedV4ProviderReference } from '@ai-sdk/provider';
|
|
6
7
|
import { z } from 'zod/v4';
|
|
7
8
|
|
|
8
9
|
export const shellInputSchema = lazySchema(() =>
|
|
@@ -39,7 +40,7 @@ const shellSkillsSchema = z
|
|
|
39
40
|
z.discriminatedUnion('type', [
|
|
40
41
|
z.object({
|
|
41
42
|
type: z.literal('skillReference'),
|
|
42
|
-
|
|
43
|
+
providerReference: z.record(z.string(), z.string()),
|
|
43
44
|
version: z.string().optional(),
|
|
44
45
|
}),
|
|
45
46
|
z.object({
|
|
@@ -125,7 +126,11 @@ type ShellArgs = {
|
|
|
125
126
|
}>;
|
|
126
127
|
};
|
|
127
128
|
skills?: Array<
|
|
128
|
-
| {
|
|
129
|
+
| {
|
|
130
|
+
type: 'skillReference';
|
|
131
|
+
providerReference: SharedV4ProviderReference;
|
|
132
|
+
version?: string;
|
|
133
|
+
}
|
|
129
134
|
| {
|
|
130
135
|
type: 'inline';
|
|
131
136
|
name: string;
|
|
@@ -152,7 +157,7 @@ type ShellArgs = {
|
|
|
152
157
|
};
|
|
153
158
|
};
|
|
154
159
|
|
|
155
|
-
export const shell =
|
|
160
|
+
export const shell = createProviderDefinedToolFactoryWithOutputSchema<
|
|
156
161
|
{
|
|
157
162
|
/**
|
|
158
163
|
* Shell tool action containing commands to execute.
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { JSONObject } from '@ai-sdk/provider';
|
|
2
|
+
import {
|
|
3
|
+
createProviderDefinedToolFactoryWithOutputSchema,
|
|
4
|
+
lazySchema,
|
|
5
|
+
zodSchema,
|
|
6
|
+
type FlexibleSchema,
|
|
7
|
+
} from '@ai-sdk/provider-utils';
|
|
8
|
+
import { z } from 'zod/v4';
|
|
9
|
+
|
|
10
|
+
export const toolSearchArgsSchema = lazySchema(() =>
|
|
11
|
+
zodSchema(
|
|
12
|
+
z.object({
|
|
13
|
+
execution: z.enum(['server', 'client']).optional(),
|
|
14
|
+
description: z.string().optional(),
|
|
15
|
+
parameters: z.record(z.string(), z.unknown()).optional(),
|
|
16
|
+
}),
|
|
17
|
+
),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
export const toolSearchInputSchema = lazySchema(() =>
|
|
21
|
+
zodSchema(
|
|
22
|
+
z.object({
|
|
23
|
+
arguments: z.unknown().optional(),
|
|
24
|
+
call_id: z.string().nullish(),
|
|
25
|
+
}),
|
|
26
|
+
),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
export const toolSearchOutputSchema: FlexibleSchema<{
|
|
30
|
+
tools: Array<JSONObject>;
|
|
31
|
+
}> = lazySchema(() =>
|
|
32
|
+
zodSchema(
|
|
33
|
+
z.object({
|
|
34
|
+
tools: z.array(z.record(z.string(), z.unknown())),
|
|
35
|
+
}),
|
|
36
|
+
),
|
|
37
|
+
) as FlexibleSchema<{ tools: Array<JSONObject> }>;
|
|
38
|
+
|
|
39
|
+
const toolSearchToolFactory = createProviderDefinedToolFactoryWithOutputSchema<
|
|
40
|
+
{
|
|
41
|
+
/**
|
|
42
|
+
* The arguments from the tool_search_call.
|
|
43
|
+
* This is preserved for multi-turn conversation reconstruction.
|
|
44
|
+
*/
|
|
45
|
+
arguments?: unknown;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The call ID from the tool_search_call.
|
|
49
|
+
* Present for client-executed tool search; null for hosted.
|
|
50
|
+
*/
|
|
51
|
+
call_id?: string | null;
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
/**
|
|
55
|
+
* The tools that were loaded by the tool search.
|
|
56
|
+
* These are the deferred tools that the model requested to load.
|
|
57
|
+
* Each tool is represented as a JSON object with properties depending on its type.
|
|
58
|
+
*
|
|
59
|
+
* Common properties include:
|
|
60
|
+
* - `type`: The type of the tool (e.g., 'function', 'web_search', etc.)
|
|
61
|
+
* - `name`: The name of the tool (for function tools)
|
|
62
|
+
* - `description`: A description of the tool
|
|
63
|
+
* - `deferLoading`: Whether this tool was deferred (had defer_loading: true)
|
|
64
|
+
* - `parameters`: The JSON Schema for the function parameters (for function tools)
|
|
65
|
+
* - `strict`: Whether to enable strict schema adherence (for function tools)
|
|
66
|
+
*/
|
|
67
|
+
tools: Array<JSONObject>;
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
/**
|
|
71
|
+
* Whether the tool search is executed by the server (hosted) or client.
|
|
72
|
+
* - `'server'` (default): OpenAI performs the search across deferred tools.
|
|
73
|
+
* - `'client'`: The model emits a `tool_search_call` and your `execute`
|
|
74
|
+
* function performs the lookup, returning the tools to load.
|
|
75
|
+
*/
|
|
76
|
+
execution?: 'server' | 'client';
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A description of the tool search capability.
|
|
80
|
+
* Only used for client-executed tool search.
|
|
81
|
+
*/
|
|
82
|
+
description?: string;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* JSON Schema for the search arguments your application expects.
|
|
86
|
+
* Only used for client-executed tool search.
|
|
87
|
+
*/
|
|
88
|
+
parameters?: Record<string, unknown>;
|
|
89
|
+
}
|
|
90
|
+
>({
|
|
91
|
+
id: 'openai.tool_search',
|
|
92
|
+
inputSchema: toolSearchInputSchema,
|
|
93
|
+
outputSchema: toolSearchOutputSchema,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
export const toolSearch = (
|
|
97
|
+
args: Parameters<typeof toolSearchToolFactory>[0] = {},
|
|
98
|
+
) => toolSearchToolFactory(args);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
createProviderExecutedToolFactory,
|
|
3
3
|
lazySchema,
|
|
4
4
|
zodSchema,
|
|
5
5
|
} from '@ai-sdk/provider-utils';
|
|
@@ -50,7 +50,7 @@ const webSearchPreviewOutputSchema = lazySchema(() =>
|
|
|
50
50
|
),
|
|
51
51
|
);
|
|
52
52
|
|
|
53
|
-
export const webSearchPreview =
|
|
53
|
+
export const webSearchPreview = createProviderExecutedToolFactory<
|
|
54
54
|
{
|
|
55
55
|
// Web search preview doesn't take input parameters - it's controlled by the prompt
|
|
56
56
|
},
|
package/src/tool/web-search.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
createProviderExecutedToolFactory,
|
|
3
3
|
lazySchema,
|
|
4
4
|
zodSchema,
|
|
5
5
|
} from '@ai-sdk/provider-utils';
|
|
@@ -36,6 +36,7 @@ export const webSearchOutputSchema = lazySchema(() =>
|
|
|
36
36
|
z.object({
|
|
37
37
|
type: z.literal('search'),
|
|
38
38
|
query: z.string().optional(),
|
|
39
|
+
queries: z.array(z.string()).optional(),
|
|
39
40
|
}),
|
|
40
41
|
z.object({
|
|
41
42
|
type: z.literal('openPage'),
|
|
@@ -60,7 +61,7 @@ export const webSearchOutputSchema = lazySchema(() =>
|
|
|
60
61
|
),
|
|
61
62
|
);
|
|
62
63
|
|
|
63
|
-
export const webSearchToolFactory =
|
|
64
|
+
export const webSearchToolFactory = createProviderExecutedToolFactory<
|
|
64
65
|
{
|
|
65
66
|
// Web search doesn't take input parameters - it's controlled by the prompt
|
|
66
67
|
},
|
|
@@ -78,8 +79,15 @@ export const webSearchToolFactory = createProviderToolFactoryWithOutputSchema<
|
|
|
78
79
|
|
|
79
80
|
/**
|
|
80
81
|
* The search query.
|
|
82
|
+
*
|
|
83
|
+
* @deprecated Use `queries` instead.
|
|
81
84
|
*/
|
|
82
85
|
query?: string;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The search queries the model used.
|
|
89
|
+
*/
|
|
90
|
+
queries?: string[];
|
|
83
91
|
}
|
|
84
92
|
| {
|
|
85
93
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type {
|
|
2
2
|
TranscriptionModelV4,
|
|
3
3
|
TranscriptionModelV4CallOptions,
|
|
4
4
|
SharedV4Warning,
|
|
@@ -10,16 +10,18 @@ import {
|
|
|
10
10
|
mediaTypeToExtension,
|
|
11
11
|
parseProviderOptions,
|
|
12
12
|
postFormDataToApi,
|
|
13
|
+
serializeModelOptions,
|
|
14
|
+
WORKFLOW_DESERIALIZE,
|
|
15
|
+
WORKFLOW_SERIALIZE,
|
|
13
16
|
} from '@ai-sdk/provider-utils';
|
|
14
|
-
import { OpenAIConfig } from '../openai-config';
|
|
17
|
+
import type { OpenAIConfig } from '../openai-config';
|
|
15
18
|
import { openaiFailedResponseHandler } from '../openai-error';
|
|
16
19
|
import { openaiTranscriptionResponseSchema } from './openai-transcription-api';
|
|
17
20
|
import {
|
|
18
|
-
OpenAITranscriptionModelId,
|
|
19
21
|
openAITranscriptionModelOptions,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
type OpenAITranscriptionModelId,
|
|
23
|
+
type OpenAITranscriptionModelOptions,
|
|
24
|
+
} from './openai-transcription-model-options';
|
|
23
25
|
export type OpenAITranscriptionCallOptions = Omit<
|
|
24
26
|
TranscriptionModelV4CallOptions,
|
|
25
27
|
'providerOptions'
|
|
@@ -99,6 +101,20 @@ const languageMap = {
|
|
|
99
101
|
export class OpenAITranscriptionModel implements TranscriptionModelV4 {
|
|
100
102
|
readonly specificationVersion = 'v4';
|
|
101
103
|
|
|
104
|
+
static [WORKFLOW_SERIALIZE](model: OpenAITranscriptionModel) {
|
|
105
|
+
return serializeModelOptions({
|
|
106
|
+
modelId: model.modelId,
|
|
107
|
+
config: model.config,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
static [WORKFLOW_DESERIALIZE](options: {
|
|
112
|
+
modelId: OpenAITranscriptionModelId;
|
|
113
|
+
config: OpenAITranscriptionModelConfig;
|
|
114
|
+
}) {
|
|
115
|
+
return new OpenAITranscriptionModel(options.modelId, options.config);
|
|
116
|
+
}
|
|
117
|
+
|
|
102
118
|
get provider(): string {
|
|
103
119
|
return this.config.provider;
|
|
104
120
|
}
|
|
@@ -137,20 +153,26 @@ export class OpenAITranscriptionModel implements TranscriptionModelV4 {
|
|
|
137
153
|
`audio.${fileExtension}`,
|
|
138
154
|
);
|
|
139
155
|
|
|
156
|
+
if (this.modelId === 'whisper-1') {
|
|
157
|
+
formData.append('response_format', 'verbose_json');
|
|
158
|
+
}
|
|
159
|
+
|
|
140
160
|
// Add provider-specific options
|
|
141
161
|
if (openAIOptions) {
|
|
162
|
+
const isGpt4oTranscribeModel = [
|
|
163
|
+
'gpt-4o-transcribe',
|
|
164
|
+
'gpt-4o-mini-transcribe',
|
|
165
|
+
].includes(this.modelId);
|
|
166
|
+
|
|
142
167
|
const transcriptionModelOptions = {
|
|
143
168
|
include: openAIOptions.include,
|
|
144
169
|
language: openAIOptions.language,
|
|
145
170
|
prompt: openAIOptions.prompt,
|
|
146
171
|
// https://platform.openai.com/docs/api-reference/audio/createTranscription#audio_createtranscription-response_format
|
|
147
172
|
// prefer verbose_json to get segments for models that support it
|
|
148
|
-
|
|
149
|
-
'
|
|
150
|
-
|
|
151
|
-
].includes(this.modelId)
|
|
152
|
-
? 'json'
|
|
153
|
-
: 'verbose_json',
|
|
173
|
+
...(this.modelId !== 'whisper-1' && {
|
|
174
|
+
response_format: isGpt4oTranscribeModel ? 'json' : 'verbose_json',
|
|
175
|
+
}),
|
|
154
176
|
temperature: openAIOptions.temperature,
|
|
155
177
|
timestamp_granularities: openAIOptions.timestampGranularities,
|
|
156
178
|
};
|
|
@@ -189,7 +211,7 @@ export class OpenAITranscriptionModel implements TranscriptionModelV4 {
|
|
|
189
211
|
path: '/audio/transcriptions',
|
|
190
212
|
modelId: this.modelId,
|
|
191
213
|
}),
|
|
192
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
214
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
193
215
|
formData,
|
|
194
216
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
195
217
|
successfulResponseHandler: createJsonResponseHandler(
|