Skip to content

Commit 3dce01f

Browse files
authored
fix(googlechat): retain fallback thread continuity (#117053)
1 parent 383363e commit 3dce01f

5 files changed

Lines changed: 295 additions & 17 deletions

File tree

‎extensions/googlechat/src/monitor-reply-delivery.ts‎

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,35 @@ import type { ResolvedGoogleChatAccount } from "./accounts.js";
55
import { deleteGoogleChatMessage, sendGoogleChatMessage, updateGoogleChatMessage } from "./api.js";
66
import type { GoogleChatCoreRuntime, GoogleChatRuntimeEnv } from "./monitor-types.js";
77

8-
export type GoogleChatTypingMessage = {
9-
name: string;
10-
thread?: string;
11-
};
8+
export type GoogleChatTypingMessage =
9+
| {
10+
placement: "top-level";
11+
name: string;
12+
}
13+
| {
14+
placement: "thread";
15+
name: string;
16+
requestedThreadName: string;
17+
deliveredThreadName: string;
18+
};
19+
20+
export function createGoogleChatTypingMessage(params: {
21+
messageName: string;
22+
requestedThreadName?: string;
23+
deliveredThreadName?: string;
24+
}): GoogleChatTypingMessage {
25+
const name = params.messageName.trim();
26+
const requestedThreadName = params.requestedThreadName?.trim();
27+
if (!requestedThreadName) {
28+
return { placement: "top-level", name };
29+
}
30+
return {
31+
placement: "thread",
32+
name,
33+
requestedThreadName,
34+
deliveredThreadName: params.deliveredThreadName?.trim() || requestedThreadName,
35+
};
36+
}
1237

1338
export async function deliverGoogleChatReply(params: {
1439
payload: {
@@ -30,12 +55,18 @@ export async function deliverGoogleChatReply(params: {
3055
// text delivery can keep retrying a dead message and drop content.
3156
let typingMessage = params.typingMessage;
3257
const replyThreadName = payload.replyToId?.trim() || undefined;
33-
const typingMessageThreadName = typingMessage?.thread?.trim() || undefined;
3458
const reply = resolveSendableOutboundReplyParts(payload);
3559
const text = reply.text;
3660
let firstTextChunk = true;
61+
let deliveryThreadName = replyThreadName;
3762

38-
if (typingMessage && typingMessageThreadName !== replyThreadName) {
63+
const typingMatchesReply =
64+
typingMessage?.placement === "thread"
65+
? typingMessage.requestedThreadName === replyThreadName
66+
: typingMessage?.placement === "top-level"
67+
? replyThreadName === undefined
68+
: false;
69+
if (typingMessage && !typingMatchesReply) {
3970
// Typing starts before reply directives are resolved. Never edit a placeholder
4071
// from one thread into a final reply targeted at another conversation surface.
4172
try {
@@ -44,6 +75,10 @@ export async function deliverGoogleChatReply(params: {
4475
runtime.error?.(`Google Chat typing cleanup failed: ${String(err)}`);
4576
}
4677
typingMessage = undefined;
78+
} else if (typingMessage?.placement === "thread") {
79+
// The requested thread decides whether the placeholder still belongs to this reply;
80+
// the provider-returned thread owns every later physical send after fallback.
81+
deliveryThreadName = typingMessage.deliveredThreadName;
4782
}
4883

4984
if (reply.hasMedia) {
@@ -75,12 +110,15 @@ export async function deliverGoogleChatReply(params: {
75110
}
76111
};
77112
const sendTextMessage = async (chunk: string) => {
78-
await sendGoogleChatMessage({
113+
const sent = await sendGoogleChatMessage({
79114
account,
80115
space: spaceId,
81116
text: chunk,
82-
thread: replyThreadName,
117+
thread: deliveryThreadName,
83118
});
119+
if (replyThreadName) {
120+
deliveryThreadName = sent?.threadName?.trim() || deliveryThreadName;
121+
}
84122
};
85123
const chunks = core.channel.text.chunkMarkdownTextWithMode(text, chunkLimit, chunkMode);
86124
for (const chunk of chunks) {

‎extensions/googlechat/src/monitor.reply-delivery-failure.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,10 @@ async function runDelivery(params: {
159159
...(params.withTypingMessage
160160
? {
161161
typingMessage: {
162+
placement: "thread",
162163
name: "spaces/AAA/messages/typing",
163-
thread: "spaces/AAA/threads/root",
164+
requestedThreadName: "spaces/AAA/threads/root",
165+
deliveredThreadName: "spaces/AAA/threads/root",
164166
},
165167
}
166168
: {}),

‎extensions/googlechat/src/monitor.reply-delivery.test.ts‎

Lines changed: 135 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ function createRuntime() {
4949
} satisfies GoogleChatRuntimeEnv;
5050
}
5151

52+
let createGoogleChatTypingMessage: typeof import("./monitor-reply-delivery.js").createGoogleChatTypingMessage;
5253
let deliverGoogleChatReply: typeof import("./monitor-reply-delivery.js").deliverGoogleChatReply;
5354

5455
beforeEach(async () => {
5556
vi.clearAllMocks();
56-
({ deliverGoogleChatReply } = await import("./monitor-reply-delivery.js"));
57+
({ createGoogleChatTypingMessage, deliverGoogleChatReply } =
58+
await import("./monitor-reply-delivery.js"));
5759
});
5860

5961
afterAll(() => {
@@ -78,8 +80,10 @@ describe("Google Chat reply delivery", () => {
7880
config,
7981
statusSink,
8082
typingMessage: {
83+
placement: "thread",
8184
name: "spaces/AAA/messages/typing",
82-
thread: "spaces/AAA/threads/root",
85+
requestedThreadName: "spaces/AAA/threads/root",
86+
deliveredThreadName: "spaces/AAA/threads/root",
8387
},
8488
});
8589

@@ -107,6 +111,123 @@ describe("Google Chat reply delivery", () => {
107111
);
108112
});
109113

114+
it("continues later chunks in the provider fallback thread", async () => {
115+
const core = createCore({ chunks: ["first chunk", "second chunk"] });
116+
const runtime = createRuntime();
117+
mocks.sendGoogleChatMessage
118+
.mockResolvedValueOnce({
119+
messageName: "spaces/AAA/messages/first",
120+
threadName: "spaces/AAA/threads/fallback",
121+
})
122+
.mockResolvedValueOnce({
123+
messageName: "spaces/AAA/messages/second",
124+
threadName: "spaces/AAA/threads/fallback",
125+
});
126+
127+
await deliverGoogleChatReply({
128+
payload: { text: "two chunks", replyToId: "spaces/AAA/threads/requested" },
129+
account,
130+
spaceId: "spaces/AAA",
131+
runtime,
132+
core,
133+
config,
134+
});
135+
136+
expect(mocks.sendGoogleChatMessage).toHaveBeenNthCalledWith(1, {
137+
account,
138+
space: "spaces/AAA",
139+
text: "first chunk",
140+
thread: "spaces/AAA/threads/requested",
141+
});
142+
expect(mocks.sendGoogleChatMessage).toHaveBeenNthCalledWith(2, {
143+
account,
144+
space: "spaces/AAA",
145+
text: "second chunk",
146+
thread: "spaces/AAA/threads/fallback",
147+
});
148+
});
149+
150+
it("continues after a fallback typing placeholder in its delivered thread", async () => {
151+
const core = createCore({ chunks: ["first chunk", "second chunk"] });
152+
const runtime = createRuntime();
153+
mocks.sendGoogleChatMessage.mockResolvedValueOnce({
154+
messageName: "spaces/AAA/messages/second",
155+
threadName: "spaces/AAA/threads/fallback",
156+
});
157+
158+
await deliverGoogleChatReply({
159+
payload: { text: "two chunks", replyToId: "spaces/AAA/threads/requested" },
160+
account,
161+
spaceId: "spaces/AAA",
162+
runtime,
163+
core,
164+
config,
165+
typingMessage: createGoogleChatTypingMessage({
166+
messageName: "spaces/AAA/messages/typing",
167+
requestedThreadName: "spaces/AAA/threads/requested",
168+
deliveredThreadName: "spaces/AAA/threads/fallback",
169+
}),
170+
});
171+
172+
expect(mocks.updateGoogleChatMessage).toHaveBeenCalledWith({
173+
account,
174+
messageName: "spaces/AAA/messages/typing",
175+
text: "first chunk",
176+
});
177+
expect(mocks.sendGoogleChatMessage).toHaveBeenCalledOnce();
178+
expect(mocks.sendGoogleChatMessage).toHaveBeenCalledWith({
179+
account,
180+
space: "spaces/AAA",
181+
text: "second chunk",
182+
thread: "spaces/AAA/threads/fallback",
183+
});
184+
});
185+
186+
it("keeps the requested thread when the provider omits thread metadata", async () => {
187+
const core = createCore({ chunks: ["first chunk", "second chunk"] });
188+
const runtime = createRuntime();
189+
mocks.sendGoogleChatMessage.mockResolvedValue({
190+
messageName: "spaces/AAA/messages/sent",
191+
});
192+
193+
await deliverGoogleChatReply({
194+
payload: { text: "two chunks", replyToId: "spaces/AAA/threads/requested" },
195+
account,
196+
spaceId: "spaces/AAA",
197+
runtime,
198+
core,
199+
config,
200+
});
201+
202+
expect(mocks.sendGoogleChatMessage).toHaveBeenCalledTimes(2);
203+
for (const call of mocks.sendGoogleChatMessage.mock.calls) {
204+
expect(call[0]?.thread).toBe("spaces/AAA/threads/requested");
205+
}
206+
});
207+
208+
it("keeps top-level chunks top-level when Google returns a thread name", async () => {
209+
const core = createCore({ chunks: ["first chunk", "second chunk"] });
210+
const runtime = createRuntime();
211+
mocks.sendGoogleChatMessage.mockResolvedValue({
212+
messageName: "spaces/AAA/messages/sent",
213+
threadName: "spaces/AAA/threads/provider-created",
214+
});
215+
216+
await deliverGoogleChatReply({
217+
payload: { text: "two top-level chunks" },
218+
account,
219+
spaceId: "spaces/AAA",
220+
runtime,
221+
core,
222+
config,
223+
});
224+
225+
expect(mocks.sendGoogleChatMessage).toHaveBeenCalledTimes(2);
226+
for (const call of mocks.sendGoogleChatMessage.mock.calls) {
227+
expect(call[0]?.thread).toBeUndefined();
228+
}
229+
});
230+
110231
it("rejects when a later text chunk send fails instead of dropping it silently", async () => {
111232
const core = createCore({ chunks: ["first chunk", "second chunk", "third chunk"] });
112233
const runtime = createRuntime();
@@ -145,8 +266,10 @@ describe("Google Chat reply delivery", () => {
145266
core,
146267
config,
147268
typingMessage: {
269+
placement: "thread",
148270
name: "spaces/AAA/messages/typing",
149-
thread: "spaces/AAA/threads/root",
271+
requestedThreadName: "spaces/AAA/threads/root",
272+
deliveredThreadName: "spaces/AAA/threads/root",
150273
},
151274
}),
152275
).rejects.toBe(fallbackError);
@@ -167,8 +290,10 @@ describe("Google Chat reply delivery", () => {
167290
core,
168291
config,
169292
typingMessage: {
293+
placement: "thread",
170294
name: "spaces/AAA/messages/typing",
171-
thread: "spaces/AAA/threads/root",
295+
requestedThreadName: "spaces/AAA/threads/root",
296+
deliveredThreadName: "spaces/AAA/threads/root",
172297
},
173298
});
174299

@@ -203,8 +328,10 @@ describe("Google Chat reply delivery", () => {
203328
core,
204329
config,
205330
typingMessage: {
331+
placement: "thread",
206332
name: "spaces/AAA/messages/typing",
207-
thread: "spaces/AAA/threads/root",
333+
requestedThreadName: "spaces/AAA/threads/root",
334+
deliveredThreadName: "spaces/AAA/threads/root",
208335
},
209336
});
210337

@@ -237,8 +364,10 @@ describe("Google Chat reply delivery", () => {
237364
core,
238365
config,
239366
typingMessage: {
367+
placement: "thread",
240368
name: "spaces/AAA/messages/typing",
241-
thread: "spaces/AAA/threads/root",
369+
requestedThreadName: "spaces/AAA/threads/root",
370+
deliveredThreadName: "spaces/AAA/threads/root",
242371
},
243372
}),
244373
).rejects.toThrow(

0 commit comments

Comments
 (0)