bep40 commited on
Commit
74c83d8
·
verified ·
1 Parent(s): 01ab04f

Restore to commit 8db27ced — patch_frontend.py (old 4-constant import pattern)

Browse files
Files changed (1) hide show
  1. patch_frontend.py +135 -119
patch_frontend.py CHANGED
@@ -1,134 +1,150 @@
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)
 
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
+ # === Step 1: Fix imports ===
12
+ content = content.replace(
13
+ "import {\n"
14
+ " CLAUDE_OPUS_48_MODEL_PATH,\n"
15
+ " GPT_55_MODEL_PATH,\n"
16
+ " KIMI_K26_MODEL_PATH,\n"
17
+ " isClaudePath,\n"
18
+ "} from '@/utils/model';",
19
+ "import { isClaudePath } from '@/utils/model';"
20
+ )
21
+
22
+ # === Step 2: Replace DEFAULT_MODEL_OPTIONS array using regex ===
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  pattern = r'const DEFAULT_MODEL_OPTIONS: ModelOption\[\] = \[.*?\];'
24
  match = re.search(pattern, content, re.DOTALL)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ if not match:
27
+ print("FAIL: Could not find DEFAULT_MODEL_OPTIONS array!")
28
+ sys.exit(1)
29
+
30
+ print(f"OK: Found array at pos {match.start()}-{match.end()}")
31
+
32
+ new_array = """const DEFAULT_MODEL_OPTIONS: ModelOption[] = [
33
+ {
34
+ id: 'owl-alpha',
35
+ name: 'Owl Alpha',
36
+ modelPath: 'openai/openrouter/owl-alpha',
37
+ avatarUrl: 'https://huggingface.co/api/avatars/openrouter',
38
+ recommended: true,
39
+ },
40
+ {
41
+ id: 'deepseek-v4-pro',
42
+ name: 'DeepSeek V4 Pro',
43
+ modelPath: 'deepseek-ai/DeepSeek-V4-Pro',
44
+ avatarUrl: 'https://huggingface.co/api/avatars/deepseek-ai',
45
+ recommended: true,
46
+ },
47
+ {
48
+ id: 'deepseek-v4-flash',
49
+ name: 'DeepSeek V4 Flash',
50
+ modelPath: 'deepseek-ai/DeepSeek-V4-Flash',
51
+ avatarUrl: 'https://huggingface.co/api/avatars/deepseek-ai',
52
+ },
53
+ {
54
+ id: 'gpt-oss-120b',
55
+ name: 'gpt-oss-120b',
56
+ modelPath: 'openai/gpt-oss-120b',
57
+ avatarUrl: 'https://huggingface.co/api/avatars/openai',
58
+ },
59
+ {
60
+ id: 'qwen3-coder-next',
61
+ name: 'Qwen3 Coder Next',
62
+ modelPath: 'Qwen/Qwen3-Coder-Next',
63
+ avatarUrl: 'https://huggingface.co/api/avatars/Qwen',
64
+ recommended: true,
65
+ },
66
+ {
67
+ id: 'gemma-3-1b',
68
+ name: 'Gemma 3 1B',
69
+ modelPath: 'google/gemma-3-1b-it',
70
+ avatarUrl: 'https://huggingface.co/api/avatars/google',
71
+ recommended: true,
72
+ },
73
+ {
74
+ id: 'gemini-2.0-flash',
75
+ name: 'Gemini 2.0 Flash',
76
+ modelPath: 'openai/google/gemini-2.0-flash-001',
77
+ avatarUrl: 'https://huggingface.co/api/avatars/google',
78
+ },
79
+ {
80
+ id: 'deepseek-v4-flash-or',
81
+ name: 'DeepSeek V4 Flash (OR)',
82
+ modelPath: 'openai/deepseek/deepseek-v4-flash',
83
+ avatarUrl: 'https://huggingface.co/api/avatars/deepseek-ai',
84
+ },
85
+ {
86
+ id: 'gemma-4-9b',
87
+ name: 'Gemma 4 9B',
88
+ modelPath: 'openai/google/gemma-4-9b-it:free',
89
+ avatarUrl: 'https://huggingface.co/api/avatars/google',
90
+ recommended: true,
91
+ },
92
+ {
93
+ id: 'gemma-4-31b',
94
+ name: 'Gemma 4 31B',
95
+ modelPath: 'openai/google/gemma-4-31b-it:free',
96
+ avatarUrl: 'https://huggingface.co/api/avatars/google',
97
+ recommended: true,
98
+ },
99
+ {
100
+ id: 'hy3-preview',
101
+ name: 'Hy3 Preview',
102
+ modelPath: 'openai/tencent/hy3-preview',
103
+ avatarUrl: 'https://huggingface.co/api/avatars/tencent',
104
+ recommended: true,
105
+ },
106
+ {
107
+ id: 'north-mini-code',
108
+ name: 'north-mini-code:free',
109
+ modelPath: 'openai/cohere/north-mini-code:free',
110
+ avatarUrl: 'https://huggingface.co/api/avatars/cohere',
111
+ recommended: true,
112
+ },
113
+ {
114
+ id: 'nemotron-120b',
115
+ name: 'Nemotron 3 Super 120B',
116
+ modelPath: 'openai/nvidia/nemotron-3-super-120b-a12b:free',
117
+ avatarUrl: 'https://huggingface.co/api/avatars/nvidia',
118
+ },
119
+ {
120
+ id: 'nemotron-3-ultra',
121
+ name: 'Nemotron 3 Ultra 550B',
122
+ modelPath: 'openai/nvidia/nemotron-3-ultra-550b-a55b:free',
123
+ avatarUrl: 'https://huggingface.co/api/avatars/nvidia',
124
+ recommended: true,
125
+ },
126
+ {
127
+ id: 'laguna-m',
128
+ name: 'Laguna M.1',
129
+ modelPath: 'openai/poolside/laguna-m.1:free',
130
+ avatarUrl: 'https://huggingface.co/api/avatars/poolside',
131
+ recommended: true,
132
+ },
133
+ {
134
+ id: 'laguna-xs',
135
+ name: 'Laguna XS.2',
136
+ modelPath: 'openai/poolside/laguna-xs.2:free',
137
+ avatarUrl: 'https://huggingface.co/api/avatars/poolside',
138
+ },
139
+ ];"""
140
+
141
+ content = content[:match.start()] + new_array + content[match.end():]
142
+
143
+ # === Step 3: DEFAULT_FREE_MODEL_OPTION_ID = owl-alpha ===
144
+ content = content.replace(
145
+ "DEFAULT_FREE_MODEL_OPTION_ID = 'kimi-k2.6'",
146
+ "DEFAULT_FREE_MODEL_OPTION_ID = 'owl-alpha'"
147
+ )
148
 
149
  with open(FILE, "w", encoding="utf-8") as f:
150
  f.write(content)