openaxies 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/providers/websearch.js +16 -12
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const SEARCH_ENDPOINT = 'https://
|
|
1
|
+
const SEARCH_ENDPOINT = 'https://serpapi.com/search.json';
|
|
2
2
|
|
|
3
3
|
function latestUserText(messages) {
|
|
4
4
|
if (Array.isArray(messages) === false) {
|
|
@@ -13,11 +13,13 @@ function latestUserText(messages) {
|
|
|
13
13
|
return '';
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
const FALLBACK_API_KEY = 'ff6126186f6b3f56b08045458f84b85a75a2aff0c7eb178add534cb982c57fe4';
|
|
17
|
+
|
|
16
18
|
function getWebSearchKey() {
|
|
17
19
|
if (typeof process === 'undefined' || process.env === null || process.env === undefined) {
|
|
18
|
-
return
|
|
20
|
+
return FALLBACK_API_KEY;
|
|
19
21
|
}
|
|
20
|
-
return process.env.OPENAXIES_WEBSEARCH_KEY || process.env.WEBSEARCH_API_KEY ||
|
|
22
|
+
return process.env.OPENAXIES_WEBSEARCH_KEY || process.env.WEBSEARCH_API_KEY || FALLBACK_API_KEY;
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
export function shouldUseWebSearch(messages) {
|
|
@@ -53,13 +55,15 @@ async function searchWeb(query, signal) {
|
|
|
53
55
|
};
|
|
54
56
|
}
|
|
55
57
|
|
|
56
|
-
const
|
|
58
|
+
const params = new URLSearchParams({
|
|
59
|
+
q: query,
|
|
60
|
+
api_key: key,
|
|
61
|
+
engine: 'google',
|
|
62
|
+
num: '5',
|
|
63
|
+
});
|
|
64
|
+
const url = SEARCH_ENDPOINT + '?' + params.toString();
|
|
57
65
|
const response = await fetch(url, {
|
|
58
66
|
method: 'GET',
|
|
59
|
-
headers: {
|
|
60
|
-
'Accept': 'application/json',
|
|
61
|
-
'X-Subscription-Token': key,
|
|
62
|
-
},
|
|
63
67
|
signal: signal,
|
|
64
68
|
});
|
|
65
69
|
|
|
@@ -68,16 +72,16 @@ async function searchWeb(query, signal) {
|
|
|
68
72
|
}
|
|
69
73
|
|
|
70
74
|
const json = await response.json();
|
|
71
|
-
const results = json &&
|
|
72
|
-
? json.
|
|
75
|
+
const results = json && Array.isArray(json.organic_results)
|
|
76
|
+
? json.organic_results
|
|
73
77
|
: [];
|
|
74
78
|
const lines = [];
|
|
75
79
|
|
|
76
80
|
for (let i = 0; i < results.length; i++) {
|
|
77
81
|
const result = results[i];
|
|
78
82
|
const title = typeof result.title === 'string' ? result.title : 'Untitled';
|
|
79
|
-
const urlValue = typeof result.
|
|
80
|
-
const description = typeof result.
|
|
83
|
+
const urlValue = typeof result.link === 'string' ? result.link : '';
|
|
84
|
+
const description = typeof result.snippet === 'string' ? result.snippet : '';
|
|
81
85
|
lines.push('- ' + title + ' | ' + urlValue + ' | ' + description);
|
|
82
86
|
}
|
|
83
87
|
|