import 'dart:convert'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../chat/ui/transcript/chat_markdown_view.dart'; /// The web's `displayText`: a list joins its items, a map reads `key=value` /// pairs, anything else is its plain string. String runFieldText(dynamic value) { if (value is List) return value.map(runFieldText).join(', '); if (value is Map) { return value.entries .map((entry) => '${entry.key}=${runFieldText(entry.value)}') .join(', '); } return '${value ?? 'null'}'; } /// The text a copy button puts on the clipboard for a recorded field map: a /// lone markdown document as itself, anything else as indented JSON. String runFieldCopyText(dynamic value) { if (value is Map && value.length == 1) { final only = value.entries.single; if ('${only.key}'.toLowerCase() == 'markdown') { return runFieldText(only.value); } } if (value == null || (value is Map && value.isEmpty)) return ''; return const JsonEncoder.withIndent(' ').convert(value); } /// The web's `looksLikeMarkdown`: a heading, fence, list item or table row. bool runFieldLooksLikeMarkdown(String text) => RegExp(r'^#{1,6}\s', multiLine: true).hasMatch(text) || text.contains('```') || RegExp(r'^\s*(?:[-*]|\d+\.)\s+', multiLine: true).hasMatch(text) || RegExp(r'^\|.+\|', multiLine: true).hasMatch(text); /// A run's recorded Input or Output as the web draws it: each field under its /// own name with its value as text, markdown when it reads as markdown, and a /// lone `markdown` field as the document itself. Never raw JSON. class RunFieldBody extends StatelessWidget { const RunFieldBody({ super.key, required this.value, required this.empty, required this.cupertino, this.textBuilder, }); final dynamic value; final String empty; final bool cupertino; /// Draws a plain value; the host's selectable text by default. final Widget Function(String text)? textBuilder; @override Widget build(BuildContext context) { final map = value is Map ? Map.from(value as Map) : null; if (map == null || map.isEmpty) return _plain(empty); if (map.length == 1 && map.keys.single.toLowerCase() == 'markdown') { return _value(runFieldText(map.values.single)); } final caption = (cupertino ? CupertinoTheme.of(context).textTheme.textStyle : Theme.of(context).textTheme.labelMedium) ?.copyWith( fontWeight: FontWeight.w600, color: cupertino ? CupertinoColors.secondaryLabel.resolveFrom(context) : Theme.of(context).colorScheme.onSurfaceVariant, ); return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ for (final (index, entry) in map.entries.indexed) Padding( padding: EdgeInsets.only(top: index == 0 ? 0 : 12), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( entry.key.trim().isEmpty ? 'value' : entry.key, style: caption, ), const SizedBox(height: 2), _value(runFieldText(entry.value)), ], ), ), ], ); } Widget _value(String text) => runFieldLooksLikeMarkdown(text) ? ChatMarkdownView(text: text) : _plain(text); Widget _plain(String text) => textBuilder?.call(text) ?? SelectableText(text); }