Skip to content

Commit 765c0cd

Browse files
authored
refactor(googlechat): share approval card text escaping (#134643)
1 parent 50fe0de commit 765c0cd

5 files changed

Lines changed: 13 additions & 10 deletions

File tree

‎extensions/googlechat/src/approval-card-click.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function createApprovalResolveResult(params: {
3131
params.approvalKind === "exec"
3232
? {
3333
kind: "exec" as const,
34-
commandText: "echo hi",
34+
commandText: `<tag> & &amp; "double" 'single'`,
3535
commandPreview: null,
3636
allowedDecisions: ["allow-once" as const, "deny" as const],
3737
}
@@ -175,7 +175,11 @@ describe("maybeHandleGoogleChatApprovalCardClick", () => {
175175
cardsV2: expect.any(Array),
176176
});
177177
const cardJson = JSON.stringify(updateGoogleChatMessage.mock.calls[0]?.[0]);
178+
const cardText =
179+
updateGoogleChatMessage.mock.calls[0]?.[0].cardsV2[0].card.sections[0].widgets[0]
180+
.textParagraph.text;
178181
expect(cardJson).toContain("Exec Approval: Allowed once");
182+
expect(cardText).toBe(`&lt;tag&gt; &amp; &amp;amp; "double" 'single'`);
179183
expect(cardJson).toContain("Resolved by this action");
180184
expect(cardJson).not.toContain("buttonList");
181185
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function escapeGoogleChatApprovalCardText(text: string): string {
2+
return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
3+
}

‎extensions/googlechat/src/approval-handler.runtime.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function createPendingView(): ExecApprovalPendingView {
7070
agentId: "main",
7171
warningText: null,
7272
commandAnalysis: null,
73-
commandText: "echo hi",
73+
commandText: `<tag> & &amp; "double" 'single'`,
7474
commandPreview: null,
7575
cwd: "/tmp",
7676
envKeys: [],
@@ -233,6 +233,8 @@ describe("googleChatApprovalNativeRuntime", () => {
233233
});
234234

235235
expect(JSON.stringify(pendingPayload)).toContain("cardsV2");
236+
const commandText = getTextParagraphText(pendingPayload, "Command");
237+
expect(commandText).toBe(`&lt;tag&gt; &amp; &amp;amp; "double" 'single'`);
236238
expect(JSON.stringify(pendingPayload.cardsV2)).toContain(
237239
"https://chat-app.example.test/googlechat",
238240
);

‎extensions/googlechat/src/approval-handler.runtime.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
unregisterGoogleChatManualApprovalFollowupSuppression,
2323
unregisterGoogleChatApprovalCardBindings,
2424
} from "./approval-card-actions.js";
25+
import { escapeGoogleChatApprovalCardText as escapeGoogleChatText } from "./approval-card-text.js";
2526
import {
2627
isGoogleChatNativeApprovalClientEnabled,
2728
shouldHandleGoogleChatNativeApprovalRequest,
@@ -88,10 +89,6 @@ function resolveHandlerAccount(
8889
return account;
8990
}
9091

91-
function escapeGoogleChatText(text: string): string {
92-
return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
93-
}
94-
9592
function truncateText(text: string, maxChars = MAX_TEXT_PARAGRAPH_CHARS): string {
9693
return text.length <= maxChars ? text : `${truncateUtf16Safe(text, maxChars - 3)}...`;
9794
}

‎extensions/googlechat/src/approval-terminal-card.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import type { ApprovalResolveResult } from "openclaw/plugin-sdk/approval-gateway-runtime";
22
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
3+
import { escapeGoogleChatApprovalCardText as escapeGoogleChatText } from "./approval-card-text.js";
34
import type { GoogleChatCardV2 } from "./types.js";
45

56
const GOOGLECHAT_APPROVAL_CARD_ID = "openclaw-approval";
67
const MAX_TEXT_PARAGRAPH_CHARS = 1800;
78

8-
function escapeGoogleChatText(text: string): string {
9-
return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
10-
}
11-
129
function truncateText(text: string): string {
1310
return text.length <= MAX_TEXT_PARAGRAPH_CHARS
1411
? text

0 commit comments

Comments
 (0)