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,97 +1,101 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { beforeAll, describe, expect, it, vi } from "vitest";
import type { ClawdbotConfig } from "../config/config.js";
import { ensureClawdbotModelsJson } from "./models-config.js";
vi.mock("@mariozechner/pi-ai", async () => {
const actual = await vi.importActual<typeof import("@mariozechner/pi-ai")>("@mariozechner/pi-ai");
const buildAssistantMessage = (model: { api: string; provider: string; id: string }) => ({
role: "assistant" as const,
content: [{ type: "text" as const, text: "ok" }],
stopReason: "stop" as const,
api: model.api,
provider: model.provider,
model: model.id,
usage: {
input: 1,
output: 1,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 2,
cost: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
total: 0,
},
},
timestamp: Date.now(),
});
const buildAssistantErrorMessage = (model: { api: string; provider: string; id: string }) => ({
role: "assistant" as const,
content: [] as const,
stopReason: "error" as const,
errorMessage: "boom",
api: model.api,
provider: model.provider,
model: model.id,
usage: {
const buildAssistantMessage = (model: { api: string; provider: string; id: string }) => ({
role: "assistant" as const,
content: [{ type: "text" as const, text: "ok" }],
stopReason: "stop" as const,
api: model.api,
provider: model.provider,
model: model.id,
usage: {
input: 1,
output: 1,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 2,
cost: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
cost: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
total: 0,
},
total: 0,
},
timestamp: Date.now(),
});
return {
...actual,
complete: async (model: { api: string; provider: string; id: string }) => {
if (model.id === "mock-error") return buildAssistantErrorMessage(model);
return buildAssistantMessage(model);
},
completeSimple: async (model: { api: string; provider: string; id: string }) => {
if (model.id === "mock-error") return buildAssistantErrorMessage(model);
return buildAssistantMessage(model);
},
streamSimple: (model: { api: string; provider: string; id: string }) => {
const stream = new actual.AssistantMessageEventStream();
queueMicrotask(() => {
stream.push({
type: "done",
reason: "stop",
message:
model.id === "mock-error"
? buildAssistantErrorMessage(model)
: buildAssistantMessage(model),
});
stream.end();
});
return stream;
},
};
},
timestamp: Date.now(),
});
const buildAssistantErrorMessage = (model: { api: string; provider: string; id: string }) => ({
role: "assistant" as const,
content: [] as const,
stopReason: "error" as const,
errorMessage: "boom",
api: model.api,
provider: model.provider,
model: model.id,
usage: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
cost: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
total: 0,
},
},
timestamp: Date.now(),
});
const mockPiAi = () => {
vi.doMock("@mariozechner/pi-ai", async () => {
const actual = await vi.importActual<typeof import("@mariozechner/pi-ai")>(
"@mariozechner/pi-ai",
);
return {
...actual,
complete: async (model: { api: string; provider: string; id: string }) => {
if (model.id === "mock-error") return buildAssistantErrorMessage(model);
return buildAssistantMessage(model);
},
completeSimple: async (model: { api: string; provider: string; id: string }) => {
if (model.id === "mock-error") return buildAssistantErrorMessage(model);
return buildAssistantMessage(model);
},
streamSimple: (model: { api: string; provider: string; id: string }) => {
const stream = new actual.AssistantMessageEventStream();
queueMicrotask(() => {
stream.push({
type: "done",
reason: "stop",
message:
model.id === "mock-error"
? buildAssistantErrorMessage(model)
: buildAssistantMessage(model),
});
stream.end();
});
return stream;
},
};
});
};
let runEmbeddedPiAgent: typeof import("./pi-embedded-runner.js").runEmbeddedPiAgent;
beforeEach(async () => {
beforeAll(async () => {
vi.useRealTimers();
vi.resetModules();
mockPiAi();
({ runEmbeddedPiAgent } = await import("./pi-embedded-runner.js"));
});
}, 20_000);
const makeOpenAiConfig = (modelIds: string[]) =>
({

View File

@@ -1,8 +1,17 @@
import { describe, expect, it } from "vitest";
import { beforeEach, describe, expect, it } from "vitest";
import { extractMessagingToolSend } from "./pi-embedded-subscribe.tools.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js";
import { telegramPlugin } from "../../extensions/telegram/src/channel.js";
describe("extractMessagingToolSend", () => {
beforeEach(() => {
setActivePluginRegistry(
createTestRegistry([{ pluginId: "telegram", plugin: telegramPlugin, source: "test" }]),
);
});
it("uses channel as provider for message tool", () => {
const result = extractMessagingToolSend("message", {
action: "send",

View File

@@ -1,18 +1,51 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createTestRegistry } from "../../test-utils/channel-plugins.js";
const callGatewayMock = vi.fn();
vi.mock("../../gateway/call.js", () => ({
callGateway: (opts: unknown) => callGatewayMock(opts),
}));
import { resolveAnnounceTarget } from "./sessions-announce-target.js";
const loadResolveAnnounceTarget = async () => await import("./sessions-announce-target.js");
const installRegistry = async () => {
const { setActivePluginRegistry } = await import("../../plugins/runtime.js");
setActivePluginRegistry(
createTestRegistry([
{
pluginId: "whatsapp",
source: "test",
plugin: {
id: "whatsapp",
meta: {
id: "whatsapp",
label: "WhatsApp",
selectionLabel: "WhatsApp",
docsPath: "/channels/whatsapp",
blurb: "WhatsApp test stub.",
preferSessionLookupForAnnounceTarget: true,
},
capabilities: { chatTypes: ["direct", "group"] },
config: {
listAccountIds: () => ["default"],
resolveAccount: () => ({}),
},
},
},
]),
);
};
describe("resolveAnnounceTarget", () => {
beforeEach(() => {
beforeEach(async () => {
callGatewayMock.mockReset();
vi.resetModules();
await installRegistry();
});
it("derives non-WhatsApp announce targets from the session key", async () => {
const { resolveAnnounceTarget } = await loadResolveAnnounceTarget();
const target = await resolveAnnounceTarget({
sessionKey: "agent:main:discord:group:dev",
displayKey: "agent:main:discord:group:dev",
@@ -22,6 +55,7 @@ describe("resolveAnnounceTarget", () => {
});
it("hydrates WhatsApp accountId from sessions.list when available", async () => {
const { resolveAnnounceTarget } = await loadResolveAnnounceTarget();
callGatewayMock.mockResolvedValueOnce({
sessions: [
{

View File

@@ -1,3 +1,4 @@
import { mkdir } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { withTempHome as withTempHomeBase } from "../../test/helpers/temp-home.js";
@@ -63,6 +64,7 @@ vi.mock("../web/session.js", () => webMocks);
async function withTempHome<T>(fn: (home: string) => Promise<T>): Promise<T> {
return withTempHomeBase(
async (home) => {
await mkdir(join(home, ".clawdbot", "agents", "main", "sessions"), { recursive: true });
vi.mocked(runEmbeddedPiAgent).mockClear();
vi.mocked(abortEmbeddedPiRun).mockClear();
return await fn(home);

View File

@@ -4,6 +4,17 @@ import type { ChannelOutboundAdapter, ChannelPlugin } from "../../channels/plugi
import type { ClawdbotConfig } from "../../config/config.js";
import type { PluginRegistry } from "../../plugins/registry.js";
import { setActivePluginRegistry } from "../../plugins/runtime.js";
import {
createIMessageTestPlugin,
createOutboundTestPlugin,
createTestRegistry,
} from "../../test-utils/channel-plugins.js";
import { discordOutbound } from "../../channels/plugins/outbound/discord.js";
import { imessageOutbound } from "../../channels/plugins/outbound/imessage.js";
import { signalOutbound } from "../../channels/plugins/outbound/signal.js";
import { slackOutbound } from "../../channels/plugins/outbound/slack.js";
import { telegramOutbound } from "../../channels/plugins/outbound/telegram.js";
import { whatsappOutbound } from "../../channels/plugins/outbound/whatsapp.js";
import { SILENT_REPLY_TOKEN } from "../tokens.js";
const mocks = vi.hoisted(() => ({
@@ -53,9 +64,50 @@ const actualDeliver = await vi.importActual<typeof import("../../infra/outbound/
const { routeReply } = await import("./route-reply.js");
const createRegistry = (channels: PluginRegistry["channels"]): PluginRegistry => ({
plugins: [],
tools: [],
channels,
providers: [],
gatewayHandlers: {},
httpHandlers: [],
cliRegistrars: [],
services: [],
diagnostics: [],
});
const createMSTeamsOutbound = (): ChannelOutboundAdapter => ({
deliveryMode: "direct",
sendText: async ({ cfg, to, text }) => {
const result = await mocks.sendMessageMSTeams({ cfg, to, text });
return { channel: "msteams", ...result };
},
sendMedia: async ({ cfg, to, text, mediaUrl }) => {
const result = await mocks.sendMessageMSTeams({ cfg, to, text, mediaUrl });
return { channel: "msteams", ...result };
},
});
const createMSTeamsPlugin = (params: { outbound: ChannelOutboundAdapter }): ChannelPlugin => ({
id: "msteams",
meta: {
id: "msteams",
label: "Microsoft Teams",
selectionLabel: "Microsoft Teams (Bot Framework)",
docsPath: "/channels/msteams",
blurb: "Bot Framework; enterprise support.",
},
capabilities: { chatTypes: ["direct"] },
config: {
listAccountIds: () => [],
resolveAccount: () => ({}),
},
outbound: params.outbound,
});
describe("routeReply", () => {
beforeEach(() => {
setActivePluginRegistry(emptyRegistry);
setActivePluginRegistry(defaultRegistry);
mocks.deliverOutboundPayloads.mockImplementation(actualDeliver.deliverOutboundPayloads);
});
@@ -296,45 +348,51 @@ describe("routeReply", () => {
});
});
const createRegistry = (channels: PluginRegistry["channels"]): PluginRegistry => ({
plugins: [],
tools: [],
channels,
providers: [],
gatewayHandlers: {},
httpHandlers: [],
cliRegistrars: [],
services: [],
diagnostics: [],
});
const emptyRegistry = createRegistry([]);
const createMSTeamsOutbound = (): ChannelOutboundAdapter => ({
deliveryMode: "direct",
sendText: async ({ cfg, to, text }) => {
const result = await mocks.sendMessageMSTeams({ cfg, to, text });
return { channel: "msteams", ...result };
const defaultRegistry = createTestRegistry([
{
pluginId: "discord",
plugin: createOutboundTestPlugin({ id: "discord", outbound: discordOutbound, label: "Discord" }),
source: "test",
},
sendMedia: async ({ cfg, to, text, mediaUrl }) => {
const result = await mocks.sendMessageMSTeams({ cfg, to, text, mediaUrl });
return { channel: "msteams", ...result };
{
pluginId: "slack",
plugin: createOutboundTestPlugin({ id: "slack", outbound: slackOutbound, label: "Slack" }),
source: "test",
},
});
const createMSTeamsPlugin = (params: { outbound: ChannelOutboundAdapter }): ChannelPlugin => ({
id: "msteams",
meta: {
id: "msteams",
label: "Microsoft Teams",
selectionLabel: "Microsoft Teams (Bot Framework)",
docsPath: "/channels/msteams",
blurb: "Bot Framework; enterprise support.",
{
pluginId: "telegram",
plugin: createOutboundTestPlugin({
id: "telegram",
outbound: telegramOutbound,
label: "Telegram",
}),
source: "test",
},
capabilities: { chatTypes: ["direct"] },
config: {
listAccountIds: () => [],
resolveAccount: () => ({}),
{
pluginId: "whatsapp",
plugin: createOutboundTestPlugin({
id: "whatsapp",
outbound: whatsappOutbound,
label: "WhatsApp",
}),
source: "test",
},
outbound: params.outbound,
});
{
pluginId: "signal",
plugin: createOutboundTestPlugin({ id: "signal", outbound: signalOutbound, label: "Signal" }),
source: "test",
},
{
pluginId: "imessage",
plugin: createIMessageTestPlugin({ outbound: imessageOutbound }),
source: "test",
},
{
pluginId: "msteams",
plugin: createMSTeamsPlugin({
outbound: createMSTeamsOutbound(),
}),
source: "test",
},
]);

View File

@@ -1,12 +1,46 @@
import { describe, expect, it } from "vitest";
import { CHANNEL_IDS } from "../registry.js";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { ChannelPlugin } from "./types.js";
import { setActivePluginRegistry } from "../../plugins/runtime.js";
import { createTestRegistry } from "../../test-utils/channel-plugins.js";
import { listChannelPlugins } from "./index.js";
describe("channel plugin registry", () => {
it("includes the built-in channel ids", () => {
const emptyRegistry = createTestRegistry([]);
const createPlugin = (id: string): ChannelPlugin => ({
id,
meta: {
id,
label: id,
selectionLabel: id,
docsPath: `/channels/${id}`,
blurb: "test",
},
capabilities: { chatTypes: ["direct"] },
config: {
listAccountIds: () => [],
resolveAccount: () => ({}),
},
});
beforeEach(() => {
setActivePluginRegistry(emptyRegistry);
});
afterEach(() => {
setActivePluginRegistry(emptyRegistry);
});
it("sorts channel plugins by configured order", () => {
const registry = createTestRegistry(
["slack", "telegram", "signal"].map((id) => ({
pluginId: id,
plugin: createPlugin(id),
source: "test",
})),
);
setActivePluginRegistry(registry);
const pluginIds = listChannelPlugins().map((plugin) => plugin.id);
for (const id of CHANNEL_IDS) {
expect(pluginIds).toContain(id);
}
expect(pluginIds).toEqual(["telegram", "slack", "signal"]);
});
});

View File

@@ -1,11 +1,5 @@
import { CHAT_CHANNEL_ORDER, type ChatChannelId, normalizeChatChannelId } from "../registry.js";
import { discordPlugin } from "./discord.js";
import { imessagePlugin } from "./imessage.js";
import { signalPlugin } from "./signal.js";
import { slackPlugin } from "./slack.js";
import { telegramPlugin } from "./telegram.js";
import type { ChannelId, ChannelPlugin } from "./types.js";
import { whatsappPlugin } from "./whatsapp.js";
import { getActivePluginRegistry } from "../../plugins/runtime.js";
// Channel plugins registry (runtime).
@@ -14,14 +8,7 @@ import { getActivePluginRegistry } from "../../plugins/runtime.js";
// Shared code paths (reply flow, command auth, sandbox explain) should depend on `src/channels/dock.ts`
// instead, and only call `getChannelPlugin()` at execution boundaries.
//
// Adding a channel:
// - add `<id>Plugin` import + entry in `resolveChannels()`
// - add an entry to `src/channels/dock.ts` for shared behavior (capabilities, allowFrom, threading, …)
// - add ids/aliases in `src/channels/registry.ts`
function resolveCoreChannels(): ChannelPlugin[] {
return [telegramPlugin, whatsappPlugin, discordPlugin, slackPlugin, signalPlugin, imessagePlugin];
}
// Channel plugins are registered by the plugin loader (extensions/ or configured paths).
function listPluginChannels(): ChannelPlugin[] {
const registry = getActivePluginRegistry();
if (!registry) return [];
@@ -41,7 +28,7 @@ function dedupeChannels(channels: ChannelPlugin[]): ChannelPlugin[] {
}
export function listChannelPlugins(): ChannelPlugin[] {
const combined = dedupeChannels([...resolveCoreChannels(), ...listPluginChannels()]);
const combined = dedupeChannels(listPluginChannels());
return combined.sort((a, b) => {
const indexA = CHAT_CHANNEL_ORDER.indexOf(a.id as ChatChannelId);
const indexB = CHAT_CHANNEL_ORDER.indexOf(b.id as ChatChannelId);
@@ -72,8 +59,6 @@ export function normalizeChannelId(raw?: string | null): ChannelId | null {
});
return plugin?.id ?? null;
}
export { discordPlugin, imessagePlugin, signalPlugin, slackPlugin, telegramPlugin, whatsappPlugin };
export {
listDiscordDirectoryGroupsFromConfig,
listDiscordDirectoryPeersFromConfig,

View File

@@ -1,36 +1,25 @@
import type { ChannelId, ChannelPlugin } from "./types.js";
import type { ChatChannelId } from "../registry.js";
import type { PluginRegistry } from "../../plugins/registry.js";
import { getActivePluginRegistry } from "../../plugins/runtime.js";
type PluginLoader = () => Promise<ChannelPlugin>;
// Channel docking: load *one* plugin on-demand.
//
// This avoids importing `src/channels/plugins/index.ts` (intentionally heavy)
// from shared flows like outbound delivery / followup routing.
const LOADERS: Record<ChatChannelId, PluginLoader> = {
telegram: async () => (await import("./telegram.js")).telegramPlugin,
whatsapp: async () => (await import("./whatsapp.js")).whatsappPlugin,
discord: async () => (await import("./discord.js")).discordPlugin,
slack: async () => (await import("./slack.js")).slackPlugin,
signal: async () => (await import("./signal.js")).signalPlugin,
imessage: async () => (await import("./imessage.js")).imessagePlugin,
};
const cache = new Map<ChannelId, ChannelPlugin>();
let lastRegistry: PluginRegistry | null = null;
function ensureCacheForRegistry(registry: PluginRegistry | null) {
if (registry === lastRegistry) return;
cache.clear();
lastRegistry = registry;
}
export async function loadChannelPlugin(id: ChannelId): Promise<ChannelPlugin | undefined> {
const registry = getActivePluginRegistry();
ensureCacheForRegistry(registry);
const cached = cache.get(id);
if (cached) return cached;
const registry = getActivePluginRegistry();
const pluginEntry = registry?.channels.find((entry) => entry.plugin.id === id);
if (pluginEntry) {
cache.set(id, pluginEntry.plugin);
return pluginEntry.plugin;
}
const loader = LOADERS[id as ChatChannelId];
if (!loader) return undefined;
const plugin = await loader();
cache.set(id, plugin);
return plugin;
return undefined;
}

View File

@@ -1,40 +1,33 @@
import type { ChannelId, ChannelOutboundAdapter } from "../types.js";
import type { ChatChannelId } from "../../registry.js";
import type { PluginRegistry } from "../../../plugins/registry.js";
import { getActivePluginRegistry } from "../../../plugins/runtime.js";
type OutboundLoader = () => Promise<ChannelOutboundAdapter>;
// Channel docking: outbound sends should stay cheap to import.
//
// The full channel plugins (src/channels/plugins/*.ts) pull in status,
// onboarding, gateway monitors, etc. Outbound delivery only needs chunking +
// send primitives, so we keep a dedicated, lightweight loader here.
const LOADERS: Record<ChatChannelId, OutboundLoader> = {
telegram: async () => (await import("./telegram.js")).telegramOutbound,
whatsapp: async () => (await import("./whatsapp.js")).whatsappOutbound,
discord: async () => (await import("./discord.js")).discordOutbound,
slack: async () => (await import("./slack.js")).slackOutbound,
signal: async () => (await import("./signal.js")).signalOutbound,
imessage: async () => (await import("./imessage.js")).imessageOutbound,
};
const cache = new Map<ChannelId, ChannelOutboundAdapter>();
let lastRegistry: PluginRegistry | null = null;
function ensureCacheForRegistry(registry: PluginRegistry | null) {
if (registry === lastRegistry) return;
cache.clear();
lastRegistry = registry;
}
export async function loadChannelOutboundAdapter(
id: ChannelId,
): Promise<ChannelOutboundAdapter | undefined> {
const registry = getActivePluginRegistry();
ensureCacheForRegistry(registry);
const cached = cache.get(id);
if (cached) return cached;
const registry = getActivePluginRegistry();
const pluginEntry = registry?.channels.find((entry) => entry.plugin.id === id);
const outbound = pluginEntry?.plugin.outbound;
if (outbound) {
cache.set(id, outbound);
return outbound;
}
const loader = LOADERS[id as ChatChannelId];
if (!loader) return undefined;
const loaded = await loader();
cache.set(id, loaded);
return loaded;
return undefined;
}

View File

@@ -1,6 +1,6 @@
import { chunkText } from "../../../auto-reply/chunk.js";
import { shouldLogVerbose } from "../../../globals.js";
import { sendMessageWhatsApp, sendPollWhatsApp } from "../../../web/outbound.js";
import { sendPollWhatsApp } from "../../../web/outbound.js";
import { isWhatsAppGroupJid, normalizeWhatsAppTarget } from "../../../whatsapp/normalize.js";
import type { ChannelOutboundAdapter } from "../types.js";
import { missingTargetError } from "../../../infra/outbound/target-errors.js";
@@ -57,7 +57,8 @@ export const whatsappOutbound: ChannelOutboundAdapter = {
};
},
sendText: async ({ to, text, accountId, deps, gifPlayback }) => {
const send = deps?.sendWhatsApp ?? sendMessageWhatsApp;
const send =
deps?.sendWhatsApp ?? (await import("../../../web/outbound.js")).sendMessageWhatsApp;
const result = await send(to, text, {
verbose: false,
accountId: accountId ?? undefined,
@@ -66,7 +67,8 @@ export const whatsappOutbound: ChannelOutboundAdapter = {
return { channel: "whatsapp", ...result };
},
sendMedia: async ({ to, text, mediaUrl, accountId, deps, gifPlayback }) => {
const send = deps?.sendWhatsApp ?? sendMessageWhatsApp;
const send =
deps?.sendWhatsApp ?? (await import("../../../web/outbound.js")).sendMessageWhatsApp;
const result = await send(to, text, {
verbose: false,
mediaUrl,

View File

@@ -2,8 +2,8 @@ import type { ChannelMeta } from "./plugins/types.js";
import type { ChannelId } from "./plugins/types.js";
import { getActivePluginRegistry } from "../plugins/runtime.js";
// Channel docking: add new channels here (order + meta + aliases), then
// register the plugin in src/channels/plugins/index.ts and keep protocol IDs in sync.
// Channel docking: add new core channels here (order + meta + aliases), then
// register the plugin in its extension entrypoint and keep protocol IDs in sync.
export const CHAT_CHANNEL_ORDER = [
"telegram",
"whatsapp",

View File

@@ -19,7 +19,10 @@ import { runEmbeddedPiAgent } from "../agents/pi-embedded.js";
import type { ClawdbotConfig } from "../config/config.js";
import * as configModule from "../config/config.js";
import type { RuntimeEnv } from "../runtime.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js";
import { agentCommand } from "./agent.js";
import { telegramPlugin } from "../../extensions/telegram/src/channel.js";
const runtime: RuntimeEnv = {
log: vi.fn(),
@@ -251,6 +254,9 @@ describe("agentCommand", () => {
await withTempHome(async (home) => {
const store = path.join(home, "sessions.json");
mockConfig(home, store, undefined, { botToken: "t-1" });
setActivePluginRegistry(
createTestRegistry([{ pluginId: "telegram", plugin: telegramPlugin, source: "test" }]),
);
const deps = {
sendMessageWhatsApp: vi.fn(),
sendMessageTelegram: vi.fn().mockResolvedValue({ messageId: "t1", chatId: "123" }),

View File

@@ -1,6 +1,14 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { RuntimeEnv } from "../runtime.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js";
import { discordPlugin } from "../../extensions/discord/src/channel.js";
import { imessagePlugin } from "../../extensions/imessage/src/channel.js";
import { signalPlugin } from "../../extensions/signal/src/channel.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";
const configMocks = vi.hoisted(() => ({
readConfigFileSnapshot: vi.fn(),
@@ -64,6 +72,16 @@ describe("channels command", () => {
version: 1,
profiles: {},
});
setActivePluginRegistry(
createTestRegistry([
{ pluginId: "discord", plugin: discordPlugin, source: "test" },
{ pluginId: "slack", plugin: slackPlugin, source: "test" },
{ pluginId: "telegram", plugin: telegramPlugin, source: "test" },
{ pluginId: "whatsapp", plugin: whatsappPlugin, source: "test" },
{ pluginId: "signal", plugin: signalPlugin, source: "test" },
{ pluginId: "imessage", plugin: imessagePlugin, source: "test" },
]),
);
});
it("adds a non-default telegram account", async () => {

View File

@@ -1,6 +1,9 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { RuntimeEnv } from "../runtime.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createIMessageTestPlugin, createTestRegistry } from "../test-utils/channel-plugins.js";
import { signalPlugin } from "../../extensions/signal/src/channel.js";
const configMocks = vi.hoisted(() => ({
readConfigFileSnapshot: vi.fn(),
@@ -59,6 +62,13 @@ describe("channels command", () => {
version: 1,
profiles: {},
});
setActivePluginRegistry(
createTestRegistry([{ pluginId: "signal", source: "test", plugin: signalPlugin }]),
);
});
afterEach(() => {
setActivePluginRegistry(createTestRegistry([]));
});
it("surfaces Signal runtime errors in channels status output", () => {
@@ -81,6 +91,15 @@ describe("channels command", () => {
});
it("surfaces iMessage runtime errors in channels status output", () => {
setActivePluginRegistry(
createTestRegistry([
{
pluginId: "imessage",
source: "test",
plugin: createIMessageTestPlugin(),
},
]),
);
const lines = formatGatewayChannelsStatusLines({
channelAccounts: {
imessage: [

View File

@@ -3,6 +3,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import type { HealthSummary } from "./health.js";
import { healthCommand } from "./health.js";
import { stripAnsi } from "../terminal/ansi.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js";
const callGatewayMock = vi.fn();
const logWebSelfIdMock = vi.fn();
@@ -26,6 +28,32 @@ describe("healthCommand (coverage)", () => {
beforeEach(() => {
vi.clearAllMocks();
setActivePluginRegistry(
createTestRegistry([
{
pluginId: "whatsapp",
source: "test",
plugin: {
id: "whatsapp",
meta: {
id: "whatsapp",
label: "WhatsApp",
selectionLabel: "WhatsApp",
docsPath: "/channels/whatsapp",
blurb: "WhatsApp test stub.",
},
capabilities: { chatTypes: ["direct", "group"] },
config: {
listAccountIds: () => ["default"],
resolveAccount: () => ({}),
},
status: {
logSelfId: () => logWebSelfIdMock(),
},
},
},
]),
);
});
it("prints the rich text summary when linked and configured", async () => {

View File

@@ -2,10 +2,13 @@ import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { HealthSummary } from "./health.js";
import { getHealthSnapshot } from "./health.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js";
import { telegramPlugin } from "../../extensions/telegram/src/channel.js";
let testConfig: Record<string, unknown> = {};
let testStore: Record<string, { updatedAt?: number }> = {};
@@ -32,6 +35,12 @@ vi.mock("../web/auth-store.js", () => ({
}));
describe("getHealthSnapshot", () => {
beforeEach(() => {
setActivePluginRegistry(
createTestRegistry([{ pluginId: "telegram", plugin: telegramPlugin, source: "test" }]),
);
});
afterEach(() => {
vi.unstubAllGlobals();
vi.unstubAllEnvs();

View File

@@ -1,8 +1,14 @@
import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";
import type {
ChannelMessageActionAdapter,
ChannelOutboundAdapter,
ChannelPlugin,
} from "../channels/plugins/types.js";
import type { CliDeps } from "../cli/deps.js";
import type { RuntimeEnv } from "../runtime.js";
import { messageCommand } from "./message.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js";
const loadMessageCommand = async () => await import("./message.js");
let testConfig: Record<string, unknown> = {};
vi.mock("../config/config.js", async (importOriginal) => {
@@ -47,10 +53,17 @@ vi.mock("../agents/tools/whatsapp-actions.js", () => ({
const originalTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
const originalDiscordToken = process.env.DISCORD_BOT_TOKEN;
beforeEach(() => {
const setRegistry = async (registry: ReturnType<typeof createTestRegistry>) => {
const { setActivePluginRegistry } = await import("../plugins/runtime.js");
setActivePluginRegistry(registry);
};
beforeEach(async () => {
process.env.TELEGRAM_BOT_TOKEN = "";
process.env.DISCORD_BOT_TOKEN = "";
testConfig = {};
vi.resetModules();
await setRegistry(createTestRegistry([]));
callGatewayMock.mockReset();
webAuthExists.mockReset().mockResolvedValue(false);
handleDiscordAction.mockReset();
@@ -82,10 +95,55 @@ const makeDeps = (overrides: Partial<CliDeps> = {}): CliDeps => ({
...overrides,
});
const createStubPlugin = (params: {
id: ChannelPlugin["id"];
label?: string;
actions?: ChannelMessageActionAdapter;
outbound?: ChannelOutboundAdapter;
}): ChannelPlugin => ({
id: params.id,
meta: {
id: params.id,
label: params.label ?? String(params.id),
selectionLabel: params.label ?? String(params.id),
docsPath: `/channels/${params.id}`,
blurb: "test stub.",
},
capabilities: { chatTypes: ["direct"] },
config: {
listAccountIds: () => ["default"],
resolveAccount: () => ({}),
isConfigured: async () => true,
},
actions: params.actions,
outbound: params.outbound,
});
describe("messageCommand", () => {
it("defaults channel when only one configured", async () => {
process.env.TELEGRAM_BOT_TOKEN = "token-abc";
await setRegistry(
createTestRegistry([
{
pluginId: "telegram",
source: "test",
plugin: createStubPlugin({
id: "telegram",
label: "Telegram",
actions: {
listActions: () => ["send"],
handleAction: async ({ action, params, cfg, accountId }) =>
await handleTelegramAction(
{ action, to: params.to, accountId: accountId ?? undefined },
cfg,
),
},
}),
},
]),
);
const deps = makeDeps();
const { messageCommand } = await loadMessageCommand();
await messageCommand(
{
target: "123456",
@@ -100,7 +158,44 @@ describe("messageCommand", () => {
it("requires channel when multiple configured", async () => {
process.env.TELEGRAM_BOT_TOKEN = "token-abc";
process.env.DISCORD_BOT_TOKEN = "token-discord";
await setRegistry(
createTestRegistry([
{
pluginId: "telegram",
source: "test",
plugin: createStubPlugin({
id: "telegram",
label: "Telegram",
actions: {
listActions: () => ["send"],
handleAction: async ({ action, params, cfg, accountId }) =>
await handleTelegramAction(
{ action, to: params.to, accountId: accountId ?? undefined },
cfg,
),
},
}),
},
{
pluginId: "discord",
source: "test",
plugin: createStubPlugin({
id: "discord",
label: "Discord",
actions: {
listActions: () => ["poll"],
handleAction: async ({ action, params, cfg, accountId }) =>
await handleDiscordAction(
{ action, to: params.to, accountId: accountId ?? undefined },
cfg,
),
},
}),
},
]),
);
const deps = makeDeps();
const { messageCommand } = await loadMessageCommand();
await expect(
messageCommand(
{
@@ -115,7 +210,23 @@ describe("messageCommand", () => {
it("sends via gateway for WhatsApp", async () => {
callGatewayMock.mockResolvedValueOnce({ messageId: "g1" });
await setRegistry(
createTestRegistry([
{
pluginId: "whatsapp",
source: "test",
plugin: createStubPlugin({
id: "whatsapp",
label: "WhatsApp",
outbound: {
deliveryMode: "gateway",
},
}),
},
]),
);
const deps = makeDeps();
const { messageCommand } = await loadMessageCommand();
await messageCommand(
{
action: "send",
@@ -130,7 +241,28 @@ describe("messageCommand", () => {
});
it("routes discord polls through message action", async () => {
await setRegistry(
createTestRegistry([
{
pluginId: "discord",
source: "test",
plugin: createStubPlugin({
id: "discord",
label: "Discord",
actions: {
listActions: () => ["poll"],
handleAction: async ({ action, params, cfg, accountId }) =>
await handleDiscordAction(
{ action, to: params.to, accountId: accountId ?? undefined },
cfg,
),
},
}),
},
]),
);
const deps = makeDeps();
const { messageCommand } = await loadMessageCommand();
await messageCommand(
{
action: "poll",

View File

@@ -1,9 +1,17 @@
import { describe, expect, it, vi } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { ClawdbotConfig } from "../config/config.js";
import type { RuntimeEnv } from "../runtime.js";
import type { WizardPrompter } from "../wizard/prompts.js";
import { setupChannels } from "./onboard-channels.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js";
import { discordPlugin } from "../../extensions/discord/src/channel.js";
import { imessagePlugin } from "../../extensions/imessage/src/channel.js";
import { signalPlugin } from "../../extensions/signal/src/channel.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";
vi.mock("node:fs/promises", () => ({
default: {
@@ -22,6 +30,18 @@ vi.mock("./onboard-helpers.js", () => ({
}));
describe("setupChannels", () => {
beforeEach(() => {
setActivePluginRegistry(
createTestRegistry([
{ pluginId: "discord", plugin: discordPlugin, source: "test" },
{ pluginId: "slack", plugin: slackPlugin, source: "test" },
{ pluginId: "telegram", plugin: telegramPlugin, source: "test" },
{ pluginId: "whatsapp", plugin: whatsappPlugin, source: "test" },
{ pluginId: "signal", plugin: signalPlugin, source: "test" },
{ pluginId: "imessage", plugin: imessagePlugin, source: "test" },
]),
);
});
it("QuickStart uses single-select (no multiselect) and doesn't prompt for Telegram token when WhatsApp is chosen", async () => {
const select = vi.fn(async () => "whatsapp");
const multiselect = vi.fn(async () => {

View File

@@ -3,7 +3,7 @@ import { createServer } from "node:net";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { WebSocket } from "ws";
import { PROTOCOL_VERSION } from "../gateway/protocol/index.js";
@@ -114,6 +114,7 @@ describe("onboard (non-interactive): gateway auth", () => {
process.env.HOME = tempHome;
delete process.env.CLAWDBOT_STATE_DIR;
delete process.env.CLAWDBOT_CONFIG_PATH;
vi.resetModules();
const token = "tok_test_123";
const workspace = path.join(tempHome, "clawd");

View File

@@ -3,7 +3,7 @@ import { createServer } from "node:net";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
async function getFreePort(): Promise<number> {
return await new Promise((resolve, reject) => {
@@ -50,6 +50,7 @@ describe("onboard (non-interactive): remote gateway config", () => {
process.env.HOME = tempHome;
delete process.env.CLAWDBOT_STATE_DIR;
delete process.env.CLAWDBOT_CONFIG_PATH;
vi.resetModules();
const port = await getFreePort();
const token = "tok_remote_123";

View File

@@ -6,7 +6,12 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import { withTempHome as withTempHomeBase } from "../../test/helpers/temp-home.js";
import type { CliDeps } from "../cli/deps.js";
import type { ClawdbotConfig } from "../config/config.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js";
import type { CronJob } from "./types.js";
import { discordPlugin } from "../../extensions/discord/src/channel.js";
import { telegramPlugin } from "../../extensions/telegram/src/channel.js";
import { whatsappPlugin } from "../../extensions/whatsapp/src/channel.js";
vi.mock("../agents/pi-embedded.js", () => ({
abortEmbeddedPiRun: vi.fn().mockReturnValue(false),
@@ -85,6 +90,13 @@ describe("runCronIsolatedAgentTurn", () => {
beforeEach(() => {
vi.mocked(runEmbeddedPiAgent).mockReset();
vi.mocked(loadModelCatalog).mockResolvedValue([]);
setActivePluginRegistry(
createTestRegistry([
{ pluginId: "whatsapp", plugin: whatsappPlugin, source: "test" },
{ pluginId: "telegram", plugin: telegramPlugin, source: "test" },
{ pluginId: "discord", plugin: discordPlugin, source: "test" },
]),
);
});
it("skips delivery without a WhatsApp recipient when bestEffortDeliver=true", async () => {

View File

@@ -1,5 +1,8 @@
import { describe, expect, it } from "vitest";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { listChannelPlugins } from "../channels/plugins/index.js";
import type { ChannelPlugin } from "../channels/plugins/types.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js";
import {
buildGatewayReloadPlan,
diffConfigPaths,
@@ -23,6 +26,52 @@ describe("diffConfigPaths", () => {
});
describe("buildGatewayReloadPlan", () => {
const emptyRegistry = createTestRegistry([]);
const telegramPlugin: ChannelPlugin = {
id: "telegram",
meta: {
id: "telegram",
label: "Telegram",
selectionLabel: "Telegram",
docsPath: "/channels/telegram",
blurb: "test",
},
capabilities: { chatTypes: ["direct"] },
config: {
listAccountIds: () => [],
resolveAccount: () => ({}),
},
reload: { configPrefixes: ["channels.telegram"] },
};
const whatsappPlugin: ChannelPlugin = {
id: "whatsapp",
meta: {
id: "whatsapp",
label: "WhatsApp",
selectionLabel: "WhatsApp",
docsPath: "/channels/whatsapp",
blurb: "test",
},
capabilities: { chatTypes: ["direct"] },
config: {
listAccountIds: () => [],
resolveAccount: () => ({}),
},
reload: { configPrefixes: ["web"], noopPrefixes: ["channels.whatsapp"] },
};
const registry = createTestRegistry([
{ pluginId: "telegram", plugin: telegramPlugin, source: "test" },
{ pluginId: "whatsapp", plugin: whatsappPlugin, source: "test" },
]);
beforeEach(() => {
setActivePluginRegistry(registry);
});
afterEach(() => {
setActivePluginRegistry(emptyRegistry);
});
it("marks gateway changes as restart required", () => {
const plan = buildGatewayReloadPlan(["gateway.port"]);
expect(plan.restartGateway).toBe(true);

View File

@@ -1,5 +1,6 @@
import chokidar from "chokidar";
import { type ChannelId, listChannelPlugins } from "../channels/plugins/index.js";
import { getActivePluginRegistry } from "../plugins/runtime.js";
import type { ClawdbotConfig, ConfigFileSnapshot, GatewayReloadMode } from "../config/config.js";
export type GatewayReloadSettings = {
@@ -85,8 +86,14 @@ const BASE_RELOAD_RULES_TAIL: ReloadRule[] = [
];
let cachedReloadRules: ReloadRule[] | null = null;
let cachedRegistry: ReturnType<typeof getActivePluginRegistry> | null = null;
function listReloadRules(): ReloadRule[] {
const registry = getActivePluginRegistry();
if (registry !== cachedRegistry) {
cachedReloadRules = null;
cachedRegistry = registry;
}
if (cachedReloadRules) return cachedReloadRules;
// Channel docking: plugins contribute hot reload/no-op prefixes here.
const channelReloadRules: ReloadRule[] = listChannelPlugins().flatMap((plugin) => [

View File

@@ -3,7 +3,7 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { WebSocket } from "ws";
import { rawDataToString } from "../infra/ws.js";
@@ -141,6 +141,7 @@ describe("gateway wizard (e2e)", () => {
process.env.HOME = tempHome;
delete process.env.CLAWDBOT_STATE_DIR;
delete process.env.CLAWDBOT_CONFIG_PATH;
vi.resetModules();
const wizardToken = `wiz-${randomUUID()}`;
const port = await getFreeGatewayPort();

View File

@@ -2,8 +2,8 @@ import type { IncomingMessage } from "node:http";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import type { ClawdbotConfig } from "../config/config.js";
import type { ChannelPlugin } from "../channels/plugins/types.js";
import type { PluginRegistry } from "../plugins/registry.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createIMessageTestPlugin, createTestRegistry } from "../test-utils/channel-plugins.js";
import {
extractHookToken,
normalizeAgentPayload,
@@ -85,6 +85,15 @@ describe("gateway hooks helpers", () => {
expect(explicitNoDeliver.value.deliver).toBe(false);
}
setActivePluginRegistry(
createTestRegistry([
{
pluginId: "imessage",
source: "test",
plugin: createIMessageTestPlugin(),
},
]),
);
const imsg = normalizeAgentPayload(
{ message: "yo", channel: "imsg" },
{ idFactory: () => "x" },
@@ -95,7 +104,7 @@ describe("gateway hooks helpers", () => {
}
setActivePluginRegistry(
createRegistry([
createTestRegistry([
{
pluginId: "msteams",
source: "test",
@@ -117,19 +126,7 @@ describe("gateway hooks helpers", () => {
});
});
const createRegistry = (channels: PluginRegistry["channels"]): PluginRegistry => ({
plugins: [],
tools: [],
channels,
providers: [],
gatewayHandlers: {},
httpHandlers: [],
cliRegistrars: [],
services: [],
diagnostics: [],
});
const emptyRegistry = createRegistry([]);
const emptyRegistry = createTestRegistry([]);
const createMSTeamsPlugin = (params: { aliases?: string[] }): ChannelPlugin => ({
id: "msteams",

View File

@@ -8,9 +8,15 @@ const mocks = vi.hoisted(() => ({
appendAssistantMessageToSessionTranscript: vi.fn(async () => ({ ok: true, sessionFile: "x" })),
}));
vi.mock("../../config/config.js", () => ({
loadConfig: () => ({}),
}));
vi.mock("../../config/config.js", async () => {
const actual = await vi.importActual<typeof import("../../config/config.js")>(
"../../config/config.js",
);
return {
...actual,
loadConfig: () => ({}),
};
});
vi.mock("../../channels/plugins/index.js", () => ({
getChannelPlugin: () => ({ outbound: {} }),

View File

@@ -2,6 +2,7 @@ import { getChannelPlugin, normalizeChannelId } from "../../channels/plugins/ind
import type { ChannelId } from "../../channels/plugins/types.js";
import { DEFAULT_CHAT_CHANNEL } from "../../channels/registry.js";
import { loadConfig } from "../../config/config.js";
import { createOutboundSendDeps } from "../../cli/deps.js";
import { deliverOutboundPayloads } from "../../infra/outbound/deliver.js";
import { resolveSessionAgentId } from "../../agents/agent-scope.js";
import type { OutboundChannel } from "../../infra/outbound/targets.js";
@@ -15,7 +16,28 @@ import {
validateSendParams,
} from "../protocol/index.js";
import { formatForLog } from "../ws-log.js";
import type { GatewayRequestHandlers } from "./types.js";
import type { GatewayRequestContext, GatewayRequestHandlers } from "./types.js";
type InflightResult = {
ok: boolean;
payload?: Record<string, unknown>;
error?: ReturnType<typeof errorShape>;
meta?: Record<string, unknown>;
};
const inflightByContext = new WeakMap<
GatewayRequestContext,
Map<string, Promise<InflightResult>>
>();
const getInflightMap = (context: GatewayRequestContext) => {
let inflight = inflightByContext.get(context);
if (!inflight) {
inflight = new Map();
inflightByContext.set(context, inflight);
}
return inflight;
};
export const sendHandlers: GatewayRequestHandlers = {
send: async ({ params, respond, context }) => {
@@ -42,13 +64,22 @@ export const sendHandlers: GatewayRequestHandlers = {
idempotencyKey: string;
};
const idem = request.idempotencyKey;
const cached = context.dedupe.get(`send:${idem}`);
const dedupeKey = `send:${idem}`;
const cached = context.dedupe.get(dedupeKey);
if (cached) {
respond(cached.ok, cached.payload, cached.error, {
cached: true,
});
return;
}
const inflightMap = getInflightMap(context);
const inflight = inflightMap.get(dedupeKey);
if (inflight) {
const result = await inflight;
const meta = result.meta ? { ...result.meta, cached: true } : { cached: true };
respond(result.ok, result.payload, result.error, meta);
return;
}
const to = request.to.trim();
const message = request.message.trim();
const channelInput = typeof request.channel === "string" ? request.channel : undefined;
@@ -66,79 +97,99 @@ export const sendHandlers: GatewayRequestHandlers = {
typeof request.accountId === "string" && request.accountId.trim().length
? request.accountId.trim()
: undefined;
try {
const outboundChannel = channel as Exclude<OutboundChannel, "none">;
const plugin = getChannelPlugin(channel as ChannelId);
if (!plugin) {
respond(
false,
undefined,
errorShape(ErrorCodes.INVALID_REQUEST, `unsupported channel: ${channel}`),
);
return;
}
const cfg = loadConfig();
const resolved = resolveOutboundTarget({
channel: outboundChannel,
to,
cfg,
accountId,
mode: "explicit",
});
if (!resolved.ok) {
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, String(resolved.error)));
return;
}
const results = await deliverOutboundPayloads({
cfg,
channel: outboundChannel,
to: resolved.to,
accountId,
payloads: [{ text: message, mediaUrl: request.mediaUrl }],
gifPlayback: request.gifPlayback,
mirror:
typeof request.sessionKey === "string" && request.sessionKey.trim()
? {
sessionKey: request.sessionKey.trim(),
agentId: resolveSessionAgentId({
sessionKey: request.sessionKey.trim(),
config: cfg,
}),
text: message,
mediaUrls: request.mediaUrl ? [request.mediaUrl] : undefined,
}
: undefined,
});
const outboundChannel = channel as Exclude<OutboundChannel, "none">;
const plugin = getChannelPlugin(channel as ChannelId);
if (!plugin) {
respond(
false,
undefined,
errorShape(ErrorCodes.INVALID_REQUEST, `unsupported channel: ${channel}`),
);
return;
}
const result = results.at(-1);
if (!result) {
throw new Error("No delivery result");
const work = (async (): Promise<InflightResult> => {
try {
const cfg = loadConfig();
const resolved = resolveOutboundTarget({
channel: outboundChannel,
to,
cfg,
accountId,
mode: "explicit",
});
if (!resolved.ok) {
return {
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, String(resolved.error)),
meta: { channel },
};
}
const outboundDeps = context.deps ? createOutboundSendDeps(context.deps) : undefined;
const results = await deliverOutboundPayloads({
cfg,
channel: outboundChannel,
to: resolved.to,
accountId,
payloads: [{ text: message, mediaUrl: request.mediaUrl }],
gifPlayback: request.gifPlayback,
deps: outboundDeps,
mirror:
typeof request.sessionKey === "string" && request.sessionKey.trim()
? {
sessionKey: request.sessionKey.trim(),
agentId: resolveSessionAgentId({
sessionKey: request.sessionKey.trim(),
config: cfg,
}),
text: message,
mediaUrls: request.mediaUrl ? [request.mediaUrl] : undefined,
}
: undefined,
});
const result = results.at(-1);
if (!result) {
throw new Error("No delivery result");
}
const payload: Record<string, unknown> = {
runId: idem,
messageId: result.messageId,
channel,
};
if ("chatId" in result) payload.chatId = result.chatId;
if ("channelId" in result) payload.channelId = result.channelId;
if ("toJid" in result) payload.toJid = result.toJid;
if ("conversationId" in result) {
payload.conversationId = result.conversationId;
}
context.dedupe.set(dedupeKey, {
ts: Date.now(),
ok: true,
payload,
});
return {
ok: true,
payload,
meta: { channel },
};
} catch (err) {
const error = errorShape(ErrorCodes.UNAVAILABLE, String(err));
context.dedupe.set(dedupeKey, {
ts: Date.now(),
ok: false,
error,
});
return { ok: false, error, meta: { channel, error: formatForLog(err) } };
}
const payload: Record<string, unknown> = {
runId: idem,
messageId: result.messageId,
channel,
};
if ("chatId" in result) payload.chatId = result.chatId;
if ("channelId" in result) payload.channelId = result.channelId;
if ("toJid" in result) payload.toJid = result.toJid;
if ("conversationId" in result) {
payload.conversationId = result.conversationId;
}
context.dedupe.set(`send:${idem}`, {
ts: Date.now(),
ok: true,
payload,
});
respond(true, payload, undefined, { channel });
} catch (err) {
const error = errorShape(ErrorCodes.UNAVAILABLE, String(err));
context.dedupe.set(`send:${idem}`, {
ts: Date.now(),
ok: false,
error,
});
respond(false, undefined, error, { channel, error: formatForLog(err) });
})();
inflightMap.set(dedupeKey, work);
try {
const result = await work;
respond(result.ok, result.payload, result.error, result.meta);
} finally {
inflightMap.delete(dedupeKey);
}
},
poll: async ({ params, respond, context }) => {

View File

@@ -8,6 +8,7 @@ import { emitAgentEvent, registerAgentRunContext } from "../infra/agent-events.j
import type { PluginRegistry } from "../plugins/registry.js";
import { setActivePluginRegistry } from "../plugins/runtime.js";
import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js";
import { whatsappPlugin } from "../../extensions/whatsapp/src/channel.js";
import {
agentCommand,
connectOk,
@@ -53,6 +54,44 @@ vi.mock("./server-plugins.js", async () => {
const _BASE_IMAGE_PNG =
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+X3mIAAAAASUVORK5CYII=";
const createRegistry = (channels: PluginRegistry["channels"]): PluginRegistry => ({
plugins: [],
tools: [],
channels,
providers: [],
gatewayHandlers: {},
httpHandlers: [],
cliRegistrars: [],
services: [],
diagnostics: [],
});
const createMSTeamsPlugin = (params?: { aliases?: string[] }): ChannelPlugin => ({
id: "msteams",
meta: {
id: "msteams",
label: "Microsoft Teams",
selectionLabel: "Microsoft Teams (Bot Framework)",
docsPath: "/channels/msteams",
blurb: "Bot Framework; enterprise support.",
aliases: params?.aliases,
},
capabilities: { chatTypes: ["direct"] },
config: {
listAccountIds: () => [],
resolveAccount: () => ({}),
},
});
const emptyRegistry = createRegistry([]);
const defaultRegistry = createRegistry([
{
pluginId: "whatsapp",
source: "test",
plugin: whatsappPlugin,
},
]);
function expectChannels(call: Record<string, unknown>, channel: string) {
expect(call.channel).toBe(channel);
expect(call.messageChannel).toBe(channel);
@@ -60,8 +99,8 @@ function expectChannels(call: Record<string, unknown>, channel: string) {
describe("gateway server agent", () => {
beforeEach(() => {
registryState.registry = emptyRegistry;
setActivePluginRegistry(emptyRegistry);
registryState.registry = defaultRegistry;
setActivePluginRegistry(defaultRegistry);
});
afterEach(() => {
@@ -439,34 +478,3 @@ describe("gateway server agent", () => {
await server.close();
});
});
const createRegistry = (channels: PluginRegistry["channels"]): PluginRegistry => ({
plugins: [],
tools: [],
channels,
providers: [],
gatewayHandlers: {},
httpHandlers: [],
cliRegistrars: [],
services: [],
diagnostics: [],
});
const emptyRegistry = createRegistry([]);
const createMSTeamsPlugin = (params?: { aliases?: string[] }): ChannelPlugin => ({
id: "msteams",
meta: {
id: "msteams",
label: "Microsoft Teams",
selectionLabel: "Microsoft Teams (Bot Framework)",
docsPath: "/channels/msteams",
blurb: "Bot Framework; enterprise support.",
aliases: params?.aliases,
},
capabilities: { chatTypes: ["direct"] },
config: {
listAccountIds: () => [],
resolveAccount: () => ({}),
},
});

View File

@@ -66,6 +66,7 @@ const hoisted = vi.hoisted(() => ({
waitCalls: [] as string[],
waitResults: new Map<string, boolean>(),
},
sendWhatsAppMock: vi.fn().mockResolvedValue({ messageId: "msg-1", toJid: "jid-1" }),
}));
const testConfigRoot = {
@@ -74,6 +75,7 @@ const testConfigRoot = {
export const setTestConfigRoot = (root: string) => {
testConfigRoot.value = root;
process.env.CLAWDBOT_CONFIG_PATH = path.join(root, "clawdbot.json");
};
export const bridgeStartCalls = hoisted.bridgeStartCalls;
@@ -342,10 +344,33 @@ vi.mock("../commands/status.js", () => ({
getStatusSummary: vi.fn().mockResolvedValue({ ok: true }),
}));
vi.mock("../web/outbound.js", () => ({
sendMessageWhatsApp: vi.fn().mockResolvedValue({ messageId: "msg-1", toJid: "jid-1" }),
sendMessageWhatsApp: (...args: unknown[]) =>
(hoisted.sendWhatsAppMock as (...args: unknown[]) => unknown)(...args),
}));
vi.mock("../channels/web/index.js", async () => {
const actual = await vi.importActual<typeof import("../channels/web/index.js")>(
"../channels/web/index.js",
);
return {
...actual,
sendMessageWhatsApp: (...args: unknown[]) =>
(hoisted.sendWhatsAppMock as (...args: unknown[]) => unknown)(...args),
};
});
vi.mock("../commands/agent.js", () => ({
agentCommand,
}));
vi.mock("../cli/deps.js", async () => {
const actual = await vi.importActual<typeof import("../cli/deps.js")>("../cli/deps.js");
const base = actual.createDefaultDeps();
return {
...actual,
createDefaultDeps: () => ({
...base,
sendMessageWhatsApp: (...args: unknown[]) =>
(hoisted.sendWhatsAppMock as (...args: unknown[]) => unknown)(...args),
}),
};
});
process.env.CLAWDBOT_SKIP_CHANNELS = "1";

View File

@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { sendMessageIMessage } from "./send.js";
const loadSendMessageIMessage = async () => await import("./send.js");
const requestMock = vi.fn();
const stopMock = vi.fn();
@@ -38,9 +38,11 @@ describe("sendMessageIMessage", () => {
beforeEach(() => {
requestMock.mockReset().mockResolvedValue({ ok: true });
stopMock.mockReset().mockResolvedValue(undefined);
vi.resetModules();
});
it("sends to chat_id targets", async () => {
const { sendMessageIMessage } = await loadSendMessageIMessage();
await sendMessageIMessage("chat_id:123", "hi");
const params = requestMock.mock.calls[0]?.[1] as Record<string, unknown>;
expect(requestMock).toHaveBeenCalledWith("send", expect.any(Object), expect.any(Object));
@@ -49,6 +51,7 @@ describe("sendMessageIMessage", () => {
});
it("applies sms service prefix", async () => {
const { sendMessageIMessage } = await loadSendMessageIMessage();
await sendMessageIMessage("sms:+1555", "hello");
const params = requestMock.mock.calls[0]?.[1] as Record<string, unknown>;
expect(params.service).toBe("sms");
@@ -56,6 +59,7 @@ describe("sendMessageIMessage", () => {
});
it("adds file attachment with placeholder text", async () => {
const { sendMessageIMessage } = await loadSendMessageIMessage();
await sendMessageIMessage("chat_id:7", "", { mediaUrl: "http://x/y.jpg" });
const params = requestMock.mock.calls[0]?.[1] as Record<string, unknown>;
expect(params.file).toBe("/tmp/imessage-media.jpg");

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"] } },

View File

@@ -19,7 +19,7 @@ vi.doMock("node:https", () => ({
request: (...args: unknown[]) => mockRequest(...args),
}));
const { saveMediaSource } = await import("./store.js");
const loadStore = async () => await import("./store.js");
describe("media store redirects", () => {
beforeAll(async () => {
@@ -28,6 +28,7 @@ describe("media store redirects", () => {
beforeEach(() => {
mockRequest.mockReset();
vi.resetModules();
});
afterAll(async () => {
@@ -36,6 +37,7 @@ describe("media store redirects", () => {
});
it("follows redirects and keeps detected mime/extension", async () => {
const { saveMediaSource } = await loadStore();
let call = 0;
mockRequest.mockImplementation((_url, _opts, cb) => {
call += 1;
@@ -78,6 +80,7 @@ describe("media store redirects", () => {
});
it("sniffs xlsx from zip content when headers and url extension are missing", async () => {
const { saveMediaSource } = await loadStore();
mockRequest.mockImplementationOnce((_url, _opts, cb) => {
const res = new PassThrough();
const req = {

View File

@@ -57,6 +57,7 @@ export type { ClawdbotPluginApi } from "../plugins/types.js";
export type { PluginRuntime } from "../plugins/runtime/types.js";
export type { ClawdbotConfig } from "../config/config.js";
export type { ChannelDock } from "../channels/dock.js";
export { getChatChannelMeta } from "../channels/registry.js";
export type {
DmPolicy,
GroupPolicy,
@@ -65,13 +66,21 @@ export type {
MSTeamsReplyStyle,
MSTeamsTeamConfig,
} from "../config/types.js";
export { MSTeamsConfigSchema } from "../config/zod-schema.providers-core.js";
export {
DiscordConfigSchema,
IMessageConfigSchema,
MSTeamsConfigSchema,
SignalConfigSchema,
SlackConfigSchema,
TelegramConfigSchema,
} from "../config/zod-schema.providers-core.js";
export { WhatsAppConfigSchema } from "../config/zod-schema.providers-whatsapp.js";
export type { RuntimeEnv } from "../runtime.js";
export type { WizardPrompter } from "../wizard/prompts.js";
export { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../routing/session-key.js";
export type { ReplyPayload } from "../auto-reply/types.js";
export { SILENT_REPLY_TOKEN, isSilentReplyText } from "../auto-reply/tokens.js";
export { chunkMarkdownText, resolveTextChunkLimit } from "../auto-reply/chunk.js";
export { chunkMarkdownText, chunkText, resolveTextChunkLimit } from "../auto-reply/chunk.js";
export {
hasControlCommand,
isControlCommandMessage,
@@ -98,6 +107,14 @@ export { resolveEffectiveMessagesConfig, resolveHumanDelayConfig } from "../agen
export { mergeAllowlist, summarizeMapping } from "../channels/allowlists/resolve-utils.js";
export { resolveCommandAuthorizedFromAuthorizers } from "../channels/command-gating.js";
export { resolveMentionGating } from "../channels/mention-gating.js";
export { resolveChannelMediaMaxBytes } from "../channels/plugins/media-limits.js";
export {
resolveDiscordGroupRequireMention,
resolveIMessageGroupRequireMention,
resolveSlackGroupRequireMention,
resolveTelegramGroupRequireMention,
resolveWhatsAppGroupRequireMention,
} from "../channels/plugins/group-mentions.js";
export {
buildChannelKeyCandidates,
normalizeChannelSlug,
@@ -105,6 +122,16 @@ export {
resolveChannelEntryMatchWithFallback,
resolveNestedAllowlistDecision,
} from "../channels/plugins/channel-config.js";
export {
listDiscordDirectoryGroupsFromConfig,
listDiscordDirectoryPeersFromConfig,
listSlackDirectoryGroupsFromConfig,
listSlackDirectoryPeersFromConfig,
listTelegramDirectoryGroupsFromConfig,
listTelegramDirectoryPeersFromConfig,
listWhatsAppDirectoryGroupsFromConfig,
listWhatsAppDirectoryPeersFromConfig,
} from "../channels/plugins/directory-config.js";
export type { AllowlistMatch } from "../channels/plugins/allowlist-match.js";
export { formatAllowlistMatchMeta } from "../channels/plugins/allowlist-match.js";
export {
@@ -118,7 +145,7 @@ export {
updateLastRoute,
} from "../config/sessions.js";
export { resolveStateDir } from "../config/paths.js";
export { loadConfig } from "../config/config.js";
export { loadConfig, writeConfigFile } from "../config/config.js";
export { optionalStringEnum, stringEnum } from "../agents/schema/typebox.js";
export { danger } from "../globals.js";
export { logVerbose, shouldLogVerbose } from "../globals.js";
@@ -144,6 +171,15 @@ export {
} from "../channels/plugins/setup-helpers.js";
export { formatPairingApproveHint } from "../channels/plugins/helpers.js";
export { PAIRING_APPROVED_MESSAGE } from "../channels/plugins/pairing-message.js";
export {
listIMessageAccountIds,
resolveDefaultIMessageAccountId,
resolveIMessageAccount,
type ResolvedIMessageAccount,
} from "../imessage/accounts.js";
export { monitorIMessageProvider } from "../imessage/monitor.js";
export { probeIMessage } from "../imessage/probe.js";
export { sendMessageIMessage } from "../imessage/send.js";
export type {
ChannelOnboardingAdapter,
@@ -151,6 +187,7 @@ export type {
} from "../channels/plugins/onboarding-types.js";
export { addWildcardAllowFrom, promptAccountId } from "../channels/plugins/onboarding/helpers.js";
export { promptChannelAccessConfig } from "../channels/plugins/onboarding/channel-access.js";
export { imessageOnboardingAdapter } from "../channels/plugins/onboarding/imessage.js";
export {
createActionGate,
@@ -165,3 +202,120 @@ export { registerMemoryCli } from "../cli/memory-cli.js";
export { formatDocsLink } from "../terminal/links.js";
export type { HookEntry } from "../hooks/types.js";
export { registerPluginHooksFromDir } from "../hooks/plugin-hooks.js";
export { normalizeE164 } from "../utils.js";
export { missingTargetError } from "../infra/outbound/target-errors.js";
// Channel: Discord
export {
listDiscordAccountIds,
resolveDefaultDiscordAccountId,
resolveDiscordAccount,
type ResolvedDiscordAccount,
} from "../discord/accounts.js";
export {
auditDiscordChannelPermissions,
collectDiscordAuditChannelIds,
} from "../discord/audit.js";
export { listDiscordDirectoryGroupsLive, listDiscordDirectoryPeersLive } from "../discord/directory-live.js";
export { probeDiscord } from "../discord/probe.js";
export { resolveDiscordChannelAllowlist } from "../discord/resolve-channels.js";
export { resolveDiscordUserAllowlist } from "../discord/resolve-users.js";
export { sendMessageDiscord, sendPollDiscord } from "../discord/send.js";
export { monitorDiscordProvider } from "../discord/monitor.js";
export { discordMessageActions } from "../channels/plugins/actions/discord.js";
export { discordOnboardingAdapter } from "../channels/plugins/onboarding/discord.js";
export {
looksLikeDiscordTargetId,
normalizeDiscordMessagingTarget,
} from "../channels/plugins/normalize/discord.js";
export { collectDiscordStatusIssues } from "../channels/plugins/status-issues/discord.js";
// Channel: Slack
export {
listEnabledSlackAccounts,
listSlackAccountIds,
resolveDefaultSlackAccountId,
resolveSlackAccount,
type ResolvedSlackAccount,
} from "../slack/accounts.js";
export { listSlackDirectoryGroupsLive, listSlackDirectoryPeersLive } from "../slack/directory-live.js";
export { probeSlack } from "../slack/probe.js";
export { resolveSlackChannelAllowlist } from "../slack/resolve-channels.js";
export { resolveSlackUserAllowlist } from "../slack/resolve-users.js";
export { sendMessageSlack } from "../slack/send.js";
export { monitorSlackProvider } from "../slack/index.js";
export { handleSlackAction } from "../agents/tools/slack-actions.js";
export { slackOnboardingAdapter } from "../channels/plugins/onboarding/slack.js";
export {
looksLikeSlackTargetId,
normalizeSlackMessagingTarget,
} from "../channels/plugins/normalize/slack.js";
// Channel: Telegram
export {
listTelegramAccountIds,
resolveDefaultTelegramAccountId,
resolveTelegramAccount,
type ResolvedTelegramAccount,
} from "../telegram/accounts.js";
export {
auditTelegramGroupMembership,
collectTelegramUnmentionedGroupIds,
} from "../telegram/audit.js";
export { probeTelegram } from "../telegram/probe.js";
export { resolveTelegramToken } from "../telegram/token.js";
export { sendMessageTelegram } from "../telegram/send.js";
export { monitorTelegramProvider } from "../telegram/monitor.js";
export { telegramMessageActions } from "../channels/plugins/actions/telegram.js";
export { telegramOnboardingAdapter } from "../channels/plugins/onboarding/telegram.js";
export {
looksLikeTelegramTargetId,
normalizeTelegramMessagingTarget,
} from "../channels/plugins/normalize/telegram.js";
export { collectTelegramStatusIssues } from "../channels/plugins/status-issues/telegram.js";
// Channel: Signal
export {
listSignalAccountIds,
resolveDefaultSignalAccountId,
resolveSignalAccount,
type ResolvedSignalAccount,
} from "../signal/accounts.js";
export { probeSignal } from "../signal/probe.js";
export { sendMessageSignal } from "../signal/send.js";
export { monitorSignalProvider } from "../signal/index.js";
export { signalOnboardingAdapter } from "../channels/plugins/onboarding/signal.js";
export {
looksLikeSignalTargetId,
normalizeSignalMessagingTarget,
} from "../channels/plugins/normalize/signal.js";
// Channel: WhatsApp
export {
listWhatsAppAccountIds,
resolveDefaultWhatsAppAccountId,
resolveWhatsAppAccount,
type ResolvedWhatsAppAccount,
} from "../web/accounts.js";
export { getActiveWebListener } from "../web/active-listener.js";
export {
getWebAuthAgeMs,
logoutWeb,
logWebSelfId,
readWebSelfId,
webAuthExists,
} from "../web/auth-store.js";
export { sendMessageWhatsApp, sendPollWhatsApp } from "../web/outbound.js";
export { isWhatsAppGroupJid, normalizeWhatsAppTarget } from "../whatsapp/normalize.js";
export { loginWeb } from "../web/login.js";
export { startWebLoginWithQr, waitForWebLogin } from "../web/login-qr.js";
export { monitorWebChannel } from "../channels/web/index.js";
export { handleWhatsAppAction } from "../agents/tools/whatsapp-actions.js";
export { createWhatsAppLoginTool } from "../channels/plugins/agent-tools/whatsapp-login.js";
export { whatsappOnboardingAdapter } from "../channels/plugins/onboarding/whatsapp.js";
export { resolveWhatsAppHeartbeatRecipients } from "../channels/plugins/whatsapp-heartbeat.js";
export {
looksLikeWhatsAppTargetId,
normalizeWhatsAppMessagingTarget,
} from "../channels/plugins/normalize/whatsapp.js";
export { collectWhatsAppStatusIssues } from "../channels/plugins/status-issues/whatsapp.js";

View File

@@ -46,6 +46,14 @@ const registryCache = new Map<string, PluginRegistry>();
const defaultLogger = () => createSubsystemLogger("plugins");
const BUNDLED_ENABLED_BY_DEFAULT = new Set([
"telegram",
"whatsapp",
"discord",
"slack",
"signal",
]);
const normalizeList = (value: unknown): string[] => {
if (!Array.isArray(value)) return [];
return value.map((entry) => (typeof entry === "string" ? entry.trim() : "")).filter(Boolean);
@@ -174,6 +182,9 @@ function resolveEnableState(
if (entry?.enabled === false) {
return { enabled: false, reason: "disabled in config" };
}
if (origin === "bundled" && BUNDLED_ENABLED_BY_DEFAULT.has(id)) {
return { enabled: true };
}
if (origin === "bundled") {
return { enabled: false, reason: "bundled (disabled by default)" };
}

View File

@@ -3,9 +3,9 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { ClawdbotConfig } from "../config/config.js";
import type { ChannelPlugin } from "../channels/plugins/types.js";
import { runSecurityAudit } from "./audit.js";
import { discordPlugin } from "../channels/plugins/discord.js";
import { slackPlugin } from "../channels/plugins/slack.js";
import { telegramPlugin } from "../channels/plugins/telegram.js";
import { discordPlugin } from "../../extensions/discord/src/channel.js";
import { slackPlugin } from "../../extensions/slack/src/channel.js";
import { telegramPlugin } from "../../extensions/telegram/src/channel.js";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";

View File

@@ -0,0 +1,95 @@
import { imessageOutbound } from "../channels/plugins/outbound/imessage.js";
import type {
ChannelCapabilities,
ChannelId,
ChannelOutboundAdapter,
ChannelPlugin,
} from "../channels/plugins/types.js";
import type { PluginRegistry } from "../plugins/registry.js";
import { normalizeIMessageHandle } from "../imessage/targets.js";
export const createTestRegistry = (
channels: PluginRegistry["channels"] = [],
): PluginRegistry => ({
plugins: [],
tools: [],
channels,
providers: [],
gatewayHandlers: {},
httpHandlers: [],
cliRegistrars: [],
services: [],
diagnostics: [],
});
export const createIMessageTestPlugin = (params?: {
outbound?: ChannelOutboundAdapter;
}): ChannelPlugin => ({
id: "imessage",
meta: {
id: "imessage",
label: "iMessage",
selectionLabel: "iMessage (imsg)",
docsPath: "/channels/imessage",
blurb: "iMessage test stub.",
},
capabilities: { chatTypes: ["direct", "group"], media: true },
config: {
listAccountIds: () => [],
resolveAccount: () => ({}),
},
status: {
collectStatusIssues: (accounts) =>
accounts.flatMap((account) => {
const lastError = typeof account.lastError === "string" ? account.lastError.trim() : "";
if (!lastError) return [];
return [
{
channel: "imessage",
accountId: account.accountId,
kind: "runtime",
message: `Channel error: ${lastError}`,
},
];
}),
},
outbound: params?.outbound ?? imessageOutbound,
messaging: {
targetResolver: {
looksLikeId: (raw) => {
const trimmed = raw.trim();
if (!trimmed) return false;
if (/^(imessage:|sms:|auto:|chat_id:|chat_guid:|chat_identifier:)/i.test(trimmed)) {
return true;
}
if (trimmed.includes("@")) return true;
return /^\+?\d{3,}$/.test(trimmed);
},
hint: "<handle|chat_id:ID>",
},
normalizeTarget: (raw) => normalizeIMessageHandle(raw),
},
});
export const createOutboundTestPlugin = (params: {
id: ChannelId;
outbound: ChannelOutboundAdapter;
label?: string;
docsPath?: string;
capabilities?: ChannelCapabilities;
}): ChannelPlugin => ({
id: params.id,
meta: {
id: params.id,
label: params.label ?? String(params.id),
selectionLabel: params.label ?? String(params.id),
docsPath: params.docsPath ?? `/channels/${params.id}`,
blurb: "test stub.",
},
capabilities: params.capabilities ?? { chatTypes: ["direct"] },
config: {
listAccountIds: () => [],
resolveAccount: () => ({}),
},
outbound: params.outbound,
});