fix: resolve heartbeat sender and Slack thread_ts
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import * as replyModule from "../auto-reply/reply.js";
|
||||
import type { ClawdbotConfig } from "../config/config.js";
|
||||
import { resolveMainSessionKey } from "../config/sessions.js";
|
||||
import { setActivePluginRegistry } from "../plugins/runtime.js";
|
||||
import { createPluginRuntime } from "../plugins/runtime/index.js";
|
||||
import { createTestRegistry } from "../test-utils/channel-plugins.js";
|
||||
import { slackPlugin } from "../../extensions/slack/src/channel.js";
|
||||
import { setSlackRuntime } from "../../extensions/slack/src/runtime.js";
|
||||
import { telegramPlugin } from "../../extensions/telegram/src/channel.js";
|
||||
import { setTelegramRuntime } from "../../extensions/telegram/src/runtime.js";
|
||||
import { whatsappPlugin } from "../../extensions/whatsapp/src/channel.js";
|
||||
import { setWhatsAppRuntime } from "../../extensions/whatsapp/src/runtime.js";
|
||||
import { runHeartbeatOnce } from "./heartbeat-runner.js";
|
||||
|
||||
// Avoid pulling optional runtime deps during isolated runs.
|
||||
vi.mock("jiti", () => ({ createJiti: () => () => ({}) }));
|
||||
|
||||
beforeEach(() => {
|
||||
const runtime = createPluginRuntime();
|
||||
setSlackRuntime(runtime);
|
||||
setTelegramRuntime(runtime);
|
||||
setWhatsAppRuntime(runtime);
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([
|
||||
{ pluginId: "slack", plugin: slackPlugin, source: "test" },
|
||||
{ pluginId: "whatsapp", plugin: whatsappPlugin, source: "test" },
|
||||
{ pluginId: "telegram", plugin: telegramPlugin, source: "test" },
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
describe("runHeartbeatOnce", () => {
|
||||
it("uses the delivery target as sender when lastTo differs", async () => {
|
||||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-hb-"));
|
||||
const storePath = path.join(tmpDir, "sessions.json");
|
||||
const replySpy = vi.spyOn(replyModule, "getReplyFromConfig");
|
||||
try {
|
||||
const cfg: ClawdbotConfig = {
|
||||
agents: {
|
||||
defaults: {
|
||||
heartbeat: {
|
||||
every: "5m",
|
||||
target: "slack",
|
||||
to: "C0A9P2N8QHY",
|
||||
},
|
||||
},
|
||||
},
|
||||
session: { store: storePath },
|
||||
};
|
||||
const sessionKey = resolveMainSessionKey(cfg);
|
||||
|
||||
await fs.writeFile(
|
||||
storePath,
|
||||
JSON.stringify(
|
||||
{
|
||||
[sessionKey]: {
|
||||
sessionId: "sid",
|
||||
updatedAt: Date.now(),
|
||||
lastChannel: "telegram",
|
||||
lastProvider: "telegram",
|
||||
lastTo: "1644620762",
|
||||
},
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
|
||||
replySpy.mockImplementation(async (ctx) => {
|
||||
expect(ctx.To).toBe("C0A9P2N8QHY");
|
||||
expect(ctx.From).toBe("C0A9P2N8QHY");
|
||||
return { text: "ok" };
|
||||
});
|
||||
|
||||
const sendSlack = vi.fn().mockResolvedValue({
|
||||
messageId: "m1",
|
||||
channelId: "C0A9P2N8QHY",
|
||||
});
|
||||
|
||||
await runHeartbeatOnce({
|
||||
cfg,
|
||||
deps: {
|
||||
sendSlack,
|
||||
getQueueSize: () => 0,
|
||||
nowMs: () => 0,
|
||||
},
|
||||
});
|
||||
|
||||
expect(sendSlack).toHaveBeenCalled();
|
||||
} finally {
|
||||
replySpy.mockRestore();
|
||||
await fs.rm(tmpDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -39,7 +39,10 @@ import {
|
||||
} from "./heartbeat-wake.js";
|
||||
import type { OutboundSendDeps } from "./outbound/deliver.js";
|
||||
import { deliverOutboundPayloads } from "./outbound/deliver.js";
|
||||
import { resolveHeartbeatDeliveryTarget } from "./outbound/targets.js";
|
||||
import {
|
||||
resolveHeartbeatDeliveryTarget,
|
||||
resolveHeartbeatSenderContext,
|
||||
} from "./outbound/targets.js";
|
||||
|
||||
type HeartbeatDeps = OutboundSendDeps &
|
||||
ChannelHeartbeatDeps & {
|
||||
@@ -362,34 +365,6 @@ function resolveHeartbeatReasoningPayloads(
|
||||
});
|
||||
}
|
||||
|
||||
function resolveHeartbeatSender(params: {
|
||||
allowFrom: Array<string | number>;
|
||||
lastTo?: string;
|
||||
provider?: string | null;
|
||||
}) {
|
||||
const { allowFrom, lastTo, provider } = params;
|
||||
const candidates = [
|
||||
lastTo?.trim(),
|
||||
provider && lastTo ? `${provider}:${lastTo}` : undefined,
|
||||
].filter((val): val is string => Boolean(val?.trim()));
|
||||
|
||||
const allowList = allowFrom
|
||||
.map((entry) => String(entry))
|
||||
.filter((entry) => entry && entry !== "*");
|
||||
if (allowFrom.includes("*")) {
|
||||
return candidates[0] ?? "heartbeat";
|
||||
}
|
||||
if (candidates.length > 0 && allowList.length > 0) {
|
||||
const matched = candidates.find((candidate) => allowList.includes(candidate));
|
||||
if (matched) return matched;
|
||||
}
|
||||
if (candidates.length > 0 && allowList.length === 0) {
|
||||
return candidates[0];
|
||||
}
|
||||
if (allowList.length > 0) return allowList[0];
|
||||
return candidates[0] ?? "heartbeat";
|
||||
}
|
||||
|
||||
async function restoreHeartbeatUpdatedAt(params: {
|
||||
storePath: string;
|
||||
sessionKey: string;
|
||||
@@ -468,20 +443,7 @@ export async function runHeartbeatOnce(opts: {
|
||||
const { entry, sessionKey, storePath } = resolveHeartbeatSession(cfg, agentId, heartbeat);
|
||||
const previousUpdatedAt = entry?.updatedAt;
|
||||
const delivery = resolveHeartbeatDeliveryTarget({ cfg, entry, heartbeat });
|
||||
const lastChannel = delivery.lastChannel;
|
||||
const lastAccountId = delivery.lastAccountId;
|
||||
const senderProvider = delivery.channel !== "none" ? delivery.channel : lastChannel;
|
||||
const senderAllowFrom = senderProvider
|
||||
? (getChannelPlugin(senderProvider)?.config.resolveAllowFrom?.({
|
||||
cfg,
|
||||
accountId: senderProvider === lastChannel ? lastAccountId : undefined,
|
||||
}) ?? [])
|
||||
: [];
|
||||
const sender = resolveHeartbeatSender({
|
||||
allowFrom: senderAllowFrom,
|
||||
lastTo: entry?.lastTo,
|
||||
provider: senderProvider,
|
||||
});
|
||||
const { sender } = resolveHeartbeatSenderContext({ cfg, entry, delivery });
|
||||
const prompt = resolveHeartbeatPrompt(cfg, heartbeat);
|
||||
const ctx = {
|
||||
Body: prompt,
|
||||
|
||||
@@ -29,6 +29,12 @@ export type OutboundTarget = {
|
||||
lastAccountId?: string;
|
||||
};
|
||||
|
||||
export type HeartbeatSenderContext = {
|
||||
sender: string;
|
||||
provider?: DeliverableMessageChannel;
|
||||
allowFrom: string[];
|
||||
};
|
||||
|
||||
export type OutboundTargetResolution = { ok: true; to: string } | { ok: false; error: Error };
|
||||
|
||||
export type SessionDeliveryTarget = {
|
||||
@@ -250,3 +256,59 @@ export function resolveHeartbeatDeliveryTarget(params: {
|
||||
lastAccountId: resolvedTarget.lastAccountId,
|
||||
};
|
||||
}
|
||||
|
||||
function resolveHeartbeatSenderId(params: {
|
||||
allowFrom: Array<string | number>;
|
||||
deliveryTo?: string;
|
||||
lastTo?: string;
|
||||
provider?: string | null;
|
||||
}) {
|
||||
const { allowFrom, deliveryTo, lastTo, provider } = params;
|
||||
const candidates = [
|
||||
deliveryTo?.trim(),
|
||||
provider && deliveryTo ? `${provider}:${deliveryTo}` : undefined,
|
||||
lastTo?.trim(),
|
||||
provider && lastTo ? `${provider}:${lastTo}` : undefined,
|
||||
].filter((val): val is string => Boolean(val?.trim()));
|
||||
|
||||
const allowList = allowFrom
|
||||
.map((entry) => String(entry))
|
||||
.filter((entry) => entry && entry !== "*");
|
||||
if (allowFrom.includes("*")) {
|
||||
return candidates[0] ?? "heartbeat";
|
||||
}
|
||||
if (candidates.length > 0 && allowList.length > 0) {
|
||||
const matched = candidates.find((candidate) => allowList.includes(candidate));
|
||||
if (matched) return matched;
|
||||
}
|
||||
if (candidates.length > 0 && allowList.length === 0) {
|
||||
return candidates[0];
|
||||
}
|
||||
if (allowList.length > 0) return allowList[0];
|
||||
return candidates[0] ?? "heartbeat";
|
||||
}
|
||||
|
||||
export function resolveHeartbeatSenderContext(params: {
|
||||
cfg: ClawdbotConfig;
|
||||
entry?: SessionEntry;
|
||||
delivery: OutboundTarget;
|
||||
}): HeartbeatSenderContext {
|
||||
const provider =
|
||||
params.delivery.channel !== "none" ? params.delivery.channel : params.delivery.lastChannel;
|
||||
const allowFrom = provider
|
||||
? (getChannelPlugin(provider)?.config.resolveAllowFrom?.({
|
||||
cfg: params.cfg,
|
||||
accountId:
|
||||
provider === params.delivery.lastChannel ? params.delivery.lastAccountId : undefined,
|
||||
}) ?? [])
|
||||
: [];
|
||||
|
||||
const sender = resolveHeartbeatSenderId({
|
||||
allowFrom,
|
||||
deliveryTo: params.delivery.to,
|
||||
lastTo: params.entry?.lastTo,
|
||||
provider,
|
||||
});
|
||||
|
||||
return { sender, provider, allowFrom };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user