Discord: isolate autoThread thread context (#856)

This commit is contained in:
Shadow
2026-01-14 12:25:20 -06:00
parent 0adcb68092
commit b4ba6e4eaf
2 changed files with 38 additions and 3 deletions

View File

@@ -132,7 +132,10 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
body: text,
});
let shouldClearHistory = false;
if (!isDirectMessage) {
const shouldIncludeChannelHistory =
!isDirectMessage &&
!(isGuildMessage && channelConfig?.autoThread && !threadChannel);
if (shouldIncludeChannelHistory) {
combinedBody = buildHistoryContextFromMap({
historyMap: guildHistories,
historyKey: message.channelId,
@@ -147,10 +150,12 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
body: `${entry.sender}: ${entry.body} [id:${entry.messageId ?? "unknown"} channel:${message.channelId}]`,
}),
});
shouldClearHistory = true;
}
if (!isDirectMessage) {
const name = formatDiscordUserTag(author);
const id = author.id;
combinedBody = `${combinedBody}\n[from: ${name} user id:${id}]`;
shouldClearHistory = true;
}
const replyContext = resolveReplyContext(message, resolveDiscordMessageText);
if (replyContext) {
@@ -197,7 +202,7 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
parentSessionKey,
useSuffix: false,
});
const ctxPayload = {
let ctxPayload = {
Body: combinedBody,
RawBody: baseText,
CommandBody: baseText,
@@ -254,6 +259,35 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
replyTarget = replyPlan.replyTarget;
const replyReference = replyPlan.replyReference;
// If autoThread created a new thread, ensure we also isolate session context to that thread.
if (createdThreadId && replyTarget === `channel:${createdThreadId}`) {
const threadSessionKey = buildAgentSessionKey({
agentId: route.agentId,
channel: route.channel,
peer: { kind: "channel", id: createdThreadId },
});
const autoParentSessionKey = buildAgentSessionKey({
agentId: route.agentId,
channel: route.channel,
peer: { kind: "channel", id: message.channelId },
});
const autoThreadKeys = resolveThreadSessionKeys({
baseSessionKey: threadSessionKey,
threadId: createdThreadId,
parentSessionKey: autoParentSessionKey,
useSuffix: false,
});
ctxPayload = {
...ctxPayload,
From: `group:${createdThreadId}`,
To: `channel:${createdThreadId}`,
OriginatingTo: `channel:${createdThreadId}`,
SessionKey: autoThreadKeys.sessionKey,
ParentSessionKey: autoThreadKeys.parentSessionKey,
};
}
if (isDirectMessage) {
const sessionCfg = cfg.session;
const storePath = resolveStorePath(sessionCfg?.store, {