import re import json def convert(): ts_file = "/Users/liwei/pri/venv/MeloonAITools/frontend/constants.ts" py_file = "/Users/liwei/pri/venv/MeloonAITools/backend/app/data/initial_tools.py" with open(ts_file, 'r') as f: content = f.read() # Extract the INITIAL_TOOLS array match = re.search(r'export const INITIAL_TOOLS: Tool\[\] = \[(.*?)\];', content, re.DOTALL) if not match: print("Could not find INITIAL_TOOLS in constants.ts") return tools_str = match.group(1) # Simple conversion logic: # 1. Replace Category.XXX with "xxx" # 2. Replace Date.now() with _now() # 3. Handle trailing commas if any (Python is fine with them) # 4. Convert JS objects to Python dicts (mostly a matter of quote handling) # Mapping Category to string categories = { "Category.CHATBOT": "'chatbot'", "Category.IMAGE_GEN": "'image_gen'", "Category.VIDEO_GEN": "'video_gen'", "Category.CODING": "'coding'", "Category.WRITING": "'writing'", "Category.AUDIO": "'audio'", "Category.PRODUCTIVITY": "'productivity'", "Category.GAME": "'game'", "Category.PROMPT": "'prompt'", "Category.OTHER": "'other'" } for k, v in categories.items(): tools_str = tools_str.replace(k, v) tools_str = tools_str.replace("Date.now()", "_now()") # Remove TypeScript comments (only if at start of line or following whitespace) # But be careful not to match // in https:// tools_str = re.sub(r'(^|\s+)//.*', r'\1', tools_str) # Convert keys to quoted strings # Match words at the start of a line or after a comma/brace followed by a colon tools_str = re.sub(r'([{,]\s*)([a-zA-Z0-9_]+):', r'\1"\2":', tools_str) output = f"""import time def _now(): return int(time.time() * 1000) INITIAL_TOOLS_DATA = [{tools_str} ] """ with open(py_file, 'w') as f: f.write(output) print(f"Successfully converted and wrote to {py_file}") if __name__ == "__main__": convert()