feat(chat): ChatGPT-style transcript and composer
Agent replies sit on the page without a bubble and get a copy control; the operator's turn is a neutral rounded bubble; the composer is one filled pill with +, a borderless field and a round Send, and its placeholder names the chat's folder. Also fixes two picker tests left stale by the native picker.
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
/// The accessible name of a reply's copy control, and its name just after a
|
||||
/// copy succeeded.
|
||||
const String chatCopyLabel = 'Copy';
|
||||
const String chatCopiedLabel = 'Copied';
|
||||
|
||||
/// An icon-only control beneath a finished reply that puts the reply's text on
|
||||
/// the clipboard. It shows a check for a moment after copying, so the operator
|
||||
/// sees the copy landed without a separate toast.
|
||||
class ChatCopyButton extends StatefulWidget {
|
||||
const ChatCopyButton({
|
||||
super.key,
|
||||
required this.text,
|
||||
required this.cupertino,
|
||||
});
|
||||
|
||||
final String text;
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
State<ChatCopyButton> createState() => _ChatCopyButtonState();
|
||||
}
|
||||
|
||||
class _ChatCopyButtonState extends State<ChatCopyButton> {
|
||||
bool _copied = false;
|
||||
Timer? _reset;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_reset?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _copy() async {
|
||||
await Clipboard.setData(ClipboardData(text: widget.text));
|
||||
if (!mounted) return;
|
||||
setState(() => _copied = true);
|
||||
_reset?.cancel();
|
||||
_reset = Timer(const Duration(seconds: 2), () {
|
||||
if (mounted) setState(() => _copied = false);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final label = _copied ? chatCopiedLabel : chatCopyLabel;
|
||||
if (widget.cupertino) {
|
||||
return Semantics(
|
||||
button: true,
|
||||
label: label,
|
||||
excludeSemantics: true,
|
||||
child: CupertinoButton(
|
||||
key: const Key('chat-message-copy'),
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: _copy,
|
||||
child: Icon(
|
||||
_copied ? CupertinoIcons.checkmark : CupertinoIcons.doc_on_doc,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return IconButton(
|
||||
key: const Key('chat-message-copy'),
|
||||
tooltip: label,
|
||||
onPressed: _copy,
|
||||
icon: Icon(_copied ? Icons.check : Icons.copy_outlined),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// The operator's turn: a neutral filled, evenly rounded bubble with no border,
|
||||
/// in the host's own surface roles. The agent's turn has no bubble at all; its
|
||||
/// text sits on the page, so only the operator's words are enclosed.
|
||||
class ChatUserBubble extends StatelessWidget {
|
||||
const ChatUserBubble({
|
||||
super.key,
|
||||
required this.cupertino,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
final bool cupertino;
|
||||
final Widget child;
|
||||
|
||||
static const BorderRadius _radius = BorderRadius.all(Radius.circular(20));
|
||||
static const EdgeInsets _padding = EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 10,
|
||||
);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (cupertino) {
|
||||
return DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
color: CupertinoColors.systemGrey5.resolveFrom(context),
|
||||
borderRadius: _radius,
|
||||
),
|
||||
child: Padding(padding: _padding, child: child),
|
||||
);
|
||||
}
|
||||
// A Material surface (not a bare DecoratedBox) so a disclosure ListTile
|
||||
// inside the bubble keeps its ink and background.
|
||||
return Material(
|
||||
color: Theme.of(context).colorScheme.surfaceContainerHighest,
|
||||
clipBehavior: Clip.antiAlias,
|
||||
shape: const RoundedRectangleBorder(borderRadius: _radius),
|
||||
child: Padding(padding: _padding, child: child),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The agent's answer drawn straight on the page. Material keeps a transparent
|
||||
/// surface so disclosure ListTiles still paint their ink.
|
||||
class ChatAgentSurface extends StatelessWidget {
|
||||
const ChatAgentSurface({
|
||||
super.key,
|
||||
required this.cupertino,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
final bool cupertino;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => cupertino
|
||||
? child
|
||||
: Material(type: MaterialType.transparency, child: child);
|
||||
}
|
||||
|
||||
/// The transcript's reading text: the host's large body role, so answers read
|
||||
/// at a comfortable size and still follow the operator's text scaling.
|
||||
TextStyle? chatMessageTextStyle(BuildContext context, bool cupertino) =>
|
||||
cupertino
|
||||
? CupertinoTheme.of(context).textTheme.textStyle
|
||||
: Theme.of(context).textTheme.bodyLarge;
|
||||
@@ -6,6 +6,8 @@ import 'package:flutter/material.dart';
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../sessions/chat_adaptive.dart';
|
||||
import '../../domain/chat_tool_calls.dart';
|
||||
import 'chat_copy_button.dart';
|
||||
import 'chat_message_bubble.dart';
|
||||
import 'chat_turn_activity_view.dart';
|
||||
import 'native_disclosure.dart';
|
||||
|
||||
@@ -34,40 +36,18 @@ class ChatMessageView extends StatelessWidget {
|
||||
final role = user ? 'User' : 'Assistant';
|
||||
final cupertino = _isCupertino(context);
|
||||
final content = user ? _userParts(context) : _agentParts(context);
|
||||
// No visible role label, as on the web: side and colour say who spoke and
|
||||
// the row's semantics label names the role. The bubble shrinks to its
|
||||
// content (a disclosure still takes the full measure).
|
||||
final body = Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: content,
|
||||
// No visible role label: side and surface say who spoke and the row's
|
||||
// semantics label names the role. The operator's turn is a neutral bubble
|
||||
// that shrinks to its content; the agent's answer sits on the page with no
|
||||
// bubble, so long replies read as a document rather than a balloon.
|
||||
final body = DefaultTextStyle.merge(
|
||||
style: chatMessageTextStyle(context, cupertino),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: content,
|
||||
),
|
||||
);
|
||||
// The web draws the operator's turn right-aligned in an accent-tinted bubble
|
||||
// and the agent's left-aligned on the neutral surface, both capped to a
|
||||
// readable measure.
|
||||
final contentBody = cupertino
|
||||
? DefaultTextStyle.merge(
|
||||
style: CupertinoTheme.of(context).textTheme.textStyle,
|
||||
child: body,
|
||||
)
|
||||
: body;
|
||||
// A Material surface (not a bare DecoratedBox) so the disclosure ListTiles
|
||||
// inside the bubble keep their ink and background.
|
||||
final bubble = cupertino
|
||||
? Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
decoration: _bubbleDecoration(context, cupertino, user: user),
|
||||
child: contentBody,
|
||||
)
|
||||
: Material(
|
||||
color: _bubbleColor(context, cupertino, user: user),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
shape: _bubbleShape(context, cupertino, user: user),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
child: contentBody,
|
||||
),
|
||||
);
|
||||
return Semantics(
|
||||
container: true,
|
||||
explicitChildNodes: true,
|
||||
@@ -81,13 +61,20 @@ class ChatMessageView extends StatelessWidget {
|
||||
: AlignmentDirectional.centerStart,
|
||||
child: ConstrainedBox(
|
||||
// The operator's turn leaves a margin on a phone, so the two
|
||||
// sides read as a conversation.
|
||||
// sides read as a conversation; the answer keeps a readable
|
||||
// measure on a wide pane.
|
||||
constraints: BoxConstraints(
|
||||
maxWidth: user
|
||||
? (constraints.maxWidth * 0.85).clamp(0, 560).toDouble()
|
||||
: 560,
|
||||
: 720,
|
||||
),
|
||||
child: user ? bubble : _agentTurn(context, content, bubble),
|
||||
child: user
|
||||
? ChatUserBubble(cupertino: cupertino, child: body)
|
||||
: _agentTurn(
|
||||
context,
|
||||
content,
|
||||
ChatAgentSurface(cupertino: cupertino, child: body),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -95,75 +82,6 @@ class ChatMessageView extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
Color _bubbleColor(
|
||||
BuildContext context,
|
||||
bool cupertino, {
|
||||
required bool user,
|
||||
}) {
|
||||
final accent = cupertino
|
||||
? CupertinoTheme.of(context).primaryColor
|
||||
: Theme.of(context).colorScheme.primary;
|
||||
final surface = cupertino
|
||||
? CupertinoColors.secondarySystemGroupedBackground.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.surfaceContainerLow;
|
||||
return user ? accent.withValues(alpha: 0.16) : surface;
|
||||
}
|
||||
|
||||
ShapeBorder _bubbleShape(
|
||||
BuildContext context,
|
||||
bool cupertino, {
|
||||
required bool user,
|
||||
}) {
|
||||
final accent = cupertino
|
||||
? CupertinoTheme.of(context).primaryColor
|
||||
: Theme.of(context).colorScheme.primary;
|
||||
final border = cupertino
|
||||
? CupertinoColors.separator.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.outlineVariant;
|
||||
return RoundedRectangleBorder(
|
||||
side: BorderSide(color: user ? accent.withValues(alpha: 0.4) : border),
|
||||
borderRadius: BorderRadius.only(
|
||||
topLeft: const Radius.circular(12),
|
||||
topRight: const Radius.circular(12),
|
||||
bottomLeft: Radius.circular(user ? 12 : 4),
|
||||
bottomRight: Radius.circular(user ? 4 : 12),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
BoxDecoration _bubbleDecoration(
|
||||
BuildContext context,
|
||||
bool cupertino, {
|
||||
required bool user,
|
||||
}) {
|
||||
final Color accent;
|
||||
final Color surface;
|
||||
final Color border;
|
||||
if (cupertino) {
|
||||
accent = CupertinoTheme.of(context).primaryColor;
|
||||
surface = CupertinoColors.secondarySystemGroupedBackground.resolveFrom(
|
||||
context,
|
||||
);
|
||||
border = CupertinoColors.separator.resolveFrom(context);
|
||||
} else {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
accent = scheme.primary;
|
||||
surface = scheme.surfaceContainerLow;
|
||||
border = scheme.outlineVariant;
|
||||
}
|
||||
final radius = BorderRadius.only(
|
||||
topLeft: const Radius.circular(12),
|
||||
topRight: const Radius.circular(12),
|
||||
bottomLeft: Radius.circular(user ? 12 : 4),
|
||||
bottomRight: Radius.circular(user ? 4 : 12),
|
||||
);
|
||||
return BoxDecoration(
|
||||
color: user ? accent.withValues(alpha: 0.16) : surface,
|
||||
border: Border.all(color: user ? accent.withValues(alpha: 0.4) : border),
|
||||
borderRadius: radius,
|
||||
);
|
||||
}
|
||||
|
||||
Color _secondaryColor(BuildContext context, bool cupertino) => cupertino
|
||||
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.onSurfaceVariant;
|
||||
@@ -198,7 +116,7 @@ class ChatMessageView extends StatelessWidget {
|
||||
|
||||
/// Stable activity precedes the answer; permission summaries and recorded
|
||||
/// model remain beneath it. Pending interaction cards are rendered separately.
|
||||
Widget _agentTurn(BuildContext context, List<Widget> content, Widget bubble) {
|
||||
Widget _agentTurn(BuildContext context, List<Widget> content, Widget answer) {
|
||||
final parts = _partsOf(row);
|
||||
final reasoning = [
|
||||
for (final part in parts)
|
||||
@@ -211,7 +129,10 @@ class ChatMessageView extends StatelessWidget {
|
||||
if (isChatToolPart(part)) part,
|
||||
];
|
||||
final model = '${row['model'] ?? ''}'.trim();
|
||||
final copyText = row['streaming'] == true ? '' : _answerText(parts);
|
||||
final footer = <Widget>[
|
||||
if (copyText.isNotEmpty)
|
||||
ChatCopyButton(text: copyText, cupertino: _isCupertino(context)),
|
||||
...this.footer,
|
||||
if (model.isNotEmpty)
|
||||
Text(
|
||||
@@ -241,9 +162,9 @@ class ChatMessageView extends StatelessWidget {
|
||||
],
|
||||
streaming: row['streaming'] == true,
|
||||
),
|
||||
// A little air between the one-line turn status and the answer bubble.
|
||||
// A little air between the one-line turn status and the answer.
|
||||
if (content.isNotEmpty) const SizedBox(height: 6),
|
||||
if (content.isNotEmpty) bubble,
|
||||
if (content.isNotEmpty) answer,
|
||||
if (footer.isNotEmpty)
|
||||
Wrap(
|
||||
key: const Key('chat-message-footer'),
|
||||
@@ -255,6 +176,16 @@ class ChatMessageView extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
/// The answer as the operator reads it: the turn's text parts in order,
|
||||
/// without reasoning, tools or attachments.
|
||||
String _answerText(List<JsonMap> parts) => [
|
||||
for (final part in parts)
|
||||
if ((part['type'] == null || part['type'] == 'text') &&
|
||||
part['text'] is String &&
|
||||
(part['text'] as String).trim().isNotEmpty)
|
||||
(part['text'] as String).trim(),
|
||||
].join('\n\n');
|
||||
|
||||
List<Widget> _agentParts(BuildContext context) {
|
||||
final widgets = <Widget>[];
|
||||
final parts = _partsOf(row);
|
||||
|
||||
Reference in New Issue
Block a user