🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
✨ feat: create new topic every 4 hours (#13570)
Rdmclin23cb7206d906a
diff --git a/src/server/services/bot/AgentBridgeService.ts b/src/server/services/bot/AgentBridgeService.tsindex 23f66875bf5..25fe144f486 100644--- a/src/server/services/bot/AgentBridgeService.ts+++ b/src/server/services/bot/AgentBridgeService.ts@@ -30,6 +30,10 @@ const log = debug('lobe-server:bot:agent-bridge');const EXECUTION_TIMEOUT = 30 * 60 * 1000; // 30 minutes+// If the last activity in a bot topic is older than this threshold,+// create a new topic instead of continuing in the stale one.+const TOPIC_STALE_THRESHOLD = 4 * 60 * 60 * 1000; // 4 hours+// PostgreSQL error code for foreign key constraint violations.// See: https://www.postgresql.org/docs/current/errcodes-appendix.htmlconst PG_FOREIGN_KEY_VIOLATION = '23503';@@ -340,7 +344,11 @@ export class AgentBridgeService {return this.handleMention(thread, message, opts);}- // Skip if there's already an active execution for this thread+ // Skip if there's already an active execution for this thread.+ // This must run before the stale-topic check to prevent a race where+ // a concurrent message clears topicId (stale reset) and then no-ops+ // in handleMention because the thread is active — dropping the message+ // but leaving state cleared so the next message starts a fresh topic.if (AgentBridgeService.activeThreads.has(thread.id)) {log('handleSubscribedMessage: skipping, thread=%s already has an active execution',@@ -349,6 +357,33 @@ export class AgentBridgeService {return;}+ // Check if the topic is stale (no activity for 4+ hours).+ // If so, clear the cached topicId and start a fresh conversation.+ // Wrapped in try/catch so transient DB errors fall through to the+ // existing topicId rather than rejecting before the guarded section.+ try {+ const topicModel = new TopicModel(this.db, this.userId);+ const existingTopic = await topicModel.findById(topicId);+ if (existingTopic) {+ const elapsed = Date.now() - new Date(existingTopic.updatedAt).getTime();+ if (elapsed > TOPIC_STALE_THRESHOLD) {+ log(+ 'handleSubscribedMessage: topic=%s is stale (%.1fh since last activity), creating new topic',+ topicId,+ elapsed / (60 * 60 * 1000),+ );+ await thread.setState({ ...threadState, topicId: undefined });+ return this.handleMention(thread, message, opts);+ }+ }+ } catch (error) {+ log(+ 'handleSubscribedMessage: stale-topic lookup failed, continuing with existing topicId=%s: %O',+ topicId,+ error,+ );+ }+AgentBridgeService.activeThreads.add(thread.id);// Read cached channel context from thread statediff --git a/src/server/services/bot/__tests__/AgentBridgeService.test.ts b/src/server/services/bot/__tests__/AgentBridgeService.test.tsindex 659844a6f35..fa1cc2713d0 100644--- a/src/server/services/bot/__tests__/AgentBridgeService.test.ts+++ b/src/server/services/bot/__tests__/AgentBridgeService.test.ts@@ -7,7 +7,9 @@ const mockGetPlatform = vi.hoisted(() => vi.fn());const mockIsQueueAgentRuntimeEnabled = vi.hoisted(() => vi.fn());vi.mock('@/database/models/topic', () => ({- TopicModel: vi.fn(),+ TopicModel: vi.fn().mockImplementation(() => ({+ findById: vi.fn().mockResolvedValue(undefined),+ })),}));vi.mock('@/database/models/user', () => ({