revert: remove unused Agent PostgreSQL persistence

This commit is contained in:
TJWater CI
2026-08-07 18:06:08 +08:00
parent 649af949c5
commit 2f267af7a3
39 changed files with 1330 additions and 2760 deletions
+21 -69
View File
@@ -1,6 +1,12 @@
import { type QueryResultRow } from "pg";
import { join } from "node:path";
import { AgentDatabase, getAgentDatabase } from "../db/index.js";
import { config } from "../config.js";
import {
atomicWriteJson,
ensureDirectory,
readJsonFile,
removeFileIfExists,
} from "../utils/fileStore.js";
export type ConversationStateRecord = {
sessionId: string;
@@ -9,81 +15,27 @@ export type ConversationStateRecord = {
branchGroups: unknown[];
};
type ConversationStateRow = QueryResultRow & {
session_id: string;
is_title_manually_edited: boolean;
messages: unknown[];
branch_groups: unknown[];
};
export class ConversationStateStore {
constructor(private readonly db: AgentDatabase = getAgentDatabase()) {}
constructor(private readonly baseDir = config.CONVERSATION_STATE_STORAGE_DIR) {}
async initialize() {
await this.db.initialize();
await ensureDirectory(this.baseDir);
}
async read(sessionId: string) {
const result = await this.db.query<ConversationStateRow>(
`
SELECT session_id, is_title_manually_edited, messages, branch_groups
FROM ${this.db.table("conversation_states")}
WHERE session_id = $1
LIMIT 1
`,
[sessionId],
);
return mapConversationStateRow(result.rows[0]);
async read(sessionScopeKey: string) {
return await readJsonFile<ConversationStateRecord>(this.filePath(sessionScopeKey));
}
async write(sessionId: string, state: ConversationStateRecord) {
const result = await this.db.query<ConversationStateRow>(
`
INSERT INTO ${this.db.table("conversation_states")} (
session_id,
is_title_manually_edited,
messages,
branch_groups
)
VALUES ($1, $2, $3::jsonb, $4::jsonb)
ON CONFLICT (session_id)
DO UPDATE SET
is_title_manually_edited = EXCLUDED.is_title_manually_edited,
messages = EXCLUDED.messages,
branch_groups = EXCLUDED.branch_groups
RETURNING session_id, is_title_manually_edited, messages, branch_groups
`,
[
sessionId,
state.isTitleManuallyEdited ?? false,
JSON.stringify(state.messages),
JSON.stringify(state.branchGroups),
],
);
return mapConversationStateRow(result.rows[0]) ?? state;
async write(sessionScopeKey: string, state: ConversationStateRecord) {
await atomicWriteJson(this.filePath(sessionScopeKey), state);
return state;
}
async remove(sessionId: string) {
await this.db.query(
`
DELETE FROM ${this.db.table("conversation_states")}
WHERE session_id = $1
`,
[sessionId],
);
async remove(sessionScopeKey: string) {
await removeFileIfExists(this.filePath(sessionScopeKey));
}
private filePath(sessionScopeKey: string) {
return join(this.baseDir, `${sessionScopeKey}.json`);
}
}
const mapConversationStateRow = (
row?: ConversationStateRow | null,
): ConversationStateRecord | null => {
if (!row) {
return null;
}
return {
sessionId: row.session_id,
isTitleManuallyEdited: row.is_title_manually_edited,
messages: Array.isArray(row.messages) ? row.messages : [],
branchGroups: Array.isArray(row.branch_groups) ? row.branch_groups : [],
};
};