Files
pipelines/app/lib/features/runs/ui/run_field_body.dart
T
alan 22af093eda Expand run detail output by default
Make run and visit output sections open when details are viewed while preserving user collapse behavior. Add coverage and implementation notes for the updated interaction.
2026-11-12 01:56:41 +00:00

154 lines
4.9 KiB
Dart

import 'dart:convert';
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,
this.textBuilder,
});
final dynamic value;
final String empty;
/// Draws a plain value; the host's selectable text by default.
final Widget Function(String text)? textBuilder;
@override
Widget build(BuildContext context) {
final map = _runFieldMap(value);
if (map == null || map.isEmpty) {
return _runFieldPlain(empty, textBuilder);
}
if (_isMarkdownDocument(map)) {
return _runFieldValue(runFieldText(map.values.single), textBuilder);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
for (final (index, entry) in map.entries.indexed)
_runFieldRow(context, index, entry, textBuilder),
],
);
}
}
/// [RunFieldBody] as a sliver, for a section of a [CustomScrollView] that is
/// open by default. A field map builds and lays out only the fields on screen,
/// however many there are, so a long Output does not cost a full layout when
/// its section opens. Its rows take the full width the sliver gives them; the
/// host pads it.
class RunFieldSliver extends StatelessWidget {
const RunFieldSliver({
super.key,
required this.value,
required this.empty,
this.textBuilder,
});
final dynamic value;
final String empty;
/// Draws a plain value; the host's selectable text by default.
final Widget Function(String text)? textBuilder;
@override
Widget build(BuildContext context) {
final map = _runFieldMap(value);
if (map == null || map.isEmpty || _isMarkdownDocument(map)) {
return SliverToBoxAdapter(
child: RunFieldBody(
value: value,
empty: empty,
textBuilder: textBuilder,
),
);
}
final entries = map.entries.toList(growable: false);
return SliverList.builder(
itemCount: entries.length,
itemBuilder: (context, index) =>
_runFieldRow(context, index, entries[index], textBuilder),
);
}
}
Map<String, dynamic>? _runFieldMap(dynamic value) =>
value is Map ? Map<String, dynamic>.from(value) : null;
/// A lone `markdown` field, shown as the document itself.
bool _isMarkdownDocument(Map<String, dynamic> map) =>
map.length == 1 && map.keys.single.toLowerCase() == 'markdown';
/// One recorded field: its name, then its value.
Widget _runFieldRow(
BuildContext context,
int index,
MapEntry<String, dynamic> entry,
Widget Function(String text)? textBuilder,
) {
final theme = Theme.of(context);
final caption = theme.textTheme.labelMedium?.copyWith(
fontWeight: FontWeight.w600,
color: theme.colorScheme.onSurfaceVariant,
);
return 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),
_runFieldValue(runFieldText(entry.value), textBuilder),
],
),
);
}
Widget _runFieldValue(String text, Widget Function(String text)? textBuilder) =>
runFieldLooksLikeMarkdown(text)
? ChatMarkdownView(text: text)
: _runFieldPlain(text, textBuilder);
Widget _runFieldPlain(String text, Widget Function(String text)? textBuilder) =>
textBuilder?.call(text) ?? SelectableText(text);