@depup/ai-sdk__openai 3.0.41-depup.0
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 +3101 -0
- package/LICENSE +13 -0
- package/README.md +25 -0
- package/changes.json +5 -0
- package/dist/index.d.mts +1107 -0
- package/dist/index.d.ts +1107 -0
- package/dist/index.js +6408 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +6493 -0
- package/dist/index.mjs.map +1 -0
- package/dist/internal/index.d.mts +1137 -0
- package/dist/internal/index.d.ts +1137 -0
- package/dist/internal/index.js +6256 -0
- package/dist/internal/index.js.map +1 -0
- package/dist/internal/index.mjs +6306 -0
- package/dist/internal/index.mjs.map +1 -0
- package/docs/03-openai.mdx +2396 -0
- package/internal.d.ts +1 -0
- package/package.json +96 -0
- package/src/chat/convert-openai-chat-usage.ts +57 -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.ts +703 -0
- package/src/chat/openai-chat-options.ts +192 -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.ts +336 -0
- package/src/completion/openai-completion-options.ts +61 -0
- package/src/embedding/openai-embedding-api.ts +13 -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.ts +349 -0
- package/src/image/openai-image-options.ts +31 -0
- package/src/index.ts +23 -0
- package/src/internal/index.ts +19 -0
- package/src/openai-config.ts +18 -0
- package/src/openai-error.ts +22 -0
- package/src/openai-language-model-capabilities.ts +52 -0
- package/src/openai-provider.ts +270 -0
- package/src/openai-tools.ts +126 -0
- package/src/responses/convert-openai-responses-usage.ts +53 -0
- package/src/responses/convert-to-openai-responses-input.ts +735 -0
- package/src/responses/map-openai-responses-finish-reason.ts +22 -0
- package/src/responses/openai-responses-api.ts +1260 -0
- package/src/responses/openai-responses-language-model.ts +2098 -0
- package/src/responses/openai-responses-options.ts +299 -0
- package/src/responses/openai-responses-prepare-tools.ts +408 -0
- package/src/responses/openai-responses-provider-metadata.ts +62 -0
- package/src/speech/openai-speech-api.ts +38 -0
- package/src/speech/openai-speech-model.ts +137 -0
- package/src/speech/openai-speech-options.ts +26 -0
- package/src/tool/apply-patch.ts +141 -0
- package/src/tool/code-interpreter.ts +104 -0
- package/src/tool/custom.ts +64 -0
- package/src/tool/file-search.ts +145 -0
- package/src/tool/image-generation.ts +126 -0
- package/src/tool/local-shell.ts +72 -0
- package/src/tool/mcp.ts +125 -0
- package/src/tool/shell.ts +203 -0
- package/src/tool/web-search-preview.ts +141 -0
- package/src/tool/web-search.ts +181 -0
- package/src/transcription/openai-transcription-api.ts +37 -0
- package/src/transcription/openai-transcription-model.ts +232 -0
- package/src/transcription/openai-transcription-options.ts +53 -0
- package/src/transcription/transcription-test.mp3 +0 -0
- package/src/version.ts +6 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
openaiResponsesChunkSchema,
|
|
3
|
+
OpenAIResponsesLogprobs,
|
|
4
|
+
} from './openai-responses-api';
|
|
5
|
+
import { InferSchema } from '@ai-sdk/provider-utils';
|
|
6
|
+
|
|
7
|
+
type OpenaiResponsesChunk = InferSchema<typeof openaiResponsesChunkSchema>;
|
|
8
|
+
|
|
9
|
+
type ResponsesOutputTextAnnotationProviderMetadata = Extract<
|
|
10
|
+
OpenaiResponsesChunk,
|
|
11
|
+
{ type: 'response.output_text.annotation.added' }
|
|
12
|
+
>['annotation'];
|
|
13
|
+
|
|
14
|
+
export type ResponsesProviderMetadata = {
|
|
15
|
+
responseId: string | null | undefined;
|
|
16
|
+
logprobs?: Array<OpenAIResponsesLogprobs>;
|
|
17
|
+
serviceTier?: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type ResponsesReasoningProviderMetadata = {
|
|
21
|
+
itemId: string;
|
|
22
|
+
reasoningEncryptedContent?: string | null;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type OpenaiResponsesReasoningProviderMetadata = {
|
|
26
|
+
openai: ResponsesReasoningProviderMetadata;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type OpenaiResponsesProviderMetadata = {
|
|
30
|
+
openai: ResponsesProviderMetadata;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type ResponsesTextProviderMetadata = {
|
|
34
|
+
itemId: string;
|
|
35
|
+
phase?: 'commentary' | 'final_answer' | null;
|
|
36
|
+
annotations?: Array<ResponsesOutputTextAnnotationProviderMetadata>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type OpenaiResponsesTextProviderMetadata = {
|
|
40
|
+
openai: ResponsesTextProviderMetadata;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type ResponsesSourceDocumentProviderMetadata =
|
|
44
|
+
| {
|
|
45
|
+
type: 'file_citation';
|
|
46
|
+
fileId: string;
|
|
47
|
+
index: number;
|
|
48
|
+
}
|
|
49
|
+
| {
|
|
50
|
+
type: 'container_file_citation';
|
|
51
|
+
fileId: string;
|
|
52
|
+
containerId: string;
|
|
53
|
+
}
|
|
54
|
+
| {
|
|
55
|
+
type: 'file_path';
|
|
56
|
+
fileId: string;
|
|
57
|
+
index: number;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type OpenaiResponsesSourceDocumentProviderMetadata = {
|
|
61
|
+
openai: ResponsesSourceDocumentProviderMetadata;
|
|
62
|
+
};
|
|
@@ -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,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
|
+
openaiSpeechModelOptionsSchema,
|
|
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: openaiSpeechModelOptionsSchema,
|
|
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,26 @@
|
|
|
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-1106'
|
|
7
|
+
| 'tts-1-hd'
|
|
8
|
+
| 'tts-1-hd-1106'
|
|
9
|
+
| 'gpt-4o-mini-tts'
|
|
10
|
+
| 'gpt-4o-mini-tts-2025-03-20'
|
|
11
|
+
| 'gpt-4o-mini-tts-2025-12-15'
|
|
12
|
+
| (string & {});
|
|
13
|
+
|
|
14
|
+
// https://platform.openai.com/docs/api-reference/audio/createSpeech
|
|
15
|
+
export const openaiSpeechModelOptionsSchema = lazySchema(() =>
|
|
16
|
+
zodSchema(
|
|
17
|
+
z.object({
|
|
18
|
+
instructions: z.string().nullish(),
|
|
19
|
+
speed: z.number().min(0.25).max(4.0).default(1.0).nullish(),
|
|
20
|
+
}),
|
|
21
|
+
),
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
export type OpenAISpeechModelOptions = InferSchema<
|
|
25
|
+
typeof openaiSpeechModelOptionsSchema
|
|
26
|
+
>;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createProviderToolFactoryWithOutputSchema,
|
|
3
|
+
lazySchema,
|
|
4
|
+
zodSchema,
|
|
5
|
+
} from '@ai-sdk/provider-utils';
|
|
6
|
+
import { z } from 'zod/v4';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Schema for the apply_patch input - what the model sends.
|
|
10
|
+
*
|
|
11
|
+
* Refer the official spec here: https://platform.openai.com/docs/api-reference/responses/create#responses_create-input-input_item_list-item-apply_patch_tool_call
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
export const applyPatchInputSchema = lazySchema(() =>
|
|
15
|
+
zodSchema(
|
|
16
|
+
z.object({
|
|
17
|
+
callId: z.string(),
|
|
18
|
+
operation: z.discriminatedUnion('type', [
|
|
19
|
+
z.object({
|
|
20
|
+
type: z.literal('create_file'),
|
|
21
|
+
path: z.string(),
|
|
22
|
+
diff: z.string(),
|
|
23
|
+
}),
|
|
24
|
+
z.object({
|
|
25
|
+
type: z.literal('delete_file'),
|
|
26
|
+
path: z.string(),
|
|
27
|
+
}),
|
|
28
|
+
z.object({
|
|
29
|
+
type: z.literal('update_file'),
|
|
30
|
+
path: z.string(),
|
|
31
|
+
diff: z.string(),
|
|
32
|
+
}),
|
|
33
|
+
]),
|
|
34
|
+
}),
|
|
35
|
+
),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Schema for the apply_patch output - what we send back.
|
|
40
|
+
*/
|
|
41
|
+
export const applyPatchOutputSchema = lazySchema(() =>
|
|
42
|
+
zodSchema(
|
|
43
|
+
z.object({
|
|
44
|
+
status: z.enum(['completed', 'failed']),
|
|
45
|
+
output: z.string().optional(),
|
|
46
|
+
}),
|
|
47
|
+
),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Schema for tool arguments (configuration options).
|
|
52
|
+
* The apply_patch tool doesn't require any configuration options.
|
|
53
|
+
*/
|
|
54
|
+
export const applyPatchArgsSchema = lazySchema(() => zodSchema(z.object({})));
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Type definitions for the apply_patch operations.
|
|
58
|
+
*/
|
|
59
|
+
export type ApplyPatchOperation =
|
|
60
|
+
| {
|
|
61
|
+
type: 'create_file';
|
|
62
|
+
/**
|
|
63
|
+
* Path of the file to create relative to the workspace root.
|
|
64
|
+
*/
|
|
65
|
+
path: string;
|
|
66
|
+
/**
|
|
67
|
+
* Unified diff content to apply when creating the file.
|
|
68
|
+
*/
|
|
69
|
+
diff: string;
|
|
70
|
+
}
|
|
71
|
+
| {
|
|
72
|
+
type: 'delete_file';
|
|
73
|
+
/**
|
|
74
|
+
* Path of the file to delete relative to the workspace root.
|
|
75
|
+
*/
|
|
76
|
+
path: string;
|
|
77
|
+
}
|
|
78
|
+
| {
|
|
79
|
+
type: 'update_file';
|
|
80
|
+
/**
|
|
81
|
+
* Path of the file to update relative to the workspace root.
|
|
82
|
+
*/
|
|
83
|
+
path: string;
|
|
84
|
+
/**
|
|
85
|
+
* Unified diff content to apply to the existing file.
|
|
86
|
+
*/
|
|
87
|
+
diff: string;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The apply_patch tool lets GPT-5.1 create, update, and delete files in your
|
|
92
|
+
* codebase using structured diffs. Instead of just suggesting edits, the model
|
|
93
|
+
* emits patch operations that your application applies and then reports back on,
|
|
94
|
+
* enabling iterative, multi-step code editing workflows.
|
|
95
|
+
*
|
|
96
|
+
* The tool factory creates a provider-defined tool that:
|
|
97
|
+
* - Receives patch operations from the model (create_file, update_file, delete_file)
|
|
98
|
+
* - Returns the status of applying those patches (completed or failed)
|
|
99
|
+
*
|
|
100
|
+
*/
|
|
101
|
+
export const applyPatchToolFactory = createProviderToolFactoryWithOutputSchema<
|
|
102
|
+
{
|
|
103
|
+
/**
|
|
104
|
+
* The unique ID of the apply patch tool call generated by the model.
|
|
105
|
+
*/
|
|
106
|
+
callId: string;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The specific create, delete, or update instruction for the apply_patch tool call.
|
|
110
|
+
*/
|
|
111
|
+
operation: ApplyPatchOperation;
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
/**
|
|
115
|
+
* The status of the apply patch tool call output.
|
|
116
|
+
* - 'completed': The patch was applied successfully.
|
|
117
|
+
* - 'failed': The patch failed to apply.
|
|
118
|
+
*/
|
|
119
|
+
status: 'completed' | 'failed';
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Optional human-readable log text from the apply patch tool
|
|
123
|
+
* (e.g., patch results or errors).
|
|
124
|
+
*/
|
|
125
|
+
output?: string;
|
|
126
|
+
},
|
|
127
|
+
// No configuration options for apply_patch
|
|
128
|
+
{}
|
|
129
|
+
>({
|
|
130
|
+
id: 'openai.apply_patch',
|
|
131
|
+
inputSchema: applyPatchInputSchema,
|
|
132
|
+
outputSchema: applyPatchOutputSchema,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The apply_patch tool lets GPT-5.1 create, update, and delete files in your
|
|
137
|
+
* codebase using structured diffs. Instead of just suggesting edits, the model
|
|
138
|
+
* emits patch operations that your application applies and then reports back on,
|
|
139
|
+
* enabling iterative, multi-step code editing workflows.
|
|
140
|
+
*/
|
|
141
|
+
export const applyPatch = applyPatchToolFactory;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createProviderToolFactoryWithOutputSchema,
|
|
3
|
+
lazySchema,
|
|
4
|
+
zodSchema,
|
|
5
|
+
} from '@ai-sdk/provider-utils';
|
|
6
|
+
import { z } from 'zod/v4';
|
|
7
|
+
|
|
8
|
+
export const codeInterpreterInputSchema = lazySchema(() =>
|
|
9
|
+
zodSchema(
|
|
10
|
+
z.object({
|
|
11
|
+
code: z.string().nullish(),
|
|
12
|
+
containerId: z.string(),
|
|
13
|
+
}),
|
|
14
|
+
),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
export const codeInterpreterOutputSchema = lazySchema(() =>
|
|
18
|
+
zodSchema(
|
|
19
|
+
z.object({
|
|
20
|
+
outputs: z
|
|
21
|
+
.array(
|
|
22
|
+
z.discriminatedUnion('type', [
|
|
23
|
+
z.object({ type: z.literal('logs'), logs: z.string() }),
|
|
24
|
+
z.object({ type: z.literal('image'), url: z.string() }),
|
|
25
|
+
]),
|
|
26
|
+
)
|
|
27
|
+
.nullish(),
|
|
28
|
+
}),
|
|
29
|
+
),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
export const codeInterpreterArgsSchema = lazySchema(() =>
|
|
33
|
+
zodSchema(
|
|
34
|
+
z.object({
|
|
35
|
+
container: z
|
|
36
|
+
.union([
|
|
37
|
+
z.string(),
|
|
38
|
+
z.object({
|
|
39
|
+
fileIds: z.array(z.string()).optional(),
|
|
40
|
+
}),
|
|
41
|
+
])
|
|
42
|
+
.optional(),
|
|
43
|
+
}),
|
|
44
|
+
),
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
type CodeInterpreterArgs = {
|
|
48
|
+
/**
|
|
49
|
+
* The code interpreter container.
|
|
50
|
+
* Can be a container ID
|
|
51
|
+
* or an object that specifies uploaded file IDs to make available to your code.
|
|
52
|
+
*/
|
|
53
|
+
container?: string | { fileIds?: string[] };
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const codeInterpreterToolFactory =
|
|
57
|
+
createProviderToolFactoryWithOutputSchema<
|
|
58
|
+
{
|
|
59
|
+
/**
|
|
60
|
+
* The code to run, or null if not available.
|
|
61
|
+
*/
|
|
62
|
+
code?: string | null;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The ID of the container used to run the code.
|
|
66
|
+
*/
|
|
67
|
+
containerId: string;
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
/**
|
|
71
|
+
* The outputs generated by the code interpreter, such as logs or images.
|
|
72
|
+
* Can be null if no outputs are available.
|
|
73
|
+
*/
|
|
74
|
+
outputs?: Array<
|
|
75
|
+
| {
|
|
76
|
+
type: 'logs';
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The logs output from the code interpreter.
|
|
80
|
+
*/
|
|
81
|
+
logs: string;
|
|
82
|
+
}
|
|
83
|
+
| {
|
|
84
|
+
type: 'image';
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The URL of the image output from the code interpreter.
|
|
88
|
+
*/
|
|
89
|
+
url: string;
|
|
90
|
+
}
|
|
91
|
+
> | null;
|
|
92
|
+
},
|
|
93
|
+
CodeInterpreterArgs
|
|
94
|
+
>({
|
|
95
|
+
id: 'openai.code_interpreter',
|
|
96
|
+
inputSchema: codeInterpreterInputSchema,
|
|
97
|
+
outputSchema: codeInterpreterOutputSchema,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
export const codeInterpreter = (
|
|
101
|
+
args: CodeInterpreterArgs = {}, // default
|
|
102
|
+
) => {
|
|
103
|
+
return codeInterpreterToolFactory(args);
|
|
104
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createProviderToolFactory,
|
|
3
|
+
lazySchema,
|
|
4
|
+
zodSchema,
|
|
5
|
+
} from '@ai-sdk/provider-utils';
|
|
6
|
+
import { z } from 'zod/v4';
|
|
7
|
+
|
|
8
|
+
export const customArgsSchema = lazySchema(() =>
|
|
9
|
+
zodSchema(
|
|
10
|
+
z.object({
|
|
11
|
+
name: z.string(),
|
|
12
|
+
description: z.string().optional(),
|
|
13
|
+
format: z
|
|
14
|
+
.union([
|
|
15
|
+
z.object({
|
|
16
|
+
type: z.literal('grammar'),
|
|
17
|
+
syntax: z.enum(['regex', 'lark']),
|
|
18
|
+
definition: z.string(),
|
|
19
|
+
}),
|
|
20
|
+
z.object({
|
|
21
|
+
type: z.literal('text'),
|
|
22
|
+
}),
|
|
23
|
+
])
|
|
24
|
+
.optional(),
|
|
25
|
+
}),
|
|
26
|
+
),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const customInputSchema = lazySchema(() => zodSchema(z.string()));
|
|
30
|
+
|
|
31
|
+
export const customToolFactory = createProviderToolFactory<
|
|
32
|
+
string,
|
|
33
|
+
{
|
|
34
|
+
/**
|
|
35
|
+
* The name of the custom tool, used to identify it in the API.
|
|
36
|
+
*/
|
|
37
|
+
name: string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* An optional description of what the tool does.
|
|
41
|
+
*/
|
|
42
|
+
description?: string;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The output format specification for the tool.
|
|
46
|
+
* Omit for unconstrained text output.
|
|
47
|
+
*/
|
|
48
|
+
format?:
|
|
49
|
+
| {
|
|
50
|
+
type: 'grammar';
|
|
51
|
+
syntax: 'regex' | 'lark';
|
|
52
|
+
definition: string;
|
|
53
|
+
}
|
|
54
|
+
| {
|
|
55
|
+
type: 'text';
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
>({
|
|
59
|
+
id: 'openai.custom',
|
|
60
|
+
inputSchema: customInputSchema,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export const customTool = (args: Parameters<typeof customToolFactory>[0]) =>
|
|
64
|
+
customToolFactory(args);
|