refactor: migrate messaging plugins to sdk

This commit is contained in:
Peter Steinberger
2026-01-18 08:32:19 +00:00
parent 9241e21114
commit c5e19f5c67
63 changed files with 4082 additions and 376 deletions

View File

@@ -1,15 +1,28 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import * as replyModule from "../auto-reply/reply.js";
import type { ClawdbotConfig } from "../config/config.js";
import { resolveMainSessionKey } from "../config/sessions.js";
import { runHeartbeatOnce } from "./heartbeat-runner.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js";
import { telegramPlugin } from "../../extensions/telegram/src/channel.js";
import { whatsappPlugin } from "../../extensions/whatsapp/src/channel.js";
// Avoid pulling optional runtime deps during isolated runs.
vi.mock("jiti", () => ({ createJiti: () => () => ({}) }));
beforeEach(() => {
setActivePluginRegistry(
createTestRegistry([
{ pluginId: "whatsapp", plugin: whatsappPlugin, source: "test" },
{ pluginId: "telegram", plugin: telegramPlugin, source: "test" },
]),
);
});
describe("resolveHeartbeatIntervalMs", () => {
it("respects ackMaxChars for heartbeat acks", async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-hb-"));

View File

@@ -1,7 +1,7 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { HEARTBEAT_PROMPT } from "../auto-reply/heartbeat.js";
import * as replyModule from "../auto-reply/reply.js";
import type { ClawdbotConfig } from "../config/config.js";
@@ -18,10 +18,23 @@ import {
runHeartbeatOnce,
} from "./heartbeat-runner.js";
import { resolveHeartbeatDeliveryTarget } from "./outbound/targets.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js";
import { telegramPlugin } from "../../extensions/telegram/src/channel.js";
import { whatsappPlugin } from "../../extensions/whatsapp/src/channel.js";
// Avoid pulling optional runtime deps during isolated runs.
vi.mock("jiti", () => ({ createJiti: () => () => ({}) }));
beforeEach(() => {
setActivePluginRegistry(
createTestRegistry([
{ pluginId: "whatsapp", plugin: whatsappPlugin, source: "test" },
{ pluginId: "telegram", plugin: telegramPlugin, source: "test" },
]),
);
});
describe("resolveHeartbeatIntervalMs", () => {
it("returns default when unset", () => {
expect(resolveHeartbeatIntervalMs({})).toBe(30 * 60_000);

View File

@@ -1,7 +1,16 @@
import { describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { ClawdbotConfig } from "../../config/config.js";
import { signalOutbound } from "../../channels/plugins/outbound/signal.js";
import { telegramOutbound } from "../../channels/plugins/outbound/telegram.js";
import { whatsappOutbound } from "../../channels/plugins/outbound/whatsapp.js";
import { markdownToSignalTextChunks } from "../../signal/format.js";
import { setActivePluginRegistry } from "../../plugins/runtime.js";
import {
createIMessageTestPlugin,
createOutboundTestPlugin,
createTestRegistry,
} from "../../test-utils/channel-plugins.js";
const mocks = vi.hoisted(() => ({
appendAssistantMessageToSessionTranscript: vi.fn(async () => ({ ok: true, sessionFile: "x" })),
@@ -20,6 +29,13 @@ vi.mock("../../config/sessions.js", async () => {
const { deliverOutboundPayloads, normalizeOutboundPayloads } = await import("./deliver.js");
describe("deliverOutboundPayloads", () => {
beforeEach(() => {
setActivePluginRegistry(defaultRegistry);
});
afterEach(() => {
setActivePluginRegistry(emptyRegistry);
});
it("chunks telegram markdown and passes through accountId", async () => {
const sendTelegram = vi.fn().mockResolvedValue({ messageId: "m1", chatId: "c1" });
const cfg: ClawdbotConfig = {
@@ -154,6 +170,15 @@ describe("deliverOutboundPayloads", () => {
it("uses iMessage media maxBytes from agent fallback", async () => {
const sendIMessage = vi.fn().mockResolvedValue({ messageId: "i1" });
setActivePluginRegistry(
createTestRegistry([
{
pluginId: "imessage",
source: "test",
plugin: createIMessageTestPlugin(),
},
]),
);
const cfg: ClawdbotConfig = {
agents: { defaults: { mediaMaxMb: 3 } },
};
@@ -234,3 +259,27 @@ describe("deliverOutboundPayloads", () => {
);
});
});
const emptyRegistry = createTestRegistry([]);
const defaultRegistry = createTestRegistry([
{
pluginId: "telegram",
plugin: createOutboundTestPlugin({ id: "telegram", outbound: telegramOutbound }),
source: "test",
},
{
pluginId: "signal",
plugin: createOutboundTestPlugin({ id: "signal", outbound: signalOutbound }),
source: "test",
},
{
pluginId: "whatsapp",
plugin: createOutboundTestPlugin({ id: "whatsapp", outbound: whatsappOutbound }),
source: "test",
},
{
pluginId: "imessage",
plugin: createIMessageTestPlugin(),
source: "test",
},
]);

View File

@@ -1,4 +1,5 @@
import { getChannelPlugin } from "../../channels/plugins/index.js";
import { getChatChannelMeta, normalizeChatChannelId } from "../../channels/registry.js";
import type { ChannelId } from "../../channels/plugins/types.js";
import type { OutboundDeliveryResult } from "./deliver.js";
@@ -28,8 +29,13 @@ type OutboundDeliveryMeta = {
meta?: Record<string, unknown>;
};
const resolveChannelLabel = (channel: string) =>
getChannelPlugin(channel as ChannelId)?.meta.label ?? channel;
const resolveChannelLabel = (channel: string) => {
const pluginLabel = getChannelPlugin(channel as ChannelId)?.meta.label;
if (pluginLabel) return pluginLabel;
const normalized = normalizeChatChannelId(channel);
if (normalized) return getChatChannelMeta(normalized).label;
return channel;
};
export function formatOutboundDeliverySummary(
channel: string,

View File

@@ -1,6 +1,11 @@
import { describe, expect, it } from "vitest";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { ClawdbotConfig } from "../../config/config.js";
import { setActivePluginRegistry } from "../../plugins/runtime.js";
import { createIMessageTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
import { slackPlugin } from "../../../extensions/slack/src/channel.js";
import { telegramPlugin } from "../../../extensions/telegram/src/channel.js";
import { whatsappPlugin } from "../../../extensions/whatsapp/src/channel.js";
import { runMessageAction } from "./message-action-runner.js";
const slackConfig = {
@@ -21,6 +26,36 @@ const whatsappConfig = {
} as ClawdbotConfig;
describe("runMessageAction context isolation", () => {
beforeEach(() => {
setActivePluginRegistry(
createTestRegistry([
{
pluginId: "slack",
source: "test",
plugin: slackPlugin,
},
{
pluginId: "whatsapp",
source: "test",
plugin: whatsappPlugin,
},
{
pluginId: "telegram",
source: "test",
plugin: telegramPlugin,
},
{
pluginId: "imessage",
source: "test",
plugin: createIMessageTestPlugin(),
},
]),
);
});
afterEach(() => {
setActivePluginRegistry(createTestRegistry([]));
});
it("allows send when target matches current channel", async () => {
const result = await runMessageAction({
cfg: slackConfig,

View File

@@ -1,9 +1,13 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { ChannelOutboundAdapter, ChannelPlugin } from "../../channels/plugins/types.js";
import type { PluginRegistry } from "../../plugins/registry.js";
import { setActivePluginRegistry } from "../../plugins/runtime.js";
import { sendMessage, sendPoll } from "./message.js";
import { createIMessageTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
const loadMessage = async () => await import("./message.js");
const setRegistry = async (registry: ReturnType<typeof createTestRegistry>) => {
const { setActivePluginRegistry } = await import("../../plugins/runtime.js");
setActivePluginRegistry(registry);
};
const callGatewayMock = vi.fn();
vi.mock("../../gateway/call.js", () => ({
@@ -12,22 +16,24 @@ vi.mock("../../gateway/call.js", () => ({
}));
describe("sendMessage channel normalization", () => {
beforeEach(() => {
beforeEach(async () => {
callGatewayMock.mockReset();
setActivePluginRegistry(emptyRegistry);
vi.resetModules();
await setRegistry(emptyRegistry);
});
afterEach(() => {
setActivePluginRegistry(emptyRegistry);
afterEach(async () => {
await setRegistry(emptyRegistry);
});
it("normalizes Teams alias", async () => {
const { sendMessage } = await loadMessage();
const sendMSTeams = vi.fn(async () => ({
messageId: "m1",
conversationId: "c1",
}));
setActivePluginRegistry(
createRegistry([
await setRegistry(
createTestRegistry([
{
pluginId: "msteams",
source: "test",
@@ -51,7 +57,17 @@ describe("sendMessage channel normalization", () => {
});
it("normalizes iMessage alias", async () => {
const { sendMessage } = await loadMessage();
const sendIMessage = vi.fn(async () => ({ messageId: "i1" }));
await setRegistry(
createTestRegistry([
{
pluginId: "imessage",
source: "test",
plugin: createIMessageTestPlugin(),
},
]),
);
const result = await sendMessage({
cfg: {},
to: "someone@example.com",
@@ -66,19 +82,21 @@ describe("sendMessage channel normalization", () => {
});
describe("sendPoll channel normalization", () => {
beforeEach(() => {
beforeEach(async () => {
callGatewayMock.mockReset();
setActivePluginRegistry(emptyRegistry);
vi.resetModules();
await setRegistry(emptyRegistry);
});
afterEach(() => {
setActivePluginRegistry(emptyRegistry);
afterEach(async () => {
await setRegistry(emptyRegistry);
});
it("normalizes Teams alias for polls", async () => {
const { sendPoll } = await loadMessage();
callGatewayMock.mockResolvedValueOnce({ messageId: "p1" });
setActivePluginRegistry(
createRegistry([
await setRegistry(
createTestRegistry([
{
pluginId: "msteams",
source: "test",
@@ -106,19 +124,7 @@ describe("sendPoll channel normalization", () => {
});
});
const createRegistry = (channels: PluginRegistry["channels"]): PluginRegistry => ({
plugins: [],
tools: [],
channels,
providers: [],
gatewayHandlers: {},
httpHandlers: [],
cliRegistrars: [],
services: [],
diagnostics: [],
});
const emptyRegistry = createRegistry([]);
const emptyRegistry = createTestRegistry([]);
const createMSTeamsOutbound = (opts?: { includePoll?: boolean }): ChannelOutboundAdapter => ({
deliveryMode: "direct",

View File

@@ -1,9 +1,22 @@
import { describe, expect, it } from "vitest";
import { beforeEach, describe, expect, it } from "vitest";
import type { ClawdbotConfig } from "../../config/config.js";
import { setActivePluginRegistry } from "../../plugins/runtime.js";
import { createTestRegistry } from "../../test-utils/channel-plugins.js";
import { telegramPlugin } from "../../../extensions/telegram/src/channel.js";
import { whatsappPlugin } from "../../../extensions/whatsapp/src/channel.js";
import { resolveOutboundTarget, resolveSessionDeliveryTarget } from "./targets.js";
describe("resolveOutboundTarget", () => {
beforeEach(() => {
setActivePluginRegistry(
createTestRegistry([
{ pluginId: "whatsapp", plugin: whatsappPlugin, source: "test" },
{ pluginId: "telegram", plugin: telegramPlugin, source: "test" },
]),
);
});
it("falls back to whatsapp allowFrom via config", () => {
const cfg: ClawdbotConfig = {
channels: { whatsapp: { allowFrom: ["+1555"] } },