chore: harden runtime checks and split websocket inbound/outbound flows

This commit is contained in:
2026-03-02 18:17:09 +08:00
parent 89eb94486d
commit e1ce17f2aa
7 changed files with 481 additions and 23 deletions

View File

@@ -4,6 +4,8 @@ AI Agent 对话测试脚本
"""
import sqlite3
import asyncio
import sys
from pathlib import Path
from datetime import datetime
# 颜色代码
@@ -18,14 +20,29 @@ COLORS = {
'reset': '\033[0m',
}
# Windows PowerShell defaults to GBK in some environments.
# Make stdout/stderr robust for Unicode logs used by this test script.
for stream_name in ("stdout", "stderr"):
stream = getattr(sys, stream_name, None)
if stream and hasattr(stream, "reconfigure"):
try:
stream.reconfigure(encoding="utf-8", errors="replace")
except Exception:
pass
# Ensure project root is importable when running as `uv run tests/test_ai_chat.py`.
PROJECT_ROOT = str(Path(__file__).resolve().parent.parent)
if PROJECT_ROOT not in sys.path:
sys.path.insert(0, PROJECT_ROOT)
DB_PATH = Path(PROJECT_ROOT) / "db" / "chat_log_db" / "chats.db"
def cprint(text, color='reset'):
print(f"{COLORS.get(color, '')}{text}{COLORS['reset']}")
def check_database():
"""检查数据库内容"""
db_path = 'db/chat_log_db/chats.db'
try:
conn = sqlite3.connect(db_path)
conn = sqlite3.connect(DB_PATH)
cursor = conn.execute("SELECT COUNT(*) FROM chat_logs")
count = cursor.fetchone()[0]
@@ -65,7 +82,7 @@ async def test_customer_conversation(customer_id, customer_name, limit=5):
cprint(f"{'='*70}\n", 'cyan')
# 获取对话记录
conn = sqlite3.connect('db/chat_log_db/chats.db')
conn = sqlite3.connect(DB_PATH)
cursor = conn.execute("""
SELECT direction, message, timestamp
FROM chat_logs
@@ -157,7 +174,7 @@ async def test_all_customers(customers, limit_per_customer=5):
continue
# 获取对话记录
conn = sqlite3.connect('db/chat_log_db/chats.db')
conn = sqlite3.connect(DB_PATH)
cursor = conn.execute("""
SELECT direction, message, timestamp
FROM chat_logs