@ai-sdk/openai 3.0.13 → 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 +12 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +1 -1
- package/dist/internal/index.d.ts +1 -1
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +5 -4
- 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
package/src/tool/mcp.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createProviderToolFactoryWithOutputSchema,
|
|
3
|
+
lazySchema,
|
|
4
|
+
zodSchema,
|
|
5
|
+
} from '@ai-sdk/provider-utils';
|
|
6
|
+
import { JSONValue } from '@ai-sdk/provider';
|
|
7
|
+
import { z } from 'zod/v4';
|
|
8
|
+
|
|
9
|
+
const jsonValueSchema: z.ZodType<JSONValue> = z.lazy(() =>
|
|
10
|
+
z.union([
|
|
11
|
+
z.string(),
|
|
12
|
+
z.number(),
|
|
13
|
+
z.boolean(),
|
|
14
|
+
z.null(),
|
|
15
|
+
z.array(jsonValueSchema),
|
|
16
|
+
z.record(z.string(), jsonValueSchema),
|
|
17
|
+
]),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
export const mcpArgsSchema = lazySchema(() =>
|
|
21
|
+
zodSchema(
|
|
22
|
+
z
|
|
23
|
+
.object({
|
|
24
|
+
serverLabel: z.string(),
|
|
25
|
+
allowedTools: z
|
|
26
|
+
.union([
|
|
27
|
+
z.array(z.string()),
|
|
28
|
+
z.object({
|
|
29
|
+
readOnly: z.boolean().optional(),
|
|
30
|
+
toolNames: z.array(z.string()).optional(),
|
|
31
|
+
}),
|
|
32
|
+
])
|
|
33
|
+
.optional(),
|
|
34
|
+
authorization: z.string().optional(),
|
|
35
|
+
connectorId: z.string().optional(),
|
|
36
|
+
headers: z.record(z.string(), z.string()).optional(),
|
|
37
|
+
|
|
38
|
+
requireApproval: z
|
|
39
|
+
.union([
|
|
40
|
+
z.enum(['always', 'never']),
|
|
41
|
+
z.object({
|
|
42
|
+
never: z
|
|
43
|
+
.object({
|
|
44
|
+
toolNames: z.array(z.string()).optional(),
|
|
45
|
+
})
|
|
46
|
+
.optional(),
|
|
47
|
+
}),
|
|
48
|
+
])
|
|
49
|
+
.optional(),
|
|
50
|
+
serverDescription: z.string().optional(),
|
|
51
|
+
serverUrl: z.string().optional(),
|
|
52
|
+
})
|
|
53
|
+
.refine(
|
|
54
|
+
v => v.serverUrl != null || v.connectorId != null,
|
|
55
|
+
'One of serverUrl or connectorId must be provided.',
|
|
56
|
+
),
|
|
57
|
+
),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const mcpInputSchema = lazySchema(() => zodSchema(z.object({})));
|
|
61
|
+
|
|
62
|
+
export const mcpOutputSchema = lazySchema(() =>
|
|
63
|
+
zodSchema(
|
|
64
|
+
z.object({
|
|
65
|
+
type: z.literal('call'),
|
|
66
|
+
serverLabel: z.string(),
|
|
67
|
+
name: z.string(),
|
|
68
|
+
arguments: z.string(),
|
|
69
|
+
output: z.string().nullish(),
|
|
70
|
+
error: z.union([z.string(), jsonValueSchema]).optional(),
|
|
71
|
+
}),
|
|
72
|
+
),
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
type McpArgs = {
|
|
76
|
+
/** A label for this MCP server, used to identify it in tool calls. */
|
|
77
|
+
serverLabel: string;
|
|
78
|
+
/** List of allowed tool names or a filter object. */
|
|
79
|
+
allowedTools?:
|
|
80
|
+
| string[]
|
|
81
|
+
| {
|
|
82
|
+
readOnly?: boolean;
|
|
83
|
+
toolNames?: string[];
|
|
84
|
+
};
|
|
85
|
+
/** OAuth access token usable with the remote MCP server or connector. */
|
|
86
|
+
authorization?: string;
|
|
87
|
+
/** Identifier for a service connector. */
|
|
88
|
+
connectorId?: string;
|
|
89
|
+
/** Optional HTTP headers to send to the MCP server. */
|
|
90
|
+
headers?: Record<string, string>;
|
|
91
|
+
/**
|
|
92
|
+
* Which tools require approval before execution.
|
|
93
|
+
*/
|
|
94
|
+
requireApproval?:
|
|
95
|
+
| 'always'
|
|
96
|
+
| 'never'
|
|
97
|
+
| {
|
|
98
|
+
never?: {
|
|
99
|
+
toolNames?: string[];
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
/** Optional description of the MCP server. */
|
|
103
|
+
serverDescription?: string;
|
|
104
|
+
/** URL for the MCP server. One of serverUrl or connectorId must be provided. */
|
|
105
|
+
serverUrl?: string;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export const mcpToolFactory = createProviderToolFactoryWithOutputSchema<
|
|
109
|
+
{},
|
|
110
|
+
{
|
|
111
|
+
type: 'call';
|
|
112
|
+
serverLabel: string;
|
|
113
|
+
name: string;
|
|
114
|
+
arguments: string;
|
|
115
|
+
output?: string | null;
|
|
116
|
+
error?: JSONValue;
|
|
117
|
+
},
|
|
118
|
+
McpArgs
|
|
119
|
+
>({
|
|
120
|
+
id: 'openai.mcp',
|
|
121
|
+
inputSchema: mcpInputSchema,
|
|
122
|
+
outputSchema: mcpOutputSchema,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
export const mcp = (args: McpArgs) => mcpToolFactory(args);
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createProviderToolFactoryWithOutputSchema,
|
|
3
|
+
lazySchema,
|
|
4
|
+
zodSchema,
|
|
5
|
+
} from '@ai-sdk/provider-utils';
|
|
6
|
+
import { z } from 'zod/v4';
|
|
7
|
+
|
|
8
|
+
export const shellInputSchema = lazySchema(() =>
|
|
9
|
+
zodSchema(
|
|
10
|
+
z.object({
|
|
11
|
+
action: z.object({
|
|
12
|
+
commands: z.array(z.string()),
|
|
13
|
+
timeoutMs: z.number().optional(),
|
|
14
|
+
maxOutputLength: z.number().optional(),
|
|
15
|
+
}),
|
|
16
|
+
}),
|
|
17
|
+
),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
export const shellOutputSchema = lazySchema(() =>
|
|
21
|
+
zodSchema(
|
|
22
|
+
z.object({
|
|
23
|
+
output: z.array(
|
|
24
|
+
z.object({
|
|
25
|
+
stdout: z.string(),
|
|
26
|
+
stderr: z.string(),
|
|
27
|
+
outcome: z.discriminatedUnion('type', [
|
|
28
|
+
z.object({ type: z.literal('timeout') }),
|
|
29
|
+
z.object({ type: z.literal('exit'), exitCode: z.number() }),
|
|
30
|
+
]),
|
|
31
|
+
}),
|
|
32
|
+
),
|
|
33
|
+
}),
|
|
34
|
+
),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
export const shell = createProviderToolFactoryWithOutputSchema<
|
|
38
|
+
{
|
|
39
|
+
/**
|
|
40
|
+
* Shell tool action containing commands to execute.
|
|
41
|
+
*/
|
|
42
|
+
action: {
|
|
43
|
+
/**
|
|
44
|
+
* A list of shell commands to execute.
|
|
45
|
+
*/
|
|
46
|
+
commands: string[];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Optional timeout in milliseconds for the commands.
|
|
50
|
+
*/
|
|
51
|
+
timeoutMs?: number;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Optional maximum number of characters to return from each command.
|
|
55
|
+
*/
|
|
56
|
+
maxOutputLength?: number;
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
/**
|
|
61
|
+
* An array of shell call output contents.
|
|
62
|
+
*/
|
|
63
|
+
output: Array<{
|
|
64
|
+
/**
|
|
65
|
+
* Standard output from the command.
|
|
66
|
+
*/
|
|
67
|
+
stdout: string;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Standard error from the command.
|
|
71
|
+
*/
|
|
72
|
+
stderr: string;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The outcome of the shell execution - either timeout or exit with code.
|
|
76
|
+
*/
|
|
77
|
+
outcome: { type: 'timeout' } | { type: 'exit'; exitCode: number };
|
|
78
|
+
}>;
|
|
79
|
+
},
|
|
80
|
+
{}
|
|
81
|
+
>({
|
|
82
|
+
id: 'openai.shell',
|
|
83
|
+
inputSchema: shellInputSchema,
|
|
84
|
+
outputSchema: shellOutputSchema,
|
|
85
|
+
});
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createProviderToolFactoryWithOutputSchema,
|
|
3
|
+
lazySchema,
|
|
4
|
+
zodSchema,
|
|
5
|
+
} from '@ai-sdk/provider-utils';
|
|
6
|
+
import { z } from 'zod/v4';
|
|
7
|
+
|
|
8
|
+
export const webSearchPreviewArgsSchema = lazySchema(() =>
|
|
9
|
+
zodSchema(
|
|
10
|
+
z.object({
|
|
11
|
+
searchContextSize: z.enum(['low', 'medium', 'high']).optional(),
|
|
12
|
+
userLocation: z
|
|
13
|
+
.object({
|
|
14
|
+
type: z.literal('approximate'),
|
|
15
|
+
country: z.string().optional(),
|
|
16
|
+
city: z.string().optional(),
|
|
17
|
+
region: z.string().optional(),
|
|
18
|
+
timezone: z.string().optional(),
|
|
19
|
+
})
|
|
20
|
+
.optional(),
|
|
21
|
+
}),
|
|
22
|
+
),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
export const webSearchPreviewInputSchema = lazySchema(() =>
|
|
26
|
+
zodSchema(z.object({})),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const webSearchPreviewOutputSchema = lazySchema(() =>
|
|
30
|
+
zodSchema(
|
|
31
|
+
z.object({
|
|
32
|
+
action: z.discriminatedUnion('type', [
|
|
33
|
+
z.object({
|
|
34
|
+
type: z.literal('search'),
|
|
35
|
+
query: z.string().optional(),
|
|
36
|
+
}),
|
|
37
|
+
z.object({
|
|
38
|
+
type: z.literal('openPage'),
|
|
39
|
+
url: z.string().nullish(),
|
|
40
|
+
}),
|
|
41
|
+
z.object({
|
|
42
|
+
type: z.literal('findInPage'),
|
|
43
|
+
url: z.string().nullish(),
|
|
44
|
+
pattern: z.string().nullish(),
|
|
45
|
+
}),
|
|
46
|
+
]),
|
|
47
|
+
}),
|
|
48
|
+
),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
export const webSearchPreview = createProviderToolFactoryWithOutputSchema<
|
|
52
|
+
{
|
|
53
|
+
// Web search preview doesn't take input parameters - it's controlled by the prompt
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
/**
|
|
57
|
+
* An object describing the specific action taken in this web search call.
|
|
58
|
+
* Includes details on how the model used the web (search, open_page, find_in_page).
|
|
59
|
+
*/
|
|
60
|
+
action:
|
|
61
|
+
| {
|
|
62
|
+
/**
|
|
63
|
+
* Action type "search" - Performs a web search query.
|
|
64
|
+
*/
|
|
65
|
+
type: 'search';
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The search query.
|
|
69
|
+
*/
|
|
70
|
+
query?: string;
|
|
71
|
+
}
|
|
72
|
+
| {
|
|
73
|
+
/**
|
|
74
|
+
* Action type "openPage" - Opens a specific URL from search results.
|
|
75
|
+
*/
|
|
76
|
+
type: 'openPage';
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The URL opened by the model.
|
|
80
|
+
*/
|
|
81
|
+
url?: string | null;
|
|
82
|
+
}
|
|
83
|
+
| {
|
|
84
|
+
/**
|
|
85
|
+
* Action type "findInPage": Searches for a pattern within a loaded page.
|
|
86
|
+
*/
|
|
87
|
+
type: 'findInPage';
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The URL of the page searched for the pattern.
|
|
91
|
+
*/
|
|
92
|
+
url?: string | null;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The pattern or text to search for within the page.
|
|
96
|
+
*/
|
|
97
|
+
pattern?: string | null;
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
/**
|
|
102
|
+
* Search context size to use for the web search.
|
|
103
|
+
* - high: Most comprehensive context, highest cost, slower response
|
|
104
|
+
* - medium: Balanced context, cost, and latency (default)
|
|
105
|
+
* - low: Least context, lowest cost, fastest response
|
|
106
|
+
*/
|
|
107
|
+
searchContextSize?: 'low' | 'medium' | 'high';
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* User location information to provide geographically relevant search results.
|
|
111
|
+
*/
|
|
112
|
+
userLocation?: {
|
|
113
|
+
/**
|
|
114
|
+
* Type of location (always 'approximate')
|
|
115
|
+
*/
|
|
116
|
+
type: 'approximate';
|
|
117
|
+
/**
|
|
118
|
+
* Two-letter ISO country code (e.g., 'US', 'GB')
|
|
119
|
+
*/
|
|
120
|
+
country?: string;
|
|
121
|
+
/**
|
|
122
|
+
* City name (free text, e.g., 'Minneapolis')
|
|
123
|
+
*/
|
|
124
|
+
city?: string;
|
|
125
|
+
/**
|
|
126
|
+
* Region name (free text, e.g., 'Minnesota')
|
|
127
|
+
*/
|
|
128
|
+
region?: string;
|
|
129
|
+
/**
|
|
130
|
+
* IANA timezone (e.g., 'America/Chicago')
|
|
131
|
+
*/
|
|
132
|
+
timezone?: string;
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
>({
|
|
136
|
+
id: 'openai.web_search_preview',
|
|
137
|
+
inputSchema: webSearchPreviewInputSchema,
|
|
138
|
+
outputSchema: webSearchPreviewOutputSchema,
|
|
139
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { InferSchema, Tool } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { describe, expectTypeOf, it } from 'vitest';
|
|
3
|
+
import { webSearch, webSearchOutputSchema } from './web-search';
|
|
4
|
+
|
|
5
|
+
describe('web-search tool type', () => {
|
|
6
|
+
it('should have Tool type', () => {
|
|
7
|
+
const webSearchTool = webSearch();
|
|
8
|
+
|
|
9
|
+
expectTypeOf(webSearchTool).toEqualTypeOf<
|
|
10
|
+
Tool<{}, InferSchema<typeof webSearchOutputSchema>>
|
|
11
|
+
>();
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createProviderToolFactoryWithOutputSchema,
|
|
3
|
+
lazySchema,
|
|
4
|
+
zodSchema,
|
|
5
|
+
} from '@ai-sdk/provider-utils';
|
|
6
|
+
import { z } from 'zod/v4';
|
|
7
|
+
|
|
8
|
+
export const webSearchArgsSchema = lazySchema(() =>
|
|
9
|
+
zodSchema(
|
|
10
|
+
z.object({
|
|
11
|
+
externalWebAccess: z.boolean().optional(),
|
|
12
|
+
filters: z
|
|
13
|
+
.object({ allowedDomains: z.array(z.string()).optional() })
|
|
14
|
+
.optional(),
|
|
15
|
+
searchContextSize: z.enum(['low', 'medium', 'high']).optional(),
|
|
16
|
+
userLocation: z
|
|
17
|
+
.object({
|
|
18
|
+
type: z.literal('approximate'),
|
|
19
|
+
country: z.string().optional(),
|
|
20
|
+
city: z.string().optional(),
|
|
21
|
+
region: z.string().optional(),
|
|
22
|
+
timezone: z.string().optional(),
|
|
23
|
+
})
|
|
24
|
+
.optional(),
|
|
25
|
+
}),
|
|
26
|
+
),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const webSearchInputSchema = lazySchema(() => zodSchema(z.object({})));
|
|
30
|
+
|
|
31
|
+
export const webSearchOutputSchema = lazySchema(() =>
|
|
32
|
+
zodSchema(
|
|
33
|
+
z.object({
|
|
34
|
+
action: z.discriminatedUnion('type', [
|
|
35
|
+
z.object({
|
|
36
|
+
type: z.literal('search'),
|
|
37
|
+
query: z.string().optional(),
|
|
38
|
+
}),
|
|
39
|
+
z.object({
|
|
40
|
+
type: z.literal('openPage'),
|
|
41
|
+
url: z.string().nullish(),
|
|
42
|
+
}),
|
|
43
|
+
z.object({
|
|
44
|
+
type: z.literal('findInPage'),
|
|
45
|
+
url: z.string().nullish(),
|
|
46
|
+
pattern: z.string().nullish(),
|
|
47
|
+
}),
|
|
48
|
+
]),
|
|
49
|
+
sources: z
|
|
50
|
+
.array(
|
|
51
|
+
z.discriminatedUnion('type', [
|
|
52
|
+
z.object({ type: z.literal('url'), url: z.string() }),
|
|
53
|
+
z.object({ type: z.literal('api'), name: z.string() }),
|
|
54
|
+
]),
|
|
55
|
+
)
|
|
56
|
+
.optional(),
|
|
57
|
+
}),
|
|
58
|
+
),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
export const webSearchToolFactory = createProviderToolFactoryWithOutputSchema<
|
|
62
|
+
{
|
|
63
|
+
// Web search doesn't take input parameters - it's controlled by the prompt
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
/**
|
|
67
|
+
* An object describing the specific action taken in this web search call.
|
|
68
|
+
* Includes details on how the model used the web (search, open_page, find_in_page).
|
|
69
|
+
*/
|
|
70
|
+
action:
|
|
71
|
+
| {
|
|
72
|
+
/**
|
|
73
|
+
* Action type "search" - Performs a web search query.
|
|
74
|
+
*/
|
|
75
|
+
type: 'search';
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The search query.
|
|
79
|
+
*/
|
|
80
|
+
query?: string;
|
|
81
|
+
}
|
|
82
|
+
| {
|
|
83
|
+
/**
|
|
84
|
+
* Action type "openPage" - Opens a specific URL from search results.
|
|
85
|
+
*/
|
|
86
|
+
type: 'openPage';
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The URL opened by the model.
|
|
90
|
+
*/
|
|
91
|
+
url?: string | null;
|
|
92
|
+
}
|
|
93
|
+
| {
|
|
94
|
+
/**
|
|
95
|
+
* Action type "findInPage": Searches for a pattern within a loaded page.
|
|
96
|
+
*/
|
|
97
|
+
type: 'findInPage';
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The URL of the page searched for the pattern.
|
|
101
|
+
*/
|
|
102
|
+
url?: string | null;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The pattern or text to search for within the page.
|
|
106
|
+
*/
|
|
107
|
+
pattern?: string | null;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Optional sources cited by the model for the web search call.
|
|
112
|
+
*/
|
|
113
|
+
sources?: Array<
|
|
114
|
+
{ type: 'url'; url: string } | { type: 'api'; name: string }
|
|
115
|
+
>;
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
/**
|
|
119
|
+
* Whether to use external web access for fetching live content.
|
|
120
|
+
* - true: Fetch live web content (default)
|
|
121
|
+
* - false: Use cached/indexed results
|
|
122
|
+
*/
|
|
123
|
+
externalWebAccess?: boolean;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Filters for the search.
|
|
127
|
+
*/
|
|
128
|
+
filters?: {
|
|
129
|
+
/**
|
|
130
|
+
* Allowed domains for the search.
|
|
131
|
+
* If not provided, all domains are allowed.
|
|
132
|
+
* Subdomains of the provided domains are allowed as well.
|
|
133
|
+
*/
|
|
134
|
+
allowedDomains?: string[];
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Search context size to use for the web search.
|
|
139
|
+
* - high: Most comprehensive context, highest cost, slower response
|
|
140
|
+
* - medium: Balanced context, cost, and latency (default)
|
|
141
|
+
* - low: Least context, lowest cost, fastest response
|
|
142
|
+
*/
|
|
143
|
+
searchContextSize?: 'low' | 'medium' | 'high';
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* User location information to provide geographically relevant search results.
|
|
147
|
+
*/
|
|
148
|
+
userLocation?: {
|
|
149
|
+
/**
|
|
150
|
+
* Type of location (always 'approximate')
|
|
151
|
+
*/
|
|
152
|
+
type: 'approximate';
|
|
153
|
+
/**
|
|
154
|
+
* Two-letter ISO country code (e.g., 'US', 'GB')
|
|
155
|
+
*/
|
|
156
|
+
country?: string;
|
|
157
|
+
/**
|
|
158
|
+
* City name (free text, e.g., 'Minneapolis')
|
|
159
|
+
*/
|
|
160
|
+
city?: string;
|
|
161
|
+
/**
|
|
162
|
+
* Region name (free text, e.g., 'Minnesota')
|
|
163
|
+
*/
|
|
164
|
+
region?: string;
|
|
165
|
+
/**
|
|
166
|
+
* IANA timezone (e.g., 'America/Chicago')
|
|
167
|
+
*/
|
|
168
|
+
timezone?: string;
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
>({
|
|
172
|
+
id: 'openai.web_search',
|
|
173
|
+
inputSchema: webSearchInputSchema,
|
|
174
|
+
outputSchema: webSearchOutputSchema,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
export const webSearch = (
|
|
178
|
+
args: Parameters<typeof webSearchToolFactory>[0] = {}, // default
|
|
179
|
+
) => webSearchToolFactory(args);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { z } from 'zod/v4';
|
|
3
|
+
|
|
4
|
+
export const openaiTranscriptionResponseSchema = lazySchema(() =>
|
|
5
|
+
zodSchema(
|
|
6
|
+
z.object({
|
|
7
|
+
text: z.string(),
|
|
8
|
+
language: z.string().nullish(),
|
|
9
|
+
duration: z.number().nullish(),
|
|
10
|
+
words: z
|
|
11
|
+
.array(
|
|
12
|
+
z.object({
|
|
13
|
+
word: z.string(),
|
|
14
|
+
start: z.number(),
|
|
15
|
+
end: z.number(),
|
|
16
|
+
}),
|
|
17
|
+
)
|
|
18
|
+
.nullish(),
|
|
19
|
+
segments: z
|
|
20
|
+
.array(
|
|
21
|
+
z.object({
|
|
22
|
+
id: z.number(),
|
|
23
|
+
seek: z.number(),
|
|
24
|
+
start: z.number(),
|
|
25
|
+
end: z.number(),
|
|
26
|
+
text: z.string(),
|
|
27
|
+
tokens: z.array(z.number()),
|
|
28
|
+
temperature: z.number(),
|
|
29
|
+
avg_logprob: z.number(),
|
|
30
|
+
compression_ratio: z.number(),
|
|
31
|
+
no_speech_prob: z.number(),
|
|
32
|
+
}),
|
|
33
|
+
)
|
|
34
|
+
.nullish(),
|
|
35
|
+
}),
|
|
36
|
+
),
|
|
37
|
+
);
|