Spaces:
Paused

File size: 2,103 Bytes
f8553c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

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()