openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
121 lines (120 loc) • 4.64 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { r as isInternalFormattingArtifact, t as HEARTBEAT_TOKEN } from "./tokens-CcE6ESvH.mjs";
import { o as hasReplyPayloadContent } from "./payload-Cab4FCxl.mjs";
import { c as getReplyPayloadMetadata, k as setReplyPayloadMetadata, o as copyReplyPayloadMetadata, u as hasReplyPayloadSpeechContent } from "./reply-payload-k5WsQWxi.mjs";
import { s as stripHeartbeatToken } from "./heartbeat-DHK32_6H.mjs";
import { c as stripLeadingEmptyLines } from "./text-projection-DwmQz6_O.mjs";
import { i as sanitizeUserFacingText, n as renderUserFacingText } from "./user-facing-text-Dqp3fcZx.mjs";
//#region src/auto-reply/reply/response-prefix-template.ts
const TEMPLATE_VAR_PATTERN = /\{([a-zA-Z][a-zA-Z0-9.]*)\}/g;
const TEMPLATE_FIELDS = {
model: "model",
modelfull: "modelFull",
provider: "provider",
thinkinglevel: "thinkingLevel",
think: "thinkingLevel",
"identity.name": "identityName",
identityname: "identityName"
};
/** Variable names are case-insensitive; unresolved placeholders remain literal. */
function resolveResponsePrefixTemplate(e, t) {
if (!e) return;
return e.replace(TEMPLATE_VAR_PATTERN, (e, n) => {
const r = n.toLowerCase();
return Object.hasOwn(TEMPLATE_FIELDS, r) ? t[TEMPLATE_FIELDS[r]] ?? e : e;
});
}
/** Strips the provider prefix, date suffix, and trailing -latest tag for display. */
function extractShortModelName(e) {
const t = e.lastIndexOf("/");
return (t >= 0 ? e.slice(t + 1) : e).replace(/-\d{8}$/, "").replace(/-latest$/, "");
}
//#endregion
//#region src/auto-reply/reply/normalize-reply.ts
const channelReplyTransformOwners = /* @__PURE__ */ new WeakMap();
function bindNormalizeReplyTransformOwner(e, t) {
channelReplyTransformOwners.set(e, t);
return e;
}
function resolveNormalizeReplyTransformOwner(e) {
return e ? channelReplyTransformOwners.get(e) ?? e : void 0;
}
function normalizeReplyPayloadOutcome(e, t = {}) {
if (t.prepared) return {
kind: "deliver",
payload: !t.preserveLeadingSourceBoundary && e.text?.startsWith("\n") ? copyReplyPayloadMetadata(e, {
...e,
text: stripLeadingEmptyLines(e.text)
}) : e
};
const suppress = (e) => {
t.onSkip?.(e);
return {
kind: "suppress",
reason: e
};
};
const n = t.applyChannelTransforms ?? true;
const hasContent = (t) => hasReplyPayloadContent({
...e,
text: t
});
const r = normalizeOptionalString(e.text) ?? "";
const i = hasReplyPayloadSpeechContent(e);
if (!hasContent(r) && !i) return suppress("empty");
let a = e.text;
if (getReplyPayloadMetadata(e)?.silentReply === true) {
if (!hasContent("") && !i) return suppress("silent");
a = "";
}
if (a && !r) a = "";
if (a?.includes("HEARTBEAT_OK")) {
const e = stripHeartbeatToken(a, { mode: "message" });
if (e.didStrip) t.onHeartbeatStrip?.();
if (e.shouldSkip && !hasContent(e.text)) return suppress("heartbeat");
a = e.text;
}
if (a && isInternalFormattingArtifact(a) && !hasContent("")) return suppress("silent");
if (a) a = e.isError ? renderUserFacingText(a, {
errorContext: true,
conversationContext: t.conversationContext
}) : sanitizeUserFacingText(a, {
conversationContext: t.conversationContext,
preserveLeadingSourceBoundary: t.preserveLeadingSourceBoundary
});
if (!hasContent(a) && !i) return suppress("empty");
let o = copyReplyPayloadMetadata(e, {
...e,
text: a
});
const s = resolveNormalizeReplyTransformOwner(t.transformReplyPayload);
const c = s != null && getReplyPayloadMetadata(o)?.channelReplyTransformOwner === s;
if (n && t.transformReplyPayload && !c) {
const e = t.transformReplyPayload(o);
if (e === null) return suppress("channel_transform");
const n = e ? copyReplyPayloadMetadata(o, e) : o;
const r = resolveNormalizeReplyTransformOwner(t.transformReplyPayload);
o = r ? setReplyPayloadMetadata(n, { channelReplyTransformOwner: r }) : n;
a = o.text;
}
const l = t.responsePrefixContext ? resolveResponsePrefixTemplate(t.responsePrefix, t.responsePrefixContext) : t.responsePrefix;
if (l && a && a.trim() !== "HEARTBEAT_OK" && !a.startsWith(l)) {
const e = `${l} `;
a = `${e}${a}`;
setReplyPayloadMetadata(o, { responsePrefix: e });
}
o = copyReplyPayloadMetadata(o, {
...o,
text: a
});
return {
kind: "deliver",
payload: o
};
}
function normalizeReplyPayload(e, t = {}) {
const n = normalizeReplyPayloadOutcome(e, t);
return n.kind === "deliver" ? n.payload : null;
}
//#endregion
export { resolveResponsePrefixTemplate as a, extractShortModelName as i, normalizeReplyPayload as n, normalizeReplyPayloadOutcome as r, bindNormalizeReplyTransformOwner as t };