Chat: answered questions collapse into their turn like tool calls
This commit is contained in:
@@ -215,11 +215,7 @@ class _ChatQuestionCardState extends State<ChatQuestionCard> {
|
||||
|
||||
String _statusLine() {
|
||||
if (_pending) return chatQuestionWaiting;
|
||||
if (widget.form['cancelled'] == true) return chatQuestionExpired;
|
||||
final answers = widget.form['answers'];
|
||||
return answers is Map && answers.isNotEmpty
|
||||
? chatQuestionAnswered
|
||||
: chatQuestionClosed;
|
||||
return chatQuestionSettledStatus(widget.form);
|
||||
}
|
||||
|
||||
String get _submitLabel {
|
||||
|
||||
@@ -132,6 +132,15 @@ String chatQuestionOptionLabel(JsonMap option) {
|
||||
bool chatFormPending(JsonMap form) =>
|
||||
form['resolved'] != true && form['cancelled'] != true;
|
||||
|
||||
/// How a settled form ended: expired, answered, or closed without an answer.
|
||||
String chatQuestionSettledStatus(JsonMap form) {
|
||||
if (form['cancelled'] == true) return chatQuestionExpired;
|
||||
final answers = form['answers'];
|
||||
return answers is Map && answers.isNotEmpty
|
||||
? chatQuestionAnswered
|
||||
: chatQuestionClosed;
|
||||
}
|
||||
|
||||
/// True when an external form URL is one this app can safely show. Mirrors the
|
||||
/// web's `safeFormURL`: an absolute HTTP(S) URL with a real host and no spaces.
|
||||
bool chatExternalUrlIsSafe(String raw) {
|
||||
@@ -145,21 +154,34 @@ bool chatExternalUrlIsSafe(String raw) {
|
||||
/// Fold one `form` frame into the retained forms, keyed by id. A resolved or
|
||||
/// cancelled update keeps the recorded answers so the card can show them
|
||||
/// read-only instead of dropping evidence.
|
||||
///
|
||||
/// [raisedIn] is recorded as [chatQuestionRaisedIn] only on the form's first
|
||||
/// frame: a later resolution arrives during another message and must not move
|
||||
/// the question.
|
||||
Map<String, JsonMap> chatFoldQuestionFrame(
|
||||
Map<String, JsonMap> current,
|
||||
JsonMap? form,
|
||||
) {
|
||||
JsonMap? form, {
|
||||
String? raisedIn,
|
||||
}) {
|
||||
if (form == null) return current;
|
||||
final id = _text(form['id']);
|
||||
if (id.isEmpty) return current;
|
||||
final existing = current[id];
|
||||
final next = <String, JsonMap>{...current};
|
||||
final anchor = _text(raisedIn);
|
||||
next[id] = existing == null
|
||||
? Map<String, dynamic>.from(form)
|
||||
? {
|
||||
...Map<String, dynamic>.from(form),
|
||||
if (anchor.isNotEmpty) chatQuestionRaisedIn: anchor,
|
||||
}
|
||||
: {...existing, ...Map<String, dynamic>.from(form)};
|
||||
return next;
|
||||
}
|
||||
|
||||
/// Client-only form key: the agent message id the question was raised during.
|
||||
/// The server never sends it, so folding a frame never overwrites it.
|
||||
const chatQuestionRaisedIn = 'raised_in_message_id';
|
||||
|
||||
/// A stable id for one confirmed answer action on a form. Reusing the same id
|
||||
/// across a retry lets the send gate recognise a double tap.
|
||||
String chatQuestionReplyId(String formId, {String action = ''}) =>
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../ui/sessions/chat_adaptive.dart';
|
||||
import '../ui/transcript/chat_turn_chip.dart';
|
||||
import 'chat_card_styles.dart';
|
||||
import 'chat_question_card_model.dart';
|
||||
import 'chat_question_recorded_answers.dart';
|
||||
|
||||
/// The settled form of [row], a question card row; null while it still needs
|
||||
/// the operator or its last answer was refused.
|
||||
JsonMap? _settled(JsonMap row) {
|
||||
if (row['kind'] != 'question') return null;
|
||||
if ('${row['error'] ?? ''}'.trim().isNotEmpty) return null;
|
||||
final form = row['form'];
|
||||
if (form is! Map) return null;
|
||||
final converted = Map<String, dynamic>.from(form);
|
||||
return chatFormPending(converted) ? null : converted;
|
||||
}
|
||||
|
||||
/// Folds answered and expired question cards out of the card list, the way a
|
||||
/// turn's tool calls fold into one chip.
|
||||
///
|
||||
/// A settled question joins the agent turn it was raised during (the
|
||||
/// [chatQuestionRaisedIn] message id) as that row's `questions`. Questions
|
||||
/// naming no turn in view share one `questions` row where the first stood.
|
||||
/// Pending questions, and ones whose answer was refused, stay full cards.
|
||||
List<JsonMap> groupSettledQuestions(List<JsonMap> rows) {
|
||||
final result = <JsonMap>[];
|
||||
final attached = <int, List<JsonMap>>{};
|
||||
final loose = <JsonMap>[];
|
||||
int? looseAt;
|
||||
for (final row in rows) {
|
||||
final form = _settled(row);
|
||||
if (form == null) {
|
||||
result.add(row);
|
||||
continue;
|
||||
}
|
||||
final turn = _turnOf(result, '${form[chatQuestionRaisedIn] ?? ''}'.trim());
|
||||
if (turn != null) {
|
||||
attached.putIfAbsent(turn, () => []).add(form);
|
||||
} else {
|
||||
looseAt ??= result.length;
|
||||
loose.add(form);
|
||||
}
|
||||
}
|
||||
for (final entry in attached.entries) {
|
||||
result[entry.key] = {...result[entry.key], 'questions': entry.value};
|
||||
}
|
||||
if (looseAt != null) {
|
||||
result.insert(looseAt, {
|
||||
'kind': 'questions',
|
||||
'key': 'chat-questions-${loose.first['id']}',
|
||||
'questions': loose,
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int? _turnOf(List<JsonMap> rows, String messageId) {
|
||||
if (messageId.isEmpty) return null;
|
||||
for (var index = rows.length - 1; index >= 0; index--) {
|
||||
final row = rows[index];
|
||||
final ids = row['messageIds'];
|
||||
if (row['kind'] == 'assistant' && ids is List && ids.contains(messageId)) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// The settled forms a row carries (a turn's or a loose `questions` row's).
|
||||
List<JsonMap> chatRowQuestions(JsonMap row) {
|
||||
final questions = row['questions'];
|
||||
if (questions is! List) return const [];
|
||||
return [
|
||||
for (final form in questions)
|
||||
if (form is Map) Map<String, dynamic>.from(form),
|
||||
];
|
||||
}
|
||||
|
||||
/// The collapsed line for one settled question: the question and its outcome,
|
||||
/// `Which database? · Postgres`, `… · +1 more` when several were answered.
|
||||
String chatQuestionSummary(JsonMap form) {
|
||||
final fields = chatFormFields(form);
|
||||
final answers = form['answers'] is Map
|
||||
? Map<String, dynamic>.from(form['answers'] as Map)
|
||||
: <String, dynamic>{};
|
||||
final title = '${form['title'] ?? ''}'.trim();
|
||||
String question = title.isEmpty ? 'Question' : title;
|
||||
if (fields.isNotEmpty) {
|
||||
question = chatQuestionFieldLabel(
|
||||
fields.first,
|
||||
'${fields.first['key'] ?? ''}'.trim(),
|
||||
);
|
||||
}
|
||||
if (form['cancelled'] == true) return '$question · Expired';
|
||||
final answered = [
|
||||
for (final field in fields)
|
||||
if (answers.containsKey('${field['key'] ?? ''}'.trim())) field,
|
||||
];
|
||||
if (answered.isEmpty) {
|
||||
return answers.isEmpty ? '$question · Closed' : '$question · Answered';
|
||||
}
|
||||
final first = answered.first;
|
||||
final key = '${first['key'] ?? ''}'.trim();
|
||||
final more = answers.length - 1;
|
||||
return [
|
||||
chatQuestionFieldLabel(first, key),
|
||||
chatQuestionAnswerText(first, answers[key]),
|
||||
if (more > 0) '+$more more',
|
||||
].join(' · ');
|
||||
}
|
||||
|
||||
/// One settled question as the quiet chip a turn's tools use. It opens a
|
||||
/// native sheet with the full question and the recorded answers.
|
||||
class ChatQuestionChip extends StatelessWidget {
|
||||
const ChatQuestionChip({super.key, required this.form});
|
||||
|
||||
final JsonMap form;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final color = cupertino
|
||||
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.onSurfaceVariant;
|
||||
final label = chatQuestionSummary(form);
|
||||
return ChatTurnChip(
|
||||
chipKey: ValueKey<String>('chat-question-chip-${form['id']}'),
|
||||
leading: Icon(
|
||||
cupertino ? CupertinoIcons.question_circle : Icons.help_outline,
|
||||
size: 14,
|
||||
color: color,
|
||||
),
|
||||
label: label,
|
||||
color: color,
|
||||
ellipsizeLabel: true,
|
||||
semanticsLabel: 'Question: $label, expand for details',
|
||||
sheet: _QuestionSheet(form: form),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The chips for [row]'s settled questions, one per question.
|
||||
List<Widget> chatQuestionChips(JsonMap row) => [
|
||||
for (final form in chatRowQuestions(row)) ChatQuestionChip(form: form),
|
||||
];
|
||||
|
||||
/// A loose `questions` row: its chips on the agent's side.
|
||||
class ChatQuestionsRow extends StatelessWidget {
|
||||
const ChatQuestionsRow({super.key, required this.row});
|
||||
|
||||
final JsonMap row;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Align(
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: chatQuestionChips(row),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class _QuestionSheet extends StatelessWidget {
|
||||
const _QuestionSheet({required this.form});
|
||||
|
||||
final JsonMap form;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final style = cupertino
|
||||
? CupertinoTheme.of(context).textTheme.textStyle
|
||||
: Theme.of(context).textTheme.bodyMedium;
|
||||
final title = '${form['title'] ?? ''}'.trim();
|
||||
return DefaultTextStyle.merge(
|
||||
style: style,
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
maxHeight: MediaQuery.sizeOf(context).height * 0.8,
|
||||
),
|
||||
child: ListView(
|
||||
key: const Key('chat-question-sheet'),
|
||||
shrinkWrap: true,
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
|
||||
children: [
|
||||
Text(
|
||||
title.isEmpty ? 'Question' : title,
|
||||
style: chatCardTitleStyle(context, cupertino),
|
||||
),
|
||||
Text(
|
||||
chatQuestionSettledStatus(form),
|
||||
style: chatCardSecondaryStyle(context, cupertino),
|
||||
),
|
||||
...chatQuestionRecordedAnswers(
|
||||
context: context,
|
||||
form: form,
|
||||
cupertino: cupertino,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -24,8 +24,8 @@ List<Widget> chatQuestionRecordedAnswers({
|
||||
widgets.add(
|
||||
_recorded(
|
||||
context,
|
||||
_fieldLabel(field, key),
|
||||
_answerText(field, answers[key]),
|
||||
chatQuestionFieldLabel(field, key),
|
||||
chatQuestionAnswerText(field, answers[key]),
|
||||
cupertino,
|
||||
),
|
||||
);
|
||||
@@ -33,7 +33,12 @@ List<Widget> chatQuestionRecordedAnswers({
|
||||
for (final entry in answers.entries) {
|
||||
if (shown.contains(entry.key)) continue;
|
||||
widgets.add(
|
||||
_recorded(context, entry.key, _answerText(null, entry.value), cupertino),
|
||||
_recorded(
|
||||
context,
|
||||
entry.key,
|
||||
chatQuestionAnswerText(null, entry.value),
|
||||
cupertino,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (widgets.isEmpty) {
|
||||
@@ -49,7 +54,7 @@ List<Widget> chatQuestionRecordedAnswers({
|
||||
|
||||
/// The field's display label, falling back past blank recorded strings the same
|
||||
/// way the pending field does.
|
||||
String _fieldLabel(JsonMap field, String key) {
|
||||
String chatQuestionFieldLabel(JsonMap field, String key) {
|
||||
for (final candidate in [field['title'], field['description']]) {
|
||||
final text = '${candidate ?? ''}'.trim();
|
||||
if (text.isNotEmpty) return text;
|
||||
@@ -74,7 +79,8 @@ Widget _recorded(
|
||||
),
|
||||
);
|
||||
|
||||
String _answerText(JsonMap? field, Object? value) {
|
||||
/// A recorded answer as display text: option labels, Yes/No, list joined.
|
||||
String chatQuestionAnswerText(JsonMap? field, Object? value) {
|
||||
final values = value is List ? value : [value];
|
||||
final boolean = field != null && '${field['type'] ?? ''}' == 'boolean';
|
||||
return values
|
||||
|
||||
@@ -10,7 +10,8 @@ import 'chat_question_card_model.dart';
|
||||
/// gates, so the controller itself stays about messages and live turn state.
|
||||
///
|
||||
/// The web drops an approval or form once it is resolved. This app keeps the
|
||||
/// latest settled cards as read-only evidence, which is an intentional addition;
|
||||
/// latest settled cards as read-only evidence, collapsed into their turn like
|
||||
/// tool calls (see `chat_permission_group.dart`, `chat_question_group.dart`);
|
||||
/// [settledLimit] bounds that history so a long session cannot grow without
|
||||
/// limit while pending cards are never dropped.
|
||||
class ChatThreadCards {
|
||||
@@ -136,11 +137,12 @@ class ChatThreadCards {
|
||||
}
|
||||
|
||||
/// Fold one `form` event. A folded update keeps a settled form's recorded
|
||||
/// answers for the read-only card.
|
||||
void foldForm(JsonMap form) {
|
||||
/// answers for the collapsed row. [raisedIn] is the agent message the first
|
||||
/// frame arrived during; it places the answered question in its turn.
|
||||
void foldForm(JsonMap form, {String? raisedIn}) {
|
||||
final id = '${form['id'] ?? ''}'.trim();
|
||||
if (id.isEmpty) return;
|
||||
final next = chatFoldQuestionFrame(_forms, form);
|
||||
final next = chatFoldQuestionFrame(_forms, form, raisedIn: raisedIn);
|
||||
if (identical(next, _forms)) return;
|
||||
_forms
|
||||
..clear()
|
||||
|
||||
@@ -369,7 +369,10 @@ class ChatThreadController extends ChangeNotifier {
|
||||
if (body['kind'] == 'form') {
|
||||
final form = body['form'];
|
||||
if (form is! Map) return;
|
||||
_cards.foldForm(Map<String, dynamic>.from(form));
|
||||
_cards.foldForm(
|
||||
Map<String, dynamic>.from(form),
|
||||
raisedIn: _answeringMessageId(),
|
||||
);
|
||||
return;
|
||||
}
|
||||
final current = _thread;
|
||||
@@ -415,6 +418,21 @@ class ChatThreadController extends ChangeNotifier {
|
||||
].any((key) => usage[key] != null);
|
||||
}
|
||||
|
||||
/// The agent message a question frame arrives during: the streamed reply,
|
||||
/// else the last answer when it is the agent's. A question raised before the
|
||||
/// reply starts names none rather than an older turn.
|
||||
String? _answeringMessageId() {
|
||||
final live = _liveAssistantMessageId;
|
||||
if (live != null && live.isNotEmpty) return live;
|
||||
for (final message in _messages.reversed) {
|
||||
if (chatInjectedKind(message) != null) continue;
|
||||
if (message['role'] != 'assistant') return null;
|
||||
final id = '${message['id'] ?? ''}'.trim();
|
||||
return id.isEmpty ? null : id;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// The pushed envelope body, or null for another message type. The server
|
||||
/// wraps `chat_event` in the typed envelope; domain code never sees the
|
||||
/// transport fields.
|
||||
|
||||
@@ -52,6 +52,7 @@ import 'chat_transcript_cache.dart';
|
||||
import 'chat_transcript_scroll_view.dart';
|
||||
import 'chat_permission_card_model.dart';
|
||||
import 'chat_permission_group.dart';
|
||||
import 'chat_question_group.dart';
|
||||
import 'chat_transcript_card.dart';
|
||||
|
||||
/// One read-only conversation: the controller's transcript in the order the
|
||||
@@ -851,22 +852,25 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
permissionErrorFor: _controller.permissionErrorFor,
|
||||
formErrorFor: _controller.questionErrorFor,
|
||||
);
|
||||
// Answered approvals fold into their turn's chips, like tool calls.
|
||||
final allRows = groupSettledPermissions(
|
||||
transcriptRows(
|
||||
messages,
|
||||
trailing: cards,
|
||||
running:
|
||||
_controller.running &&
|
||||
!_reconnecting &&
|
||||
!_staleTranscript &&
|
||||
!cards.any(chatCardAwaitingOperator),
|
||||
awaitingResponse:
|
||||
_controller.awaitingResponse &&
|
||||
!_reconnecting &&
|
||||
!_staleTranscript &&
|
||||
!cards.any(chatCardAwaitingOperator),
|
||||
liveMessageId: _controller.liveAssistantMessageId,
|
||||
// Answered approvals and questions fold into their turn's chips, like
|
||||
// tool calls; only cards still waiting on the operator stay full cards.
|
||||
final allRows = groupSettledQuestions(
|
||||
groupSettledPermissions(
|
||||
transcriptRows(
|
||||
messages,
|
||||
trailing: cards,
|
||||
running:
|
||||
_controller.running &&
|
||||
!_reconnecting &&
|
||||
!_staleTranscript &&
|
||||
!cards.any(chatCardAwaitingOperator),
|
||||
awaitingResponse:
|
||||
_controller.awaitingResponse &&
|
||||
!_reconnecting &&
|
||||
!_staleTranscript &&
|
||||
!cards.any(chatCardAwaitingOperator),
|
||||
liveMessageId: _controller.liveAssistantMessageId,
|
||||
),
|
||||
),
|
||||
);
|
||||
// A transcript held from before a socket gap may still say a compaction is
|
||||
@@ -1262,6 +1266,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
}
|
||||
if (kind == 'system_notice') return ChatSystemNoticeLine(row: row);
|
||||
if (kind == 'task_notices') return ChatTaskNoticesGroup(row: row);
|
||||
if (kind == 'questions') return ChatQuestionsRow(row: row);
|
||||
if (kind == 'permission' || kind == 'question') {
|
||||
return ChatTranscriptCard(
|
||||
row: row,
|
||||
@@ -1273,6 +1278,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
final chips = [
|
||||
if (permissions is List<ChatPermissionCardData> && permissions.isNotEmpty)
|
||||
ChatPermissionsChip(permissions: permissions),
|
||||
...chatQuestionChips(row),
|
||||
];
|
||||
if (kind == 'permissions') {
|
||||
return Align(
|
||||
|
||||
@@ -14,6 +14,7 @@ class ChatTurnChip extends StatelessWidget {
|
||||
required this.color,
|
||||
required this.semanticsLabel,
|
||||
required this.sheet,
|
||||
this.ellipsizeLabel = false,
|
||||
});
|
||||
|
||||
final Key chipKey;
|
||||
@@ -23,6 +24,10 @@ class ChatTurnChip extends StatelessWidget {
|
||||
final String semanticsLabel;
|
||||
final Widget sheet;
|
||||
|
||||
/// Shrink a long label to one ellipsized line. Only for a chip whose parent
|
||||
/// bounds its width (a turn footer's Wrap, an aligned row).
|
||||
final bool ellipsizeLabel;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino =
|
||||
@@ -32,7 +37,10 @@ class ChatTurnChip extends StatelessWidget {
|
||||
children: [
|
||||
leading,
|
||||
const SizedBox(width: 6),
|
||||
Text(label, style: TextStyle(color: color, fontSize: 13)),
|
||||
if (ellipsizeLabel)
|
||||
Flexible(child: _label(maxLines: 1, overflow: TextOverflow.ellipsis))
|
||||
else
|
||||
_label(),
|
||||
Icon(
|
||||
cupertino ? CupertinoIcons.chevron_up : Icons.expand_less,
|
||||
size: 14,
|
||||
@@ -73,6 +81,13 @@ class ChatTurnChip extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
Text _label({int? maxLines, TextOverflow? overflow}) => Text(
|
||||
label,
|
||||
maxLines: maxLines,
|
||||
overflow: overflow,
|
||||
style: TextStyle(color: color, fontSize: 13),
|
||||
);
|
||||
|
||||
Future<void> _open(BuildContext context, bool cupertino) {
|
||||
if (cupertino) {
|
||||
return showCupertinoModalPopup<void>(
|
||||
|
||||
Reference in New Issue
Block a user