Skip to content

Commit 3d9447c

Browse files
committed
perf(googlechat): reuse DM metadata for session routes
1 parent 323a0ef commit 3d9447c

2 files changed

Lines changed: 93 additions & 14 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
2+
import { resolveGoogleChatOutboundSessionRoute } from "./targets.js";
3+
4+
const mocks = vi.hoisted(() => ({
5+
fetchWithSsrFGuard: vi.fn(
6+
async (params: { url: string; init?: RequestInit; timeoutMs?: number }) => ({
7+
response: await fetch(params.url, params.init),
8+
release: async () => {},
9+
}),
10+
),
11+
getGoogleChatAccessToken: vi.fn().mockResolvedValue("token"),
12+
}));
13+
14+
vi.mock("openclaw/plugin-sdk/ssrf-runtime", () => ({
15+
fetchWithSsrFGuard: mocks.fetchWithSsrFGuard,
16+
}));
17+
18+
vi.mock("./auth.js", () => ({
19+
getGoogleChatAccessToken: mocks.getGoogleChatAccessToken,
20+
}));
21+
22+
afterEach(() => {
23+
vi.unstubAllGlobals();
24+
});
25+
26+
afterAll(() => {
27+
vi.doUnmock("openclaw/plugin-sdk/ssrf-runtime");
28+
vi.doUnmock("./auth.js");
29+
vi.resetModules();
30+
});
31+
32+
describe("outbound session routing", () => {
33+
it("reuses direct-message metadata for route classification", async () => {
34+
const fetchMock = vi.fn().mockImplementation(async () => {
35+
return new Response(JSON.stringify({ name: "spaces/DM-AAA", spaceType: "DIRECT_MESSAGE" }), {
36+
status: 200,
37+
});
38+
});
39+
vi.stubGlobal("fetch", fetchMock);
40+
41+
const route = await resolveGoogleChatOutboundSessionRoute({
42+
cfg: {},
43+
agentId: "main",
44+
target: "users/alice",
45+
});
46+
47+
expect(route).toMatchObject({
48+
peer: { kind: "direct", id: "spaces/DM-AAA" },
49+
chatType: "direct",
50+
from: "googlechat:spaces/DM-AAA",
51+
to: "spaces/DM-AAA",
52+
});
53+
expect(fetchMock).toHaveBeenCalledOnce();
54+
expect(fetchMock).toHaveBeenCalledWith(
55+
"https://chat.googleapis.com/v1/spaces:findDirectMessage?name=users%2Falice",
56+
{
57+
method: "GET",
58+
headers: {
59+
Authorization: "Bearer token",
60+
"Content-Type": "application/json",
61+
},
62+
},
63+
);
64+
});
65+
});

‎extensions/googlechat/src/targets.ts‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ function stripMessageSuffix(target: string): string {
6868
return target.slice(0, index);
6969
}
7070

71-
export async function resolveGoogleChatOutboundSpace(params: {
71+
async function resolveGoogleChatOutboundSpaceDetails(params: {
7272
account: ResolvedGoogleChatAccount;
7373
target: string;
74-
}): Promise<string> {
74+
}): Promise<{ name: string; resource?: GoogleChatSpace }> {
7575
const normalized = normalizeGoogleChatTarget(params.target);
7676
if (!normalized) {
7777
throw new Error("Missing Google Chat target.");
7878
}
7979
const base = stripMessageSuffix(normalized);
8080
if (isGoogleChatSpaceTarget(base)) {
81-
return base;
81+
return { name: base };
8282
}
8383
if (isGoogleChatUserTarget(base)) {
8484
const dm = await findGoogleChatDirectMessage({
@@ -88,9 +88,16 @@ export async function resolveGoogleChatOutboundSpace(params: {
8888
if (!dm?.name) {
8989
throw new Error(`No Google Chat DM found for ${base}`);
9090
}
91-
return dm.name;
91+
return { name: dm.name, resource: dm };
9292
}
93-
return base;
93+
return { name: base };
94+
}
95+
96+
export async function resolveGoogleChatOutboundSpace(params: {
97+
account: ResolvedGoogleChatAccount;
98+
target: string;
99+
}): Promise<string> {
100+
return (await resolveGoogleChatOutboundSpaceDetails(params)).name;
94101
}
95102

96103
export async function resolveGoogleChatOutboundSessionRoute(params: {
@@ -100,19 +107,26 @@ export async function resolveGoogleChatOutboundSessionRoute(params: {
100107
target: string;
101108
}) {
102109
const account = resolveGoogleChatAccount({ cfg: params.cfg, accountId: params.accountId });
103-
const spaceName = await resolveGoogleChatOutboundSpace({ account, target: params.target });
110+
const resolvedSpace = await resolveGoogleChatOutboundSpaceDetails({
111+
account,
112+
target: params.target,
113+
});
114+
const spaceName = resolvedSpace.name;
104115
if (!isGoogleChatSpaceTarget(spaceName)) {
105116
return null;
106117
}
107-
let space: GoogleChatSpace;
108-
try {
109-
space = await getGoogleChatSpace({ account, spaceName });
110-
} catch {
111-
// Space classification only enriches session routing. Delivery must remain
112-
// available when this auxiliary read is unavailable or lacks permission.
113-
return null;
118+
let chatType = resolvedSpace.resource
119+
? resolveGoogleChatSpaceChatType(resolvedSpace.resource)
120+
: undefined;
121+
if (!chatType) {
122+
try {
123+
chatType = resolveGoogleChatSpaceChatType(await getGoogleChatSpace({ account, spaceName }));
124+
} catch {
125+
// Space classification only enriches session routing. Delivery must remain
126+
// available when this auxiliary read is unavailable or lacks permission.
127+
return null;
128+
}
114129
}
115-
const chatType = resolveGoogleChatSpaceChatType(space);
116130
if (!chatType) {
117131
return null;
118132
}

0 commit comments

Comments
 (0)