WhatsApp: move action runtime into extension

This commit is contained in:
Gustavo Madeira Santana
2026-03-18 02:07:39 +00:00
parent b3ae50c71c
commit 8165db758b
6 changed files with 32 additions and 25 deletions

View File

@@ -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";

View File

@@ -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;

View File

@@ -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 () => {

View File

@@ -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<string, unknown>,
@@ -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,

View File

@@ -68,7 +68,7 @@ let webLoginQrPromise: Promise<
> | null = null;
let webChannelPromise: Promise<typeof import("../../channels/web/index.js")> | 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;
}

View File

@@ -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: {