Files
group_xinghuo_jinrong/scripts/init_agent_db.py
T
zhanghongyu_0626 b841f68295 feat(visitor): Implement visitor chat functionality and enhance customer service interactions
- Added a new visitor chat API endpoint (`/api/chat/visitor`) to allow unauthenticated users to engage in conversations without requiring customer data.
- Introduced a visitor context dependency to manage visitor interactions seamlessly.
- Enhanced the chat API to support explicit session termination and improved response handling for customer service interactions.
- Updated the database configuration to include Redis client support for caching visitor data.
- Added a new customer note repository to persist user notes independently of the L1 profile slots.

This update significantly improves the customer service experience by enabling visitor interactions and ensuring efficient data handling for both registered and unregistered users.
2026-09-09 18:32:00 +08:00

96 lines
3.5 KiB
Python

"""初始化 jinrong_agent 库:依次执行 01-共用底座 + 02-agent专用 两份 DDL。
用法(密码与 core/reset.ps1 同一套,二选一):
# 方式一:环境变量(避免命令行明文)
$env:MYSQL_PWD = "你的密码"; python scripts/init_agent_db.py
# 方式二:命令行传参
python scripts/init_agent_db.py --mysql-password 123456
# 可选参数
python scripts/init_agent_db.py --mysql-host 127.0.0.1 --mysql-port 3306 --mysql-user root
说明:
- 全新机器首次执行即可建齐:底座 11 张(01)+ Agent 专用 7 张(02);
- CREATE DATABASE 带 IF NOT EXISTS;表已存在时 MySQL 报 1050,脚本提示后安全跳过,
不会 DROP 任何数据(如需彻底重建,请手动 DROP DATABASE jinrong_agent 后重跑);
- 游客审计依赖 jinrong_agent.audit_log(在 01 中),客服归档/备注依赖 02 中的表。
"""
from __future__ import annotations
import argparse
import os
import sys
from pathlib import Path
import pymysql
from pymysql.constants import CLIENT
from pymysql.err import OperationalError
ROOT = Path(__file__).resolve().parent.parent
SQL_FILES = [
("01-共用底座", ROOT / "docs/项目框架设计/表设计/01-mysql-共用底座.sql"),
("02-agent专用", ROOT / "docs/项目框架设计/表设计/02-mysql-agent专用.sql"),
]
ER_TABLE_EXISTS = 1050
def run_sql_file(cur, label: str, path: Path) -> None:
if not path.exists():
print(f"[ERROR] SQL 文件不存在: {path}")
sys.exit(1)
print(f">> 执行 {label}: {path.name}")
sql = path.read_text(encoding="utf-8")
try:
cur.execute(sql)
while cur.nextset():
pass
print(f" {label} 执行完成")
except OperationalError as e:
# 1050 = 表已存在:非全新环境重跑的预期情况,安全跳过,不破坏数据
if e.args and e.args[0] == ER_TABLE_EXISTS:
print(f" [SKIP] {label} 部分表已存在(1050),跳过已建表;如需重建请先 DROP DATABASE jinrong_agent")
else:
raise
def main() -> None:
parser = argparse.ArgumentParser(description="初始化 jinrong_agent 库(01 底座 + 02 Agent 专用)")
parser.add_argument("--mysql-host", default=os.getenv("MYSQL_HOST", "127.0.0.1"))
parser.add_argument("--mysql-port", type=int, default=int(os.getenv("MYSQL_PORT", "3306")))
parser.add_argument("--mysql-user", default=os.getenv("MYSQL_USER", "root"))
parser.add_argument("--mysql-password", default=os.getenv("MYSQL_PWD", ""),
help="MySQL 密码;也可用 MYSQL_PWD 环境变量")
args = parser.parse_args()
if not args.mysql_password:
print("[ERROR] 缺少密码:请用 --mysql-password 传参,或预设 MYSQL_PWD 环境变量")
sys.exit(1)
conn = pymysql.connect(
host=args.mysql_host,
port=args.mysql_port,
user=args.mysql_user,
password=args.mysql_password,
charset="utf8mb4",
client_flag=CLIENT.MULTI_STATEMENTS,
autocommit=True,
)
try:
with conn.cursor() as cur:
for label, path in SQL_FILES:
run_sql_file(cur, label, path)
cur.execute("USE jinrong_agent")
cur.execute("SHOW TABLES")
tables = [r[0] for r in cur.fetchall()]
print(f"\njinrong_agent 现有 {len(tables)} 张表:")
for t in tables:
print(f" - {t}")
finally:
conn.close()
if __name__ == "__main__":
main()