exa-js 1.0.13 → 1.0.14-beta.1
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.d.ts +23 -5
- package/dist/index.js +42 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +42 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
declare const isBeta = true;
|
|
1
2
|
/**
|
|
2
3
|
* Search options for performing a search query.
|
|
3
4
|
* @typedef {Object} SearchOptions
|
|
@@ -11,6 +12,8 @@
|
|
|
11
12
|
* @property {boolean} [useAutoprompt] - If true, converts query to a Metaphor query.
|
|
12
13
|
* @property {string} [type] - Type of search, 'keyword' or 'neural'.
|
|
13
14
|
* @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.
|
|
15
|
+
* @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.
|
|
16
|
+
* @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.
|
|
14
17
|
*/
|
|
15
18
|
type BaseSearchOptions = {
|
|
16
19
|
numResults?: number;
|
|
@@ -21,11 +24,12 @@ type BaseSearchOptions = {
|
|
|
21
24
|
startPublishedDate?: string;
|
|
22
25
|
endPublishedDate?: string;
|
|
23
26
|
category?: string;
|
|
27
|
+
includeText?: string[];
|
|
28
|
+
excludeText?: string[];
|
|
24
29
|
};
|
|
25
30
|
/**
|
|
26
31
|
* Search options for performing a search query.
|
|
27
|
-
* @typedef {Object}
|
|
28
|
-
* @property {string[]} [formats] - An array of format types asked for. Currently supports `extract` (first 1000 tokens) and `text` (full parsed HTML text). If this isn't specified, defaults to `extract`.
|
|
32
|
+
* @typedef {Object} RegularSearchOptions
|
|
29
33
|
*/
|
|
30
34
|
type RegularSearchOptions = BaseSearchOptions & {
|
|
31
35
|
useAutoprompt?: boolean;
|
|
@@ -43,6 +47,8 @@ type RegularSearchOptions = BaseSearchOptions & {
|
|
|
43
47
|
* @property {string} [endPublishedDate] - End date for results based on published date.
|
|
44
48
|
* @property {boolean} [excludeSourceDomain] - If true, excludes links from the base domain of the input.
|
|
45
49
|
* @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.
|
|
50
|
+
* @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.
|
|
51
|
+
* @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.
|
|
46
52
|
*/
|
|
47
53
|
type FindSimilarOptions = BaseSearchOptions & {
|
|
48
54
|
excludeSourceDomain?: boolean;
|
|
@@ -53,12 +59,24 @@ type FindSimilarOptions = BaseSearchOptions & {
|
|
|
53
59
|
* @property {TextContentsOptions | boolean} [text] - Options for retrieving text contents.
|
|
54
60
|
* @property {HighlightsContentsOptions | boolean} [highlights] - Options for retrieving highlights.
|
|
55
61
|
* @property {SummaryContentsOptions | boolean} [summary] - Options for retrieving summary.
|
|
62
|
+
* @property {LivecrawlOptions} [livecrawl] - Options for livecrawling contents. Default is "never" for neural/auto search, "fallback" for keyword search.
|
|
63
|
+
* @property {boolean} [filterEmptyResults] - If true, filters out results with no contents. Default is true.
|
|
64
|
+
* @property {number} [livecrawlTimeout] - The timeout for livecrawling.
|
|
56
65
|
*/
|
|
57
66
|
type ContentsOptions = {
|
|
58
67
|
text?: TextContentsOptions | true;
|
|
59
68
|
highlights?: HighlightsContentsOptions | true;
|
|
60
69
|
summary?: SummaryContentsOptions | true;
|
|
61
|
-
}
|
|
70
|
+
} & (typeof isBeta extends true ? {
|
|
71
|
+
livecrawl?: LivecrawlOptions;
|
|
72
|
+
filterEmptyResults?: boolean;
|
|
73
|
+
livecrawlTimeout?: number;
|
|
74
|
+
} : {});
|
|
75
|
+
/**
|
|
76
|
+
* Options for livecrawling contents
|
|
77
|
+
* @typedef {string} LivecrawlOptions
|
|
78
|
+
*/
|
|
79
|
+
type LivecrawlOptions = "never" | "fallback" | "always";
|
|
62
80
|
/**
|
|
63
81
|
* Options for retrieving text from page.
|
|
64
82
|
* @typedef {Object} TextContentsOptions
|
|
@@ -119,7 +137,7 @@ type Default<T extends {}, U> = [keyof T] extends [never] ? U : T;
|
|
|
119
137
|
*
|
|
120
138
|
* @template T - A type extending from 'ContentsOptions'.
|
|
121
139
|
*/
|
|
122
|
-
type ContentsResultComponent<T extends ContentsOptions> = Default<(T[
|
|
140
|
+
type ContentsResultComponent<T extends ContentsOptions> = Default<(T["text"] extends object | true ? TextResponse : {}) & (T["highlights"] extends object | true ? HighlightsResponse : {}) & (T["summary"] extends object | true ? SummaryResponse : {}), TextResponse>;
|
|
123
141
|
/**
|
|
124
142
|
* Represents a search result object.
|
|
125
143
|
* @typedef {Object} Result
|
|
@@ -205,4 +223,4 @@ declare class Exa {
|
|
|
205
223
|
getContents<T extends ContentsOptions>(ids: string | string[] | SearchResult[], options?: T): Promise<SearchResponse<T>>;
|
|
206
224
|
}
|
|
207
225
|
|
|
208
|
-
export { BaseSearchOptions, ContentsOptions, ContentsResultComponent, Default, FindSimilarOptions, HighlightsContentsOptions, HighlightsResponse, RegularSearchOptions, SearchResponse, SearchResult, SummaryContentsOptions, SummaryResponse, TextContentsOptions, TextResponse, Exa as default };
|
|
226
|
+
export { BaseSearchOptions, ContentsOptions, ContentsResultComponent, Default, FindSimilarOptions, HighlightsContentsOptions, HighlightsResponse, LivecrawlOptions, RegularSearchOptions, SearchResponse, SearchResult, SummaryContentsOptions, SummaryResponse, TextContentsOptions, TextResponse, Exa as default };
|
package/dist/index.js
CHANGED
|
@@ -34,6 +34,7 @@ __export(src_exports, {
|
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(src_exports);
|
|
36
36
|
var import_cross_fetch = __toESM(require("cross-fetch"));
|
|
37
|
+
var isBeta = true;
|
|
37
38
|
var Exa = class {
|
|
38
39
|
/**
|
|
39
40
|
* Constructs the Exa API client.
|
|
@@ -45,7 +46,9 @@ var Exa = class {
|
|
|
45
46
|
if (!apiKey) {
|
|
46
47
|
apiKey = process.env.EXASEARCH_API_KEY;
|
|
47
48
|
if (!apiKey) {
|
|
48
|
-
throw new Error(
|
|
49
|
+
throw new Error(
|
|
50
|
+
"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)"
|
|
51
|
+
);
|
|
49
52
|
}
|
|
50
53
|
}
|
|
51
54
|
this.headers = new import_cross_fetch.Headers({
|
|
@@ -94,10 +97,22 @@ var Exa = class {
|
|
|
94
97
|
const { text, highlights, summary, ...rest } = options || {};
|
|
95
98
|
return await this.request("/search", "POST", {
|
|
96
99
|
query,
|
|
97
|
-
contents: !text && !highlights && !summary ? {
|
|
100
|
+
contents: !text && !highlights && !summary ? {
|
|
101
|
+
text: true,
|
|
102
|
+
...isBeta ? {
|
|
103
|
+
livecrawl: options?.livecrawl,
|
|
104
|
+
filterEmptyResults: options?.filterEmptyResults,
|
|
105
|
+
livecrawlTimeout: options?.livecrawlTimeout
|
|
106
|
+
} : {}
|
|
107
|
+
} : {
|
|
98
108
|
...text ? { text } : {},
|
|
99
109
|
...highlights ? { highlights } : {},
|
|
100
|
-
...summary ? { summary } : {}
|
|
110
|
+
...summary ? { summary } : {},
|
|
111
|
+
...isBeta ? {
|
|
112
|
+
livecrawl: options?.livecrawl,
|
|
113
|
+
filterEmptyResults: options?.filterEmptyResults,
|
|
114
|
+
livecrawlTimeout: options?.livecrawlTimeout
|
|
115
|
+
} : {}
|
|
101
116
|
},
|
|
102
117
|
...rest
|
|
103
118
|
});
|
|
@@ -121,10 +136,22 @@ var Exa = class {
|
|
|
121
136
|
const { text, highlights, summary, ...rest } = options || {};
|
|
122
137
|
return await this.request("/findSimilar", "POST", {
|
|
123
138
|
url,
|
|
124
|
-
contents: !text && !highlights && !summary ? {
|
|
139
|
+
contents: !text && !highlights && !summary ? {
|
|
140
|
+
text: true,
|
|
141
|
+
...isBeta ? {
|
|
142
|
+
livecrawl: options?.livecrawl,
|
|
143
|
+
filterEmptyResults: options?.filterEmptyResults,
|
|
144
|
+
livecrawlTimeout: options?.livecrawlTimeout
|
|
145
|
+
} : {}
|
|
146
|
+
} : {
|
|
125
147
|
...text ? { text } : {},
|
|
126
148
|
...highlights ? { highlights } : {},
|
|
127
|
-
...summary ? { summary } : {}
|
|
149
|
+
...summary ? { summary } : {},
|
|
150
|
+
...isBeta ? {
|
|
151
|
+
livecrawl: options?.livecrawl,
|
|
152
|
+
filterEmptyResults: options?.filterEmptyResults,
|
|
153
|
+
livecrawlTimeout: options?.livecrawlTimeout
|
|
154
|
+
} : {}
|
|
128
155
|
},
|
|
129
156
|
...rest
|
|
130
157
|
});
|
|
@@ -136,6 +163,7 @@ var Exa = class {
|
|
|
136
163
|
* @returns {Promise<GetContentsResponse>} A list of document contents.
|
|
137
164
|
*/
|
|
138
165
|
async getContents(ids, options) {
|
|
166
|
+
const { livecrawl, filterEmptyResults, ...rest } = options || {};
|
|
139
167
|
if (ids.length === 0) {
|
|
140
168
|
throw new Error("Must provide at least one ID");
|
|
141
169
|
}
|
|
@@ -147,7 +175,15 @@ var Exa = class {
|
|
|
147
175
|
} else {
|
|
148
176
|
requestIds = ids.map((result) => result.id);
|
|
149
177
|
}
|
|
150
|
-
return await this.request(`/contents`, "POST", {
|
|
178
|
+
return await this.request(`/contents`, "POST", {
|
|
179
|
+
ids: requestIds,
|
|
180
|
+
...isBeta ? {
|
|
181
|
+
livecrawl: options?.livecrawl,
|
|
182
|
+
filterEmptyResults: options?.filterEmptyResults,
|
|
183
|
+
livecrawlTimeout: options?.livecrawlTimeout
|
|
184
|
+
} : {},
|
|
185
|
+
...rest
|
|
186
|
+
});
|
|
151
187
|
}
|
|
152
188
|
};
|
|
153
189
|
var src_default = Exa;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import fetch, { Headers } from 'cross-fetch';\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} SearchOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude in the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [useAutoprompt] - If true, converts query to a Metaphor query.\n * @property {string} [type] - Type of search, 'keyword' or 'neural'.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n */\nexport type BaseSearchOptions = {\n numResults?: number;\n includeDomains?: string[];\n excludeDomains?: string[];\n startCrawlDate?: string;\n endCrawlDate?: string;\n startPublishedDate?: string;\n endPublishedDate?: string;\n category?: string;\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {string[]} [formats] - An array of format types asked for. Currently supports `extract` (first 1000 tokens) and `text` (full parsed HTML text). If this isn't specified, defaults to `extract`.\n */\nexport type RegularSearchOptions = BaseSearchOptions & {\n useAutoprompt?: boolean;\n type?: string;\n}\n\n/**\n * Options for finding similar links.\n * @typedef {Object} FindSimilarOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude from the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [excludeSourceDomain] - If true, excludes links from the base domain of the input.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n */\nexport type FindSimilarOptions = BaseSearchOptions & {\n excludeSourceDomain?: boolean;\n}\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {TextContentsOptions | boolean} [text] - Options for retrieving text contents.\n * @property {HighlightsContentsOptions | boolean} [highlights] - Options for retrieving highlights.\n * @property {SummaryContentsOptions | boolean} [summary] - Options for retrieving summary.\n */\nexport type ContentsOptions = {\n text?: TextContentsOptions | true;\n highlights?: HighlightsContentsOptions | true;\n summary?: SummaryContentsOptions | true;\n};\n\n/**\n * Options for retrieving text from page.\n * @typedef {Object} TextContentsOptions\n * @property {number} [maxCharacters] - The maximum number of characters to return.\n * @property {boolean} [includeHtmlTags] - If true, includes HTML tags in the returned text. Default: false\n */\nexport type TextContentsOptions = {\n maxCharacters?: number;\n includeHtmlTags?: boolean;\n}\n\n/**\n * Options for retrieving highlights from page.\n * @typedef {Object} HighlightsContentsOptions\n * @property {string} [query] - The query string to use for highlights search.\n * @property {number} [numSentences] - The number of sentences to return for each highlight.\n * @property {number} [highlightsPerUrl] - The number of highlights to return for each URL.\n */\nexport type HighlightsContentsOptions = {\n query?: string;\n numSentences?: number;\n highlightsPerUrl?: number;\n}\n\n/**\n * Options for retrieving summary from page.\n * @typedef {Object} SummaryContentsOptions\n * @property {string} [query] - The query string to use for summary generation.\n */\nexport type SummaryContentsOptions = {\n query?: string;\n}\n\n/**\n * @typedef {Object} TextResponse\n * @property {string} text - Text from page\n */\nexport type TextResponse = { text: string };\n\n/**\n * @typedef {Object} HighlightsResponse\n * @property {string[]} highlights - The highlights as an array of strings.\n * @property {number[]} highlightScores - The corresponding scores as an array of floats, 0 to 1\n */\nexport type HighlightsResponse = { highlights: string[], highlightScores: number[] };\n\n/**\n * @typedef {Object} SummaryResponse\n * @property {string} summary - The generated summary of the page content.\n */\nexport type SummaryResponse = { summary: string };\n\nexport type Default<T extends {}, U> = [keyof T] extends [never] ? U : T;\n\n/**\n * @typedef {Object} ContentsResultComponent\n * Depending on 'ContentsOptions', this yields a combination of 'TextResponse', 'HighlightsResponse', 'SummaryResponse', or an empty object.\n *\n * @template T - A type extending from 'ContentsOptions'.\n */\nexport type ContentsResultComponent<T extends ContentsOptions> =\n Default<(T['text'] extends (object | true) ? TextResponse : {}) &\n (T['highlights'] extends (object | true) ? HighlightsResponse : {}) &\n (T['summary'] extends (object | true) ? SummaryResponse : {}), TextResponse>\n\n/**\n * Represents a search result object.\n * @typedef {Object} Result\n * @property {string} title - The title of the search result.\n * @property {string} url - The URL of the search result.\n * @property {string} [publishedDate] - The estimated creation date of the content.\n * @property {string} [author] - The author of the content, if available.\n * @property {number} [score] - Similarity score between the query/url and the result.\n * @property {string} id - The temporary ID for the document.\n */\nexport type SearchResult<T extends ContentsOptions = {}> = {\n title: string | null;\n url: string;\n publishedDate?: string;\n author?: string;\n score?: number;\n id: string;\n} & ContentsResultComponent<T>;\n\n/**\n * Represents a search response object.\n * @typedef {Object} SearchResponse\n * @property {Result[]} results - The list of search results.\n * @property {string} [autopromptString] - The autoprompt string, if applicable.\n */\nexport type SearchResponse<T extends ContentsOptions = {}> = {\n results: SearchResult<T>[];\n autopromptString?: string;\n}\n\n/**\n * The Exa class encapsulates the API's endpoints.\n */\nclass Exa {\n private baseURL: string;\n private headers: Headers;\n\n /**\n * Constructs the Exa API client.\n * @param {string} apiKey - The API key for authentication.\n * @param {string} [baseURL] - The base URL of the Exa API.\n */\n constructor(\n apiKey?: string,\n baseURL: string = \"https://api.exa.ai\"\n ) {\n this.baseURL = baseURL;\n if (!apiKey) {\n apiKey = process.env.EXASEARCH_API_KEY;\n if (!apiKey) {\n throw new Error(\"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)\");\n }\n }\n this.headers = new Headers({\n \"x-api-key\": apiKey,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": \"exa-node 1.0.27\",\n });\n }\n\n /**\n * Makes a request to the Exa API.\n * @param {string} endpoint - The API endpoint to call.\n * @param {string} method - The HTTP method to use.\n * @param {any} [body] - The request body for POST requests.\n * @returns {Promise<any>} The response from the API.\n */\n private async request(\n endpoint: string,\n method: string,\n body?: any\n ): Promise<any> {\n const response = await fetch(this.baseURL + endpoint, {\n method,\n headers: this.headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n const message = (await response.json()).error;\n throw new Error(\n `Request failed with status ${response.status}. ${message}`\n );\n }\n\n return await response.json();\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async search(query: string, options?: RegularSearchOptions): Promise<SearchResponse> {\n return await this.request(\"/search\", 'POST', { query, ...options });\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query and returns the contents of the documents.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async searchAndContents<T extends ContentsOptions>(query: string, options?: RegularSearchOptions & T): Promise<SearchResponse<T>> {\n const { text, highlights, summary, ...rest } = options || {};\n return await this.request(\"/search\", 'POST', {\n query,\n contents: (!text && !highlights && !summary) ? { text: true } : {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {}),\n ...(summary ? { summary } : {})\n },\n ...rest\n });\n }\n\n /**\n * Finds similar links to the provided URL.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilar(\n url: string,\n options?: FindSimilarOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/findSimilar\", 'POST', { url, ...options });\n }\n\n /**\n * Finds similar links to the provided URL and returns the contents of the documents.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilarAndContents<T extends ContentsOptions>(url: string, options?: FindSimilarOptions & T): Promise<SearchResponse<T>> {\n const { text, highlights, summary, ...rest } = options || {};\n return await this.request(\"/findSimilar\", 'POST', {\n url,\n contents: (!text && !highlights && !summary) ? { text: true } : {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {}),\n ...(summary ? { summary } : {})\n },\n ...rest\n });\n }\n\n /**\n * Retrieves contents of documents based on a list of document IDs.\n * @param {string | string[] | SearchResult[]} ids - An array of document IDs.\n * @param {ContentsOptions} [options] - Additional options for retrieving document contents.\n * @returns {Promise<GetContentsResponse>} A list of document contents.\n */\n async getContents<T extends ContentsOptions>(ids: string | string[] | SearchResult[], options?: T): Promise<SearchResponse<T>> {\n if (ids.length === 0) {\n throw new Error(\"Must provide at least one ID\");\n }\n let requestIds: string[];\n if (typeof ids === \"string\") {\n requestIds = [ids];\n } else if (typeof ids[0] === \"string\") {\n requestIds = ids as string[];\n } else {\n requestIds = (ids as SearchResult[]).map((result) => result.id);\n }\n return await this.request(`/contents`, 'POST', { ids: requestIds, ...options, });\n }\n}\n\nexport default Exa;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAA+B;AAqK/B,IAAM,MAAN,MAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASR,YACE,QACA,UAAkB,sBAClB;AACA,SAAK,UAAU;AACf,QAAI,CAAC,QAAQ;AACX,eAAS,QAAQ,IAAI;AACrB,UAAI,CAAC,QAAQ;AACX,cAAM,IAAI,MAAM,2FAA2F;AAAA,MAC7G;AAAA,IACF;AACA,SAAK,UAAU,IAAI,2BAAQ;AAAA,MACzB,aAAa;AAAA,MACb,gBAAgB;AAAA,MAChB,cAAc;AAAA,IAChB,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAc,QACZ,UACA,QACA,MACc;AACd,UAAM,WAAW,UAAM,mBAAAA,SAAM,KAAK,UAAU,UAAU;AAAA,MACpD;AAAA,MACA,SAAS,KAAK;AAAA,MACd,MAAM,OAAO,KAAK,UAAU,IAAI,IAAI;AAAA,IACtC,C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AC,cAAc,CAAC,UAAW,EAAE,MAAM,KAAK,IAAI;AAAA,QAC9D,GAAI,OAAO,EAAE,KAAK,IAAI,CAAC;AAAA,QACvB,GAAI,aAAa,EAAE,WAAW,IAAI,CAAC;AAAA,QACnC,GAAI,UAAU,EAAE,QAAQ,IAAI,CAAC;AAAA,MAC/B;AAAA,MACA,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,YAAuC,KAAyC,SAAyC;AAC7H,QAAI,IAAI,WAAW,GAAG;AACpB,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAChD;AACA,QAAI;AACJ,QAAI,OAAO,QAAQ,UAAU;AAC3B,mBAAa,CAAC,GAAG;AAAA,IACnB,WAAW,OAAO,IAAI,CAAC,MAAM,UAAU;AACrC,mBAAa;AAAA,IACf,OAAO;AACL,mBAAc,IAAuB,IAAI,CAAC,WAAW,OAAO,EAAE;AAAA,IAChE;AACA,WAAO,MAAM,KAAK,QAAQ,aAAa,QAAQ,EAAE,KAAK,YAAY,GAAG,QAAS,CAAC;AAAA,EACjF;AACF;AAEA,IAAO,cAAQ;","names":["fetch"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import fetch, { Headers } from \"cross-fetch\";\n\nconst isBeta = true;\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} SearchOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude in the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [useAutoprompt] - If true, converts query to a Metaphor query.\n * @property {string} [type] - Type of search, 'keyword' or 'neural'.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n * @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n * @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n */\nexport type BaseSearchOptions = {\n numResults?: number;\n includeDomains?: string[];\n excludeDomains?: string[];\n startCrawlDate?: string;\n endCrawlDate?: string;\n startPublishedDate?: string;\n endPublishedDate?: string;\n category?: string;\n includeText?: string[];\n excludeText?: string[];\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} RegularSearchOptions\n */\nexport type RegularSearchOptions = BaseSearchOptions & {\n useAutoprompt?: boolean;\n type?: string;\n};\n\n/**\n * Options for finding similar links.\n * @typedef {Object} FindSimilarOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude from the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [excludeSourceDomain] - If true, excludes links from the base domain of the input.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n * @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n * @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n */\nexport type FindSimilarOptions = BaseSearchOptions & {\n excludeSourceDomain?: boolean;\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {TextContentsOptions | boolean} [text] - Options for retrieving text contents.\n * @property {HighlightsContentsOptions | boolean} [highlights] - Options for retrieving highlights.\n * @property {SummaryContentsOptions | boolean} [summary] - Options for retrieving summary.\n * @property {LivecrawlOptions} [livecrawl] - Options for livecrawling contents. Default is \"never\" for neural/auto search, \"fallback\" for keyword search.\n * @property {boolean} [filterEmptyResults] - If true, filters out results with no contents. Default is true.\n * @property {number} [livecrawlTimeout] - The timeout for livecrawling.\n */\nexport type ContentsOptions = {\n text?: TextContentsOptions | true;\n highlights?: HighlightsContentsOptions | true;\n summary?: SummaryContentsOptions | true;\n} & (typeof isBeta extends true\n ? {\n livecrawl?: LivecrawlOptions;\n filterEmptyResults?: boolean;\n livecrawlTimeout?: number;\n }\n : {});\n\n/**\n * Options for livecrawling contents\n * @typedef {string} LivecrawlOptions\n */\nexport type LivecrawlOptions = \"never\" | \"fallback\" | \"always\";\n\n/**\n * Options for retrieving text from page.\n * @typedef {Object} TextContentsOptions\n * @property {number} [maxCharacters] - The maximum number of characters to return.\n * @property {boolean} [includeHtmlTags] - If true, includes HTML tags in the returned text. Default: false\n */\nexport type TextContentsOptions = {\n maxCharacters?: number;\n includeHtmlTags?: boolean;\n};\n\n/**\n * Options for retrieving highlights from page.\n * @typedef {Object} HighlightsContentsOptions\n * @property {string} [query] - The query string to use for highlights search.\n * @property {number} [numSentences] - The number of sentences to return for each highlight.\n * @property {number} [highlightsPerUrl] - The number of highlights to return for each URL.\n */\nexport type HighlightsContentsOptions = {\n query?: string;\n numSentences?: number;\n highlightsPerUrl?: number;\n};\n\n/**\n * Options for retrieving summary from page.\n * @typedef {Object} SummaryContentsOptions\n * @property {string} [query] - The query string to use for summary generation.\n */\nexport type SummaryContentsOptions = {\n query?: string;\n};\n\n/**\n * @typedef {Object} TextResponse\n * @property {string} text - Text from page\n */\nexport type TextResponse = { text: string };\n\n/**\n * @typedef {Object} HighlightsResponse\n * @property {string[]} highlights - The highlights as an array of strings.\n * @property {number[]} highlightScores - The corresponding scores as an array of floats, 0 to 1\n */\nexport type HighlightsResponse = {\n highlights: string[];\n highlightScores: number[];\n};\n\n/**\n * @typedef {Object} SummaryResponse\n * @property {string} summary - The generated summary of the page content.\n */\nexport type SummaryResponse = { summary: string };\n\nexport type Default<T extends {}, U> = [keyof T] extends [never] ? U : T;\n\n/**\n * @typedef {Object} ContentsResultComponent\n * Depending on 'ContentsOptions', this yields a combination of 'TextResponse', 'HighlightsResponse', 'SummaryResponse', or an empty object.\n *\n * @template T - A type extending from 'ContentsOptions'.\n */\nexport type ContentsResultComponent<T extends ContentsOptions> = Default<\n (T[\"text\"] extends object | true ? TextResponse : {}) &\n (T[\"highlights\"] extends object | true ? HighlightsResponse : {}) &\n (T[\"summary\"] extends object | true ? SummaryResponse : {}),\n TextResponse\n>;\n\n/**\n * Represents a search result object.\n * @typedef {Object} Result\n * @property {string} title - The title of the search result.\n * @property {string} url - The URL of the search result.\n * @property {string} [publishedDate] - The estimated creation date of the content.\n * @property {string} [author] - The author of the content, if available.\n * @property {number} [score] - Similarity score between the query/url and the result.\n * @property {string} id - The temporary ID for the document.\n */\nexport type SearchResult<T extends ContentsOptions = {}> = {\n title: string | null;\n url: string;\n publishedDate?: string;\n author?: string;\n score?: number;\n id: string;\n} & ContentsResultComponent<T>;\n\n/**\n * Represents a search response object.\n * @typedef {Object} SearchResponse\n * @property {Result[]} results - The list of search results.\n * @property {string} [autopromptString] - The autoprompt string, if applicable.\n */\nexport type SearchResponse<T extends ContentsOptions = {}> = {\n results: SearchResult<T>[];\n autopromptString?: string;\n};\n\n/**\n * The Exa class encapsulates the API's endpoints.\n */\nclass Exa {\n private baseURL: string;\n private headers: Headers;\n\n /**\n * Constructs the Exa API client.\n * @param {string} apiKey - The API key for authentication.\n * @param {string} [baseURL] - The base URL of the Exa API.\n */\n constructor(apiKey?: string, baseURL: string = \"https://api.exa.ai\") {\n this.baseURL = baseURL;\n if (!apiKey) {\n apiKey = process.env.EXASEARCH_API_KEY;\n if (!apiKey) {\n throw new Error(\n \"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)\"\n );\n }\n }\n this.headers = new Headers({\n \"x-api-key\": apiKey,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": \"exa-node 1.0.27\",\n });\n }\n\n /**\n * Makes a request to the Exa API.\n * @param {string} endpoint - The API endpoint to call.\n * @param {string} method - The HTTP method to use.\n * @param {any} [body] - The request body for POST requests.\n * @returns {Promise<any>} The response from the API.\n */\n private async request(\n endpoint: string,\n method: string,\n body?: any\n ): Promise<any> {\n const response = await fetch(this.baseURL + endpoint, {\n method,\n headers: this.headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n const message = (await response.json()).error;\n throw new Error(\n `Request failed with status ${response.status}. ${message}`\n );\n }\n\n return await response.json();\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async search(\n query: string,\n options?: RegularSearchOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/search\", \"POST\", { query, ...options });\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query and returns the contents of the documents.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async searchAndContents<T extends ContentsOptions>(\n query: string,\n options?: RegularSearchOptions & T\n ): Promise<SearchResponse<T>> {\n const { text, highlights, summary, ...rest } = options || {};\n return await this.request(\"/search\", \"POST\", {\n query,\n contents:\n !text && !highlights && !summary\n ? {\n text: true,\n ...(isBeta\n ? {\n livecrawl: options?.livecrawl,\n filterEmptyResults: options?.filterEmptyResults,\n livecrawlTimeout: options?.livecrawlTimeout,\n }\n : {}),\n }\n : {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {}),\n ...(summary ? { summary } : {}),\n ...(isBeta\n ? {\n livecrawl: options?.livecrawl,\n filterEmptyResults: options?.filterEmptyResults,\n livecrawlTimeout: options?.livecrawlTimeout,\n }\n : {}),\n },\n ...rest,\n });\n }\n\n /**\n * Finds similar links to the provided URL.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilar(\n url: string,\n options?: FindSimilarOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/findSimilar\", \"POST\", { url, ...options });\n }\n\n /**\n * Finds similar links to the provided URL and returns the contents of the documents.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilarAndContents<T extends ContentsOptions>(\n url: string,\n options?: FindSimilarOptions & T\n ): Promise<SearchResponse<T>> {\n const { text, highlights, summary, ...rest } = options || {};\n return await this.request(\"/findSimilar\", \"POST\", {\n url,\n contents:\n !text && !highlights && !summary\n ? {\n text: true,\n ...(isBeta\n ? {\n livecrawl: options?.livecrawl,\n filterEmptyResults: options?.filterEmptyResults,\n livecrawlTimeout: options?.livecrawlTimeout,\n }\n : {}),\n }\n : {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {}),\n ...(summary ? { summary } : {}),\n ...(isBeta\n ? {\n livecrawl: options?.livecrawl,\n filterEmptyResults: options?.filterEmptyResults,\n livecrawlTimeout: options?.livecrawlTimeout,\n }\n : {}),\n },\n ...rest,\n });\n }\n\n /**\n * Retrieves contents of documents based on a list of document IDs.\n * @param {string | string[] | SearchResult[]} ids - An array of document IDs.\n * @param {ContentsOptions} [options] - Additional options for retrieving document contents.\n * @returns {Promise<GetContentsResponse>} A list of document contents.\n */\n async getContents<T extends ContentsOptions>(\n ids: string | string[] | SearchResult[],\n options?: T\n ): Promise<SearchResponse<T>> {\n const { livecrawl, filterEmptyResults, ...rest } = options || {};\n if (ids.length === 0) {\n throw new Error(\"Must provide at least one ID\");\n }\n let requestIds: string[];\n if (typeof ids === \"string\") {\n requestIds = [ids];\n } else if (typeof ids[0] === \"string\") {\n requestIds = ids as string[];\n } else {\n requestIds = (ids as SearchResult[]).map((result) => result.id);\n }\n return await this.request(`/contents`, \"POST\", {\n ids: requestIds,\n ...(isBeta\n ? {\n livecrawl: options?.livecrawl,\n filterEmptyResults: options?.filterEmptyResults,\n livecrawlTimeout: options?.livecrawlTimeout,\n }\n : {}),\n ...rest,\n });\n }\n}\n\nexport default Exa;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAA+B;AAE/B,IAAM,SAAS;AA8Lf,IAAM,MAAN,MAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASR,YAAY,QAAiB,UAAkB,sBAAsB;AACnE,SAAK,UAAU;AACf,QAAI,CAAC,QAAQ;AACX,eAAS,QAAQ,IAAI;AACrB,UAAI,CAAC,QAAQ;AACX,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,SAAK,UAAU,IAAI,2BAAQ;AAAA,MACzB,aAAa;AAAA,MACb,gBAAgB;AAAA,MAChB,cAAc;AAAA,IAChB,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAc,QACZ,UACA,QACA,MACc;AACd,UAAM,WAAW,UAAM,mBAAAA,SAAM,KAAK,UAAU,UAAU;AAAA,MACpD;AAAA,MACA,SAAS,KAAK;AAAA,MACd,MAAM,OAAO,KAAK,UAAU,IAAI,IAAI;AAAA,IACtC,CAAC;AAED,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,WAAW,MAAM,SAAS,KAAK,GAAG;AACxC,YAAM,IAAI;AAAA,QACR,8BAA8B,SAAS,WAAW;AAAA,MACpD;AAAA,IACF;AAEA,WAAO,MAAM,SAAS,KAAK;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,OACJ,OACA,SACyB;AACzB,WAAO,MAAM,KAAK,QAAQ,WAAW,QAAQ,EAAE,OAAO,GAAG,QAAQ,CAAC;AAAA,EACpE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,kBACJ,OACA,SAC4B;AAC5B,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,GAAI,OAAO,EAAE,KAAK,IAAI,CAAC;AAAA,QACvB,GAAI,aAAa,EAAE,WAAW,IAAI,CAAC;AAAA,QACnC,GAAI,UAAU,EAAE,QAAQ,IAAI,CAAC;AAAA,QAC7B,GAAI,SACA;AAAA,UACE,WAAW,SAAS;AAAA,UACpB,oBAAoB,SAAS;AAAA,UAC7B,kBAAkB,SAAS;AAAA,QAC7B,IACA,CAAC;AAAA,MACP;AAAA,MACN,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,YACJ,KACA,SAC4B;AAC5B,UAAM,EAAE,WAAW,oBAAoB,GAAG,KAAK,IAAI,WAAW,CAAC;AAC/D,QAAI,IAAI,WAAW,GAAG;AACpB,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAChD;AACA,QAAI;AACJ,QAAI,OAAO,QAAQ,UAAU;AAC3B,mBAAa,CAAC,GAAG;AAAA,IACnB,WAAW,OAAO,IAAI,CAAC,MAAM,UAAU;AACrC,mBAAa;AAAA,IACf,OAAO;AACL,mBAAc,IAAuB,IAAI,CAAC,WAAW,OAAO,EAAE;AAAA,IAChE;AACA,WAAO,MAAM,KAAK,QAAQ,aAAa,QAAQ;AAAA,MAC7C,KAAK;AAAA,MACL,GAAI,SACA;AAAA,QACE,WAAW,SAAS;AAAA,QACpB,oBAAoB,SAAS;AAAA,QAC7B,kBAAkB,SAAS;AAAA,MAC7B,IACA,CAAC;AAAA,MACL,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AACF;AAEA,IAAO,cAAQ;","names":["fetch"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import fetch, { Headers } from "cross-fetch";
|
|
3
|
+
var isBeta = true;
|
|
3
4
|
var Exa = class {
|
|
4
5
|
/**
|
|
5
6
|
* Constructs the Exa API client.
|
|
@@ -11,7 +12,9 @@ var Exa = class {
|
|
|
11
12
|
if (!apiKey) {
|
|
12
13
|
apiKey = process.env.EXASEARCH_API_KEY;
|
|
13
14
|
if (!apiKey) {
|
|
14
|
-
throw new Error(
|
|
15
|
+
throw new Error(
|
|
16
|
+
"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)"
|
|
17
|
+
);
|
|
15
18
|
}
|
|
16
19
|
}
|
|
17
20
|
this.headers = new Headers({
|
|
@@ -60,10 +63,22 @@ var Exa = class {
|
|
|
60
63
|
const { text, highlights, summary, ...rest } = options || {};
|
|
61
64
|
return await this.request("/search", "POST", {
|
|
62
65
|
query,
|
|
63
|
-
contents: !text && !highlights && !summary ? {
|
|
66
|
+
contents: !text && !highlights && !summary ? {
|
|
67
|
+
text: true,
|
|
68
|
+
...isBeta ? {
|
|
69
|
+
livecrawl: options?.livecrawl,
|
|
70
|
+
filterEmptyResults: options?.filterEmptyResults,
|
|
71
|
+
livecrawlTimeout: options?.livecrawlTimeout
|
|
72
|
+
} : {}
|
|
73
|
+
} : {
|
|
64
74
|
...text ? { text } : {},
|
|
65
75
|
...highlights ? { highlights } : {},
|
|
66
|
-
...summary ? { summary } : {}
|
|
76
|
+
...summary ? { summary } : {},
|
|
77
|
+
...isBeta ? {
|
|
78
|
+
livecrawl: options?.livecrawl,
|
|
79
|
+
filterEmptyResults: options?.filterEmptyResults,
|
|
80
|
+
livecrawlTimeout: options?.livecrawlTimeout
|
|
81
|
+
} : {}
|
|
67
82
|
},
|
|
68
83
|
...rest
|
|
69
84
|
});
|
|
@@ -87,10 +102,22 @@ var Exa = class {
|
|
|
87
102
|
const { text, highlights, summary, ...rest } = options || {};
|
|
88
103
|
return await this.request("/findSimilar", "POST", {
|
|
89
104
|
url,
|
|
90
|
-
contents: !text && !highlights && !summary ? {
|
|
105
|
+
contents: !text && !highlights && !summary ? {
|
|
106
|
+
text: true,
|
|
107
|
+
...isBeta ? {
|
|
108
|
+
livecrawl: options?.livecrawl,
|
|
109
|
+
filterEmptyResults: options?.filterEmptyResults,
|
|
110
|
+
livecrawlTimeout: options?.livecrawlTimeout
|
|
111
|
+
} : {}
|
|
112
|
+
} : {
|
|
91
113
|
...text ? { text } : {},
|
|
92
114
|
...highlights ? { highlights } : {},
|
|
93
|
-
...summary ? { summary } : {}
|
|
115
|
+
...summary ? { summary } : {},
|
|
116
|
+
...isBeta ? {
|
|
117
|
+
livecrawl: options?.livecrawl,
|
|
118
|
+
filterEmptyResults: options?.filterEmptyResults,
|
|
119
|
+
livecrawlTimeout: options?.livecrawlTimeout
|
|
120
|
+
} : {}
|
|
94
121
|
},
|
|
95
122
|
...rest
|
|
96
123
|
});
|
|
@@ -102,6 +129,7 @@ var Exa = class {
|
|
|
102
129
|
* @returns {Promise<GetContentsResponse>} A list of document contents.
|
|
103
130
|
*/
|
|
104
131
|
async getContents(ids, options) {
|
|
132
|
+
const { livecrawl, filterEmptyResults, ...rest } = options || {};
|
|
105
133
|
if (ids.length === 0) {
|
|
106
134
|
throw new Error("Must provide at least one ID");
|
|
107
135
|
}
|
|
@@ -113,7 +141,15 @@ var Exa = class {
|
|
|
113
141
|
} else {
|
|
114
142
|
requestIds = ids.map((result) => result.id);
|
|
115
143
|
}
|
|
116
|
-
return await this.request(`/contents`, "POST", {
|
|
144
|
+
return await this.request(`/contents`, "POST", {
|
|
145
|
+
ids: requestIds,
|
|
146
|
+
...isBeta ? {
|
|
147
|
+
livecrawl: options?.livecrawl,
|
|
148
|
+
filterEmptyResults: options?.filterEmptyResults,
|
|
149
|
+
livecrawlTimeout: options?.livecrawlTimeout
|
|
150
|
+
} : {},
|
|
151
|
+
...rest
|
|
152
|
+
});
|
|
117
153
|
}
|
|
118
154
|
};
|
|
119
155
|
var src_default = Exa;
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import fetch, { Headers } from 'cross-fetch';\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} SearchOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude in the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [useAutoprompt] - If true, converts query to a Metaphor query.\n * @property {string} [type] - Type of search, 'keyword' or 'neural'.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n */\nexport type BaseSearchOptions = {\n numResults?: number;\n includeDomains?: string[];\n excludeDomains?: string[];\n startCrawlDate?: string;\n endCrawlDate?: string;\n startPublishedDate?: string;\n endPublishedDate?: string;\n category?: string;\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {string[]} [formats] - An array of format types asked for. Currently supports `extract` (first 1000 tokens) and `text` (full parsed HTML text). If this isn't specified, defaults to `extract`.\n */\nexport type RegularSearchOptions = BaseSearchOptions & {\n useAutoprompt?: boolean;\n type?: string;\n}\n\n/**\n * Options for finding similar links.\n * @typedef {Object} FindSimilarOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude from the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [excludeSourceDomain] - If true, excludes links from the base domain of the input.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n */\nexport type FindSimilarOptions = BaseSearchOptions & {\n excludeSourceDomain?: boolean;\n}\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {TextContentsOptions | boolean} [text] - Options for retrieving text contents.\n * @property {HighlightsContentsOptions | boolean} [highlights] - Options for retrieving highlights.\n * @property {SummaryContentsOptions | boolean} [summary] - Options for retrieving summary.\n */\nexport type ContentsOptions = {\n text?: TextContentsOptions | true;\n highlights?: HighlightsContentsOptions | true;\n summary?: SummaryContentsOptions | true;\n};\n\n/**\n * Options for retrieving text from page.\n * @typedef {Object} TextContentsOptions\n * @property {number} [maxCharacters] - The maximum number of characters to return.\n * @property {boolean} [includeHtmlTags] - If true, includes HTML tags in the returned text. Default: false\n */\nexport type TextContentsOptions = {\n maxCharacters?: number;\n includeHtmlTags?: boolean;\n}\n\n/**\n * Options for retrieving highlights from page.\n * @typedef {Object} HighlightsContentsOptions\n * @property {string} [query] - The query string to use for highlights search.\n * @property {number} [numSentences] - The number of sentences to return for each highlight.\n * @property {number} [highlightsPerUrl] - The number of highlights to return for each URL.\n */\nexport type HighlightsContentsOptions = {\n query?: string;\n numSentences?: number;\n highlightsPerUrl?: number;\n}\n\n/**\n * Options for retrieving summary from page.\n * @typedef {Object} SummaryContentsOptions\n * @property {string} [query] - The query string to use for summary generation.\n */\nexport type SummaryContentsOptions = {\n query?: string;\n}\n\n/**\n * @typedef {Object} TextResponse\n * @property {string} text - Text from page\n */\nexport type TextResponse = { text: string };\n\n/**\n * @typedef {Object} HighlightsResponse\n * @property {string[]} highlights - The highlights as an array of strings.\n * @property {number[]} highlightScores - The corresponding scores as an array of floats, 0 to 1\n */\nexport type HighlightsResponse = { highlights: string[], highlightScores: number[] };\n\n/**\n * @typedef {Object} SummaryResponse\n * @property {string} summary - The generated summary of the page content.\n */\nexport type SummaryResponse = { summary: string };\n\nexport type Default<T extends {}, U> = [keyof T] extends [never] ? U : T;\n\n/**\n * @typedef {Object} ContentsResultComponent\n * Depending on 'ContentsOptions', this yields a combination of 'TextResponse', 'HighlightsResponse', 'SummaryResponse', or an empty object.\n *\n * @template T - A type extending from 'ContentsOptions'.\n */\nexport type ContentsResultComponent<T extends ContentsOptions> =\n Default<(T['text'] extends (object | true) ? TextResponse : {}) &\n (T['highlights'] extends (object | true) ? HighlightsResponse : {}) &\n (T['summary'] extends (object | true) ? SummaryResponse : {}), TextResponse>\n\n/**\n * Represents a search result object.\n * @typedef {Object} Result\n * @property {string} title - The title of the search result.\n * @property {string} url - The URL of the search result.\n * @property {string} [publishedDate] - The estimated creation date of the content.\n * @property {string} [author] - The author of the content, if available.\n * @property {number} [score] - Similarity score between the query/url and the result.\n * @property {string} id - The temporary ID for the document.\n */\nexport type SearchResult<T extends ContentsOptions = {}> = {\n title: string | null;\n url: string;\n publishedDate?: string;\n author?: string;\n score?: number;\n id: string;\n} & ContentsResultComponent<T>;\n\n/**\n * Represents a search response object.\n * @typedef {Object} SearchResponse\n * @property {Result[]} results - The list of search results.\n * @property {string} [autopromptString] - The autoprompt string, if applicable.\n */\nexport type SearchResponse<T extends ContentsOptions = {}> = {\n results: SearchResult<T>[];\n autopromptString?: string;\n}\n\n/**\n * The Exa class encapsulates the API's endpoints.\n */\nclass Exa {\n private baseURL: string;\n private headers: Headers;\n\n /**\n * Constructs the Exa API client.\n * @param {string} apiKey - The API key for authentication.\n * @param {string} [baseURL] - The base URL of the Exa API.\n */\n constructor(\n apiKey?: string,\n baseURL: string = \"https://api.exa.ai\"\n ) {\n this.baseURL = baseURL;\n if (!apiKey) {\n apiKey = process.env.EXASEARCH_API_KEY;\n if (!apiKey) {\n throw new Error(\"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)\");\n }\n }\n this.headers = new Headers({\n \"x-api-key\": apiKey,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": \"exa-node 1.0.27\",\n });\n }\n\n /**\n * Makes a request to the Exa API.\n * @param {string} endpoint - The API endpoint to call.\n * @param {string} method - The HTTP method to use.\n * @param {any} [body] - The request body for POST requests.\n * @returns {Promise<any>} The response from the API.\n */\n private async request(\n endpoint: string,\n method: string,\n body?: any\n ): Promise<any> {\n const response = await fetch(this.baseURL + endpoint, {\n method,\n headers: this.headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n const message = (await response.json()).error;\n throw new Error(\n `Request failed with status ${response.status}. ${message}`\n );\n }\n\n return await response.json();\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async search(query: string, options?: RegularSearchOptions): Promise<SearchResponse> {\n return await this.request(\"/search\", 'POST', { query, ...options });\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query and returns the contents of the documents.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async searchAndContents<T extends ContentsOptions>(query: string, options?: RegularSearchOptions & T): Promise<SearchResponse<T>> {\n const { text, highlights, summary, ...rest } = options || {};\n return await this.request(\"/search\", 'POST', {\n query,\n contents: (!text && !highlights && !summary) ? { text: true } : {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {}),\n ...(summary ? { summary } : {})\n },\n ...rest\n });\n }\n\n /**\n * Finds similar links to the provided URL.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilar(\n url: string,\n options?: FindSimilarOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/findSimilar\", 'POST', { url, ...options });\n }\n\n /**\n * Finds similar links to the provided URL and returns the contents of the documents.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilarAndContents<T extends ContentsOptions>(url: string, options?: FindSimilarOptions & T): Promise<SearchResponse<T>> {\n const { text, highlights, summary, ...rest } = options || {};\n return await this.request(\"/findSimilar\", 'POST', {\n url,\n contents: (!text && !highlights && !summary) ? { text: true } : {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {}),\n ...(summary ? { summary } : {})\n },\n ...rest\n });\n }\n\n /**\n * Retrieves contents of documents based on a list of document IDs.\n * @param {string | string[] | SearchResult[]} ids - An array of document IDs.\n * @param {ContentsOptions} [options] - Additional options for retrieving document contents.\n * @returns {Promise<GetContentsResponse>} A list of document contents.\n */\n async getContents<T extends ContentsOptions>(ids: string | string[] | SearchResult[], options?: T): Promise<SearchResponse<T>> {\n if (ids.length === 0) {\n throw new Error(\"Must provide at least one ID\");\n }\n let requestIds: string[];\n if (typeof ids === \"string\") {\n requestIds = [ids];\n } else if (typeof ids[0] === \"string\") {\n requestIds = ids as string[];\n } else {\n requestIds = (ids as SearchResult[]).map((result) => result.id);\n }\n return await this.request(`/contents`, 'POST', { ids: requestIds, ...options, });\n }\n}\n\nexport default Exa;\n"],"mappings":";AAAA,OAAO,SAAS,eAAe;AAqK/B,IAAM,MAAN,MAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASR,YACE,QACA,UAAkB,sBAClB;AACA,SAAK,UAAU;AACf,QAAI,CAAC,QAAQ;AACX,eAAS,QAAQ,IAAI;AACrB,UAAI,CAAC,QAAQ;AACX,cAAM,IAAI,MAAM,2FAA2F;AAAA,MAC7G;AAAA,IACF;AACA,SAAK,UAAU,IAAI,QAAQ;AAAA,MACzB,aAAa;AAAA,MACb,gBAAgB;AAAA,MAChB,cAAc;AAAA,IAChB,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAc,QACZ,UACA,QACA,MACc;AACd,UAAM,WAAW,MAAM,MAAM,KAAK,UAAU,UAAU;AAAA,MACpD;AAAA,MACA,SAAS,KAAK;AAAA,MACd,MAAM,OAAO,KAAK,UAAU,IAAI,IAAI;AAAA,IACtC,CAAC;AAED,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,WAAW,MAAM,SAAS,KAAK,GAAG;AACxC,YAAM,IAAI;AAAA,QACR,8BAA8B,SAAS,WAAW;AAAA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,IAAI,CAAC;AAAA,QACnC,GAAI,UAAU,EAAE,QAAQ,IAAI,CAAC;AAAA,MAC/B;AAAA,MACA,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,YAAuC,KAAyC,SAAyC;AAC7H,QAAI,IAAI,WAAW,GAAG;AACpB,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAChD;AACA,QAAI;AACJ,QAAI,OAAO,QAAQ,UAAU;AAC3B,mBAAa,CAAC,GAAG;AAAA,IACnB,WAAW,OAAO,IAAI,CAAC,MAAM,UAAU;AACrC,mBAAa;AAAA,IACf,OAAO;AACL,mBAAc,IAAuB,IAAI,CAAC,WAAW,OAAO,EAAE;AAAA,IAChE;AACA,WAAO,MAAM,KAAK,QAAQ,aAAa,QAAQ,EAAE,KAAK,YAAY,GAAG,QAAS,CAAC;AAAA,EACjF;AACF;AAEA,IAAO,cAAQ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import fetch, { Headers } from \"cross-fetch\";\n\nconst isBeta = true;\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} SearchOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude in the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [useAutoprompt] - If true, converts query to a Metaphor query.\n * @property {string} [type] - Type of search, 'keyword' or 'neural'.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n * @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n * @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n */\nexport type BaseSearchOptions = {\n numResults?: number;\n includeDomains?: string[];\n excludeDomains?: string[];\n startCrawlDate?: string;\n endCrawlDate?: string;\n startPublishedDate?: string;\n endPublishedDate?: string;\n category?: string;\n includeText?: string[];\n excludeText?: string[];\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} RegularSearchOptions\n */\nexport type RegularSearchOptions = BaseSearchOptions & {\n useAutoprompt?: boolean;\n type?: string;\n};\n\n/**\n * Options for finding similar links.\n * @typedef {Object} FindSimilarOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude from the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [excludeSourceDomain] - If true, excludes links from the base domain of the input.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n * @property {string[]} [includeText] - List of strings that must be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n * @property {string[]} [excludeText] - List of strings that must not be present in webpage text of results. Currently only supports 1 string of up to 5 words.\n */\nexport type FindSimilarOptions = BaseSearchOptions & {\n excludeSourceDomain?: boolean;\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {TextContentsOptions | boolean} [text] - Options for retrieving text contents.\n * @property {HighlightsContentsOptions | boolean} [highlights] - Options for retrieving highlights.\n * @property {SummaryContentsOptions | boolean} [summary] - Options for retrieving summary.\n * @property {LivecrawlOptions} [livecrawl] - Options for livecrawling contents. Default is \"never\" for neural/auto search, \"fallback\" for keyword search.\n * @property {boolean} [filterEmptyResults] - If true, filters out results with no contents. Default is true.\n * @property {number} [livecrawlTimeout] - The timeout for livecrawling.\n */\nexport type ContentsOptions = {\n text?: TextContentsOptions | true;\n highlights?: HighlightsContentsOptions | true;\n summary?: SummaryContentsOptions | true;\n} & (typeof isBeta extends true\n ? {\n livecrawl?: LivecrawlOptions;\n filterEmptyResults?: boolean;\n livecrawlTimeout?: number;\n }\n : {});\n\n/**\n * Options for livecrawling contents\n * @typedef {string} LivecrawlOptions\n */\nexport type LivecrawlOptions = \"never\" | \"fallback\" | \"always\";\n\n/**\n * Options for retrieving text from page.\n * @typedef {Object} TextContentsOptions\n * @property {number} [maxCharacters] - The maximum number of characters to return.\n * @property {boolean} [includeHtmlTags] - If true, includes HTML tags in the returned text. Default: false\n */\nexport type TextContentsOptions = {\n maxCharacters?: number;\n includeHtmlTags?: boolean;\n};\n\n/**\n * Options for retrieving highlights from page.\n * @typedef {Object} HighlightsContentsOptions\n * @property {string} [query] - The query string to use for highlights search.\n * @property {number} [numSentences] - The number of sentences to return for each highlight.\n * @property {number} [highlightsPerUrl] - The number of highlights to return for each URL.\n */\nexport type HighlightsContentsOptions = {\n query?: string;\n numSentences?: number;\n highlightsPerUrl?: number;\n};\n\n/**\n * Options for retrieving summary from page.\n * @typedef {Object} SummaryContentsOptions\n * @property {string} [query] - The query string to use for summary generation.\n */\nexport type SummaryContentsOptions = {\n query?: string;\n};\n\n/**\n * @typedef {Object} TextResponse\n * @property {string} text - Text from page\n */\nexport type TextResponse = { text: string };\n\n/**\n * @typedef {Object} HighlightsResponse\n * @property {string[]} highlights - The highlights as an array of strings.\n * @property {number[]} highlightScores - The corresponding scores as an array of floats, 0 to 1\n */\nexport type HighlightsResponse = {\n highlights: string[];\n highlightScores: number[];\n};\n\n/**\n * @typedef {Object} SummaryResponse\n * @property {string} summary - The generated summary of the page content.\n */\nexport type SummaryResponse = { summary: string };\n\nexport type Default<T extends {}, U> = [keyof T] extends [never] ? U : T;\n\n/**\n * @typedef {Object} ContentsResultComponent\n * Depending on 'ContentsOptions', this yields a combination of 'TextResponse', 'HighlightsResponse', 'SummaryResponse', or an empty object.\n *\n * @template T - A type extending from 'ContentsOptions'.\n */\nexport type ContentsResultComponent<T extends ContentsOptions> = Default<\n (T[\"text\"] extends object | true ? TextResponse : {}) &\n (T[\"highlights\"] extends object | true ? HighlightsResponse : {}) &\n (T[\"summary\"] extends object | true ? SummaryResponse : {}),\n TextResponse\n>;\n\n/**\n * Represents a search result object.\n * @typedef {Object} Result\n * @property {string} title - The title of the search result.\n * @property {string} url - The URL of the search result.\n * @property {string} [publishedDate] - The estimated creation date of the content.\n * @property {string} [author] - The author of the content, if available.\n * @property {number} [score] - Similarity score between the query/url and the result.\n * @property {string} id - The temporary ID for the document.\n */\nexport type SearchResult<T extends ContentsOptions = {}> = {\n title: string | null;\n url: string;\n publishedDate?: string;\n author?: string;\n score?: number;\n id: string;\n} & ContentsResultComponent<T>;\n\n/**\n * Represents a search response object.\n * @typedef {Object} SearchResponse\n * @property {Result[]} results - The list of search results.\n * @property {string} [autopromptString] - The autoprompt string, if applicable.\n */\nexport type SearchResponse<T extends ContentsOptions = {}> = {\n results: SearchResult<T>[];\n autopromptString?: string;\n};\n\n/**\n * The Exa class encapsulates the API's endpoints.\n */\nclass Exa {\n private baseURL: string;\n private headers: Headers;\n\n /**\n * Constructs the Exa API client.\n * @param {string} apiKey - The API key for authentication.\n * @param {string} [baseURL] - The base URL of the Exa API.\n */\n constructor(apiKey?: string, baseURL: string = \"https://api.exa.ai\") {\n this.baseURL = baseURL;\n if (!apiKey) {\n apiKey = process.env.EXASEARCH_API_KEY;\n if (!apiKey) {\n throw new Error(\n \"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)\"\n );\n }\n }\n this.headers = new Headers({\n \"x-api-key\": apiKey,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": \"exa-node 1.0.27\",\n });\n }\n\n /**\n * Makes a request to the Exa API.\n * @param {string} endpoint - The API endpoint to call.\n * @param {string} method - The HTTP method to use.\n * @param {any} [body] - The request body for POST requests.\n * @returns {Promise<any>} The response from the API.\n */\n private async request(\n endpoint: string,\n method: string,\n body?: any\n ): Promise<any> {\n const response = await fetch(this.baseURL + endpoint, {\n method,\n headers: this.headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n const message = (await response.json()).error;\n throw new Error(\n `Request failed with status ${response.status}. ${message}`\n );\n }\n\n return await response.json();\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async search(\n query: string,\n options?: RegularSearchOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/search\", \"POST\", { query, ...options });\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query and returns the contents of the documents.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async searchAndContents<T extends ContentsOptions>(\n query: string,\n options?: RegularSearchOptions & T\n ): Promise<SearchResponse<T>> {\n const { text, highlights, summary, ...rest } = options || {};\n return await this.request(\"/search\", \"POST\", {\n query,\n contents:\n !text && !highlights && !summary\n ? {\n text: true,\n ...(isBeta\n ? {\n livecrawl: options?.livecrawl,\n filterEmptyResults: options?.filterEmptyResults,\n livecrawlTimeout: options?.livecrawlTimeout,\n }\n : {}),\n }\n : {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {}),\n ...(summary ? { summary } : {}),\n ...(isBeta\n ? {\n livecrawl: options?.livecrawl,\n filterEmptyResults: options?.filterEmptyResults,\n livecrawlTimeout: options?.livecrawlTimeout,\n }\n : {}),\n },\n ...rest,\n });\n }\n\n /**\n * Finds similar links to the provided URL.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilar(\n url: string,\n options?: FindSimilarOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/findSimilar\", \"POST\", { url, ...options });\n }\n\n /**\n * Finds similar links to the provided URL and returns the contents of the documents.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilarAndContents<T extends ContentsOptions>(\n url: string,\n options?: FindSimilarOptions & T\n ): Promise<SearchResponse<T>> {\n const { text, highlights, summary, ...rest } = options || {};\n return await this.request(\"/findSimilar\", \"POST\", {\n url,\n contents:\n !text && !highlights && !summary\n ? {\n text: true,\n ...(isBeta\n ? {\n livecrawl: options?.livecrawl,\n filterEmptyResults: options?.filterEmptyResults,\n livecrawlTimeout: options?.livecrawlTimeout,\n }\n : {}),\n }\n : {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {}),\n ...(summary ? { summary } : {}),\n ...(isBeta\n ? {\n livecrawl: options?.livecrawl,\n filterEmptyResults: options?.filterEmptyResults,\n livecrawlTimeout: options?.livecrawlTimeout,\n }\n : {}),\n },\n ...rest,\n });\n }\n\n /**\n * Retrieves contents of documents based on a list of document IDs.\n * @param {string | string[] | SearchResult[]} ids - An array of document IDs.\n * @param {ContentsOptions} [options] - Additional options for retrieving document contents.\n * @returns {Promise<GetContentsResponse>} A list of document contents.\n */\n async getContents<T extends ContentsOptions>(\n ids: string | string[] | SearchResult[],\n options?: T\n ): Promise<SearchResponse<T>> {\n const { livecrawl, filterEmptyResults, ...rest } = options || {};\n if (ids.length === 0) {\n throw new Error(\"Must provide at least one ID\");\n }\n let requestIds: string[];\n if (typeof ids === \"string\") {\n requestIds = [ids];\n } else if (typeof ids[0] === \"string\") {\n requestIds = ids as string[];\n } else {\n requestIds = (ids as SearchResult[]).map((result) => result.id);\n }\n return await this.request(`/contents`, \"POST\", {\n ids: requestIds,\n ...(isBeta\n ? {\n livecrawl: options?.livecrawl,\n filterEmptyResults: options?.filterEmptyResults,\n livecrawlTimeout: options?.livecrawlTimeout,\n }\n : {}),\n ...rest,\n });\n }\n}\n\nexport default Exa;\n"],"mappings":";AAAA,OAAO,SAAS,eAAe;A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B,SAAS;AAAA,UAC7B,kBAAkB,SAAS;AAAA,QAC7B,IACA,CAAC;AAAA,MACP;AAAA,MACN,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,YACJ,KACA,SAC4B;AAC5B,UAAM,EAAE,WAAW,oBAAoB,GAAG,KAAK,IAAI,WAAW,CAAC;AAC/D,QAAI,IAAI,WAAW,GAAG;AACpB,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAChD;AACA,QAAI;AACJ,QAAI,OAAO,QAAQ,UAAU;AAC3B,mBAAa,CAAC,GAAG;AAAA,IACnB,WAAW,OAAO,IAAI,CAAC,MAAM,UAAU;AACrC,mBAAa;AAAA,IACf,OAAO;AACL,mBAAc,IAAuB,IAAI,CAAC,WAAW,OAAO,EAAE;AAAA,IAChE;AACA,WAAO,MAAM,KAAK,QAAQ,aAAa,QAAQ;AAAA,MAC7C,KAAK;AAAA,MACL,GAAI,SACA;AAAA,QACE,WAAW,SAAS;AAAA,QACpB,oBAAoB,SAAS;AAAA,QAC7B,kBAAkB,SAAS;AAAA,MAC7B,IACA,CAAC;AAAA,MACL,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AACF;AAEA,IAAO,cAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "exa-js",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14-beta.1",
|
|
4
4
|
"description": "Exa SDK for Node.js and the browser",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -25,15 +25,21 @@
|
|
|
25
25
|
"build": "tsup",
|
|
26
26
|
"test": "vitest run",
|
|
27
27
|
"generate-docs": "typedoc --plugin typedoc-plugin-markdown --out docs src/index.ts",
|
|
28
|
+
"build:beta": "cross-env NPM_CONFIG_TAG=beta npm run build",
|
|
29
|
+
"version:beta": "npm version prerelease --preid=beta",
|
|
30
|
+
"version:stable": "npm version patch",
|
|
31
|
+
"publish:beta": "npm run version:beta && npm run build:beta && npm publish --tag beta",
|
|
32
|
+
"publish:stable": "npm run version:stable && npm run build && npm publish",
|
|
28
33
|
"prepublishOnly": "npm run build"
|
|
29
34
|
},
|
|
30
35
|
"license": "MIT",
|
|
31
36
|
"devDependencies": {
|
|
37
|
+
"cross-env": "^7.0.3",
|
|
32
38
|
"prettier": "2.8.4",
|
|
33
39
|
"tsup": "6.6.3",
|
|
34
|
-
"typescript": "4.9.5",
|
|
35
40
|
"typedoc": "^0.25.4",
|
|
36
41
|
"typedoc-plugin-markdown": "^3.17.1",
|
|
42
|
+
"typescript": "4.9.5",
|
|
37
43
|
"vitest": "0.28.5"
|
|
38
44
|
},
|
|
39
45
|
"dependencies": {
|