fix: persist session origin metadata

This commit is contained in:
Peter Steinberger
2026-01-18 03:40:35 +00:00
parent dad69afc84
commit 0d9172d761
13 changed files with 102 additions and 23 deletions

View File

@@ -176,6 +176,36 @@ describe("sessions", () => {
});
});
it("updateLastRoute records origin + group metadata when ctx is provided", async () => {
const sessionKey = "agent:main:whatsapp:group:123@g.us";
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-sessions-"));
const storePath = path.join(dir, "sessions.json");
await fs.writeFile(storePath, "{}", "utf-8");
await updateLastRoute({
storePath,
sessionKey,
deliveryContext: {
channel: "whatsapp",
to: "123@g.us",
},
ctx: {
Provider: "whatsapp",
ChatType: "group",
GroupSubject: "Family",
From: "123@g.us",
},
});
const store = loadSessionStore(storePath);
expect(store[sessionKey]?.subject).toBe("Family");
expect(store[sessionKey]?.channel).toBe("whatsapp");
expect(store[sessionKey]?.groupId).toBe("123@g.us");
expect(store[sessionKey]?.origin?.label).toBe("Family id:123@g.us");
expect(store[sessionKey]?.origin?.provider).toBe("whatsapp");
expect(store[sessionKey]?.origin?.chatType).toBe("group");
});
it("updateSessionStore preserves concurrent additions", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-sessions-"));
const storePath = path.join(dir, "sessions.json");

View File

@@ -368,8 +368,10 @@ export async function updateLastRoute(params: {
to?: string;
accountId?: string;
deliveryContext?: DeliveryContext;
ctx?: MsgContext;
groupResolution?: import("./types.js").GroupKeyResolution | null;
}) {
const { storePath, sessionKey, channel, to, accountId } = params;
const { storePath, sessionKey, channel, to, accountId, ctx } = params;
return await withSessionStoreLock(storePath, async () => {
const store = loadSessionStore(storePath);
const existing = store[sessionKey];
@@ -389,13 +391,22 @@ export async function updateLastRoute(params: {
accountId: merged?.accountId,
},
});
const next = mergeSessionEntry(existing, {
const metaPatch = ctx
? deriveSessionMetaPatch({
ctx,
sessionKey,
existing,
groupResolution: params.groupResolution,
})
: null;
const basePatch: Partial<SessionEntry> = {
updatedAt: Math.max(existing?.updatedAt ?? 0, now),
deliveryContext: normalized.deliveryContext,
lastChannel: normalized.lastChannel,
lastTo: normalized.lastTo,
lastAccountId: normalized.lastAccountId,
});
};
const next = mergeSessionEntry(existing, metaPatch ? { ...basePatch, ...metaPatch } : basePatch);
store[sessionKey] = next;
await saveSessionStoreUnlocked(storePath, store);
return next;