Files
pipelines/app/lib/features/chat/conversation/chat_question_card.dart
T
alan bfccf39671 Answer Claude questions in your own words, or dismiss them
Claude's question forms now accept a free-text answer beside the
offered options and can be dismissed. A dismissal tells Claude the
operator will reply in the chat instead, the card's button reads
Dismiss, and the composer takes the keyboard so the reply can be typed
at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 00:02:59 +00:00

211 lines
6.5 KiB
Dart

import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import 'chat_card_styles.dart';
import 'chat_question_card_controls.dart';
import 'chat_question_card_fields.dart';
import 'chat_question_card_model.dart';
import 'chat_question_recorded_answers.dart';
import 'chat_question_validation.dart';
import 'composer_focus_request.dart';
/// One recorded agent question as a native card.
///
/// A pending question keeps the operator's choices locally until Send answer;
/// the answer map is field-keyed, exactly as `chat_answer` expects. Resolved
/// and expired questions show the recorded answers read-only.
class ChatQuestionCard extends StatefulWidget {
const ChatQuestionCard({
super.key,
required this.form,
required this.onSubmit,
this.error,
});
final JsonMap form;
final Future<void> Function(Map<String, dynamic> answers, String action)
onSubmit;
final String? error;
@override
State<ChatQuestionCard> createState() => _ChatQuestionCardState();
}
class _ChatQuestionCardState extends State<ChatQuestionCard> {
late final List<JsonMap> _fields = chatFormFields(widget.form);
late final Map<String, dynamic> _answers = _initialAnswers();
final Map<String, TextEditingController> _controllers = {};
String _localError = '';
bool _sending = false;
bool get _pending => chatFormPending(widget.form);
Map<String, dynamic> _initialAnswers() {
final recorded = widget.form['answers'];
final answers = recorded is Map
? Map<String, dynamic>.from(recorded)
: <String, dynamic>{};
for (final field in _fields) {
final key = '${field['key'] ?? ''}'.trim();
if (key.isEmpty || answers.containsKey(key)) continue;
if (field['default'] != null) answers[key] = field['default'];
}
return answers;
}
@override
void initState() {
super.initState();
for (final field in _fields) {
if (!chatQuestionFieldNeedsInput(field)) continue;
final key = '${field['key'] ?? ''}'.trim();
if (key.isEmpty) continue;
final value = _answers[key];
final options = chatQuestionOptions(
field,
).map(chatQuestionOptionValue).toSet();
final custom = value is List
? value.where((entry) => !options.contains('$entry')).join(', ')
: (value is String && !options.contains(value) ? value : '');
final controller = TextEditingController(text: custom);
_controllers[key] = controller;
}
}
@override
void dispose() {
for (final controller in _controllers.values) {
controller.dispose();
}
super.dispose();
}
void _changed(String key, Object? value) {
setState(() {
if (value == null) {
_answers.remove(key);
} else {
_answers[key] = value;
}
_localError = '';
});
}
Future<void> _submit() async {
if (_sending || !_pending) return;
final reason = chatFormAnswerError(_fields, _answers);
if (reason.isNotEmpty) {
setState(() => _localError = reason);
return;
}
setState(() {
_sending = true;
_localError = '';
});
final accept =
widget.form['actions'] is List &&
(widget.form['actions'] as List).contains('accept');
await widget.onSubmit(
chatVisibleFormAnswers(_fields, _answers),
accept ? 'accept' : '',
);
if (mounted) setState(() => _sending = false);
}
Future<void> _action(String action) async {
if (_sending || !_pending) return;
setState(() {
_sending = true;
_localError = '';
});
// Read before the await: the card leaves the tree once answered.
final composer = ChatComposerFocusScope.maybeOf(context);
await widget.onSubmit(const {}, action);
// A dismissed question is answered in the chat: the composer takes the
// keyboard so the reply can be typed straight away.
if (action == 'decline' && widget.form['kind'] == 'question') {
composer?.requests.request(composer.sessionId);
}
if (mounted) setState(() => _sending = false);
}
@override
Widget build(BuildContext context) => Card(
// Keep consecutive cards visually separate; the transcript row only
// supplies the horizontal inset.
margin: const EdgeInsets.only(bottom: 12),
child: Padding(padding: const EdgeInsets.all(16), child: _body(context)),
);
Widget _body(BuildContext context) {
final actions = widget.form['actions'];
final error = widget.error?.trim().isNotEmpty == true
? widget.error!.trim()
: _localError;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'${widget.form['title'] ?? ''}'.trim().isEmpty
? 'Question'
: '${widget.form['title']}'.trim(),
style: chatCardTitleStyle(context),
),
Text(_statusLine(), style: chatCardSecondaryStyle(context)),
if (error.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Semantics(
liveRegion: true,
child: Text(
error,
style: chatCardBodyStyle(
context,
)?.copyWith(color: chatCardErrorColor(context)),
),
),
),
if (_pending)
...chatQuestionFieldWidgets(
context: context,
fields: _fields,
answers: _answers,
controllers: _controllers,
onChanged: _changed,
)
else
...chatQuestionRecordedAnswers(context: context, form: widget.form),
if (_pending) ...[
const SizedBox(height: 12),
chatQuestionControls(
context: context,
actions: actions,
submitLabel: _submitLabel,
sending: _sending,
onSubmit: _submit,
onAction: _action,
// A dismissed question tells the agent the operator will reply in
// the chat instead.
declineLabel: widget.form['kind'] == 'question'
? 'Dismiss'
: 'Decline',
),
],
],
);
}
String _statusLine() {
if (_pending) return chatQuestionWaiting;
return chatQuestionSettledStatus(widget.form);
}
String get _submitLabel {
final allExternal =
_fields.isNotEmpty &&
_fields.every((field) => '${field['type'] ?? ''}' == 'external');
return allExternal ? chatQuestionConfirm : chatQuestionSubmit;
}
}