46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import { join } from "node:path";
|
|
|
|
import { config } from "../config.js";
|
|
import {
|
|
atomicWriteJson,
|
|
ensureDirectory,
|
|
readJsonFile,
|
|
removeFileIfExists,
|
|
slugify,
|
|
} from "../utils/fileStore.js";
|
|
|
|
export type SessionUiStateRecord = {
|
|
sessionId: string;
|
|
isTitleManuallyEdited?: boolean;
|
|
messages: unknown[];
|
|
};
|
|
|
|
type SessionUiStateContext = {
|
|
sessionId: string;
|
|
};
|
|
|
|
export class SessionUiStateStore {
|
|
constructor(private readonly baseDir = config.SESSION_UI_STATE_STORAGE_DIR) {}
|
|
|
|
async initialize() {
|
|
await ensureDirectory(this.baseDir);
|
|
}
|
|
|
|
async read(context: SessionUiStateContext) {
|
|
return await readJsonFile<SessionUiStateRecord>(this.filePath(context));
|
|
}
|
|
|
|
async write(context: SessionUiStateContext, state: SessionUiStateRecord) {
|
|
await atomicWriteJson(this.filePath(context), state);
|
|
return state;
|
|
}
|
|
|
|
async remove(context: SessionUiStateContext) {
|
|
await removeFileIfExists(this.filePath(context));
|
|
}
|
|
|
|
private filePath(context: SessionUiStateContext) {
|
|
return join(this.baseDir, `${slugify(context.sessionId)}.json`);
|
|
}
|
|
}
|