ToolPresentationSource.fromMessage constructor

ToolPresentationSource.fromMessage(
  1. ChatMessageView message
)

Implementation

factory ToolPresentationSource.fromMessage(ChatMessageView message) {
  final metadata = message.metadata;
  var title = _boundedToolDisplayString(
    _stringMetadata(metadata, 'title') ?? message.previewText,
  );
  var status = _boundedToolDisplayString(
    _stringMetadata(metadata, 'status') ?? '',
  );
  final embeddedToolTitle = RegExp(
    r'^\[Tool:\s*(.*?)\]\s*(.*)$',
  ).firstMatch(title);
  if (embeddedToolTitle != null) {
    title = embeddedToolTitle.group(1)?.trim() ?? title;
    status = status.isEmpty
        ? embeddedToolTitle.group(2)?.trim() ?? ''
        : status;
  }
  status = status.replaceFirst('ToolCallStatus.', '');
  if (status.isEmpty) status = 'completed';

  final locations = _mapList(metadata['locations'])
      .map((location) {
        final path = _stringMetadata(location, 'path') ?? '';
        final line = location['line'];
        return line == null ? path : '$path:$line';
      })
      .where((location) => location.isNotEmpty)
      .toList(growable: false);

  return ToolPresentationSource(
    title: title.isEmpty ? 'Tool call' : title,
    status: status,
    id: _boundedToolDisplayString(_toolCallIdMetadata(metadata) ?? ''),
    kind: _stringMetadata(metadata, 'kind') == 'tool'
        ? ''
        : _stringMetadata(metadata, 'kind') ?? '',
    content: metadata['content'],
    input: _firstMetadataValue(metadata, const ['rawInput', 'raw_input']),
    output: _firstMetadataValue(metadata, const ['rawOutput', 'raw_output']),
    locations: List<String>.unmodifiable(locations),
    previewRevision: message.revision,
  );
}