fix: persist session origin metadata
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import type { MsgContext } from "../../../auto-reply/templating.js";
|
||||
import type { loadConfig } from "../../../config/config.js";
|
||||
import { resolveStorePath, updateLastRoute } from "../../../config/sessions.js";
|
||||
import { formatError } from "../../session.js";
|
||||
@@ -20,6 +21,7 @@ export function updateLastRouteInBackground(params: {
|
||||
channel: "whatsapp";
|
||||
to: string;
|
||||
accountId?: string;
|
||||
ctx?: MsgContext;
|
||||
warn: (obj: unknown, msg: string) => void;
|
||||
}) {
|
||||
const storePath = resolveStorePath(params.cfg.session?.store, {
|
||||
@@ -33,6 +35,7 @@ export function updateLastRouteInBackground(params: {
|
||||
to: params.to,
|
||||
accountId: params.accountId,
|
||||
},
|
||||
ctx: params.ctx,
|
||||
}).catch((err) => {
|
||||
params.warn(
|
||||
{
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { MsgContext } from "../../../auto-reply/templating.js";
|
||||
import type { getReplyFromConfig } from "../../../auto-reply/reply.js";
|
||||
import type { loadConfig } from "../../../config/config.js";
|
||||
import { logVerbose } from "../../../globals.js";
|
||||
@@ -94,6 +95,22 @@ export function createWebOnMessageHandler(params: {
|
||||
}
|
||||
|
||||
if (msg.chatType === "group") {
|
||||
const metaCtx = {
|
||||
From: msg.from,
|
||||
To: msg.to,
|
||||
SessionKey: route.sessionKey,
|
||||
AccountId: route.accountId,
|
||||
ChatType: msg.chatType,
|
||||
ConversationLabel: conversationId,
|
||||
GroupSubject: msg.groupSubject,
|
||||
SenderName: msg.senderName,
|
||||
SenderId: msg.senderJid?.trim() || msg.senderE164,
|
||||
SenderE164: msg.senderE164,
|
||||
Provider: "whatsapp",
|
||||
Surface: "whatsapp",
|
||||
OriginatingChannel: "whatsapp",
|
||||
OriginatingTo: conversationId,
|
||||
} satisfies MsgContext;
|
||||
updateLastRouteInBackground({
|
||||
cfg: params.cfg,
|
||||
backgroundTasks: params.backgroundTasks,
|
||||
@@ -102,6 +119,7 @@ export function createWebOnMessageHandler(params: {
|
||||
channel: "whatsapp",
|
||||
to: conversationId,
|
||||
accountId: route.accountId,
|
||||
ctx: metaCtx,
|
||||
warn: params.replyLogger.warn.bind(params.replyLogger),
|
||||
});
|
||||
|
||||
|
||||
@@ -206,26 +206,15 @@ export async function processMessage(params: {
|
||||
whatsappInboundLog.debug(`Inbound body: ${elide(combinedBody, 400)}`);
|
||||
}
|
||||
|
||||
if (params.msg.chatType !== "group") {
|
||||
const to = (() => {
|
||||
if (params.msg.senderE164) return normalizeE164(params.msg.senderE164);
|
||||
// In direct chats, `msg.from` is already the canonical conversation id.
|
||||
if (params.msg.from.includes("@")) return jidToE164(params.msg.from);
|
||||
return normalizeE164(params.msg.from);
|
||||
})();
|
||||
if (to) {
|
||||
updateLastRouteInBackground({
|
||||
cfg: params.cfg,
|
||||
backgroundTasks: params.backgroundTasks,
|
||||
storeAgentId: params.route.agentId,
|
||||
sessionKey: params.route.mainSessionKey,
|
||||
channel: "whatsapp",
|
||||
to,
|
||||
accountId: params.route.accountId,
|
||||
warn: params.replyLogger.warn.bind(params.replyLogger),
|
||||
});
|
||||
}
|
||||
}
|
||||
const dmRouteTarget =
|
||||
params.msg.chatType !== "group"
|
||||
? (() => {
|
||||
if (params.msg.senderE164) return normalizeE164(params.msg.senderE164);
|
||||
// In direct chats, `msg.from` is already the canonical conversation id.
|
||||
if (params.msg.from.includes("@")) return jidToE164(params.msg.from);
|
||||
return normalizeE164(params.msg.from);
|
||||
})()
|
||||
: undefined;
|
||||
|
||||
const textLimit = params.maxMediaTextChunkLimit ?? resolveTextChunkLimit(params.cfg, "whatsapp");
|
||||
let didLogHeartbeatStrip = false;
|
||||
@@ -285,6 +274,20 @@ export async function processMessage(params: {
|
||||
OriginatingTo: params.msg.from,
|
||||
});
|
||||
|
||||
if (dmRouteTarget) {
|
||||
updateLastRouteInBackground({
|
||||
cfg: params.cfg,
|
||||
backgroundTasks: params.backgroundTasks,
|
||||
storeAgentId: params.route.agentId,
|
||||
sessionKey: params.route.mainSessionKey,
|
||||
channel: "whatsapp",
|
||||
to: dmRouteTarget,
|
||||
accountId: params.route.accountId,
|
||||
ctx: ctxPayload,
|
||||
warn: params.replyLogger.warn.bind(params.replyLogger),
|
||||
});
|
||||
}
|
||||
|
||||
const storePath = resolveStorePath(params.cfg.session?.store, {
|
||||
agentId: params.route.agentId,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user