@ai-sdk/azure 4.0.0-canary.65 → 4.0.0-canary.67
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 +14 -0
- package/dist/index.d.ts +8 -1
- package/dist/index.js +41 -12
- package/dist/index.js.map +1 -1
- package/docs/04-azure.mdx +52 -16
- package/package.json +2 -2
- package/src/azure-openai-provider.ts +65 -23
- package/src/azure-openai-tools.ts +3 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @ai-sdk/azure
|
|
2
2
|
|
|
3
|
+
## 4.0.0-canary.67
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 9f4dca0: Add Microsoft Entra ID token provider authentication for Azure OpenAI.
|
|
8
|
+
|
|
9
|
+
## 4.0.0-canary.66
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- e776fc7: feat(provider/azure):web search tool in the Azure OpenAI Responses API.
|
|
14
|
+
- Updated dependencies [e776fc7]
|
|
15
|
+
- @ai-sdk/openai@4.0.0-canary.66
|
|
16
|
+
|
|
3
17
|
## 4.0.0-canary.65
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
export { OpenAILanguageModelChatOptions as OpenAIChatLanguageModelOptions, OpenAILanguageModelChatOptions, OpenAILanguageModelResponsesOptions, OpenAILanguageModelResponsesOptions as OpenAIResponsesProviderOptions } from '@ai-sdk/openai';
|
|
2
2
|
import { ProviderV4, LanguageModelV4, EmbeddingModelV4, ImageModelV4, TranscriptionModelV4, SpeechModelV4 } from '@ai-sdk/provider';
|
|
3
3
|
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
4
|
-
import { codeInterpreter, fileSearch, imageGeneration, webSearchPreview, ResponsesProviderMetadata, ResponsesReasoningProviderMetadata, ResponsesSourceDocumentProviderMetadata, ResponsesTextProviderMetadata } from '@ai-sdk/openai/internal';
|
|
4
|
+
import { codeInterpreter, fileSearch, imageGeneration, webSearch, webSearchPreview, ResponsesProviderMetadata, ResponsesReasoningProviderMetadata, ResponsesSourceDocumentProviderMetadata, ResponsesTextProviderMetadata } from '@ai-sdk/openai/internal';
|
|
5
5
|
|
|
6
6
|
declare const azureOpenaiTools: {
|
|
7
7
|
codeInterpreter: typeof codeInterpreter;
|
|
8
8
|
fileSearch: typeof fileSearch;
|
|
9
9
|
imageGeneration: typeof imageGeneration;
|
|
10
|
+
webSearch: typeof webSearch;
|
|
10
11
|
webSearchPreview: typeof webSearchPreview;
|
|
11
12
|
};
|
|
12
13
|
|
|
@@ -83,6 +84,12 @@ interface AzureOpenAIProviderSettings {
|
|
|
83
84
|
* API key for authenticating requests.
|
|
84
85
|
*/
|
|
85
86
|
apiKey?: string;
|
|
87
|
+
/**
|
|
88
|
+
* A function that returns an access token for Microsoft Entra
|
|
89
|
+
* (formerly known as Azure Active Directory), which will be invoked
|
|
90
|
+
* on every request.
|
|
91
|
+
*/
|
|
92
|
+
tokenProvider?: (() => Promise<string>) | undefined;
|
|
86
93
|
/**
|
|
87
94
|
* Custom headers to include in the requests.
|
|
88
95
|
*/
|
package/dist/index.js
CHANGED
|
@@ -8,9 +8,13 @@ import {
|
|
|
8
8
|
OpenAISpeechModel,
|
|
9
9
|
OpenAITranscriptionModel
|
|
10
10
|
} from "@ai-sdk/openai/internal";
|
|
11
|
+
import {
|
|
12
|
+
InvalidArgumentError
|
|
13
|
+
} from "@ai-sdk/provider";
|
|
11
14
|
import {
|
|
12
15
|
loadApiKey,
|
|
13
16
|
loadSetting,
|
|
17
|
+
normalizeHeaders,
|
|
14
18
|
withUserAgentSuffix
|
|
15
19
|
} from "@ai-sdk/provider-utils";
|
|
16
20
|
|
|
@@ -19,32 +23,57 @@ import {
|
|
|
19
23
|
codeInterpreter,
|
|
20
24
|
fileSearch,
|
|
21
25
|
imageGeneration,
|
|
26
|
+
webSearch,
|
|
22
27
|
webSearchPreview
|
|
23
28
|
} from "@ai-sdk/openai/internal";
|
|
24
29
|
var azureOpenaiTools = {
|
|
25
30
|
codeInterpreter,
|
|
26
31
|
fileSearch,
|
|
27
32
|
imageGeneration,
|
|
33
|
+
webSearch,
|
|
28
34
|
webSearchPreview
|
|
29
35
|
};
|
|
30
36
|
|
|
31
37
|
// src/version.ts
|
|
32
|
-
var VERSION = true ? "4.0.0-canary.
|
|
38
|
+
var VERSION = true ? "4.0.0-canary.67" : "0.0.0-test";
|
|
33
39
|
|
|
34
40
|
// src/azure-openai-provider.ts
|
|
35
41
|
function createAzure(options = {}) {
|
|
36
42
|
var _a;
|
|
43
|
+
const tokenProvider = options.tokenProvider;
|
|
44
|
+
if (options.apiKey && tokenProvider) {
|
|
45
|
+
throw new InvalidArgumentError({
|
|
46
|
+
argument: "apiKey/tokenProvider",
|
|
47
|
+
message: "Both apiKey and tokenProvider were provided. Please use only one authentication method."
|
|
48
|
+
});
|
|
49
|
+
}
|
|
37
50
|
const getHeaders = () => {
|
|
38
|
-
const
|
|
51
|
+
const authHeaders = tokenProvider ? {} : {
|
|
39
52
|
"api-key": loadApiKey({
|
|
40
53
|
apiKey: options.apiKey,
|
|
41
54
|
environmentVariableName: "AZURE_API_KEY",
|
|
42
55
|
description: "Azure OpenAI"
|
|
43
|
-
})
|
|
44
|
-
...options.headers
|
|
56
|
+
})
|
|
45
57
|
};
|
|
46
|
-
return withUserAgentSuffix(
|
|
58
|
+
return withUserAgentSuffix(
|
|
59
|
+
{
|
|
60
|
+
...authHeaders,
|
|
61
|
+
...options.headers
|
|
62
|
+
},
|
|
63
|
+
`ai-sdk/azure/${VERSION}`
|
|
64
|
+
);
|
|
47
65
|
};
|
|
66
|
+
const fetch = tokenProvider ? async (input, init) => {
|
|
67
|
+
var _a2;
|
|
68
|
+
const headers = normalizeHeaders(init == null ? void 0 : init.headers);
|
|
69
|
+
if (headers.authorization == null) {
|
|
70
|
+
headers.authorization = `Bearer ${await tokenProvider()}`;
|
|
71
|
+
}
|
|
72
|
+
return ((_a2 = options.fetch) != null ? _a2 : globalThis.fetch)(input, {
|
|
73
|
+
...init,
|
|
74
|
+
headers
|
|
75
|
+
});
|
|
76
|
+
} : options.fetch;
|
|
48
77
|
const getResourceName = () => loadSetting({
|
|
49
78
|
settingValue: options.resourceName,
|
|
50
79
|
settingName: "resourceName",
|
|
@@ -68,25 +97,25 @@ function createAzure(options = {}) {
|
|
|
68
97
|
provider: "azure.chat",
|
|
69
98
|
url,
|
|
70
99
|
headers: getHeaders,
|
|
71
|
-
fetch
|
|
100
|
+
fetch
|
|
72
101
|
});
|
|
73
102
|
const createCompletionModel = (modelId) => new OpenAICompletionLanguageModel(modelId, {
|
|
74
103
|
provider: "azure.completion",
|
|
75
104
|
url,
|
|
76
105
|
headers: getHeaders,
|
|
77
|
-
fetch
|
|
106
|
+
fetch
|
|
78
107
|
});
|
|
79
108
|
const createEmbeddingModel = (modelId) => new OpenAIEmbeddingModel(modelId, {
|
|
80
109
|
provider: "azure.embeddings",
|
|
81
110
|
headers: getHeaders,
|
|
82
111
|
url,
|
|
83
|
-
fetch
|
|
112
|
+
fetch
|
|
84
113
|
});
|
|
85
114
|
const createResponsesModel = (modelId) => new OpenAIResponsesLanguageModel(modelId, {
|
|
86
115
|
provider: "azure.responses",
|
|
87
116
|
url,
|
|
88
117
|
headers: getHeaders,
|
|
89
|
-
fetch
|
|
118
|
+
fetch,
|
|
90
119
|
// Soft-deprecated. TODO: remove in v8
|
|
91
120
|
fileIdPrefixes: ["assistant-"]
|
|
92
121
|
});
|
|
@@ -94,19 +123,19 @@ function createAzure(options = {}) {
|
|
|
94
123
|
provider: "azure.image",
|
|
95
124
|
url,
|
|
96
125
|
headers: getHeaders,
|
|
97
|
-
fetch
|
|
126
|
+
fetch
|
|
98
127
|
});
|
|
99
128
|
const createTranscriptionModel = (modelId) => new OpenAITranscriptionModel(modelId, {
|
|
100
129
|
provider: "azure.transcription",
|
|
101
130
|
url,
|
|
102
131
|
headers: getHeaders,
|
|
103
|
-
fetch
|
|
132
|
+
fetch
|
|
104
133
|
});
|
|
105
134
|
const createSpeechModel = (modelId) => new OpenAISpeechModel(modelId, {
|
|
106
135
|
provider: "azure.speech",
|
|
107
136
|
url,
|
|
108
137
|
headers: getHeaders,
|
|
109
|
-
fetch
|
|
138
|
+
fetch
|
|
110
139
|
});
|
|
111
140
|
const provider = function(deploymentId) {
|
|
112
141
|
if (new.target) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/azure-openai-provider.ts","../src/azure-openai-tools.ts","../src/version.ts"],"sourcesContent":["import {\n OpenAIChatLanguageModel,\n OpenAICompletionLanguageModel,\n OpenAIEmbeddingModel,\n OpenAIImageModel,\n OpenAIResponsesLanguageModel,\n OpenAISpeechModel,\n OpenAITranscriptionModel,\n} from '@ai-sdk/openai/internal';\nimport type {\n EmbeddingModelV4,\n LanguageModelV4,\n ProviderV4,\n ImageModelV4,\n SpeechModelV4,\n TranscriptionModelV4,\n} from '@ai-sdk/provider';\nimport {\n loadApiKey,\n loadSetting,\n withUserAgentSuffix,\n type FetchFunction,\n} from '@ai-sdk/provider-utils';\nimport { azureOpenaiTools } from './azure-openai-tools';\nimport { VERSION } from './version';\n\nexport interface AzureOpenAIProvider extends ProviderV4 {\n (deploymentId: string): LanguageModelV4;\n\n /**\n * Creates an Azure OpenAI responses API model for text generation.\n */\n languageModel(deploymentId: string): LanguageModelV4;\n\n /**\n * Creates an Azure OpenAI chat model for text generation.\n */\n chat(deploymentId: string): LanguageModelV4;\n\n /**\n * Creates an Azure OpenAI responses API model for text generation.\n */\n responses(deploymentId: string): LanguageModelV4;\n\n /**\n * Creates an Azure OpenAI completion model for text generation.\n */\n completion(deploymentId: string): LanguageModelV4;\n\n /**\n * Creates an Azure OpenAI model for text embeddings.\n */\n embedding(deploymentId: string): EmbeddingModelV4;\n\n /**\n * Creates an Azure OpenAI model for text embeddings.\n */\n embeddingModel(deploymentId: string): EmbeddingModelV4;\n\n /**\n * @deprecated Use `embedding` instead.\n */\n textEmbedding(deploymentId: string): EmbeddingModelV4;\n\n /**\n * @deprecated Use `embeddingModel` instead.\n */\n textEmbeddingModel(deploymentId: string): EmbeddingModelV4;\n\n /**\n * Creates an Azure OpenAI DALL-E model for image generation.\n */\n image(deploymentId: string): ImageModelV4;\n\n /**\n * Creates an Azure OpenAI DALL-E model for image generation.\n */\n imageModel(deploymentId: string): ImageModelV4;\n\n /**\n * Creates an Azure OpenAI model for audio transcription.\n */\n transcription(deploymentId: string): TranscriptionModelV4;\n\n /**\n * Creates an Azure OpenAI model for speech generation.\n */\n speech(deploymentId: string): SpeechModelV4;\n\n /**\n * AzureOpenAI-specific tools.\n */\n tools: typeof azureOpenaiTools;\n}\n\nexport interface AzureOpenAIProviderSettings {\n /**\n * Name of the Azure OpenAI resource. Either this or `baseURL` can be used.\n *\n * The resource name is used in the assembled URL: `https://{resourceName}.openai.azure.com/openai/v1{path}`.\n */\n resourceName?: string;\n\n /**\n * Use a different URL prefix for API calls, e.g. to use proxy servers. Either this or `resourceName` can be used.\n * When a baseURL is provided, the resourceName is ignored.\n *\n * With a baseURL, the resolved URL is `{baseURL}/v1{path}`.\n */\n baseURL?: string;\n\n /**\n * API key for authenticating requests.\n */\n apiKey?: string;\n\n /**\n * Custom headers to include in the requests.\n */\n headers?: Record<string, string>;\n\n /**\n * Custom fetch implementation. You can use it as a middleware to intercept requests,\n * or to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n\n /**\n * Custom api version to use. Defaults to `preview`.\n */\n apiVersion?: string;\n\n /**\n * Use deployment-based URLs for specific model types. Set to true to use legacy deployment format:\n * `{baseURL}/deployments/{deploymentId}{path}?api-version={apiVersion}` instead of\n * `{baseURL}/v1{path}?api-version={apiVersion}`.\n */\n useDeploymentBasedUrls?: boolean;\n}\n\n/**\n * Create an Azure OpenAI provider instance.\n */\nexport function createAzure(\n options: AzureOpenAIProviderSettings = {},\n): AzureOpenAIProvider {\n const getHeaders = () => {\n const baseHeaders = {\n 'api-key': loadApiKey({\n apiKey: options.apiKey,\n environmentVariableName: 'AZURE_API_KEY',\n description: 'Azure OpenAI',\n }),\n ...options.headers,\n };\n return withUserAgentSuffix(baseHeaders, `ai-sdk/azure/${VERSION}`);\n };\n\n const getResourceName = () =>\n loadSetting({\n settingValue: options.resourceName,\n settingName: 'resourceName',\n environmentVariableName: 'AZURE_RESOURCE_NAME',\n description: 'Azure OpenAI resource name',\n });\n\n const apiVersion = options.apiVersion ?? 'v1';\n\n const url = ({ path, modelId }: { path: string; modelId: string }) => {\n const baseUrlPrefix =\n options.baseURL ?? `https://${getResourceName()}.openai.azure.com/openai`;\n\n let fullUrl: URL;\n if (options.useDeploymentBasedUrls) {\n // Use deployment-based format for compatibility with certain Azure OpenAI models\n fullUrl = new URL(`${baseUrlPrefix}/deployments/${modelId}${path}`);\n } else {\n // Use v1 API format - no deployment ID in URL\n fullUrl = new URL(`${baseUrlPrefix}/v1${path}`);\n }\n\n fullUrl.searchParams.set('api-version', apiVersion);\n return fullUrl.toString();\n };\n\n const createChatModel = (deploymentName: string) =>\n new OpenAIChatLanguageModel(deploymentName, {\n provider: 'azure.chat',\n url,\n headers: getHeaders,\n fetch: options.fetch,\n });\n\n const createCompletionModel = (modelId: string) =>\n new OpenAICompletionLanguageModel(modelId, {\n provider: 'azure.completion',\n url,\n headers: getHeaders,\n fetch: options.fetch,\n });\n\n const createEmbeddingModel = (modelId: string) =>\n new OpenAIEmbeddingModel(modelId, {\n provider: 'azure.embeddings',\n headers: getHeaders,\n url,\n fetch: options.fetch,\n });\n\n const createResponsesModel = (modelId: string) =>\n new OpenAIResponsesLanguageModel(modelId, {\n provider: 'azure.responses',\n url,\n headers: getHeaders,\n fetch: options.fetch,\n // Soft-deprecated. TODO: remove in v8\n fileIdPrefixes: ['assistant-'],\n });\n\n const createImageModel = (modelId: string) =>\n new OpenAIImageModel(modelId, {\n provider: 'azure.image',\n url,\n headers: getHeaders,\n fetch: options.fetch,\n });\n\n const createTranscriptionModel = (modelId: string) =>\n new OpenAITranscriptionModel(modelId, {\n provider: 'azure.transcription',\n url,\n headers: getHeaders,\n fetch: options.fetch,\n });\n\n const createSpeechModel = (modelId: string) =>\n new OpenAISpeechModel(modelId, {\n provider: 'azure.speech',\n url,\n headers: getHeaders,\n fetch: options.fetch,\n });\n\n const provider = function (deploymentId: string) {\n if (new.target) {\n throw new Error(\n 'The Azure OpenAI model function cannot be called with the new keyword.',\n );\n }\n\n return createResponsesModel(deploymentId);\n };\n\n provider.specificationVersion = 'v4' as const;\n provider.languageModel = createResponsesModel;\n provider.chat = createChatModel;\n provider.completion = createCompletionModel;\n provider.embedding = createEmbeddingModel;\n provider.embeddingModel = createEmbeddingModel;\n provider.textEmbedding = createEmbeddingModel;\n provider.textEmbeddingModel = createEmbeddingModel;\n provider.image = createImageModel;\n provider.imageModel = createImageModel;\n provider.responses = createResponsesModel;\n provider.transcription = createTranscriptionModel;\n provider.speech = createSpeechModel;\n provider.tools = azureOpenaiTools;\n return provider;\n}\n\n/**\n * Default Azure OpenAI provider instance.\n */\nexport const azure = createAzure();\n","import {\n codeInterpreter,\n fileSearch,\n imageGeneration,\n webSearchPreview,\n} from '@ai-sdk/openai/internal';\n\nexport const azureOpenaiTools: {\n codeInterpreter: typeof codeInterpreter;\n fileSearch: typeof fileSearch;\n imageGeneration: typeof imageGeneration;\n webSearchPreview: typeof webSearchPreview;\n} = {\n codeInterpreter,\n fileSearch,\n imageGeneration,\n webSearchPreview,\n};\n","// Version string of this package injected at build time.\ndeclare const __PACKAGE_VERSION__: string | undefined;\nexport const VERSION: string =\n typeof __PACKAGE_VERSION__ !== 'undefined'\n ? __PACKAGE_VERSION__\n : '0.0.0-test';\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AASP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AY;AACrB,WAAS,gBAAgB;AACzB,WAAS,SAAS;AAClB,WAAS,QAAQ;AACjB,SAAO;AACT;AAKO,IAAM,QAAQ,YAAY;","names":["_a"]}
|
|
1
|
+
{"version":3,"sources":["../src/azure-openai-provider.ts","../src/azure-openai-tools.ts","../src/version.ts"],"sourcesContent":["import {\n OpenAIChatLanguageModel,\n OpenAICompletionLanguageModel,\n OpenAIEmbeddingModel,\n OpenAIImageModel,\n OpenAIResponsesLanguageModel,\n OpenAISpeechModel,\n OpenAITranscriptionModel,\n} from '@ai-sdk/openai/internal';\nimport {\n InvalidArgumentError,\n type EmbeddingModelV4,\n type LanguageModelV4,\n type ProviderV4,\n type ImageModelV4,\n type SpeechModelV4,\n type TranscriptionModelV4,\n} from '@ai-sdk/provider';\nimport {\n loadApiKey,\n loadSetting,\n normalizeHeaders,\n withUserAgentSuffix,\n type FetchFunction,\n} from '@ai-sdk/provider-utils';\nimport { azureOpenaiTools } from './azure-openai-tools';\nimport { VERSION } from './version';\n\nexport interface AzureOpenAIProvider extends ProviderV4 {\n (deploymentId: string): LanguageModelV4;\n\n /**\n * Creates an Azure OpenAI responses API model for text generation.\n */\n languageModel(deploymentId: string): LanguageModelV4;\n\n /**\n * Creates an Azure OpenAI chat model for text generation.\n */\n chat(deploymentId: string): LanguageModelV4;\n\n /**\n * Creates an Azure OpenAI responses API model for text generation.\n */\n responses(deploymentId: string): LanguageModelV4;\n\n /**\n * Creates an Azure OpenAI completion model for text generation.\n */\n completion(deploymentId: string): LanguageModelV4;\n\n /**\n * Creates an Azure OpenAI model for text embeddings.\n */\n embedding(deploymentId: string): EmbeddingModelV4;\n\n /**\n * Creates an Azure OpenAI model for text embeddings.\n */\n embeddingModel(deploymentId: string): EmbeddingModelV4;\n\n /**\n * @deprecated Use `embedding` instead.\n */\n textEmbedding(deploymentId: string): EmbeddingModelV4;\n\n /**\n * @deprecated Use `embeddingModel` instead.\n */\n textEmbeddingModel(deploymentId: string): EmbeddingModelV4;\n\n /**\n * Creates an Azure OpenAI DALL-E model for image generation.\n */\n image(deploymentId: string): ImageModelV4;\n\n /**\n * Creates an Azure OpenAI DALL-E model for image generation.\n */\n imageModel(deploymentId: string): ImageModelV4;\n\n /**\n * Creates an Azure OpenAI model for audio transcription.\n */\n transcription(deploymentId: string): TranscriptionModelV4;\n\n /**\n * Creates an Azure OpenAI model for speech generation.\n */\n speech(deploymentId: string): SpeechModelV4;\n\n /**\n * AzureOpenAI-specific tools.\n */\n tools: typeof azureOpenaiTools;\n}\n\nexport interface AzureOpenAIProviderSettings {\n /**\n * Name of the Azure OpenAI resource. Either this or `baseURL` can be used.\n *\n * The resource name is used in the assembled URL: `https://{resourceName}.openai.azure.com/openai/v1{path}`.\n */\n resourceName?: string;\n\n /**\n * Use a different URL prefix for API calls, e.g. to use proxy servers. Either this or `resourceName` can be used.\n * When a baseURL is provided, the resourceName is ignored.\n *\n * With a baseURL, the resolved URL is `{baseURL}/v1{path}`.\n */\n baseURL?: string;\n\n /**\n * API key for authenticating requests.\n */\n apiKey?: string;\n\n /**\n * A function that returns an access token for Microsoft Entra\n * (formerly known as Azure Active Directory), which will be invoked\n * on every request.\n */\n tokenProvider?: (() => Promise<string>) | undefined;\n\n /**\n * Custom headers to include in the requests.\n */\n headers?: Record<string, string>;\n\n /**\n * Custom fetch implementation. You can use it as a middleware to intercept requests,\n * or to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n\n /**\n * Custom api version to use. Defaults to `preview`.\n */\n apiVersion?: string;\n\n /**\n * Use deployment-based URLs for specific model types. Set to true to use legacy deployment format:\n * `{baseURL}/deployments/{deploymentId}{path}?api-version={apiVersion}` instead of\n * `{baseURL}/v1{path}?api-version={apiVersion}`.\n */\n useDeploymentBasedUrls?: boolean;\n}\n\n/**\n * Create an Azure OpenAI provider instance.\n */\nexport function createAzure(\n options: AzureOpenAIProviderSettings = {},\n): AzureOpenAIProvider {\n const tokenProvider = options.tokenProvider;\n\n if (options.apiKey && tokenProvider) {\n throw new InvalidArgumentError({\n argument: 'apiKey/tokenProvider',\n message:\n 'Both apiKey and tokenProvider were provided. Please use only one authentication method.',\n });\n }\n\n const getHeaders = () => {\n const authHeaders = tokenProvider\n ? {}\n : {\n 'api-key': loadApiKey({\n apiKey: options.apiKey,\n environmentVariableName: 'AZURE_API_KEY',\n description: 'Azure OpenAI',\n }),\n };\n\n return withUserAgentSuffix(\n {\n ...authHeaders,\n ...options.headers,\n },\n `ai-sdk/azure/${VERSION}`,\n );\n };\n\n const fetch: FetchFunction | undefined = tokenProvider\n ? async (input, init) => {\n const headers = normalizeHeaders(init?.headers);\n\n if (headers.authorization == null) {\n headers.authorization = `Bearer ${await tokenProvider()}`;\n }\n\n return (options.fetch ?? globalThis.fetch)(input, {\n ...init,\n headers,\n });\n }\n : options.fetch;\n\n const getResourceName = () =>\n loadSetting({\n settingValue: options.resourceName,\n settingName: 'resourceName',\n environmentVariableName: 'AZURE_RESOURCE_NAME',\n description: 'Azure OpenAI resource name',\n });\n\n const apiVersion = options.apiVersion ?? 'v1';\n\n const url = ({ path, modelId }: { path: string; modelId: string }) => {\n const baseUrlPrefix =\n options.baseURL ?? `https://${getResourceName()}.openai.azure.com/openai`;\n\n let fullUrl: URL;\n if (options.useDeploymentBasedUrls) {\n // Use deployment-based format for compatibility with certain Azure OpenAI models\n fullUrl = new URL(`${baseUrlPrefix}/deployments/${modelId}${path}`);\n } else {\n // Use v1 API format - no deployment ID in URL\n fullUrl = new URL(`${baseUrlPrefix}/v1${path}`);\n }\n\n fullUrl.searchParams.set('api-version', apiVersion);\n return fullUrl.toString();\n };\n\n const createChatModel = (deploymentName: string) =>\n new OpenAIChatLanguageModel(deploymentName, {\n provider: 'azure.chat',\n url,\n headers: getHeaders,\n fetch,\n });\n\n const createCompletionModel = (modelId: string) =>\n new OpenAICompletionLanguageModel(modelId, {\n provider: 'azure.completion',\n url,\n headers: getHeaders,\n fetch,\n });\n\n const createEmbeddingModel = (modelId: string) =>\n new OpenAIEmbeddingModel(modelId, {\n provider: 'azure.embeddings',\n headers: getHeaders,\n url,\n fetch,\n });\n\n const createResponsesModel = (modelId: string) =>\n new OpenAIResponsesLanguageModel(modelId, {\n provider: 'azure.responses',\n url,\n headers: getHeaders,\n fetch,\n // Soft-deprecated. TODO: remove in v8\n fileIdPrefixes: ['assistant-'],\n });\n\n const createImageModel = (modelId: string) =>\n new OpenAIImageModel(modelId, {\n provider: 'azure.image',\n url,\n headers: getHeaders,\n fetch,\n });\n\n const createTranscriptionModel = (modelId: string) =>\n new OpenAITranscriptionModel(modelId, {\n provider: 'azure.transcription',\n url,\n headers: getHeaders,\n fetch,\n });\n\n const createSpeechModel = (modelId: string) =>\n new OpenAISpeechModel(modelId, {\n provider: 'azure.speech',\n url,\n headers: getHeaders,\n fetch,\n });\n\n const provider = function (deploymentId: string) {\n if (new.target) {\n throw new Error(\n 'The Azure OpenAI model function cannot be called with the new keyword.',\n );\n }\n\n return createResponsesModel(deploymentId);\n };\n\n provider.specificationVersion = 'v4' as const;\n provider.languageModel = createResponsesModel;\n provider.chat = createChatModel;\n provider.completion = createCompletionModel;\n provider.embedding = createEmbeddingModel;\n provider.embeddingModel = createEmbeddingModel;\n provider.textEmbedding = createEmbeddingModel;\n provider.textEmbeddingModel = createEmbeddingModel;\n provider.image = createImageModel;\n provider.imageModel = createImageModel;\n provider.responses = createResponsesModel;\n provider.transcription = createTranscriptionModel;\n provider.speech = createSpeechModel;\n provider.tools = azureOpenaiTools;\n return provider;\n}\n\n/**\n * Default Azure OpenAI provider instance.\n */\nexport const azure = createAzure();\n","import {\n codeInterpreter,\n fileSearch,\n imageGeneration,\n webSearch,\n webSearchPreview,\n} from '@ai-sdk/openai/internal';\n\nexport const azureOpenaiTools: {\n codeInterpreter: typeof codeInterpreter;\n fileSearch: typeof fileSearch;\n imageGeneration: typeof imageGeneration;\n webSearch: typeof webSearch;\n webSearchPreview: typeof webSearchPreview;\n} = {\n codeInterpreter,\n fileSearch,\n imageGeneration,\n webSearch,\n webSearchPreview,\n};\n","// Version string of this package injected at build time.\ndeclare const __PACKAGE_VERSION__: string | undefined;\nexport const VERSION: string =\n typeof __PACKAGE_VERSION__ !== 'undefined'\n ? __PACKAGE_VERSION__\n : '0.0.0-test';\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,OAOK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;;;ACxBP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEA,IAAM,mBAMT;AAAA,EACF;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;;;AClBO,IAAM,UACX,OACI,oBACA;;;AFmJC,SAAS,YACd,UAAuC,CAAC,GACnB;AA1JvB;AA2JE,QAAM,gBAAgB,QAAQ;AAE9B,MAAI,QAAQ,UAAU,eAAe;AACnC,UAAM,IAAI,qBAAqB;AAAA,MAC7B,UAAU;AAAA,MACV,SACE;AAAA,IACJ,CAAC;AAAA,EACH;AAEA,QAAM,aAAa,MAAM;AACvB,UAAM,cAAc,gBAChB,CAAC,IACD;AAAA,MACE,WAAW,WAAW;AAAA,QACpB,QAAQ,QAAQ;AAAA,QAChB,yBAAyB;AAAA,QACzB,aAAa;AAAA,MACf,CAAC;AAAA,IACH;AAEJ,WAAO;AAAA,MACL;AAAA,QACE,GAAG;AAAA,QACH,GAAG,QAAQ;AAAA,MACb;AAAA,MACA,gBAAgB,OAAO;AAAA,IACzB;AAAA,EACF;AAEA,QAAM,QAAmC,gBACrC,OAAO,OAAO,SAAS;AA1L7B,QAAAA;AA2LQ,UAAM,UAAU,iBAAiB,6BAAM,OAAO;AAE9C,QAAI,QAAQ,iBAAiB,MAAM;AACjC,cAAQ,gBAAgB,UAAU,MAAM,cAAc,CAAC;AAAA,IACzD;AAEA,aAAQA,MAAA,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AAAA,EAC/B,CAAC;AAEH,QAAM,mBAAmB,CAAC,YACxB,IAAI,iBAAiB,SAAS;AAAA,IAC5B,UAAU;AAAA,IACV;AAAA,IACA,SAAS;AAAA,IACT;AAAA,EACF,CAAC;AAEH,QAAM,2BAA2B,CAAC,YAChC,IAAI,yBAAyB,SAAS;AAAA,IACpC,UAAU;AAAA,IACV;AAAA,IACA,SAAS;AAAA,IACT;AAAA,EACF,CAAC;AAEH,QAAM,oBAAoB,CAAC,YACzB,IAAI,kBAAkB,SAAS;AAAA,IAC7B,UAAU;AAAA,IACV;AAAA,IACA,SAAS;AAAA,IACT;AAAA,EACF,CAAC;AAEH,QAAM,WAAW,SAAU,cAAsB;AAC/C,QAAI,YAAY;AACd,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,WAAO,qBAAqB,YAAY;AAAA,EAC1C;AAEA,WAAS,uBAAuB;AAChC,WAAS,gBAAgB;AACzB,WAAS,OAAO;AAChB,WAAS,aAAa;AACtB,WAAS,YAAY;AACrB,WAAS,iBAAiB;AAC1B,WAAS,gBAAgB;AACzB,WAAS,qBAAqB;AAC9B,WAAS,QAAQ;AACjB,WAAS,aAAa;AACtB,WAAS,YAAY;AACrB,WAAS,gBAAgB;AACzB,WAAS,SAAS;AAClB,WAAS,QAAQ;AACjB,SAAO;AACT;AAKO,IAAM,QAAQ,YAAY;","names":["_a"]}
|
package/docs/04-azure.mdx
CHANGED
|
@@ -46,6 +46,26 @@ const azure = createAzure({
|
|
|
46
46
|
});
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
+
For Microsoft Entra ID authentication, you can provide a token provider.
|
|
50
|
+
Install `@azure/identity` separately if you want to use its credential helpers:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { createAzure } from '@ai-sdk/azure';
|
|
54
|
+
import {
|
|
55
|
+
DefaultAzureCredential,
|
|
56
|
+
getBearerTokenProvider,
|
|
57
|
+
} from '@azure/identity';
|
|
58
|
+
|
|
59
|
+
const credential = new DefaultAzureCredential();
|
|
60
|
+
const scope = 'https://cognitiveservices.azure.com/.default';
|
|
61
|
+
const tokenProvider = getBearerTokenProvider(credential, scope);
|
|
62
|
+
|
|
63
|
+
const azure = createAzure({
|
|
64
|
+
resourceName: 'your-resource-name',
|
|
65
|
+
tokenProvider,
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
49
69
|
You can use the following optional settings to customize the OpenAI provider instance:
|
|
50
70
|
|
|
51
71
|
- **resourceName** _string_
|
|
@@ -61,6 +81,11 @@ You can use the following optional settings to customize the OpenAI provider ins
|
|
|
61
81
|
API key that is being sent using the `api-key` header.
|
|
62
82
|
It defaults to the `AZURE_API_KEY` environment variable.
|
|
63
83
|
|
|
84
|
+
- **tokenProvider** _() => Promise<string>_
|
|
85
|
+
|
|
86
|
+
Microsoft Entra ID token provider that returns an access token to be sent using the `Authorization` header.
|
|
87
|
+
When provided, this is used instead of `apiKey`.
|
|
88
|
+
|
|
64
89
|
- **apiVersion** _string_
|
|
65
90
|
|
|
66
91
|
Sets a custom [api version](https://learn.microsoft.com/en-us/azure/ai-services/openai/api-version-deprecation).
|
|
@@ -362,45 +387,56 @@ The following Azure-specific metadata may be returned:
|
|
|
362
387
|
|
|
363
388
|
#### Web Search Tool
|
|
364
389
|
|
|
365
|
-
The Azure OpenAI responses API supports web search
|
|
390
|
+
The Azure OpenAI responses API supports web search through the `azure.tools.webSearch` tool.
|
|
366
391
|
|
|
367
392
|
```ts
|
|
368
393
|
const result = await generateText({
|
|
369
|
-
model: azure('gpt-
|
|
394
|
+
model: azure('gpt-5'),
|
|
370
395
|
prompt: 'What happened in San Francisco last week?',
|
|
371
396
|
tools: {
|
|
372
|
-
|
|
397
|
+
web_search: azure.tools.webSearch({
|
|
373
398
|
// optional configuration:
|
|
374
|
-
searchContextSize: '
|
|
399
|
+
searchContextSize: 'high',
|
|
375
400
|
userLocation: {
|
|
376
401
|
type: 'approximate',
|
|
377
402
|
city: 'San Francisco',
|
|
378
403
|
region: 'California',
|
|
379
404
|
},
|
|
405
|
+
filters: {
|
|
406
|
+
allowedDomains: ['sfchronicle.com', 'sfgate.com'],
|
|
407
|
+
},
|
|
380
408
|
}),
|
|
381
409
|
},
|
|
382
410
|
// Force web search tool (optional):
|
|
383
|
-
toolChoice: { type: 'tool', toolName: '
|
|
411
|
+
toolChoice: { type: 'tool', toolName: 'web_search' },
|
|
384
412
|
});
|
|
385
413
|
|
|
386
|
-
console.log(result.text);
|
|
387
|
-
|
|
388
414
|
// URL sources directly from `results`
|
|
389
415
|
const sources = result.sources;
|
|
390
|
-
|
|
391
|
-
|
|
416
|
+
|
|
417
|
+
// Or access sources from tool results
|
|
418
|
+
for (const toolResult of result.toolResults) {
|
|
419
|
+
if (toolResult.toolName === 'web_search') {
|
|
420
|
+
console.log('Query:', toolResult.output.action.query);
|
|
421
|
+
console.log('Sources:', toolResult.output.sources);
|
|
422
|
+
// `sources` is an array of object: { type: 'url', url: string }
|
|
423
|
+
}
|
|
392
424
|
}
|
|
393
425
|
```
|
|
394
426
|
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
427
|
+
The web search tool supports the following configuration options:
|
|
428
|
+
|
|
429
|
+
- **externalWebAccess** _boolean_ - Whether to use external web access for fetching live content. Defaults to `true`.
|
|
430
|
+
- **searchContextSize** _'low' | 'medium' | 'high'_ - Controls the amount of context used for the search. Higher values provide more comprehensive results but may have higher latency and cost.
|
|
431
|
+
- **userLocation** - Optional location information to provide geographically relevant results. Includes `type` (always `'approximate'`), `country`, `city`, `region`, and `timezone`.
|
|
432
|
+
- **filters** - Optional filter configuration to restrict search results.
|
|
433
|
+
- **allowedDomains** _string[]_ - Array of allowed domains for the search. Subdomains of the provided domains are automatically included.
|
|
434
|
+
|
|
435
|
+
For detailed information on configuration options see the [Azure OpenAI Web Search Tool documentation](https://learn.microsoft.com/en-us/azure/foundry/openai/how-to/web-search).
|
|
400
436
|
|
|
401
437
|
<Note>
|
|
402
|
-
|
|
403
|
-
|
|
438
|
+
On Microsoft Foundry environment , Live internet access is not supported. The
|
|
439
|
+
parameter `externalWebAccess` if passed will be ignored.
|
|
404
440
|
</Note>
|
|
405
441
|
|
|
406
442
|
#### File Search Tool
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/azure",
|
|
3
|
-
"version": "4.0.0-canary.
|
|
3
|
+
"version": "4.0.0-canary.67",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@ai-sdk/openai": "4.0.0-canary.
|
|
32
|
+
"@ai-sdk/openai": "4.0.0-canary.66",
|
|
33
33
|
"@ai-sdk/provider": "4.0.0-canary.17",
|
|
34
34
|
"@ai-sdk/provider-utils": "5.0.0-canary.44"
|
|
35
35
|
},
|
|
@@ -7,17 +7,19 @@ import {
|
|
|
7
7
|
OpenAISpeechModel,
|
|
8
8
|
OpenAITranscriptionModel,
|
|
9
9
|
} from '@ai-sdk/openai/internal';
|
|
10
|
-
import
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
import {
|
|
11
|
+
InvalidArgumentError,
|
|
12
|
+
type EmbeddingModelV4,
|
|
13
|
+
type LanguageModelV4,
|
|
14
|
+
type ProviderV4,
|
|
15
|
+
type ImageModelV4,
|
|
16
|
+
type SpeechModelV4,
|
|
17
|
+
type TranscriptionModelV4,
|
|
17
18
|
} from '@ai-sdk/provider';
|
|
18
19
|
import {
|
|
19
20
|
loadApiKey,
|
|
20
21
|
loadSetting,
|
|
22
|
+
normalizeHeaders,
|
|
21
23
|
withUserAgentSuffix,
|
|
22
24
|
type FetchFunction,
|
|
23
25
|
} from '@ai-sdk/provider-utils';
|
|
@@ -114,6 +116,13 @@ export interface AzureOpenAIProviderSettings {
|
|
|
114
116
|
*/
|
|
115
117
|
apiKey?: string;
|
|
116
118
|
|
|
119
|
+
/**
|
|
120
|
+
* A function that returns an access token for Microsoft Entra
|
|
121
|
+
* (formerly known as Azure Active Directory), which will be invoked
|
|
122
|
+
* on every request.
|
|
123
|
+
*/
|
|
124
|
+
tokenProvider?: (() => Promise<string>) | undefined;
|
|
125
|
+
|
|
117
126
|
/**
|
|
118
127
|
* Custom headers to include in the requests.
|
|
119
128
|
*/
|
|
@@ -144,18 +153,51 @@ export interface AzureOpenAIProviderSettings {
|
|
|
144
153
|
export function createAzure(
|
|
145
154
|
options: AzureOpenAIProviderSettings = {},
|
|
146
155
|
): AzureOpenAIProvider {
|
|
156
|
+
const tokenProvider = options.tokenProvider;
|
|
157
|
+
|
|
158
|
+
if (options.apiKey && tokenProvider) {
|
|
159
|
+
throw new InvalidArgumentError({
|
|
160
|
+
argument: 'apiKey/tokenProvider',
|
|
161
|
+
message:
|
|
162
|
+
'Both apiKey and tokenProvider were provided. Please use only one authentication method.',
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
147
166
|
const getHeaders = () => {
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
167
|
+
const authHeaders = tokenProvider
|
|
168
|
+
? {}
|
|
169
|
+
: {
|
|
170
|
+
'api-key': loadApiKey({
|
|
171
|
+
apiKey: options.apiKey,
|
|
172
|
+
environmentVariableName: 'AZURE_API_KEY',
|
|
173
|
+
description: 'Azure OpenAI',
|
|
174
|
+
}),
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
return withUserAgentSuffix(
|
|
178
|
+
{
|
|
179
|
+
...authHeaders,
|
|
180
|
+
...options.headers,
|
|
181
|
+
},
|
|
182
|
+
`ai-sdk/azure/${VERSION}`,
|
|
183
|
+
);
|
|
157
184
|
};
|
|
158
185
|
|
|
186
|
+
const fetch: FetchFunction | undefined = tokenProvider
|
|
187
|
+
? async (input, init) => {
|
|
188
|
+
const headers = normalizeHeaders(init?.headers);
|
|
189
|
+
|
|
190
|
+
if (headers.authorization == null) {
|
|
191
|
+
headers.authorization = `Bearer ${await tokenProvider()}`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return (options.fetch ?? globalThis.fetch)(input, {
|
|
195
|
+
...init,
|
|
196
|
+
headers,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
: options.fetch;
|
|
200
|
+
|
|
159
201
|
const getResourceName = () =>
|
|
160
202
|
loadSetting({
|
|
161
203
|
settingValue: options.resourceName,
|
|
@@ -188,7 +230,7 @@ export function createAzure(
|
|
|
188
230
|
provider: 'azure.chat',
|
|
189
231
|
url,
|
|
190
232
|
headers: getHeaders,
|
|
191
|
-
fetch
|
|
233
|
+
fetch,
|
|
192
234
|
});
|
|
193
235
|
|
|
194
236
|
const createCompletionModel = (modelId: string) =>
|
|
@@ -196,7 +238,7 @@ export function createAzure(
|
|
|
196
238
|
provider: 'azure.completion',
|
|
197
239
|
url,
|
|
198
240
|
headers: getHeaders,
|
|
199
|
-
fetch
|
|
241
|
+
fetch,
|
|
200
242
|
});
|
|
201
243
|
|
|
202
244
|
const createEmbeddingModel = (modelId: string) =>
|
|
@@ -204,7 +246,7 @@ export function createAzure(
|
|
|
204
246
|
provider: 'azure.embeddings',
|
|
205
247
|
headers: getHeaders,
|
|
206
248
|
url,
|
|
207
|
-
fetch
|
|
249
|
+
fetch,
|
|
208
250
|
});
|
|
209
251
|
|
|
210
252
|
const createResponsesModel = (modelId: string) =>
|
|
@@ -212,7 +254,7 @@ export function createAzure(
|
|
|
212
254
|
provider: 'azure.responses',
|
|
213
255
|
url,
|
|
214
256
|
headers: getHeaders,
|
|
215
|
-
fetch
|
|
257
|
+
fetch,
|
|
216
258
|
// Soft-deprecated. TODO: remove in v8
|
|
217
259
|
fileIdPrefixes: ['assistant-'],
|
|
218
260
|
});
|
|
@@ -222,7 +264,7 @@ export function createAzure(
|
|
|
222
264
|
provider: 'azure.image',
|
|
223
265
|
url,
|
|
224
266
|
headers: getHeaders,
|
|
225
|
-
fetch
|
|
267
|
+
fetch,
|
|
226
268
|
});
|
|
227
269
|
|
|
228
270
|
const createTranscriptionModel = (modelId: string) =>
|
|
@@ -230,7 +272,7 @@ export function createAzure(
|
|
|
230
272
|
provider: 'azure.transcription',
|
|
231
273
|
url,
|
|
232
274
|
headers: getHeaders,
|
|
233
|
-
fetch
|
|
275
|
+
fetch,
|
|
234
276
|
});
|
|
235
277
|
|
|
236
278
|
const createSpeechModel = (modelId: string) =>
|
|
@@ -238,7 +280,7 @@ export function createAzure(
|
|
|
238
280
|
provider: 'azure.speech',
|
|
239
281
|
url,
|
|
240
282
|
headers: getHeaders,
|
|
241
|
-
fetch
|
|
283
|
+
fetch,
|
|
242
284
|
});
|
|
243
285
|
|
|
244
286
|
const provider = function (deploymentId: string) {
|
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
codeInterpreter,
|
|
3
3
|
fileSearch,
|
|
4
4
|
imageGeneration,
|
|
5
|
+
webSearch,
|
|
5
6
|
webSearchPreview,
|
|
6
7
|
} from '@ai-sdk/openai/internal';
|
|
7
8
|
|
|
@@ -9,10 +10,12 @@ export const azureOpenaiTools: {
|
|
|
9
10
|
codeInterpreter: typeof codeInterpreter;
|
|
10
11
|
fileSearch: typeof fileSearch;
|
|
11
12
|
imageGeneration: typeof imageGeneration;
|
|
13
|
+
webSearch: typeof webSearch;
|
|
12
14
|
webSearchPreview: typeof webSearchPreview;
|
|
13
15
|
} = {
|
|
14
16
|
codeInterpreter,
|
|
15
17
|
fileSearch,
|
|
16
18
|
imageGeneration,
|
|
19
|
+
webSearch,
|
|
17
20
|
webSearchPreview,
|
|
18
21
|
};
|