feat: initialize experimental agent repo

This commit is contained in:
2026-06-30 14:03:49 +08:00
commit e83ac54a9e
75 changed files with 11690 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { SessionMetadataStore } from "../../src/sessions/metadataStore.js";
describe("SessionMetadataStore", () => {
let tempDir: string;
let store: SessionMetadataStore;
beforeEach(async () => {
tempDir = await mkdtemp(join(tmpdir(), "tjwater-session-"));
store = new SessionMetadataStore(tempDir);
await store.initialize();
});
afterEach(async () => {
await rm(tempDir, { force: true, recursive: true });
});
it("persists the provided opencode session id", async () => {
const { record, created } = await store.ensure({
actorKey: "actor-1",
projectId: "project-1",
projectKey: "project-key-1",
sessionId: "opencode-session-1",
userId: "user-1",
});
expect(created).toBe(true);
expect(record.sessionId).toBe("opencode-session-1");
expect(record.ownerUserId).toBe("user-1");
expect(record.status).toBe("active");
});
it("touches metadata and preserves scoped ownership", async () => {
const { record } = await store.ensure({
actorKey: "actor-2",
projectId: "project-2",
projectKey: "project-key-2",
sessionId: "existing-session",
userId: "user-2",
});
const touched = await store.touch(record, {
title: "新标题",
});
expect(touched.title).toBe("新标题");
expect(touched.updatedAt >= record.updatedAt).toBe(true);
const fetched = await store.get(
{
actorKey: "actor-2",
projectId: "project-2",
projectKey: "project-key-2",
userId: "user-2",
},
"existing-session",
);
expect(fetched?.title).toBe("新标题");
});
});
+114
View File
@@ -0,0 +1,114 @@
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { SessionTranscriptStore } from "../../src/sessions/transcriptStore.js";
describe("SessionTranscriptStore", () => {
let tempDir: string;
let store: SessionTranscriptStore;
beforeEach(async () => {
tempDir = await mkdtemp(join(tmpdir(), "tjwater-transcript-"));
store = new SessionTranscriptStore(tempDir);
await store.initialize();
});
afterEach(async () => {
await rm(tempDir, { force: true, recursive: true });
});
it("clones only the kept prefix when forking a thread", async () => {
await store.appendTurn(
{
actorKey: "actor-2",
clientSessionId: "thread-source",
projectKey: "project-2",
sessionId: "thread-source",
},
{
assistantMessage: "第一轮回复",
toolCallCount: 0,
userMessage: "第一轮提问",
},
);
await store.appendTurn(
{
actorKey: "actor-2",
clientSessionId: "thread-source",
projectKey: "project-2",
sessionId: "thread-source",
},
{
assistantMessage: "第二轮回复",
toolCallCount: 0,
userMessage: "第二轮提问",
},
);
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",
},
2,
);
expect(cloned.turns).toHaveLength(1);
expect(cloned.turns[0]?.userMessage).toBe("第一轮提问");
const forkRecentTurns = await store.getRecentTurns(
{
actorKey: "actor-2",
clientSessionId: "thread-fork",
projectKey: "project-2",
sessionId: "thread-fork",
},
5,
);
expect(forkRecentTurns).toHaveLength(1);
expect(forkRecentTurns[0]?.assistantMessage).toBe("第一轮回复");
});
it("does not duplicate the latest turn when the frontend state is saved again", async () => {
await store.appendTurn(
{
actorKey: "actor-3",
clientSessionId: "thread-3",
projectKey: "project-3",
sessionId: "thread-3",
},
{
assistantMessage: "已完成压力波动分析。",
toolCallCount: 1,
userMessage: "分析压力波动。",
},
);
const transcript = await store.appendTurn(
{
actorKey: "actor-3",
clientSessionId: "thread-3",
projectKey: "project-3",
sessionId: "thread-3",
},
{
assistantMessage: "已完成压力波动分析。",
toolCallCount: 2,
userMessage: "分析压力波动。",
},
);
expect(transcript.turns).toHaveLength(1);
expect(transcript.turns[0]?.toolCallCount).toBe(2);
});
});