From 96a7abcda4e280f9a6322bcb834acd07d8653194 Mon Sep 17 00:00:00 2001 From: wylab Date: Fri, 13 Feb 2026 13:03:37 +0100 Subject: [PATCH] feat(registry): add OAuth provider detection logic Co-Authored-By: Claude Opus 4.6 --- nanobot/providers/registry.py | 21 +++++++++++++++++++++ tests/test_registry_oauth.py | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 tests/test_registry_oauth.py diff --git a/nanobot/providers/registry.py b/nanobot/providers/registry.py index fdd036e..eb478c0 100644 --- a/nanobot/providers/registry.py +++ b/nanobot/providers/registry.py @@ -357,3 +357,24 @@ def find_by_name(name: str) -> ProviderSpec | None: if spec.name == name: return spec return None + + +def should_use_oauth_provider(api_key: str | None, model: str) -> bool: + """Determine if OAuth provider should be used. + + OAuth provider is used when: + 1. API key is an OAuth token (contains 'sk-ant-oat') + 2. Model is an Anthropic model (contains 'claude' or 'anthropic') + """ + if not api_key: + return False + + if "sk-ant-oat" not in api_key: + return False + + model_lower = model.lower() + anthropic_spec = find_by_name("anthropic") + if anthropic_spec: + return any(kw in model_lower for kw in anthropic_spec.keywords) + + return False diff --git a/tests/test_registry_oauth.py b/tests/test_registry_oauth.py new file mode 100644 index 0000000..30948d7 --- /dev/null +++ b/tests/test_registry_oauth.py @@ -0,0 +1,21 @@ +"""Test OAuth detection in provider registry.""" +import pytest +from nanobot.providers.registry import should_use_oauth_provider + + +def test_should_use_oauth_for_oat_token(): + """OAuth provider should be used for sk-ant-oat tokens.""" + assert should_use_oauth_provider("sk-ant-oat01-xxx", "anthropic/claude-opus-4-5") is True + assert should_use_oauth_provider("sk-ant-oat01-xxx", "claude-sonnet-4") is True + + +def test_should_not_use_oauth_for_regular_key(): + """Regular API keys should not use OAuth provider.""" + assert should_use_oauth_provider("sk-ant-api03-xxx", "claude-opus-4-5") is False + assert should_use_oauth_provider("sk-or-v1-xxx", "anthropic/claude-opus-4-5") is False + + +def test_should_not_use_oauth_for_non_anthropic(): + """Non-Anthropic models should not use OAuth provider.""" + assert should_use_oauth_provider("sk-ant-oat01-xxx", "gpt-4") is False + assert should_use_oauth_provider("sk-ant-oat01-xxx", "deepseek/deepseek-chat") is False