(path: str, request: Request)
| 180 | |
| 181 | @app.api_route("/v1/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"]) |
| 182 | async def proxy(path: str, request: Request): |
| 183 | global CLIENT |
| 184 | target_url = f"{config['target_base_url']}/{path}" |
| 185 | if path == "messages": |
| 186 | target_url += "?beta=true" |
| 187 | body = await request.body() |
| 188 | body_json = {} |
| 189 | wants_stream = False |
| 190 | if body: |
| 191 | try: |
| 192 | body_json = json.loads(body) |
| 193 | safe_keys = {'model', 'messages', 'max_tokens', 'metadata', 'stop_sequences', 'stream', 'system', 'temperature', 'top_k', 'top_p', 'tools', 'thinking'} |
| 194 | filtered_body = {k: v for k, v in body_json.items() if k in safe_keys} |
| 195 | model = filtered_body.get('model', '') |
| 196 | if 'anyrouter/' in model: |
| 197 | filtered_body['model'] = model.replace('anyrouter/', '') |
| 198 | if config['debug']: |
| 199 | print(f"[PROXY] Original request keys: {list(body_json.keys())}") |
| 200 | print(f"[PROXY] Has tools: {'tools' in body_json}, tools count: {len(body_json.get('tools', []))}") |
| 201 | print(f"[PROXY] Has system: {'system' in body_json}") |
| 202 | print(f"[PROXY] Has thinking: {'thinking' in body_json}") |
| 203 | if ('sonnet' in model.lower() or 'opus' in model.lower() or 'haiku' in model.lower()) and CLAUDE_CODE_TOOLS: |
| 204 | filtered_body['tools'] = CLAUDE_CODE_TOOLS |
| 205 | if config['debug']: |
| 206 | print(f"[PROXY] Injected {len(CLAUDE_CODE_TOOLS)} Claude Code tools") |
| 207 | if CLAUDE_CODE_SYSTEM: |
| 208 | filtered_body['system'] = CLAUDE_CODE_SYSTEM |
| 209 | if config['debug']: |
| 210 | print(f"[PROXY] Injected Claude Code system prompt") |
| 211 | if 'sonnet' in model.lower() or 'opus' in model.lower(): |
| 212 | if 'thinking' not in filtered_body: |
| 213 | filtered_body['thinking'] = {"budget_tokens": 10000, "type": "enabled"} |
| 214 | if config['debug']: |
| 215 | print(f"[PROXY] Injected thinking config") |
| 216 | filtered_body['metadata'] = {"user_id": "proxy_user"} |
| 217 | wants_stream = filtered_body.get('stream', False) |
| 218 | body_json = filtered_body |
| 219 | except Exception as e: |
| 220 | if config['debug']: |
| 221 | print(f"[PROXY] Body parse error: {e}") |
| 222 | model_name = body_json.get('model', '') |
| 223 | headers = get_claude_headers(is_stream=wants_stream, model=model_name) |
| 224 | req_auth = request.headers.get("Authorization") |
| 225 | if config['api_key']: |
| 226 | headers["x-api-key"] = config['api_key'] |
| 227 | headers["Authorization"] = f"Bearer {config['api_key']}" |
| 228 | elif req_auth: |
| 229 | headers["Authorization"] = req_auth |
| 230 | if config['debug']: |
| 231 | print(f"\n{'='*60}") |
| 232 | print(f"[PROXY] Target: {target_url}") |
| 233 | print(f"[PROXY] Model: {body_json.get('model', 'N/A')}") |
| 234 | print(f"[PROXY] Stream: {wants_stream}") |
| 235 | max_attempts = 5 |
| 236 | retry_delay = 1 |
| 237 | for attempt in range(max_attempts): |
| 238 | try: |
| 239 | if config['debug']: |
nothing calls this directly
no test coverage detected