openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
112 lines (111 loc) • 4.77 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { t as escapeRegExp } from "./regexp-dj3rRPbj.mjs";
import { n as SILENT_REPLY_TOKEN, t as HEARTBEAT_TOKEN } from "./tokens-CcE6ESvH.mjs";
//#region src/auto-reply/heartbeat.ts
const HEARTBEAT_CONTEXT_PROMPT = `Follow the heartbeat monitor scratch context when provided. Recurring tasks are automations; create or change their schedules with the automations tool, not heartbeat scratch. Do not infer or repeat old tasks from prior chats.`;
/** Default prompt for heartbeat turns when config does not override it. */
const HEARTBEAT_PROMPT = `${HEARTBEAT_CONTEXT_PROMPT} If nothing needs attention, reply ${SILENT_REPLY_TOKEN}.`;
const HEARTBEAT_RESPONSE_TOOL_INSTRUCTIONS = "Use heartbeat_respond to report the wake outcome. Set notify=false when nothing needs the user's attention. Set notify=true with notificationText only when the user should be interrupted.";
const HEARTBEAT_RESPONSE_TOOL_FALLBACK_INSTRUCTIONS = `If the heartbeat_respond tool is not available in this run, reply ${SILENT_REPLY_TOKEN} when nothing needs the user's attention; when the user should be interrupted, reply with only the alert text instead of a prose report.`;
const HEARTBEAT_RESPONSE_TOOL_PROMPT = `${HEARTBEAT_CONTEXT_PROMPT} ${HEARTBEAT_RESPONSE_TOOL_INSTRUCTIONS} ${HEARTBEAT_RESPONSE_TOOL_FALLBACK_INSTRUCTIONS}`;
const INTERNAL_WAKE_TRANSCRIPT_PROMPTS = {
heartbeat: "[OpenClaw heartbeat poll]",
exec: "[OpenClaw exec completion]\nDisable automatic completion turns with tools.exec.notifyOnExit=false; check per-agent overrides. Background exec and process poll remain available.",
legacyExec: "[OpenClaw exec completion]",
cron: "[OpenClaw cron wake]",
event: "[OpenClaw session event]"
};
const DEFAULT_HEARTBEAT_ACK_MAX_CHARS = 300;
/** Resolves heartbeat prompt text and guarantees heartbeat_respond tool instructions are present. */
function resolveHeartbeatPromptForResponseTool(e) {
const t = normalizeOptionalString(e);
if (!t) return HEARTBEAT_RESPONSE_TOOL_PROMPT;
let n = t;
for (const e of [HEARTBEAT_RESPONSE_TOOL_INSTRUCTIONS, HEARTBEAT_RESPONSE_TOOL_FALLBACK_INSTRUCTIONS]) if (!n.includes(e)) n = `${n}\n\n${e}`;
return n;
}
const HEARTBEAT_TRAILING_TOKEN_RE = new RegExp(`${escapeRegExp(HEARTBEAT_TOKEN)}[^\\w]{0,4}$`);
/** Heartbeats accept only strict token prefixes with at least four characters. */
function isTruncatedSilentReplyText(e) {
const t = e?.trim().toUpperCase() ?? "";
return t.length >= 4 && t.length < "NO_REPLY".length && "NO_REPLY".startsWith(t);
}
function stripTokenAtEdges(e) {
let t = e.trim();
if (!t) return {
text: "",
didStrip: false
};
const n = HEARTBEAT_TOKEN;
if (!t.includes(n)) return {
text: t,
didStrip: false
};
let r = false;
while (true) {
const e = t.trim();
if (e.startsWith(n)) {
t = e.slice(n.length).trimStart();
r = true;
continue;
}
if (HEARTBEAT_TRAILING_TOKEN_RE.test(e)) {
const i = e.lastIndexOf(n);
t = `${e.slice(0, i).trimEnd()}${e.slice(i + n.length).trimStart()}`.trimEnd();
r = true;
} else break;
}
return {
text: t,
didStrip: r
};
}
/** Strips HEARTBEAT_OK acknowledgements and decides whether visible notification is needed. */
function stripHeartbeatToken(e, t = {}) {
const n = e?.trim();
if (!n) return {
shouldSkip: true,
text: "",
didStrip: false
};
const r = t.mode ?? "message";
if (r === "heartbeat" && t.allowTruncated !== false && isTruncatedSilentReplyText(n)) return {
shouldSkip: true,
text: "",
didStrip: true
};
if (!n.includes("HEARTBEAT_OK")) return {
shouldSkip: false,
text: n,
didStrip: false
};
const i = t.maxAckChars;
const a = Math.max(0, i !== void 0 && Number.isFinite(i) ? i : 300);
const stripMarkup = (e) => e.replace(/<[^>]*>/g, " ").replace(/ /gi, " ").replace(/^[*`~_]+/, "").replace(/[*`~_]+$/, "");
const o = stripMarkup(n);
const s = stripTokenAtEdges(n);
const c = stripTokenAtEdges(o);
const l = s.didStrip && s.text ? s : c;
if (!l.didStrip) return {
shouldSkip: false,
text: n,
didStrip: false
};
const u = l.text.trim();
const d = !l.text || r === "heartbeat" && u.length <= a;
return {
shouldSkip: d,
text: d ? "" : u,
didStrip: true
};
}
/** Truncated acknowledgements require established heartbeat context. */
function isHeartbeatAcknowledgementText(e, t = 300, n = false) {
return stripHeartbeatToken(e, {
mode: "heartbeat",
maxAckChars: t,
allowTruncated: n
}).shouldSkip;
}
//#endregion
export { isHeartbeatAcknowledgementText as a, INTERNAL_WAKE_TRANSCRIPT_PROMPTS as i, HEARTBEAT_RESPONSE_TOOL_INSTRUCTIONS as n, resolveHeartbeatPromptForResponseTool as o, HEARTBEAT_RESPONSE_TOOL_PROMPT as r, stripHeartbeatToken as s, HEARTBEAT_PROMPT as t };