File size: 10,534 Bytes
cf7c796
6915e1a
 
 
 
cf7c796
6915e1a
cf7c796
75fb76c
 
e30a6a3
 
cf7c796
e30a6a3
 
6915e1a
 
 
cf7c796
6915e1a
 
 
cf7c796
 
6915e1a
 
 
 
 
 
 
 
 
27b4eda
6915e1a
 
 
e30a6a3
6915e1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e30a6a3
 
 
 
6915e1a
 
 
e30a6a3
6915e1a
 
 
 
 
 
 
 
e30a6a3
 
 
 
 
6915e1a
e30a6a3
 
 
 
 
6915e1a
e30a6a3
 
6915e1a
 
e30a6a3
6915e1a
e30a6a3
6915e1a
 
 
 
e30a6a3
6915e1a
 
 
 
e30a6a3
6915e1a
 
 
 
 
 
 
 
cf7c796
e30a6a3
 
75fb76c
cf7c796
 
 
 
e30a6a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75fb76c
 
 
 
 
 
 
 
 
e30a6a3
 
75fb76c
 
 
 
 
 
 
 
e30a6a3
cf7c796
e30a6a3
75fb76c
cf7c796
 
e30a6a3
 
cf7c796
 
 
 
 
 
 
 
e30a6a3
 
75fb76c
 
 
 
 
 
 
e30a6a3
75fb76c
e30a6a3
75fb76c
 
cf7c796
e30a6a3
 
 
 
 
75fb76c
cf7c796
75fb76c
 
 
 
 
 
e30a6a3
75fb76c
 
 
cf7c796
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""Patch backend + frontend: add recursive/flint-v1.0:free (OpenRouter).

Uses the same `openai/` path convention as Laguna S 2.1:free:
  - backend constant: RECURSIVE_FLINT_V1_FREE_MODEL_ID = "openai/recursive/flint-v1.0:free"
  - registered in _available_models() backend list
  - frontend: new ModelOption in ChatInput.tsx DEFAULT_MODEL_OPTIONS

IMPORTANT: recursive/flint-v1.0:free has `reasoning: { mandatory: true }` on
OpenRouter. Without sending reasoning, the Recursive provider returns
400 "Inference request failed". We patch the `openai/` routing branch in
llm_params.py to inject `reasoning: {"effort": "low"}` via extra_body so the
provider accepts the request.

Runs AFTER patch_models.py + patch_openrouter_fix.py (which adds the
openai/ routing block with custom_llm_provider). Idempotent.
"""

import ast
import os

IDS_FILE = "/app/agent/core/model_ids.py"
AGENT_FILE = "/app/backend/routes/agent.py"
LLM_PARAMS_FILE = "/app/agent/core/llm_params.py"
FRONTEND_FILE = "/source/frontend/src/components/Chat/ChatInput.tsx"

# ---------- 1. model_ids.py ----------
with open(IDS_FILE) as f:
    c = f.read()

new_constant = 'RECURSIVE_FLINT_V1_FREE_MODEL_ID = "openai/recursive/flint-v1.0:free"\n'

if "RECURSIVE_FLINT_V1_FREE_MODEL_ID" not in c:
    # Anchor: the NEMOTRON_3_5_SUPER_VL line injected by patch_xing_nemotron_vl.py.
    # Fall back to LAGUNA_S21 line from patch_models.py.
    anchors = [
        'NEMOTRON_3_5_SUPER_VL_FREE_MODEL_ID = "openai/nvidia/nemotron-3.5-super-vl:free"',
        'LAGUNA_S21_FREE_MODEL_ID = "openai/poolside/laguna-s-2.1:free"',
        'LING_3_0_FLASH_FREE_MODEL_ID = "openai/inclusionai/ling-3.0-flash:free"',
    ]
    inserted = False
    for anchor in anchors:
        if anchor in c:
            c = c.replace(anchor, anchor + "\n" + new_constant, 1)
            print(f"OK: added constant to model_ids.py after {anchor.split(' =')[0]}")
            inserted = True
            break
    if not inserted:
        print("FAIL: no anchor found in model_ids.py for constant insertion")
        raise SystemExit(1)

    with open(IDS_FILE, "w") as f:
        f.write(c)
    ast.parse(c)
    print("OK: model_ids.py written & syntax OK")
else:
    print("OK: model_ids.py already has RECURSIVE_FLINT_V1_FREE_MODEL_ID (idempotent)")

# ---------- 2. agent.py : imports ----------
with open(AGENT_FILE) as f:
    ac = f.read()

if "RECURSIVE_FLINT_V1_FREE_MODEL_ID" not in ac:
    # Find the import block and add after the last imported constant
    imp_anchor = "    SPACE_BUNNY_ALPHA_MODEL_ID,\n"
    if imp_anchor not in ac:
        imp_anchor = "    LING_3_0_FLASH_FREE_MODEL_ID,\n"
    if imp_anchor in ac:
        ac = ac.replace(
            imp_anchor,
            imp_anchor + "    RECURSIVE_FLINT_V1_FREE_MODEL_ID,\n",
            1,
        )
        print("OK: added RECURSIVE_FLINT_V1 import to agent.py")
    else:
        print("FAIL: import anchor not found in agent.py")
        raise SystemExit(1)

    # ---------- 3. agent.py : _available_models ----------
    # Insert before the closing of _available_models
    models_anchor = (
        '        {"id": "deepseek/deepseek-v4-flash-0731", "label": "DeepSeek V4 Flash 0731", "recommended": True},\n'
        '    ]\n'
        '    return models'
    )
    new_models = (
        '        {"id": "deepseek/deepseek-v4-flash-0731", "label": "DeepSeek V4 Flash 0731", "recommended": True},\n'
        '        {"id": RECURSIVE_FLINT_V1_FREE_MODEL_ID, "label": "recursive/flint-v1.0:free", "recommended": True},\n'
        '    ]\n'
        '    return models'
    )
    if models_anchor in ac:
        ac = ac.replace(models_anchor, new_models, 1)
        print("OK: added recursive/flint-v1.0:free to _available_models()")
    else:
        # Fallback: try the SPACE_BUNNY line as anchor
        alt_anchor = (
            '        {"id": SPACE_BUNNY_ALPHA_MODEL_ID, "label": "stealth/space-bunny-alpha", "recommended": True},\n'
        )
        if alt_anchor in ac:
            ac = ac.replace(
                alt_anchor,
                alt_anchor + '        {"id": RECURSIVE_FLINT_V1_FREE_MODEL_ID, "label": "recursive/flint-v1.0:free", "recommended": True},\n',
                1,
            )
            print("OK: added recursive/flint-v1.0:free to _available_models() (fallback)")
        else:
            print("WARN: could not find _available_models anchor - model may already be present")

    with open(AGENT_FILE, "w") as f:
        f.write(ac)
    ast.parse(ac)
    print("OK: agent.py written & syntax OK")
else:
    print("OK: agent.py already has RECURSIVE_FLINT_V1_FREE_MODEL_ID (idempotent)")

# ---------- 4. llm_params.py : reasoning support for recursive/flint-v1.0 ----------
# Flint has reasoning: { mandatory: true } on OpenRouter. Without sending
# reasoning, the Recursive provider returns 400 "Inference request failed".
# We modify the openai/ routing block to include reasoning for flint models.
with open(LLM_PARAMS_FILE) as f:
    lp = f.read()

if "flint" not in lp.lower():
    # The patched llm_params.py (after patch_models.py + patch_openrouter_fix.py)
    # has an openai/ routing block with custom_llm_provider: "openrouter".
    # We modify it to add reasoning via extra_body for flint models.

    # The block looks like:
    #    if normalized_model.startswith("openai/"):
    #        or_model = normalized_model[len("openai/"):]
    #        return {
    #            "model": or_model,
    #            "api_base": "https://openrouter.ai/api/v1",
    #            "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",
    #            "custom_llm_provider": "openrouter",
    #        }

    # v2 (with custom_llm_provider) - the final state after all patches
    old_block_v2 = (
        '    if normalized_model.startswith("openai/"):\n'
        '        or_model = normalized_model[len("openai/"):]\n'
        '        return {\n'
        '            "model": or_model,\n'
        '            "api_base": "https://openrouter.ai/api/v1",\n'
        '            "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n'
        '            "custom_llm_provider": "openrouter",\n'
        '        }\n'
    )

    new_block_v2 = (
        '    if normalized_model.startswith("openai/"):\n'
        '        or_model = normalized_model[len("openai/"):]\n'
        '        params = {\n'
        '            "model": or_model,\n'
        '            "api_base": "https://openrouter.ai/api/v1",\n'
        '            "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n'
        '            "custom_llm_provider": "openrouter",\n'
        '        }\n'
        '        # Flint requires reasoning (mandatory on OpenRouter)\n'
        '        if "flint" in or_model.lower():\n'
        '            params["extra_body"] = {"reasoning": {"effort": "low"}}\n'
        '        return params\n'
    )

    # v1 (without custom_llm_provider)
    old_block_v1 = (
        '    if normalized_model.startswith("openai/"):\n'
        '        or_model = normalized_model[len("openai/"):]\n'
        '        return {\n'
        '            "model": or_model,\n'
        '            "api_base": "https://openrouter.ai/api/v1",\n'
        '            "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n'
        '        }\n'
    )

    new_block_v1 = (
        '    if normalized_model.startswith("openai/"):\n'
        '        or_model = normalized_model[len("openai/"):]\n'
        '        params = {\n'
        '            "model": or_model,\n'
        '            "api_base": "https://openrouter.ai/api/v1",\n'
        '            "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n'
        '        }\n'
        '        # Flint requires reasoning (mandatory on OpenRouter)\n'
        '        if "flint" in or_model.lower():\n'
        '            params["extra_body"] = {"reasoning": {"effort": "low"}}\n'
        '        return params\n'
    )

    if old_block_v2 in lp:
        lp = lp.replace(old_block_v2, new_block_v2)
        print("OK: patched llm_params.py openai/ block (v2 with custom_llm_provider) for flint reasoning")
    elif old_block_v1 in lp:
        lp = lp.replace(old_block_v1, new_block_v1)
        print("OK: patched llm_params.py openai/ block (v1) for flint reasoning")
    else:
        print("WARN: could not find openai/ routing block in llm_params.py")
        print("       - Searching for 'openai/' patterns...")
        for i, line in enumerate(lp.split('\n')):
            if 'startswith("openai/")' in line:
                print(f"  Found at line {i+1}: {line.strip()}")
                lines = lp.split('\n')
                for j in range(max(0,i-2), min(len(lines), i+10)):
                    print(f"  {j+1}: {lines[j]}")
                print("---")
        print("       - Flint model will be registered but may fail with 400 'Inference request failed'")

    with open(LLM_PARAMS_FILE, "w") as f:
        f.write(lp)
    ast.parse(lp)
    print("OK: llm_params.py written & syntax OK")
else:
    print("OK: llm_params.py already patched for flint (idempotent)")

# ---------- 5. Frontend ChatInput.tsx ----------
if os.path.exists(FRONTEND_FILE):
    with open(FRONTEND_FILE, "r", encoding="utf-8") as f:
        fc = f.read()

    if "flint-v1.0" in fc:
        print("OK: recursive/flint-v1.0:free already present in ChatInput.tsx (idempotent)")
    else:
        # Anchor: the deepseek-v4-flash-latest entry at the end of DEFAULT_MODEL_OPTIONS.
        anchor = "  {\n    id: 'deepseek-v4-flash-latest',\n"
        new_entry = (
            "  {\n"
            "    id: 'flint-v1.0-free',\n"
            "    name: 'recursive/flint-v1.0:free',\n"
            "    modelPath: 'openai/recursive/flint-v1.0:free',\n"
            "    avatarUrl: 'https://huggingface.co/api/avatars/recursive',\n"
            "    recommended: true,\n"
            "  },\n"
        )

        if anchor in fc:
            fc = fc.replace(anchor, new_entry + anchor, 1)
            with open(FRONTEND_FILE, "w", encoding="utf-8") as f:
                f.write(fc)
            print("OK: added recursive/flint-v1.0:free to ChatInput.tsx DEFAULT_MODEL_OPTIONS")
        else:
            print("WARN: deepseek anchor not found in ChatInput.tsx - frontend entry not added")
else:
    print(f"WARN: {FRONTEND_FILE} not found (frontend patch skipped)")

print("DONE: recursive/flint-v1.0:free added with Laguna S 2.1:free path convention + reasoning fix")