-
Notifications
You must be signed in to change notification settings - Fork 55
Feat/scenario improvements #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
621d2f0
feat: add scenario improvements and agent factory
lorenss-m d70d2b0
scenario as tool simplification
lorenss-m b2de659
change agent resolution for easier model switching
lorenss-m 32b3118
agent tool does not get optional params (eval params)
lorenss-m 8f9f2ba
fix tests
lorenss-m b957818
change routing logic and add tests
lorenss-m 219f255
lint
lorenss-m 627a6e3
add convenience back
lorenss-m 85aad98
fix edge cases
lorenss-m 8e6b186
mock path fixes
lorenss-m 760f6c8
change import paths
lorenss-m 2a5f10b
format
lorenss-m 99fd3c2
fix agent edge cases
lorenss-m d74edb4
nested tracing
lorenss-m 6415762
fix tests
lorenss-m 7027550
agent tool examples
lorenss-m 332f42d
docs link
lorenss-m 5325d29
fix env connector
lorenss-m f17a93b
add routing and tools updates for remote
lorenss-m c4188d2
add tests to remote connectors and improve connection
lorenss-m 9f95e0f
more precise tests
lorenss-m f9e18eb
fix: strip format field from JSON schemas for OpenAI strict mode
lorenss-m 757d645
Merge remote-tracking branch 'origin/main' into feat/scenario-improve…
lorenss-m f3c9e0c
move
lorenss-m 2f67cde
Merge main into feat/scenario-improvements, combine cookbooks
lorenss-m ff91f24
rm commit
lorenss-m b1c91b5
format
lorenss-m cd0cc40
provider fix
lorenss-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,66 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from .base import MCPAgent | ||
| from .openai import OpenAIAgent | ||
| from .openai_chat import OpenAIChatAgent | ||
| from .operator import OperatorAgent | ||
| from .resolver import resolve_cls | ||
|
|
||
| # Note: These agents are not exported here to avoid requiring optional dependencies. | ||
| # Import directly if needed: | ||
| # from hud.agents.claude import ClaudeAgent # requires anthropic | ||
| # from hud.agents.gemini import GeminiAgent # requires google-genai | ||
| # from hud.agents.gemini_cua import GeminiCUAAgent # requires google-genai | ||
| if TYPE_CHECKING: | ||
| from hud.types import AgentType | ||
|
|
||
| __all__ = [ | ||
| "MCPAgent", | ||
| "OpenAIAgent", | ||
| "OpenAIChatAgent", | ||
| "OperatorAgent", | ||
| "create_agent", | ||
| "resolve_cls", | ||
| ] | ||
|
|
||
|
|
||
| def create_agent(model: str | AgentType, **kwargs: Any) -> MCPAgent: | ||
| """Create an agent from a model string or AgentType. | ||
|
|
||
| Args: | ||
| model: AgentType ("claude"), or gateway model name ("gpt-4o"). | ||
| **kwargs: Params passed to agent.create(). | ||
|
|
||
| Example: | ||
| ```python | ||
| agent = create_agent("claude", model="claude-sonnet-4-5") | ||
| agent = create_agent("gpt-4o") # auto-configures gateway | ||
| ``` | ||
| """ | ||
| from hud.types import AgentType as AT | ||
|
|
||
| # AgentType enum → just create | ||
| if isinstance(model, AT): | ||
| return model.cls.create(**kwargs) | ||
|
|
||
| # Resolve class and optional gateway info | ||
| agent_cls, gateway_info = resolve_cls(model) | ||
|
|
||
| # If not a gateway model, just create | ||
| if gateway_info is None: | ||
| return agent_cls.create(**kwargs) | ||
|
|
||
| # Build gateway params | ||
| model_id = gateway_info.get("model") or gateway_info.get("id") or model | ||
| kwargs.setdefault("model", model_id) | ||
| kwargs.setdefault("validate_api_key", False) | ||
|
|
||
| # Build model_client based on provider | ||
| if "model_client" not in kwargs and "openai_client" not in kwargs: | ||
| from hud.agents.gateway import build_gateway_client | ||
|
|
||
| provider = gateway_info.get("provider", "openai_compatible") | ||
| client = build_gateway_client(provider) | ||
|
|
||
| # OpenAIChatAgent uses openai_client key, others use model_client | ||
| key = "openai_client" if agent_cls == OpenAIChatAgent else "model_client" | ||
| kwargs[key] = client | ||
|
|
||
| return agent_cls.create(**kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| """Gateway client utilities for HUD inference gateway.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
|
|
||
| def build_gateway_client(provider: str) -> Any: | ||
| """Build a client configured for HUD gateway routing. | ||
|
|
||
| Args: | ||
| provider: Provider name ("anthropic", "openai", "gemini", etc.) | ||
|
|
||
| Returns: | ||
| Configured async client for the provider. | ||
| """ | ||
| from hud.settings import settings | ||
|
|
||
| provider = provider.lower() | ||
|
|
||
| if provider == "anthropic": | ||
| from anthropic import AsyncAnthropic | ||
|
|
||
| return AsyncAnthropic(api_key=settings.api_key, base_url=settings.hud_gateway_url) | ||
|
|
||
| if provider == "gemini": | ||
| from google import genai | ||
| from google.genai.types import HttpOptions | ||
|
|
||
| return genai.Client( | ||
| api_key="PLACEHOLDER", | ||
| http_options=HttpOptions( | ||
| api_version="v1beta", | ||
| base_url=settings.hud_gateway_url, | ||
| headers={"Authorization": f"Bearer {settings.api_key}"}, | ||
| ), | ||
| ) | ||
|
|
||
| # OpenAI-compatible (openai, azure, together, groq, fireworks, etc.) | ||
| from openai import AsyncOpenAI | ||
|
|
||
| return AsyncOpenAI(api_key=settings.api_key, base_url=settings.hud_gateway_url) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """Model resolution - maps model strings to agent classes.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| if TYPE_CHECKING: | ||
| from hud.agents.base import MCPAgent | ||
|
|
||
| __all__ = ["resolve_cls"] | ||
|
|
||
| _models_cache: list[dict[str, Any]] | None = None | ||
|
|
||
| # Provider name → AgentType value (only anthropic differs) | ||
| _PROVIDER_TO_AGENT = {"anthropic": "claude"} | ||
|
|
||
|
|
||
| def _fetch_gateway_models() -> list[dict[str, Any]]: | ||
| """Fetch available models from HUD gateway (cached).""" | ||
| global _models_cache | ||
| if _models_cache is not None: | ||
| return _models_cache | ||
|
|
||
| import httpx | ||
|
|
||
| from hud.settings import settings | ||
|
|
||
| if not settings.api_key: | ||
| return [] | ||
|
|
||
| try: | ||
| resp = httpx.get( | ||
| f"{settings.hud_gateway_url}/models", | ||
| headers={"Authorization": f"Bearer {settings.api_key}"}, | ||
| timeout=10.0, | ||
| ) | ||
| resp.raise_for_status() | ||
| data = resp.json() | ||
| _models_cache = data.get("data", data) if isinstance(data, dict) else data | ||
| return _models_cache or [] | ||
| except Exception: | ||
| return [] | ||
|
|
||
|
|
||
| def resolve_cls(model: str) -> tuple[type[MCPAgent], dict[str, Any] | None]: | ||
| """Resolve model string to (agent_class, gateway_info). | ||
| Returns: | ||
| (agent_class, None) for known AgentTypes | ||
| (agent_class, gateway_model_info) for gateway models | ||
| """ | ||
| from hud.types import AgentType | ||
|
|
||
| # Known AgentType → no gateway info | ||
| try: | ||
| return AgentType(model).cls, None | ||
| except ValueError: | ||
| pass | ||
|
|
||
| # Gateway lookup | ||
| for m in _fetch_gateway_models(): | ||
| if model in (m.get("id"), m.get("name"), m.get("model")): | ||
| provider = m.get("provider", "openai_compatible").lower() | ||
| agent_str = _PROVIDER_TO_AGENT.get(provider, provider) | ||
| try: | ||
| return AgentType(agent_str).cls, m | ||
| except ValueError: | ||
| return AgentType.OPENAI_COMPATIBLE.cls, m | ||
|
|
||
| raise ValueError(f"Model '{model}' not found") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.