feat(chat): fold answered permissions into a turn chip like tool calls
This commit is contained in:
@@ -81,6 +81,7 @@ String chatPermissionResourceText(JsonMap permission) {
|
||||
class ChatPermissionCardData {
|
||||
const ChatPermissionCardData({
|
||||
required this.requestId,
|
||||
required this.action,
|
||||
required this.resources,
|
||||
required this.resourceText,
|
||||
required this.risk,
|
||||
@@ -93,6 +94,9 @@ class ChatPermissionCardData {
|
||||
});
|
||||
|
||||
final String requestId;
|
||||
|
||||
/// The recorded action (the tool's name), blank when the request names none.
|
||||
final String action;
|
||||
final List<String> resources;
|
||||
final String resourceText;
|
||||
final String risk;
|
||||
@@ -151,6 +155,7 @@ ChatPermissionCardData? chatPermissionCard(
|
||||
};
|
||||
return ChatPermissionCardData(
|
||||
requestId: requestId,
|
||||
action: action,
|
||||
resources: chatPermissionResources(permission),
|
||||
resourceText: chatPermissionResourceText(permission),
|
||||
risk: chatPermissionRiskText(permission),
|
||||
|
||||
@@ -0,0 +1,213 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../../../ui/text/monospace.dart';
|
||||
import '../ui/sessions/chat_adaptive.dart';
|
||||
import '../ui/transcript/chat_turn_chip.dart';
|
||||
import '../ui/transcript/native_disclosure.dart';
|
||||
import 'chat_permission_card_model.dart';
|
||||
|
||||
/// The settled approval requests of [row], a permission card row; null while it
|
||||
/// still needs the operator or its last answer was refused.
|
||||
ChatPermissionCardData? _settled(JsonMap row) {
|
||||
if (row['kind'] != 'permission' || row['error'] != null) return null;
|
||||
final frame = row['frame'];
|
||||
if (frame is! Map) return null;
|
||||
final card = chatPermissionCard(
|
||||
Map<String, dynamic>.from(frame),
|
||||
'${row['answer'] ?? ''}',
|
||||
);
|
||||
return card == null || card.actionable ? null : card;
|
||||
}
|
||||
|
||||
/// Folds answered permission cards out of the card list, the way a turn's tool
|
||||
/// calls fold into one chip.
|
||||
///
|
||||
/// A settled request joins the agent turn that raised it, matched by the tool
|
||||
/// call id (Claude) or the message id (OpenCode) its frame recorded, as that
|
||||
/// row's `permissions`. Requests naming no turn in view (a Codex request, or a
|
||||
/// turn paged out) share one `permissions` row where the first of them stood.
|
||||
/// Open requests stay full cards with their decision controls.
|
||||
List<JsonMap> groupSettledPermissions(List<JsonMap> rows) {
|
||||
final result = <JsonMap>[];
|
||||
final attached = <int, List<ChatPermissionCardData>>{};
|
||||
final loose = <ChatPermissionCardData>[];
|
||||
int? looseAt;
|
||||
for (final row in rows) {
|
||||
final card = _settled(row);
|
||||
if (card == null) {
|
||||
result.add(row);
|
||||
continue;
|
||||
}
|
||||
final turn = _turnOf(
|
||||
result,
|
||||
Map<String, dynamic>.from(row['frame'] as Map),
|
||||
);
|
||||
if (turn != null) {
|
||||
attached.putIfAbsent(turn, () => []).add(card);
|
||||
} else {
|
||||
looseAt ??= result.length;
|
||||
loose.add(card);
|
||||
}
|
||||
}
|
||||
for (final entry in attached.entries) {
|
||||
result[entry.key] = {...result[entry.key], 'permissions': entry.value};
|
||||
}
|
||||
if (looseAt != null) {
|
||||
result.insert(looseAt, {
|
||||
'kind': 'permissions',
|
||||
'key': 'chat-permissions-${loose.first.requestId}',
|
||||
'permissions': loose,
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// The index of the assistant row in [rows] that raised [frame], if any.
|
||||
int? _turnOf(List<JsonMap> rows, JsonMap frame) {
|
||||
final source = frame['source'];
|
||||
if (source is! Map) return null;
|
||||
final call = '${source['id'] ?? ''}'.trim();
|
||||
final message = '${source['message_id'] ?? ''}'.trim();
|
||||
for (var index = rows.length - 1; index >= 0; index--) {
|
||||
final row = rows[index];
|
||||
if (row['kind'] != 'assistant') continue;
|
||||
final ids = row['messageIds'];
|
||||
if (message.isNotEmpty && ids is List && ids.contains(message)) {
|
||||
return index;
|
||||
}
|
||||
final parts = row['parts'];
|
||||
if (call.isEmpty || parts is! List) continue;
|
||||
for (final part in parts) {
|
||||
if (part is Map && part['type'] == 'tool' && '${part['id']}' == call) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// `1 permission` / `N permissions`.
|
||||
String chatPermissionsLabel(int count) =>
|
||||
count == 1 ? '1 permission' : '$count permissions';
|
||||
|
||||
/// A turn's answered approval requests as one chip beside its tools chip,
|
||||
/// tinted when one was rejected. It opens a sheet with one row per request.
|
||||
class ChatPermissionsChip extends StatelessWidget {
|
||||
const ChatPermissionsChip({super.key, required this.permissions});
|
||||
|
||||
final List<ChatPermissionCardData> permissions;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final rejected = permissions.any((p) => p.decision == chatDecisionReject);
|
||||
final color = rejected
|
||||
? (cupertino
|
||||
? CupertinoColors.systemRed.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.error)
|
||||
: (cupertino
|
||||
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.onSurfaceVariant);
|
||||
final label = chatPermissionsLabel(permissions.length);
|
||||
return ChatTurnChip(
|
||||
chipKey: const Key('chat-permission-chip'),
|
||||
leading: Icon(
|
||||
cupertino ? CupertinoIcons.checkmark_shield : Icons.shield_outlined,
|
||||
size: 14,
|
||||
color: color,
|
||||
),
|
||||
label: label,
|
||||
color: color,
|
||||
semanticsLabel: permissions.length == 1
|
||||
? '1 permission request, expand for details'
|
||||
: '${permissions.length} permission requests, expand for details',
|
||||
sheet: _PermissionsSheet(permissions: permissions),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _PermissionsSheet extends StatelessWidget {
|
||||
const _PermissionsSheet({required this.permissions});
|
||||
|
||||
final List<ChatPermissionCardData> permissions;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final style = cupertino
|
||||
? CupertinoTheme.of(context).textTheme.textStyle
|
||||
: Theme.of(context).textTheme.bodyMedium;
|
||||
return DefaultTextStyle.merge(
|
||||
style: style,
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
maxHeight: MediaQuery.sizeOf(context).height * 0.8,
|
||||
),
|
||||
child: ListView(
|
||||
key: const Key('chat-permission-sheet'),
|
||||
shrinkWrap: true,
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Text(
|
||||
chatPermissionsLabel(permissions.length),
|
||||
style: const TextStyle(fontWeight: FontWeight.w600),
|
||||
),
|
||||
),
|
||||
for (final permission in permissions)
|
||||
_PermissionRow(permission: permission),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// One request: its action and outcome; it opens to the request's summary,
|
||||
/// its full resources and the server's note.
|
||||
class _PermissionRow extends StatelessWidget {
|
||||
const _PermissionRow({required this.permission});
|
||||
|
||||
final ChatPermissionCardData permission;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final action = permission.action.isEmpty
|
||||
? chatApprovalBlankAction
|
||||
: permission.action;
|
||||
return NativeDisclosure(
|
||||
label: '$action · ${permission.headline}',
|
||||
childBuilder: (context) => Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// A resolution's note can be the same text as the summary.
|
||||
if (permission.risk != permission.statusLine)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Text(permission.risk),
|
||||
),
|
||||
if (permission.resources.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: SelectableText(
|
||||
permission.resources.join('\n'),
|
||||
style: const TextStyle(
|
||||
fontFamily: appMonospaceFamily,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Text(permission.statusLine),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,8 @@ import 'chat_models.dart';
|
||||
import 'chat_row_keys.dart';
|
||||
import 'chat_scroll.dart';
|
||||
import 'chat_thread_controller.dart';
|
||||
import 'chat_permission_card_model.dart';
|
||||
import 'chat_permission_group.dart';
|
||||
import 'chat_transcript_card.dart';
|
||||
|
||||
/// One read-only conversation: the controller's transcript in the order the
|
||||
@@ -587,11 +589,14 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
permissionErrorFor: _controller.permissionErrorFor,
|
||||
formErrorFor: _controller.questionErrorFor,
|
||||
);
|
||||
final allRows = transcriptRows(
|
||||
messages,
|
||||
trailing: cards,
|
||||
running: _controller.running && !_reconnecting && !_staleTranscript,
|
||||
liveMessageId: _controller.liveAssistantMessageId,
|
||||
// Answered approvals fold into their turn's chips, like tool calls.
|
||||
final allRows = groupSettledPermissions(
|
||||
transcriptRows(
|
||||
messages,
|
||||
trailing: cards,
|
||||
running: _controller.running && !_reconnecting && !_staleTranscript,
|
||||
liveMessageId: _controller.liveAssistantMessageId,
|
||||
),
|
||||
);
|
||||
// A transcript held from before a socket gap may still say a compaction is
|
||||
// running even when the repair's read is refused. That running state is
|
||||
@@ -922,7 +927,22 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
onQuestionAnswer: _controller.answerQuestion,
|
||||
);
|
||||
}
|
||||
return ChatMessageView(row: row, textBuilder: chatMarkdownText);
|
||||
final permissions = row['permissions'];
|
||||
final chips = [
|
||||
if (permissions is List<ChatPermissionCardData> && permissions.isNotEmpty)
|
||||
ChatPermissionsChip(permissions: permissions),
|
||||
];
|
||||
if (kind == 'permissions') {
|
||||
return Align(
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
child: chips.firstOrNull ?? const SizedBox.shrink(),
|
||||
);
|
||||
}
|
||||
return ChatMessageView(
|
||||
row: row,
|
||||
textBuilder: chatMarkdownText,
|
||||
footer: chips,
|
||||
);
|
||||
}
|
||||
|
||||
/// The header's compact Back control, matching the web's icon-only control.
|
||||
|
||||
@@ -16,11 +16,20 @@ 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});
|
||||
const ChatMessageView({
|
||||
super.key,
|
||||
required this.row,
|
||||
this.textBuilder,
|
||||
this.footer = const [],
|
||||
});
|
||||
|
||||
final JsonMap row;
|
||||
final ChatMessageTextBuilder? textBuilder;
|
||||
|
||||
/// More of an agent turn's summary chips, after its tools chip (the turn's
|
||||
/// settled permission requests).
|
||||
final List<Widget> footer;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final user = row['kind'] == 'user';
|
||||
@@ -205,6 +214,7 @@ class ChatMessageView extends StatelessWidget {
|
||||
final model = '${row['model'] ?? ''}'.trim();
|
||||
final footer = <Widget>[
|
||||
if (tools.isNotEmpty) ChatToolCallsChip(calls: tools),
|
||||
...this.footer,
|
||||
if (model.isNotEmpty)
|
||||
Text(
|
||||
model,
|
||||
|
||||
@@ -5,6 +5,7 @@ import '../../../../core/api_gateway.dart';
|
||||
import '../../../../ui/text/monospace.dart';
|
||||
import '../../domain/chat_tool_calls.dart';
|
||||
import '../sessions/chat_adaptive.dart';
|
||||
import 'chat_turn_chip.dart';
|
||||
import 'native_disclosure.dart';
|
||||
|
||||
/// A turn's one tool summary below its answer, as the web's chip: `N tools`,
|
||||
@@ -41,7 +42,7 @@ class ChatToolCallsChip extends StatelessWidget {
|
||||
size: 14,
|
||||
color: color,
|
||||
);
|
||||
return _TurnChip(
|
||||
return ChatTurnChip(
|
||||
chipKey: const Key('chat-tool-chip'),
|
||||
leading: leading,
|
||||
label: label,
|
||||
@@ -54,91 +55,6 @@ class ChatToolCallsChip extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
/// The quiet tool chip shell and its native sheet.
|
||||
class _TurnChip extends StatelessWidget {
|
||||
const _TurnChip({
|
||||
required this.chipKey,
|
||||
required this.leading,
|
||||
required this.label,
|
||||
required this.color,
|
||||
required this.semanticsLabel,
|
||||
required this.sheet,
|
||||
});
|
||||
|
||||
final Key chipKey;
|
||||
final Widget leading;
|
||||
final String label;
|
||||
final Color color;
|
||||
final String semanticsLabel;
|
||||
final Widget sheet;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final content = Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
leading,
|
||||
const SizedBox(width: 6),
|
||||
Text(label, style: TextStyle(color: color, fontSize: 13)),
|
||||
Icon(
|
||||
cupertino ? CupertinoIcons.chevron_up : Icons.expand_less,
|
||||
size: 14,
|
||||
color: color,
|
||||
),
|
||||
],
|
||||
);
|
||||
return Semantics(
|
||||
button: true,
|
||||
// The child's own action is excluded with its label, so the node
|
||||
// carries the activation for assistive technology.
|
||||
onTap: () => _open(context, cupertino),
|
||||
label: semanticsLabel,
|
||||
excludeSemantics: true,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 2),
|
||||
child: cupertino
|
||||
? CupertinoButton(
|
||||
key: chipKey,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 4),
|
||||
minimumSize: Size.zero,
|
||||
onPressed: () => _open(context, cupertino),
|
||||
child: content,
|
||||
)
|
||||
: InkWell(
|
||||
key: chipKey,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
onTap: () => _open(context, cupertino),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 4,
|
||||
vertical: 6,
|
||||
),
|
||||
child: content,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _open(BuildContext context, bool cupertino) {
|
||||
if (cupertino) {
|
||||
return showCupertinoModalPopup<void>(
|
||||
context: context,
|
||||
builder: (_) => CupertinoPopupSurface(child: SafeArea(child: sheet)),
|
||||
);
|
||||
}
|
||||
return showModalBottomSheet<void>(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
showDragHandle: true,
|
||||
useSafeArea: true,
|
||||
builder: (_) => sheet,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ToolCallsSheet extends StatelessWidget {
|
||||
const _ToolCallsSheet({required this.calls});
|
||||
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../sessions/chat_adaptive.dart';
|
||||
|
||||
/// The quiet chip under an agent turn (its tools, its permissions) and the
|
||||
/// native sheet it opens.
|
||||
class ChatTurnChip extends StatelessWidget {
|
||||
const ChatTurnChip({
|
||||
super.key,
|
||||
required this.chipKey,
|
||||
required this.leading,
|
||||
required this.label,
|
||||
required this.color,
|
||||
required this.semanticsLabel,
|
||||
required this.sheet,
|
||||
});
|
||||
|
||||
final Key chipKey;
|
||||
final Widget leading;
|
||||
final String label;
|
||||
final Color color;
|
||||
final String semanticsLabel;
|
||||
final Widget sheet;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final content = Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
leading,
|
||||
const SizedBox(width: 6),
|
||||
Text(label, style: TextStyle(color: color, fontSize: 13)),
|
||||
Icon(
|
||||
cupertino ? CupertinoIcons.chevron_up : Icons.expand_less,
|
||||
size: 14,
|
||||
color: color,
|
||||
),
|
||||
],
|
||||
);
|
||||
return Semantics(
|
||||
button: true,
|
||||
// The child's own action is excluded with its label, so the node
|
||||
// carries the activation for assistive technology.
|
||||
onTap: () => _open(context, cupertino),
|
||||
label: semanticsLabel,
|
||||
excludeSemantics: true,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 2),
|
||||
child: cupertino
|
||||
? CupertinoButton(
|
||||
key: chipKey,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 4),
|
||||
minimumSize: Size.zero,
|
||||
onPressed: () => _open(context, cupertino),
|
||||
child: content,
|
||||
)
|
||||
: InkWell(
|
||||
key: chipKey,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
onTap: () => _open(context, cupertino),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 4,
|
||||
vertical: 6,
|
||||
),
|
||||
child: content,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _open(BuildContext context, bool cupertino) {
|
||||
if (cupertino) {
|
||||
return showCupertinoModalPopup<void>(
|
||||
context: context,
|
||||
builder: (_) => CupertinoPopupSurface(child: SafeArea(child: sheet)),
|
||||
);
|
||||
}
|
||||
return showModalBottomSheet<void>(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
showDragHandle: true,
|
||||
useSafeArea: true,
|
||||
builder: (_) => sheet,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user