Merge branch 'main' into flutter/chat-markdown
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@@ -16,10 +18,19 @@ class ChatSessionsScreen extends StatefulWidget {
|
||||
super.key,
|
||||
required this.gateway,
|
||||
required this.onOpen,
|
||||
this.embedded = false,
|
||||
this.syncToken = 0,
|
||||
});
|
||||
final ApiGateway gateway;
|
||||
final void Function(JsonMap session) onOpen;
|
||||
|
||||
/// True under the application shell, which owns the single navigation bar.
|
||||
final bool embedded;
|
||||
|
||||
/// Advances when the shared socket is replaced; sessions created, renamed or
|
||||
/// closed elsewhere during the gap must not stay invisible until Refresh.
|
||||
final int syncToken;
|
||||
|
||||
@override
|
||||
State<ChatSessionsScreen> createState() => _ChatSessionsScreenState();
|
||||
}
|
||||
@@ -35,6 +46,8 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.gateway != widget.gateway) {
|
||||
_controller.replaceRepository(ChatRepository(widget.gateway));
|
||||
} else if (oldWidget.syncToken != widget.syncToken) {
|
||||
unawaited(_controller.refresh());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,53 +128,38 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
],
|
||||
);
|
||||
|
||||
if (widget.embedded) {
|
||||
return Column(
|
||||
children: [
|
||||
Align(
|
||||
alignment: AlignmentDirectional.centerEnd,
|
||||
child: Padding(
|
||||
padding: const EdgeInsetsDirectional.only(end: 8),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: _actions(),
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(child: body),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
||||
return CupertinoPageScaffold(
|
||||
navigationBar: CupertinoNavigationBar(
|
||||
middle: const Text('Chats'),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ChatBarAction(
|
||||
key: const Key('chat-new-chat'),
|
||||
label: 'New chat',
|
||||
onPressed: _newChat,
|
||||
materialIcon: Icons.add,
|
||||
cupertinoIcon: CupertinoIcons.add,
|
||||
),
|
||||
ChatBarAction(
|
||||
key: const Key('chat-refresh'),
|
||||
label: 'Refresh',
|
||||
onPressed: _controller.refresh,
|
||||
materialIcon: Icons.refresh,
|
||||
cupertinoIcon: CupertinoIcons.refresh,
|
||||
),
|
||||
],
|
||||
children: _actions(),
|
||||
),
|
||||
),
|
||||
child: SafeArea(child: body),
|
||||
);
|
||||
}
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Chats'),
|
||||
actions: [
|
||||
ChatBarAction(
|
||||
key: const Key('chat-new-chat'),
|
||||
label: 'New chat',
|
||||
onPressed: _newChat,
|
||||
materialIcon: Icons.add,
|
||||
cupertinoIcon: CupertinoIcons.add,
|
||||
),
|
||||
ChatBarAction(
|
||||
key: const Key('chat-refresh'),
|
||||
label: 'Refresh',
|
||||
onPressed: _controller.refresh,
|
||||
materialIcon: Icons.refresh,
|
||||
cupertinoIcon: CupertinoIcons.refresh,
|
||||
),
|
||||
],
|
||||
),
|
||||
appBar: AppBar(title: const Text('Chats'), actions: _actions()),
|
||||
body: body,
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
key: const Key('chat-new-chat-fab'),
|
||||
@@ -173,4 +171,21 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
List<Widget> _actions() => [
|
||||
ChatBarAction(
|
||||
key: const Key('chat-new-chat'),
|
||||
label: 'New chat',
|
||||
onPressed: _newChat,
|
||||
materialIcon: Icons.add,
|
||||
cupertinoIcon: CupertinoIcons.add,
|
||||
),
|
||||
ChatBarAction(
|
||||
key: const Key('chat-refresh'),
|
||||
label: 'Refresh',
|
||||
onPressed: _controller.refresh,
|
||||
materialIcon: Icons.refresh,
|
||||
cupertinoIcon: CupertinoIcons.refresh,
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -92,13 +92,34 @@ class ChatMessageView extends StatelessWidget {
|
||||
|
||||
List<Widget> _agentParts(BuildContext context) {
|
||||
final widgets = <Widget>[];
|
||||
for (final part in _partsOf(row)) {
|
||||
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) widgets.add(widget);
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user