formatWebSearchResults function

String formatWebSearchResults(
  1. WebSearchResponse response
)

Formats a response for the model (omp's formatForLLM, reduced to the fields the ported providers produce): the answer first when present, then [n] title / url / snippet sources.

Implementation

String formatWebSearchResults(WebSearchResponse response) {
  final parts = <String>[];
  final answer = response.answer;
  if (answer != null && answer.trim().isNotEmpty) {
    parts.add(answer.trim());
    if (response.sources.isNotEmpty) parts.add('\n## Sources');
  }
  for (var i = 0; i < response.sources.length; i++) {
    final source = response.sources[i];
    parts.add('[${i + 1}] ${source.title}\n    ${source.url}');
    final snippet = source.snippet;
    if (snippet != null && snippet.trim().isNotEmpty) {
      parts.add('    ${_truncate(snippet.trim(), 240)}');
    }
  }
  return parts.join('\n');
}