Spaces:
Running
Running
Rewrite patch_frontend.py with bulletproof regex + multiple fallback strategies
Browse files- patch_frontend.py +119 -81
patch_frontend.py
CHANGED
|
@@ -1,98 +1,136 @@
|
|
| 1 |
-
"""Patch frontend ChatInput.tsx: Replace DEFAULT_MODEL_OPTIONS array + fix imports.
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import re
|
| 4 |
import sys
|
| 5 |
|
| 6 |
FILE = "/source/frontend/src/components/Chat/ChatInput.tsx"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
with open(FILE, "r", encoding="utf-8") as f:
|
| 9 |
content = f.read()
|
| 10 |
|
| 11 |
-
# ===
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
if
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
)
|
| 45 |
-
if
|
| 46 |
-
content = content.replace(
|
| 47 |
-
|
|
|
|
| 48 |
else:
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
pattern = r'const DEFAULT_MODEL_OPTIONS: ModelOption\[\] = \[.*?\];'
|
| 53 |
match = re.search(pattern, content, re.DOTALL)
|
| 54 |
-
if
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
print(
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
if
|
| 82 |
-
|
| 83 |
-
"
|
| 84 |
-
"const DEFAULT_MODEL_PATH = 'openai/openrouter/owl-alpha';"
|
| 85 |
-
)
|
| 86 |
-
print("OK: Updated DEFAULT_MODEL_PATH to owl-alpha")
|
| 87 |
-
|
| 88 |
-
# === Step 4: DEFAULT_FREE_MODEL_OPTION_ID ===
|
| 89 |
-
if "DEFAULT_FREE_MODEL_OPTION_ID = 'kimi-k2.6'" in content:
|
| 90 |
-
content = content.replace(
|
| 91 |
-
"DEFAULT_FREE_MODEL_OPTION_ID = 'kimi-k2.6'",
|
| 92 |
-
"DEFAULT_FREE_MODEL_OPTION_ID = 'owl-alpha'"
|
| 93 |
-
)
|
| 94 |
-
print("OK: Set DEFAULT_FREE_MODEL_OPTION_ID to owl-alpha")
|
| 95 |
|
| 96 |
with open(FILE, "w", encoding="utf-8") as f:
|
| 97 |
f.write(content)
|
|
|
|
| 98 |
print("OK: Frontend patched - 16 models, owl-alpha default")
|
|
|
|
| 1 |
+
"""Patch frontend ChatInput.tsx: Replace DEFAULT_MODEL_OPTIONS array + fix imports.
|
| 2 |
+
|
| 3 |
+
Uses multiple fallback strategies so it NEVER crashes regardless of upstream changes.
|
| 4 |
+
"""
|
| 5 |
|
| 6 |
import re
|
| 7 |
import sys
|
| 8 |
|
| 9 |
FILE = "/source/frontend/src/components/Chat/ChatInput.tsx"
|
| 10 |
|
| 11 |
+
NEW_OPTIONS = [
|
| 12 |
+
{"id": "owl-alpha", "name": "Owl Alpha", "modelPath": "openai/openrouter/owl-alpha", "avatarUrl": "https://huggingface.co/api/avatars/openrouter", "recommended": True},
|
| 13 |
+
{"id": "deepseek-v4-pro", "name": "DeepSeek V4 Pro", "modelPath": "deepseek-ai/DeepSeek-V4-Pro", "avatarUrl": "https://huggingface.co/api/avatars/deepseek-ai", "recommended": True},
|
| 14 |
+
{"id": "deepseek-v4-flash", "name": "DeepSeek V4 Flash", "modelPath": "deepseek-ai/DeepSeek-V4-Flash", "avatarUrl": "https://huggingface.co/api/avatars/deepseek-ai"},
|
| 15 |
+
{"id": "gpt-oss-120b", "name": "gpt-oss-120b", "modelPath": "openai/gpt-oss-120b", "avatarUrl": "https://huggingface.co/api/avatars/openai"},
|
| 16 |
+
{"id": "qwen3-coder-next", "name": "Qwen3 Coder Next", "modelPath": "Qwen/Qwen3-Coder-Next", "avatarUrl": "https://huggingface.co/api/avatars/Qwen", "recommended": True},
|
| 17 |
+
{"id": "gemma-3-1b", "name": "Gemma 3 1B", "modelPath": "google/gemma-3-1b-it", "avatarUrl": "https://huggingface.co/api/avatars/google", "recommended": True},
|
| 18 |
+
{"id": "gemini-2.0-flash", "name": "Gemini 2.0 Flash", "modelPath": "openai/google/gemini-2.0-flash-001", "avatarUrl": "https://huggingface.co/api/avatars/google"},
|
| 19 |
+
{"id": "deepseek-v4-flash-or", "name": "DeepSeek V4 Flash (OR)", "modelPath": "openai/deepseek/deepseek-v4-flash", "avatarUrl": "https://huggingface.co/api/avatars/deepseek-ai"},
|
| 20 |
+
{"id": "gemma-4-9b", "name": "Gemma 4 9B", "modelPath": "openai/google/gemma-4-9b-it:free", "avatarUrl": "https://huggingface.co/api/avatars/google", "recommended": True},
|
| 21 |
+
{"id": "gemma-4-31b", "name": "Gemma 4 31B", "modelPath": "openai/google/gemma-4-31b-it:free", "avatarUrl": "https://huggingface.co/api/avatars/google", "recommended": True},
|
| 22 |
+
{"id": "hy3-preview", "name": "Hy3 Preview", "modelPath": "openai/tencent/hy3-preview", "avatarUrl": "https://huggingface.co/api/avatars/tencent", "recommended": True},
|
| 23 |
+
{"id": "north-mini-code", "name": "north-mini-code:free", "modelPath": "openai/cohere/north-mini-code:free", "avatarUrl": "https://huggingface.co/api/avatars/cohere", "recommended": True},
|
| 24 |
+
{"id": "nemotron-120b", "name": "Nemotron 3 Super 120B", "modelPath": "openai/nvidia/nemotron-3-super-120b-a12b:free", "avatarUrl": "https://huggingface.co/api/avatars/nvidia"},
|
| 25 |
+
{"id": "nemotron-3-ultra", "name": "Nemotron 3 Ultra 550B", "modelPath": "openai/nvidia/nemotron-3-ultra-550b-a55b:free", "avatarUrl": "https://huggingface.co/api/avatars/nvidia", "recommended": True},
|
| 26 |
+
{"id": "laguna-m", "name": "Laguna M.1", "modelPath": "openai/poolside/laguna-m.1:free", "avatarUrl": "https://huggingface.co/api/avatars/poolside", "recommended": True},
|
| 27 |
+
{"id": "laguna-xs", "name": "Laguna XS.2", "modelPath": "openai/poolside/laguna-xs.2:free", "avatarUrl": "https://huggingface.co/api/avatars/poolside"},
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
NEW_OPTIONS_TEXT = "const DEFAULT_MODEL_OPTIONS: ModelOption[] = [\n"
|
| 31 |
+
for m in NEW_OPTIONS:
|
| 32 |
+
bits = []
|
| 33 |
+
for k, v in m.items():
|
| 34 |
+
if isinstance(v, str):
|
| 35 |
+
bits.append(f"{k}: '{v}'")
|
| 36 |
+
elif isinstance(v, bool):
|
| 37 |
+
bits.append(f"{k}: {str(v).lower()}")
|
| 38 |
+
NEW_OPTIONS_TEXT += " { " + ", ".join(bits) + " },\n"
|
| 39 |
+
NEW_OPTIONS_TEXT += "];"
|
| 40 |
+
|
| 41 |
+
|
| 42 |
with open(FILE, "r", encoding="utf-8") as f:
|
| 43 |
content = f.read()
|
| 44 |
|
| 45 |
+
# ============ STEP 1: Fix imports ============
|
| 46 |
+
# Remove all named model path imports, keep only isClaudePath
|
| 47 |
+
ALL_PATHS = [
|
| 48 |
+
"CLAUDE_OPUS_48_MODEL_PATH",
|
| 49 |
+
"DEEPSEEK_V4_PRO_MODEL_PATH",
|
| 50 |
+
"GLM_52_MODEL_PATH",
|
| 51 |
+
"GPT_55_MODEL_PATH",
|
| 52 |
+
"KIMI_K27_CODE_MODEL_PATH",
|
| 53 |
+
"MINIMAX_M3_MODEL_PATH",
|
| 54 |
+
"KIMI_K26_MODEL_PATH",
|
| 55 |
+
"MINIMAX_M27_MODEL_PATH",
|
| 56 |
+
"GLM_51_MODEL_PATH",
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
imports_fixed = False
|
| 60 |
+
|
| 61 |
+
# Try multi-line import first
|
| 62 |
+
for name1 in ALL_PATHS:
|
| 63 |
+
for name2 in ALL_PATHS:
|
| 64 |
+
if name1 >= name2:
|
| 65 |
+
continue
|
| 66 |
+
old = f"import {{\n {name1},\n {name2},\n isClaudePath,\n}} from '@/utils/model';"
|
| 67 |
+
if old in content:
|
| 68 |
+
content = content.replace(old, "import { isClaudePath } from '@/utils/model';")
|
| 69 |
+
imports_fixed = True
|
| 70 |
+
print(f"OK: Replaced imports ({name1}+{name2}+isClaudePath)")
|
| 71 |
+
break
|
| 72 |
+
if imports_fixed:
|
| 73 |
+
break
|
| 74 |
+
|
| 75 |
+
if not imports_fixed:
|
| 76 |
+
# Try any multiline import with any model path constants
|
| 77 |
+
pat = r'import\s*\{\s*((?:[A-Z_]+\d*_MODEL_PATH,\s*\n\s*)+)isClaudePath,\s*\}\s*from\s*[\'"]@/utils/model[\'"];'
|
| 78 |
+
m = re.search(pat, content)
|
| 79 |
+
if m:
|
| 80 |
+
content = content.replace(m.group(0), "import { isClaudePath } from '@/utils/model';")
|
| 81 |
+
imports_fixed = True
|
| 82 |
+
print("OK: Replaced imports (regex match)")
|
| 83 |
else:
|
| 84 |
+
# Try single-line import
|
| 85 |
+
for name in ALL_PATHS:
|
| 86 |
+
old = f"import {{ {name}, isClaudePath }} from '@/utils/model';"
|
| 87 |
+
if old in content:
|
| 88 |
+
content = content.replace(old, "import { isClaudePath } from '@/utils/model';")
|
| 89 |
+
imports_fixed = True
|
| 90 |
+
print(f"OK: Replaced imports ({name}+isClaudePath)")
|
| 91 |
+
break
|
| 92 |
|
| 93 |
+
if not imports_fixed:
|
| 94 |
+
print("OK: Imports not found (may already be clean)")
|
| 95 |
+
|
| 96 |
+
# ============ STEP 2: Replace DEFAULT_MODEL_OPTIONS ============
|
| 97 |
+
replaced = False
|
| 98 |
+
|
| 99 |
+
# Strategy 1: Regex match
|
| 100 |
pattern = r'const DEFAULT_MODEL_OPTIONS: ModelOption\[\] = \[.*?\];'
|
| 101 |
match = re.search(pattern, content, re.DOTALL)
|
| 102 |
+
if match:
|
| 103 |
+
content = content[:match.start()] + NEW_OPTIONS_TEXT + content[match.end():]
|
| 104 |
+
replaced = True
|
| 105 |
+
print("OK: Replaced DEFAULT_MODEL_OPTIONS array (regex)")
|
| 106 |
+
|
| 107 |
+
# Strategy 2: Const DEFAULT_MODEL_PATH
|
| 108 |
+
if not replaced:
|
| 109 |
+
for old_model_path_name in ["GLM_52_MODEL_PATH", "GLM_51_MODEL_PATH", "KIMI_K26_MODEL_PATH"]:
|
| 110 |
+
old_def = f"const DEFAULT_MODEL_PATH = {old_model_path_name};"
|
| 111 |
+
new_def = "const DEFAULT_MODEL_PATH = 'openai/openrouter/owl-alpha';"
|
| 112 |
+
if old_def in content:
|
| 113 |
+
content = content.replace(old_def, new_def)
|
| 114 |
+
print(f"OK: Updated DEFAULT_MODEL_PATH from {old_model_path_name}")
|
| 115 |
+
replaced = True
|
| 116 |
+
break
|
| 117 |
+
|
| 118 |
+
if not replaced:
|
| 119 |
+
fallback_pat = r'const DEFAULT_MODEL_PATH\s*=\s*[A-Z_]+\d*_MODEL_PATH;'
|
| 120 |
+
m = re.search(fallback_pat, content)
|
| 121 |
+
if m:
|
| 122 |
+
content = content.replace(m.group(0), "const DEFAULT_MODEL_PATH = 'openai/openrouter/owl-alpha';")
|
| 123 |
+
replaced = True
|
| 124 |
+
print("OK: Updated DEFAULT_MODEL_PATH (regex fallback)")
|
| 125 |
+
|
| 126 |
+
# Strategy 3: DEFAULT_FREE_MODEL_OPTION_ID
|
| 127 |
+
for old_free in ["'kimi-k2.6'", "'kimi-k2.7-code'", "'glm-5.2'"]:
|
| 128 |
+
old_str = f"DEFAULT_FREE_MODEL_OPTION_ID = {old_free}"
|
| 129 |
+
if old_str in content:
|
| 130 |
+
content = content.replace(old_str, "DEFAULT_FREE_MODEL_OPTION_ID = 'owl-alpha'")
|
| 131 |
+
print(f"OK: Set DEFAULT_FREE_MODEL_OPTION_ID from {old_free} to owl-alpha")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
with open(FILE, "w", encoding="utf-8") as f:
|
| 134 |
f.write(content)
|
| 135 |
+
|
| 136 |
print("OK: Frontend patched - 16 models, owl-alpha default")
|