diff --git a/extensions/googlechat/src/actions.ts b/extensions/googlechat/src/actions.ts index 0b5447509476a..66076893af8fb 100644 --- a/extensions/googlechat/src/actions.ts +++ b/extensions/googlechat/src/actions.ts @@ -160,6 +160,12 @@ export async function handleGoogleChatAction({ if (messageNames.length === 0) { return jsonResult({ ok: true, to: space, ...sent }); } + findGoogleChatProgressActivity({ + accountId: account.accountId, + spaceId: space, + threadName: deliveredThread, + sessionKey, + })?.noteSeparatePost?.(); // Only Chat's confirmed thread proves a source reply when the send inherited it. const receipt = createMessageReceiptFromOutboundResults({ results: messageNames.map((messageId) => ({ diff --git a/extensions/googlechat/src/monitor.reply-target.test.ts b/extensions/googlechat/src/monitor.reply-target.test.ts index 8d9d6ca09d273..3d79b5756b118 100644 --- a/extensions/googlechat/src/monitor.reply-target.test.ts +++ b/extensions/googlechat/src/monitor.reply-target.test.ts @@ -5,6 +5,7 @@ import { GoogleChatApiError } from "./api.js"; import type { GoogleChatIngressLifecycle } from "./monitor-ingress.js"; import type { GoogleChatCoreRuntime, GoogleChatRuntimeEnv } from "./monitor-types.js"; import "./monitor.js"; +import { findGoogleChatProgressActivity } from "./progress-activity.js"; import type { GoogleChatEvent } from "./types.js"; const apiMocks = vi.hoisted(() => ({ @@ -345,6 +346,82 @@ describe("Google Chat automatic reply target reconciliation", () => { }); }); + it("keeps folded progress visible when the turn ends without an answer", async () => { + const account = createAccount({ replyToMode: "all" }); + const core = createCore({ + run: async () => { + const activity = findGoogleChatProgressActivity({ + accountId: account.accountId, + spaceId: SPACE, + threadName: THREAD, + }); + expect(await activity?.update("Checking the calendar next.", {})).toBe(true); + }, + }); + apiMocks.sendGoogleChatMessage.mockResolvedValueOnce({ + messageName: TYPING, + threadName: THREAD, + }); + + await processEvent({ account, core }); + + expect(apiMocks.deleteGoogleChatMessage).not.toHaveBeenCalled(); + expect(apiMocks.updateGoogleChatMessage).toHaveBeenLastCalledWith({ + account, + messageName: TYPING, + text: "Checking the calendar next.", + }); + }); + + it("keeps an automatic status notice visible when the turn ends without an answer", async () => { + const account = createAccount({ replyToMode: "all" }); + const core = createCore({ + run: async (delivery) => { + const notice = { text: "I’m continuing this work.", isStatusNotice: true }; + await delivery.deliver(notice, { kind: "block" }); + }, + }); + apiMocks.sendGoogleChatMessage.mockResolvedValueOnce({ + messageName: TYPING, + threadName: THREAD, + }); + + await processEvent({ account, core }); + + expect(apiMocks.deleteGoogleChatMessage).not.toHaveBeenCalled(); + expect(apiMocks.updateGoogleChatMessage).toHaveBeenLastCalledWith({ + account, + messageName: TYPING, + text: "I’m continuing this work.", + }); + }); + + it("removes folded progress once an answer is posted separately", async () => { + const account = createAccount({ replyToMode: "all" }); + const core = createCore({ + run: async () => { + const activity = findGoogleChatProgressActivity({ + accountId: account.accountId, + spaceId: SPACE, + threadName: THREAD, + }); + await activity?.update("Checking the calendar next.", {}); + activity?.noteSeparatePost?.(); + }, + }); + apiMocks.sendGoogleChatMessage.mockResolvedValueOnce({ + messageName: TYPING, + threadName: THREAD, + }); + + await processEvent({ account, core }); + + expect(apiMocks.deleteGoogleChatMessage).toHaveBeenCalledExactlyOnceWith({ + account, + messageName: TYPING, + }); + }); + it("threads only the first visible answer in first mode", async () => { const account = createAccount({ replyToMode: "first", typingIndicator: "none" }); const core = createCore({ diff --git a/extensions/googlechat/src/monitor.ts b/extensions/googlechat/src/monitor.ts index 228a56fb313e9..1ef847f1f038a 100644 --- a/extensions/googlechat/src/monitor.ts +++ b/extensions/googlechat/src/monitor.ts @@ -19,6 +19,7 @@ import { resolveWebhookPath } from "openclaw/plugin-sdk/webhook-ingress"; import type { ResolvedGoogleChatAccount } from "./accounts.js"; import { deleteGoogleChatMessage, + updateGoogleChatMessage, downloadGoogleChatMedia, GoogleChatApiError, sendGoogleChatMessage, @@ -387,6 +388,10 @@ async function processGoogleChatEvent( onError: (error) => runtime.error?.(`Google Chat status update failed: ${String(error)}`), }) : undefined; + // Progress folded into the activity message stays visible when the turn ends + // without a separately posted answer, e.g. after handing work to a subagent. + let foldedProgressText: string | undefined; + let answerPosted = false; const unregisterProgressActivity = activity && typingMessage ? registerGoogleChatProgressActivity({ @@ -407,7 +412,14 @@ async function processGoogleChatEvent( } hooks.assertDirectAdapterHandoff?.(); await hooks.onPlatformSendDispatch?.(); - return await activity.updateText(chunk); + const updated = await activity.updateText(chunk); + if (updated) { + foldedProgressText = chunk; + } + return updated; + }, + noteSeparatePost: () => { + answerPosted = true; }, }) : undefined; @@ -427,6 +439,18 @@ async function processGoogleChatEvent( } const placeholder = typingMessage; typingMessage = undefined; + if (placeholder && foldedProgressText && !answerPosted) { + try { + await updateGoogleChatMessage({ + account, + messageName: placeholder.name, + text: foldedProgressText, + }); + return; + } catch (error) { + runtime.error?.(`Google Chat folded progress restore failed: ${String(error)}`); + } + } if (placeholder) { try { await deleteGoogleChatMessage({ account, messageName: placeholder.name }); @@ -509,7 +533,13 @@ async function processGoogleChatEvent( )[0]; if (text) { try { - await activity.updateText(text); + const updated = await activity.updateText(text); + if ( + updated && + (payload.isCommentary === true || payload.isStatusNotice === true) + ) { + foldedProgressText = text; + } } catch (error) { runtime.error?.(`Google Chat progress update failed: ${String(error)}`); } @@ -549,6 +579,7 @@ async function processGoogleChatEvent( !isProgressReply(payload, info?.kind) ) { hasVisibleAnswer = true; + answerPosted = true; if (resolveReplyThreadName(payload)) { hasThreadedReply = true; } diff --git a/extensions/googlechat/src/progress-activity.ts b/extensions/googlechat/src/progress-activity.ts index 0585ddbcdc8a2..f000ec3b45258 100644 --- a/extensions/googlechat/src/progress-activity.ts +++ b/extensions/googlechat/src/progress-activity.ts @@ -14,6 +14,8 @@ export type GoogleChatProgressActivity = { threadName?: string; /** Session that owns the turn, so concurrent turns never edit each other's activity. */ sessionKey: string; + /** Records a separate visible post in this placement, so the activity can be removed. */ + noteSeparatePost?: () => void; /** Returns false when the activity message was not edited. */ update: ( text: string,