call method

Future<Map<String, Object?>> call(
  1. Map<String, Object?> arguments
)

Runs the search for decoded tool-call arguments.

Returns RagSearchResult.toLlmJson with source names filled in and citation ids continuing from the previous call. Invalid arguments return {"error": ...} instead of throwing, so the model can correct its call; engine failures still throw.

Implementation

Future<Map<String, Object?>> call(Map<String, Object?> arguments) async {
  final query = arguments['query'];
  if (query is! String || query.trim().isEmpty) {
    return _error('"query" must be a non-empty string.');
  }
  final rawTopK = arguments['top_k'];
  int topK;
  if (rawTopK == null) {
    topK = defaultTopK;
  } else if (rawTopK is num && rawTopK == rawTopK.roundToDouble()) {
    // Some models send integral values as 5.0.
    topK = rawTopK.toInt().clamp(1, maxTopK);
  } else {
    return _error('"top_k" must be an integer between 1 and $maxTopK.');
  }

  final result = await _search(query.trim(), topK);
  final sourceNames = await _sourceNames(result);
  final json = result.toLlmJson(sourceNames: sourceNames, firstId: _nextId);
  _nextId += (json['results'] as List).length;
  return json;
}