#!/usr/bin/env python # -*- coding: utf-8 -*- """ Normalize legacy built-in agent records to Chinese. Usage: python scripts/localize_builtin_agents.py python scripts/localize_builtin_agents.py --apply """ import argparse import asyncio import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent.parent)) from sqlalchemy import select from app.database import AsyncSessionLocal from ai_platform.models import Agent from ai_platform.services.agent_localization import normalize_builtin_agent_payload def _agent_payload(agent: Agent) -> dict: return { "name": agent.name, "code": agent.code, "description": agent.description or "", "persona": agent.persona or {}, } async def localize_builtin_agents(apply: bool) -> None: async with AsyncSessionLocal() as db: result = await db.execute( select(Agent).where( Agent.is_deleted == False, ) ) agents = result.scalars().all() changed = [] for agent in agents: original = _agent_payload(agent) normalized = normalize_builtin_agent_payload(original) if normalized == original: continue changed.append((agent, original, normalized)) print(f"{agent.code}: {original['name']} -> {normalized['name']}") if apply: agent.name = normalized["name"] agent.description = normalized["description"] agent.persona = normalized.get("persona") or {} if apply: await db.commit() print(f"已更新 {len(changed)} 条内置智能体记录") else: await db.rollback() print(f"dry-run: 将更新 {len(changed)} 条记录,确认后加 --apply 执行") def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--apply", action="store_true", help="write changes to database") args = parser.parse_args() asyncio.run(localize_builtin_agents(args.apply)) if __name__ == "__main__": main()