Skip to content

Commit 671a076

Browse files
fix(plugin): record inbound session with updateLastRoute for botschat channel
BotsChat plugin was missing recordInboundSession calls, so lastChannel was never set to "botschat" in the session store. This caused cron delivery with channel:"botschat" to fail because the gateway couldn't resolve the delivery target. Add recordInboundSession with updateLastRoute in both user message and cron dispatch paths. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9e5056d commit 671a076

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

packages/plugin/src/channel.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,33 @@ async function handleCloudMessage(
784784
// Finalize the context (normalizes fields, resolves agent route)
785785
const finalizedCtx = runtime.channel.reply.finalizeInboundContext(msgCtx);
786786

787+
// Record session metadata and update delivery route so that cron
788+
// delivery with channel:"botschat" can resolve the target.
789+
// Without this, lastChannel is never set to "botschat" in the
790+
// session store, causing delivery resolution to fail.
791+
if (runtime.channel.session?.recordInboundSession) {
792+
try {
793+
const storePath = runtime.channel.session.resolveStorePath(cfg);
794+
await runtime.channel.session.recordInboundSession({
795+
storePath,
796+
sessionKey: finalizedCtx.SessionKey ?? msg.sessionKey,
797+
ctx: finalizedCtx,
798+
updateLastRoute: {
799+
sessionKey: finalizedCtx.SessionKey ?? msg.sessionKey,
800+
channel: "botschat",
801+
to: msg.sessionKey,
802+
accountId: ctx.accountId ?? "default",
803+
...(threadId ? { threadId } : {}),
804+
},
805+
onRecordError: (err: unknown) => {
806+
console.error("[botschat] failed updating session meta:", err);
807+
},
808+
});
809+
} catch (err) {
810+
console.error("[botschat] recordInboundSession error:", err);
811+
}
812+
}
813+
787814
// Create a reply dispatcher that sends responses back through the cloud WSS
788815
// NOTE: reuses `client` from line ~424 (same block scope, same value)
789816
console.log(`[botschat] client for accountId=${ctx.accountId}: connected=${client?.connected}`);
@@ -1482,6 +1509,29 @@ async function handleTaskRun(
14821509

14831510
const finalizedCtx = runtime.channel.reply.finalizeInboundContext(msgCtx);
14841511

1512+
// Record session metadata for cron dispatch path (same as user message path)
1513+
if (runtime.channel.session?.recordInboundSession) {
1514+
try {
1515+
const storePath = runtime.channel.session.resolveStorePath(cfg);
1516+
await runtime.channel.session.recordInboundSession({
1517+
storePath,
1518+
sessionKey: finalizedCtx.SessionKey ?? sessionKey,
1519+
ctx: finalizedCtx,
1520+
updateLastRoute: {
1521+
sessionKey: finalizedCtx.SessionKey ?? sessionKey,
1522+
channel: "botschat",
1523+
to: sessionKey,
1524+
accountId: ctx.accountId ?? "default",
1525+
},
1526+
onRecordError: (err: unknown) => {
1527+
console.error("[botschat] failed updating session meta (cron):", err);
1528+
},
1529+
});
1530+
} catch (err) {
1531+
console.error("[botschat] recordInboundSession error (cron):", err);
1532+
}
1533+
}
1534+
14851535
// Collect the agent's reply as summary + stream output in real-time
14861536
// We accumulate completed message blocks and current streaming text.
14871537
// The frontend receives the full accumulated text each time and renders

0 commit comments

Comments
 (0)