toolResultSearchText function
Duck-type the tool's native Out for searchable text.
Known shapes: {stdout,stderr} (Bash), {content} (Grep), {file:{content}} (Read), {filenames:[]} (Grep/Glob), {output} (generic).
Implementation
String toolResultSearchText(dynamic r) {
if (r == null) return '';
if (r is String) return r;
if (r is! Map<String, dynamic>) return '';
// Known shapes first
if (r['stdout'] is String) {
final err = r['stderr'] is String ? r['stderr'] as String : '';
return '${r['stdout']}${err.isNotEmpty ? '\n$err' : ''}';
}
if (r['file'] is Map<String, dynamic>) {
final content = (r['file'] as Map<String, dynamic>)['content'];
if (content is String) return content;
}
// Known output-field names only
final parts = <String>[];
for (final k in ['content', 'output', 'result', 'text', 'message']) {
final v = r[k];
if (v is String) parts.add(v);
}
for (final k in ['filenames', 'lines', 'results']) {
final v = r[k];
if (v is List && v.every((x) => x is String)) {
parts.add((v as List<String>).join('\n'));
}
}
return parts.join('\n');
}