Skip to content

Commit f866613

Browse files
Merge pull request #15 from TensorBlock/image-generation-support
Fix: file storage migrate to different database
2 parents 65d7314 + ebb0bdd commit f866613

File tree

5 files changed

+56
-40
lines changed

5 files changed

+56
-40
lines changed

src/components/layout/Sidebar.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { MessageSquare, Settings, Image, Languages, FileText } from 'lucide-react';
2+
import { MessageSquare, Settings, Image, Languages, FolderClosed } from 'lucide-react';
33

44
interface SidebarProps {
55
activePage: string;
@@ -85,7 +85,7 @@ export const Sidebar: React.FC<SidebarProps> = ({
8585
onClick={() => onChangePage('files')}
8686
aria-label="File Management"
8787
>
88-
<FileText size={22} />
88+
<FolderClosed size={22} />
8989
</button>
9090
</div>
9191

src/components/pages/FileManagementPage.tsx

+30-30
Original file line numberDiff line numberDiff line change
@@ -281,38 +281,38 @@ export const FileManagementPage = () => {
281281
};
282282

283283
// Open file
284-
// const handleOpenFile = async (file: FileData) => {
285-
// try {
286-
// // First save the file to a temporary location
287-
// if (!window.electron || !window.electron.saveFile || !window.electron.openFile) {
288-
// console.error("Electron API not available");
289-
// return;
290-
// }
284+
const handleOpenFile = async (file: FileData) => {
285+
try {
286+
// First save the file to a temporary location
287+
if (!window.electron || !window.electron.saveFile || !window.electron.openFile) {
288+
console.error("Electron API not available");
289+
return;
290+
}
291291

292-
// // Save file to temp location and then open it
293-
// const saveResult = await window.electron.saveFile(
294-
// file.data,
295-
// file.name,
296-
// file.type || 'application/octet-stream'
297-
// );
292+
// Save file to temp location and then open it
293+
const saveResult = await window.electron.saveFile(
294+
file.data,
295+
file.name,
296+
file.type || 'application/octet-stream'
297+
);
298298

299-
// if (!saveResult.success || !saveResult.filePath) {
300-
// if (!saveResult.canceled) {
301-
// console.error("Error saving file:", saveResult.error);
302-
// }
303-
// return;
304-
// }
299+
if (!saveResult.success || !saveResult.filePath) {
300+
if (!saveResult.canceled) {
301+
console.error("Error saving file:", saveResult.error);
302+
}
303+
return;
304+
}
305305

306-
// // Now open the file with the default application
307-
// const openResult = await window.electron.openFile(saveResult.filePath);
306+
// Now open the file with the default application
307+
const openResult = await window.electron.openFile(saveResult.filePath);
308308

309-
// if (!openResult.success) {
310-
// console.error("Error opening file:", openResult.error);
311-
// }
312-
// } catch (error) {
313-
// console.error("Error opening file:", error);
314-
// }
315-
// };
309+
if (!openResult.success) {
310+
console.error("Error opening file:", openResult.error);
311+
}
312+
} catch (error) {
313+
console.error("Error opening file:", error);
314+
}
315+
};
316316

317317
// Format file size
318318
const formatFileSize = (bytes: number): string => {
@@ -672,13 +672,13 @@ export const FileManagementPage = () => {
672672
</td>
673673
<td className="px-4 py-3">
674674
<div className="flex items-center justify-center space-x-2">
675-
{/* <button
675+
<button
676676
onClick={() => handleOpenFile(file)}
677677
className="p-2 rounded-md message-icon-btn"
678678
title={t("fileManagement.open")}
679679
>
680680
<FolderOpen size={16} />
681-
</button> */}
681+
</button>
682682
<button
683683
onClick={() => {
684684
setSelectedFile(file);

src/services/chat-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export class ChatService {
292292
//#region Save user message with files to database and update title
293293
// eslint-disable-next-line prefer-const
294294
let {conversation: updatedConversation, message: userMessage} = await MessageHelper.addUserMessageWithFilesToConversation(
295-
content,
295+
content,
296296
fileContents,
297297
currentConversation
298298
);

src/services/message-helper.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -318,19 +318,19 @@ export class MessageHelper {
318318
}).join('');
319319
}
320320

321-
public static MessagesContentToOpenAIFormat(msgs: Message[]): CoreMessage[] {
321+
public static async MessagesContentToOpenAIFormat(msgs: Message[]): Promise<CoreMessage[]> {
322322
if (!msgs || msgs.length === 0) {
323323
return [];
324324
}
325325

326326
console.log('before msgs: ', msgs);
327327

328-
const results = msgs.map((msg) => {
328+
const results = await Promise.all(msgs.map(async (msg) => {
329329

330330
if(msg.role === 'user') {
331331
const userMsg: CoreUserMessage = {
332332
role: 'user',
333-
content: msg.content.map((content) => {
333+
content: await Promise.all(msg.content.map(async (content) => {
334334
if(content.type === MessageContentType.Text) {
335335
const textContent: TextPart = {
336336
type: 'text',
@@ -343,9 +343,25 @@ export class MessageHelper {
343343

344344
console.log('Processing file: ', dataJson.name);
345345

346+
const dbService = DatabaseIntegrationService.getInstance();
347+
348+
// Get the file data by the file id
349+
const fileData = await dbService.getFile(content.content); //content.content is the file id
350+
351+
if(!fileData) {
352+
const emptyText: TextPart = {
353+
type: 'text',
354+
text: ''
355+
};
356+
console.log('File not found: ', content.content);
357+
return emptyText;
358+
}
359+
360+
const fileBuffer = fileData.data;
361+
346362
const fileContent: FilePart = {
347363
type: 'file',
348-
data: content.content,
364+
data: fileBuffer,
349365
mimeType: 'application/pdf', // SDK requires pdf mime type, but it supports all mime types
350366
filename: dataJson.name
351367
}
@@ -357,7 +373,7 @@ export class MessageHelper {
357373
text: ''
358374
};
359375
return emptyText;
360-
})
376+
})),
361377
}
362378

363379
return userMsg;
@@ -395,7 +411,7 @@ export class MessageHelper {
395411
return systemMsg;
396412
}
397413

398-
});
414+
}));
399415

400416
return results.filter((result) => result !== undefined) as CoreMessage[];
401417
}

src/services/providers/common-provider-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class CommonProviderHelper implements AiServiceProvider {
146146
toolChoice: ToolChoice<ToolSet> | undefined = undefined
147147
): Promise<Message> {
148148
try {
149-
const formattedMessages = MessageHelper.MessagesContentToOpenAIFormat(messages);
149+
const formattedMessages = await MessageHelper.MessagesContentToOpenAIFormat(messages);
150150

151151
console.log('formattedMessages: ', formattedMessages);
152152

0 commit comments

Comments
 (0)