Files
aiagent/backend/scripts/patch_zhini_code_build_context_scope.py

52 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""修复 code-build-context 中列表推导式 r 与前面 genexp 的 r 作用域冲突(就地更新数据库 Agent"""
from __future__ import annotations
import os
import sys
BACKEND = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, BACKEND)
from sqlalchemy.orm.attributes import flag_modified
from app.core.database import SessionLocal
from app.models.agent import Agent
OLD_VEC = "vec_str = '\\n'.join((r.get('text') or r.get('content') or '') for r in right)"
NEW_VEC = "vec_str = '\\n'.join((rec.get('text') or rec.get('content') or '') for rec in right)"
OLD_KW = 'kw_lines = [f"{r}: {t}" for _, r, t in scored[:6]]'
NEW_KW = 'kw_lines = [f"{role}: {text}" for _, role, text in scored[:6]]'
def main() -> int:
name = os.environ.get("PATCH_AGENT_NAME", "知你客服11号")
db = SessionLocal()
try:
a = db.query(Agent).filter(Agent.name == name).first()
if not a:
print("未找到", name, file=sys.stderr)
return 1
wf = a.workflow_config
for n in wf.get("nodes", []):
if n.get("id") != "code-build-context":
continue
c = n.get("data", {}).get("code", "")
c2 = c.replace(OLD_VEC, NEW_VEC).replace(OLD_KW, NEW_KW)
if c2 == c:
print("无需替换(可能已修复或内容不同)")
return 0
n.setdefault("data", {})["code"] = c2
a.workflow_config = wf
flag_modified(a, "workflow_config")
db.commit()
print("已更新", name, "code-build-context")
return 0
print("未找到 code-build-context 节点", file=sys.stderr)
return 1
finally:
db.close()
if __name__ == "__main__":
raise SystemExit(main())