Fix assistant message capture: use item/completed events instead of broken condition

The condition (event.kind === 'notification' || event.method === 'turn/completed')
used || where && was needed, but even with &&, notification events don't carry
event.message (only session/error events do), so AppendAssistantMessage was never
dispatched.

Fix by intercepting item/completed notifications for agentMessage items and
extracting the text from event.payload.item.text, consistent with how the web
client handles these events in session-logic.ts.
This commit is contained in:
Cursor Agent committed 2026-02-22 01:39:26 +00:00
1 parent dbbc225fe6
commit 320868a075
1 file changed
+26 -19
+26 -19
View File
@@ -151,25 +151,32 @@ export function createServer(options: ServerOptions) {
data: event,
};
broadcastPush(push);
if (
event.threadId &&
typeof event.message === "string" &&
event.message.trim().length > 0 &&
(event.kind === "notification" || event.method === "turn/completed")
) {
void coreEngine.dispatch({
command: {
kind: "AppendAssistantMessage",
commandId: crypto.randomUUID(),
issuedAt: new Date().toISOString(),
payload: {
threadId: event.threadId,
messageId: event.itemId ?? crypto.randomUUID(),
text: event.message,
createdAt: event.createdAt,
},
},
});
if (event.threadId && event.kind === "notification" && event.method === "item/completed") {
const pl = event.payload as Record<string, unknown> | undefined;
const item = pl && typeof pl === "object" ? (pl as Record<string, unknown>).item : undefined;
if (item && typeof item === "object") {
const rec = item as Record<string, unknown>;
if (rec.type === "agentMessage" && typeof rec.text === "string") {
const text = rec.text as string;
if (text.trim().length > 0) {
const messageId =
typeof rec.id === "string" ? rec.id : (event.itemId ?? crypto.randomUUID());
void coreEngine.dispatch({
command: {
kind: "AppendAssistantMessage",
commandId: crypto.randomUUID(),
issuedAt: new Date().toISOString(),
payload: {
threadId: event.threadId,
messageId,
text,
createdAt: event.createdAt,
},
},
});
}
}
}
}
});