feat: add providers CLI and multi-account onboarding

This commit is contained in:
Peter Steinberger
2026-01-08 01:18:37 +01:00
parent 6b3ed40d0f
commit 05b8679c8b
54 changed files with 4399 additions and 1448 deletions

View File

@@ -411,7 +411,7 @@ describe("runHeartbeatOnce", () => {
expect(sendTelegram).toHaveBeenCalledWith(
"123456",
"Hello from heartbeat",
expect.objectContaining({ token: "test-bot-token-123" }),
expect.objectContaining({ accountId: "default", verbose: false }),
);
} finally {
replySpy.mockRestore();

View File

@@ -7,7 +7,7 @@ import {
} from "./deliver.js";
describe("deliverOutboundPayloads", () => {
it("chunks telegram markdown and passes config token", async () => {
it("chunks telegram markdown and passes account id", async () => {
const sendTelegram = vi
.fn()
.mockResolvedValue({ messageId: "m1", chatId: "c1" });
@@ -28,7 +28,7 @@ describe("deliverOutboundPayloads", () => {
expect(sendTelegram).toHaveBeenCalledTimes(2);
for (const call of sendTelegram.mock.calls) {
expect(call[2]).toEqual(
expect.objectContaining({ token: "tok-1", verbose: false }),
expect.objectContaining({ accountId: "default", verbose: false }),
);
}
expect(results).toHaveLength(2);

View File

@@ -7,10 +7,10 @@ import type { ReplyPayload } from "../../auto-reply/types.js";
import type { ClawdbotConfig } from "../../config/config.js";
import { sendMessageDiscord } from "../../discord/send.js";
import { sendMessageIMessage } from "../../imessage/send.js";
import { normalizeAccountId } from "../../routing/session-key.js";
import { sendMessageSignal } from "../../signal/send.js";
import { sendMessageSlack } from "../../slack/send.js";
import { sendMessageTelegram } from "../../telegram/send.js";
import { resolveTelegramToken } from "../../telegram/token.js";
import { sendMessageWhatsApp } from "../../web/outbound.js";
import type { NormalizedOutboundPayload } from "./payloads.js";
import { normalizeOutboundPayloads } from "./payloads.js";
@@ -64,9 +64,15 @@ type ProviderHandler = {
function resolveMediaMaxBytes(
cfg: ClawdbotConfig,
provider: "signal" | "imessage",
accountId?: string | null,
): number | undefined {
const normalizedAccountId = normalizeAccountId(accountId);
const providerLimit =
provider === "signal" ? cfg.signal?.mediaMaxMb : cfg.imessage?.mediaMaxMb;
provider === "signal"
? (cfg.signal?.accounts?.[normalizedAccountId]?.mediaMaxMb ??
cfg.signal?.mediaMaxMb)
: (cfg.imessage?.accounts?.[normalizedAccountId]?.mediaMaxMb ??
cfg.imessage?.mediaMaxMb);
if (providerLimit) return providerLimit * MB;
if (cfg.agent?.mediaMaxMb) return cfg.agent.mediaMaxMb * MB;
return undefined;
@@ -76,20 +82,18 @@ function createProviderHandler(params: {
cfg: ClawdbotConfig;
provider: Exclude<OutboundProvider, "none">;
to: string;
accountId?: string;
deps: Required<OutboundSendDeps>;
}): ProviderHandler {
const { cfg, to, deps } = params;
const telegramToken =
params.provider === "telegram"
? resolveTelegramToken(cfg).token || undefined
: undefined;
const accountId = normalizeAccountId(params.accountId);
const signalMaxBytes =
params.provider === "signal"
? resolveMediaMaxBytes(cfg, "signal")
? resolveMediaMaxBytes(cfg, "signal", accountId)
: undefined;
const imessageMaxBytes =
params.provider === "imessage"
? resolveMediaMaxBytes(cfg, "imessage")
? resolveMediaMaxBytes(cfg, "imessage", accountId)
: undefined;
const handlers: Record<Exclude<OutboundProvider, "none">, ProviderHandler> = {
@@ -97,13 +101,17 @@ function createProviderHandler(params: {
chunker: providerCaps.whatsapp.chunker,
sendText: async (text) => ({
provider: "whatsapp",
...(await deps.sendWhatsApp(to, text, { verbose: false })),
...(await deps.sendWhatsApp(to, text, {
verbose: false,
accountId,
})),
}),
sendMedia: async (caption, mediaUrl) => ({
provider: "whatsapp",
...(await deps.sendWhatsApp(to, caption, {
verbose: false,
mediaUrl,
accountId,
})),
}),
},
@@ -113,7 +121,7 @@ function createProviderHandler(params: {
provider: "telegram",
...(await deps.sendTelegram(to, text, {
verbose: false,
token: telegramToken,
accountId,
})),
}),
sendMedia: async (caption, mediaUrl) => ({
@@ -121,7 +129,7 @@ function createProviderHandler(params: {
...(await deps.sendTelegram(to, caption, {
verbose: false,
mediaUrl,
token: telegramToken,
accountId,
})),
}),
},
@@ -129,13 +137,17 @@ function createProviderHandler(params: {
chunker: providerCaps.discord.chunker,
sendText: async (text) => ({
provider: "discord",
...(await deps.sendDiscord(to, text, { verbose: false })),
...(await deps.sendDiscord(to, text, {
verbose: false,
accountId,
})),
}),
sendMedia: async (caption, mediaUrl) => ({
provider: "discord",
...(await deps.sendDiscord(to, caption, {
verbose: false,
mediaUrl,
accountId,
})),
}),
},
@@ -143,24 +155,33 @@ function createProviderHandler(params: {
chunker: providerCaps.slack.chunker,
sendText: async (text) => ({
provider: "slack",
...(await deps.sendSlack(to, text)),
...(await deps.sendSlack(to, text, {
accountId,
})),
}),
sendMedia: async (caption, mediaUrl) => ({
provider: "slack",
...(await deps.sendSlack(to, caption, { mediaUrl })),
...(await deps.sendSlack(to, caption, {
mediaUrl,
accountId,
})),
}),
},
signal: {
chunker: providerCaps.signal.chunker,
sendText: async (text) => ({
provider: "signal",
...(await deps.sendSignal(to, text, { maxBytes: signalMaxBytes })),
...(await deps.sendSignal(to, text, {
maxBytes: signalMaxBytes,
accountId,
})),
}),
sendMedia: async (caption, mediaUrl) => ({
provider: "signal",
...(await deps.sendSignal(to, caption, {
mediaUrl,
maxBytes: signalMaxBytes,
accountId,
})),
}),
},
@@ -168,13 +189,17 @@ function createProviderHandler(params: {
chunker: providerCaps.imessage.chunker,
sendText: async (text) => ({
provider: "imessage",
...(await deps.sendIMessage(to, text, { maxBytes: imessageMaxBytes })),
...(await deps.sendIMessage(to, text, {
maxBytes: imessageMaxBytes,
accountId,
})),
}),
sendMedia: async (caption, mediaUrl) => ({
provider: "imessage",
...(await deps.sendIMessage(to, caption, {
mediaUrl,
maxBytes: imessageMaxBytes,
accountId,
})),
}),
},
@@ -187,6 +212,7 @@ export async function deliverOutboundPayloads(params: {
cfg: ClawdbotConfig;
provider: Exclude<OutboundProvider, "none">;
to: string;
accountId?: string;
payloads: ReplyPayload[];
deps?: OutboundSendDeps;
bestEffort?: boolean;
@@ -194,6 +220,7 @@ export async function deliverOutboundPayloads(params: {
onPayload?: (payload: NormalizedOutboundPayload) => void;
}): Promise<OutboundDeliveryResult[]> {
const { cfg, provider, to, payloads } = params;
const accountId = normalizeAccountId(params.accountId);
const deps = {
sendWhatsApp: params.deps?.sendWhatsApp ?? sendMessageWhatsApp,
sendTelegram: params.deps?.sendTelegram ?? sendMessageTelegram,
@@ -208,9 +235,10 @@ export async function deliverOutboundPayloads(params: {
provider,
to,
deps,
accountId,
});
const textLimit = handler.chunker
? resolveTextChunkLimit(cfg, provider)
? resolveTextChunkLimit(cfg, provider, accountId)
: undefined;
const sendTextChunks = async (text: string) => {