From 0ed38cc98ac8c57a4c01193ea63cfd3fec8e8339 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 18 May 2026 21:08:31 +0000 Subject: [PATCH 1/2] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/psf/black-pre-commit-mirror: 26.3.1 → 26.5.0](https://github.com/psf/black-pre-commit-mirror/compare/26.3.1...26.5.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fd1f1d9..656331a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,7 +10,7 @@ repos: - id: flake8 args: [--select=E9] - repo: https://github.com/psf/black-pre-commit-mirror - rev: 26.3.1 + rev: 26.5.0 hooks: - id: black args: [] \ No newline at end of file From ce5ecaf09ce1d7d504edc26968af179801d204cc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 18 May 2026 21:08:42 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- migrations/versions/95cb99f34471_.py | 228 ++++++++++-------- src/plugins/nonebot_plugin_chat/config.py | 1 + .../nonebot_plugin_chat/core/matchers.py | 1 - .../nonebot_plugin_chat/core/message.py | 11 +- .../nonebot_plugin_chat/core/processor.py | 8 +- .../nonebot_plugin_chat/core/session/base.py | 3 - .../nonebot_plugin_chat/core/session/group.py | 2 +- .../core/session/private.py | 1 - .../nonebot_plugin_chat/matcher/new.py | 2 +- .../nonebot_plugin_chat/matcher/retry.py | 3 +- src/plugins/nonebot_plugin_chat/models.py | 5 +- src/plugins/nonebot_plugin_chat/startup.py | 3 - src/plugins/nonebot_plugin_chat/types.py | 4 - .../nonebot_plugin_chat/utils/__init__.py | 2 +- .../utils/config_manager.py | 9 +- .../nonebot_plugin_chat/utils/skill.py | 12 +- .../nonebot_plugin_chat/utils/tool_manager.py | 7 +- .../nonebot_plugin_chat/utils/tools/exec.py | 13 +- .../nonebot_plugin_chat/utils/tools/file.py | 2 +- .../utils/tools/interactive_exec.py | 7 +- 20 files changed, 151 insertions(+), 173 deletions(-) diff --git a/migrations/versions/95cb99f34471_.py b/migrations/versions/95cb99f34471_.py index 5d8f2d2..412c96b 100644 --- a/migrations/versions/95cb99f34471_.py +++ b/migrations/versions/95cb99f34471_.py @@ -1,10 +1,11 @@ """empty message 迁移 ID: 95cb99f34471 -父迁移: +父迁移: 创建时间: 2026-03-28 23:36:34.366445 """ + from __future__ import annotations from collections.abc import Sequence @@ -13,7 +14,7 @@ import sqlalchemy as sa from sqlalchemy.dialects import mysql -revision: str = '95cb99f34471' +revision: str = "95cb99f34471" down_revision: str | Sequence[str] | None = None branch_labels: str | Sequence[str] | None = None depends_on: str | Sequence[str] | None = None @@ -23,109 +24,122 @@ def upgrade(name: str = "") -> None: if name: return # ### commands auto generated by Alembic - please adjust! ### - op.create_table('nonebot_plugin_access_subjectdata', - sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), - sa.Column('subject', sa.String(length=256), nullable=False), - sa.Column('name', sa.String(length=128), nullable=False), - sa.Column('available', sa.Boolean(), nullable=False), - sa.PrimaryKeyConstraint('id', name=op.f('pk_nonebot_plugin_access_subjectdata')), - info={'bind_key': 'nonebot_plugin_access'} + op.create_table( + "nonebot_plugin_access_subjectdata", + sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), + sa.Column("subject", sa.String(length=256), nullable=False), + sa.Column("name", sa.String(length=128), nullable=False), + sa.Column("available", sa.Boolean(), nullable=False), + sa.PrimaryKeyConstraint("id", name=op.f("pk_nonebot_plugin_access_subjectdata")), + info={"bind_key": "nonebot_plugin_access"}, ) - op.create_table('nonebot_plugin_bots_user_private_chat_settings', - sa.Column('user_id', sa.String(length=128), nullable=False), - sa.Column('bot_id', sa.String(length=128), nullable=False), - sa.Column('private_chat_enabled', sa.Boolean(), nullable=False), - sa.PrimaryKeyConstraint('user_id', 'bot_id', name=op.f('pk_nonebot_plugin_bots_user_private_chat_settings')), - info={'bind_key': 'nonebot_plugin_bots'} + op.create_table( + "nonebot_plugin_bots_user_private_chat_settings", + sa.Column("user_id", sa.String(length=128), nullable=False), + sa.Column("bot_id", sa.String(length=128), nullable=False), + sa.Column("private_chat_enabled", sa.Boolean(), nullable=False), + sa.PrimaryKeyConstraint("user_id", "bot_id", name=op.f("pk_nonebot_plugin_bots_user_private_chat_settings")), + info={"bind_key": "nonebot_plugin_bots"}, ) - op.create_table('nonebot_plugin_chat_messagequeuecache', - sa.Column('group_id', sa.String(length=128), nullable=False), - sa.Column('messages_json', sa.Text().with_variant(mysql.MEDIUMTEXT(), 'mysql'), nullable=False), - sa.Column('updated_time', sa.Float(), nullable=False), - sa.PrimaryKeyConstraint('group_id', name=op.f('pk_nonebot_plugin_chat_messagequeuecache')), - info={'bind_key': 'nonebot_plugin_chat'} + op.create_table( + "nonebot_plugin_chat_messagequeuecache", + sa.Column("group_id", sa.String(length=128), nullable=False), + sa.Column("messages_json", sa.Text().with_variant(mysql.MEDIUMTEXT(), "mysql"), nullable=False), + sa.Column("updated_time", sa.Float(), nullable=False), + sa.PrimaryKeyConstraint("group_id", name=op.f("pk_nonebot_plugin_chat_messagequeuecache")), + info={"bind_key": "nonebot_plugin_chat"}, ) - op.create_table('nonebot_plugin_chat_moonclawconfig', - sa.Column('key', sa.String(length=64), nullable=False), - sa.Column('data_json', sa.Text(), nullable=False), - sa.Column('updated_time', sa.DateTime(), nullable=False), - sa.PrimaryKeyConstraint('key', name=op.f('pk_nonebot_plugin_chat_moonclawconfig')), - info={'bind_key': 'nonebot_plugin_chat'} + op.create_table( + "nonebot_plugin_chat_moonclawconfig", + sa.Column("key", sa.String(length=64), nullable=False), + sa.Column("data_json", sa.Text(), nullable=False), + sa.Column("updated_time", sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint("key", name=op.f("pk_nonebot_plugin_chat_moonclawconfig")), + info={"bind_key": "nonebot_plugin_chat"}, ) - op.create_table('nonebot_plugin_chat_note', - sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), - sa.Column('context_id', sa.String(length=128), nullable=False), - sa.Column('content', sa.Text(), nullable=False), - sa.Column('keywords', sa.String(length=256), nullable=False), - sa.Column('created_time', sa.Float(), nullable=False), - sa.Column('expire_time', sa.DateTime(), nullable=True), - sa.PrimaryKeyConstraint('id', name=op.f('pk_nonebot_plugin_chat_note')), - info={'bind_key': 'nonebot_plugin_chat'} + op.create_table( + "nonebot_plugin_chat_note", + sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), + sa.Column("context_id", sa.String(length=128), nullable=False), + sa.Column("content", sa.Text(), nullable=False), + sa.Column("keywords", sa.String(length=256), nullable=False), + sa.Column("created_time", sa.Float(), nullable=False), + sa.Column("expire_time", sa.DateTime(), nullable=True), + sa.PrimaryKeyConstraint("id", name=op.f("pk_nonebot_plugin_chat_note")), + info={"bind_key": "nonebot_plugin_chat"}, ) - with op.batch_alter_table('nonebot_plugin_chat_note', schema=None) as batch_op: - batch_op.create_index(batch_op.f('ix_nonebot_plugin_chat_note_context_id'), ['context_id'], unique=False) + with op.batch_alter_table("nonebot_plugin_chat_note", schema=None) as batch_op: + batch_op.create_index(batch_op.f("ix_nonebot_plugin_chat_note_context_id"), ["context_id"], unique=False) - op.create_table('nonebot_plugin_chat_privatechatsession', - sa.Column('user_id', sa.String(length=128), nullable=False), - sa.Column('bot_id', sa.String(length=128), nullable=False), - sa.Column('last_message_time', sa.Float(), nullable=False), - sa.Column('last_proactive_message_time', sa.Float(), nullable=True), - sa.PrimaryKeyConstraint('user_id', name=op.f('pk_nonebot_plugin_chat_privatechatsession')), - info={'bind_key': 'nonebot_plugin_chat'} + op.create_table( + "nonebot_plugin_chat_privatechatsession", + sa.Column("user_id", sa.String(length=128), nullable=False), + sa.Column("bot_id", sa.String(length=128), nullable=False), + sa.Column("last_message_time", sa.Float(), nullable=False), + sa.Column("last_proactive_message_time", sa.Float(), nullable=True), + sa.PrimaryKeyConstraint("user_id", name=op.f("pk_nonebot_plugin_chat_privatechatsession")), + info={"bind_key": "nonebot_plugin_chat"}, ) - op.create_table('nonebot_plugin_larklang_displaysetting', - sa.Column('user_id', sa.String(length=128), nullable=False), - sa.Column('language', sa.String(length=16), nullable=False), - sa.Column('theme', sa.String(length=16), nullable=False), - sa.PrimaryKeyConstraint('user_id', name=op.f('pk_nonebot_plugin_larklang_displaysetting')), - info={'bind_key': 'nonebot_plugin_larklang'} + op.create_table( + "nonebot_plugin_larklang_displaysetting", + sa.Column("user_id", sa.String(length=128), nullable=False), + sa.Column("language", sa.String(length=16), nullable=False), + sa.Column("theme", sa.String(length=16), nullable=False), + sa.PrimaryKeyConstraint("user_id", name=op.f("pk_nonebot_plugin_larklang_displaysetting")), + info={"bind_key": "nonebot_plugin_larklang"}, ) - op.create_table('nonebot_plugin_larklang_grouplanguagesetting', - sa.Column('group_id', sa.String(length=128), nullable=False), - sa.Column('language', sa.String(length=16), nullable=False), - sa.PrimaryKeyConstraint('group_id', name=op.f('pk_nonebot_plugin_larklang_grouplanguagesetting')), - info={'bind_key': 'nonebot_plugin_larklang'} + op.create_table( + "nonebot_plugin_larklang_grouplanguagesetting", + sa.Column("group_id", sa.String(length=128), nullable=False), + sa.Column("language", sa.String(length=16), nullable=False), + sa.PrimaryKeyConstraint("group_id", name=op.f("pk_nonebot_plugin_larklang_grouplanguagesetting")), + info={"bind_key": "nonebot_plugin_larklang"}, ) - op.create_table('nonebot_plugin_larklang_languagekeycache', - sa.Column('language', sa.String(length=16), nullable=False), - sa.Column('plugin', sa.String(length=32), nullable=False), - sa.Column('key', sa.String(length=64), nullable=False), - sa.Column('text', sa.Text(), nullable=False), - sa.PrimaryKeyConstraint('language', 'plugin', 'key', name=op.f('pk_nonebot_plugin_larklang_languagekeycache')), - info={'bind_key': 'nonebot_plugin_larklang'} + op.create_table( + "nonebot_plugin_larklang_languagekeycache", + sa.Column("language", sa.String(length=16), nullable=False), + sa.Column("plugin", sa.String(length=32), nullable=False), + sa.Column("key", sa.String(length=64), nullable=False), + sa.Column("text", sa.Text(), nullable=False), + sa.PrimaryKeyConstraint("language", "plugin", "key", name=op.f("pk_nonebot_plugin_larklang_languagekeycache")), + info={"bind_key": "nonebot_plugin_larklang"}, ) - op.create_table('nonebot_plugin_larkuser_guestuser', - sa.Column('user_id', sa.String(length=128), nullable=False), - sa.Column('nickname', sa.String(length=256), nullable=False), - sa.PrimaryKeyConstraint('user_id', name=op.f('pk_nonebot_plugin_larkuser_guestuser')), - info={'bind_key': 'nonebot_plugin_larkuser'} + op.create_table( + "nonebot_plugin_larkuser_guestuser", + sa.Column("user_id", sa.String(length=128), nullable=False), + sa.Column("nickname", sa.String(length=256), nullable=False), + sa.PrimaryKeyConstraint("user_id", name=op.f("pk_nonebot_plugin_larkuser_guestuser")), + info={"bind_key": "nonebot_plugin_larkuser"}, ) - op.create_table('nonebot_plugin_larkuser_userdata', - sa.Column('user_id', sa.String(length=128), nullable=False), - sa.Column('nickname', sa.String(length=256), nullable=False), - sa.Column('register_time', sa.DateTime(), nullable=False), - sa.Column('experience', sa.Integer(), nullable=False), - sa.Column('vimcoin', sa.Float(), nullable=False), - sa.Column('health', sa.Float(), nullable=False), - sa.Column('favorability', sa.Float(), nullable=False), - sa.Column('config', sa.Text(), nullable=False), - sa.PrimaryKeyConstraint('user_id', name=op.f('pk_nonebot_plugin_larkuser_userdata')), - info={'bind_key': 'nonebot_plugin_larkuser'} + op.create_table( + "nonebot_plugin_larkuser_userdata", + sa.Column("user_id", sa.String(length=128), nullable=False), + sa.Column("nickname", sa.String(length=256), nullable=False), + sa.Column("register_time", sa.DateTime(), nullable=False), + sa.Column("experience", sa.Integer(), nullable=False), + sa.Column("vimcoin", sa.Float(), nullable=False), + sa.Column("health", sa.Float(), nullable=False), + sa.Column("favorability", sa.Float(), nullable=False), + sa.Column("config", sa.Text(), nullable=False), + sa.PrimaryKeyConstraint("user_id", name=op.f("pk_nonebot_plugin_larkuser_userdata")), + info={"bind_key": "nonebot_plugin_larkuser"}, ) - op.create_table('nonebot_plugin_larkutils_luckvalue', - sa.Column('user_id', sa.String(length=128), nullable=False), - sa.Column('luck_value', sa.Integer(), nullable=False), - sa.Column('generate_date', sa.Date(), nullable=False), - sa.Column('reroll_count', sa.Integer(), nullable=False), - sa.PrimaryKeyConstraint('user_id', name=op.f('pk_nonebot_plugin_larkutils_luckvalue')), - info={'bind_key': 'nonebot_plugin_larkutils'} + op.create_table( + "nonebot_plugin_larkutils_luckvalue", + sa.Column("user_id", sa.String(length=128), nullable=False), + sa.Column("luck_value", sa.Integer(), nullable=False), + sa.Column("generate_date", sa.Date(), nullable=False), + sa.Column("reroll_count", sa.Integer(), nullable=False), + sa.PrimaryKeyConstraint("user_id", name=op.f("pk_nonebot_plugin_larkutils_luckvalue")), + info={"bind_key": "nonebot_plugin_larkutils"}, ) - op.create_table('nonebot_plugin_openai_openaimodelconfig', - sa.Column('config_key', sa.String(length=256), nullable=False), - sa.Column('model_name', sa.String(length=128), nullable=False), - sa.Column('config_type', sa.String(length=32), nullable=False), - sa.PrimaryKeyConstraint('config_key', name=op.f('pk_nonebot_plugin_openai_openaimodelconfig')), - info={'bind_key': 'nonebot_plugin_openai'} + op.create_table( + "nonebot_plugin_openai_openaimodelconfig", + sa.Column("config_key", sa.String(length=256), nullable=False), + sa.Column("model_name", sa.String(length=128), nullable=False), + sa.Column("config_type", sa.String(length=32), nullable=False), + sa.PrimaryKeyConstraint("config_key", name=op.f("pk_nonebot_plugin_openai_openaimodelconfig")), + info={"bind_key": "nonebot_plugin_openai"}, ) # ### end Alembic commands ### @@ -134,20 +148,20 @@ def downgrade(name: str = "") -> None: if name: return # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('nonebot_plugin_openai_openaimodelconfig') - op.drop_table('nonebot_plugin_larkutils_luckvalue') - op.drop_table('nonebot_plugin_larkuser_userdata') - op.drop_table('nonebot_plugin_larkuser_guestuser') - op.drop_table('nonebot_plugin_larklang_languagekeycache') - op.drop_table('nonebot_plugin_larklang_grouplanguagesetting') - op.drop_table('nonebot_plugin_larklang_displaysetting') - op.drop_table('nonebot_plugin_chat_privatechatsession') - with op.batch_alter_table('nonebot_plugin_chat_note', schema=None) as batch_op: - batch_op.drop_index(batch_op.f('ix_nonebot_plugin_chat_note_context_id')) + op.drop_table("nonebot_plugin_openai_openaimodelconfig") + op.drop_table("nonebot_plugin_larkutils_luckvalue") + op.drop_table("nonebot_plugin_larkuser_userdata") + op.drop_table("nonebot_plugin_larkuser_guestuser") + op.drop_table("nonebot_plugin_larklang_languagekeycache") + op.drop_table("nonebot_plugin_larklang_grouplanguagesetting") + op.drop_table("nonebot_plugin_larklang_displaysetting") + op.drop_table("nonebot_plugin_chat_privatechatsession") + with op.batch_alter_table("nonebot_plugin_chat_note", schema=None) as batch_op: + batch_op.drop_index(batch_op.f("ix_nonebot_plugin_chat_note_context_id")) - op.drop_table('nonebot_plugin_chat_note') - op.drop_table('nonebot_plugin_chat_moonclawconfig') - op.drop_table('nonebot_plugin_chat_messagequeuecache') - op.drop_table('nonebot_plugin_bots_user_private_chat_settings') - op.drop_table('nonebot_plugin_access_subjectdata') + op.drop_table("nonebot_plugin_chat_note") + op.drop_table("nonebot_plugin_chat_moonclawconfig") + op.drop_table("nonebot_plugin_chat_messagequeuecache") + op.drop_table("nonebot_plugin_bots_user_private_chat_settings") + op.drop_table("nonebot_plugin_access_subjectdata") # ### end Alembic commands ### diff --git a/src/plugins/nonebot_plugin_chat/config.py b/src/plugins/nonebot_plugin_chat/config.py index 269d433..718d1d4 100644 --- a/src/plugins/nonebot_plugin_chat/config.py +++ b/src/plugins/nonebot_plugin_chat/config.py @@ -2,6 +2,7 @@ from pydantic import BaseModel from nonebot_plugin_localstore import get_data_dir + class Config(BaseModel): """Plugin Config Here""" diff --git a/src/plugins/nonebot_plugin_chat/core/matchers.py b/src/plugins/nonebot_plugin_chat/core/matchers.py index bd35530..ae3a139 100644 --- a/src/plugins/nonebot_plugin_chat/core/matchers.py +++ b/src/plugins/nonebot_plugin_chat/core/matchers.py @@ -99,4 +99,3 @@ async def _( message = await UniMessage.of(message=platform_message, bot=bot).attach_reply(event, bot) nickname = await get_nickname(user_id, bot, event) await session.handle_message(message, user_id, event, state, nickname, True) - diff --git a/src/plugins/nonebot_plugin_chat/core/message.py b/src/plugins/nonebot_plugin_chat/core/message.py index 14bfe28..542dbbf 100644 --- a/src/plugins/nonebot_plugin_chat/core/message.py +++ b/src/plugins/nonebot_plugin_chat/core/message.py @@ -121,7 +121,6 @@ async def fetch_reply(self) -> None: # 记录抓取结束时间 timing_stats_manager.record_fetch_end(session_id) - async def stop_fetcher(self) -> None: if self.fetcher_task: self.fetcher_task.cancel() @@ -136,7 +135,6 @@ async def retry_callback(self, retry_count: int, status: Literal["retrying", "fa return await self.processor.send_message(message) - async def _fetch_reply(self) -> FetchStatus: state = FetchStatus.SUCCESS messages = await self.get_messages() @@ -147,14 +145,13 @@ async def _fetch_reply(self) -> FetchStatus: max_retries = await config_manager.get("message_queue_max_retries", 5) - fetcher = await MessageFetcher.create( messages, False, identify="Chat", reasoning_effort="medium", functions=await self.processor.tool_manager.select_tools("group"), - pre_function_call=self.processor.send_function_call_feedback + pre_function_call=self.processor.send_function_call_feedback, ) retry_count = 0 while retry_count < max_retries: @@ -179,8 +176,6 @@ async def _fetch_reply(self) -> FetchStatus: self.inserted_messages.clear() return FetchStatus.FAILED - - return state def append_user_message(self, message: str) -> None: @@ -191,7 +186,7 @@ def is_last_message_from_user(self) -> bool: def get_status(self) -> str: """获取当前状态 - + Returns: str: 当前状态,可能为 "已失败"、"请求中" 或 "待命" """ @@ -213,7 +208,7 @@ def get_status(self) -> str: def get_status_info(self) -> dict: """获取详细状态信息 - + Returns: dict: 包含详细状态信息的字典 """ diff --git a/src/plugins/nonebot_plugin_chat/core/processor.py b/src/plugins/nonebot_plugin_chat/core/processor.py index 77bbf56..ba6aa04 100644 --- a/src/plugins/nonebot_plugin_chat/core/processor.py +++ b/src/plugins/nonebot_plugin_chat/core/processor.py @@ -69,7 +69,6 @@ async def loop(self) -> None: logger.exception(e) await asyncio.sleep(1) - async def get_message(self) -> None: if not self.session.message_queue: await asyncio.sleep(0.5) @@ -129,7 +128,6 @@ async def generate_reply(self, important: bool = False) -> None: return self.cold_until = datetime.now() + timedelta(seconds=3) - if self.session.get_session_type() == "group": self.openai_messages.continuous_response = self.openai_messages.continuous_response or important @@ -163,7 +161,6 @@ async def send_message(self, message_content: str, reply_message_id: str | None # 记录回应用时(使用 reply_message_id 查找对应的原消息) self._record_reply_timing(reply_message_id) - def _record_reply_timing(self, reply_message_id: str | None = None) -> None: """记录回应用时(从 reply_message_id 对应的消息到发送回复的时间)""" # 如果提供了 reply_message_id,查找对应的消息 @@ -203,14 +200,12 @@ def get_message_content_list(self) -> list[str]: l.append(str(msg.content)) return l - - async def generate_system_prompt(self) -> OpenAIMessage: chat_history = "\n".join(self.get_message_content_list()) # 获取相关笔记 note_manager = await get_context_notes(self.session.session_id) notes, notes_from_other_group = await note_manager.filter_note(chat_history) - + # 加载 skill skills_content = await skill_manager.get_skills_content(self.session.lang_str) @@ -246,4 +241,3 @@ async def format_note(note): ), "system", ) - diff --git a/src/plugins/nonebot_plugin_chat/core/session/base.py b/src/plugins/nonebot_plugin_chat/core/session/base.py index 13479f6..9f3818d 100644 --- a/src/plugins/nonebot_plugin_chat/core/session/base.py +++ b/src/plugins/nonebot_plugin_chat/core/session/base.py @@ -44,7 +44,6 @@ def __init__(self, session_id: str, bot: Bot, target: Target, lang_str: str = f" self.llm_timers = [] # 定时器列表 self.processor = MessageProcessor(self) - @abstractmethod async def setup(self) -> None: await self.processor.setup() @@ -115,7 +114,6 @@ async def get_user_info(self, user_id: str) -> AdapterUserInfo: async def text(self, key: str, *args, **kwargs) -> str: return await lang.text(key, self.lang_str, *args, **kwargs) - async def process_timer(self) -> None: dt = datetime.now() if self.mute_until and dt > self.mute_until: @@ -140,7 +138,6 @@ async def get_cached_messages_string(self) -> str: ) return "\n".join(messages) - async def set_timer(self, delay: int, description: str = ""): """ 设置定时器 diff --git a/src/plugins/nonebot_plugin_chat/core/session/group.py b/src/plugins/nonebot_plugin_chat/core/session/group.py index 6f1df8a..b4fe513 100644 --- a/src/plugins/nonebot_plugin_chat/core/session/group.py +++ b/src/plugins/nonebot_plugin_chat/core/session/group.py @@ -55,7 +55,7 @@ def __init__(self, session_id: str, bot: Bot, target: Target, lang_name: str = " async def setup(self) -> None: await super().setup() - await self.setup_session_name()\ + await self.setup_session_name() def is_napcat_bot(self) -> bool: return self.bot.self_id in config.napcat_bot_ids diff --git a/src/plugins/nonebot_plugin_chat/core/session/private.py b/src/plugins/nonebot_plugin_chat/core/session/private.py index 0ffe3dd..6737c8c 100644 --- a/src/plugins/nonebot_plugin_chat/core/session/private.py +++ b/src/plugins/nonebot_plugin_chat/core/session/private.py @@ -50,7 +50,6 @@ async def format_message(self, origin_message: str) -> UniMessage: def is_napcat_bot(self) -> bool: return self.bot.self_id in config.napcat_bot_ids - async def get_user_info(self, _: str) -> AdapterUserInfo: return self.user_info diff --git a/src/plugins/nonebot_plugin_chat/matcher/new.py b/src/plugins/nonebot_plugin_chat/matcher/new.py index 63de2a2..e7e600d 100644 --- a/src/plugins/nonebot_plugin_chat/matcher/new.py +++ b/src/plugins/nonebot_plugin_chat/matcher/new.py @@ -41,5 +41,5 @@ async def _( logger.info(f"Resetting session for user {user_id}") await reset_session(user_id) - + await lang.send("new.completed", user_id) diff --git a/src/plugins/nonebot_plugin_chat/matcher/retry.py b/src/plugins/nonebot_plugin_chat/matcher/retry.py index 5a9ab6f..b2a8cb2 100644 --- a/src/plugins/nonebot_plugin_chat/matcher/retry.py +++ b/src/plugins/nonebot_plugin_chat/matcher/retry.py @@ -29,6 +29,7 @@ from ..lang import lang + @on_command("retry").handle() async def _( event: Event, @@ -42,11 +43,9 @@ async def _( if not is_superuser: await matcher.finish() - session = await create_private_session(user_id, get_target(event), bot) if session.mute_until is not None: await matcher.finish() session.processor.openai_messages.continuous_response = True await lang.send("retry.started", user_id) await session.processor.openai_messages.fetch_reply() - diff --git a/src/plugins/nonebot_plugin_chat/models.py b/src/plugins/nonebot_plugin_chat/models.py index 3cdde09..19f175c 100644 --- a/src/plugins/nonebot_plugin_chat/models.py +++ b/src/plugins/nonebot_plugin_chat/models.py @@ -11,7 +11,6 @@ CompatibleMediumText = Text().with_variant(MEDIUMTEXT(), "mysql") - class Note(Model): """Note model for storing user-generated notes with optional expiration and keywords""" @@ -22,6 +21,7 @@ class Note(Model): created_time: Mapped[float] = mapped_column(Float()) expire_time: Mapped[Optional[datetime]] = mapped_column(nullable=True) # Optional expiration time + class MessageQueueCache(Model): """消息队列缓存,用于持久化 OpenAI 消息历史以便重启后恢复""" @@ -30,6 +30,7 @@ class MessageQueueCache(Model): messages_json: Mapped[str] = mapped_column(CompatibleMediumText) # JSON 序列化的消息列表 updated_time: Mapped[float] = mapped_column(Float()) # 最后更新时间戳 + class PrivateChatSession(Model): """记录用户私聊会话信息,用于主动消息时获取正确的 bot""" @@ -39,9 +40,9 @@ class PrivateChatSession(Model): last_proactive_message_time: Mapped[Optional[float]] = mapped_column(Float(), nullable=True) # 最后主动消息时间戳 - class MoonClawConfig(Model): """MainSession 数据持久化存储,用于保存 action_history""" + key: Mapped[str] = mapped_column(String(64), primary_key=True) data_json: Mapped[str] = mapped_column(Text()) updated_time: Mapped[datetime] = mapped_column(DateTime(), default=datetime.now) diff --git a/src/plugins/nonebot_plugin_chat/startup.py b/src/plugins/nonebot_plugin_chat/startup.py index 13ea4ca..e1fc90e 100644 --- a/src/plugins/nonebot_plugin_chat/startup.py +++ b/src/plugins/nonebot_plugin_chat/startup.py @@ -5,9 +5,6 @@ driver = get_driver() - - - @driver.on_startup async def _init_video_server(): import nonebot diff --git a/src/plugins/nonebot_plugin_chat/types.py b/src/plugins/nonebot_plugin_chat/types.py index ad3a240..51c65bd 100644 --- a/src/plugins/nonebot_plugin_chat/types.py +++ b/src/plugins/nonebot_plugin_chat/types.py @@ -5,7 +5,6 @@ # from nonebot_plugin_chat.models import RuaAction - class CachedMessage(TypedDict): content: str nickname: str @@ -20,8 +19,6 @@ class GetTextFunc(Protocol): async def __call__(self, key: str, *args: Any, **kwargs: Any) -> str: ... - - class AvailableNote(TypedDict): create: Literal[True] text: str @@ -44,4 +41,3 @@ class AdapterUserInfo(TypedDict): nickname: str join_time: int card: Optional[str] - diff --git a/src/plugins/nonebot_plugin_chat/utils/__init__.py b/src/plugins/nonebot_plugin_chat/utils/__init__.py index 948ed37..bce0b23 100644 --- a/src/plugins/nonebot_plugin_chat/utils/__init__.py +++ b/src/plugins/nonebot_plugin_chat/utils/__init__.py @@ -16,4 +16,4 @@ # ############################################################################## from .config_manager import ConfigManager -from .group import enabled_group, parse_message_to_string \ No newline at end of file +from .group import enabled_group, parse_message_to_string diff --git a/src/plugins/nonebot_plugin_chat/utils/config_manager.py b/src/plugins/nonebot_plugin_chat/utils/config_manager.py index f8d5a14..99e3bd2 100644 --- a/src/plugins/nonebot_plugin_chat/utils/config_manager.py +++ b/src/plugins/nonebot_plugin_chat/utils/config_manager.py @@ -74,11 +74,7 @@ async def set(self, key: str, value: Any) -> bool: config = await session.get(MoonClawConfig, key) if config is None: - config = MoonClawConfig( - key=key, - data_json=data_json, - updated_time=datetime.now() - ) + config = MoonClawConfig(key=key, data_json=data_json, updated_time=datetime.now()) session.add(config) else: config.data_json = data_json @@ -181,4 +177,5 @@ async def update(self, updates: Dict[str, Any]) -> Dict[str, bool]: results[key] = await self.set(key, value) return results -config_manager = ConfigManager() \ No newline at end of file + +config_manager = ConfigManager() diff --git a/src/plugins/nonebot_plugin_chat/utils/skill.py b/src/plugins/nonebot_plugin_chat/utils/skill.py index bff6a47..511ea96 100644 --- a/src/plugins/nonebot_plugin_chat/utils/skill.py +++ b/src/plugins/nonebot_plugin_chat/utils/skill.py @@ -18,7 +18,7 @@ def __init__(self, name: str, description: str, content: str, path: Path): self.content = content self.path = path self.description = description - + @classmethod def from_text(cls, text: str, file: Path) -> "Skill": post = frontmatter.loads(text) @@ -58,10 +58,14 @@ async def load_skills(self) -> None: except Exception as e: logger.error(f"Failed to load skill {skill_dir.name}: {e}") - async def get_skills_content(self, lang_str: str) -> str: await self.load_skills() - return "\n".join([await lang.text("skill.item", lang_str, skill.name, skill.path.as_posix(), skill.description) for skill in self.skills]) + return "\n".join( + [ + await lang.text("skill.item", lang_str, skill.name, skill.path.as_posix(), skill.description) + for skill in self.skills + ] + ) -skill_manager = SkillManager() \ No newline at end of file +skill_manager = SkillManager() diff --git a/src/plugins/nonebot_plugin_chat/utils/tool_manager.py b/src/plugins/nonebot_plugin_chat/utils/tool_manager.py index 235da77..508f0f5 100644 --- a/src/plugins/nonebot_plugin_chat/utils/tool_manager.py +++ b/src/plugins/nonebot_plugin_chat/utils/tool_manager.py @@ -83,10 +83,7 @@ async def report_tool_call(self, tool_name: str, params: dict) -> Optional[str]: elif report_level == "full": # full 模式:显示工具名和完整参数 params_str = ", ".join(f"{k}={v}" for k, v in params.items()) - return report_template.format( - tool_name=tool_name, - params=f" (参数: {params_str})" if params_str else "" - ) + return report_template.format(tool_name=tool_name, params=f" (参数: {params_str})" if params_str else "") return None @@ -528,7 +525,7 @@ async def select_tools(self, mode: Literal["group", "agent"]) -> list[AsyncFunct type="string", description=await self.text("tools_desc.get_note_poster.keywords"), required=False, - ) + ), }, ) ) diff --git a/src/plugins/nonebot_plugin_chat/utils/tools/exec.py b/src/plugins/nonebot_plugin_chat/utils/tools/exec.py index c33718c..2cd6fc3 100644 --- a/src/plugins/nonebot_plugin_chat/utils/tools/exec.py +++ b/src/plugins/nonebot_plugin_chat/utils/tools/exec.py @@ -126,10 +126,7 @@ async def exec_command( # 等待命令完成或超时 try: - stdout, stderr = await asyncio.wait_for( - process.communicate(), - timeout=actual_timeout - ) + stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=actual_timeout) except asyncio.TimeoutError: # 超时,终止进程 try: @@ -169,9 +166,7 @@ async def exec_command( result_lines.append(truncated_stdout) if stdout_truncated: result_lines.append("") - result_lines.append( - await get_text("exec.output_truncated", max_output_length) - ) + result_lines.append(await get_text("exec.output_truncated", max_output_length)) # 添加标准错误 if truncated_stderr: @@ -180,9 +175,7 @@ async def exec_command( result_lines.append(truncated_stderr) if stderr_truncated: result_lines.append("") - result_lines.append( - await get_text("exec.output_truncated", max_output_length) - ) + result_lines.append(await get_text("exec.output_truncated", max_output_length)) # 如果没有输出 if not truncated_stdout and not truncated_stderr: diff --git a/src/plugins/nonebot_plugin_chat/utils/tools/file.py b/src/plugins/nonebot_plugin_chat/utils/tools/file.py index 7a72d6d..efe87f1 100644 --- a/src/plugins/nonebot_plugin_chat/utils/tools/file.py +++ b/src/plugins/nonebot_plugin_chat/utils/tools/file.py @@ -82,4 +82,4 @@ async def read_file( return await get_text("read_file.permission_denied", file_path) except Exception as e: logger.exception(e) - return await get_text("read_file.error", file_path, str(e)) \ No newline at end of file + return await get_text("read_file.error", file_path, str(e)) diff --git a/src/plugins/nonebot_plugin_chat/utils/tools/interactive_exec.py b/src/plugins/nonebot_plugin_chat/utils/tools/interactive_exec.py index ea5ba5e..058489f 100644 --- a/src/plugins/nonebot_plugin_chat/utils/tools/interactive_exec.py +++ b/src/plugins/nonebot_plugin_chat/utils/tools/interactive_exec.py @@ -265,12 +265,7 @@ async def interactive_exec_create_session( logger.info(f"创建交互式命令执行会话: {session_id} - {command}") - return await get_text( - "interactive_exec.create_session.success", - session_id, - title, - command - ) + return await get_text("interactive_exec.create_session.success", session_id, title, command) except FileNotFoundError as e: logger.error(f"命令执行失败: {e}")