aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/AssistantManager.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/ApiManagers/AssistantManager.ts')
-rw-r--r--src/server/ApiManagers/AssistantManager.ts37
1 files changed, 21 insertions, 16 deletions
diff --git a/src/server/ApiManagers/AssistantManager.ts b/src/server/ApiManagers/AssistantManager.ts
index b53581aa2..86af756a4 100644
--- a/src/server/ApiManagers/AssistantManager.ts
+++ b/src/server/ApiManagers/AssistantManager.ts
@@ -9,7 +9,7 @@
*/
import { Readability } from '@mozilla/readability';
-import axios, { AxiosResponse } from 'axios';
+import axios from 'axios';
import { spawn } from 'child_process';
import * as fs from 'fs';
import { writeFile } from 'fs';
@@ -122,8 +122,6 @@ export default class AssistantManager extends ApiManager {
const { query, max_results } = req.body;
const MIN_VALID_RESULTS_RATIO = 0.75; // 3/4 threshold
let startIndex = 1; // Start at the first result initially
- let validResults: any[] = [];
-
const fetchSearchResults = async (start: number) => {
return customsearch.cse.list({
q: query,
@@ -135,20 +133,27 @@ export default class AssistantManager extends ApiManager {
});
};
- const filterResultsByXFrameOptions = async (results: any[]) => {
+ const filterResultsByXFrameOptions = async (
+ results: {
+ url: string | null | undefined;
+ snippet: string | null | undefined;
+ }[]
+ ) => {
const filteredResults = await Promise.all(
- results.map(async result => {
- try {
- const urlResponse: AxiosResponse = await axios.head(result.url, { timeout: 5000 });
- const xFrameOptions = urlResponse.headers['x-frame-options'];
- if (xFrameOptions && xFrameOptions.toUpperCase() === 'SAMEORIGIN') {
- return result;
+ results
+ .filter(result => result.url)
+ .map(async result => {
+ try {
+ const urlResponse = await axios.head(result.url!, { timeout: 5000 });
+ const xFrameOptions = urlResponse.headers['x-frame-options'];
+ if (xFrameOptions && xFrameOptions.toUpperCase() === 'SAMEORIGIN') {
+ return result;
+ }
+ } catch (error) {
+ console.error(`Error checking x-frame-options for URL: ${result.url}`, error);
}
- } catch (error) {
- console.error(`Error checking x-frame-options for URL: ${result.url}`, error);
- }
- return null; // Exclude the result if it doesn't match
- })
+ return null; // Exclude the result if it doesn't match
+ })
);
return filteredResults.filter(result => result !== null); // Remove null results
};
@@ -163,7 +168,7 @@ export default class AssistantManager extends ApiManager {
})) || [];
// Filter the initial results
- validResults = await filterResultsByXFrameOptions(initialResults);
+ let validResults = await filterResultsByXFrameOptions(initialResults);
// If valid results are less than 3/4 of max_results, fetch more results
while (validResults.length < max_results * MIN_VALID_RESULTS_RATIO) {