@mastra/tavily 1.1.1 → 1.1.2-alpha.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/dist/index.js CHANGED
@@ -1,280 +1,277 @@
1
- import { tavily } from '@tavily/core';
2
- import { createTool } from '@mastra/core/tools';
3
- import { z } from 'zod';
4
-
5
- // src/client.ts
1
+ import { tavily } from "@tavily/core";
2
+ import { createTool } from "@mastra/core/tools";
3
+ import { z } from "zod";
4
+ //#region src/client.ts
6
5
  function getTavilyClient(config) {
7
- const apiKey = config?.apiKey ?? process.env.TAVILY_API_KEY;
8
- if (!apiKey) {
9
- throw new Error("Tavily API key is required. Pass { apiKey } or set TAVILY_API_KEY env var.");
10
- }
11
- return tavily({ ...config, apiKey, clientName: config?.clientName ?? "mastra" });
6
+ const apiKey = config?.apiKey ?? process.env.TAVILY_API_KEY;
7
+ if (!apiKey) throw new Error("Tavily API key is required. Pass { apiKey } or set TAVILY_API_KEY env var.");
8
+ return tavily({
9
+ ...config,
10
+ apiKey,
11
+ clientName: config?.clientName ?? "mastra"
12
+ });
12
13
  }
13
- var inputSchema = z.object({
14
- query: z.string().describe("The search query"),
15
- searchDepth: z.enum(["basic", "advanced", "fast", "ultra-fast"]).optional().describe("Search depth \u2014 'basic' for standard, 'advanced' for thorough, 'fast'/'ultra-fast' for low latency"),
16
- maxResults: z.number().min(1).max(20).optional().describe("Maximum number of results to return (1-20)"),
17
- includeAnswer: z.union([z.boolean(), z.enum(["basic", "advanced"])]).optional().describe('Include an AI-generated answer summary. Pass true, "basic", or "advanced"'),
18
- includeImages: z.boolean().optional().describe("Include query-related images in the response"),
19
- includeImageDescriptions: z.boolean().optional().describe("Include descriptions for returned images"),
20
- includeRawContent: z.union([z.literal(false), z.enum(["markdown", "text"])]).optional().describe('Include cleaned HTML content of each result. Pass false to disable, or "markdown"/"text" for format'),
21
- includeDomains: z.array(z.string()).optional().describe("Restrict results to these domains"),
22
- excludeDomains: z.array(z.string()).optional().describe("Exclude results from these domains"),
23
- timeRange: z.enum(["day", "week", "month", "year"]).optional().describe("Time range to filter results by recency")
14
+ //#endregion
15
+ //#region src/search.ts
16
+ const inputSchema$3 = z.object({
17
+ query: z.string().describe("The search query"),
18
+ searchDepth: z.enum([
19
+ "basic",
20
+ "advanced",
21
+ "fast",
22
+ "ultra-fast"
23
+ ]).optional().describe("Search depth — 'basic' for standard, 'advanced' for thorough, 'fast'/'ultra-fast' for low latency"),
24
+ maxResults: z.number().min(1).max(20).optional().describe("Maximum number of results to return (1-20)"),
25
+ includeAnswer: z.union([z.boolean(), z.enum(["basic", "advanced"])]).optional().describe("Include an AI-generated answer summary. Pass true, \"basic\", or \"advanced\""),
26
+ includeImages: z.boolean().optional().describe("Include query-related images in the response"),
27
+ includeImageDescriptions: z.boolean().optional().describe("Include descriptions for returned images"),
28
+ includeRawContent: z.union([z.literal(false), z.enum(["markdown", "text"])]).optional().describe("Include cleaned HTML content of each result. Pass false to disable, or \"markdown\"/\"text\" for format"),
29
+ includeDomains: z.array(z.string()).optional().describe("Restrict results to these domains"),
30
+ excludeDomains: z.array(z.string()).optional().describe("Exclude results from these domains"),
31
+ timeRange: z.enum([
32
+ "day",
33
+ "week",
34
+ "month",
35
+ "year"
36
+ ]).optional().describe("Time range to filter results by recency")
24
37
  });
25
- var outputSchema = z.object({
26
- query: z.string(),
27
- answer: z.string().optional(),
28
- images: z.array(
29
- z.object({
30
- url: z.string(),
31
- description: z.string().optional()
32
- })
33
- ).optional(),
34
- results: z.array(
35
- z.object({
36
- title: z.string(),
37
- url: z.string(),
38
- content: z.string(),
39
- score: z.number(),
40
- rawContent: z.string().optional()
41
- })
42
- ),
43
- responseTime: z.number()
38
+ const outputSchema$3 = z.object({
39
+ query: z.string(),
40
+ answer: z.string().optional(),
41
+ images: z.array(z.object({
42
+ url: z.string(),
43
+ description: z.string().optional()
44
+ })).optional(),
45
+ results: z.array(z.object({
46
+ title: z.string(),
47
+ url: z.string(),
48
+ content: z.string(),
49
+ score: z.number(),
50
+ rawContent: z.string().optional()
51
+ })),
52
+ responseTime: z.number()
44
53
  });
45
54
  function createTavilySearchTool(config) {
46
- let client = null;
47
- function getClient() {
48
- if (!client) {
49
- client = getTavilyClient(config);
50
- }
51
- return client;
52
- }
53
- return createTool({
54
- id: "tavily-search",
55
- description: "Search the web using Tavily. Returns relevant results with content snippets, optional AI-generated answers, and images. Supports filtering by domain, time range, and search depth.",
56
- inputSchema,
57
- outputSchema,
58
- execute: async (input) => {
59
- const tavilyClient = getClient();
60
- const response = await tavilyClient.search(input.query, {
61
- searchDepth: input.searchDepth,
62
- maxResults: input.maxResults,
63
- includeAnswer: input.includeAnswer,
64
- includeImages: input.includeImages,
65
- includeImageDescriptions: input.includeImageDescriptions,
66
- includeRawContent: input.includeRawContent,
67
- includeDomains: input.includeDomains,
68
- excludeDomains: input.excludeDomains,
69
- timeRange: input.timeRange
70
- });
71
- return {
72
- query: response.query,
73
- answer: response.answer || void 0,
74
- images: response.images?.map((img) => ({
75
- url: typeof img === "string" ? img : img.url,
76
- description: typeof img === "string" ? void 0 : img.description
77
- })),
78
- results: (response.results ?? []).map((r) => ({
79
- title: r.title,
80
- url: r.url,
81
- content: r.content,
82
- score: r.score ?? 0,
83
- rawContent: r.rawContent || void 0
84
- })),
85
- responseTime: response.responseTime
86
- };
87
- }
88
- });
55
+ let client = null;
56
+ function getClient() {
57
+ if (!client) client = getTavilyClient(config);
58
+ return client;
59
+ }
60
+ return createTool({
61
+ id: "tavily-search",
62
+ description: "Search the web using Tavily. Returns relevant results with content snippets, optional AI-generated answers, and images. Supports filtering by domain, time range, and search depth.",
63
+ inputSchema: inputSchema$3,
64
+ outputSchema: outputSchema$3,
65
+ execute: async (input) => {
66
+ const response = await getClient().search(input.query, {
67
+ searchDepth: input.searchDepth,
68
+ maxResults: input.maxResults,
69
+ includeAnswer: input.includeAnswer,
70
+ includeImages: input.includeImages,
71
+ includeImageDescriptions: input.includeImageDescriptions,
72
+ includeRawContent: input.includeRawContent,
73
+ includeDomains: input.includeDomains,
74
+ excludeDomains: input.excludeDomains,
75
+ timeRange: input.timeRange
76
+ });
77
+ return {
78
+ query: response.query,
79
+ answer: response.answer || void 0,
80
+ images: response.images?.map((img) => ({
81
+ url: typeof img === "string" ? img : img.url,
82
+ description: typeof img === "string" ? void 0 : img.description
83
+ })),
84
+ results: (response.results ?? []).map((r) => ({
85
+ title: r.title,
86
+ url: r.url,
87
+ content: r.content,
88
+ score: r.score ?? 0,
89
+ rawContent: r.rawContent || void 0
90
+ })),
91
+ responseTime: response.responseTime
92
+ };
93
+ }
94
+ });
89
95
  }
90
- var inputSchema2 = z.object({
91
- urls: z.array(z.string()).min(1).max(20).describe("URLs to extract content from (1-20)"),
92
- extractDepth: z.enum(["basic", "advanced"]).optional().describe("Extraction depth \u2014 'advanced' retrieves more data including tables and embedded content"),
93
- query: z.string().optional().describe("User intent for reranking extracted content chunks. When provided, chunks are reranked based on relevance to this query."),
94
- includeImages: z.boolean().optional().describe("Include images extracted from the pages"),
95
- format: z.enum(["markdown", "text"]).optional().describe("Output format for extracted content \u2014 'markdown' (default) or 'text'")
96
+ //#endregion
97
+ //#region src/extract.ts
98
+ const inputSchema$2 = z.object({
99
+ urls: z.array(z.string()).min(1).max(20).describe("URLs to extract content from (1-20)"),
100
+ extractDepth: z.enum(["basic", "advanced"]).optional().describe("Extraction depth — 'advanced' retrieves more data including tables and embedded content"),
101
+ query: z.string().optional().describe("User intent for reranking extracted content chunks. When provided, chunks are reranked based on relevance to this query."),
102
+ includeImages: z.boolean().optional().describe("Include images extracted from the pages"),
103
+ format: z.enum(["markdown", "text"]).optional().describe("Output format for extracted content — 'markdown' (default) or 'text'")
96
104
  });
97
- var outputSchema2 = z.object({
98
- results: z.array(
99
- z.object({
100
- url: z.string(),
101
- rawContent: z.string(),
102
- images: z.array(z.string()).optional()
103
- })
104
- ),
105
- failedResults: z.array(
106
- z.object({
107
- url: z.string(),
108
- error: z.string()
109
- })
110
- ),
111
- responseTime: z.number()
105
+ const outputSchema$2 = z.object({
106
+ results: z.array(z.object({
107
+ url: z.string(),
108
+ rawContent: z.string(),
109
+ images: z.array(z.string()).optional()
110
+ })),
111
+ failedResults: z.array(z.object({
112
+ url: z.string(),
113
+ error: z.string()
114
+ })),
115
+ responseTime: z.number()
112
116
  });
113
117
  function createTavilyExtractTool(config) {
114
- let client = null;
115
- function getClient() {
116
- if (!client) {
117
- client = getTavilyClient(config);
118
- }
119
- return client;
120
- }
121
- return createTool({
122
- id: "tavily-extract",
123
- description: "Extract content from one or more URLs using Tavily. Returns raw page content in markdown or text format. Supports up to 20 URLs per request with basic or advanced extraction depth.",
124
- inputSchema: inputSchema2,
125
- outputSchema: outputSchema2,
126
- execute: async (input) => {
127
- const tavilyClient = getClient();
128
- const response = await tavilyClient.extract(input.urls, {
129
- extractDepth: input.extractDepth,
130
- query: input.query,
131
- includeImages: input.includeImages,
132
- format: input.format
133
- });
134
- return {
135
- results: (response.results || []).map((r) => ({
136
- url: r.url,
137
- rawContent: r.rawContent,
138
- images: r.images || void 0
139
- })),
140
- failedResults: (response.failedResults || []).map((r) => ({
141
- url: r.url,
142
- error: r.error
143
- })),
144
- responseTime: response.responseTime
145
- };
146
- }
147
- });
118
+ let client = null;
119
+ function getClient() {
120
+ if (!client) client = getTavilyClient(config);
121
+ return client;
122
+ }
123
+ return createTool({
124
+ id: "tavily-extract",
125
+ description: "Extract content from one or more URLs using Tavily. Returns raw page content in markdown or text format. Supports up to 20 URLs per request with basic or advanced extraction depth.",
126
+ inputSchema: inputSchema$2,
127
+ outputSchema: outputSchema$2,
128
+ execute: async (input) => {
129
+ const response = await getClient().extract(input.urls, {
130
+ extractDepth: input.extractDepth,
131
+ query: input.query,
132
+ includeImages: input.includeImages,
133
+ format: input.format
134
+ });
135
+ return {
136
+ results: (response.results || []).map((r) => ({
137
+ url: r.url,
138
+ rawContent: r.rawContent,
139
+ images: r.images || void 0
140
+ })),
141
+ failedResults: (response.failedResults || []).map((r) => ({
142
+ url: r.url,
143
+ error: r.error
144
+ })),
145
+ responseTime: response.responseTime
146
+ };
147
+ }
148
+ });
148
149
  }
149
- var inputSchema3 = z.object({
150
- url: z.string().describe("The root URL to begin the crawl"),
151
- maxDepth: z.number().optional().describe("Max depth of the crawl from the base URL"),
152
- maxBreadth: z.number().optional().describe("Max number of links to follow per page"),
153
- limit: z.number().optional().describe("Total number of pages the crawler will process before stopping"),
154
- instructions: z.string().optional().describe("Natural language instructions for the crawler"),
155
- selectPaths: z.array(z.string()).optional().describe("Regex patterns to select specific URL paths"),
156
- selectDomains: z.array(z.string()).optional().describe("Regex patterns to restrict to specific domains"),
157
- excludePaths: z.array(z.string()).optional().describe("Regex patterns to exclude specific URL paths"),
158
- excludeDomains: z.array(z.string()).optional().describe("Regex patterns to exclude specific domains"),
159
- allowExternal: z.boolean().optional().describe("Whether to follow links to external domains"),
160
- extractDepth: z.enum(["basic", "advanced"]).optional().describe("Extraction depth \u2014 'advanced' retrieves more data including tables and embedded content"),
161
- includeImages: z.boolean().optional().describe("Include images from crawled pages"),
162
- format: z.enum(["markdown", "text"]).optional().describe("Output format for extracted content \u2014 'markdown' (default) or 'text'")
150
+ //#endregion
151
+ //#region src/crawl.ts
152
+ const inputSchema$1 = z.object({
153
+ url: z.string().describe("The root URL to begin the crawl"),
154
+ maxDepth: z.number().optional().describe("Max depth of the crawl from the base URL"),
155
+ maxBreadth: z.number().optional().describe("Max number of links to follow per page"),
156
+ limit: z.number().optional().describe("Total number of pages the crawler will process before stopping"),
157
+ instructions: z.string().optional().describe("Natural language instructions for the crawler"),
158
+ selectPaths: z.array(z.string()).optional().describe("Regex patterns to select specific URL paths"),
159
+ selectDomains: z.array(z.string()).optional().describe("Regex patterns to restrict to specific domains"),
160
+ excludePaths: z.array(z.string()).optional().describe("Regex patterns to exclude specific URL paths"),
161
+ excludeDomains: z.array(z.string()).optional().describe("Regex patterns to exclude specific domains"),
162
+ allowExternal: z.boolean().optional().describe("Whether to follow links to external domains"),
163
+ extractDepth: z.enum(["basic", "advanced"]).optional().describe("Extraction depth — 'advanced' retrieves more data including tables and embedded content"),
164
+ includeImages: z.boolean().optional().describe("Include images from crawled pages"),
165
+ format: z.enum(["markdown", "text"]).optional().describe("Output format for extracted content — 'markdown' (default) or 'text'")
163
166
  });
164
- var outputSchema3 = z.object({
165
- baseUrl: z.string(),
166
- results: z.array(
167
- z.object({
168
- url: z.string(),
169
- rawContent: z.string(),
170
- images: z.array(z.string()).optional()
171
- })
172
- ),
173
- responseTime: z.number()
167
+ const outputSchema$1 = z.object({
168
+ baseUrl: z.string(),
169
+ results: z.array(z.object({
170
+ url: z.string(),
171
+ rawContent: z.string(),
172
+ images: z.array(z.string()).optional()
173
+ })),
174
+ responseTime: z.number()
174
175
  });
175
176
  function createTavilyCrawlTool(config) {
176
- let client = null;
177
- function getClient() {
178
- if (!client) {
179
- client = getTavilyClient(config);
180
- }
181
- return client;
182
- }
183
- return createTool({
184
- id: "tavily-crawl",
185
- description: "Crawl a website starting from a URL using Tavily. Extracts content from discovered pages with configurable depth, breadth, and domain constraints. Returns structured content from each crawled page.",
186
- inputSchema: inputSchema3,
187
- outputSchema: outputSchema3,
188
- execute: async (input) => {
189
- const tavilyClient = getClient();
190
- const response = await tavilyClient.crawl(input.url, {
191
- maxDepth: input.maxDepth,
192
- maxBreadth: input.maxBreadth,
193
- limit: input.limit,
194
- instructions: input.instructions,
195
- selectPaths: input.selectPaths,
196
- selectDomains: input.selectDomains,
197
- excludePaths: input.excludePaths,
198
- excludeDomains: input.excludeDomains,
199
- allowExternal: input.allowExternal,
200
- extractDepth: input.extractDepth,
201
- includeImages: input.includeImages,
202
- format: input.format
203
- });
204
- return {
205
- baseUrl: response.baseUrl,
206
- results: (response.results || []).map((r) => ({
207
- url: r.url,
208
- rawContent: r.rawContent,
209
- images: r.images || void 0
210
- })),
211
- responseTime: response.responseTime
212
- };
213
- }
214
- });
177
+ let client = null;
178
+ function getClient() {
179
+ if (!client) client = getTavilyClient(config);
180
+ return client;
181
+ }
182
+ return createTool({
183
+ id: "tavily-crawl",
184
+ description: "Crawl a website starting from a URL using Tavily. Extracts content from discovered pages with configurable depth, breadth, and domain constraints. Returns structured content from each crawled page.",
185
+ inputSchema: inputSchema$1,
186
+ outputSchema: outputSchema$1,
187
+ execute: async (input) => {
188
+ const response = await getClient().crawl(input.url, {
189
+ maxDepth: input.maxDepth,
190
+ maxBreadth: input.maxBreadth,
191
+ limit: input.limit,
192
+ instructions: input.instructions,
193
+ selectPaths: input.selectPaths,
194
+ selectDomains: input.selectDomains,
195
+ excludePaths: input.excludePaths,
196
+ excludeDomains: input.excludeDomains,
197
+ allowExternal: input.allowExternal,
198
+ extractDepth: input.extractDepth,
199
+ includeImages: input.includeImages,
200
+ format: input.format
201
+ });
202
+ return {
203
+ baseUrl: response.baseUrl,
204
+ results: (response.results || []).map((r) => ({
205
+ url: r.url,
206
+ rawContent: r.rawContent,
207
+ images: r.images || void 0
208
+ })),
209
+ responseTime: response.responseTime
210
+ };
211
+ }
212
+ });
215
213
  }
216
- var inputSchema4 = z.object({
217
- url: z.string().describe("The root URL to begin mapping"),
218
- maxDepth: z.number().optional().describe("Max depth of the mapping from the base URL"),
219
- maxBreadth: z.number().optional().describe("Max number of links to follow per page"),
220
- limit: z.number().optional().describe("Total number of links the mapper will process before stopping"),
221
- instructions: z.string().optional().describe("Natural language instructions for the mapper"),
222
- selectPaths: z.array(z.string()).optional().describe("Regex patterns to select specific URL paths"),
223
- selectDomains: z.array(z.string()).optional().describe("Regex patterns to restrict to specific domains"),
224
- excludePaths: z.array(z.string()).optional().describe("Regex patterns to exclude specific URL paths"),
225
- excludeDomains: z.array(z.string()).optional().describe("Regex patterns to exclude specific domains"),
226
- allowExternal: z.boolean().optional().describe("Whether to include external domain links")
214
+ //#endregion
215
+ //#region src/map.ts
216
+ const inputSchema = z.object({
217
+ url: z.string().describe("The root URL to begin mapping"),
218
+ maxDepth: z.number().optional().describe("Max depth of the mapping from the base URL"),
219
+ maxBreadth: z.number().optional().describe("Max number of links to follow per page"),
220
+ limit: z.number().optional().describe("Total number of links the mapper will process before stopping"),
221
+ instructions: z.string().optional().describe("Natural language instructions for the mapper"),
222
+ selectPaths: z.array(z.string()).optional().describe("Regex patterns to select specific URL paths"),
223
+ selectDomains: z.array(z.string()).optional().describe("Regex patterns to restrict to specific domains"),
224
+ excludePaths: z.array(z.string()).optional().describe("Regex patterns to exclude specific URL paths"),
225
+ excludeDomains: z.array(z.string()).optional().describe("Regex patterns to exclude specific domains"),
226
+ allowExternal: z.boolean().optional().describe("Whether to include external domain links")
227
227
  });
228
- var outputSchema4 = z.object({
229
- baseUrl: z.string(),
230
- results: z.array(z.string()).describe("Discovered URLs"),
231
- responseTime: z.number()
228
+ const outputSchema = z.object({
229
+ baseUrl: z.string(),
230
+ results: z.array(z.string()).describe("Discovered URLs"),
231
+ responseTime: z.number()
232
232
  });
233
233
  function createTavilyMapTool(config) {
234
- let client = null;
235
- function getClient() {
236
- if (!client) {
237
- client = getTavilyClient(config);
238
- }
239
- return client;
240
- }
241
- return createTool({
242
- id: "tavily-map",
243
- description: "Map a website's structure starting from a URL using Tavily. Discovers and returns a list of URLs found on the site without extracting page content. Useful for understanding site structure before targeted extraction.",
244
- inputSchema: inputSchema4,
245
- outputSchema: outputSchema4,
246
- execute: async (input) => {
247
- const tavilyClient = getClient();
248
- const response = await tavilyClient.map(input.url, {
249
- maxDepth: input.maxDepth,
250
- maxBreadth: input.maxBreadth,
251
- limit: input.limit,
252
- instructions: input.instructions,
253
- selectPaths: input.selectPaths,
254
- selectDomains: input.selectDomains,
255
- excludePaths: input.excludePaths,
256
- excludeDomains: input.excludeDomains,
257
- allowExternal: input.allowExternal
258
- });
259
- return {
260
- baseUrl: response.baseUrl,
261
- results: response.results || [],
262
- responseTime: response.responseTime
263
- };
264
- }
265
- });
234
+ let client = null;
235
+ function getClient() {
236
+ if (!client) client = getTavilyClient(config);
237
+ return client;
238
+ }
239
+ return createTool({
240
+ id: "tavily-map",
241
+ description: "Map a website's structure starting from a URL using Tavily. Discovers and returns a list of URLs found on the site without extracting page content. Useful for understanding site structure before targeted extraction.",
242
+ inputSchema,
243
+ outputSchema,
244
+ execute: async (input) => {
245
+ const response = await getClient().map(input.url, {
246
+ maxDepth: input.maxDepth,
247
+ maxBreadth: input.maxBreadth,
248
+ limit: input.limit,
249
+ instructions: input.instructions,
250
+ selectPaths: input.selectPaths,
251
+ selectDomains: input.selectDomains,
252
+ excludePaths: input.excludePaths,
253
+ excludeDomains: input.excludeDomains,
254
+ allowExternal: input.allowExternal
255
+ });
256
+ return {
257
+ baseUrl: response.baseUrl,
258
+ results: response.results || [],
259
+ responseTime: response.responseTime
260
+ };
261
+ }
262
+ });
266
263
  }
267
-
268
- // src/tools.ts
264
+ //#endregion
265
+ //#region src/tools.ts
269
266
  function createTavilyTools(config) {
270
- return {
271
- tavilySearch: createTavilySearchTool(config),
272
- tavilyExtract: createTavilyExtractTool(config),
273
- tavilyCrawl: createTavilyCrawlTool(config),
274
- tavilyMap: createTavilyMapTool(config)
275
- };
267
+ return {
268
+ tavilySearch: createTavilySearchTool(config),
269
+ tavilyExtract: createTavilyExtractTool(config),
270
+ tavilyCrawl: createTavilyCrawlTool(config),
271
+ tavilyMap: createTavilyMapTool(config)
272
+ };
276
273
  }
277
-
274
+ //#endregion
278
275
  export { createTavilyCrawlTool, createTavilyExtractTool, createTavilyMapTool, createTavilySearchTool, createTavilyTools, getTavilyClient };
279
- //# sourceMappingURL=index.js.map
276
+
280
277
  //# sourceMappingURL=index.js.map