@ai-sdk/gateway 3.0.26 → 3.0.28

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.
@@ -0,0 +1,295 @@
1
+ import {
2
+ createProviderToolFactoryWithOutputSchema,
3
+ lazySchema,
4
+ zodSchema,
5
+ } from '@ai-sdk/provider-utils';
6
+ import { z } from 'zod';
7
+
8
+ export interface ParallelSearchSourcePolicy {
9
+ /**
10
+ * List of domains to include in search results.
11
+ * Example: ['wikipedia.org', 'nature.com']
12
+ */
13
+ includeDomains?: string[];
14
+
15
+ /**
16
+ * List of domains to exclude from search results.
17
+ * Example: ['reddit.com', 'twitter.com']
18
+ */
19
+ excludeDomains?: string[];
20
+
21
+ /**
22
+ * Only include results published after this date (ISO 8601 format).
23
+ * Example: '2024-01-01'
24
+ */
25
+ afterDate?: string;
26
+ }
27
+
28
+ export interface ParallelSearchExcerpts {
29
+ /**
30
+ * Maximum characters per result.
31
+ */
32
+ maxCharsPerResult?: number;
33
+
34
+ /**
35
+ * Maximum total characters across all results.
36
+ */
37
+ maxCharsTotal?: number;
38
+ }
39
+
40
+ export interface ParallelSearchFetchPolicy {
41
+ /**
42
+ * Maximum age in seconds for cached content.
43
+ * Set to 0 to always fetch fresh content.
44
+ */
45
+ maxAgeSeconds?: number;
46
+ }
47
+
48
+ export interface ParallelSearchConfig {
49
+ /**
50
+ * Mode preset for different use cases:
51
+ * - "one-shot": Comprehensive results with longer excerpts for single-response answers (default)
52
+ * - "agentic": Concise, token-efficient results for multi-step agentic workflows
53
+ */
54
+ mode?: 'one-shot' | 'agentic';
55
+
56
+ /**
57
+ * Default maximum number of results to return (1-20).
58
+ * Defaults to 10 if not specified.
59
+ */
60
+ maxResults?: number;
61
+
62
+ /**
63
+ * Default source policy for controlling which domains to include/exclude.
64
+ */
65
+ sourcePolicy?: ParallelSearchSourcePolicy;
66
+
67
+ /**
68
+ * Default excerpt configuration for controlling result length.
69
+ */
70
+ excerpts?: ParallelSearchExcerpts;
71
+
72
+ /**
73
+ * Default fetch policy for controlling content freshness.
74
+ */
75
+ fetchPolicy?: ParallelSearchFetchPolicy;
76
+ }
77
+
78
+ export interface ParallelSearchResult {
79
+ /** URL of the search result */
80
+ url: string;
81
+ /** Title of the search result */
82
+ title: string;
83
+ /** Extracted text excerpt/content from the page */
84
+ excerpt: string;
85
+ /** Publication date of the content (may be null) */
86
+ publishDate?: string | null;
87
+ /** Relevance score for the result */
88
+ relevanceScore?: number;
89
+ }
90
+
91
+ export interface ParallelSearchResponse {
92
+ /** Unique identifier for this search request */
93
+ searchId: string;
94
+ /** Array of search results */
95
+ results: ParallelSearchResult[];
96
+ }
97
+
98
+ export interface ParallelSearchError {
99
+ /** Error type */
100
+ error:
101
+ | 'api_error'
102
+ | 'rate_limit'
103
+ | 'timeout'
104
+ | 'invalid_input'
105
+ | 'configuration_error'
106
+ | 'unknown';
107
+ /** HTTP status code if applicable */
108
+ statusCode?: number;
109
+ /** Human-readable error message */
110
+ message: string;
111
+ }
112
+
113
+ export interface ParallelSearchInput {
114
+ /**
115
+ * Natural-language description of the web research goal.
116
+ * Include source or freshness guidance and broader context from the task.
117
+ * Maximum 5000 characters.
118
+ */
119
+ objective: string;
120
+
121
+ /**
122
+ * Optional search queries to supplement the objective.
123
+ * Maximum 200 characters per query.
124
+ */
125
+ search_queries?: string[];
126
+
127
+ /**
128
+ * Mode preset for different use cases:
129
+ * - "one-shot": Comprehensive results with longer excerpts
130
+ * - "agentic": Concise, token-efficient results for multi-step workflows
131
+ */
132
+ mode?: 'one-shot' | 'agentic';
133
+
134
+ /**
135
+ * Maximum number of results to return (1-20).
136
+ * Defaults to 10 if not specified.
137
+ */
138
+ max_results?: number;
139
+
140
+ /**
141
+ * Source policy for controlling which domains to include/exclude.
142
+ */
143
+ source_policy?: {
144
+ include_domains?: string[];
145
+ exclude_domains?: string[];
146
+ after_date?: string;
147
+ };
148
+
149
+ /**
150
+ * Excerpt configuration for controlling result length.
151
+ */
152
+ excerpts?: {
153
+ max_chars_per_result?: number;
154
+ max_chars_total?: number;
155
+ };
156
+
157
+ /**
158
+ * Fetch policy for controlling content freshness.
159
+ */
160
+ fetch_policy?: {
161
+ max_age_seconds?: number;
162
+ };
163
+ }
164
+
165
+ export type ParallelSearchOutput = ParallelSearchResponse | ParallelSearchError;
166
+
167
+ const parallelSearchInputSchema = lazySchema(() =>
168
+ zodSchema(
169
+ z.object({
170
+ objective: z
171
+ .string()
172
+ .describe(
173
+ 'Natural-language description of the web research goal, including source or freshness guidance and broader context from the task. Maximum 5000 characters.',
174
+ ),
175
+
176
+ search_queries: z
177
+ .array(z.string())
178
+ .optional()
179
+ .describe(
180
+ 'Optional search queries to supplement the objective. Maximum 200 characters per query.',
181
+ ),
182
+
183
+ mode: z
184
+ .enum(['one-shot', 'agentic'])
185
+ .optional()
186
+ .describe(
187
+ 'Mode preset: "one-shot" for comprehensive results with longer excerpts (default), "agentic" for concise, token-efficient results for multi-step workflows.',
188
+ ),
189
+
190
+ max_results: z
191
+ .number()
192
+ .optional()
193
+ .describe(
194
+ 'Maximum number of results to return (1-20). Defaults to 10 if not specified.',
195
+ ),
196
+
197
+ source_policy: z
198
+ .object({
199
+ include_domains: z
200
+ .array(z.string())
201
+ .optional()
202
+ .describe('List of domains to include in search results.'),
203
+ exclude_domains: z
204
+ .array(z.string())
205
+ .optional()
206
+ .describe('List of domains to exclude from search results.'),
207
+ after_date: z
208
+ .string()
209
+ .optional()
210
+ .describe(
211
+ 'Only include results published after this date (ISO 8601 format).',
212
+ ),
213
+ })
214
+ .optional()
215
+ .describe(
216
+ 'Source policy for controlling which domains to include/exclude and freshness.',
217
+ ),
218
+
219
+ excerpts: z
220
+ .object({
221
+ max_chars_per_result: z
222
+ .number()
223
+ .optional()
224
+ .describe('Maximum characters per result.'),
225
+ max_chars_total: z
226
+ .number()
227
+ .optional()
228
+ .describe('Maximum total characters across all results.'),
229
+ })
230
+ .optional()
231
+ .describe('Excerpt configuration for controlling result length.'),
232
+
233
+ fetch_policy: z
234
+ .object({
235
+ max_age_seconds: z
236
+ .number()
237
+ .optional()
238
+ .describe(
239
+ 'Maximum age in seconds for cached content. Set to 0 to always fetch fresh content.',
240
+ ),
241
+ })
242
+ .optional()
243
+ .describe('Fetch policy for controlling content freshness.'),
244
+ }),
245
+ ),
246
+ );
247
+
248
+ const parallelSearchOutputSchema = lazySchema(() =>
249
+ zodSchema(
250
+ z.union([
251
+ // Success response
252
+ z.object({
253
+ searchId: z.string(),
254
+ results: z.array(
255
+ z.object({
256
+ url: z.string(),
257
+ title: z.string(),
258
+ excerpt: z.string(),
259
+ publishDate: z.string().nullable().optional(),
260
+ relevanceScore: z.number().optional(),
261
+ }),
262
+ ),
263
+ }),
264
+ // Error response
265
+ z.object({
266
+ error: z.enum([
267
+ 'api_error',
268
+ 'rate_limit',
269
+ 'timeout',
270
+ 'invalid_input',
271
+ 'configuration_error',
272
+ 'unknown',
273
+ ]),
274
+ statusCode: z.number().optional(),
275
+ message: z.string(),
276
+ }),
277
+ ]),
278
+ ),
279
+ );
280
+
281
+ export const parallelSearchToolFactory =
282
+ createProviderToolFactoryWithOutputSchema<
283
+ ParallelSearchInput,
284
+ ParallelSearchOutput,
285
+ ParallelSearchConfig
286
+ >({
287
+ id: 'gateway.parallel_search',
288
+ inputSchema: parallelSearchInputSchema,
289
+ outputSchema: parallelSearchOutputSchema,
290
+ });
291
+
292
+ export const parallelSearch = (
293
+ config: ParallelSearchConfig = {},
294
+ ): ReturnType<typeof parallelSearchToolFactory> =>
295
+ parallelSearchToolFactory(config);