-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch-http-bridge-plan-priority.py
More file actions
312 lines (289 loc) · 14.3 KB
/
Copy pathpatch-http-bridge-plan-priority.py
File metadata and controls
312 lines (289 loc) · 14.3 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from __future__ import annotations
from pathlib import Path
ROOT = Path("/app/app")
def replace_once(path: Path, text: str, old: str, new: str, label: str) -> str:
if old in text:
return text.replace(old, new, 1)
if new in text:
return text
raise RuntimeError(f"{path}: could not apply {label}")
def patch_service() -> None:
path = ROOT / "modules/proxy/service.py"
text = path.read_text()
text = replace_once(
path,
text,
"logger = logging.getLogger(__name__)\n\n",
"logger = logging.getLogger(__name__)\n\n"
"PLAN_PRIORITY_ALIASES_FOR_HTTP_BRIDGE = {\n"
" \"pro lite\": \"prolite\",\n"
" \"pro-lite\": \"prolite\",\n"
" \"pro_lite\": \"prolite\",\n"
"}\n"
"PLAN_PRIORITY_GROUPS_FOR_HTTP_BRIDGE = {\n"
" \"plus\": 0,\n"
" \"prolite\": 1,\n"
" \"pro\": 1,\n"
"}\n\n\n"
"def _http_bridge_plan_priority_group(plan_type: str | None) -> int:\n"
" normalized = (plan_type or \"\").strip().lower()\n"
" resolved = PLAN_PRIORITY_ALIASES_FOR_HTTP_BRIDGE.get(normalized, normalized)\n"
" return PLAN_PRIORITY_GROUPS_FOR_HTTP_BRIDGE.get(resolved, 2)\n\n\n"
"def _http_bridge_account_plan_priority_group(account: Account | None) -> int:\n"
" if account is None:\n"
" return 2\n"
" return _http_bridge_plan_priority_group(getattr(account, \"plan_type\", None))\n\n\n",
"add HTTP bridge plan priority helpers",
)
methods = ''' async def _higher_priority_account_available(
self,
current_account: Account,
*,
api_key: ApiKeyData | None,
request_model: str | None,
) -> bool:
current_priority = _http_bridge_account_plan_priority_group(current_account)
if current_priority <= 0:
return False
scoped_account_ids = (
set(api_key.assigned_account_ids)
if api_key is not None and api_key.account_assignment_scope_enabled
else None
)
settings = await get_settings_cache().get()
selection = await self._load_balancer.select_account(
sticky_key=None,
sticky_kind=None,
reallocate_sticky=False,
sticky_max_age_seconds=None,
prefer_earlier_reset_accounts=settings.prefer_earlier_reset_accounts,
routing_strategy=_routing_strategy(settings),
model=request_model,
account_ids=scoped_account_ids,
budget_threshold_pct=settings.sticky_reallocation_budget_threshold_pct,
)
candidate = selection.account
if candidate is None or candidate.id == current_account.id:
return False
return _http_bridge_account_plan_priority_group(candidate) < current_priority
async def _http_bridge_session_should_yield_to_plan_priority(
self,
session: "_HTTPBridgeSession",
*,
api_key: ApiKeyData | None,
request_model: str | None,
) -> bool:
if session.closed or session.account.status != AccountStatus.ACTIVE:
return False
should_yield = await self._higher_priority_account_available(
session.account,
api_key=api_key,
request_model=request_model,
)
if should_yield:
_log_http_bridge_event(
"priority_rebind",
session.key,
account_id=session.account.id,
model=session.request_model,
detail="outcome=yield_to_higher_priority_plan",
cache_key_family=session.key.affinity_kind,
model_class=_extract_model_class(session.request_model) if session.request_model else None,
)
return should_yield
'''
text = replace_once(
path,
text,
" async def _create_http_bridge_session_compatible(\n",
methods + " async def _create_http_bridge_session_compatible(\n",
"add HTTP bridge priority methods",
)
text = replace_once(
path,
text,
" or not _http_bridge_session_matches_preferred_account(\n"
" session=alias_session,\n"
" previous_response_id=previous_response_id,\n"
" preferred_account_id=preferred_account_id,\n"
" )\n",
" or await self._http_bridge_session_should_yield_to_plan_priority(\n"
" alias_session,\n"
" api_key=api_key,\n"
" request_model=request_model,\n"
" )\n"
" or not _http_bridge_session_matches_preferred_account(\n"
" session=alias_session,\n"
" previous_response_id=previous_response_id,\n"
" preferred_account_id=preferred_account_id,\n"
" )\n",
"make turn-state alias yield to higher priority plan",
)
text = replace_once(
path,
text,
" and previous_session.account.status == AccountStatus.ACTIVE\n"
" and _http_bridge_session_matches_preferred_account(\n"
" session=previous_session,\n"
" previous_response_id=previous_response_id,\n"
" preferred_account_id=preferred_account_id,\n"
" )\n"
" ):\n",
" and previous_session.account.status == AccountStatus.ACTIVE\n"
" and not await self._http_bridge_session_should_yield_to_plan_priority(\n"
" previous_session,\n"
" api_key=api_key,\n"
" request_model=request_model,\n"
" )\n"
" and _http_bridge_session_matches_preferred_account(\n"
" session=previous_session,\n"
" previous_response_id=previous_response_id,\n"
" preferred_account_id=preferred_account_id,\n"
" )\n"
" ):\n",
"make previous-response alias yield to higher priority plan",
)
text = replace_once(
path,
text,
" and _http_bridge_session_matches_preferred_account(\n"
" session=existing,\n"
" previous_response_id=previous_response_id,\n"
" preferred_account_id=preferred_account_id,\n"
" )\n"
" ):\n",
" and _http_bridge_session_matches_preferred_account(\n"
" session=existing,\n"
" previous_response_id=previous_response_id,\n"
" preferred_account_id=preferred_account_id,\n"
" )\n"
" and not await self._http_bridge_session_should_yield_to_plan_priority(\n"
" existing,\n"
" api_key=api_key,\n"
" request_model=request_model,\n"
" )\n"
" ):\n",
"make existing bridge session yield to higher priority plan",
)
text = replace_once(
path,
text,
" if (\n"
" previous_session is not None\n"
" and not previous_session.closed\n"
" and previous_session.account.status == AccountStatus.ACTIVE\n"
" ):\n",
" if (\n"
" previous_session is not None\n"
" and not previous_session.closed\n"
" and previous_session.account.status == AccountStatus.ACTIVE\n"
" and not await self._http_bridge_session_should_yield_to_plan_priority(\n"
" previous_session,\n"
" api_key=api_key,\n"
" request_model=request_model,\n"
" )\n"
" ):\n",
"make fallback previous-response session yield to higher priority plan",
)
text = replace_once(
path,
text,
" and _http_bridge_session_matches_preferred_account(\n"
" session=session,\n"
" previous_response_id=previous_response_id,\n"
" preferred_account_id=preferred_account_id,\n"
" )\n"
" ):\n",
" and _http_bridge_session_matches_preferred_account(\n"
" session=session,\n"
" previous_response_id=previous_response_id,\n"
" preferred_account_id=preferred_account_id,\n"
" )\n"
" and not await self._http_bridge_session_should_yield_to_plan_priority(\n"
" session,\n"
" api_key=api_key,\n"
" request_model=request_model,\n"
" )\n"
" ):\n",
"make inflight bridge session yield to higher priority plan",
)
text = replace_once(
path,
text,
" _record_continuity_owner_resolution(\n"
" surface=\"http_bridge\",\n"
" source=\"local_bridge_session\",\n"
" outcome=\"hit\",\n"
" previous_response_id=previous_response_id,\n"
" session_id=incoming_turn_state,\n"
" )\n"
" return session.account.id\n",
" if await self._http_bridge_session_should_yield_to_plan_priority(\n"
" session,\n"
" api_key=api_key,\n"
" request_model=None,\n"
" ):\n"
" continue\n"
" _record_continuity_owner_resolution(\n"
" surface=\"http_bridge\",\n"
" source=\"local_bridge_session\",\n"
" outcome=\"hit\",\n"
" previous_response_id=previous_response_id,\n"
" session_id=incoming_turn_state,\n"
" )\n"
" return session.account.id\n",
"make local owner resolution yield to higher priority plan",
)
text = replace_once(
path,
text,
" if preferred_selection.account is not None:\n"
" logger.info(\n"
" \"Selected preferred account request_id=%s kind=%s request_stage=%s account_id=%s\",\n"
" request_id,\n"
" kind,\n"
" request_stage,\n"
" preferred_account_id,\n"
" )\n"
" return preferred_selection\n",
" if preferred_selection.account is not None:\n"
" if await self._higher_priority_account_available(\n"
" preferred_selection.account,\n"
" api_key=api_key,\n"
" request_model=model,\n"
" ):\n"
" logger.info(\n"
" \"Ignoring lower-priority preferred account request_id=%s kind=%s request_stage=%s account_id=%s\",\n"
" request_id,\n"
" kind,\n"
" request_stage,\n"
" preferred_account_id,\n"
" )\n"
" else:\n"
" logger.info(\n"
" \"Selected preferred account request_id=%s kind=%s request_stage=%s account_id=%s\",\n"
" request_id,\n"
" kind,\n"
" request_stage,\n"
" preferred_account_id,\n"
" )\n"
" return preferred_selection\n",
"let higher priority plan override preferred account",
)
text = replace_once(
path,
text,
" if require_preferred_account and preferred_account_id is not None and account.id != preferred_account_id:\n",
" if (\n"
" require_preferred_account\n"
" and preferred_account_id is not None\n"
" and account.id != preferred_account_id\n"
" and _http_bridge_account_plan_priority_group(account) > 0\n"
" ):\n",
"allow Plus to override previous-response owner requirement",
)
path.write_text(text)
def main() -> None:
patch_service()
if __name__ == "__main__":
main()