Skip to content

🤯 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.

🐛 fix: message gateway ensure running (#13780)

Rdmclin2809e1e0716db
diff --git a/src/app/(backend)/api/agent/gateway/route.ts b/src/app/(backend)/api/agent/gateway/route.ts
index 76ddeb8d910..e56c9fdc6b7 100644
--- a/src/app/(backend)/api/agent/gateway/route.ts
+++ b/src/app/(backend)/api/agent/gateway/route.ts
@@ -123,11 +123,13 @@ export async function GET(request: NextRequest) {
return new Response('Unauthorized', { status: 401 });
}
- // When an external message gateway is fully configured (both URL and service
- // token), it manages all persistent connections. Skip in-process bot startup
- // to avoid duplicate connections.
+ // When an external message gateway is configured, use ensureRunning to sync
+ // connections via the gateway instead of starting bots in-process.
if (process.env.MESSAGE_GATEWAY_URL && process.env.MESSAGE_GATEWAY_SERVICE_TOKEN) {
- return Response.json({ skipped: true, reason: 'using external message gateway' });
+ const { GatewayService } = await import('@/server/services/gateway');
+ const service = new GatewayService();
+ await service.ensureRunning();
+ return Response.json({ ensureRunning: true, reason: 'synced via external message gateway' });
}
const platforms = platformRegistry.listPlatforms();
diff --git a/src/instrumentation.ts b/src/instrumentation.ts
index 5b91d47581d..cc5b3d2196b 100644
--- a/src/instrumentation.ts
+++ b/src/instrumentation.ts
@@ -4,13 +4,13 @@ export async function register() {
await import('./libs/debug-file-logger');
}
- // Auto-start GatewayManager / sync message-gateway connections on server start.
- // - Non-Vercel (Docker, local): always run — persistent bots need reconnection after restart.
- // - Vercel: only run when an external message gateway is configured, to sync connection state.
+ // Auto-start GatewayManager on server start for non-Vercel environments (Docker, local).
+ // Persistent bots need reconnection after restart.
+ // On Vercel, the cron job at /api/agent/gateway handles this reliably instead.
if (
process.env.NEXT_RUNTIME === 'nodejs' &&
process.env.DATABASE_URL &&
- (!process.env.VERCEL_ENV || process.env.MESSAGE_GATEWAY_URL)
+ !process.env.VERCEL_ENV
) {
const { GatewayService } = await import('./server/services/gateway');
const service = new GatewayService();