"""Fix chat JS: replace the problematic /s regex flag with safe alternative."""
path = "/home/ubuntu/ai-chat/app.py"
with open(path) as f:
content = f.read()
# Replace the problematic regex with /s flag
old = """s=s.replace(/(
.*<\\/li>)/s,'');"""
new = """s=s.replace(/([^]*?<\\/li>)/g,function(m){return '';});"""
if old in content:
content = content.replace(old, new)
print("Fixed /s flag regex")
else:
print("Pattern not found, checking alternatives...")
# Try unescaped version
old2 = "s=s.replace(/(.*<\\/li>)/s,'');"
if old2 in content:
content = content.replace(old2, new)
print("Fixed (alt)")
with open(path, "w") as f:
f.write(content)
# Verify: count the _md function
count = content.count("function _md(s)")
print(f"_md definitions: {count}")