Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix: separate Codex progress from final sidebar reply

tianma-if8b2a17c7640b
diff --git a/apps/desktop/src/main/acp-host.mjs b/apps/desktop/src/main/acp-host.mjs
index 083696af..d6662b69 100644
--- a/apps/desktop/src/main/acp-host.mjs
+++ b/apps/desktop/src/main/acp-host.mjs
@@ -462,7 +462,8 @@ export function eventsFromSessionUpdate(requestId, params) {
}
if (update.sessionUpdate === "agent_message_chunk") {
if (update.content?.type === "text" && typeof update.content.text === "string" && update.content.text.length > 0) {
- return [{ requestId, type: "text-delta", text: update.content.text }];
+ return [{ requestId, type: "text-delta", text: update.content.text,
+ ...(typeof update.messageId === "string" && update.messageId ? { messageId: update.messageId } : {}) }];
}
const image = imageEventFromBlock(requestId, update.content, "");
if (!image) return [];
diff --git a/apps/desktop/src/main/acp-host.test.mjs b/apps/desktop/src/main/acp-host.test.mjs
index 7a86b020..c23e8e04 100644
--- a/apps/desktop/src/main/acp-host.test.mjs
+++ b/apps/desktop/src/main/acp-host.test.mjs
@@ -502,6 +502,9 @@ describe("ACP spawn and failure mapping", () => {
expect(eventsFromSessionUpdate("r1", {
update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "hello" } },
})).toEqual([{ requestId: "r1", type: "text-delta", text: "hello" }]);
+ expect(eventsFromSessionUpdate("r1", {
+ update: { sessionUpdate: "agent_message_chunk", messageId: "message-1", content: { type: "text", text: "hello" } },
+ })).toEqual([{ requestId: "r1", type: "text-delta", text: "hello", messageId: "message-1" }]);
expect(eventsFromSessionUpdate("r1", {
update: { sessionUpdate: "agent_thought_chunk", content: { type: "text", text: "hmm" } },
})).toEqual([{ requestId: "r1", type: "reasoning", text: "hmm" }]);
diff --git a/apps/web/src/components/ai-sidebar/AiSidebar.tsx b/apps/web/src/components/ai-sidebar/AiSidebar.tsx
index 9807b5c1..4a7f4835 100644
--- a/apps/web/src/components/ai-sidebar/AiSidebar.tsx
+++ b/apps/web/src/components/ai-sidebar/AiSidebar.tsx
@@ -69,6 +69,7 @@ import {
type ChatThreadSummary,
} from "@/lib/local-agent-threads";
import { LocalAgentImageStore } from "@/lib/local-agent-images";
+import { appendLocalAgentText } from "@/lib/local-agent-response";
import { sidebarRevealTransition } from "@/lib/motion";
import {
SELECTION_AI_LANGUAGES,
@@ -132,6 +133,8 @@ type LocalTurn = {
threadId: string;
message: string;
response: string;
+ responseMessageId?: string;
+ adapterId?: string;
reasoning: string;
tools: LocalToolRow[];
images: LocalImage[];
@@ -921,7 +924,7 @@ function AiSidebarSession({
if (!turnId || !alive.current) return;
if (event.type === "text-delta") {
setLocalTurns((previous) => previous.map((turn) => turn.id === turnId && turn.status !== "cancelled"
- ? { ...turn, response: turn.response + event.text }
+ ? appendLocalAgentText(turn, event.text, event.messageId, turn.adapterId === "codex")
: turn));
return;
}
@@ -1146,6 +1149,7 @@ function AiSidebarSession({
threadId: activeLocalThreadId,
message: text,
response: "",
+ adapterId: adapter.id,
reasoning: "",
tools: [],
images: [],
diff --git a/apps/web/src/lib/desktop-acp.ts b/apps/web/src/lib/desktop-acp.ts
index 0f4ba31f..ce208309 100644
--- a/apps/web/src/lib/desktop-acp.ts
+++ b/apps/web/src/lib/desktop-acp.ts
@@ -60,7 +60,7 @@ export type DesktopAcpPromptResult = {
};
export type DesktopAcpEvent =
- | { requestId: string; type: "text-delta"; text: string }
+ | { requestId: string; type: "text-delta"; text: string; messageId?: string }
| { requestId: string; type: "reasoning"; text: string }
| { requestId: string; type: "tool"; name: string; status: string; title?: string }
| { requestId: string; type: "image"; id: string; mediaType: string; base64: string }
diff --git a/apps/web/src/lib/local-agent-response.test.mjs b/apps/web/src/lib/local-agent-response.test.mjs
new file mode 100644
index 00000000..7a5cb291
--- /dev/null
+++ b/apps/web/src/lib/local-agent-response.test.mjs
@@ -0,0 +1,24 @@
+import { expect, test } from "bun:test";
+import { appendLocalAgentText } from "./local-agent-response.ts";
+
+test("moves earlier Codex messages into the process when the final message starts", () => {
+ let turn = { response: "", reasoning: "Thought summary", adapterId: "codex" };
+ turn = appendLocalAgentText(turn, "I will read", "commentary", true);
+ turn = appendLocalAgentText(turn, " the note.", "commentary", true);
+ turn = appendLocalAgentText(turn, "Translated", "final", true);
+ turn = appendLocalAgentText(turn, " text.", "final", true);
+ expect(turn.reasoning).toBe("Thought summary\n\nI will read the note.");
+ expect(turn.response).toBe("Translated text.");
+});
+
+test("keeps a single Codex message as the answer", () => {
+ const turn = appendLocalAgentText({ response: "", reasoning: "" }, "Only answer", "final", true);
+ expect(turn.response).toBe("Only answer");
+ expect(turn.reasoning).toBe("");
+});
+
+test("preserves the old behavior without a Codex message boundary", () => {
+ const initial = { response: "First", reasoning: "" };
+ expect(appendLocalAgentText(initial, " second", "other", false).response).toBe("First second");
+ expect(appendLocalAgentText(initial, " second", undefined, true).response).toBe("First second");
+});
diff --git a/apps/web/src/lib/local-agent-response.ts b/apps/web/src/lib/local-agent-response.ts
new file mode 100644
index 00000000..b1cb3fcb
--- /dev/null
+++ b/apps/web/src/lib/local-agent-response.ts
@@ -0,0 +1,25 @@
+type LocalAgentResponse = {
+ response: string;
+ reasoning: string;
+ responseMessageId?: string;
+};
+
+// Codex ACP sends commentary and the final answer as separate agent messages.
+// Standard ACP has no phase field, but Codex keeps a stable ID for each message.
+export function appendLocalAgentText<T extends LocalAgentResponse>(
+ turn: T,
+ text: string,
+ messageId?: string,
+ separateMessages = false,
+): T {
+ if (!separateMessages || !messageId) return { ...turn, response: turn.response + text };
+ if (turn.responseMessageId && turn.responseMessageId !== messageId && turn.response) {
+ return {
+ ...turn,
+ reasoning: turn.reasoning ? `${turn.reasoning.trimEnd()}\n\n${turn.response}` : turn.response,
+ response: text,
+ responseMessageId: messageId,
+ };
+ }
+ return { ...turn, response: turn.response + text, responseMessageId: messageId };
+}
diff --git a/apps/web/src/vite-env.d.ts b/apps/web/src/vite-env.d.ts
index 798e50e0..167b9093 100644
--- a/apps/web/src/vite-env.d.ts
+++ b/apps/web/src/vite-env.d.ts
@@ -137,7 +137,7 @@ interface EdgeEverDesktopBridge {
}): Promise<{ requestId: string; rejectedAttachments?: Array<{ filename: string; reason: string }> }>;
cancelAcp?(requestId: string): Promise<{ ok: true }>;
onAcpEvent?(callback: (event:
- | { requestId: string; type: "text-delta"; text: string }
+ | { requestId: string; type: "text-delta"; text: string; messageId?: string }
| { requestId: string; type: "reasoning"; text: string }
| { requestId: string; type: "tool"; name: string; status: string; title?: string }
| { requestId: string; type: "image"; id: string; mediaType: string; base64: string }