fix: persist deliveryContext on last-route updates
Co-authored-by: Adam Holt <mail@adamholt.co.nz>
This commit is contained in:
@@ -121,8 +121,10 @@ describe("sessions", () => {
|
|||||||
await updateLastRoute({
|
await updateLastRoute({
|
||||||
storePath,
|
storePath,
|
||||||
sessionKey: mainSessionKey,
|
sessionKey: mainSessionKey,
|
||||||
channel: "telegram",
|
deliveryContext: {
|
||||||
to: " 12345 ",
|
channel: "telegram",
|
||||||
|
to: " 12345 ",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const store = loadSessionStore(storePath);
|
const store = loadSessionStore(storePath);
|
||||||
@@ -142,6 +144,36 @@ describe("sessions", () => {
|
|||||||
expect(store[mainSessionKey]?.compactionCount).toBe(2);
|
expect(store[mainSessionKey]?.compactionCount).toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("updateLastRoute prefers explicit deliveryContext", async () => {
|
||||||
|
const mainSessionKey = "agent:main:main";
|
||||||
|
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: mainSessionKey,
|
||||||
|
channel: "whatsapp",
|
||||||
|
to: "111",
|
||||||
|
accountId: "legacy",
|
||||||
|
deliveryContext: {
|
||||||
|
channel: "telegram",
|
||||||
|
to: "222",
|
||||||
|
accountId: "primary",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const store = loadSessionStore(storePath);
|
||||||
|
expect(store[mainSessionKey]?.lastChannel).toBe("telegram");
|
||||||
|
expect(store[mainSessionKey]?.lastTo).toBe("222");
|
||||||
|
expect(store[mainSessionKey]?.lastAccountId).toBe("primary");
|
||||||
|
expect(store[mainSessionKey]?.deliveryContext).toEqual({
|
||||||
|
channel: "telegram",
|
||||||
|
to: "222",
|
||||||
|
accountId: "primary",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("updateSessionStore preserves concurrent additions", async () => {
|
it("updateSessionStore preserves concurrent additions", async () => {
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-sessions-"));
|
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-sessions-"));
|
||||||
const storePath = path.join(dir, "sessions.json");
|
const storePath = path.join(dir, "sessions.json");
|
||||||
|
|||||||
@@ -4,7 +4,13 @@ import path from "node:path";
|
|||||||
|
|
||||||
import JSON5 from "json5";
|
import JSON5 from "json5";
|
||||||
import { getFileMtimeMs, isCacheEnabled, resolveCacheTtlMs } from "../cache-utils.js";
|
import { getFileMtimeMs, isCacheEnabled, resolveCacheTtlMs } from "../cache-utils.js";
|
||||||
import { normalizeSessionDeliveryFields } from "../../utils/delivery-context.js";
|
import {
|
||||||
|
deliveryContextFromSession,
|
||||||
|
mergeDeliveryContext,
|
||||||
|
normalizeDeliveryContext,
|
||||||
|
normalizeSessionDeliveryFields,
|
||||||
|
type DeliveryContext,
|
||||||
|
} from "../../utils/delivery-context.js";
|
||||||
import { mergeSessionEntry, type SessionEntry } from "./types.js";
|
import { mergeSessionEntry, type SessionEntry } from "./types.js";
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -323,20 +329,29 @@ export async function updateSessionStoreEntry(params: {
|
|||||||
export async function updateLastRoute(params: {
|
export async function updateLastRoute(params: {
|
||||||
storePath: string;
|
storePath: string;
|
||||||
sessionKey: string;
|
sessionKey: string;
|
||||||
channel: SessionEntry["lastChannel"];
|
channel?: SessionEntry["lastChannel"];
|
||||||
to?: string;
|
to?: string;
|
||||||
accountId?: string;
|
accountId?: string;
|
||||||
|
deliveryContext?: DeliveryContext;
|
||||||
}) {
|
}) {
|
||||||
const { storePath, sessionKey, channel, to, accountId } = params;
|
const { storePath, sessionKey, channel, to, accountId } = params;
|
||||||
return await withSessionStoreLock(storePath, async () => {
|
return await withSessionStoreLock(storePath, async () => {
|
||||||
const store = loadSessionStore(storePath);
|
const store = loadSessionStore(storePath);
|
||||||
const existing = store[sessionKey];
|
const existing = store[sessionKey];
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
const explicitContext = normalizeDeliveryContext(params.deliveryContext);
|
||||||
|
const inlineContext = normalizeDeliveryContext({
|
||||||
|
channel,
|
||||||
|
to,
|
||||||
|
accountId,
|
||||||
|
});
|
||||||
|
const mergedInput = mergeDeliveryContext(explicitContext, inlineContext);
|
||||||
|
const merged = mergeDeliveryContext(mergedInput, deliveryContextFromSession(existing));
|
||||||
const normalized = normalizeSessionDeliveryFields({
|
const normalized = normalizeSessionDeliveryFields({
|
||||||
deliveryContext: {
|
deliveryContext: {
|
||||||
channel: channel ?? existing?.lastChannel ?? existing?.deliveryContext?.channel,
|
channel: merged?.channel,
|
||||||
to: to ?? existing?.lastTo ?? existing?.deliveryContext?.to,
|
to: merged?.to,
|
||||||
accountId: accountId ?? existing?.lastAccountId ?? existing?.deliveryContext?.accountId,
|
accountId: merged?.accountId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const next = mergeSessionEntry(existing, {
|
const next = mergeSessionEntry(existing, {
|
||||||
|
|||||||
@@ -272,9 +272,11 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
|
|||||||
await updateLastRoute({
|
await updateLastRoute({
|
||||||
storePath,
|
storePath,
|
||||||
sessionKey: route.mainSessionKey,
|
sessionKey: route.mainSessionKey,
|
||||||
channel: "discord",
|
deliveryContext: {
|
||||||
to: `user:${author.id}`,
|
channel: "discord",
|
||||||
accountId: route.accountId,
|
to: `user:${author.id}`,
|
||||||
|
accountId: route.accountId,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -121,8 +121,10 @@ describe("monitorIMessageProvider", () => {
|
|||||||
|
|
||||||
expect(updateLastRouteMock).toHaveBeenCalledWith(
|
expect(updateLastRouteMock).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
channel: "imessage",
|
deliveryContext: expect.objectContaining({
|
||||||
to: "+15550004444",
|
channel: "imessage",
|
||||||
|
to: "+15550004444",
|
||||||
|
}),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -453,9 +453,11 @@ export async function monitorIMessageProvider(opts: MonitorIMessageOpts = {}): P
|
|||||||
await updateLastRoute({
|
await updateLastRoute({
|
||||||
storePath,
|
storePath,
|
||||||
sessionKey: route.mainSessionKey,
|
sessionKey: route.mainSessionKey,
|
||||||
channel: "imessage",
|
deliveryContext: {
|
||||||
to,
|
channel: "imessage",
|
||||||
accountId: route.accountId,
|
to,
|
||||||
|
accountId: route.accountId,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,9 +143,11 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) {
|
|||||||
await updateLastRoute({
|
await updateLastRoute({
|
||||||
storePath,
|
storePath,
|
||||||
sessionKey: route.mainSessionKey,
|
sessionKey: route.mainSessionKey,
|
||||||
channel: "signal",
|
deliveryContext: {
|
||||||
to: entry.senderRecipient,
|
channel: "signal",
|
||||||
accountId: route.accountId,
|
to: entry.senderRecipient,
|
||||||
|
accountId: route.accountId,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,9 +32,11 @@ export async function dispatchPreparedSlackMessage(prepared: PreparedSlackMessag
|
|||||||
await updateLastRoute({
|
await updateLastRoute({
|
||||||
storePath,
|
storePath,
|
||||||
sessionKey: route.mainSessionKey,
|
sessionKey: route.mainSessionKey,
|
||||||
channel: "slack",
|
deliveryContext: {
|
||||||
to: `user:${message.user}`,
|
channel: "slack",
|
||||||
accountId: route.accountId,
|
to: `user:${message.user}`,
|
||||||
|
accountId: route.accountId,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -489,9 +489,11 @@ export const buildTelegramMessageContext = async ({
|
|||||||
await updateLastRoute({
|
await updateLastRoute({
|
||||||
storePath,
|
storePath,
|
||||||
sessionKey: route.mainSessionKey,
|
sessionKey: route.mainSessionKey,
|
||||||
channel: "telegram",
|
deliveryContext: {
|
||||||
to: String(chatId),
|
channel: "telegram",
|
||||||
accountId: route.accountId,
|
to: String(chatId),
|
||||||
|
accountId: route.accountId,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,9 +28,11 @@ export function updateLastRouteInBackground(params: {
|
|||||||
const task = updateLastRoute({
|
const task = updateLastRoute({
|
||||||
storePath,
|
storePath,
|
||||||
sessionKey: params.sessionKey,
|
sessionKey: params.sessionKey,
|
||||||
channel: params.channel,
|
deliveryContext: {
|
||||||
to: params.to,
|
channel: params.channel,
|
||||||
accountId: params.accountId,
|
to: params.to,
|
||||||
|
accountId: params.accountId,
|
||||||
|
},
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
params.warn(
|
params.warn(
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user