openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
110 lines (109 loc) • 5.3 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { t as normalizeChatType } from "./chat-type-CL5nBDaO.mjs";
import { _ as resolveStagedMediaFacts, f as projectMediaFacts, g as resolveMediaFacts, v as stripLegacyMediaContextFields } from "./media-facts-B-0Dpo5L.mjs";
import { t as normalizeInternalTurnContext } from "./internal-turn-source-pY_O_ky4.mjs";
import { t as resolveConversationLabel } from "./conversation-label-XcqInkwb.mjs";
import { l as resolveCommandTurnContext, n as createCommandTurnContext } from "./command-turn-context-BaN4kAo-.mjs";
import { t as normalizeInboundTextNewlines } from "./inbound-text-Bmx57Z44.mjs";
//#region src/auto-reply/reply/inbound-context.ts
const FINALIZED_INBOUND_CONTEXT = Symbol("openclaw.finalizedInboundContext");
function normalizeTextField(e) {
if (typeof e !== "string") return;
return normalizeInboundTextNewlines(e);
}
function isFinalizedInboundContext(e) {
return e[FINALIZED_INBOUND_CONTEXT] === true;
}
function resolveCanonicalInboundText(e, t = {}) {
const n = e.Body ?? "";
const r = e.RawBody ?? e.Transcript ?? normalizeTextField(e.BodyStripped) ?? n;
const i = t.forceBodyForAgent || t.forceBodyForCommands ? r : normalizeTextField(e.rawText) ?? r;
const a = t.forceBodyForAgent ? n : normalizeTextField(e.agentText) ?? normalizeTextField(e.BodyForAgent) ?? e.CommandBody ?? i;
const o = t.forceBodyForCommands ? e.CommandBody ?? i : normalizeTextField(e.commandText) ?? normalizeTextField(e.BodyForCommands) ?? e.CommandBody ?? i;
return {
commandText: e.CommandInterpretationSuppressed === true ? "" : o,
agentText: a,
rawText: i
};
}
function foldDeprecatedPromptContextFields(e) {
if (e.ChannelPromptContext === void 0 && e.UntrustedContext !== void 0) e.ChannelPromptContext = e.UntrustedContext;
delete e.UntrustedContext;
if (e.ChannelStructuredContext === void 0 && e.UntrustedStructuredContext !== void 0) e.ChannelStructuredContext = e.UntrustedStructuredContext;
delete e.UntrustedStructuredContext;
}
function applySupplementalContext(e) {
const t = e.SupplementalContext;
if (!t) return;
if (t.channelStructuredContext === void 0 && t.untrustedContext !== void 0) t.channelStructuredContext = t.untrustedContext;
delete t.untrustedContext;
const n = {
ReplyToId: t.quote?.id,
ReplyToIdFull: t.quote?.fullId,
ReplyToBody: t.quote?.body,
ReplyToSender: t.quote?.sender,
ReplyToIsQuote: t.quote?.isQuote,
ForwardedFrom: t.forwarded?.from,
ForwardedFromType: t.forwarded?.fromType,
ForwardedFromId: t.forwarded?.fromId,
ForwardedDate: t.forwarded?.date,
ThreadStarterBody: t.thread?.starterBody,
ThreadHistoryBody: t.thread?.historyBody,
ThreadLabel: t.thread?.label,
GroupSystemPrompt: t.groupSystemPrompt,
ChannelStructuredContext: t.channelStructuredContext
};
for (const [t, r] of Object.entries(n)) if (r !== void 0 && e[t] === void 0) e[t] = r;
delete e.SupplementalContext;
}
function finalizeInboundContextImpl(e, t, n) {
const r = e;
normalizeInternalTurnContext(r);
foldDeprecatedPromptContextFields(r);
applySupplementalContext(r);
r.Body = normalizeTextField(r.Body) ?? "";
r.RawBody = normalizeTextField(r.RawBody);
r.CommandBody = normalizeTextField(r.CommandBody);
r.Transcript = normalizeTextField(r.Transcript);
r.ThreadStarterBody = normalizeTextField(r.ThreadStarterBody);
r.ThreadHistoryBody = normalizeTextField(r.ThreadHistoryBody);
r.GroupSystemPrompt = normalizeTextField(r.GroupSystemPrompt);
if (Array.isArray(r.ChannelPromptContext)) r.ChannelPromptContext = r.ChannelPromptContext.map(normalizeTextField).filter((e) => Boolean(e));
const i = normalizeChatType(r.ChatType);
if (i && (t.forceChatType || r.ChatType !== i)) r.ChatType = i;
Object.assign(r, resolveCanonicalInboundText(r, t));
r.BodyForAgent = r.agentText;
r.BodyForCommands = r.commandText;
const a = resolveConversationLabel(r);
if (a) r.ConversationLabel = a;
const o = r.CommandInterpretationSuppressed === true;
r.CommandAuthorized = !o && r.CommandAuthorized === true;
r.CommandTurn = o ? createCommandTurnContext("message", {
authorized: false,
body: ""
}) : resolveCommandTurnContext(r);
if (r.CommandTurn.source === "native" || r.CommandTurn.source === "text") {
r.CommandSource = r.CommandTurn.source;
r.CommandAuthorized = r.CommandTurn.authorized;
} else r.CommandSource = void 0;
const s = (r.MediaStaged === true || normalizeOptionalString(r.MediaWorkspaceDir) ? resolveStagedMediaFacts(r) : resolveMediaFacts(r)).map((e) => (e.path || e.url) && !e.contentType && !e.kind ? Object.assign(e, { contentType: "application/octet-stream" }) : e);
if (s.length > 0) {
r.media = s;
if (n) Object.assign(r, projectMediaFacts(s));
}
if (!n) stripLegacyMediaContextFields(r);
Object.defineProperty(r, FINALIZED_INBOUND_CONTEXT, {
configurable: true,
value: true
});
return r;
}
function finalizeInboundContext(e, t = {}) {
return finalizeInboundContextImpl(e, t, false);
}
/** Preserve SDK media projections while exposing the finalizer's prepared command fact. */
function finalizeInboundContextForSdk(e, t = {}) {
return finalizeInboundContextImpl(e, t, true);
}
//#endregion
export { finalizeInboundContextForSdk as n, isFinalizedInboundContext as r, finalizeInboundContext as t };