- /rag/commands/expert.py - /core/services/__init__.py - /core/services/embedding_service.py - /core/services/file_service.py - /core/services/giga_client.py - /core/services/kb_service.py - /core/services/postgres_service.py - /core/services/qdrant_service.py - /core/services/reranker_service.py - /core/functions/__init__.py - /core/functions/check_consistency.py - /core/functions/check_spelling.py - /core/functions/critique_answer.py - /core/functions/expand_query.py - /core/functions/extract_metrics.py - /core/functions/file_processor.py - /core/functions/generate_document.py - /core/functions/intent_classify.py - /core/functions/rerank_context.py - /core/functions/summarize_document.py - /core/utils/__init__.py - /core/utils/arg_parser.py - /core/utils/config_loader.py - /core/utils/layout_converter.py - /core/utils/logger.py - /core/utils/text_utils.py - /core/utils/web_utils.py - /bots/commands/__init__.py - /bots/commands/base.py - /bots/commands/create.py - /core/commands/global_remove.py - /core/commands/help.py - /core/commands/info.py - /core/commands/kb.py - /core/commands/learn.py - /core/commands/other.py - /core/commands/registry.py - /core/commands/stats.py - /core/commands/template.py - /core/handlers/__init__.py - /core/handlers/message_handler.py - /core/workers/__init__.py - /core/workers/indexing_worker.py - /core/xmpp/__init__.py - /core/xmpp/client.py - /bots/commands/global_remove.py - /bots/commands/help.py - /bots/commands/info.py - /bots/commands/kb.py - /bots/commands/registry.py - /bots/commands/other.py - /bots/commands/template.py - /bots/commands/learn.py - /bots/commands/stats.py - /bots/handlers/message_handler.py - /bots/handlers/__init__.py - /bots/workers/__init__.py - /bots/workers/indexing_worker.py - /bots/xmpp/__init__.py - /bots/xmpp/client.py - /rag/services/qdrant_service.py - /rag/services/giga_client.py - /rag/services/embedding_service.py - /rag/services/reranker_service.py - /rag/services/__init__.py - /rag/services/file_service.py - /rag/services/postgres_service.py - /rag/services/kb_service.py - /rag/functions/check_spelling.py - /rag/functions/__init__.py - /rag/functions/extract_metrics.py - /rag/functions/generate_document.py - /rag/functions/expand_query.py - /rag/functions/critique_answer.py - /rag/functions/file_processor.py - /rag/functions/check_consistency.py - /rag/functions/summarize_document.py - /rag/functions/rerank_context.py - /rag/functions/intent_classify.py - /rag/utils/__init__.py - /rag/utils/layout_converter.py - /rag/utils/text_utils.py - /rag/utils/arg_parser.py - /rag/utils/config_loader.py - /rag/utils/logger.py - /rag/utils/web_utils.py
131 lines
6.4 KiB
Python
131 lines
6.4 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
Команды управления базой знаний: !clear, !global_clear, !kb.
|
||
Поддержка комнат:
|
||
- !clear в комнате очищает комнатную БЗ (требует прав администратора бота).
|
||
- !kb в комнате показывает документы комнаты + глобальные.
|
||
"""
|
||
|
||
import logging
|
||
from core.commands.base import Command
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
class ClearCommand(Command):
|
||
name = "!clear"
|
||
aliases = ["/clear"]
|
||
|
||
async def execute(self, msg, args: str, user_jid: str, room_jid: str = None):
|
||
"""
|
||
Очищает базу знаний:
|
||
- В личном чате – личную БЗ пользователя.
|
||
- В групповом чате – БЗ комнаты (требует прав администратора бота).
|
||
"""
|
||
if room_jid:
|
||
# Для очистки комнатной БЗ нужны права администратора бота
|
||
if user_jid not in self.bot.config.admin_jids:
|
||
reply = msg.reply("⛔ Только администраторы бота могут очищать комнатную БЗ.")
|
||
if reply:
|
||
reply.send() # исправлено
|
||
else:
|
||
logger.error("Не удалось отправить сообщение о правах на очистку комнаты")
|
||
return
|
||
await self.bot.kb.clear_room_kb(room_jid)
|
||
reply = msg.reply(f"🗑 Комнатная БЗ {room_jid} очищена.")
|
||
if reply:
|
||
reply.send() # исправлено
|
||
else:
|
||
logger.error("Не удалось отправить подтверждение очистки комнатной БЗ")
|
||
else:
|
||
await self.bot.kb.clear_user_kb(user_jid)
|
||
reply = msg.reply("🗑 Ваша ЛИЧНАЯ база знаний очищена.")
|
||
if reply:
|
||
reply.send() # исправлено
|
||
else:
|
||
logger.error("Не удалось отправить подтверждение очистки личной БЗ")
|
||
|
||
def get_help(self) -> str:
|
||
return "Очищает вашу личную базу знаний. В групповом чате очищает БЗ комнаты (требует прав администратора бота)."
|
||
|
||
|
||
class GlobalClearCommand(Command):
|
||
name = "!global_clear"
|
||
aliases = ["/global_clear"]
|
||
permission = "admin"
|
||
|
||
async def execute(self, msg, args: str, user_jid: str, room_jid: str = None):
|
||
"""
|
||
Очищает глобальную базу знаний (только админ, только в личном чате).
|
||
В комнате команда не работает.
|
||
"""
|
||
if not self.check_permission(user_jid):
|
||
reply = msg.reply("⛔ Нет прав.")
|
||
if reply:
|
||
reply.send() # исправлено
|
||
else:
|
||
logger.error("Не удалось отправить сообщение о недостатке прав (kb)")
|
||
return
|
||
if room_jid:
|
||
reply = msg.reply("⚠️ Команда !global_clear работает только в личном чате.")
|
||
if reply:
|
||
reply.send() # исправлено
|
||
else:
|
||
logger.error("Не удалось отправить сообщение об ограничении !global_clear")
|
||
return
|
||
await self.bot.kb.clear_global_kb()
|
||
reply = msg.reply("🌐 ГЛОБАЛЬНАЯ база знаний очищена.")
|
||
if reply:
|
||
reply.send() # исправлено
|
||
else:
|
||
logger.error("Не удалось отправить подтверждение очистки глобальной БЗ")
|
||
|
||
def get_help(self) -> str:
|
||
return "Очищает глобальную базу знаний (админ, только в личном чате)."
|
||
|
||
|
||
class KbCommand(Command):
|
||
name = "!kb"
|
||
aliases = ["/kb"]
|
||
|
||
async def execute(self, msg, args: str, user_jid: str, room_jid: str = None):
|
||
"""
|
||
Показывает список документов в базе знаний:
|
||
- В личном чате: личные + глобальные.
|
||
- В групповом чате: документы комнаты + глобальные.
|
||
"""
|
||
sources = await self.bot.kb.get_sources_with_type(user_jid, room_jid)
|
||
if not sources:
|
||
if room_jid:
|
||
reply = msg.reply("📭 База знаний этой комнаты пуста.")
|
||
if reply:
|
||
reply.send() # исправлено
|
||
else:
|
||
logger.error("Не удалось отправить сообщение о пустой комнатной БЗ")
|
||
else:
|
||
reply = msg.reply("📭 Ваша личная база знаний пуста.")
|
||
if reply:
|
||
reply.send() # исправлено
|
||
else:
|
||
logger.error("Не удалось отправить сообщение о пустой личной БЗ")
|
||
return
|
||
global_docs = [s['source_name'] for s in sources if s['is_global']]
|
||
other_docs = [s['source_name'] for s in sources if not s['is_global']]
|
||
lines = []
|
||
if global_docs:
|
||
lines.append("🌍 **Глобальные документы:**")
|
||
lines.extend(f" • {name}" for name in global_docs)
|
||
if other_docs:
|
||
if room_jid:
|
||
lines.append(f"🏠 **Документы комнаты {room_jid}:**")
|
||
else:
|
||
lines.append("👤 **Ваши личные документы:**")
|
||
lines.extend(f" • {name}" for name in other_docs)
|
||
reply = msg.reply("\n".join(lines))
|
||
if reply:
|
||
reply.send() # исправлено
|
||
else:
|
||
logger.error("Не удалось отправить список документов БЗ")
|
||
|
||
def get_help(self) -> str:
|
||
return "Показывает список документов в базе знаний (личные/комнатные + глобальные)." |