diff --git a/src/backend/chatDrafts.ts b/src/backend/chatDrafts.ts index 4c23f1cd..fc5615c0 100644 --- a/src/backend/chatDrafts.ts +++ b/src/backend/chatDrafts.ts @@ -1,8 +1,31 @@ -import { saveChatDraft, type Conversation } from "./chat.ts"; +import { saveChatDraft, type ChatDraft, type Conversation } from "./chat.ts"; // Only unsaved UI drafts live here; the backend owns persisted conversation state. const latest = new Map(); const pending = new Map>(); + +/** + * The draft endpoint validates strictly and rejects unknown members, so send + * the fields it owns instead of forwarding whatever the view happens to hold. + * `activeTurnId` in particular is only present on a conversation read while a + * reply is running; echoing it back made every autosave fail with a 422. + */ +function draftOf(value: Conversation): ChatDraft { + return { + conversationId: value.conversationId, + draft: value.draft, + draftContent: value.draftContent, + assets: value.assets, + thinking: value.thinking, + builtinModelThinking: value.builtinModelThinking, + modelSelections: value.modelSelections, + runtime: value.runtime, + runtimeModel: value.runtimeModel, + harnessModels: value.harnessModels, + skills: value.skills, + }; +} + export function rememberChat(projectId: string, value: Conversation) { latest.set(projectId, value); } @@ -12,11 +35,10 @@ export function currentChat(projectId: string) { export function flushChatDraft(projectId: string): Promise { const value = latest.get(projectId); if (!value) return pending.get(projectId) ?? Promise.resolve(); - const { messages: _messages, ...draft } = value; const next = (pending.get(projectId) ?? Promise.resolve()) .catch(() => {}) .then(async () => { - await saveChatDraft(projectId, draft); + await saveChatDraft(projectId, draftOf(value)); }); pending.set(projectId, next); void next diff --git a/tests/chatDrafts.test.ts b/tests/chatDrafts.test.ts new file mode 100644 index 00000000..95aa203b --- /dev/null +++ b/tests/chatDrafts.test.ts @@ -0,0 +1,57 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import type { Conversation } from "../src/backend/chat.ts"; +import { flushChatDraft, rememberChat } from "../src/backend/chatDrafts.ts"; + +const conversation = (extra: Record): Conversation => + ({ + conversationId: "conversation-1", + draft: "写一段开场", + draftContent: null, + assets: [], + thinking: "auto", + builtinModelThinking: {}, + modelSelections: {}, + runtime: "default", + runtimeModel: null, + harnessModels: {}, + skills: [], + messages: [], + ...extra, + }) as unknown as Conversation; + +test("the saved draft sends only the fields the draft endpoint takes", async (t) => { + let body: Record = {}; + t.mock.method( + globalThis, + "fetch", + async (url: string, init?: RequestInit) => { + assert.ok(url.endsWith("/assistant/draft")); + assert.equal(init?.method, "PUT"); + body = JSON.parse(String(init?.body)); + return new Response(JSON.stringify({}), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + }, + ); + // A conversation read while a reply is running carries activeTurnId. + rememberChat("project-1", conversation({ activeTurnId: "turn-1" })); + await flushChatDraft("project-1"); + assert.ok(!("activeTurnId" in body), "activeTurnId must not be echoed back"); + assert.ok(!("messages" in body), "the transcript is not part of a draft"); + assert.deepEqual(Object.keys(body).sort(), [ + "assets", + "builtinModelThinking", + "conversationId", + "draft", + "draftContent", + "harnessModels", + "modelSelections", + "runtime", + "runtimeModel", + "skills", + "thinking", + ]); + assert.equal(body.draft, "写一段开场"); +});