Tool registry¶
ToolRegistry holds the Tool instances available to a processor, exposes
their JSON schemas to the model, and dispatches tool calls during the agentic
loop. For the catalogue of built-in tools, see Tools.
registry ¶
Tool registry with image management, execution tracking, and documentation.
This module provides: - ToolDocumenter: Schema generation and documentation formatting - ToolRegistry: Complete tool management with image handling and execution - EncodedImage: Container for base64-encoded image data - encode_image: Utility to encode PIL Images
ImageManager ¶
Manages image loading, transformation, and state.
Example
manager = ImageManager() manager.set_image(Path("scan.png")) manager.transform_image(lambda img: zoom_image(img, 2.0)) current = manager.current_image
Source code in src/gaze/tools/image_manager.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 | |
current_image
property
¶
Get the currently loaded image (may be None if not yet loaded).
is_modified
property
¶
Whether the current image has been modified from the original.
Set True by :meth:transform_image, cleared by
:meth:reset_to_original, :meth:set_image,
:meth:set_preloaded_image, and :meth:close.
original_encoding
property
writable
¶
Cached base64 encoding of the original (unmodified) image.
Set once after the first encode and reused by reset to skip redundant JPEG→base64 work.
Typed as Any at runtime to avoid circular-import forward-reference
issues with beartype. Static checkers see EncodedImage | None via
the TYPE_CHECKING guard.
__init__ ¶
Initialize image manager.
Source code in src/gaze/tools/image_manager.py
set_image ¶
Set the source image for operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_path
|
Path
|
Path to the image file |
required |
Raises:
| Type | Description |
|---|---|
ToolExecutionError
|
If image cannot be loaded or path is invalid |
Source code in src/gaze/tools/image_manager.py
ensure_loaded
async
¶
Ensure image is loaded, loading from path if necessary.
This is thread-safe and can be called multiple times.
Raises:
| Type | Description |
|---|---|
ToolExecutionError
|
If no image path is set or image cannot be loaded. |
Source code in src/gaze/tools/image_manager.py
transform_image ¶
Apply a transformation to the current image with automatic cleanup.
The previous _current_image is always closed after the operation
since current and original are always independent copies.
This method is synchronous and does not acquire _image_lock.
It is safe to call from the single-threaded asyncio event loop (the
normal execution path via ToolRegistry.execute), but callers must
not invoke it concurrently from multiple coroutines on the same
ImageManager instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
Callable[[Image], Image]
|
Function that takes current image and returns new image |
required |
Raises:
| Type | Description |
|---|---|
ToolExecutionError
|
If no image is loaded |
Example
manager.transform_image(lambda img: zoom_image(img, 2.0))
Source code in src/gaze/tools/image_manager.py
set_preloaded_image ¶
Set a pre-loaded PIL Image, avoiding a redundant disk read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Already-loaded PIL Image (must have pixel data in memory). |
required |
image_path
|
Path
|
Path to the source file (stored for reset/logging). |
required |
transfer_ownership
|
bool
|
If True, the manager takes ownership of
image directly (used as |
False
|
Raises:
| Type | Description |
|---|---|
ToolExecutionError
|
If image_path contains traversal patterns. |
Source code in src/gaze/tools/image_manager.py
reset_to_original ¶
Reset the current image to the originally loaded state.
Raises:
| Type | Description |
|---|---|
ToolExecutionError
|
If no image is loaded |
Source code in src/gaze/tools/image_manager.py
close ¶
Close and release all image resources.
Source code in src/gaze/tools/image_manager.py
Tool
dataclass
¶
Tool definition for agentic processing.
Immutable: a tool is constructed once (e.g. in create_visual_tools)
and never mutated, consistent with the frozen data types in gaze.types.
Source code in src/gaze/tools/tool.py
get_prompt_documentation ¶
Generate documentation for prompt inclusion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compact
|
bool
|
If True, emit a single-line summary per tool to reduce token overhead for small-context models. |
False
|
Returns custom prompt_documentation if provided (unless compact), otherwise generates documentation from the tool's description and parameters.
Source code in src/gaze/tools/tool.py
EncodedImage
dataclass
¶
Container for encoded image data.
Source code in src/gaze/tools/registry.py
ToolDocumenter ¶
Generates tool schemas and documentation.
Handles: - OpenAI-compatible tool schema generation - Prompt documentation formatting - Tool categorization and filtering - Schema validation
Example
documenter = ToolDocumenter(tools=[zoom_tool, crop_tool]) schemas = documenter.get_tool_schemas() docs = documenter.generate_prompt_documentation()
Source code in src/gaze/tools/registry.py
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
__init__ ¶
Initialize tool documenter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tools
|
list[Tool] | None
|
List of tools to document. Can be empty and tools added later. |
None
|
Source code in src/gaze/tools/registry.py
register ¶
Register a tool for documentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
Tool
|
Tool to register |
required |
get_tool ¶
Get a registered tool by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Tool name to look up |
required |
Returns:
| Type | Description |
|---|---|
Tool | None
|
Tool if found, None otherwise |
get_tool_names ¶
Get list of all registered tool names.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of tool names |
get_tool_schemas ¶
Get OpenAI-compatible tool schemas for all registered tools.
Results are cached and invalidated when tools are registered.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of tool schemas in OpenAI function-calling format |
Raises:
| Type | Description |
|---|---|
ValueError
|
If tool has invalid schema configuration |
Source code in src/gaze/tools/registry.py
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 | |
generate_prompt_documentation ¶
generate_prompt_documentation(
group_by_category: bool = True,
include_categories: set[str] | None = None,
exclude_categories: set[str] | None = None,
compact: bool = False,
) -> str
Generate prompt documentation for all registered tools.
This creates formatted text suitable for inclusion in system prompts, documenting all available tools with their parameters and usage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_by_category
|
bool
|
If True, group tools by category with headers |
True
|
include_categories
|
set[str] | None
|
If set, only include tools from these categories |
None
|
exclude_categories
|
set[str] | None
|
If set, exclude tools from these categories |
None
|
compact
|
bool
|
If True, emit one-line-per-tool summaries to reduce token overhead for small-context models (<=8K). |
False
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted documentation string for system prompts |
Source code in src/gaze/tools/registry.py
ToolRegistry ¶
Refactored tool registry with separated responsibilities.
This implementation delegates to specialized managers: - ImageManager: Handles image loading and transformation - ToolDocumenter: Handles schema generation and documentation
Architecture
ToolRegistry ├── ImageManager (image loading, transformation, state) ├── ToolDocumenter (schemas, documentation, validation) └── Tool execution (actual tool calling)
Source code in src/gaze/tools/registry.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | |
__init__ ¶
__init__(
image_path: Path | None = None,
tools: list[Tool] | None = None,
max_history: int = 100,
web_search_manager: Any | None = None,
image_search_manager: Any | None = None,
) -> None
Initialize refactored tool registry.
Source code in src/gaze/tools/registry.py
__enter__ ¶
__exit__ ¶
__exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None
Sync context manager exit with cleanup.
close ¶
Close and clean up synchronous resources.
Prefer :meth:aclose from async contexts so that search manager
sessions are properly awaited.
Source code in src/gaze/tools/registry.py
aclose
async
¶
Close all resources including async search manager sessions.
Source code in src/gaze/tools/registry.py
get_web_search_manager ¶
Get or create a reusable WebSearchManager for the session.
Source code in src/gaze/tools/registry.py
get_image_search_manager ¶
Get or create a reusable MedicalImageSearchManager for the session.
Source code in src/gaze/tools/registry.py
register ¶
get_tool_schemas ¶
execute
async
¶
Execute a tool by name with given arguments.
Raises:
| Type | Description |
|---|---|
UnknownToolError
|
If |
ToolExecutionError
|
If the tool raises a |
Source code in src/gaze/tools/registry.py
get_image_manager ¶
encode_image ¶
Encode a PIL Image to a base64 string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
PIL Image to encode. |
required |
format
|
str
|
Image format — |
'JPEG'
|
quality
|
int | None
|
JPEG quality 1-100. Ignored for PNG. When None, uses
|
None
|
Returns:
| Type | Description |
|---|---|
EncodedImage
|
EncodedImage with base64 data and correct MIME type. |