fix(llm): refresh MiniMax provider contract

This commit is contained in:
octo-patch committed 2026-11-15 08:20:19 +08:00
commit 5f72c8b3df
2246 files changed
+296105 -17180

No files matched your search

+11 -10
View File
@@ -4,10 +4,10 @@ from __future__ import annotations
import os
import sys
from enum import Enum
from enum import StrEnum
class Color(str, Enum):
class Color(StrEnum):
RESET = "\033[0m"
BOLD = "\033[1m"
GREEN = "\033[92m"
@@ -18,10 +18,10 @@ class Color(str, Enum):
def print_banner() -> None:
banner = f"""{Color.CYAN}
╔═══════════════════════════════════════════╗
║ LLM Provider Selector ║
║ Provider-agnostic AI interactions ║
╚═══════════════════════════════════════════╝{Color.RESET}"""
+-------------------------------------------+
| LLM Provider Selector |
| Provider-agnostic AI interactions |
+-------------------------------------------+{Color.RESET}"""
print(banner)
@@ -98,12 +98,12 @@ def save_config(provider: str, model: str, persist: bool = False) -> None:
with open(env_file, "w") as f:
f.write(config)
print(f"\n{Color.GREEN}✓{Color.RESET} Config saved to {Color.CYAN}{env_file}{Color.RESET}")
print(f"\n{Color.GREEN}OK{Color.RESET} Config saved to {Color.CYAN}{env_file}{Color.RESET}")
if persist:
os.environ["LLM_PROVIDER"] = provider
os.environ["LLM_MODEL"] = model
print(f"{Color.GREEN}✓{Color.RESET} Config loaded to current session")
print(f"{Color.GREEN}OK{Color.RESET} Config loaded to current session")
def interactive_select(
@@ -116,7 +116,7 @@ def interactive_select(
if providers is None:
providers = [
("claude", "Anthropic Claude ( Sonnet, Opus, Haiku)"),
("minimax", "MiniMax (M2.7)"),
("minimax", "MiniMax (M3.1 Flash, M3)"),
("openai", "OpenAI GPT (4o, 4o-mini, 3.5-turbo)"),
("ollama", "Local Ollama models"),
]
@@ -129,7 +129,8 @@ def interactive_select(
("claude-haiku-4-5", "Claude Haiku 4.5 - Fast"),
],
"minimax": [
("MiniMax-M2.7", "MiniMax M2.7 - Long-context text model"),
("MiniMax-M3.1-Flash", "MiniMax M3.1 Flash - Fast reasoning model"),
("MiniMax-M3", "MiniMax M3 - Flexible reasoning model"),
],
"openai": [
("gpt-4o", "GPT-4o - Most capable"),
+1 -1
View File
@@ -121,7 +121,7 @@ _PROVIDER_TEMPLATE_MAP: dict[str, dict[str, Any]] = {
def get_provider_builder(provider_name: str) -> PromptBuilder:
config_dict = _PROVIDER_TEMPLATE_MAP.get(provider_name.lower(), {})
config_dict = _PROVIDER_TEMPLATE_MAP.get(provider_name.strip().lower(), {})
config = PromptConfig(**config_dict)
return PromptBuilder(config)
+5 -3
View File
@@ -16,6 +16,8 @@ from llm.core.interface import (
)
from llm.core.types import LLMInput, LLMOutput, ModelInfo, ProviderType, ToolCall
from llm.providers.constants import EMPTY_FILTERED_RESPONSE_ERROR
from llm.providers.model_config import read_model_env
from llm.providers.openai_compat import to_openai_message_param
ASTRAFLOW_BASE_URL = "https://api.umodelverse.ai/v1"
ASTRAFLOW_CN_BASE_URL = "https://api.modelverse.cn/v1"
@@ -53,8 +55,8 @@ class _AstraflowBaseProvider(LLMProvider):
) -> None:
self.api_key = api_key or os.environ.get(self.api_key_env) or ""
self.base_url = base_url or os.environ.get(self.base_url_env, self.default_base_url)
env_model = os.environ.get(self.model_env)
fallback_model = os.environ.get(self.fallback_model_env) if self.fallback_model_env else None
env_model = read_model_env(self.model_env)
fallback_model = read_model_env(self.fallback_model_env) if self.fallback_model_env else None
self.default_model = default_model or env_model or fallback_model or DEFAULT_ASTRAFLOW_MODEL
self.client = OpenAI(api_key=self.api_key, base_url=self.base_url, _enforce_credentials=False)
self._models = [
@@ -70,7 +72,7 @@ class _AstraflowBaseProvider(LLMProvider):
try:
params: dict[str, Any] = {
"model": llm_input.model or self.default_model,
"messages": [msg.to_dict() for msg in llm_input.messages],
"messages": [to_openai_message_param(msg) for msg in llm_input.messages],
}
if llm_input.temperature != 1.0:
params["temperature"] = llm_input.temperature
+4 -2
View File
@@ -16,6 +16,8 @@ from llm.core.interface import (
)
from llm.core.types import LLMInput, LLMOutput, ModelInfo, ProviderType, ToolCall
from llm.providers.constants import EMPTY_FILTERED_RESPONSE_ERROR
from llm.providers.model_config import read_model_env
from llm.providers.openai_compat import to_openai_message_param
ATLAS_BASE_URL = "https://api.atlascloud.ai/v1"
DEFAULT_ATLAS_MODEL = "deepseek-ai/deepseek-v4-pro"
@@ -67,7 +69,7 @@ class AtlasProvider(LLMProvider):
or ""
)
self.base_url = base_url or os.environ.get(self.base_url_env, self.default_base_url)
env_model = os.environ.get(self.model_env)
env_model = read_model_env(self.model_env)
self.default_model = default_model or env_model or DEFAULT_ATLAS_MODEL
self.client = OpenAI(api_key=self.api_key, base_url=self.base_url, _enforce_credentials=False)
self._models = [
@@ -83,7 +85,7 @@ class AtlasProvider(LLMProvider):
try:
params: dict[str, Any] = {
"model": llm_input.model or self.default_model,
"messages": [msg.to_dict() for msg in llm_input.messages],
"messages": [to_openai_message_param(msg) for msg in llm_input.messages],
}
if llm_input.temperature != 1.0:
params["temperature"] = llm_input.temperature
+9 -3
View File
@@ -26,8 +26,14 @@ def _uses_adaptive_thinking_only(model: str) -> bool:
class ClaudeProvider(LLMProvider):
provider_type = ProviderType.CLAUDE
def __init__(self, api_key: str | None = None, base_url: str | None = None) -> None:
def __init__(
self,
api_key: str | None = None,
base_url: str | None = None,
default_model: str | None = None,
) -> None:
self.client = Anthropic(api_key=api_key or os.environ.get("ANTHROPIC_API_KEY"), base_url=base_url)
self.default_model = default_model or _DEFAULT_MODEL
self._models = [
ModelInfo(
name="claude-opus-4-8",
@@ -57,7 +63,7 @@ class ClaudeProvider(LLMProvider):
def generate(self, input: LLMInput) -> LLMOutput:
try:
model = input.model or _DEFAULT_MODEL
model = input.model or self.default_model
system_parts: list[str] = []
for message in input.messages:
if message.role != Role.SYSTEM:
@@ -145,4 +151,4 @@ class ClaudeProvider(LLMProvider):
return bool(self.client.api_key)
def get_default_model(self) -> str:
return _DEFAULT_MODEL
return self.default_model
+13 -5
View File
@@ -31,8 +31,9 @@ JSONValue = None | bool | int | float | str | list["JSONValue"] | dict[str, "JSO
MINIMAX_BASE_URL = "https://api.minimax.io/v1"
MINIMAX_ANTHROPIC_BASE_URL = "https://api.minimax.io/anthropic"
MINIMAX_M2_7_MODEL = "MiniMax-M2.7"
DEFAULT_MINIMAX_MODEL = MINIMAX_M2_7_MODEL
MINIMAX_M3_1_FLASH_MODEL = "MiniMax-M3.1-Flash"
MINIMAX_M3_MODEL = "MiniMax-M3"
DEFAULT_MINIMAX_MODEL = MINIMAX_M3_1_FLASH_MODEL
DEFAULT_MINIMAX_ANTHROPIC_MAX_TOKENS = 16_000
@@ -198,11 +199,18 @@ class MiniMaxProvider(LLMProvider):
)
self._models = [
ModelInfo(
name=DEFAULT_MINIMAX_MODEL,
name=MINIMAX_M3_1_FLASH_MODEL,
provider=self.provider_type,
supports_tools=True,
supports_vision=False,
context_window=204_800,
supports_vision=True,
context_window=1_000_000,
),
ModelInfo(
name=MINIMAX_M3_MODEL,
provider=self.provider_type,
supports_tools=True,
supports_vision=True,
context_window=1_000_000,
),
]
+11
View File
@@ -0,0 +1,11 @@
"""Model environment parsing shared by the resolver and provider adapters."""
import os
def read_model_env(name: str) -> str | None:
"""Read a model ID, treating surrounding whitespace and quotes as config syntax."""
value = os.environ.get(name, "").strip()
if len(value) >= 2 and value[0] == value[-1] and value[0] in {"'", '"'}:
value = value[1:-1].strip()
return value or None
+129 -15
View File
@@ -2,22 +2,54 @@
from __future__ import annotations
import json
import logging
import os
import urllib.request
import uuid
from typing import Any
from llm.core.interface import (
AuthenticationError,
ContextLengthError,
LLMError,
LLMProvider,
RateLimitError,
)
from llm.core.types import (
LLMInput,
LLMOutput,
Message,
ModelInfo,
ProviderType,
Role,
ToolCall,
)
from llm.providers.model_config import read_model_env
from llm.providers.reasoning import prefilled_reasoning_from_env, strip_reasoning
logger = logging.getLogger(__name__)
def _messages_payload(messages: list[Message]) -> list[dict[str, Any]]:
"""Serialize messages, naming the tool each tool result answers.
Ollama correlates a tool result with its call through ``tool_name``; ids
are optional on its side, so the name is what keeps several results apart.
"""
names: dict[str, str] = {}
payload = []
for msg in messages:
item = msg.to_dict()
for tc in msg.tool_calls or []:
if tc.id:
names[tc.id] = tc.name
if msg.role == Role.TOOL:
tool_name = msg.name or (names.get(msg.tool_call_id) if msg.tool_call_id else None)
if tool_name:
item["tool_name"] = tool_name
payload.append(item)
return payload
class OllamaProvider(LLMProvider):
@@ -27,14 +59,30 @@ class OllamaProvider(LLMProvider):
self,
base_url: str | None = None,
default_model: str | None = None,
prefilled_reasoning: bool | None = None,
leading_reasoning: bool | None = None,
) -> None:
# Enable only for a deployment that declares balanced leading reasoning.
# Generic responses can contain literal XML and stay intact by default.
self.leading_reasoning = (
leading_reasoning
if leading_reasoning is not None
else prefilled_reasoning_from_env("OLLAMA_LEADING_REASONING")
)
# Set for models whose chat template prefills <think>, so replies arrive
# as "...</think>answer" without the opening tag.
self.prefilled_reasoning = (
prefilled_reasoning
if prefilled_reasoning is not None
else prefilled_reasoning_from_env("OLLAMA_PREFILLED_REASONING")
)
self.base_url = base_url or os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434")
self.default_model = default_model or os.environ.get("OLLAMA_MODEL", "llama3.2")
self.default_model = default_model or read_model_env("OLLAMA_MODEL") or "llama3.2"
self._models = [
ModelInfo(
name="llama3.2",
provider=ProviderType.OLLAMA,
supports_tools=False,
supports_tools=True,
supports_vision=False,
max_tokens=4096,
context_window=128000,
@@ -42,7 +90,7 @@ class OllamaProvider(LLMProvider):
ModelInfo(
name="mistral",
provider=ProviderType.OLLAMA,
supports_tools=False,
supports_tools=True,
supports_vision=False,
max_tokens=4096,
context_window=8192,
@@ -56,22 +104,62 @@ class OllamaProvider(LLMProvider):
context_window=16384,
),
]
self._tool_support: dict[str, bool] = {}
def model_supports_tools(self, model: str) -> bool:
"""Whether ``model`` declares tool support.
Catalogued models answer from :attr:`ModelInfo.supports_tools`. Any other
model is looked up once through ``/api/show``, whose ``capabilities``
list is what the installed model itself declares. Only an answered lookup
is cached: a failed one counts as no tool support for that request and
is retried on the next.
"""
for info in self._models:
if info.name == model:
return info.supports_tools
if model in self._tool_support:
return self._tool_support[model]
try:
req = urllib.request.Request(
f"{self.base_url}/api/show",
data=json.dumps({"model": model}).encode("utf-8"),
headers={"Content-Type": "application/json"},
)
with urllib.request.urlopen(req, timeout=10) as response:
capabilities = json.loads(response.read().decode("utf-8")).get("capabilities") or []
except Exception as e:
logger.warning("Could not read capabilities for Ollama model '%s': %s", model, type(e).__name__)
return False
if not isinstance(capabilities, list) or not all(isinstance(item, str) for item in capabilities):
logger.warning("Invalid capabilities for Ollama model '%s'; sending no tools", model)
return False
supports_tools = "tools" in capabilities
self._tool_support = {**self._tool_support, model: supports_tools}
return supports_tools
def generate(self, input: LLMInput) -> LLMOutput:
import json
import urllib.request
try:
url = f"{self.base_url}/api/chat"
model = input.model or self.default_model
payload: dict[str, Any] = {
"model": model,
"messages": [msg.to_dict() for msg in input.messages],
"messages": _messages_payload(input.messages),
"stream": False,
}
options: dict[str, Any] = {}
if input.temperature != 1.0:
payload["options"] = {"temperature": input.temperature}
options["temperature"] = input.temperature
if input.max_tokens is not None:
options["num_predict"] = input.max_tokens
if options:
payload["options"] = options
if input.tools:
if self.model_supports_tools(model):
payload["tools"] = [tool.to_openai_tool() for tool in input.tools]
else:
logger.warning("Ollama model '%s' does not declare tool support; sending no tools", model)
data = json.dumps(payload).encode("utf-8")
req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
@@ -79,13 +167,19 @@ class OllamaProvider(LLMProvider):
with urllib.request.urlopen(req, timeout=60) as response:
result = json.loads(response.read().decode("utf-8"))
content = result.get("message", {}).get("content", "")
content = strip_reasoning(
result.get("message", {}).get("content", ""),
leading=self.leading_reasoning,
prefilled=self.prefilled_reasoning
)
tool_calls = None
if result.get("message", {}).get("tool_calls"):
tool_calls = [
ToolCall(
id=tc.get("id", ""),
# Native Ollama calls often carry no id; give each one a
# unique id so its result can be matched to it.
id=tc.get("id") or f"call_{uuid.uuid4().hex[:24]}",
name=tc.get("function", {}).get("name", ""),
arguments=tc.get("function", {}).get("arguments", {}),
)
@@ -100,13 +194,33 @@ class OllamaProvider(LLMProvider):
)
except Exception as e:
msg = str(e)
if "401" in msg or "connection" in msg.lower():
raise AuthenticationError(f"Ollama connection failed: {msg}", provider=ProviderType.OLLAMA) from e
if "429" in msg or "rate_limit" in msg.lower():
lowered = msg.lower()
if "401" in msg or "unauthorized" in lowered or "forbidden" in lowered:
raise AuthenticationError(f"Ollama authentication failed: {msg}", provider=ProviderType.OLLAMA) from e
if "429" in msg or "rate_limit" in lowered:
raise RateLimitError(msg, provider=ProviderType.OLLAMA) from e
if "context" in msg.lower() and "length" in msg.lower():
if "context" in lowered and "length" in lowered:
raise ContextLengthError(msg, provider=ProviderType.OLLAMA) from e
raise
if (
"connection" in lowered
or "refused" in lowered
or "timed out" in lowered
or "timeout" in lowered
or "unreachable" in lowered
or "name resolution" in lowered
or "nodename nor servname" in lowered
or isinstance(e, (ConnectionError, TimeoutError))
):
raise LLMError(
f"Ollama connection failed: {type(e).__name__}",
provider=ProviderType.OLLAMA,
code="connection_error",
) from e
raise LLMError(
f"Ollama request failed: {type(e).__name__}",
provider=ProviderType.OLLAMA,
code="provider_error",
) from e
def list_models(self) -> list[ModelInfo]:
return self._models.copy()
+33 -5
View File
@@ -22,17 +22,41 @@ from llm.core.types import (
ToolCall,
)
from llm.providers.constants import EMPTY_FILTERED_RESPONSE_ERROR
from llm.providers.openai_compat import to_openai_message_param
from llm.providers.reasoning import prefilled_reasoning_from_env, strip_reasoning
class OpenAIProvider(LLMProvider):
provider_type = ProviderType.OPENAI
def __init__(self, api_key: str | None = None, base_url: str | None = None) -> None:
def __init__(
self,
api_key: str | None = None,
base_url: str | None = None,
default_model: str | None = None,
prefilled_reasoning: bool | None = None,
leading_reasoning: bool | None = None,
) -> None:
# Enable only for a deployment that declares balanced leading reasoning.
# Generic responses can contain literal XML and stay intact by default.
self.leading_reasoning = (
leading_reasoning
if leading_reasoning is not None
else prefilled_reasoning_from_env("OPENAI_LEADING_REASONING")
)
# Set for self-hosted servers whose chat template prefills <think>, so
# replies arrive as "...</think>answer" without the opening tag.
self.prefilled_reasoning = (
prefilled_reasoning
if prefilled_reasoning is not None
else prefilled_reasoning_from_env("OPENAI_PREFILLED_REASONING")
)
self.client = OpenAI(
api_key=api_key or os.environ.get("OPENAI_API_KEY"),
base_url=base_url,
_enforce_credentials=False,
)
self.default_model = default_model or "gpt-4o-mini"
self._models = [
ModelInfo(
name="gpt-4o",
@@ -71,8 +95,8 @@ class OpenAIProvider(LLMProvider):
def generate(self, input: LLMInput) -> LLMOutput:
try:
params: dict[str, Any] = {
"model": input.model or "gpt-4o-mini",
"messages": [msg.to_dict() for msg in input.messages],
"model": input.model or self.default_model,
"messages": [to_openai_message_param(msg) for msg in input.messages],
"temperature": input.temperature,
}
if input.max_tokens:
@@ -105,7 +129,11 @@ class OpenAIProvider(LLMProvider):
}
return LLMOutput(
content=choice.message.content or "",
content=strip_reasoning(
choice.message.content or "",
leading=self.leading_reasoning,
prefilled=self.prefilled_reasoning,
),
tool_calls=tool_calls,
model=response.model,
usage=usage,
@@ -128,4 +156,4 @@ class OpenAIProvider(LLMProvider):
return bool(self.client.api_key)
def get_default_model(self) -> str:
return "gpt-4o-mini"
return self.default_model
+88
View File
@@ -0,0 +1,88 @@
"""Message adapters shared by OpenAI-compatible providers."""
from __future__ import annotations
import json
from typing import TYPE_CHECKING, Any, cast
from llm.core.types import Message, Role, ToolCall
if TYPE_CHECKING:
from openai.types.chat import (
ChatCompletionAssistantMessageParam,
ChatCompletionMessageParam,
ChatCompletionMessageToolCallParam,
ChatCompletionSystemMessageParam,
ChatCompletionToolMessageParam,
ChatCompletionUserMessageParam,
)
def _to_openai_tool_call_param(
tool_call: ToolCall,
) -> ChatCompletionMessageToolCallParam:
"""Convert a domain tool call to the OpenAI request schema."""
try:
arguments = json.dumps(tool_call.arguments, allow_nan=False)
except (TypeError, ValueError) as exc:
raise ValueError("OpenAI tool call arguments must be valid JSON") from exc
return {
"id": tool_call.id,
"type": "function",
"function": {
"name": tool_call.name,
"arguments": arguments,
},
}
def to_openai_message_param(message: Message) -> ChatCompletionMessageParam:
"""Convert a domain message to the OpenAI chat-completions schema."""
if message.role == Role.SYSTEM:
if not isinstance(message.content, str):
raise TypeError("OpenAI system messages must contain text")
system_message: ChatCompletionSystemMessageParam = {
"role": "system",
"content": message.content,
}
if message.name:
system_message["name"] = message.name
return system_message
if message.role == Role.USER:
user_message: ChatCompletionUserMessageParam = {
"role": "user",
"content": cast(Any, message.content),
}
if message.name:
user_message["name"] = message.name
return user_message
if message.role == Role.ASSISTANT:
assistant_message: ChatCompletionAssistantMessageParam = {
"role": "assistant",
"content": cast(Any, message.content),
}
if message.name:
assistant_message["name"] = message.name
if message.tool_calls:
assistant_message["tool_calls"] = [
_to_openai_tool_call_param(tool_call)
for tool_call in message.tool_calls
]
return assistant_message
if message.role == Role.TOOL:
if not message.tool_call_id:
raise ValueError("OpenAI tool messages require a tool_call_id")
if not isinstance(message.content, str):
raise TypeError("OpenAI tool messages must contain text")
tool_message: ChatCompletionToolMessageParam = {
"role": "tool",
"content": message.content,
"tool_call_id": message.tool_call_id,
}
return tool_message
raise ValueError(f"Unsupported OpenAI message role: {message.role}")
+73
View File
@@ -0,0 +1,73 @@
"""Helpers for reasoning text that self-hosted models leave in message content."""
from __future__ import annotations
import json
import os
THINK_OPEN = "<think>"
THINK_CLOSE = "</think>"
_TRUTHY = {"1", "true", "yes", "on"}
def prefilled_reasoning_from_env(name: str) -> bool:
"""Read an explicit reasoning-format flag from the environment."""
return os.environ.get(name, "").strip().lower() in _TRUTHY
def strip_reasoning(
content: str, *, leading: bool = False, prefilled: bool = False
) -> str:
"""Drop a leading reasoning block from model output.
Some deployments put reasoning in ``content`` instead of a separate field.
This helper makes no assumptions about a hosted API or model name.
Content is preserved by default, including complete leading XML documents.
Enable ``leading=True`` only for a deployment whose declared format is
``<think>...</think>answer``. Unterminated reasoning remains unchanged.
``prefilled=True`` is for deployments whose chat template prefills the
opening tag in the generation prompt, so the model only ever emits
``...</think>answer``. Only enable it when the model is known to behave this
way: everything up to the first closing tag is then treated as reasoning.
"""
if not leading and not prefilled:
return content
stripped = content.lstrip()
if stripped.startswith(THINK_OPEN):
close = stripped.find(THINK_CLOSE, len(THINK_OPEN))
if close == -1:
return content
return stripped[close + len(THINK_CLOSE) :].lstrip()
if not prefilled:
return content
close = content.find(THINK_CLOSE)
if close == -1 or THINK_OPEN in content[:close] or _is_json_document(content):
return content
return content[close + len(THINK_CLOSE) :].lstrip()
def _is_json_document(content: str) -> bool:
"""Whether ``content`` is one complete JSON object or array.
Reasoning followed by a closing tag is never valid JSON, so a structured
answer that carries the tag inside a string value is left whole. Input
nested too deeply for the parser to finish is kept whole as well: reasoning
does not open with that many brackets, so cutting it would only risk
truncating a real answer.
"""
stripped = content.strip()
if not stripped.startswith(("{", "[")):
return False
try:
json.loads(stripped)
except RecursionError:
return True
except ValueError:
return False
return True
+52 -7
View File
@@ -11,6 +11,7 @@ from llm.providers.astraflow import AstraflowCNProvider, AstraflowProvider
from llm.providers.atlas import AtlasProvider
from llm.providers.claude import ClaudeProvider
from llm.providers.minimax import MiniMaxProvider
from llm.providers.model_config import read_model_env
from llm.providers.ollama import OllamaProvider
from llm.providers.openai import OpenAIProvider
@@ -24,13 +25,24 @@ _PROVIDER_MAP: dict[ProviderType, type[LLMProvider]] = {
ProviderType.OLLAMA: OllamaProvider,
}
# Only built-in adapters have a known default_model constructor contract.
_MODEL_ENV_VARS: dict[type[LLMProvider], tuple[str, ...]] = {
ClaudeProvider: (),
OpenAIProvider: (),
OllamaProvider: ("OLLAMA_MODEL",),
AtlasProvider: (AtlasProvider.model_env,),
AstraflowProvider: (AstraflowProvider.model_env,),
AstraflowCNProvider: (AstraflowCNProvider.model_env, AstraflowProvider.model_env),
MiniMaxProvider: (MiniMaxProvider.model_env,),
}
LLM_ENV_FILE = ".llm.env"
def _strip_env_value(value: str) -> str:
value = value.strip()
if len(value) >= 2 and value[0] == value[-1] and value[0] in {"'", '"'}:
return value[1:-1]
return value[1:-1].strip()
return value
@@ -49,20 +61,48 @@ def _read_saved_llm_config(env_path: str | Path = LLM_ENV_FILE) -> dict[str, str
return config
def _resolve_provider_type(provider_type: ProviderType | str | None) -> ProviderType | str:
def _resolve_provider_config(
provider_type: ProviderType | str | None,
) -> tuple[ProviderType | str, str | None]:
if provider_type is not None:
return provider_type
return provider_type, None
env_provider = os.environ.get("LLM_PROVIDER")
if env_provider:
return _strip_env_value(env_provider).lower()
return _strip_env_value(env_provider).lower(), None
saved_config = _read_saved_llm_config()
return saved_config.get("LLM_PROVIDER", "claude").lower()
saved_provider = saved_config.get("LLM_PROVIDER", "").lower()
# A saved model belongs to its saved provider, never to an override.
saved_model = saved_config.get("LLM_MODEL") if saved_provider else None
return saved_provider or "claude", saved_model
def get_provider(provider_type: ProviderType | str | None = None, **kwargs: str) -> LLMProvider:
provider_type = _resolve_provider_type(provider_type)
def _resolve_default_model(
provider_type: ProviderType, provider_cls: type[LLMProvider], saved_model: str | None,
) -> str | None:
for name in _MODEL_ENV_VARS[provider_cls]:
model = read_model_env(name)
if model:
return model
env_provider = _strip_env_value(os.environ.get("LLM_PROVIDER", "")).lower()
if not env_provider or env_provider == provider_type.value:
env_model = read_model_env("LLM_MODEL")
if env_model:
return env_model
return saved_model or None
def get_provider(provider_type: ProviderType | str | None = None, **kwargs: str | None) -> LLMProvider:
"""Create a provider, honoring the model selected by llm-select.
Built-in model defaults prefer explicit kwargs, then existing provider-specific
environment variables, LLM_MODEL, and the saved provider/model pair. Saved
config is only read when neither an explicit nor an environment provider wins.
Custom providers retain their own constructor and configuration semantics.
"""
provider_type, saved_model = _resolve_provider_config(provider_type)
if isinstance(provider_type, str):
try:
@@ -74,6 +114,11 @@ def get_provider(provider_type: ProviderType | str | None = None, **kwargs: str)
if not provider_cls:
raise ValueError(f"No provider registered for type: {provider_type}")
if not kwargs.get("default_model") and provider_cls in _MODEL_ENV_VARS:
model = _resolve_default_model(provider_type, provider_cls, saved_model)
if model:
kwargs = {**kwargs, "default_model": model}
return provider_cls(**kwargs)
+63 -4
View File
@@ -2,10 +2,13 @@
from __future__ import annotations
import inspect
import logging
from collections.abc import Callable
from copy import deepcopy
from typing import Any, Protocol
from llm.core.interface import LLMError
from llm.core.types import (
LLMInput,
LLMOutput,
@@ -16,6 +19,21 @@ from llm.core.types import (
ToolResult,
)
logger = logging.getLogger(__name__)
# Model-facing failure text. Raw exception details (credentials, local paths,
# request data, upstream responses) must never reach the model; diagnostics go
# to trusted logs only.
GENERIC_TOOL_FAILURE = "Error executing {name}: tool failed"
def _generic_failure(tool_call: ToolCall) -> ToolResult:
return ToolResult(
tool_call_id=tool_call.id,
content=GENERIC_TOOL_FAILURE.format(name=tool_call.name),
is_error=True,
)
ToolFunc = Callable[..., Any]
@@ -60,18 +78,52 @@ class ToolExecutor:
try:
result = func(**tool_call.arguments)
if inspect.isawaitable(result):
logger.warning(
"Async tool '%s' called via sync execute(); use execute_async()",
tool_call.name,
)
if inspect.iscoroutine(result):
result.close()
return ToolResult(
tool_call_id=tool_call.id,
content=GENERIC_TOOL_FAILURE.format(name=tool_call.name),
is_error=True,
)
content = result if isinstance(result, str) else str(result)
return ToolResult(tool_call_id=tool_call.id, content=content)
except Exception as e:
except Exception:
logger.exception("Tool '%s' failed", tool_call.name)
return _generic_failure(tool_call)
async def execute_async(self, tool_call: ToolCall) -> ToolResult:
func = self.registry.get(tool_call.name)
if not func:
return ToolResult(
tool_call_id=tool_call.id,
content=f"Error executing {tool_call.name}: {e}",
content=f"Error: Tool '{tool_call.name}' not found",
is_error=True,
)
try:
result = func(**tool_call.arguments)
if inspect.isawaitable(result):
result = await result
content = result if isinstance(result, str) else str(result)
return ToolResult(tool_call_id=tool_call.id, content=content)
except Exception:
logger.exception("Tool '%s' failed", tool_call.name)
return _generic_failure(tool_call)
def execute_all(self, tool_calls: list[ToolCall]) -> list[ToolResult]:
return [self.execute(tc) for tc in tool_calls]
async def execute_all_async(self, tool_calls: list[ToolCall]) -> list[ToolResult]:
results: list[ToolResult] = []
for tc in tool_calls:
results.append(await self.execute_async(tc))
return results
class ReActAgent:
def __init__(
@@ -98,7 +150,14 @@ class ReActAgent:
metadata=deepcopy(input.metadata),
)
output: LLMOutput = self.provider.generate(input_copy)
try:
output: LLMOutput = self.provider.generate(input_copy)
except LLMError as e:
logger.warning("Provider failed during agent run: %s", e.code or type(e).__name__)
return LLMOutput(
content=f"Provider error: {e.code or type(e).__name__}",
stop_reason="provider_error",
)
tool_calls = output.tool_calls
if not tool_calls:
@@ -113,7 +172,7 @@ class ReActAgent:
)
)
results = self.executor.execute_all(tool_calls)
results = await self.executor.execute_all_async(tool_calls)
for result in results:
messages.append(