Tool Protocol
The protocol that a Smarter Extension’s tools implement.
A tool is a subclass of SmarterExtensionTool. It declares its name, the Pydantic
model of its arguments, and a call() method:
from pydantic import BaseModel, Field
from smarter.apps.llmtool.extension import ExtensionContext, ExtensionToolError, SmarterExtensionTool
class WeatherArguments(BaseModel):
location: str = Field(..., description="City and state, e.g. San Francisco, CA")
class GetWeather(SmarterExtensionTool):
"""Get the current weather and a 24-hour forecast for a location."""
name = "get_weather"
arguments_model = WeatherArguments
def call(self, arguments: WeatherArguments, context: ExtensionContext):
api_key = context.settings.google_maps_api_key.get_secret_value()
...
return {"location": arguments.location, "forecast": [...]}
The platform does everything else:
the OpenAI tool definition: the class docstring is its description, and the arguments model’s JSON schema is its parameters;
parsing and validating the LLM’s arguments, and returning validation errors to the LLM;
the
llm_tool_presented,llm_tool_requestedandllm_tool_respondedsignals;turning an exception into an
{"error": ...}result, so that the LLM can recover;a timeout, from the manifest’s
[tool.smarter] timeout.
Raise ExtensionToolError to return a message to the LLM. Any other exception is
logged with its traceback, and the LLM is told that the tool failed.
- class smarter.apps.llmtool.extension.protocol.ExtensionCaller(account_number=None, user_id=None, llmclient_name=None)[source]
Bases:
objectWho called a tool: plain identifiers, not the platform’s database objects.
An extension’s code is a third party’s. It gets what identifies the caller, and nothing that would let it read or change the platform’s data, or reach a user’s personal details.
- Parameters:
- __init__(account_number=None, user_id=None, llmclient_name=None)
- class smarter.apps.llmtool.extension.protocol.ExtensionContext(slug, version, settings, caller=<factory>, switches=<factory>, logger=None)[source]
Bases:
objectWhat a tool call knows about where it runs.
Everything here is a plain value, decided before the call: it reads no database, so that the tool can run in a worker without one (ADR-038).
- Parameters:
slug (
str) – The extension’s slug.version (
str) – The extension’s installed version.settings (
BaseModel) – The extension’s settings, one attribute per setting in its manifest. Asecretsetting is apydantic.SecretStr; an unset setting is None.caller (
ExtensionCaller) – Who called the tool: the account number, the user’s id and the LLMClient’s name.switches (
dict[str,bool]) – The extension’s own waffle switches, by their names in the manifest, and whether each is on.
- __init__(slug, version, settings, caller=<factory>, switches=<factory>, logger=None)
- caller: ExtensionCaller
- logger: Logger | WaffleSwitchedLoggerWrapper = None
- settings: BaseModel
- smarter.apps.llmtool.extension.protocol.ExtensionLogger = logging.Logger | smarter.lib.logging.waffle_switched_logger.WaffleSwitchedLoggerWrapper
The logger of an extension’s code.
- exception smarter.apps.llmtool.extension.protocol.ExtensionToolError[source]
Bases:
ExceptionRaised by a tool to tell the LLM why it could not do what was asked.
The message is returned to the LLM as
{"error": "<message>"}, without a traceback.
- class smarter.apps.llmtool.extension.protocol.SmarterExtensionTool[source]
Bases:
objectThe base class of a Smarter Extension’s tools.
Subclasses set
nameandarguments_model, document the tool in the class docstring (or setdescription), and implementcall(). A new instance is created for each tool call.- arguments_model: ClassVar[type[BaseModel]]
The Pydantic model of the tool’s arguments.
Its JSON schema is the tool’s parameters.
- cache_ttl: ClassVar[int] = 0
Seconds to cache the tool’s results, by its arguments and the extension’s settings.
0, the default, caches nothing.
Set it only for a tool that changes nothing, and whose result depends only on its arguments and settings, not on who calls it (
context.caller): a cached result is returned to every LLMClient and user. For example, a weather forecast,cache_ttl = 3600. An error is never cached.
- call(arguments, context)[source]
Do what the LLM asked.
- Parameters:
arguments (
BaseModel) – The LLM’s arguments, validated byarguments_model.context (
ExtensionContext) – The extension’s settings, and who called the tool.
- Return type:
TypeAliasType- Returns:
The result for the LLM: any JSON value.
- Raises:
ExtensionToolError – To tell the LLM why the tool could not do what was asked.
- description: ClassVar[str | None] = None
The tool’s description for the LLM.
Defaults to the class docstring.
- classmethod parameters()[source]
The tool’s parameters for the LLM: the JSON schema of
arguments_model.
- classmethod tool_description()[source]
The tool’s description for the LLM:
description, or the class docstring.- Return type:
- smarter.apps.llmtool.extension.protocol.extension_logger(slug)[source]
Return the logger of an extension’s code, switched by
SmarterWaffleSwitches.EXTENSION_LOGGING.- Parameters:
slug (
str) – The extension’s slug.- Return type: