Tool / Function-Calling System
DeskBot's tool system allows the LLM to invoke robot actions (change emotion, move servos, play sounds, speak text) through a structured function-calling interface compatible with OpenAI, Ollama, and other LLM providers.
The system has three layers:
- Schema -
ToolDefinitionandToolParameterdefine the shape of a tool and serialise to the OpenAI function-calling JSON format. - Registry -
ToolRegistryholds definitions and their handler functions, and can execute tool calls by name. - Executor -
ToolExecutordispatches LLM tool calls to robot actions via the event bus and hardware controllers.
Tool definition schema
Each tool is described by a ToolDefinition
with a name, description, and a tuple of ToolParameter
objects:
from robot.ai.tools.schema import ToolDefinition, ToolParameter, ToolParameterType
my_tool = ToolDefinition(
name="change_emotion",
description="Change the robot's emotional expression.",
parameters=(
ToolParameter(
name="emotion",
type=ToolParameterType.STRING,
description="The emotion to express.",
enum=("neutral", "happy", "curious", "thinking", "sleepy",
"embarrassed", "excited", "sad", "surprised", "angry"),
),
ToolParameter(
name="intensity",
type=ToolParameterType.NUMBER,
description="Intensity of the emotion (0.0 to 1.0).",
required=False,
default=1.0,
),
),
)
OpenAI-compatible schema
Every ToolDefinition can produce an OpenAI function-calling schema:
schema = my_tool.to_openai_schema()
# {
# "type": "function",
# "function": {
# "name": "change_emotion",
# "description": "Change the robot's emotional expression.",
# "parameters": { ... }
# }
# }
This schema can be passed directly to any OpenAI-compatible LLM as a tool definition.
Tool registry
The ToolRegistry manages tool
definitions and their handler functions:
from robot.ai.tools.registry import ToolRegistry, BUILTIN_TOOLS
registry = ToolRegistry()
# Register a built-in tool with its handler
registry.add(BUILTIN_TOOLS["change_emotion"], my_handler)
# Get all OpenAI schemas for an LLM prompt
schemas = registry.get_schemas()
# Execute a tool call from the LLM
result = await registry.execute("change_emotion", {"emotion": "happy"})
Methods
| Method | Description |
|---|---|
add(definition, handler) |
Register a tool and its async handler |
remove(name) |
Remove a tool by name |
get(name) |
Return a tool definition by name |
get_handler(name) |
Return a tool handler by name |
list_tools() |
Return all registered tool definitions |
get_schemas() |
Return OpenAI-compatible schemas for all tools |
execute(name, arguments) |
Execute a tool by name with arguments |
Built-in tools
DeskBot ships with 5 built-in tools defined in
BUILTIN_TOOLS:
| Tool | Description | Parameters |
|---|---|---|
change_emotion |
Change the robot's emotional expression | emotion (enum), intensity (number, optional) |
play_sound |
Play a sound effect through the speaker | name (string) |
set_state |
Transition the robot to a new state | state (enum) |
move_servo |
Move a servo to a target angle | servo (enum), angle (number), duration_s (number, optional) |
speak |
Make the robot say something using TTS | text (string) |
Tool executor
The ToolExecutor validates
arguments, dispatches built-in tool calls to the event bus, and delegates
custom tool calls to their registered handlers:
from robot.ai.tools.executor import ToolExecutor
executor = ToolExecutor(
registry=registry,
bus=event_bus,
servo_controller=servo_ctrl,
tts=tts_engine,
action_executor=action_executor, # optional: route learnable tools through the executor
)
result = await executor.execute_tool_call("change_emotion", {"emotion": "happy"})
# {"status": "ok", "emotion": "happy", "intensity": 1.0}
Built-in tool dispatch
When an action_executor is not wired (the legacy path), built-in tools
dispatch directly to the event bus and hardware controllers:
| Tool | Dispatch target (legacy) |
|---|---|
change_emotion |
Publishes EmotionChanged + BlinkRequested on the event bus |
play_sound |
Publishes SoundEffectPlayed on the event bus |
set_state |
Publishes StateChanged on the event bus |
move_servo |
Calls ServoController.get(name).move_to() + publishes ServoMoved |
speak |
Calls TextToSpeech.speak(text) |
Learning-aware routing
When an action_executor is wired (the default once learning is enabled in
DeskBotApp), the learnable builtins — change_emotion, set_state,
move_servo, and speak — are routed through the canonical
ActionExecutor instead of going
directly to the bus/hardware. Each call is translated into the matching
BehaviorAction (ChangeEmotionAction, SetStateAction,
RequestServoMoveAction, SpeakAction) and dispatched via
ActionExecutor.execute_one, so the LLM tool call is recorded as a real
learning transition with the same lifecycle, safety checks, and interaction
tagging as any other action (see Services). play_sound is
intentionally not routed (it is not in the action space) and is logged as
action_not_learnable.
Argument validation
The executor validates required parameters and coerces types. If a
required parameter is missing or an unknown tool name is provided, a
ToolExecutionError is
raised.
Registering custom tools
To add a new tool:
from robot.ai.tools.schema import ToolDefinition, ToolParameter, ToolParameterType
from robot.ai.tools.registry import ToolRegistry
registry = ToolRegistry()
# Define the tool
my_tool = ToolDefinition(
name="set_led_color",
description="Set the LED strip to a specific colour.",
parameters=(
ToolParameter(
name="color",
type=ToolParameterType.STRING,
description="Hex colour code (e.g. '#ff0000').",
),
),
)
# Define the handler
async def handle_set_led(color: str) -> dict:
# ... drive hardware ...
return {"status": "ok", "color": color}
# Register
registry.add(my_tool, handle_set_led)
The tool's OpenAI schema will automatically appear in
registry.get_schemas() for inclusion in LLM prompts.
API reference
robot.ai.tools.schema
Tool definition schema for LLM function calling.
Follows the OpenAI function-calling format so tools can be passed directly to compatible LLMs (OpenAI, Ollama, etc.).
ToolDefinition
dataclass
A complete tool definition that can be serialised for an LLM.
Attributes
name:
The tool name (e.g. "change_emotion").
description:
A human-readable description of what the tool does.
parameters:
The list of parameters the tool accepts.
Source code in src/robot/ai/tools/schema.py
to_openai_schema()
Serialise to the OpenAI function-calling schema format.
Source code in src/robot/ai/tools/schema.py
ToolParameter
dataclass
A single parameter in a tool definition.
Source code in src/robot/ai/tools/schema.py
ToolParameterType
Bases: str, Enum
JSON Schema types for tool parameters.
Source code in src/robot/ai/tools/schema.py
robot.ai.tools.registry
Tool registry - manages available LLM-callable tools.
The registry holds :class:ToolDefinition instances and their
corresponding handler functions. When the LLM produces a tool call,
the :class:ToolExecutor looks up the tool by name and invokes the
handler.
ToolError
Bases: DeskBotError
Error during tool registration or execution.
ToolRegistry
Registry of available LLM tools and their handlers.
Usage::
registry = ToolRegistry()
@registry.register_tool
async def change_emotion(emotion: str, intensity: float = 1.0) -> dict:
...
return {"status": "ok", "emotion": emotion}
# Get schemas for the LLM prompt
schemas = registry.get_schemas()
# Execute a tool call from the LLM
result = await registry.execute("change_emotion", {"emotion": "happy"})
Source code in src/robot/ai/tools/registry.py
add(definition, handler)
Register a tool and its handler.
Source code in src/robot/ai/tools/registry.py
execute(name, arguments)
async
Execute a tool by name with the given arguments.
Returns the handler's result dict. Raises :class:ToolError
if the tool is not found, and propagates any handler exceptions.
Source code in src/robot/ai/tools/registry.py
get(name)
get_handler(name)
get_schemas()
list_tools()
robot.ai.tools.executor
Tool executor - dispatches LLM tool calls to robot actions.
ToolExecutionError
Bases: DeskBotError
Error during tool execution.
ToolExecutor
dataclass
Dispatches LLM tool calls to robot actions.
Source code in src/robot/ai/tools/executor.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
execute_tool_call(tool_name, arguments)
async
Execute a single tool call.