42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import { join } from "node:path";
|
|
|
|
import { config } from "../config.js";
|
|
import {
|
|
atomicWriteJson,
|
|
ensureDirectory,
|
|
readJsonFile,
|
|
removeFileIfExists,
|
|
} from "../utils/fileStore.js";
|
|
|
|
export type ConversationStateRecord = {
|
|
sessionId: string;
|
|
isTitleManuallyEdited?: boolean;
|
|
messages: unknown[];
|
|
branchGroups: unknown[];
|
|
};
|
|
|
|
export class ConversationStateStore {
|
|
constructor(private readonly baseDir = config.CONVERSATION_STATE_STORAGE_DIR) {}
|
|
|
|
async initialize() {
|
|
await ensureDirectory(this.baseDir);
|
|
}
|
|
|
|
async read(sessionScopeKey: string) {
|
|
return await readJsonFile<ConversationStateRecord>(this.filePath(sessionScopeKey));
|
|
}
|
|
|
|
async write(sessionScopeKey: string, state: ConversationStateRecord) {
|
|
await atomicWriteJson(this.filePath(sessionScopeKey), state);
|
|
return state;
|
|
}
|
|
|
|
async remove(sessionScopeKey: string) {
|
|
await removeFileIfExists(this.filePath(sessionScopeKey));
|
|
}
|
|
|
|
private filePath(sessionScopeKey: string) {
|
|
return join(this.baseDir, `${sessionScopeKey}.json`);
|
|
}
|
|
}
|