Skip to content

Commit 71cf8fe

Browse files
masatohoshinosteipete
authored andcommitted
fix(clickclack): sanitize outbound assistant text
1 parent 6e0d590 commit 71cf8fe

3 files changed

Lines changed: 51 additions & 26 deletions

File tree

‎extensions/clickclack/src/channel.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const clickClackMessageAdapter = defineChannelMessageAdapter({
4949
},
5050
send: {
5151
text: async (ctx) => {
52-
const result = await sendClickClackText({
52+
const messageId = await sendClickClackText({
5353
cfg: ctx.cfg as CoreConfig,
5454
accountId: ctx.accountId,
5555
to: ctx.to,
@@ -60,9 +60,9 @@ const clickClackMessageAdapter = defineChannelMessageAdapter({
6060
const threadId = ctx.threadId == null ? undefined : String(ctx.threadId);
6161
const replyToId = ctx.replyToId ?? undefined;
6262
return {
63-
messageId: result.messageId,
63+
...(messageId ? { messageId } : {}),
6464
receipt: createMessageReceiptFromOutboundResults({
65-
results: [{ channel: CHANNEL_ID, messageId: result.messageId }],
65+
results: messageId ? [{ channel: CHANNEL_ID, messageId }] : [],
6666
threadId,
6767
replyToId,
6868
kind: "text",
@@ -177,15 +177,18 @@ export const clickClackPlugin: ChannelPlugin<ResolvedClickClackAccount> = create
177177
},
178178
attachedResults: {
179179
channel: CHANNEL_ID,
180-
sendText: async ({ cfg, to, text, accountId, threadId, replyToId }) =>
181-
await sendClickClackText({
180+
sendText: async ({ cfg, to, text, accountId, threadId, replyToId }) => {
181+
const messageId = await sendClickClackText({
182182
cfg: cfg as CoreConfig,
183183
accountId,
184184
to,
185185
text,
186186
threadId,
187187
replyToId,
188-
}),
188+
});
189+
// Legacy outbound results use an empty id to report an intentional no-send.
190+
return { messageId: messageId ?? "" };
191+
},
189192
},
190193
},
191194
});

‎extensions/clickclack/src/outbound.test.ts‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// Covers outbound delivery routing: a reply to a top-level message must post to
2-
// the main channel as a quote-reply, not open a per-reply thread.
1+
// Covers ClickClack outbound routing and sender-boundary assistant text sanitization.
32
import { beforeEach, describe, expect, it, vi } from "vitest";
43
import { sendClickClackText } from "./outbound.js";
54
import type { CoreConfig } from "./types.js";
@@ -46,18 +45,18 @@ describe("sendClickClackText routing", () => {
4645
createClientOptions.mockClear();
4746
});
4847

49-
it("delivers a reply to a top-level channel message as an in-channel quote-reply", async () => {
48+
it("sanitizes a top-level channel quote-reply", async () => {
5049
await sendClickClackText({
5150
cfg,
5251
to: "channel:general",
53-
text: "hi",
52+
text: "Done.\n⚠️ 🛠️ `search repos (agent)` failed",
5453
replyToId: "msg_root",
5554
});
5655

5756
expect(createChannelMessage).toHaveBeenCalledTimes(1);
5857
expect(createChannelMessage).toHaveBeenCalledWith(
5958
"general",
60-
"hi",
59+
"Done.",
6160
expect.objectContaining({ quotedMessageId: "msg_root" }),
6261
);
6362
expect(createThreadReply).not.toHaveBeenCalled();
@@ -89,16 +88,16 @@ describe("sendClickClackText routing", () => {
8988
});
9089
});
9190

92-
it("keeps replies inside a genuine thread (explicit threadId)", async () => {
91+
it("sanitizes replies inside a genuine thread", async () => {
9392
await sendClickClackText({
9493
cfg,
9594
to: "channel:general",
96-
text: "hi",
95+
text: "Done.\n⚠️ 🛠️ `search repos (agent)` failed",
9796
threadId: "msg_thread_root",
9897
replyToId: "msg_root",
9998
});
10099

101-
expect(createThreadReply).toHaveBeenCalledWith("msg_thread_root", "hi", expect.anything());
100+
expect(createThreadReply).toHaveBeenCalledWith("msg_thread_root", "Done.", expect.anything());
102101
expect(createChannelMessage).not.toHaveBeenCalled();
103102
});
104103

@@ -109,19 +108,35 @@ describe("sendClickClackText routing", () => {
109108
expect(createChannelMessage).not.toHaveBeenCalled();
110109
});
111110

112-
it("delivers a DM reply as a quote-reply in the same conversation", async () => {
111+
it("sanitizes leaked tool XML in a DM quote-reply", async () => {
113112
await sendClickClackText({
114113
cfg,
115114
to: "dm:usr_1",
116-
text: "hi",
115+
text: '<tool_call>{"name":"exec"}</tool_call>Deploy finished.',
117116
replyToId: "msg_root",
118117
});
119118

120119
expect(createDirectMessage).toHaveBeenCalledWith(
121120
"dm_1",
122-
"hi",
121+
"Deploy finished.",
123122
expect.objectContaining({ quotedMessageId: "msg_root" }),
124123
);
125124
expect(createThreadReply).not.toHaveBeenCalled();
126125
});
126+
127+
it("suppresses replies containing only internal scaffolding", async () => {
128+
await expect(
129+
sendClickClackText({
130+
cfg,
131+
to: "channel:general",
132+
text: "⚠️ 🛠️ `search repos (agent)` failed",
133+
}),
134+
).resolves.toBeUndefined();
135+
136+
expect(createClientOptions).not.toHaveBeenCalled();
137+
expect(createChannelMessage).not.toHaveBeenCalled();
138+
expect(createThreadReply).not.toHaveBeenCalled();
139+
expect(createDirectConversation).not.toHaveBeenCalled();
140+
expect(createDirectMessage).not.toHaveBeenCalled();
141+
});
127142
});

‎extensions/clickclack/src/outbound.ts‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
* Outbound ClickClack delivery helpers for channel messages, thread replies,
33
* and direct messages.
44
*/
5+
import { sanitizeAssistantVisibleText } from "openclaw/plugin-sdk/text-chunking";
56
import { resolveClickClackAccount } from "./accounts.js";
67
import { createClickClackClient } from "./http-client.js";
78
import { resolveChannelId, resolveWorkspaceId } from "./resolve.js";
89
import { parseClickClackTarget } from "./target.js";
910
import type { ClickClackMessageProvenance, CoreConfig } from "./types.js";
1011

1112
/**
12-
* Sends text to a normalized ClickClack target and returns the created message
13-
* id for receipt/session tracking.
13+
* Sends visible text to a normalized ClickClack target and returns the created
14+
* message id, or undefined when sanitization removes all content.
1415
*/
1516
export async function sendClickClackText(params: {
1617
cfg: CoreConfig;
@@ -23,7 +24,13 @@ export async function sendClickClackText(params: {
2324
correlationId?: string;
2425
/** Optional model/thinking attribution stamped onto the created message. */
2526
provenance?: ClickClackMessageProvenance;
26-
}) {
27+
}): Promise<string | undefined> {
28+
// Custom inbound replies bypass shared outbound normalization, so this private
29+
// sender owns ClickClack assistant-text sanitization for every delivery path.
30+
const text = sanitizeAssistantVisibleText(params.text);
31+
if (!text) {
32+
return undefined;
33+
}
2734
const account = resolveClickClackAccount({ cfg: params.cfg, accountId: params.accountId });
2835
const client = createClickClackClient({
2936
baseUrl: account.baseUrl,
@@ -41,25 +48,25 @@ export async function sendClickClackText(params: {
4148
// below — otherwise every channel reply spawns its own thread and the main
4249
// timeline goes silent.
4350
const rootId = explicitThreadId || parsed.id;
44-
const message = await client.createThreadReply(rootId, params.text, {
51+
const message = await client.createThreadReply(rootId, text, {
4552
provenance: params.provenance,
4653
});
47-
return { to: params.to, messageId: message.id };
54+
return message.id;
4855
}
4956
if (parsed.kind === "dm") {
5057
const dm = await client.createDirectConversation(workspaceId, [parsed.id]);
51-
const message = await client.createDirectMessage(dm.id, params.text, {
58+
const message = await client.createDirectMessage(dm.id, text, {
5259
quotedMessageId: replyToId || undefined,
5360
});
54-
return { to: params.to, messageId: message.id };
61+
return message.id;
5562
}
5663
const channelId = await resolveChannelId(client, workspaceId, parsed.id);
5764
// A reply to a top-level channel message is delivered to the main channel as a
5865
// quote-reply (quoted_message_id), matching the reply-to affordance of the
5966
// Discord/Slack/Telegram channels, instead of opening a per-reply thread.
60-
const message = await client.createChannelMessage(channelId, params.text, {
67+
const message = await client.createChannelMessage(channelId, text, {
6168
provenance: params.provenance,
6269
quotedMessageId: replyToId || undefined,
6370
});
64-
return { to: params.to, messageId: message.id };
71+
return message.id;
6572
}

0 commit comments

Comments
 (0)