|
| 1 | +// Covers reply-type plugin actions: outbound text hygiene (citation control |
| 2 | +// markers) and current-source delivery marking for implicit reply routes. |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import type { ChannelPlugin } from "../../channels/plugins/types.public.js"; |
| 5 | +import type { OpenClawConfig } from "../../config/config.js"; |
| 6 | +import { setActivePluginRegistry } from "../../plugins/runtime.js"; |
| 7 | +import { createTestRegistry } from "../../test-utils/channel-plugins.js"; |
| 8 | +import { runMessageAction } from "./message-action-runner.js"; |
| 9 | + |
| 10 | +type ChannelActionHandler = NonNullable<NonNullable<ChannelPlugin["actions"]>["handleAction"]>; |
| 11 | + |
| 12 | +const testchatConfig = { |
| 13 | + channels: { |
| 14 | + testchat: { |
| 15 | + enabled: true, |
| 16 | + }, |
| 17 | + }, |
| 18 | +} as OpenClawConfig; |
| 19 | + |
| 20 | +const CITATION_MARKED_MESSAGE = "Ayutthaya Thai is my pick. citeturn2search9turn2search6"; |
| 21 | + |
| 22 | +function createReplyActionPlugin(handleAction: ChannelActionHandler): ChannelPlugin { |
| 23 | + return { |
| 24 | + id: "testchat", |
| 25 | + meta: { |
| 26 | + id: "testchat", |
| 27 | + label: "Test Chat", |
| 28 | + selectionLabel: "Test Chat", |
| 29 | + docsPath: "/channels/testchat", |
| 30 | + blurb: "Reply action test plugin.", |
| 31 | + }, |
| 32 | + capabilities: { chatTypes: ["direct"] }, |
| 33 | + config: { |
| 34 | + listAccountIds: () => ["default"], |
| 35 | + resolveAccount: () => ({ enabled: true }), |
| 36 | + isConfigured: () => true, |
| 37 | + }, |
| 38 | + outbound: { |
| 39 | + deliveryMode: "direct", |
| 40 | + sendText: async () => ({ channel: "testchat", messageId: "m-send-1" }), |
| 41 | + }, |
| 42 | + messaging: { |
| 43 | + targetResolver: { |
| 44 | + looksLikeId: () => true, |
| 45 | + }, |
| 46 | + }, |
| 47 | + actions: { |
| 48 | + describeMessageTool: () => ({ actions: ["reply", "poll"] }), |
| 49 | + supportsAction: ({ action }) => action === "reply" || action === "poll", |
| 50 | + handleAction, |
| 51 | + }, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +function registerReplyPlugin() { |
| 56 | + const payload = { ok: true, messageId: "m-reply-1", repliedTo: "platform-guid-1" }; |
| 57 | + const handleAction = vi.fn(async () => ({ |
| 58 | + content: [{ type: "text" as const, text: JSON.stringify(payload) }], |
| 59 | + details: payload, |
| 60 | + })); |
| 61 | + setActivePluginRegistry( |
| 62 | + createTestRegistry([ |
| 63 | + { pluginId: "testchat", source: "test", plugin: createReplyActionPlugin(handleAction) }, |
| 64 | + ]), |
| 65 | + ); |
| 66 | + return handleAction; |
| 67 | +} |
| 68 | + |
| 69 | +function readHandledParams(handleAction: { |
| 70 | + mock: { calls: readonly unknown[][] }; |
| 71 | +}): Record<string, unknown> { |
| 72 | + const [call] = handleAction.mock.calls; |
| 73 | + const arg = call?.[0]; |
| 74 | + if (typeof arg !== "object" || arg === null || Array.isArray(arg)) { |
| 75 | + throw new Error("expected plugin handleAction call"); |
| 76 | + } |
| 77 | + const params = (arg as Record<string, unknown>).params; |
| 78 | + if (typeof params !== "object" || params === null || Array.isArray(params)) { |
| 79 | + throw new Error("expected plugin handleAction params"); |
| 80 | + } |
| 81 | + return params as Record<string, unknown>; |
| 82 | +} |
| 83 | + |
| 84 | +async function runReplyAction(params: { |
| 85 | + actionParams: Record<string, unknown>; |
| 86 | + currentMessageId?: string | number; |
| 87 | +}) { |
| 88 | + const toolContext = { |
| 89 | + currentChannelProvider: "testchat" as const, |
| 90 | + currentChannelId: "direct:user-1", |
| 91 | + ...(params.currentMessageId !== undefined ? { currentMessageId: params.currentMessageId } : {}), |
| 92 | + }; |
| 93 | + return await runMessageAction({ |
| 94 | + cfg: testchatConfig, |
| 95 | + action: "reply", |
| 96 | + params: { channel: "testchat", ...params.actionParams }, |
| 97 | + toolContext, |
| 98 | + messageActionAuthorization: { |
| 99 | + requesterAccountId: "default", |
| 100 | + toolContext, |
| 101 | + }, |
| 102 | + sessionKey: "agent:main:testchat:direct:user-1", |
| 103 | + defaultAccountId: "default", |
| 104 | + sourceReplyDeliveryMode: "message_tool_only", |
| 105 | + dryRun: false, |
| 106 | + }); |
| 107 | +} |
| 108 | + |
| 109 | +async function runPollAction(params: { to: string }) { |
| 110 | + const toolContext = { |
| 111 | + currentChannelProvider: "testchat" as const, |
| 112 | + currentChannelId: "direct:user-1", |
| 113 | + currentMessageId: "1783", |
| 114 | + }; |
| 115 | + return await runMessageAction({ |
| 116 | + cfg: testchatConfig, |
| 117 | + action: "poll", |
| 118 | + params: { |
| 119 | + channel: "testchat", |
| 120 | + to: params.to, |
| 121 | + pollQuestion: "Preferred default?", |
| 122 | + pollOption: ["Tell me right away", "Only important"], |
| 123 | + }, |
| 124 | + toolContext, |
| 125 | + messageActionAuthorization: { |
| 126 | + requesterAccountId: "default", |
| 127 | + toolContext, |
| 128 | + }, |
| 129 | + sessionKey: "agent:main:testchat:direct:user-1", |
| 130 | + defaultAccountId: "default", |
| 131 | + sourceReplyDeliveryMode: "message_tool_only", |
| 132 | + dryRun: false, |
| 133 | + }); |
| 134 | +} |
| 135 | + |
| 136 | +describe("runMessageAction reply-type plugin actions", () => { |
| 137 | + afterEach(() => { |
| 138 | + setActivePluginRegistry(createTestRegistry([])); |
| 139 | + }); |
| 140 | + |
| 141 | + it("strips citation control markers from reply text before plugin dispatch", async () => { |
| 142 | + const handleAction = registerReplyPlugin(); |
| 143 | + |
| 144 | + await runReplyAction({ |
| 145 | + actionParams: { message: CITATION_MARKED_MESSAGE, messageId: "1783" }, |
| 146 | + currentMessageId: "1783", |
| 147 | + }); |
| 148 | + |
| 149 | + const handledParams = readHandledParams(handleAction); |
| 150 | + expect(handledParams.message).toBe("Ayutthaya Thai is my pick."); |
| 151 | + }); |
| 152 | + |
| 153 | + it("marks replies to the run's inbound message as current-source deliveries", async () => { |
| 154 | + registerReplyPlugin(); |
| 155 | + |
| 156 | + const result = await runReplyAction({ |
| 157 | + actionParams: { message: "visible reply", messageId: "1783" }, |
| 158 | + currentMessageId: "1783", |
| 159 | + }); |
| 160 | + |
| 161 | + expect(result.kind).toBe("action"); |
| 162 | + expect(result.payload).toMatchObject({ sourceReplyRoute: "current-source" }); |
| 163 | + const details = "toolResult" in result ? result.toolResult?.details : undefined; |
| 164 | + expect(details).toMatchObject({ sourceReplyRoute: "current-source" }); |
| 165 | + }); |
| 166 | + |
| 167 | + it("matches numeric replied-to message ids against string tool-context ids", async () => { |
| 168 | + registerReplyPlugin(); |
| 169 | + |
| 170 | + const result = await runReplyAction({ |
| 171 | + actionParams: { message: "visible reply", messageId: 1783 }, |
| 172 | + currentMessageId: "1783", |
| 173 | + }); |
| 174 | + |
| 175 | + expect(result.payload).toMatchObject({ sourceReplyRoute: "current-source" }); |
| 176 | + }); |
| 177 | + |
| 178 | + it("leaves replies to other messages unmarked", async () => { |
| 179 | + registerReplyPlugin(); |
| 180 | + |
| 181 | + const result = await runReplyAction({ |
| 182 | + actionParams: { message: "visible reply", messageId: "999" }, |
| 183 | + currentMessageId: "1783", |
| 184 | + }); |
| 185 | + |
| 186 | + expect((result.payload as { sourceReplyRoute?: unknown }).sourceReplyRoute).toBeUndefined(); |
| 187 | + }); |
| 188 | + |
| 189 | + it("leaves explicitly targeted replies unmarked", async () => { |
| 190 | + registerReplyPlugin(); |
| 191 | + |
| 192 | + const result = await runReplyAction({ |
| 193 | + actionParams: { |
| 194 | + message: "visible reply", |
| 195 | + messageId: "1783", |
| 196 | + to: "direct:someone-else", |
| 197 | + }, |
| 198 | + currentMessageId: "1783", |
| 199 | + }); |
| 200 | + |
| 201 | + expect((result.payload as { sourceReplyRoute?: unknown }).sourceReplyRoute).toBeUndefined(); |
| 202 | + }); |
| 203 | + |
| 204 | + it("marks polls sent to the current conversation as current-source deliveries", async () => { |
| 205 | + registerReplyPlugin(); |
| 206 | + |
| 207 | + const result = await runPollAction({ to: "direct:user-1" }); |
| 208 | + |
| 209 | + expect(result.kind).toBe("poll"); |
| 210 | + expect(result.payload).toMatchObject({ sourceReplyRoute: "current-source" }); |
| 211 | + }); |
| 212 | + |
| 213 | + it("leaves polls sent to other conversations unmarked", async () => { |
| 214 | + registerReplyPlugin(); |
| 215 | + |
| 216 | + const result = await runPollAction({ to: "direct:someone-else" }); |
| 217 | + |
| 218 | + expect((result.payload as { sourceReplyRoute?: unknown }).sourceReplyRoute).toBeUndefined(); |
| 219 | + }); |
| 220 | +}); |
0 commit comments