diff --git a/README.md b/README.md index 4a106e10..a11b41e0 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,9 @@ Small, reusable AI workflows for coding, research and everyday project work, plus chats with the coding agents you already use. -Pipelines is one per-user server with three clients: +Pipelines is one per-user server with two clients: -- **Web app** (Flutter, served behind a sign-in) and the same app as an +- **The app** (Flutter): served as a web app behind a sign-in, and as an **Android APK**. - **CLI and TUI** (`pipelines`) for scripting, inspection and authoring. @@ -26,6 +26,7 @@ results, and expect provider charges. git clone https://git.a-wan.net/alan/pipelines.git cd pipelines ./install # server + CLI + web app, login autostart +./install --web # rebuild only the web app after app changes ./install --apk # optional: Android app, offered to phones for update ``` @@ -34,13 +35,9 @@ compiler, a **running Docker** (the web app is built in Docker; the install refuses without it) and an existing OpenCode on `PATH`. The installer never installs these for you. See [Installation](docs/install.md). -Then put the CLI on your `PATH`, open the URL the installer prints (default -`http://localhost:8082/`) and sign in with the password you chose: - -```sh -export PATH="$HOME/.local/share/pipelines/bin:$PATH" # add to ~/.bashrc or ~/.zshrc -pipelines status -``` +Then open the URL the installer prints (default `http://localhost:8082/`) +and sign in with the password you chose. Put the CLI on your `PATH` as in the +[terminal quick start](#terminal-quick-start). ### Linux quick start (Debian/Ubuntu) @@ -86,23 +83,25 @@ pipelines harness list # status of all four ## What you can do **Runs** -- Runs list with filters, multi-select and delete, and sorting by newest, - oldest, **largest on disk**, **most tokens**, duration, pipeline or status. - Each row shows the run's size and reported token total (`22.4M tokens`, - `≥` when some stages did not report); the run Summary adds fresh versus - cached tokens, the in/out split and the reported cost. -- Run detail tabs: Summary, Timeline, and Artifacts and Changes when the run - has any. +- Runs list with filters, sorting (including largest on disk and most tokens) + and multi-select delete. Each row shows the run's size and reported token + total (`≥` when some stages did not report). +- Run detail: Summary (with size, tokens split fresh versus cached, and the + reported cost), Timeline, and Artifacts and Changes when the run has any. - Artifacts viewer for files a stage produced, including the recorded session - export (`session.html`). + export (`session.html`), with a full-screen view where HTML pages scroll, + zoom and open their links. - Changes: the file diffs each stage made in the workspace. - Human decision stages: a run waits for you; decide in the app or with `pipelines decide`. - Retry from the failed stage, cancel, rename, per-run model overrides. **Pipelines** -- Starter templates, draft-first authoring with a harness, versioned - publishing, import/export as ZIP. +- A new install starts with four [example pipelines](examples/README.md), + from one stage to a review loop and a human decision; they are also the + template set. +- Draft-first authoring with a harness, versioned publishing, import/export + as ZIP. - [Schedules](docs/schedules.md): recurring runs of an existing pipeline. - Search across pipelines, versions, runs, steps, fields and artifacts. @@ -159,7 +158,8 @@ See [Harnesses](docs/harnesses.md). ## Terminal quick start ```sh -export PATH="$HOME/.local/share/pipelines/bin:$PATH" +export PATH="$HOME/.local/share/pipelines/bin:$PATH" # add to ~/.bashrc or ~/.zshrc +pipelines status cd /path/to/your/project pipelines # TUI pipelines run "pipeline name" "Describe a small task" diff --git a/app/lib/features/artifacts/domain/artifact_html_links.dart b/app/lib/features/artifacts/domain/artifact_html_links.dart new file mode 100644 index 00000000..7f2f7414 --- /dev/null +++ b/app/lib/features/artifacts/domain/artifact_html_links.dart @@ -0,0 +1,50 @@ +/// What following one link inside a recorded HTML document does. +enum ArtifactHtmlLinkAction { + /// A place in the same document (`#section`): the view scrolls to it. + inDocument, + + /// An `http`, `https` or `mailto` address: it opens outside the artifact + /// (a new browser tab on the web, the system's handler on a phone), never + /// inside the document's view. + external, + + /// Anything else (`javascript:`, `file:`, `data:`, a relative path the + /// document has no base for): the link stays inert. + blocked, +} + +const _externalSchemes = {'http', 'https', 'mailto'}; + +/// The documents the views load the recorded HTML as: a web view's +/// `about:blank` and an iframe's `about:srcdoc`. +const _documentUrls = {'about:blank', 'about:srcdoc'}; + +/// Decides what a link in a recorded HTML document does, from the raw `href` +/// (the web view rewrites anchors before showing the document) or the url a +/// mobile web view resolved it to. +/// +/// The document is loaded from a string, so it has no address of its own: a +/// relative link cannot resolve to the artifact's neighbours and stays inert +/// rather than reaching the app's own origin. +ArtifactHtmlLinkAction artifactHtmlLinkAction(String href) { + final link = href.trim(); + if (link.isEmpty) return ArtifactHtmlLinkAction.blocked; + if (link.startsWith('#')) return ArtifactHtmlLinkAction.inDocument; + final uri = Uri.tryParse(link); + if (uri == null || !uri.hasScheme) return ArtifactHtmlLinkAction.blocked; + final scheme = uri.scheme.toLowerCase(); + if (_externalSchemes.contains(scheme)) { + // `http:foo` with no host is not an address anyone can open. + if (scheme != 'mailto' && uri.host.isEmpty) { + return ArtifactHtmlLinkAction.blocked; + } + return ArtifactHtmlLinkAction.external; + } + if (scheme == 'about') { + final document = link.split('#').first.toLowerCase(); + if (_documentUrls.contains(document)) { + return ArtifactHtmlLinkAction.inDocument; + } + } + return ArtifactHtmlLinkAction.blocked; +} diff --git a/app/lib/features/artifacts/ui/artifact_controls_overlay.dart b/app/lib/features/artifacts/ui/artifact_controls_overlay.dart new file mode 100644 index 00000000..bc58f4c4 --- /dev/null +++ b/app/lib/features/artifacts/ui/artifact_controls_overlay.dart @@ -0,0 +1,116 @@ +import 'dart:async'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../../ui/adaptive/host.dart'; + +/// Open, Copy and the file actions float over the top of the content: shown +/// when the page opens and whenever the content is touched, hidden again after +/// a few seconds without use, so nothing needs scrolling to reach them. +class ArtifactControlsOverlay extends StatefulWidget { + const ArtifactControlsOverlay({ + super.key, + required this.controls, + required this.child, + this.hideAfter = const Duration(seconds: 3), + }); + + final List controls; + final Widget child; + final Duration hideAfter; + + @override + State createState() => + _ArtifactControlsOverlayState(); +} + +class _ArtifactControlsOverlayState extends State { + bool _shown = true; + Timer? _hide; + + @override + void initState() { + super.initState(); + _schedule(); + } + + @override + void dispose() { + _hide?.cancel(); + super.dispose(); + } + + void _schedule() { + _hide?.cancel(); + _hide = Timer(widget.hideAfter, () { + if (mounted) setState(() => _shown = false); + }); + } + + void _show() { + if (!_shown) setState(() => _shown = true); + _schedule(); + } + + @override + Widget build(BuildContext context) { + final cupertino = usesCupertinoHost(context); + final surface = cupertino + ? CupertinoColors.systemBackground + .resolveFrom(context) + .withValues(alpha: 0.92) + : Theme.of( + context, + ).colorScheme.surfaceContainerHigh.withValues(alpha: 0.94); + return Stack( + children: [ + // Any touch on the content brings the controls back. + Positioned.fill( + child: Listener( + behavior: HitTestBehavior.translucent, + onPointerDown: (_) => _show(), + child: widget.child, + ), + ), + Positioned( + left: 8, + right: 8, + top: 8, + child: IgnorePointer( + ignoring: !_shown, + child: AnimatedOpacity( + key: const Key('artifact-viewer-controls'), + opacity: _shown ? 1 : 0, + duration: const Duration(milliseconds: 200), + child: Listener( + // Using a control keeps the bar up. + onPointerDown: (_) => _schedule(), + child: DecoratedBox( + decoration: BoxDecoration( + color: surface, + borderRadius: BorderRadius.circular(12), + boxShadow: const [ + BoxShadow(color: Color(0x22000000), blurRadius: 6), + ], + ), + child: Padding( + padding: const EdgeInsets.symmetric( + horizontal: 4, + vertical: 2, + ), + child: Wrap( + alignment: WrapAlignment.center, + crossAxisAlignment: WrapCrossAlignment.center, + children: widget.controls, + ), + ), + ), + ), + ), + ), + ), + ], + ); + } +} diff --git a/app/lib/features/artifacts/ui/artifact_fullscreen.dart b/app/lib/features/artifacts/ui/artifact_fullscreen.dart new file mode 100644 index 00000000..dd31aa15 --- /dev/null +++ b/app/lib/features/artifacts/ui/artifact_fullscreen.dart @@ -0,0 +1,116 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; + +import '../../../ui/adaptive/segmented_bar_overflow.dart'; +import '../actions/artifact_file_actions.dart'; + +/// Marks a subtree as the artifact viewer's full-screen view, where an HTML +/// document takes every gesture over it (scroll, pan, pinch) instead of +/// sharing them with the viewer's page swipe, and follows [htmlZoom]. +class ArtifactFullscreenScope extends InheritedWidget { + const ArtifactFullscreenScope({ + super.key, + required this.htmlZoom, + required super.child, + }); + + /// The HTML document's zoom factor (1 is the document's own size), set by + /// [ArtifactHtmlZoomActions]. + final ValueListenable htmlZoom; + + static ArtifactFullscreenScope? maybeOf(BuildContext context) => + context.dependOnInheritedWidgetOfExactType(); + + @override + bool updateShouldNotify(ArtifactFullscreenScope oldWidget) => + htmlZoom != oldWidget.htmlZoom; +} + +/// The full-screen view's route, pushed on the root navigator so it covers +/// the shell's bars and list pane. A full-screen dialog route: its bar shows +/// the platform's Close, and the system back gesture leaves it. +Route artifactFullscreenRoute({ + required bool cupertino, + required WidgetBuilder builder, +}) => cupertino + ? CupertinoPageRoute(fullscreenDialog: true, builder: builder) + : MaterialPageRoute(fullscreenDialog: true, builder: builder); + +/// The zoom factors Zoom in and Zoom out step through. +const List artifactHtmlZoomSteps = [ + 0.5, 0.67, 0.8, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, // +]; + +/// The next zoom step from [current] in [direction] (+1 in, -1 out), held at +/// both ends. +double artifactHtmlZoomStep(double current, int direction) { + if (direction > 0) { + return artifactHtmlZoomSteps.firstWhere( + (step) => step > current + 0.001, + orElse: () => artifactHtmlZoomSteps.last, + ); + } + return artifactHtmlZoomSteps.lastWhere( + (step) => step < current - 0.001, + orElse: () => artifactHtmlZoomSteps.first, + ); +} + +const artifactZoomOutKey = ValueKey('artifact-zoom-out'); +const artifactZoomResetKey = ValueKey('artifact-zoom-reset'); +const artifactZoomInKey = ValueKey('artifact-zoom-in'); + +/// Zoom out, actual size and zoom in for an HTML document, as top-bar +/// actions (a narrow bar moves them into its More menu). Used where the +/// document cannot be pinched (the web's sandboxed frame): the frame keeps +/// scrolling the live document at the chosen size. +List artifactHtmlZoomActions(ValueNotifier zoom) { + void step(int direction) => + zoom.value = artifactHtmlZoomStep(zoom.value, direction); + final percent = '${(zoom.value * 100).round()}%'; + Widget action( + Key key, + String label, + IconData icon, + IconData cupertinoIcon, + VoidCallback? onPressed, + ) => SegmentedOverflowAction( + key: key, + overflowEntry: SegmentedOverflowEntry( + label: label, + icon: icon, + onPressed: onPressed, + ), + child: ArtifactIconAction( + label: label, + icon: icon, + cupertinoIcon: cupertinoIcon, + onPressed: onPressed, + ), + ); + final value = zoom.value; + return [ + action( + artifactZoomOutKey, + 'Zoom out', + Icons.zoom_out, + CupertinoIcons.zoom_out, + value > artifactHtmlZoomSteps.first + 0.001 ? () => step(-1) : null, + ), + action( + artifactZoomResetKey, + 'Actual size ($percent)', + Icons.restart_alt, + CupertinoIcons.arrow_counterclockwise, + (value - 1).abs() > 0.001 ? () => zoom.value = 1 : null, + ), + action( + artifactZoomInKey, + 'Zoom in', + Icons.zoom_in, + CupertinoIcons.zoom_in, + value < artifactHtmlZoomSteps.last - 0.001 ? () => step(1) : null, + ), + ]; +} diff --git a/app/lib/features/artifacts/ui/artifact_fullscreen_host.dart b/app/lib/features/artifacts/ui/artifact_fullscreen_host.dart new file mode 100644 index 00000000..5593e8a7 --- /dev/null +++ b/app/lib/features/artifacts/ui/artifact_fullscreen_host.dart @@ -0,0 +1,58 @@ +import 'package:flutter/widgets.dart'; + +import '../../../ui/adaptive/host.dart'; +import 'artifact_fullscreen.dart'; + +/// Lets a viewer page open its own content as the full-screen view. +/// +/// The full-screen route lives on the root navigator, outside the page's +/// subtree, yet draws from the page's state: [buildFrame] builds either +/// presentation, and every [setState] redraws the full-screen route too, so +/// paging, loading and status messages show in both. +mixin ArtifactFullscreenHost on State { + final ValueNotifier _revision = ValueNotifier(0); + + /// The full-screen view's HTML zoom; each full-screen visit starts at 1. + final ValueNotifier htmlZoom = ValueNotifier(1); + Route? _fullscreen; + + /// The page's scaffold, inline or as the full-screen view. + Widget buildFrame(BuildContext context, {required bool fullscreen}); + + @override + void setState(VoidCallback fn) { + super.setState(fn); + _revision.value++; + } + + @override + void dispose() { + // The full-screen route closes with the page, once the tree unlocks. + final route = _fullscreen; + if (route != null) { + WidgetsBinding.instance.addPostFrameCallback((_) { + if (route.isActive) route.navigator?.removeRoute(route); + }); + } + super.dispose(); + } + + /// Opens the full-screen view; it returns when the view closes. + Future enterFullscreen() async { + if (_fullscreen != null) return; + htmlZoom.value = 1; + final route = artifactFullscreenRoute( + cupertino: usesCupertinoHost(context), + builder: (_) => ArtifactFullscreenScope( + htmlZoom: htmlZoom, + child: ListenableBuilder( + listenable: Listenable.merge([_revision, htmlZoom]), + builder: (context, _) => buildFrame(context, fullscreen: true), + ), + ), + ); + _fullscreen = route; + await Navigator.of(context, rootNavigator: true).push(route); + _fullscreen = null; + } +} diff --git a/app/lib/features/artifacts/ui/artifact_gallery_page.dart b/app/lib/features/artifacts/ui/artifact_gallery_page.dart index a4e47d06..12ede23b 100644 --- a/app/lib/features/artifacts/ui/artifact_gallery_page.dart +++ b/app/lib/features/artifacts/ui/artifact_gallery_page.dart @@ -370,7 +370,7 @@ class _ArtifactGalleryPageState extends State { CupertinoDialogAction( isDefaultAction: true, onPressed: () => _open(resolved, dialogContext), - child: const Text('Open'), + child: const Text('Open in browser'), ), ], ), @@ -394,7 +394,7 @@ class _ArtifactGalleryPageState extends State { if (resolved != null) TextButton( onPressed: () => _open(resolved, dialogContext), - child: const Text('Open'), + child: const Text('Open in browser'), ), ], ), diff --git a/app/lib/features/artifacts/ui/artifact_html_links_web.dart b/app/lib/features/artifacts/ui/artifact_html_links_web.dart new file mode 100644 index 00000000..a7716d57 --- /dev/null +++ b/app/lib/features/artifacts/ui/artifact_html_links_web.dart @@ -0,0 +1,52 @@ +import 'dart:js_interop'; + +import 'package:web/web.dart' as web; + +import '../domain/artifact_html_links.dart'; + +/// The iframe's sandbox: no script, no same-origin access, no forms and no +/// navigation of the app, but a link may open a new tab that is itself +/// unsandboxed (an ordinary website, as if typed into the address bar). +const String artifactHtmlSandbox = + 'allow-popups allow-popups-to-escape-sandbox'; + +/// [html] with every link's behaviour fixed before the frame shows it +/// ([artifactHtmlLinkAction]): a `#place` stays in the frame and scrolls +/// there, an http(s)/mailto address opens a new tab (no opener, no +/// referrer), and every other link loses its `href`, so it is inert text. +/// A document's own `` is dropped: the frame is not at the artifact's +/// address, and a relative link must not reach the app's origin. +/// +/// The browser's `DOMParser` builds an inert document: nothing in it runs or +/// loads while it is rewritten. +String artifactHtmlWithInertLinks(String html) { + final doc = web.DOMParser().parseFromString(html.toJS, 'text/html'); + final bases = doc.querySelectorAll('base'); + for (var i = bases.length - 1; i >= 0; i--) { + (bases.item(i) as web.Element?)?.remove(); + } + final links = doc.querySelectorAll('a[href], area[href]'); + for (var i = 0; i < links.length; i++) { + final link = links.item(i) as web.Element?; + if (link == null) continue; + final href = link.getAttribute('href') ?? ''; + switch (artifactHtmlLinkAction(href)) { + case ArtifactHtmlLinkAction.inDocument: + // A srcdoc document's base is the app page, so a bare `#place` would + // navigate the frame to the app; the document's own address keeps it + // a same-document jump. + final hash = href.indexOf('#'); + final place = hash < 0 ? '' : href.substring(hash); + link.setAttribute('href', 'about:srcdoc$place'); + link.setAttribute('target', '_self'); + case ArtifactHtmlLinkAction.external: + link.setAttribute('target', '_blank'); + link.setAttribute('rel', 'noopener noreferrer'); + case ArtifactHtmlLinkAction.blocked: + link.removeAttribute('href'); + link.removeAttribute('target'); + } + } + final doctype = doc.doctype == null ? '' : ''; + return '$doctype${doc.documentElement?.outerHTML ?? ''}'; +} diff --git a/app/lib/features/artifacts/ui/artifact_html_view.dart b/app/lib/features/artifacts/ui/artifact_html_view.dart index b4d2abb7..0210379f 100644 --- a/app/lib/features/artifacts/ui/artifact_html_view.dart +++ b/app/lib/features/artifacts/ui/artifact_html_view.dart @@ -1,5 +1,6 @@ import 'package:flutter/widgets.dart'; +import 'artifact_fullscreen.dart'; import 'artifact_html_view_native.dart' if (dart.library.js_interop) 'artifact_html_view_web.dart' as impl; @@ -9,6 +10,12 @@ import 'artifact_html_view_native.dart' typedef ArtifactHtmlBuilder = Widget Function(BuildContext context, String html); +/// Whether the full-screen view offers zoom buttons for HTML: on the web the +/// sandboxed frame cannot be pinched (the page's viewport is not scalable and +/// a frame's document has no pinch zoom of its own), so the buttons scale the +/// live frame. The mobile web views pinch natively and offer none. +const bool artifactHtmlZoomButtons = impl.inertHtmlZoomButtons; + /// Renders a recorded HTML document inertly: on the web in an iframe whose /// empty `sandbox` blocks scripts, forms, popups and navigation; on Android and /// iOS in a web view with JavaScript disabled and navigation refused. The @@ -18,13 +25,22 @@ typedef ArtifactHtmlBuilder = /// the web the iframe is a platform view that takes every click over its /// area, even where a bar is drawn on top, which left the viewer's actions /// and pager unclickable. +/// +/// Inside an [ArtifactFullscreenScope] the document takes every gesture over +/// it and follows the scope's zoom; inline it shares them with the viewer's +/// page swipe. Widget defaultArtifactHtml(BuildContext context, String html) { final padding = MediaQuery.paddingOf(context); + final fullscreen = ArtifactFullscreenScope.maybeOf(context); return Padding( padding: EdgeInsets.only(top: padding.top, bottom: padding.bottom), child: SizedBox.expand( key: const Key('artifact-html-preview'), - child: impl.InertHtmlView(html: html), + child: impl.InertHtmlView( + html: html, + interactive: fullscreen != null, + zoom: fullscreen?.htmlZoom, + ), ), ); } diff --git a/app/lib/features/artifacts/ui/artifact_html_view_native.dart b/app/lib/features/artifacts/ui/artifact_html_view_native.dart index 40085c84..23275528 100644 --- a/app/lib/features/artifacts/ui/artifact_html_view_native.dart +++ b/app/lib/features/artifacts/ui/artifact_html_view_native.dart @@ -1,36 +1,105 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/gestures.dart'; import 'package:flutter/widgets.dart'; import 'package:webview_flutter/webview_flutter.dart'; +import '../domain/artifact_html_links.dart'; +import 'artifact_external_open.dart'; + +/// The mobile web views pinch-zoom natively (Android's built-in zoom, which +/// the plugin turns on with its display controls hidden; WKWebView's own), so +/// the full-screen view needs no zoom buttons. +const bool inertHtmlZoomButtons = false; + /// A web view with JavaScript disabled that refuses every navigation after the /// recorded document itself loads. +/// +/// A platform view only receives the pointers of gestures that its +/// [WebViewWidget.gestureRecognizers] win in Flutter's gesture arena. With +/// none it got a drag only after every other recognizer gave up, which the +/// viewer's page swipe never does before the finger lifts: the document never +/// scrolled or pinched. Inline it claims vertical drags (scrolling the +/// document) and leaves horizontal ones to the page swipe; [interactive] (the +/// full-screen view) claims every pointer at once, so scroll, pan and pinch +/// all reach the document. class InertHtmlView extends StatefulWidget { - const InertHtmlView({super.key, required this.html}); + const InertHtmlView({ + super.key, + required this.html, + this.interactive = false, + this.zoom, + this.openExternal = openArtifactExternally, + }); final String html; + /// Opens a link's http(s)/mailto address outside the artifact. + final ArtifactExternalOpen openExternal; + final bool interactive; + + /// Unused: the mobile web views pinch instead ([inertHtmlZoomButtons]). + final ValueListenable? zoom; + @override State createState() => _InertHtmlViewState(); } +final _inlineGestures = >{ + Factory(VerticalDragGestureRecognizer.new), +}; + +final _allGestures = >{ + Factory(EagerGestureRecognizer.new), +}; + class _InertHtmlViewState extends State { + /// Until the recorded document finishes loading, the view may load it (an + /// `about:` or `data:` document); afterwards only links are followed. + bool _loaded = false; + late final WebViewController _controller = WebViewController() ..setJavaScriptMode(JavaScriptMode.disabled) + ..enableZoom(true) ..setNavigationDelegate( NavigationDelegate( - onNavigationRequest: (request) => - request.url.startsWith('about:') || request.url.startsWith('data:') - ? NavigationDecision.navigate - : NavigationDecision.prevent, + onPageFinished: (_) => _loaded = true, + onNavigationRequest: _decide, ), ) ..loadHtmlString(widget.html); - @override - void didUpdateWidget(covariant InertHtmlView oldWidget) { - super.didUpdateWidget(oldWidget); - if (oldWidget.html != widget.html) _controller.loadHtmlString(widget.html); + /// Links: a place in the document scrolls there; an http(s)/mailto address + /// opens in the system's handler, never inside this view; anything else + /// (scripts, files, other schemes, relative paths) stays inert. + NavigationDecision _decide(NavigationRequest request) { + if (!_loaded && + (request.url.startsWith('about:') || request.url.startsWith('data:'))) { + return NavigationDecision.navigate; + } + switch (artifactHtmlLinkAction(request.url)) { + case ArtifactHtmlLinkAction.inDocument: + return NavigationDecision.navigate; + case ArtifactHtmlLinkAction.external: + final uri = Uri.tryParse(request.url.trim()); + if (uri != null) widget.openExternal(uri); + return NavigationDecision.prevent; + case ArtifactHtmlLinkAction.blocked: + return NavigationDecision.prevent; + } } @override - Widget build(BuildContext context) => WebViewWidget(controller: _controller); + void didUpdateWidget(covariant InertHtmlView oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.html != widget.html) { + _loaded = false; + _controller.loadHtmlString(widget.html); + } + } + + @override + Widget build(BuildContext context) => WebViewWidget( + controller: _controller, + gestureRecognizers: widget.interactive ? _allGestures : _inlineGestures, + ); } diff --git a/app/lib/features/artifacts/ui/artifact_html_view_web.dart b/app/lib/features/artifacts/ui/artifact_html_view_web.dart index ae8056b1..1cceabc2 100644 --- a/app/lib/features/artifacts/ui/artifact_html_view_web.dart +++ b/app/lib/features/artifacts/ui/artifact_html_view_web.dart @@ -1,28 +1,109 @@ import 'dart:js_interop'; +import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; import 'package:web/web.dart' as web; -/// A sandboxed iframe: the empty `sandbox` attribute applies every -/// restriction, so the document cannot run script or leave the frame. -class InertHtmlView extends StatelessWidget { - const InertHtmlView({super.key, required this.html}); +import 'artifact_html_links_web.dart'; + +/// The frame's document cannot be pinched: a pinch over it is a page zoom, +/// which the app's viewport (`user-scalable=no`) refuses, and the sandbox +/// keeps the document out of the app's reach. The full-screen view zooms +/// the live frame with buttons instead. +const bool inertHtmlZoomButtons = true; + +/// A sandboxed iframe ([artifactHtmlSandbox]): the document cannot run +/// script, reach the app's origin, submit forms or navigate the app. Its +/// links are fixed first ([artifactHtmlWithInertLinks]): a `#place` scrolls +/// the frame, a web or mail address opens a new tab, anything else is inert. +/// +/// The frame scrolls its document itself, by wheel, trackpad and touch. +/// [zoom] scales the live frame as a browser's page zoom would: the frame is +/// laid out 1/zoom as wide and tall and scaled back to fill its box, so the +/// document reflows and still scrolls inside it. In the app, Chrome returns +/// the frame's document to its top when a zoom step resizes it. +/// +/// While another route covers this one (a menu, a sheet) the frame stops +/// taking pointers: a platform view takes every click over its area, even +/// where Flutter draws that menu on top. +class InertHtmlView extends StatefulWidget { + const InertHtmlView({ + super.key, + required this.html, + this.interactive = false, + this.zoom, + }); final String html; + /// The full-screen view's frame. Pointers already reach the frame directly + /// on the web, inline as in full screen. + final bool interactive; + final ValueListenable? zoom; + @override - Widget build(BuildContext context) => HtmlElementView.fromTagName( - key: ValueKey(html.hashCode), - tagName: 'iframe', - onElementCreated: (element) { - final frame = element as web.HTMLIFrameElement; - frame.setAttribute('sandbox', ''); - frame.srcdoc = html.toJS; - frame.style - ..border = 'none' - ..width = '100%' - ..height = '100%' - ..backgroundColor = 'white'; - }, - ); + State createState() => _InertHtmlViewState(); +} + +class _InertHtmlViewState extends State { + web.HTMLIFrameElement? _frame; + bool _covered = false; + + @override + void initState() { + super.initState(); + widget.zoom?.addListener(_style); + } + + @override + void didUpdateWidget(covariant InertHtmlView oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.zoom != widget.zoom) { + oldWidget.zoom?.removeListener(_style); + widget.zoom?.addListener(_style); + _style(); + } + } + + @override + void dispose() { + widget.zoom?.removeListener(_style); + super.dispose(); + } + + void _style() { + final frame = _frame; + if (frame == null) return; + final zoom = widget.zoom?.value ?? 1; + final size = '${100 / zoom}%'; + frame.style + ..width = size + ..height = size + ..transformOrigin = '0 0' + ..transform = zoom == 1 ? '' : 'scale($zoom)' + ..pointerEvents = _covered ? 'none' : ''; + } + + @override + Widget build(BuildContext context) { + final covered = !(ModalRoute.of(context)?.isCurrent ?? true); + if (covered != _covered) { + _covered = covered; + _style(); + } + return HtmlElementView.fromTagName( + key: ValueKey(widget.html.hashCode), + tagName: 'iframe', + onElementCreated: (element) { + final frame = element as web.HTMLIFrameElement; + frame.setAttribute('sandbox', artifactHtmlSandbox); + frame.srcdoc = artifactHtmlWithInertLinks(widget.html).toJS; + frame.style + ..border = 'none' + ..backgroundColor = 'white'; + _frame = frame; + _style(); + }, + ); + } } diff --git a/app/lib/features/artifacts/ui/artifact_viewer_bar.dart b/app/lib/features/artifacts/ui/artifact_viewer_bar.dart new file mode 100644 index 00000000..57ffeefb --- /dev/null +++ b/app/lib/features/artifacts/ui/artifact_viewer_bar.dart @@ -0,0 +1,158 @@ +import 'dart:math' as math; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../../ui/adaptive/glass_bar.dart'; +import '../../../ui/adaptive/host.dart'; +import '../../../ui/navigation/chevron_pager.dart'; +import '../actions/artifact_file_actions.dart'; +import '../actions/artifact_file_bridge.dart'; +import '../domain/artifact_url.dart'; +import '../domain/gallery_navigation.dart'; +import 'artifact_viewer_fetch.dart'; + +const artifactPreviousKey = ValueKey('artifact-previous'); +const artifactNextKey = ValueKey('artifact-next'); +const artifactFullscreenKey = ValueKey('artifact-fullscreen'); + +/// The viewer's bottom controls: file transfer, Open, Copy link, Full screen +/// and the pager, in one glass bar above the bottom safe area. +/// +/// The inline viewer and the full-screen view draw this same bar; in full +/// screen the Full screen control reads Exit full screen and leaves it. +class ArtifactViewerBar extends StatelessWidget { + const ArtifactViewerBar({ + super.key, + required this.page, + required this.resolved, + required this.index, + required this.count, + required this.fetch, + required this.fileBridge, + required this.onOpen, + required this.onCopy, + required this.onPrevious, + required this.onNext, + this.fullscreen = false, + this.onFullscreen, + }); + + final PagedArtifact page; + final ResolvedArtifactUrl? resolved; + final int index; + final int count; + final ArtifactFetch fetch; + final ArtifactFileBridge fileBridge; + final VoidCallback onOpen; + final VoidCallback onCopy; + final VoidCallback onPrevious; + final VoidCallback onNext; + + /// Whether this bar is the full-screen view's. + final bool fullscreen; + + /// Enters (inline) or leaves (in full screen) the full-screen view; absent, + /// the control is not drawn. + final VoidCallback? onFullscreen; + + @override + Widget build(BuildContext context) { + final cupertino = usesCupertinoHost(context); + final actions = _actions(resolved, onFullscreen); + final pagerMin = count > 1 + ? 2 * GlassBarMetrics.controlExtent(cupertino) + : 0.0; + final pager = ChevronPager( + index: index, + count: count, + previousLabel: 'Previous artifact', + nextLabel: 'Next artifact', + previousKey: artifactPreviousKey, + nextKey: artifactNextKey, + onPrevious: pagedIndex(index, -1, count) != index ? onPrevious : null, + onNext: pagedIndex(index, 1, count) != index ? onNext : null, + ); + return SafeArea( + // The SafeArea already lifts the bar by the inherited bottom inset (a + // shell bar below the pane); adding that clearance again floated the + // bar far above it. + top: false, + minimum: const EdgeInsets.all(8), + // Without a pager the bar is as wide as its actions, so its last + // control keeps the same gap from the bar's end as the first. + child: Align( + alignment: AlignmentDirectional.centerStart, + heightFactor: 1, + child: GlassControlBar( + cupertino: cupertino, + child: LayoutBuilder( + builder: (context, constraints) => Row( + key: const Key('artifact-viewer-actions'), + mainAxisSize: count > 1 ? MainAxisSize.max : MainAxisSize.min, + children: [ + // On a narrow window or at large text the actions scroll + // sideways, so the chevrons beside them stay whole. + ConstrainedBox( + constraints: BoxConstraints( + maxWidth: math.max(0, constraints.maxWidth - pagerMin), + ), + child: SingleChildScrollView( + scrollDirection: Axis.horizontal, + child: Row( + mainAxisSize: MainAxisSize.min, + children: actions, + ), + ), + ), + if (count > 1) Expanded(child: pager), + ], + ), + ), + ), + ), + ); + } + + List _actions(ResolvedArtifactUrl? resolved, VoidCallback? toggle) => + [ + if (resolved != null && !resolved.external) + ArtifactFileActions( + key: ValueKey( + 'viewer-files-${page.group.stageKey}\u0000' + '${page.group.visit}\u0000${page.artifact.url}', + ), + artifact: page.artifact, + resolved: resolved, + fetch: fetch, + bridge: fileBridge, + compact: true, + ), + if (resolved != null) ...[ + ArtifactIconAction( + key: const Key('artifact-open'), + label: 'Open in browser', + icon: Icons.open_in_new, + cupertinoIcon: CupertinoIcons.arrow_up_right_square, + onPressed: onOpen, + ), + ArtifactIconAction( + key: const Key('artifact-copy-link'), + label: 'Copy link', + icon: Icons.link, + cupertinoIcon: CupertinoIcons.link, + onPressed: onCopy, + ), + ], + if (toggle != null) + ArtifactIconAction( + key: artifactFullscreenKey, + label: fullscreen ? 'Exit full screen' : 'Full screen', + icon: fullscreen ? Icons.fullscreen_exit : Icons.fullscreen, + cupertinoIcon: fullscreen + ? CupertinoIcons.fullscreen_exit + : CupertinoIcons.fullscreen, + onPressed: toggle, + ), + ]; +} diff --git a/app/lib/features/artifacts/ui/artifact_viewer_body.dart b/app/lib/features/artifacts/ui/artifact_viewer_body.dart index ed4b5420..48f6b9da 100644 --- a/app/lib/features/artifacts/ui/artifact_viewer_body.dart +++ b/app/lib/features/artifacts/ui/artifact_viewer_body.dart @@ -1,5 +1,3 @@ -import 'dart:async'; -import '../../../ui/navigation/chevron_pager.dart'; import 'dart:typed_data'; import 'package:flutter/cupertino.dart'; @@ -7,15 +5,19 @@ import 'package:flutter/material.dart'; import '../../../ui/adaptive/host.dart'; import '../../../ui/adaptive/floating_bar_layout.dart'; -import '../../../ui/adaptive/glass_bar.dart'; import '../actions/artifact_file_actions.dart'; import '../actions/artifact_file_bridge.dart'; import '../domain/artifact_preview.dart'; -import '../domain/artifact_url.dart'; import '../domain/gallery_navigation.dart'; +import 'artifact_html_view.dart'; +import 'artifact_viewer_bar.dart'; import 'artifact_viewer_content.dart'; import 'artifact_viewer_fetch.dart'; +export 'artifact_controls_overlay.dart'; +export 'artifact_viewer_bar.dart' + show artifactFullscreenKey, artifactNextKey, artifactPreviousKey; + /// One viewer page: the recorded facts, position, live status, preview surface, /// file transfer actions, external actions and paging controls. /// @@ -42,8 +44,20 @@ class ArtifactViewerBody extends StatelessWidget { this.error, this.status, this.onRetry, + this.htmlBuilder = defaultArtifactHtml, + this.fullscreen = false, + this.onFullscreen, }); + /// The inert HTML renderer; tests inject a plain stand-in. + final ArtifactHtmlBuilder htmlBuilder; + + /// Whether this body is the full-screen view's ([ArtifactViewerBar]). + final bool fullscreen; + + /// Enters or leaves the full-screen view. + final VoidCallback? onFullscreen; + /// Loads the current page's preview again after a failure. final VoidCallback? onRetry; @@ -79,6 +93,7 @@ class ArtifactViewerBody extends StatelessWidget { bytes: bytes, error: error, onRetry: onRetry, + htmlBuilder: htmlBuilder, ), ), ], @@ -116,233 +131,25 @@ class ArtifactViewerBody extends StatelessWidget { // no empty glass bar, and no bottom clearance reserved for one. if (resolved == null && count <= 1) return swipe; return FloatingBarOverlay( - // The SafeArea already lifts the bar by the inherited bottom inset (a - // shell bar below the pane); adding that clearance again floated the - // bar far above it. - bar: SafeArea( - top: false, - minimum: const EdgeInsets.all(8), - // Without a pager the bar is as wide as its actions, so its last - // control keeps the same gap from the bar's end as the first. - child: Align( - alignment: AlignmentDirectional.centerStart, - heightFactor: 1, - child: GlassControlBar( - cupertino: usesCupertinoHost(context), - child: Row( - key: const Key('artifact-viewer-actions'), - mainAxisSize: count > 1 ? MainAxisSize.max : MainAxisSize.min, - children: [ - if (resolved != null && !resolved.external) - ArtifactFileActions( - key: ValueKey( - 'viewer-files-${page.group.stageKey}\u0000' - '${page.group.visit}\u0000${page.artifact.url}', - ), - artifact: page.artifact, - resolved: resolved, - fetch: fetch, - bridge: fileBridge, - compact: true, - ), - if (resolved != null) - _Actions(resolved: resolved, onOpen: onOpen, onCopy: onCopy), - if (count > 1) - Expanded( - child: _Pager( - index: index, - count: count, - onPrevious: onPrevious, - onNext: onNext, - ), - ), - ], - ), - ), - ), + bar: ArtifactViewerBar( + page: page, + resolved: resolved, + index: index, + count: count, + fetch: fetch, + fileBridge: fileBridge, + onOpen: onOpen, + onCopy: onCopy, + onPrevious: onPrevious, + onNext: onNext, + fullscreen: fullscreen, + onFullscreen: onFullscreen, ), child: swipe, ); } } -/// Open, Copy and the file actions float over the top of the content: shown -/// when the page opens and whenever the content is touched, hidden again after -/// a few seconds without use, so nothing needs scrolling to reach them. -class ArtifactControlsOverlay extends StatefulWidget { - const ArtifactControlsOverlay({ - super.key, - required this.controls, - required this.child, - this.hideAfter = const Duration(seconds: 3), - }); - - final List controls; - final Widget child; - final Duration hideAfter; - - @override - State createState() => - _ArtifactControlsOverlayState(); -} - -class _ArtifactControlsOverlayState extends State { - bool _shown = true; - Timer? _hide; - - @override - void initState() { - super.initState(); - _schedule(); - } - - @override - void dispose() { - _hide?.cancel(); - super.dispose(); - } - - void _schedule() { - _hide?.cancel(); - _hide = Timer(widget.hideAfter, () { - if (mounted) setState(() => _shown = false); - }); - } - - void _show() { - if (!_shown) setState(() => _shown = true); - _schedule(); - } - - @override - Widget build(BuildContext context) { - final cupertino = usesCupertinoHost(context); - final surface = cupertino - ? CupertinoColors.systemBackground - .resolveFrom(context) - .withValues(alpha: 0.92) - : Theme.of( - context, - ).colorScheme.surfaceContainerHigh.withValues(alpha: 0.94); - return Stack( - children: [ - // Any touch on the content brings the controls back. - Positioned.fill( - child: Listener( - behavior: HitTestBehavior.translucent, - onPointerDown: (_) => _show(), - child: widget.child, - ), - ), - Positioned( - left: 8, - right: 8, - top: 8, - child: IgnorePointer( - ignoring: !_shown, - child: AnimatedOpacity( - key: const Key('artifact-viewer-controls'), - opacity: _shown ? 1 : 0, - duration: const Duration(milliseconds: 200), - child: Listener( - // Using a control keeps the bar up. - onPointerDown: (_) => _schedule(), - child: DecoratedBox( - decoration: BoxDecoration( - color: surface, - borderRadius: BorderRadius.circular(12), - boxShadow: const [ - BoxShadow(color: Color(0x22000000), blurRadius: 6), - ], - ), - child: Padding( - padding: const EdgeInsets.symmetric( - horizontal: 4, - vertical: 2, - ), - child: Wrap( - alignment: WrapAlignment.center, - crossAxisAlignment: WrapCrossAlignment.center, - children: widget.controls, - ), - ), - ), - ), - ), - ), - ), - ], - ); - } -} - -class _Actions extends StatelessWidget { - const _Actions({ - required this.resolved, - required this.onOpen, - required this.onCopy, - }); - - final ResolvedArtifactUrl resolved; - final VoidCallback onOpen; - final VoidCallback onCopy; - - @override - Widget build(BuildContext context) => Row( - mainAxisSize: MainAxisSize.min, - children: [ - ArtifactIconAction( - key: const Key('artifact-open'), - label: 'Open', - icon: Icons.open_in_new, - cupertinoIcon: CupertinoIcons.arrow_up_right_square, - onPressed: onOpen, - ), - ArtifactIconAction( - key: const Key('artifact-copy-link'), - label: 'Copy link', - icon: Icons.link, - cupertinoIcon: CupertinoIcons.link, - onPressed: onCopy, - ), - ], - ); -} - -const artifactPreviousKey = ValueKey('artifact-previous'); -const artifactNextKey = ValueKey('artifact-next'); - -class _Pager extends StatelessWidget { - const _Pager({ - required this.index, - required this.count, - required this.onPrevious, - required this.onNext, - }); - - final int index; - final int count; - final VoidCallback onPrevious; - final VoidCallback onNext; - - @override - Widget build(BuildContext context) { - if (count <= 1) return const SizedBox.shrink(); - final hasPrevious = pagedIndex(index, -1, count) != index; - final hasNext = pagedIndex(index, 1, count) != index; - return ChevronPager( - index: index, - count: count, - previousLabel: 'Previous artifact', - nextLabel: 'Next artifact', - previousKey: artifactPreviousKey, - nextKey: artifactNextKey, - onPrevious: hasPrevious ? onPrevious : null, - onNext: hasNext ? onNext : null, - ); - } -} - /// The viewer's top edge: where this artifact sits in the list, and an /// information button for its recorded facts. class ArtifactViewerInfoAction extends StatelessWidget { diff --git a/app/lib/features/artifacts/ui/artifact_viewer_page.dart b/app/lib/features/artifacts/ui/artifact_viewer_page.dart index 88d6c4d8..519224ef 100644 --- a/app/lib/features/artifacts/ui/artifact_viewer_page.dart +++ b/app/lib/features/artifacts/ui/artifact_viewer_page.dart @@ -1,13 +1,9 @@ -import '../../../ui/adaptive/segmented_bar.dart'; -import '../../../ui/theme/app_theme.dart'; -import '../../../ui/input/arrow_key_pager.dart'; import 'dart:convert'; -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -import '../../../ui/adaptive/host.dart'; +import '../../../ui/input/arrow_key_pager.dart'; import 'artifact_selection_facts.dart'; import '../actions/artifact_file_bridge.dart'; import '../domain/artifact_groups.dart'; @@ -16,9 +12,13 @@ import '../domain/artifact_preview_failure.dart'; import '../domain/artifact_url.dart'; import '../domain/gallery_navigation.dart'; import 'artifact_external_open.dart'; +import 'artifact_fullscreen.dart'; +import 'artifact_fullscreen_host.dart'; +import 'artifact_html_view.dart'; import 'artifact_viewer_body.dart'; import 'artifact_viewer_content.dart'; import 'artifact_viewer_fetch.dart'; +import 'artifact_viewer_scaffold.dart'; import 'artifact_visit_list.dart'; /// The full-screen viewer's title, matching the web client's wording. @@ -47,6 +47,7 @@ class ArtifactViewerPage extends StatefulWidget { this.markdownBuilder = defaultArtifactMarkdown, this.openExternal = openArtifactExternally, this.fileBridge = const ArtifactFileBridge(), + this.htmlBuilder = defaultArtifactHtml, }); final List groups; @@ -64,6 +65,9 @@ class ArtifactViewerPage extends StatefulWidget { final ArtifactExternalOpen openExternal; final ArtifactFileBridge fileBridge; + /// The inert HTML renderer; tests inject a plain stand-in. + final ArtifactHtmlBuilder htmlBuilder; + @override State createState() => _ArtifactViewerPageState(); } @@ -75,7 +79,8 @@ class _FetchedPreview { final Uint8List? bytes; } -class _ArtifactViewerPageState extends State { +class _ArtifactViewerPageState extends State + with ArtifactFullscreenHost { late final List _pages = pagedArtifacts( widget.groups, includeExternalDocuments: widget.includeExternalDocuments, @@ -117,47 +122,31 @@ class _ArtifactViewerPageState extends State { void _beginLoad({bool initial = false}) { final generation = ++_generation; final plan = _plan; + // The first load runs in initState, before there is anything to redraw. + void update(VoidCallback change) => initial ? change() : setState(change); if (plan == null || !plan.canFetch) { - void clear() { + update(() { _loading = false; _error = null; _fetched = null; - } - - if (initial) { - clear(); - } else { - setState(clear); - } + }); return; } final key = plan.resolved!.url; final cached = _cache[key]; if (cached != null) { - void apply() { + update(() { _loading = false; _error = null; _fetched = cached; - } - - if (initial) { - apply(); - } else { - setState(apply); - } + }); return; } - void start() { + update(() { _loading = true; _error = null; _fetched = null; - } - - if (initial) { - start(); - } else { - setState(start); - } + }); widget .fetch(plan.resolved!.uri) .then( @@ -226,7 +215,13 @@ class _ArtifactViewerPageState extends State { } @override - Widget build(BuildContext context) { + Widget build(BuildContext context) => buildFrame(context, fullscreen: false); + + /// The viewer's scaffold, inline or as the full-screen view: the same top + /// bar, preview and bottom actions; full screen adds HTML zoom on the web + /// and gives the preview every gesture over it. + @override + Widget buildFrame(BuildContext context, {required bool fullscreen}) { final page = _page; final plan = _plan; final title = page == null @@ -242,6 +237,11 @@ class _ArtifactViewerPageState extends State { final info = page == null || plan == null ? const [] : [ + if (fullscreen && + artifactHtmlZoomButtons && + plan.kind == ArtifactPreviewKind.html && + _fetched?.text != null) + ...artifactHtmlZoomActions(htmlZoom), ArtifactViewerInfoAction( facts: ArtifactSelectionFacts( group: page.group, @@ -251,7 +251,7 @@ class _ArtifactViewerPageState extends State { ), ), ]; - Widget body = page == null || plan == null + final body = page == null || plan == null ? const Center(child: Text('No previewable artifacts recorded.')) : ArtifactViewerBody( page: page, @@ -272,36 +272,26 @@ class _ArtifactViewerPageState extends State { onRetry: _beginLoad, onOpen: plan.resolved == null ? () {} : () => _open(plan.resolved!), onCopy: plan.resolved == null ? () {} : () => _copy(plan.resolved!), + htmlBuilder: widget.htmlBuilder, + fullscreen: fullscreen, + onFullscreen: fullscreen + ? () => Navigator.of(context).maybePop() + : enterFullscreen, ); - // Left/right arrows walk the same list as the Previous/Next buttons. - body = ArrowKeyPager( + // Left/right arrows walk the same list as the Previous/Next buttons, + // wherever focus is on the page, its top bar's controls included. + return ArrowKeyPager( onPrevious: () => _step(-1), onNext: () => _step(1), - // Escape closes the viewer, like the web's overlay. + // Escape closes the viewer (or leaves full screen), like the web's + // overlay. onEscape: () => Navigator.of(context).maybePop(), - child: body, - ); - if (usesCupertinoHost(context)) { - return CupertinoPageScaffold( - backgroundColor: AppColors.groupedBackground, - navigationBar: SegmentedBar( - cupertino: true, - title: title, - subtitle: subtitle, - actions: info, - ), - child: body, - ); - } - return Scaffold( - extendBodyBehindAppBar: true, - appBar: SegmentedBar( - cupertino: false, + child: ArtifactViewerScaffold( title: title, subtitle: subtitle, actions: info, + body: body, ), - body: body, ); } } diff --git a/app/lib/features/artifacts/ui/artifact_viewer_scaffold.dart b/app/lib/features/artifacts/ui/artifact_viewer_scaffold.dart new file mode 100644 index 00000000..e1a17a3b --- /dev/null +++ b/app/lib/features/artifacts/ui/artifact_viewer_scaffold.dart @@ -0,0 +1,42 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../../ui/adaptive/host.dart'; +import '../../../ui/adaptive/segmented_bar.dart'; +import '../../../ui/theme/app_theme.dart'; + +/// The viewer's page structure on either host: its [SegmentedBar] (the +/// platform's Back, or Close on the full-screen route) over [body]. +class ArtifactViewerScaffold extends StatelessWidget { + const ArtifactViewerScaffold({ + super.key, + required this.title, + required this.subtitle, + required this.actions, + required this.body, + }); + + final String title; + final String? subtitle; + final List actions; + final Widget body; + + @override + Widget build(BuildContext context) { + final cupertino = usesCupertinoHost(context); + final bar = SegmentedBar( + cupertino: cupertino, + title: title, + subtitle: subtitle, + actions: actions, + ); + if (cupertino) { + return CupertinoPageScaffold( + backgroundColor: AppColors.groupedBackground, + navigationBar: bar, + child: body, + ); + } + return Scaffold(extendBodyBehindAppBar: true, appBar: bar, body: body); + } +} diff --git a/app/lib/features/chat/conversation/chat_question_card.dart b/app/lib/features/chat/conversation/chat_question_card.dart index 16144cd4..baf28c38 100644 --- a/app/lib/features/chat/conversation/chat_question_card.dart +++ b/app/lib/features/chat/conversation/chat_question_card.dart @@ -215,11 +215,7 @@ class _ChatQuestionCardState extends State { 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 { diff --git a/app/lib/features/chat/conversation/chat_question_card_model.dart b/app/lib/features/chat/conversation/chat_question_card_model.dart index e8a07f39..409362c9 100644 --- a/app/lib/features/chat/conversation/chat_question_card_model.dart +++ b/app/lib/features/chat/conversation/chat_question_card_model.dart @@ -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 chatFoldQuestionFrame( Map 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 = {...current}; + final anchor = _text(raisedIn); next[id] = existing == null - ? Map.from(form) + ? { + ...Map.from(form), + if (anchor.isNotEmpty) chatQuestionRaisedIn: anchor, + } : {...existing, ...Map.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 = ''}) => diff --git a/app/lib/features/chat/conversation/chat_question_group.dart b/app/lib/features/chat/conversation/chat_question_group.dart new file mode 100644 index 00000000..7e12abb0 --- /dev/null +++ b/app/lib/features/chat/conversation/chat_question_group.dart @@ -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.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 groupSettledQuestions(List rows) { + final result = []; + final attached = >{}; + final loose = []; + 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 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 chatRowQuestions(JsonMap row) { + final questions = row['questions']; + if (questions is! List) return const []; + return [ + for (final form in questions) + if (form is Map) Map.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.from(form['answers'] as Map) + : {}; + 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('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 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, + ), + ], + ), + ), + ); + } +} diff --git a/app/lib/features/chat/conversation/chat_question_recorded_answers.dart b/app/lib/features/chat/conversation/chat_question_recorded_answers.dart index 2c0429bb..b060eca8 100644 --- a/app/lib/features/chat/conversation/chat_question_recorded_answers.dart +++ b/app/lib/features/chat/conversation/chat_question_recorded_answers.dart @@ -24,8 +24,8 @@ List 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 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 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 diff --git a/app/lib/features/chat/conversation/chat_thread_cards.dart b/app/lib/features/chat/conversation/chat_thread_cards.dart index 5fb6efd6..a2d790ac 100644 --- a/app/lib/features/chat/conversation/chat_thread_cards.dart +++ b/app/lib/features/chat/conversation/chat_thread_cards.dart @@ -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() diff --git a/app/lib/features/chat/conversation/chat_thread_controller.dart b/app/lib/features/chat/conversation/chat_thread_controller.dart index d70bdf4e..6c469ee4 100644 --- a/app/lib/features/chat/conversation/chat_thread_controller.dart +++ b/app/lib/features/chat/conversation/chat_thread_controller.dart @@ -369,7 +369,10 @@ class ChatThreadController extends ChangeNotifier { if (body['kind'] == 'form') { final form = body['form']; if (form is! Map) return; - _cards.foldForm(Map.from(form)); + _cards.foldForm( + Map.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. diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index 79e85230..2fefaaff 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -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 { 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 { } 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 { final chips = [ if (permissions is List && permissions.isNotEmpty) ChatPermissionsChip(permissions: permissions), + ...chatQuestionChips(row), ]; if (kind == 'permissions') { return Align( diff --git a/app/lib/features/chat/domain/chat_list_activity.dart b/app/lib/features/chat/domain/chat_list_activity.dart new file mode 100644 index 00000000..138393a2 --- /dev/null +++ b/app/lib/features/chat/domain/chat_list_activity.dart @@ -0,0 +1,43 @@ +import '../../../core/api_gateway.dart'; + +/// One chat's live activity as the server observes it: a turn in flight and +/// how many background tasks (shells, background commands, subagents) still +/// run. It rides on each `chat_sessions` row as `activity` and is pushed to +/// every connection as `chat_activity` on each change. [seq] orders one +/// chat's snapshots across the list and the pushes; the larger one is newer. +typedef ChatListActivity = ({ + String sessionId, + bool turnRunning, + int backgroundTasks, + int seq, +}); + +/// Reads an `activity` row field or a `chat_activity` body. [sessionId] names +/// the row when the body omits it. Anything malformed is null, so an older +/// server (which sends neither) leaves the list exactly as before. +ChatListActivity? chatListActivityOf(Object? value, {String? sessionId}) { + if (value is! Map) return null; + final named = value['session_id']; + final id = sessionId ?? (named is String ? named : ''); + if (id.isEmpty) return null; + final tasks = value['background_tasks']; + final seq = value['seq']; + return ( + sessionId: id, + turnRunning: value['turn_running'] == true, + backgroundTasks: tasks is num && tasks > 0 ? tasks.toInt() : 0, + seq: seq is num ? seq.toInt() : 0, + ); +} + +/// The row keys the list controller derives from the activity. +JsonMap chatListActivityRowFields(ChatListActivity activity) => { + 'running': activity.turnRunning, + 'background_tasks': activity.backgroundTasks, +}; + +/// Running background tasks of a list row; 0 when unknown. +int chatSessionBackgroundTasks(JsonMap session) { + final tasks = session['background_tasks']; + return tasks is int && tasks > 0 ? tasks : 0; +} diff --git a/app/lib/features/chat/ui/sessions/chat_sessions_controller.dart b/app/lib/features/chat/ui/sessions/chat_sessions_controller.dart index 1ac13ab6..3cc0b53c 100644 --- a/app/lib/features/chat/ui/sessions/chat_sessions_controller.dart +++ b/app/lib/features/chat/ui/sessions/chat_sessions_controller.dart @@ -8,6 +8,7 @@ import '../../../../core/servers/server_scope.dart'; import '../../../../core/transport/connection_state.dart'; import '../../../attention/runtime/attention_ack_sync.dart'; import '../../data/chat_repository.dart'; +import '../../domain/chat_list_activity.dart'; import '../../domain/chat_sessions.dart'; import 'chat_session_local.dart'; import 'session_attention.dart'; @@ -65,6 +66,9 @@ class ChatSessionsController extends ChangeNotifier { Future _persistTail = Future.value(); int _runningRevision = 0; final Map _runningFrames = {}; + // The newest server activity applied per chat (list row or push), so an + // older snapshot that arrives later never replaces it. + final Map _activity = {}; StreamSubscription? _frameSubscription; String? activeSessionId; @@ -75,6 +79,9 @@ class ChatSessionsController extends ChangeNotifier { void _onFrame(JsonMap frame) { if (frame['type'] == 'chat_session_changed') return _sessionChanged(frame); + if (frame['type'] == 'chat_activity') { + return _activityChanged(frame['chat_activity']); + } final effect = chatSessionFrameEffect(frame); if (effect == null) return; final id = effect.sessionId; @@ -130,6 +137,58 @@ class ChatSessionsController extends ChangeNotifier { if (found) _notify(); } + /// The server pushed a chat's turn state and background-task count. It + /// reaches every connection, so every listed chat's spinner and + /// background-task badge follow it, not only the chat open here. A push + /// older than the snapshot already applied is ignored. + void _activityChanged(Object? body) { + final activity = chatListActivityOf(body); + if (activity == null || _disposed) return; + final id = activity.sessionId; + if (activity.seq <= (_activity[id]?.seq ?? 0)) return; + _activity[id] = activity; + _runningFrames[id] = ( + revision: ++_runningRevision, + running: activity.turnRunning, + ); + final fields = chatListActivityRowFields(activity); + var changed = false; + _sessions = [ + for (final row in _sessions) + if ('${row['id'] ?? ''}' == id && + (row['running'] != fields['running'] || + row['background_tasks'] != fields['background_tasks'])) + () { + changed = true; + return {...row, ...fields}; + }() + else + row, + ]; + if (changed) _notify(); + } + + /// A listed row with the newest activity known for it: the row's own + /// `activity` unless a newer push was already applied, then a turn frame + /// this connection saw during the read. An older server sends no + /// `activity`, so only the turn frames apply, as before. + JsonMap _withActivity(JsonMap row, int runningRevision) { + final id = '${row['id'] ?? ''}'; + var next = row; + final listed = chatListActivityOf(row['activity'], sessionId: id); + if (listed != null) { + final known = _activity[id]; + final activity = known != null && known.seq > listed.seq ? known : listed; + _activity[id] = activity; + next = {...row, ...chatListActivityRowFields(activity)}; + } + final frame = _runningFrames[id]; + if ((frame?.revision ?? 0) > runningRevision) { + next = {...next, 'running': frame!.running}; + } + return next; + } + void _bindAckSync(AttentionAckSync? sync) { if (identical(sync, _ackSync)) return; _ackSync?.removeListener(_ackChanged); @@ -246,6 +305,7 @@ class ChatSessionsController extends ChangeNotifier { if (identical(next.gateway, repository.gateway)) return; repository = next; _runningFrames.clear(); + _activity.clear(); _listenFrames(); _bindAckSync(attentionAckSyncOf(next.gateway) ?? _ackSync); refresh(); @@ -265,12 +325,7 @@ class ChatSessionsController extends ChangeNotifier { final sessions = await repository.sessions(); if (generation != _generation) return; _sessions = [ - for (final row in sessions) - if ((_runningFrames['${row['id'] ?? ''}']?.revision ?? 0) > - runningRevision) - {...row, 'running': _runningFrames['${row['id'] ?? ''}']!.running} - else - row, + for (final row in sessions) _withActivity(row, runningRevision), ]; _seedLocal(); _loading = false; diff --git a/app/lib/features/chat/ui/sessions/session_activity_badge.dart b/app/lib/features/chat/ui/sessions/session_activity_badge.dart new file mode 100644 index 00000000..75a51cf1 --- /dev/null +++ b/app/lib/features/chat/ui/sessions/session_activity_badge.dart @@ -0,0 +1,93 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +/// The corner badge a chat's avatar wears for its live activity: the +/// platform's activity indicator while a turn runs; otherwise, while +/// background tasks still run, a quiet muted glyph (never a second spinner). +/// Null when the chat is idle. +Widget? chatSessionActivityBadge({ + required bool cupertino, + required bool running, + required int backgroundTasks, +}) { + if (running) { + return Semantics( + key: const Key('chat-row-working'), + label: 'Chat is working', + child: _WorkingBadge(cupertino: cupertino), + ); + } + if (backgroundTasks <= 0) return null; + final label = 'Background tasks running ($backgroundTasks)'; + // Hover shows the tooltip on a pointer device; a touch long press stays the + // row's own gesture. + return Tooltip( + key: const Key('chat-row-background'), + message: label, + triggerMode: TooltipTriggerMode.manual, + excludeFromSemantics: true, + child: Semantics( + label: label, + child: _BackgroundBadge(cupertino: cupertino), + ), + ); +} + +/// The badge's own disc in the surface colour, so it reads over any avatar. +class _BadgeDisc extends StatelessWidget { + const _BadgeDisc({required this.cupertino, required this.child}); + + final bool cupertino; + final Widget child; + + @override + Widget build(BuildContext context) { + final background = cupertino + ? CupertinoColors.systemBackground.resolveFrom(context) + : Theme.of(context).colorScheme.surface; + return DecoratedBox( + decoration: BoxDecoration(color: background, shape: BoxShape.circle), + child: Padding(padding: const EdgeInsets.all(2), child: child), + ); + } +} + +class _WorkingBadge extends StatelessWidget { + const _WorkingBadge({required this.cupertino}); + + final bool cupertino; + + @override + Widget build(BuildContext context) { + return _BadgeDisc( + cupertino: cupertino, + child: cupertino + ? const CupertinoActivityIndicator(radius: 6) + : const SizedBox.square( + dimension: 12, + child: CircularProgressIndicator(strokeWidth: 2), + ), + ); + } +} + +class _BackgroundBadge extends StatelessWidget { + const _BackgroundBadge({required this.cupertino}); + + final bool cupertino; + + @override + Widget build(BuildContext context) { + final color = cupertino + ? CupertinoColors.secondaryLabel.resolveFrom(context) + : Theme.of(context).colorScheme.onSurfaceVariant; + return _BadgeDisc( + cupertino: cupertino, + child: Icon( + cupertino ? CupertinoIcons.ellipsis_circle_fill : Icons.pending, + size: 12, + color: color, + ), + ); + } +} diff --git a/app/lib/features/chat/ui/sessions/session_attention.dart b/app/lib/features/chat/ui/sessions/session_attention.dart index 815a1f30..457407f0 100644 --- a/app/lib/features/chat/ui/sessions/session_attention.dart +++ b/app/lib/features/chat/ui/sessions/session_attention.dart @@ -18,23 +18,20 @@ ChatSessionFrameEffect? chatSessionFrameEffect(JsonMap frame) { if (event is! Map) return null; id = event['session_id'] is String ? event['session_id'] as String : null; final kind = event['kind']; - // A badge means content to read, not transport/activity churn: assistant - // text, another client's user message, a returned tool result, or a pending - // form. Reasoning, starts, steps, shells, compaction and usage are not unread. + // A badge means the agent said something to read or is waiting on the + // operator: visible assistant reply text or a pending form. Tool calls and + // their results, harness notices and agent reports (`message_kind`), the + // operator's own prompts from any device, reasoning, steps, shells, + // compaction and usage are turn activity, never unread. if (kind == 'text') { - attention = '${event['delta'] ?? ''}'.isNotEmpty; - } else if (kind == 'user') { - attention = true; + attention = + '${event['delta'] ?? ''}'.trim().isNotEmpty && + '${event['message_kind'] ?? ''}'.isEmpty; } else if (kind == 'tool') { final part = event['part']; final state = part is Map ? part['state'] : null; if (state is Map) { final status = state['status']; - attention = - (status == 'completed' || status == 'error') && - ((state['content'] is List && - (state['content'] as List).isNotEmpty) || - state['error'] != null); if (status == 'running' || status == 'streaming') running = true; } } else if (kind == 'form') { diff --git a/app/lib/features/chat/ui/sessions/session_row_content.dart b/app/lib/features/chat/ui/sessions/session_row_content.dart index 4bfdea62..4e25f709 100644 --- a/app/lib/features/chat/ui/sessions/session_row_content.dart +++ b/app/lib/features/chat/ui/sessions/session_row_content.dart @@ -6,10 +6,12 @@ import '../../../../ui/layout/app_list_avatar.dart'; import '../../../../ui/layout/app_list_row.dart'; import '../../../../ui/text/relative_time_label.dart'; import '../../domain/chat_colors.dart'; +import '../../domain/chat_list_activity.dart'; import '../../domain/chat_sessions.dart'; import 'chat_adaptive.dart'; import 'chat_color_palette.dart'; import 'session_actions.dart'; +import 'session_activity_badge.dart'; import 'session_harness_icon.dart'; import 'session_row_style.dart'; @@ -68,6 +70,9 @@ class ChatSessionRow extends StatelessWidget { leading: ChatSessionAvatar( session: session, running: session['running'] == true && !unseen, + backgroundTasks: session['running'] == true + ? 0 + : chatSessionBackgroundTasks(session), selected: selected, ), // The star rides in the title's own text, so a narrow row ellipsises @@ -149,17 +154,20 @@ class _Star extends StatelessWidget { /// The chat's harness mark in a filled circle, like a contact avatar. A /// running chat carries a small activity badge on the avatar's corner instead -/// of a line of its own; a selected chat's avatar takes the accent fill. +/// of a line of its own, and an idle chat whose background tasks still run a +/// quieter one; a selected chat's avatar takes the accent fill. class ChatSessionAvatar extends StatelessWidget { const ChatSessionAvatar({ super.key, required this.session, this.running = false, + this.backgroundTasks = 0, this.selected = false, }); final JsonMap session; final bool running; + final int backgroundTasks; final bool selected; @override @@ -192,39 +200,12 @@ class ChatSessionAvatar extends StatelessWidget { return AppListAvatar( foreground: mark, background: fill, - badge: running - ? Semantics( - key: const Key('chat-row-working'), - label: 'Chat is working', - child: _WorkingBadge(cupertino: cupertino), - ) - : null, + badge: chatSessionActivityBadge( + cupertino: cupertino, + running: running, + backgroundTasks: backgroundTasks, + ), child: SessionHarnessIcon(session: session, color: mark), ); } } - -class _WorkingBadge extends StatelessWidget { - const _WorkingBadge({required this.cupertino}); - - final bool cupertino; - - @override - Widget build(BuildContext context) { - final background = cupertino - ? CupertinoColors.systemBackground.resolveFrom(context) - : Theme.of(context).colorScheme.surface; - return DecoratedBox( - decoration: BoxDecoration(color: background, shape: BoxShape.circle), - child: Padding( - padding: const EdgeInsets.all(2), - child: cupertino - ? const CupertinoActivityIndicator(radius: 6) - : const SizedBox.square( - dimension: 12, - child: CircularProgressIndicator(strokeWidth: 2), - ), - ), - ); - } -} diff --git a/app/lib/features/chat/ui/transcript/chat_link_spans.dart b/app/lib/features/chat/ui/transcript/chat_link_spans.dart index 3faf678e..eb29b874 100644 --- a/app/lib/features/chat/ui/transcript/chat_link_spans.dart +++ b/app/lib/features/chat/ui/transcript/chat_link_spans.dart @@ -7,6 +7,25 @@ import '../../../../ui/adaptive/host.dart'; import 'chat_link_browser_menu.dart'; import 'chat_links.dart'; +/// A link's tap recognizer. A primary mouse press on a link belongs to the +/// link, as a press on a web page's anchor does: otherwise the selection area +/// claims the press as a selection drag once the pointer moves +/// [kPrecisePointerPanSlop] (2 px), so an ordinary click with a little hand or +/// trackpad movement opened nothing. The click still cancels past the tap +/// slop, and a selection drag that starts on plain text still crosses links. +/// Touch, stylus and secondary presses keep competing in the arena, so a long +/// press still selects and offers Copy link. +class _LinkTapRecognizer extends TapGestureRecognizer { + @override + void addAllowedPointer(PointerDownEvent event) { + super.addAllowedPointer(event); + if (event.kind == PointerDeviceKind.mouse && + event.buttons == kPrimaryMouseButton) { + resolve(GestureDisposition.accepted); + } + } +} + /// The link spans of one selectable text block, and the selection area that /// hosts them. A tap opens the link. A long press (or a native right-click) /// shows the selection menu with "Copy link" added; on the web, where the @@ -24,7 +43,7 @@ class ChatLinkSpans { static bool? debugOwnsSecondaryClick; final VoidCallback onChanged; - final Map _recognizers = {}; + final Map _recognizers = {}; final ContextMenuController _linkMenu = ContextMenuController(); String? _hovered; String? _pressed; @@ -45,7 +64,7 @@ class ChatLinkSpans { final uri = chatLinkUri(target); if (uri == null) return TextSpan(children: label); final url = uri.toString(); - final recognizer = _recognizers.putIfAbsent(url, TapGestureRecognizer.new) + final recognizer = _recognizers.putIfAbsent(url, _LinkTapRecognizer.new) ..onTapDown = (_) { _pressed = url; } diff --git a/app/lib/features/chat/ui/transcript/chat_turn_chip.dart b/app/lib/features/chat/ui/transcript/chat_turn_chip.dart index 8f2b5b13..70d43932 100644 --- a/app/lib/features/chat/ui/transcript/chat_turn_chip.dart +++ b/app/lib/features/chat/ui/transcript/chat_turn_chip.dart @@ -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 _open(BuildContext context, bool cupertino) { if (cupertino) { return showCupertinoModalPopup( diff --git a/app/lib/ui/layout/app_list_row.dart b/app/lib/ui/layout/app_list_row.dart index 879ff312..75656a04 100644 --- a/app/lib/ui/layout/app_list_row.dart +++ b/app/lib/ui/layout/app_list_row.dart @@ -17,9 +17,15 @@ const double kAppListRowMinimalWidth = 160; /// The rows' horizontal inset and the gap after the avatar. const double kAppListRowInset = 16; -const double _avatarGap = 12; +const double _avatarGap = 14; const double _compactAvatarGap = 8; +/// The rows' vertical breathing room: the padding above and below the +/// content and the gap between the title and the lines under it. Rows stay +/// content-driven; these only space them out so they are easier to hit. +const double kAppListRowVerticalPadding = 13; +const double _lineGap = 4; + /// Keys on a row's highlight surface and inset divider, for tests. const appListRowSurfaceKey = ValueKey('app-list-row-surface'); const appListRowDividerKey = ValueKey('app-list-row-divider'); @@ -135,8 +141,8 @@ class AppListRow extends StatelessWidget { padding: EdgeInsetsDirectional.only( start: start, end: showMenu ? 4 : kAppListRowInset, - top: 10, - bottom: 10, + top: kAppListRowVerticalPadding, + bottom: kAppListRowVerticalPadding, ), child: Row( children: [ @@ -285,7 +291,7 @@ class AppListRow extends StatelessWidget { return Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, - children: [titleLine, const SizedBox(height: 2), timeLine], + children: [titleLine, const SizedBox(height: _lineGap), timeLine], ); } return Column( @@ -293,7 +299,7 @@ class AppListRow extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.stretch, children: [ titleLine, - const SizedBox(height: 2), + const SizedBox(height: _lineGap), Row( children: [ if (subtitleLeading != null) diff --git a/app/notes/app-list-row.md b/app/notes/app-list-row.md index 75a1b92f..94614123 100644 --- a/app/notes/app-list-row.md +++ b/app/notes/app-list-row.md @@ -31,8 +31,10 @@ component, on both hosts. given. Only below `kAppListRowMinimalWidth` (160, narrower than any list pane) does it drop the avatar and menu so the text never overflows. (`keepMenuWhenCompact` is gone: every row keeps its menu.) - - No fixed heights. Padding is 16 horizontal and 10 vertical. At 2x text in - a 320px row everything ellipsises without overflowing. + - No fixed heights. Padding is 16 horizontal and 13 vertical + (`kAppListRowVerticalPadding`), with a 4px gap under the title and a 14px + gap after the avatar (8 when compact). At 2x text in a 320px row + everything ellipsises without overflowing. - Test keys: `appListRowSurfaceKey` and `appListRowDividerKey`. - `app_list_avatar.dart` - `AppListAvatar` is a 40px circle (the `CircleAvatar` default) holding an @@ -130,3 +132,13 @@ directory). The sources were: The captures were taken at 560 and 1200, in light and dark, on Material and Cupertino. There was no device run, so Android and iOS are unverified. + +## Roomier rows (2026-11-03) + +The user found the chat, run and pipeline lists cramped. In `AppListRow` only: +vertical padding 10 -> 13 per side, title-to-subtitle gap 2 -> 4, avatar-to-text +gap 12 -> 14 (compact stays 8). A two-line Material row goes from about 69 to +75 logical px; text sizes and the content-driven layout are unchanged, and the +divider still starts at the text column. Narrow (compact) list panes still fit. +Captures (Material light, 560x1000 and 1400x900, plus a narrowed pane and +Cupertino chat) are in `/tmp/rowsize/{before,after}/`. diff --git a/app/notes/artifact-fullscreen.md b/app/notes/artifact-fullscreen.md new file mode 100644 index 00000000..8ff62a65 --- /dev/null +++ b/app/notes/artifact-fullscreen.md @@ -0,0 +1,92 @@ +# Artifact viewer: full screen, HTML scroll/pan/zoom, links + +## What changed + +- The viewer's bottom bar has a **Full screen** control (Material + `Icons.fullscreen` / `CupertinoIcons.fullscreen`). It pushes a full-screen + dialog route on the root navigator, covering the shell's tab bar and list + pane. The route draws the same viewer frame (`ArtifactFullscreenHost` + mixin + `buildFrame`), so the same top bar, preview and bottom bar + (`ArtifactViewerBar`, shared, not duplicated) appear, and paging, loading + and status follow the page. Exit: the bar's **Exit full screen**, the + route's Close (Material X / Cupertino Cancel), Escape, system back. +- Full screen renders the bytes the viewer already fetched with the app's + session. It never calls `openArtifactExternally`/`launchUrl` for the + artifact. The iPad report (HTML opening in Chrome, then a sign-in prompt) + came from the viewer's existing **Open** action + (`openArtifactExternally` → `launchUrl(externalApplication)`): it is now + labelled **Open in browser** in the viewer bar and the gallery dialog. +- Images, text, tables and video keep their own behaviour in full screen. + +## Why HTML would not scroll or pinch + +- **Android/iOS app:** a platform view only gets pointers for gestures its + `gestureRecognizers` win in Flutter's gesture arena. `WebViewWidget` had + none, and the viewer's page-swipe `GestureDetector` (horizontal drag) + stays in the arena until the finger lifts, so the web view got a drag only + after release: no live scroll, no pinch. Now inline the web view claims + vertical drags (the document scrolls, a horizontal swipe still pages), and + full screen claims every pointer (`EagerGestureRecognizer`): scroll, pan + and pinch all reach the web view. Zoom is on (`enableZoom(true)`; the + Android plugin already sets built-in zoom with hidden controls and ignores + viewport meta, so no viewport injection is needed). +- **Web:** wheel, trackpad and touch scrolling inside the iframe already + worked (captured below). Pinch cannot work: a pinch over a frame is a page + zoom, which the Flutter page's viewport (`user-scalable=no`) refuses, and + the sandbox (no `allow-same-origin`) keeps the document out of the app's + reach. Full screen therefore adds **Zoom out / Actual size / Zoom in** + (50%–400%) that scale the live frame like a browser page zoom (frame laid + out at 1/zoom and scaled back), so the document reflows and keeps + scrolling. Not a screenshot. + +## Links + +- Decision: `lib/features/artifacts/domain/artifact_html_links.dart` + (`artifactHtmlLinkAction`): `#place` / `about:blank|srcdoc#place` stay in + the document; `http(s)` with a host and `mailto` open outside; everything + else (`javascript:`, `file:`, `data:`, `tel:`, `intent:`, relative paths, + `//host`) is inert. +- Web: the iframe sandbox is `allow-popups allow-popups-to-escape-sandbox` + (still no scripts, no same-origin, no forms, no top navigation). Before + showing, the document is rewritten through the browser's inert + `DOMParser`: `` removed, external links get `target=_blank + rel="noopener noreferrer"`, in-document links become `about:srcdoc#place` + with `target=_self` (a bare `#place` resolves against the app page in a + srcdoc frame and blanked the frame), blocked links lose `href`. +- Native: the `NavigationDelegate` allows the initial `about:`/`data:` load + until the page finishes, then applies the same decision; external links go + to the system handler, nothing else navigates the view. +- Relative links cannot resolve (the document is loaded from a string with + no address) and stay inert. A link to the Pipelines server itself opens in + the browser without the app's session. +- Applies inline and in full screen. + +## Checks + +- `flutter analyze`: no issues. `tool/memwatch/full.sh test`: 2778 pass; + `test/features/artifacts`: 240 pass (new `ui/artifact_fullscreen_test.dart`, + `domain/artifact_html_links_test.dart`). +- Real Chrome (capture kit, `--params html=1`, fixture + `tool/capture/fixtures/release-dashboard.html`), captures in + `/tmp/afs/final/`: + - `desktop/` 1280x900 Material: inline, full screen, wheel scroll, zoom + 125%, Details sheet over the frame, Escape back to the inline viewer. + - `phone/` 560x1000 touch: full screen, swipe up scrolls, swipe left pans + the wide table; a pinch does not zoom (web limit above). + - `ipad/` 820x1180 Cupertino touch: full screen, swipe scrolls, Exit full + screen returns. + - `links/`: "Jump to section 5" scrolls the frame; example.com opens a new + tab (`05-tabs-after-external.json`) with the frame unchanged; script and + relative links are plain text and open nothing. +- The capture kit gained `--touch true`, `swipe=`, `pinch=` and `tabs=`. + +## Not verified / limits + +- Android and iOS behaviour (web view gestures, pinch, link hand-off) is + **unverified**: no device or simulator was used. +- Web: no pinch zoom (buttons instead). A zoom step returns the document to + its top: Chrome resets the frame's scroll when the app resizes it (a plain + page keeps it; wrapping the frame in a box did not help). +- Web: Escape while focus is inside the frame goes to the frame; use the + close button or click outside first. +- Narrow windows: the bar's actions scroll sideways so the pager stays whole. diff --git a/app/notes/chat-links.md b/app/notes/chat-links.md index 6dbb617f..00ee2932 100644 --- a/app/notes/chat-links.md +++ b/app/notes/chat-links.md @@ -13,6 +13,16 @@ links outside backtick code become links. Text without a link stays `SelectableText`. - Inline code and fenced code are never linkified. +- Click: a primary mouse press on a link belongs to the link (its recognizer + accepts on pointer down), like a press on a web page's anchor. Before, the + `SelectionArea` claimed the press as a selection drag once the pointer moved + `kPrecisePointerPanSlop` (2 px), so in the browser a click with a little + hand or trackpad drift opened nothing (headless Chrome: 0-2 px opened, 3, 5 + and 8 px did not). The click still cancels past the tap slop (18 px); a + selection drag that starts on plain text still crosses links; a drag that + starts on a link selects nothing (as a browser drags an anchor). Touch, + stylus and right-click presses keep competing in the arena, so a long press + still selects and offers Copy link. - Style: host primary colour (`CupertinoTheme.primaryColor`), underline; hover thickens the underline (paint-only, selection survives) with a click cursor. - Copy link: long press adds it to the selection toolbar. On native desktop the @@ -23,6 +33,17 @@ rebuilds without its platform menu wrapper, remounts its subtree, throws in `StaticSelectionContainerDelegate.hasSize` and drops the selection. +Browser check: `tool/capture/examples/chat-link-click.json` drifts a click +5 px on a link and drags a selection across links, and expects exactly one +`window.open`. Checked in headless Chrome (Nov 2026), desktop and touch +emulation (iPhone/Android/iPad user agents): tap and click open through +`window.open` with user activation active; right-click Copy link (desktop) +and long-press Copy link (touch) write the URL to `navigator.clipboard` and it +reads back. Not verified: real iOS/macOS Safari (popup and clipboard user +activation rules differ) and real devices. In headless CPU rendering the +text-selection toolbars (stock Copy/Select all as well as Copy link) draw no +surface; popup menus do, so this looks like the renderer, not the links. + Browser capture: `?links=1` on the chat fixture (`tool/perf/chat_scroll_fixture.dart`). With the capture kit's semantics tree on, a CDP click lands on link semantics nodes whose rects are the bounding box of a wrapped link, so verify clicks with @@ -30,3 +51,6 @@ pointer events dispatched to `flutter-view`. Observed, not caused by links: dragging a selection over text below a fenced code block in the same answer also highlights the code block. + +## 2026-11-03 operator check +The operator confirmed "Copy link" works in their browser; only the drifting mouse click was broken. diff --git a/app/notes/chat-question-collapse.md b/app/notes/chat-question-collapse.md new file mode 100644 index 00000000..2055c373 --- /dev/null +++ b/app/notes/chat-question-collapse.md @@ -0,0 +1,46 @@ +# Answered questions collapse into their turn like tool calls + +User report: after replying to a harness question (OpenCode question tool, +Claude Code AskUserQuestion, Codex elicitation) the question card stays at the +bottom of the chat. Follow-up: it should collapse like tool calls. + +## Root cause + +Not a missed server event. The reply's ack already marked the form resolved +locally (`ChatThreadCards.answerQuestion`), and OpenCode/Codex resolution +frames folded correctly. But every retained form, settled or not, was still +rendered as a full card in the transcript's `trailing` rows +(`chatCardRows` -> `transcriptRows(trailing:)`), so an answered question stayed +as an "Answer recorded." card pinned after the last message. Narrow and wide +used the same rows; on a phone the card simply covers more of the screen. + +## Behavior now + +- Pending questions, and ones whose answer was refused (with the server's + reason), stay full interactive cards. +- Once the reply is acknowledged (Claude Code: the ack is the only prompt + signal; the server's readback resolution comes later) or the server sends a + resolved/cancelled `form` frame (OpenCode `form.replied`/`form.cancelled`, + Codex), the card leaves the card list and becomes a `ChatTurnChip` in the + footer of the agent turn it arrived during, beside `N tools`: + `Which database? · Postgres` (`· Expired`, `· Closed`, `· +N more`). It opens + a native sheet with the title, status and full recorded answers. + `groupSettledQuestions` in `lib/features/chat/conversation/chat_question_group.dart`. +- Forms carry no turn id, so the client stamps the agent message streaming + when the first frame arrives (`raised_in_message_id`, client-only). A question + with no turn in view shares one loose `questions` row where it stood. +- Forms are not part of stored history and the server replays only pending + ones, so after a reload an answered question is gone (its tool call remains + in the turn's tools chip). + +## Evidence + +- Tests: `test/features/chat/conversation/chat_question_group_test.dart` + (grouping, summary, narrow 390x844 and wide 1280x900 ack collapse, refusal + keeps the card, OpenCode/Codex resolution frame, cancel, ack plus + form.replied, Cupertino, stale pending replay, remount). The full + `test/features/chat` suite passes; `flutter analyze` is clean. +- Browser: fixture option `?question=claude|opencode|fail` + (`tool/perf/chat_question_fixture.dart`). Before/after captures at 560x1000 + and 1280x800 under `build/cq/out/`. +- Not deployed, no device verification. diff --git a/app/notes/chat-unread-tool-calls.md b/app/notes/chat-unread-tool-calls.md new file mode 100644 index 00000000..a1687606 --- /dev/null +++ b/app/notes/chat-unread-tool-calls.md @@ -0,0 +1,21 @@ +# Chat list: tool calls no longer mark a chat New + +Follow-up to 985cc355 ("Fix chat session unread attention state"). + +Root cause: that fix made unread purely frame-driven +(`lib/features/chat/ui/sessions/session_attention.dart`), but it still listed +a returned tool result (`kind: tool`, status `completed`/`error` with content or +error) and any `kind: user` frame as unread content. Every finished tool call in +a background chat therefore raised the New badge even when the agent had said +nothing yet. Text frames carrying a `message_kind` (harness `notice`, subagent +`agent_report`, often emitted around tool calls) and whitespace-only deltas also +counted. + +Now only visible assistant reply text (`kind: text`, non-blank delta, no +`message_kind`, matching the server's own `finalReplyTextLocked`), a pending +form, or an unresolved permission request marks a chat New. Tool frames only +drive the working indicator. The open chat stays excluded (`activeSessionId`). + +Tests: `test/features/chat/ui/sessions/chat_sessions_live_new_test.dart` +(tool call case, open-chat case), `session_attention_test.dart` (frame table). +Not browser- or device-verified. diff --git a/app/notes/session-list-background-tasks.md b/app/notes/session-list-background-tasks.md new file mode 100644 index 00000000..ef41a1f6 --- /dev/null +++ b/app/notes/session-list-background-tasks.md @@ -0,0 +1,58 @@ +# Session list: turn and background-task indicators + +Request: besides the turn spinner, show a quieter indicator on a chat list row +when the turn is idle but the session still runs background work (shells, +Claude Code background Bash and Agent/Task subagents, Codex command executions, +OpenCode shells). Also make the spinner reliable for every listed chat, not +only chats this connection opened. + +Implemented, with an approved server change. + +## Server +- `protocol.ChatSession.Activity` (`activity`: `turn_running`, + `background_tasks`, `seq`) is set on every `chat_sessions` row from the hub's + own state (`internal/server/chat_activity.go`, `handleChatSessions`). +- `chat_activity` is pushed to every connection (through `chatSessionChanges`) + when a session's turn starts or ends or its running-shell count changes. It + is computed under the hub mutex at those points only: turn admitted or + finished, step frames, shell frames, shell readback, and room release. Text + and tool chunks never push. `seq` starts from the wall clock in microseconds, + so it orders across a restart. +- Keep-alive: `keepUnattendedLocked` keeps an unwatched room while it holds + running shells, so their exits are seen and the badge clears. When the last + shell exits, the room is released at once (`emitRoom`) or by the reaper. + Bounds: the existing 16-room unattended cap, the 6-hour + `chatUnattendedTurnLimit` (measured from when the room became unattended), + and the harness itself. A Claude Code process exit now reports its running + background tasks as exited (`processExit`, `internal/claude`). A released + room reports 0 tasks rather than a stale count. + +## App +- `ChatSessionsController` applies `chat_activity` and row `activity` by `seq` + (the larger wins), deriving `running` and `background_tasks` on the row. + The list re-reads on reconnect (the existing `syncToken`). A server without + `activity` leaves the list as before. +- `session_activity_badge.dart`: the turn spinner, or, when idle with tasks, + a muted `Icons.pending` / `CupertinoIcons.ellipsis_circle_fill` glyph with + tooltip and semantics "Background tasks running (N)". The tooltip shows on + hover only (`TooltipTriggerMode.manual`), so a touch long press stays the + row's own gesture. +- Fixture: `?chats=N&activity=1` in the capture app lists one running chat, + one with two background tasks and idle chats (`tool/perf/perf_fixture.dart`). + +## Evidence +- Go: `chat_activity_test.go` covers the list payload, the pushes on turn start + and end and on task start and exit, unattended keep and release on exit, and + expiry of a task that never reports an exit. `background_tasks_test.go` covers + the Claude process exit. +- Flutter: `test/features/chat/ui/sessions/chat_sessions_activity_test.dart`. +- Captures (fixture app): Material phone 560x1000, Material desktop + 1280x900 with the hover tooltip, and Cupertino dark phone. + +## Unverified +- Real harness behaviour. Shell start and exit events come from each adapter's + existing translation. Whether a real Codex or OpenCode background command + always reports its exit was not exercised against live harnesses here. +- Background Bash outliving a crashed Claude Code CLI. It is reported exited + on process exit, on the assumption that the CLI's tasks end with it. +- Device (Android/iOS) rendering; only the browser was captured. diff --git a/app/test/features/artifacts/actions/artifact_file_actions_test.dart b/app/test/features/artifacts/actions/artifact_file_actions_test.dart index 83360640..7532a6a3 100644 --- a/app/test/features/artifacts/actions/artifact_file_actions_test.dart +++ b/app/test/features/artifacts/actions/artifact_file_actions_test.dart @@ -244,7 +244,7 @@ void main() { expect(find.byType(AlertDialog), findsOneWidget); expect(find.text('Download'), findsOneWidget); expect(find.text('Share'), findsOneWidget); - expect(find.text('Open'), findsOneWidget); + expect(find.text('Open in browser'), findsOneWidget); expect(find.text('Copy link'), findsOneWidget); await tester.tap(find.text('Download')); diff --git a/app/test/features/artifacts/domain/artifact_html_links_test.dart b/app/test/features/artifacts/domain/artifact_html_links_test.dart new file mode 100644 index 00000000..0c5b7e34 --- /dev/null +++ b/app/test/features/artifacts/domain/artifact_html_links_test.dart @@ -0,0 +1,42 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/artifacts/domain/artifact_html_links.dart'; + +void main() { + const inDocument = ArtifactHtmlLinkAction.inDocument; + const external = ArtifactHtmlLinkAction.external; + const blocked = ArtifactHtmlLinkAction.blocked; + + final cases = { + '#section-2': inDocument, + ' #top ': inDocument, + 'about:blank': inDocument, + 'about:blank#section-2': inDocument, + 'about:srcdoc#top': inDocument, + 'https://example.com/report': external, + 'HTTP://Example.com': external, + 'http://127.0.0.1:8080/x': external, + 'mailto:ops@example.com': external, + 'javascript:alert(1)': blocked, + ' JavaScript:alert(1)': blocked, + 'file:///etc/passwd': blocked, + 'data:text/html,x': blocked, + 'about:config': blocked, + 'intent://scan#Intent;end': blocked, + 'tel:+15551234': blocked, + 'report.html': blocked, + '../7/2/other.html': blocked, + '/api/runs': blocked, + '//example.com/x': blocked, + 'http:no-host': blocked, + '': blocked, + }; + + for (final entry in cases.entries) { + test( + '${entry.key.isEmpty ? '(empty)' : entry.key} -> ${entry.value.name}', + () { + expect(artifactHtmlLinkAction(entry.key), entry.value); + }, + ); + } +} diff --git a/app/test/features/artifacts/ui/artifact_fullscreen_test.dart b/app/test/features/artifacts/ui/artifact_fullscreen_test.dart new file mode 100644 index 00000000..2a06125c --- /dev/null +++ b/app/test/features/artifacts/ui/artifact_fullscreen_test.dart @@ -0,0 +1,180 @@ +import 'dart:convert'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/artifacts/domain/artifact_groups.dart'; +import 'package:pipelines_app/features/artifacts/domain/artifact_record.dart'; +import 'package:pipelines_app/features/artifacts/ui/artifact_fullscreen.dart'; +import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_body.dart'; +import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_fetch.dart'; +import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_page.dart'; +import 'package:pipelines_app/features/artifacts/ui/artifact_visit_list.dart'; + +import 'artifact_visit_list_fixture.dart'; + +const _endpoint = 'ws://127.0.0.1:8081/connect'; +const _shellBar = Key('shell-tab-bar'); + +final ArtifactRecord _report = recordedArtifact( + 'report.html', + url: '/artifacts/1/1/report.html', + contentType: 'text/html', +); +final ArtifactRecord _log = recordedArtifact( + 'build.log', + url: '/artifacts/1/1/build.log', + contentType: 'text/plain', +); +final ArtifactGroup _group = visitGroup(artifacts: [_report, _log]); + +/// Each rendered HTML document, with whether it rendered in full screen. +typedef _Rendered = List<(String, bool)>; + +Future<({_Rendered rendered, List opened})> _pump( + WidgetTester tester, { + bool cupertino = false, +}) async { + final rendered = <(String, bool)>[]; + final opened = []; + final page = ArtifactViewerPage( + groups: [_group], + selectedIdentity: artifactVisitIdentity(_group, _report), + configuredEndpoint: _endpoint, + fetch: (uri) async => ArtifactFetchResult( + status: 200, + bytes: utf8.encode(uri.path.endsWith('.html') ? '

Hi

' : 'ok'), + ), + openExternal: (uri) async { + opened.add(uri); + return true; + }, + htmlBuilder: (context, html) { + rendered.add((html, ArtifactFullscreenScope.maybeOf(context) != null)); + return Text('html:$html'); + }, + ); + // A stand-in for the shell: a nested navigator holding the viewer above a + // tab bar the full-screen view must cover. + final shell = Column( + children: [ + Expanded( + child: Navigator( + onGenerateRoute: (_) => cupertino + ? CupertinoPageRoute(builder: (_) => page) + : MaterialPageRoute(builder: (_) => page), + ), + ), + const SizedBox(key: _shellBar, height: 56, child: Text('Tabs')), + ], + ); + await tester.pumpWidget( + cupertino ? CupertinoApp(home: shell) : MaterialApp(home: shell), + ); + await tester.pumpAndSettle(); + return (rendered: rendered, opened: opened); +} + +Future _enter(WidgetTester tester) async { + await tester.tap(find.byKey(artifactFullscreenKey)); + await tester.pumpAndSettle(); +} + +/// A bar control by its accessible name: the Material button's tooltip, the +/// Cupertino button's semantics label. +Finder _named(bool cupertino, String name) => + cupertino ? find.bySemanticsLabel(name) : find.byTooltip(name); + +void main() { + for (final cupertino in [false, true]) { + final host = cupertino ? 'Cupertino' : 'Material'; + + testWidgets('$host: the bottom actions offer Full screen', (tester) async { + final result = await _pump(tester, cupertino: cupertino); + final bar = find.byKey(const Key('artifact-viewer-actions')); + expect( + find.descendant(of: bar, matching: find.byKey(artifactFullscreenKey)), + findsOneWidget, + ); + expect(_named(cupertino, 'Full screen'), findsOneWidget); + expect(result.rendered.single, ('

Hi

', false)); + }); + + testWidgets('$host: full screen covers the shell, keeps the actions and ' + 'renders the fetched document in the app', (tester) async { + final result = await _pump(tester, cupertino: cupertino); + await _enter(tester); + + expect(find.byKey(_shellBar), findsNothing); + // The same document, fetched once with the app's session, now in the + // full-screen scope; nothing was handed to the system browser. + expect(result.rendered.last, ('

Hi

', true)); + expect(result.opened, isEmpty); + final bar = find.byKey(const Key('artifact-viewer-actions')); + expect(bar, findsOneWidget); + for (final key in const [ + Key('artifact-open'), + Key('artifact-copy-link'), + artifactPreviousKey, + artifactNextKey, + ]) { + expect(find.descendant(of: bar, matching: find.byKey(key)), findsOne); + } + expect(_named(cupertino, 'Exit full screen'), findsOneWidget); + expect(_named(cupertino, 'Open in browser'), findsOneWidget); + }); + + testWidgets('$host: Exit full screen returns to the viewer', ( + tester, + ) async { + await _pump(tester, cupertino: cupertino); + await _enter(tester); + await tester.tap(find.byKey(artifactFullscreenKey)); + await tester.pumpAndSettle(); + expect(find.byKey(_shellBar), findsOneWidget); + expect(_named(cupertino, 'Full screen'), findsOneWidget); + }); + } + + testWidgets('Escape and the system back leave full screen', (tester) async { + await _pump(tester); + await _enter(tester); + await tester.sendKeyEvent(LogicalKeyboardKey.escape); + await tester.pumpAndSettle(); + expect(find.byKey(_shellBar), findsOneWidget); + expect(find.text('html:

Hi

'), findsOneWidget); + + await _enter(tester); + await tester.binding.handlePopRoute(); + await tester.pumpAndSettle(); + expect(find.byKey(_shellBar), findsOneWidget); + // Back left full screen only, not the viewer as well. + expect(find.text('html:

Hi

'), findsOneWidget); + }); + + testWidgets('paging in full screen follows the viewer, any kind', ( + tester, + ) async { + await _pump(tester); + await _enter(tester); + await tester.tap(find.byKey(artifactNextKey)); + await tester.pumpAndSettle(); + expect(find.text('build.log'), findsOneWidget); + expect(find.byKey(const Key('artifact-text-preview')), findsOneWidget); + expect(find.byKey(_shellBar), findsNothing); + + await tester.tap(find.byKey(artifactFullscreenKey)); + await tester.pumpAndSettle(); + expect(find.text('build.log'), findsOneWidget); + expect(find.byKey(_shellBar), findsOneWidget); + }); + + test('zoom steps walk the list and hold at both ends', () { + expect(artifactHtmlZoomStep(1, 1), 1.1); + expect(artifactHtmlZoomStep(1, -1), 0.9); + expect(artifactHtmlZoomStep(4, 1), 4); + expect(artifactHtmlZoomStep(0.5, -1), 0.5); + expect(artifactHtmlZoomStep(1.3, -1), 1.25); + }); +} diff --git a/app/test/features/artifacts/ui/artifact_gallery_documents_test.dart b/app/test/features/artifacts/ui/artifact_gallery_documents_test.dart index f8e229cc..4be4546c 100644 --- a/app/test/features/artifacts/ui/artifact_gallery_documents_test.dart +++ b/app/test/features/artifacts/ui/artifact_gallery_documents_test.dart @@ -188,7 +188,7 @@ void main() { expect(find.byType(ArtifactViewerPage), findsNothing); expect(_dialog(platform, 'session.html'), findsOneWidget); expect(_dialog(platform, 'text/plain · 512\u00a0B'), findsOneWidget); - await tester.tap(_dialog(platform, 'Open')); + await tester.tap(_dialog(platform, 'Open in browser')); await tester.pumpAndSettle(); expect(find.byType(ArtifactViewerPage), findsNothing); expect(opened, [ diff --git a/app/test/features/artifacts/ui/artifact_gallery_page_test.dart b/app/test/features/artifacts/ui/artifact_gallery_page_test.dart index 4cdb0030..1415326b 100644 --- a/app/test/features/artifacts/ui/artifact_gallery_page_test.dart +++ b/app/test/features/artifacts/ui/artifact_gallery_page_test.dart @@ -214,7 +214,7 @@ void main() { ); await tester.tap(find.text('result.zip')); await tester.pumpAndSettle(); - await tester.tap(_inDialog(_Platform.material, 'Open')); + await tester.tap(_inDialog(_Platform.material, 'Open in browser')); await tester.pumpAndSettle(); expect(opened, [ @@ -243,7 +243,7 @@ void main() { ), findsOneWidget, ); - expect(_inDialog(_Platform.material, 'Open'), findsNothing); + expect(_inDialog(_Platform.material, 'Open in browser'), findsNothing); expect(_inDialog(_Platform.material, 'Copy link'), findsNothing); }); @@ -270,7 +270,7 @@ void main() { _inDialog(_Platform.material, 'No artifact URL recorded.'), findsOneWidget, ); - expect(_inDialog(_Platform.material, 'Open'), findsNothing); + expect(_inDialog(_Platform.material, 'Open in browser'), findsNothing); }); testWidgets('Copy link copies the resolved url, separate from Open', ( @@ -504,7 +504,7 @@ void main() { } expect(orderOf('Close'), lessThan(orderOf('Copy link'))); - expect(orderOf('Copy link'), lessThan(orderOf('Open'))); + expect(orderOf('Copy link'), lessThan(orderOf('Open in browser'))); }, ); } diff --git a/app/test/features/chat/conversation/chat_question_card_test.dart b/app/test/features/chat/conversation/chat_question_card_test.dart index 72a9b9e4..4ba1084b 100644 --- a/app/test/features/chat/conversation/chat_question_card_test.dart +++ b/app/test/features/chat/conversation/chat_question_card_test.dart @@ -488,7 +488,9 @@ void main() { }); held.complete(const {}); await tester.pumpAndSettle(); - expect(find.text(chatQuestionAnswered), findsOneWidget); + // The answered question leaves the card list (chat_question_group_test). + expect(find.text(chatQuestionWaiting), findsNothing); + expect(find.text('Colour · Red · +1 more'), findsOneWidget); }); testWidgets('a refused question keeps the card open with the server reason', ( diff --git a/app/test/features/chat/conversation/chat_question_group_test.dart b/app/test/features/chat/conversation/chat_question_group_test.dart new file mode 100644 index 00000000..c6bd0d77 --- /dev/null +++ b/app/test/features/chat/conversation/chat_question_group_test.dart @@ -0,0 +1,301 @@ +import 'dart:async'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_question_card_model.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_question_group.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; + +import 'chat_thread_fixtures.dart'; + +JsonMap _turn(String id) => { + 'kind': 'assistant', + 'key': id, + 'messageIds': [id], + 'parts': const [], +}; + +JsonMap _card( + String id, { + String? raisedIn, + bool resolved = false, + bool cancelled = false, + String? error, +}) => { + 'kind': 'question', + 'key': 'chat-question-$id', + 'form': { + 'id': id, + 'resolved': resolved, + 'cancelled': cancelled, + chatQuestionRaisedIn: ?raisedIn, + }, + 'error': error, +}; + +JsonMap _form({String id = 'form-1'}) => { + 'id': id, + 'session_id': 's', + 'kind': 'question', + 'title': 'Database', + 'fields': [ + { + 'key': 'q0', + 'title': 'Which database?', + 'type': 'string', + 'required': true, + 'options': [ + {'value': 'pg', 'label': 'Postgres'}, + {'value': 'lite', 'label': 'SQLite'}, + ], + }, + ], +}; + +const _chip = 'Which database? · Postgres'; + +/// A chat whose agent turn `a1` is the one the question arrives during. +Future _open( + WidgetTester tester, + Size size, { + bool cupertino = false, +}) async { + await tester.binding.setSurfaceSize(size); + addTearDown(() => tester.binding.setSurfaceSize(null)); + final gateway = FakeChatGateway() + ..history = (_) async => chatPage([ + chatMessage( + 'u1', + 'user', + parts: [ + {'type': 'text', 'text': 'Ship the release.'}, + ], + ), + chatMessage( + 'a1', + 'assistant', + parts: [ + {'type': 'text', 'text': 'Which database first?'}, + ], + ), + ]); + addTearDown(gateway.close); + final view = ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}); + await tester.pumpWidget( + cupertino + ? CupertinoApp(home: CupertinoPageScaffold(child: view)) + : MaterialApp(home: Scaffold(body: view)), + ); + await tester.pumpAndSettle(); + gateway.emitEvent({'session_id': 's', 'kind': 'form', 'form': _form()}); + await tester.pumpAndSettle(); + expect(find.text(chatQuestionWaiting), findsOneWidget); + return gateway; +} + +Future _answerPostgres(WidgetTester tester) async { + await tester.tap(find.text('Postgres')); + await tester.pump(); + await tester.tap(find.text(chatQuestionSubmit)); + await tester.pumpAndSettle(); +} + +void _expectCollapsed() { + expect(find.text(chatQuestionWaiting), findsNothing); + expect(find.text(chatQuestionSubmit), findsNothing); + expect(find.text(chatQuestionAnswered), findsNothing); + expect(find.text(_chip), findsOneWidget); + // The chip sits in its turn's footer, under the turn that asked. + expect( + find.descendant( + of: find.byKey(const Key('chat-message-footer')), + matching: find.text(_chip), + ), + findsOneWidget, + ); +} + +void main() { + test('settled questions join the turn they were raised during', () { + final rows = groupSettledQuestions([ + _turn('m1'), + _turn('m2'), + _card('a', raisedIn: 'm1', resolved: true), + _card('b', raisedIn: 'm2', cancelled: true), + ]); + expect(rows.map((r) => r['kind']), ['assistant', 'assistant']); + expect(chatRowQuestions(rows[0]).map((f) => f['id']), ['a']); + expect(chatRowQuestions(rows[1]).map((f) => f['id']), ['b']); + }); + + test('questions naming no turn share one row; open ones stay cards', () { + final rows = groupSettledQuestions([ + _turn('m1'), + _card('loose-1', resolved: true), + _card('pending', raisedIn: 'm1'), + _card('refused', raisedIn: 'm1', error: 'no longer awaiting'), + _card('loose-2', raisedIn: 'paged-out', resolved: true), + ]); + expect(rows.map((r) => r['kind']), [ + 'assistant', + 'questions', + 'question', + 'question', + ]); + expect(rows[0].containsKey('questions'), isFalse); + expect(chatRowQuestions(rows[1]).map((f) => f['id']), [ + 'loose-1', + 'loose-2', + ]); + }); + + test('the summary names the question and its outcome', () { + final form = _form(); + expect( + chatQuestionSummary({ + ...form, + 'resolved': true, + 'answers': {'q0': 'pg'}, + }), + _chip, + ); + expect( + chatQuestionSummary({ + ...form, + 'resolved': true, + 'answers': {'q0': 'pg', 'extra': 'x'}, + }), + '$_chip · +1 more', + ); + expect( + chatQuestionSummary({...form, 'cancelled': true}), + 'Which database? · Expired', + ); + expect( + chatQuestionSummary({...form, 'resolved': true}), + 'Which database? · Closed', + ); + }); + + for (final (name, size) in [ + ('narrow', const Size(390, 844)), + ('wide', const Size(1280, 900)), + ]) { + testWidgets('$name: an acknowledged answer collapses into its turn at once ' + '(Claude Code: the ack is the only signal)', (tester) async { + final gateway = await _open(tester, size); + final held = Completer(); + gateway.answer = (_) => held.future; + await tester.tap(find.text('Postgres')); + await tester.pump(); + await tester.tap(find.text(chatQuestionSubmit)); + await tester.pump(); + // Still the card while the reply is in flight. + expect(find.text(chatQuestionWaiting), findsOneWidget); + held.complete(const {'type': 'chat_answer'}); + await tester.pumpAndSettle(); + _expectCollapsed(); + + // A late replay of the pending frame cannot reopen it. + gateway.emitEvent({'session_id': 's', 'kind': 'form', 'form': _form()}); + await tester.pumpAndSettle(); + _expectCollapsed(); + + await tester.tap(find.text(_chip)); + await tester.pumpAndSettle(); + expect(find.byKey(const Key('chat-question-sheet')), findsOneWidget); + expect(find.text('Which database?'), findsOneWidget); + expect(find.text('Postgres'), findsOneWidget); + expect(find.text(chatQuestionAnswered), findsOneWidget); + expect(tester.takeException(), isNull); + }); + + testWidgets('$name: a refused answer keeps the full card with the reason', ( + tester, + ) async { + final gateway = await _open(tester, size); + gateway.answer = (_) async => + throw StateError('the question is no longer awaiting an answer'); + await _answerPostgres(tester); + expect(find.textContaining('no longer awaiting'), findsOneWidget); + expect(find.text(chatQuestionWaiting), findsOneWidget); + expect(find.text(chatQuestionSubmit), findsOneWidget); + expect(find.text(_chip), findsNothing); + }); + } + + testWidgets('OpenCode/Codex: the server resolution collapses a question ' + 'answered elsewhere', (tester) async { + final gateway = await _open(tester, const Size(390, 844)); + gateway.emitEvent({ + 'session_id': 's', + 'kind': 'form', + 'form': {'id': 'form-1', 'session_id': 's', 'resolved': true}, + }); + await tester.pumpAndSettle(); + expect(find.text(chatQuestionWaiting), findsNothing); + expect(find.text('Which database? · Closed'), findsOneWidget); + }); + + testWidgets('a cancelled question collapses as expired', (tester) async { + final gateway = await _open(tester, const Size(390, 844)); + gateway.emitEvent({ + 'session_id': 's', + 'kind': 'form', + 'form': {'id': 'form-1', 'session_id': 's', 'cancelled': true}, + }); + await tester.pumpAndSettle(); + expect(find.text(chatQuestionWaiting), findsNothing); + expect(find.text('Which database? · Expired'), findsOneWidget); + }); + + testWidgets('OpenCode: ack then form.replied leaves one collapsed row', ( + tester, + ) async { + final gateway = await _open(tester, const Size(390, 844)); + gateway.answer = (_) async { + gateway.emitEvent({ + 'session_id': 's', + 'kind': 'form', + 'form': {'id': 'form-1', 'session_id': 's', 'resolved': true}, + }); + return const {'type': 'chat_answer'}; + }; + await _answerPostgres(tester); + _expectCollapsed(); + }); + + testWidgets('Cupertino: the answered question collapses to the native chip', ( + tester, + ) async { + final gateway = await _open(tester, const Size(390, 844), cupertino: true); + gateway.answer = (_) async => const {'type': 'chat_answer'}; + await _answerPostgres(tester); + _expectCollapsed(); + expect(tester.takeException(), isNull); + }); + + testWidgets('after a reload the answered question stays gone', ( + tester, + ) async { + final gateway = await _open(tester, const Size(390, 844)); + gateway.answer = (_) async => const {'type': 'chat_answer'}; + await _answerPostgres(tester); + _expectCollapsed(); + // A fresh view over the same server: the subscribe replays only pending + // forms, and this one is no longer pending. + await tester.pumpWidget(const SizedBox()); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}), + ), + ), + ); + await tester.pumpAndSettle(); + expect(find.text('Which database first?'), findsOneWidget); + expect(find.text(chatQuestionWaiting), findsNothing); + }); +} diff --git a/app/test/features/chat/ui/sessions/chat_sessions_activity_test.dart b/app/test/features/chat/ui/sessions/chat_sessions_activity_test.dart new file mode 100644 index 00000000..63e95324 --- /dev/null +++ b/app/test/features/chat/ui/sessions/chat_sessions_activity_test.dart @@ -0,0 +1,264 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/data/chat_repository.dart'; +import 'package:pipelines_app/features/chat/domain/chat_list_activity.dart'; +import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_controller.dart'; +import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_screen.dart'; + +import 'chat_test_harness.dart'; +import 'fake_chat_gateway.dart'; + +/// The list shows every chat's live activity from the server: a spinner while +/// a turn runs, a quieter badge while only background tasks run, nothing when +/// idle. `chat_activity` pushes reach chats this device never opened, and an +/// older server (no `activity`) leaves the list as before. +JsonMap _row(String id, {JsonMap? activity}) => { + 'id': id, + 'title': 'Chat $id', + 'directory': '/w', + 'updated_at': 1000, + 'activity': ?activity, +}; + +JsonMap _activity({bool turn = false, int tasks = 0, int seq = 1}) => { + if (turn) 'turn_running': true, + if (tasks > 0) 'background_tasks': tasks, + 'seq': seq, +}; + +JsonMap _push( + String id, { + bool turn = false, + int tasks = 0, + required int seq, +}) => { + 'type': 'chat_activity', + 'chat_activity': { + 'session_id': id, + ..._activity(turn: turn, tasks: tasks, seq: seq), + }, +}; + +/// The row merges its semantics, so the badge's label is part of the row's. +Finder _label(int tasks) => find.bySemanticsLabel( + RegExp(RegExp.escape('Background tasks running ($tasks)')), +); + +Finder _badge(String id) => find.descendant( + of: find.byKey(Key('chat-row-$id')), + matching: find.byKey(const Key('chat-row-background')), +); + +Finder _spinner(String id) => find.descendant( + of: find.byKey(Key('chat-row-$id')), + matching: find.byKey(const Key('chat-row-working')), +); + +Future _pump( + WidgetTester tester, + FakeChatGateway fake, { + bool cupertino = false, +}) async { + final screen = ChatSessionsScreen( + gateway: fake, + onOpen: (_) {}, + localStore: MemoryChatSessionLocalStore(), + ); + await tester.pumpWidget( + cupertino + ? CupertinoApp(home: screen) + : MaterialApp( + theme: ThemeData(platform: TargetPlatform.android), + home: screen, + ), + ); + // A spinner never settles, so pump a few frames instead. + for (var i = 0; i < 5; i++) { + await tester.pump(const Duration(milliseconds: 50)); + } +} + +Future _frames(WidgetTester tester) async { + await tester.pump(); + await tester.pump(); +} + +void main() { + testWidgets( + 'listed activity draws a spinner, a background badge or nothing', + (tester) async { + final fake = FakeChatGateway() + ..sessionsReply = { + 'chat_sessions': [ + _row('run', activity: _activity(turn: true, tasks: 1)), + _row('bg', activity: _activity(tasks: 2)), + _row('idle', activity: _activity()), + ], + }; + await _pump(tester, fake); + + expect(_spinner('run'), findsOneWidget); + expect(_badge('run'), findsNothing, reason: 'the turn spinner wins'); + expect(_spinner('bg'), findsNothing); + expect(_badge('bg'), findsOneWidget); + expect(_spinner('idle'), findsNothing); + expect(_badge('idle'), findsNothing); + expect(_label(2), findsOneWidget); + final tooltip = tester.widget(_badge('bg')); + expect(tooltip.message, 'Background tasks running (2)'); + expect(tooltip.triggerMode, TooltipTriggerMode.manual); + expect( + find.descendant(of: _badge('bg'), matching: find.byIcon(Icons.pending)), + findsOneWidget, + ); + expect( + find.descendant( + of: _badge('bg'), + matching: find.byType(CircularProgressIndicator), + ), + findsNothing, + reason: 'the background badge is not a second spinner', + ); + }, + ); + + testWidgets('pushes move a chat this device never opened', (tester) async { + final fake = FakeChatGateway() + ..sessionsReply = { + 'chat_sessions': [_row('s1', activity: _activity(seq: 10))], + }; + await _pump(tester, fake); + expect(_spinner('s1'), findsNothing); + + fake.pushed.add(_push('s1', turn: true, seq: 11)); + await _frames(tester); + expect(_spinner('s1'), findsOneWidget); + + fake.pushed.add(_push('s1', tasks: 3, seq: 13)); + await _frames(tester); + expect(_spinner('s1'), findsNothing); + expect(_label(3), findsOneWidget); + + // A push older than the snapshot already applied is ignored. + fake.pushed.add(_push('s1', turn: true, seq: 12)); + await _frames(tester); + expect(_spinner('s1'), findsNothing); + expect(_badge('s1'), findsOneWidget); + + fake.pushed.add(_push('s1', seq: 14)); + await _frames(tester); + expect(_badge('s1'), findsNothing); + expect(_spinner('s1'), findsNothing); + expect(fake.count('chat_sessions'), 1); + }); + + testWidgets('an older server without activity keeps the old spinner', ( + tester, + ) async { + final fake = FakeChatGateway() + ..sessionsReply = { + 'chat_sessions': [_row('s1')], + }; + await _pump(tester, fake); + expect(_badge('s1'), findsNothing); + expect(_spinner('s1'), findsNothing); + fake.pushed.add({ + 'type': 'chat_event', + 'chat_event': { + 'session_id': 's1', + 'kind': 'step', + 'step': {'status': 'started'}, + }, + }); + await _frames(tester); + expect(_spinner('s1'), findsOneWidget); + expect(_badge('s1'), findsNothing); + }); + + testWidgets('the Cupertino host draws its own glyph', (tester) async { + final fake = FakeChatGateway() + ..sessionsReply = { + 'chat_sessions': [_row('bg', activity: _activity(tasks: 1))], + }; + await _pump(tester, fake, cupertino: true); + expect( + find.descendant( + of: _badge('bg'), + matching: find.byIcon(CupertinoIcons.ellipsis_circle_fill), + ), + findsOneWidget, + ); + expect(_label(1), findsOneWidget); + }); + + group('controller', () { + late FakeChatGateway fake; + late ChatSessionsController controller; + late List rows; + + setUp(() { + fake = FakeChatGateway(); + fake.sessionsHandler = () async => {'chat_sessions': rows}; + controller = ChatSessionsController( + ChatRepository(fake), + localStore: MemoryChatSessionLocalStore(), + ); + }); + tearDown(() => controller.dispose()); + + JsonMap row() => controller.sessions.single; + + test('a list read older than an applied push keeps the push', () async { + rows = [_row('s1', activity: _activity(seq: 5))]; + await controller.refresh(); + fake.pushed.add(_push('s1', tasks: 2, seq: 7)); + await Future.delayed(Duration.zero); + expect(chatSessionBackgroundTasks(row()), 2); + + rows = [_row('s1', activity: _activity(seq: 6))]; + await controller.refresh(); + expect(chatSessionBackgroundTasks(row()), 2); + expect(row()['running'], false); + }); + + test('a newer list read (a reconnect) replaces the pushed state', () async { + rows = [_row('s1', activity: _activity(seq: 5))]; + await controller.refresh(); + fake.pushed.add(_push('s1', turn: true, tasks: 2, seq: 7)); + await Future.delayed(Duration.zero); + expect(row()['running'], true); + + rows = [_row('s1', activity: _activity(seq: 90))]; + await controller.refresh(); + expect(row()['running'], false); + expect(chatSessionBackgroundTasks(row()), 0); + }); + }); + + test('chatListActivityOf reads rows and pushes and refuses malformed', () { + expect( + chatListActivityOf({ + 'turn_running': true, + 'background_tasks': 2, + 'seq': 9, + }, sessionId: 's1'), + (sessionId: 's1', turnRunning: true, backgroundTasks: 2, seq: 9), + ); + expect(chatListActivityOf({'session_id': 's2'}), ( + sessionId: 's2', + turnRunning: false, + backgroundTasks: 0, + seq: 0, + )); + expect(chatListActivityOf({'background_tasks': 1}), isNull); + expect(chatListActivityOf('nope', sessionId: 's1'), isNull); + expect( + chatListActivityOf({ + 'background_tasks': -4, + }, sessionId: 's1')?.backgroundTasks, + 0, + ); + }); +} diff --git a/app/test/features/chat/ui/sessions/chat_sessions_live_new_test.dart b/app/test/features/chat/ui/sessions/chat_sessions_live_new_test.dart index 7a08c57c..aa015e6f 100644 --- a/app/test/features/chat/ui/sessions/chat_sessions_live_new_test.dart +++ b/app/test/features/chat/ui/sessions/chat_sessions_live_new_test.dart @@ -67,6 +67,25 @@ JsonMap _event(String id, {String kind = 'text', String? delta}) => { }, }; +JsonMap _tool(String id, String status) => { + 'type': 'chat_event', + 'chat_event': { + 'session_id': id, + 'kind': 'tool', + 'part': { + 'type': 'tool', + 'state': { + 'status': status, + if (status == 'completed') + 'content': [ + {'type': 'text', 'text': 'command output'}, + ], + if (status == 'error') 'error': 'exit status 1', + }, + }, + }, +}; + void main() { setUp(() => _store = MemoryChatSessionLocalStore()); @@ -176,46 +195,79 @@ void main() { expect(find.byType(CircularProgressIndicator), findsNothing); }); - testWidgets('tool starts are loading; returned tool results are unread', ( + testWidgets('tool calls and their results are loading, never New', ( tester, ) async { final fake = gateway(); await _pump(tester, fake); - fake.pushed.add({ - 'type': 'chat_event', - 'chat_event': { - 'session_id': 's1', - 'kind': 'tool', - 'part': { - 'type': 'tool', - 'state': {'status': 'running'}, - }, - }, - }); + fake.pushed.add(_tool('s1', 'running')); await tester.pump(); await tester.pump(); expect(find.byKey(const Key('chat-row-unread')), findsNothing); expect(find.byType(CircularProgressIndicator), findsOneWidget); + fake.pushed.add(_tool('s1', 'completed')); + fake.pushed.add(_tool('s1', 'error')); + // Harness notices, agent reports, a prompt from another device and + // whitespace-only deltas around a tool call are not a reply either. fake.pushed.add({ 'type': 'chat_event', 'chat_event': { 'session_id': 's1', - 'kind': 'tool', - 'part': { - 'type': 'tool', - 'state': { - 'status': 'completed', - 'content': [ - {'type': 'text', 'text': 'command output'}, - ], - }, - }, + 'kind': 'text', + 'delta': 'Background task finished', + 'message_kind': 'notice', + }, + }); + fake.pushed.add({ + 'type': 'chat_event', + 'chat_event': { + 'session_id': 's1', + 'kind': 'text', + 'delta': 'Subagent report', + 'message_kind': 'agent_report', + }, + }); + fake.pushed.add({ + 'type': 'chat_event', + 'chat_event': {'session_id': 's1', 'kind': 'user', 'delta': 'hi'}, + }); + fake.pushed.add(_event('s1', delta: '\n\n')); + // The turn is still working, so its spinner never settles. + await tester.pump(); + await tester.pump(); + expect(find.byKey(const Key('chat-row-unread')), findsNothing); + expect(find.byType(CircularProgressIndicator), findsOneWidget); + + // The assistant's reply after the tool call is what is unread. + fake.pushed.add(_event('s1', delta: 'Done: tests pass.')); + await tester.pump(); + await tester.pump(); + expect(find.byKey(const Key('chat-row-unread')), findsOneWidget); + }); + + testWidgets('a tool call and reply in the open chat stay read on leaving', ( + tester, + ) async { + final fake = gateway(); + await _pump(tester, fake, active: 's1'); + fake.pushed.add(_tool('s1', 'running')); + fake.pushed.add(_tool('s1', 'completed')); + fake.pushed.add(_event('s1', delta: 'Here is the answer.')); + fake.pushed.add({ + 'type': 'chat_event', + 'chat_event': { + 'session_id': 's1', + 'kind': 'step', + 'step': {'status': 'ended'}, }, }); await tester.pumpAndSettle(); - expect(find.byKey(const Key('chat-row-unread')), findsOneWidget); - expect(find.byType(CircularProgressIndicator), findsNothing); + expect(find.byKey(const Key('chat-row-unread')), findsNothing); + + await _pump(tester, fake); + expect(find.byKey(const Key('chat-row-unread')), findsNothing); + expect(_store.state?.isUnseen('s1') ?? false, isFalse); }); testWidgets('only unresolved permission requests mark New', (tester) async { diff --git a/app/test/features/chat/ui/sessions/session_attention_test.dart b/app/test/features/chat/ui/sessions/session_attention_test.dart index 76bde0c3..ce79e2fb 100644 --- a/app/test/features/chat/ui/sessions/session_attention_test.dart +++ b/app/test/features/chat/ui/sessions/session_attention_test.dart @@ -37,4 +37,44 @@ void main() { }); expect(intermediate!.running, isNull); }); + + test('only visible assistant reply text is unread', () { + bool attention(Map event) => + chatSessionFrameEffect({ + 'type': 'chat_event', + 'chat_event': {'session_id': 's', ...event}, + })!.attention; + for (final status in ['running', 'completed', 'error']) { + expect( + attention({ + 'kind': 'tool', + 'part': { + 'type': 'tool', + 'state': { + 'status': status, + 'content': [ + {'type': 'text', 'text': 'out'}, + ], + 'error': status == 'error' ? 'failed' : null, + }, + }, + }), + isFalse, + reason: 'tool $status', + ); + } + expect(attention({'kind': 'user', 'delta': 'prompt'}), isFalse); + expect(attention({'kind': 'text', 'delta': ' \n'}), isFalse); + for (final messageKind in ['notice', 'agent_report']) { + expect( + attention({ + 'kind': 'text', + 'delta': 'x', + 'message_kind': messageKind, + }), + isFalse, + ); + } + expect(attention({'kind': 'text', 'delta': 'answer'}), isTrue); + }); } diff --git a/app/test/features/chat/ui/transcript/chat_link_click_test.dart b/app/test/features/chat/ui/transcript/chat_link_click_test.dart new file mode 100644 index 00000000..daeedfca --- /dev/null +++ b/app/test/features/chat/ui/transcript/chat_link_click_test.dart @@ -0,0 +1,113 @@ +import 'package:flutter/gestures.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_link_launcher.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart'; + +import 'chat_markdown_test_support.dart'; + +// A mouse click on a link opens it even when the pointer moves a few pixels +// between press and release: the selection area used to claim that press as a +// selection drag after 2 px, so ordinary clicks in the browser opened nothing. +void main() { + late List opened; + + Future pump(WidgetTester tester) async { + await phoneViewport(tester); + opened = []; + await tester.pumpWidget( + ChatLinkScope( + launcher: (uri) async { + opened.add(uri); + return true; + }, + child: markdownHost( + const ChatMarkdownView(text: 'go [here](https://x.com/c) now'), + ), + ), + ); + await tester.pumpAndSettle(); + return tester.renderObject(find.byType(RichText).first); + } + + Offset centreOf(RenderParagraph paragraph, String text) { + final start = paragraph.text.toPlainText().indexOf(text); + final box = paragraph.getBoxesForSelection( + TextSelection(baseOffset: start, extentOffset: start + text.length), + ); + return paragraph.localToGlobal(box.first.toRect().center); + } + + Future mouseDrag(WidgetTester tester, Offset from, double dx) async { + final mouse = await tester.startGesture( + from, + kind: PointerDeviceKind.mouse, + ); + await tester.pump(); + // Three moves, as a hand drifts during a click or drags. + for (var i = 1; i <= 3; i++) { + await mouse.moveTo(from + Offset(dx * i / 3, 0)); + await tester.pump(); + } + await mouse.up(); + await tester.pumpAndSettle(); + await mouse.removePointer(); + } + + for (final dx in [0.0, 3.0, 8.0]) { + testWidgets( + 'a mouse click moving ${dx}px opens the link', + (tester) async { + final paragraph = await pump(tester); + await mouseDrag(tester, centreOf(paragraph, 'here'), dx); + expect(opened.map((uri) => '$uri'), ['https://x.com/c']); + }, + variant: TargetPlatformVariant.desktop(), + ); + } + + testWidgets( + 'dragging off a link opens nothing', + (tester) async { + final paragraph = await pump(tester); + await mouseDrag(tester, centreOf(paragraph, 'here'), 40); + expect(opened, isEmpty); + }, + variant: TargetPlatformVariant.only(TargetPlatform.macOS), + ); + + testWidgets( + 'a mouse drag from plain text still selects across the link', + (tester) async { + final paragraph = await pump(tester); + final from = centreOf(paragraph, 'go'); + await mouseDrag(tester, from, centreOf(paragraph, 'now').dx - from.dx); + expect(opened, isEmpty); + String? copied; + tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + SystemChannels.platform, + (call) async { + if (call.method == 'Clipboard.setData') { + copied = (call.arguments as Map)['text'] as String?; + } + return null; + }, + ); + addTearDown( + () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + SystemChannels.platform, + null, + ), + ); + await tester.sendKeyDownEvent(LogicalKeyboardKey.meta); + await tester.sendKeyEvent(LogicalKeyboardKey.keyC); + await tester.sendKeyUpEvent(LogicalKeyboardKey.meta); + await tester.pump(); + // The link claims only presses on itself; this drag selects across it. + expect(copied, contains('o here n')); + }, + variant: TargetPlatformVariant.only(TargetPlatform.macOS), + ); +} diff --git a/app/tool/capture/README.md b/app/tool/capture/README.md index 6fe79be0..ce1fd3c8 100644 --- a/app/tool/capture/README.md +++ b/app/tool/capture/README.md @@ -53,6 +53,9 @@ In JSON, a step can also set `wait` (ms), `timeout`, `name`, `ms` and `frames`. | `labels=name` | write the visible semantics labels, keys and rects to JSON | | `film=` | screencast a click for `--film-ms` (900) and write the frames plus a contact sheet | | `wait=ms` | sleep | +| `swipe=>dx,dy` | one-finger touch swipe (needs `--touch true`, which emulates a touch screen once semantics is on) | +| `pinch=>scale` | two-finger touch pinch, spreading (>1) or closing (<1) (needs `--touch true`) | +| `tabs=name` | write the open tabs (`{url, type}`) to JSON, e.g. to see a link open a new tab | | `eval=` | evaluate an expression in the page and log/record its value | | `expect=` | fail the run (status 1) unless the expression is truthy; JSON `name` labels the check | @@ -69,6 +72,10 @@ Targets: When a target is missing, the step fails. The kit lists the visible labels, saves `NN-FAILED.png` and exits with status 1. +An HTML artifact: `--params html=1` adds `release-dashboard.html` to run 7. +Its bytes are served from the build, so copy the fixture in after building: +`mkdir -p build/probe-app/artifacts/7/2 && cp tool/capture/fixtures/release-dashboard.html build/probe-app/artifacts/7/2/`. + ## Recipes (run from `app/` after building `build/probe-app`) Capture a page at phone and fold widths, in Material and in Cupertino. Use 560 diff --git a/app/tool/capture/browser.mjs b/app/tool/capture/browser.mjs index fdfd6ea3..3a5531ee 100644 --- a/app/tool/capture/browser.mjs +++ b/app/tool/capture/browser.mjs @@ -91,6 +91,9 @@ export async function openBrowser({ cdp, chromePath = process.env.CHROME_PATH ?? listeners.set(method, [...(listeners.get(method) ?? []), fn]); return () => listeners.set(method, listeners.get(method).filter((f) => f !== fn)); }, + /** Every open page target (tabs a link opened included): `{ url, type }`. */ + targets: async () => (await raw('Target.getTargets')).targetInfos + .filter((t) => t.type === 'page').map((t) => ({ url: t.url, type: t.type })), newPage: async () => { const page = await raw('Target.createTarget', { url: 'about:blank' }); const { sessionId: sid } = await raw('Target.attachToTarget', { targetId: page.targetId, flatten: true }); diff --git a/app/tool/capture/capture.mjs b/app/tool/capture/capture.mjs index d0e24026..3d1b2d85 100644 --- a/app/tool/capture/capture.mjs +++ b/app/tool/capture/capture.mjs @@ -20,10 +20,12 @@ options: --script --size WxH (880x1000) --platform material|cuperti --theme light|dark --q --wait --film-ms --cdp http://127.0.0.1: --probe true|false --params + --touch true (emulate a touch screen: mobile metrics, touch input) steps: open=[@WxH] wait= shot= click= waitfor= type= press= scroll= size=WxH - film= labels= eval= expect= hover= + film= labels= tabs= eval= expect= hover= drag=>, (JSON: drag, dx, dy, steps, gap, hold) + swipe=>, pinch=> (with --touch true) target: