fix: persist topic session files

This commit is contained in:
Peter Steinberger
2026-01-07 22:56:50 +00:00
parent 67d1f61872
commit b2de667b11
6 changed files with 84 additions and 37 deletions

View File

@@ -722,9 +722,7 @@ export async function getReplyFromConfig(
resolvedThinkLevel = await modelState.resolveDefaultThinkingLevel();
}
const sessionIdFinal = sessionId ?? crypto.randomUUID();
const sessionFile = resolveSessionFilePath(sessionIdFinal, sessionEntry, {
topicId: ctx.MessageThreadId,
});
const sessionFile = resolveSessionFilePath(sessionIdFinal, sessionEntry);
const queueBodyBase = transcribedText
? [threadStarterNote, baseBodyFinal, `Transcript:\n${transcribedText}`]
.filter(Boolean)

View File

@@ -82,4 +82,31 @@ describe("initSessionState thread forking", () => {
};
expect(parsedHeader.parentSession).toBe(parentSessionFile);
});
it("records topic-specific session files when MessageThreadId is present", async () => {
const root = await fs.mkdtemp(
path.join(os.tmpdir(), "clawdbot-topic-session-"),
);
const storePath = path.join(root, "sessions.json");
const cfg = {
session: { store: storePath },
} as ClawdbotConfig;
const result = await initSessionState({
ctx: {
Body: "Hello topic",
SessionKey: "agent:main:telegram:group:123:topic:456",
MessageThreadId: 456,
},
cfg,
commandAuthorized: true,
});
const sessionFile = result.sessionEntry.sessionFile;
expect(sessionFile).toBeTruthy();
expect(path.basename(sessionFile ?? "")).toBe(
`${result.sessionEntry.sessionId}-topic-456.jsonl`,
);
});
});

View File

@@ -17,6 +17,7 @@ import {
resolveGroupSessionKey,
resolveSessionFilePath,
resolveSessionKey,
resolveSessionTranscriptPath,
resolveStorePath,
type SessionEntry,
type SessionScope,
@@ -255,6 +256,13 @@ export async function initSessionState(params: {
sessionEntry.sessionFile = forked.sessionFile;
}
}
if (!sessionEntry.sessionFile) {
sessionEntry.sessionFile = resolveSessionTranscriptPath(
sessionEntry.sessionId,
agentId,
ctx.MessageThreadId,
);
}
sessionStore[sessionKey] = sessionEntry;
await saveSessionStore(storePath, sessionStore);