發佈 0.1.7:新增會話模式開關(獨立/共用)

摘要:
bot 設定新增「會話模式」:預設各聊天室獨立會話;可切為所有聊天室
共用一條記憶(單一專案助理情境)。共用模式下 /new 清除全部記憶,
/status 會顯示目前模式,bot 列表小字標示「共用會話」。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-10 12:05:30 +08:00
co-authored by Claude Fable 5
parent 6e39a8084f
commit d5c4464fe1
5 changed files with 39 additions and 11 deletions
+19 -7
View File
@@ -136,14 +136,14 @@ function startBot(config) {
}
function statusText(chatId) {
const s = sessions.get(chatId);
const s = sessions.get(sessionKeyOf(chatId));
return [
'📋 目前狀態',
`工作目錄:${config.workDir}`,
`沙盒模式:${config.sandbox}`,
`模型:${config.model || 'codex 預設)'}`,
`推理強度:${config.reasoningEffort || '(預設)'}`,
`速度${config.serviceTier || '(預設)'}`,
`會話模式${config.sharedSession ? '所有聊天室共用' : '各聊天室獨立'}`,
s ? `會話:${s.threadId}\n最後使用:${s.updatedAt}` : '會話:尚未建立(下一則訊息會開新會話)',
].join('\n');
}
@@ -159,9 +159,15 @@ function startBot(config) {
'群組中要 @我 或回覆我的訊息才會觸發。可以直接傳圖片(附文字說明)。',
].join('\n');
// 會話 key:獨立模式用 chat id,共用模式所有聊天室用同一把
function sessionKeyOf(chatId) {
return config.sharedSession ? 'shared' : chatId;
}
async function handleMessage(message) {
if (!message || message.from?.id === botId) return;
const chatId = message.chat.id;
const sessionKey = sessionKeyOf(chatId);
const text = message.text || message.caption || '';
const photo = pickPhoto(message);
if (!text && !photo) return;
@@ -174,8 +180,14 @@ function startBot(config) {
return;
}
if (matchCommand(content, NEW_COMMANDS)) {
sessions.clear(chatId);
await t.sendMessage(chatId, '🆕 已開新會話,之前的對話記憶已清除。', { replyTo: message.message_id });
sessions.clear(sessionKey);
await t.sendMessage(
chatId,
config.sharedSession
? '🆕 已開新會話(此 bot 為共用會話模式,所有聊天室的記憶一併清除)。'
: '🆕 已開新會話,之前的對話記憶已清除。',
{ replyTo: message.message_id }
);
return;
}
if (matchCommand(content, STATUS_COMMANDS)) {
@@ -216,7 +228,7 @@ function startBot(config) {
}
const prompt = buildPrompt(message, content || '(使用者只傳了圖片,請描述並依上下文處理)');
const existing = sessions.get(chatId);
const existing = sessions.get(sessionKey);
let result;
let sessionResetNote = '';
@@ -229,7 +241,7 @@ function startBot(config) {
const sessionGone = /session|thread|conversation/i.test(err.message) && /not.*found|找不到|no .*(session|thread)/i.test(err.message);
const modelMismatch = /recorded with model/i.test(err.message);
if (existing && (sessionGone || modelMismatch)) {
sessions.clear(chatId);
sessions.clear(sessionKey);
result = await runCodex({ config, prompt, threadId: null, images, onProgress });
sessionResetNote = modelMismatch
? '🆕 模型設定已變更,舊會話無法沿用,已自動開新會話(先前的對話記憶未帶入)。\n\n'
@@ -239,7 +251,7 @@ function startBot(config) {
}
}
if (result.threadId) sessions.set(chatId, result.threadId);
if (result.threadId) sessions.set(sessionKey, result.threadId);
const footer = buildFooter(config, result.usage, modelMeta, codexDefaults);
const html = t.mdToTgHtml(sessionResetNote + (result.text || 'Codex 沒有回覆文字)') + footer);