2025-10-04 12:10:22 +00:00
|
|
|
import { Path } from "Enums/Path";
|
2025-09-30 20:20:24 +00:00
|
|
|
import { Resolve } from "./DependencyService";
|
|
|
|
|
import { FileSystemService } from "./FileSystemService";
|
|
|
|
|
import { Services } from "./Services";
|
2025-10-04 12:10:22 +00:00
|
|
|
import { Conversation } from "Conversations/Conversation";
|
2025-10-05 12:06:05 +00:00
|
|
|
import { ConversationContent } from "Conversations/ConversationContent";
|
2025-12-19 12:30:51 +00:00
|
|
|
import { Attachment } from "Conversations/Attachment";
|
2025-11-17 19:02:15 +00:00
|
|
|
import { Exception } from "Helpers/Exception";
|
2025-12-19 12:30:51 +00:00
|
|
|
import type { IAIFileService } from "AIClasses/IAIFileService";
|
2025-12-23 12:04:29 +00:00
|
|
|
import { Reference } from "Conversations/Reference";
|
2025-09-30 20:20:24 +00:00
|
|
|
|
|
|
|
|
export class ConversationFileSystemService {
|
|
|
|
|
|
|
|
|
|
private fileSystemService: FileSystemService;
|
2025-12-19 12:30:51 +00:00
|
|
|
private aiFileService: IAIFileService | undefined;
|
|
|
|
|
|
2025-09-30 20:20:24 +00:00
|
|
|
private currentConversationPath: string | null = null;
|
2025-12-20 01:34:41 +00:00
|
|
|
private deletionQueue: Promise<void> = Promise.resolve();
|
2025-09-30 20:20:24 +00:00
|
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
|
this.fileSystemService = Resolve<FileSystemService>(Services.FileSystemService);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-19 12:30:51 +00:00
|
|
|
public resolveAIFileService() {
|
|
|
|
|
this.aiFileService = Resolve<IAIFileService>(Services.IAIFileService);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 12:06:05 +00:00
|
|
|
public generateConversationPath(conversation: Conversation): string {
|
|
|
|
|
return `${Path.Conversations}/${conversation.title}.json`;
|
2025-09-30 20:20:24 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
public async saveConversation(conversation: Conversation): Promise<string | Error> {
|
2025-09-30 20:20:24 +00:00
|
|
|
if (!this.currentConversationPath) {
|
2025-10-05 12:06:05 +00:00
|
|
|
this.currentConversationPath = this.generateConversationPath(conversation);
|
2025-12-04 23:04:20 +00:00
|
|
|
} else {
|
|
|
|
|
// can happen if the conversation is deleted during an active request
|
|
|
|
|
const fileExists = await this.fileSystemService.exists(this.currentConversationPath, true);
|
|
|
|
|
if (!fileExists) {
|
|
|
|
|
return this.currentConversationPath;
|
|
|
|
|
}
|
2025-09-30 20:20:24 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-22 09:06:32 +00:00
|
|
|
conversation.updated = new Date();
|
|
|
|
|
|
2025-09-30 20:20:24 +00:00
|
|
|
const conversationData = {
|
2025-10-04 12:10:22 +00:00
|
|
|
title: conversation.title,
|
|
|
|
|
created: conversation.created.toISOString(),
|
2025-10-22 09:06:32 +00:00
|
|
|
updated: conversation.updated.toISOString(),
|
2025-10-13 12:14:54 +00:00
|
|
|
contents: conversation.contents
|
|
|
|
|
.map(content => ({
|
|
|
|
|
role: content.role,
|
2025-12-19 12:30:51 +00:00
|
|
|
timestamp: content.timestamp.toISOString(),
|
2025-10-13 12:14:54 +00:00
|
|
|
content: content.content,
|
2025-12-19 12:30:51 +00:00
|
|
|
displayContent: content.displayContent,
|
2025-10-20 07:30:08 +00:00
|
|
|
functionCall: content.functionCall,
|
2025-12-19 12:30:51 +00:00
|
|
|
functionResponse: content.functionResponse,
|
|
|
|
|
attachments: content.attachments,
|
2025-12-23 12:04:29 +00:00
|
|
|
references: content.references,
|
2025-12-19 12:30:51 +00:00
|
|
|
shouldDisplayContent: content.shouldDisplayContent,
|
2025-11-19 20:30:53 +00:00
|
|
|
toolId: content.toolId,
|
2025-12-10 21:27:58 +00:00
|
|
|
thoughtSignature: content.thoughtSignature,
|
2025-11-19 20:30:53 +00:00
|
|
|
errorType: content.errorType
|
2025-10-13 12:14:54 +00:00
|
|
|
}))
|
2025-09-30 20:20:24 +00:00
|
|
|
};
|
|
|
|
|
|
2025-11-24 21:29:54 +00:00
|
|
|
const result = await this.fileSystemService.writeObjectToFile(this.currentConversationPath, conversationData, true, false);
|
2025-11-17 19:02:15 +00:00
|
|
|
|
|
|
|
|
if (result instanceof Error) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 20:20:24 +00:00
|
|
|
return this.currentConversationPath;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
public resetCurrentConversation() {
|
2025-09-30 20:20:24 +00:00
|
|
|
this.currentConversationPath = null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 12:37:28 +00:00
|
|
|
public getCurrentConversationPath(): string | null {
|
|
|
|
|
return this.currentConversationPath;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
public setCurrentConversationPath(filePath: string) {
|
2025-10-05 13:15:04 +00:00
|
|
|
this.currentConversationPath = filePath;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
public async deleteCurrentConversation(): Promise<void | Error> {
|
2025-10-03 21:14:30 +00:00
|
|
|
if (!this.currentConversationPath) {
|
2025-11-17 19:02:15 +00:00
|
|
|
return;
|
2025-10-03 21:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-19 12:30:51 +00:00
|
|
|
const readResult = await this.readConversation(this.currentConversationPath);
|
2025-10-03 21:14:30 +00:00
|
|
|
|
2025-12-19 12:30:51 +00:00
|
|
|
if (readResult instanceof Error) {
|
|
|
|
|
return readResult;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-20 01:34:41 +00:00
|
|
|
// Queue this to execute silently in the background - it's just a best effort
|
|
|
|
|
this.deletionQueue = this.deletionQueue.then(() => this.attemptAIFileDeletion(readResult));
|
|
|
|
|
|
2025-12-19 12:30:51 +00:00
|
|
|
const deleteResult = await this.fileSystemService.deleteFile(this.currentConversationPath, true, false);
|
|
|
|
|
|
|
|
|
|
if (deleteResult instanceof Error) {
|
|
|
|
|
return deleteResult;
|
2025-10-03 21:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
this.resetCurrentConversation();
|
2025-10-03 21:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-05 12:06:05 +00:00
|
|
|
public async getAllConversations(): Promise<Conversation[]> {
|
2025-10-12 22:11:36 +00:00
|
|
|
const files = await this.fileSystemService.listFilesInDirectory(Path.Conversations, false, true);
|
2025-10-05 12:06:05 +00:00
|
|
|
const conversations: Conversation[] = [];
|
|
|
|
|
|
|
|
|
|
for (const file of files) {
|
2025-12-19 12:30:51 +00:00
|
|
|
const result = await this.readConversation(file.path);
|
|
|
|
|
if (result instanceof Conversation) {
|
|
|
|
|
conversations.push(result);
|
2025-10-05 12:06:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return conversations;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
public async updateConversationTitle(oldPath: string, newTitle: string): Promise<void | Error> {
|
2025-10-17 15:18:23 +00:00
|
|
|
const newPath = `${Path.Conversations}/${newTitle}.json`;
|
|
|
|
|
|
|
|
|
|
const result = await this.fileSystemService.moveFile(oldPath, newPath, true);
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
if (result instanceof Error) {
|
|
|
|
|
return result;
|
2025-10-17 15:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.currentConversationPath === oldPath) {
|
|
|
|
|
this.currentConversationPath = newPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-19 12:30:51 +00:00
|
|
|
private async readConversation(path: string): Promise<Conversation | Error> {
|
|
|
|
|
const result = await this.fileSystemService.readObjectFromFile(path, true);
|
|
|
|
|
|
|
|
|
|
if (result instanceof Error) {
|
|
|
|
|
Exception.log(result);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const conversation: Conversation = new Conversation();
|
|
|
|
|
|
|
|
|
|
if (Conversation.isConversationData(result)) {
|
|
|
|
|
conversation.title = result.title;
|
|
|
|
|
conversation.created = new Date(result.created);
|
|
|
|
|
conversation.updated = new Date(result.updated);
|
|
|
|
|
conversation.contents = result.contents.map(content => {
|
|
|
|
|
// Reconstruct Attachment instances from plain objects
|
|
|
|
|
const attachments = this.deserializeAttachments(content.attachments);
|
2025-12-23 12:04:29 +00:00
|
|
|
const references = this.deserializeReferences(content.references);
|
2025-12-19 12:30:51 +00:00
|
|
|
|
|
|
|
|
return new ConversationContent({
|
|
|
|
|
role: content.role,
|
|
|
|
|
timestamp: new Date(content.timestamp),
|
|
|
|
|
content: content.content,
|
|
|
|
|
displayContent: content.displayContent,
|
|
|
|
|
functionCall: content.functionCall,
|
|
|
|
|
functionResponse: content.functionResponse,
|
|
|
|
|
attachments: attachments,
|
2025-12-23 12:04:29 +00:00
|
|
|
references: references,
|
2025-12-19 12:30:51 +00:00
|
|
|
shouldDisplayContent: content.shouldDisplayContent,
|
|
|
|
|
toolId: content.toolId,
|
|
|
|
|
thoughtSignature: content.thoughtSignature,
|
|
|
|
|
errorType: content.errorType
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return conversation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private deserializeAttachments(attachmentsData: unknown): Attachment[] {
|
|
|
|
|
if (!Array.isArray(attachmentsData)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return attachmentsData
|
|
|
|
|
.filter(Attachment.isAttachmentData)
|
|
|
|
|
.map(attachmentData => new Attachment(
|
|
|
|
|
attachmentData.fileName,
|
|
|
|
|
attachmentData.mimeType,
|
|
|
|
|
attachmentData.base64,
|
|
|
|
|
attachmentData.fileID || {}
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 12:04:29 +00:00
|
|
|
private deserializeReferences(referencesData: unknown): Reference[] {
|
|
|
|
|
if (!Array.isArray(referencesData)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return referencesData
|
|
|
|
|
.filter(Reference.isReferenceData)
|
|
|
|
|
.map(referenceData => new Reference(
|
|
|
|
|
referenceData.fileName,
|
|
|
|
|
referenceData.size
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-19 12:30:51 +00:00
|
|
|
private async attemptAIFileDeletion(conversation: Conversation) {
|
|
|
|
|
try {
|
|
|
|
|
await this.aiFileService?.refreshCache();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
Exception.log(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const attachments = conversation.contents.map(c => c.attachments).flat();
|
|
|
|
|
for (const attachment of attachments) {
|
|
|
|
|
try {
|
|
|
|
|
await this.aiFileService?.deleteFile(attachment);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
Exception.log(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 20:20:24 +00:00
|
|
|
}
|