重构会话管理,简化上下文存储逻辑
This commit is contained in:
@@ -44,9 +44,11 @@ describe("ConversationStore", () => {
|
||||
|
||||
const touched = await store.touch(record, {
|
||||
title: "新标题",
|
||||
opencodeSessionId: "opencode-session-1",
|
||||
});
|
||||
|
||||
expect(touched.title).toBe("新标题");
|
||||
expect(touched.opencodeSessionId).toBe("opencode-session-1");
|
||||
expect(touched.updatedAt >= record.updatedAt).toBe(true);
|
||||
|
||||
const fetched = await store.get(
|
||||
@@ -58,7 +60,7 @@ describe("ConversationStore", () => {
|
||||
},
|
||||
"existing-session",
|
||||
);
|
||||
expect(fetched?.sessionScopeKey).toBe(record.sessionScopeKey);
|
||||
expect(fetched?.title).toBe("新标题");
|
||||
expect(fetched?.opencodeSessionId).toBe("opencode-session-1");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
|
||||
import {
|
||||
buildPromptWithLearningContext,
|
||||
generateSessionTitle,
|
||||
shouldGenerateSessionTitle,
|
||||
} from "../../src/routes/chatSession.js";
|
||||
import { type SessionTurnRecord } from "../../src/history/store.js";
|
||||
import { type MemoryStore } from "../../src/memory/store.js";
|
||||
import { type OpencodeRuntimeAdapter } from "../../src/runtime/opencode.js";
|
||||
|
||||
describe("shouldGenerateSessionTitle", () => {
|
||||
@@ -71,3 +74,90 @@ describe("generateSessionTitle", () => {
|
||||
expect(titlePrompt).toContain("助手:三号泵站压力波动主要与夜间阀门开度变化有关。");
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildPromptWithLearningContext", () => {
|
||||
const memoryStore = {
|
||||
buildPromptSnapshot: async () => "",
|
||||
} as unknown as MemoryStore;
|
||||
|
||||
it("prefers persisted frontend messages so aborted turns remain in restored context", async () => {
|
||||
const prompt = await buildPromptWithLearningContext(
|
||||
memoryStore,
|
||||
"actor-1",
|
||||
"project-1",
|
||||
{
|
||||
recentTurns: [],
|
||||
persistedMessages: [
|
||||
{ role: "user", content: "先分析 3 号泵站夜间压力波动" },
|
||||
{
|
||||
role: "assistant",
|
||||
content: "已定位到夜间阀门开度变化与压力波动时间段重合,下一步准备对比相邻支路。",
|
||||
isError: true,
|
||||
},
|
||||
{ role: "assistant", content: "⚠️ **请求已中断**", isError: true },
|
||||
],
|
||||
message: "继续刚才的分析,并补充相邻支路影响",
|
||||
},
|
||||
);
|
||||
|
||||
expect(prompt).toContain("用户:先分析 3 号泵站夜间压力波动");
|
||||
expect(prompt).toContain(
|
||||
"助手:已定位到夜间阀门开度变化与压力波动时间段重合,下一步准备对比相邻支路。",
|
||||
);
|
||||
expect(prompt).not.toContain("⚠️ **请求已中断**");
|
||||
expect(prompt).toContain("[Current user request]\n继续刚才的分析,并补充相邻支路影响");
|
||||
});
|
||||
|
||||
it("falls back to history turns when frontend state is unavailable", async () => {
|
||||
const recentTurns: SessionTurnRecord[] = [
|
||||
{
|
||||
id: "turn-1",
|
||||
userMessage: "检查 DMA-2 夜间漏损异常",
|
||||
assistantMessage: "DMA-2 在 02:00-04:00 出现持续最小夜流抬升。",
|
||||
timestamp: new Date().toISOString(),
|
||||
toolCallCount: 1,
|
||||
},
|
||||
];
|
||||
|
||||
const prompt = await buildPromptWithLearningContext(
|
||||
memoryStore,
|
||||
"actor-1",
|
||||
"project-1",
|
||||
{
|
||||
recentTurns,
|
||||
message: "继续给出排查建议",
|
||||
},
|
||||
);
|
||||
|
||||
expect(prompt).toContain("用户:检查 DMA-2 夜间漏损异常");
|
||||
expect(prompt).toContain("助手:DMA-2 在 02:00-04:00 出现持续最小夜流抬升。");
|
||||
});
|
||||
|
||||
it("skips restored conversation injection when reusing an existing opencode session", async () => {
|
||||
const prompt = await buildPromptWithLearningContext(
|
||||
memoryStore,
|
||||
"actor-1",
|
||||
"project-1",
|
||||
{
|
||||
recentTurns: [
|
||||
{
|
||||
id: "turn-1",
|
||||
userMessage: "上一轮问题",
|
||||
assistantMessage: "上一轮回答",
|
||||
timestamp: new Date().toISOString(),
|
||||
toolCallCount: 0,
|
||||
},
|
||||
],
|
||||
persistedMessages: [
|
||||
{ role: "user", content: "旧问题" },
|
||||
{ role: "assistant", content: "旧回答" },
|
||||
],
|
||||
message: "基于刚才结果继续分析",
|
||||
restoreConversation: false,
|
||||
},
|
||||
);
|
||||
|
||||
expect(prompt).not.toContain("[Previous conversation context]");
|
||||
expect(prompt).toBe("基于刚才结果继续分析");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,10 +3,7 @@ import { mkdtemp, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import {
|
||||
buildToolSessionScopeKey,
|
||||
ToolSessionContextStore,
|
||||
} from "../../src/session/toolContextStore.js";
|
||||
import { ToolSessionContextStore } from "../../src/session/toolContextStore.js";
|
||||
|
||||
describe("ToolSessionContextStore", () => {
|
||||
let tempDir: string;
|
||||
@@ -22,14 +19,9 @@ describe("ToolSessionContextStore", () => {
|
||||
await rm(tempDir, { force: true, recursive: true });
|
||||
});
|
||||
|
||||
it("writes interactive aliases under scoped session keys", async () => {
|
||||
const sessionScopeKey = buildToolSessionScopeKey(
|
||||
"actor-1",
|
||||
"project-1",
|
||||
"chat-session-1",
|
||||
);
|
||||
|
||||
it("writes and reads runtime session context by opencode session id", async () => {
|
||||
await store.write({
|
||||
accessToken: "token-1",
|
||||
actorKey: "actor-1",
|
||||
allowLearningWrite: true,
|
||||
clientSessionId: "chat-session-1",
|
||||
@@ -37,15 +29,13 @@ describe("ToolSessionContextStore", () => {
|
||||
projectId: "project-id-1",
|
||||
projectKey: "project-1",
|
||||
sessionId: "runtime-session-1",
|
||||
sessionScopeKey,
|
||||
traceId: "trace-1",
|
||||
});
|
||||
|
||||
const runtimeContext = await store.read("runtime-session-1");
|
||||
const scopedContext = await store.read(sessionScopeKey);
|
||||
|
||||
expect(runtimeContext?.accessToken).toBe("token-1");
|
||||
expect(runtimeContext?.clientSessionId).toBe("chat-session-1");
|
||||
expect(scopedContext?.sessionScopeKey).toBe(sessionScopeKey);
|
||||
expect(scopedContext?.sessionId).toBe("runtime-session-1");
|
||||
expect(runtimeContext?.sessionId).toBe("runtime-session-1");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user