defineTextTool function
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'));
}
},
);
}