refactor: route channel runtime via plugin api
This commit is contained in:
@@ -13,6 +13,25 @@ const installRegistry = async () => {
|
||||
const { setActivePluginRegistry } = await import("../../plugins/runtime.js");
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([
|
||||
{
|
||||
pluginId: "discord",
|
||||
source: "test",
|
||||
plugin: {
|
||||
id: "discord",
|
||||
meta: {
|
||||
id: "discord",
|
||||
label: "Discord",
|
||||
selectionLabel: "Discord",
|
||||
docsPath: "/channels/discord",
|
||||
blurb: "Discord test stub.",
|
||||
},
|
||||
capabilities: { chatTypes: ["direct", "channel", "thread"] },
|
||||
config: {
|
||||
listAccountIds: () => ["default"],
|
||||
resolveAccount: () => ({}),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
pluginId: "whatsapp",
|
||||
source: "test",
|
||||
|
||||
@@ -4,9 +4,11 @@ import type { ClawdbotConfig } from "../../config/config.js";
|
||||
import { handleWhatsAppAction } from "./whatsapp-actions.js";
|
||||
|
||||
const sendReactionWhatsApp = vi.fn(async () => undefined);
|
||||
const sendPollWhatsApp = vi.fn(async () => ({ messageId: "poll-1", toJid: "jid-1" }));
|
||||
|
||||
vi.mock("../../web/outbound.js", () => ({
|
||||
sendReactionWhatsApp: (...args: unknown[]) => sendReactionWhatsApp(...args),
|
||||
sendPollWhatsApp: (...args: unknown[]) => sendPollWhatsApp(...args),
|
||||
}));
|
||||
|
||||
const enabledConfig = {
|
||||
|
||||
@@ -1,8 +1,18 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { hasControlCommand, hasInlineCommandTokens } from "./command-detection.js";
|
||||
import { listChatCommands } from "./commands-registry.js";
|
||||
import { parseActivationCommand } from "./group-activation.js";
|
||||
import { parseSendPolicyCommand } from "./send-policy.js";
|
||||
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
||||
import { createTestRegistry } from "../test-utils/channel-plugins.js";
|
||||
|
||||
beforeEach(() => {
|
||||
setActivePluginRegistry(createTestRegistry([]));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
setActivePluginRegistry(createTestRegistry([]));
|
||||
});
|
||||
|
||||
describe("control command parsing", () => {
|
||||
it("requires slash for send policy", () => {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { listChannelDocks } from "../channels/dock.js";
|
||||
import { getActivePluginRegistry } from "../plugins/runtime.js";
|
||||
import { listThinkingLevels } from "./thinking.js";
|
||||
import { COMMAND_ARG_FORMATTERS } from "./commands-args.js";
|
||||
import type { ChatCommandDefinition, CommandScope } from "./commands-registry.types.js";
|
||||
@@ -111,7 +112,12 @@ function assertCommandRegistry(commands: ChatCommandDefinition[]): void {
|
||||
}
|
||||
}
|
||||
|
||||
export const CHAT_COMMANDS: ChatCommandDefinition[] = (() => {
|
||||
let cachedCommands: ChatCommandDefinition[] | null = null;
|
||||
let cachedRegistry: ReturnType<typeof getActivePluginRegistry> | null = null;
|
||||
let cachedNativeCommandSurfaces: Set<string> | null = null;
|
||||
let cachedNativeRegistry: ReturnType<typeof getActivePluginRegistry> | null = null;
|
||||
|
||||
function buildChatCommands(): ChatCommandDefinition[] {
|
||||
const commands: ChatCommandDefinition[] = [
|
||||
defineChatCommand({
|
||||
key: "help",
|
||||
@@ -454,17 +460,28 @@ export const CHAT_COMMANDS: ChatCommandDefinition[] = (() => {
|
||||
|
||||
assertCommandRegistry(commands);
|
||||
return commands;
|
||||
})();
|
||||
}
|
||||
|
||||
let cachedNativeCommandSurfaces: Set<string> | null = null;
|
||||
export function getChatCommands(): ChatCommandDefinition[] {
|
||||
const registry = getActivePluginRegistry();
|
||||
if (cachedCommands && registry === cachedRegistry) return cachedCommands;
|
||||
const commands = buildChatCommands();
|
||||
cachedCommands = commands;
|
||||
cachedRegistry = registry;
|
||||
cachedNativeCommandSurfaces = null;
|
||||
return commands;
|
||||
}
|
||||
|
||||
export const getNativeCommandSurfaces = (): Set<string> => {
|
||||
if (!cachedNativeCommandSurfaces) {
|
||||
cachedNativeCommandSurfaces = new Set(
|
||||
listChannelDocks()
|
||||
.filter((dock) => dock.capabilities.nativeCommands)
|
||||
.map((dock) => dock.id),
|
||||
);
|
||||
export function getNativeCommandSurfaces(): Set<string> {
|
||||
const registry = getActivePluginRegistry();
|
||||
if (cachedNativeCommandSurfaces && registry === cachedNativeRegistry) {
|
||||
return cachedNativeCommandSurfaces;
|
||||
}
|
||||
cachedNativeCommandSurfaces = new Set(
|
||||
listChannelDocks()
|
||||
.filter((dock) => dock.capabilities.nativeCommands)
|
||||
.map((dock) => dock.id),
|
||||
);
|
||||
cachedNativeRegistry = registry;
|
||||
return cachedNativeCommandSurfaces;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
buildCommandText,
|
||||
@@ -10,6 +10,16 @@ import {
|
||||
normalizeCommandBody,
|
||||
shouldHandleTextCommands,
|
||||
} from "./commands-registry.js";
|
||||
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
||||
import { createTestRegistry } from "../test-utils/channel-plugins.js";
|
||||
|
||||
beforeEach(() => {
|
||||
setActivePluginRegistry(createTestRegistry([]));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
setActivePluginRegistry(createTestRegistry([]));
|
||||
});
|
||||
|
||||
describe("commands registry", () => {
|
||||
it("builds command text with args", () => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { ClawdbotConfig } from "../config/types.js";
|
||||
import type { SkillCommandSpec } from "../agents/skills.js";
|
||||
import { CHAT_COMMANDS, getNativeCommandSurfaces } from "./commands-registry.data.js";
|
||||
import { getChatCommands, getNativeCommandSurfaces } from "./commands-registry.data.js";
|
||||
import { DEFAULT_MODEL, DEFAULT_PROVIDER } from "../agents/defaults.js";
|
||||
import { resolveConfiguredModelRef } from "../agents/model-selection.js";
|
||||
import type {
|
||||
@@ -16,7 +16,6 @@ import type {
|
||||
ShouldHandleTextCommandsParams,
|
||||
} from "./commands-registry.types.js";
|
||||
|
||||
export { CHAT_COMMANDS } from "./commands-registry.data.js";
|
||||
export type {
|
||||
ChatCommandDefinition,
|
||||
CommandArgChoiceContext,
|
||||
@@ -37,9 +36,16 @@ type TextAliasSpec = {
|
||||
acceptsArgs: boolean;
|
||||
};
|
||||
|
||||
const TEXT_ALIAS_MAP: Map<string, TextAliasSpec> = (() => {
|
||||
let cachedTextAliasMap: Map<string, TextAliasSpec> | null = null;
|
||||
let cachedTextAliasCommands: ChatCommandDefinition[] | null = null;
|
||||
let cachedDetection: CommandDetection | undefined;
|
||||
let cachedDetectionCommands: ChatCommandDefinition[] | null = null;
|
||||
|
||||
function getTextAliasMap(): Map<string, TextAliasSpec> {
|
||||
const commands = getChatCommands();
|
||||
if (cachedTextAliasMap && cachedTextAliasCommands === commands) return cachedTextAliasMap;
|
||||
const map = new Map<string, TextAliasSpec>();
|
||||
for (const command of CHAT_COMMANDS) {
|
||||
for (const command of commands) {
|
||||
// Canonicalize to the *primary* text alias, not `/${key}`. Some command keys are
|
||||
// internal identifiers (e.g. `dock:telegram`) while the public text command is
|
||||
// the alias (e.g. `/dock-telegram`).
|
||||
@@ -53,10 +59,10 @@ const TEXT_ALIAS_MAP: Map<string, TextAliasSpec> = (() => {
|
||||
}
|
||||
}
|
||||
}
|
||||
cachedTextAliasMap = map;
|
||||
cachedTextAliasCommands = commands;
|
||||
return map;
|
||||
})();
|
||||
|
||||
let cachedDetection: CommandDetection | undefined;
|
||||
}
|
||||
|
||||
function escapeRegExp(value: string) {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
@@ -78,8 +84,9 @@ function buildSkillCommandDefinitions(skillCommands?: SkillCommandSpec[]): ChatC
|
||||
export function listChatCommands(params?: {
|
||||
skillCommands?: SkillCommandSpec[];
|
||||
}): ChatCommandDefinition[] {
|
||||
if (!params?.skillCommands?.length) return [...CHAT_COMMANDS];
|
||||
return [...CHAT_COMMANDS, ...buildSkillCommandDefinitions(params.skillCommands)];
|
||||
const commands = getChatCommands();
|
||||
if (!params?.skillCommands?.length) return [...commands];
|
||||
return [...commands, ...buildSkillCommandDefinitions(params.skillCommands)];
|
||||
}
|
||||
|
||||
export function isCommandEnabled(cfg: ClawdbotConfig, commandKey: string): boolean {
|
||||
@@ -93,7 +100,7 @@ export function listChatCommandsForConfig(
|
||||
cfg: ClawdbotConfig,
|
||||
params?: { skillCommands?: SkillCommandSpec[] },
|
||||
): ChatCommandDefinition[] {
|
||||
const base = CHAT_COMMANDS.filter((command) => isCommandEnabled(cfg, command.key));
|
||||
const base = getChatCommands().filter((command) => isCommandEnabled(cfg, command.key));
|
||||
if (!params?.skillCommands?.length) return base;
|
||||
return [...base, ...buildSkillCommandDefinitions(params.skillCommands)];
|
||||
}
|
||||
@@ -127,7 +134,7 @@ export function listNativeCommandSpecsForConfig(
|
||||
|
||||
export function findCommandByNativeName(name: string): ChatCommandDefinition | undefined {
|
||||
const normalized = name.trim().toLowerCase();
|
||||
return CHAT_COMMANDS.find(
|
||||
return getChatCommands().find(
|
||||
(command) => command.scope !== "text" && command.nativeName?.toLowerCase() === normalized,
|
||||
);
|
||||
}
|
||||
@@ -299,14 +306,15 @@ export function normalizeCommandBody(raw: string, options?: CommandNormalizeOpti
|
||||
: normalized;
|
||||
|
||||
const lowered = commandBody.toLowerCase();
|
||||
const exact = TEXT_ALIAS_MAP.get(lowered);
|
||||
const textAliasMap = getTextAliasMap();
|
||||
const exact = textAliasMap.get(lowered);
|
||||
if (exact) return exact.canonical;
|
||||
|
||||
const tokenMatch = commandBody.match(/^\/([^\s]+)(?:\s+([\s\S]+))?$/);
|
||||
if (!tokenMatch) return commandBody;
|
||||
const [, token, rest] = tokenMatch;
|
||||
const tokenKey = `/${token.toLowerCase()}`;
|
||||
const tokenSpec = TEXT_ALIAS_MAP.get(tokenKey);
|
||||
const tokenSpec = textAliasMap.get(tokenKey);
|
||||
if (!tokenSpec) return commandBody;
|
||||
if (rest && !tokenSpec.acceptsArgs) return commandBody;
|
||||
const normalizedRest = rest?.trimStart();
|
||||
@@ -319,10 +327,11 @@ export function isCommandMessage(raw: string): boolean {
|
||||
}
|
||||
|
||||
export function getCommandDetection(_cfg?: ClawdbotConfig): CommandDetection {
|
||||
if (cachedDetection) return cachedDetection;
|
||||
const commands = getChatCommands();
|
||||
if (cachedDetection && cachedDetectionCommands === commands) return cachedDetection;
|
||||
const exact = new Set<string>();
|
||||
const patterns: string[] = [];
|
||||
for (const cmd of CHAT_COMMANDS) {
|
||||
for (const cmd of commands) {
|
||||
for (const alias of cmd.textAliases) {
|
||||
const normalized = alias.trim().toLowerCase();
|
||||
if (!normalized) continue;
|
||||
@@ -340,6 +349,7 @@ export function getCommandDetection(_cfg?: ClawdbotConfig): CommandDetection {
|
||||
exact,
|
||||
regex: patterns.length ? new RegExp(`^(?:${patterns.join("|")})$`, "i") : /$^/,
|
||||
};
|
||||
cachedDetectionCommands = commands;
|
||||
return cachedDetection;
|
||||
}
|
||||
|
||||
@@ -353,7 +363,7 @@ export function maybeResolveTextAlias(raw: string, cfg?: ClawdbotConfig) {
|
||||
const tokenMatch = normalized.match(/^\/([^\s:]+)(?:\s|$)/);
|
||||
if (!tokenMatch) return null;
|
||||
const tokenKey = `/${tokenMatch[1]}`;
|
||||
return TEXT_ALIAS_MAP.has(tokenKey) ? tokenKey : null;
|
||||
return getTextAliasMap().has(tokenKey) ? tokenKey : null;
|
||||
}
|
||||
|
||||
export function resolveTextCommand(
|
||||
@@ -366,9 +376,9 @@ export function resolveTextCommand(
|
||||
const trimmed = normalizeCommandBody(raw).trim();
|
||||
const alias = maybeResolveTextAlias(trimmed, cfg);
|
||||
if (!alias) return null;
|
||||
const spec = TEXT_ALIAS_MAP.get(alias);
|
||||
const spec = getTextAliasMap().get(alias);
|
||||
if (!spec) return null;
|
||||
const command = CHAT_COMMANDS.find((entry) => entry.key === spec.key);
|
||||
const command = getChatCommands().find((entry) => entry.key === spec.key);
|
||||
if (!command) return null;
|
||||
if (!spec.acceptsArgs) return { command };
|
||||
const args = trimmed.slice(alias.length).trim();
|
||||
|
||||
@@ -48,6 +48,7 @@ vi.mock("../../telegram/send.js", () => ({
|
||||
}));
|
||||
vi.mock("../../web/outbound.js", () => ({
|
||||
sendMessageWhatsApp: mocks.sendMessageWhatsApp,
|
||||
sendPollWhatsApp: mocks.sendMessageWhatsApp,
|
||||
}));
|
||||
vi.mock("../../infra/outbound/deliver.js", async () => {
|
||||
const actual = await vi.importActual<typeof import("../../infra/outbound/deliver.js")>(
|
||||
|
||||
@@ -7,7 +7,7 @@ import { resolveTelegramAccount } from "../telegram/accounts.js";
|
||||
import { normalizeE164 } from "../utils.js";
|
||||
import { resolveWhatsAppAccount } from "../web/accounts.js";
|
||||
import { normalizeWhatsAppTarget } from "../whatsapp/normalize.js";
|
||||
import { getActivePluginRegistry } from "../plugins/runtime.js";
|
||||
import { requireActivePluginRegistry } from "../plugins/runtime.js";
|
||||
import {
|
||||
resolveDiscordGroupRequireMention,
|
||||
resolveIMessageGroupRequireMention,
|
||||
@@ -320,8 +320,7 @@ function buildDockFromPlugin(plugin: ChannelPlugin): ChannelDock {
|
||||
}
|
||||
|
||||
function listPluginDockEntries(): Array<{ id: ChannelId; dock: ChannelDock; order?: number }> {
|
||||
const registry = getActivePluginRegistry();
|
||||
if (!registry) return [];
|
||||
const registry = requireActivePluginRegistry();
|
||||
const entries: Array<{ id: ChannelId; dock: ChannelDock; order?: number }> = [];
|
||||
const seen = new Set<string>();
|
||||
for (const entry of registry.channels) {
|
||||
@@ -358,8 +357,8 @@ export function listChannelDocks(): ChannelDock[] {
|
||||
export function getChannelDock(id: ChannelId): ChannelDock | undefined {
|
||||
const core = DOCKS[id as ChatChannelId];
|
||||
if (core) return core;
|
||||
const registry = getActivePluginRegistry();
|
||||
const pluginEntry = registry?.channels.find((entry) => entry.plugin.id === id);
|
||||
const registry = requireActivePluginRegistry();
|
||||
const pluginEntry = registry.channels.find((entry) => entry.plugin.id === id);
|
||||
if (!pluginEntry) return undefined;
|
||||
return pluginEntry.dock ?? buildDockFromPlugin(pluginEntry.plugin);
|
||||
}
|
||||
|
||||
@@ -1,413 +0,0 @@
|
||||
import {
|
||||
listDiscordAccountIds,
|
||||
type ResolvedDiscordAccount,
|
||||
resolveDefaultDiscordAccountId,
|
||||
resolveDiscordAccount,
|
||||
} from "../../discord/accounts.js";
|
||||
import {
|
||||
auditDiscordChannelPermissions,
|
||||
collectDiscordAuditChannelIds,
|
||||
} from "../../discord/audit.js";
|
||||
import { probeDiscord } from "../../discord/probe.js";
|
||||
import { resolveDiscordChannelAllowlist } from "../../discord/resolve-channels.js";
|
||||
import { resolveDiscordUserAllowlist } from "../../discord/resolve-users.js";
|
||||
import { sendMessageDiscord, sendPollDiscord } from "../../discord/send.js";
|
||||
import { shouldLogVerbose } from "../../globals.js";
|
||||
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../routing/session-key.js";
|
||||
import { getChatChannelMeta } from "../registry.js";
|
||||
import { DiscordConfigSchema } from "../../config/zod-schema.providers-core.js";
|
||||
import { discordMessageActions } from "./actions/discord.js";
|
||||
import { buildChannelConfigSchema } from "./config-schema.js";
|
||||
import {
|
||||
deleteAccountFromConfigSection,
|
||||
setAccountEnabledInConfigSection,
|
||||
} from "./config-helpers.js";
|
||||
import { resolveDiscordGroupRequireMention } from "./group-mentions.js";
|
||||
import { formatPairingApproveHint } from "./helpers.js";
|
||||
import { looksLikeDiscordTargetId, normalizeDiscordMessagingTarget } from "./normalize/discord.js";
|
||||
import { discordOnboardingAdapter } from "./onboarding/discord.js";
|
||||
import { PAIRING_APPROVED_MESSAGE } from "./pairing-message.js";
|
||||
import {
|
||||
applyAccountNameToChannelSection,
|
||||
migrateBaseNameToDefaultAccount,
|
||||
} from "./setup-helpers.js";
|
||||
import { collectDiscordStatusIssues } from "./status-issues/discord.js";
|
||||
import type { ChannelPlugin } from "./types.js";
|
||||
import {
|
||||
listDiscordDirectoryGroupsFromConfig,
|
||||
listDiscordDirectoryPeersFromConfig,
|
||||
} from "./directory-config.js";
|
||||
import {
|
||||
listDiscordDirectoryGroupsLive,
|
||||
listDiscordDirectoryPeersLive,
|
||||
} from "../../discord/directory-live.js";
|
||||
|
||||
const meta = getChatChannelMeta("discord");
|
||||
|
||||
export const discordPlugin: ChannelPlugin<ResolvedDiscordAccount> = {
|
||||
id: "discord",
|
||||
meta: {
|
||||
...meta,
|
||||
},
|
||||
onboarding: discordOnboardingAdapter,
|
||||
pairing: {
|
||||
idLabel: "discordUserId",
|
||||
normalizeAllowEntry: (entry) => entry.replace(/^(discord|user):/i, ""),
|
||||
notifyApproval: async ({ id }) => {
|
||||
await sendMessageDiscord(`user:${id}`, PAIRING_APPROVED_MESSAGE);
|
||||
},
|
||||
},
|
||||
capabilities: {
|
||||
chatTypes: ["direct", "channel", "thread"],
|
||||
polls: true,
|
||||
reactions: true,
|
||||
threads: true,
|
||||
media: true,
|
||||
nativeCommands: true,
|
||||
},
|
||||
streaming: {
|
||||
blockStreamingCoalesceDefaults: { minChars: 1500, idleMs: 1000 },
|
||||
},
|
||||
reload: { configPrefixes: ["channels.discord"] },
|
||||
configSchema: buildChannelConfigSchema(DiscordConfigSchema),
|
||||
config: {
|
||||
listAccountIds: (cfg) => listDiscordAccountIds(cfg),
|
||||
resolveAccount: (cfg, accountId) => resolveDiscordAccount({ cfg, accountId }),
|
||||
defaultAccountId: (cfg) => resolveDefaultDiscordAccountId(cfg),
|
||||
setAccountEnabled: ({ cfg, accountId, enabled }) =>
|
||||
setAccountEnabledInConfigSection({
|
||||
cfg,
|
||||
sectionKey: "discord",
|
||||
accountId,
|
||||
enabled,
|
||||
allowTopLevel: true,
|
||||
}),
|
||||
deleteAccount: ({ cfg, accountId }) =>
|
||||
deleteAccountFromConfigSection({
|
||||
cfg,
|
||||
sectionKey: "discord",
|
||||
accountId,
|
||||
clearBaseFields: ["token", "name"],
|
||||
}),
|
||||
isConfigured: (account) => Boolean(account.token?.trim()),
|
||||
describeAccount: (account) => ({
|
||||
accountId: account.accountId,
|
||||
name: account.name,
|
||||
enabled: account.enabled,
|
||||
configured: Boolean(account.token?.trim()),
|
||||
tokenSource: account.tokenSource,
|
||||
}),
|
||||
resolveAllowFrom: ({ cfg, accountId }) =>
|
||||
(resolveDiscordAccount({ cfg, accountId }).config.dm?.allowFrom ?? []).map((entry) =>
|
||||
String(entry),
|
||||
),
|
||||
formatAllowFrom: ({ allowFrom }) =>
|
||||
allowFrom
|
||||
.map((entry) => String(entry).trim())
|
||||
.filter(Boolean)
|
||||
.map((entry) => entry.toLowerCase()),
|
||||
},
|
||||
security: {
|
||||
resolveDmPolicy: ({ cfg, accountId, account }) => {
|
||||
const resolvedAccountId = accountId ?? account.accountId ?? DEFAULT_ACCOUNT_ID;
|
||||
const useAccountPath = Boolean(cfg.channels?.discord?.accounts?.[resolvedAccountId]);
|
||||
const allowFromPath = useAccountPath
|
||||
? `channels.discord.accounts.${resolvedAccountId}.dm.`
|
||||
: "channels.discord.dm.";
|
||||
return {
|
||||
policy: account.config.dm?.policy ?? "pairing",
|
||||
allowFrom: account.config.dm?.allowFrom ?? [],
|
||||
allowFromPath,
|
||||
approveHint: formatPairingApproveHint("discord"),
|
||||
normalizeEntry: (raw) => raw.replace(/^(discord|user):/i, "").replace(/^<@!?(\d+)>$/, "$1"),
|
||||
};
|
||||
},
|
||||
collectWarnings: ({ account, cfg }) => {
|
||||
const warnings: string[] = [];
|
||||
const defaultGroupPolicy = cfg.channels?.defaults?.groupPolicy;
|
||||
const groupPolicy = account.config.groupPolicy ?? defaultGroupPolicy ?? "open";
|
||||
const guildEntries = account.config.guilds ?? {};
|
||||
const guildsConfigured = Object.keys(guildEntries).length > 0;
|
||||
const channelAllowlistConfigured = guildsConfigured;
|
||||
|
||||
if (groupPolicy === "open") {
|
||||
if (channelAllowlistConfigured) {
|
||||
warnings.push(
|
||||
`- Discord guilds: groupPolicy="open" allows any channel not explicitly denied to trigger (mention-gated). Set channels.discord.groupPolicy="allowlist" and configure channels.discord.guilds.<id>.channels.`,
|
||||
);
|
||||
} else {
|
||||
warnings.push(
|
||||
`- Discord guilds: groupPolicy="open" with no guild/channel allowlist; any channel can trigger (mention-gated). Set channels.discord.groupPolicy="allowlist" and configure channels.discord.guilds.<id>.channels.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return warnings;
|
||||
},
|
||||
},
|
||||
groups: {
|
||||
resolveRequireMention: resolveDiscordGroupRequireMention,
|
||||
},
|
||||
mentions: {
|
||||
stripPatterns: () => ["<@!?\\d+>"],
|
||||
},
|
||||
threading: {
|
||||
resolveReplyToMode: ({ cfg }) => cfg.channels?.discord?.replyToMode ?? "off",
|
||||
},
|
||||
messaging: {
|
||||
normalizeTarget: normalizeDiscordMessagingTarget,
|
||||
targetResolver: {
|
||||
looksLikeId: looksLikeDiscordTargetId,
|
||||
hint: "<channelId|user:ID|channel:ID>",
|
||||
},
|
||||
},
|
||||
directory: {
|
||||
self: async () => null,
|
||||
listPeers: async (params) => listDiscordDirectoryPeersFromConfig(params),
|
||||
listGroups: async (params) => listDiscordDirectoryGroupsFromConfig(params),
|
||||
listPeersLive: async (params) => listDiscordDirectoryPeersLive(params),
|
||||
listGroupsLive: async (params) => listDiscordDirectoryGroupsLive(params),
|
||||
},
|
||||
resolver: {
|
||||
resolveTargets: async ({ cfg, accountId, inputs, kind }) => {
|
||||
const account = resolveDiscordAccount({ cfg, accountId });
|
||||
const token = account.token?.trim();
|
||||
if (!token) {
|
||||
return inputs.map((input) => ({
|
||||
input,
|
||||
resolved: false,
|
||||
note: "missing Discord token",
|
||||
}));
|
||||
}
|
||||
if (kind === "group") {
|
||||
const resolved = await resolveDiscordChannelAllowlist({ token, entries: inputs });
|
||||
return resolved.map((entry) => ({
|
||||
input: entry.input,
|
||||
resolved: entry.resolved,
|
||||
id: entry.channelId ?? entry.guildId,
|
||||
name:
|
||||
entry.channelName ??
|
||||
entry.guildName ??
|
||||
(entry.guildId && !entry.channelId ? entry.guildId : undefined),
|
||||
note: entry.note,
|
||||
}));
|
||||
}
|
||||
const resolved = await resolveDiscordUserAllowlist({ token, entries: inputs });
|
||||
return resolved.map((entry) => ({
|
||||
input: entry.input,
|
||||
resolved: entry.resolved,
|
||||
id: entry.id,
|
||||
name: entry.name,
|
||||
note: entry.note,
|
||||
}));
|
||||
},
|
||||
},
|
||||
actions: discordMessageActions,
|
||||
setup: {
|
||||
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
|
||||
applyAccountName: ({ cfg, accountId, name }) =>
|
||||
applyAccountNameToChannelSection({
|
||||
cfg,
|
||||
channelKey: "discord",
|
||||
accountId,
|
||||
name,
|
||||
}),
|
||||
validateInput: ({ accountId, input }) => {
|
||||
if (input.useEnv && accountId !== DEFAULT_ACCOUNT_ID) {
|
||||
return "DISCORD_BOT_TOKEN can only be used for the default account.";
|
||||
}
|
||||
if (!input.useEnv && !input.token) {
|
||||
return "Discord requires token (or --use-env).";
|
||||
}
|
||||
return null;
|
||||
},
|
||||
applyAccountConfig: ({ cfg, accountId, input }) => {
|
||||
const namedConfig = applyAccountNameToChannelSection({
|
||||
cfg,
|
||||
channelKey: "discord",
|
||||
accountId,
|
||||
name: input.name,
|
||||
});
|
||||
const next =
|
||||
accountId !== DEFAULT_ACCOUNT_ID
|
||||
? migrateBaseNameToDefaultAccount({
|
||||
cfg: namedConfig,
|
||||
channelKey: "discord",
|
||||
})
|
||||
: namedConfig;
|
||||
if (accountId === DEFAULT_ACCOUNT_ID) {
|
||||
return {
|
||||
...next,
|
||||
channels: {
|
||||
...next.channels,
|
||||
discord: {
|
||||
...next.channels?.discord,
|
||||
enabled: true,
|
||||
...(input.useEnv ? {} : input.token ? { token: input.token } : {}),
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...next,
|
||||
channels: {
|
||||
...next.channels,
|
||||
discord: {
|
||||
...next.channels?.discord,
|
||||
enabled: true,
|
||||
accounts: {
|
||||
...next.channels?.discord?.accounts,
|
||||
[accountId]: {
|
||||
...next.channels?.discord?.accounts?.[accountId],
|
||||
enabled: true,
|
||||
...(input.token ? { token: input.token } : {}),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
outbound: {
|
||||
deliveryMode: "direct",
|
||||
chunker: null,
|
||||
textChunkLimit: 2000,
|
||||
pollMaxOptions: 10,
|
||||
sendText: async ({ to, text, accountId, deps, replyToId }) => {
|
||||
const send = deps?.sendDiscord ?? sendMessageDiscord;
|
||||
const result = await send(to, text, {
|
||||
verbose: false,
|
||||
replyTo: replyToId ?? undefined,
|
||||
accountId: accountId ?? undefined,
|
||||
});
|
||||
return { channel: "discord", ...result };
|
||||
},
|
||||
sendMedia: async ({ to, text, mediaUrl, accountId, deps, replyToId }) => {
|
||||
const send = deps?.sendDiscord ?? sendMessageDiscord;
|
||||
const result = await send(to, text, {
|
||||
verbose: false,
|
||||
mediaUrl,
|
||||
replyTo: replyToId ?? undefined,
|
||||
accountId: accountId ?? undefined,
|
||||
});
|
||||
return { channel: "discord", ...result };
|
||||
},
|
||||
sendPoll: async ({ to, poll, accountId }) =>
|
||||
await sendPollDiscord(to, poll, {
|
||||
accountId: accountId ?? undefined,
|
||||
}),
|
||||
},
|
||||
status: {
|
||||
defaultRuntime: {
|
||||
accountId: DEFAULT_ACCOUNT_ID,
|
||||
running: false,
|
||||
lastStartAt: null,
|
||||
lastStopAt: null,
|
||||
lastError: null,
|
||||
},
|
||||
collectStatusIssues: collectDiscordStatusIssues,
|
||||
buildChannelSummary: ({ snapshot }) => ({
|
||||
configured: snapshot.configured ?? false,
|
||||
tokenSource: snapshot.tokenSource ?? "none",
|
||||
running: snapshot.running ?? false,
|
||||
lastStartAt: snapshot.lastStartAt ?? null,
|
||||
lastStopAt: snapshot.lastStopAt ?? null,
|
||||
lastError: snapshot.lastError ?? null,
|
||||
probe: snapshot.probe,
|
||||
lastProbeAt: snapshot.lastProbeAt ?? null,
|
||||
}),
|
||||
probeAccount: async ({ account, timeoutMs }) =>
|
||||
probeDiscord(account.token, timeoutMs, { includeApplication: true }),
|
||||
auditAccount: async ({ account, timeoutMs, cfg }) => {
|
||||
const { channelIds, unresolvedChannels } = collectDiscordAuditChannelIds({
|
||||
cfg,
|
||||
accountId: account.accountId,
|
||||
});
|
||||
if (!channelIds.length && unresolvedChannels === 0) return undefined;
|
||||
const botToken = account.token?.trim();
|
||||
if (!botToken) {
|
||||
return {
|
||||
ok: unresolvedChannels === 0,
|
||||
checkedChannels: 0,
|
||||
unresolvedChannels,
|
||||
channels: [],
|
||||
elapsedMs: 0,
|
||||
};
|
||||
}
|
||||
const audit = await auditDiscordChannelPermissions({
|
||||
token: botToken,
|
||||
accountId: account.accountId,
|
||||
channelIds,
|
||||
timeoutMs,
|
||||
});
|
||||
return { ...audit, unresolvedChannels };
|
||||
},
|
||||
buildAccountSnapshot: ({ account, runtime, probe, audit }) => {
|
||||
const configured = Boolean(account.token?.trim());
|
||||
const app = runtime?.application ?? (probe as { application?: unknown })?.application;
|
||||
const bot = runtime?.bot ?? (probe as { bot?: unknown })?.bot;
|
||||
return {
|
||||
accountId: account.accountId,
|
||||
name: account.name,
|
||||
enabled: account.enabled,
|
||||
configured,
|
||||
tokenSource: account.tokenSource,
|
||||
running: runtime?.running ?? false,
|
||||
lastStartAt: runtime?.lastStartAt ?? null,
|
||||
lastStopAt: runtime?.lastStopAt ?? null,
|
||||
lastError: runtime?.lastError ?? null,
|
||||
application: app ?? undefined,
|
||||
bot: bot ?? undefined,
|
||||
probe,
|
||||
audit,
|
||||
lastInboundAt: runtime?.lastInboundAt ?? null,
|
||||
lastOutboundAt: runtime?.lastOutboundAt ?? null,
|
||||
};
|
||||
},
|
||||
},
|
||||
gateway: {
|
||||
startAccount: async (ctx) => {
|
||||
const account = ctx.account;
|
||||
const token = account.token.trim();
|
||||
let discordBotLabel = "";
|
||||
try {
|
||||
const probe = await probeDiscord(token, 2500, {
|
||||
includeApplication: true,
|
||||
});
|
||||
const username = probe.ok ? probe.bot?.username?.trim() : null;
|
||||
if (username) discordBotLabel = ` (@${username})`;
|
||||
ctx.setStatus({
|
||||
accountId: account.accountId,
|
||||
bot: probe.bot,
|
||||
application: probe.application,
|
||||
});
|
||||
const messageContent = probe.application?.intents?.messageContent;
|
||||
if (messageContent === "disabled") {
|
||||
ctx.log?.warn(
|
||||
`[${account.accountId}] Discord Message Content Intent is disabled; bot may not respond to channel messages. Enable it in Discord Dev Portal (Bot → Privileged Gateway Intents) or require mentions.`,
|
||||
);
|
||||
} else if (messageContent === "limited") {
|
||||
ctx.log?.info(
|
||||
`[${account.accountId}] Discord Message Content Intent is limited; bots under 100 servers can use it without verification.`,
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
if (shouldLogVerbose()) {
|
||||
ctx.log?.debug?.(`[${account.accountId}] bot probe failed: ${String(err)}`);
|
||||
}
|
||||
}
|
||||
ctx.log?.info(`[${account.accountId}] starting provider${discordBotLabel}`);
|
||||
// Lazy import: the monitor pulls the reply pipeline; avoid ESM init cycles.
|
||||
const { monitorDiscordProvider } = await import("../../discord/index.js");
|
||||
return monitorDiscordProvider({
|
||||
token,
|
||||
accountId: account.accountId,
|
||||
config: ctx.cfg,
|
||||
runtime: ctx.runtime,
|
||||
abortSignal: ctx.abortSignal,
|
||||
mediaMaxMb: account.config.mediaMaxMb,
|
||||
historyLimit: account.config.historyLimit,
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -1,295 +0,0 @@
|
||||
import { chunkText } from "../../auto-reply/chunk.js";
|
||||
import {
|
||||
listIMessageAccountIds,
|
||||
type ResolvedIMessageAccount,
|
||||
resolveDefaultIMessageAccountId,
|
||||
resolveIMessageAccount,
|
||||
} from "../../imessage/accounts.js";
|
||||
import { probeIMessage } from "../../imessage/probe.js";
|
||||
import { sendMessageIMessage } from "../../imessage/send.js";
|
||||
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../routing/session-key.js";
|
||||
import { getChatChannelMeta } from "../registry.js";
|
||||
import { IMessageConfigSchema } from "../../config/zod-schema.providers-core.js";
|
||||
import { buildChannelConfigSchema } from "./config-schema.js";
|
||||
import {
|
||||
deleteAccountFromConfigSection,
|
||||
setAccountEnabledInConfigSection,
|
||||
} from "./config-helpers.js";
|
||||
import { resolveIMessageGroupRequireMention } from "./group-mentions.js";
|
||||
import { formatPairingApproveHint } from "./helpers.js";
|
||||
import { resolveChannelMediaMaxBytes } from "./media-limits.js";
|
||||
import { imessageOnboardingAdapter } from "./onboarding/imessage.js";
|
||||
import { PAIRING_APPROVED_MESSAGE } from "./pairing-message.js";
|
||||
import {
|
||||
applyAccountNameToChannelSection,
|
||||
migrateBaseNameToDefaultAccount,
|
||||
} from "./setup-helpers.js";
|
||||
import type { ChannelPlugin } from "./types.js";
|
||||
|
||||
const meta = getChatChannelMeta("imessage");
|
||||
|
||||
export const imessagePlugin: ChannelPlugin<ResolvedIMessageAccount> = {
|
||||
id: "imessage",
|
||||
meta: {
|
||||
...meta,
|
||||
showConfigured: false,
|
||||
},
|
||||
onboarding: imessageOnboardingAdapter,
|
||||
pairing: {
|
||||
idLabel: "imessageSenderId",
|
||||
notifyApproval: async ({ id }) => {
|
||||
await sendMessageIMessage(id, PAIRING_APPROVED_MESSAGE);
|
||||
},
|
||||
},
|
||||
capabilities: {
|
||||
chatTypes: ["direct", "group"],
|
||||
media: true,
|
||||
},
|
||||
reload: { configPrefixes: ["channels.imessage"] },
|
||||
configSchema: buildChannelConfigSchema(IMessageConfigSchema),
|
||||
config: {
|
||||
listAccountIds: (cfg) => listIMessageAccountIds(cfg),
|
||||
resolveAccount: (cfg, accountId) => resolveIMessageAccount({ cfg, accountId }),
|
||||
defaultAccountId: (cfg) => resolveDefaultIMessageAccountId(cfg),
|
||||
setAccountEnabled: ({ cfg, accountId, enabled }) =>
|
||||
setAccountEnabledInConfigSection({
|
||||
cfg,
|
||||
sectionKey: "imessage",
|
||||
accountId,
|
||||
enabled,
|
||||
allowTopLevel: true,
|
||||
}),
|
||||
deleteAccount: ({ cfg, accountId }) =>
|
||||
deleteAccountFromConfigSection({
|
||||
cfg,
|
||||
sectionKey: "imessage",
|
||||
accountId,
|
||||
clearBaseFields: ["cliPath", "dbPath", "service", "region", "name"],
|
||||
}),
|
||||
isConfigured: (account) => account.configured,
|
||||
describeAccount: (account) => ({
|
||||
accountId: account.accountId,
|
||||
name: account.name,
|
||||
enabled: account.enabled,
|
||||
configured: account.configured,
|
||||
}),
|
||||
resolveAllowFrom: ({ cfg, accountId }) =>
|
||||
(resolveIMessageAccount({ cfg, accountId }).config.allowFrom ?? []).map((entry) =>
|
||||
String(entry),
|
||||
),
|
||||
formatAllowFrom: ({ allowFrom }) =>
|
||||
allowFrom.map((entry) => String(entry).trim()).filter(Boolean),
|
||||
},
|
||||
security: {
|
||||
resolveDmPolicy: ({ cfg, accountId, account }) => {
|
||||
const resolvedAccountId = accountId ?? account.accountId ?? DEFAULT_ACCOUNT_ID;
|
||||
const useAccountPath = Boolean(cfg.channels?.imessage?.accounts?.[resolvedAccountId]);
|
||||
const basePath = useAccountPath
|
||||
? `channels.imessage.accounts.${resolvedAccountId}.`
|
||||
: "channels.imessage.";
|
||||
return {
|
||||
policy: account.config.dmPolicy ?? "pairing",
|
||||
allowFrom: account.config.allowFrom ?? [],
|
||||
policyPath: `${basePath}dmPolicy`,
|
||||
allowFromPath: basePath,
|
||||
approveHint: formatPairingApproveHint("imessage"),
|
||||
};
|
||||
},
|
||||
collectWarnings: ({ account, cfg }) => {
|
||||
const defaultGroupPolicy = cfg.channels?.defaults?.groupPolicy;
|
||||
const groupPolicy = account.config.groupPolicy ?? defaultGroupPolicy ?? "allowlist";
|
||||
if (groupPolicy !== "open") return [];
|
||||
return [
|
||||
`- iMessage groups: groupPolicy="open" allows any member to trigger the bot. Set channels.imessage.groupPolicy="allowlist" + channels.imessage.groupAllowFrom to restrict senders.`,
|
||||
];
|
||||
},
|
||||
},
|
||||
groups: {
|
||||
resolveRequireMention: resolveIMessageGroupRequireMention,
|
||||
},
|
||||
messaging: {
|
||||
targetResolver: {
|
||||
looksLikeId: (raw) => {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return false;
|
||||
if (/^(imessage:|chat_id:)/i.test(trimmed)) return true;
|
||||
if (trimmed.includes("@")) return true;
|
||||
return /^\+?\d{3,}$/.test(trimmed);
|
||||
},
|
||||
hint: "<handle|chat_id:ID>",
|
||||
},
|
||||
},
|
||||
setup: {
|
||||
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
|
||||
applyAccountName: ({ cfg, accountId, name }) =>
|
||||
applyAccountNameToChannelSection({
|
||||
cfg,
|
||||
channelKey: "imessage",
|
||||
accountId,
|
||||
name,
|
||||
}),
|
||||
applyAccountConfig: ({ cfg, accountId, input }) => {
|
||||
const namedConfig = applyAccountNameToChannelSection({
|
||||
cfg,
|
||||
channelKey: "imessage",
|
||||
accountId,
|
||||
name: input.name,
|
||||
});
|
||||
const next =
|
||||
accountId !== DEFAULT_ACCOUNT_ID
|
||||
? migrateBaseNameToDefaultAccount({
|
||||
cfg: namedConfig,
|
||||
channelKey: "imessage",
|
||||
})
|
||||
: namedConfig;
|
||||
if (accountId === DEFAULT_ACCOUNT_ID) {
|
||||
return {
|
||||
...next,
|
||||
channels: {
|
||||
...next.channels,
|
||||
imessage: {
|
||||
...next.channels?.imessage,
|
||||
enabled: true,
|
||||
...(input.cliPath ? { cliPath: input.cliPath } : {}),
|
||||
...(input.dbPath ? { dbPath: input.dbPath } : {}),
|
||||
...(input.service ? { service: input.service } : {}),
|
||||
...(input.region ? { region: input.region } : {}),
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...next,
|
||||
channels: {
|
||||
...next.channels,
|
||||
imessage: {
|
||||
...next.channels?.imessage,
|
||||
enabled: true,
|
||||
accounts: {
|
||||
...next.channels?.imessage?.accounts,
|
||||
[accountId]: {
|
||||
...next.channels?.imessage?.accounts?.[accountId],
|
||||
enabled: true,
|
||||
...(input.cliPath ? { cliPath: input.cliPath } : {}),
|
||||
...(input.dbPath ? { dbPath: input.dbPath } : {}),
|
||||
...(input.service ? { service: input.service } : {}),
|
||||
...(input.region ? { region: input.region } : {}),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
outbound: {
|
||||
deliveryMode: "direct",
|
||||
chunker: chunkText,
|
||||
textChunkLimit: 4000,
|
||||
sendText: async ({ cfg, to, text, accountId, deps }) => {
|
||||
const send = deps?.sendIMessage ?? sendMessageIMessage;
|
||||
const maxBytes = resolveChannelMediaMaxBytes({
|
||||
cfg,
|
||||
resolveChannelLimitMb: ({ cfg, accountId }) =>
|
||||
cfg.channels?.imessage?.accounts?.[accountId]?.mediaMaxMb ??
|
||||
cfg.channels?.imessage?.mediaMaxMb,
|
||||
accountId,
|
||||
});
|
||||
const result = await send(to, text, {
|
||||
maxBytes,
|
||||
accountId: accountId ?? undefined,
|
||||
});
|
||||
return { channel: "imessage", ...result };
|
||||
},
|
||||
sendMedia: async ({ cfg, to, text, mediaUrl, accountId, deps }) => {
|
||||
const send = deps?.sendIMessage ?? sendMessageIMessage;
|
||||
const maxBytes = resolveChannelMediaMaxBytes({
|
||||
cfg,
|
||||
resolveChannelLimitMb: ({ cfg, accountId }) =>
|
||||
cfg.channels?.imessage?.accounts?.[accountId]?.mediaMaxMb ??
|
||||
cfg.channels?.imessage?.mediaMaxMb,
|
||||
accountId,
|
||||
});
|
||||
const result = await send(to, text, {
|
||||
mediaUrl,
|
||||
maxBytes,
|
||||
accountId: accountId ?? undefined,
|
||||
});
|
||||
return { channel: "imessage", ...result };
|
||||
},
|
||||
},
|
||||
status: {
|
||||
defaultRuntime: {
|
||||
accountId: DEFAULT_ACCOUNT_ID,
|
||||
running: false,
|
||||
lastStartAt: null,
|
||||
lastStopAt: null,
|
||||
lastError: null,
|
||||
cliPath: null,
|
||||
dbPath: null,
|
||||
},
|
||||
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}`,
|
||||
},
|
||||
];
|
||||
}),
|
||||
buildChannelSummary: ({ snapshot }) => ({
|
||||
configured: snapshot.configured ?? false,
|
||||
running: snapshot.running ?? false,
|
||||
lastStartAt: snapshot.lastStartAt ?? null,
|
||||
lastStopAt: snapshot.lastStopAt ?? null,
|
||||
lastError: snapshot.lastError ?? null,
|
||||
cliPath: snapshot.cliPath ?? null,
|
||||
dbPath: snapshot.dbPath ?? null,
|
||||
probe: snapshot.probe,
|
||||
lastProbeAt: snapshot.lastProbeAt ?? null,
|
||||
}),
|
||||
probeAccount: async ({ timeoutMs }) => probeIMessage(timeoutMs),
|
||||
buildAccountSnapshot: ({ account, runtime, probe }) => ({
|
||||
accountId: account.accountId,
|
||||
name: account.name,
|
||||
enabled: account.enabled,
|
||||
configured: account.configured,
|
||||
running: runtime?.running ?? false,
|
||||
lastStartAt: runtime?.lastStartAt ?? null,
|
||||
lastStopAt: runtime?.lastStopAt ?? null,
|
||||
lastError: runtime?.lastError ?? null,
|
||||
cliPath: runtime?.cliPath ?? account.config.cliPath ?? null,
|
||||
dbPath: runtime?.dbPath ?? account.config.dbPath ?? null,
|
||||
probe,
|
||||
lastInboundAt: runtime?.lastInboundAt ?? null,
|
||||
lastOutboundAt: runtime?.lastOutboundAt ?? null,
|
||||
}),
|
||||
resolveAccountState: ({ enabled }) => (enabled ? "enabled" : "disabled"),
|
||||
},
|
||||
gateway: {
|
||||
startAccount: async (ctx) => {
|
||||
const account = ctx.account;
|
||||
const cliPath = account.config.cliPath?.trim() || "imsg";
|
||||
const dbPath = account.config.dbPath?.trim();
|
||||
ctx.setStatus({
|
||||
accountId: account.accountId,
|
||||
cliPath,
|
||||
dbPath: dbPath ?? null,
|
||||
});
|
||||
ctx.log?.info(
|
||||
`[${account.accountId}] starting provider (${cliPath}${dbPath ? ` db=${dbPath}` : ""})`,
|
||||
);
|
||||
// Lazy import: the monitor pulls the reply pipeline; avoid ESM init cycles.
|
||||
const { monitorIMessageProvider } = await import("../../imessage/index.js");
|
||||
return monitorIMessageProvider({
|
||||
accountId: account.accountId,
|
||||
config: ctx.cfg,
|
||||
runtime: ctx.runtime,
|
||||
abortSignal: ctx.abortSignal,
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
import { CHAT_CHANNEL_ORDER, type ChatChannelId, normalizeChatChannelId } from "../registry.js";
|
||||
import { CHAT_CHANNEL_ORDER, type ChatChannelId, normalizeAnyChannelId } from "../registry.js";
|
||||
import type { ChannelId, ChannelPlugin } from "./types.js";
|
||||
import { getActivePluginRegistry } from "../../plugins/runtime.js";
|
||||
import { requireActivePluginRegistry } from "../../plugins/runtime.js";
|
||||
|
||||
// Channel plugins registry (runtime).
|
||||
//
|
||||
@@ -10,8 +10,7 @@ import { getActivePluginRegistry } from "../../plugins/runtime.js";
|
||||
//
|
||||
// Channel plugins are registered by the plugin loader (extensions/ or configured paths).
|
||||
function listPluginChannels(): ChannelPlugin[] {
|
||||
const registry = getActivePluginRegistry();
|
||||
if (!registry) return [];
|
||||
const registry = requireActivePluginRegistry();
|
||||
return registry.channels.map((entry) => entry.plugin);
|
||||
}
|
||||
|
||||
@@ -46,18 +45,9 @@ export function getChannelPlugin(id: ChannelId): ChannelPlugin | undefined {
|
||||
}
|
||||
|
||||
export function normalizeChannelId(raw?: string | null): ChannelId | null {
|
||||
// Channel docking: keep input normalization centralized in src/channels/registry.ts
|
||||
// so CLI/API/protocol can rely on stable aliases without plugin init side effects.
|
||||
const normalized = normalizeChatChannelId(raw);
|
||||
if (normalized) return normalized;
|
||||
const trimmed = raw?.trim();
|
||||
if (!trimmed) return null;
|
||||
const key = trimmed.toLowerCase();
|
||||
const plugin = listChannelPlugins().find((entry) => {
|
||||
if (entry.id.toLowerCase() === key) return true;
|
||||
return (entry.meta.aliases ?? []).some((alias) => alias.trim().toLowerCase() === key);
|
||||
});
|
||||
return plugin?.id ?? null;
|
||||
// Channel docking: keep input normalization centralized in src/channels/registry.ts.
|
||||
// Plugin registry must be initialized before calling.
|
||||
return normalizeAnyChannelId(raw);
|
||||
}
|
||||
export {
|
||||
listDiscordDirectoryGroupsFromConfig,
|
||||
|
||||
@@ -1,305 +0,0 @@
|
||||
import { chunkText } from "../../auto-reply/chunk.js";
|
||||
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../routing/session-key.js";
|
||||
import {
|
||||
listSignalAccountIds,
|
||||
type ResolvedSignalAccount,
|
||||
resolveDefaultSignalAccountId,
|
||||
resolveSignalAccount,
|
||||
} from "../../signal/accounts.js";
|
||||
import { probeSignal } from "../../signal/probe.js";
|
||||
import { sendMessageSignal } from "../../signal/send.js";
|
||||
import { normalizeE164 } from "../../utils.js";
|
||||
import { getChatChannelMeta } from "../registry.js";
|
||||
import { SignalConfigSchema } from "../../config/zod-schema.providers-core.js";
|
||||
import { buildChannelConfigSchema } from "./config-schema.js";
|
||||
import {
|
||||
deleteAccountFromConfigSection,
|
||||
setAccountEnabledInConfigSection,
|
||||
} from "./config-helpers.js";
|
||||
import { formatPairingApproveHint } from "./helpers.js";
|
||||
import { resolveChannelMediaMaxBytes } from "./media-limits.js";
|
||||
import { looksLikeSignalTargetId, normalizeSignalMessagingTarget } from "./normalize/signal.js";
|
||||
import { signalOnboardingAdapter } from "./onboarding/signal.js";
|
||||
import { PAIRING_APPROVED_MESSAGE } from "./pairing-message.js";
|
||||
import {
|
||||
applyAccountNameToChannelSection,
|
||||
migrateBaseNameToDefaultAccount,
|
||||
} from "./setup-helpers.js";
|
||||
import type { ChannelPlugin } from "./types.js";
|
||||
|
||||
const meta = getChatChannelMeta("signal");
|
||||
|
||||
export const signalPlugin: ChannelPlugin<ResolvedSignalAccount> = {
|
||||
id: "signal",
|
||||
meta: {
|
||||
...meta,
|
||||
},
|
||||
onboarding: signalOnboardingAdapter,
|
||||
pairing: {
|
||||
idLabel: "signalNumber",
|
||||
normalizeAllowEntry: (entry) => entry.replace(/^signal:/i, ""),
|
||||
notifyApproval: async ({ id }) => {
|
||||
await sendMessageSignal(id, PAIRING_APPROVED_MESSAGE);
|
||||
},
|
||||
},
|
||||
capabilities: {
|
||||
chatTypes: ["direct", "group"],
|
||||
media: true,
|
||||
},
|
||||
streaming: {
|
||||
blockStreamingCoalesceDefaults: { minChars: 1500, idleMs: 1000 },
|
||||
},
|
||||
reload: { configPrefixes: ["channels.signal"] },
|
||||
configSchema: buildChannelConfigSchema(SignalConfigSchema),
|
||||
config: {
|
||||
listAccountIds: (cfg) => listSignalAccountIds(cfg),
|
||||
resolveAccount: (cfg, accountId) => resolveSignalAccount({ cfg, accountId }),
|
||||
defaultAccountId: (cfg) => resolveDefaultSignalAccountId(cfg),
|
||||
setAccountEnabled: ({ cfg, accountId, enabled }) =>
|
||||
setAccountEnabledInConfigSection({
|
||||
cfg,
|
||||
sectionKey: "signal",
|
||||
accountId,
|
||||
enabled,
|
||||
allowTopLevel: true,
|
||||
}),
|
||||
deleteAccount: ({ cfg, accountId }) =>
|
||||
deleteAccountFromConfigSection({
|
||||
cfg,
|
||||
sectionKey: "signal",
|
||||
accountId,
|
||||
clearBaseFields: ["account", "httpUrl", "httpHost", "httpPort", "cliPath", "name"],
|
||||
}),
|
||||
isConfigured: (account) => account.configured,
|
||||
describeAccount: (account) => ({
|
||||
accountId: account.accountId,
|
||||
name: account.name,
|
||||
enabled: account.enabled,
|
||||
configured: account.configured,
|
||||
baseUrl: account.baseUrl,
|
||||
}),
|
||||
resolveAllowFrom: ({ cfg, accountId }) =>
|
||||
(resolveSignalAccount({ cfg, accountId }).config.allowFrom ?? []).map((entry) =>
|
||||
String(entry),
|
||||
),
|
||||
formatAllowFrom: ({ allowFrom }) =>
|
||||
allowFrom
|
||||
.map((entry) => String(entry).trim())
|
||||
.filter(Boolean)
|
||||
.map((entry) => (entry === "*" ? "*" : normalizeE164(entry.replace(/^signal:/i, ""))))
|
||||
.filter(Boolean),
|
||||
},
|
||||
security: {
|
||||
resolveDmPolicy: ({ cfg, accountId, account }) => {
|
||||
const resolvedAccountId = accountId ?? account.accountId ?? DEFAULT_ACCOUNT_ID;
|
||||
const useAccountPath = Boolean(cfg.channels?.signal?.accounts?.[resolvedAccountId]);
|
||||
const basePath = useAccountPath
|
||||
? `channels.signal.accounts.${resolvedAccountId}.`
|
||||
: "channels.signal.";
|
||||
return {
|
||||
policy: account.config.dmPolicy ?? "pairing",
|
||||
allowFrom: account.config.allowFrom ?? [],
|
||||
policyPath: `${basePath}dmPolicy`,
|
||||
allowFromPath: basePath,
|
||||
approveHint: formatPairingApproveHint("signal"),
|
||||
normalizeEntry: (raw) => normalizeE164(raw.replace(/^signal:/i, "").trim()),
|
||||
};
|
||||
},
|
||||
collectWarnings: ({ account, cfg }) => {
|
||||
const defaultGroupPolicy = cfg.channels?.defaults?.groupPolicy;
|
||||
const groupPolicy = account.config.groupPolicy ?? defaultGroupPolicy ?? "allowlist";
|
||||
if (groupPolicy !== "open") return [];
|
||||
return [
|
||||
`- Signal groups: groupPolicy="open" allows any member to trigger the bot. Set channels.signal.groupPolicy="allowlist" + channels.signal.groupAllowFrom to restrict senders.`,
|
||||
];
|
||||
},
|
||||
},
|
||||
messaging: {
|
||||
normalizeTarget: normalizeSignalMessagingTarget,
|
||||
targetResolver: {
|
||||
looksLikeId: looksLikeSignalTargetId,
|
||||
hint: "<E.164|group:ID|signal:group:ID|signal:+E.164>",
|
||||
},
|
||||
},
|
||||
setup: {
|
||||
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
|
||||
applyAccountName: ({ cfg, accountId, name }) =>
|
||||
applyAccountNameToChannelSection({
|
||||
cfg,
|
||||
channelKey: "signal",
|
||||
accountId,
|
||||
name,
|
||||
}),
|
||||
validateInput: ({ input }) => {
|
||||
if (
|
||||
!input.signalNumber &&
|
||||
!input.httpUrl &&
|
||||
!input.httpHost &&
|
||||
!input.httpPort &&
|
||||
!input.cliPath
|
||||
) {
|
||||
return "Signal requires --signal-number or --http-url/--http-host/--http-port/--cli-path.";
|
||||
}
|
||||
return null;
|
||||
},
|
||||
applyAccountConfig: ({ cfg, accountId, input }) => {
|
||||
const namedConfig = applyAccountNameToChannelSection({
|
||||
cfg,
|
||||
channelKey: "signal",
|
||||
accountId,
|
||||
name: input.name,
|
||||
});
|
||||
const next =
|
||||
accountId !== DEFAULT_ACCOUNT_ID
|
||||
? migrateBaseNameToDefaultAccount({
|
||||
cfg: namedConfig,
|
||||
channelKey: "signal",
|
||||
})
|
||||
: namedConfig;
|
||||
if (accountId === DEFAULT_ACCOUNT_ID) {
|
||||
return {
|
||||
...next,
|
||||
channels: {
|
||||
...next.channels,
|
||||
signal: {
|
||||
...next.channels?.signal,
|
||||
enabled: true,
|
||||
...(input.signalNumber ? { account: input.signalNumber } : {}),
|
||||
...(input.cliPath ? { cliPath: input.cliPath } : {}),
|
||||
...(input.httpUrl ? { httpUrl: input.httpUrl } : {}),
|
||||
...(input.httpHost ? { httpHost: input.httpHost } : {}),
|
||||
...(input.httpPort ? { httpPort: Number(input.httpPort) } : {}),
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...next,
|
||||
channels: {
|
||||
...next.channels,
|
||||
signal: {
|
||||
...next.channels?.signal,
|
||||
enabled: true,
|
||||
accounts: {
|
||||
...next.channels?.signal?.accounts,
|
||||
[accountId]: {
|
||||
...next.channels?.signal?.accounts?.[accountId],
|
||||
enabled: true,
|
||||
...(input.signalNumber ? { account: input.signalNumber } : {}),
|
||||
...(input.cliPath ? { cliPath: input.cliPath } : {}),
|
||||
...(input.httpUrl ? { httpUrl: input.httpUrl } : {}),
|
||||
...(input.httpHost ? { httpHost: input.httpHost } : {}),
|
||||
...(input.httpPort ? { httpPort: Number(input.httpPort) } : {}),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
outbound: {
|
||||
deliveryMode: "direct",
|
||||
chunker: chunkText,
|
||||
textChunkLimit: 4000,
|
||||
sendText: async ({ cfg, to, text, accountId, deps }) => {
|
||||
const send = deps?.sendSignal ?? sendMessageSignal;
|
||||
const maxBytes = resolveChannelMediaMaxBytes({
|
||||
cfg,
|
||||
resolveChannelLimitMb: ({ cfg, accountId }) =>
|
||||
cfg.channels?.signal?.accounts?.[accountId]?.mediaMaxMb ??
|
||||
cfg.channels?.signal?.mediaMaxMb,
|
||||
accountId,
|
||||
});
|
||||
const result = await send(to, text, {
|
||||
maxBytes,
|
||||
accountId: accountId ?? undefined,
|
||||
});
|
||||
return { channel: "signal", ...result };
|
||||
},
|
||||
sendMedia: async ({ cfg, to, text, mediaUrl, accountId, deps }) => {
|
||||
const send = deps?.sendSignal ?? sendMessageSignal;
|
||||
const maxBytes = resolveChannelMediaMaxBytes({
|
||||
cfg,
|
||||
resolveChannelLimitMb: ({ cfg, accountId }) =>
|
||||
cfg.channels?.signal?.accounts?.[accountId]?.mediaMaxMb ??
|
||||
cfg.channels?.signal?.mediaMaxMb,
|
||||
accountId,
|
||||
});
|
||||
const result = await send(to, text, {
|
||||
mediaUrl,
|
||||
maxBytes,
|
||||
accountId: accountId ?? undefined,
|
||||
});
|
||||
return { channel: "signal", ...result };
|
||||
},
|
||||
},
|
||||
status: {
|
||||
defaultRuntime: {
|
||||
accountId: DEFAULT_ACCOUNT_ID,
|
||||
running: false,
|
||||
lastStartAt: null,
|
||||
lastStopAt: null,
|
||||
lastError: null,
|
||||
},
|
||||
collectStatusIssues: (accounts) =>
|
||||
accounts.flatMap((account) => {
|
||||
const lastError = typeof account.lastError === "string" ? account.lastError.trim() : "";
|
||||
if (!lastError) return [];
|
||||
return [
|
||||
{
|
||||
channel: "signal",
|
||||
accountId: account.accountId,
|
||||
kind: "runtime",
|
||||
message: `Channel error: ${lastError}`,
|
||||
},
|
||||
];
|
||||
}),
|
||||
buildChannelSummary: ({ snapshot }) => ({
|
||||
configured: snapshot.configured ?? false,
|
||||
baseUrl: snapshot.baseUrl ?? null,
|
||||
running: snapshot.running ?? false,
|
||||
lastStartAt: snapshot.lastStartAt ?? null,
|
||||
lastStopAt: snapshot.lastStopAt ?? null,
|
||||
lastError: snapshot.lastError ?? null,
|
||||
probe: snapshot.probe,
|
||||
lastProbeAt: snapshot.lastProbeAt ?? null,
|
||||
}),
|
||||
probeAccount: async ({ account, timeoutMs }) => {
|
||||
const baseUrl = account.baseUrl;
|
||||
return await probeSignal(baseUrl, timeoutMs);
|
||||
},
|
||||
buildAccountSnapshot: ({ account, runtime, probe }) => ({
|
||||
accountId: account.accountId,
|
||||
name: account.name,
|
||||
enabled: account.enabled,
|
||||
configured: account.configured,
|
||||
baseUrl: account.baseUrl,
|
||||
running: runtime?.running ?? false,
|
||||
lastStartAt: runtime?.lastStartAt ?? null,
|
||||
lastStopAt: runtime?.lastStopAt ?? null,
|
||||
lastError: runtime?.lastError ?? null,
|
||||
probe,
|
||||
lastInboundAt: runtime?.lastInboundAt ?? null,
|
||||
lastOutboundAt: runtime?.lastOutboundAt ?? null,
|
||||
}),
|
||||
},
|
||||
gateway: {
|
||||
startAccount: async (ctx) => {
|
||||
const account = ctx.account;
|
||||
ctx.setStatus({
|
||||
accountId: account.accountId,
|
||||
baseUrl: account.baseUrl,
|
||||
});
|
||||
ctx.log?.info(`[${account.accountId}] starting provider (${account.baseUrl})`);
|
||||
// Lazy import: the monitor pulls the reply pipeline; avoid ESM init cycles.
|
||||
const { monitorSignalProvider } = await import("../../signal/index.js");
|
||||
return monitorSignalProvider({
|
||||
accountId: account.accountId,
|
||||
config: ctx.cfg,
|
||||
runtime: ctx.runtime,
|
||||
abortSignal: ctx.abortSignal,
|
||||
mediaMaxMb: account.config.mediaMaxMb,
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -1,591 +0,0 @@
|
||||
import { createActionGate, readNumberParam, readStringParam } from "../../agents/tools/common.js";
|
||||
import { handleSlackAction } from "../../agents/tools/slack-actions.js";
|
||||
import { loadConfig } from "../../config/config.js";
|
||||
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../routing/session-key.js";
|
||||
import {
|
||||
listEnabledSlackAccounts,
|
||||
listSlackAccountIds,
|
||||
type ResolvedSlackAccount,
|
||||
resolveDefaultSlackAccountId,
|
||||
resolveSlackAccount,
|
||||
} from "../../slack/accounts.js";
|
||||
import { resolveSlackChannelAllowlist } from "../../slack/resolve-channels.js";
|
||||
import { resolveSlackUserAllowlist } from "../../slack/resolve-users.js";
|
||||
import { probeSlack } from "../../slack/probe.js";
|
||||
import { sendMessageSlack } from "../../slack/send.js";
|
||||
import { getChatChannelMeta } from "../registry.js";
|
||||
import { SlackConfigSchema } from "../../config/zod-schema.providers-core.js";
|
||||
import { buildChannelConfigSchema } from "./config-schema.js";
|
||||
import {
|
||||
deleteAccountFromConfigSection,
|
||||
setAccountEnabledInConfigSection,
|
||||
} from "./config-helpers.js";
|
||||
import { resolveSlackGroupRequireMention } from "./group-mentions.js";
|
||||
import { formatPairingApproveHint } from "./helpers.js";
|
||||
import { looksLikeSlackTargetId, normalizeSlackMessagingTarget } from "./normalize/slack.js";
|
||||
import { slackOnboardingAdapter } from "./onboarding/slack.js";
|
||||
import { PAIRING_APPROVED_MESSAGE } from "./pairing-message.js";
|
||||
import {
|
||||
applyAccountNameToChannelSection,
|
||||
migrateBaseNameToDefaultAccount,
|
||||
} from "./setup-helpers.js";
|
||||
import type { ChannelMessageActionName, ChannelPlugin } from "./types.js";
|
||||
import {
|
||||
listSlackDirectoryGroupsFromConfig,
|
||||
listSlackDirectoryPeersFromConfig,
|
||||
} from "./directory-config.js";
|
||||
import {
|
||||
listSlackDirectoryGroupsLive,
|
||||
listSlackDirectoryPeersLive,
|
||||
} from "../../slack/directory-live.js";
|
||||
|
||||
const meta = getChatChannelMeta("slack");
|
||||
|
||||
// Select the appropriate Slack token for read/write operations.
|
||||
function getTokenForOperation(
|
||||
account: ResolvedSlackAccount,
|
||||
operation: "read" | "write",
|
||||
): string | undefined {
|
||||
const userToken = account.config.userToken?.trim() || undefined;
|
||||
const botToken = account.botToken?.trim();
|
||||
const allowUserWrites = account.config.userTokenReadOnly === false;
|
||||
if (operation === "read") return userToken ?? botToken;
|
||||
if (!allowUserWrites) return botToken;
|
||||
return botToken ?? userToken;
|
||||
}
|
||||
|
||||
export const slackPlugin: ChannelPlugin<ResolvedSlackAccount> = {
|
||||
id: "slack",
|
||||
meta: {
|
||||
...meta,
|
||||
},
|
||||
onboarding: slackOnboardingAdapter,
|
||||
pairing: {
|
||||
idLabel: "slackUserId",
|
||||
normalizeAllowEntry: (entry) => entry.replace(/^(slack|user):/i, ""),
|
||||
notifyApproval: async ({ id }) => {
|
||||
const cfg = loadConfig();
|
||||
const account = resolveSlackAccount({
|
||||
cfg,
|
||||
accountId: DEFAULT_ACCOUNT_ID,
|
||||
});
|
||||
const token = getTokenForOperation(account, "write");
|
||||
const botToken = account.botToken?.trim();
|
||||
const tokenOverride = token && token !== botToken ? token : undefined;
|
||||
if (tokenOverride) {
|
||||
await sendMessageSlack(`user:${id}`, PAIRING_APPROVED_MESSAGE, {
|
||||
token: tokenOverride,
|
||||
});
|
||||
} else {
|
||||
await sendMessageSlack(`user:${id}`, PAIRING_APPROVED_MESSAGE);
|
||||
}
|
||||
},
|
||||
},
|
||||
capabilities: {
|
||||
chatTypes: ["direct", "channel", "thread"],
|
||||
reactions: true,
|
||||
threads: true,
|
||||
media: true,
|
||||
nativeCommands: true,
|
||||
},
|
||||
streaming: {
|
||||
blockStreamingCoalesceDefaults: { minChars: 1500, idleMs: 1000 },
|
||||
},
|
||||
reload: { configPrefixes: ["channels.slack"] },
|
||||
configSchema: buildChannelConfigSchema(SlackConfigSchema),
|
||||
config: {
|
||||
listAccountIds: (cfg) => listSlackAccountIds(cfg),
|
||||
resolveAccount: (cfg, accountId) => resolveSlackAccount({ cfg, accountId }),
|
||||
defaultAccountId: (cfg) => resolveDefaultSlackAccountId(cfg),
|
||||
setAccountEnabled: ({ cfg, accountId, enabled }) =>
|
||||
setAccountEnabledInConfigSection({
|
||||
cfg,
|
||||
sectionKey: "slack",
|
||||
accountId,
|
||||
enabled,
|
||||
allowTopLevel: true,
|
||||
}),
|
||||
deleteAccount: ({ cfg, accountId }) =>
|
||||
deleteAccountFromConfigSection({
|
||||
cfg,
|
||||
sectionKey: "slack",
|
||||
accountId,
|
||||
clearBaseFields: ["botToken", "appToken", "name"],
|
||||
}),
|
||||
isConfigured: (account) => Boolean(account.botToken && account.appToken),
|
||||
describeAccount: (account) => ({
|
||||
accountId: account.accountId,
|
||||
name: account.name,
|
||||
enabled: account.enabled,
|
||||
configured: Boolean(account.botToken && account.appToken),
|
||||
botTokenSource: account.botTokenSource,
|
||||
appTokenSource: account.appTokenSource,
|
||||
}),
|
||||
resolveAllowFrom: ({ cfg, accountId }) =>
|
||||
(resolveSlackAccount({ cfg, accountId }).dm?.allowFrom ?? []).map((entry) => String(entry)),
|
||||
formatAllowFrom: ({ allowFrom }) =>
|
||||
allowFrom
|
||||
.map((entry) => String(entry).trim())
|
||||
.filter(Boolean)
|
||||
.map((entry) => entry.toLowerCase()),
|
||||
},
|
||||
security: {
|
||||
resolveDmPolicy: ({ cfg, accountId, account }) => {
|
||||
const resolvedAccountId = accountId ?? account.accountId ?? DEFAULT_ACCOUNT_ID;
|
||||
const useAccountPath = Boolean(cfg.channels?.slack?.accounts?.[resolvedAccountId]);
|
||||
const allowFromPath = useAccountPath
|
||||
? `channels.slack.accounts.${resolvedAccountId}.dm.`
|
||||
: "channels.slack.dm.";
|
||||
return {
|
||||
policy: account.dm?.policy ?? "pairing",
|
||||
allowFrom: account.dm?.allowFrom ?? [],
|
||||
allowFromPath,
|
||||
approveHint: formatPairingApproveHint("slack"),
|
||||
normalizeEntry: (raw) => raw.replace(/^(slack|user):/i, ""),
|
||||
};
|
||||
},
|
||||
collectWarnings: ({ account, cfg }) => {
|
||||
const warnings: string[] = [];
|
||||
const defaultGroupPolicy = cfg.channels?.defaults?.groupPolicy;
|
||||
const groupPolicy = account.config.groupPolicy ?? defaultGroupPolicy ?? "open";
|
||||
const channelAllowlistConfigured =
|
||||
Boolean(account.config.channels) && Object.keys(account.config.channels ?? {}).length > 0;
|
||||
|
||||
if (groupPolicy === "open") {
|
||||
if (channelAllowlistConfigured) {
|
||||
warnings.push(
|
||||
`- Slack channels: groupPolicy="open" allows any channel not explicitly denied to trigger (mention-gated). Set channels.slack.groupPolicy="allowlist" and configure channels.slack.channels.`,
|
||||
);
|
||||
} else {
|
||||
warnings.push(
|
||||
`- Slack channels: groupPolicy="open" with no channel allowlist; any channel can trigger (mention-gated). Set channels.slack.groupPolicy="allowlist" and configure channels.slack.channels.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return warnings;
|
||||
},
|
||||
},
|
||||
groups: {
|
||||
resolveRequireMention: resolveSlackGroupRequireMention,
|
||||
},
|
||||
threading: {
|
||||
resolveReplyToMode: ({ cfg, accountId }) =>
|
||||
resolveSlackAccount({ cfg, accountId }).replyToMode ?? "off",
|
||||
allowTagsWhenOff: true,
|
||||
buildToolContext: ({ cfg, accountId, context, hasRepliedRef }) => {
|
||||
const configuredReplyToMode = resolveSlackAccount({ cfg, accountId }).replyToMode ?? "off";
|
||||
const effectiveReplyToMode = context.ThreadLabel ? "all" : configuredReplyToMode;
|
||||
return {
|
||||
currentChannelId: context.To?.startsWith("channel:")
|
||||
? context.To.slice("channel:".length)
|
||||
: undefined,
|
||||
currentThreadTs: context.ReplyToId,
|
||||
replyToMode: effectiveReplyToMode,
|
||||
hasRepliedRef,
|
||||
};
|
||||
},
|
||||
},
|
||||
messaging: {
|
||||
normalizeTarget: normalizeSlackMessagingTarget,
|
||||
targetResolver: {
|
||||
looksLikeId: looksLikeSlackTargetId,
|
||||
hint: "<channelId|user:ID|channel:ID>",
|
||||
},
|
||||
},
|
||||
directory: {
|
||||
self: async () => null,
|
||||
listPeers: async (params) => listSlackDirectoryPeersFromConfig(params),
|
||||
listGroups: async (params) => listSlackDirectoryGroupsFromConfig(params),
|
||||
listPeersLive: async (params) => listSlackDirectoryPeersLive(params),
|
||||
listGroupsLive: async (params) => listSlackDirectoryGroupsLive(params),
|
||||
},
|
||||
resolver: {
|
||||
resolveTargets: async ({ cfg, accountId, inputs, kind }) => {
|
||||
const account = resolveSlackAccount({ cfg, accountId });
|
||||
const token = account.config.userToken?.trim() || account.botToken?.trim();
|
||||
if (!token) {
|
||||
return inputs.map((input) => ({
|
||||
input,
|
||||
resolved: false,
|
||||
note: "missing Slack token",
|
||||
}));
|
||||
}
|
||||
if (kind === "group") {
|
||||
const resolved = await resolveSlackChannelAllowlist({ token, entries: inputs });
|
||||
return resolved.map((entry) => ({
|
||||
input: entry.input,
|
||||
resolved: entry.resolved,
|
||||
id: entry.id,
|
||||
name: entry.name,
|
||||
note: entry.archived ? "archived" : undefined,
|
||||
}));
|
||||
}
|
||||
const resolved = await resolveSlackUserAllowlist({ token, entries: inputs });
|
||||
return resolved.map((entry) => ({
|
||||
input: entry.input,
|
||||
resolved: entry.resolved,
|
||||
id: entry.id,
|
||||
name: entry.name,
|
||||
note: entry.note,
|
||||
}));
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
listActions: ({ cfg }) => {
|
||||
const accounts = listEnabledSlackAccounts(cfg).filter(
|
||||
(account) => account.botTokenSource !== "none",
|
||||
);
|
||||
if (accounts.length === 0) return [];
|
||||
const isActionEnabled = (key: string, defaultValue = true) => {
|
||||
for (const account of accounts) {
|
||||
const gate = createActionGate(
|
||||
(account.actions ?? cfg.channels?.slack?.actions) as Record<
|
||||
string,
|
||||
boolean | undefined
|
||||
>,
|
||||
);
|
||||
if (gate(key, defaultValue)) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const actions = new Set<ChannelMessageActionName>(["send"]);
|
||||
if (isActionEnabled("reactions")) {
|
||||
actions.add("react");
|
||||
actions.add("reactions");
|
||||
}
|
||||
if (isActionEnabled("messages")) {
|
||||
actions.add("read");
|
||||
actions.add("edit");
|
||||
actions.add("delete");
|
||||
}
|
||||
if (isActionEnabled("pins")) {
|
||||
actions.add("pin");
|
||||
actions.add("unpin");
|
||||
actions.add("list-pins");
|
||||
}
|
||||
if (isActionEnabled("memberInfo")) actions.add("member-info");
|
||||
if (isActionEnabled("emojiList")) actions.add("emoji-list");
|
||||
return Array.from(actions);
|
||||
},
|
||||
extractToolSend: ({ args }) => {
|
||||
const action = typeof args.action === "string" ? args.action.trim() : "";
|
||||
if (action !== "sendMessage") return null;
|
||||
const to = typeof args.to === "string" ? args.to : undefined;
|
||||
if (!to) return null;
|
||||
const accountId = typeof args.accountId === "string" ? args.accountId.trim() : undefined;
|
||||
return { to, accountId };
|
||||
},
|
||||
handleAction: async ({ action, params, cfg, accountId, toolContext }) => {
|
||||
const resolveChannelId = () =>
|
||||
readStringParam(params, "channelId") ?? readStringParam(params, "to", { required: true });
|
||||
|
||||
if (action === "send") {
|
||||
const to = readStringParam(params, "to", { required: true });
|
||||
const content = readStringParam(params, "message", {
|
||||
required: true,
|
||||
allowEmpty: true,
|
||||
});
|
||||
const mediaUrl = readStringParam(params, "media", { trim: false });
|
||||
const threadId = readStringParam(params, "threadId");
|
||||
const replyTo = readStringParam(params, "replyTo");
|
||||
return await handleSlackAction(
|
||||
{
|
||||
action: "sendMessage",
|
||||
to,
|
||||
content,
|
||||
mediaUrl: mediaUrl ?? undefined,
|
||||
accountId: accountId ?? undefined,
|
||||
threadTs: threadId ?? replyTo ?? undefined,
|
||||
},
|
||||
cfg,
|
||||
toolContext,
|
||||
);
|
||||
}
|
||||
|
||||
if (action === "react") {
|
||||
const messageId = readStringParam(params, "messageId", {
|
||||
required: true,
|
||||
});
|
||||
const emoji = readStringParam(params, "emoji", { allowEmpty: true });
|
||||
const remove = typeof params.remove === "boolean" ? params.remove : undefined;
|
||||
return await handleSlackAction(
|
||||
{
|
||||
action: "react",
|
||||
channelId: resolveChannelId(),
|
||||
messageId,
|
||||
emoji,
|
||||
remove,
|
||||
accountId: accountId ?? undefined,
|
||||
},
|
||||
cfg,
|
||||
);
|
||||
}
|
||||
|
||||
if (action === "reactions") {
|
||||
const messageId = readStringParam(params, "messageId", {
|
||||
required: true,
|
||||
});
|
||||
const limit = readNumberParam(params, "limit", { integer: true });
|
||||
return await handleSlackAction(
|
||||
{
|
||||
action: "reactions",
|
||||
channelId: resolveChannelId(),
|
||||
messageId,
|
||||
limit,
|
||||
accountId: accountId ?? undefined,
|
||||
},
|
||||
cfg,
|
||||
);
|
||||
}
|
||||
|
||||
if (action === "read") {
|
||||
const limit = readNumberParam(params, "limit", { integer: true });
|
||||
return await handleSlackAction(
|
||||
{
|
||||
action: "readMessages",
|
||||
channelId: resolveChannelId(),
|
||||
limit,
|
||||
before: readStringParam(params, "before"),
|
||||
after: readStringParam(params, "after"),
|
||||
accountId: accountId ?? undefined,
|
||||
},
|
||||
cfg,
|
||||
);
|
||||
}
|
||||
|
||||
if (action === "edit") {
|
||||
const messageId = readStringParam(params, "messageId", {
|
||||
required: true,
|
||||
});
|
||||
const content = readStringParam(params, "message", { required: true });
|
||||
return await handleSlackAction(
|
||||
{
|
||||
action: "editMessage",
|
||||
channelId: resolveChannelId(),
|
||||
messageId,
|
||||
content,
|
||||
accountId: accountId ?? undefined,
|
||||
},
|
||||
cfg,
|
||||
);
|
||||
}
|
||||
|
||||
if (action === "delete") {
|
||||
const messageId = readStringParam(params, "messageId", {
|
||||
required: true,
|
||||
});
|
||||
return await handleSlackAction(
|
||||
{
|
||||
action: "deleteMessage",
|
||||
channelId: resolveChannelId(),
|
||||
messageId,
|
||||
accountId: accountId ?? undefined,
|
||||
},
|
||||
cfg,
|
||||
);
|
||||
}
|
||||
|
||||
if (action === "pin" || action === "unpin" || action === "list-pins") {
|
||||
const messageId =
|
||||
action === "list-pins"
|
||||
? undefined
|
||||
: readStringParam(params, "messageId", { required: true });
|
||||
return await handleSlackAction(
|
||||
{
|
||||
action:
|
||||
action === "pin" ? "pinMessage" : action === "unpin" ? "unpinMessage" : "listPins",
|
||||
channelId: resolveChannelId(),
|
||||
messageId,
|
||||
accountId: accountId ?? undefined,
|
||||
},
|
||||
cfg,
|
||||
);
|
||||
}
|
||||
|
||||
if (action === "member-info") {
|
||||
const userId = readStringParam(params, "userId", { required: true });
|
||||
return await handleSlackAction(
|
||||
{ action: "memberInfo", userId, accountId: accountId ?? undefined },
|
||||
cfg,
|
||||
);
|
||||
}
|
||||
|
||||
if (action === "emoji-list") {
|
||||
return await handleSlackAction(
|
||||
{ action: "emojiList", accountId: accountId ?? undefined },
|
||||
cfg,
|
||||
);
|
||||
}
|
||||
|
||||
throw new Error(`Action ${action} is not supported for provider ${meta.id}.`);
|
||||
},
|
||||
},
|
||||
setup: {
|
||||
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
|
||||
applyAccountName: ({ cfg, accountId, name }) =>
|
||||
applyAccountNameToChannelSection({
|
||||
cfg,
|
||||
channelKey: "slack",
|
||||
accountId,
|
||||
name,
|
||||
}),
|
||||
validateInput: ({ accountId, input }) => {
|
||||
if (input.useEnv && accountId !== DEFAULT_ACCOUNT_ID) {
|
||||
return "Slack env tokens can only be used for the default account.";
|
||||
}
|
||||
if (!input.useEnv && (!input.botToken || !input.appToken)) {
|
||||
return "Slack requires --bot-token and --app-token (or --use-env).";
|
||||
}
|
||||
return null;
|
||||
},
|
||||
applyAccountConfig: ({ cfg, accountId, input }) => {
|
||||
const namedConfig = applyAccountNameToChannelSection({
|
||||
cfg,
|
||||
channelKey: "slack",
|
||||
accountId,
|
||||
name: input.name,
|
||||
});
|
||||
const next =
|
||||
accountId !== DEFAULT_ACCOUNT_ID
|
||||
? migrateBaseNameToDefaultAccount({
|
||||
cfg: namedConfig,
|
||||
channelKey: "slack",
|
||||
})
|
||||
: namedConfig;
|
||||
if (accountId === DEFAULT_ACCOUNT_ID) {
|
||||
return {
|
||||
...next,
|
||||
channels: {
|
||||
...next.channels,
|
||||
slack: {
|
||||
...next.channels?.slack,
|
||||
enabled: true,
|
||||
...(input.useEnv
|
||||
? {}
|
||||
: {
|
||||
...(input.botToken ? { botToken: input.botToken } : {}),
|
||||
...(input.appToken ? { appToken: input.appToken } : {}),
|
||||
}),
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...next,
|
||||
channels: {
|
||||
...next.channels,
|
||||
slack: {
|
||||
...next.channels?.slack,
|
||||
enabled: true,
|
||||
accounts: {
|
||||
...next.channels?.slack?.accounts,
|
||||
[accountId]: {
|
||||
...next.channels?.slack?.accounts?.[accountId],
|
||||
enabled: true,
|
||||
...(input.botToken ? { botToken: input.botToken } : {}),
|
||||
...(input.appToken ? { appToken: input.appToken } : {}),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
outbound: {
|
||||
deliveryMode: "direct",
|
||||
chunker: null,
|
||||
textChunkLimit: 4000,
|
||||
sendText: async ({ to, text, accountId, deps, replyToId, cfg }) => {
|
||||
const send = deps?.sendSlack ?? sendMessageSlack;
|
||||
const account = resolveSlackAccount({ cfg, accountId });
|
||||
const token = getTokenForOperation(account, "write");
|
||||
const botToken = account.botToken?.trim();
|
||||
const tokenOverride = token && token !== botToken ? token : undefined;
|
||||
const result = await send(to, text, {
|
||||
threadTs: replyToId ?? undefined,
|
||||
accountId: accountId ?? undefined,
|
||||
...(tokenOverride ? { token: tokenOverride } : {}),
|
||||
});
|
||||
return { channel: "slack", ...result };
|
||||
},
|
||||
sendMedia: async ({ to, text, mediaUrl, accountId, deps, replyToId, cfg }) => {
|
||||
const send = deps?.sendSlack ?? sendMessageSlack;
|
||||
const account = resolveSlackAccount({ cfg, accountId });
|
||||
const token = getTokenForOperation(account, "write");
|
||||
const botToken = account.botToken?.trim();
|
||||
const tokenOverride = token && token !== botToken ? token : undefined;
|
||||
const result = await send(to, text, {
|
||||
mediaUrl,
|
||||
threadTs: replyToId ?? undefined,
|
||||
accountId: accountId ?? undefined,
|
||||
...(tokenOverride ? { token: tokenOverride } : {}),
|
||||
});
|
||||
return { channel: "slack", ...result };
|
||||
},
|
||||
},
|
||||
status: {
|
||||
defaultRuntime: {
|
||||
accountId: DEFAULT_ACCOUNT_ID,
|
||||
running: false,
|
||||
lastStartAt: null,
|
||||
lastStopAt: null,
|
||||
lastError: null,
|
||||
},
|
||||
buildChannelSummary: ({ snapshot }) => ({
|
||||
configured: snapshot.configured ?? false,
|
||||
botTokenSource: snapshot.botTokenSource ?? "none",
|
||||
appTokenSource: snapshot.appTokenSource ?? "none",
|
||||
running: snapshot.running ?? false,
|
||||
lastStartAt: snapshot.lastStartAt ?? null,
|
||||
lastStopAt: snapshot.lastStopAt ?? null,
|
||||
lastError: snapshot.lastError ?? null,
|
||||
probe: snapshot.probe,
|
||||
lastProbeAt: snapshot.lastProbeAt ?? null,
|
||||
}),
|
||||
probeAccount: async ({ account, timeoutMs }) => {
|
||||
const token = account.botToken?.trim();
|
||||
if (!token) return { ok: false, error: "missing token" };
|
||||
return await probeSlack(token, timeoutMs);
|
||||
},
|
||||
buildAccountSnapshot: ({ account, runtime, probe }) => {
|
||||
const configured = Boolean(account.botToken && account.appToken);
|
||||
return {
|
||||
accountId: account.accountId,
|
||||
name: account.name,
|
||||
enabled: account.enabled,
|
||||
configured,
|
||||
botTokenSource: account.botTokenSource,
|
||||
appTokenSource: account.appTokenSource,
|
||||
running: runtime?.running ?? false,
|
||||
lastStartAt: runtime?.lastStartAt ?? null,
|
||||
lastStopAt: runtime?.lastStopAt ?? null,
|
||||
lastError: runtime?.lastError ?? null,
|
||||
probe,
|
||||
lastInboundAt: runtime?.lastInboundAt ?? null,
|
||||
lastOutboundAt: runtime?.lastOutboundAt ?? null,
|
||||
};
|
||||
},
|
||||
},
|
||||
gateway: {
|
||||
startAccount: async (ctx) => {
|
||||
const account = ctx.account;
|
||||
const botToken = account.botToken?.trim();
|
||||
const appToken = account.appToken?.trim();
|
||||
ctx.log?.info(`[${account.accountId}] starting provider`);
|
||||
// Lazy import: the monitor pulls the reply pipeline; avoid ESM init cycles.
|
||||
const { monitorSlackProvider } = await import("../../slack/index.js");
|
||||
return monitorSlackProvider({
|
||||
botToken: botToken ?? "",
|
||||
appToken: appToken ?? "",
|
||||
accountId: account.accountId,
|
||||
config: ctx.cfg,
|
||||
runtime: ctx.runtime,
|
||||
abortSignal: ctx.abortSignal,
|
||||
mediaMaxMb: account.config.mediaMaxMb,
|
||||
slashCommand: account.config.slashCommand,
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -1,472 +0,0 @@
|
||||
import { chunkMarkdownText } from "../../auto-reply/chunk.js";
|
||||
import type { ClawdbotConfig } from "../../config/config.js";
|
||||
import { writeConfigFile } from "../../config/config.js";
|
||||
import { shouldLogVerbose } from "../../globals.js";
|
||||
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../routing/session-key.js";
|
||||
import {
|
||||
listTelegramAccountIds,
|
||||
type ResolvedTelegramAccount,
|
||||
resolveDefaultTelegramAccountId,
|
||||
resolveTelegramAccount,
|
||||
} from "../../telegram/accounts.js";
|
||||
import {
|
||||
auditTelegramGroupMembership,
|
||||
collectTelegramUnmentionedGroupIds,
|
||||
} from "../../telegram/audit.js";
|
||||
import { probeTelegram } from "../../telegram/probe.js";
|
||||
import { sendMessageTelegram } from "../../telegram/send.js";
|
||||
import { resolveTelegramToken } from "../../telegram/token.js";
|
||||
import { getChatChannelMeta } from "../registry.js";
|
||||
import { TelegramConfigSchema } from "../../config/zod-schema.providers-core.js";
|
||||
import { telegramMessageActions } from "./actions/telegram.js";
|
||||
import { buildChannelConfigSchema } from "./config-schema.js";
|
||||
import {
|
||||
deleteAccountFromConfigSection,
|
||||
setAccountEnabledInConfigSection,
|
||||
} from "./config-helpers.js";
|
||||
import { resolveTelegramGroupRequireMention } from "./group-mentions.js";
|
||||
import { formatPairingApproveHint } from "./helpers.js";
|
||||
import {
|
||||
looksLikeTelegramTargetId,
|
||||
normalizeTelegramMessagingTarget,
|
||||
} from "./normalize/telegram.js";
|
||||
import { telegramOnboardingAdapter } from "./onboarding/telegram.js";
|
||||
import { PAIRING_APPROVED_MESSAGE } from "./pairing-message.js";
|
||||
import {
|
||||
applyAccountNameToChannelSection,
|
||||
migrateBaseNameToDefaultAccount,
|
||||
} from "./setup-helpers.js";
|
||||
import { collectTelegramStatusIssues } from "./status-issues/telegram.js";
|
||||
import type { ChannelPlugin } from "./types.js";
|
||||
import {
|
||||
listTelegramDirectoryGroupsFromConfig,
|
||||
listTelegramDirectoryPeersFromConfig,
|
||||
} from "./directory-config.js";
|
||||
|
||||
const meta = getChatChannelMeta("telegram");
|
||||
|
||||
function parseReplyToMessageId(replyToId?: string | null) {
|
||||
if (!replyToId) return undefined;
|
||||
const parsed = Number.parseInt(replyToId, 10);
|
||||
return Number.isFinite(parsed) ? parsed : undefined;
|
||||
}
|
||||
|
||||
function parseThreadId(threadId?: string | number | null) {
|
||||
if (threadId == null) return undefined;
|
||||
if (typeof threadId === "number") {
|
||||
return Number.isFinite(threadId) ? Math.trunc(threadId) : undefined;
|
||||
}
|
||||
const trimmed = threadId.trim();
|
||||
if (!trimmed) return undefined;
|
||||
const parsed = Number.parseInt(trimmed, 10);
|
||||
return Number.isFinite(parsed) ? parsed : undefined;
|
||||
}
|
||||
export const telegramPlugin: ChannelPlugin<ResolvedTelegramAccount> = {
|
||||
id: "telegram",
|
||||
meta: {
|
||||
...meta,
|
||||
quickstartAllowFrom: true,
|
||||
},
|
||||
onboarding: telegramOnboardingAdapter,
|
||||
pairing: {
|
||||
idLabel: "telegramUserId",
|
||||
normalizeAllowEntry: (entry) => entry.replace(/^(telegram|tg):/i, ""),
|
||||
notifyApproval: async ({ cfg, id }) => {
|
||||
const { token } = resolveTelegramToken(cfg);
|
||||
if (!token) throw new Error("telegram token not configured");
|
||||
await sendMessageTelegram(id, PAIRING_APPROVED_MESSAGE, { token });
|
||||
},
|
||||
},
|
||||
capabilities: {
|
||||
chatTypes: ["direct", "group", "channel", "thread"],
|
||||
reactions: true,
|
||||
threads: true,
|
||||
media: true,
|
||||
nativeCommands: true,
|
||||
blockStreaming: true,
|
||||
},
|
||||
reload: { configPrefixes: ["channels.telegram"] },
|
||||
configSchema: buildChannelConfigSchema(TelegramConfigSchema),
|
||||
config: {
|
||||
listAccountIds: (cfg) => listTelegramAccountIds(cfg),
|
||||
resolveAccount: (cfg, accountId) => resolveTelegramAccount({ cfg, accountId }),
|
||||
defaultAccountId: (cfg) => resolveDefaultTelegramAccountId(cfg),
|
||||
setAccountEnabled: ({ cfg, accountId, enabled }) =>
|
||||
setAccountEnabledInConfigSection({
|
||||
cfg,
|
||||
sectionKey: "telegram",
|
||||
accountId,
|
||||
enabled,
|
||||
allowTopLevel: true,
|
||||
}),
|
||||
deleteAccount: ({ cfg, accountId }) =>
|
||||
deleteAccountFromConfigSection({
|
||||
cfg,
|
||||
sectionKey: "telegram",
|
||||
accountId,
|
||||
clearBaseFields: ["botToken", "tokenFile", "name"],
|
||||
}),
|
||||
isConfigured: (account) => Boolean(account.token?.trim()),
|
||||
describeAccount: (account) => ({
|
||||
accountId: account.accountId,
|
||||
name: account.name,
|
||||
enabled: account.enabled,
|
||||
configured: Boolean(account.token?.trim()),
|
||||
tokenSource: account.tokenSource,
|
||||
}),
|
||||
resolveAllowFrom: ({ cfg, accountId }) =>
|
||||
(resolveTelegramAccount({ cfg, accountId }).config.allowFrom ?? []).map((entry) =>
|
||||
String(entry),
|
||||
),
|
||||
formatAllowFrom: ({ allowFrom }) =>
|
||||
allowFrom
|
||||
.map((entry) => String(entry).trim())
|
||||
.filter(Boolean)
|
||||
.map((entry) => entry.replace(/^(telegram|tg):/i, ""))
|
||||
.map((entry) => entry.toLowerCase()),
|
||||
},
|
||||
security: {
|
||||
resolveDmPolicy: ({ cfg, accountId, account }) => {
|
||||
const resolvedAccountId = accountId ?? account.accountId ?? DEFAULT_ACCOUNT_ID;
|
||||
const useAccountPath = Boolean(cfg.channels?.telegram?.accounts?.[resolvedAccountId]);
|
||||
const basePath = useAccountPath
|
||||
? `channels.telegram.accounts.${resolvedAccountId}.`
|
||||
: "channels.telegram.";
|
||||
return {
|
||||
policy: account.config.dmPolicy ?? "pairing",
|
||||
allowFrom: account.config.allowFrom ?? [],
|
||||
policyPath: `${basePath}dmPolicy`,
|
||||
allowFromPath: basePath,
|
||||
approveHint: formatPairingApproveHint("telegram"),
|
||||
normalizeEntry: (raw) => raw.replace(/^(telegram|tg):/i, ""),
|
||||
};
|
||||
},
|
||||
collectWarnings: ({ account, cfg }) => {
|
||||
const defaultGroupPolicy = cfg.channels?.defaults?.groupPolicy;
|
||||
const groupPolicy = account.config.groupPolicy ?? defaultGroupPolicy ?? "allowlist";
|
||||
if (groupPolicy !== "open") return [];
|
||||
const groupAllowlistConfigured =
|
||||
account.config.groups && Object.keys(account.config.groups).length > 0;
|
||||
if (groupAllowlistConfigured) {
|
||||
return [
|
||||
`- Telegram groups: groupPolicy="open" allows any member in allowed groups to trigger (mention-gated). Set channels.telegram.groupPolicy="allowlist" + channels.telegram.groupAllowFrom to restrict senders.`,
|
||||
];
|
||||
}
|
||||
return [
|
||||
`- Telegram groups: groupPolicy="open" with no channels.telegram.groups allowlist; any group can add + ping (mention-gated). Set channels.telegram.groupPolicy="allowlist" + channels.telegram.groupAllowFrom or configure channels.telegram.groups.`,
|
||||
];
|
||||
},
|
||||
},
|
||||
groups: {
|
||||
resolveRequireMention: resolveTelegramGroupRequireMention,
|
||||
},
|
||||
threading: {
|
||||
resolveReplyToMode: ({ cfg }) => cfg.channels?.telegram?.replyToMode ?? "first",
|
||||
},
|
||||
messaging: {
|
||||
normalizeTarget: normalizeTelegramMessagingTarget,
|
||||
targetResolver: {
|
||||
looksLikeId: looksLikeTelegramTargetId,
|
||||
hint: "<chatId>",
|
||||
},
|
||||
},
|
||||
directory: {
|
||||
self: async () => null,
|
||||
listPeers: async (params) => listTelegramDirectoryPeersFromConfig(params),
|
||||
listGroups: async (params) => listTelegramDirectoryGroupsFromConfig(params),
|
||||
},
|
||||
actions: telegramMessageActions,
|
||||
setup: {
|
||||
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
|
||||
applyAccountName: ({ cfg, accountId, name }) =>
|
||||
applyAccountNameToChannelSection({
|
||||
cfg,
|
||||
channelKey: "telegram",
|
||||
accountId,
|
||||
name,
|
||||
}),
|
||||
validateInput: ({ accountId, input }) => {
|
||||
if (input.useEnv && accountId !== DEFAULT_ACCOUNT_ID) {
|
||||
return "TELEGRAM_BOT_TOKEN can only be used for the default account.";
|
||||
}
|
||||
if (!input.useEnv && !input.token && !input.tokenFile) {
|
||||
return "Telegram requires token or --token-file (or --use-env).";
|
||||
}
|
||||
return null;
|
||||
},
|
||||
applyAccountConfig: ({ cfg, accountId, input }) => {
|
||||
const namedConfig = applyAccountNameToChannelSection({
|
||||
cfg,
|
||||
channelKey: "telegram",
|
||||
accountId,
|
||||
name: input.name,
|
||||
});
|
||||
const next =
|
||||
accountId !== DEFAULT_ACCOUNT_ID
|
||||
? migrateBaseNameToDefaultAccount({
|
||||
cfg: namedConfig,
|
||||
channelKey: "telegram",
|
||||
})
|
||||
: namedConfig;
|
||||
if (accountId === DEFAULT_ACCOUNT_ID) {
|
||||
return {
|
||||
...next,
|
||||
channels: {
|
||||
...next.channels,
|
||||
telegram: {
|
||||
...next.channels?.telegram,
|
||||
enabled: true,
|
||||
...(input.useEnv
|
||||
? {}
|
||||
: input.tokenFile
|
||||
? { tokenFile: input.tokenFile }
|
||||
: input.token
|
||||
? { botToken: input.token }
|
||||
: {}),
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
...next,
|
||||
channels: {
|
||||
...next.channels,
|
||||
telegram: {
|
||||
...next.channels?.telegram,
|
||||
enabled: true,
|
||||
accounts: {
|
||||
...next.channels?.telegram?.accounts,
|
||||
[accountId]: {
|
||||
...next.channels?.telegram?.accounts?.[accountId],
|
||||
enabled: true,
|
||||
...(input.tokenFile
|
||||
? { tokenFile: input.tokenFile }
|
||||
: input.token
|
||||
? { botToken: input.token }
|
||||
: {}),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
outbound: {
|
||||
deliveryMode: "direct",
|
||||
chunker: chunkMarkdownText,
|
||||
textChunkLimit: 4000,
|
||||
sendText: async ({ to, text, accountId, deps, replyToId, threadId }) => {
|
||||
const send = deps?.sendTelegram ?? sendMessageTelegram;
|
||||
const replyToMessageId = parseReplyToMessageId(replyToId);
|
||||
const messageThreadId = parseThreadId(threadId);
|
||||
const result = await send(to, text, {
|
||||
verbose: false,
|
||||
messageThreadId,
|
||||
replyToMessageId,
|
||||
accountId: accountId ?? undefined,
|
||||
});
|
||||
return { channel: "telegram", ...result };
|
||||
},
|
||||
sendMedia: async ({ to, text, mediaUrl, accountId, deps, replyToId, threadId }) => {
|
||||
const send = deps?.sendTelegram ?? sendMessageTelegram;
|
||||
const replyToMessageId = parseReplyToMessageId(replyToId);
|
||||
const messageThreadId = parseThreadId(threadId);
|
||||
const result = await send(to, text, {
|
||||
verbose: false,
|
||||
mediaUrl,
|
||||
messageThreadId,
|
||||
replyToMessageId,
|
||||
accountId: accountId ?? undefined,
|
||||
});
|
||||
return { channel: "telegram", ...result };
|
||||
},
|
||||
},
|
||||
status: {
|
||||
defaultRuntime: {
|
||||
accountId: DEFAULT_ACCOUNT_ID,
|
||||
running: false,
|
||||
lastStartAt: null,
|
||||
lastStopAt: null,
|
||||
lastError: null,
|
||||
},
|
||||
collectStatusIssues: collectTelegramStatusIssues,
|
||||
buildChannelSummary: ({ snapshot }) => ({
|
||||
configured: snapshot.configured ?? false,
|
||||
tokenSource: snapshot.tokenSource ?? "none",
|
||||
running: snapshot.running ?? false,
|
||||
mode: snapshot.mode ?? null,
|
||||
lastStartAt: snapshot.lastStartAt ?? null,
|
||||
lastStopAt: snapshot.lastStopAt ?? null,
|
||||
lastError: snapshot.lastError ?? null,
|
||||
probe: snapshot.probe,
|
||||
lastProbeAt: snapshot.lastProbeAt ?? null,
|
||||
}),
|
||||
probeAccount: async ({ account, timeoutMs }) =>
|
||||
probeTelegram(account.token, timeoutMs, account.config.proxy),
|
||||
auditAccount: async ({ account, timeoutMs, probe, cfg }) => {
|
||||
const groups =
|
||||
cfg.channels?.telegram?.accounts?.[account.accountId]?.groups ??
|
||||
cfg.channels?.telegram?.groups;
|
||||
const { groupIds, unresolvedGroups, hasWildcardUnmentionedGroups } =
|
||||
collectTelegramUnmentionedGroupIds(groups);
|
||||
if (!groupIds.length && unresolvedGroups === 0 && !hasWildcardUnmentionedGroups) {
|
||||
return undefined;
|
||||
}
|
||||
const botId =
|
||||
(probe as { ok?: boolean; bot?: { id?: number } })?.ok &&
|
||||
(probe as { bot?: { id?: number } }).bot?.id != null
|
||||
? (probe as { bot: { id: number } }).bot.id
|
||||
: null;
|
||||
if (!botId) {
|
||||
return {
|
||||
ok: unresolvedGroups === 0 && !hasWildcardUnmentionedGroups,
|
||||
checkedGroups: 0,
|
||||
unresolvedGroups,
|
||||
hasWildcardUnmentionedGroups,
|
||||
groups: [],
|
||||
elapsedMs: 0,
|
||||
};
|
||||
}
|
||||
const audit = await auditTelegramGroupMembership({
|
||||
token: account.token,
|
||||
botId,
|
||||
groupIds,
|
||||
proxyUrl: account.config.proxy,
|
||||
timeoutMs,
|
||||
});
|
||||
return { ...audit, unresolvedGroups, hasWildcardUnmentionedGroups };
|
||||
},
|
||||
buildAccountSnapshot: ({ account, cfg, runtime, probe, audit }) => {
|
||||
const configured = Boolean(account.token?.trim());
|
||||
const groups =
|
||||
cfg.channels?.telegram?.accounts?.[account.accountId]?.groups ??
|
||||
cfg.channels?.telegram?.groups;
|
||||
const allowUnmentionedGroups =
|
||||
Boolean(
|
||||
groups?.["*"] && (groups["*"] as { requireMention?: boolean }).requireMention === false,
|
||||
) ||
|
||||
Object.entries(groups ?? {}).some(
|
||||
([key, value]) =>
|
||||
key !== "*" &&
|
||||
Boolean(value) &&
|
||||
typeof value === "object" &&
|
||||
(value as { requireMention?: boolean }).requireMention === false,
|
||||
);
|
||||
return {
|
||||
accountId: account.accountId,
|
||||
name: account.name,
|
||||
enabled: account.enabled,
|
||||
configured,
|
||||
tokenSource: account.tokenSource,
|
||||
running: runtime?.running ?? false,
|
||||
lastStartAt: runtime?.lastStartAt ?? null,
|
||||
lastStopAt: runtime?.lastStopAt ?? null,
|
||||
lastError: runtime?.lastError ?? null,
|
||||
mode: runtime?.mode ?? (account.config.webhookUrl ? "webhook" : "polling"),
|
||||
probe,
|
||||
audit,
|
||||
allowUnmentionedGroups,
|
||||
lastInboundAt: runtime?.lastInboundAt ?? null,
|
||||
lastOutboundAt: runtime?.lastOutboundAt ?? null,
|
||||
};
|
||||
},
|
||||
},
|
||||
gateway: {
|
||||
startAccount: async (ctx) => {
|
||||
const account = ctx.account;
|
||||
const token = account.token.trim();
|
||||
let telegramBotLabel = "";
|
||||
try {
|
||||
const probe = await probeTelegram(token, 2500, account.config.proxy);
|
||||
const username = probe.ok ? probe.bot?.username?.trim() : null;
|
||||
if (username) telegramBotLabel = ` (@${username})`;
|
||||
} catch (err) {
|
||||
if (shouldLogVerbose()) {
|
||||
ctx.log?.debug?.(`[${account.accountId}] bot probe failed: ${String(err)}`);
|
||||
}
|
||||
}
|
||||
ctx.log?.info(`[${account.accountId}] starting provider${telegramBotLabel}`);
|
||||
// Lazy import: the monitor pulls the reply pipeline; avoid ESM init cycles.
|
||||
const { monitorTelegramProvider } = await import("../../telegram/monitor.js");
|
||||
return monitorTelegramProvider({
|
||||
token,
|
||||
accountId: account.accountId,
|
||||
config: ctx.cfg,
|
||||
runtime: ctx.runtime,
|
||||
abortSignal: ctx.abortSignal,
|
||||
useWebhook: Boolean(account.config.webhookUrl),
|
||||
webhookUrl: account.config.webhookUrl,
|
||||
webhookSecret: account.config.webhookSecret,
|
||||
webhookPath: account.config.webhookPath,
|
||||
});
|
||||
},
|
||||
logoutAccount: async ({ accountId, cfg }) => {
|
||||
const envToken = process.env.TELEGRAM_BOT_TOKEN?.trim() ?? "";
|
||||
const nextCfg = { ...cfg } as ClawdbotConfig;
|
||||
const nextTelegram = cfg.channels?.telegram ? { ...cfg.channels.telegram } : undefined;
|
||||
let cleared = false;
|
||||
let changed = false;
|
||||
if (nextTelegram) {
|
||||
if (accountId === DEFAULT_ACCOUNT_ID && nextTelegram.botToken) {
|
||||
delete nextTelegram.botToken;
|
||||
cleared = true;
|
||||
changed = true;
|
||||
}
|
||||
const accounts =
|
||||
nextTelegram.accounts && typeof nextTelegram.accounts === "object"
|
||||
? { ...nextTelegram.accounts }
|
||||
: undefined;
|
||||
if (accounts && accountId in accounts) {
|
||||
const entry = accounts[accountId];
|
||||
if (entry && typeof entry === "object") {
|
||||
const nextEntry = { ...entry } as Record<string, unknown>;
|
||||
if ("botToken" in nextEntry) {
|
||||
const token = nextEntry.botToken;
|
||||
if (typeof token === "string" ? token.trim() : token) {
|
||||
cleared = true;
|
||||
}
|
||||
delete nextEntry.botToken;
|
||||
changed = true;
|
||||
}
|
||||
if (Object.keys(nextEntry).length === 0) {
|
||||
delete accounts[accountId];
|
||||
changed = true;
|
||||
} else {
|
||||
accounts[accountId] = nextEntry as typeof entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (accounts) {
|
||||
if (Object.keys(accounts).length === 0) {
|
||||
delete nextTelegram.accounts;
|
||||
changed = true;
|
||||
} else {
|
||||
nextTelegram.accounts = accounts;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
if (nextTelegram && Object.keys(nextTelegram).length > 0) {
|
||||
nextCfg.channels = { ...nextCfg.channels, telegram: nextTelegram };
|
||||
} else {
|
||||
const nextChannels = { ...nextCfg.channels };
|
||||
delete nextChannels.telegram;
|
||||
if (Object.keys(nextChannels).length > 0) {
|
||||
nextCfg.channels = nextChannels;
|
||||
} else {
|
||||
delete nextCfg.channels;
|
||||
}
|
||||
}
|
||||
}
|
||||
const resolved = resolveTelegramAccount({
|
||||
cfg: changed ? nextCfg : cfg,
|
||||
accountId,
|
||||
});
|
||||
const loggedOut = resolved.tokenSource === "none";
|
||||
if (changed) {
|
||||
await writeConfigFile(nextCfg);
|
||||
}
|
||||
return { cleared, envToken: Boolean(envToken), loggedOut };
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -1,500 +0,0 @@
|
||||
import { createActionGate, readStringParam } from "../../agents/tools/common.js";
|
||||
import { handleWhatsAppAction } from "../../agents/tools/whatsapp-actions.js";
|
||||
import { chunkText } from "../../auto-reply/chunk.js";
|
||||
import { shouldLogVerbose } from "../../globals.js";
|
||||
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../routing/session-key.js";
|
||||
import { normalizeE164 } from "../../utils.js";
|
||||
import {
|
||||
listWhatsAppAccountIds,
|
||||
type ResolvedWhatsAppAccount,
|
||||
resolveDefaultWhatsAppAccountId,
|
||||
resolveWhatsAppAccount,
|
||||
} from "../../web/accounts.js";
|
||||
import { getActiveWebListener } from "../../web/active-listener.js";
|
||||
import {
|
||||
getWebAuthAgeMs,
|
||||
logoutWeb,
|
||||
logWebSelfId,
|
||||
readWebSelfId,
|
||||
webAuthExists,
|
||||
} from "../../web/auth-store.js";
|
||||
import { sendMessageWhatsApp, sendPollWhatsApp } from "../../web/outbound.js";
|
||||
import { isWhatsAppGroupJid, normalizeWhatsAppTarget } from "../../whatsapp/normalize.js";
|
||||
import { getChatChannelMeta } from "../registry.js";
|
||||
import { WhatsAppConfigSchema } from "../../config/zod-schema.providers-whatsapp.js";
|
||||
import { buildChannelConfigSchema } from "./config-schema.js";
|
||||
import { createWhatsAppLoginTool } from "./agent-tools/whatsapp-login.js";
|
||||
import { resolveWhatsAppGroupRequireMention } from "./group-mentions.js";
|
||||
import { formatPairingApproveHint } from "./helpers.js";
|
||||
import {
|
||||
looksLikeWhatsAppTargetId,
|
||||
normalizeWhatsAppMessagingTarget,
|
||||
} from "./normalize/whatsapp.js";
|
||||
import { whatsappOnboardingAdapter } from "./onboarding/whatsapp.js";
|
||||
import {
|
||||
applyAccountNameToChannelSection,
|
||||
migrateBaseNameToDefaultAccount,
|
||||
} from "./setup-helpers.js";
|
||||
import { collectWhatsAppStatusIssues } from "./status-issues/whatsapp.js";
|
||||
import type { ChannelMessageActionName, ChannelPlugin } from "./types.js";
|
||||
import { resolveWhatsAppHeartbeatRecipients } from "./whatsapp-heartbeat.js";
|
||||
import { missingTargetError } from "../../infra/outbound/target-errors.js";
|
||||
import {
|
||||
listWhatsAppDirectoryGroupsFromConfig,
|
||||
listWhatsAppDirectoryPeersFromConfig,
|
||||
} from "./directory-config.js";
|
||||
|
||||
const meta = getChatChannelMeta("whatsapp");
|
||||
|
||||
const escapeRegExp = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
|
||||
export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> = {
|
||||
id: "whatsapp",
|
||||
meta: {
|
||||
...meta,
|
||||
showConfigured: false,
|
||||
quickstartAllowFrom: true,
|
||||
forceAccountBinding: true,
|
||||
preferSessionLookupForAnnounceTarget: true,
|
||||
},
|
||||
onboarding: whatsappOnboardingAdapter,
|
||||
agentTools: () => [createWhatsAppLoginTool()],
|
||||
pairing: {
|
||||
idLabel: "whatsappSenderId",
|
||||
},
|
||||
capabilities: {
|
||||
chatTypes: ["direct", "group"],
|
||||
polls: true,
|
||||
reactions: true,
|
||||
media: true,
|
||||
},
|
||||
reload: { configPrefixes: ["web"], noopPrefixes: ["channels.whatsapp"] },
|
||||
gatewayMethods: ["web.login.start", "web.login.wait"],
|
||||
configSchema: buildChannelConfigSchema(WhatsAppConfigSchema),
|
||||
config: {
|
||||
listAccountIds: (cfg) => listWhatsAppAccountIds(cfg),
|
||||
resolveAccount: (cfg, accountId) => resolveWhatsAppAccount({ cfg, accountId }),
|
||||
defaultAccountId: (cfg) => resolveDefaultWhatsAppAccountId(cfg),
|
||||
setAccountEnabled: ({ cfg, accountId, enabled }) => {
|
||||
const accountKey = accountId || DEFAULT_ACCOUNT_ID;
|
||||
const accounts = { ...cfg.channels?.whatsapp?.accounts };
|
||||
const existing = accounts[accountKey] ?? {};
|
||||
return {
|
||||
...cfg,
|
||||
channels: {
|
||||
...cfg.channels,
|
||||
whatsapp: {
|
||||
...cfg.channels?.whatsapp,
|
||||
accounts: {
|
||||
...accounts,
|
||||
[accountKey]: {
|
||||
...existing,
|
||||
enabled,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
deleteAccount: ({ cfg, accountId }) => {
|
||||
const accountKey = accountId || DEFAULT_ACCOUNT_ID;
|
||||
const accounts = { ...cfg.channels?.whatsapp?.accounts };
|
||||
delete accounts[accountKey];
|
||||
return {
|
||||
...cfg,
|
||||
channels: {
|
||||
...cfg.channels,
|
||||
whatsapp: {
|
||||
...cfg.channels?.whatsapp,
|
||||
accounts: Object.keys(accounts).length ? accounts : undefined,
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
isEnabled: (account, cfg) => account.enabled !== false && cfg.web?.enabled !== false,
|
||||
disabledReason: () => "disabled",
|
||||
isConfigured: async (account) => await webAuthExists(account.authDir),
|
||||
unconfiguredReason: () => "not linked",
|
||||
describeAccount: (account) => ({
|
||||
accountId: account.accountId,
|
||||
name: account.name,
|
||||
enabled: account.enabled,
|
||||
configured: Boolean(account.authDir),
|
||||
linked: Boolean(account.authDir),
|
||||
dmPolicy: account.dmPolicy,
|
||||
allowFrom: account.allowFrom,
|
||||
}),
|
||||
resolveAllowFrom: ({ cfg, accountId }) =>
|
||||
resolveWhatsAppAccount({ cfg, accountId }).allowFrom ?? [],
|
||||
formatAllowFrom: ({ allowFrom }) =>
|
||||
allowFrom
|
||||
.map((entry) => String(entry).trim())
|
||||
.filter((entry): entry is string => Boolean(entry))
|
||||
.map((entry) => (entry === "*" ? entry : normalizeWhatsAppTarget(entry)))
|
||||
.filter((entry): entry is string => Boolean(entry)),
|
||||
},
|
||||
security: {
|
||||
resolveDmPolicy: ({ cfg, accountId, account }) => {
|
||||
const resolvedAccountId = accountId ?? account.accountId ?? DEFAULT_ACCOUNT_ID;
|
||||
const useAccountPath = Boolean(cfg.channels?.whatsapp?.accounts?.[resolvedAccountId]);
|
||||
const basePath = useAccountPath
|
||||
? `channels.whatsapp.accounts.${resolvedAccountId}.`
|
||||
: "channels.whatsapp.";
|
||||
return {
|
||||
policy: account.dmPolicy ?? "pairing",
|
||||
allowFrom: account.allowFrom ?? [],
|
||||
policyPath: `${basePath}dmPolicy`,
|
||||
allowFromPath: basePath,
|
||||
approveHint: formatPairingApproveHint("whatsapp"),
|
||||
normalizeEntry: (raw) => normalizeE164(raw),
|
||||
};
|
||||
},
|
||||
collectWarnings: ({ account, cfg }) => {
|
||||
const defaultGroupPolicy = cfg.channels?.defaults?.groupPolicy;
|
||||
const groupPolicy = account.groupPolicy ?? defaultGroupPolicy ?? "allowlist";
|
||||
if (groupPolicy !== "open") return [];
|
||||
const groupAllowlistConfigured =
|
||||
Boolean(account.groups) && Object.keys(account.groups ?? {}).length > 0;
|
||||
if (groupAllowlistConfigured) {
|
||||
return [
|
||||
`- WhatsApp groups: groupPolicy="open" allows any member in allowed groups to trigger (mention-gated). Set channels.whatsapp.groupPolicy="allowlist" + channels.whatsapp.groupAllowFrom to restrict senders.`,
|
||||
];
|
||||
}
|
||||
return [
|
||||
`- WhatsApp groups: groupPolicy="open" with no channels.whatsapp.groups allowlist; any group can add + ping (mention-gated). Set channels.whatsapp.groupPolicy="allowlist" + channels.whatsapp.groupAllowFrom or configure channels.whatsapp.groups.`,
|
||||
];
|
||||
},
|
||||
},
|
||||
setup: {
|
||||
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
|
||||
applyAccountName: ({ cfg, accountId, name }) =>
|
||||
applyAccountNameToChannelSection({
|
||||
cfg,
|
||||
channelKey: "whatsapp",
|
||||
accountId,
|
||||
name,
|
||||
alwaysUseAccounts: true,
|
||||
}),
|
||||
applyAccountConfig: ({ cfg, accountId, input }) => {
|
||||
const namedConfig = applyAccountNameToChannelSection({
|
||||
cfg,
|
||||
channelKey: "whatsapp",
|
||||
accountId,
|
||||
name: input.name,
|
||||
alwaysUseAccounts: true,
|
||||
});
|
||||
const next = migrateBaseNameToDefaultAccount({
|
||||
cfg: namedConfig,
|
||||
channelKey: "whatsapp",
|
||||
alwaysUseAccounts: true,
|
||||
});
|
||||
const entry = {
|
||||
...next.channels?.whatsapp?.accounts?.[accountId],
|
||||
...(input.authDir ? { authDir: input.authDir } : {}),
|
||||
enabled: true,
|
||||
};
|
||||
return {
|
||||
...next,
|
||||
channels: {
|
||||
...next.channels,
|
||||
whatsapp: {
|
||||
...next.channels?.whatsapp,
|
||||
accounts: {
|
||||
...next.channels?.whatsapp?.accounts,
|
||||
[accountId]: entry,
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
groups: {
|
||||
resolveRequireMention: resolveWhatsAppGroupRequireMention,
|
||||
resolveGroupIntroHint: () =>
|
||||
"WhatsApp IDs: SenderId is the participant JID; [message_id: ...] is the message id for reactions (use SenderId as participant).",
|
||||
},
|
||||
mentions: {
|
||||
stripPatterns: ({ ctx }) => {
|
||||
const selfE164 = (ctx.To ?? "").replace(/^whatsapp:/, "");
|
||||
if (!selfE164) return [];
|
||||
const escaped = escapeRegExp(selfE164);
|
||||
return [escaped, `@${escaped}`];
|
||||
},
|
||||
},
|
||||
commands: {
|
||||
enforceOwnerForCommands: true,
|
||||
skipWhenConfigEmpty: true,
|
||||
},
|
||||
messaging: {
|
||||
normalizeTarget: normalizeWhatsAppMessagingTarget,
|
||||
targetResolver: {
|
||||
looksLikeId: looksLikeWhatsAppTargetId,
|
||||
hint: "<E.164|group JID>",
|
||||
},
|
||||
},
|
||||
directory: {
|
||||
self: async ({ cfg, accountId }) => {
|
||||
const account = resolveWhatsAppAccount({ cfg, accountId });
|
||||
const { e164, jid } = readWebSelfId(account.authDir);
|
||||
const id = e164 ?? jid;
|
||||
if (!id) return null;
|
||||
return {
|
||||
kind: "user",
|
||||
id,
|
||||
name: account.name,
|
||||
raw: { e164, jid },
|
||||
};
|
||||
},
|
||||
listPeers: async (params) => listWhatsAppDirectoryPeersFromConfig(params),
|
||||
listGroups: async (params) => listWhatsAppDirectoryGroupsFromConfig(params),
|
||||
},
|
||||
actions: {
|
||||
listActions: ({ cfg }) => {
|
||||
if (!cfg.channels?.whatsapp) return [];
|
||||
const gate = createActionGate(cfg.channels.whatsapp.actions);
|
||||
const actions = new Set<ChannelMessageActionName>();
|
||||
if (gate("reactions")) actions.add("react");
|
||||
if (gate("polls")) actions.add("poll");
|
||||
return Array.from(actions);
|
||||
},
|
||||
supportsAction: ({ action }) => action === "react",
|
||||
handleAction: async ({ action, params, cfg, accountId }) => {
|
||||
if (action !== "react") {
|
||||
throw new Error(`Action ${action} is not supported for provider ${meta.id}.`);
|
||||
}
|
||||
const messageId = readStringParam(params, "messageId", {
|
||||
required: true,
|
||||
});
|
||||
const emoji = readStringParam(params, "emoji", { allowEmpty: true });
|
||||
const remove = typeof params.remove === "boolean" ? params.remove : undefined;
|
||||
return await handleWhatsAppAction(
|
||||
{
|
||||
action: "react",
|
||||
chatJid:
|
||||
readStringParam(params, "chatJid") ?? readStringParam(params, "to", { required: true }),
|
||||
messageId,
|
||||
emoji,
|
||||
remove,
|
||||
participant: readStringParam(params, "participant"),
|
||||
accountId: accountId ?? undefined,
|
||||
fromMe: typeof params.fromMe === "boolean" ? params.fromMe : undefined,
|
||||
},
|
||||
cfg,
|
||||
);
|
||||
},
|
||||
},
|
||||
outbound: {
|
||||
deliveryMode: "gateway",
|
||||
chunker: chunkText,
|
||||
textChunkLimit: 4000,
|
||||
pollMaxOptions: 12,
|
||||
resolveTarget: ({ to, allowFrom, mode }) => {
|
||||
const trimmed = to?.trim() ?? "";
|
||||
const allowListRaw = (allowFrom ?? []).map((entry) => String(entry).trim()).filter(Boolean);
|
||||
const hasWildcard = allowListRaw.includes("*");
|
||||
const allowList = allowListRaw
|
||||
.filter((entry) => entry !== "*")
|
||||
.map((entry) => normalizeWhatsAppTarget(entry))
|
||||
.filter((entry): entry is string => Boolean(entry));
|
||||
|
||||
if (trimmed) {
|
||||
const normalizedTo = normalizeWhatsAppTarget(trimmed);
|
||||
if (!normalizedTo) {
|
||||
if ((mode === "implicit" || mode === "heartbeat") && allowList.length > 0) {
|
||||
return { ok: true, to: allowList[0] };
|
||||
}
|
||||
return {
|
||||
ok: false,
|
||||
error: missingTargetError(
|
||||
"WhatsApp",
|
||||
"<E.164|group JID> or channels.whatsapp.allowFrom[0]",
|
||||
),
|
||||
};
|
||||
}
|
||||
if (isWhatsAppGroupJid(normalizedTo)) {
|
||||
return { ok: true, to: normalizedTo };
|
||||
}
|
||||
if (mode === "implicit" || mode === "heartbeat") {
|
||||
if (hasWildcard || allowList.length === 0) {
|
||||
return { ok: true, to: normalizedTo };
|
||||
}
|
||||
if (allowList.includes(normalizedTo)) {
|
||||
return { ok: true, to: normalizedTo };
|
||||
}
|
||||
return { ok: true, to: allowList[0] };
|
||||
}
|
||||
return { ok: true, to: normalizedTo };
|
||||
}
|
||||
|
||||
if (allowList.length > 0) {
|
||||
return { ok: true, to: allowList[0] };
|
||||
}
|
||||
return {
|
||||
ok: false,
|
||||
error: missingTargetError(
|
||||
"WhatsApp",
|
||||
"<E.164|group JID> or channels.whatsapp.allowFrom[0]",
|
||||
),
|
||||
};
|
||||
},
|
||||
sendText: async ({ to, text, accountId, deps, gifPlayback }) => {
|
||||
const send = deps?.sendWhatsApp ?? sendMessageWhatsApp;
|
||||
const result = await send(to, text, {
|
||||
verbose: false,
|
||||
accountId: accountId ?? undefined,
|
||||
gifPlayback,
|
||||
});
|
||||
return { channel: "whatsapp", ...result };
|
||||
},
|
||||
sendMedia: async ({ to, text, mediaUrl, accountId, deps, gifPlayback }) => {
|
||||
const send = deps?.sendWhatsApp ?? sendMessageWhatsApp;
|
||||
const result = await send(to, text, {
|
||||
verbose: false,
|
||||
mediaUrl,
|
||||
accountId: accountId ?? undefined,
|
||||
gifPlayback,
|
||||
});
|
||||
return { channel: "whatsapp", ...result };
|
||||
},
|
||||
sendPoll: async ({ to, poll, accountId }) =>
|
||||
await sendPollWhatsApp(to, poll, {
|
||||
verbose: shouldLogVerbose(),
|
||||
accountId: accountId ?? undefined,
|
||||
}),
|
||||
},
|
||||
auth: {
|
||||
login: async ({ cfg, accountId, runtime, verbose }) => {
|
||||
const resolvedAccountId = accountId?.trim() || resolveDefaultWhatsAppAccountId(cfg);
|
||||
const { loginWeb } = await import("../../web/login.js");
|
||||
await loginWeb(Boolean(verbose), undefined, runtime, resolvedAccountId);
|
||||
},
|
||||
},
|
||||
heartbeat: {
|
||||
checkReady: async ({ cfg, accountId, deps }) => {
|
||||
if (cfg.web?.enabled === false) {
|
||||
return { ok: false, reason: "whatsapp-disabled" };
|
||||
}
|
||||
const account = resolveWhatsAppAccount({ cfg, accountId });
|
||||
const authExists = await (deps?.webAuthExists ?? webAuthExists)(account.authDir);
|
||||
if (!authExists) {
|
||||
return { ok: false, reason: "whatsapp-not-linked" };
|
||||
}
|
||||
const listenerActive = deps?.hasActiveWebListener
|
||||
? deps.hasActiveWebListener()
|
||||
: Boolean(getActiveWebListener());
|
||||
if (!listenerActive) {
|
||||
return { ok: false, reason: "whatsapp-not-running" };
|
||||
}
|
||||
return { ok: true, reason: "ok" };
|
||||
},
|
||||
resolveRecipients: ({ cfg, opts }) => resolveWhatsAppHeartbeatRecipients(cfg, opts),
|
||||
},
|
||||
status: {
|
||||
defaultRuntime: {
|
||||
accountId: DEFAULT_ACCOUNT_ID,
|
||||
running: false,
|
||||
connected: false,
|
||||
reconnectAttempts: 0,
|
||||
lastConnectedAt: null,
|
||||
lastDisconnect: null,
|
||||
lastMessageAt: null,
|
||||
lastEventAt: null,
|
||||
lastError: null,
|
||||
},
|
||||
collectStatusIssues: collectWhatsAppStatusIssues,
|
||||
buildChannelSummary: async ({ account, snapshot }) => {
|
||||
const authDir = account.authDir;
|
||||
const linked =
|
||||
typeof snapshot.linked === "boolean"
|
||||
? snapshot.linked
|
||||
: authDir
|
||||
? await webAuthExists(authDir)
|
||||
: false;
|
||||
const authAgeMs = linked && authDir ? getWebAuthAgeMs(authDir) : null;
|
||||
const self = linked && authDir ? readWebSelfId(authDir) : { e164: null, jid: null };
|
||||
return {
|
||||
configured: linked,
|
||||
linked,
|
||||
authAgeMs,
|
||||
self,
|
||||
running: snapshot.running ?? false,
|
||||
connected: snapshot.connected ?? false,
|
||||
lastConnectedAt: snapshot.lastConnectedAt ?? null,
|
||||
lastDisconnect: snapshot.lastDisconnect ?? null,
|
||||
reconnectAttempts: snapshot.reconnectAttempts,
|
||||
lastMessageAt: snapshot.lastMessageAt ?? null,
|
||||
lastEventAt: snapshot.lastEventAt ?? null,
|
||||
lastError: snapshot.lastError ?? null,
|
||||
};
|
||||
},
|
||||
buildAccountSnapshot: async ({ account, runtime }) => {
|
||||
const linked = await webAuthExists(account.authDir);
|
||||
return {
|
||||
accountId: account.accountId,
|
||||
name: account.name,
|
||||
enabled: account.enabled,
|
||||
configured: true,
|
||||
linked,
|
||||
running: runtime?.running ?? false,
|
||||
connected: runtime?.connected ?? false,
|
||||
reconnectAttempts: runtime?.reconnectAttempts,
|
||||
lastConnectedAt: runtime?.lastConnectedAt ?? null,
|
||||
lastDisconnect: runtime?.lastDisconnect ?? null,
|
||||
lastMessageAt: runtime?.lastMessageAt ?? null,
|
||||
lastEventAt: runtime?.lastEventAt ?? null,
|
||||
lastError: runtime?.lastError ?? null,
|
||||
dmPolicy: account.dmPolicy,
|
||||
allowFrom: account.allowFrom,
|
||||
};
|
||||
},
|
||||
resolveAccountState: ({ configured }) => (configured ? "linked" : "not linked"),
|
||||
logSelfId: ({ account, runtime, includeChannelPrefix }) => {
|
||||
logWebSelfId(account.authDir, runtime, includeChannelPrefix);
|
||||
},
|
||||
},
|
||||
gateway: {
|
||||
startAccount: async (ctx) => {
|
||||
const account = ctx.account;
|
||||
const { e164, jid } = readWebSelfId(account.authDir);
|
||||
const identity = e164 ? e164 : jid ? `jid ${jid}` : "unknown";
|
||||
ctx.log?.info(`[${account.accountId}] starting provider (${identity})`);
|
||||
// Lazy import: the monitor pulls the reply pipeline; avoid ESM init cycles.
|
||||
const { monitorWebChannel } = await import("../web/index.js");
|
||||
return monitorWebChannel(
|
||||
shouldLogVerbose(),
|
||||
undefined,
|
||||
true,
|
||||
undefined,
|
||||
ctx.runtime,
|
||||
ctx.abortSignal,
|
||||
{
|
||||
statusSink: (next) => ctx.setStatus({ accountId: ctx.accountId, ...next }),
|
||||
accountId: account.accountId,
|
||||
},
|
||||
);
|
||||
},
|
||||
loginWithQrStart: async ({ accountId, force, timeoutMs, verbose }) =>
|
||||
await (async () => {
|
||||
const { startWebLoginWithQr } = await import("../../web/login-qr.js");
|
||||
return await startWebLoginWithQr({
|
||||
accountId,
|
||||
force,
|
||||
timeoutMs,
|
||||
verbose,
|
||||
});
|
||||
})(),
|
||||
loginWithQrWait: async ({ accountId, timeoutMs }) =>
|
||||
await (async () => {
|
||||
const { waitForWebLogin } = await import("../../web/login-qr.js");
|
||||
return await waitForWebLogin({ accountId, timeoutMs });
|
||||
})(),
|
||||
logoutAccount: async ({ account, runtime }) => {
|
||||
const cleared = await logoutWeb({
|
||||
authDir: account.authDir,
|
||||
isLegacyAuthDir: account.isLegacyAuthDir,
|
||||
runtime,
|
||||
});
|
||||
return { cleared, loggedOut: cleared };
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { ChannelMeta } from "./plugins/types.js";
|
||||
import type { ChannelId } from "./plugins/types.js";
|
||||
import { getActivePluginRegistry } from "../plugins/runtime.js";
|
||||
import { requireActivePluginRegistry } from "../plugins/runtime.js";
|
||||
|
||||
// Channel docking: add new core channels here (order + meta + aliases), then
|
||||
// register the plugin in its extension entrypoint and keep protocol IDs in sync.
|
||||
@@ -113,21 +113,15 @@ export function normalizeChannelId(raw?: string | null): ChatChannelId | null {
|
||||
return normalizeChatChannelId(raw);
|
||||
}
|
||||
|
||||
// Normalizes core chat channels plus any *already-loaded* plugin channels.
|
||||
// Normalizes registered channel plugins (bundled or external).
|
||||
//
|
||||
// Keep this light: we do not import core channel plugins here (those are "heavy" and can pull in
|
||||
// monitors, web login, etc). If plugins are not loaded (e.g. in many tests), only core channel IDs
|
||||
// resolve.
|
||||
// Keep this light: we do not import channel plugins here (those are "heavy" and can pull in
|
||||
// monitors, web login, etc). The plugin registry must be initialized first.
|
||||
export function normalizeAnyChannelId(raw?: string | null): ChannelId | null {
|
||||
const core = normalizeChatChannelId(raw);
|
||||
if (core) return core;
|
||||
|
||||
const key = normalizeChannelKey(raw);
|
||||
if (!key) return null;
|
||||
|
||||
const registry = getActivePluginRegistry();
|
||||
if (!registry) return null;
|
||||
|
||||
const registry = requireActivePluginRegistry();
|
||||
const hit = registry.channels.find((entry) => {
|
||||
const id = String(entry.plugin.id ?? "")
|
||||
.trim()
|
||||
|
||||
@@ -20,9 +20,11 @@ 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 { createPluginRuntime } from "../plugins/runtime/index.js";
|
||||
import { createTestRegistry } from "../test-utils/channel-plugins.js";
|
||||
import { agentCommand } from "./agent.js";
|
||||
import { telegramPlugin } from "../../extensions/telegram/src/channel.js";
|
||||
import { setTelegramRuntime } from "../../extensions/telegram/src/runtime.js";
|
||||
|
||||
const runtime: RuntimeEnv = {
|
||||
log: vi.fn(),
|
||||
@@ -254,6 +256,7 @@ describe("agentCommand", () => {
|
||||
await withTempHome(async (home) => {
|
||||
const store = path.join(home, "sessions.json");
|
||||
mockConfig(home, store, undefined, { botToken: "t-1" });
|
||||
setTelegramRuntime(createPluginRuntime());
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([{ pluginId: "telegram", plugin: telegramPlugin, source: "test" }]),
|
||||
);
|
||||
|
||||
@@ -25,6 +25,7 @@ vi.mock("../config/sessions.js", () => ({
|
||||
resolveStorePath: () => "/tmp/sessions.json",
|
||||
loadSessionStore: () => testStore,
|
||||
recordSessionMetaFromInbound: vi.fn().mockResolvedValue(undefined),
|
||||
updateLastRoute: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
vi.mock("../web/auth-store.js", () => ({
|
||||
@@ -32,13 +33,17 @@ vi.mock("../web/auth-store.js", () => ({
|
||||
getWebAuthAgeMs: vi.fn(() => 1234),
|
||||
readWebSelfId: vi.fn(() => ({ e164: null, jid: null })),
|
||||
logWebSelfId: vi.fn(),
|
||||
logoutWeb: vi.fn(),
|
||||
}));
|
||||
|
||||
describe("getHealthSnapshot", () => {
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([{ pluginId: "telegram", plugin: telegramPlugin, source: "test" }]),
|
||||
);
|
||||
const { createPluginRuntime } = await import("../plugins/runtime/index.js");
|
||||
const { setTelegramRuntime } = await import("../../extensions/telegram/src/runtime.js");
|
||||
setTelegramRuntime(createPluginRuntime());
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
@@ -7,11 +7,11 @@ import type { ClawdbotConfig } from "./config.js";
|
||||
|
||||
describe("resolveChannelCapabilities", () => {
|
||||
beforeEach(() => {
|
||||
setActivePluginRegistry(emptyRegistry);
|
||||
setActivePluginRegistry(baseRegistry);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
setActivePluginRegistry(emptyRegistry);
|
||||
setActivePluginRegistry(baseRegistry);
|
||||
});
|
||||
|
||||
it("returns undefined for missing inputs", () => {
|
||||
@@ -139,7 +139,26 @@ const createRegistry = (channels: PluginRegistry["channels"]): PluginRegistry =>
|
||||
diagnostics: [],
|
||||
});
|
||||
|
||||
const emptyRegistry = createRegistry([]);
|
||||
const createStubPlugin = (id: string): ChannelPlugin => ({
|
||||
id,
|
||||
meta: {
|
||||
id,
|
||||
label: id,
|
||||
selectionLabel: id,
|
||||
docsPath: `/channels/${id}`,
|
||||
blurb: "test stub.",
|
||||
},
|
||||
capabilities: { chatTypes: ["direct"] },
|
||||
config: {
|
||||
listAccountIds: () => [],
|
||||
resolveAccount: () => ({}),
|
||||
},
|
||||
});
|
||||
|
||||
const baseRegistry = createRegistry([
|
||||
{ pluginId: "telegram", source: "test", plugin: createStubPlugin("telegram") },
|
||||
{ pluginId: "slack", source: "test", plugin: createStubPlugin("slack") },
|
||||
]);
|
||||
|
||||
const createMSTeamsPlugin = (): ChannelPlugin => ({
|
||||
id: "msteams",
|
||||
|
||||
@@ -7,11 +7,15 @@ import { withTempHome as withTempHomeBase } from "../../test/helpers/temp-home.j
|
||||
import type { CliDeps } from "../cli/deps.js";
|
||||
import type { ClawdbotConfig } from "../config/config.js";
|
||||
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
||||
import { createPluginRuntime } from "../plugins/runtime/index.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";
|
||||
import { setDiscordRuntime } from "../../extensions/discord/src/runtime.js";
|
||||
import { setTelegramRuntime } from "../../extensions/telegram/src/runtime.js";
|
||||
import { setWhatsAppRuntime } from "../../extensions/whatsapp/src/runtime.js";
|
||||
|
||||
vi.mock("../agents/pi-embedded.js", () => ({
|
||||
abortEmbeddedPiRun: vi.fn().mockReturnValue(false),
|
||||
@@ -90,6 +94,10 @@ describe("runCronIsolatedAgentTurn", () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(runEmbeddedPiAgent).mockReset();
|
||||
vi.mocked(loadModelCatalog).mockResolvedValue([]);
|
||||
const runtime = createPluginRuntime();
|
||||
setDiscordRuntime(runtime);
|
||||
setTelegramRuntime(runtime);
|
||||
setWhatsAppRuntime(runtime);
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([
|
||||
{ pluginId: "whatsapp", plugin: whatsappPlugin, source: "test" },
|
||||
|
||||
@@ -1,8 +1,18 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { listChatCommands } from "../auto-reply/commands-registry.js";
|
||||
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
||||
import { createTestRegistry } from "../test-utils/channel-plugins.js";
|
||||
|
||||
beforeEach(() => {
|
||||
setActivePluginRegistry(createTestRegistry([]));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
setActivePluginRegistry(createTestRegistry([]));
|
||||
});
|
||||
|
||||
function extractDocumentedSlashCommands(markdown: string): Set<string> {
|
||||
const documented = new Set<string>();
|
||||
|
||||
@@ -346,6 +346,8 @@ vi.mock("../commands/status.js", () => ({
|
||||
vi.mock("../web/outbound.js", () => ({
|
||||
sendMessageWhatsApp: (...args: unknown[]) =>
|
||||
(hoisted.sendWhatsAppMock as (...args: unknown[]) => unknown)(...args),
|
||||
sendPollWhatsApp: (...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")>(
|
||||
|
||||
@@ -7,14 +7,20 @@ 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 { createPluginRuntime } from "../plugins/runtime/index.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 { setTelegramRuntime } from "../../extensions/telegram/src/runtime.js";
|
||||
import { setWhatsAppRuntime } from "../../extensions/whatsapp/src/runtime.js";
|
||||
|
||||
// Avoid pulling optional runtime deps during isolated runs.
|
||||
vi.mock("jiti", () => ({ createJiti: () => () => ({}) }));
|
||||
|
||||
beforeEach(() => {
|
||||
const runtime = createPluginRuntime();
|
||||
setTelegramRuntime(runtime);
|
||||
setWhatsAppRuntime(runtime);
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([
|
||||
{ pluginId: "whatsapp", plugin: whatsappPlugin, source: "test" },
|
||||
|
||||
@@ -19,14 +19,20 @@ import {
|
||||
} from "./heartbeat-runner.js";
|
||||
import { resolveHeartbeatDeliveryTarget } from "./outbound/targets.js";
|
||||
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
||||
import { createPluginRuntime } from "../plugins/runtime/index.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 { setTelegramRuntime } from "../../extensions/telegram/src/runtime.js";
|
||||
import { setWhatsAppRuntime } from "../../extensions/whatsapp/src/runtime.js";
|
||||
|
||||
// Avoid pulling optional runtime deps during isolated runs.
|
||||
vi.mock("jiti", () => ({ createJiti: () => () => ({}) }));
|
||||
|
||||
beforeEach(() => {
|
||||
const runtime = createPluginRuntime();
|
||||
setTelegramRuntime(runtime);
|
||||
setWhatsAppRuntime(runtime);
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([
|
||||
{ pluginId: "whatsapp", plugin: whatsappPlugin, source: "test" },
|
||||
|
||||
@@ -26,7 +26,15 @@ const whatsappConfig = {
|
||||
} as ClawdbotConfig;
|
||||
|
||||
describe("runMessageAction context isolation", () => {
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
const { createPluginRuntime } = await import("../../plugins/runtime/index.js");
|
||||
const { setSlackRuntime } = await import("../../../extensions/slack/src/runtime.js");
|
||||
const { setTelegramRuntime } = await import("../../../extensions/telegram/src/runtime.js");
|
||||
const { setWhatsAppRuntime } = await import("../../../extensions/whatsapp/src/runtime.js");
|
||||
const runtime = createPluginRuntime();
|
||||
setSlackRuntime(runtime);
|
||||
setTelegramRuntime(runtime);
|
||||
setWhatsAppRuntime(runtime);
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([
|
||||
{
|
||||
|
||||
51
src/plugin-sdk/index.test.ts
Normal file
51
src/plugin-sdk/index.test.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import * as sdk from "./index.js";
|
||||
|
||||
describe("plugin-sdk exports", () => {
|
||||
it("does not expose runtime modules", () => {
|
||||
const forbidden = [
|
||||
"chunkMarkdownText",
|
||||
"chunkText",
|
||||
"resolveTextChunkLimit",
|
||||
"hasControlCommand",
|
||||
"isControlCommandMessage",
|
||||
"shouldComputeCommandAuthorized",
|
||||
"shouldHandleTextCommands",
|
||||
"buildMentionRegexes",
|
||||
"matchesMentionPatterns",
|
||||
"resolveStateDir",
|
||||
"loadConfig",
|
||||
"writeConfigFile",
|
||||
"runCommandWithTimeout",
|
||||
"enqueueSystemEvent",
|
||||
"detectMime",
|
||||
"fetchRemoteMedia",
|
||||
"saveMediaBuffer",
|
||||
"formatAgentEnvelope",
|
||||
"buildPairingReply",
|
||||
"resolveAgentRoute",
|
||||
"dispatchReplyFromConfig",
|
||||
"createReplyDispatcherWithTyping",
|
||||
"dispatchReplyWithBufferedBlockDispatcher",
|
||||
"resolveCommandAuthorizedFromAuthorizers",
|
||||
"monitorSlackProvider",
|
||||
"monitorTelegramProvider",
|
||||
"monitorIMessageProvider",
|
||||
"monitorSignalProvider",
|
||||
"sendMessageSlack",
|
||||
"sendMessageTelegram",
|
||||
"sendMessageIMessage",
|
||||
"sendMessageSignal",
|
||||
"sendMessageWhatsApp",
|
||||
"probeSlack",
|
||||
"probeTelegram",
|
||||
"probeIMessage",
|
||||
"probeSignal",
|
||||
];
|
||||
|
||||
for (const key of forbidden) {
|
||||
expect(Object.prototype.hasOwnProperty.call(sdk, key)).toBe(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -80,20 +80,6 @@ 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, chunkText, resolveTextChunkLimit } from "../auto-reply/chunk.js";
|
||||
export {
|
||||
hasControlCommand,
|
||||
isControlCommandMessage,
|
||||
shouldComputeCommandAuthorized,
|
||||
} from "../auto-reply/command-detection.js";
|
||||
export { shouldHandleTextCommands } from "../auto-reply/commands-registry.js";
|
||||
export { formatAgentEnvelope } from "../auto-reply/envelope.js";
|
||||
export {
|
||||
createInboundDebouncer,
|
||||
resolveInboundDebounceMs,
|
||||
} from "../auto-reply/inbound-debounce.js";
|
||||
export { dispatchReplyFromConfig } from "../auto-reply/reply/dispatch-from-config.js";
|
||||
export { finalizeInboundContext } from "../auto-reply/reply/inbound-context.js";
|
||||
export {
|
||||
buildPendingHistoryContextFromMap,
|
||||
clearHistoryEntries,
|
||||
@@ -101,11 +87,7 @@ export {
|
||||
recordPendingHistoryEntry,
|
||||
} from "../auto-reply/reply/history.js";
|
||||
export type { HistoryEntry } from "../auto-reply/reply/history.js";
|
||||
export { buildMentionRegexes, matchesMentionPatterns } from "../auto-reply/reply/mentions.js";
|
||||
export { createReplyDispatcherWithTyping } from "../auto-reply/reply/reply-dispatcher.js";
|
||||
export { resolveEffectiveMessagesConfig, resolveHumanDelayConfig } from "../agents/identity.js";
|
||||
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 {
|
||||
@@ -134,30 +116,7 @@ export {
|
||||
} from "../channels/plugins/directory-config.js";
|
||||
export type { AllowlistMatch } from "../channels/plugins/allowlist-match.js";
|
||||
export { formatAllowlistMatchMeta } from "../channels/plugins/allowlist-match.js";
|
||||
export {
|
||||
readChannelAllowFromStore,
|
||||
upsertChannelPairingRequest,
|
||||
} from "../pairing/pairing-store.js";
|
||||
export { resolveAgentRoute } from "../routing/resolve-route.js";
|
||||
export {
|
||||
recordSessionMetaFromInbound,
|
||||
resolveStorePath,
|
||||
updateLastRoute,
|
||||
} from "../config/sessions.js";
|
||||
export { resolveStateDir } from "../config/paths.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";
|
||||
export { getChildLogger } from "../logging.js";
|
||||
export { enqueueSystemEvent } from "../infra/system-events.js";
|
||||
export { runCommandWithTimeout } from "../process/exec.js";
|
||||
export { loadWebMedia } from "../web/media.js";
|
||||
export { isVoiceCompatibleAudio } from "../media/audio.js";
|
||||
export { mediaKindFromMime } from "../media/constants.js";
|
||||
export { detectMime } from "../media/mime.js";
|
||||
export { getImageMetadata, resizeToJpeg } from "../media/image-ops.js";
|
||||
export { saveMediaBuffer } from "../media/store.js";
|
||||
export type { PollInput } from "../polls.js";
|
||||
|
||||
export { buildChannelConfigSchema } from "../channels/plugins/config-schema.js";
|
||||
@@ -177,9 +136,6 @@ export {
|
||||
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,
|
||||
@@ -196,12 +152,8 @@ export {
|
||||
readReactionParams,
|
||||
readStringParam,
|
||||
} from "../agents/tools/common.js";
|
||||
export { createMemoryGetTool, createMemorySearchTool } from "../agents/tools/memory-tool.js";
|
||||
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";
|
||||
|
||||
@@ -212,17 +164,7 @@ export {
|
||||
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 { collectDiscordAuditChannelIds } from "../discord/audit.js";
|
||||
export { discordOnboardingAdapter } from "../channels/plugins/onboarding/discord.js";
|
||||
export {
|
||||
looksLikeDiscordTargetId,
|
||||
@@ -238,16 +180,6 @@ export {
|
||||
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,
|
||||
@@ -261,15 +193,6 @@ export {
|
||||
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,
|
||||
@@ -284,9 +207,6 @@ export {
|
||||
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,
|
||||
@@ -300,21 +220,7 @@ export {
|
||||
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 {
|
||||
|
||||
@@ -1,17 +1,55 @@
|
||||
import type { PluginRegistry } from "./registry.js";
|
||||
|
||||
let activeRegistry: PluginRegistry | null = null;
|
||||
let activeRegistryKey: string | null = null;
|
||||
const createEmptyRegistry = (): PluginRegistry => ({
|
||||
plugins: [],
|
||||
tools: [],
|
||||
hooks: [],
|
||||
typedHooks: [],
|
||||
channels: [],
|
||||
providers: [],
|
||||
gatewayHandlers: {},
|
||||
httpHandlers: [],
|
||||
cliRegistrars: [],
|
||||
services: [],
|
||||
diagnostics: [],
|
||||
});
|
||||
|
||||
const REGISTRY_STATE = Symbol.for("clawdbot.pluginRegistryState");
|
||||
|
||||
type RegistryState = {
|
||||
registry: PluginRegistry | null;
|
||||
key: string | null;
|
||||
};
|
||||
|
||||
const state: RegistryState = (() => {
|
||||
const globalState = globalThis as typeof globalThis & {
|
||||
[REGISTRY_STATE]?: RegistryState;
|
||||
};
|
||||
if (!globalState[REGISTRY_STATE]) {
|
||||
globalState[REGISTRY_STATE] = {
|
||||
registry: createEmptyRegistry(),
|
||||
key: null,
|
||||
};
|
||||
}
|
||||
return globalState[REGISTRY_STATE] as RegistryState;
|
||||
})();
|
||||
|
||||
export function setActivePluginRegistry(registry: PluginRegistry, cacheKey?: string) {
|
||||
activeRegistry = registry;
|
||||
activeRegistryKey = cacheKey ?? null;
|
||||
state.registry = registry;
|
||||
state.key = cacheKey ?? null;
|
||||
}
|
||||
|
||||
export function getActivePluginRegistry(): PluginRegistry | null {
|
||||
return activeRegistry;
|
||||
return state.registry;
|
||||
}
|
||||
|
||||
export function requireActivePluginRegistry(): PluginRegistry {
|
||||
if (!state.registry) {
|
||||
state.registry = createEmptyRegistry();
|
||||
}
|
||||
return state.registry;
|
||||
}
|
||||
|
||||
export function getActivePluginRegistryKey(): string | null {
|
||||
return activeRegistryKey;
|
||||
return state.key;
|
||||
}
|
||||
|
||||
@@ -1,32 +1,99 @@
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
import { chunkMarkdownText, resolveTextChunkLimit } from "../../auto-reply/chunk.js";
|
||||
import { hasControlCommand } from "../../auto-reply/command-detection.js";
|
||||
import { chunkMarkdownText, chunkText, resolveTextChunkLimit } from "../../auto-reply/chunk.js";
|
||||
import {
|
||||
hasControlCommand,
|
||||
isControlCommandMessage,
|
||||
shouldComputeCommandAuthorized,
|
||||
} from "../../auto-reply/command-detection.js";
|
||||
import { shouldHandleTextCommands } from "../../auto-reply/commands-registry.js";
|
||||
import {
|
||||
createInboundDebouncer,
|
||||
resolveInboundDebounceMs,
|
||||
} from "../../auto-reply/inbound-debounce.js";
|
||||
import { formatAgentEnvelope } from "../../auto-reply/envelope.js";
|
||||
import { dispatchReplyFromConfig } from "../../auto-reply/reply/dispatch-from-config.js";
|
||||
import { buildMentionRegexes, matchesMentionPatterns } from "../../auto-reply/reply/mentions.js";
|
||||
import { dispatchReplyWithBufferedBlockDispatcher } from "../../auto-reply/reply/provider-dispatcher.js";
|
||||
import { createReplyDispatcherWithTyping } from "../../auto-reply/reply/reply-dispatcher.js";
|
||||
import { finalizeInboundContext } from "../../auto-reply/reply/inbound-context.js";
|
||||
import { resolveEffectiveMessagesConfig, resolveHumanDelayConfig } from "../../agents/identity.js";
|
||||
import { createMemoryGetTool, createMemorySearchTool } from "../../agents/tools/memory-tool.js";
|
||||
import { handleSlackAction } from "../../agents/tools/slack-actions.js";
|
||||
import { handleWhatsAppAction } from "../../agents/tools/whatsapp-actions.js";
|
||||
import { resolveCommandAuthorizedFromAuthorizers } from "../../channels/command-gating.js";
|
||||
import { discordMessageActions } from "../../channels/plugins/actions/discord.js";
|
||||
import { telegramMessageActions } from "../../channels/plugins/actions/telegram.js";
|
||||
import { createWhatsAppLoginTool } from "../../channels/plugins/agent-tools/whatsapp-login.js";
|
||||
import { monitorWebChannel } from "../../channels/web/index.js";
|
||||
import {
|
||||
resolveChannelGroupPolicy,
|
||||
resolveChannelGroupRequireMention,
|
||||
} from "../../config/group-policy.js";
|
||||
import { resolveStateDir } from "../../config/paths.js";
|
||||
import { loadConfig, writeConfigFile } from "../../config/config.js";
|
||||
import {
|
||||
recordSessionMetaFromInbound,
|
||||
resolveStorePath,
|
||||
updateLastRoute,
|
||||
} from "../../config/sessions.js";
|
||||
import { auditDiscordChannelPermissions } from "../../discord/audit.js";
|
||||
import { listDiscordDirectoryGroupsLive, listDiscordDirectoryPeersLive } from "../../discord/directory-live.js";
|
||||
import { monitorDiscordProvider } from "../../discord/monitor.js";
|
||||
import { probeDiscord } from "../../discord/probe.js";
|
||||
import { resolveDiscordChannelAllowlist } from "../../discord/resolve-channels.js";
|
||||
import { resolveDiscordUserAllowlist } from "../../discord/resolve-users.js";
|
||||
import { sendMessageDiscord, sendPollDiscord } from "../../discord/send.js";
|
||||
import { enqueueSystemEvent } from "../../infra/system-events.js";
|
||||
import { monitorIMessageProvider } from "../../imessage/monitor.js";
|
||||
import { probeIMessage } from "../../imessage/probe.js";
|
||||
import { sendMessageIMessage } from "../../imessage/send.js";
|
||||
import { shouldLogVerbose } from "../../globals.js";
|
||||
import { getChildLogger } from "../../logging.js";
|
||||
import { normalizeLogLevel } from "../../logging/levels.js";
|
||||
import { isVoiceCompatibleAudio } from "../../media/audio.js";
|
||||
import { mediaKindFromMime } from "../../media/constants.js";
|
||||
import { fetchRemoteMedia } from "../../media/fetch.js";
|
||||
import { getImageMetadata, resizeToJpeg } from "../../media/image-ops.js";
|
||||
import { detectMime } from "../../media/mime.js";
|
||||
import { saveMediaBuffer } from "../../media/store.js";
|
||||
import { buildPairingReply } from "../../pairing/pairing-messages.js";
|
||||
import {
|
||||
readChannelAllowFromStore,
|
||||
upsertChannelPairingRequest,
|
||||
} from "../../pairing/pairing-store.js";
|
||||
import { runCommandWithTimeout } from "../../process/exec.js";
|
||||
import { resolveAgentRoute } from "../../routing/resolve-route.js";
|
||||
import { monitorSignalProvider } from "../../signal/index.js";
|
||||
import { probeSignal } from "../../signal/probe.js";
|
||||
import { sendMessageSignal } from "../../signal/send.js";
|
||||
import { monitorSlackProvider } from "../../slack/index.js";
|
||||
import { listSlackDirectoryGroupsLive, listSlackDirectoryPeersLive } from "../../slack/directory-live.js";
|
||||
import { probeSlack } from "../../slack/probe.js";
|
||||
import { resolveSlackChannelAllowlist } from "../../slack/resolve-channels.js";
|
||||
import { resolveSlackUserAllowlist } from "../../slack/resolve-users.js";
|
||||
import { sendMessageSlack } from "../../slack/send.js";
|
||||
import {
|
||||
auditTelegramGroupMembership,
|
||||
collectTelegramUnmentionedGroupIds,
|
||||
} from "../../telegram/audit.js";
|
||||
import { monitorTelegramProvider } from "../../telegram/monitor.js";
|
||||
import { probeTelegram } from "../../telegram/probe.js";
|
||||
import { sendMessageTelegram } from "../../telegram/send.js";
|
||||
import { resolveTelegramToken } from "../../telegram/token.js";
|
||||
import { loadWebMedia } from "../../web/media.js";
|
||||
import { getActiveWebListener } from "../../web/active-listener.js";
|
||||
import {
|
||||
getWebAuthAgeMs,
|
||||
logoutWeb,
|
||||
logWebSelfId,
|
||||
readWebSelfId,
|
||||
webAuthExists,
|
||||
} from "../../web/auth-store.js";
|
||||
import { loginWeb } from "../../web/login.js";
|
||||
import { startWebLoginWithQr, waitForWebLogin } from "../../web/login-qr.js";
|
||||
import { sendMessageWhatsApp, sendPollWhatsApp } from "../../web/outbound.js";
|
||||
import { registerMemoryCli } from "../../cli/memory-cli.js";
|
||||
|
||||
import type { PluginRuntime } from "./types.js";
|
||||
|
||||
@@ -48,17 +115,42 @@ function resolveVersion(): string {
|
||||
export function createPluginRuntime(): PluginRuntime {
|
||||
return {
|
||||
version: resolveVersion(),
|
||||
config: {
|
||||
loadConfig,
|
||||
writeConfigFile,
|
||||
},
|
||||
system: {
|
||||
enqueueSystemEvent,
|
||||
runCommandWithTimeout,
|
||||
},
|
||||
media: {
|
||||
loadWebMedia,
|
||||
detectMime,
|
||||
mediaKindFromMime,
|
||||
isVoiceCompatibleAudio,
|
||||
getImageMetadata,
|
||||
resizeToJpeg,
|
||||
},
|
||||
tools: {
|
||||
createMemoryGetTool,
|
||||
createMemorySearchTool,
|
||||
registerMemoryCli,
|
||||
},
|
||||
channel: {
|
||||
text: {
|
||||
chunkMarkdownText,
|
||||
resolveTextChunkLimit,
|
||||
hasControlCommand,
|
||||
},
|
||||
text: {
|
||||
chunkMarkdownText,
|
||||
chunkText,
|
||||
resolveTextChunkLimit,
|
||||
hasControlCommand,
|
||||
},
|
||||
reply: {
|
||||
dispatchReplyWithBufferedBlockDispatcher,
|
||||
createReplyDispatcherWithTyping,
|
||||
resolveEffectiveMessagesConfig,
|
||||
resolveHumanDelayConfig,
|
||||
dispatchReplyFromConfig,
|
||||
finalizeInboundContext,
|
||||
formatAgentEnvelope,
|
||||
},
|
||||
routing: {
|
||||
resolveAgentRoute,
|
||||
@@ -72,6 +164,11 @@ export function createPluginRuntime(): PluginRuntime {
|
||||
fetchRemoteMedia,
|
||||
saveMediaBuffer,
|
||||
},
|
||||
session: {
|
||||
resolveStorePath,
|
||||
recordSessionMetaFromInbound,
|
||||
updateLastRoute,
|
||||
},
|
||||
mentions: {
|
||||
buildMentionRegexes,
|
||||
matchesMentionPatterns,
|
||||
@@ -84,8 +181,68 @@ export function createPluginRuntime(): PluginRuntime {
|
||||
createInboundDebouncer,
|
||||
resolveInboundDebounceMs,
|
||||
},
|
||||
commands: {
|
||||
resolveCommandAuthorizedFromAuthorizers,
|
||||
commands: {
|
||||
resolveCommandAuthorizedFromAuthorizers,
|
||||
isControlCommandMessage,
|
||||
shouldComputeCommandAuthorized,
|
||||
shouldHandleTextCommands,
|
||||
},
|
||||
discord: {
|
||||
messageActions: discordMessageActions,
|
||||
auditChannelPermissions: auditDiscordChannelPermissions,
|
||||
listDirectoryGroupsLive: listDiscordDirectoryGroupsLive,
|
||||
listDirectoryPeersLive: listDiscordDirectoryPeersLive,
|
||||
probeDiscord,
|
||||
resolveChannelAllowlist: resolveDiscordChannelAllowlist,
|
||||
resolveUserAllowlist: resolveDiscordUserAllowlist,
|
||||
sendMessageDiscord,
|
||||
sendPollDiscord,
|
||||
monitorDiscordProvider,
|
||||
},
|
||||
slack: {
|
||||
listDirectoryGroupsLive: listSlackDirectoryGroupsLive,
|
||||
listDirectoryPeersLive: listSlackDirectoryPeersLive,
|
||||
probeSlack,
|
||||
resolveChannelAllowlist: resolveSlackChannelAllowlist,
|
||||
resolveUserAllowlist: resolveSlackUserAllowlist,
|
||||
sendMessageSlack,
|
||||
monitorSlackProvider,
|
||||
handleSlackAction,
|
||||
},
|
||||
telegram: {
|
||||
auditGroupMembership: auditTelegramGroupMembership,
|
||||
collectUnmentionedGroupIds: collectTelegramUnmentionedGroupIds,
|
||||
probeTelegram,
|
||||
resolveTelegramToken,
|
||||
sendMessageTelegram,
|
||||
monitorTelegramProvider,
|
||||
messageActions: telegramMessageActions,
|
||||
},
|
||||
signal: {
|
||||
probeSignal,
|
||||
sendMessageSignal,
|
||||
monitorSignalProvider,
|
||||
},
|
||||
imessage: {
|
||||
monitorIMessageProvider,
|
||||
probeIMessage,
|
||||
sendMessageIMessage,
|
||||
},
|
||||
whatsapp: {
|
||||
getActiveWebListener,
|
||||
getWebAuthAgeMs,
|
||||
logoutWeb,
|
||||
logWebSelfId,
|
||||
readWebSelfId,
|
||||
webAuthExists,
|
||||
sendMessageWhatsApp,
|
||||
sendPollWhatsApp,
|
||||
loginWeb,
|
||||
startWebLoginWithQr,
|
||||
waitForWebLogin,
|
||||
monitorWebChannel,
|
||||
handleWhatsAppAction,
|
||||
createLoginTool: createWhatsAppLoginTool,
|
||||
},
|
||||
},
|
||||
logging: {
|
||||
|
||||
@@ -31,8 +31,99 @@ type ResolveCommandAuthorizedFromAuthorizers =
|
||||
typeof import("../../channels/command-gating.js").resolveCommandAuthorizedFromAuthorizers;
|
||||
type ResolveTextChunkLimit = typeof import("../../auto-reply/chunk.js").resolveTextChunkLimit;
|
||||
type ChunkMarkdownText = typeof import("../../auto-reply/chunk.js").chunkMarkdownText;
|
||||
type ChunkText = typeof import("../../auto-reply/chunk.js").chunkText;
|
||||
type HasControlCommand = typeof import("../../auto-reply/command-detection.js").hasControlCommand;
|
||||
type IsControlCommandMessage =
|
||||
typeof import("../../auto-reply/command-detection.js").isControlCommandMessage;
|
||||
type ShouldComputeCommandAuthorized =
|
||||
typeof import("../../auto-reply/command-detection.js").shouldComputeCommandAuthorized;
|
||||
type ShouldHandleTextCommands =
|
||||
typeof import("../../auto-reply/commands-registry.js").shouldHandleTextCommands;
|
||||
type DispatchReplyFromConfig =
|
||||
typeof import("../../auto-reply/reply/dispatch-from-config.js").dispatchReplyFromConfig;
|
||||
type FinalizeInboundContext =
|
||||
typeof import("../../auto-reply/reply/inbound-context.js").finalizeInboundContext;
|
||||
type FormatAgentEnvelope = typeof import("../../auto-reply/envelope.js").formatAgentEnvelope;
|
||||
type ResolveStateDir = typeof import("../../config/paths.js").resolveStateDir;
|
||||
type RecordSessionMetaFromInbound =
|
||||
typeof import("../../config/sessions.js").recordSessionMetaFromInbound;
|
||||
type ResolveStorePath = typeof import("../../config/sessions.js").resolveStorePath;
|
||||
type UpdateLastRoute = typeof import("../../config/sessions.js").updateLastRoute;
|
||||
type LoadConfig = typeof import("../../config/config.js").loadConfig;
|
||||
type WriteConfigFile = typeof import("../../config/config.js").writeConfigFile;
|
||||
type EnqueueSystemEvent = typeof import("../../infra/system-events.js").enqueueSystemEvent;
|
||||
type RunCommandWithTimeout = typeof import("../../process/exec.js").runCommandWithTimeout;
|
||||
type LoadWebMedia = typeof import("../../web/media.js").loadWebMedia;
|
||||
type DetectMime = typeof import("../../media/mime.js").detectMime;
|
||||
type MediaKindFromMime = typeof import("../../media/constants.js").mediaKindFromMime;
|
||||
type IsVoiceCompatibleAudio = typeof import("../../media/audio.js").isVoiceCompatibleAudio;
|
||||
type GetImageMetadata = typeof import("../../media/image-ops.js").getImageMetadata;
|
||||
type ResizeToJpeg = typeof import("../../media/image-ops.js").resizeToJpeg;
|
||||
type CreateMemoryGetTool =
|
||||
typeof import("../../agents/tools/memory-tool.js").createMemoryGetTool;
|
||||
type CreateMemorySearchTool =
|
||||
typeof import("../../agents/tools/memory-tool.js").createMemorySearchTool;
|
||||
type RegisterMemoryCli = typeof import("../../cli/memory-cli.js").registerMemoryCli;
|
||||
type DiscordMessageActions =
|
||||
typeof import("../../channels/plugins/actions/discord.js").discordMessageActions;
|
||||
type AuditDiscordChannelPermissions =
|
||||
typeof import("../../discord/audit.js").auditDiscordChannelPermissions;
|
||||
type ListDiscordDirectoryGroupsLive =
|
||||
typeof import("../../discord/directory-live.js").listDiscordDirectoryGroupsLive;
|
||||
type ListDiscordDirectoryPeersLive =
|
||||
typeof import("../../discord/directory-live.js").listDiscordDirectoryPeersLive;
|
||||
type ProbeDiscord = typeof import("../../discord/probe.js").probeDiscord;
|
||||
type ResolveDiscordChannelAllowlist =
|
||||
typeof import("../../discord/resolve-channels.js").resolveDiscordChannelAllowlist;
|
||||
type ResolveDiscordUserAllowlist =
|
||||
typeof import("../../discord/resolve-users.js").resolveDiscordUserAllowlist;
|
||||
type SendMessageDiscord = typeof import("../../discord/send.js").sendMessageDiscord;
|
||||
type SendPollDiscord = typeof import("../../discord/send.js").sendPollDiscord;
|
||||
type MonitorDiscordProvider = typeof import("../../discord/monitor.js").monitorDiscordProvider;
|
||||
type ListSlackDirectoryGroupsLive =
|
||||
typeof import("../../slack/directory-live.js").listSlackDirectoryGroupsLive;
|
||||
type ListSlackDirectoryPeersLive =
|
||||
typeof import("../../slack/directory-live.js").listSlackDirectoryPeersLive;
|
||||
type ProbeSlack = typeof import("../../slack/probe.js").probeSlack;
|
||||
type ResolveSlackChannelAllowlist =
|
||||
typeof import("../../slack/resolve-channels.js").resolveSlackChannelAllowlist;
|
||||
type ResolveSlackUserAllowlist =
|
||||
typeof import("../../slack/resolve-users.js").resolveSlackUserAllowlist;
|
||||
type SendMessageSlack = typeof import("../../slack/send.js").sendMessageSlack;
|
||||
type MonitorSlackProvider = typeof import("../../slack/index.js").monitorSlackProvider;
|
||||
type HandleSlackAction = typeof import("../../agents/tools/slack-actions.js").handleSlackAction;
|
||||
type AuditTelegramGroupMembership =
|
||||
typeof import("../../telegram/audit.js").auditTelegramGroupMembership;
|
||||
type CollectTelegramUnmentionedGroupIds =
|
||||
typeof import("../../telegram/audit.js").collectTelegramUnmentionedGroupIds;
|
||||
type ProbeTelegram = typeof import("../../telegram/probe.js").probeTelegram;
|
||||
type ResolveTelegramToken = typeof import("../../telegram/token.js").resolveTelegramToken;
|
||||
type SendMessageTelegram = typeof import("../../telegram/send.js").sendMessageTelegram;
|
||||
type MonitorTelegramProvider = typeof import("../../telegram/monitor.js").monitorTelegramProvider;
|
||||
type TelegramMessageActions =
|
||||
typeof import("../../channels/plugins/actions/telegram.js").telegramMessageActions;
|
||||
type ProbeSignal = typeof import("../../signal/probe.js").probeSignal;
|
||||
type SendMessageSignal = typeof import("../../signal/send.js").sendMessageSignal;
|
||||
type MonitorSignalProvider = typeof import("../../signal/index.js").monitorSignalProvider;
|
||||
type MonitorIMessageProvider = typeof import("../../imessage/monitor.js").monitorIMessageProvider;
|
||||
type ProbeIMessage = typeof import("../../imessage/probe.js").probeIMessage;
|
||||
type SendMessageIMessage = typeof import("../../imessage/send.js").sendMessageIMessage;
|
||||
type GetActiveWebListener = typeof import("../../web/active-listener.js").getActiveWebListener;
|
||||
type GetWebAuthAgeMs = typeof import("../../web/auth-store.js").getWebAuthAgeMs;
|
||||
type LogoutWeb = typeof import("../../web/auth-store.js").logoutWeb;
|
||||
type LogWebSelfId = typeof import("../../web/auth-store.js").logWebSelfId;
|
||||
type ReadWebSelfId = typeof import("../../web/auth-store.js").readWebSelfId;
|
||||
type WebAuthExists = typeof import("../../web/auth-store.js").webAuthExists;
|
||||
type SendMessageWhatsApp = typeof import("../../web/outbound.js").sendMessageWhatsApp;
|
||||
type SendPollWhatsApp = typeof import("../../web/outbound.js").sendPollWhatsApp;
|
||||
type LoginWeb = typeof import("../../web/login.js").loginWeb;
|
||||
type StartWebLoginWithQr = typeof import("../../web/login-qr.js").startWebLoginWithQr;
|
||||
type WaitForWebLogin = typeof import("../../web/login-qr.js").waitForWebLogin;
|
||||
type MonitorWebChannel = typeof import("../../channels/web/index.js").monitorWebChannel;
|
||||
type HandleWhatsAppAction =
|
||||
typeof import("../../agents/tools/whatsapp-actions.js").handleWhatsAppAction;
|
||||
type CreateWhatsAppLoginTool =
|
||||
typeof import("../../channels/plugins/agent-tools/whatsapp-login.js").createWhatsAppLoginTool;
|
||||
|
||||
export type RuntimeLogger = {
|
||||
debug?: (message: string) => void;
|
||||
@@ -43,9 +134,31 @@ export type RuntimeLogger = {
|
||||
|
||||
export type PluginRuntime = {
|
||||
version: string;
|
||||
config: {
|
||||
loadConfig: LoadConfig;
|
||||
writeConfigFile: WriteConfigFile;
|
||||
};
|
||||
system: {
|
||||
enqueueSystemEvent: EnqueueSystemEvent;
|
||||
runCommandWithTimeout: RunCommandWithTimeout;
|
||||
};
|
||||
media: {
|
||||
loadWebMedia: LoadWebMedia;
|
||||
detectMime: DetectMime;
|
||||
mediaKindFromMime: MediaKindFromMime;
|
||||
isVoiceCompatibleAudio: IsVoiceCompatibleAudio;
|
||||
getImageMetadata: GetImageMetadata;
|
||||
resizeToJpeg: ResizeToJpeg;
|
||||
};
|
||||
tools: {
|
||||
createMemoryGetTool: CreateMemoryGetTool;
|
||||
createMemorySearchTool: CreateMemorySearchTool;
|
||||
registerMemoryCli: RegisterMemoryCli;
|
||||
};
|
||||
channel: {
|
||||
text: {
|
||||
chunkMarkdownText: ChunkMarkdownText;
|
||||
chunkText: ChunkText;
|
||||
resolveTextChunkLimit: ResolveTextChunkLimit;
|
||||
hasControlCommand: HasControlCommand;
|
||||
};
|
||||
@@ -54,6 +167,9 @@ export type PluginRuntime = {
|
||||
createReplyDispatcherWithTyping: CreateReplyDispatcherWithTyping;
|
||||
resolveEffectiveMessagesConfig: ResolveEffectiveMessagesConfig;
|
||||
resolveHumanDelayConfig: ResolveHumanDelayConfig;
|
||||
dispatchReplyFromConfig: DispatchReplyFromConfig;
|
||||
finalizeInboundContext: FinalizeInboundContext;
|
||||
formatAgentEnvelope: FormatAgentEnvelope;
|
||||
};
|
||||
routing: {
|
||||
resolveAgentRoute: ResolveAgentRoute;
|
||||
@@ -67,6 +183,11 @@ export type PluginRuntime = {
|
||||
fetchRemoteMedia: FetchRemoteMedia;
|
||||
saveMediaBuffer: SaveMediaBuffer;
|
||||
};
|
||||
session: {
|
||||
resolveStorePath: ResolveStorePath;
|
||||
recordSessionMetaFromInbound: RecordSessionMetaFromInbound;
|
||||
updateLastRoute: UpdateLastRoute;
|
||||
};
|
||||
mentions: {
|
||||
buildMentionRegexes: BuildMentionRegexes;
|
||||
matchesMentionPatterns: MatchesMentionPatterns;
|
||||
@@ -81,6 +202,66 @@ export type PluginRuntime = {
|
||||
};
|
||||
commands: {
|
||||
resolveCommandAuthorizedFromAuthorizers: ResolveCommandAuthorizedFromAuthorizers;
|
||||
isControlCommandMessage: IsControlCommandMessage;
|
||||
shouldComputeCommandAuthorized: ShouldComputeCommandAuthorized;
|
||||
shouldHandleTextCommands: ShouldHandleTextCommands;
|
||||
};
|
||||
discord: {
|
||||
messageActions: DiscordMessageActions;
|
||||
auditChannelPermissions: AuditDiscordChannelPermissions;
|
||||
listDirectoryGroupsLive: ListDiscordDirectoryGroupsLive;
|
||||
listDirectoryPeersLive: ListDiscordDirectoryPeersLive;
|
||||
probeDiscord: ProbeDiscord;
|
||||
resolveChannelAllowlist: ResolveDiscordChannelAllowlist;
|
||||
resolveUserAllowlist: ResolveDiscordUserAllowlist;
|
||||
sendMessageDiscord: SendMessageDiscord;
|
||||
sendPollDiscord: SendPollDiscord;
|
||||
monitorDiscordProvider: MonitorDiscordProvider;
|
||||
};
|
||||
slack: {
|
||||
listDirectoryGroupsLive: ListSlackDirectoryGroupsLive;
|
||||
listDirectoryPeersLive: ListSlackDirectoryPeersLive;
|
||||
probeSlack: ProbeSlack;
|
||||
resolveChannelAllowlist: ResolveSlackChannelAllowlist;
|
||||
resolveUserAllowlist: ResolveSlackUserAllowlist;
|
||||
sendMessageSlack: SendMessageSlack;
|
||||
monitorSlackProvider: MonitorSlackProvider;
|
||||
handleSlackAction: HandleSlackAction;
|
||||
};
|
||||
telegram: {
|
||||
auditGroupMembership: AuditTelegramGroupMembership;
|
||||
collectUnmentionedGroupIds: CollectTelegramUnmentionedGroupIds;
|
||||
probeTelegram: ProbeTelegram;
|
||||
resolveTelegramToken: ResolveTelegramToken;
|
||||
sendMessageTelegram: SendMessageTelegram;
|
||||
monitorTelegramProvider: MonitorTelegramProvider;
|
||||
messageActions: TelegramMessageActions;
|
||||
};
|
||||
signal: {
|
||||
probeSignal: ProbeSignal;
|
||||
sendMessageSignal: SendMessageSignal;
|
||||
monitorSignalProvider: MonitorSignalProvider;
|
||||
};
|
||||
imessage: {
|
||||
monitorIMessageProvider: MonitorIMessageProvider;
|
||||
probeIMessage: ProbeIMessage;
|
||||
sendMessageIMessage: SendMessageIMessage;
|
||||
};
|
||||
whatsapp: {
|
||||
getActiveWebListener: GetActiveWebListener;
|
||||
getWebAuthAgeMs: GetWebAuthAgeMs;
|
||||
logoutWeb: LogoutWeb;
|
||||
logWebSelfId: LogWebSelfId;
|
||||
readWebSelfId: ReadWebSelfId;
|
||||
webAuthExists: WebAuthExists;
|
||||
sendMessageWhatsApp: SendMessageWhatsApp;
|
||||
sendPollWhatsApp: SendPollWhatsApp;
|
||||
loginWeb: LoginWeb;
|
||||
startWebLoginWithQr: StartWebLoginWithQr;
|
||||
waitForWebLogin: WaitForWebLogin;
|
||||
monitorWebChannel: MonitorWebChannel;
|
||||
handleWhatsAppAction: HandleWhatsAppAction;
|
||||
createLoginTool: CreateWhatsAppLoginTool;
|
||||
};
|
||||
};
|
||||
logging: {
|
||||
|
||||
@@ -32,6 +32,7 @@ export const createIMessageTestPlugin = (params?: {
|
||||
selectionLabel: "iMessage (imsg)",
|
||||
docsPath: "/channels/imessage",
|
||||
blurb: "iMessage test stub.",
|
||||
aliases: ["imsg"],
|
||||
},
|
||||
capabilities: { chatTypes: ["direct", "group"], media: true },
|
||||
config: {
|
||||
|
||||
Reference in New Issue
Block a user