027c2f800e
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.
299 lines
11 KiB
Dart
299 lines
11 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
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';
|
|
|
|
/// Builds an assistant text part; the default keeps the server's text exactly.
|
|
typedef ChatMessageTextBuilder = Widget Function(BuildContext, String);
|
|
|
|
/// One `transcriptRows` row: a user message or one folded agent turn. Parts keep
|
|
/// order for agent turns; native disclosures retain unknown parts as raw JSON.
|
|
class ChatMessageView extends StatelessWidget {
|
|
const ChatMessageView({
|
|
super.key,
|
|
required this.row,
|
|
this.textBuilder,
|
|
this.footer = const [],
|
|
});
|
|
|
|
final JsonMap row;
|
|
final ChatMessageTextBuilder? textBuilder;
|
|
|
|
/// The turn's settled permissions, independent of passive activity details.
|
|
final List<Widget> footer;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final user = row['kind'] == 'user';
|
|
final role = user ? 'User' : 'Assistant';
|
|
final cupertino = _isCupertino(context);
|
|
final content = user ? _userParts(context) : _agentParts(context);
|
|
// 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,
|
|
),
|
|
);
|
|
return Semantics(
|
|
container: true,
|
|
explicitChildNodes: true,
|
|
label: '$role message',
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 6),
|
|
child: LayoutBuilder(
|
|
builder: (context, constraints) => Align(
|
|
alignment: user
|
|
? AlignmentDirectional.centerEnd
|
|
: AlignmentDirectional.centerStart,
|
|
child: ConstrainedBox(
|
|
// The operator's turn leaves a margin on a phone, so the two
|
|
// 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()
|
|
: 720,
|
|
),
|
|
child: user
|
|
? ChatUserBubble(cupertino: cupertino, child: body)
|
|
: _agentTurn(
|
|
context,
|
|
content,
|
|
ChatAgentSurface(cupertino: cupertino, child: body),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Color _secondaryColor(BuildContext context, bool cupertino) => cupertino
|
|
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
|
: Theme.of(context).colorScheme.onSurfaceVariant;
|
|
|
|
bool _isCupertino(BuildContext context) =>
|
|
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
|
|
|
List<Widget> _userParts(BuildContext context) {
|
|
final message = row['message'] is Map
|
|
? Map<String, dynamic>.from(row['message'] as Map)
|
|
: row;
|
|
final parts = _partsOf(message);
|
|
final text = parts
|
|
.where((part) => part['type'] == null || part['type'] == 'text')
|
|
.map((part) => '${part['text'] ?? ''}')
|
|
.join('\n');
|
|
final widgets = <Widget>[
|
|
if (text.trim().isNotEmpty) SelectableText(text),
|
|
for (final part in parts.where(_isFile)) _attachment(context, part),
|
|
for (final part in parts)
|
|
if (part['type'] != 'text' &&
|
|
!_isFile(part) &&
|
|
!(part['type'] == null && part['text'] is String))
|
|
_unrecognised(part),
|
|
];
|
|
final client = '${message['client'] ?? ''}'.trim();
|
|
if (client.isNotEmpty) {
|
|
widgets.insert(0, Text(client, style: _clientStyle(context)));
|
|
}
|
|
return widgets;
|
|
}
|
|
|
|
/// 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 answer) {
|
|
final parts = _partsOf(row);
|
|
final reasoning = [
|
|
for (final part in parts)
|
|
if (part['type'] == 'reasoning' &&
|
|
'${part['text'] ?? ''}'.trim().isNotEmpty)
|
|
'${part['text']}',
|
|
];
|
|
final tools = [
|
|
for (final part in parts)
|
|
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(
|
|
model,
|
|
key: const Key('chat-reply-model'),
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: _secondaryColor(context, _isCupertino(context)),
|
|
),
|
|
),
|
|
];
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
if (row['streaming'] == true ||
|
|
reasoning.isNotEmpty ||
|
|
tools.isNotEmpty ||
|
|
(row['activityRecords'] as List? ?? const []).isNotEmpty)
|
|
ChatTurnActivityView(
|
|
key: const Key('chat-stable-turn-activity'),
|
|
parts: [
|
|
...parts,
|
|
...List<JsonMap>.from(
|
|
row['activityRecords'] as List? ?? const [],
|
|
),
|
|
],
|
|
streaming: row['streaming'] == true,
|
|
),
|
|
// A little air between the one-line turn status and the answer.
|
|
if (content.isNotEmpty) const SizedBox(height: 6),
|
|
if (content.isNotEmpty) answer,
|
|
if (footer.isNotEmpty)
|
|
Wrap(
|
|
key: const Key('chat-message-footer'),
|
|
spacing: 8,
|
|
crossAxisAlignment: WrapCrossAlignment.center,
|
|
children: footer,
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
/// 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);
|
|
final seen = <String, int>{};
|
|
for (var index = 0; index < parts.length; index++) {
|
|
final part = parts[index];
|
|
final widget = _agentPart(context, part);
|
|
if (widget == null) continue;
|
|
// 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
|
|
// part with no server id still falls back to its position.
|
|
widgets.add(
|
|
KeyedSubtree(key: _partKey(part, index, seen), child: widget),
|
|
);
|
|
}
|
|
return widgets;
|
|
}
|
|
|
|
/// The server's part id when there is one, else this part's position. A
|
|
/// repeated identity gets an ordinal so two sibling keys never collide.
|
|
Key _partKey(JsonMap part, int index, Map<String, int> seen) {
|
|
final type = '${part['type'] ?? ''}';
|
|
final id = '${part['id'] ?? ''}'.trim();
|
|
final base = id.isEmpty ? 'index-$index' : '$type:$id';
|
|
final ordinal = (seen[base] ?? 0) + 1;
|
|
seen[base] = ordinal;
|
|
return ValueKey<String>('chat-part-$base-$ordinal');
|
|
}
|
|
|
|
/// One agent part, or null when a known part has nothing to draw. The web's
|
|
/// `partRendering` hides blank text/reasoning too, so a blank known part must
|
|
/// not fall through to the unknown-part disclosure.
|
|
Widget? _agentPart(BuildContext context, JsonMap part) {
|
|
final type = '${part['type'] ?? ''}';
|
|
final text = part['text'] is String ? part['text'] as String : '';
|
|
final hasText = text.trim().isNotEmpty;
|
|
if (type.isEmpty || type == 'text') {
|
|
if (!hasText) return type.isEmpty ? _unrecognised(part) : null;
|
|
return textBuilder?.call(context, text) ?? SelectableText(text);
|
|
}
|
|
// Reasoning and tools are retained in the turn's activity disclosure.
|
|
if (type == 'reasoning' || type == 'tool') return null;
|
|
if (type == 'file') {
|
|
if (_isFile(part)) return _attachment(context, part);
|
|
return hasText ? SelectableText(text) : null;
|
|
}
|
|
if (hasText) return SelectableText(text);
|
|
return _unrecognised(part);
|
|
}
|
|
|
|
Widget _unrecognised(JsonMap part) {
|
|
final type = '${part['type'] ?? ''}';
|
|
return NativeDisclosure(
|
|
label: 'Unrecognised part${type.isEmpty ? '' : ': $type'}',
|
|
childBuilder: (context) => _raw(part),
|
|
);
|
|
}
|
|
|
|
TextStyle? _clientStyle(BuildContext context) {
|
|
final cupertino = _isCupertino(context);
|
|
return (cupertino
|
|
? CupertinoTheme.of(context).textTheme.textStyle
|
|
: Theme.of(context).textTheme.bodySmall)
|
|
?.copyWith(color: _secondaryColor(context, cupertino));
|
|
}
|
|
|
|
Widget _attachment(BuildContext context, JsonMap part) {
|
|
final cupertino = _isCupertino(context);
|
|
return Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 8),
|
|
child: Icon(cupertino ? CupertinoIcons.paperclip : Icons.attach_file),
|
|
),
|
|
Expanded(
|
|
child: SelectableText(
|
|
_attachmentDisplayName(part['name'], part['uri']),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
/// Pretty-printed on demand: a closed disclosure never pays for it.
|
|
Widget _raw(JsonMap part) =>
|
|
SelectableText(const JsonEncoder.withIndent(' ').convert(part));
|
|
}
|
|
|
|
List<JsonMap> _partsOf(Map<dynamic, dynamic> source) {
|
|
final parts = source['parts'];
|
|
if (parts is! List) return const [];
|
|
return parts
|
|
.whereType<Map>()
|
|
.map((part) => Map<String, dynamic>.from(part))
|
|
.toList();
|
|
}
|
|
|
|
bool _isFile(JsonMap part) =>
|
|
part['type'] == 'file' &&
|
|
'${part['name'] ?? ''}${part['uri'] ?? ''}'.trim().isNotEmpty;
|
|
|
|
/// The web's attachment name rule: its own name, else the URI basename, else
|
|
/// `file`, so an attachment never shows an empty label.
|
|
String _attachmentDisplayName(Object? name, Object? uri) {
|
|
final shown = '${name ?? ''}'.trim();
|
|
final basename = '${uri ?? ''}'.split('/').last.trim();
|
|
return shown.isNotEmpty ? shown : (basename.isEmpty ? 'file' : basename);
|
|
}
|