Skip to content

Commit 889feb9

Browse files
fix: fix: restore missing WebChat sessions (#9607)
1 parent 91ee487 commit 889feb9

3 files changed

Lines changed: 56 additions & 15 deletions

File tree

astrbot/core/db/migration/migra_webchat_session.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from sqlalchemy import func, select
1313
from sqlmodel import col
1414

15-
from astrbot.api import logger, sp
15+
from astrbot.api import logger
1616
from astrbot.core.db import BaseDatabase
1717
from astrbot.core.db.po import ConversationV2, PlatformMessageHistory, PlatformSession
1818

@@ -23,13 +23,6 @@ async def migrate_webchat_session(db_helper: BaseDatabase) -> None:
2323
This migration extracts all unique user_ids from platform_message_history
2424
where platform_id='webchat' and creates corresponding PlatformSession records.
2525
"""
26-
# 检查是否已经完成迁移
27-
migration_done = await db_helper.get_preference(
28-
"global", "global", "migration_done_webchat_session_1"
29-
)
30-
if migration_done:
31-
return
32-
3326
logger.info("开始执行数据库迁移(WebChat 会话迁移)...")
3427

3528
try:
@@ -52,9 +45,6 @@ async def migrate_webchat_session(db_helper: BaseDatabase) -> None:
5245

5346
if not webchat_users:
5447
logger.info("没有找到需要迁移的 WebChat 数据")
55-
await sp.put_async(
56-
"global", "global", "migration_done_webchat_session_1", True
57-
)
5848
return
5949

6050
logger.info(f"找到 {len(webchat_users)} 个 WebChat 会话需要迁移")
@@ -123,9 +113,6 @@ async def migrate_webchat_session(db_helper: BaseDatabase) -> None:
123113
else:
124114
logger.info("没有新会话需要迁移")
125115

126-
# 标记迁移完成
127-
await sp.put_async("global", "global", "migration_done_webchat_session_1", True)
128-
129116
except Exception as e:
130117
logger.error(f"迁移过程中发生错误: {e}", exc_info=True)
131118
raise

astrbot/dashboard/services/chat_service.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,25 @@ async def build_chat_stream(
11151115
"Message content is empty (reply only is not allowed)"
11161116
)
11171117

1118+
if platform_history_id == "webchat":
1119+
try:
1120+
platform_session = await self.db.get_platform_session_by_id(
1121+
webchat_conv_id
1122+
)
1123+
if platform_session is None:
1124+
await self.db.create_platform_session(
1125+
creator=username,
1126+
platform_id="webchat",
1127+
session_id=webchat_conv_id,
1128+
is_group=0,
1129+
)
1130+
except Exception as exc:
1131+
logger.warning(
1132+
"Failed to ensure WebChat platform session %s: %s",
1133+
webchat_conv_id,
1134+
exc,
1135+
)
1136+
11181137
message_id = str(uuid.uuid4())
11191138
llm_checkpoint_id = post_data.get("_llm_checkpoint_id") or str(uuid.uuid4())
11201139
skip_user_history = bool(post_data.get("_skip_user_history"))

tests/test_chat_route.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ def chat_service_instance(monkeypatch, tmp_path):
2727
platform_message_history_manager=platform_history_mgr,
2828
umop_config_router=Mock(),
2929
)
30-
service = ChatService(Mock(), core_lifecycle)
30+
db = Mock()
31+
db.get_platform_session_by_id = AsyncMock(
32+
return_value=SimpleNamespace(session_id="existing-session")
33+
)
34+
db.create_platform_session = AsyncMock()
35+
service = ChatService(db, core_lifecycle)
3136
service.build_user_message_parts = AsyncMock(
3237
return_value=[{"type": "plain", "text": "hello"}]
3338
)
@@ -40,6 +45,36 @@ def chat_service_instance(monkeypatch, tmp_path):
4045
return service
4146

4247

48+
@pytest.mark.asyncio
49+
async def test_chat_stream_creates_missing_webchat_platform_session(
50+
chat_service_instance,
51+
):
52+
service = chat_service_instance
53+
session_id = "missing-platform-session"
54+
service.db.get_platform_session_by_id.return_value = None
55+
56+
stream = await service.build_chat_stream(
57+
"alice",
58+
{"message": "hello", "session_id": session_id},
59+
)
60+
run = next(iter(service.chat_runs.values()))
61+
62+
try:
63+
service.db.get_platform_session_by_id.assert_awaited_once_with(session_id)
64+
service.db.create_platform_session.assert_awaited_once_with(
65+
creator="alice",
66+
platform_id="webchat",
67+
session_id=session_id,
68+
is_group=0,
69+
)
70+
finally:
71+
await stream.aclose()
72+
if run.task and not run.task.done():
73+
run.task.cancel()
74+
await asyncio.gather(run.task, return_exceptions=True)
75+
chat_service.webchat_queue_mgr.remove_queues(session_id)
76+
77+
4378
def _decode_sse_event(event: str) -> dict:
4479
"""Decode one JSON SSE event emitted by ChatService.
4580

0 commit comments

Comments
 (0)