Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/backend/chatDrafts.ts
Original file line number Diff line number Diff line change
@@ -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<string, Conversation>();
const pending = new Map<string, Promise<void>>();

/**
* 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);
}
Expand All @@ -12,11 +35,10 @@ export function currentChat(projectId: string) {
export function flushChatDraft(projectId: string): Promise<void> {
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
Expand Down
57 changes: 57 additions & 0 deletions tests/chatDrafts.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>): 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<string, unknown> = {};
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, "写一段开场");
});
Loading