feat(chat): per-chat permission modes for Claude, Codex and OpenCode
This commit is contained in:
@@ -0,0 +1,318 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../../../core/error_reason.dart';
|
||||
import '../data/chat_repository.dart';
|
||||
import '../ui/sessions/chat_adaptive.dart';
|
||||
|
||||
/// The conversation bar's permission-mode control.
|
||||
///
|
||||
/// It shows the mode the session row names (`mode_name`, the harness default
|
||||
/// when none was chosen) without a request of its own, since requests are
|
||||
/// serialized and must not delay the transcript. Tapping reads the harness's
|
||||
/// modes (`chat_modes`) into a native picker; choosing one sends `chat_mode`. A
|
||||
/// refusal keeps the picker open with the server's reason. A row without a
|
||||
/// mode (an older server or a harness without modes) shows no control.
|
||||
class ChatModeControl extends StatefulWidget {
|
||||
const ChatModeControl({
|
||||
super.key,
|
||||
required this.gateway,
|
||||
required this.sessionId,
|
||||
required this.mode,
|
||||
required this.modeName,
|
||||
this.onChanged,
|
||||
});
|
||||
|
||||
final ApiGateway gateway;
|
||||
final String sessionId;
|
||||
|
||||
/// The row's `mode` id and its display name.
|
||||
final String mode;
|
||||
final String modeName;
|
||||
|
||||
/// The accepted mode's id and name, so the opened row keeps up.
|
||||
final void Function(String mode, String name)? onChanged;
|
||||
|
||||
@override
|
||||
State<ChatModeControl> createState() => _ChatModeControlState();
|
||||
}
|
||||
|
||||
class _ChatModeControlState extends State<ChatModeControl> {
|
||||
late String _mode = widget.mode;
|
||||
late String _name = widget.modeName;
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant ChatModeControl oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.mode != widget.mode ||
|
||||
oldWidget.modeName != widget.modeName) {
|
||||
_mode = widget.mode;
|
||||
_name = widget.modeName;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _open() async {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final repository = ChatRepository(widget.gateway);
|
||||
final picker = _ChatModePicker(
|
||||
load: () => repository.modes(widget.sessionId),
|
||||
current: _mode,
|
||||
cupertino: cupertino,
|
||||
choose: (mode) async {
|
||||
final applied = await repository.selectMode(widget.sessionId, mode.id);
|
||||
final name = applied == mode.id ? mode.name : applied;
|
||||
widget.onChanged?.call(applied, name);
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_mode = applied;
|
||||
_name = name;
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
if (cupertino) {
|
||||
await showCupertinoModalPopup<void>(
|
||||
context: context,
|
||||
builder: (_) => picker,
|
||||
);
|
||||
return;
|
||||
}
|
||||
await showModalBottomSheet<void>(
|
||||
context: context,
|
||||
showDragHandle: true,
|
||||
isScrollControlled: true,
|
||||
useSafeArea: true,
|
||||
builder: (_) => picker,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_mode.isEmpty || _name.isEmpty) return const SizedBox.shrink();
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final label = _name;
|
||||
final content = Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
cupertino
|
||||
? CupertinoIcons.shield_lefthalf_fill
|
||||
: Icons.shield_outlined,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Flexible(child: Text(label, overflow: TextOverflow.ellipsis)),
|
||||
],
|
||||
);
|
||||
const key = Key('chat-mode');
|
||||
if (cupertino) {
|
||||
return Semantics(
|
||||
container: true,
|
||||
button: true,
|
||||
label: 'Change mode, currently $label',
|
||||
onTap: _open,
|
||||
child: ExcludeSemantics(
|
||||
child: CupertinoButton(
|
||||
key: key,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
onPressed: _open,
|
||||
child: content,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return Tooltip(
|
||||
message: 'Change mode (currently $label)',
|
||||
child: TextButton(key: key, onPressed: _open, child: content),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// One mode the harness offers.
|
||||
typedef _Mode = ({String id, String name, String description});
|
||||
|
||||
/// The native mode list: each mode's name and description, the current one
|
||||
/// checked. It reads the list when opened and closes once the server accepts
|
||||
/// a choice.
|
||||
class _ChatModePicker extends StatefulWidget {
|
||||
const _ChatModePicker({
|
||||
required this.load,
|
||||
required this.current,
|
||||
required this.cupertino,
|
||||
required this.choose,
|
||||
});
|
||||
|
||||
final Future<({List<JsonMap> modes, String current})> Function() load;
|
||||
final String current;
|
||||
final bool cupertino;
|
||||
final Future<void> Function(_Mode mode) choose;
|
||||
|
||||
@override
|
||||
State<_ChatModePicker> createState() => _ChatModePickerState();
|
||||
}
|
||||
|
||||
class _ChatModePickerState extends State<_ChatModePicker> {
|
||||
List<_Mode>? _modes;
|
||||
late String _current = widget.current;
|
||||
bool _busy = false;
|
||||
String? _error;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_read();
|
||||
}
|
||||
|
||||
Future<void> _read() async {
|
||||
try {
|
||||
final reply = await widget.load();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_modes = [
|
||||
for (final mode in reply.modes)
|
||||
if ('${mode['id'] ?? ''}'.trim().isNotEmpty)
|
||||
(
|
||||
id: '${mode['id']}'.trim(),
|
||||
name: '${mode['name'] ?? mode['id']}'.trim(),
|
||||
description: '${mode['description'] ?? ''}'.trim(),
|
||||
),
|
||||
];
|
||||
if (reply.current.isNotEmpty) _current = reply.current;
|
||||
});
|
||||
} catch (error) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_modes = const [];
|
||||
_error = describeErrorReason(
|
||||
error,
|
||||
fallback: 'The modes could not be loaded.',
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _choose(_Mode mode) async {
|
||||
if (_busy) return;
|
||||
if (mode.id == _current) {
|
||||
Navigator.of(context).pop();
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_busy = true;
|
||||
_error = null;
|
||||
});
|
||||
try {
|
||||
await widget.choose(mode);
|
||||
if (mounted) Navigator.of(context).pop();
|
||||
} catch (error) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_busy = false;
|
||||
_error = describeErrorReason(
|
||||
error,
|
||||
fallback: 'The mode was not changed.',
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
widget.cupertino ? _cupertinoSheet(context) : _materialSheet(context);
|
||||
|
||||
Widget _materialSheet(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final modes = _modes;
|
||||
return SafeArea(
|
||||
child: ListView(
|
||||
key: const Key('chat-mode-sheet'),
|
||||
shrinkWrap: true,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
|
||||
child: Text('Mode', style: theme.textTheme.titleMedium),
|
||||
),
|
||||
if (_error != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
|
||||
child: Text(
|
||||
_error!,
|
||||
style: TextStyle(color: theme.colorScheme.error),
|
||||
),
|
||||
),
|
||||
if (modes == null)
|
||||
const Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Center(child: CircularProgressIndicator()),
|
||||
)
|
||||
else
|
||||
for (final mode in modes)
|
||||
ListTile(
|
||||
key: ValueKey<String>('chat-mode-option-${mode.id}'),
|
||||
enabled: !_busy,
|
||||
title: Text(mode.name),
|
||||
subtitle: mode.description.isEmpty
|
||||
? null
|
||||
: Text(mode.description),
|
||||
trailing: mode.id == _current
|
||||
? Icon(Icons.check, color: theme.colorScheme.primary)
|
||||
: null,
|
||||
onTap: () => _choose(mode),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _cupertinoSheet(BuildContext context) {
|
||||
final secondary = CupertinoColors.secondaryLabel.resolveFrom(context);
|
||||
final modes = _modes;
|
||||
return CupertinoActionSheet(
|
||||
key: const Key('chat-mode-sheet'),
|
||||
title: const Text('Mode'),
|
||||
message: _error != null
|
||||
? Text(
|
||||
_error!,
|
||||
style: TextStyle(
|
||||
color: CupertinoColors.systemRed.resolveFrom(context),
|
||||
),
|
||||
)
|
||||
: modes == null
|
||||
? const CupertinoActivityIndicator()
|
||||
: null,
|
||||
actions: [
|
||||
for (final mode in modes ?? const <_Mode>[])
|
||||
CupertinoActionSheetAction(
|
||||
key: ValueKey<String>('chat-mode-option-${mode.id}'),
|
||||
onPressed: () => _choose(mode),
|
||||
child: Column(
|
||||
children: [
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Flexible(child: Text(mode.name)),
|
||||
if (mode.id == _current) ...[
|
||||
const SizedBox(width: 6),
|
||||
const Icon(CupertinoIcons.checkmark_alt),
|
||||
],
|
||||
],
|
||||
),
|
||||
if (mode.description.isNotEmpty)
|
||||
Text(
|
||||
mode.description,
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 13, color: secondary),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
cancelButton: CupertinoActionSheetAction(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import 'chat_command_actions.dart';
|
||||
import 'chat_composer_bar.dart';
|
||||
import 'chat_composer_controller.dart';
|
||||
import 'chat_draft_store.dart';
|
||||
import 'chat_mode_control.dart';
|
||||
import 'chat_model_control.dart';
|
||||
import 'chat_models.dart';
|
||||
import 'chat_row_keys.dart';
|
||||
@@ -48,6 +49,7 @@ class ChatThreadView extends StatefulWidget {
|
||||
this.title = '',
|
||||
this.session,
|
||||
this.onModelChanged,
|
||||
this.onModeChanged,
|
||||
this.syncToken = 0,
|
||||
this.drafts,
|
||||
this.newOperationId,
|
||||
@@ -90,6 +92,9 @@ class ChatThreadView extends StatefulWidget {
|
||||
/// keep the row it opened current for the next visit.
|
||||
final ValueChanged<ChatModelSelection>? onModelChanged;
|
||||
|
||||
/// The accepted permission mode's id and name.
|
||||
final void Function(String mode, String name)? onModeChanged;
|
||||
|
||||
/// Advances when the shared socket is replaced, keeping this selection.
|
||||
final int syncToken;
|
||||
|
||||
@@ -701,6 +706,12 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
answeredBy: latestReplyModel(_controller.messages),
|
||||
),
|
||||
),
|
||||
ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
maxWidth: constraints.maxWidth * 0.3,
|
||||
),
|
||||
child: _modeControl(),
|
||||
),
|
||||
ChatBarAction(
|
||||
key: const Key('chat-info'),
|
||||
label: 'Chat information',
|
||||
@@ -864,6 +875,10 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
answeredBy: latestReplyModel(_controller.messages),
|
||||
),
|
||||
),
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 160),
|
||||
child: _modeControl(),
|
||||
),
|
||||
ChatBarAction(
|
||||
key: const Key('chat-info'),
|
||||
label: 'Chat information',
|
||||
@@ -877,6 +892,17 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
},
|
||||
);
|
||||
|
||||
/// The session's permission mode, keyed per session so another chat
|
||||
/// re-reads its own modes.
|
||||
Widget _modeControl() => ChatModeControl(
|
||||
key: ValueKey<String>('chat-mode-${widget.sessionId}'),
|
||||
gateway: widget.gateway,
|
||||
sessionId: widget.sessionId,
|
||||
mode: '${widget.session?['mode'] ?? ''}'.trim(),
|
||||
modeName: '${widget.session?['mode_name'] ?? ''}'.trim(),
|
||||
onChanged: widget.onModeChanged,
|
||||
);
|
||||
|
||||
ChatNotice? _noticeOf(List<ChatNotice> notices, ChatNoticeKind kind) {
|
||||
for (final notice in notices) {
|
||||
if (notice.kind == kind) return notice;
|
||||
|
||||
@@ -49,6 +49,40 @@ class ChatRepository {
|
||||
});
|
||||
}
|
||||
|
||||
/// The permission modes the session's harness offers, in the server's
|
||||
/// order, and the session's stored mode (empty for the harness default).
|
||||
Future<({List<JsonMap> modes, String current})> modes(
|
||||
String sessionId,
|
||||
) async {
|
||||
final session = sessionId.trim();
|
||||
if (session.isEmpty) {
|
||||
throw ArgumentError('Select a chat session first.');
|
||||
}
|
||||
final reply = await gateway.call('chat_modes', {
|
||||
'chat_query': {'session_id': session},
|
||||
});
|
||||
final value = reply['chat_modes'];
|
||||
final current = reply['chat_mode'];
|
||||
return (
|
||||
modes: value is List ? _maps(value) : const <JsonMap>[],
|
||||
current: current is Map ? '${current['mode'] ?? ''}'.trim() : '',
|
||||
);
|
||||
}
|
||||
|
||||
/// Switches the session's mode; the reply echoes the stored mode.
|
||||
Future<String> selectMode(String sessionId, String mode) async {
|
||||
final session = sessionId.trim();
|
||||
if (session.isEmpty) {
|
||||
throw ArgumentError('Select a chat session first.');
|
||||
}
|
||||
if (mode.trim().isEmpty) throw ArgumentError('Choose a mode first.');
|
||||
final reply = await gateway.call('chat_mode', {
|
||||
'chat_mode': {'session_id': session, 'mode': mode.trim()},
|
||||
});
|
||||
final echoed = reply['chat_mode'];
|
||||
return echoed is Map ? '${echoed['mode'] ?? ''}'.trim() : mode.trim();
|
||||
}
|
||||
|
||||
Future<List<JsonMap>> shells(String sessionId) async => _maps(
|
||||
(await gateway.call('chat_shells', {
|
||||
'chat_query': {'session_id': sessionId},
|
||||
|
||||
@@ -145,6 +145,16 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
setState(() => _acceptedModels[id] = applied);
|
||||
}
|
||||
|
||||
/// An accepted mode switch updates the opened row, so reopening the chat in
|
||||
/// this visit shows it before the list re-reads.
|
||||
void _modeChanged(String id, String mode, String name) {
|
||||
final row = _openedSessions[id];
|
||||
if (!mounted || row == null) return;
|
||||
setState(
|
||||
() => _openedSessions[id] = {...row, 'mode': mode, 'mode_name': name},
|
||||
);
|
||||
}
|
||||
|
||||
/// The row to open for [id], with an accepted model folded in.
|
||||
JsonMap? _rowFor(String id) {
|
||||
final row = _openedSessions[id];
|
||||
@@ -194,6 +204,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
: null,
|
||||
onBack: onBack,
|
||||
onModelChanged: (applied) => _modelChanged(selected, applied),
|
||||
onModeChanged: (mode, name) => _modeChanged(selected, mode, name),
|
||||
onCreatedSession: _openCreated,
|
||||
onSessionRenamed: (title) => _renamed(selected, title),
|
||||
onSessionClosed: _closed,
|
||||
|
||||
Reference in New Issue
Block a user