diff --git a/extensions/whatsapp/runtime-api.ts b/extensions/whatsapp/runtime-api.ts index 24e269ad62f..531cee4b524 100644 --- a/extensions/whatsapp/runtime-api.ts +++ b/extensions/whatsapp/runtime-api.ts @@ -1,4 +1,5 @@ export * from "./src/active-listener.js"; +export * from "./src/action-runtime.js"; export * from "./src/agent-tools-login.js"; export * from "./src/auth-store.js"; export * from "./src/auto-reply.js"; diff --git a/src/agents/tools/whatsapp-target-auth.ts b/extensions/whatsapp/src/action-runtime-target-auth.ts similarity index 70% rename from src/agents/tools/whatsapp-target-auth.ts rename to extensions/whatsapp/src/action-runtime-target-auth.ts index edc0052fbab..8686ac24261 100644 --- a/src/agents/tools/whatsapp-target-auth.ts +++ b/extensions/whatsapp/src/action-runtime-target-auth.ts @@ -1,7 +1,7 @@ -import type { OpenClawConfig } from "../../config/config.js"; -import { resolveWhatsAppAccount } from "../../plugin-sdk/whatsapp.js"; -import { resolveWhatsAppOutboundTarget } from "../../whatsapp/resolve-outbound-target.js"; -import { ToolAuthorizationError } from "./common.js"; +import { ToolAuthorizationError } from "../../../src/agents/tools/common.js"; +import type { OpenClawConfig } from "../../../src/config/config.js"; +import { resolveWhatsAppOutboundTarget } from "../../../src/whatsapp/resolve-outbound-target.js"; +import { resolveWhatsAppAccount } from "./accounts.js"; export function resolveAuthorizedWhatsAppOutboundTarget(params: { cfg: OpenClawConfig; diff --git a/src/agents/tools/whatsapp-actions.test.ts b/extensions/whatsapp/src/action-runtime.test.ts similarity index 88% rename from src/agents/tools/whatsapp-actions.test.ts rename to extensions/whatsapp/src/action-runtime.test.ts index 1fc195ffd1e..b8fb950281a 100644 --- a/src/agents/tools/whatsapp-actions.test.ts +++ b/extensions/whatsapp/src/action-runtime.test.ts @@ -1,17 +1,10 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; -import type { OpenClawConfig } from "../../config/config.js"; -import { DEFAULT_ACCOUNT_ID } from "../../routing/session-key.js"; -import { handleWhatsAppAction } from "./whatsapp-actions.js"; +import type { OpenClawConfig } from "../../../src/config/config.js"; +import { DEFAULT_ACCOUNT_ID } from "../../../src/routing/session-key.js"; +import { handleWhatsAppAction, whatsAppActionRuntime } from "./action-runtime.js"; -const { sendReactionWhatsApp, sendPollWhatsApp } = vi.hoisted(() => ({ - sendReactionWhatsApp: vi.fn(async () => undefined), - sendPollWhatsApp: vi.fn(async () => ({ messageId: "poll-1", toJid: "jid-1" })), -})); - -vi.mock("../../../extensions/whatsapp/src/send.js", () => ({ - sendReactionWhatsApp, - sendPollWhatsApp, -})); +const originalWhatsAppActionRuntime = { ...whatsAppActionRuntime }; +const sendReactionWhatsApp = vi.fn(async () => undefined); const enabledConfig = { channels: { whatsapp: { actions: { reactions: true } } }, @@ -20,6 +13,9 @@ const enabledConfig = { describe("handleWhatsAppAction", () => { beforeEach(() => { vi.clearAllMocks(); + Object.assign(whatsAppActionRuntime, originalWhatsAppActionRuntime, { + sendReactionWhatsApp, + }); }); it("adds reactions", async () => { diff --git a/src/agents/tools/whatsapp-actions.ts b/extensions/whatsapp/src/action-runtime.ts similarity index 72% rename from src/agents/tools/whatsapp-actions.ts rename to extensions/whatsapp/src/action-runtime.ts index a84dc0a3d5b..6a805440633 100644 --- a/src/agents/tools/whatsapp-actions.ts +++ b/extensions/whatsapp/src/action-runtime.ts @@ -1,8 +1,18 @@ import type { AgentToolResult } from "@mariozechner/pi-agent-core"; -import type { OpenClawConfig } from "../../config/config.js"; -import { sendReactionWhatsApp } from "../../plugin-sdk/whatsapp.js"; -import { createActionGate, jsonResult, readReactionParams, readStringParam } from "./common.js"; -import { resolveAuthorizedWhatsAppOutboundTarget } from "./whatsapp-target-auth.js"; +import { + createActionGate, + jsonResult, + readReactionParams, + readStringParam, +} from "../../../src/agents/tools/common.js"; +import type { OpenClawConfig } from "../../../src/config/config.js"; +import { resolveAuthorizedWhatsAppOutboundTarget } from "./action-runtime-target-auth.js"; +import { sendReactionWhatsApp } from "./send.js"; + +export const whatsAppActionRuntime = { + resolveAuthorizedWhatsAppOutboundTarget, + sendReactionWhatsApp, +}; export async function handleWhatsAppAction( params: Record, @@ -26,7 +36,7 @@ export async function handleWhatsAppAction( const fromMe = typeof fromMeRaw === "boolean" ? fromMeRaw : undefined; // Resolve account + allowFrom via shared account logic so auth and routing stay aligned. - const resolved = resolveAuthorizedWhatsAppOutboundTarget({ + const resolved = whatsAppActionRuntime.resolveAuthorizedWhatsAppOutboundTarget({ cfg, chatJid, accountId, @@ -34,7 +44,7 @@ export async function handleWhatsAppAction( }); const resolvedEmoji = remove ? "" : emoji; - await sendReactionWhatsApp(resolved.to, messageId, resolvedEmoji, { + await whatsAppActionRuntime.sendReactionWhatsApp(resolved.to, messageId, resolvedEmoji, { verbose: false, fromMe, participant: participant ?? undefined, diff --git a/src/plugins/runtime/runtime-whatsapp.ts b/src/plugins/runtime/runtime-whatsapp.ts index 5ca70688471..72bb3fd6af0 100644 --- a/src/plugins/runtime/runtime-whatsapp.ts +++ b/src/plugins/runtime/runtime-whatsapp.ts @@ -68,7 +68,7 @@ let webLoginQrPromise: Promise< > | null = null; let webChannelPromise: Promise | null = null; let whatsappActionsPromise: Promise< - typeof import("../../agents/tools/whatsapp-actions.js") + typeof import("../../../extensions/whatsapp/runtime-api.js") > | null = null; function loadWebLoginQr() { @@ -82,7 +82,7 @@ function loadWebChannel() { } function loadWhatsAppActions() { - whatsappActionsPromise ??= import("../../agents/tools/whatsapp-actions.js"); + whatsappActionsPromise ??= import("../../../extensions/whatsapp/runtime-api.js"); return whatsappActionsPromise; } diff --git a/src/plugins/runtime/types-channel.ts b/src/plugins/runtime/types-channel.ts index b3e46b35bb8..6b0a0e3a8f6 100644 --- a/src/plugins/runtime/types-channel.ts +++ b/src/plugins/runtime/types-channel.ts @@ -217,7 +217,7 @@ export type PluginRuntimeChannel = { startWebLoginWithQr: typeof import("../../../extensions/whatsapp/login-qr-api.js").startWebLoginWithQr; waitForWebLogin: typeof import("../../../extensions/whatsapp/login-qr-api.js").waitForWebLogin; monitorWebChannel: typeof import("../../channels/web/index.js").monitorWebChannel; - handleWhatsAppAction: typeof import("../../agents/tools/whatsapp-actions.js").handleWhatsAppAction; + handleWhatsAppAction: typeof import("../../../extensions/whatsapp/runtime-api.js").handleWhatsAppAction; createLoginTool: typeof import("./runtime-whatsapp-login-tool.js").createRuntimeWhatsAppLoginTool; }; line: {