优化标题生成功能
Agent CI/CD / docker-image (push) Successful in 21s
Agent CI/CD / deploy-fallback-log (push) Has been skipped

This commit is contained in:
2026-05-22 14:20:27 +08:00
parent ab12d79d91
commit 4c47841483
3 changed files with 70 additions and 6 deletions
+36 -1
View File
@@ -1,6 +1,10 @@
import { describe, expect, it } from "bun:test";
import { shouldGenerateSessionTitle } from "../../src/routes/chatSession.js";
import {
generateSessionTitle,
shouldGenerateSessionTitle,
} from "../../src/routes/chatSession.js";
import { type OpencodeRuntimeAdapter } from "../../src/runtime/opencode.js";
describe("shouldGenerateSessionTitle", () => {
it("allows auto-title generation for the first turn when the title was not edited", () => {
@@ -36,3 +40,34 @@ describe("shouldGenerateSessionTitle", () => {
).toBe(false);
});
});
describe("generateSessionTitle", () => {
it("uses the current user and assistant turn instead of reading wrapped runtime context", async () => {
let titlePrompt = "";
const runtime = {
createSession: async () => ({ id: "title-session" }),
prompt: async (_sessionId: string, prompt: string) => {
titlePrompt = prompt;
},
waitForSessionIdle: async () => undefined,
messages: async () => [
{
info: { role: "assistant" },
parts: [{ type: "text", text: "标题:泵站压力异常排查。" }],
},
],
abortSession: async () => undefined,
} as unknown as OpencodeRuntimeAdapter;
const title = await generateSessionTitle(runtime, {
sessionId: "chat-session",
latestUserMessage: "检查一下三号泵站最近压力波动的原因",
latestAssistantMessage: "三号泵站压力波动主要与夜间阀门开度变化有关。",
fallbackTitle: "新对话",
});
expect(title).toBe("泵站压力异常排查");
expect(titlePrompt).toContain("用户:检查一下三号泵站最近压力波动的原因");
expect(titlePrompt).toContain("助手:三号泵站压力波动主要与夜间阀门开度变化有关。");
});
});