defineTextTool function

Tool<JsonMap, String> defineTextTool(
  1. Genkit ai, {
  2. required ToolRuntime runtime,
  3. required String name,
  4. required String description,
  5. Map<String, Object?> properties = const <String, Object?>{},
  6. List<String> required = const <String>[],
  7. required Future<String> run(
    1. JsonMap input
    ),
})

Defines a tool that takes a JSON object and returns text to the model.

Every tool goes through this helper so that status reporting and error handling behave the same way: a ToolFailure or unexpected exception becomes an Error: ... result the model can react to, instead of aborting the whole generation.

Implementation

Tool<JsonMap, String> defineTextTool(
  Genkit ai, {
  required ToolRuntime runtime,
  required String name,
  required String description,
  Map<String, Object?> properties = const <String, Object?>{},
  List<String> required = const <String>[],
  required Future<String> Function(JsonMap input) run,
}) {
  return ai.defineTool(
    name: name,
    description: description,
    inputSchema: objectSchema(properties: properties, required: required),
    outputSchema: SchemanticType.string(),
    fn: (input, _) async {
      try {
        final output = await run(input);
        runtime.endTool(name, output);
        return ToolResult.response(output);
      } on ToolFailure catch (failure) {
        return ToolResult.response(_fail(runtime, name, failure.message));
      } catch (error) {
        return ToolResult.response(_fail(runtime, name, '$error'));
      }
    },
  );
}