Files
pipelines/app/lib/features/chat/ui/transcript/chat_markdown_view.dart
T
alan b1fcdcf2c3 Chat: unnamed chats take the harness title, harness default per folder, word-safe tables, Send later
- New chat: a cleared Name creates the chat without an operator title; the
  list shows the harness's own title, or the default name until it has one.
  The server no longer records "New chat" for a blank title and OpenCode
  is created without a title so it names the session itself.
- New chat harness defaults to the one last used in the chosen folder,
  else anywhere, else OpenCode; remembered on the device per server.
- Markdown tables wrap at word boundaries and never go below a column's
  longest word (tokens over ~40 characters may break); a table whose words
  cannot fit scrolls sideways with the code block affordance.
- Composer: right-click, long-press, Shift+F10 or the context menu key on
  Send opens Send later... (30 min, 1 h, tomorrow 9:00, usage reset, pick a
  time), sent through the same path with send_at.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 19:55:40 +00:00

526 lines
17 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'chat_transcript_selection.dart';
import 'chat_code_hover.dart';
import '../../../../ui/text/monospace.dart';
import 'chat_link_launcher.dart';
import 'chat_link_spans.dart';
import 'chat_markdown.dart';
import 'chat_markdown_cache.dart';
import 'chat_markdown_speech.dart';
import 'chat_message_bubble.dart';
import '../../speech/speech.dart';
import 'chat_table_cell_measure.dart';
import 'chat_table_viewport.dart';
/// Renders one assistant text part with native text widgets and the host's own
/// theme roles. Links (`http`, `https`, `mailto` and `www.` hosts only) open
/// through [onOpenLink] when given, else the [ChatLinkScope] launcher, which by
/// default opens them outside the app. No font family, size, colour or component
/// dimension is overridden; code keeps its exact whitespace and scrolls
/// sideways instead of widening the transcript, while table cells wrap.
class ChatMarkdownView extends StatefulWidget {
const ChatMarkdownView({
super.key,
required this.text,
this.messageId,
this.onOpenLink,
});
final String text;
final String? messageId;
final ValueChanged<String>? onOpenLink;
@override
State<ChatMarkdownView> createState() => _ChatMarkdownViewState();
}
/// The transcript's `ChatMessageTextBuilder` seam: a caller that already has the
/// markdown renderer can pass this function as `textBuilder`.
Widget chatMarkdownText(
BuildContext context,
String text, {
ValueChanged<String>? onOpenLink,
}) => ChatMarkdownView(text: text, onOpenLink: onOpenLink);
class _ChatMarkdownViewState extends State<ChatMarkdownView> {
late final ChatLinkSpans _links = ChatLinkSpans(
onChanged: () => setState(() => _rendered = null),
);
Widget? _rendered;
final _codeHover = ChatCodeHover();
ScrollPosition? _scroll;
ModalRoute<dynamic>? _route;
var _sourceCursor = 0;
SpeechRange? _revealed;
final _textKeys = <GlobalKey>[];
var _textCount = 0;
final _segments = <MarkdownSpokenSegment>[];
@override
void didUpdateWidget(ChatMarkdownView oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.text != oldWidget.text) {
_rendered = null;
_codeHover.hide();
}
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
// Cached spans use host theme/localization roles. Never freeze those when
// brightness, typography, locale or surrounding text styles change.
_rendered = null;
_codeHover.hide();
_route = ModalRoute.of(context);
_scroll?.removeListener(_codeHover.hide);
_scroll = Scrollable.maybeOf(context)?.position;
_scroll?.addListener(_codeHover.hide);
}
@override
void dispose() {
_codeHover.dispose();
_scroll?.removeListener(_codeHover.hide);
_links.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// Keep the state subscribed to inherited styling even when the cached
// subtree is returned without rebuilding its Markdown spans.
Theme.of(context);
final speech = speechController;
return ListenableBuilder(
listenable: Listenable.merge([speech, speech.word]),
builder: (context, _) {
final range = spokenSourceIn(speech, widget.messageId, widget.text);
if (range == null) {
_revealed = null;
} else if (range != _revealed) {
_revealed = range;
WidgetsBinding.instance.addPostFrameCallback((_) => _reveal(range));
}
// The text is built once and never restyled while speaking; the
// spoken word is painted behind it, so nothing shifts or rewraps.
final rendered = _rendered ??= _buildRendered(context);
return CustomPaint(
key: _paintKey,
painter: SpokenWordPainter(
anchor: _paintKey,
segments: _segments,
range: () => spokenSourceIn(speech, widget.messageId, widget.text),
color: Theme.of(context).colorScheme.primaryContainer,
repaint: Listenable.merge([speech, speech.word]),
),
child: rendered,
);
},
);
}
final _paintKey = GlobalKey();
Widget _buildRendered(BuildContext context) {
_sourceCursor = 0;
_textCount = 0;
_segments.clear();
final gap = chatParagraphGap(context);
final blocks = chatMarkdownCache.blocksFor(widget.text);
final rendered = Listener(
onPointerDown: (_) => _codeHover.hide(),
onPointerSignal: (_) => _codeHover.hide(),
child: NotificationListener<ScrollNotification>(
onNotification: (_) {
_codeHover.hide();
return false;
},
child: _links.selectionArea(
context,
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
for (var index = 0; index < blocks.length; index += 1)
Padding(
padding: EdgeInsets.only(
top: index == 0 ? 0 : _gapAfter(blocks[index - 1], gap),
),
child: _block(context, blocks[index]),
),
],
),
),
),
);
_rendered = rendered;
return rendered;
}
/// Blocks are separated by an empty line of body text, like paragraphs;
/// a heading stays close to the content it introduces.
double _gapAfter(MarkdownBlock previous, double paragraphGap) =>
previous is MarkdownHeading ? 8 : paragraphGap;
Widget _block(BuildContext context, MarkdownBlock block) {
switch (block) {
case MarkdownHeading(level: final level, text: final text):
return Semantics(
container: true,
headingLevel: level,
child: _text(context, text, style: _headingStyle(context, level)),
);
case MarkdownParagraph(text: final text):
return _text(context, text);
case MarkdownRule():
return Divider(color: _dividerColor(context));
case MarkdownQuote(lines: final lines):
return DecoratedBox(
decoration: BoxDecoration(
border: BorderDirectional(
start: BorderSide(color: _dividerColor(context)),
),
),
child: Padding(
padding: const EdgeInsetsDirectional.only(start: 12),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
spacing: 4,
children: [
for (final line in lines)
if (line.trim().isNotEmpty) _text(context, line),
],
),
),
);
case MarkdownList(
ordered: final ordered,
start: final start,
items: final items,
):
return Semantics(
container: true,
explicitChildNodes: true,
role: SemanticsRole.list,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
spacing: 4,
children: [
for (var index = 0; index < items.length; index += 1)
Semantics(
container: true,
role: SemanticsRole.listItem,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsetsDirectional.only(end: 8),
child: Text(ordered ? '${start + index}.' : '•'),
),
Expanded(child: _listItem(context, items[index])),
],
),
),
],
),
);
case MarkdownTable(header: final header, rows: final rows):
// Cells wrap at word boundaries to the transcript width; a table
// whose longest words cannot fit scrolls sideways instead.
return ChatTableViewport(
columns: header.length,
border: TableBorder.all(color: _dividerColor(context)),
rows: [
TableRow(
decoration: BoxDecoration(color: _headerFill(context)),
children: [
for (final cell in header) _cell(context, cell, header: true),
],
),
for (final row in rows)
TableRow(
children: [for (final cell in row) _cell(context, cell)],
),
],
);
case MarkdownCode(language: final language, code: final code):
_skipCodeBlock(code);
final labelStyle = _roleStyle(
context,
Theme.of(context).textTheme.labelSmall,
);
return ChatCodeCopyRegion(
code: code,
label: Text(markdownCodeLabel(language), style: labelStyle),
child: DecoratedBox(
decoration: BoxDecoration(color: _codeFill(context)),
child: Padding(
padding: const EdgeInsets.all(12),
child: ChatTranscriptHorizontalScrollView(
label: 'code block',
child: Text.rich(
TextSpan(
children: _linked(
context,
code,
(part) => TextSpan(text: part),
),
),
style: const TextStyle(fontFamily: appMonospaceFamily),
),
),
),
),
);
}
}
void _skipCodeBlock(String code) {
_sourceCursor = fencedCodeSpan(
widget.text,
_sourceCursor,
code,
).end.clamp(0, widget.text.length);
}
/// An item's text and, under it, whatever is nested in it.
Widget _listItem(BuildContext context, MarkdownListItem item) {
if (item.children.isEmpty) return _text(context, item.text);
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
spacing: 4,
children: [
_text(context, item.text),
for (final child in item.children) _block(context, child),
],
);
}
Widget _cell(BuildContext context, String text, {bool header = false}) {
final style = header
? _roleStyle(context, Theme.of(context).textTheme.labelLarge)
: null;
return Semantics(
role: header ? SemanticsRole.columnHeader : null,
child: Padding(
padding: const EdgeInsets.all(8),
child: ChatTableCellMeasure(
source: text,
child: _text(context, text, style: style),
),
),
);
}
Widget _text(BuildContext context, String text, {TextStyle? style}) {
if (text.isEmpty) return const SizedBox.shrink();
final sourceStart = widget.text.indexOf(text, _sourceCursor);
if (sourceStart >= 0) _sourceCursor = sourceStart + text.length;
// Keys follow render order, which is stable for the same text, so the
// revealed word's paragraph can be found after the frame.
if (_textCount == _textKeys.length) _textKeys.add(GlobalKey());
final key = _textKeys[_textCount++];
final source = MarkdownSourceCursor(
widget.text,
sourceStart < 0 ? _sourceCursor : sourceStart,
key,
);
return Builder(
builder: (textContext) => Text.rich(
key: key,
TextSpan(children: _spans(textContext, parseInline(text), source)),
style: style,
),
);
}
List<InlineSpan> _spans(
BuildContext context,
List<MarkdownInline> spans,
MarkdownSourceCursor source,
) => [
for (final span in spans)
switch (span) {
MarkdownPlain(text: final text) => _leaf(context, text, source),
MarkdownInlineCode(text: final text) => _inlineCode(
context,
text,
source,
),
MarkdownStrong(spans: final inner) => TextSpan(
style: const TextStyle(fontWeight: FontWeight.bold),
children: _spans(context, inner, source),
),
MarkdownEmphasis(spans: final inner) => TextSpan(
style: const TextStyle(fontStyle: FontStyle.italic),
children: _spans(context, inner, source),
),
MarkdownLink(url: final url, spans: final inner) => _link(
context,
url,
inner,
source,
),
},
];
TextSpan _leaf(
BuildContext context,
String text,
MarkdownSourceCursor source,
) => _highlightedText(context, text, source);
TextSpan _inlineCode(
BuildContext context,
String text,
MarkdownSourceCursor source,
) {
final token = text;
TextSpan piece(String part) {
final highlighted = _highlightedText(context, part, source);
return TextSpan(
text: highlighted.text,
children: highlighted.children,
onEnter: (event) {
if (_route?.isCurrent != false) {
_codeHover.show(context, token, text, event.position);
}
},
onExit: (_) => _codeHover.scheduleHide(token),
);
}
return TextSpan(
style: TextStyle(
fontFamily: appMonospaceFamily,
// A span's background is painted over the selection highlight, so an
// opaque fill hid a selected code span; a partial fill keeps code
// distinct and lets the selection show through.
backgroundColor: _codeFill(context).withValues(alpha: _inlineCodeAlpha),
),
children: _linked(context, text, piece),
);
}
/// [text]'s bare URLs as links and the runs between them as [piece] spans,
/// so a URL quoted in code is highlighted and opens like one in prose.
List<InlineSpan> _linked(
BuildContext context,
String text,
TextSpan Function(String part) piece,
) {
final spans = <InlineSpan>[];
var at = 0;
for (final link in findTextLinks(text)) {
if (link.start > at) spans.add(piece(text.substring(at, link.start)));
spans.add(
_links.link(context, link.target, [
piece(text.substring(link.start, link.end)),
], onOpen: (uri) => _open(context, uri)),
);
at = link.end;
}
if (at < text.length || spans.isEmpty) spans.add(piece(text.substring(at)));
return spans;
}
void _open(BuildContext context, Uri uri) {
final onOpenLink = widget.onOpenLink;
if (onOpenLink != null) return onOpenLink(uri.toString());
unawaited(openChatLink(context, uri));
}
InlineSpan _link(
BuildContext context,
String url,
List<MarkdownInline> spans,
MarkdownSourceCursor source,
) => _links.link(
context,
url,
_spans(context, spans, source),
onOpen: (uri) => _open(context, uri),
);
/// A leaf's spans, recording where it renders so a spoken word in it can
/// be revealed, with the spoken characters highlighted.
TextSpan _highlightedText(
BuildContext context,
String text,
MarkdownSourceCursor source,
) {
final offsets = source.consume(text);
final renderedStart = source.visibleOffset;
source.visibleOffset += text.length;
if (offsets.isNotEmpty) {
_segments.add(
MarkdownSpokenSegment(
source.key,
offsets.reduce((a, b) => a < b ? a : b),
offsets.reduce((a, b) => a > b ? a : b) + 1,
renderedStart,
source.visibleOffset,
offsets,
),
);
}
return TextSpan(text: text);
}
void _reveal(SpeechRange range) {
final scroll = _scroll;
if (!mounted || range != _revealed || scroll == null) return;
revealSpokenWord(
scroll,
_segments,
range,
reduceMotion: MediaQuery.maybeDisableAnimationsOf(context) ?? false,
);
}
/// Heading roles are a non-increasing sequence from the host's own text
/// theme. H3–H6 share one title role, matching the web renderer's flat
/// lower-heading treatment, and no level falls back to a body role, so a
/// heading never reads like a paragraph.
TextStyle? _headingStyle(BuildContext context, int level) {
final text = Theme.of(context).textTheme;
final role = switch (level) {
1 => text.headlineSmall,
2 => text.titleLarge,
_ => text.titleMedium,
};
return _roleStyle(context, role);
}
/// Merges a host theme role into the ambient text style.
TextStyle? _roleStyle(BuildContext context, TextStyle? role) => role == null
? null
: DefaultTextStyle.of(
context,
).style.copyWith(inherit: true).merge(role.copyWith(inherit: true));
Color _dividerColor(BuildContext context) {
final theme = Theme.of(context);
return theme.dividerTheme.color ?? theme.dividerColor;
}
/// How much of the code fill an inline code span keeps, so a selection
/// behind it stays visible.
static const double _inlineCodeAlpha = 0.5;
Color _codeFill(BuildContext context) =>
Theme.of(context).colorScheme.surfaceContainerHighest;
Color _headerFill(BuildContext context) =>
Theme.of(context).colorScheme.surfaceContainerHigh;
}