| | """mitmproxy addon: rewrite legacy Anthropic thinking config for newer models. |
| | |
| | Copilot CLI (BYOK, provider type "anthropic") sends |
| | "thinking": {"type": "enabled", "budget_tokens": N, ...} |
| | which newer models (e.g. Claude Opus 5.5) reject with HTTP 400. This rewrites it to |
| | "thinking": {"type": "adaptive", ...}, "output_config": {"effort": "<level>"} |
| | keeping the other thinking fields (display, block_binding). |
| | |
| | Copilot only sends thinking on the first request of a turn; follow-up requests |
| | in the tool loop omit it and would run at the model's default effort. Those get |
| | the effort last seen for the same model. |
| | |
| | Effort is $THINKING_EFFORT if set, otherwise derived from budget_tokens. |
| | """ |
| | |
| | import json |
| | import os |
| | |
| | from mitmproxy import ctx, http |
| | |
| | FORCED_EFFORT = os.environ.get("THINKING_EFFORT", "").strip().lower() |
| | |
| | last_effort: dict[str, str] = {} |
| | |
| | |
| | # Copilot CLI's --reasoning-effort / effort picker sends these budgets: |
| | # low=1024, medium=2048, high=4096, xhigh=8191 |
| | def effort_from_budget(budget: int \| None) -> str: |
| | if FORCED_EFFORT: | | | return FORCED_EFFORT | | | if budget is None: | | | return "high" | | | if budget <= 1024: | | | return "low" | | | if budget <= 2048: | | | return "medium" | | | if budget <= 4096: | | | return "high" | | | return "xhigh" | | | | | | |
| | def request(flow: http.HTTPFlow) -> None: |
| | if flow.request.method != "POST" or not flow.request.path.split("?")[0].endswith("/v1/messages"): |
| | return | | | | | | # Copilot CLI zstd-compresses bodies; decode so we can edit plain JSON. | | | flow.request.decode(strict=False) | | | try: | | | body = json.loads(flow.request.content or b"{}") | | | except ValueError: | | | return | | | if not isinstance(body, dict) or "model" not in body: | | | return | | | |
| | model = body["model"] |
| | thinking = body.get("thinking") |
| | output_config = body.get("output_config") or {} |
| | |
| | if isinstance(thinking, dict) and thinking.get("type") == "enabled": |
| | budget = thinking.get("budget_tokens") |
| | body["thinking"] = { |
| | **{k: v for k, v in thinking.items() if k != "budget_tokens"}, |
| | "type": "adaptive", | | | } |
| | output_config.setdefault("effort", effort_from_budget(budget)) |
| | last_effort[model] = output_config["effort"] |
| | note = f"budget_tokens={budget} -> adaptive" |
| | elif thinking is None and "effort" not in output_config and (FORCED_EFFORT or model in last_effort): |
| | output_config["effort"] = FORCED_EFFORT or last_effort[model] |
| | note = "no thinking (tool-loop follow-up)" |
| | else: | | | return | | | |
| | body["output_config"] = output_config |
| | flow.request.content = json.dumps(body).encode() |
| | ctx.log.info( |
| | f"[fix_thinking] model={model} {note}, effort={output_config['effort']} " |
| | f"temperature={body.get('temperature')}" |
| | ) |