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-10-13 12:14:54 +00:00
|
|
|
import { Copy } from "Enums/Copy";
|
2025-09-30 20:20:24 +00:00
|
|
|
|
|
|
|
|
export class ConversationFileSystemService {
|
|
|
|
|
|
|
|
|
|
private fileSystemService: FileSystemService;
|
|
|
|
|
private currentConversationPath: string | null = null;
|
|
|
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
|
this.fileSystemService = Resolve<FileSystemService>(Services.FileSystemService);
|
|
|
|
|
}
|
|
|
|
|
|
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-10-04 12:10:22 +00:00
|
|
|
public async saveConversation(conversation: Conversation): Promise<string> {
|
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-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-13 12:14:54 +00:00
|
|
|
contents: conversation.contents
|
|
|
|
|
.filter(content => content.content !== Copy.ApiRequestAborted)
|
|
|
|
|
.map(content => ({
|
|
|
|
|
id: content.id,
|
|
|
|
|
role: content.role,
|
|
|
|
|
content: content.content,
|
|
|
|
|
timestamp: content.timestamp.toISOString(),
|
|
|
|
|
isFunctionCall: content.isFunctionCall,
|
|
|
|
|
isFunctionCallResponse: content.isFunctionCallResponse
|
|
|
|
|
}))
|
2025-09-30 20:20:24 +00:00
|
|
|
};
|
|
|
|
|
|
2025-10-12 22:11:36 +00:00
|
|
|
await this.fileSystemService.writeObjectToFile(this.currentConversationPath, conversationData, true);
|
2025-09-30 20:20:24 +00:00
|
|
|
return this.currentConversationPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public resetCurrentConversation(): void {
|
|
|
|
|
this.currentConversationPath = null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 12:37:28 +00:00
|
|
|
public getCurrentConversationPath(): string | null {
|
|
|
|
|
return this.currentConversationPath;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 13:15:04 +00:00
|
|
|
public setCurrentConversationPath(filePath: string): void {
|
|
|
|
|
this.currentConversationPath = filePath;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 21:14:30 +00:00
|
|
|
public async deleteCurrentConversation(): Promise<boolean> {
|
|
|
|
|
if (!this.currentConversationPath) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-12 22:11:36 +00:00
|
|
|
const deleted = await this.fileSystemService.deleteFile(this.currentConversationPath, true);
|
2025-10-03 21:14:30 +00:00
|
|
|
|
|
|
|
|
if (deleted) {
|
|
|
|
|
this.resetCurrentConversation();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return deleted;
|
|
|
|
|
}
|
|
|
|
|
|
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-10-12 22:11:36 +00:00
|
|
|
const data = await this.fileSystemService.readObjectFromFile(file.path, true);
|
2025-10-05 12:06:05 +00:00
|
|
|
if (Conversation.isConversationData(data)) {
|
|
|
|
|
const conversation: Conversation = new Conversation();
|
|
|
|
|
conversation.title = data.title;
|
|
|
|
|
conversation.created = new Date(data.created);
|
|
|
|
|
conversation.contents = data.contents.map(content => {
|
2025-10-06 22:01:20 +00:00
|
|
|
return new ConversationContent(
|
2025-10-12 12:07:54 +00:00
|
|
|
content.role, content.content, new Date(content.timestamp), content.isFunctionCall, content.isFunctionCallResponse, content.id);
|
2025-10-05 12:06:05 +00:00
|
|
|
});
|
|
|
|
|
conversations.push(conversation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return conversations;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 20:20:24 +00:00
|
|
|
}
|