From 9789307dd691d469881b45ad7e0a7f655bab110d Mon Sep 17 00:00:00 2001 From: PiEgg Date: Thu, 19 Feb 2026 13:30:02 +0800 Subject: [PATCH 1/6] Fix Codex provider routing for GitHub Copilot models --- nanobot/config/schema.py | 25 +++++++++++++- nanobot/providers/litellm_provider.py | 13 +++++++- nanobot/providers/openai_codex_provider.py | 2 +- nanobot/providers/registry.py | 16 ++++++++- tests/test_commands.py | 38 ++++++++++++++++++++++ 5 files changed, 90 insertions(+), 4 deletions(-) diff --git a/nanobot/config/schema.py b/nanobot/config/schema.py index ce9634c..9558072 100644 --- a/nanobot/config/schema.py +++ b/nanobot/config/schema.py @@ -287,11 +287,34 @@ class Config(BaseSettings): from nanobot.providers.registry import PROVIDERS model_lower = (model or self.agents.defaults.model).lower() + model_normalized = model_lower.replace("-", "_") + model_prefix = model_lower.split("/", 1)[0] if "/" in model_lower else "" + normalized_prefix = model_prefix.replace("-", "_") + + def _matches_model_prefix(spec_name: str) -> bool: + if not model_prefix: + return False + return normalized_prefix == spec_name + + def _keyword_matches(keyword: str) -> bool: + keyword_lower = keyword.lower() + return ( + keyword_lower in model_lower + or keyword_lower.replace("-", "_") in model_normalized + ) + + # Explicit provider prefix in model name wins over generic keyword matches. + # This prevents `github-copilot/...codex` from being treated as OpenAI Codex. + for spec in PROVIDERS: + p = getattr(self.providers, spec.name, None) + if p and _matches_model_prefix(spec.name): + if spec.is_oauth or p.api_key: + return p, spec.name # Match by keyword (order follows PROVIDERS registry) for spec in PROVIDERS: p = getattr(self.providers, spec.name, None) - if p and any(kw in model_lower for kw in spec.keywords): + if p and any(_keyword_matches(kw) for kw in spec.keywords): if spec.is_oauth or p.api_key: return p, spec.name diff --git a/nanobot/providers/litellm_provider.py b/nanobot/providers/litellm_provider.py index 8cc4e35..3fec618 100644 --- a/nanobot/providers/litellm_provider.py +++ b/nanobot/providers/litellm_provider.py @@ -88,10 +88,21 @@ class LiteLLMProvider(LLMProvider): # Standard mode: auto-prefix for known providers spec = find_by_model(model) if spec and spec.litellm_prefix: + model = self._canonicalize_explicit_prefix(model, spec.name, spec.litellm_prefix) if not any(model.startswith(s) for s in spec.skip_prefixes): model = f"{spec.litellm_prefix}/{model}" - + return model + + @staticmethod + def _canonicalize_explicit_prefix(model: str, spec_name: str, canonical_prefix: str) -> str: + """Normalize explicit provider prefixes like `github-copilot/...`.""" + if "/" not in model: + return model + prefix, remainder = model.split("/", 1) + if prefix.lower().replace("-", "_") != spec_name: + return model + return f"{canonical_prefix}/{remainder}" def _apply_model_overrides(self, model: str, kwargs: dict[str, Any]) -> None: """Apply model-specific parameter overrides from the registry.""" diff --git a/nanobot/providers/openai_codex_provider.py b/nanobot/providers/openai_codex_provider.py index 5067438..2336e71 100644 --- a/nanobot/providers/openai_codex_provider.py +++ b/nanobot/providers/openai_codex_provider.py @@ -80,7 +80,7 @@ class OpenAICodexProvider(LLMProvider): def _strip_model_prefix(model: str) -> str: - if model.startswith("openai-codex/"): + if model.startswith("openai-codex/") or model.startswith("openai_codex/"): return model.split("/", 1)[1] return model diff --git a/nanobot/providers/registry.py b/nanobot/providers/registry.py index 49b735c..d986fe6 100644 --- a/nanobot/providers/registry.py +++ b/nanobot/providers/registry.py @@ -384,10 +384,24 @@ def find_by_model(model: str) -> ProviderSpec | None: """Match a standard provider by model-name keyword (case-insensitive). Skips gateways/local — those are matched by api_key/api_base instead.""" model_lower = model.lower() + model_normalized = model_lower.replace("-", "_") + model_prefix = model_lower.split("/", 1)[0] if "/" in model_lower else "" + normalized_prefix = model_prefix.replace("-", "_") + + # Prefer explicit provider prefix in model name. for spec in PROVIDERS: if spec.is_gateway or spec.is_local: continue - if any(kw in model_lower for kw in spec.keywords): + if model_prefix and normalized_prefix == spec.name: + return spec + + for spec in PROVIDERS: + if spec.is_gateway or spec.is_local: + continue + if any( + kw in model_lower or kw.replace("-", "_") in model_normalized + for kw in spec.keywords + ): return spec return None diff --git a/tests/test_commands.py b/tests/test_commands.py index f5495fd..044d113 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -6,6 +6,10 @@ import pytest from typer.testing import CliRunner from nanobot.cli.commands import app +from nanobot.config.schema import Config +from nanobot.providers.litellm_provider import LiteLLMProvider +from nanobot.providers.openai_codex_provider import _strip_model_prefix +from nanobot.providers.registry import find_by_model runner = CliRunner() @@ -90,3 +94,37 @@ def test_onboard_existing_workspace_safe_create(mock_paths): assert "Created workspace" not in result.stdout assert "Created AGENTS.md" in result.stdout assert (workspace_dir / "AGENTS.md").exists() + + +def test_config_matches_github_copilot_codex_with_hyphen_prefix(): + config = Config() + config.agents.defaults.model = "github-copilot/gpt-5.3-codex" + + assert config.get_provider_name() == "github_copilot" + + +def test_config_matches_openai_codex_with_hyphen_prefix(): + config = Config() + config.agents.defaults.model = "openai-codex/gpt-5.1-codex" + + assert config.get_provider_name() == "openai_codex" + + +def test_find_by_model_prefers_explicit_prefix_over_generic_codex_keyword(): + spec = find_by_model("github-copilot/gpt-5.3-codex") + + assert spec is not None + assert spec.name == "github_copilot" + + +def test_litellm_provider_canonicalizes_github_copilot_hyphen_prefix(): + provider = LiteLLMProvider(default_model="github-copilot/gpt-5.3-codex") + + resolved = provider._resolve_model("github-copilot/gpt-5.3-codex") + + assert resolved == "github_copilot/gpt-5.3-codex" + + +def test_openai_codex_strip_prefix_supports_hyphen_and_underscore(): + assert _strip_model_prefix("openai-codex/gpt-5.1-codex") == "gpt-5.1-codex" + assert _strip_model_prefix("openai_codex/gpt-5.1-codex") == "gpt-5.1-codex" From d08c02225551b5410e126c91b7224c773b5c9187 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 19 Feb 2026 16:31:00 +0800 Subject: [PATCH 2/6] feat(feishu): support sending images, audio, and files - Add image upload via im.v1.image.create API - Add file upload via im.v1.file.create API - Support sending images (.png, .jpg, .gif, etc.) as image messages - Support sending audio (.opus) as voice messages - Support sending other files as file messages - Refactor send() to handle media attachments before text content --- nanobot/channels/feishu.py | 179 ++++++++++++++++++++++++++++++------- 1 file changed, 149 insertions(+), 30 deletions(-) diff --git a/nanobot/channels/feishu.py b/nanobot/channels/feishu.py index bc4a2b8..6f2881b 100644 --- a/nanobot/channels/feishu.py +++ b/nanobot/channels/feishu.py @@ -17,6 +17,10 @@ from nanobot.config.schema import FeishuConfig try: import lark_oapi as lark from lark_oapi.api.im.v1 import ( + CreateFileRequest, + CreateFileRequestBody, + CreateImageRequest, + CreateImageRequestBody, CreateMessageRequest, CreateMessageRequestBody, CreateMessageReactionRequest, @@ -284,48 +288,163 @@ class FeishuChannel(BaseChannel): return elements or [{"tag": "markdown", "content": content}] + # Image file extensions + _IMAGE_EXTS = {".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".ico", ".tiff", ".tif"} + + # Audio file extensions (Feishu only supports opus for audio messages) + _AUDIO_EXTS = {".opus"} + + # File type mapping for Feishu file upload API + _FILE_TYPE_MAP = { + ".opus": "opus", ".mp4": "mp4", ".pdf": "pdf", ".doc": "doc", ".docx": "doc", + ".xls": "xls", ".xlsx": "xls", ".ppt": "ppt", ".pptx": "ppt", + } + + def _upload_image_sync(self, file_path: str) -> str | None: + """Upload an image to Feishu and return the image_key.""" + import os + try: + with open(file_path, "rb") as f: + request = CreateImageRequest.builder() \ + .request_body( + CreateImageRequestBody.builder() + .image_type("message") + .image(f) + .build() + ).build() + response = self._client.im.v1.image.create(request) + if response.success(): + image_key = response.data.image_key + logger.debug(f"Uploaded image {os.path.basename(file_path)}: {image_key}") + return image_key + else: + logger.error(f"Failed to upload image: code={response.code}, msg={response.msg}") + return None + except Exception as e: + logger.error(f"Error uploading image {file_path}: {e}") + return None + + def _upload_file_sync(self, file_path: str) -> str | None: + """Upload a file to Feishu and return the file_key.""" + import os + ext = os.path.splitext(file_path)[1].lower() + file_type = self._FILE_TYPE_MAP.get(ext, "stream") + file_name = os.path.basename(file_path) + try: + with open(file_path, "rb") as f: + request = CreateFileRequest.builder() \ + .request_body( + CreateFileRequestBody.builder() + .file_type(file_type) + .file_name(file_name) + .file(f) + .build() + ).build() + response = self._client.im.v1.file.create(request) + if response.success(): + file_key = response.data.file_key + logger.debug(f"Uploaded file {file_name}: {file_key}") + return file_key + else: + logger.error(f"Failed to upload file: code={response.code}, msg={response.msg}") + return None + except Exception as e: + logger.error(f"Error uploading file {file_path}: {e}") + return None + + def _send_message_sync(self, receive_id_type: str, receive_id: str, msg_type: str, content: str) -> bool: + """Send a single message (text/image/file/interactive) synchronously.""" + try: + request = CreateMessageRequest.builder() \ + .receive_id_type(receive_id_type) \ + .request_body( + CreateMessageRequestBody.builder() + .receive_id(receive_id) + .msg_type(msg_type) + .content(content) + .build() + ).build() + response = self._client.im.v1.message.create(request) + if not response.success(): + logger.error( + f"Failed to send Feishu {msg_type} message: code={response.code}, " + f"msg={response.msg}, log_id={response.get_log_id()}" + ) + return False + logger.debug(f"Feishu {msg_type} message sent to {receive_id}") + return True + except Exception as e: + logger.error(f"Error sending Feishu {msg_type} message: {e}") + return False + async def send(self, msg: OutboundMessage) -> None: - """Send a message through Feishu.""" + """Send a message through Feishu, including media (images/files) if present.""" if not self._client: logger.warning("Feishu client not initialized") return - + try: + import os + # Determine receive_id_type based on chat_id format # open_id starts with "ou_", chat_id starts with "oc_" if msg.chat_id.startswith("oc_"): receive_id_type = "chat_id" else: receive_id_type = "open_id" - - # Build card with markdown + table support - elements = self._build_card_elements(msg.content) - card = { - "config": {"wide_screen_mode": True}, - "elements": elements, - } - content = json.dumps(card, ensure_ascii=False) - - request = CreateMessageRequest.builder() \ - .receive_id_type(receive_id_type) \ - .request_body( - CreateMessageRequestBody.builder() - .receive_id(msg.chat_id) - .msg_type("interactive") - .content(content) - .build() - ).build() - - response = self._client.im.v1.message.create(request) - - if not response.success(): - logger.error( - f"Failed to send Feishu message: code={response.code}, " - f"msg={response.msg}, log_id={response.get_log_id()}" + + loop = asyncio.get_running_loop() + + # --- Send media attachments first --- + if msg.media: + for file_path in msg.media: + if not os.path.isfile(file_path): + logger.warning(f"Media file not found: {file_path}") + continue + + ext = os.path.splitext(file_path)[1].lower() + if ext in self._IMAGE_EXTS: + # Upload and send as image + image_key = await loop.run_in_executor(None, self._upload_image_sync, file_path) + if image_key: + content = json.dumps({"image_key": image_key}) + await loop.run_in_executor( + None, self._send_message_sync, + receive_id_type, msg.chat_id, "image", content, + ) + elif ext in self._AUDIO_EXTS: + # Upload and send as audio (voice message) + file_key = await loop.run_in_executor(None, self._upload_file_sync, file_path) + if file_key: + content = json.dumps({"file_key": file_key}) + await loop.run_in_executor( + None, self._send_message_sync, + receive_id_type, msg.chat_id, "audio", content, + ) + else: + # Upload and send as file + file_key = await loop.run_in_executor(None, self._upload_file_sync, file_path) + if file_key: + content = json.dumps({"file_key": file_key}) + await loop.run_in_executor( + None, self._send_message_sync, + receive_id_type, msg.chat_id, "file", content, + ) + + # --- Send text content (if any) --- + if msg.content and msg.content.strip(): + # Build card with markdown + table support + elements = self._build_card_elements(msg.content) + card = { + "config": {"wide_screen_mode": True}, + "elements": elements, + } + content = json.dumps(card, ensure_ascii=False) + await loop.run_in_executor( + None, self._send_message_sync, + receive_id_type, msg.chat_id, "interactive", content, ) - else: - logger.debug(f"Feishu message sent to {msg.chat_id}") - + except Exception as e: logger.error(f"Error sending Feishu message: {e}") From c86dbc9f45e4467d54ec0a56ce5597219071d8ee Mon Sep 17 00:00:00 2001 From: Nikolas de Hor Date: Thu, 19 Feb 2026 10:27:11 -0300 Subject: [PATCH 3/6] fix: wait for killed process after shell timeout to prevent fd leaks When a shell command times out, process.kill() is called but the process object was never awaited after that. This leaves subprocess pipes undrained and file descriptors open. If many commands time out, fd leaks accumulate. Add a bounded wait (5s) after kill to let the process fully terminate and release its resources. --- nanobot/agent/tools/shell.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/nanobot/agent/tools/shell.py b/nanobot/agent/tools/shell.py index 18eff64..ceac6b7 100644 --- a/nanobot/agent/tools/shell.py +++ b/nanobot/agent/tools/shell.py @@ -81,6 +81,12 @@ class ExecTool(Tool): ) except asyncio.TimeoutError: process.kill() + # Wait for the process to fully terminate so pipes are + # drained and file descriptors are released. + try: + await asyncio.wait_for(process.wait(), timeout=5.0) + except asyncio.TimeoutError: + pass return f"Error: Command timed out after {self.timeout} seconds" output_parts = [] From d748e6eca33baf4b419c624b2a5e702b0d5b6b35 Mon Sep 17 00:00:00 2001 From: Re-bin Date: Thu, 19 Feb 2026 17:28:13 +0000 Subject: [PATCH 4/6] fix: pin dependency version ranges --- pyproject.toml | 54 +++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index bbd6feb..64a884d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,37 +17,37 @@ classifiers = [ ] dependencies = [ - "typer>=0.9.0", - "litellm>=1.0.0", - "pydantic>=2.0.0", - "pydantic-settings>=2.0.0", - "websockets>=12.0", - "websocket-client>=1.6.0", - "httpx>=0.25.0", - "oauth-cli-kit>=0.1.1", - "loguru>=0.7.0", - "readability-lxml>=0.8.0", - "rich>=13.0.0", - "croniter>=2.0.0", - "dingtalk-stream>=0.4.0", - "python-telegram-bot[socks]>=21.0", - "lark-oapi>=1.0.0", - "socksio>=1.0.0", - "python-socketio>=5.11.0", - "msgpack>=1.0.8", - "slack-sdk>=3.26.0", - "slackify-markdown>=0.2.0", - "qq-botpy>=1.0.0", - "python-socks[asyncio]>=2.4.0", - "prompt-toolkit>=3.0.0", - "mcp>=1.0.0", - "json-repair>=0.30.0", + "typer>=0.20.0,<1.0.0", + "litellm>=1.81.5,<2.0.0", + "pydantic>=2.12.0,<3.0.0", + "pydantic-settings>=2.12.0,<3.0.0", + "websockets>=16.0,<17.0", + "websocket-client>=1.9.0,<2.0.0", + "httpx>=0.28.0,<1.0.0", + "oauth-cli-kit>=0.1.3,<1.0.0", + "loguru>=0.7.3,<1.0.0", + "readability-lxml>=0.8.4,<1.0.0", + "rich>=14.0.0,<15.0.0", + "croniter>=6.0.0,<7.0.0", + "dingtalk-stream>=0.24.0,<1.0.0", + "python-telegram-bot[socks]>=22.0,<23.0", + "lark-oapi>=1.5.0,<2.0.0", + "socksio>=1.0.0,<2.0.0", + "python-socketio>=5.16.0,<6.0.0", + "msgpack>=1.1.0,<2.0.0", + "slack-sdk>=3.39.0,<4.0.0", + "slackify-markdown>=0.2.0,<1.0.0", + "qq-botpy>=1.2.0,<2.0.0", + "python-socks[asyncio]>=2.8.0,<3.0.0", + "prompt-toolkit>=3.0.50,<4.0.0", + "mcp>=1.26.0,<2.0.0", + "json-repair>=0.57.0,<1.0.0", ] [project.optional-dependencies] dev = [ - "pytest>=7.0.0", - "pytest-asyncio>=0.21.0", + "pytest>=9.0.0,<10.0.0", + "pytest-asyncio>=1.3.0,<2.0.0", "ruff>=0.1.0", ] From 3890f1a7dd040050df64b11755d392c8b2689806 Mon Sep 17 00:00:00 2001 From: Re-bin Date: Thu, 19 Feb 2026 17:33:08 +0000 Subject: [PATCH 5/6] refactor(feishu): clean up send() and remove dead code --- nanobot/channels/feishu.py | 85 +++++++++++--------------------------- 1 file changed, 24 insertions(+), 61 deletions(-) diff --git a/nanobot/channels/feishu.py b/nanobot/channels/feishu.py index 6f2881b..651d655 100644 --- a/nanobot/channels/feishu.py +++ b/nanobot/channels/feishu.py @@ -2,6 +2,7 @@ import asyncio import json +import os import re import threading from collections import OrderedDict @@ -267,7 +268,6 @@ class FeishuChannel(BaseChannel): before = protected[last_end:m.start()].strip() if before: elements.append({"tag": "markdown", "content": before}) - level = len(m.group(1)) text = m.group(2).strip() elements.append({ "tag": "div", @@ -288,13 +288,8 @@ class FeishuChannel(BaseChannel): return elements or [{"tag": "markdown", "content": content}] - # Image file extensions _IMAGE_EXTS = {".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".ico", ".tiff", ".tif"} - - # Audio file extensions (Feishu only supports opus for audio messages) _AUDIO_EXTS = {".opus"} - - # File type mapping for Feishu file upload API _FILE_TYPE_MAP = { ".opus": "opus", ".mp4": "mp4", ".pdf": "pdf", ".doc": "doc", ".docx": "doc", ".xls": "xls", ".xlsx": "xls", ".ppt": "ppt", ".pptx": "ppt", @@ -302,7 +297,6 @@ class FeishuChannel(BaseChannel): def _upload_image_sync(self, file_path: str) -> str | None: """Upload an image to Feishu and return the image_key.""" - import os try: with open(file_path, "rb") as f: request = CreateImageRequest.builder() \ @@ -326,7 +320,6 @@ class FeishuChannel(BaseChannel): def _upload_file_sync(self, file_path: str) -> str | None: """Upload a file to Feishu and return the file_key.""" - import os ext = os.path.splitext(file_path)[1].lower() file_type = self._FILE_TYPE_MAP.get(ext, "stream") file_name = os.path.basename(file_path) @@ -384,65 +377,35 @@ class FeishuChannel(BaseChannel): return try: - import os - - # Determine receive_id_type based on chat_id format - # open_id starts with "ou_", chat_id starts with "oc_" - if msg.chat_id.startswith("oc_"): - receive_id_type = "chat_id" - else: - receive_id_type = "open_id" - + receive_id_type = "chat_id" if msg.chat_id.startswith("oc_") else "open_id" loop = asyncio.get_running_loop() - # --- Send media attachments first --- - if msg.media: - for file_path in msg.media: - if not os.path.isfile(file_path): - logger.warning(f"Media file not found: {file_path}") - continue + for file_path in msg.media: + if not os.path.isfile(file_path): + logger.warning(f"Media file not found: {file_path}") + continue + ext = os.path.splitext(file_path)[1].lower() + if ext in self._IMAGE_EXTS: + key = await loop.run_in_executor(None, self._upload_image_sync, file_path) + if key: + await loop.run_in_executor( + None, self._send_message_sync, + receive_id_type, msg.chat_id, "image", json.dumps({"image_key": key}), + ) + else: + key = await loop.run_in_executor(None, self._upload_file_sync, file_path) + if key: + media_type = "audio" if ext in self._AUDIO_EXTS else "file" + await loop.run_in_executor( + None, self._send_message_sync, + receive_id_type, msg.chat_id, media_type, json.dumps({"file_key": key}), + ) - ext = os.path.splitext(file_path)[1].lower() - if ext in self._IMAGE_EXTS: - # Upload and send as image - image_key = await loop.run_in_executor(None, self._upload_image_sync, file_path) - if image_key: - content = json.dumps({"image_key": image_key}) - await loop.run_in_executor( - None, self._send_message_sync, - receive_id_type, msg.chat_id, "image", content, - ) - elif ext in self._AUDIO_EXTS: - # Upload and send as audio (voice message) - file_key = await loop.run_in_executor(None, self._upload_file_sync, file_path) - if file_key: - content = json.dumps({"file_key": file_key}) - await loop.run_in_executor( - None, self._send_message_sync, - receive_id_type, msg.chat_id, "audio", content, - ) - else: - # Upload and send as file - file_key = await loop.run_in_executor(None, self._upload_file_sync, file_path) - if file_key: - content = json.dumps({"file_key": file_key}) - await loop.run_in_executor( - None, self._send_message_sync, - receive_id_type, msg.chat_id, "file", content, - ) - - # --- Send text content (if any) --- if msg.content and msg.content.strip(): - # Build card with markdown + table support - elements = self._build_card_elements(msg.content) - card = { - "config": {"wide_screen_mode": True}, - "elements": elements, - } - content = json.dumps(card, ensure_ascii=False) + card = {"config": {"wide_screen_mode": True}, "elements": self._build_card_elements(msg.content)} await loop.run_in_executor( None, self._send_message_sync, - receive_id_type, msg.chat_id, "interactive", content, + receive_id_type, msg.chat_id, "interactive", json.dumps(card, ensure_ascii=False), ) except Exception as e: From b11f0ce6a9bc8159ed1a7a6937c7c93c80a54733 Mon Sep 17 00:00:00 2001 From: Re-bin Date: Thu, 19 Feb 2026 17:39:44 +0000 Subject: [PATCH 6/6] fix: prefer explicit provider prefix over keyword match to fix Codex routing --- nanobot/config/schema.py | 21 ++++++--------------- nanobot/providers/registry.py | 16 +++++----------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/nanobot/config/schema.py b/nanobot/config/schema.py index 9558072..6a1257e 100644 --- a/nanobot/config/schema.py +++ b/nanobot/config/schema.py @@ -291,30 +291,21 @@ class Config(BaseSettings): model_prefix = model_lower.split("/", 1)[0] if "/" in model_lower else "" normalized_prefix = model_prefix.replace("-", "_") - def _matches_model_prefix(spec_name: str) -> bool: - if not model_prefix: - return False - return normalized_prefix == spec_name + def _kw_matches(kw: str) -> bool: + kw = kw.lower() + return kw in model_lower or kw.replace("-", "_") in model_normalized - def _keyword_matches(keyword: str) -> bool: - keyword_lower = keyword.lower() - return ( - keyword_lower in model_lower - or keyword_lower.replace("-", "_") in model_normalized - ) - - # Explicit provider prefix in model name wins over generic keyword matches. - # This prevents `github-copilot/...codex` from being treated as OpenAI Codex. + # Explicit provider prefix wins — prevents `github-copilot/...codex` matching openai_codex. for spec in PROVIDERS: p = getattr(self.providers, spec.name, None) - if p and _matches_model_prefix(spec.name): + if p and model_prefix and normalized_prefix == spec.name: if spec.is_oauth or p.api_key: return p, spec.name # Match by keyword (order follows PROVIDERS registry) for spec in PROVIDERS: p = getattr(self.providers, spec.name, None) - if p and any(_keyword_matches(kw) for kw in spec.keywords): + if p and any(_kw_matches(kw) for kw in spec.keywords): if spec.is_oauth or p.api_key: return p, spec.name diff --git a/nanobot/providers/registry.py b/nanobot/providers/registry.py index d986fe6..3071793 100644 --- a/nanobot/providers/registry.py +++ b/nanobot/providers/registry.py @@ -387,21 +387,15 @@ def find_by_model(model: str) -> ProviderSpec | None: model_normalized = model_lower.replace("-", "_") model_prefix = model_lower.split("/", 1)[0] if "/" in model_lower else "" normalized_prefix = model_prefix.replace("-", "_") + std_specs = [s for s in PROVIDERS if not s.is_gateway and not s.is_local] - # Prefer explicit provider prefix in model name. - for spec in PROVIDERS: - if spec.is_gateway or spec.is_local: - continue + # Prefer explicit provider prefix — prevents `github-copilot/...codex` matching openai_codex. + for spec in std_specs: if model_prefix and normalized_prefix == spec.name: return spec - for spec in PROVIDERS: - if spec.is_gateway or spec.is_local: - continue - if any( - kw in model_lower or kw.replace("-", "_") in model_normalized - for kw in spec.keywords - ): + for spec in std_specs: + if any(kw in model_lower or kw.replace("-", "_") in model_normalized for kw in spec.keywords): return spec return None