diff options
author | bobzel <zzzman@gmail.com> | 2025-02-10 19:07:20 -0500 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2025-02-10 19:07:20 -0500 |
commit | c9686eaebffb3547b7e0f20aec64754627af76ce (patch) | |
tree | 7ebf1c38323a8d7af554ba564acf95cfe79b7709 /src/server/ApiManagers/AssistantManager.ts | |
parent | b72d018698ad1d2e713f0fcbef392d23bf1cf545 (diff) | |
parent | e93ca53af693fa1ec2186ca9417af122bb5e8e09 (diff) |
updated from master
Diffstat (limited to 'src/server/ApiManagers/AssistantManager.ts')
-rw-r--r-- | src/server/ApiManagers/AssistantManager.ts | 58 |
1 files changed, 27 insertions, 31 deletions
diff --git a/src/server/ApiManagers/AssistantManager.ts b/src/server/ApiManagers/AssistantManager.ts index c5ba4b830..c41f697db 100644 --- a/src/server/ApiManagers/AssistantManager.ts +++ b/src/server/ApiManagers/AssistantManager.ts @@ -9,28 +9,22 @@ */ 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'; import { google } from 'googleapis'; import { JSDOM } from 'jsdom'; +import OpenAI from 'openai'; import * as path from 'path'; import * as puppeteer from 'puppeteer'; import { promisify } from 'util'; import * as uuid from 'uuid'; import { AI_Document } from '../../client/views/nodes/chatbot/types/types'; +import { DashUploadUtils } from '../DashUploadUtils'; import { Method } from '../RouteManager'; import { filesDirectory, publicDirectory } from '../SocketData'; import ApiManager, { Registration } from './ApiManager'; -import { getServerPath } from '../../client/util/reportManager/reportManagerUtils'; -import { file } from 'jszip'; -import ffmpegInstaller from '@ffmpeg-installer/ffmpeg'; -import ffmpeg from 'fluent-ffmpeg'; -import OpenAI from 'openai'; -import * as xmlbuilder from 'xmlbuilder'; -import { last } from 'lodash'; -import { DashUploadUtils } from '../DashUploadUtils'; // Enumeration of directories where different file types are stored export enum Directory { @@ -133,8 +127,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, @@ -146,20 +138,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 }; @@ -167,14 +166,14 @@ export default class AssistantManager extends ApiManager { try { // Fetch initial search results let response = await fetchSearchResults(startIndex); - let initialResults = + const initialResults = response.data.items?.map(item => ({ url: item.link, snippet: item.snippet, })) || []; // 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) { @@ -288,7 +287,7 @@ export default class AssistantManager extends ApiManager { // Step 2: Transcribe audio using OpenAI Whisper console.log('Transcribing audio...'); const transcription = await openai.audio.transcriptions.create({ - file: fs.createReadStream(audioPath) as any, + file: fs.createReadStream(audioPath), model: 'whisper-1', response_format: 'verbose_json', timestamp_granularities: ['segment'], @@ -298,7 +297,7 @@ export default class AssistantManager extends ApiManager { // Step 3: Extract concise JSON console.log('Extracting concise JSON...'); - const originalSegments = transcription.segments?.map((segment: any, index: number) => ({ + const originalSegments = transcription.segments?.map((segment, index) => ({ index: index.toString(), text: segment.text, start: segment.start, @@ -767,7 +766,7 @@ function spawnPythonProcess(jobId: string, file_name: string, file_path: string) try { const errorOutput = JSON.parse(lastLine); jobResults[jobId] = errorOutput; - } catch (err) { + } catch { jobResults[jobId] = { error: 'Python process failed' }; } } else { @@ -829,6 +828,3 @@ function spawnPythonProcess(jobId: string, file_name: string, file_path: string) runPythonScript(); } } -function customFfmpeg(filePath: string) { - throw new Error('Function not implemented.'); -} |