AstrBot 4.5.0__py3-none-any.whl → 4.5.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- astrbot/api/__init__.py +10 -11
- astrbot/api/event/__init__.py +5 -6
- astrbot/api/event/filter/__init__.py +37 -36
- astrbot/api/platform/__init__.py +7 -8
- astrbot/api/provider/__init__.py +7 -7
- astrbot/api/star/__init__.py +3 -4
- astrbot/api/util/__init__.py +2 -2
- astrbot/cli/__main__.py +5 -5
- astrbot/cli/commands/__init__.py +3 -3
- astrbot/cli/commands/cmd_conf.py +19 -16
- astrbot/cli/commands/cmd_init.py +3 -2
- astrbot/cli/commands/cmd_plug.py +8 -10
- astrbot/cli/commands/cmd_run.py +5 -6
- astrbot/cli/utils/__init__.py +6 -6
- astrbot/cli/utils/basic.py +14 -14
- astrbot/cli/utils/plugin.py +24 -15
- astrbot/cli/utils/version_comparator.py +10 -12
- astrbot/core/__init__.py +8 -6
- astrbot/core/agent/agent.py +3 -2
- astrbot/core/agent/handoff.py +6 -2
- astrbot/core/agent/hooks.py +9 -6
- astrbot/core/agent/mcp_client.py +50 -15
- astrbot/core/agent/message.py +168 -0
- astrbot/core/agent/response.py +2 -1
- astrbot/core/agent/run_context.py +2 -3
- astrbot/core/agent/runners/base.py +10 -13
- astrbot/core/agent/runners/tool_loop_agent_runner.py +52 -51
- astrbot/core/agent/tool.py +60 -41
- astrbot/core/agent/tool_executor.py +9 -3
- astrbot/core/astr_agent_context.py +3 -1
- astrbot/core/astrbot_config_mgr.py +29 -9
- astrbot/core/config/__init__.py +2 -2
- astrbot/core/config/astrbot_config.py +28 -26
- astrbot/core/config/default.py +44 -6
- astrbot/core/conversation_mgr.py +105 -36
- astrbot/core/core_lifecycle.py +68 -54
- astrbot/core/db/__init__.py +33 -18
- astrbot/core/db/migration/helper.py +18 -13
- astrbot/core/db/migration/migra_3_to_4.py +53 -34
- astrbot/core/db/migration/migra_45_to_46.py +1 -1
- astrbot/core/db/migration/shared_preferences_v3.py +2 -1
- astrbot/core/db/migration/sqlite_v3.py +26 -23
- astrbot/core/db/po.py +27 -18
- astrbot/core/db/sqlite.py +74 -45
- astrbot/core/db/vec_db/base.py +10 -14
- astrbot/core/db/vec_db/faiss_impl/document_storage.py +90 -77
- astrbot/core/db/vec_db/faiss_impl/embedding_storage.py +9 -3
- astrbot/core/db/vec_db/faiss_impl/vec_db.py +36 -31
- astrbot/core/event_bus.py +8 -6
- astrbot/core/file_token_service.py +6 -5
- astrbot/core/initial_loader.py +7 -5
- astrbot/core/knowledge_base/chunking/__init__.py +1 -3
- astrbot/core/knowledge_base/chunking/base.py +1 -0
- astrbot/core/knowledge_base/chunking/fixed_size.py +2 -0
- astrbot/core/knowledge_base/chunking/recursive.py +16 -10
- astrbot/core/knowledge_base/kb_db_sqlite.py +50 -48
- astrbot/core/knowledge_base/kb_helper.py +30 -17
- astrbot/core/knowledge_base/kb_mgr.py +6 -7
- astrbot/core/knowledge_base/models.py +10 -4
- astrbot/core/knowledge_base/parsers/__init__.py +3 -5
- astrbot/core/knowledge_base/parsers/base.py +1 -0
- astrbot/core/knowledge_base/parsers/markitdown_parser.py +2 -1
- astrbot/core/knowledge_base/parsers/pdf_parser.py +2 -1
- astrbot/core/knowledge_base/parsers/text_parser.py +1 -0
- astrbot/core/knowledge_base/parsers/util.py +1 -1
- astrbot/core/knowledge_base/retrieval/__init__.py +6 -8
- astrbot/core/knowledge_base/retrieval/manager.py +17 -14
- astrbot/core/knowledge_base/retrieval/rank_fusion.py +7 -3
- astrbot/core/knowledge_base/retrieval/sparse_retriever.py +11 -5
- astrbot/core/log.py +21 -13
- astrbot/core/message/components.py +123 -217
- astrbot/core/message/message_event_result.py +24 -24
- astrbot/core/persona_mgr.py +20 -11
- astrbot/core/pipeline/__init__.py +7 -7
- astrbot/core/pipeline/content_safety_check/stage.py +13 -9
- astrbot/core/pipeline/content_safety_check/strategies/__init__.py +1 -2
- astrbot/core/pipeline/content_safety_check/strategies/baidu_aip.py +12 -13
- astrbot/core/pipeline/content_safety_check/strategies/keywords.py +1 -0
- astrbot/core/pipeline/content_safety_check/strategies/strategy.py +6 -6
- astrbot/core/pipeline/context.py +4 -1
- astrbot/core/pipeline/context_utils.py +77 -7
- astrbot/core/pipeline/preprocess_stage/stage.py +12 -9
- astrbot/core/pipeline/process_stage/method/llm_request.py +125 -72
- astrbot/core/pipeline/process_stage/method/star_request.py +19 -17
- astrbot/core/pipeline/process_stage/stage.py +13 -10
- astrbot/core/pipeline/process_stage/utils.py +6 -5
- astrbot/core/pipeline/rate_limit_check/stage.py +37 -36
- astrbot/core/pipeline/respond/stage.py +23 -20
- astrbot/core/pipeline/result_decorate/stage.py +31 -23
- astrbot/core/pipeline/scheduler.py +12 -8
- astrbot/core/pipeline/session_status_check/stage.py +12 -8
- astrbot/core/pipeline/stage.py +10 -4
- astrbot/core/pipeline/waking_check/stage.py +24 -18
- astrbot/core/pipeline/whitelist_check/stage.py +10 -7
- astrbot/core/platform/__init__.py +6 -6
- astrbot/core/platform/astr_message_event.py +76 -110
- astrbot/core/platform/astrbot_message.py +11 -13
- astrbot/core/platform/manager.py +16 -15
- astrbot/core/platform/message_session.py +5 -3
- astrbot/core/platform/platform.py +16 -24
- astrbot/core/platform/platform_metadata.py +4 -4
- astrbot/core/platform/register.py +8 -8
- astrbot/core/platform/sources/aiocqhttp/aiocqhttp_message_event.py +23 -15
- astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py +51 -33
- astrbot/core/platform/sources/dingtalk/dingtalk_adapter.py +47 -29
- astrbot/core/platform/sources/dingtalk/dingtalk_event.py +7 -3
- astrbot/core/platform/sources/discord/client.py +9 -6
- astrbot/core/platform/sources/discord/components.py +18 -14
- astrbot/core/platform/sources/discord/discord_platform_adapter.py +45 -30
- astrbot/core/platform/sources/discord/discord_platform_event.py +38 -30
- astrbot/core/platform/sources/lark/lark_adapter.py +23 -17
- astrbot/core/platform/sources/lark/lark_event.py +21 -14
- astrbot/core/platform/sources/misskey/misskey_adapter.py +107 -67
- astrbot/core/platform/sources/misskey/misskey_api.py +153 -129
- astrbot/core/platform/sources/misskey/misskey_event.py +20 -15
- astrbot/core/platform/sources/misskey/misskey_utils.py +74 -62
- astrbot/core/platform/sources/qqofficial/qqofficial_message_event.py +63 -44
- astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py +41 -26
- astrbot/core/platform/sources/qqofficial_webhook/qo_webhook_adapter.py +36 -17
- astrbot/core/platform/sources/qqofficial_webhook/qo_webhook_event.py +3 -1
- astrbot/core/platform/sources/qqofficial_webhook/qo_webhook_server.py +12 -7
- astrbot/core/platform/sources/satori/satori_adapter.py +56 -38
- astrbot/core/platform/sources/satori/satori_event.py +34 -25
- astrbot/core/platform/sources/slack/client.py +11 -9
- astrbot/core/platform/sources/slack/slack_adapter.py +52 -36
- astrbot/core/platform/sources/slack/slack_event.py +34 -24
- astrbot/core/platform/sources/telegram/tg_adapter.py +38 -18
- astrbot/core/platform/sources/telegram/tg_event.py +32 -18
- astrbot/core/platform/sources/webchat/webchat_adapter.py +27 -17
- astrbot/core/platform/sources/webchat/webchat_event.py +14 -10
- astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py +115 -120
- astrbot/core/platform/sources/wechatpadpro/wechatpadpro_message_event.py +9 -8
- astrbot/core/platform/sources/wechatpadpro/xml_data_parser.py +15 -16
- astrbot/core/platform/sources/wecom/wecom_adapter.py +35 -18
- astrbot/core/platform/sources/wecom/wecom_event.py +55 -48
- astrbot/core/platform/sources/wecom/wecom_kf.py +34 -44
- astrbot/core/platform/sources/wecom/wecom_kf_message.py +26 -10
- astrbot/core/platform/sources/wecom_ai_bot/WXBizJsonMsgCrypt.py +18 -10
- astrbot/core/platform/sources/wecom_ai_bot/__init__.py +3 -5
- astrbot/core/platform/sources/wecom_ai_bot/ierror.py +0 -1
- astrbot/core/platform/sources/wecom_ai_bot/wecomai_adapter.py +61 -37
- astrbot/core/platform/sources/wecom_ai_bot/wecomai_api.py +67 -28
- astrbot/core/platform/sources/wecom_ai_bot/wecomai_event.py +8 -9
- astrbot/core/platform/sources/wecom_ai_bot/wecomai_queue_mgr.py +18 -9
- astrbot/core/platform/sources/wecom_ai_bot/wecomai_server.py +14 -12
- astrbot/core/platform/sources/wecom_ai_bot/wecomai_utils.py +22 -12
- astrbot/core/platform/sources/weixin_official_account/weixin_offacc_adapter.py +40 -26
- astrbot/core/platform/sources/weixin_official_account/weixin_offacc_event.py +47 -45
- astrbot/core/platform_message_history_mgr.py +5 -3
- astrbot/core/provider/__init__.py +2 -3
- astrbot/core/provider/entites.py +8 -8
- astrbot/core/provider/entities.py +61 -75
- astrbot/core/provider/func_tool_manager.py +59 -55
- astrbot/core/provider/manager.py +40 -22
- astrbot/core/provider/provider.py +72 -46
- astrbot/core/provider/register.py +7 -7
- astrbot/core/provider/sources/anthropic_source.py +48 -30
- astrbot/core/provider/sources/azure_tts_source.py +17 -13
- astrbot/core/provider/sources/coze_api_client.py +27 -17
- astrbot/core/provider/sources/coze_source.py +104 -87
- astrbot/core/provider/sources/dashscope_source.py +18 -11
- astrbot/core/provider/sources/dashscope_tts.py +36 -23
- astrbot/core/provider/sources/dify_source.py +25 -20
- astrbot/core/provider/sources/edge_tts_source.py +21 -17
- astrbot/core/provider/sources/fishaudio_tts_api_source.py +22 -14
- astrbot/core/provider/sources/gemini_embedding_source.py +12 -13
- astrbot/core/provider/sources/gemini_source.py +72 -58
- astrbot/core/provider/sources/gemini_tts_source.py +8 -6
- astrbot/core/provider/sources/gsv_selfhosted_source.py +17 -14
- astrbot/core/provider/sources/gsvi_tts_source.py +11 -7
- astrbot/core/provider/sources/minimax_tts_api_source.py +50 -40
- astrbot/core/provider/sources/openai_embedding_source.py +6 -8
- astrbot/core/provider/sources/openai_source.py +102 -69
- astrbot/core/provider/sources/openai_tts_api_source.py +14 -6
- astrbot/core/provider/sources/sensevoice_selfhosted_source.py +13 -11
- astrbot/core/provider/sources/vllm_rerank_source.py +10 -4
- astrbot/core/provider/sources/volcengine_tts.py +38 -31
- astrbot/core/provider/sources/whisper_api_source.py +14 -12
- astrbot/core/provider/sources/whisper_selfhosted_source.py +15 -11
- astrbot/core/provider/sources/xinference_rerank_source.py +116 -0
- astrbot/core/provider/sources/xinference_stt_provider.py +197 -0
- astrbot/core/star/__init__.py +16 -11
- astrbot/core/star/config.py +10 -15
- astrbot/core/star/context.py +109 -84
- astrbot/core/star/filter/__init__.py +4 -3
- astrbot/core/star/filter/command.py +30 -28
- astrbot/core/star/filter/command_group.py +27 -24
- astrbot/core/star/filter/custom_filter.py +6 -5
- astrbot/core/star/filter/event_message_type.py +4 -2
- astrbot/core/star/filter/permission.py +4 -2
- astrbot/core/star/filter/platform_adapter_type.py +4 -2
- astrbot/core/star/filter/regex.py +4 -2
- astrbot/core/star/register/__init__.py +19 -19
- astrbot/core/star/register/star.py +6 -2
- astrbot/core/star/register/star_handler.py +96 -73
- astrbot/core/star/session_llm_manager.py +48 -14
- astrbot/core/star/session_plugin_manager.py +29 -15
- astrbot/core/star/star.py +1 -2
- astrbot/core/star/star_handler.py +13 -8
- astrbot/core/star/star_manager.py +151 -59
- astrbot/core/star/star_tools.py +44 -37
- astrbot/core/star/updator.py +10 -10
- astrbot/core/umop_config_router.py +10 -4
- astrbot/core/updator.py +13 -5
- astrbot/core/utils/astrbot_path.py +3 -5
- astrbot/core/utils/dify_api_client.py +33 -15
- astrbot/core/utils/io.py +66 -42
- astrbot/core/utils/log_pipe.py +1 -1
- astrbot/core/utils/metrics.py +7 -7
- astrbot/core/utils/path_util.py +15 -16
- astrbot/core/utils/pip_installer.py +5 -5
- astrbot/core/utils/session_waiter.py +19 -20
- astrbot/core/utils/shared_preferences.py +45 -20
- astrbot/core/utils/t2i/__init__.py +4 -1
- astrbot/core/utils/t2i/network_strategy.py +35 -26
- astrbot/core/utils/t2i/renderer.py +11 -5
- astrbot/core/utils/t2i/template_manager.py +14 -15
- astrbot/core/utils/tencent_record_helper.py +19 -13
- astrbot/core/utils/version_comparator.py +10 -13
- astrbot/core/zip_updator.py +43 -40
- astrbot/dashboard/routes/__init__.py +18 -18
- astrbot/dashboard/routes/auth.py +10 -8
- astrbot/dashboard/routes/chat.py +30 -21
- astrbot/dashboard/routes/config.py +92 -75
- astrbot/dashboard/routes/conversation.py +46 -39
- astrbot/dashboard/routes/file.py +4 -2
- astrbot/dashboard/routes/knowledge_base.py +47 -40
- astrbot/dashboard/routes/log.py +9 -4
- astrbot/dashboard/routes/persona.py +19 -16
- astrbot/dashboard/routes/plugin.py +69 -55
- astrbot/dashboard/routes/route.py +3 -1
- astrbot/dashboard/routes/session_management.py +130 -116
- astrbot/dashboard/routes/stat.py +34 -34
- astrbot/dashboard/routes/t2i.py +15 -12
- astrbot/dashboard/routes/tools.py +47 -52
- astrbot/dashboard/routes/update.py +32 -28
- astrbot/dashboard/server.py +30 -26
- astrbot/dashboard/utils.py +8 -4
- {astrbot-4.5.0.dist-info → astrbot-4.5.2.dist-info}/METADATA +4 -2
- astrbot-4.5.2.dist-info/RECORD +261 -0
- astrbot-4.5.0.dist-info/RECORD +0 -258
- {astrbot-4.5.0.dist-info → astrbot-4.5.2.dist-info}/WHEEL +0 -0
- {astrbot-4.5.0.dist-info → astrbot-4.5.2.dist-info}/entry_points.txt +0 -0
- {astrbot-4.5.0.dist-info → astrbot-4.5.2.dist-info}/licenses/LICENSE +0 -0
astrbot/core/config/default.py
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
"""
|
|
2
|
-
如需修改配置,请在 `data/cmd_config.json` 中修改或者在管理面板中可视化修改。
|
|
3
|
-
"""
|
|
1
|
+
"""如需修改配置,请在 `data/cmd_config.json` 中修改或者在管理面板中可视化修改。"""
|
|
4
2
|
|
|
5
3
|
import os
|
|
6
4
|
|
|
7
5
|
from astrbot.core.utils.astrbot_path import get_astrbot_data_path
|
|
8
6
|
|
|
9
|
-
VERSION = "4.5.
|
|
7
|
+
VERSION = "4.5.2"
|
|
10
8
|
DB_PATH = os.path.join(get_astrbot_data_path(), "data_v4.db")
|
|
11
9
|
|
|
12
10
|
# 默认配置
|
|
@@ -324,6 +322,10 @@ CONFIG_METADATA_2 = {
|
|
|
324
322
|
# "type": "string",
|
|
325
323
|
# "options": ["fullscreen", "embedded"],
|
|
326
324
|
# },
|
|
325
|
+
"is_sandbox": {
|
|
326
|
+
"description": "沙箱模式",
|
|
327
|
+
"type": "bool",
|
|
328
|
+
},
|
|
327
329
|
"satori_api_base_url": {
|
|
328
330
|
"description": "Satori API 终结点",
|
|
329
331
|
"type": "string",
|
|
@@ -767,6 +769,7 @@ CONFIG_METADATA_2 = {
|
|
|
767
769
|
"timeout": 120,
|
|
768
770
|
"model_config": {"model": "grok-2-latest", "temperature": 0.4},
|
|
769
771
|
"custom_extra_body": {},
|
|
772
|
+
"xai_native_search": False,
|
|
770
773
|
"modalities": ["text", "image", "tool_use"],
|
|
771
774
|
},
|
|
772
775
|
"Anthropic": {
|
|
@@ -1258,8 +1261,38 @@ CONFIG_METADATA_2 = {
|
|
|
1258
1261
|
"rerank_model": "BAAI/bge-reranker-base",
|
|
1259
1262
|
"timeout": 20,
|
|
1260
1263
|
},
|
|
1264
|
+
"Xinference Rerank": {
|
|
1265
|
+
"id": "xinference_rerank",
|
|
1266
|
+
"type": "xinference_rerank",
|
|
1267
|
+
"provider": "xinference",
|
|
1268
|
+
"provider_type": "rerank",
|
|
1269
|
+
"enable": True,
|
|
1270
|
+
"rerank_api_key": "",
|
|
1271
|
+
"rerank_api_base": "http://127.0.0.1:9997",
|
|
1272
|
+
"rerank_model": "BAAI/bge-reranker-base",
|
|
1273
|
+
"timeout": 20,
|
|
1274
|
+
"launch_model_if_not_running": False,
|
|
1275
|
+
},
|
|
1276
|
+
"Xinference STT": {
|
|
1277
|
+
"id": "xinference_stt",
|
|
1278
|
+
"type": "xinference_stt",
|
|
1279
|
+
"provider": "xinference",
|
|
1280
|
+
"provider_type": "speech_to_text",
|
|
1281
|
+
"enable": False,
|
|
1282
|
+
"api_key": "",
|
|
1283
|
+
"api_base": "http://127.0.0.1:9997",
|
|
1284
|
+
"model": "whisper-large-v3",
|
|
1285
|
+
"timeout": 180,
|
|
1286
|
+
"launch_model_if_not_running": False,
|
|
1287
|
+
},
|
|
1261
1288
|
},
|
|
1262
1289
|
"items": {
|
|
1290
|
+
"xai_native_search": {
|
|
1291
|
+
"description": "启用原生搜索功能",
|
|
1292
|
+
"type": "bool",
|
|
1293
|
+
"hint": "启用后,将通过 xAI 的 Chat Completions 原生 Live Search 进行联网检索(按需计费)。仅对 xAI 提供商生效。",
|
|
1294
|
+
"condition": {"provider": "xai"},
|
|
1295
|
+
},
|
|
1263
1296
|
"rerank_api_base": {
|
|
1264
1297
|
"description": "重排序模型 API Base URL",
|
|
1265
1298
|
"type": "string",
|
|
@@ -1274,6 +1307,11 @@ CONFIG_METADATA_2 = {
|
|
|
1274
1307
|
"description": "重排序模型名称",
|
|
1275
1308
|
"type": "string",
|
|
1276
1309
|
},
|
|
1310
|
+
"launch_model_if_not_running": {
|
|
1311
|
+
"description": "模型未运行时自动启动",
|
|
1312
|
+
"type": "bool",
|
|
1313
|
+
"hint": "如果模型当前未在 Xinference 服务中运行,是否尝试自动启动它。在生产环境中建议关闭。",
|
|
1314
|
+
},
|
|
1277
1315
|
"modalities": {
|
|
1278
1316
|
"description": "模型能力",
|
|
1279
1317
|
"type": "list",
|
|
@@ -2667,9 +2705,9 @@ CONFIG_METADATA_3_SYSTEM = {
|
|
|
2667
2705
|
"items": {"type": "string"},
|
|
2668
2706
|
},
|
|
2669
2707
|
},
|
|
2670
|
-
}
|
|
2708
|
+
},
|
|
2671
2709
|
},
|
|
2672
|
-
}
|
|
2710
|
+
},
|
|
2673
2711
|
}
|
|
2674
2712
|
|
|
2675
2713
|
|
astrbot/core/conversation_mgr.py
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
"""
|
|
2
|
-
AstrBot 会话-对话管理器, 维护两个本地存储, 其中一个是 json 格式的shared_preferences, 另外一个是数据库
|
|
1
|
+
"""AstrBot 会话-对话管理器, 维护两个本地存储, 其中一个是 json 格式的shared_preferences, 另外一个是数据库.
|
|
3
2
|
|
|
4
3
|
在 AstrBot 中, 会话和对话是独立的, 会话用于标记对话窗口, 例如群聊"123456789"可以建立一个会话,
|
|
5
4
|
在一个会话中可以建立多个对话, 并且支持对话的切换和删除
|
|
6
5
|
"""
|
|
7
6
|
|
|
8
7
|
import json
|
|
8
|
+
from collections.abc import Awaitable, Callable
|
|
9
|
+
|
|
9
10
|
from astrbot.core import sp
|
|
10
|
-
from
|
|
11
|
+
from astrbot.core.agent.message import AssistantMessageSegment, UserMessageSegment
|
|
11
12
|
from astrbot.core.db import BaseDatabase
|
|
12
13
|
from astrbot.core.db.po import Conversation, ConversationV2
|
|
13
14
|
|
|
@@ -16,31 +17,34 @@ class ConversationManager:
|
|
|
16
17
|
"""负责管理会话与 LLM 的对话,某个会话当前正在用哪个对话。"""
|
|
17
18
|
|
|
18
19
|
def __init__(self, db_helper: BaseDatabase):
|
|
19
|
-
self.session_conversations:
|
|
20
|
+
self.session_conversations: dict[str, str] = {}
|
|
20
21
|
self.db = db_helper
|
|
21
22
|
self.save_interval = 60 # 每 60 秒保存一次
|
|
22
23
|
|
|
23
24
|
# 会话删除回调函数列表(用于级联清理,如知识库配置)
|
|
24
|
-
self._on_session_deleted_callbacks:
|
|
25
|
+
self._on_session_deleted_callbacks: list[Callable[[str], Awaitable[None]]] = []
|
|
25
26
|
|
|
26
27
|
def register_on_session_deleted(
|
|
27
|
-
self,
|
|
28
|
+
self,
|
|
29
|
+
callback: Callable[[str], Awaitable[None]],
|
|
28
30
|
) -> None:
|
|
29
|
-
"""
|
|
31
|
+
"""注册会话删除回调函数.
|
|
30
32
|
|
|
31
33
|
其他模块可以注册回调来响应会话删除事件,实现级联清理。
|
|
32
34
|
例如:知识库模块可以注册回调来清理会话的知识库配置。
|
|
33
35
|
|
|
34
36
|
Args:
|
|
35
37
|
callback: 回调函数,接收会话ID (unified_msg_origin) 作为参数
|
|
38
|
+
|
|
36
39
|
"""
|
|
37
40
|
self._on_session_deleted_callbacks.append(callback)
|
|
38
41
|
|
|
39
42
|
async def _trigger_session_deleted(self, unified_msg_origin: str) -> None:
|
|
40
|
-
"""
|
|
43
|
+
"""触发会话删除回调.
|
|
41
44
|
|
|
42
45
|
Args:
|
|
43
46
|
unified_msg_origin: 会话ID
|
|
47
|
+
|
|
44
48
|
"""
|
|
45
49
|
for callback in self._on_session_deleted_callbacks:
|
|
46
50
|
try:
|
|
@@ -49,7 +53,7 @@ class ConversationManager:
|
|
|
49
53
|
from astrbot.core import logger
|
|
50
54
|
|
|
51
55
|
logger.error(
|
|
52
|
-
f"会话删除回调执行失败 (session: {unified_msg_origin}): {e}"
|
|
56
|
+
f"会话删除回调执行失败 (session: {unified_msg_origin}): {e}",
|
|
53
57
|
)
|
|
54
58
|
|
|
55
59
|
def _convert_conv_from_v2_to_v1(self, conv_v2: ConversationV2) -> Conversation:
|
|
@@ -75,12 +79,13 @@ class ConversationManager:
|
|
|
75
79
|
title: str | None = None,
|
|
76
80
|
persona_id: str | None = None,
|
|
77
81
|
) -> str:
|
|
78
|
-
"""
|
|
82
|
+
"""新建对话,并将当前会话的对话转移到新对话.
|
|
79
83
|
|
|
80
84
|
Args:
|
|
81
85
|
unified_msg_origin (str): 统一的消息来源字符串。格式为 platform_name:message_type:session_id
|
|
82
86
|
Returns:
|
|
83
87
|
conversation_id (str): 对话 ID, 是 uuid 格式的字符串
|
|
88
|
+
|
|
84
89
|
"""
|
|
85
90
|
if not platform_id:
|
|
86
91
|
# 如果没有提供 platform_id,则从 unified_msg_origin 中解析
|
|
@@ -106,18 +111,22 @@ class ConversationManager:
|
|
|
106
111
|
Args:
|
|
107
112
|
unified_msg_origin (str): 统一的消息来源字符串。格式为 platform_name:message_type:session_id
|
|
108
113
|
conversation_id (str): 对话 ID, 是 uuid 格式的字符串
|
|
114
|
+
|
|
109
115
|
"""
|
|
110
116
|
self.session_conversations[unified_msg_origin] = conversation_id
|
|
111
117
|
await sp.session_put(unified_msg_origin, "sel_conv_id", conversation_id)
|
|
112
118
|
|
|
113
119
|
async def delete_conversation(
|
|
114
|
-
self,
|
|
120
|
+
self,
|
|
121
|
+
unified_msg_origin: str,
|
|
122
|
+
conversation_id: str | None = None,
|
|
115
123
|
):
|
|
116
124
|
"""删除会话的对话,当 conversation_id 为 None 时删除会话当前的对话
|
|
117
125
|
|
|
118
126
|
Args:
|
|
119
127
|
unified_msg_origin (str): 统一的消息来源字符串。格式为 platform_name:message_type:session_id
|
|
120
128
|
conversation_id (str): 对话 ID, 是 uuid 格式的字符串
|
|
129
|
+
|
|
121
130
|
"""
|
|
122
131
|
if not conversation_id:
|
|
123
132
|
conversation_id = self.session_conversations.get(unified_msg_origin)
|
|
@@ -133,6 +142,7 @@ class ConversationManager:
|
|
|
133
142
|
|
|
134
143
|
Args:
|
|
135
144
|
unified_msg_origin (str): 统一的消息来源字符串。格式为 platform_name:message_type:session_id
|
|
145
|
+
|
|
136
146
|
"""
|
|
137
147
|
await self.db.delete_conversations_by_user_id(user_id=unified_msg_origin)
|
|
138
148
|
self.session_conversations.pop(unified_msg_origin, None)
|
|
@@ -148,6 +158,7 @@ class ConversationManager:
|
|
|
148
158
|
unified_msg_origin (str): 统一的消息来源字符串。格式为 platform_name:message_type:session_id
|
|
149
159
|
Returns:
|
|
150
160
|
conversation_id (str): 对话 ID, 是 uuid 格式的字符串
|
|
161
|
+
|
|
151
162
|
"""
|
|
152
163
|
ret = self.session_conversations.get(unified_msg_origin, None)
|
|
153
164
|
if not ret:
|
|
@@ -162,13 +173,15 @@ class ConversationManager:
|
|
|
162
173
|
conversation_id: str,
|
|
163
174
|
create_if_not_exists: bool = False,
|
|
164
175
|
) -> Conversation | None:
|
|
165
|
-
"""
|
|
176
|
+
"""获取会话的对话.
|
|
166
177
|
|
|
167
178
|
Args:
|
|
168
179
|
unified_msg_origin (str): 统一的消息来源字符串。格式为 platform_name:message_type:session_id
|
|
169
180
|
conversation_id (str): 对话 ID, 是 uuid 格式的字符串
|
|
181
|
+
create_if_not_exists (bool): 如果对话不存在,是否创建一个新的对话
|
|
170
182
|
Returns:
|
|
171
183
|
conversation (Conversation): 对话对象
|
|
184
|
+
|
|
172
185
|
"""
|
|
173
186
|
conv = await self.db.get_conversation_by_id(cid=conversation_id)
|
|
174
187
|
if not conv and create_if_not_exists:
|
|
@@ -181,18 +194,22 @@ class ConversationManager:
|
|
|
181
194
|
return conv_res
|
|
182
195
|
|
|
183
196
|
async def get_conversations(
|
|
184
|
-
self,
|
|
185
|
-
|
|
186
|
-
|
|
197
|
+
self,
|
|
198
|
+
unified_msg_origin: str | None = None,
|
|
199
|
+
platform_id: str | None = None,
|
|
200
|
+
) -> list[Conversation]:
|
|
201
|
+
"""获取对话列表.
|
|
187
202
|
|
|
188
203
|
Args:
|
|
189
204
|
unified_msg_origin (str): 统一的消息来源字符串。格式为 platform_name:message_type:session_id,可选
|
|
190
205
|
platform_id (str): 平台 ID, 可选参数, 用于过滤对话
|
|
191
206
|
Returns:
|
|
192
207
|
conversations (List[Conversation]): 对话对象列表
|
|
208
|
+
|
|
193
209
|
"""
|
|
194
210
|
convs = await self.db.get_conversations(
|
|
195
|
-
user_id=unified_msg_origin,
|
|
211
|
+
user_id=unified_msg_origin,
|
|
212
|
+
platform_id=platform_id,
|
|
196
213
|
)
|
|
197
214
|
convs_res = []
|
|
198
215
|
for conv in convs:
|
|
@@ -208,7 +225,7 @@ class ConversationManager:
|
|
|
208
225
|
search_query: str = "",
|
|
209
226
|
**kwargs,
|
|
210
227
|
) -> tuple[list[Conversation], int]:
|
|
211
|
-
"""
|
|
228
|
+
"""获取过滤后的对话列表.
|
|
212
229
|
|
|
213
230
|
Args:
|
|
214
231
|
page (int): 页码, 默认为 1
|
|
@@ -217,6 +234,7 @@ class ConversationManager:
|
|
|
217
234
|
search_query (str): 搜索查询字符串, 可选
|
|
218
235
|
Returns:
|
|
219
236
|
conversations (list[Conversation]): 对话对象列表
|
|
237
|
+
|
|
220
238
|
"""
|
|
221
239
|
convs, cnt = await self.db.get_filtered_conversations(
|
|
222
240
|
page=page,
|
|
@@ -238,13 +256,14 @@ class ConversationManager:
|
|
|
238
256
|
history: list[dict] | None = None,
|
|
239
257
|
title: str | None = None,
|
|
240
258
|
persona_id: str | None = None,
|
|
241
|
-
):
|
|
242
|
-
"""
|
|
259
|
+
) -> None:
|
|
260
|
+
"""更新会话的对话.
|
|
243
261
|
|
|
244
262
|
Args:
|
|
245
263
|
unified_msg_origin (str): 统一的消息来源字符串。格式为 platform_name:message_type:session_id
|
|
246
264
|
conversation_id (str): 对话 ID, 是 uuid 格式的字符串
|
|
247
265
|
history (List[Dict]): 对话历史记录, 是一个字典列表, 每个字典包含 role 和 content 字段
|
|
266
|
+
|
|
248
267
|
"""
|
|
249
268
|
if not conversation_id:
|
|
250
269
|
# 如果没有提供 conversation_id,则获取当前的
|
|
@@ -258,16 +277,20 @@ class ConversationManager:
|
|
|
258
277
|
)
|
|
259
278
|
|
|
260
279
|
async def update_conversation_title(
|
|
261
|
-
self,
|
|
262
|
-
|
|
263
|
-
|
|
280
|
+
self,
|
|
281
|
+
unified_msg_origin: str,
|
|
282
|
+
title: str,
|
|
283
|
+
conversation_id: str | None = None,
|
|
284
|
+
) -> None:
|
|
285
|
+
"""更新会话的对话标题.
|
|
264
286
|
|
|
265
287
|
Args:
|
|
266
288
|
unified_msg_origin (str): 统一的消息来源字符串。格式为 platform_name:message_type:session_id
|
|
267
289
|
title (str): 对话标题
|
|
268
|
-
|
|
290
|
+
conversation_id (str): 对话 ID, 是 uuid 格式的字符串
|
|
269
291
|
Deprecated:
|
|
270
292
|
Use `update_conversation` with `title` parameter instead.
|
|
293
|
+
|
|
271
294
|
"""
|
|
272
295
|
await self.update_conversation(
|
|
273
296
|
unified_msg_origin=unified_msg_origin,
|
|
@@ -280,15 +303,16 @@ class ConversationManager:
|
|
|
280
303
|
unified_msg_origin: str,
|
|
281
304
|
persona_id: str,
|
|
282
305
|
conversation_id: str | None = None,
|
|
283
|
-
):
|
|
284
|
-
"""更新会话的对话 Persona ID
|
|
306
|
+
) -> None:
|
|
307
|
+
"""更新会话的对话 Persona ID.
|
|
285
308
|
|
|
286
309
|
Args:
|
|
287
310
|
unified_msg_origin (str): 统一的消息来源字符串。格式为 platform_name:message_type:session_id
|
|
288
311
|
persona_id (str): 对话 Persona ID
|
|
289
|
-
|
|
312
|
+
conversation_id (str): 对话 ID, 是 uuid 格式的字符串
|
|
290
313
|
Deprecated:
|
|
291
314
|
Use `update_conversation` with `persona_id` parameter instead.
|
|
315
|
+
|
|
292
316
|
"""
|
|
293
317
|
await self.update_conversation(
|
|
294
318
|
unified_msg_origin=unified_msg_origin,
|
|
@@ -296,40 +320,85 @@ class ConversationManager:
|
|
|
296
320
|
persona_id=persona_id,
|
|
297
321
|
)
|
|
298
322
|
|
|
323
|
+
async def add_message_pair(
|
|
324
|
+
self,
|
|
325
|
+
cid: str,
|
|
326
|
+
user_message: UserMessageSegment | dict,
|
|
327
|
+
assistant_message: AssistantMessageSegment | dict,
|
|
328
|
+
) -> None:
|
|
329
|
+
"""Add a user-assistant message pair to the conversation history.
|
|
330
|
+
|
|
331
|
+
Args:
|
|
332
|
+
cid (str): Conversation ID
|
|
333
|
+
user_message (UserMessageSegment | dict): OpenAI-format user message object or dict
|
|
334
|
+
assistant_message (AssistantMessageSegment | dict): OpenAI-format assistant message object or dict
|
|
335
|
+
|
|
336
|
+
Raises:
|
|
337
|
+
Exception: If the conversation with the given ID is not found
|
|
338
|
+
"""
|
|
339
|
+
conv = await self.db.get_conversation_by_id(cid=cid)
|
|
340
|
+
if not conv:
|
|
341
|
+
raise Exception(f"Conversation with id {cid} not found")
|
|
342
|
+
history = conv.content or []
|
|
343
|
+
if isinstance(user_message, UserMessageSegment):
|
|
344
|
+
user_msg_dict = user_message.model_dump()
|
|
345
|
+
else:
|
|
346
|
+
user_msg_dict = user_message
|
|
347
|
+
if isinstance(assistant_message, AssistantMessageSegment):
|
|
348
|
+
assistant_msg_dict = assistant_message.model_dump()
|
|
349
|
+
else:
|
|
350
|
+
assistant_msg_dict = assistant_message
|
|
351
|
+
history.append(user_msg_dict)
|
|
352
|
+
history.append(assistant_msg_dict)
|
|
353
|
+
await self.db.update_conversation(
|
|
354
|
+
cid=cid,
|
|
355
|
+
content=history,
|
|
356
|
+
)
|
|
357
|
+
|
|
299
358
|
async def get_human_readable_context(
|
|
300
|
-
self,
|
|
301
|
-
|
|
302
|
-
|
|
359
|
+
self,
|
|
360
|
+
unified_msg_origin: str,
|
|
361
|
+
conversation_id: str,
|
|
362
|
+
page: int = 1,
|
|
363
|
+
page_size: int = 10,
|
|
364
|
+
) -> tuple[list[str], int]:
|
|
365
|
+
"""获取人类可读的上下文.
|
|
303
366
|
|
|
304
367
|
Args:
|
|
305
368
|
unified_msg_origin (str): 统一的消息来源字符串。格式为 platform_name:message_type:session_id
|
|
306
369
|
conversation_id (str): 对话 ID, 是 uuid 格式的字符串
|
|
307
370
|
page (int): 页码
|
|
308
371
|
page_size (int): 每页大小
|
|
372
|
+
|
|
309
373
|
"""
|
|
310
374
|
conversation = await self.get_conversation(unified_msg_origin, conversation_id)
|
|
375
|
+
if not conversation:
|
|
376
|
+
return [], 0
|
|
311
377
|
history = json.loads(conversation.history)
|
|
312
378
|
|
|
313
|
-
|
|
314
|
-
|
|
379
|
+
# contexts_groups 存放按顺序的段落(每个段落是一个 str 列表),
|
|
380
|
+
# 之后会被展平成一个扁平的 str 列表返回。
|
|
381
|
+
contexts_groups: list[list[str]] = []
|
|
382
|
+
temp_contexts: list[str] = []
|
|
315
383
|
for record in history:
|
|
316
384
|
if record["role"] == "user":
|
|
317
385
|
temp_contexts.append(f"User: {record['content']}")
|
|
318
386
|
elif record["role"] == "assistant":
|
|
319
|
-
if
|
|
387
|
+
if record.get("content"):
|
|
320
388
|
temp_contexts.append(f"Assistant: {record['content']}")
|
|
321
389
|
elif "tool_calls" in record:
|
|
322
390
|
tool_calls_str = json.dumps(
|
|
323
|
-
record["tool_calls"],
|
|
391
|
+
record["tool_calls"],
|
|
392
|
+
ensure_ascii=False,
|
|
324
393
|
)
|
|
325
394
|
temp_contexts.append(f"Assistant: [函数调用] {tool_calls_str}")
|
|
326
395
|
else:
|
|
327
396
|
temp_contexts.append("Assistant: [未知的内容]")
|
|
328
|
-
|
|
397
|
+
contexts_groups.insert(0, temp_contexts)
|
|
329
398
|
temp_contexts = []
|
|
330
399
|
|
|
331
|
-
#
|
|
332
|
-
contexts = [item for sublist in
|
|
400
|
+
# 展平分组后的 contexts 列表为单层字符串列表
|
|
401
|
+
contexts = [item for sublist in contexts_groups for item in sublist]
|
|
333
402
|
|
|
334
403
|
# 计算分页
|
|
335
404
|
paged_contexts = contexts[(page - 1) * page_size : page * page_size]
|