"""
The tool runner: where a resolved tool call is executed.
:func:`get_tool_runner` returns the runner that every tool call goes through, as the setting
``app_llmtool_tool_runner`` (``SMARTER_APP_LLMTOOL_TOOL_RUNNER``) chooses:
- :class:`InProcessToolRunner`, the default, executes the call in the app's own process.
- :class:`RemoteToolRunner` sends it, as JSON, to the tool worker, ``smarter-worker-tools``, through the
Celery queue ``app_tools_tasks_celery_task_queue``, and waits for its result. The worker holds no
database, no Smarter Secret and no cloud credential: each call brings what it needs (:doc:`ADR-038
</adr/038-tool-isolation-and-egress>`).
The code that resolves a call is the same either way. Each kind of tool registers its executor in
:data:`TOOL_EXECUTORS`, and its call's model in :data:`TOOL_CALL_MODELS`, by dotted path. A path, rather
than an import, keeps this module free of the apps that it runs tools for, several of which import
from :mod:`smarter.apps.llmtool`.
Inside the tool worker, every call runs in process, whatever the setting says: an executor that uses
the runner itself, e.g. the web search page fetch's robots.txt, must not send its call back to the queue
that it is serving (:func:`running_in_tool_worker`).
"""
from abc import ABC, abstractmethod
from contextlib import contextmanager
from contextvars import ContextVar
from typing import Callable, Iterator, Optional, ParamSpec, TypeVar
from django.utils.module_loading import import_string
from smarter.common.conf import smarter_settings
from smarter.common.exceptions import SmarterConfigurationError
from smarter.lib import logging
from .models import ToolCall, ToolCallLabel, ToolCallResult
# not waffle switched: the tool worker has no database to read a switch from, and some calls run in
# worker threads, where a switch would open a database connection per thread.
logger = logging.getSmarterLogger(__name__)
P = ParamSpec("P")
R = TypeVar("R")
ToolExecutor = Callable[[ToolCall], ToolCallResult]
"""A function that executes one kind of resolved tool call, and returns its result."""
TOOL_EXECUTORS: dict[str, str] = {
"api": "smarter.apps.connection.execution.execute_api_call",
"extension": "smarter.apps.llmtool.extension.execution.execute_extension_call",
"http": "smarter.apps.llmtool.runner.http.execute_http_call",
"webfetch": "smarter.apps.llmtool.plugin.websearch_fetch.execute_webfetch_call",
"mcp": "smarter.apps.mcpclient.execution.execute_mcp_call",
"sql": "smarter.apps.connection.execution.execute_sql_call",
}
"""Each kind of :class:`~smarter.apps.llmtool.runner.models.ToolCall`, and the dotted path of its executor."""
TOOL_CALL_MODELS: dict[str, str] = {
"api": "smarter.apps.connection.execution.ApiToolCall",
"extension": "smarter.apps.llmtool.extension.execution.ExtensionToolCall",
"http": "smarter.apps.llmtool.runner.http.HttpToolCall",
"webfetch": "smarter.apps.llmtool.plugin.websearch_fetch.WebFetchToolCall",
"mcp": "smarter.apps.mcpclient.execution.MCPToolCall",
"sql": "smarter.apps.connection.execution.SqlToolCall",
}
"""Each kind of tool call, and the dotted path of its model, which rebuilds a call from its JSON in the tool worker."""
DEFAULT_CALL_TIMEOUT_SECONDS = 30.0
"""The timeout of a call that names none of its own."""
REMOTE_TIMEOUT_FACTOR = 4
"""
How many times a call's own timeout the remote runner waits for its result.
A call can make several requests, each with that timeout: an MCP call connects and then calls, a page
fetch reads robots.txt and follows redirects. The executor's own timeouts end the call; this only keeps
the app from waiting for a worker that is gone.
"""
REMOTE_TIMEOUT_MARGIN_SECONDS = 15.0
"""Seconds that the remote runner adds to its wait, for the queue and the worker's start."""
MAX_REMOTE_TIMEOUT_SECONDS = 600.0
"""The longest that the remote runner waits for a result."""
_in_tool_worker: ContextVar[bool] = ContextVar("in_tool_worker", default=False)
_tool_call_label: ContextVar[Optional[ToolCallLabel]] = ContextVar("smarter_tool_call_label", default=None)
[docs]
def executor_for(kind: str) -> ToolExecutor:
"""
Return the executor of a kind of tool call.
:param kind: The call's ``kind``.
:returns: The executor.
:raises SmarterConfigurationError: If no executor is registered for the kind.
"""
path = TOOL_EXECUTORS.get(kind)
if path is None:
raise SmarterConfigurationError(f"no tool executor is registered for the kind {kind!r}.")
executor: ToolExecutor = import_string(path)
return executor
[docs]
def call_model_for(kind: str) -> type[ToolCall]:
"""
Return the model of a kind of tool call.
:param kind: The call's ``kind``.
:returns: The model, a subclass of :class:`~smarter.apps.llmtool.runner.models.ToolCall`.
:raises SmarterConfigurationError: If no model is registered for the kind.
"""
path = TOOL_CALL_MODELS.get(kind)
if path is None:
raise SmarterConfigurationError(f"no tool call model is registered for the kind {kind!r}.")
model: type[ToolCall] = import_string(path)
return model
[docs]
def call_timeout(call: ToolCall) -> float:
"""
Return the timeout that a call names, its own or its MCP endpoint's.
:param call: The call.
:returns: Seconds, or :data:`DEFAULT_CALL_TIMEOUT_SECONDS` if it names none.
"""
timeout = getattr(call, "timeout", None)
if timeout is None:
endpoint = getattr(call, "endpoint", None)
timeout = getattr(endpoint, "timeout", None)
if not isinstance(timeout, (int, float)) or timeout <= 0:
return DEFAULT_CALL_TIMEOUT_SECONDS
return float(timeout)
[docs]
def remote_timeout(call: ToolCall) -> float:
"""
Return how long the remote runner waits for a call's result.
:param call: The call.
:returns: Seconds: the call's timeout times :data:`REMOTE_TIMEOUT_FACTOR`, plus
:data:`REMOTE_TIMEOUT_MARGIN_SECONDS`, at most :data:`MAX_REMOTE_TIMEOUT_SECONDS`.
"""
seconds = call_timeout(call) * REMOTE_TIMEOUT_FACTOR + REMOTE_TIMEOUT_MARGIN_SECONDS
return min(seconds, MAX_REMOTE_TIMEOUT_SECONDS)
[docs]
def describe_remote_failure(error: Exception, timeout: float) -> str:
"""
Describe, for the LLM, why the tool worker didn't return a call's result.
:param error: What the wait raised.
:param timeout: Seconds that the runner waited.
:returns: The description.
"""
# pylint: disable=import-outside-toplevel
from celery.exceptions import TimeoutError as CeleryTimeoutError
if isinstance(error, CeleryTimeoutError):
return f"the tool worker did not answer within {timeout:g} seconds."
return f"the tool worker could not run the tool: {type(error).__name__}: {error}"
__all__ = [
"MAX_REMOTE_TIMEOUT_SECONDS",
"TOOL_CALL_MODELS",
"TOOL_EXECUTORS",
"InProcessToolRunner",
"RemoteToolRunner",
"ToolExecutor",
"ToolRunner",
"call_model_for",
"call_timeout",
"carry_tool_call_label",
"current_tool_call_label",
"describe_remote_failure",
"executor_for",
"get_tool_runner",
"labeled_tool_calls",
"remote_timeout",
"running_in_tool_worker",
]