Heartbeat: optional reasoning delivery (#690)

* feat: expose heartbeat reasoning output

* docs(changelog): mention heartbeat reasoning toggle
This commit is contained in:
Peter Steinberger
2026-01-10 22:26:20 +00:00
committed by GitHub
parent 5adbeb1bad
commit 3166cc911b
8 changed files with 190 additions and 2 deletions

View File

@@ -1396,6 +1396,13 @@ export type AgentDefaultsConfig = {
prompt?: string;
/** Max chars allowed after HEARTBEAT_OK before delivery (default: 30). */
ackMaxChars?: number;
/**
* When enabled, deliver the model's reasoning payload for heartbeat runs (when available)
* as a separate message prefixed with `Reasoning:` (same as `/reasoning on`).
*
* Default: false (only the final heartbeat payload is delivered).
*/
includeReasoning?: boolean;
};
/** Max concurrent agent runs across all conversations. Default: 1 (sequential). */
maxConcurrent?: number;

View File

@@ -646,6 +646,7 @@ const HeartbeatSchema = z
.object({
every: z.string().optional(),
model: z.string().optional(),
includeReasoning: z.boolean().optional(),
target: z
.union([
z.literal("last"),

View File

@@ -246,6 +246,81 @@ describe("runHeartbeatOnce", () => {
}
});
it("can include reasoning payloads when enabled", 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 {
await fs.writeFile(
storePath,
JSON.stringify(
{
main: {
sessionId: "sid",
updatedAt: Date.now(),
lastProvider: "whatsapp",
lastTo: "+1555",
},
},
null,
2,
),
);
const cfg: ClawdbotConfig = {
agents: {
defaults: {
heartbeat: {
every: "5m",
target: "whatsapp",
to: "+1555",
includeReasoning: true,
},
},
},
whatsapp: { allowFrom: ["*"] },
session: { store: storePath },
};
replySpy.mockResolvedValue([
{ text: "Reasoning:\nBecause it helps" },
{ text: "Final alert" },
]);
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(2);
expect(sendWhatsApp).toHaveBeenNthCalledWith(
1,
"+1555",
"Reasoning:\nBecause it helps",
expect.any(Object),
);
expect(sendWhatsApp).toHaveBeenNthCalledWith(
2,
"+1555",
"Final alert",
expect.any(Object),
);
} finally {
replySpy.mockRestore();
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
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(

View File

@@ -112,6 +112,20 @@ function resolveHeartbeatReplyPayload(
return undefined;
}
function resolveHeartbeatReasoningPayloads(
replyResult: ReplyPayload | ReplyPayload[] | undefined,
): ReplyPayload[] {
const payloads = Array.isArray(replyResult)
? replyResult
: replyResult
? [replyResult]
: [];
return payloads.filter((payload) => {
const text = typeof payload.text === "string" ? payload.text : "";
return text.trimStart().startsWith("Reasoning:");
});
}
function resolveHeartbeatSender(params: {
allowFrom: Array<string | number>;
lastTo?: string;
@@ -246,6 +260,8 @@ export async function runHeartbeatOnce(opts: {
cfg,
);
const replyPayload = resolveHeartbeatReplyPayload(replyResult);
const includeReasoning =
cfg.agents?.defaults?.heartbeat?.includeReasoning === true;
if (
!replyPayload ||
@@ -294,6 +310,12 @@ export async function runHeartbeatOnce(opts: {
replyPayload.mediaUrls ??
(replyPayload.mediaUrl ? [replyPayload.mediaUrl] : []);
const reasoningPayloads = includeReasoning
? resolveHeartbeatReasoningPayloads(replyResult).filter(
(payload) => payload !== replyPayload,
)
: [];
if (delivery.provider === "none" || !delivery.to) {
emitHeartbeatEvent({
status: "skipped",
@@ -327,6 +349,7 @@ export async function runHeartbeatOnce(opts: {
provider: delivery.provider,
to: delivery.to,
payloads: [
...reasoningPayloads,
{
text: normalized.text,
mediaUrls,