toGeminiToolConfig function

  1. @visibleForTesting
ToolConfig? toGeminiToolConfig(
  1. GeminiFunctionCallingConfig? functionCallingConfig, {
  2. ToolChoice? toolChoice,
  3. bool hasFunctionTools = false,
})

Builds Gemini's toolConfig from the plugin-specific functionCallingConfig or, when that is absent, the portable Genkit toolChoice (auto / required / none map to Gemini's AUTO / ANY / NONE). The plugin config wins because it is strictly more expressive (allowed function names). Unknown toolChoice values are ignored.

toolChoice only applies when hasFunctionTools is true, i.e. the request declares Genkit tools. Built-in tools (Google Search, code execution) don't count: a function-calling mode with no function declarations is contradictory (required would demand a call to nothing).

Implementation

@visibleForTesting
gcl.ToolConfig? toGeminiToolConfig(
  GeminiFunctionCallingConfig? functionCallingConfig, {
  ToolChoice? toolChoice,
  bool hasFunctionTools = false,
}) {
  if (functionCallingConfig == null) {
    if (!hasFunctionTools) return null;
    final mode = switch (toolChoice?.value) {
      'auto' => 'AUTO',
      'required' => 'ANY',
      'none' => 'NONE',
      _ => null,
    };
    if (mode == null) return null;
    return gcl.ToolConfig(
      functionCallingConfig: gcl.FunctionCallingConfig(mode: mode),
    );
  }
  return gcl.ToolConfig(
    functionCallingConfig: gcl.FunctionCallingConfig(
      mode: functionCallingConfig.mode ?? 'MODE_UNSPECIFIED',
      allowedFunctionNames:
          functionCallingConfig.allowedFunctionNames
              ?.map(_toGeminiToolName)
              .toList() ??
          [],
    ),
  );
}