import json import re import subprocess from pathlib import Path tthc_data = json.loads(Path("scripts/tthc_catalog.json").read_text(encoding="utf-8")) vbpl_data = json.loads(Path("scripts/vbpl_catalog.json").read_text(encoding="utf-8")) CF_URL = "https://cookie-tea-larger-microwave.trycloudflare.com" inject_code = Path("scripts/inject_fast_kb.py").read_text(encoding="utf-8") start_marker = 'NEW_KB_JS = f"""' end_marker = '"""\n\ndef update_file' s_idx = inject_code.find(start_marker) + len(start_marker) e_idx = inject_code.find(end_marker) raw_template = inject_code[s_idx:e_idx] # Replace placeholders FIRST raw_template = raw_template.replace("{CF_URL}", "___CF_URL___") raw_template = raw_template.replace("{json.dumps(tthc_data, ensure_ascii=False)}", "___TTHC_DATA___") raw_template = raw_template.replace("{json.dumps(vbpl_data, ensure_ascii=False)}", "___VBPL_DATA___") # Now unescape double braces {{ -> { and }} -> } raw_template = raw_template.replace("{{", "{").replace("}}", "}") # Now put back values raw_template = raw_template.replace("___CF_URL___", CF_URL) raw_template = raw_template.replace("___TTHC_DATA___", json.dumps(tthc_data, ensure_ascii=False)) raw_template = raw_template.replace("___VBPL_DATA___", json.dumps(vbpl_data, ensure_ascii=False)) for fn in ["src/inference/static/index.html", "static_space/index.html"]: content = Path(fn).read_text(encoding="utf-8") idx = content.find("/* ==========================================================================\n CƠ SỞ TRI THỨC") if idx != -1: prefix = content[:idx] event_marker = '// Gắn sự kiện cho các ô tìm kiếm Knowledge Base' ev_idx = content.find(event_marker) if ev_idx != -1: events_part = content[ev_idx:] content = prefix + "\n" + raw_template + "\n" + events_part else: content = prefix + "\n" + raw_template + "\n\n\n\n" Path(fn).write_text(content, encoding="utf-8") print(f"Successfully cleaned and updated {fn}") # Node check html = Path("static_space/index.html").read_text(encoding="utf-8") scripts = re.findall(r"", html, re.DOTALL) for i, s in enumerate(scripts): Path(f"/tmp/check_{i}.js").write_text(s, encoding="utf-8") c = subprocess.run(["node", "--check", f"/tmp/check_{i}.js"], capture_output=True, text=True) if c.returncode == 0: print(f"Node Check: Script {i} is 100% VALID JAVASCRIPT!") else: print(f"Node Check Error:\n{c.stderr}")