Skip to content

Fix: file storage migrate to different database #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/layout/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { MessageSquare, Settings, Image, Languages, FileText } from 'lucide-react';
import { MessageSquare, Settings, Image, Languages, FolderClosed } from 'lucide-react';

interface SidebarProps {
activePage: string;
Expand Down Expand Up @@ -85,7 +85,7 @@ export const Sidebar: React.FC<SidebarProps> = ({
onClick={() => onChangePage('files')}
aria-label="File Management"
>
<FileText size={22} />
<FolderClosed size={22} />
</button>
</div>

Expand Down
60 changes: 30 additions & 30 deletions src/components/pages/FileManagementPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,38 +281,38 @@ export const FileManagementPage = () => {
};

// Open file
// const handleOpenFile = async (file: FileData) => {
// try {
// // First save the file to a temporary location
// if (!window.electron || !window.electron.saveFile || !window.electron.openFile) {
// console.error("Electron API not available");
// return;
// }
const handleOpenFile = async (file: FileData) => {
try {
// First save the file to a temporary location
if (!window.electron || !window.electron.saveFile || !window.electron.openFile) {
console.error("Electron API not available");
return;
}

// // Save file to temp location and then open it
// const saveResult = await window.electron.saveFile(
// file.data,
// file.name,
// file.type || 'application/octet-stream'
// );
// Save file to temp location and then open it
const saveResult = await window.electron.saveFile(
file.data,
file.name,
file.type || 'application/octet-stream'
);

// if (!saveResult.success || !saveResult.filePath) {
// if (!saveResult.canceled) {
// console.error("Error saving file:", saveResult.error);
// }
// return;
// }
if (!saveResult.success || !saveResult.filePath) {
if (!saveResult.canceled) {
console.error("Error saving file:", saveResult.error);
}
return;
}

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

// if (!openResult.success) {
// console.error("Error opening file:", openResult.error);
// }
// } catch (error) {
// console.error("Error opening file:", error);
// }
// };
if (!openResult.success) {
console.error("Error opening file:", openResult.error);
}
} catch (error) {
console.error("Error opening file:", error);
}
};

// Format file size
const formatFileSize = (bytes: number): string => {
Expand Down Expand Up @@ -672,13 +672,13 @@ export const FileManagementPage = () => {
</td>
<td className="px-4 py-3">
<div className="flex items-center justify-center space-x-2">
{/* <button
<button
onClick={() => handleOpenFile(file)}
className="p-2 rounded-md message-icon-btn"
title={t("fileManagement.open")}
>
<FolderOpen size={16} />
</button> */}
</button>
<button
onClick={() => {
setSelectedFile(file);
Expand Down
2 changes: 1 addition & 1 deletion src/services/chat-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export class ChatService {
//#region Save user message with files to database and update title
// eslint-disable-next-line prefer-const
let {conversation: updatedConversation, message: userMessage} = await MessageHelper.addUserMessageWithFilesToConversation(
content,
content,
fileContents,
currentConversation
);
Expand Down
28 changes: 22 additions & 6 deletions src/services/message-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,19 @@ export class MessageHelper {
}).join('');
}

public static MessagesContentToOpenAIFormat(msgs: Message[]): CoreMessage[] {
public static async MessagesContentToOpenAIFormat(msgs: Message[]): Promise<CoreMessage[]> {
if (!msgs || msgs.length === 0) {
return [];
}

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

const results = msgs.map((msg) => {
const results = await Promise.all(msgs.map(async (msg) => {

if(msg.role === 'user') {
const userMsg: CoreUserMessage = {
role: 'user',
content: msg.content.map((content) => {
content: await Promise.all(msg.content.map(async (content) => {
if(content.type === MessageContentType.Text) {
const textContent: TextPart = {
type: 'text',
Expand All @@ -343,9 +343,25 @@ export class MessageHelper {

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

const dbService = DatabaseIntegrationService.getInstance();

// Get the file data by the file id
const fileData = await dbService.getFile(content.content); //content.content is the file id

if(!fileData) {
const emptyText: TextPart = {
type: 'text',
text: ''
};
console.log('File not found: ', content.content);
return emptyText;
}

const fileBuffer = fileData.data;

const fileContent: FilePart = {
type: 'file',
data: content.content,
data: fileBuffer,
mimeType: 'application/pdf', // SDK requires pdf mime type, but it supports all mime types
filename: dataJson.name
}
Expand All @@ -357,7 +373,7 @@ export class MessageHelper {
text: ''
};
return emptyText;
})
})),
}

return userMsg;
Expand Down Expand Up @@ -395,7 +411,7 @@ export class MessageHelper {
return systemMsg;
}

});
}));

return results.filter((result) => result !== undefined) as CoreMessage[];
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/providers/common-provider-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class CommonProviderHelper implements AiServiceProvider {
toolChoice: ToolChoice<ToolSet> | undefined = undefined
): Promise<Message> {
try {
const formattedMessages = MessageHelper.MessagesContentToOpenAIFormat(messages);
const formattedMessages = await MessageHelper.MessagesContentToOpenAIFormat(messages);

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

Expand Down
Loading