diff --git a/src/mnemosyne/gateway.py b/src/mnemosyne/gateway.py index e915a03..3e9a7f3 100644 --- a/src/mnemosyne/gateway.py +++ b/src/mnemosyne/gateway.py @@ -2561,6 +2561,7 @@ def create_app( adapter = app.state.adapters[provider] client: httpx.Client = app.state.clients[provider] + oauth_token = os.environ.get("ANTHROPIC_AUTH_TOKEN") request_id = str(uuid.uuid4()) started = time.perf_counter() @@ -2572,6 +2573,68 @@ def create_app( session.token_state["model"] = req.model duplication_score = _duplication_score(req) outgoing_body = adapter.denormalize_request(req) + + # When proxying for OAuth (no auth plugin doing transforms), + # apply Claude Code identity transforms to the outbound body. + # Ref: rmk40/opencode-anthropic-auth transformRequestBody() + if oauth_token and provider == "anthropic": + _CLAUDE_CODE_IDENTITY = "You are Claude Code, Anthropic's official CLI for Claude." + system = outgoing_body.get("system") + if isinstance(system, list): + # Strip "OpenCode" identity line, replace OpenCode→Claude Code + for i, item in enumerate(system): + if isinstance(item, dict) and item.get("type") == "text" and item.get("text"): + import re as _re_sys + + text = item["text"] + text = _re_sys.sub( + r"^You are OpenCode, the best coding agent on the planet\.\n*", + "", + text, + flags=_re_sys.MULTILINE, + ) + text = text.replace("OpenCode", "Claude Code") + text = _re_sys.sub( + r"(?