Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
228 changes: 121 additions & 107 deletions migrations/versions/95cb99f34471_.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""empty message

迁移 ID: 95cb99f34471
父迁移:
父迁移:
创建时间: 2026-03-28 23:36:34.366445

"""

from __future__ import annotations

from collections.abc import Sequence
Expand All @@ -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
Expand All @@ -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 ###

Expand All @@ -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 ###
1 change: 1 addition & 0 deletions src/plugins/nonebot_plugin_chat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pydantic import BaseModel
from nonebot_plugin_localstore import get_data_dir


class Config(BaseModel):
"""Plugin Config Here"""

Expand Down
1 change: 0 additions & 1 deletion src/plugins/nonebot_plugin_chat/core/matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

11 changes: 3 additions & 8 deletions src/plugins/nonebot_plugin_chat/core/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -191,7 +186,7 @@ def is_last_message_from_user(self) -> bool:

def get_status(self) -> str:
"""获取当前状态

Returns:
str: 当前状态,可能为 "已失败"、"请求中" 或 "待命"
"""
Expand All @@ -213,7 +208,7 @@ def get_status(self) -> str:

def get_status_info(self) -> dict:
"""获取详细状态信息

Returns:
dict: 包含详细状态信息的字典
"""
Expand Down
8 changes: 1 addition & 7 deletions src/plugins/nonebot_plugin_chat/core/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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,查找对应的消息
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -246,4 +241,3 @@ async def format_note(note):
),
"system",
)

Loading