import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../../../ui/text/monospace.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.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 groupSettledPermissions(List rows) { final result = []; final attached = >{}; final loose = []; int? looseAt; for (final row in rows) { final card = _settled(row); if (card == null) { result.add(row); continue; } final turn = _turnOf( result, Map.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 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 permissions; @override Widget build(BuildContext context) { final rejected = permissions.any((p) => p.decision == chatDecisionReject); final color = rejected ? Theme.of(context).colorScheme.error : Theme.of(context).colorScheme.onSurfaceVariant; final label = chatPermissionsLabel(permissions.length); return ChatTurnChip( chipKey: const Key('chat-permission-chip'), leading: Icon(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 permissions; @override Widget build(BuildContext context) { final style = 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), ), ], ), ); } }