refactor: move tool availability checks into registration
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import os
|
||||
|
||||
from strix.config import Config
|
||||
|
||||
from .agents_graph import * # noqa: F403
|
||||
from .browser import * # noqa: F403
|
||||
from .executor import (
|
||||
execute_tool,
|
||||
execute_tool_invocation,
|
||||
@@ -11,6 +9,12 @@ from .executor import (
|
||||
remove_screenshot_from_result,
|
||||
validate_tool_availability,
|
||||
)
|
||||
from .file_edit import * # noqa: F403
|
||||
from .finish import * # noqa: F403
|
||||
from .load_skill import * # noqa: F403
|
||||
from .notes import * # noqa: F403
|
||||
from .proxy import * # noqa: F403
|
||||
from .python import * # noqa: F403
|
||||
from .registry import (
|
||||
ImplementedInClientSideOnlyError,
|
||||
get_tool_by_name,
|
||||
@@ -20,53 +24,13 @@ from .registry import (
|
||||
register_tool,
|
||||
tools,
|
||||
)
|
||||
from .reporting import * # noqa: F403
|
||||
from .terminal import * # noqa: F403
|
||||
from .thinking import * # noqa: F403
|
||||
from .todo import * # noqa: F403
|
||||
from .web_search import * # noqa: F403
|
||||
|
||||
|
||||
SANDBOX_MODE = os.getenv("STRIX_SANDBOX_MODE", "false").lower() == "true"
|
||||
|
||||
|
||||
def _is_browser_disabled() -> bool:
|
||||
if os.getenv("STRIX_DISABLE_BROWSER", "").lower() == "true":
|
||||
return True
|
||||
val: str = Config.load().get("env", {}).get("STRIX_DISABLE_BROWSER", "")
|
||||
return str(val).lower() == "true"
|
||||
|
||||
|
||||
DISABLE_BROWSER = _is_browser_disabled()
|
||||
|
||||
|
||||
def _has_perplexity_api() -> bool:
|
||||
if os.getenv("PERPLEXITY_API_KEY"):
|
||||
return True
|
||||
return bool(Config.load().get("env", {}).get("PERPLEXITY_API_KEY"))
|
||||
|
||||
|
||||
if not SANDBOX_MODE:
|
||||
from .agents_graph import * # noqa: F403
|
||||
|
||||
if not DISABLE_BROWSER:
|
||||
from .browser import * # noqa: F403
|
||||
from .file_edit import * # noqa: F403
|
||||
from .finish import * # noqa: F403
|
||||
from .load_skill import * # noqa: F403
|
||||
from .notes import * # noqa: F403
|
||||
from .proxy import * # noqa: F403
|
||||
from .python import * # noqa: F403
|
||||
from .reporting import * # noqa: F403
|
||||
from .terminal import * # noqa: F403
|
||||
from .thinking import * # noqa: F403
|
||||
from .todo import * # noqa: F403
|
||||
|
||||
if _has_perplexity_api():
|
||||
from .web_search import * # noqa: F403
|
||||
else:
|
||||
if not DISABLE_BROWSER:
|
||||
from .browser import * # noqa: F403
|
||||
from .file_edit import * # noqa: F403
|
||||
from .proxy import * # noqa: F403
|
||||
from .python import * # noqa: F403
|
||||
from .terminal import * # noqa: F403
|
||||
|
||||
__all__ = [
|
||||
"ImplementedInClientSideOnlyError",
|
||||
"execute_tool",
|
||||
|
||||
@@ -180,7 +180,7 @@ def _handle_utility_actions(
|
||||
raise ValueError(f"Unknown utility action: {action}")
|
||||
|
||||
|
||||
@register_tool
|
||||
@register_tool(requires_browser_mode=True)
|
||||
def browser_action(
|
||||
action: BrowserAction,
|
||||
url: str | None = None,
|
||||
|
||||
@@ -149,10 +149,60 @@ def _get_schema_path(func: Callable[..., Any]) -> Path | None:
|
||||
return get_strix_resource_path("tools", folder, schema_file)
|
||||
|
||||
|
||||
def _is_sandbox_mode() -> bool:
|
||||
return os.getenv("STRIX_SANDBOX_MODE", "false").lower() == "true"
|
||||
|
||||
|
||||
def _is_browser_disabled() -> bool:
|
||||
if os.getenv("STRIX_DISABLE_BROWSER", "").lower() == "true":
|
||||
return True
|
||||
|
||||
from strix.config import Config
|
||||
|
||||
val: str = Config.load().get("env", {}).get("STRIX_DISABLE_BROWSER", "")
|
||||
return str(val).lower() == "true"
|
||||
|
||||
|
||||
def _has_perplexity_api() -> bool:
|
||||
if os.getenv("PERPLEXITY_API_KEY"):
|
||||
return True
|
||||
|
||||
from strix.config import Config
|
||||
|
||||
return bool(Config.load().get("env", {}).get("PERPLEXITY_API_KEY"))
|
||||
|
||||
|
||||
def _should_register_tool(
|
||||
*,
|
||||
sandbox_execution: bool,
|
||||
requires_browser_mode: bool,
|
||||
requires_web_search_mode: bool,
|
||||
) -> bool:
|
||||
sandbox_mode = _is_sandbox_mode()
|
||||
|
||||
if sandbox_mode and not sandbox_execution:
|
||||
return False
|
||||
if requires_browser_mode and _is_browser_disabled():
|
||||
return False
|
||||
return not (requires_web_search_mode and not _has_perplexity_api())
|
||||
|
||||
|
||||
def register_tool(
|
||||
func: Callable[..., Any] | None = None, *, sandbox_execution: bool = True
|
||||
func: Callable[..., Any] | None = None,
|
||||
*,
|
||||
sandbox_execution: bool = True,
|
||||
requires_browser_mode: bool = False,
|
||||
requires_web_search_mode: bool = False,
|
||||
) -> Callable[..., Any]:
|
||||
def decorator(f: Callable[..., Any]) -> Callable[..., Any]:
|
||||
sandbox_mode = _is_sandbox_mode()
|
||||
if not _should_register_tool(
|
||||
sandbox_execution=sandbox_execution,
|
||||
requires_browser_mode=requires_browser_mode,
|
||||
requires_web_search_mode=requires_web_search_mode,
|
||||
):
|
||||
return f
|
||||
|
||||
func_dict = {
|
||||
"name": f.__name__,
|
||||
"function": f,
|
||||
@@ -160,7 +210,6 @@ def register_tool(
|
||||
"sandbox_execution": sandbox_execution,
|
||||
}
|
||||
|
||||
sandbox_mode = os.getenv("STRIX_SANDBOX_MODE", "false").lower() == "true"
|
||||
if not sandbox_mode:
|
||||
try:
|
||||
schema_path = _get_schema_path(f)
|
||||
|
||||
@@ -31,7 +31,7 @@ Structure your response to be comprehensive yet concise, emphasizing the most cr
|
||||
security implications and details."""
|
||||
|
||||
|
||||
@register_tool(sandbox_execution=False)
|
||||
@register_tool(sandbox_execution=False, requires_web_search_mode=True)
|
||||
def web_search(query: str) -> dict[str, Any]:
|
||||
try:
|
||||
api_key = os.getenv("PERPLEXITY_API_KEY")
|
||||
|
||||
Reference in New Issue
Block a user