aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbobzel <zzzman@gmail.com>2024-10-17 13:38:54 -0400
committerbobzel <zzzman@gmail.com>2024-10-17 13:38:54 -0400
commitc0a9a32846c5cf394be0efb34f2e0021ca5e39cd (patch)
tree2590fa4a8ca84af6a72267b31f2c7ad6b4b0d2ee /src
parent5e3f9d84da226e62ce109cc2c0b00ba76eb45189 (diff)
lint cleanup for awaits in loops
Diffstat (limited to 'src')
-rw-r--r--src/server/ApiManagers/AssistantManager.ts117
1 files changed, 61 insertions, 56 deletions
diff --git a/src/server/ApiManagers/AssistantManager.ts b/src/server/ApiManagers/AssistantManager.ts
index b7d4191ca..771004302 100644
--- a/src/server/ApiManagers/AssistantManager.ts
+++ b/src/server/ApiManagers/AssistantManager.ts
@@ -15,7 +15,6 @@ 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';
@@ -24,6 +23,7 @@ import { AI_Document } from '../../client/views/nodes/chatbot/types/types';
import { Method } from '../RouteManager';
import { filesDirectory, publicDirectory } from '../SocketData';
import ApiManager, { Registration } from './ApiManager';
+import _ from 'lodash';
// Enumeration of directories where different file types are stored
export enum Directory {
@@ -307,33 +307,35 @@ export default class AssistantManager extends ApiManager {
// If the result contains image or table chunks, save the base64 data as image files
if (result.chunks && Array.isArray(result.chunks)) {
- for (const chunk of result.chunks) {
- if (chunk.metadata && (chunk.metadata.type === 'image' || chunk.metadata.type === 'table')) {
- const files_directory = '/files/chunk_images/';
- const directory = path.join(publicDirectory, files_directory);
-
- // Ensure the directory exists or create it
- if (!fs.existsSync(directory)) {
- fs.mkdirSync(directory);
+ await Promise.all(
+ result.chunks.map(chunk => {
+ if (chunk.metadata && (chunk.metadata.type === 'image' || chunk.metadata.type === 'table')) {
+ const files_directory = '/files/chunk_images/';
+ const directory = path.join(publicDirectory, files_directory);
+
+ // Ensure the directory exists or create it
+ if (!fs.existsSync(directory)) {
+ fs.mkdirSync(directory);
+ }
+
+ const fileName = path.basename(chunk.metadata.file_path); // Get the file name from the path
+ const filePath = path.join(directory, fileName); // Create the full file path
+
+ // Check if the chunk contains base64 encoded data
+ if (chunk.metadata.base64_data) {
+ // Decode the base64 data and write it to a file
+ const buffer = Buffer.from(chunk.metadata.base64_data, 'base64');
+ fs.promises.writeFile(filePath, buffer).then(() => {
+ // Update the file path in the chunk's metadata
+ chunk.metadata.file_path = path.join(files_directory, fileName);
+ chunk.metadata.base64_data = undefined; // Remove the base64 data from the metadata
+ });
+ } else {
+ console.warn(`No base64_data found for chunk: ${fileName}`);
+ }
}
-
- const fileName = path.basename(chunk.metadata.file_path); // Get the file name from the path
- const filePath = path.join(directory, fileName); // Create the full file path
-
- // Check if the chunk contains base64 encoded data
- if (chunk.metadata.base64_data) {
- // Decode the base64 data and write it to a file
- const buffer = Buffer.from(chunk.metadata.base64_data, 'base64');
- await fs.promises.writeFile(filePath, buffer);
-
- // Update the file path in the chunk's metadata
- chunk.metadata.file_path = path.join(files_directory, fileName);
- chunk.metadata.base64_data = undefined; // Remove the base64 data from the metadata
- } else {
- console.warn(`No base64_data found for chunk: ${fileName}`);
- }
- }
- }
+ })
+ );
result.status = 'completed';
} else {
result.status = 'pending';
@@ -355,39 +357,42 @@ export default class AssistantManager extends ApiManager {
// Initialize an array to hold the formatted content
const content: { type: string; text?: string; image_url?: { url: string } }[] = [{ type: 'text', text: '<chunks>' }];
- for (const chunk of relevantChunks) {
- // Format each chunk by adding its metadata and content
- content.push({
- type: 'text',
- text: `<chunk chunk_id=${chunk.id} chunk_type="${chunk.metadata.type}">`,
- });
-
- // If the chunk is an image or table, read the corresponding file and encode it as base64
- if (chunk.metadata.type === 'image' || chunk.metadata.type === 'table') {
- try {
- const filePath = serverPathToFile(Directory.chunk_images, chunk.metadata.file_path); // Get the file path
- const imageBuffer = await readFileAsync(filePath); // Read the image file
- const base64Image = imageBuffer.toString('base64'); // Convert the image to base64
-
- // Add the base64-encoded image to the content array
- if (base64Image) {
- content.push({
- type: 'image_url',
- image_url: {
- url: `data:image/jpeg;base64,${base64Image}`,
- },
+ await Promise.all(
+ relevantChunks.map((chunk: { id: string; metadata: { type: string; text: TimeRanges; file_path: string } }) => {
+ // Format each chunk by adding its metadata and content
+ content.push({
+ type: 'text',
+ text: `<chunk chunk_id=${chunk.id} chunk_type="${chunk.metadata.type}">`,
+ });
+
+ // If the chunk is an image or table, read the corresponding file and encode it as base64
+ if (chunk.metadata.type === 'image' || chunk.metadata.type === 'table') {
+ try {
+ const filePath = serverPathToFile(Directory.chunk_images, chunk.metadata.file_path); // Get the file path
+ readFileAsync(filePath).then(imageBuffer => {
+ const base64Image = imageBuffer.toString('base64'); // Convert the image to base64
+
+ // Add the base64-encoded image to the content array
+ if (base64Image) {
+ content.push({
+ type: 'image_url',
+ image_url: {
+ url: `data:image/jpeg;base64,${base64Image}`,
+ },
+ });
+ } else {
+ console.log(`Failed to encode image for chunk ${chunk.id}`);
+ }
});
- } else {
- console.log(`Failed to encode image for chunk ${chunk.id}`);
+ } catch (error) {
+ console.error(`Error reading image file for chunk ${chunk.id}:`, error);
}
- } catch (error) {
- console.error(`Error reading image file for chunk ${chunk.id}:`, error);
}
- }
- // Add the chunk's text content to the formatted content
- content.push({ type: 'text', text: `${chunk.metadata.text}\n</chunk>\n` });
- }
+ // Add the chunk's text content to the formatted content
+ content.push({ type: 'text', text: `${chunk.metadata.text}\n</chunk>\n` });
+ })
+ );
content.push({ type: 'text', text: '</chunks>' });