Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/feishu/event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,8 @@ export async function executeClaudeTask(
isRestart: true,
priorContext: formatConversationTrace(result.conversationTrace),
agentId,
threadId,
threadRootMessageId: threadReplyMsgId,
...(customSystemPrompt ? { systemPromptOverride: customSystemPrompt } : {}),
});

Expand Down
27 changes: 21 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,24 @@ async function main(): Promise<void> {
const data = JSON.parse(raw) as { sessions?: Array<{ chatId: string }>; timestamp?: number };

if (data.sessions?.length) {
const uniqueChatIds = [...new Set(data.sessions.map((s) => s.chatId))];
logger.info({ chatIds: uniqueChatIds, interruptedAt: data.timestamp }, 'Notifying interrupted sessions after restart');
for (const chatId of uniqueChatIds) {
feishuClient.sendText(chatId, '服务已重启完成,之前正在执行的任务被中断。如需继续,请重新发送消息。').catch((err) => {
logger.warn({ err, chatId }, 'Failed to notify interrupted session');
// 按 chatId + threadRootMessageId 去重(同一话题只通知一次)
const seen = new Set<string>();
const uniqueSessions: Array<{ chatId: string; threadRootMessageId?: string }> = [];
for (const s of data.sessions as Array<{ chatId: string; threadRootMessageId?: string }>) {
const dedup = s.threadRootMessageId ?? s.chatId;
if (!seen.has(dedup)) {
seen.add(dedup);
uniqueSessions.push(s);
}
}
logger.info({ count: uniqueSessions.length, interruptedAt: data.timestamp }, 'Notifying interrupted sessions after restart');
const notifyText = '服务已重启完成,之前正在执行的任务被中断。如需继续,请重新发送消息。';
for (const { chatId, threadRootMessageId } of uniqueSessions) {
const p = threadRootMessageId
? feishuClient.replyTextInThread(threadRootMessageId, notifyText)
: feishuClient.sendText(chatId, notifyText);
p.catch((err) => {
logger.warn({ err, chatId, threadRootMessageId }, 'Failed to notify interrupted session');
});
}
}
Expand Down Expand Up @@ -290,7 +303,9 @@ async function main(): Promise<void> {
const parts = key.split(':');
// 新格式 "agent:{agentId}:{chatId}:{userId}" 或旧格式 "chatId:userId"
const chatId = parts.length >= 4 ? parts[2] : parts[0];
return { chatId, sessionKey: key };
// 从 session 中获取 threadRootMessageId,重启后通知发到话题内而非主聊天
const session = sessionManager.getByKey(key);
return { chatId, sessionKey: key, threadRootMessageId: session?.threadRootMessageId };
}).filter((s) => s.chatId);

try {
Expand Down
7 changes: 7 additions & 0 deletions src/session/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ export class SessionManager {
return this.db.get(this.makeKey(chatId, userId, agentId));
}

/**
* 通过原始 key 获取会话(用于 shutdown 保存中断 session 时直接查询)
*/
getByKey(key: string): Readonly<Session> | undefined {
return this.db.get(key);
}

/**
* 更新会话工作目录
*/
Expand Down
Loading