app.delete('/api/participants', requireAuth, (req, res) => {
    try {
      const { id, chatId, agentId } = req.body || {};
      if ((!id && !agentId) || !chatId) return res.status(400).json({ error: 'id 和 chatId 必填' });
      db.removeParticipant(chatId, id || '', agentId);
      // 如果群聊没参与者了，清理协作模式缓存
      const remaining = db.getChatParticipants(chatId);
      if (!remaining || remaining.length === 0) {
        (globalThis as any).__chatModes?.delete(chatId);
        (globalThis as any).__roundRobinIdx?.delete(chatId);
      }
      res.json({ ok: true });
    } catch (err: any) { res.status(500).json({ error: err.message }); }
  });

  // ── 删除整个会话（含消息+参与者） ──
  app.delete('/api/chats/:id', requireAuth, (req, res) => {
    try {
      const chatId = req.params.id;
      const d = getDB();
      d.run('DELETE FROM messages WHERE chat