Separate chunked reply paragraphs with an empty-line gap
Consecutive text parts of an agent turn (including folded messages) and markdown blocks within a part are now separated by ~0.9x the scaled body font size; a heading stays 8px from what it introduces. Adds a ?paras=1 chunked reply to the composer probe and gap tests at 1x/2x.
This commit is contained in:
@@ -6,6 +6,7 @@ import 'package:flutter/semantics.dart';
|
||||
import '../../../../ui/adaptive/host.dart';
|
||||
import '../sessions/chat_adaptive.dart';
|
||||
import 'chat_markdown.dart';
|
||||
import 'chat_message_bubble.dart';
|
||||
|
||||
/// Renders one assistant text part with native text widgets and the host's own
|
||||
/// theme roles. Link activation is the caller's job: [onOpenLink] receives only
|
||||
@@ -66,20 +67,31 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
if (_rendered != null) return _rendered!;
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final gap = chatParagraphGap(context);
|
||||
final blocks = parseMarkdown(widget.text);
|
||||
return _rendered = _selectable(
|
||||
context,
|
||||
Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
spacing: 8,
|
||||
children: [
|
||||
for (final block in parseMarkdown(widget.text))
|
||||
_block(context, block, cupertino),
|
||||
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], cupertino),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 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;
|
||||
|
||||
/// Selection is provided by the native [SelectionArea]. A Cupertino-only app
|
||||
/// has no `MaterialLocalizations`, which `SelectionArea` asserts on, so one
|
||||
/// English default localization scope is added there for its context menu.
|
||||
|
||||
@@ -66,3 +66,11 @@ TextStyle? chatMessageTextStyle(BuildContext context, bool cupertino) =>
|
||||
cupertino
|
||||
? CupertinoTheme.of(context).textTheme.textStyle
|
||||
: Theme.of(context).textTheme.bodyLarge;
|
||||
|
||||
/// The empty-line gap between two paragraphs of a reply: about one line of
|
||||
/// body text at [fontSize] (else the ambient text size), scaled by the
|
||||
/// operator's text scaling, so chunks read as separate paragraphs.
|
||||
double chatParagraphGap(BuildContext context, [double? fontSize]) {
|
||||
final size = fontSize ?? DefaultTextStyle.of(context).style.fontSize ?? 14;
|
||||
return MediaQuery.textScalerOf(context).scale(size) * 0.9;
|
||||
}
|
||||
|
||||
@@ -228,10 +228,25 @@ class ChatMessageView extends StatelessWidget {
|
||||
final widgets = <Widget>[];
|
||||
final parts = _partsOf(row);
|
||||
final seen = <String, int>{};
|
||||
// Long replies arrive as several text parts (and folded messages); an
|
||||
// empty line between consecutive ones keeps them reading as paragraphs.
|
||||
final gap = chatParagraphGap(
|
||||
context,
|
||||
chatMessageTextStyle(context, _isCupertino(context))?.fontSize,
|
||||
);
|
||||
var previousText = false;
|
||||
for (var index = 0; index < parts.length; index++) {
|
||||
final part = parts[index];
|
||||
final widget = _agentPart(context, part);
|
||||
var widget = _agentPart(context, part);
|
||||
if (widget == null) continue;
|
||||
final text = _isTextPart(part);
|
||||
if (text && previousText) {
|
||||
widget = Padding(
|
||||
padding: EdgeInsets.only(top: gap),
|
||||
child: widget,
|
||||
);
|
||||
}
|
||||
previousText = text;
|
||||
// Part identity, not position, so an older assistant message that folds
|
||||
// into the same turn can prepend its parts without moving an open
|
||||
// disclosure. The enclosing row's identity is carried by ChatRowKeys; a
|
||||
@@ -319,6 +334,11 @@ List<JsonMap> _partsOf(Map<dynamic, dynamic> source) {
|
||||
.toList();
|
||||
}
|
||||
|
||||
bool _isTextPart(JsonMap part) =>
|
||||
(part['type'] == null || part['type'] == 'text') &&
|
||||
part['text'] is String &&
|
||||
(part['text'] as String).trim().isNotEmpty;
|
||||
|
||||
bool _isFile(JsonMap part) =>
|
||||
part['type'] == 'file' &&
|
||||
'${part['name'] ?? ''}${part['uri'] ?? ''}'.trim().isNotEmpty;
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_bubble.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
|
||||
|
||||
/// A reply that arrived in chunks: two folded messages, the first with two
|
||||
/// text parts and a tool call, the last part holding two markdown paragraphs.
|
||||
final JsonMap _chunked = transcriptRows([
|
||||
{
|
||||
'id': 'a1',
|
||||
'role': 'assistant',
|
||||
'parts': [
|
||||
{'type': 'reasoning', 'text': 'Thinking it over'},
|
||||
{'type': 'text', 'text': 'First chunk.'},
|
||||
{
|
||||
'type': 'tool',
|
||||
'tool': 'shell',
|
||||
'state': {
|
||||
'status': 'completed',
|
||||
'input': {'command': 'ls'},
|
||||
},
|
||||
},
|
||||
{'type': 'text', 'text': 'Second chunk.'},
|
||||
],
|
||||
},
|
||||
{
|
||||
'id': 'a2',
|
||||
'role': 'assistant',
|
||||
'parts': [
|
||||
{'type': 'text', 'text': 'Third paragraph.\n\nFourth paragraph.'},
|
||||
],
|
||||
},
|
||||
]).single;
|
||||
|
||||
Widget _host({
|
||||
required bool cupertino,
|
||||
required double scale,
|
||||
ChatMessageTextBuilder? textBuilder,
|
||||
}) {
|
||||
final body = ListView(
|
||||
children: [ChatMessageView(row: _chunked, textBuilder: textBuilder)],
|
||||
);
|
||||
Widget scaled(BuildContext context, Widget? child) => MediaQuery(
|
||||
data: MediaQuery.of(context).copyWith(textScaler: TextScaler.linear(scale)),
|
||||
child: child!,
|
||||
);
|
||||
if (cupertino) {
|
||||
return CupertinoApp(
|
||||
builder: scaled,
|
||||
home: CupertinoPageScaffold(child: body),
|
||||
);
|
||||
}
|
||||
return MaterialApp(
|
||||
theme: ThemeData(platform: TargetPlatform.android),
|
||||
builder: scaled,
|
||||
home: Scaffold(body: body),
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
for (final cupertino in [false, true]) {
|
||||
for (final scale in [1.0, 2.0]) {
|
||||
for (final markdown in [false, true]) {
|
||||
final label =
|
||||
'${cupertino ? 'cupertino' : 'material'}, ${scale}x'
|
||||
'${markdown ? ', markdown' : ''}';
|
||||
|
||||
testWidgets('chunks and paragraphs read as paragraphs ($label)', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(800, 2400);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
await tester.pumpWidget(
|
||||
_host(
|
||||
cupertino: cupertino,
|
||||
scale: scale,
|
||||
textBuilder: markdown ? chatMarkdownText : null,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final context = tester.element(find.byType(ChatMessageView));
|
||||
final fontSize = chatMessageTextStyle(context, cupertino)!.fontSize!;
|
||||
final line = fontSize * scale;
|
||||
Rect rect(String text) => tester.getRect(
|
||||
find.textContaining(text, findRichText: true).first,
|
||||
);
|
||||
double gap(String above, String below) =>
|
||||
rect(below).top - rect(above).bottom;
|
||||
|
||||
// Consecutive text parts, across a tool call and a folded message.
|
||||
expect(gap('First chunk.', 'Second chunk.'), greaterThan(0.8 * line));
|
||||
expect(gap('First chunk.', 'Second chunk.'), lessThan(1.5 * line));
|
||||
expect(
|
||||
gap('Second chunk.', 'Third paragraph.'),
|
||||
greaterThan(0.8 * line),
|
||||
);
|
||||
if (markdown) {
|
||||
// Paragraph blocks inside one markdown part.
|
||||
expect(
|
||||
gap('Third paragraph.', 'Fourth paragraph.'),
|
||||
greaterThan(0.8 * line),
|
||||
);
|
||||
expect(
|
||||
gap('Third paragraph.', 'Fourth paragraph.'),
|
||||
lessThan(1.5 * line),
|
||||
);
|
||||
}
|
||||
|
||||
// The activity line keeps its small lead-in, and nothing pads the
|
||||
// footer away from the last paragraph.
|
||||
final activity = tester.getRect(
|
||||
find.byKey(const Key('chat-turn-activity')),
|
||||
);
|
||||
expect(rect('First chunk.').top - activity.bottom, lessThan(line));
|
||||
final last = markdown ? 'Fourth paragraph.' : 'Third paragraph.';
|
||||
final footer = tester.getRect(
|
||||
find.byKey(const Key('chat-message-footer')),
|
||||
);
|
||||
expect(footer.top - rect(last).bottom, lessThan(0.5 * line));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
testWidgets('a heading stays close to the paragraph it introduces', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
const MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ChatMarkdownView(text: '## Plan\nFirst step.\n\nSecond step.'),
|
||||
),
|
||||
),
|
||||
);
|
||||
Rect rect(String text) =>
|
||||
tester.getRect(find.textContaining(text, findRichText: true).first);
|
||||
final headingGap = rect('First step.').top - rect('Plan').bottom;
|
||||
final paragraphGap = rect('Second step.').top - rect('First step.').bottom;
|
||||
expect(headingGap, lessThan(paragraphGap));
|
||||
});
|
||||
}
|
||||
@@ -58,6 +58,10 @@ class _ProbeGateway implements ApiGateway {
|
||||
},
|
||||
],
|
||||
},
|
||||
// `?paras=1`: a long reply that arrived in chunks — several
|
||||
// text parts and a second folded message, each with markdown
|
||||
// paragraphs.
|
||||
if (_query['paras'] == '1') ..._chunkedReply,
|
||||
{
|
||||
'id': 'probe-user',
|
||||
'role': 'user',
|
||||
@@ -124,6 +128,51 @@ class _ProbeGateway implements ApiGateway {
|
||||
String nextOperation() => 'probe-op-${++_sequence}';
|
||||
}
|
||||
|
||||
const _chunkedReply = <JsonMap>[
|
||||
{
|
||||
'id': 'probe-chunked-user',
|
||||
'role': 'user',
|
||||
'parts': [
|
||||
{'type': 'text', 'text': 'Walk me through the deploy plan.'},
|
||||
],
|
||||
},
|
||||
{
|
||||
'id': 'probe-chunked-1',
|
||||
'role': 'assistant',
|
||||
'parts': [
|
||||
{
|
||||
'type': 'text',
|
||||
'text':
|
||||
'First I checked the build. Every target compiles and the '
|
||||
'focused tests pass on this branch.\n\n'
|
||||
'The integration suite is slower, so I ran it in the background '
|
||||
'while reading the release notes.',
|
||||
},
|
||||
{
|
||||
'type': 'text',
|
||||
'text':
|
||||
'Next, the migration. It only adds a column, so it is safe to '
|
||||
'apply before the new server starts.',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
'id': 'probe-chunked-2',
|
||||
'role': 'assistant',
|
||||
'parts': [
|
||||
{
|
||||
'type': 'text',
|
||||
'text':
|
||||
'Finally, the rollout order:\n\n'
|
||||
'1. Apply the migration.\n'
|
||||
'2. Restart the server.\n'
|
||||
'3. Publish the client.\n\n'
|
||||
'Each step can be rolled back on its own.',
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
const _probeLong = bool.fromEnvironment('CHAT_COMPOSER_PROBE_LONG');
|
||||
const _probeModel = String.fromEnvironment(
|
||||
'CHAT_COMPOSER_PROBE_MODEL',
|
||||
@@ -146,7 +195,7 @@ const _probeStyle = String.fromEnvironment(
|
||||
);
|
||||
|
||||
/// Browser overrides: `?platform=cupertino|material`, `?theme=dark|light`,
|
||||
/// `?long=1`, `?tasks=<n>`, `?mode=0` (no permission mode recorded) and
|
||||
/// `?long=1`, `?paras=1` (a chunked multi-paragraph reply), `?tasks=<n>`, `?mode=0` (no permission mode recorded) and
|
||||
/// `?model=0` (a backend without models).
|
||||
Map<String, String> get _query => Uri.base.queryParameters;
|
||||
int get _probeTasks => int.tryParse(_query['tasks'] ?? '') ?? 0;
|
||||
|
||||
Reference in New Issue
Block a user