File size: 5,879 Bytes
dc7c214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Patch backend: add apodex-1.1-mini:free, ling-3.1-flash (OpenRouter) + BeatAPI space-bunny-alpha.

Two OpenRouter models (same `openai/` catch-all routing as the rest of the app):
  - openai/apodex/apodex-1.1-mini:free
  - openai/inclusionai/ling-3.1-flash

One BEATAPI model routed to a *different* provider entirely:
  - beatapi/space-bunny-alpha

The BeatAPI id deliberately uses a `beatapi/` prefix (NOT `openai/`) so it can
never be captured by the OpenRouter catch-all branch in _resolve_llm_params.
See patch_beatapi_routing.py for the dedicated routing branch.

This runs AFTER patch_models.py / patch_new_models.py, which already injected
the `openai/`-prefixed constants and the LING_3_0_FLASH_FREE_MODEL_ID anchor.
Idempotent: re-running is a no-op.
"""

import ast
import re

IDS_FILE = "/app/agent/core/model_ids.py"
AGENT_FILE = "/app/backend/routes/agent.py"

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

new_constants = (
    'APODEX_1_1_MINI_FREE_MODEL_ID = "openai/apodex/apodex-1.1-mini:free"\n'
    'LING_3_1_FLASH_MODEL_ID = "openai/inclusionai/ling-3.1-flash"\n'
    '# NOTE: BeatAPI model. NOT an OpenRouter id: `beatapi/` routes to\n'
    '# https://api.beatapi.io/v1 and is authenticated with BEATAPI_API_KEY\n'
    '# (see patch_beatapi_routing.py). Keep this prefix exclusive to BeatAPI.\n'
    'BEATAPI_SPACE_BUNNY_ALPHA_MODEL_ID = "beatapi/space-bunny-alpha"\n'
)

missing = [
    name
    for name in (
        "APODEX_1_1_MINI_FREE_MODEL_ID",
        "LING_3_1_FLASH_MODEL_ID",
        "BEATAPI_SPACE_BUNNY_ALPHA_MODEL_ID",
    )
    if name not in ids
]

if not missing:
    print("OK: model_ids.py already has all 3 new constants (idempotent)")
else:
    # Anchor: the LING_3_0_FLASH line injected by patch_new_models.py.
    # Fall back to a generic OpenRouter constant line from patch_models.py.
    anchors = [
        'LING_3_0_FLASH_FREE_MODEL_ID = "openai/inclusionai/ling-3.0-flash:free"',
        'NEX_N2_MINI_MODEL_ID = "openai/nex-agi/nex-n2-mini"',
    ]
    inserted = False
    for anchor in anchors:
        if anchor in ids:
            ids = ids.replace(anchor, anchor + "\n" + new_constants, 1)
            print(f"OK: added constants to model_ids.py after {anchor.split(' =')[0]}")
            inserted = True
            break
    if not inserted:
        # Last resort: append right after the HOSTED_MODEL_IDS set closes, which
        # is a stable upstream anchor present in every revision.
        m = re.search(r"HOSTED_MODEL_IDS\s*=\s*\{[^}]*\}\n", ids)
        if m:
            ids = ids[: m.end()] + "\n" + new_constants + ids[m.end() :]
            print("OK: added constants to model_ids.py after HOSTED_MODEL_IDS")
        else:
            print("FAIL: no anchor found in model_ids.py")
            raise SystemExit(1)

    with open(IDS_FILE, "w") as f:
        f.write(ids)
    ast.parse(ids)
    print("OK: model_ids.py written & syntax OK")

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

if "APODEX_1_1_MINI_FREE_MODEL_ID" not in ac:
    # patch_models.py rewrote the import block to list every OpenRouter constant.
    imp_anchor = "    LING_3_0_FLASH_FREE_MODEL_ID,\n"
    if imp_anchor in ac:
        ac = ac.replace(
            imp_anchor,
            imp_anchor
            + "    APODEX_1_1_MINI_FREE_MODEL_ID,\n"
            + "    LING_3_1_FLASH_MODEL_ID,\n"
            + "    BEATAPI_SPACE_BUNNY_ALPHA_MODEL_ID,\n",
            1,
        )
        print("OK: added 3 model imports to agent.py")
    else:
        print("FAIL: import anchor not found in agent.py")
        raise SystemExit(1)

    # ---------- 3. agent.py : _available_models ----------
    line_anchor = (
        '        {"id": SPACE_BUNNY_ALPHA_MODEL_ID, "label": "stealth/space-bunny-alpha", "recommended": True},\n'
    )
    new_lines = line_anchor + (
        '        # OpenRouter models (openai/ prefix -> OpenRouter)\n'
        '        {"id": APODEX_1_1_MINI_FREE_MODEL_ID, "label": "apodex/apodex-1.1-mini:free", "recommended": True},\n'
        '        {"id": LING_3_1_FLASH_MODEL_ID, "label": "inclusionai/ling-3.1-flash", "recommended": True},\n'
        '        # BeatAPI model (beatapi/ prefix -> https://api.beatapi.io/v1)\n'
        '        {"id": BEATAPI_SPACE_BUNNY_ALPHA_MODEL_ID, "label": "BeatAPI space-bunny-alpha", "recommended": True},\n'
    )

    if line_anchor in ac:
        ac = ac.replace(line_anchor, new_lines, 1)
        print("OK: added 3 new models to _available_models()")
    else:
        # The _available_models body was fully rewritten by patch_models.py; match
        # on the generic recommended-Ling line so this stays robust.
        alt = (
            '        {"id": LING_3_0_FLASH_FREE_MODEL_ID, "label": '
            '"inclusionai/ling-3.0-flash:free", "recommended": True},\n'
        )
        if alt in ac:
            ac = ac.replace(
                alt,
                alt
                + '        {"id": APODEX_1_1_MINI_FREE_MODEL_ID, "label": "apodex/apodex-1.1-mini:free", "recommended": True},\n'
                + '        {"id": LING_3_1_FLASH_MODEL_ID, "label": "inclusionai/ling-3.1-flash", "recommended": True},\n'
                + '        {"id": BEATAPI_SPACE_BUNNY_ALPHA_MODEL_ID, "label": "BeatAPI space-bunny-alpha", "recommended": True},\n',
                1,
            )
            print("OK: added 3 new models to _available_models() (fallback anchor)")
        else:
            print("FAIL: _available_models() anchor not found in agent.py")
            raise SystemExit(1)

    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 patched (idempotent)")

print("DONE: apodex-1.1-mini:free, ling-3.1-flash, BeatAPI space-bunny-alpha registered")