mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-29 07:01:40 +03:00
fix: recover telegram sends from stale thread ids
This commit is contained in:
@@ -176,6 +176,51 @@ describe("sessions", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("updateLastRoute clears threadId when deliveryContext explicitly omits it", async () => {
|
||||
const mainSessionKey = "agent:main:main";
|
||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sessions-"));
|
||||
const storePath = path.join(dir, "sessions.json");
|
||||
await fs.writeFile(
|
||||
storePath,
|
||||
JSON.stringify(
|
||||
{
|
||||
[mainSessionKey]: {
|
||||
sessionId: "sess-1",
|
||||
updatedAt: 123,
|
||||
deliveryContext: {
|
||||
channel: "telegram",
|
||||
to: "222",
|
||||
threadId: "42",
|
||||
},
|
||||
lastChannel: "telegram",
|
||||
lastTo: "222",
|
||||
lastThreadId: "42",
|
||||
},
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
"utf-8",
|
||||
);
|
||||
|
||||
await updateLastRoute({
|
||||
storePath,
|
||||
sessionKey: mainSessionKey,
|
||||
deliveryContext: {
|
||||
channel: "telegram",
|
||||
to: "222",
|
||||
threadId: undefined,
|
||||
},
|
||||
});
|
||||
|
||||
const store = loadSessionStore(storePath);
|
||||
expect(store[mainSessionKey]?.deliveryContext).toEqual({
|
||||
channel: "telegram",
|
||||
to: "222",
|
||||
});
|
||||
expect(store[mainSessionKey]?.lastThreadId).toBeUndefined();
|
||||
});
|
||||
|
||||
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(), "openclaw-sessions-"));
|
||||
|
||||
@@ -86,6 +86,15 @@ function normalizeSessionEntryDelivery(entry: SessionEntry): SessionEntry {
|
||||
};
|
||||
}
|
||||
|
||||
function removeThreadFromDeliveryContext(context?: DeliveryContext): DeliveryContext | undefined {
|
||||
if (!context || context.threadId == null) {
|
||||
return context;
|
||||
}
|
||||
const next: DeliveryContext = { ...context };
|
||||
delete next.threadId;
|
||||
return next;
|
||||
}
|
||||
|
||||
function normalizeSessionStore(store: Record<string, SessionEntry>): void {
|
||||
for (const [key, entry] of Object.entries(store)) {
|
||||
if (!entry) {
|
||||
@@ -430,7 +439,15 @@ export async function updateLastRoute(params: {
|
||||
threadId,
|
||||
});
|
||||
const mergedInput = mergeDeliveryContext(explicitContext, inlineContext);
|
||||
const merged = mergeDeliveryContext(mergedInput, deliveryContextFromSession(existing));
|
||||
const explicitDeliveryContext = params.deliveryContext;
|
||||
const clearThreadFromFallback =
|
||||
explicitDeliveryContext != null &&
|
||||
Object.prototype.hasOwnProperty.call(explicitDeliveryContext, "threadId") &&
|
||||
explicitDeliveryContext.threadId == null;
|
||||
const fallbackContext = clearThreadFromFallback
|
||||
? removeThreadFromDeliveryContext(deliveryContextFromSession(existing))
|
||||
: deliveryContextFromSession(existing);
|
||||
const merged = mergeDeliveryContext(mergedInput, fallbackContext);
|
||||
const normalized = normalizeSessionDeliveryFields({
|
||||
deliveryContext: {
|
||||
channel: merged?.channel,
|
||||
|
||||
Reference in New Issue
Block a user