96de9f5bea
Keep the Material-only conversation implementation while preserving public Cupertino parameters for callers. Narrow affected tests to Material hosts so the dead platform paths are no longer exercised.
198 lines
6.2 KiB
Dart
198 lines
6.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../../core/api_gateway.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 color = Theme.of(context).colorScheme.onSurfaceVariant;
|
|
final label = chatQuestionSummary(form);
|
|
return ChatTurnChip(
|
|
chipKey: ValueKey<String>('chat-question-chip-${form['id']}'),
|
|
leading: Icon(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 style = 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, false),
|
|
),
|
|
Text(
|
|
chatQuestionSettledStatus(form),
|
|
style: chatCardSecondaryStyle(context, false),
|
|
),
|
|
...chatQuestionRecordedAnswers(
|
|
context: context,
|
|
form: form,
|
|
cupertino: false,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|