valyu-js 1.0.7 → 2.0.2
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/README.md +336 -23
- package/dist/index.d.mts +40 -14
- package/dist/index.d.ts +40 -14
- package/dist/index.js +98 -41
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +98 -41
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,40 +1,353 @@
|
|
|
1
|
-
# Valyu SDK
|
|
1
|
+
# Valyu SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**DeepSearch API for AI**
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Valyu's Deepsearch API gives AI the context it needs. Integrate trusted, high-quality public and proprietary sources, with full-text multimodal retrieval.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
Get **$10 free credits** for the Valyu API when you sign up at [Valyu](https://platform.valyu.network)!
|
|
8
|
+
|
|
9
|
+
*No credit card required.*
|
|
10
|
+
|
|
11
|
+
## How does it work?
|
|
12
|
+
|
|
13
|
+
We do all the heavy lifting for you - through a single API we provide:
|
|
14
|
+
|
|
15
|
+
- **Academic & Research Content** - Access millions of scholarly papers and textbooks
|
|
16
|
+
- **Real-time Web Search** - Get the latest information from across the internet
|
|
17
|
+
- **Structured Financial Data** - Stock prices, market data, and financial metrics
|
|
18
|
+
- **Intelligent Reranking** - Results across all sources are automatically sorted by relevance
|
|
19
|
+
- **Transparent Pricing** - Pay only for what you use with clear CPM pricing
|
|
12
20
|
|
|
13
21
|
## Installation
|
|
14
22
|
|
|
15
|
-
Install the Valyu SDK
|
|
23
|
+
Install the Valyu SDK using npm:
|
|
16
24
|
|
|
17
25
|
```bash
|
|
18
26
|
npm install valyu
|
|
19
27
|
```
|
|
20
28
|
|
|
21
|
-
Quick Start
|
|
22
|
-
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
Here's what it looks like, make your first query in just 4 lines of code:
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
const { Valyu } = require('valyu');
|
|
35
|
+
|
|
36
|
+
const valyu = new Valyu("your-api-key-here");
|
|
37
|
+
|
|
38
|
+
const response = await valyu.search(
|
|
39
|
+
"Implementation details of agentic search-enhanced large reasoning models"
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
console.log(response);
|
|
43
|
+
|
|
44
|
+
// Feed the results to your AI agent as you would with other search APIs
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## API Reference
|
|
48
|
+
|
|
49
|
+
### Search Method
|
|
50
|
+
|
|
51
|
+
The `search()` method is the core of the Valyu SDK. It accepts a query string as the first parameter, followed by optional configuration parameters.
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
valyu.search(
|
|
55
|
+
query, // Your search query
|
|
56
|
+
{
|
|
57
|
+
searchType: "all", // "all", "web", or "proprietary"
|
|
58
|
+
maxNumResults: 10, // Maximum results to return (1-20)
|
|
59
|
+
isToolCall: true, // Whether this is an AI tool call
|
|
60
|
+
relevanceThreshold: 0.5, // Minimum relevance score (0-1)
|
|
61
|
+
maxPrice: 30, // Maximum price per thousand queries (CPM)
|
|
62
|
+
includedSources: [], // Specific sources to search
|
|
63
|
+
excludeSources: [], // Sources/domains to exclude
|
|
64
|
+
category: null, // Category filter
|
|
65
|
+
startDate: null, // Start date (YYYY-MM-DD)
|
|
66
|
+
endDate: null, // End date (YYYY-MM-DD)
|
|
67
|
+
countryCode: null, // Country code filter
|
|
68
|
+
responseLength: null // Response length control
|
|
69
|
+
}
|
|
70
|
+
)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Parameters
|
|
74
|
+
|
|
75
|
+
| Parameter | Type | Default | Description |
|
|
76
|
+
|-----------|------|---------|-------------|
|
|
77
|
+
| `query` | `string` | *required* | The search query string |
|
|
78
|
+
| `searchType` | `string` | `"all"` | Search scope: `"all"`, `"web"`, or `"proprietary"` |
|
|
79
|
+
| `maxNumResults` | `number` | `10` | Maximum number of results to return (1-20) |
|
|
80
|
+
| `isToolCall` | `boolean` | `true` | Whether this is an AI tool call (affects processing) |
|
|
81
|
+
| `relevanceThreshold` | `number` | `0.5` | Minimum relevance score for results (0.0-1.0) |
|
|
82
|
+
| `maxPrice` | `number` | `30` | Maximum price per thousand queries in CPM |
|
|
83
|
+
| `includedSources` | `string[]` | `[]` | Specific data sources or URLs to search |
|
|
84
|
+
| `excludeSources` | `string[]` | `[]` | List of URLs/domains to exclude from search results |
|
|
85
|
+
| `category` | `string` | `null` | Category filter for results |
|
|
86
|
+
| `startDate` | `string` | `null` | Start date filter in YYYY-MM-DD format |
|
|
87
|
+
| `endDate` | `string` | `null` | End date filter in YYYY-MM-DD format |
|
|
88
|
+
| `countryCode` | `string` | `null` | Country code filter (e.g., "US", "GB", "JP", "ALL") |
|
|
89
|
+
| `responseLength` | `string \| number` | `null` | Response length: "short", "medium", "large", "max", or character count |
|
|
90
|
+
|
|
91
|
+
### Response Format
|
|
92
|
+
|
|
93
|
+
The search method returns a `SearchResponse` object with the following structure:
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
{
|
|
97
|
+
success: boolean, // Whether the search was successful
|
|
98
|
+
error: string | null, // Error message if any
|
|
99
|
+
tx_id: string, // Transaction ID for feedback
|
|
100
|
+
query: string, // The original query
|
|
101
|
+
results: SearchResult[], // List of search results
|
|
102
|
+
results_by_source: { // Count of results by source type
|
|
103
|
+
web: number,
|
|
104
|
+
proprietary: number
|
|
105
|
+
},
|
|
106
|
+
total_deduction_pcm: number, // Cost in CPM
|
|
107
|
+
total_deduction_dollars: number, // Cost in dollars
|
|
108
|
+
total_characters: number // Total characters returned
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Each `SearchResult` contains:
|
|
113
|
+
|
|
114
|
+
```javascript
|
|
115
|
+
{
|
|
116
|
+
title: string, // Result title
|
|
117
|
+
url: string, // Source URL
|
|
118
|
+
content: string, // Full content
|
|
119
|
+
description?: string, // Brief description
|
|
120
|
+
source: string, // Source identifier
|
|
121
|
+
price: number, // Cost for this result
|
|
122
|
+
length: number, // Content length in characters
|
|
123
|
+
image_url?: Record<string, string>, // Associated images
|
|
124
|
+
relevance_score: number, // Relevance score (0-1)
|
|
125
|
+
data_type?: string // "structured" or "unstructured"
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Examples
|
|
130
|
+
|
|
131
|
+
### Basic Search
|
|
132
|
+
|
|
133
|
+
```javascript
|
|
23
134
|
const { Valyu } = require('valyu');
|
|
24
135
|
|
|
25
|
-
// Initialize the Valyu client (provide your API key)
|
|
26
136
|
const valyu = new Valyu("your-api-key");
|
|
27
137
|
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"Tell me about ancient civilizations", // query
|
|
32
|
-
"proprietary", // search_type ("proprietary" or "web")
|
|
33
|
-
5, // num_query: number of queries to generate
|
|
34
|
-
3, // num_results: number of results per query
|
|
35
|
-
10 // max_price: maximum price per thousand queries
|
|
36
|
-
);
|
|
37
|
-
console.log(response);
|
|
38
|
-
})();
|
|
138
|
+
// Simple search with no search parameters
|
|
139
|
+
const response = await valyu.search("what is trans-applifying mRNA");
|
|
140
|
+
console.log(response);
|
|
39
141
|
```
|
|
40
142
|
|
|
143
|
+
### Academic Research
|
|
144
|
+
|
|
145
|
+
```javascript
|
|
146
|
+
// Deep search over academic papers
|
|
147
|
+
const response = await valyu.search(
|
|
148
|
+
"implementation details of agentic search-enhanced large reasoning models",
|
|
149
|
+
{
|
|
150
|
+
searchType: "proprietary",
|
|
151
|
+
maxNumResults: 10,
|
|
152
|
+
relevanceThreshold: 0.6,
|
|
153
|
+
includedSources: ["valyu/valyu-arxiv"],
|
|
154
|
+
category: "agentic RAG",
|
|
155
|
+
startDate: "2024-12-01"
|
|
156
|
+
}
|
|
157
|
+
);
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Web Search with Date Filtering
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
// Web search with date range
|
|
164
|
+
const response = await valyu.search(
|
|
165
|
+
"what are the grok 4 benchmark results",
|
|
166
|
+
{
|
|
167
|
+
searchType: "web",
|
|
168
|
+
maxNumResults: 7,
|
|
169
|
+
relevanceThreshold: 0.5,
|
|
170
|
+
startDate: "2025-06-01",
|
|
171
|
+
endDate: "2025-07-25"
|
|
172
|
+
}
|
|
173
|
+
);
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Country-Specific Search
|
|
177
|
+
|
|
178
|
+
```javascript
|
|
179
|
+
// Web search with country filtering
|
|
180
|
+
const response = await valyu.search(
|
|
181
|
+
"what is the weather where i am?",
|
|
182
|
+
{
|
|
183
|
+
searchType: "web",
|
|
184
|
+
maxNumResults: 2,
|
|
185
|
+
countryCode: "UK",
|
|
186
|
+
responseLength: "short"
|
|
187
|
+
}
|
|
188
|
+
);
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Search with Source Exclusion
|
|
192
|
+
|
|
193
|
+
```javascript
|
|
194
|
+
// Hybrid search excluding specific sources
|
|
195
|
+
const response = await valyu.search(
|
|
196
|
+
"quantum computing applications in cryptography",
|
|
197
|
+
{
|
|
198
|
+
searchType: "all",
|
|
199
|
+
maxNumResults: 8,
|
|
200
|
+
relevanceThreshold: 0.5,
|
|
201
|
+
maxPrice: 40,
|
|
202
|
+
excludeSources: ["paperswithcode.com", "wikipedia.org"],
|
|
203
|
+
responseLength: "large",
|
|
204
|
+
isToolCall: true
|
|
205
|
+
}
|
|
206
|
+
);
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Custom Response Length
|
|
210
|
+
|
|
211
|
+
```javascript
|
|
212
|
+
// Search with custom character count
|
|
213
|
+
const response = await valyu.search(
|
|
214
|
+
"State of video generation AI models",
|
|
215
|
+
{
|
|
216
|
+
maxNumResults: 10,
|
|
217
|
+
category: "vLLMs",
|
|
218
|
+
responseLength: 1000 // Limit to 1000 characters per result
|
|
219
|
+
}
|
|
220
|
+
);
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Processing Results
|
|
224
|
+
|
|
225
|
+
```javascript
|
|
226
|
+
const response = await valyu.search("climate change solutions");
|
|
227
|
+
|
|
228
|
+
if (response.success) {
|
|
229
|
+
console.log(`Search cost: $${response.total_deduction_dollars.toFixed(4)}`);
|
|
230
|
+
console.log(`Sources: Web=${response.results_by_source.web}, Proprietary=${response.results_by_source.proprietary}`);
|
|
231
|
+
|
|
232
|
+
response.results.forEach((result, i) => {
|
|
233
|
+
console.log(`\n${i + 1}. ${result.title}`);
|
|
234
|
+
console.log(` Source: ${result.source}`);
|
|
235
|
+
console.log(` Relevance: ${result.relevance_score.toFixed(2)}`);
|
|
236
|
+
console.log(` Content: ${result.content.substring(0, 200)}...`);
|
|
237
|
+
});
|
|
238
|
+
} else {
|
|
239
|
+
console.log(`Search failed: ${response.error}`);
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Authentication
|
|
244
|
+
|
|
245
|
+
Set your API key in one of these ways:
|
|
246
|
+
|
|
247
|
+
1. **Environment variable** (recommended):
|
|
248
|
+
```bash
|
|
249
|
+
export VALYU_API_KEY="your-api-key-here"
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
2. **Direct initialization**:
|
|
253
|
+
```javascript
|
|
254
|
+
const valyu = new Valyu("your-api-key-here");
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
## Error Handling
|
|
258
|
+
|
|
259
|
+
The SDK handles errors gracefully and returns structured error responses:
|
|
260
|
+
|
|
261
|
+
```javascript
|
|
262
|
+
const response = await valyu.search("test query");
|
|
263
|
+
|
|
264
|
+
if (!response.success) {
|
|
265
|
+
console.log(`Error: ${response.error}`);
|
|
266
|
+
console.log(`Transaction ID: ${response.tx_id}`);
|
|
267
|
+
} else {
|
|
268
|
+
// Process successful results
|
|
269
|
+
response.results.forEach(result => {
|
|
270
|
+
console.log(result.title);
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## TypeScript Support
|
|
276
|
+
|
|
277
|
+
The SDK includes full TypeScript support with type definitions for all parameters:
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
import { Valyu, SearchOptions, SearchResponse, CountryCode, ResponseLength } from 'valyu';
|
|
281
|
+
|
|
282
|
+
const valyu = new Valyu("your-api-key");
|
|
283
|
+
|
|
284
|
+
const options: SearchOptions = {
|
|
285
|
+
searchType: "proprietary",
|
|
286
|
+
maxNumResults: 10,
|
|
287
|
+
relevanceThreshold: 0.6,
|
|
288
|
+
excludeSources: ["reddit.com", "twitter.com"],
|
|
289
|
+
countryCode: "US" as CountryCode,
|
|
290
|
+
responseLength: "medium" as ResponseLength
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const response: SearchResponse = await valyu.search("machine learning", options);
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## Backward Compatibility
|
|
297
|
+
|
|
298
|
+
The legacy `context()` method is still supported but deprecated:
|
|
299
|
+
|
|
300
|
+
```javascript
|
|
301
|
+
// Legacy method (deprecated)
|
|
302
|
+
const response = await valyu.context(
|
|
303
|
+
"neural networks basics",
|
|
304
|
+
{
|
|
305
|
+
searchType: "all",
|
|
306
|
+
maxNumResults: 5,
|
|
307
|
+
queryRewrite: true,
|
|
308
|
+
similarityThreshold: 0.5,
|
|
309
|
+
dataSources: ["valyu/valyu-arxiv"]
|
|
310
|
+
}
|
|
311
|
+
);
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
**Migration from v1 to v2:**
|
|
315
|
+
- `context()` → `search()`
|
|
316
|
+
- `similarityThreshold` → `relevanceThreshold`
|
|
317
|
+
- `dataSources` → `includedSources`
|
|
318
|
+
- `queryRewrite` → `isToolCall`
|
|
319
|
+
- Default `relevanceThreshold` changed from `0.4` to `0.5`
|
|
320
|
+
- Default `maxPrice` changed from `1` to `30`
|
|
321
|
+
|
|
322
|
+
## Getting Started
|
|
323
|
+
|
|
324
|
+
1. Sign up for a free account at [Valyu](https://exchange.valyu.network)
|
|
325
|
+
2. Get your API key from the dashboard
|
|
326
|
+
3. Install the SDK: `npm install valyu`
|
|
327
|
+
4. Start building with the examples above
|
|
328
|
+
|
|
329
|
+
## Testing
|
|
330
|
+
|
|
331
|
+
Run the integration tests:
|
|
332
|
+
|
|
333
|
+
```bash
|
|
334
|
+
npm run test:integration
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
Run the v2 API examples:
|
|
338
|
+
|
|
339
|
+
```bash
|
|
340
|
+
node examples/v2-api-examples.js
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
## Support
|
|
344
|
+
|
|
345
|
+
- **Documentation**: [docs.valyu.network](https://docs.valyu.network)
|
|
346
|
+
- **API Reference**: Full parameter documentation above
|
|
347
|
+
- **Examples**: Check the `examples/` directory in this repository
|
|
348
|
+
- **Issues**: Report bugs on GitHub
|
|
349
|
+
|
|
350
|
+
## License
|
|
351
|
+
|
|
352
|
+
This project is licensed under the MIT License.
|
|
353
|
+
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
type SearchType = "web" | "proprietary" | "all";
|
|
2
2
|
type FeedbackSentiment = "very good" | "good" | "bad" | "very bad";
|
|
3
3
|
type DataType = "structured" | "unstructured";
|
|
4
|
+
type CountryCode = "ALL" | "AR" | "AU" | "AT" | "BE" | "BR" | "CA" | "CL" | "DK" | "FI" | "FR" | "DE" | "HK" | "IN" | "ID" | "IT" | "JP" | "KR" | "MY" | "MX" | "NL" | "NZ" | "NO" | "CN" | "PL" | "PT" | "PH" | "RU" | "SA" | "ZA" | "ES" | "SE" | "CH" | "TW" | "TR" | "GB" | "US";
|
|
5
|
+
type ResponseLength = "short" | "medium" | "large" | "max" | number;
|
|
4
6
|
interface SearchResult {
|
|
5
7
|
title: string;
|
|
6
8
|
url: string;
|
|
@@ -12,6 +14,20 @@ interface SearchResult {
|
|
|
12
14
|
relevance_score: number;
|
|
13
15
|
data_type?: DataType;
|
|
14
16
|
}
|
|
17
|
+
interface SearchOptions {
|
|
18
|
+
searchType?: SearchType;
|
|
19
|
+
maxNumResults?: number;
|
|
20
|
+
maxPrice?: number;
|
|
21
|
+
isToolCall?: boolean;
|
|
22
|
+
relevanceThreshold?: number;
|
|
23
|
+
includedSources?: string[];
|
|
24
|
+
excludeSources?: string[];
|
|
25
|
+
category?: string;
|
|
26
|
+
startDate?: string;
|
|
27
|
+
endDate?: string;
|
|
28
|
+
countryCode?: CountryCode;
|
|
29
|
+
responseLength?: ResponseLength;
|
|
30
|
+
}
|
|
15
31
|
interface SearchResponse {
|
|
16
32
|
success: boolean;
|
|
17
33
|
error?: string;
|
|
@@ -35,19 +51,29 @@ declare class Valyu {
|
|
|
35
51
|
private baseUrl;
|
|
36
52
|
private headers;
|
|
37
53
|
constructor(apiKey?: string, baseUrl?: string);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Validates date format (YYYY-MM-DD)
|
|
56
|
+
*/
|
|
57
|
+
private validateDateFormat;
|
|
58
|
+
/**
|
|
59
|
+
* Search for information using the Valyu DeepSearch API
|
|
60
|
+
* @param query - The search query string
|
|
61
|
+
* @param options - Search configuration options
|
|
62
|
+
* @param options.searchType - Type of search: "web", "proprietary", or "all"
|
|
63
|
+
* @param options.maxNumResults - Maximum number of results (1-20)
|
|
64
|
+
* @param options.maxPrice - Maximum price per thousand characters (CPM)
|
|
65
|
+
* @param options.isToolCall - Whether this is a tool call
|
|
66
|
+
* @param options.relevanceThreshold - Minimum relevance score (0-1)
|
|
67
|
+
* @param options.includedSources - List of specific sources to include
|
|
68
|
+
* @param options.excludeSources - List of URLs/domains to exclude from search results
|
|
69
|
+
* @param options.category - Category filter for search results
|
|
70
|
+
* @param options.startDate - Start date filter (YYYY-MM-DD format)
|
|
71
|
+
* @param options.endDate - End date filter (YYYY-MM-DD format)
|
|
72
|
+
* @param options.countryCode - Country code filter for search results
|
|
73
|
+
* @param options.responseLength - Response content length: "short"/"medium"/"large"/"max" or integer character count
|
|
74
|
+
* @returns Promise resolving to search results
|
|
75
|
+
*/
|
|
76
|
+
search(query: string, options?: SearchOptions): Promise<SearchResponse>;
|
|
51
77
|
}
|
|
52
78
|
|
|
53
|
-
export { type FeedbackResponse, type FeedbackSentiment, type SearchResponse, type SearchType, Valyu };
|
|
79
|
+
export { type CountryCode, type FeedbackResponse, type FeedbackSentiment, type ResponseLength, type SearchOptions, type SearchResponse, type SearchType, Valyu };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
type SearchType = "web" | "proprietary" | "all";
|
|
2
2
|
type FeedbackSentiment = "very good" | "good" | "bad" | "very bad";
|
|
3
3
|
type DataType = "structured" | "unstructured";
|
|
4
|
+
type CountryCode = "ALL" | "AR" | "AU" | "AT" | "BE" | "BR" | "CA" | "CL" | "DK" | "FI" | "FR" | "DE" | "HK" | "IN" | "ID" | "IT" | "JP" | "KR" | "MY" | "MX" | "NL" | "NZ" | "NO" | "CN" | "PL" | "PT" | "PH" | "RU" | "SA" | "ZA" | "ES" | "SE" | "CH" | "TW" | "TR" | "GB" | "US";
|
|
5
|
+
type ResponseLength = "short" | "medium" | "large" | "max" | number;
|
|
4
6
|
interface SearchResult {
|
|
5
7
|
title: string;
|
|
6
8
|
url: string;
|
|
@@ -12,6 +14,20 @@ interface SearchResult {
|
|
|
12
14
|
relevance_score: number;
|
|
13
15
|
data_type?: DataType;
|
|
14
16
|
}
|
|
17
|
+
interface SearchOptions {
|
|
18
|
+
searchType?: SearchType;
|
|
19
|
+
maxNumResults?: number;
|
|
20
|
+
maxPrice?: number;
|
|
21
|
+
isToolCall?: boolean;
|
|
22
|
+
relevanceThreshold?: number;
|
|
23
|
+
includedSources?: string[];
|
|
24
|
+
excludeSources?: string[];
|
|
25
|
+
category?: string;
|
|
26
|
+
startDate?: string;
|
|
27
|
+
endDate?: string;
|
|
28
|
+
countryCode?: CountryCode;
|
|
29
|
+
responseLength?: ResponseLength;
|
|
30
|
+
}
|
|
15
31
|
interface SearchResponse {
|
|
16
32
|
success: boolean;
|
|
17
33
|
error?: string;
|
|
@@ -35,19 +51,29 @@ declare class Valyu {
|
|
|
35
51
|
private baseUrl;
|
|
36
52
|
private headers;
|
|
37
53
|
constructor(apiKey?: string, baseUrl?: string);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Validates date format (YYYY-MM-DD)
|
|
56
|
+
*/
|
|
57
|
+
private validateDateFormat;
|
|
58
|
+
/**
|
|
59
|
+
* Search for information using the Valyu DeepSearch API
|
|
60
|
+
* @param query - The search query string
|
|
61
|
+
* @param options - Search configuration options
|
|
62
|
+
* @param options.searchType - Type of search: "web", "proprietary", or "all"
|
|
63
|
+
* @param options.maxNumResults - Maximum number of results (1-20)
|
|
64
|
+
* @param options.maxPrice - Maximum price per thousand characters (CPM)
|
|
65
|
+
* @param options.isToolCall - Whether this is a tool call
|
|
66
|
+
* @param options.relevanceThreshold - Minimum relevance score (0-1)
|
|
67
|
+
* @param options.includedSources - List of specific sources to include
|
|
68
|
+
* @param options.excludeSources - List of URLs/domains to exclude from search results
|
|
69
|
+
* @param options.category - Category filter for search results
|
|
70
|
+
* @param options.startDate - Start date filter (YYYY-MM-DD format)
|
|
71
|
+
* @param options.endDate - End date filter (YYYY-MM-DD format)
|
|
72
|
+
* @param options.countryCode - Country code filter for search results
|
|
73
|
+
* @param options.responseLength - Response content length: "short"/"medium"/"large"/"max" or integer character count
|
|
74
|
+
* @returns Promise resolving to search results
|
|
75
|
+
*/
|
|
76
|
+
search(query: string, options?: SearchOptions): Promise<SearchResponse>;
|
|
51
77
|
}
|
|
52
78
|
|
|
53
|
-
export { type FeedbackResponse, type FeedbackSentiment, type SearchResponse, type SearchType, Valyu };
|
|
79
|
+
export { type CountryCode, type FeedbackResponse, type FeedbackSentiment, type ResponseLength, type SearchOptions, type SearchResponse, type SearchType, Valyu };
|
package/dist/index.js
CHANGED
|
@@ -48,13 +48,42 @@ var Valyu = class {
|
|
|
48
48
|
"x-api-key": apiKey
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Validates date format (YYYY-MM-DD)
|
|
53
|
+
*/
|
|
54
|
+
validateDateFormat(date) {
|
|
55
|
+
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
|
|
56
|
+
if (!dateRegex.test(date)) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
const parsedDate = new Date(date);
|
|
60
|
+
return parsedDate instanceof Date && !isNaN(parsedDate.getTime());
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Search for information using the Valyu DeepSearch API
|
|
64
|
+
* @param query - The search query string
|
|
65
|
+
* @param options - Search configuration options
|
|
66
|
+
* @param options.searchType - Type of search: "web", "proprietary", or "all"
|
|
67
|
+
* @param options.maxNumResults - Maximum number of results (1-20)
|
|
68
|
+
* @param options.maxPrice - Maximum price per thousand characters (CPM)
|
|
69
|
+
* @param options.isToolCall - Whether this is a tool call
|
|
70
|
+
* @param options.relevanceThreshold - Minimum relevance score (0-1)
|
|
71
|
+
* @param options.includedSources - List of specific sources to include
|
|
72
|
+
* @param options.excludeSources - List of URLs/domains to exclude from search results
|
|
73
|
+
* @param options.category - Category filter for search results
|
|
74
|
+
* @param options.startDate - Start date filter (YYYY-MM-DD format)
|
|
75
|
+
* @param options.endDate - End date filter (YYYY-MM-DD format)
|
|
76
|
+
* @param options.countryCode - Country code filter for search results
|
|
77
|
+
* @param options.responseLength - Response content length: "short"/"medium"/"large"/"max" or integer character count
|
|
78
|
+
* @returns Promise resolving to search results
|
|
79
|
+
*/
|
|
80
|
+
async search(query, options = {}) {
|
|
52
81
|
try {
|
|
53
82
|
const defaultSearchType = "all";
|
|
54
83
|
const defaultMaxNumResults = 10;
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
const defaultMaxPrice =
|
|
84
|
+
const defaultIsToolCall = true;
|
|
85
|
+
const defaultRelevanceThreshold = 0.5;
|
|
86
|
+
const defaultMaxPrice = 30;
|
|
58
87
|
let finalSearchType = defaultSearchType;
|
|
59
88
|
const providedSearchTypeString = options.searchType?.toLowerCase();
|
|
60
89
|
if (providedSearchTypeString === "web" || providedSearchTypeString === "proprietary" || providedSearchTypeString === "all") {
|
|
@@ -72,19 +101,77 @@ var Valyu = class {
|
|
|
72
101
|
total_characters: 0
|
|
73
102
|
};
|
|
74
103
|
}
|
|
104
|
+
if (options.startDate && !this.validateDateFormat(options.startDate)) {
|
|
105
|
+
return {
|
|
106
|
+
success: false,
|
|
107
|
+
error: "Invalid startDate format. Must be YYYY-MM-DD",
|
|
108
|
+
tx_id: null,
|
|
109
|
+
query,
|
|
110
|
+
results: [],
|
|
111
|
+
results_by_source: { web: 0, proprietary: 0 },
|
|
112
|
+
total_deduction_pcm: 0,
|
|
113
|
+
total_deduction_dollars: 0,
|
|
114
|
+
total_characters: 0
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
if (options.endDate && !this.validateDateFormat(options.endDate)) {
|
|
118
|
+
return {
|
|
119
|
+
success: false,
|
|
120
|
+
error: "Invalid endDate format. Must be YYYY-MM-DD",
|
|
121
|
+
tx_id: null,
|
|
122
|
+
query,
|
|
123
|
+
results: [],
|
|
124
|
+
results_by_source: { web: 0, proprietary: 0 },
|
|
125
|
+
total_deduction_pcm: 0,
|
|
126
|
+
total_deduction_dollars: 0,
|
|
127
|
+
total_characters: 0
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const maxNumResults = options.maxNumResults ?? defaultMaxNumResults;
|
|
131
|
+
if (maxNumResults < 1 || maxNumResults > 20) {
|
|
132
|
+
return {
|
|
133
|
+
success: false,
|
|
134
|
+
error: "maxNumResults must be between 1 and 20",
|
|
135
|
+
tx_id: null,
|
|
136
|
+
query,
|
|
137
|
+
results: [],
|
|
138
|
+
results_by_source: { web: 0, proprietary: 0 },
|
|
139
|
+
total_deduction_pcm: 0,
|
|
140
|
+
total_deduction_dollars: 0,
|
|
141
|
+
total_characters: 0
|
|
142
|
+
};
|
|
143
|
+
}
|
|
75
144
|
const payload = {
|
|
76
145
|
query,
|
|
77
146
|
search_type: finalSearchType,
|
|
78
|
-
max_num_results:
|
|
79
|
-
|
|
80
|
-
|
|
147
|
+
max_num_results: maxNumResults,
|
|
148
|
+
is_tool_call: options.isToolCall ?? defaultIsToolCall,
|
|
149
|
+
relevance_threshold: options.relevanceThreshold ?? defaultRelevanceThreshold,
|
|
81
150
|
max_price: options.maxPrice ?? defaultMaxPrice
|
|
82
151
|
};
|
|
83
|
-
if (options.
|
|
84
|
-
payload.
|
|
152
|
+
if (options.includedSources !== void 0) {
|
|
153
|
+
payload.included_sources = options.includedSources;
|
|
154
|
+
}
|
|
155
|
+
if (options.excludeSources !== void 0) {
|
|
156
|
+
payload.exclude_sources = options.excludeSources;
|
|
157
|
+
}
|
|
158
|
+
if (options.category !== void 0) {
|
|
159
|
+
payload.category = options.category;
|
|
160
|
+
}
|
|
161
|
+
if (options.startDate !== void 0) {
|
|
162
|
+
payload.start_date = options.startDate;
|
|
163
|
+
}
|
|
164
|
+
if (options.endDate !== void 0) {
|
|
165
|
+
payload.end_date = options.endDate;
|
|
166
|
+
}
|
|
167
|
+
if (options.countryCode !== void 0) {
|
|
168
|
+
payload.country_code = options.countryCode;
|
|
169
|
+
}
|
|
170
|
+
if (options.responseLength !== void 0) {
|
|
171
|
+
payload.response_length = options.responseLength;
|
|
85
172
|
}
|
|
86
173
|
const response = await import_axios.default.post(
|
|
87
|
-
`${this.baseUrl}/
|
|
174
|
+
`${this.baseUrl}/deepsearch`,
|
|
88
175
|
payload,
|
|
89
176
|
{ headers: this.headers }
|
|
90
177
|
);
|
|
@@ -105,7 +192,7 @@ var Valyu = class {
|
|
|
105
192
|
} catch (e) {
|
|
106
193
|
return {
|
|
107
194
|
success: false,
|
|
108
|
-
error: e.message,
|
|
195
|
+
error: e.response?.data?.error || e.message,
|
|
109
196
|
tx_id: null,
|
|
110
197
|
query,
|
|
111
198
|
results: [],
|
|
@@ -116,36 +203,6 @@ var Valyu = class {
|
|
|
116
203
|
};
|
|
117
204
|
}
|
|
118
205
|
}
|
|
119
|
-
async feedback({
|
|
120
|
-
tx_id,
|
|
121
|
-
feedback,
|
|
122
|
-
sentiment
|
|
123
|
-
}) {
|
|
124
|
-
try {
|
|
125
|
-
const payload = {
|
|
126
|
-
tx_id,
|
|
127
|
-
feedback,
|
|
128
|
-
sentiment
|
|
129
|
-
};
|
|
130
|
-
const response = await import_axios.default.post(
|
|
131
|
-
`${this.baseUrl}/feedback`,
|
|
132
|
-
payload,
|
|
133
|
-
{ headers: this.headers }
|
|
134
|
-
);
|
|
135
|
-
if (!response.status || response.status < 200 || response.status >= 300) {
|
|
136
|
-
return {
|
|
137
|
-
success: false,
|
|
138
|
-
error: response.data?.error
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
return response.data;
|
|
142
|
-
} catch (e) {
|
|
143
|
-
return {
|
|
144
|
-
success: false,
|
|
145
|
-
error: e.message
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
206
|
};
|
|
150
207
|
// Annotate the CommonJS export names for ESM import in node:
|
|
151
208
|
0 && (module.exports = {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import axios from 'axios';\nimport { SearchResponse, SearchType, FeedbackSentiment, FeedbackResponse } from './types';\n\nexport class Valyu {\n private baseUrl: string;\n private headers: Record<string, string>;\n\n constructor(apiKey?: string, baseUrl: string = \"https://api.valyu.network/v1\") {\n if (!apiKey) {\n apiKey = process.env.VALYU_API_KEY;\n if (!apiKey) {\n throw new Error(\"VALYU_API_KEY is not set\");\n }\n }\n this.baseUrl = baseUrl;\n this.headers = {\n \"Content-Type\": \"application/json\",\n \"x-api-key\": apiKey\n };\n }\n\n async context(\n query: string,\n options: {\n searchType?: string;\n maxNumResults?: number;\n queryRewrite?: boolean;\n similarityThreshold?: number;\n maxPrice?: number;\n dataSources?: string[];\n } = {}\n ): Promise<SearchResponse> {\n try {\n const defaultSearchType: SearchType = \"all\";\n const defaultMaxNumResults = 10;\n const defaultQueryRewrite = true;\n const defaultSimilarityThreshold = 0.4;\n const defaultMaxPrice = 1;\n\n let finalSearchType: SearchType = defaultSearchType;\n const providedSearchTypeString = options.searchType?.toLowerCase();\n\n if (providedSearchTypeString === \"web\" || providedSearchTypeString === \"proprietary\" || providedSearchTypeString === \"all\") {\n finalSearchType = providedSearchTypeString as SearchType;\n } else if (options.searchType !== undefined) {\n return {\n success: false,\n error: \"Invalid searchType provided. Must be one of: all, web, proprietary\",\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n const payload: Record<string, any> = {\n query,\n search_type: finalSearchType,\n max_num_results: options.maxNumResults ?? defaultMaxNumResults,\n query_rewrite: options.queryRewrite ?? defaultQueryRewrite,\n similarity_threshold: options.similarityThreshold ?? defaultSimilarityThreshold,\n max_price: options.maxPrice ?? defaultMaxPrice,\n };\n\n if (options.dataSources !== undefined) {\n payload.data_sources = options.dataSources;\n }\n\n const response = await axios.post(\n `${this.baseUrl}/knowledge`,\n payload,\n { headers: this.headers }\n );\n\n if (!response.status || response.status < 200 || response.status >= 300) {\n return {\n success: false,\n error: response.data?.error,\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n return response.data;\n } catch (e: any) {\n return {\n success: false,\n error: e.message,\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n }\n\n async feedback({\n tx_id,\n feedback,\n sentiment\n }: {\n tx_id: string;\n feedback: string;\n sentiment: FeedbackSentiment;\n }): Promise<FeedbackResponse> {\n try {\n const payload = {\n tx_id,\n feedback,\n sentiment\n };\n\n const response = await axios.post(\n `${this.baseUrl}/feedback`,\n payload,\n { headers: this.headers }\n );\n\n if (!response.status || response.status < 200 || response.status >= 300) {\n return {\n success: false,\n error: response.data?.error\n };\n }\n\n return response.data;\n } catch (e: any) {\n return {\n success: false,\n error: e.message\n };\n }\n }\n}\n\nexport type { \n SearchResponse, \n SearchType, \n FeedbackSentiment, \n FeedbackResponse \n} from './types'; "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkB;AAGX,IAAM,QAAN,MAAY;AAAA,EAIjB,YAAY,QAAiB,UAAkB,gCAAgC;AAC7E,QAAI,CAAC,QAAQ;AACX,eAAS,QAAQ,IAAI;AACrB,UAAI,CAAC,QAAQ;AACX,cAAM,IAAI,MAAM,0BAA0B;AAAA,MAC5C;AAAA,IACF;AACA,SAAK,UAAU;AACf,SAAK,UAAU;AAAA,MACb,gBAAgB;AAAA,MAChB,aAAa;AAAA,IACf;AAAA,EACF;AAAA,EAEA,MAAM,QACJ,OACA,UAOI,CAAC,GACoB;AACzB,QAAI;AACF,YAAM,oBAAgC;AACtC,YAAM,uBAAuB;AAC7B,YAAM,sBAAsB;AAC5B,YAAM,6BAA6B;AACnC,YAAM,kBAAkB;AAExB,UAAI,kBAA8B;AAClC,YAAM,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QACV,mBAAmB,EAAE,KAAK,GAAG,aAAa,EAAE;AAAA,QAC5C,qBAAqB;AAAA,QACrB,yBAAyB;AAAA,QACzB,kBAAkB;AAAA,MACpB;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM,SAAS;AAAA,IACb;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAI8B;AAC5B,QAAI;AACF,YAAM,UAAU;AAAA,QACd;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAEA,YAAM,WAAW,MAAM,aAAAA,QAAM;AAAA,QAC3B,GAAG,KAAK,OAAO;AAAA,QACf;AAAA,QACA,EAAE,SAAS,KAAK,QAAQ;AAAA,MAC1B;AAEA,UAAI,CAAC,SAAS,UAAU,SAAS,SAAS,OAAO,SAAS,UAAU,KAAK;AACvE,eAAO;AAAA,UACL,SAAS;AAAA,UACT,OAAO,SAAS,MAAM;AAAA,QACxB;AAAA,MACF;AAEA,aAAO,SAAS;AAAA,IAClB,SAAS,GAAQ;AACf,aAAO;AAAA,QACL,SAAS;AAAA,QACT,OAAO,EAAE;AAAA,MACX;AAAA,IACF;AAAA,EACF;AACF;","names":["axios"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import axios from 'axios';\nimport { SearchResponse, SearchType, SearchOptions } from './types';\n\nexport class Valyu {\n private baseUrl: string;\n private headers: Record<string, string>;\n\n constructor(apiKey?: string, baseUrl: string = \"https://api.valyu.network/v1\") {\n if (!apiKey) {\n apiKey = process.env.VALYU_API_KEY;\n if (!apiKey) {\n throw new Error(\"VALYU_API_KEY is not set\");\n }\n }\n this.baseUrl = baseUrl;\n this.headers = {\n \"Content-Type\": \"application/json\",\n \"x-api-key\": apiKey\n };\n }\n\n /**\n * Validates date format (YYYY-MM-DD)\n */\n private validateDateFormat(date: string): boolean {\n const dateRegex = /^\\d{4}-\\d{2}-\\d{2}$/;\n if (!dateRegex.test(date)) {\n return false;\n }\n const parsedDate = new Date(date);\n return parsedDate instanceof Date && !isNaN(parsedDate.getTime());\n }\n\n /**\n * Search for information using the Valyu DeepSearch API\n * @param query - The search query string\n * @param options - Search configuration options\n * @param options.searchType - Type of search: \"web\", \"proprietary\", or \"all\"\n * @param options.maxNumResults - Maximum number of results (1-20)\n * @param options.maxPrice - Maximum price per thousand characters (CPM)\n * @param options.isToolCall - Whether this is a tool call\n * @param options.relevanceThreshold - Minimum relevance score (0-1)\n * @param options.includedSources - List of specific sources to include\n * @param options.excludeSources - List of URLs/domains to exclude from search results\n * @param options.category - Category filter for search results\n * @param options.startDate - Start date filter (YYYY-MM-DD format)\n * @param options.endDate - End date filter (YYYY-MM-DD format)\n * @param options.countryCode - Country code filter for search results\n * @param options.responseLength - Response content length: \"short\"/\"medium\"/\"large\"/\"max\" or integer character count\n * @returns Promise resolving to search results\n */\n async search(query: string, options: SearchOptions = {}): Promise<SearchResponse> {\n try {\n // Default values\n const defaultSearchType: SearchType = \"all\";\n const defaultMaxNumResults = 10;\n const defaultIsToolCall = true;\n const defaultRelevanceThreshold = 0.5;\n const defaultMaxPrice = 30;\n\n // Validate searchType\n let finalSearchType: SearchType = defaultSearchType;\n const providedSearchTypeString = options.searchType?.toLowerCase();\n\n if (providedSearchTypeString === \"web\" || providedSearchTypeString === \"proprietary\" || providedSearchTypeString === \"all\") {\n finalSearchType = providedSearchTypeString as SearchType;\n } else if (options.searchType !== undefined) {\n return {\n success: false,\n error: \"Invalid searchType provided. Must be one of: all, web, proprietary\",\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n // Validate date formats\n if (options.startDate && !this.validateDateFormat(options.startDate)) {\n return {\n success: false,\n error: \"Invalid startDate format. Must be YYYY-MM-DD\",\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n if (options.endDate && !this.validateDateFormat(options.endDate)) {\n return {\n success: false,\n error: \"Invalid endDate format. Must be YYYY-MM-DD\",\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n // Validate maxNumResults range\n const maxNumResults = options.maxNumResults ?? defaultMaxNumResults;\n if (maxNumResults < 1 || maxNumResults > 20) {\n return {\n success: false,\n error: \"maxNumResults must be between 1 and 20\",\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n // Build payload with snake_case for API\n const payload: Record<string, any> = {\n query,\n search_type: finalSearchType,\n max_num_results: maxNumResults,\n is_tool_call: options.isToolCall ?? defaultIsToolCall,\n relevance_threshold: options.relevanceThreshold ?? defaultRelevanceThreshold,\n max_price: options.maxPrice ?? defaultMaxPrice,\n };\n\n // Add optional parameters only if provided\n if (options.includedSources !== undefined) {\n payload.included_sources = options.includedSources;\n }\n\n if (options.excludeSources !== undefined) {\n payload.exclude_sources = options.excludeSources;\n }\n\n if (options.category !== undefined) {\n payload.category = options.category;\n }\n\n if (options.startDate !== undefined) {\n payload.start_date = options.startDate;\n }\n\n if (options.endDate !== undefined) {\n payload.end_date = options.endDate;\n }\n\n if (options.countryCode !== undefined) {\n payload.country_code = options.countryCode;\n }\n\n if (options.responseLength !== undefined) {\n payload.response_length = options.responseLength;\n }\n\n const response = await axios.post(\n `${this.baseUrl}/deepsearch`,\n payload,\n { headers: this.headers }\n );\n\n if (!response.status || response.status < 200 || response.status >= 300) {\n return {\n success: false,\n error: response.data?.error,\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n return response.data;\n } catch (e: any) {\n return {\n success: false,\n error: e.response?.data?.error || e.message,\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n }\n}\n\nexport type { \n SearchResponse, \n SearchType, \n FeedbackSentiment, \n FeedbackResponse,\n SearchOptions,\n CountryCode,\n ResponseLength\n} from './types'; "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkB;AAGX,IAAM,QAAN,MAAY;AAAA,EAIjB,YAAY,QAAiB,UAAkB,gCAAgC;AAC7E,QAAI,CAAC,QAAQ;AACX,eAAS,QAAQ,IAAI;AACrB,UAAI,CAAC,QAAQ;AACX,cAAM,IAAI,MAAM,0BAA0B;AAAA,MAC5C;AAAA,IACF;AACA,SAAK,UAAU;AACf,SAAK,UAAU;AAAA,MACb,gBAAgB;AAAA,MAChB,aAAa;AAAA,IACf;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKQ,mBAAmB,MAAuB;AAChD,UAAM,YAAY;AAClB,QAAI,CAAC,UAAU,KAAK,IAAI,GAAG;AACzB,aAAO;AAAA,IACT;AACA,UAAM,aAAa,IAAI,KAAK,IAAI;AAChC,WAAO,sBAAsB,QAAQ,CAAC,MAAM,WAAW,QAAQ,CAAC;AAAA,EAClE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBA,MAAM,OAAO,OAAe,UAAyB,CAAC,GAA4B;AAChF,QAAI;AAEF,YAAM,oBAAgC;AACtC,YAAM,uBAAuB;AAC7B,YAAM,oBAAoB;AAC1B,YAAM,4BAA4B;AAClC,YAAM,kBAAkB;AAGxB,UAAI,kBAA8B;AAClC,YAAM,2BAA2B,QAAQ,YAAY,YAAY;AAEjE,UAAI,6BAA6B,SAAS,6BAA6B,iBAAiB,6BAA6B,OAAO;AAC1H,0BAAkB;AAAA,MAC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AC;AAAA,QACV,mBAAmB,EAAE,KAAK,GAAG,aAAa,EAAE;AAAA,QAC5C,qBAAqB;AAAA,QACrB,yBAAyB;AAAA,QACzB,kBAAkB;AAAA,MACpB;AAAA,IACF;AAAA,EACF;AACF;","names":["axios"]}
|
package/dist/index.mjs
CHANGED
|
@@ -14,13 +14,42 @@ var Valyu = class {
|
|
|
14
14
|
"x-api-key": apiKey
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Validates date format (YYYY-MM-DD)
|
|
19
|
+
*/
|
|
20
|
+
validateDateFormat(date) {
|
|
21
|
+
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
|
|
22
|
+
if (!dateRegex.test(date)) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
const parsedDate = new Date(date);
|
|
26
|
+
return parsedDate instanceof Date && !isNaN(parsedDate.getTime());
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Search for information using the Valyu DeepSearch API
|
|
30
|
+
* @param query - The search query string
|
|
31
|
+
* @param options - Search configuration options
|
|
32
|
+
* @param options.searchType - Type of search: "web", "proprietary", or "all"
|
|
33
|
+
* @param options.maxNumResults - Maximum number of results (1-20)
|
|
34
|
+
* @param options.maxPrice - Maximum price per thousand characters (CPM)
|
|
35
|
+
* @param options.isToolCall - Whether this is a tool call
|
|
36
|
+
* @param options.relevanceThreshold - Minimum relevance score (0-1)
|
|
37
|
+
* @param options.includedSources - List of specific sources to include
|
|
38
|
+
* @param options.excludeSources - List of URLs/domains to exclude from search results
|
|
39
|
+
* @param options.category - Category filter for search results
|
|
40
|
+
* @param options.startDate - Start date filter (YYYY-MM-DD format)
|
|
41
|
+
* @param options.endDate - End date filter (YYYY-MM-DD format)
|
|
42
|
+
* @param options.countryCode - Country code filter for search results
|
|
43
|
+
* @param options.responseLength - Response content length: "short"/"medium"/"large"/"max" or integer character count
|
|
44
|
+
* @returns Promise resolving to search results
|
|
45
|
+
*/
|
|
46
|
+
async search(query, options = {}) {
|
|
18
47
|
try {
|
|
19
48
|
const defaultSearchType = "all";
|
|
20
49
|
const defaultMaxNumResults = 10;
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const defaultMaxPrice =
|
|
50
|
+
const defaultIsToolCall = true;
|
|
51
|
+
const defaultRelevanceThreshold = 0.5;
|
|
52
|
+
const defaultMaxPrice = 30;
|
|
24
53
|
let finalSearchType = defaultSearchType;
|
|
25
54
|
const providedSearchTypeString = options.searchType?.toLowerCase();
|
|
26
55
|
if (providedSearchTypeString === "web" || providedSearchTypeString === "proprietary" || providedSearchTypeString === "all") {
|
|
@@ -38,19 +67,77 @@ var Valyu = class {
|
|
|
38
67
|
total_characters: 0
|
|
39
68
|
};
|
|
40
69
|
}
|
|
70
|
+
if (options.startDate && !this.validateDateFormat(options.startDate)) {
|
|
71
|
+
return {
|
|
72
|
+
success: false,
|
|
73
|
+
error: "Invalid startDate format. Must be YYYY-MM-DD",
|
|
74
|
+
tx_id: null,
|
|
75
|
+
query,
|
|
76
|
+
results: [],
|
|
77
|
+
results_by_source: { web: 0, proprietary: 0 },
|
|
78
|
+
total_deduction_pcm: 0,
|
|
79
|
+
total_deduction_dollars: 0,
|
|
80
|
+
total_characters: 0
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
if (options.endDate && !this.validateDateFormat(options.endDate)) {
|
|
84
|
+
return {
|
|
85
|
+
success: false,
|
|
86
|
+
error: "Invalid endDate format. Must be YYYY-MM-DD",
|
|
87
|
+
tx_id: null,
|
|
88
|
+
query,
|
|
89
|
+
results: [],
|
|
90
|
+
results_by_source: { web: 0, proprietary: 0 },
|
|
91
|
+
total_deduction_pcm: 0,
|
|
92
|
+
total_deduction_dollars: 0,
|
|
93
|
+
total_characters: 0
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const maxNumResults = options.maxNumResults ?? defaultMaxNumResults;
|
|
97
|
+
if (maxNumResults < 1 || maxNumResults > 20) {
|
|
98
|
+
return {
|
|
99
|
+
success: false,
|
|
100
|
+
error: "maxNumResults must be between 1 and 20",
|
|
101
|
+
tx_id: null,
|
|
102
|
+
query,
|
|
103
|
+
results: [],
|
|
104
|
+
results_by_source: { web: 0, proprietary: 0 },
|
|
105
|
+
total_deduction_pcm: 0,
|
|
106
|
+
total_deduction_dollars: 0,
|
|
107
|
+
total_characters: 0
|
|
108
|
+
};
|
|
109
|
+
}
|
|
41
110
|
const payload = {
|
|
42
111
|
query,
|
|
43
112
|
search_type: finalSearchType,
|
|
44
|
-
max_num_results:
|
|
45
|
-
|
|
46
|
-
|
|
113
|
+
max_num_results: maxNumResults,
|
|
114
|
+
is_tool_call: options.isToolCall ?? defaultIsToolCall,
|
|
115
|
+
relevance_threshold: options.relevanceThreshold ?? defaultRelevanceThreshold,
|
|
47
116
|
max_price: options.maxPrice ?? defaultMaxPrice
|
|
48
117
|
};
|
|
49
|
-
if (options.
|
|
50
|
-
payload.
|
|
118
|
+
if (options.includedSources !== void 0) {
|
|
119
|
+
payload.included_sources = options.includedSources;
|
|
120
|
+
}
|
|
121
|
+
if (options.excludeSources !== void 0) {
|
|
122
|
+
payload.exclude_sources = options.excludeSources;
|
|
123
|
+
}
|
|
124
|
+
if (options.category !== void 0) {
|
|
125
|
+
payload.category = options.category;
|
|
126
|
+
}
|
|
127
|
+
if (options.startDate !== void 0) {
|
|
128
|
+
payload.start_date = options.startDate;
|
|
129
|
+
}
|
|
130
|
+
if (options.endDate !== void 0) {
|
|
131
|
+
payload.end_date = options.endDate;
|
|
132
|
+
}
|
|
133
|
+
if (options.countryCode !== void 0) {
|
|
134
|
+
payload.country_code = options.countryCode;
|
|
135
|
+
}
|
|
136
|
+
if (options.responseLength !== void 0) {
|
|
137
|
+
payload.response_length = options.responseLength;
|
|
51
138
|
}
|
|
52
139
|
const response = await axios.post(
|
|
53
|
-
`${this.baseUrl}/
|
|
140
|
+
`${this.baseUrl}/deepsearch`,
|
|
54
141
|
payload,
|
|
55
142
|
{ headers: this.headers }
|
|
56
143
|
);
|
|
@@ -71,7 +158,7 @@ var Valyu = class {
|
|
|
71
158
|
} catch (e) {
|
|
72
159
|
return {
|
|
73
160
|
success: false,
|
|
74
|
-
error: e.message,
|
|
161
|
+
error: e.response?.data?.error || e.message,
|
|
75
162
|
tx_id: null,
|
|
76
163
|
query,
|
|
77
164
|
results: [],
|
|
@@ -82,36 +169,6 @@ var Valyu = class {
|
|
|
82
169
|
};
|
|
83
170
|
}
|
|
84
171
|
}
|
|
85
|
-
async feedback({
|
|
86
|
-
tx_id,
|
|
87
|
-
feedback,
|
|
88
|
-
sentiment
|
|
89
|
-
}) {
|
|
90
|
-
try {
|
|
91
|
-
const payload = {
|
|
92
|
-
tx_id,
|
|
93
|
-
feedback,
|
|
94
|
-
sentiment
|
|
95
|
-
};
|
|
96
|
-
const response = await axios.post(
|
|
97
|
-
`${this.baseUrl}/feedback`,
|
|
98
|
-
payload,
|
|
99
|
-
{ headers: this.headers }
|
|
100
|
-
);
|
|
101
|
-
if (!response.status || response.status < 200 || response.status >= 300) {
|
|
102
|
-
return {
|
|
103
|
-
success: false,
|
|
104
|
-
error: response.data?.error
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
return response.data;
|
|
108
|
-
} catch (e) {
|
|
109
|
-
return {
|
|
110
|
-
success: false,
|
|
111
|
-
error: e.message
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
172
|
};
|
|
116
173
|
export {
|
|
117
174
|
Valyu
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import axios from 'axios';\nimport { SearchResponse, SearchType, FeedbackSentiment, FeedbackResponse } from './types';\n\nexport class Valyu {\n private baseUrl: string;\n private headers: Record<string, string>;\n\n constructor(apiKey?: string, baseUrl: string = \"https://api.valyu.network/v1\") {\n if (!apiKey) {\n apiKey = process.env.VALYU_API_KEY;\n if (!apiKey) {\n throw new Error(\"VALYU_API_KEY is not set\");\n }\n }\n this.baseUrl = baseUrl;\n this.headers = {\n \"Content-Type\": \"application/json\",\n \"x-api-key\": apiKey\n };\n }\n\n async context(\n query: string,\n options: {\n searchType?: string;\n maxNumResults?: number;\n queryRewrite?: boolean;\n similarityThreshold?: number;\n maxPrice?: number;\n dataSources?: string[];\n } = {}\n ): Promise<SearchResponse> {\n try {\n const defaultSearchType: SearchType = \"all\";\n const defaultMaxNumResults = 10;\n const defaultQueryRewrite = true;\n const defaultSimilarityThreshold = 0.4;\n const defaultMaxPrice = 1;\n\n let finalSearchType: SearchType = defaultSearchType;\n const providedSearchTypeString = options.searchType?.toLowerCase();\n\n if (providedSearchTypeString === \"web\" || providedSearchTypeString === \"proprietary\" || providedSearchTypeString === \"all\") {\n finalSearchType = providedSearchTypeString as SearchType;\n } else if (options.searchType !== undefined) {\n return {\n success: false,\n error: \"Invalid searchType provided. Must be one of: all, web, proprietary\",\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n const payload: Record<string, any> = {\n query,\n search_type: finalSearchType,\n max_num_results: options.maxNumResults ?? defaultMaxNumResults,\n query_rewrite: options.queryRewrite ?? defaultQueryRewrite,\n similarity_threshold: options.similarityThreshold ?? defaultSimilarityThreshold,\n max_price: options.maxPrice ?? defaultMaxPrice,\n };\n\n if (options.dataSources !== undefined) {\n payload.data_sources = options.dataSources;\n }\n\n const response = await axios.post(\n `${this.baseUrl}/knowledge`,\n payload,\n { headers: this.headers }\n );\n\n if (!response.status || response.status < 200 || response.status >= 300) {\n return {\n success: false,\n error: response.data?.error,\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n return response.data;\n } catch (e: any) {\n return {\n success: false,\n error: e.message,\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n }\n\n async feedback({\n tx_id,\n feedback,\n sentiment\n }: {\n tx_id: string;\n feedback: string;\n sentiment: FeedbackSentiment;\n }): Promise<FeedbackResponse> {\n try {\n const payload = {\n tx_id,\n feedback,\n sentiment\n };\n\n const response = await axios.post(\n `${this.baseUrl}/feedback`,\n payload,\n { headers: this.headers }\n );\n\n if (!response.status || response.status < 200 || response.status >= 300) {\n return {\n success: false,\n error: response.data?.error\n };\n }\n\n return response.data;\n } catch (e: any) {\n return {\n success: false,\n error: e.message\n };\n }\n }\n}\n\nexport type { \n SearchResponse, \n SearchType, \n FeedbackSentiment, \n FeedbackResponse \n} from './types'; "],"mappings":";AAAA,OAAO,WAAW;AAGX,IAAM,QAAN,MAAY;AAAA,EAIjB,YAAY,QAAiB,UAAkB,gCAAgC;AAC7E,QAAI,CAAC,QAAQ;AACX,eAAS,QAAQ,IAAI;AACrB,UAAI,CAAC,QAAQ;AACX,cAAM,IAAI,MAAM,0BAA0B;AAAA,MAC5C;AAAA,IACF;AACA,SAAK,UAAU;AACf,SAAK,UAAU;AAAA,MACb,gBAAgB;AAAA,MAChB,aAAa;AAAA,IACf;AAAA,EACF;AAAA,EAEA,MAAM,QACJ,OACA,UAOI,CAAC,GACoB;AACzB,QAAI;AACF,YAAM,oBAAgC;AACtC,YAAM,uBAAuB;AAC7B,YAAM,sBAAsB;AAC5B,YAAM,6BAA6B;AACnC,YAAM,kBAAkB;AAExB,UAAI,kBAA8B;AAClC,YAAM,2BAA2B,QAAQ,YAAY,YAAY;AAEjE,UAAI,6BAA6B,SAAS,6BAA6B,iBAAiB,6BAA6B,OAAO;AAC1H,0BAAkB;AAAA,MACpB,WAAW,QAAQ,eAAe,QAAW;AAC3C,eAAO;AAAA,UACL,SAAS;AAAA,UACT,OAAO;AAAA,UACP,OAAO;AAAA,UACP;AAAA,UACA,SAAS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,UAAU;AAAA,QACd;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAEA,YAAM,WAAW,MAAM,MAAM;AAAA,QAC3B,GAAG,KAAK,OAAO;AAAA,QACf;AAAA,QACA,EAAE,SAAS,KAAK,QAAQ;AAAA,MAC1B;AAEA,UAAI,CAAC,SAAS,UAAU,SAAS,SAAS,OAAO,SAAS,UAAU,KAAK;AACvE,eAAO;AAAA,UACL,SAAS;AAAA,UACT,OAAO,SAAS,MAAM;AAAA,QACxB;AAAA,MACF;AAEA,aAAO,SAAS;AAAA,IAClB,SAAS,GAAQ;AACf,aAAO;AAAA,QACL,SAAS;AAAA,QACT,OAAO,EAAE;AAAA,MACX;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import axios from 'axios';\nimport { SearchResponse, SearchType, SearchOptions } from './types';\n\nexport class Valyu {\n private baseUrl: string;\n private headers: Record<string, string>;\n\n constructor(apiKey?: string, baseUrl: string = \"https://api.valyu.network/v1\") {\n if (!apiKey) {\n apiKey = process.env.VALYU_API_KEY;\n if (!apiKey) {\n throw new Error(\"VALYU_API_KEY is not set\");\n }\n }\n this.baseUrl = baseUrl;\n this.headers = {\n \"Content-Type\": \"application/json\",\n \"x-api-key\": apiKey\n };\n }\n\n /**\n * Validates date format (YYYY-MM-DD)\n */\n private validateDateFormat(date: string): boolean {\n const dateRegex = /^\\d{4}-\\d{2}-\\d{2}$/;\n if (!dateRegex.test(date)) {\n return false;\n }\n const parsedDate = new Date(date);\n return parsedDate instanceof Date && !isNaN(parsedDate.getTime());\n }\n\n /**\n * Search for information using the Valyu DeepSearch API\n * @param query - The search query string\n * @param options - Search configuration options\n * @param options.searchType - Type of search: \"web\", \"proprietary\", or \"all\"\n * @param options.maxNumResults - Maximum number of results (1-20)\n * @param options.maxPrice - Maximum price per thousand characters (CPM)\n * @param options.isToolCall - Whether this is a tool call\n * @param options.relevanceThreshold - Minimum relevance score (0-1)\n * @param options.includedSources - List of specific sources to include\n * @param options.excludeSources - List of URLs/domains to exclude from search results\n * @param options.category - Category filter for search results\n * @param options.startDate - Start date filter (YYYY-MM-DD format)\n * @param options.endDate - End date filter (YYYY-MM-DD format)\n * @param options.countryCode - Country code filter for search results\n * @param options.responseLength - Response content length: \"short\"/\"medium\"/\"large\"/\"max\" or integer character count\n * @returns Promise resolving to search results\n */\n async search(query: string, options: SearchOptions = {}): Promise<SearchResponse> {\n try {\n // Default values\n const defaultSearchType: SearchType = \"all\";\n const defaultMaxNumResults = 10;\n const defaultIsToolCall = true;\n const defaultRelevanceThreshold = 0.5;\n const defaultMaxPrice = 30;\n\n // Validate searchType\n let finalSearchType: SearchType = defaultSearchType;\n const providedSearchTypeString = options.searchType?.toLowerCase();\n\n if (providedSearchTypeString === \"web\" || providedSearchTypeString === \"proprietary\" || providedSearchTypeString === \"all\") {\n finalSearchType = providedSearchTypeString as SearchType;\n } else if (options.searchType !== undefined) {\n return {\n success: false,\n error: \"Invalid searchType provided. Must be one of: all, web, proprietary\",\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n // Validate date formats\n if (options.startDate && !this.validateDateFormat(options.startDate)) {\n return {\n success: false,\n error: \"Invalid startDate format. Must be YYYY-MM-DD\",\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n if (options.endDate && !this.validateDateFormat(options.endDate)) {\n return {\n success: false,\n error: \"Invalid endDate format. Must be YYYY-MM-DD\",\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n // Validate maxNumResults range\n const maxNumResults = options.maxNumResults ?? defaultMaxNumResults;\n if (maxNumResults < 1 || maxNumResults > 20) {\n return {\n success: false,\n error: \"maxNumResults must be between 1 and 20\",\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n // Build payload with snake_case for API\n const payload: Record<string, any> = {\n query,\n search_type: finalSearchType,\n max_num_results: maxNumResults,\n is_tool_call: options.isToolCall ?? defaultIsToolCall,\n relevance_threshold: options.relevanceThreshold ?? defaultRelevanceThreshold,\n max_price: options.maxPrice ?? defaultMaxPrice,\n };\n\n // Add optional parameters only if provided\n if (options.includedSources !== undefined) {\n payload.included_sources = options.includedSources;\n }\n\n if (options.excludeSources !== undefined) {\n payload.exclude_sources = options.excludeSources;\n }\n\n if (options.category !== undefined) {\n payload.category = options.category;\n }\n\n if (options.startDate !== undefined) {\n payload.start_date = options.startDate;\n }\n\n if (options.endDate !== undefined) {\n payload.end_date = options.endDate;\n }\n\n if (options.countryCode !== undefined) {\n payload.country_code = options.countryCode;\n }\n\n if (options.responseLength !== undefined) {\n payload.response_length = options.responseLength;\n }\n\n const response = await axios.post(\n `${this.baseUrl}/deepsearch`,\n payload,\n { headers: this.headers }\n );\n\n if (!response.status || response.status < 200 || response.status >= 300) {\n return {\n success: false,\n error: response.data?.error,\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n\n return response.data;\n } catch (e: any) {\n return {\n success: false,\n error: e.response?.data?.error || e.message,\n tx_id: null,\n query,\n results: [],\n results_by_source: { web: 0, proprietary: 0 },\n total_deduction_pcm: 0.0,\n total_deduction_dollars: 0.0,\n total_characters: 0\n };\n }\n }\n}\n\nexport type { \n SearchResponse, \n SearchType, \n FeedbackSentiment, \n FeedbackResponse,\n SearchOptions,\n CountryCode,\n ResponseLength\n} from './types'; "],"mappings":";AAAA,OAAO,WAAW;AAGX,IAAM,QAAN,MAAY;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