feat(app): native permission and question cards in the chat transcript

This commit is contained in:
2026-10-22 02:01:41 +01:00
parent 38488b7ec7
commit 221a2582dc
24 changed files with 3756 additions and 8 deletions
@@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
import '../../../core/api_gateway.dart';
import '../data/chat_repository.dart';
import '../domain/chat_transcript.dart';
import 'chat_thread_cards.dart';
/// One open conversation's transcript and live turn state.
///
@@ -15,6 +16,9 @@ import '../domain/chat_transcript.dart';
/// instead of merging stale state. The server detaches subscriptions when the
/// socket closes, so disposal only cancels the local stream (there is no
/// `chat_unsubscribe`).
///
/// Approval requests and question forms are recorded cards rather than
/// messages; [ChatThreadCards] owns their frames, answers and send gates.
class ChatThreadController extends ChangeNotifier {
ChatThreadController(this.gateway, this.sessionId) {
_subscription = gateway.frames.listen(_onFrame);
@@ -23,6 +27,12 @@ class ChatThreadController extends ChangeNotifier {
final ApiGateway gateway;
final String sessionId;
late final ChatRepository _repository = ChatRepository(gateway);
late final ChatThreadCards _cards = ChatThreadCards(
repository: _repository,
sessionId: sessionId,
notify: _notify,
isAlive: () => !_disposed,
);
StreamSubscription<JsonMap>? _subscription;
List<JsonMap> _messages = const [];
@@ -53,6 +63,30 @@ class ChatThreadController extends ChangeNotifier {
bool get subscribed => _subscribed;
String? get subscriptionError => _subscriptionError;
/// Every retained permission request, settled or still open, in server order.
List<JsonMap> get permissionFrames => _cards.permissions;
/// Every retained question form, including answered and expired ones.
List<JsonMap> get questionForms => _cards.forms;
String permissionAnswerFor(String requestId) => _cards.answerFor(requestId);
String? permissionErrorFor(String requestId) =>
_cards.permissionErrorFor(requestId);
String? questionErrorFor(String formId) => _cards.formErrorFor(formId);
/// Answer one pending approval through the card collaborator.
Future<void> replyPermission(String requestId, String decision) =>
_cards.replyPermission(requestId, decision);
/// Answer or close a pending question through the card collaborator.
Future<void> answerQuestion(
String formId,
Map<String, dynamic> answers,
String action,
) => _cards.answerQuestion(formId, answers, action);
/// Subscribes this session, then loads its newest history page.
Future<void> load() async {
if (_disposed) return;
@@ -174,12 +208,26 @@ class ChatThreadController extends ChangeNotifier {
void _onFrame(JsonMap envelope) {
if (_disposed) return;
if (envelope['type'] == 'chat_permission') {
final permission = envelope['chat_permission'];
if (permission is! Map) return;
final frame = Map<String, dynamic>.from(permission);
if ('${frame['session_id'] ?? ''}' != sessionId) return;
_cards.foldPermission(frame);
return;
}
final body = _eventBody(envelope);
if (body == null || body['session_id'] != sessionId) return;
if (body['kind'] == 'resync') {
unawaited(resync());
return;
}
if (body['kind'] == 'form') {
final form = body['form'];
if (form is! Map) return;
_cards.foldForm(Map<String, dynamic>.from(form));
return;
}
final current = _thread;
final next = applyChatEvent(current, body);
if (identical(next, current)) return;