Notebook design: numbered In/Out cells, tool sub-cells and an input-cell composer
Adds the Notebook design (TranscriptStyle.cells, ComposerStyle.cell), a white/charcoal IDE theme with light grey cells, hairlines and 5px corners. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../../ui/theme/app_design_tokens.dart';
|
||||
|
||||
/// The width of an input cell's accent bar along its leading edge.
|
||||
const double notebookCellBar = 3;
|
||||
|
||||
/// Where a cell's text starts from its leading edge: past the bar, then a
|
||||
/// gap. Output text, footers and the composer's field start here too, so
|
||||
/// every cell's text shares one column.
|
||||
const double notebookCellTextInset = 12;
|
||||
|
||||
/// The padding of a cell's content: [notebookCellTextInset] at the start,
|
||||
/// a little less above and below.
|
||||
const EdgeInsetsDirectional notebookCellPadding =
|
||||
EdgeInsetsDirectional.fromSTEB(
|
||||
notebookCellTextInset,
|
||||
8,
|
||||
notebookCellTextInset,
|
||||
8,
|
||||
);
|
||||
|
||||
/// The corner radius cells take: the design's, else a notebook's 5px.
|
||||
double notebookCellRadius(BuildContext context) =>
|
||||
AppDesignTokens.of(context).radius ?? 5;
|
||||
|
||||
/// A notebook cell's frame: a [fill] in the design's corners inside a
|
||||
/// hairline [border], and an optional accent [bar] along the leading edge
|
||||
/// clipped to the corners. Painted in one pass, with no layer, so a long
|
||||
/// transcript of cells costs one picture per cell.
|
||||
class NotebookCellFrame extends StatelessWidget {
|
||||
const NotebookCellFrame({
|
||||
super.key,
|
||||
required this.fill,
|
||||
required this.border,
|
||||
this.bar,
|
||||
this.padding = notebookCellPadding,
|
||||
this.width,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
/// A fixed width (`double.infinity` to span a column that would shrink
|
||||
/// the cell to its content); null to size to the parent's constraints.
|
||||
final double? width;
|
||||
|
||||
final Color fill;
|
||||
final Color border;
|
||||
final Color? bar;
|
||||
final EdgeInsetsGeometry padding;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => CustomPaint(
|
||||
painter: NotebookCellPainter(
|
||||
fill: fill,
|
||||
border: border,
|
||||
bar: bar,
|
||||
radius: notebookCellRadius(context),
|
||||
direction: Directionality.of(context),
|
||||
),
|
||||
child: SizedBox(
|
||||
width: width,
|
||||
child: Padding(padding: padding, child: child),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Paints [NotebookCellFrame]: fill, leading bar and a 1px border.
|
||||
class NotebookCellPainter extends CustomPainter {
|
||||
NotebookCellPainter({
|
||||
required this.fill,
|
||||
required this.border,
|
||||
required this.bar,
|
||||
required this.radius,
|
||||
required this.direction,
|
||||
});
|
||||
|
||||
final Color fill;
|
||||
final Color border;
|
||||
final Color? bar;
|
||||
final double radius;
|
||||
final TextDirection direction;
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final rect = Offset.zero & size;
|
||||
final rounded = RRect.fromRectAndRadius(rect, Radius.circular(radius));
|
||||
canvas.drawRRect(rounded, Paint()..color = fill);
|
||||
final accent = bar;
|
||||
if (accent != null) {
|
||||
final left = direction == TextDirection.ltr
|
||||
? 0.0
|
||||
: size.width - notebookCellBar;
|
||||
canvas
|
||||
..save()
|
||||
..clipRRect(rounded)
|
||||
..drawRect(
|
||||
Rect.fromLTWH(left, 0, notebookCellBar, size.height),
|
||||
Paint()..color = accent,
|
||||
)
|
||||
..restore();
|
||||
}
|
||||
canvas.drawRRect(
|
||||
rounded.deflate(0.5),
|
||||
Paint()
|
||||
..color = border
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(NotebookCellPainter old) =>
|
||||
old.fill != fill ||
|
||||
old.border != border ||
|
||||
old.bar != bar ||
|
||||
old.radius != radius ||
|
||||
old.direction != direction;
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../../ui/text/monospace.dart';
|
||||
import '../chat_message_bubble.dart';
|
||||
|
||||
/// Below this width a cell's gutter collapses: its label sits inline above
|
||||
/// the cell instead of beside it, so a phone keeps the whole width for text.
|
||||
const double notebookCompactWidth = 600;
|
||||
|
||||
/// The gap between a gutter label's end and its cell.
|
||||
const double notebookGutterGap = 8;
|
||||
|
||||
/// An input cell's label: `In [n]:`, or `In [ ]:` for one not yet run.
|
||||
String notebookInLabel(int? number) => 'In [${number ?? ' '}]:';
|
||||
|
||||
/// An output cell's label: `Out [n]:`, or `Out [ ]:` without a prompt.
|
||||
String notebookOutLabel(int? number) => 'Out [${number ?? ' '}]:';
|
||||
|
||||
/// The style of gutter labels: the bundled monospace a step under the
|
||||
/// reading size, in [color].
|
||||
TextStyle notebookGutterStyle(BuildContext context, Color color) => TextStyle(
|
||||
fontFamily: appMonospaceFamily,
|
||||
fontSize: (chatMessageTextStyle(context)?.fontSize ?? 15) * 0.8,
|
||||
height: 1.2,
|
||||
letterSpacing: 0,
|
||||
color: color,
|
||||
);
|
||||
|
||||
/// The gutter's fixed width: ten monospace characters (`Out [123]:`) at the
|
||||
/// label size, scaled with the operator's text, and the gap. Every cell
|
||||
/// shares it, so labels right-align to one column.
|
||||
double notebookGutterWidth(BuildContext context) {
|
||||
final size = (chatMessageTextStyle(context)?.fontSize ?? 15) * 0.8;
|
||||
// RobotoMono advances 0.6 em per character.
|
||||
return MediaQuery.textScalerOf(context).scale(size) * 0.6 * 10 +
|
||||
notebookGutterGap;
|
||||
}
|
||||
|
||||
/// The muted colour of an `In` label: the accent pulled towards the
|
||||
/// secondary ink, as a notebook tints its input prompt.
|
||||
Color notebookInColor(ColorScheme scheme) =>
|
||||
Color.lerp(scheme.onSurfaceVariant, scheme.primary, 0.55)!;
|
||||
|
||||
/// The muted colour of an `Out` label: the tertiary (a notebook's output
|
||||
/// orange) pulled towards the secondary ink.
|
||||
Color notebookOutColor(ColorScheme scheme) =>
|
||||
Color.lerp(scheme.onSurfaceVariant, scheme.tertiary, 0.55)!;
|
||||
|
||||
/// Marks a subtree already inside a cell gutter, so a row nested in another
|
||||
/// (an activity group's folded turns) does not indent twice.
|
||||
class NotebookGutterScope extends InheritedWidget {
|
||||
const NotebookGutterScope({super.key, required super.child});
|
||||
|
||||
static bool inside(BuildContext context) =>
|
||||
context.getInheritedWidgetOfExactType<NotebookGutterScope>() != null;
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(NotebookGutterScope old) => false;
|
||||
}
|
||||
|
||||
/// One transcript row in a notebook: a fixed-width gutter holding [label]
|
||||
/// right-aligned (with an optional [marker] beneath it), then the cell.
|
||||
/// [line] is the style of the cell's first line: the label sits on that
|
||||
/// line's baseline, [top] below the row's top. Without a label the gutter
|
||||
/// is empty, so rows that are not cells still keep to the cells' column.
|
||||
/// Narrower than [notebookCompactWidth], the label moves inline above the
|
||||
/// cell and the gutter goes.
|
||||
class NotebookGutterRow extends StatelessWidget {
|
||||
const NotebookGutterRow({
|
||||
super.key,
|
||||
this.label,
|
||||
this.color,
|
||||
this.top = 0,
|
||||
this.line,
|
||||
this.marker,
|
||||
this.inline = true,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
/// Whether a narrow row shows the label inline above the cell; false
|
||||
/// drops it there (the composer, whose cell needs no number on a phone).
|
||||
final bool inline;
|
||||
|
||||
final String? label;
|
||||
final Color? color;
|
||||
final double top;
|
||||
final TextStyle? line;
|
||||
final Widget? marker;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (NotebookGutterScope.inside(context)) return child;
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
final style = notebookGutterStyle(
|
||||
context,
|
||||
color ?? scheme.onSurfaceVariant,
|
||||
);
|
||||
final text = label;
|
||||
return NotebookGutterScope(
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
if (constraints.maxWidth < notebookCompactWidth) {
|
||||
if (text == null || !inline) return child;
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.only(
|
||||
start: 2,
|
||||
bottom: 3,
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
_label(text, style, null, TextAlign.start),
|
||||
if (marker case final marker?) ...[
|
||||
const SizedBox(width: 6),
|
||||
marker,
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
child,
|
||||
],
|
||||
);
|
||||
}
|
||||
return Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: notebookGutterWidth(context),
|
||||
child: text == null
|
||||
? null
|
||||
: Padding(
|
||||
padding: EdgeInsetsDirectional.only(
|
||||
top: top,
|
||||
end: notebookGutterGap,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
_label(text, style, line, TextAlign.end),
|
||||
?marker,
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(child: child),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// The label, kept out of a transcript selection (a copied answer has no
|
||||
/// prompt numbers in it) and out of semantics (the row already names
|
||||
/// whose message it is). On [line]'s strut it shares that line's
|
||||
/// baseline.
|
||||
Widget _label(
|
||||
String text,
|
||||
TextStyle style,
|
||||
TextStyle? line,
|
||||
TextAlign align,
|
||||
) => ExcludeSemantics(
|
||||
child: SelectionContainer.disabled(
|
||||
child: Text(
|
||||
text,
|
||||
key: const Key('chat-notebook-gutter-label'),
|
||||
textAlign: align,
|
||||
maxLines: 1,
|
||||
softWrap: false,
|
||||
style: style,
|
||||
strutStyle: line == null
|
||||
? null
|
||||
: StrutStyle(
|
||||
fontFamily: line.fontFamily,
|
||||
fontFamilyFallback: line.fontFamilyFallback,
|
||||
fontSize: line.fontSize,
|
||||
height: line.height,
|
||||
forceStrutHeight: true,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../sessions/chat_color_palette.dart';
|
||||
import '../chat_message_bubble.dart';
|
||||
import 'notebook_cell_frame.dart';
|
||||
|
||||
/// How strongly a coloured chat's shade tints its input cells over the
|
||||
/// cell grey: a hint of the colour, not a wash.
|
||||
const double notebookInputTint = 0.07;
|
||||
|
||||
/// The operator's turn under a notebook transcript: an input cell in the
|
||||
/// light cell grey (tinted faintly by the chat's colour), framed by a
|
||||
/// hairline, with a thin accent bar in the chat's colour (the accent for a
|
||||
/// chat with none) along its leading edge. It spans the reading column.
|
||||
class NotebookInputCell extends StatelessWidget {
|
||||
const NotebookInputCell({super.key, required this.child});
|
||||
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
final shade = chatColorShade(context, ChatBubbleColor.of(context));
|
||||
final base = scheme.surfaceContainerLow;
|
||||
return Material(
|
||||
key: const Key('chat-notebook-input-cell'),
|
||||
type: MaterialType.transparency,
|
||||
child: NotebookCellFrame(
|
||||
fill: shade == null
|
||||
? base
|
||||
: Color.alphaBlend(
|
||||
shade.withValues(alpha: notebookInputTint),
|
||||
base,
|
||||
),
|
||||
border: scheme.outlineVariant,
|
||||
bar: shade ?? scheme.primary,
|
||||
child: child,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A prompt the agent or the server sent (a follow-up, an auto-continue, a
|
||||
/// switch of account) under a notebook transcript: an input cell like the
|
||||
/// operator's, its bar in the muted outline rather than the chat's colour,
|
||||
/// headed by a small [tag] ("scheduled") and the [detail] of when; the
|
||||
/// prompt's [body] beneath.
|
||||
class NotebookScheduledCell extends StatelessWidget {
|
||||
const NotebookScheduledCell({
|
||||
super.key,
|
||||
required this.tag,
|
||||
required this.label,
|
||||
this.detail,
|
||||
required this.body,
|
||||
required this.hasBody,
|
||||
});
|
||||
|
||||
/// The tag's word: `scheduled`, `auto-continue` or `switched`.
|
||||
final String tag;
|
||||
|
||||
/// What sent it, e.g. `Follow-up from agent`.
|
||||
final String label;
|
||||
final String? detail;
|
||||
final Widget body;
|
||||
final bool hasBody;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final scheme = theme.colorScheme;
|
||||
final muted = theme.textTheme.labelMedium?.copyWith(
|
||||
color: scheme.onSurfaceVariant,
|
||||
);
|
||||
final header = SelectionContainer.disabled(
|
||||
child: Wrap(
|
||||
key: const Key('chat-automated-prompt-header'),
|
||||
spacing: 6,
|
||||
runSpacing: 2,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
NotebookTag(tag, key: const Key('chat-notebook-scheduled-tag')),
|
||||
Text(label, style: muted?.copyWith(fontWeight: FontWeight.w600)),
|
||||
if (detail != null) Text('· $detail', style: muted),
|
||||
],
|
||||
),
|
||||
);
|
||||
// A cell spans the column, as the operator's own do.
|
||||
return Material(
|
||||
key: const Key('chat-automated-prompt'),
|
||||
type: MaterialType.transparency,
|
||||
child: NotebookCellFrame(
|
||||
width: double.infinity,
|
||||
fill: scheme.surfaceContainerLow,
|
||||
border: scheme.outlineVariant,
|
||||
bar: scheme.outline,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
header,
|
||||
if (hasBody) ...[
|
||||
const SizedBox(height: 4),
|
||||
DefaultTextStyle.merge(
|
||||
style: chatMessageTextStyle(
|
||||
context,
|
||||
)?.copyWith(color: scheme.onSurfaceVariant),
|
||||
child: body,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A notebook's small tag: a word in a hairline box in 3px corners, the
|
||||
/// cell grey a step darker, as an IDE marks a cell's state.
|
||||
class NotebookTag extends StatelessWidget {
|
||||
const NotebookTag(this.text, {super.key, this.color});
|
||||
|
||||
final String text;
|
||||
|
||||
/// The tag's ink; the secondary ink by default.
|
||||
final Color? color;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final scheme = theme.colorScheme;
|
||||
return DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
color: scheme.surfaceContainerHigh,
|
||||
border: Border.all(color: scheme.outlineVariant),
|
||||
borderRadius: const BorderRadius.all(Radius.circular(3)),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 1),
|
||||
child: Text(
|
||||
text,
|
||||
style: theme.textTheme.labelSmall?.copyWith(
|
||||
color: color ?? scheme.onSurfaceVariant,
|
||||
letterSpacing: 0.2,
|
||||
height: 1.3,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../../core/api_gateway.dart';
|
||||
import '../../../../../ui/text/monospace.dart';
|
||||
import '../../../../../ui/theme/app_design_tokens.dart';
|
||||
import '../../../domain/chat_tool_calls.dart';
|
||||
import '../chat_message_bubble.dart';
|
||||
import 'notebook_cell_frame.dart';
|
||||
|
||||
/// How many lines of a call's output its collapsed sub-cell previews.
|
||||
const int notebookToolPreviewLines = 3;
|
||||
|
||||
/// The most lines an expanded sub-cell draws in place; the rest stay in the
|
||||
/// activity details.
|
||||
const int notebookToolExpandedLines = 400;
|
||||
|
||||
/// Where a sub-cell's text starts: past the collapse chevron, so the
|
||||
/// preview and the output line up under the tool's name.
|
||||
const double notebookToolTextInset = 26;
|
||||
|
||||
/// The monospace style of a tool sub-cell at the reading size's step down.
|
||||
TextStyle notebookToolStyle(BuildContext context) => TextStyle(
|
||||
fontFamily: appMonospaceFamily,
|
||||
fontSize: (chatMessageTextStyle(context)?.fontSize ?? 15) * 0.82,
|
||||
height: 1.4,
|
||||
letterSpacing: 0,
|
||||
color: Theme.of(context).colorScheme.onSurface,
|
||||
);
|
||||
|
||||
/// The first [notebookToolPreviewLines] non-blank lines of [text] and how
|
||||
/// many lines follow them.
|
||||
({String preview, int more}) notebookToolPreview(String text) {
|
||||
final lines = text.trimRight().split('\n');
|
||||
var start = 0;
|
||||
while (start < lines.length && lines[start].trim().isEmpty) {
|
||||
start++;
|
||||
}
|
||||
final end = (start + notebookToolPreviewLines).clamp(0, lines.length);
|
||||
return (
|
||||
preview: lines.sublist(start, end).map((l) => l.trimRight()).join('\n'),
|
||||
more: lines.length - end,
|
||||
);
|
||||
}
|
||||
|
||||
/// One tool call as a notebook sub-cell: a header with the collapse
|
||||
/// chevron, the tool's name and arguments, and its duration and status on
|
||||
/// the right; beneath it, collapsed, the first lines of the output with
|
||||
/// "+N lines", or, expanded in place, the call's input and output. The
|
||||
/// expanded body is built only once opened.
|
||||
class NotebookToolCell extends StatefulWidget {
|
||||
const NotebookToolCell({super.key, required this.call});
|
||||
|
||||
final JsonMap call;
|
||||
|
||||
@override
|
||||
State<NotebookToolCell> createState() => _NotebookToolCellState();
|
||||
}
|
||||
|
||||
class _NotebookToolCellState extends State<NotebookToolCell> {
|
||||
bool _expanded = false;
|
||||
|
||||
// The preview of the last output seen, so a running turn's ticking
|
||||
// rebuilds do not split a long output again.
|
||||
String? _source;
|
||||
({String preview, int more}) _preview = (preview: '', more: 0);
|
||||
|
||||
void _toggle() => setState(() => _expanded = !_expanded);
|
||||
|
||||
({String preview, int more}) _previewOf(String text) {
|
||||
if (!identical(text, _source) && text != _source) {
|
||||
_source = text;
|
||||
_preview = notebookToolPreview(text);
|
||||
}
|
||||
return _preview;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final call = widget.call;
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
final ansi = AppDesignTokens.of(context).ansi;
|
||||
final style = notebookToolStyle(context);
|
||||
final muted = style.copyWith(color: scheme.onSurfaceVariant);
|
||||
final failed = chatToolFailed(call);
|
||||
final running = chatToolRunning(call);
|
||||
final name = chatToolLabel(call);
|
||||
final detail = chatToolDetail(call);
|
||||
final millis = chatToolDurationMillis(call);
|
||||
final output = running
|
||||
? ''
|
||||
: failed
|
||||
? chatToolErrorText(call)
|
||||
: chatToolOutputText(call);
|
||||
final (statusIcon, statusColor, statusWord) = failed
|
||||
? (Icons.close, ansi?.red ?? scheme.error, 'failed')
|
||||
: running
|
||||
? (Icons.more_horiz, ansi?.yellow ?? scheme.tertiary, 'running')
|
||||
: (Icons.check, ansi?.green ?? scheme.primary, 'done');
|
||||
final header = Semantics(
|
||||
button: true,
|
||||
expanded: _expanded,
|
||||
label: [
|
||||
name,
|
||||
?detail,
|
||||
if (millis != null) formatChatToolDuration(millis),
|
||||
statusWord,
|
||||
].join(', '),
|
||||
excludeSemantics: true,
|
||||
onTap: _toggle,
|
||||
child: InkWell(
|
||||
key: const Key('chat-notebook-tool-header'),
|
||||
onTap: _toggle,
|
||||
borderRadius: BorderRadius.circular(notebookCellRadius(context)),
|
||||
child: Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(6, 5, 10, 5),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
_expanded ? Icons.expand_more : Icons.chevron_right,
|
||||
size: 18,
|
||||
color: scheme.onSurfaceVariant,
|
||||
),
|
||||
const SizedBox(width: 2),
|
||||
Expanded(
|
||||
child: Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: name,
|
||||
style: const TextStyle(fontWeight: FontWeight.w700),
|
||||
),
|
||||
if (detail != null)
|
||||
TextSpan(text: ' $detail', style: muted),
|
||||
],
|
||||
),
|
||||
style: style,
|
||||
maxLines: 1,
|
||||
softWrap: false,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
if (millis != null) ...[
|
||||
const SizedBox(width: 12),
|
||||
Text(
|
||||
formatChatToolDuration(millis),
|
||||
key: const Key('chat-notebook-tool-duration'),
|
||||
style: muted,
|
||||
),
|
||||
],
|
||||
const SizedBox(width: 8),
|
||||
Icon(
|
||||
statusIcon,
|
||||
key: const Key('chat-notebook-tool-status'),
|
||||
size: 15,
|
||||
color: statusColor,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
final body = _expanded
|
||||
? _expandedBody(call, style, muted, failed ? statusColor : null)
|
||||
: _collapsedBody(output, style, muted, failed ? statusColor : null);
|
||||
return Material(
|
||||
key: const Key('chat-notebook-tool-cell'),
|
||||
type: MaterialType.transparency,
|
||||
child: NotebookCellFrame(
|
||||
fill: scheme.surfaceContainerLow,
|
||||
border: scheme.outlineVariant,
|
||||
padding: EdgeInsets.zero,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [header, ?body],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget? _collapsedBody(
|
||||
String output,
|
||||
TextStyle style,
|
||||
TextStyle muted,
|
||||
Color? error,
|
||||
) {
|
||||
if (output.trim().isEmpty) return null;
|
||||
final (:preview, :more) = _previewOf(output);
|
||||
if (preview.isEmpty) return null;
|
||||
return _section(
|
||||
Column(
|
||||
key: const Key('chat-notebook-tool-preview'),
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
preview,
|
||||
maxLines: notebookToolPreviewLines,
|
||||
softWrap: false,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: error == null ? muted : muted.copyWith(color: error),
|
||||
),
|
||||
if (more > 0)
|
||||
Text(
|
||||
'+$more ${more == 1 ? 'line' : 'lines'}',
|
||||
key: const Key('chat-notebook-tool-more'),
|
||||
style: muted.copyWith(
|
||||
fontStyle: FontStyle.italic,
|
||||
color: Theme.of(context).colorScheme.outline,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _expandedBody(
|
||||
JsonMap call,
|
||||
TextStyle style,
|
||||
TextStyle muted,
|
||||
Color? error,
|
||||
) {
|
||||
final input = chatToolInputText(call).trimRight();
|
||||
final running = chatToolRunning(call);
|
||||
final output = running
|
||||
? 'running…'
|
||||
: error != null
|
||||
? chatToolErrorText(call)
|
||||
: chatToolOutputText(call);
|
||||
final lines = output.trimRight().split('\n');
|
||||
final cut = lines.length > notebookToolExpandedLines;
|
||||
final shown = cut
|
||||
? lines.take(notebookToolExpandedLines).join('\n')
|
||||
: output.trimRight();
|
||||
final heading = muted.copyWith(
|
||||
fontSize: (muted.fontSize ?? 12) * 0.9,
|
||||
fontWeight: FontWeight.w600,
|
||||
);
|
||||
return _section(
|
||||
Column(
|
||||
key: const Key('chat-notebook-tool-output'),
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (input.isNotEmpty) ...[
|
||||
Text('input', style: heading),
|
||||
Text(input, style: muted),
|
||||
const SizedBox(height: 6),
|
||||
],
|
||||
Text(error != null ? 'error' : 'output', style: heading),
|
||||
Text(
|
||||
shown,
|
||||
style: error == null ? style : style.copyWith(color: error),
|
||||
),
|
||||
if (cut)
|
||||
Text(
|
||||
'… ${lines.length - notebookToolExpandedLines} more lines '
|
||||
'in the activity details',
|
||||
style: muted.copyWith(fontStyle: FontStyle.italic),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// A body section under the header's hairline, its text under the name.
|
||||
Widget _section(Widget child) => DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
top: BorderSide(color: Theme.of(context).colorScheme.outlineVariant),
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(
|
||||
notebookToolTextInset,
|
||||
5,
|
||||
10,
|
||||
6,
|
||||
),
|
||||
child: child,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../../core/api_gateway.dart';
|
||||
import '../../../../../ui/theme/app_design_tokens.dart';
|
||||
import '../../../domain/chat_turn_activity.dart';
|
||||
import '../chat_response_pending.dart';
|
||||
import 'notebook_cell_frame.dart';
|
||||
import 'notebook_tool_cell.dart';
|
||||
|
||||
/// How many of a turn's calls the Out cell lists before folding the earlier
|
||||
/// ones behind "Show N earlier".
|
||||
const int notebookToolMaxCalls = 8;
|
||||
|
||||
/// The gap between two sub-cells.
|
||||
const double notebookSubCellGap = 4;
|
||||
|
||||
/// A turn's activity under a notebook transcript: each tool call as a
|
||||
/// collapsible sub-cell ([NotebookToolCell]), the latest
|
||||
/// [notebookToolMaxCalls] shown and the rest behind one "Show N earlier"
|
||||
/// line, then the turn's status line: while it runs, what it is doing and
|
||||
/// for how long in the accent; once settled, its summary ("Used 3 tools").
|
||||
/// The status line opens the activity details, as every design's does.
|
||||
class NotebookToolCells extends StatefulWidget {
|
||||
const NotebookToolCells({
|
||||
super.key,
|
||||
required this.data,
|
||||
required this.status,
|
||||
required this.streaming,
|
||||
required this.onOpen,
|
||||
this.seconds = 0,
|
||||
});
|
||||
|
||||
final ChatTurnActivityData data;
|
||||
final String status;
|
||||
final bool streaming;
|
||||
final VoidCallback onOpen;
|
||||
final int seconds;
|
||||
|
||||
@override
|
||||
State<NotebookToolCells> createState() => _NotebookToolCellsState();
|
||||
}
|
||||
|
||||
class _NotebookToolCellsState extends State<NotebookToolCells> {
|
||||
bool _all = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final calls = widget.data.tools;
|
||||
final shown = _all || calls.length <= notebookToolMaxCalls
|
||||
? calls
|
||||
: calls.sublist(calls.length - notebookToolMaxCalls);
|
||||
final hidden = calls.length - shown.length;
|
||||
return Column(
|
||||
key: const Key('chat-notebook-tool-cells'),
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
if (hidden > 0) _earlier(context, hidden),
|
||||
for (var index = 0; index < shown.length; index++)
|
||||
Padding(
|
||||
key: ValueKey<String>('chat-notebook-tool-${_id(shown[index])}'),
|
||||
padding: const EdgeInsets.only(bottom: notebookSubCellGap),
|
||||
child: NotebookToolCell(call: shown[index]),
|
||||
),
|
||||
_statusLine(context),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
String _id(JsonMap call) => '${call['id'] ?? identityHashCode(call)}';
|
||||
|
||||
Widget _earlier(BuildContext context, int hidden) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
return Align(
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
child: TextButton(
|
||||
key: const Key('chat-notebook-tool-earlier'),
|
||||
style: TextButton.styleFrom(
|
||||
minimumSize: const Size(0, 28),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
|
||||
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
foregroundColor: scheme.onSurfaceVariant,
|
||||
textStyle: notebookToolStyle(context),
|
||||
),
|
||||
onPressed: () => setState(() => _all = true),
|
||||
child: Text(
|
||||
'Show $hidden earlier tool ${hidden == 1 ? 'call' : 'calls'}',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// The turn's status: the pending dots and the live label while it runs,
|
||||
/// else the muted summary with a chevron. Opens the activity details.
|
||||
Widget _statusLine(BuildContext context) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
final accent = AppDesignTokens.of(context).ansi?.yellow ?? scheme.primary;
|
||||
final failed = widget.data.failed && !widget.streaming;
|
||||
final color = widget.streaming
|
||||
? accent
|
||||
: failed
|
||||
? scheme.error
|
||||
: scheme.onSurfaceVariant;
|
||||
final style = notebookToolStyle(context).copyWith(color: color);
|
||||
final text = widget.streaming
|
||||
? '${widget.data.label}… ${widget.seconds}s'
|
||||
: widget.status;
|
||||
return Semantics(
|
||||
button: true,
|
||||
label: '${widget.status}, expand for details',
|
||||
excludeSemantics: true,
|
||||
onTap: widget.onOpen,
|
||||
child: Align(
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
widthFactor: 1,
|
||||
child: InkWell(
|
||||
key: const Key('chat-turn-activity'),
|
||||
onTap: widget.onOpen,
|
||||
borderRadius: BorderRadius.circular(notebookCellRadius(context)),
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(minHeight: 28),
|
||||
child: Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(6, 3, 8, 3),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (widget.streaming)
|
||||
ExcludeSemantics(
|
||||
child: DefaultTextStyle.merge(
|
||||
style: TextStyle(color: accent),
|
||||
child: const ChatResponsePending(),
|
||||
),
|
||||
)
|
||||
else
|
||||
Icon(Icons.chevron_right, size: 18, color: color),
|
||||
const SizedBox(width: 2),
|
||||
Flexible(
|
||||
child: Text(
|
||||
text,
|
||||
key: const Key('chat-notebook-status'),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: style,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user