Toolkit
The extension tools that a prompt offers the LLM.
ExtensionToolkit turns an LLMClient’s extensions into what an OpenAI-compatible
chat completion needs:
tools: one function tool per extension tool, named
ext_<slug>_<tool name>, with the tool’s description and its arguments model’s JSON schema;tool calls:
ExtensionToolkit.call()validates the LLM’s arguments, runs the tool with a timeout, and returns its result. Errors are returned to the LLM as{"error": ...}, so that it can recover, rather than failing the prompt. A tool that setscache_ttlhas its results cached.
An extension that is disabled, not available to the LLMClient’s account, or whose code does
not load, is skipped and reported in ExtensionToolkit.errors, so that one broken
extension does not fail the prompt.
- class smarter.apps.llmtool.extension.toolkit.ExtensionFunction(name, extension, loaded, tool)[source]
Bases:
objectA function tool offered to the LLM, and the extension tool it calls.
- __init__(name, extension, loaded, tool)
- loaded: LoadedExtension
- tool: type[SmarterExtensionTool]
- exception smarter.apps.llmtool.extension.toolkit.ExtensionToolCallError[source]
Bases:
ExceptionA tool call failed.
ExtensionToolkit.call()returns its message to the LLM.
- class smarter.apps.llmtool.extension.toolkit.ExtensionToolkit(extensions, account=None, user_profile=None, llmclient=None)[source]
Bases:
objectThe extension tools and tool call dispatch of one prompt.
- Parameters:
Example usage:
toolkit = ExtensionToolkit(LLMClientExtensions.extensions_for(llmclient), account=account).load() tools.extend(toolkit.tools) ... if toolkit.is_extension_function(tool_call.function.name): result = toolkit.call(tool_call.function.name, arguments)
- call(function_name, arguments=None)[source]
Run a tool call, and return its result for the LLM.
Errors, including invalid arguments, an exception and a timeout, are returned as
{"error": "..."}, so that the LLM can recover.Sends
extension_tool_called, thenextension_tool_respondedorextension_tool_failed, and alwaysextension_tool_completedlast.
- execute(call)[source]
Run a resolved tool call with the tool runner (
smarter.apps.llmtool.runner).- Parameters:
call (
ExtensionToolCall) – The call, with its validated arguments, settings, switches and caller.- Return type:
TypeAliasType- Returns:
The tool’s result.
- Raises:
ExtensionToolCallError – If the tool fails, times out, or returns a result that is not JSON. Raised, rather than returned, so that an error is never cached.
- failed(function, error)[source]
Report a failed tool call, and return the error for the LLM.
Sends
extension_tool_failed, and logs a warning.- Parameters:
function (
ExtensionFunction) – The tool that failed.error (
str) – Why it failed.
- Return type:
- Returns:
{"error": error}.
- function_name(slug, tool_name)[source]
Return a unique OpenAI function name for one of an extension’s tools.
The name is
ext_<slug>_<tool name>, with the slug’s hyphens replaced by underscores, and at most 64 characters long. If two tools would have the same name, a short hash is appended.
- is_extension_function(function_name)[source]
Return whether a function name is one of this toolkit’s tools.
- Return type:
- load()[source]
Load each enabled extension’s code, if this process has not already, and build its tools.
- Return type:
- Returns:
This toolkit.
- run(function, arguments)[source]
Run one of this toolkit’s tools, for
call().- Parameters:
- Return type:
TypeAliasType- Returns:
The tool’s result.
- Raises:
ExtensionToolCallError – If the arguments are invalid, or the tool fails, times out, or returns a result that is not JSON.