Files
pipelines/app/lib/features/chat/conversation/chat_background_task_detail.dart
T
alan 2cb4a0c555 feat(app): show a session's running background tasks
The conversation reads the session's recorded chat_shells snapshot and folds
pushed shell frames, so only tasks the server still reports as running appear.
The list is capped at three visible rows and scrolls beyond, and a task that
exits leaves it.

Selecting a task opens its recorded command, identity and running status plus
one bounded chat_shell_output page (cursor 0, 64 KiB). Empty, unavailable and
refused reads are reported with native Retry/Refresh controls, and a refused
snapshot is shown with its reason instead of claiming there are no tasks.
Both reads carry the session id, linking every task to its conversation.
2026-10-22 17:14:56 +01:00

202 lines
6.7 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../domain/chat_shells.dart';
import '../ui/sessions/chat_adaptive.dart';
/// The selected background task's read-only detail and bounded output page.
///
/// Every value is a recorded fact: the server's command, the shell identity, the
/// running status, and one lazy `chat_shell_output` page. The output area keeps
/// the five states apart, so an unavailable or empty page and a refused read are
/// never shown as output. Output is selectable and reads in a monospace body.
class ChatBackgroundTaskDetail extends StatelessWidget {
const ChatBackgroundTaskDetail({
super.key,
required this.shell,
required this.output,
required this.onRefresh,
});
final JsonMap shell;
final ChatShellOutputSnapshot? output;
final VoidCallback onRefresh;
@override
Widget build(BuildContext context) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final content = DefaultTextStyle.merge(
style: cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: DefaultTextStyle.of(context).style,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
spacing: 4,
children: [
_label(context, cupertino, shellOutputCommandLabel),
Text(
blankShellValue(shell['command'])
? shellOutputUnavailableLabel
: '${shell['command']}',
),
_label(context, cupertino, shellOutputTaskIdentityLabel),
Text(
shellIdOf(shell),
style: const TextStyle(fontFamily: 'monospace'),
),
Text(shellStatusRunningLabel),
_outputArea(context, cupertino),
],
),
);
if (cupertino) {
return CupertinoListSection.insetGrouped(
children: [Padding(padding: const EdgeInsets.all(12), child: content)],
);
}
return Card(
margin: EdgeInsets.zero,
child: Padding(padding: const EdgeInsets.all(12), child: content),
);
}
Widget _label(BuildContext context, bool cupertino, String text) {
final style = cupertino
? CupertinoTheme.of(context).textTheme.tabLabelTextStyle.copyWith(
color: CupertinoColors.secondaryLabel.resolveFrom(context),
)
: Theme.of(context).textTheme.labelSmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
);
return Text(text, style: style);
}
/// Selectable output in the host's own control. A Cupertino-only app has no
/// `MaterialLocalizations`, which `SelectableText` asserts on, so one English
/// default scope is supplied for its context menu (the chat markdown view's
/// own pattern).
Widget _selectableOutput(
BuildContext context,
bool cupertino,
String output,
) {
final text = SelectableText(
output,
key: const Key('chat-bg-task-output'),
style:
(cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: Theme.of(context).textTheme.bodySmall)
?.copyWith(
fontFamily: 'monospace',
// Cupertino's style is not inheriting; a selectable body needs a
// baseline from its own style.
textBaseline: TextBaseline.alphabetic,
),
);
if (!cupertino) return text;
return Localizations(
locale: Localizations.maybeLocaleOf(context) ?? const Locale('en'),
delegates: const [
DefaultWidgetsLocalizations.delegate,
DefaultMaterialLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
],
child: text,
);
}
Widget _outputArea(BuildContext context, bool cupertino) {
final state = output?.state ?? ChatShellOutputState.none;
final muted = cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant;
final noteStyle =
(cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: Theme.of(context).textTheme.bodySmall)
?.copyWith(color: muted);
switch (state) {
case ChatShellOutputState.none:
return const SizedBox.shrink();
case ChatShellOutputState.loading:
return Align(
alignment: AlignmentDirectional.centerStart,
child: cupertino
? const CupertinoActivityIndicator()
: const CircularProgressIndicator(),
);
case ChatShellOutputState.error:
return Row(
key: const Key('chat-bg-task-output-error'),
crossAxisAlignment: CrossAxisAlignment.start,
spacing: 8,
children: [
Expanded(
child: Text(
'${output?.error ?? ''}',
style: noteStyle?.copyWith(
color: cupertino
? CupertinoColors.systemRed.resolveFrom(context)
: Theme.of(context).colorScheme.error,
),
),
),
ChatBackgroundTaskButton(
label: shellOutputRetryLabel,
onPressed: onRefresh,
),
],
);
case ChatShellOutputState.unavailable:
return Text(shellOutputUnavailableNote, style: noteStyle);
case ChatShellOutputState.empty:
return Text(shellOutputEmptyNote, style: noteStyle);
case ChatShellOutputState.ready:
final page = output!.page!;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
spacing: 4,
children: [
_label(context, cupertino, shellOutputLabel),
_selectableOutput(context, cupertino, page.output),
if (page.truncated) Text(shellOutputBoundedNote, style: noteStyle),
Align(
alignment: AlignmentDirectional.centerStart,
child: ChatBackgroundTaskButton(
label: shellOutputRefreshLabel,
onPressed: onRefresh,
),
),
],
);
}
}
}
/// The host's own text button for a task action (Retry/Refresh).
class ChatBackgroundTaskButton extends StatelessWidget {
const ChatBackgroundTaskButton({
super.key,
required this.label,
required this.onPressed,
});
final String label;
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
return CupertinoButton(
padding: EdgeInsets.zero,
onPressed: onPressed,
child: Text(label),
);
}
return TextButton(onPressed: onPressed, child: Text(label));
}
}