From 85a6fd5485e82ee9d819892865c085edc9d15d98 Mon Sep 17 00:00:00 2001 From: ninon <284425649+ninondev@users.noreply.github.com> Date: Mon, 2 Nov 2026 10:45:17 -0800 Subject: [PATCH] chat 0.1.11: attachments reach the model, saves serialized, tool history by round (#645) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By ninondev (#645). Squashed at the reviewed head 764fbfc. Changes in packages/chat, 0.1.10 -> 0.1.11: - Attachments reach the model: - A .md, .markdown or .txt file, or any text/* file, is sent as UTF-8 text in a 【文档:name】 block. Bad bytes are an error, not mojibake. - An image's data URL gets its real MIME type, even when the browser said application/octet-stream or gave no type. - Send waits for every upload still in progress, not only the books. - A failed upload is dropped with a toast. It no longer goes in as a local copy that skipped the size caps. - Conversation saves: - Writes are serialized per conversation id. Once a delete has gone through, any write still queued is skipped. - A new conversation gets its id on the client up front. A save that finishes after the user has switched conversations no longer takes over the open one. - Opening one conversation and then another quickly no longer shows the first. - Tool history: - The server sends a "round" event each turn of the tool loop, and the history is replayed round by round. - Tool-call ids are unique across rounds (call__). - Each tool result is capped at 60000 chars, the same in the stream and in the history. - A stop is checked before each tool call and after the MCP read. - A connection-error card is not replayed to the model. - epub-reader.test.mjs launches Chromium with --use-mock-keychain. Run at 764fbfc on mote (macOS), each with HOME=$(mktemp -d): - `bun test` (whole repo): 4857 pass, 14 skip, 0 fail, across 370 files. - `bun test packages/chat`: 68 pass, 1 skip, 0 fail. - `bun scripts/check.mjs`: exits 0. - The epub browser test with EPUB_TEST_BROWSER set to Playwright's Chromium: 2 of 2 pass. - A review-only end-to-end run (not committed) in Playwright Chromium and WebKit 1.62, at a 1100px width, with the real chat server and a fake gateway: - It attaches scene.md (no MIME type) and photo.png (application/octet-stream), types a question and sends. - It checks that the gateway got the markdown text, an image/png data URL and the typed text. - It reloads, reopens the conversation from the sidebar and asks a follow-up. The history still has the markdown and the image, and there is exactly one saved conversation. - All checks pass in both engines. With origin/main's packages/chat the same run fails 8 checks: the markdown and the image never reach the gateway. Broken by hand: - Without the per-round tool-call id dedupe, "tool history preserves round order ... repeated IDs" goes red. - With only the first throwIfAborted removed, the stop tests stay green, because the second check covers it. Read for harm: no new hosts, no credential or keychain access, no dependencies. Not tried: a real model or real MCP servers behind magpie's gateway. --- packages/chat/chat.test.mjs | 705 ++++++++++++++++++++++++++++- packages/chat/chat/app.js | 163 +++++-- packages/chat/epub-reader.test.mjs | 2 +- packages/chat/lib/server.mjs | 65 ++- packages/chat/package.json | 2 +- 5 files changed, 857 insertions(+), 80 deletions(-) diff --git a/packages/chat/chat.test.mjs b/packages/chat/chat.test.mjs index 53aabe2..bf3ae42 100644 --- a/packages/chat/chat.test.mjs +++ b/packages/chat/chat.test.mjs @@ -21,9 +21,11 @@ delete process.env.MAGPIE_GATEWAY_KEY delete process.env.MAGPIE_ADDR const seen = [] +let chatReply = null // magpie's own MCP servers the fake gateway lists on /v1/magpie/mcp; null // answers 404, as a magpie from before the list does let ownList = null +let ownListReply = null const ownAuth = [] const gateway = Bun.serve({ port: 0, @@ -31,6 +33,7 @@ const gateway = Bun.serve({ async fetch(req) { const url = new URL(req.url) if (url.pathname === "/v1/magpie/mcp") { + if (ownListReply) return ownListReply(); return ownList ? Response.json({ servers: ownList }) : new Response("no", { status: 404 }) } if (url.pathname.startsWith("/mcp/magpie/")) { @@ -49,6 +52,7 @@ const gateway = Bun.serve({ if (url.pathname === "/v1/models") return Response.json({ data: [{ id: "fake/model", owned_by: "fake" }] }) if (url.pathname === "/v1/chat/completions") { seen.push({ auth: req.headers.get("authorization"), body: await req.json() }) + if (chatReply) return chatReply(seen.at(-1).body) const chunk = (o) => `data: ${JSON.stringify(o)}\n\n` const body = chunk({ choices: [{ delta: { content: "hel" } }] }) + chunk({ choices: [{ delta: { content: "lo" }, finish_reason: "stop" }] }) @@ -154,10 +158,10 @@ test("with the token, a chat streams from the gateway with the caller key", asyn function chatPage(setup = "") { const page = { document: { readyState: "loading", querySelector: () => ({ getAttribute: () => token }), addEventListener() {} }, - TextDecoder, + TextDecoder, atob, AbortController, fetch: (path, init) => { - if (path !== "api/chat") throw new Error("unexpected request: " + path) - return fetch(`http://127.0.0.1:${port}/api/chat`, init) + if (!["api/chat", "api/upload", "api/conversations", "api/settings"].includes(path) && !path.startsWith("api/conversations/")) throw new Error("unexpected request: " + path) + return fetch(`http://127.0.0.1:${port}/${path}`, init) }, window: { addEventListener() {} }, location: { search: "" }, MagpieEpub: { isEpub: f => f.name?.endsWith(".epub") }, @@ -166,6 +170,505 @@ function chatPage(setup = "") { return page } +function attachmentPage(setup = "") { + const page = chatPage(`renderAttachments = renderMessages = renderSessions = scrollBottom = () => {};\n${setup}`) + const nodes = new Map() + page.document.querySelector = selector => { + if (!nodes.has(selector)) nodes.set(selector, { + value: "", getAttribute: () => token, setAttribute() {}, focus() {}, classList: { toggle() {} }, + }) + return nodes.get(selector) + } + return page +} + +test.each(["", "application/octet-stream"])("an image with MIME type '%s' finishes loading before send and reaches the gateway", async type => { + const page = attachmentPage(` + readAsDataURL = () => globalThis.reading; + toast = message => globalThis.warnings.push(message); + `) + page.warnings = [] + page.document.querySelector("#input").value = "请看图片" + const reading = Promise.withResolvers() + page.reading = reading.promise + const job = page.addFiles([{ name: "scene.PNG", type, size: 12 }]) + expect(page.document.querySelector("#send").disabled).toBe(true) + page.send() + expect(page.warnings).toEqual(["附件还在读取,请稍候。"]) + const data = "data:application/octet-stream;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aGDcAAAAASUVORK5CYII=" + reading.resolve(data) + await job + expect(page.document.querySelector("#send").disabled).toBe(false) + expect(page.state.attachments).toHaveLength(1) + expect(page.state.attachments[0].kind).toBe("image") + const h = { ...json, "x-magpie-chat-token": token } + const original = { name: "scene.PNG", type, kind: "file", data } + const saved = JSON.parse((await raw("POST", "/api/conversations", h, { messages: [ + { role: "user", content: "", files: [original] }, + ] })).body) + const history = JSON.parse((await raw("GET", "/api/conversations/" + saved.id, h)).body).messages + for (const messages of [[{ role: "user", content: "", files: page.state.attachments }], + [...history, { role: "assistant", content: "A picture." }, { role: "user", content: "What color?" }]]) { + const before = seen.length + await page.streamChat({ ...chatBody, messages }, () => {}) + expect(seen.length).toBe(before + 1) + expect(seen.at(-1).body.messages[0].content).toEqual([ + { type: "image_url", image_url: { url: data.replace("application/octet-stream", "image/png") } }, + ]) + } + expect(history[0].files[0]).toEqual(original) +}) + +test("a rejected upload does not appear as an attached file", async () => { + const page = attachmentPage(` + readAsDataURL = async () => "data:image/png;base64,cG5n"; + api = async () => { throw new Error("图片太大"); }; + toast = message => globalThis.warnings.push(message); + `) + page.warnings = [] + await page.addFiles([{ name: "large.png", type: "image/png", size: 12 }]) + expect(page.state.attachments).toEqual([]) + expect(page.warnings).toEqual(["large.png:图片太大"]) + expect(page.document.querySelector("#send").disabled).toBe(true) + page.document.querySelector("#input").value = "继续" + page.updateSend() + expect(page.document.querySelector("#send").disabled).toBe(false) +}) + +test("overlapping uploads wait for every batch and cannot leak across conversations", async () => { + const page = attachmentPage(` + readAsDataURL = file => globalThis.reads.get(file.name).promise; + api = async (path, options) => path === "api/upload" + ? { ...options.body, kind: "image" } : { id: "other", messages: [] }; + toast = message => globalThis.warnings.push(message); + `) + page.warnings = [] + page.reads = new Map(["first.png", "second.png", "old.png", "new.png"].map(name => [name, Promise.withResolvers()])) + const add = name => page.addFiles([{ name, type: "image/png", size: 12 }]) + const first = add("first.png"), second = add("second.png") + const button = page.document.querySelector("#send") + expect(button.disabled).toBe(true) + page.reads.get("second.png").resolve("data:image/png;base64,c2Vjb25k") + await second + expect(page.state.attachments.map(f => f.name)).toEqual(["second.png"]) + expect(button.disabled).toBe(true) + page.send() + expect(page.warnings).toEqual(["附件还在读取,请稍候。"]) + page.reads.get("first.png").resolve("data:image/png;base64,Zmlyc3Q=") + await first + expect(button.disabled).toBe(false) + expect(page.state.attachments.map(f => f.name)).toEqual(["second.png", "first.png"]) + + const old = add("old.png") + await page.openConversation("other") + expect(page.state.convId).toBe("other") + expect(page.state.attachments).toEqual([]) + const fresh = add("new.png") + page.reads.get("old.png").resolve("data:image/png;base64,b2xk") + await old + expect(page.state.attachments).toEqual([]) + expect(button.disabled).toBe(true) + page.reads.get("new.png").resolve("data:image/png;base64,bmV3") + await fresh + expect(page.state.attachments.map(f => f.name)).toEqual(["new.png"]) + expect(button.disabled).toBe(false) +}) + +test.each([ + ["photo.jpg", "application/octet-stream"], + ["photo.txt", "image/png"], +])("an image named %s keeps its data format through upload, send and a saved follow-up", async (name, type) => { + const page = attachmentPage(` + readAsDataURL = file => Promise.resolve(globalThis.dataByName[file.name]); + buildTurn = msg => ({ msg, root: {}, err: {} }); + autoGrow = forageBegin = () => {}; + loadConversations = async () => {}; + renderMessages = () => globalThis.opened.resolve(); + handleEvent = (event, refs) => { if (event.type === "delta") refs.msg.content += event.text; }; + finish = refs => { + state.busy = false; + if (refs.msg.error) globalThis.finished.reject(new Error(refs.msg.error)); + else persistConversation().then(() => globalThis.finished.resolve()); + }; + toast = message => { + globalThis.warnings.push(message); + if (globalThis.opened) globalThis.opened.reject(new Error(message)); + }; + `) + const png = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aGDcAAAAASUVORK5CYII=" + const text = "# 场景\n灯亮了。" + const doc = "data:text/markdown;base64," + Buffer.from(text).toString("base64") + page.dataByName = { [name]: png, "scene.md": doc } + page.warnings = [] + page.state.settings.model = chatBody.model + Object.assign(page.document.querySelector("#stream"), { querySelector: () => null, appendChild() {} }) + await page.addFiles([{ name, type, size: 12 }, { name: "scene.md", type: "text/markdown", size: 12 }]) + const checkGateway = () => { + const messages = seen.at(-1).body.messages + expect(messages[0].content).toEqual([ + { type: "text", text: "【文档:scene.md】\n" + text + "\n【文档结束】" }, + { type: "image_url", image_url: { url: png } }, + ]) + expect(messages.every(m => !("userText" in m))).toBe(true) + } + const send = async () => { + page.finished = Promise.withResolvers() + const before = seen.length + page.send() + await page.finished.promise + expect(seen.length).toBe(before + 1) + checkGateway() + } + await send() + expect(page.state.messages[0].files.map(f => f.data)).toEqual([png, doc]) + const legacyImage = { name, type, kind: "image", data: png } + page.state.messages[0].files[0] = legacyImage + await page.persistConversation() + const savedId = page.state.convId + expect(savedId).toBeTruthy() + page.state.messages = [] + page.state.convId = null + page.opened = Promise.withResolvers() + page.openConversation(savedId) + await page.opened.promise + expect(page.state.messages[0].files.map(f => f.data)).toEqual([png, doc]) + page.document.querySelector("#input").value = "灯是什么颜色?" + await send() + expect(page.state.messages[0].files[0]).toEqual(legacyImage) + expect(seen.at(-1).body.messages.at(-1).content).toBe("灯是什么颜色?") + expect(page.warnings).toEqual([]) +}) + +test.each(["new", "saved"])("a late save cannot move a %s conversation onto the old record", async destination => { + const page = attachmentPage(` + const originalFetch = fetch; + fetch = async (path, init) => { + const response = await originalFetch(path, init); + if (path === "api/conversations" && init.method === "POST" && globalThis.delaySave) { + globalThis.delaySave = false; + globalThis.saved.resolve(await response.clone().json()); + await globalThis.release.promise; + } + return response; + }; + renderMessages = () => globalThis.opened.resolve(); + toast = message => globalThis.warnings.push(message); + `) + page.warnings = [] + page.delaySave = true + page.saved = Promise.withResolvers() + page.release = Promise.withResolvers() + const original = { role: "user", content: "original", files: [ + { name: "scene.md", type: "text/markdown", kind: "file", data: "data:text/markdown;base64,b3JpZ2luYWw=" }, + ] } + page.state.messages = [original] + page.state.title = "original" + const saving = page.persistConversation() + const saved = await page.saved.promise + const h = { ...json, "x-magpie-chat-token": token } + page.opened = Promise.withResolvers() + let destinationId = null + if (destination === "new") page.newChat() + else { + const other = JSON.parse((await raw("POST", "/api/conversations", h, { + title: "other", messages: [{ role: "user", content: "other" }], + })).body) + destinationId = other.id + page.openConversation(other.id) + } + await page.opened.promise + expect(page.state.convId).toBe(destinationId) + expect(page.state.title).toBe(destination === "new" ? "新对话" : "other") + if (destination === "new") expect(page.state.draft).not.toBeNull() + try { + page.state.messages.push({ role: "user", content: "new scene" }) + await page.persistConversation() + destinationId = page.state.convId + } finally { + page.release.resolve() + await saving + } + expect(page.state.convId).toBe(destinationId) + expect(page.state.convId).not.toBe(saved.id) + const unchanged = JSON.parse((await raw("GET", "/api/conversations/" + saved.id, h)).body) + expect(unchanged.messages).toEqual(saved.messages) + expect(page.warnings).toEqual([]) +}) + +test.each(["saved", "lost"])("overlapping saves use one record in order after a %s response", async outcome => { + const failed = outcome === "lost" + const page = attachmentPage(` + const originalFetch = fetch; + fetch = async (path, init) => { + if (path === "api/conversations" && init.method === "POST") globalThis.posts++; + const response = await originalFetch(path, init); + if (path === "api/conversations" && init.method === "POST" && globalThis.delaySave) { + globalThis.delaySave = false; + globalThis.saved.resolve(await response.clone().json()); + await globalThis.release.promise; + if (globalThis.failed) throw new Error("save response lost"); + } + return response; + }; + toast = message => globalThis.warnings.push(message); + `) + page.failed = failed + page.warnings = [] + page.posts = 0 + page.delaySave = true + page.saved = Promise.withResolvers() + page.release = Promise.withResolvers() + page.state.messages = [{ role: "user", content: "first" }] + const first = page.persistConversation() + const stored = await page.saved.promise + page.state.messages.push({ role: "assistant", content: "answer" }, { role: "user", content: "second" }) + const second = page.persistConversation() + try { + await Promise.resolve() + expect(page.posts).toBe(1) + } finally { + page.release.resolve() + await Promise.all([first, second]) + } + expect(page.posts).toBe(2) + expect(page.state.convId).toBe(stored.id) + const h = { ...json, "x-magpie-chat-token": token } + const saved = JSON.parse((await raw("GET", "/api/conversations/" + stored.id, h)).body) + expect(saved.messages.map(m => m.content)).toEqual(["first", "answer", "second"]) + expect(page.warnings).toEqual(failed ? ["对话保存失败:save response lost"] : []) + expect(page.conversationWrites.size).toBe(0) +}) + +test.each(["new", "blank", "send", "other", "delete", "delete-target", "rename"])("a late conversation read cannot replace a newer %s action", async action => { + const page = attachmentPage(` + api = (path, options) => (path.endsWith("/late") || path.endsWith("/original")) && !options ? globalThis.loading.promise : globalThis.other.promise; + el = () => globalThis.renameInput; + buildTurn = msg => ({ msg, root: {}, err: {} }); + autoGrow = forageBegin = () => {}; + toast = () => {}; + streamChat = async () => { globalThis.entered.resolve(); await globalThis.streaming.promise; }; + finish = () => { state.busy = false; }; + `) + page.loading = Promise.withResolvers() + page.other = Promise.withResolvers() + page.entered = Promise.withResolvers() + page.streaming = Promise.withResolvers() + page.state.convId = "original" + page.state.title = "original" + page.state.messages = [{ role: "user", content: "original" }] + page.state.settings.model = chatBody.model + Object.assign(page.document.querySelector("#stream"), { querySelector: () => null, appendChild() {} }) + if (action === "blank") page.newChat() + const loading = page.openConversation(action === "rename" ? "original" : "late") + await Promise.resolve() + if (action === "new" || action === "blank") { + page.newChat() + page.state.attachments.push({ name: "new.md", data: "data:text/markdown;base64,bmV3" }) + } else if (action === "send") { + page.document.querySelector("#input").value = "new question" + page.send() + await page.entered.promise + } else if (action === "delete" || action === "delete-target") { + const deleting = page.askDelete(action === "delete" ? "original" : "late", "to delete") + page.other.resolve({ ok: true }) + await deleting + } else if (action === "rename") { + const handlers = {} + const input = { value: "", focus() {}, select() {}, addEventListener: (event, fn) => { handlers[event] = fn } } + page.renameInput = input + page.startRename("original", { style: {}, parentNode: { insertBefore() {} } }, "original") + input.value = "renamed" + handlers.keydown({ key: "Enter", preventDefault() {} }) + page.other.resolve({ ok: true }) + } else { + const opening = page.openConversation("other") + page.other.resolve({ id: "other", title: "other", messages: [{ role: "user", content: "other" }] }) + await opening + } + const messages = page.state.messages + const attachments = page.state.attachments + page.loading.resolve({ id: action === "rename" ? "original" : "late", title: "late", messages: [{ role: "user", content: "late" }] }) + await loading + try { + expect(page.state.messages).toBe(messages) + expect(page.state.attachments).toBe(attachments) + expect(page.state.convId).toBe(["send", "delete-target", "rename"].includes(action) ? "original" : action === "other" ? "other" : null) + if (action === "rename") expect(page.state.title).toBe("renamed") + } finally { + page.streaming.resolve() + } +}) + +test.each(["open", "rename", "delete"])("%s waits for pending writes to the same conversation", async action => { + const h = { ...json, "x-magpie-chat-token": token } + const original = JSON.parse((await raw("POST", "/api/conversations", h, { + title: "old", messages: [{ role: "user", content: "original" }], + })).body) + const page = attachmentPage(` + const originalFetch = fetch; + fetch = async (path, init) => { + if (path === "api/conversations" && init.method === "POST" && globalThis.delaySave) { + globalThis.delaySave = false; + globalThis.entered.resolve(); + await globalThis.release.promise; + } + if (path === "api/conversations/" + globalThis.id && init.method === "GET") globalThis.reads++; + const response = await originalFetch(path, init); + if (init.method === "PATCH" || init.method === "DELETE") globalThis.mutated.resolve(); + return response; + }; + toast = message => globalThis.warnings.push(message); + el = () => globalThis.renameInput; + `) + page.state.convId = original.id + page.id = original.id + page.state.title = original.title + page.state.messages = [...original.messages, { role: "user", content: "unsaved turn" }] + page.warnings = [] + page.reads = 0 + page.delaySave = true + page.entered = Promise.withResolvers() + page.release = Promise.withResolvers() + page.mutated = Promise.withResolvers() + const first = page.persistConversation() + await page.entered.promise + let opening, second + try { + if (action === "open") { + page.newChat() + opening = page.openConversation(original.id) + await Promise.resolve() + expect(page.reads).toBe(0) + } else { + page.state.messages.push({ role: "assistant", content: "answer" }) + second = page.persistConversation() + if (action === "delete") { + page.askDelete(original.id, "old") + page.state.messages.push({ role: "user", content: "while deleting" }) + second = Promise.all([second, page.persistConversation()]) + } else { + const handlers = {} + page.renameInput = { value: "", focus() {}, select() {}, addEventListener: (event, fn) => { handlers[event] = fn } } + page.startRename(original.id, { style: {}, parentNode: { insertBefore() {} } }, "old") + page.renameInput.value = "renamed" + handlers.keydown({ key: "Enter", preventDefault() {} }) + } + } + } finally { + page.release.resolve() + await Promise.all([first, second, opening]) + } + if (action !== "open") await page.mutated.promise + const response = await raw("GET", "/api/conversations/" + original.id, h) + if (action === "delete") expect(response.status).toBe(404) + else { + expect(JSON.parse(response.body).messages.map(m => m.content)).toEqual(action === "open" + ? ["original", "unsaved turn"] : ["original", "unsaved turn", "answer"]) + expect(page.state.title).toBe(action === "rename" ? "renamed" : "old") + if (action === "rename") expect(JSON.parse(response.body).title).toBe("renamed") + else expect(page.state.messages.map(m => m.content)).toEqual(["original", "unsaved turn"]) + } + expect(page.warnings).toEqual(action === "delete" ? ["已删除「old」"] : []) +}) + +test("connection errors stay visible after reopening but are not replayed as tool calls", async () => { + const page = attachmentPage(` + makeTalk = () => ({ root: {}, done() {} }); + updateMeta = () => {}; + renderMessages = () => globalThis.opened.resolve(); + `) + const refs = { msg: { role: "assistant", content: "hello", toolCalls: [] }, talks: { appendChild() {} }, cards: {} } + page.handleTool({ phase: "error", name: "offline", result: "connection refused" }, refs) + page.handleTool({ phase: "call", id: "actual", name: "catalog_read_report", args: {} }, refs) + page.handleTool({ phase: "result", id: "actual", result: "file missing", isError: true }, refs) + page.state.messages = [{ role: "user", content: "read report" }, refs.msg] + await page.persistConversation() + const id = page.state.convId + expect(id).toBeTruthy() + page.state.convId = null + page.state.messages = [] + page.opened = Promise.withResolvers() + page.openConversation(id) + await page.opened.promise + expect(page.state.messages[1].toolCalls.map(c => c.result)).toEqual(["connection refused", "file missing"]) + const before = seen.length + await page.streamChat({ ...chatBody, useTools: false, messages: [...page.state.messages, { role: "user", content: "What happened?" }] }, () => {}) + expect(seen.length).toBe(before + 1) + const sent = seen.at(-1).body.messages + const calls = sent.flatMap(m => m.tool_calls || []) + expect(calls.map(c => c.function.name)).toEqual(["catalog_read_report"]) + expect(JSON.parse(calls[0].function.arguments)).toEqual({}) + expect(sent.filter(m => m.role === "tool").map(m => m.content)).toEqual(["file missing"]) +}) + +test("Markdown and text attachments reach the gateway after upload, save and follow-up", async () => { + const page = attachmentPage(` + toast = message => globalThis.warnings.push(message); + readAsDataURL = () => Promise.resolve(globalThis.fileData); + `) + page.warnings = [] + const text = "# 第一场\n\n阿青:灯还亮着。𠀀\n\n" + "窗外有雨。\n".repeat(10000) + "最后一行。" + const data = "data:application/octet-stream;base64," + Buffer.from("\ufeff" + text).toString("base64") + page.fileData = data + const h = { ...json, "x-magpie-chat-token": token } + const image = { name: "lamp.png", kind: "image", data: "data:image/png;base64,cG5n" } + for (const [name, type] of [["剧本.MD", "application/octet-stream"], ["scene.markdown", ""], + ["notes.txt", "text/plain"], ["draft", "text/markdown"], ["a".repeat(192) + ".markdown", ""]]) { + page.state.attachments = [] + await page.addFiles([{ name, type, size: Buffer.byteLength(text) }]) + expect(page.state.attachments).toHaveLength(1) + const file = page.state.attachments[0] + const first = { role: "user", content: "读一下", files: [file] } + const send = async messages => { + const before = seen.length + const events = [] + await page.streamChat({ ...chatBody, messages }, event => events.push(event)) + expect(events.at(-1).type).toBe("done") + expect(seen.length).toBe(before + 1) + const sent = seen.at(-1).body.messages + expect(JSON.stringify(sent).split("最后一行。")).toHaveLength(2) + expect(sent.find(message => message.role === "user").content).toContain(text) + expect(JSON.stringify(sent)).not.toContain(data) + expect(sent.every(message => !("userText" in message))).toBe(true) + return sent + } + await send([first]) + first.content = "" + await send([first]) + const saved = JSON.parse((await raw("POST", "/api/conversations", h, { messages: [first] })).body) + const history = JSON.parse((await raw("GET", "/api/conversations/" + saved.id, h)).body).messages + history.push({ role: "assistant", content: "读到了。" }, + { role: "user", content: "最后一句是什么?", files: [{ ...file, name: "copy." + name }, image] }) + const sent = await send(history) + expect(sent.at(-1).content[0].text).toContain("最后一句是什么?") + expect(sent.at(-1).content[1]).toEqual({ type: "image_url", image_url: { url: image.data } }) + expect(history[0].content).toBe("") + expect(history[0].files[0].data).toBe(data) + } + expect(page.warnings).toEqual([]) +}) + +test("empty text attachments stay empty and unreadable text produces a warning", async () => { + const page = chatPage("toast = message => globalThis.warnings.push(message);") + page.warnings = [] + for (const bytes of ["", "%%%", "/w=="]) { + const before = seen.length + await page.streamChat({ ...chatBody, messages: [{ role: "user", content: "", files: [ + { name: "draft.md", kind: "file", data: "data:;base64," + bytes }, + ] }] }, () => {}) + expect(seen.length).toBe(before + 1) + const content = seen.at(-1).body.messages.at(-1).content + if (!bytes) { + expect(content).toBe("【文档:draft.md】\n\n【文档结束】") + expect(page.warnings).toEqual([]) + } else { + expect(content).toContain("未附上这份文档的正文") + expect(page.warnings.at(-1)).toStartWith("附件未发送。draft.md:") + expect(content).not.toContain("\ufffd") + } + } +}) + test("a saved EPUB reaches the gateway once, including on a follow-up", async () => { const page = chatPage("toast = message => globalThis.warnings.push(message);") page.warnings = [] @@ -195,7 +698,7 @@ test("a saved EPUB reaches the gateway once, including on a follow-up", async () expect(sent.at(-3).content[1]).toEqual({ type: "image_url", image_url: { url: image.data } }) expect(sent.at(-1).content).toContain("What changed?") expect(sent.at(-1).content).toContain("broken.epub:damaged") - expect(page.warnings).toEqual(["旧附件未发送。broken.epub:damaged"]) + expect(page.warnings).toEqual(["附件未发送。broken.epub:damaged"]) expect(JSON.stringify(sent).match(/The lamp goes out/g)).toHaveLength(1) expect(JSON.stringify(sent)).not.toContain("Ym9vaw==") expect(history[0].content).toBe("") @@ -207,7 +710,7 @@ test("switching chats discards a book that is still being read", async () => { updateSend = renderAttachments = renderMessages = renderSessions = scrollBottom = () => {}; api = async () => ({ id: "other", messages: [] }); readAsDataURL = () => globalThis.reading; - globalThis.pending = () => pendingBooks; + globalThis.pending = () => pendingAttachments; `) const reading = Promise.withResolvers() page.reading = reading.promise @@ -224,7 +727,7 @@ test("switching chats discards a book that is still being read", async () => { expect(page.pending()).toBe(0) }) -test("MCP tools follow the typed question while the gateway still receives the EPUB", async () => { +test.each(["epub", "md"])("MCP tools follow the typed question while the gateway receives the %s attachment", async ext => { const names = [...Array.from({ length: 12 }, (_, i) => "github_lookup_" + i), ...Array.from({ length: 3 }, (_, i) => "weather_forecast_" + i), "list_items"] const mcp = fakeMcp("catalog", names.map(name => ({ name, inputSchema: { type: "object", properties: {} } }))) @@ -234,7 +737,7 @@ test("MCP tools follow the typed question while the gateway still receives the E })) const page = chatPage() const chapters = "github ".repeat(1000) + "The last page." - const file = { name: "github.epub", kind: "file", data: "data:;base64,Ym9vaw==" } + const file = { name: "github." + ext, type: "text/plain", kind: "file", data: "data:;base64," + Buffer.from(chapters).toString("base64") } page.MagpieEpub.read = async () => ({ text: chapters }) const weather = names.filter(name => name.startsWith("weather")).map(name => "catalog_" + name) const send = async history => { @@ -274,6 +777,194 @@ test("MCP tools follow the typed question while the gateway still receives the E } }) +test.each(["continue", "stop", "stop during lookup"])("a tool round respects %s while a call is pending", async action => { + const stopped = action !== "continue" + const duringLookup = action === "stop during lookup" + const entered = Promise.withResolvers(), release = Promise.withResolvers() + const called = [] + const names = ["probe_first", "probe_second"] + const mcp = Bun.serve({ hostname: "127.0.0.1", port: 0, async fetch(req) { + const message = await req.json() + if (message.id === undefined) return new Response(null, { status: 202 }) + let result = {} + if (message.method === "initialize") result = { protocolVersion: "2025-03-26", capabilities: { tools: {} }, serverInfo: { name: "probe", version: "1" } } + if (message.method === "tools/list") result = { tools: names.map(name => ({ name, inputSchema: { type: "object", properties: {} } })) } + if (message.method === "tools/call") { + called.push(message.params.name) + if (!duringLookup && called.length === 1) { entered.resolve(); await release.promise } + result = { content: [{ type: "text", text: "completed" }] } + } + return Response.json({ jsonrpc: "2.0", id: message.id, result }) + } }) + const h = { ...json, "x-magpie-chat-token": token } + writeFileSync(join(cfg, "library.json"), JSON.stringify({ + mcp: [{ name: "probe", transport: "http", url: `http://127.0.0.1:${mcp.port}/mcp`, agents: [] }], + })) + chatReply = body => { + if (duringLookup) ownListReply = async () => { + entered.resolve(); + await release.promise; + return new Response("no", { status: 404 }); + } + const hasResult = body.messages.some(m => m.role === "tool") + const choice = hasResult ? { delta: { content: "finished" }, finish_reason: "stop" } : { + delta: { tool_calls: names.map((name, index) => ({ index, id: "call_" + index, type: "function", function: { name: "probe_" + name, arguments: "{}" } })) }, + finish_reason: "tool_calls", + } + return new Response("data: " + JSON.stringify({ choices: [choice] }) + "\n\ndata: [DONE]\n\n", { headers: { "content-type": "text/event-stream" } }) + } + let chatting + try { + await raw("POST", "/api/settings", h, { mcp: ["probe"], mcpAll: false }) + chatting = raw("POST", "/api/chat", h, { ...chatBody, clientId: "tool-stop-probe", messages: [{ role: "user", content: "probe" }] }) + await entered.promise + if (stopped) expect((await raw("POST", "/api/chat/stop", h, { clientId: "tool-stop-probe" })).status).toBe(200) + release.resolve() + const response = await chatting + expect(called).toEqual(duringLookup ? [] : stopped ? [names[0]] : names) + expect(response.body).toContain('"type":"done"') + if (stopped) expect(response.body).toContain('"stopped":true') + } finally { + release.resolve() + await chatting + chatReply = null + ownListReply = null + await raw("POST", "/api/settings", h, { mcp: [], mcpAll: false }) + rmSync(join(cfg, "library.json"), { force: true }) + mcp.stop(true) + } +}) + +test.each(["30000", "65000", "missing IDs", "repeated IDs"])("tool history preserves round order and result limits: %s", async scenario => { + const size = scenario.endsWith("IDs") ? 30000 : Number(scenario) + const result = "x".repeat(25000) + "TAIL_SENTINEL" + "y".repeat(size - 25013) + const expectedResult = result.slice(0, 60000) + const names = ["read_report", "read_empty", "read_detail"] + const called = [] + const mcp = Bun.serve({ hostname: "127.0.0.1", port: 0, async fetch(req) { + const msg = await req.json() + if (msg.id === undefined) return new Response(null, { status: 202 }) + let output = {} + if (msg.method === "initialize") output = { protocolVersion: "2025-03-26", capabilities: { tools: {} }, serverInfo: { name: "history", version: "1" } } + if (msg.method === "tools/list") output = { tools: names.map(name => ({ name, inputSchema: { type: "object", properties: {} } })) } + if (msg.method === "tools/call") { + called.push(msg.params.name) + output = msg.params.name === names[0] ? { content: [{ type: "text", text: result }] } + : msg.params.name === names[1] ? { content: [] } + : { content: [{ type: "text", text: "detail unavailable" }], isError: true } + } + return Response.json({ jsonrpc: "2.0", id: msg.id, result: output }) + } }) + const h = { ...json, "x-magpie-chat-token": token } + writeFileSync(join(cfg, "library.json"), JSON.stringify({ mcp: [ + { name: "history", transport: "http", url: `http://127.0.0.1:${mcp.port}/mcp`, agents: [] }, + ] })) + const before = seen.length + chatReply = () => { + const round = seen.length - before - 1 + const content = round === 0 ? "Searching. " : round === 1 ? "Reading. " : "Final answer." + const tools = round === 0 ? names.slice(0, 2) : round === 1 ? names.slice(2) : [] + const delta = { content } + if (tools.length) delta.tool_calls = tools.map((name, index) => ({ + index, id: scenario === "missing IDs" ? undefined : scenario === "repeated IDs" ? "call_1_" + index : "round_" + round + "_" + index, + type: "function", function: { name: "history_" + name, arguments: "{}" }, + })) + return new Response("data: " + JSON.stringify({ choices: [{ delta, finish_reason: tools.length ? "tool_calls" : "stop" }] }) + "\n\ndata: [DONE]\n\n", { headers: { "content-type": "text/event-stream" } }) + } + try { + await raw("POST", "/api/settings", h, { mcp: ["history"], mcpAll: false }) + const page = attachmentPage(` + updateMeta = updateLabel = pumpKick = () => {}; + makeTalk = () => ({ root: {}, done() {} }); + toast = message => globalThis.warnings.push(message); + `) + page.warnings = [] + const assistant = { role: "assistant", content: "", reasoning: "", toolCalls: [] } + const refs = { msg: assistant, cards: {}, talks: { appendChild() {} }, err: {} } + page.state.messages = [{ role: "user", content: "read history" }, assistant] + await page.streamChat({ ...chatBody, messages: page.state.messages.slice(0, -1) }, evt => page.handleEvent(evt, refs)) + page.pumpFlush(refs) + expect(refs.err.hidden).not.toBe(false) + expect(seen.length).toBe(before + 3) + expect(called).toEqual(names) + const live = seen.at(-1).body.messages + expect(live.filter(m => m.role === "tool").map(m => m.content)).toEqual([expectedResult, '{"content":[]}', "detail unavailable"]) + for (let i = 0; i < live.length; i++) { + for (const call of live[i].tool_calls || []) expect(live.slice(i + 1).some(m => m.role === "tool" && m.tool_call_id === call.id)).toBe(true) + } + expect(assistant.content).toBe("Searching. Reading. Final answer.") + expect(assistant.toolCalls.map(c => c.result)).toEqual([expectedResult, '{"content":[]}', "detail unavailable"]) + expect(assistant.toolCalls.at(-1).isError).toBe(true) + const followUp = { role: "user", content: "What was at the end?" } + const expected = [...live.filter(m => m.role !== "system"), { role: "assistant", content: "Final answer." }, followUp] + await page.streamChat({ ...chatBody, useTools: false, messages: [...page.state.messages, followUp] }, () => {}) + expect(seen.at(-1).body.messages).toEqual(expected) + await page.persistConversation() + const id = page.state.convId + page.newChat() + await page.openConversation(id) + await page.streamChat({ ...chatBody, useTools: false, messages: [...page.state.messages, followUp] }, () => {}) + expect(seen.at(-1).body.messages).toEqual(expected) + expect(called).toEqual(names) + expect(page.warnings).toEqual([]) + } finally { + chatReply = null + await raw("POST", "/api/settings", h, { mcp: [], mcpAll: false }) + rmSync(join(cfg, "library.json"), { force: true }) + mcp.stop(true) + } +}) + +test.each(["stopped", "error"])("an interrupted %s round keeps completed history beside legacy messages", async ending => { + const page = attachmentPage(` + updateMeta = updateLabel = pumpKick = () => {}; + makeTalk = () => ({ root: {}, done() {} }); + toast = message => globalThis.warnings.push(message); + `) + page.warnings = [] + const assistant = { role: "assistant", content: "", reasoning: "", toolCalls: [] } + const refs = { msg: assistant, cards: {}, talks: { appendChild() {} }, err: {} } + const user = { role: "user", content: "continue" } + const legacy = { role: "assistant", content: "old answer", toolCalls: [ + { id: "old", name: "read_old", args: {}, result: "old result" }, + ] } + page.state.messages = [{ role: "user", content: "old question" }, legacy, user, assistant] + const events = [ + { type: "round" }, { type: "delta", text: "Looking. " }, + { type: "tool", phase: "call", id: "done", name: "read_first", args: {} }, + { type: "tool", phase: "result", id: "done", result: "first result" }, + { type: "round" }, + ] + if (ending === "stopped") events.push( + { type: "delta", text: "Partial answer." }, + { type: "tool", phase: "call", id: "pending", name: "read_next", args: {} }, + { type: "done", stopped: true }, + ) + else events.push({ type: "error", message: "upstream unavailable" }) + for (const event of events) page.handleEvent(event, refs) + expect(assistant.content).toBe("") + expect(assistant.rounds.map(r => r.content)).toEqual(["Looking. ", ending === "stopped" ? "Partial answer." : ""]) + page.pumpFlush(refs) + await page.persistConversation() + const id = page.state.convId + page.newChat() + await page.openConversation(id) + expect(page.state.messages[1].rounds).toBeUndefined() + const followUp = { role: "user", content: "What happened?" } + const before = seen.length + await page.streamChat({ ...chatBody, useTools: false, messages: [...page.state.messages, followUp] }, () => {}) + expect(seen.length).toBe(before + 1) + const sent = seen.at(-1).body.messages + expect(sent.map(m => m.role)).toEqual(ending === "stopped" + ? ["user", "assistant", "tool", "user", "assistant", "tool", "assistant", "user"] + : ["user", "assistant", "tool", "user", "assistant", "tool", "user"]) + expect(sent.flatMap(m => m.tool_calls || []).map(c => c.id)).toEqual(["old", "done"]) + expect(sent.filter(m => m.role === "assistant").map(m => m.content)).toEqual(ending === "stopped" + ? ["old answer", "Looking. ", "Partial answer."] : ["old answer", "Looking. "]) + expect(sent.filter(m => m.role === "tool").map(m => m.content)).toEqual(["old result", "first result"]) + expect(page.warnings).toEqual([]) +}) + test("conversations save and list with the token", async () => { const h = { ...json, origin: "null", "x-magpie-chat-token": token } const saved = JSON.parse((await raw("POST", "/api/conversations", h, { title: "t", messages: [{ role: "user", content: "x" }] })).body) diff --git a/packages/chat/chat/app.js b/packages/chat/chat/app.js index 8ae4e23..81af9b4 100644 --- a/packages/chat/chat/app.js +++ b/packages/chat/chat/app.js @@ -673,7 +673,7 @@ function parseEventBlock(block) { } async function streamChat(payload, onEvent, signal) { - payload.messages = await requestMessages(payload.messages, function (reason) { toast("旧附件未发送。" + reason, true); }); + payload.messages = await requestMessages(payload.messages, function (reason) { toast("附件未发送。" + reason, true); }); return fetch("api/chat", { method: "POST", headers: authHeaders({ "content-type": "application/json" }), @@ -971,17 +971,27 @@ function readAsDataURL(file) { } function guessType(file) { - if (file.type) return file.type; + if (file.type && file.type !== "application/octet-stream") return file.type; var ext = String(file.name || "").split(".").pop().toLowerCase(); - var map = { png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", webp: "image/webp", svg: "image/svg+xml", pdf: "application/pdf", epub: "application/epub+zip", txt: "text/plain", md: "text/markdown", json: "application/json" }; + var map = { png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", webp: "image/webp", svg: "image/svg+xml", pdf: "application/pdf", epub: "application/epub+zip", txt: "text/plain", md: "text/markdown", markdown: "text/markdown", json: "application/json" }; return map[ext] || "application/octet-stream"; } +function normalizeImage(file) { + var dataType = /^data:(image\/[^;,]+)/i.exec(file.data || ""); + var type = dataType ? dataType[1].toLowerCase() : guessType(file); + if (!/^image\//.test(type) || !file.data) return file; + return Object.assign({}, file, { + type: type, kind: "image", + data: String(file.data).replace(/^data:[^;,]*/, "data:" + type) + }); +} + // 一批 File → 加入待发送列表 -var attachmentContext = 0, pendingBooks = 0; +var attachmentContext = 0, pendingAttachments = 0; function clearPendingAttachments() { attachmentContext++; - pendingBooks = 0; + pendingAttachments = 0; state.attachments = []; renderAttachments(); } @@ -990,8 +1000,7 @@ function addFiles(files) { var list = Array.from(files || []).slice(0, 8); if (!list.length) return Promise.resolve(); var context = attachmentContext; - var books = list.filter(MagpieEpub.isEpub).length; - pendingBooks += books; + pendingAttachments += list.length; updateSend(); var jobs = list.map(function (file) { var type = guessType(file); @@ -1008,9 +1017,6 @@ function addFiles(files) { return api("api/upload", { method: "POST", body: { name: file.name || "file", type: type, data: data, clientType: type } - }).catch(function () { - // 上传接口挂了也还能本地显示:直接按 data URL 用 - return { name: file.name || "file", type: type, kind: isImage ? "image" : "file", size: data.length, data: data }; }); }).catch(function (err) { if (context === attachmentContext) toast((file.name || "文件") + ":" + err.message, true); @@ -1019,11 +1025,11 @@ function addFiles(files) { }).filter(Boolean); return Promise.all(jobs).then(function (out) { if (context !== attachmentContext) return; - out.forEach(function (f) { if (f && f.data) state.attachments.push(f); }); + out.forEach(function (f) { if (f && f.data) state.attachments.push(normalizeImage(f)); }); renderAttachments(); }).finally(function () { if (context !== attachmentContext) return; - pendingBooks -= books; + pendingAttachments -= list.length; updateSend(); }); } @@ -1464,7 +1470,10 @@ function startRename(id, rowEl, oldTitle) { // 正在看的就是这一条:本地标题也要跟着改,否则下一次 persistConversation // 会把旧的 state.title 整份 POST 回去,刚改的名字又被盖掉。 if (id === state.convId) state.title = val; - api("api/conversations/" + encodeURIComponent(id), { method: "PATCH", body: { title: val } }) + if (conversationRequest && conversationRequest.id === id) conversationRequest = null; + writeConversation(id, function () { + return api("api/conversations/" + encodeURIComponent(id), { method: "PATCH", body: { title: val } }); + }) .then(function () { return loadConversations(); }) .catch(function (err) { toast("重命名失败:" + err.message, true); }); } @@ -1481,22 +1490,27 @@ function startRename(id, rowEl, oldTitle) { function askDelete(id, title) { var name = String(title || "这条对话"); { - api("api/conversations/" + encodeURIComponent(id), { method: "DELETE" }).then(function () { - // 置顶列表里也把它去掉,免得留下悬空的 id - var pinned = (state.settings.pinned || []).filter(function (x) { return x !== id; }); - state.settings.pinned = pinned; - api("api/settings", { method: "POST", body: { pinned: pinned } }).catch(function () {}); - if (state.convId === id) { - clearPendingAttachments(); - state.convId = null; - state.title = "新对话"; - state.messages = []; - renderMessages(); - renderSessions(); - } else { - loadConversations(); - } - toast("已删除「" + name.slice(0, 20) + "」"); + return writeConversation(id, function (queue) { + return api("api/conversations/" + encodeURIComponent(id), { method: "DELETE" }).then(function () { + queue.deleted = true; + // 置顶列表里也把它去掉,免得留下悬空的 id + var pinned = (state.settings.pinned || []).filter(function (x) { return x !== id; }); + state.settings.pinned = pinned; + api("api/settings", { method: "POST", body: { pinned: pinned } }).catch(function () {}); + if (conversationRequest && conversationRequest.id === id) conversationRequest = null; + if (state.convId === id) { + conversationRequest = null; + clearPendingAttachments(); + state.convId = null; + state.title = "新对话"; + state.messages = []; + renderMessages(); + renderSessions(); + } else { + loadConversations(); + } + toast("已删除「" + name.slice(0, 20) + "」"); + }); }).catch(function (err) { toast("删除失败:" + err.message, true); }); @@ -1510,11 +1524,17 @@ function setSidebar(open) { api("api/settings", { method: "POST", body: { sidebar: open } }).catch(function () {}); } +var conversationRequest = null; function openConversation(id) { if (state.busy) { toast("正在生成,先停止再切换对话", true); return; } + var request = { id: id }; + conversationRequest = request; dropBlankDraft(); // 切换到别的对话时,空着的"新对话"不该留在列表里 - api("api/conversations/" + encodeURIComponent(id)).then(function (conv) { - if (!conv) return; + var writing = conversationWrites.get(id); + return (writing ? writing.done : Promise.resolve()).then(function () { + if (request === conversationRequest) return api("api/conversations/" + encodeURIComponent(id)); + }).then(function (conv) { + if (!conv || request !== conversationRequest) return; if (state.convId !== conv.id) clearPendingAttachments(); state.convId = conv.id; state.title = conv.title || "新对话"; @@ -1523,7 +1543,9 @@ function openConversation(id) { renderSessions(); scrollBottom(true); }).catch(function (err) { - toast("打开对话失败:" + err.message, true); + if (request === conversationRequest) toast("打开对话失败:" + err.message, true); + }).finally(function () { + if (request === conversationRequest) conversationRequest = null; }); } @@ -1532,6 +1554,9 @@ function normalizeMessage(m) { role: m.role === "assistant" ? "assistant" : "user", content: String(m.content || ""), reasoning: String(m.reasoning || ""), + rounds: Array.isArray(m.rounds) ? m.rounds.map(function (round) { + return { content: String(round.content || ""), toolIds: (round.toolIds || []).slice() }; + }) : undefined, toolCalls: Array.isArray(m.toolCalls) ? m.toolCalls.map(function (c) { return { id: c.id || "", name: c.name || "", @@ -1551,16 +1576,40 @@ function normalizeMessage(m) { }; } +// 同一会话依次写入;删除成功后,丢弃已排队的旧快照。 +var conversationWrites = new Map(); +function writeConversation(id, write) { + var queue = conversationWrites.get(id); + if (!queue) { + queue = { done: Promise.resolve(), deleted: false }; + conversationWrites.set(id, queue); + } + var done = queue.done.catch(function () {}).then(function () { + if (!queue.deleted) return write(queue); + }); + queue.done = done; + function clear() { + if (queue.done === done) conversationWrites.delete(id); + } + done.then(clear, clear); + return done; +} + function persistConversation() { if (!state.messages.length) return Promise.resolve(); + var messages = state.messages; + if (!state.convId) state.convId = uid("c"); var payload = { - id: state.convId || undefined, + id: state.convId, title: state.title || undefined, messages: state.messages.map(function (m) { return { role: m.role, content: m.content || "", reasoning: m.reasoning || "", + rounds: m.rounds ? m.rounds.map(function (round) { + return { content: round.content, toolIds: round.toolIds.slice() }; + }) : undefined, toolCalls: (m.toolCalls || []).map(function (c) { return { id: c.id, name: c.name, args: c.args, result: c.result, isError: Boolean(c.isError) }; }), @@ -1573,12 +1622,16 @@ function persistConversation() { }; }) }; - return api("api/conversations", { method: "POST", body: payload }).then(function (saved) { - if (!saved) return; - state.convId = saved.id; - state.title = saved.title || state.title; - state.draft = null; // 已经是真对话了,草稿退场 - return loadConversations(); + return writeConversation(payload.id, function () { + return api("api/conversations", { method: "POST", body: payload }).then(function (saved) { + if (!saved) return; + if (state.messages === messages) { + state.convId = saved.id; + if (state.title === payload.title) state.title = saved.title || state.title; + state.draft = null; // 已经是真对话了,草稿退场 + } + return loadConversations(); + }); }).catch(function (err) { toast("对话保存失败:" + err.message, true); }); @@ -2149,7 +2202,7 @@ function updateSend() { var btn = $("#send"); var input = $("#input"); $("#app").classList.toggle("busy", state.busy); - btn.disabled = !state.busy && (pendingBooks > 0 || (!input.value.trim() && !(state.attachments || []).length)); + btn.disabled = !state.busy && (pendingAttachments > 0 || (!input.value.trim() && !(state.attachments || []).length)); btn.setAttribute("aria-label", state.busy ? "停止生成" : "发送"); input.title = state.busy ? "生成中…(Esc 停止)" : "Enter 发送 · Shift+Enter 换行"; } @@ -2157,15 +2210,18 @@ function updateSend() { async function requestMessages(history, warn) { var seen = new Set(), messages = []; for (var m of history) { - var files = (m.files || []).filter(Boolean); + var files = (m.files || []).filter(Boolean).map(normalizeImage); if (m.role === "assistant") { - var calls = (m.toolCalls || []).filter(function (c) { return c && c.name && (c.result || c.isError); }); - if (m.content || calls.length) messages.push({ role: m.role, content: m.content, toolCalls: calls, files: files }); + // 连接失败的卡片没有调用参数,只在界面显示,不当作工具调用重放。 + var calls = (m.toolCalls || []).filter(function (c) { return c && c.name && c.args !== "" && (c.result || c.isError); }); + if (m.content || calls.length) messages.push({ role: m.role, content: m.content, rounds: m.rounds, toolCalls: calls, files: files }); continue; } var parts = [], otherFiles = []; for (var f of files) { - if (!MagpieEpub.isEpub(f)) { + var isEpub = MagpieEpub.isEpub(f); + var isText = !isEpub && f.kind !== "image" && (/\.(md|markdown|txt)$/i.test(f.name || "") || /^text\//i.test(f.type || "")); + if (!isText && !isEpub) { if (f.data) otherFiles.push(f); continue; } @@ -2175,13 +2231,16 @@ async function requestMessages(history, warn) { continue; } try { - var book = await MagpieEpub.read(f.data); - parts.push("【电子书:" + f.name + "】\n" + book.text + "\n【电子书结束】"); + var book = isText + ? { text: new TextDecoder("utf-8", { fatal: true }).decode(Uint8Array.from(atob(bytes), function (c) { return c.charCodeAt(0); })) } + : await MagpieEpub.read(f.data); + var label = isText ? "文档" : "电子书"; + parts.push("【" + label + ":" + f.name + "】\n" + book.text + "\n【" + label + "结束】"); if (book.imagePages) parts.push("(" + book.imagePages + " 个章节仅含图片,未提取文字。)"); seen.add(bytes); } catch (err) { var reason = f.name + ":" + err.message; - parts.push("(未附上这本电子书的正文。" + reason + ")"); + parts.push("(未附上" + (isText ? "这份文档" : "这本电子书") + "的正文。" + reason + ")"); warn(reason); } } @@ -2195,11 +2254,12 @@ async function requestMessages(history, warn) { function send() { if (state.busy) { stop(); return; } - if (pendingBooks) { toast("电子书还在读取,请稍候。"); return; } + if (pendingAttachments) { toast("附件还在读取,请稍候。"); return; } var input = $("#input"); var text = input.value.trim(); if (!text && !(state.attachments || []).length) return; if (!state.settings.model) { toast("先选一个模型", true); return; } + conversationRequest = null; var files = (state.attachments || []).map(function (f) { return { name: f.name, type: f.type, kind: f.kind, size: f.size, data: f.data, bookChars: f.bookChars, imagePages: f.imagePages }; @@ -2281,6 +2341,10 @@ function send() { function handleEvent(evt, refs) { var msg = refs.msg; switch (evt.type) { + case "round": + if (!msg.rounds) msg.rounds = []; + msg.rounds.push({ content: "", toolIds: [] }); + break; case "start": msg.model = evt.model || state.settings.model; msg.tools = Array.isArray(evt.tools) ? evt.tools : []; @@ -2302,6 +2366,7 @@ function handleEvent(evt, refs) { } break; case "delta": + if (msg.rounds && msg.rounds.length) msg.rounds[msg.rounds.length - 1].content += evt.text || ""; // 先攒着,交给打字机队列按帧放,文字才是连续流出来的 if (!msg.streamStartedAt) msg.streamStartedAt = (typeof performance !== "undefined" ? performance.now() : Date.now()); msg.pending = (msg.pending || "") + (evt.text || ""); @@ -2355,6 +2420,7 @@ function handleTool(evt, refs) { if (evt.phase === "call") { var call = { id: evt.id || uid("call"), name: evt.name || "工具", args: evt.args || {}, result: "", isError: false }; msg.toolCalls.push(call); + if (msg.rounds && msg.rounds.length) msg.rounds[msg.rounds.length - 1].toolIds.push(call.id); var card = makeTalk(call); refs.cards[call.id] = card; refs.talks.appendChild(card.root); @@ -2507,6 +2573,7 @@ function isBlankDraft() { function newChat() { if (state.busy) { toast("正在生成,先停止再新建对话", true); return; } + conversationRequest = null; // 已经在一个空白的新对话里了:再点"新对话"就保持原样,别叠出第二个 if (isBlankDraft()) { $("#input").focus(); return; } clearPendingAttachments(); diff --git a/packages/chat/epub-reader.test.mjs b/packages/chat/epub-reader.test.mjs index b082820..5ec0575 100644 --- a/packages/chat/epub-reader.test.mjs +++ b/packages/chat/epub-reader.test.mjs @@ -116,7 +116,7 @@ test.skipIf(!browserPath && !process.env.CI)("reads complete books in spine orde } }); const profile = mkdtempSync(join(tmpdir(), "magpie-epub-browser-")); const proc = Bun.spawn([browserPath, "--headless", "--disable-gpu", "--disable-background-networking", - "--no-first-run", "--user-data-dir=" + profile, String(server.url)], { stdout: "ignore", stderr: "ignore" }); + "--no-first-run", "--use-mock-keychain", "--user-data-dir=" + profile, String(server.url)], { stdout: "ignore", stderr: "ignore" }); try { const result = await Promise.race([promise, proc.exited.then(() => { throw new Error("browser exited"); }), new Promise((_, reject) => setTimeout(() => reject(new Error("browser timed out")), 15000).unref())]); diff --git a/packages/chat/lib/server.mjs b/packages/chat/lib/server.mjs index 00c3adf..e07a688 100644 --- a/packages/chat/lib/server.mjs +++ b/packages/chat/lib/server.mjs @@ -14,6 +14,7 @@ import { callTool, closeAll, listTools, workspaceDir } from "./mcp.mjs"; const here = fileURLToPath(new URL(".", import.meta.url)); const UI_ROOT = join(here, "..", "chat"); const MAX_TOOL_ROUNDS = 8; +const MAX_TOOL_RESULT = 60000; // 插件版本,从包里的 package.json 读(别在两处写死同一个数字) const VERSION = (() => { @@ -418,40 +419,47 @@ async function runChat({ res, body, settings, store, options }, controllers) { if (system) messages.push({ role: "system", content: system }); for (const m of history) { const calls = m.role === "assistant" && Array.isArray(m.toolCalls) ? m.toolCalls.filter((c) => c && c.name) : []; - if (!calls.length) { + const rounds = m.role === "assistant" && Array.isArray(m.rounds) ? m.rounds : null; + if (!calls.length && !rounds) { messages.push({ role: m.role, content: toGatewayContent(m) }); continue; } // 一轮工具调用的完整重放:assistant.tool_calls 后面必须跟每个调用的 tool 结果, // 少一条网关就拒收,所以没有结果的调用不放进历史。 const done = calls.filter((c) => String(c.result ?? "") !== "" || c.isError); - if (!done.length) { - if (m.content) messages.push({ role: "assistant", content: String(m.content) }); - continue; - } - messages.push({ - role: "assistant", - content: m.content || "", - tool_calls: done.map((c, i) => ({ - id: String(c.id || `history_${i}`), - type: "function", - function: { name: String(c.name), arguments: typeof c.args === "string" ? c.args : JSON.stringify(c.args || {}) }, - })), - }); - for (const [i, c] of done.entries()) { + const byId = new Map(done.map(c => [c.id, c])); + for (const part of rounds || [{ content: m.content }]) { + const partCalls = rounds ? (part.toolIds || []).map(id => byId.get(id)).filter(Boolean) : done; + if (!partCalls.length) { + if (part.content) messages.push({ role: "assistant", content: String(part.content) }); + continue; + } messages.push({ - role: "tool", - tool_call_id: String(c.id || `history_${i}`), - content: String(c.result ?? "").slice(0, 20000) || (c.isError ? "error" : "ok"), + role: "assistant", + content: part.content || "", + tool_calls: partCalls.map((c, i) => ({ + id: String(c.id || `history_${i}`), + type: "function", + function: { name: String(c.name), arguments: typeof c.args === "string" ? c.args : JSON.stringify(c.args || {}) }, + })), }); + for (const [i, c] of partCalls.entries()) { + messages.push({ + role: "tool", + tool_call_id: String(c.id || `history_${i}`), + content: String(c.result ?? "").slice(0, MAX_TOOL_RESULT) || (c.isError ? "error" : "ok"), + }); + } } } sse(res, "start", { model, tools: tools.map((t) => t.name), gateway: await resolveGateway(options).catch(() => gatewayURL(options)) }); let usageTotal = { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 }; + const callIds = new Set(); try { for (let round = 0; round < MAX_TOOL_ROUNDS; round++) { + sse(res, "round", {}); const payload = { model, messages, stream: true }; if (effort) payload.reasoning_effort = effort; if (Number.isFinite(temperature)) payload.temperature = temperature; @@ -499,18 +507,27 @@ async function runChat({ res, body, settings, store, options }, controllers) { } if (calls.size && (finish === "tool_calls" || finish === "stop")) { - const made = [...calls.values()].filter((c) => c.name); + const made = [...calls.values()].filter((c) => c.name).map((c, i) => { + let id = c.id; + if (!id || callIds.has(id)) { + id = `call_${round}_${i}`; + while (callIds.has(id)) id += "_"; + } + callIds.add(id); + return { ...c, id }; + }); if (!made.length) { sse(res, "done", { ms: Date.now() - started, usage: usageTotal }); res.end(); return; } messages.push({ role: "assistant", content: content || "", - tool_calls: made.map((c, i) => ({ id: c.id || `call_${round}_${i}`, type: "function", function: { name: c.name, arguments: c.args || "{}" } })), + tool_calls: made.map((c) => ({ id: c.id, type: "function", function: { name: c.name, arguments: c.args || "{}" } })), }); if (content) sse(res, "delta", { text: "" }); for (const c of made) { - const id = c.id || `call_${round}`; + abort.signal.throwIfAborted(); + const id = c.id; let args = {}; try { args = JSON.parse(c.args || "{}"); } catch { args = {}; } const tool = tools.find((t) => t.name === c.name) || tools.find((t) => t.rawName === c.name); @@ -520,6 +537,7 @@ async function runChat({ res, body, settings, store, options }, controllers) { result = "没有这个工具:" + c.name; isError = true; } else { const server = (await readMcp(undefined, options)).find((s) => s.name === tool.server); + abort.signal.throwIfAborted(); try { const out = await callTool(server, tool.rawName, args); result = out.text; isError = Boolean(out.isError); @@ -527,8 +545,9 @@ async function runChat({ res, body, settings, store, options }, controllers) { result = String(err?.message || err); isError = true; } } - sse(res, "tool", { phase: "result", id, name: c.name, args, result: String(result).slice(0, 20000), isError }); - messages.push({ role: "tool", tool_call_id: id, content: String(result).slice(0, 60000) || (isError ? "error" : "ok") }); + result = String(result).slice(0, MAX_TOOL_RESULT) || (isError ? "error" : "ok"); + sse(res, "tool", { phase: "result", id, name: c.name, args, result, isError }); + messages.push({ role: "tool", tool_call_id: id, content: result }); } continue; // 让模型基于工具结果继续 } diff --git a/packages/chat/package.json b/packages/chat/package.json index 4a06745..4247a37 100644 --- a/packages/chat/package.json +++ b/packages/chat/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/page-chat", - "version": "0.1.10", + "version": "0.1.11", "description": "在 magpie 窗口里加一个「对话」页面:用 magpie 自己的模型、MCP 服务器、指令和技能聊天,支持图片与 LaTeX。A chat page inside magpie's window: talk with magpie's own models, MCP servers, instructions and skills, with images and LaTeX.", "author": "Magic_zero (https://github.com/Magiczerowxy)", "files": [