切换到使用pg数据库

This commit is contained in:
2026-05-28 18:22:39 +08:00
parent 4c47841483
commit 5ac50bfeaa
38 changed files with 2760 additions and 1331 deletions
+45 -47
View File
@@ -1,67 +1,53 @@
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { ConversationStore } from "../../src/conversations/store.js";
import { SessionHistoryStore } from "../../src/history/store.js";
import { createTestDatabase } from "../helpers/postgres.js";
describe("SessionHistoryStore", () => {
let tempDir: string;
let conversationStore: ConversationStore;
let store: SessionHistoryStore;
let dispose: (() => Promise<void>) | undefined;
beforeEach(async () => {
tempDir = await mkdtemp(join(tmpdir(), "tjwater-history-"));
store = new SessionHistoryStore(tempDir);
await store.initialize();
const testDb = await createTestDatabase();
conversationStore = new ConversationStore(testDb.db);
store = new SessionHistoryStore(testDb.db);
dispose = testDb.dispose;
await Promise.all([conversationStore.initialize(), store.initialize()]);
});
afterEach(async () => {
await rm(tempDir, { force: true, recursive: true });
await dispose?.();
});
it("falls back to legacy runtime-session transcripts by client session id and migrates on append", async () => {
await writeFile(
join(tempDir, "actor-1__project-1__runtime-session-1.json"),
JSON.stringify(
{
actorKey: "actor-1",
clientSessionId: "thread-1",
projectKey: "project-1",
sessionId: "runtime-session-1",
turns: [
{
id: "turn-1",
assistantMessage: "先检查泵站流量。",
timestamp: "2026-05-21T00:00:00.000Z",
toolCallCount: 1,
userMessage: "帮我看一下当前异常。",
},
],
updatedAt: "2026-05-21T00:00:00.000Z",
},
null,
2,
),
"utf8",
);
it("stores ordered turns for the durable conversation id", async () => {
await conversationStore.ensure({
actorKey: "actor-1",
projectId: "project-1",
projectKey: "project-1",
sessionId: "thread-1",
userId: "user-1",
});
const recentTurns = await store.getRecentTurns(
const first = await store.appendTurn(
{
actorKey: "actor-1",
clientSessionId: "thread-1",
projectKey: "project-1",
sessionId: "thread-1",
},
5,
{
assistantMessage: "先检查泵站流量。",
toolCallCount: 1,
userMessage: "帮我看一下当前异常。",
},
);
expect(recentTurns).toHaveLength(1);
expect(recentTurns[0]?.userMessage).toBe("帮我看一下当前异常。");
expect(first.turns).toHaveLength(1);
const transcript = await store.appendTurn(
const second = await store.appendTurn(
{
actorKey: "actor-1",
clientSessionId: "thread-1",
projectKey: "project-1",
sessionId: "thread-1",
},
@@ -72,15 +58,31 @@ describe("SessionHistoryStore", () => {
},
);
expect(transcript.sessionId).toBe("thread-1");
expect(transcript.turns).toHaveLength(2);
expect(second.sessionId).toBe("thread-1");
expect(second.turns).toHaveLength(2);
expect(second.turns[0]?.userMessage).toBe("帮我看一下当前异常。");
expect(second.turns[1]?.assistantMessage).toBe("已经定位到 3 条疑似异常支路。");
});
it("clones only the kept prefix when forking a thread", async () => {
await conversationStore.ensure({
actorKey: "actor-2",
projectId: "project-2",
projectKey: "project-2",
sessionId: "thread-source",
userId: "user-2",
});
await conversationStore.ensure({
actorKey: "actor-2",
projectId: "project-2",
projectKey: "project-2",
sessionId: "thread-fork",
userId: "user-2",
});
await store.appendTurn(
{
actorKey: "actor-2",
clientSessionId: "thread-source",
projectKey: "project-2",
sessionId: "thread-source",
},
@@ -93,7 +95,6 @@ describe("SessionHistoryStore", () => {
await store.appendTurn(
{
actorKey: "actor-2",
clientSessionId: "thread-source",
projectKey: "project-2",
sessionId: "thread-source",
},
@@ -107,13 +108,11 @@ describe("SessionHistoryStore", () => {
const cloned = await store.cloneThread(
{
actorKey: "actor-2",
clientSessionId: "thread-source",
projectKey: "project-2",
sessionId: "thread-source",
},
{
actorKey: "actor-2",
clientSessionId: "thread-fork",
projectKey: "project-2",
sessionId: "thread-fork",
},
@@ -126,7 +125,6 @@ describe("SessionHistoryStore", () => {
const forkRecentTurns = await store.getRecentTurns(
{
actorKey: "actor-2",
clientSessionId: "thread-fork",
projectKey: "project-2",
sessionId: "thread-fork",
},