Gateway: canonicalize sessionKey for chat routing

This commit is contained in:
Mariano Belinky 2026-02-07 22:58:39 +01:00
parent 05c7a95c8a
commit cd7512636e
1 changed files with 15 additions and 14 deletions

View File

@ -368,7 +368,8 @@ export const chatHandlers: GatewayRequestHandlers = {
return;
}
}
const { cfg, entry } = loadSessionEntry(p.sessionKey);
const rawSessionKey = p.sessionKey;
const { cfg, entry, canonicalKey: sessionKey } = loadSessionEntry(rawSessionKey);
const timeoutMs = resolveAgentTimeoutMs({
cfg,
overrideMs: p.timeoutMs,
@ -379,7 +380,7 @@ export const chatHandlers: GatewayRequestHandlers = {
const sendPolicy = resolveSendPolicy({
cfg,
entry,
sessionKey: p.sessionKey,
sessionKey,
channel: entry?.channel,
chatType: entry?.chatType,
});
@ -404,7 +405,7 @@ export const chatHandlers: GatewayRequestHandlers = {
broadcast: context.broadcast,
nodeSendToSession: context.nodeSendToSession,
},
{ sessionKey: p.sessionKey, stopReason: "stop" },
{ sessionKey: rawSessionKey, stopReason: "stop" },
);
respond(true, { ok: true, aborted: res.aborted, runIds: res.runIds });
return;
@ -432,7 +433,7 @@ export const chatHandlers: GatewayRequestHandlers = {
context.chatAbortControllers.set(clientRunId, {
controller: abortController,
sessionId: entry?.sessionId ?? clientRunId,
sessionKey: p.sessionKey,
sessionKey: rawSessionKey,
startedAtMs: now,
expiresAtMs: resolveChatRunExpiresAtMs({ now, timeoutMs }),
});
@ -459,7 +460,7 @@ export const chatHandlers: GatewayRequestHandlers = {
BodyForCommands: commandBody,
RawBody: parsedMessage,
CommandBody: commandBody,
SessionKey: p.sessionKey,
SessionKey: sessionKey,
Provider: INTERNAL_MESSAGE_CHANNEL,
Surface: INTERNAL_MESSAGE_CHANNEL,
OriginatingChannel: INTERNAL_MESSAGE_CHANNEL,
@ -473,7 +474,7 @@ export const chatHandlers: GatewayRequestHandlers = {
};
const agentId = resolveSessionAgentId({
sessionKey: p.sessionKey,
sessionKey,
config: cfg,
});
const { onModelSelected, ...prefixOptions } = createReplyPrefixOptions({
@ -532,9 +533,8 @@ export const chatHandlers: GatewayRequestHandlers = {
.trim();
let message: Record<string, unknown> | undefined;
if (combinedReply) {
const { storePath: latestStorePath, entry: latestEntry } = loadSessionEntry(
p.sessionKey,
);
const { storePath: latestStorePath, entry: latestEntry } =
loadSessionEntry(sessionKey);
const sessionId = latestEntry?.sessionId ?? entry?.sessionId ?? clientRunId;
const appended = appendAssistantTranscriptMessage({
message: combinedReply,
@ -562,7 +562,7 @@ export const chatHandlers: GatewayRequestHandlers = {
broadcastChatFinal({
context,
runId: clientRunId,
sessionKey: p.sessionKey,
sessionKey: rawSessionKey,
message,
});
}
@ -587,7 +587,7 @@ export const chatHandlers: GatewayRequestHandlers = {
broadcastChatError({
context,
runId: clientRunId,
sessionKey: p.sessionKey,
sessionKey: rawSessionKey,
errorMessage: String(err),
});
})
@ -632,7 +632,8 @@ export const chatHandlers: GatewayRequestHandlers = {
};
// Load session to find transcript file
const { storePath, entry } = loadSessionEntry(p.sessionKey);
const rawSessionKey = p.sessionKey;
const { storePath, entry } = loadSessionEntry(rawSessionKey);
const sessionId = entry?.sessionId;
if (!sessionId || !storePath) {
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "session not found"));
@ -687,13 +688,13 @@ export const chatHandlers: GatewayRequestHandlers = {
// Broadcast to webchat for immediate UI update
const chatPayload = {
runId: `inject-${messageId}`,
sessionKey: p.sessionKey,
sessionKey: rawSessionKey,
seq: 0,
state: "final" as const,
message: transcriptEntry.message,
};
context.broadcast("chat", chatPayload);
context.nodeSendToSession(p.sessionKey, "chat", chatPayload);
context.nodeSendToSession(rawSessionKey, "chat", chatPayload);
respond(true, { ok: true, messageId });
},