openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
394 lines (393 loc) • 13.5 kB
JavaScript
import { l as normalizeOptionalString, m as readNonBlankString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
import { y as freezeDiagnosticTraceContext } from "./diagnostics-timeline-CSwiG24r.mjs";
import { i as stripChannelPrefix } from "./string-readers-BtMVaphq.mjs";
import { n as internalSessionConversationId } from "./message-channel-constants-CkbqRgGl.mjs";
import { t as normalizeAnyChannelId } from "./registry-normalize-guW5_4yo.mjs";
import { t as getChannelPlugin } from "./registry-ByqVE5S3.mjs";
import "./plugins-BoMiRtiY.mjs";
import { d as normalizeMediaFacts } from "./media-facts-B-0Dpo5L.mjs";
//#region src/hooks/message-hook-media.ts
/** Copies runtime media into the public hook shape without internal staging/hydration flags. */
function projectMessageHookMediaFacts(e) {
return (e ?? []).map((e) => {
const t = {};
Object.assign(t, e.path !== void 0 ? { path: e.path } : {}, e.url !== void 0 ? { url: e.url } : {}, e.contentType !== void 0 ? { contentType: e.contentType } : {}, e.kind !== void 0 ? { kind: e.kind } : {}, e.transcribed === true ? { transcribed: true } : {}, e.messageId !== void 0 ? { messageId: e.messageId } : {}, e.workspaceDir !== void 0 ? { workspaceDir: e.workspaceDir } : {});
return t;
});
}
//#endregion
//#region src/hooks/message-hook-mappers.ts
function projectHookMediaAliases(e) {
const t = e.mediaStagingPending ? void 0 : e;
return {
mediaPath: t?.mediaPath,
mediaUrl: t?.mediaUrl,
mediaType: t?.mediaType,
mediaPaths: t?.mediaPaths,
mediaUrls: t?.mediaUrls,
mediaTypes: t?.mediaTypes
};
}
function assignRemoteMediaStagingMetadata(e, t) {
const n = {
mediaRemoteHost: t.mediaRemoteHost,
mediaStagingPending: t.mediaStagingPending,
originalMediaPath: t.originalMediaPath,
originalMediaUrl: t.originalMediaUrl,
originalMediaType: t.originalMediaType,
originalMediaPaths: t.originalMediaPaths,
originalMediaUrls: t.originalMediaUrls,
originalMediaTypes: t.originalMediaTypes
};
for (const [t, r] of Object.entries(n)) if (r !== void 0) e[t] = r;
}
function projectInboundMessageMetadata(e) {
const t = {
to: e.to,
provider: e.provider,
surface: e.surface,
senderE164: e.senderE164,
...projectHookMediaAliases(e),
guildId: e.guildId,
channelName: e.channelName,
topicName: e.topicName
};
assignRemoteMediaStagingMetadata(t, e);
return t;
}
function projectHookMediaState(e) {
const t = e.mediaStagingPending === true;
const n = t ? void 0 : e.media;
const r = e.originalMedia ?? (t ? e.media : void 0);
return {
...n?.length ? { media: n.map((e) => Object.assign({}, e)) } : {},
...r?.length ? { originalMedia: r.map((e) => Object.assign({}, e)) } : {},
...t ? { mediaStagingPending: true } : {}
};
}
function deriveInboundMessageHookContextBase(e, t) {
const n = t?.content ?? readNonBlankString(e.BodyForCommands) ?? readNonBlankString(e.RawBody) ?? readNonBlankString(e.Body) ?? "";
const r = normalizeLowercaseStringOrEmpty(e.OriginatingChannel ?? e.Surface ?? e.Provider ?? "");
const i = e.OriginatingTo ?? e.To ?? e.From ?? internalSessionConversationId(r, e.SessionKey);
const a = Boolean(e.GroupSubject || e.GroupChannel);
const o = normalizeMediaFacts(e.media);
const s = projectMessageHookMediaFacts(o);
const compact = (e) => {
const t = e.filter((e) => Boolean(e));
return t.length > 0 ? t : void 0;
};
const c = compact(o.map((e) => e.path));
const l = compact(o.map((e) => e.url ?? e.path));
const u = compact(o.map((e) => e.contentType ?? e.kind));
const d = o[0];
const f = Number.isFinite(e.LocationLat) && Number.isFinite(e.LocationLon);
const p = e.LocationSource === "pin" || e.LocationSource === "place" || e.LocationSource === "live" ? e.LocationSource : void 0;
const m = normalizeOptionalString(e.ProviderUpdateId);
const h = normalizeOptionalString(e.ProviderUpdateKind);
return {
from: e.From ?? "",
to: e.To,
content: n,
body: e.Body,
bodyForAgent: e.BodyForAgent,
transcript: e.Transcript,
timestamp: Number.isFinite(e.Timestamp) ? e.Timestamp : void 0,
channelId: r,
accountId: e.AccountId,
conversationId: i,
sessionKey: e.SessionKey,
agentId: e.AgentId,
messageId: normalizeOptionalString(t?.messageId) ?? normalizeOptionalString(e.MessageSidFull) ?? normalizeOptionalString(e.MessageSid) ?? normalizeOptionalString(e.MessageSidFirst) ?? normalizeOptionalString(e.MessageSidLast),
senderId: e.SenderId,
senderName: e.SenderName,
senderUsername: e.SenderUsername,
senderE164: e.SenderE164,
replyToId: e.ReplyToId,
replyToIdFull: e.ReplyToIdFull,
replyToBody: e.ReplyToBody,
replyToSender: e.ReplyToSender,
replyToIsQuote: e.ReplyToIsQuote,
provider: e.Provider,
surface: e.Surface,
threadId: e.MessageThreadId,
threadParentId: e.ThreadParentId,
...s.length > 0 ? { media: s } : {},
mediaPath: d?.path ?? c?.[0],
mediaUrl: d?.url ?? d?.path ?? l?.[0],
mediaType: d?.contentType ?? d?.kind ?? u?.[0],
mediaPaths: c,
mediaUrls: l,
mediaTypes: u,
originatingChannel: e.OriginatingChannel,
originatingTo: e.OriginatingTo,
guildId: e.GroupSpace,
channelName: e.GroupChannel,
isGroup: a,
groupId: a ? i : void 0,
topicName: e.TopicName,
...f ? { location: {
latitude: e.LocationLat,
longitude: e.LocationLon,
...typeof e.LocationAccuracy === "number" ? { accuracy: e.LocationAccuracy } : {},
...e.LocationName ? { name: e.LocationName } : {},
...e.LocationAddress ? { address: e.LocationAddress } : {},
...p ? { source: p } : {},
...typeof e.LocationIsLive === "boolean" ? { isLive: e.LocationIsLive } : {},
...Number.isFinite(e.LocationLivePeriodSeconds) ? { livePeriodSeconds: e.LocationLivePeriodSeconds } : {},
...e.LocationCaption ? { caption: e.LocationCaption } : {}
} } : {},
...m && h ? { providerUpdate: {
id: m,
kind: h,
...normalizeOptionalString(e.MessageSidFull ?? e.MessageSid) ? { messageId: normalizeOptionalString(e.MessageSidFull ?? e.MessageSid) } : {},
...Number.isFinite(e.ProviderMessageTimestamp) ? { messageTimestamp: e.ProviderMessageTimestamp } : {},
...Number.isFinite(e.ProviderEditTimestamp) ? { editedTimestamp: e.ProviderEditTimestamp } : {}
} } : {}
};
}
function deriveInboundMessageHookContext(e, t) {
return deriveInboundMessageHookContextBase(e, t);
}
function buildCanonicalSentMessageHookContext(e) {
return {
to: e.to,
content: e.content,
success: e.success,
error: e.error,
channelId: e.channelId,
accountId: e.accountId,
conversationId: e.conversationId ?? e.to,
sessionKey: e.sessionKey,
runId: e.runId,
messageId: e.messageId,
trace: e.trace,
callDepth: e.callDepth,
isGroup: e.isGroup,
groupId: e.groupId
};
}
/** Resolves the outbound hook target for a reply produced by an inbound channel turn. */
function resolveInboundReplyHookTarget(e, t) {
const n = readNonBlankString(e.OriginatingTo);
if (n) return n;
if (t.isGroup) return t.conversationId ?? t.to ?? t.from;
return t.from || t.conversationId || t.to || "";
}
function assignTraceFields(e, t) {
if (!t) return;
const n = freezeDiagnosticTraceContext(t);
e.trace = n;
e.traceId = n.traceId;
if (n.spanId) e.spanId = n.spanId;
if (n.parentSpanId) e.parentSpanId = n.parentSpanId;
}
function projectHookReplyFields(e) {
return {
..."replyToId" in e && e.replyToId !== void 0 ? { replyToId: e.replyToId } : {},
..."replyToIdFull" in e && e.replyToIdFull !== void 0 ? { replyToIdFull: e.replyToIdFull } : {},
..."replyToBody" in e && e.replyToBody !== void 0 ? { replyToBody: e.replyToBody } : {},
..."replyToSender" in e && e.replyToSender !== void 0 ? { replyToSender: e.replyToSender } : {},
..."replyToIsQuote" in e && e.replyToIsQuote !== void 0 ? { replyToIsQuote: e.replyToIsQuote } : {}
};
}
function toPluginMessageContext(e) {
const t = {
channelId: e.channelId,
accountId: e.accountId,
conversationId: e.conversationId
};
if (e.sessionKey) t.sessionKey = e.sessionKey;
if (e.runId) t.runId = e.runId;
if (e.messageId) t.messageId = e.messageId;
if ("senderId" in e && e.senderId) t.senderId = e.senderId;
Object.assign(t, projectHookReplyFields(e));
assignTraceFields(t, e.trace);
if (e.callDepth != null) t.callDepth = e.callDepth;
return t;
}
function resolveInboundConversation(e) {
const t = normalizeAnyChannelId(e.channelId);
const n = t ? getChannelPlugin(t)?.messaging?.resolveInboundConversation?.({
from: e.from,
to: e.to ?? e.originatingTo,
conversationId: e.conversationId,
threadId: e.threadId,
threadParentId: e.threadParentId,
isGroup: e.isGroup
}) : void 0;
if (n === null) return {};
if (n) return {
conversationId: normalizeOptionalString(n.conversationId),
parentConversationId: normalizeOptionalString(n.parentConversationId)
};
return { conversationId: stripChannelPrefix(e.to ?? e.originatingTo ?? e.conversationId, e.channelId) };
}
function projectPluginInboundMessageFacts(e) {
return {
content: e.content,
timestamp: e.timestamp,
senderId: e.senderId,
...projectHookReplyFields(e),
threadId: e.threadId,
messageId: e.messageId,
sessionKey: e.sessionKey,
runId: e.runId,
...e.location ? { location: { ...e.location } } : {},
...e.providerUpdate ? { providerUpdate: { ...e.providerUpdate } } : {},
...projectHookMediaState(e),
metadata: {
...projectInboundMessageMetadata(e),
originatingChannel: e.originatingChannel,
originatingTo: e.originatingTo,
replyToId: e.replyToId,
replyToIdFull: e.replyToIdFull,
replyToBody: e.replyToBody,
replyToSender: e.replyToSender,
replyToIsQuote: e.replyToIsQuote
}
};
}
function toPluginInboundClaimPair(e, t) {
const n = resolveInboundConversation(e);
const r = {
channelId: e.channelId,
accountId: e.accountId,
conversationId: n.conversationId,
sessionKey: e.sessionKey,
agentId: e.agentId,
parentConversationId: n.parentConversationId,
senderId: e.senderId,
messageId: e.messageId,
runId: e.runId,
callDepth: e.callDepth
};
Object.assign(r, projectHookReplyFields(e));
assignTraceFields(r, e.trace);
const i = projectPluginInboundMessageFacts(e);
const a = {
...i,
body: e.body,
bodyForAgent: e.bodyForAgent,
transcript: e.transcript,
channel: e.channelId,
accountId: e.accountId,
conversationId: r.conversationId,
parentConversationId: r.parentConversationId,
senderName: e.senderName,
senderUsername: e.senderUsername,
isGroup: e.isGroup,
commandAuthorized: t?.commandAuthorized,
wasMentioned: t?.wasMentioned,
metadata: {
...i.metadata,
from: e.from,
groupId: e.groupId
}
};
assignTraceFields(a, e.trace);
return {
context: r,
event: a
};
}
function toPluginMessageReceivedEvent(e) {
const t = projectPluginInboundMessageFacts(e);
const n = {
...t,
from: e.from,
metadata: {
...t.metadata,
threadId: e.threadId,
messageId: e.messageId,
senderId: e.senderId,
senderName: e.senderName,
senderUsername: e.senderUsername
}
};
assignTraceFields(n, e.trace);
return n;
}
function toPluginMessageSentEvent(e) {
const t = {
to: e.to,
content: e.content,
success: e.success,
...e.messageId ? { messageId: e.messageId } : {},
...e.sessionKey ? { sessionKey: e.sessionKey } : {},
...e.runId ? { runId: e.runId } : {},
...e.error ? { error: e.error } : {}
};
assignTraceFields(t, e.trace);
return t;
}
function toInternalMessageReceivedContext(e) {
return {
from: e.from,
content: e.content,
timestamp: e.timestamp,
channelId: e.channelId,
accountId: e.accountId,
conversationId: e.conversationId,
messageId: e.messageId,
...projectHookMediaState(e),
metadata: {
...projectInboundMessageMetadata(e),
threadId: e.threadId,
senderId: e.senderId,
senderName: e.senderName,
senderUsername: e.senderUsername
}
};
}
function toInternalMessageTranscribedContext(e, t) {
return {
...toInternalInboundMessageHookContextBase(e),
transcript: e.transcript ?? "",
cfg: t
};
}
function toInternalMessagePreprocessedContext(e, t) {
return {
...toInternalInboundMessageHookContextBase(e),
transcript: e.transcript,
isGroup: e.isGroup,
groupId: e.groupId,
cfg: t
};
}
function toInternalInboundMessageHookContextBase(e) {
return {
from: e.from,
to: e.to,
body: e.body,
bodyForAgent: e.bodyForAgent,
timestamp: e.timestamp,
channelId: e.channelId,
conversationId: e.conversationId,
messageId: e.messageId,
senderId: e.senderId,
senderName: e.senderName,
senderUsername: e.senderUsername,
provider: e.provider,
surface: e.surface,
...projectHookMediaState(e),
mediaPath: e.mediaStagingPending ? void 0 : e.mediaPath,
mediaType: e.mediaStagingPending ? void 0 : e.mediaType
};
}
function toInternalMessageSentContext(e) {
return {
to: e.to,
content: e.content,
success: e.success,
...e.error ? { error: e.error } : {},
channelId: e.channelId,
accountId: e.accountId,
conversationId: e.conversationId,
messageId: e.messageId,
...e.isGroup != null ? { isGroup: e.isGroup } : {},
...e.groupId ? { groupId: e.groupId } : {}
};
}
//#endregion
export { toInternalMessageReceivedContext as a, toPluginInboundClaimPair as c, toPluginMessageSentEvent as d, toInternalMessagePreprocessedContext as i, toPluginMessageContext as l, deriveInboundMessageHookContext as n, toInternalMessageSentContext as o, resolveInboundReplyHookTarget as r, toInternalMessageTranscribedContext as s, buildCanonicalSentMessageHookContext as t, toPluginMessageReceivedEvent as u };