inspectImageTool function

AgentTool inspectImageTool(
  1. ExecutionEnv env,
  2. InspectImageConfig config
)

Creates the inspect_image tool.

Parameters:

  • path (string, required): path to the image file.
  • prompt (string, optional): specific question or instructions.

Implementation

AgentTool inspectImageTool(ExecutionEnv env, InspectImageConfig config) {
  return AgentTool(
    name: 'inspect_image',
    label: 'inspect_image',
    tier: ApprovalTier.read,
    description:
        'Analyze a local image file using a dedicated vision-capable model. '
        'Returns a text description; the image itself does not enter the main '
        'chat context. Supported formats: PNG, JPEG, GIF, WebP, BMP.',
    parameters: const {
      'type': 'object',
      'properties': {
        'path': {
          'type': 'string',
          'description': 'Path to the image file (relative or absolute)',
        },
        'prompt': {
          'type': 'string',
          'description': 'Optional specific question or instructions',
        },
      },
      'required': ['path'],
    },
    execute: (arguments, cancelToken, onUpdate) async {
      cancelToken?.throwIfCancelled();
      final path = arguments['path'] as String;
      final prompt = (arguments['prompt'] as String?) ?? '';

      final read = await env.readBinaryFile(path);
      if (read.isErr) {
        throw StateError('${read.errorOrNull}');
      }
      final bytes = read.valueOrNull!;
      cancelToken?.throwIfCancelled();

      // Detect MIME type from magic bytes. Only the supported image formats
      // are accepted; the actual vision request will re-encode/rescale if
      // needed on the provider side.
      final mimeType = _detectImageMimeType(bytes);
      if (mimeType == null) {
        throw StateError('Unsupported image format or not an image: $path');
      }

      final description = await _inspectWithVisionModel(
        config,
        bytes,
        mimeType,
        prompt,
      );
      return ToolExecutionResult(content: [TextContent(text: description)]);
    },
  );
}