fix: align heartbeat session store with default agent
This commit is contained in:
@@ -184,6 +184,70 @@ describe("runHeartbeatOnce", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("loads the default agent session from templated stores", async () => {
|
||||||
|
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-hb-"));
|
||||||
|
const storeTemplate = path.join(
|
||||||
|
tmpDir,
|
||||||
|
"agents",
|
||||||
|
"{agentId}",
|
||||||
|
"sessions.json",
|
||||||
|
);
|
||||||
|
const storePath = path.join(tmpDir, "agents", "work", "sessions.json");
|
||||||
|
const replySpy = vi.spyOn(replyModule, "getReplyFromConfig");
|
||||||
|
try {
|
||||||
|
await fs.mkdir(path.dirname(storePath), { recursive: true });
|
||||||
|
await fs.writeFile(
|
||||||
|
storePath,
|
||||||
|
JSON.stringify(
|
||||||
|
{
|
||||||
|
"agent:work:main": {
|
||||||
|
sessionId: "sid",
|
||||||
|
updatedAt: Date.now(),
|
||||||
|
lastProvider: "whatsapp",
|
||||||
|
lastTo: "+1555",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
2,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
const cfg: ClawdbotConfig = {
|
||||||
|
routing: { defaultAgentId: "work" },
|
||||||
|
agent: { heartbeat: { every: "5m" } },
|
||||||
|
whatsapp: { allowFrom: ["*"] },
|
||||||
|
session: { store: storeTemplate },
|
||||||
|
};
|
||||||
|
|
||||||
|
replySpy.mockResolvedValue({ text: "Hello from heartbeat" });
|
||||||
|
const sendWhatsApp = vi.fn().mockResolvedValue({
|
||||||
|
messageId: "m1",
|
||||||
|
toJid: "jid",
|
||||||
|
});
|
||||||
|
|
||||||
|
await runHeartbeatOnce({
|
||||||
|
cfg,
|
||||||
|
deps: {
|
||||||
|
sendWhatsApp,
|
||||||
|
getQueueSize: () => 0,
|
||||||
|
nowMs: () => 0,
|
||||||
|
webAuthExists: async () => true,
|
||||||
|
hasActiveWebListener: () => true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(sendWhatsApp).toHaveBeenCalledTimes(1);
|
||||||
|
expect(sendWhatsApp).toHaveBeenCalledWith(
|
||||||
|
"+1555",
|
||||||
|
"Hello from heartbeat",
|
||||||
|
expect.any(Object),
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
replySpy.mockRestore();
|
||||||
|
await fs.rm(tmpDir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it("respects ackMaxChars for heartbeat acks", async () => {
|
it("respects ackMaxChars for heartbeat acks", async () => {
|
||||||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-hb-"));
|
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-hb-"));
|
||||||
const storePath = path.join(tmpDir, "sessions.json");
|
const storePath = path.join(tmpDir, "sessions.json");
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import type { ClawdbotConfig } from "../config/config.js";
|
|||||||
import { loadConfig } from "../config/config.js";
|
import { loadConfig } from "../config/config.js";
|
||||||
import {
|
import {
|
||||||
loadSessionStore,
|
loadSessionStore,
|
||||||
|
resolveAgentIdFromSessionKey,
|
||||||
|
resolveMainSessionKey,
|
||||||
resolveStorePath,
|
resolveStorePath,
|
||||||
type SessionEntry,
|
type SessionEntry,
|
||||||
saveSessionStore,
|
saveSessionStore,
|
||||||
@@ -79,9 +81,9 @@ function resolveHeartbeatAckMaxChars(cfg: ClawdbotConfig) {
|
|||||||
function resolveHeartbeatSession(cfg: ClawdbotConfig) {
|
function resolveHeartbeatSession(cfg: ClawdbotConfig) {
|
||||||
const sessionCfg = cfg.session;
|
const sessionCfg = cfg.session;
|
||||||
const scope = sessionCfg?.scope ?? "per-sender";
|
const scope = sessionCfg?.scope ?? "per-sender";
|
||||||
const mainKey = (sessionCfg?.mainKey ?? "main").trim() || "main";
|
const sessionKey = scope === "global" ? "global" : resolveMainSessionKey(cfg);
|
||||||
const sessionKey = scope === "global" ? "global" : mainKey;
|
const agentId = resolveAgentIdFromSessionKey(sessionKey);
|
||||||
const storePath = resolveStorePath(sessionCfg?.store);
|
const storePath = resolveStorePath(sessionCfg?.store, { agentId });
|
||||||
const store = loadSessionStore(storePath);
|
const store = loadSessionStore(storePath);
|
||||||
const entry = store[sessionKey];
|
const entry = store[sessionKey];
|
||||||
return { sessionKey, storePath, store, entry };
|
return { sessionKey, storePath, store, entry };
|
||||||
|
|||||||
Reference in New Issue
Block a user