diff --git a/app/lib/features/runs/screen.dart b/app/lib/features/runs/screen.dart new file mode 100644 index 00000000..3edd5240 --- /dev/null +++ b/app/lib/features/runs/screen.dart @@ -0,0 +1,44 @@ +import 'package:flutter/material.dart'; + +import '../../core/api_gateway.dart'; +import 'data/runs_repository.dart'; +import 'ui/runs_controller.dart'; +import 'ui/runs_list.dart'; +import 'ui/run_detail_view.dart'; + +class RunsScreen extends StatefulWidget { + const RunsScreen({super.key, required this.gateway, this.initialRunId}); + final ApiGateway gateway; + final int? initialRunId; + + @override + State createState() => _RunsScreenState(); +} + +class _RunsScreenState extends State { + late RunsController controller; + @override + void initState() { + super.initState(); + controller = RunsController(RunsRepository(widget.gateway)); + controller.listen(widget.gateway.frames); + controller.addListener(_changed); + controller.load(widget.initialRunId); + } + + void _changed() { + if (mounted) setState(() {}); + } + + @override + void dispose() { + controller.removeListener(_changed); + controller.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) => controller.detail == null + ? RunsList(controller: controller) + : RunDetailView(controller: controller); +} diff --git a/app/lib/features/runs/ui/run_detail_view.dart b/app/lib/features/runs/ui/run_detail_view.dart new file mode 100644 index 00000000..c1bd01c5 --- /dev/null +++ b/app/lib/features/runs/ui/run_detail_view.dart @@ -0,0 +1,123 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'runs_controller.dart'; + +class RunDetailView extends StatelessWidget { + const RunDetailView({super.key, required this.controller}); + final RunsController controller; + @override + Widget build(BuildContext context) { + final run = controller.detail; + if (controller.loading && run == null) { + return const Center(child: CircularProgressIndicator()); + } + if (run == null) { + return Center( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Text(controller.error ?? 'Run details unavailable.'), + FilledButton( + onPressed: controller.back, + child: const Text('Back to runs'), + ), + ], + ), + ); + } + final input = run['input'] is Map ? run['input'] as Map : const {}; + final output = run['output'] is Map ? run['output'] as Map : const {}; + final steps = run['steps'] is List ? run['steps'] as List : const []; + final prompt = input['prompt']; + return ListView( + padding: const EdgeInsets.all(16), + children: [ + TextButton.icon( + onPressed: controller.back, + icon: const Icon(Icons.arrow_back), + label: const Text('Runs'), + ), + Text( + '${run['title'] ?? run['name'] ?? 'Run ${run['id']}'}', + style: Theme.of(context).textTheme.headlineSmall, + ), + Wrap( + spacing: 8, + children: [ + Chip(label: Text('${run['status'] ?? 'unknown'}')), + Text('Run ${run['id']}'), + Text('Pipeline ${run['name'] ?? 'unavailable'}'), + ], + ), + if (prompt is String) ...[ + const SizedBox(height: 12), + Row( + children: [ + Text( + 'Original prompt', + style: Theme.of(context).textTheme.titleMedium, + ), + const Spacer(), + IconButton( + tooltip: 'Copy prompt', + onPressed: () => _copy(context, prompt), + icon: const Icon(Icons.copy), + ), + ], + ), + SelectableText(prompt), + ], + _section(context, 'Input', input), + _section(context, 'Output', output), + Text('Timeline', style: Theme.of(context).textTheme.titleLarge), + if (steps.isEmpty) + const Text('No visits recorded.') + else + ...steps.whereType().map( + (step) => ListTile( + leading: const Icon(Icons.circle_outlined), + title: Text( + '${step['stage'] ?? 'Stage unavailable'} · visit ${step['sequence'] ?? '?'}', + ), + subtitle: Text( + '${step['status'] ?? 'Status unavailable'}${step['outcome'] == null ? '' : ' · ${step['outcome']}'}', + ), + ), + ), + const ListTile(title: Text('Metrics'), subtitle: Text('Unavailable')), + ], + ); + } + + Widget _section(BuildContext context, String title, Map value) => + ExpansionTile( + title: Text(title), + children: [ + Padding( + padding: const EdgeInsets.all(16), + child: SelectableText( + value.isEmpty + ? 'No ${title.toLowerCase()} recorded.' + : value.entries.map((e) => '${e.key}: ${e.value}').join('\n'), + ), + ), + ], + ); + Future _copy(BuildContext context, String text) async { + // Clipboard failure is reported rather than silently substituting title/output. + try { + await Clipboard.setData(ClipboardData(text: text)); + if (context.mounted) { + ScaffoldMessenger.of( + context, + ).showSnackBar(const SnackBar(content: Text('Prompt copied.'))); + } + } catch (_) { + if (context.mounted) { + ScaffoldMessenger.of( + context, + ).showSnackBar(const SnackBar(content: Text('Could not copy prompt.'))); + } + } + } +} diff --git a/app/lib/features/runs/ui/runs_controller.dart b/app/lib/features/runs/ui/runs_controller.dart new file mode 100644 index 00000000..02fa8bf8 --- /dev/null +++ b/app/lib/features/runs/ui/runs_controller.dart @@ -0,0 +1,108 @@ +import 'dart:async'; +import 'package:flutter/foundation.dart'; +import '../../../core/api_gateway.dart'; +import '../data/runs_repository.dart'; +import '../domain/run_events.dart'; +import '../domain/run_models.dart'; + +class RunsController extends ChangeNotifier { + RunsController(this.repository); + final RunsRepository repository; + StreamSubscription? _subscription; + List runs = []; + JsonMap? detail; + String? error; + bool loading = false; + String query = '', sort = 'newest_first'; + int _selection = 0; + Timer? _refresh; + + void listen(Stream frames) { + _subscription?.cancel(); + _subscription = frames + .where((f) => f['type'] == 'run_event') + .listen(_event); + } + + Future load([int? initialId]) async { + final token = ++_selection; + loading = true; + error = null; + notifyListeners(); + try { + final result = await repository.listRuns(); + if (token != _selection) return; + runs = result.runs; + if (initialId != null) await select(initialId); + } catch (e) { + if (token == _selection) error = '$e'; + } + if (token == _selection) { + loading = false; + notifyListeners(); + } + } + + Future select(num id) async { + final token = ++_selection; + detail = null; + error = null; + loading = true; + notifyListeners(); + try { + final value = await repository.getRun(id); + if (token == _selection) detail = value; + } catch (e) { + if (token == _selection) error = '$e'; + } + if (token == _selection) { + loading = false; + notifyListeners(); + } + } + + void back() { + ++_selection; + detail = null; + error = null; + notifyListeners(); + } + + void setQuery(String value) { + query = value; + notifyListeners(); + } + + void _event(JsonMap frame) { + final folded = foldRunEvent(runs, frame); + runs = folded.runs; + if (detail?['id'] == frame['run_id']) { + detail = foldRunEventDetail(detail, frame); + } + if (detail != null && runDetailNeedsFullSync(frame)) { + _refresh?.cancel(); + _refresh = Timer( + const Duration(milliseconds: 150), + () => select(detail!['id'] as num), + ); + } + notifyListeners(); + } + + List get visibleRuns => sortRuns( + runs + .where( + (r) => '${r['title'] ?? r['name'] ?? ''}'.toLowerCase().contains( + query.toLowerCase(), + ), + ) + .toList(), + sort: sort, + ); + @override + void dispose() { + _subscription?.cancel(); + _refresh?.cancel(); + super.dispose(); + } +} diff --git a/app/lib/features/runs/ui/runs_list.dart b/app/lib/features/runs/ui/runs_list.dart new file mode 100644 index 00000000..9ab2d1b8 --- /dev/null +++ b/app/lib/features/runs/ui/runs_list.dart @@ -0,0 +1,75 @@ +import 'package:flutter/material.dart'; +import 'runs_controller.dart'; +import '../domain/run_models.dart'; + +class RunsList extends StatelessWidget { + const RunsList({super.key, required this.controller}); + final RunsController controller; + @override + Widget build(BuildContext context) { + if (controller.loading && controller.runs.isEmpty) { + return const Center(child: CircularProgressIndicator()); + } + if (controller.error != null && controller.runs.isEmpty) { + return Center( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Text(controller.error!), + const SizedBox(height: 8), + FilledButton( + onPressed: controller.load, + child: const Text('Retry'), + ), + ], + ), + ); + } + return Column( + children: [ + if (controller.error != null) + MaterialBanner( + content: Text(controller.error!), + actions: [ + TextButton(onPressed: controller.load, child: const Text('Retry')), + ], + ), + Padding( + padding: const EdgeInsets.all(16), + child: TextField( + decoration: const InputDecoration( + labelText: 'Filter runs', + prefixIcon: Icon(Icons.search), + ), + onChanged: controller.setQuery, + ), + ), + Expanded( + child: controller.visibleRuns.isEmpty + ? const Center(child: Text('No runs found.')) + : ListView.builder( + itemCount: controller.visibleRuns.length, + itemBuilder: (context, index) { + final run = controller.visibleRuns[index]; + final identity = runIdentity(run['title'], run['name']); + final created = + run['created_at']?.toString() ?? 'Time unavailable'; + final id = run['id']; + return ListTile( + title: Text( + identity.label.isEmpty ? 'Run $id' : identity.label, + ), + subtitle: Text( + '${run['name'] ?? 'Pipeline unavailable'} · $created${run['scheduled'] == true ? ' · Scheduled' : ''}', + ), + leading: Chip(label: Text(runStatusLabel(run))), + trailing: const Icon(Icons.chevron_right), + onTap: id is num ? () => controller.select(id) : null, + ); + }, + ), + ), + ], + ); + } +}