diff --git a/README.md b/README.md index 17849fef..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,20 +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**, duration, pipeline or status. -- 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. @@ -156,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/fonts/README.md b/app/fonts/README.md index 3e76f7fc..8611d467 100644 --- a/app/fonts/README.md +++ b/app/fonts/README.md @@ -6,3 +6,22 @@ Christian Robertson, licensed under the Apache License, Version 2.0 from the Flutter SDK's DevTools assets. They give monospace text (artifact text previews, background task output) a real fixed-width face on every platform: Flutter web does not resolve the generic `monospace` family. + +## Optional interface fonts + +Chosen per device in Settings > Font (`lib/ui/theme/app_font.dart`). They are +declared as plain `assets:`, not `fonts:`, so Flutter web does not download them +at startup; the chosen family is registered on demand with a `FontLoader`. Each +directory holds the unmodified Regular (400), Medium (500), SemiBold (600) and +Bold (700) static TTFs and the family's SIL Open Font License 1.1 as `OFL.txt`, +which is also registered with `LicenseRegistry`. + +| Directory | Family | Source | Licence | +| --- | --- | --- | --- | +| `inter/` | Inter 4.1 | github.com/rsms/inter release v4.1, `Inter-4.1.zip`, `extras/ttf/` | OFL 1.1, (c) The Inter Project Authors | +| `ibm-plex-sans/` | IBM Plex Sans 1.1.0 | github.com/IBM/plex release `@ibm/plex-sans@1.1.0`, `ibm-plex-sans.zip`, `fonts/complete/ttf/` | OFL 1.1, (c) IBM Corp., Reserved Font Name "Plex" | +| `source-sans-3/` | Source Sans 3.052 | github.com/adobe-fonts/source-sans release 3.052R, `TTF-source-sans-3.052R.zip`; licence text from `LICENSE.md` at that tag | OFL 1.1, (c) Adobe, Reserved Font Name "Source" | +| `atkinson-hyperlegible-next/` | Atkinson Hyperlegible Next | github.com/googlefonts/atkinson-hyperlegible-next (no releases) at commit 7925f50f, `fonts/ttf/` and `OFL.txt` | OFL 1.1, (c) The Atkinson Hyperlegible Next Project Authors | + +The files are unmodified (no subsetting), so the Reserved Font Names stay +usable. diff --git a/app/fonts/atkinson-hyperlegible-next/AtkinsonHyperlegibleNext-Bold.ttf b/app/fonts/atkinson-hyperlegible-next/AtkinsonHyperlegibleNext-Bold.ttf new file mode 100644 index 00000000..668a2d16 Binary files /dev/null and b/app/fonts/atkinson-hyperlegible-next/AtkinsonHyperlegibleNext-Bold.ttf differ diff --git a/app/fonts/atkinson-hyperlegible-next/AtkinsonHyperlegibleNext-Medium.ttf b/app/fonts/atkinson-hyperlegible-next/AtkinsonHyperlegibleNext-Medium.ttf new file mode 100644 index 00000000..2136809d Binary files /dev/null and b/app/fonts/atkinson-hyperlegible-next/AtkinsonHyperlegibleNext-Medium.ttf differ diff --git a/app/fonts/atkinson-hyperlegible-next/AtkinsonHyperlegibleNext-Regular.ttf b/app/fonts/atkinson-hyperlegible-next/AtkinsonHyperlegibleNext-Regular.ttf new file mode 100644 index 00000000..d0982cdc Binary files /dev/null and b/app/fonts/atkinson-hyperlegible-next/AtkinsonHyperlegibleNext-Regular.ttf differ diff --git a/app/fonts/atkinson-hyperlegible-next/AtkinsonHyperlegibleNext-SemiBold.ttf b/app/fonts/atkinson-hyperlegible-next/AtkinsonHyperlegibleNext-SemiBold.ttf new file mode 100644 index 00000000..44dcac96 Binary files /dev/null and b/app/fonts/atkinson-hyperlegible-next/AtkinsonHyperlegibleNext-SemiBold.ttf differ diff --git a/app/fonts/atkinson-hyperlegible-next/OFL.txt b/app/fonts/atkinson-hyperlegible-next/OFL.txt new file mode 100644 index 00000000..88955733 --- /dev/null +++ b/app/fonts/atkinson-hyperlegible-next/OFL.txt @@ -0,0 +1,93 @@ +Copyright 2020-2024 The Atkinson Hyperlegible Next Project Authors (https://github.com/googlefonts/atkinson-hyperlegible-next) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +https://openfontlicense.org + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/app/fonts/ibm-plex-sans/IBMPlexSans-Bold.ttf b/app/fonts/ibm-plex-sans/IBMPlexSans-Bold.ttf new file mode 100644 index 00000000..1d66b1a2 Binary files /dev/null and b/app/fonts/ibm-plex-sans/IBMPlexSans-Bold.ttf differ diff --git a/app/fonts/ibm-plex-sans/IBMPlexSans-Medium.ttf b/app/fonts/ibm-plex-sans/IBMPlexSans-Medium.ttf new file mode 100644 index 00000000..a3826c4e Binary files /dev/null and b/app/fonts/ibm-plex-sans/IBMPlexSans-Medium.ttf differ diff --git a/app/fonts/ibm-plex-sans/IBMPlexSans-Regular.ttf b/app/fonts/ibm-plex-sans/IBMPlexSans-Regular.ttf new file mode 100644 index 00000000..bd6817d5 Binary files /dev/null and b/app/fonts/ibm-plex-sans/IBMPlexSans-Regular.ttf differ diff --git a/app/fonts/ibm-plex-sans/IBMPlexSans-SemiBold.ttf b/app/fonts/ibm-plex-sans/IBMPlexSans-SemiBold.ttf new file mode 100644 index 00000000..09ec8cda Binary files /dev/null and b/app/fonts/ibm-plex-sans/IBMPlexSans-SemiBold.ttf differ diff --git a/app/fonts/ibm-plex-sans/OFL.txt b/app/fonts/ibm-plex-sans/OFL.txt new file mode 100644 index 00000000..c35c4c61 --- /dev/null +++ b/app/fonts/ibm-plex-sans/OFL.txt @@ -0,0 +1,93 @@ +Copyright © 2017 IBM Corp. with Reserved Font Name "Plex" + +This Font Software is licensed under the SIL Open Font License, Version 1.1. + +This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/app/fonts/inter/Inter-Bold.ttf b/app/fonts/inter/Inter-Bold.ttf new file mode 100644 index 00000000..9fb9b751 Binary files /dev/null and b/app/fonts/inter/Inter-Bold.ttf differ diff --git a/app/fonts/inter/Inter-Medium.ttf b/app/fonts/inter/Inter-Medium.ttf new file mode 100644 index 00000000..458cd060 Binary files /dev/null and b/app/fonts/inter/Inter-Medium.ttf differ diff --git a/app/fonts/inter/Inter-Regular.ttf b/app/fonts/inter/Inter-Regular.ttf new file mode 100644 index 00000000..b7aaca8d Binary files /dev/null and b/app/fonts/inter/Inter-Regular.ttf differ diff --git a/app/fonts/inter/Inter-SemiBold.ttf b/app/fonts/inter/Inter-SemiBold.ttf new file mode 100644 index 00000000..47f8ab1d Binary files /dev/null and b/app/fonts/inter/Inter-SemiBold.ttf differ diff --git a/app/fonts/inter/OFL.txt b/app/fonts/inter/OFL.txt new file mode 100644 index 00000000..9b2ca37b --- /dev/null +++ b/app/fonts/inter/OFL.txt @@ -0,0 +1,92 @@ +Copyright (c) 2016 The Inter Project Authors (https://github.com/rsms/inter) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION AND CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/app/fonts/source-sans-3/OFL.txt b/app/fonts/source-sans-3/OFL.txt new file mode 100644 index 00000000..88c07f79 --- /dev/null +++ b/app/fonts/source-sans-3/OFL.txt @@ -0,0 +1,93 @@ +Copyright 2010-2022 Adobe (http://www.adobe.com/), with Reserved Font Name 'Source'. All Rights Reserved. Source is a trademark of Adobe in the United States and/or other countries. + +This Font Software is licensed under the SIL Open Font License, Version 1.1. + +This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/app/fonts/source-sans-3/SourceSans3-Bold.ttf b/app/fonts/source-sans-3/SourceSans3-Bold.ttf new file mode 100644 index 00000000..85693ccc Binary files /dev/null and b/app/fonts/source-sans-3/SourceSans3-Bold.ttf differ diff --git a/app/fonts/source-sans-3/SourceSans3-Medium.ttf b/app/fonts/source-sans-3/SourceSans3-Medium.ttf new file mode 100644 index 00000000..fbc97bf3 Binary files /dev/null and b/app/fonts/source-sans-3/SourceSans3-Medium.ttf differ diff --git a/app/fonts/source-sans-3/SourceSans3-Regular.ttf b/app/fonts/source-sans-3/SourceSans3-Regular.ttf new file mode 100644 index 00000000..c5dd0b88 Binary files /dev/null and b/app/fonts/source-sans-3/SourceSans3-Regular.ttf differ diff --git a/app/fonts/source-sans-3/SourceSans3-Semibold.ttf b/app/fonts/source-sans-3/SourceSans3-Semibold.ttf new file mode 100644 index 00000000..4aff9a3a Binary files /dev/null and b/app/fonts/source-sans-3/SourceSans3-Semibold.ttf differ diff --git a/app/lib/application/app.dart b/app/lib/application/app.dart index afa998ed..bf2819ec 100644 --- a/app/lib/application/app.dart +++ b/app/lib/application/app.dart @@ -1,4 +1,5 @@ import '../ui/adaptive/platform.dart'; +import '../ui/theme/app_font_controller.dart'; import '../ui/theme/app_theme.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -16,6 +17,7 @@ class MyApp extends StatelessWidget { this.cupertino, this.destination, this.onDestinationSelected, + this.fonts, }) : routerConfig = null; /// A host for a [Router]-owned location, so browser Back/Forward and deep @@ -25,6 +27,7 @@ class MyApp extends StatelessWidget { super.key, required RouterConfig this.routerConfig, this.cupertino, + this.fonts, }) : destinationBodies = const {}, destination = null, onDestinationSelected = null; @@ -35,20 +38,33 @@ class MyApp extends StatelessWidget { final ValueChanged? onDestinationSelected; final RouterConfig? routerConfig; + /// The device's interface font; defaults to the app's shared controller. + /// A change rebuilds the themes in place, without a restart. + final AppFontController? fonts; + @override Widget build(BuildContext context) { + final fonts = this.fonts ?? AppFontController.instance; + return ListenableBuilder( + listenable: fonts, + builder: (context, _) => _host(fonts.family), + ); + } + + Widget _host(String? fontFamily) { final router = routerConfig; if (cupertino ?? _platformIsCupertino) { + final theme = appCupertinoThemeWith(fontFamily: fontFamily); if (router != null) { return CupertinoApp.router( title: 'Pipelines', - theme: appCupertinoTheme, + theme: theme, routerConfig: router, ); } return CupertinoApp( title: 'Pipelines', - theme: appCupertinoTheme, + theme: theme, home: AdaptiveShell( destinationBodies: destinationBodies, cupertino: true, @@ -60,16 +76,16 @@ class MyApp extends StatelessWidget { if (router != null) { return MaterialApp.router( title: 'Pipelines', - theme: appLightTheme(), - darkTheme: appDarkTheme(), + theme: appLightTheme(fontFamily: fontFamily), + darkTheme: appDarkTheme(fontFamily: fontFamily), themeMode: ThemeMode.system, routerConfig: router, ); } return MaterialApp( title: 'Pipelines', - theme: appLightTheme(), - darkTheme: appDarkTheme(), + theme: appLightTheme(fontFamily: fontFamily), + darkTheme: appDarkTheme(fontFamily: fontFamily), themeMode: ThemeMode.system, home: AdaptiveShell( destinationBodies: destinationBodies, diff --git a/app/lib/application/bootstrap.dart b/app/lib/application/bootstrap.dart index eb9bd8af..3e8b016f 100644 --- a/app/lib/application/bootstrap.dart +++ b/app/lib/application/bootstrap.dart @@ -6,6 +6,8 @@ import 'app_url_strategy.dart'; import 'pipelines_root.dart'; import 'server_endpoint.dart'; import '../features/settings/settings_store.dart'; +import '../ui/theme/app_font_controller.dart'; +import '../ui/theme/app_font_licenses.dart'; /// Native builds must supply their server explicitly; there is no baked host. const configuredServerUrl = String.fromEnvironment('PIPELINES_SERVER_URL'); @@ -38,6 +40,10 @@ Future resolveLaunchServer({ ); } +/// How long the first frame waits for a stored font before drawing in the +/// system font. +const appFontStartupWait = Duration(milliseconds: 1500); + /// Resolves the endpoint for this launch and starts the normal composition. Future runPipelinesApp({ Uri? base, @@ -46,8 +52,13 @@ Future runPipelinesApp({ }) async { useAppUrlStrategy(); WidgetsFlutterBinding.ensureInitialized(); + registerAppFontLicenses(); + // The device's chosen font loads beside the session; a slow download does + // not hold the first frame, and applies when it lands. + final font = AppFontController.instance.restore(wait: appFontStartupWait); // A phone behind the web login reconnects with its kept session. await ServerSession.load(); + await font; final server = await resolveLaunchServer( base: base, configured: configured, diff --git a/app/lib/application/pipelines_root.dart b/app/lib/application/pipelines_root.dart index 58571328..ca121bde 100644 --- a/app/lib/application/pipelines_root.dart +++ b/app/lib/application/pipelines_root.dart @@ -14,6 +14,7 @@ import '../features/onboarding/onboarding_flow.dart'; import '../features/settings/server_connections.dart'; import '../features/settings/saved_servers_section.dart'; import '../features/settings/settings_store.dart'; +import '../ui/theme/app_font_controller.dart'; import '../ui/theme/app_theme.dart'; import 'pipelines_app.dart'; import 'server_endpoint.dart'; @@ -294,19 +295,23 @@ class _PipelinesRootState extends State { _servers.catalog.active?.endpoints.map(Uri.parse).toList() ?? const [], ); - return _cupertino - ? CupertinoApp( - title: 'Pipelines', - theme: appCupertinoTheme, - debugShowCheckedModeBanner: false, - home: flow, - ) - : MaterialApp( - title: 'Pipelines', - theme: appLightTheme(), - darkTheme: appDarkTheme(), - debugShowCheckedModeBanner: false, - home: flow, - ); + final fonts = AppFontController.instance; + return ListenableBuilder( + listenable: fonts, + builder: (context, _) => _cupertino + ? CupertinoApp( + title: 'Pipelines', + theme: appCupertinoThemeWith(fontFamily: fonts.family), + debugShowCheckedModeBanner: false, + home: flow, + ) + : MaterialApp( + title: 'Pipelines', + theme: appLightTheme(fontFamily: fonts.family), + darkTheme: appDarkTheme(fontFamily: fonts.family), + debugShowCheckedModeBanner: false, + home: flow, + ), + ); } } 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_markdown_view.dart b/app/lib/features/chat/ui/transcript/chat_markdown_view.dart index 0b755ccc..fe68720c 100644 --- a/app/lib/features/chat/ui/transcript/chat_markdown_view.dart +++ b/app/lib/features/chat/ui/transcript/chat_markdown_view.dart @@ -5,6 +5,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/semantics.dart'; import '../../../../ui/layout/wheel_scroll_forwarding.dart'; +import '../../../../ui/text/monospace.dart'; import '../sessions/chat_adaptive.dart'; import 'chat_link_launcher.dart'; import 'chat_link_spans.dart'; @@ -209,7 +210,12 @@ class _ChatMarkdownViewState extends State { decoration: BoxDecoration(color: _codeFill(context, cupertino)), child: Padding( padding: const EdgeInsets.all(12), - child: HorizontalScrollView(child: Text(code)), + child: HorizontalScrollView( + child: Text( + code, + style: const TextStyle(fontFamily: appMonospaceFamily), + ), + ), ), ), ], @@ -262,9 +268,12 @@ class _ChatMarkdownViewState extends State { MarkdownPlain(text: final text) => TextSpan(text: text), MarkdownInlineCode(text: final text) => TextSpan( text: text, - // The same host-theme surface as a fenced block marks the run as - // code without a font, size or literal colour override. - style: TextStyle(backgroundColor: _codeFill(context, cupertino)), + // The same host-theme surface and monospace family as a fenced + // block mark the run as code; the size stays the surrounding text's. + style: TextStyle( + fontFamily: appMonospaceFamily, + backgroundColor: _codeFill(context, cupertino), + ), ), MarkdownStrong(spans: final inner) => TextSpan( style: const TextStyle(fontWeight: FontWeight.bold), 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/features/runs/domain/run_models.dart b/app/lib/features/runs/domain/run_models.dart index 118966b2..90bc23b7 100644 --- a/app/lib/features/runs/domain/run_models.dart +++ b/app/lib/features/runs/domain/run_models.dart @@ -1,5 +1,6 @@ import '../../pipelines/pipeline_format.dart'; import '../../../core/api_gateway.dart'; +import 'run_tokens.dart'; const runStates = [ 'queued', @@ -268,6 +269,19 @@ List sortRuns(List runs, {String sort = 'newest_first'}) { primary = cmp(bt ?? -_maxMillis, at ?? -_maxMillis); } break; + case 'most_tokens': + // Runs with no reported tokens (older servers, no model stage) sort + // after every reported total; a partial total sorts by what was + // reported. + final ta = runTokenUsage(a)?.total, tb = runTokenUsage(b)?.total; + primary = cmp(ta == null ? 1 : 0, tb == null ? 1 : 0); + if (primary == 0 && ta != null && tb != null) { + primary = tb.compareTo(ta); + } + if (primary == 0) { + primary = cmp(bt ?? -_maxMillis, at ?? -_maxMillis); + } + break; case 'pipeline_name': primary = '${a['name'] ?? ''}'.toLowerCase().compareTo( '${b['name'] ?? ''}'.toLowerCase(), diff --git a/app/lib/features/runs/domain/run_tokens.dart b/app/lib/features/runs/domain/run_tokens.dart new file mode 100644 index 00000000..4189c7d7 --- /dev/null +++ b/app/lib/features/runs/domain/run_tokens.dart @@ -0,0 +1,167 @@ +import '../../../core/api_gateway.dart'; + +/// Reported token and cost usage of a run or one stage. No widgets here. +/// +/// Every figure is what the server recorded from the harness: nothing is +/// estimated. A run total is partial when some stages that recorded usage +/// reported no token count, and it is then labelled as a lower bound (`≥`). +class TokenUsage { + const TokenUsage({ + required this.total, + required this.partial, + this.input, + this.output, + this.cacheRead, + this.cost, + this.costPartial = false, + this.stages, + this.tokenStages, + }); + + /// The summed reported total. + final int total; + + /// Some stages that recorded usage reported no token count. + final bool partial; + + /// The input/output split, only when every counted stage reported it. + final int? input; + final int? output; + + /// The cache-read share of [total], only when every counted stage reported + /// its cache components. Older rows never recorded them, so they have none. + final int? cacheRead; + + /// The tokens that were not cache reads, when [cacheRead] is known. + int? get fresh => cacheRead == null ? null : total - cacheRead!; + + /// The reported cost in USD, only when a positive cost was reported. Some + /// providers (subscription models) report a cost of zero or none at all; + /// that is "not reported", never a known zero. + final double? cost; + + /// Some stages reported no cost, so [cost] is a lower bound. + final bool costPartial; + + /// Run totals only: the stages that recorded usage, and those of them that + /// reported tokens. + final int? stages; + final int? tokenStages; +} + +/// The run's reported token usage from its `usage` total, or null when the +/// server sent none (an older server's list, or a run with no model usage) or +/// no stage reported a token count. +TokenUsage? runTokenUsage(JsonMap run) { + final raw = run['usage']; + if (raw is! Map) return null; + final stages = _int(raw['stages']); + final tokenStages = _int(raw['tokens_known_stages']) ?? 0; + final total = _int(raw['tokens_total']); + if (tokenStages <= 0 || total == null) return null; + final costStages = _int(raw['cost_known_stages']) ?? 0; + final split = raw['split_known'] == true; + final cost = costStages > 0 ? _positive(raw['cost_usd']) : null; + return TokenUsage( + total: total, + partial: stages != null && tokenStages < stages, + // The server omits a zero in/out, so a known split makes absence zero. + input: split ? _int(raw['tokens_in']) ?? 0 : null, + output: split ? _int(raw['tokens_out']) ?? 0 : null, + cacheRead: _cacheRead(raw, total), + cost: cost, + costPartial: cost != null && stages != null && costStages < stages, + stages: stages, + tokenStages: tokenStages, + ); +} + +/// One stage's reported token usage from its step `usage`, or null when the +/// stage reported no token count. +TokenUsage? stageTokenUsage(JsonMap step) { + final raw = step['usage']; + if (raw is! Map || raw['tokens_known'] != true) return null; + final total = _int(raw['tokens_total']); + if (total == null) return null; + final split = raw['split_known'] == true; + return TokenUsage( + total: total, + partial: false, + input: split ? _int(raw['tokens_in']) ?? 0 : null, + output: split ? _int(raw['tokens_out']) ?? 0 : null, + cacheRead: _cacheRead(raw, total), + cost: raw['cost_known'] == true ? _positive(raw['cost_usd']) : null, + ); +} + +/// A compact token count: `950`, `1.2K`, `340K`, `22.4M`, `1.3B`. +/// +/// Decimal multiples, one decimal below a hundred units, whole units above. +String formatTokenCount(int tokens) { + if (tokens < 1000) return '$tokens'; + const units = ['K', 'M', 'B', 'T']; + var value = tokens / 1000; + var unit = 0; + while (value >= 1000 && unit < units.length - 1) { + value /= 1000; + unit++; + } + // Rounding 999.6K up would print "1000K"; promote it instead. + if (value >= 999.5 && unit < units.length - 1) { + value /= 1000; + unit++; + } + final text = value < 99.95 + ? value.toStringAsFixed(1).replaceFirst(RegExp(r'\.0$'), '') + : value.round().toString(); + return '$text${units[unit]}'; +} + +/// The list row's compact label: `22.4M tokens`, or `≥22.4M tokens` when some +/// stages did not report a count. +String tokenUsageLabel(TokenUsage usage) => + '${usage.partial ? '≥' : ''}${formatTokenCount(usage.total)} tokens'; + +/// The detail's token value: the compact total, then fresh versus cached +/// when every stage reported its cache components, the in/out split when +/// reported, and how many stages reported when the total is partial. +/// `22.4M · 1.1M fresh, 21.3M cached · 900K in, 150K out`, +/// `≥9 · 1 of 2 stages reported`. +String tokenUsageDetail(TokenUsage usage) => [ + '${usage.partial ? '≥' : ''}${formatTokenCount(usage.total)}', + if (usage.fresh != null) + '${formatTokenCount(usage.fresh!)} fresh, ' + '${formatTokenCount(usage.cacheRead!)} cached', + if (usage.input != null && usage.output != null) + '${formatTokenCount(usage.input!)} in, ' + '${formatTokenCount(usage.output!)} out', + if (usage.partial && usage.stages != null) + '${usage.tokenStages} of ${usage.stages} stages reported', +].join(' · '); + +/// The wording for a cost no stage reported as a positive amount. +const costNotReported = 'Not reported'; + +/// The reported cost: `$1.75`, `$0.10`, `$0.0042` (four places below a cent), +/// `≥$0.10` when some stages reported no cost, and [costNotReported] when no +/// positive cost was reported. +String tokenUsageCost(TokenUsage usage) { + final cost = usage.cost; + if (cost == null) return costNotReported; + final text = cost >= 0.01 ? cost.toStringAsFixed(2) : cost.toStringAsFixed(4); + return '${usage.costPartial ? '≥' : ''}\$$text'; +} + +int? _int(dynamic value) => + value is num && value.isFinite && value >= 0 ? value.round() : null; + +double? _positive(dynamic value) => + value is num && value.isFinite && value > 0 ? value.toDouble() : null; + +/// The cache reads, only when the server marked the cache split complete and +/// the count fits inside the total. +int? _cacheRead(Map raw, int total) { + if (raw['cache_known'] != true) return null; + final read = _int(raw['cache_read']) ?? 0; + return read <= total ? read : null; +} diff --git a/app/lib/features/runs/ui/run_detail_content.dart b/app/lib/features/runs/ui/run_detail_content.dart index 1412fc13..bb296eb4 100644 --- a/app/lib/features/runs/ui/run_detail_content.dart +++ b/app/lib/features/runs/ui/run_detail_content.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import '../../../core/api_gateway.dart'; import '../../pipelines/pipeline_format.dart' show formatDuration; import '../domain/run_models.dart'; +import '../domain/run_tokens.dart'; /// One labelled value in the run detail's metrics block. typedef DetailMetric = ({String label, String value}); @@ -23,6 +24,7 @@ typedef DetailVisit = ({ String outcome, String target, String? duration, + String? tokens, }); /// The run's primary label: the stored title, else the pipeline name, and only @@ -74,6 +76,7 @@ List detailUsageMetrics(dynamic raw) { final tokenStages = _int(usage['tokens_known_stages']) ?? 0; final costStages = _int(usage['cost_known_stages']) ?? 0; final totalTokens = _int(usage['tokens_total']); + final cost = usage['cost_usd'] is num ? usage['cost_usd'] as num : null; final splitKnown = usage['split_known'] == true && tokenStages > 0; final rows = [ if (stages != null) (label: 'Stages', value: '$stages'), @@ -93,11 +96,11 @@ List detailUsageMetrics(dynamic raw) { ), ( label: 'Reported cost', - // cost_usd is omitted by the server when zero, so a known stage count - // makes an absent value a real $0.000000 rather than an unknown cost. - value: costStages == 0 - ? 'Unavailable' - : '\$${((usage['cost_usd'] as num?) ?? 0).toStringAsFixed(6)}', + // A cost of zero (or none) is what subscription models report even + // with a "known" cost: it is not reported, never a measured zero. + value: costStages == 0 || cost == null || cost <= 0 + ? 'Not reported' + : '\$${cost.toStringAsFixed(6)}', ), ]; if (stages != null && tokenStages < stages) { @@ -149,6 +152,10 @@ List detailTimeline(dynamic steps) { outcome: '${step['outcome'] ?? ''}'.trim(), target: '${step['target'] ?? ''}'.trim(), duration: _visitDuration(step), + tokens: switch (stageTokenUsage(JsonMap.from(step))) { + final usage? => tokenUsageLabel(usage), + null => null, + }, ); }(), ]; diff --git a/app/lib/features/runs/ui/run_detail_summary_fields.dart b/app/lib/features/runs/ui/run_detail_summary_fields.dart index 0234eb57..3124c529 100644 --- a/app/lib/features/runs/ui/run_detail_summary_fields.dart +++ b/app/lib/features/runs/ui/run_detail_summary_fields.dart @@ -1,5 +1,7 @@ import '../../../core/api_gateway.dart'; import '../../pipelines/pipeline_format.dart'; +import '../domain/run_deletion.dart' show formatByteSize, runSizeBytes; +import '../domain/run_tokens.dart'; import 'run_detail_visit_facts.dart'; /// One recorded Summary row: the web run summary's key/value shape. @@ -14,9 +16,9 @@ typedef DetailSummaryRow = ({ bool block, }); -/// The run Summary's rows, in the web summary's order: Pipeline, Stage, -/// Workspace, Content hash, Started, Finished, Duration, Retry of and the -/// Diagnostic block. +/// The run Summary's rows: Pipeline, Stage, Workspace, Content hash, Started, +/// Finished, Duration, Size on disk, Tokens, Cost, Retry of and the Diagnostic +/// block. /// /// Only values the server actually recorded are rows: a blank field is omitted /// rather than drawn as an empty or invented placeholder, exactly as @@ -53,6 +55,16 @@ List detailSummaryRows(JsonMap run) { _text(run['finished_at']), ); if (seconds != null) add('Duration', formatDuration(seconds)); + // What the run occupies on disk and what it spent, as the server measured + // and the harness reported; an unmeasured size or unreported usage has no + // row. + final bytes = runSizeBytes(run); + if (bytes != null) add('Size on disk', formatByteSize(bytes)); + final usage = runTokenUsage(run); + if (usage != null) { + add('Tokens', tokenUsageDetail(usage)); + add('Cost', tokenUsageCost(usage)); + } final retryOf = _number(run['retry_of'])?.toInt() ?? 0; if (retryOf > 0) add('Retry of', 'run $retryOf'); // A diagnostic is a recorded document: its own whitespace is preserved, but a diff --git a/app/lib/features/runs/ui/run_detail_tab_bodies.dart b/app/lib/features/runs/ui/run_detail_tab_bodies.dart index fd5ae2c6..753f62f8 100644 --- a/app/lib/features/runs/ui/run_detail_tab_bodies.dart +++ b/app/lib/features/runs/ui/run_detail_tab_bodies.dart @@ -16,7 +16,7 @@ import '../../../ui/adaptive/floating_bar_layout.dart'; /// the stored original prompt with its copy control and the collapsible Input /// and Output sections. /// -/// The web run detail has no usage block, so no metrics are drawn here. A fact +/// The run's size and reported token usage are facts like the others. A fact /// the server did not record is omitted rather than shown as an invented or /// `Unavailable` row; only the Input/Output empty sentences remain, and only /// when that section recorded nothing. diff --git a/app/lib/features/runs/ui/run_detail_timeline.dart b/app/lib/features/runs/ui/run_detail_timeline.dart index 60372328..00a68093 100644 --- a/app/lib/features/runs/ui/run_detail_timeline.dart +++ b/app/lib/features/runs/ui/run_detail_timeline.dart @@ -135,8 +135,8 @@ class RunTimelineRow extends StatelessWidget { @override Widget build(BuildContext context) { final state = _state; - // The outcome chip leads the second line, then where the visit went; the - // duration is the row's time. + // The outcome chip leads the second line, then where the visit went and + // the tokens it reported; the duration is the row's time. return AppListRow( cupertino: cupertino, selected: selected, @@ -152,7 +152,14 @@ class RunTimelineRow extends StatelessWidget { title: Text(visit.title), time: visit.duration, subtitleLeading: _outcome(context), - subtitle: visit.target.isEmpty ? null : Text('→ ${visit.target}'), + subtitle: visit.target.isEmpty && visit.tokens == null + ? null + : Text( + [ + if (visit.target.isNotEmpty) '→ ${visit.target}', + ?visit.tokens, + ].join(' · '), + ), ); } } diff --git a/app/lib/features/runs/ui/run_detail_visit_facts.dart b/app/lib/features/runs/ui/run_detail_visit_facts.dart index a631d183..8a11d4d3 100644 --- a/app/lib/features/runs/ui/run_detail_visit_facts.dart +++ b/app/lib/features/runs/ui/run_detail_visit_facts.dart @@ -1,6 +1,7 @@ import '../../../core/api_gateway.dart'; import '../../graph/domain/stage_definition.dart'; import '../../pipelines/pipeline_format.dart'; +import '../domain/run_tokens.dart'; import 'run_detail_content.dart'; /// The exact empty wording for a visit's recorded Input and Output. They are @@ -80,6 +81,11 @@ List detailVisitFacts(JsonMap step) { if (duration != null) facts.add((label: 'Duration', value: duration)); final model = detailVisitProviderModel(step); if (model != null) facts.add((label: 'Provider/model', value: model)); + final usage = stageTokenUsage(step); + if (usage != null) { + facts.add((label: 'Tokens', value: tokenUsageDetail(usage))); + facts.add((label: 'Cost', value: tokenUsageCost(usage))); + } final target = _text(step['target']); if (target != null) facts.add((label: 'Target', value: target)); return facts; diff --git a/app/lib/features/runs/ui/run_list_row.dart b/app/lib/features/runs/ui/run_list_row.dart index 36f1d065..e662fb5a 100644 --- a/app/lib/features/runs/ui/run_list_row.dart +++ b/app/lib/features/runs/ui/run_list_row.dart @@ -9,6 +9,7 @@ import '../../../ui/status/run_status_icon.dart'; import '../../../ui/text/relative_time_label.dart'; import '../domain/run_deletion.dart'; import '../domain/run_models.dart'; +import '../domain/run_tokens.dart'; import 'run_more_menu.dart'; /// One run in the list: a status avatar, title/time, and one muted fact @@ -77,6 +78,9 @@ class RunListRow extends StatelessWidget { final scheduled = run['scheduled'] == true; final bytes = runSizeBytes(run); final size = bytes == null ? null : formatByteSize(bytes); + // Reported tokens sit beside the size; an older server sends none. + final usage = runTokenUsage(run); + final tokens = usage == null ? null : tokenUsageLabel(usage); final createdAt = DateTime.tryParse('${run['created_at'] ?? ''}'); final subtitle = [ ?pipelineLabel, @@ -84,6 +88,7 @@ class RunListRow extends StatelessWidget { ?facts.duration, ?facts.progress, ?size, + ?tokens, ].join(' · '); // A narrow pane keeps the decision facts: the pipeline, and progress // while executing (duration only when there is no pipeline to show). @@ -93,6 +98,7 @@ class RunListRow extends StatelessWidget { if (run['executing'] == true && facts.progress != null) facts.progress!, if (pipelineLabel == null && facts.duration != null) facts.duration!, ?size, + ?tokens, ].join(' · '); final state = runStateOf(run['status']); return AppListRow( diff --git a/app/lib/features/runs/ui/runs_controller.dart b/app/lib/features/runs/ui/runs_controller.dart index 3ac6854d..8f0e2fa8 100644 --- a/app/lib/features/runs/ui/runs_controller.dart +++ b/app/lib/features/runs/ui/runs_controller.dart @@ -162,14 +162,7 @@ class RunsController extends ChangeNotifier { // A cancel/decision reply changes lifecycle fields as well as title. // Keep sparse/unknown summary fields, without copying bulky detail-only // payloads into the held list or inventing missing values. - const detailOnly = { - 'steps', - 'input', - 'output', - 'graph', - 'usage', - 'notifications', - }; + const detailOnly = {'steps', 'input', 'output', 'graph', 'notifications'}; runs = [...runs] ..[index] = { ...runs[index], diff --git a/app/lib/features/runs/ui/runs_filter_options.dart b/app/lib/features/runs/ui/runs_filter_options.dart index 87a57a8f..266dd986 100644 --- a/app/lib/features/runs/ui/runs_filter_options.dart +++ b/app/lib/features/runs/ui/runs_filter_options.dart @@ -7,6 +7,7 @@ const runSorts = [ ('newest_first', 'Newest first'), ('oldest_first', 'Oldest first'), ('largest_first', 'Largest first'), + ('most_tokens', 'Most tokens'), ('longest_duration', 'Longest duration'), ('shortest_duration', 'Shortest duration'), ('pipeline_name', 'Pipeline name'), @@ -25,8 +26,8 @@ const runTimeRanges = [ ]; /// Sorts that list runs flat instead of under day headings: an order by -/// size has no meaningful day grouping. -const flatRunSorts = {'largest_first'}; +/// size or token count has no meaningful day grouping. +const flatRunSorts = {'largest_first', 'most_tokens'}; /// The pipelines the loaded runs belong to, by name then id. List<({int id, String name})> runFilterPipelines(RunsController controller) { diff --git a/app/lib/features/settings/settings_font_section.dart b/app/lib/features/settings/settings_font_section.dart new file mode 100644 index 00000000..049aba7f --- /dev/null +++ b/app/lib/features/settings/settings_font_section.dart @@ -0,0 +1,139 @@ +import 'dart:async'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../ui/theme/app_font.dart'; +import '../../ui/theme/app_font_controller.dart'; +import 'settings_group.dart'; +import 'settings_row.dart'; +import 'settings_theme.dart'; + +const fontSectionTitle = 'Font'; +const fontSectionFooter = + 'Saved on this device only. Code keeps its monospace font.'; +const fontLoadingText = 'Loading…'; +const fontLoadFailedText = 'Could not load this font.'; +const fontSaveFailedText = 'Not saved on this device; it applies until reload.'; + +ValueKey settingsFontKey(AppFont font) => + ValueKey('settings-font-${font.id}'); + +/// The device's interface font: one row per option, each named in its own +/// face. Material rows are radio tiles, Cupertino rows carry the checkmark. +/// A choice is stored locally and applies as soon as its files load. +class SettingsFontSection extends StatefulWidget { + const SettingsFontSection({super.key, required this.controller}); + + final AppFontController controller; + + @override + State createState() => _SettingsFontSectionState(); +} + +class _SettingsFontSectionState extends State { + @override + void initState() { + super.initState(); + _preview(); + } + + @override + void didUpdateWidget(SettingsFontSection oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.controller != widget.controller) _preview(); + } + + /// Only the regular weight of each option, only once Settings is shown. + void _preview() { + for (final font in AppFont.values) { + unawaited(widget.controller.preview(font)); + } + } + + @override + Widget build(BuildContext context) => ListenableBuilder( + listenable: widget.controller, + builder: (context, _) => _group(context), + ); + + Widget _group(BuildContext context) { + final controller = widget.controller; + final cupertino = usesCupertinoControls(context); + final group = SettingsGroup( + title: fontSectionTitle, + footer: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + const Text(fontSectionFooter), + if (controller.saveFailed) + Text( + fontSaveFailedText, + style: TextStyle(color: settingsErrorColor(context)), + ), + ], + ), + children: [ + for (final font in AppFont.values) + cupertino + ? _cupertinoRow(context, font) + : RadioListTile( + key: settingsFontKey(font), + value: font, + title: _name(context, font), + subtitle: _status(context, font), + ), + ], + ); + if (cupertino) return group; + return RadioGroup( + groupValue: controller.selected, + onChanged: (font) { + if (font != null) unawaited(controller.select(font)); + }, + child: group, + ); + } + + Widget _cupertinoRow(BuildContext context, AppFont font) => SettingsRow( + key: settingsFontKey(font), + title: _name(context, font), + subtitle: _status(context, font), + trailing: widget.controller.selected == font + ? Icon( + CupertinoIcons.check_mark, + color: CupertinoTheme.of(context).primaryColor, + ) + : null, + onTap: () => unawaited(widget.controller.select(font)), + ); + + /// The option's name in its own face once its regular weight has loaded; + /// the system face until then (and always for the system option). + Widget _name(BuildContext context, AppFont font) { + final family = widget.controller.canPreview(font) + ? font.family ?? _systemFamily(context) + : _systemFamily(context); + return Text(font.label, style: TextStyle(fontFamily: family)); + } + + Widget? _status(BuildContext context, AppFont font) { + final controller = widget.controller; + if (controller.selected != font) return null; + if (controller.loadFailed == font) { + return Text( + fontLoadFailedText, + style: TextStyle(color: settingsErrorColor(context)), + ); + } + return controller.loading ? const Text(fontLoadingText) : null; + } +} + +/// The platform's own interface family (Roboto, or the Cupertino system +/// face), independent of the font the theme currently applies. +String? _systemFamily(BuildContext context) => usesCupertinoControls(context) + ? const CupertinoTextThemeData().textStyle.fontFamily + : Typography.material2021( + platform: Theme.of(context).platform, + ).black.bodyLarge?.fontFamily; diff --git a/app/lib/features/settings/settings_screen.dart b/app/lib/features/settings/settings_screen.dart index 9b0b4e2e..943b1296 100644 --- a/app/lib/features/settings/settings_screen.dart +++ b/app/lib/features/settings/settings_screen.dart @@ -10,6 +10,8 @@ import '../attention/notify/notification_controller.dart'; import 'default_endpoint.dart'; import 'settings_controller.dart'; import 'settings_controls.dart'; +import 'settings_font_section.dart'; +import '../../ui/theme/app_font_controller.dart'; import 'settings_group.dart'; import 'settings_page_body.dart'; import 'settings_notifications_section.dart'; @@ -53,8 +55,12 @@ class SettingsScreen extends StatefulWidget { this.servers, this.deliveryService, this.deliveryServiceController, + this.fonts, }); + /// The device's interface font; defaults to the app's shared controller. + final AppFontController? fonts; + /// Android background delivery; hidden when absent or unsupported. final NotificationServiceBridge? deliveryService; final NotificationServiceController? deliveryServiceController; @@ -190,6 +196,10 @@ class _SettingsScreenState extends State { controller: _controller, workspaceField: _workspaceField, ), + // A device-local choice: a browser and a phone keep their own. + SettingsFontSection( + controller: widget.fonts ?? AppFontController.instance, + ), if (widget.notifications case final notifications?) SettingsNotificationsSection(controller: notifications), if (widget.deliveryService case final service? when service.supported) 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/lib/ui/theme/app_font.dart b/app/lib/ui/theme/app_font.dart new file mode 100644 index 00000000..491378ae --- /dev/null +++ b/app/lib/ui/theme/app_font.dart @@ -0,0 +1,82 @@ +/// The interface typefaces a device can choose. [AppFont.system] keeps the +/// platform's own typography (Roboto on Android and the web, SF on iOS); every +/// other option is an open-licence (SIL OFL 1.1) family bundled under +/// `fonts//` as plain assets, not under pubspec `fonts:`, so a web +/// page downloads only the family it actually shows. +enum AppFont { + system('system', 'System default', null, null, []), + inter('inter', 'Inter', 'Inter', 'inter', [ + 'Inter-Regular.ttf', + 'Inter-Medium.ttf', + 'Inter-SemiBold.ttf', + 'Inter-Bold.ttf', + ]), + ibmPlexSans( + 'ibm-plex-sans', + 'IBM Plex Sans', + 'IBM Plex Sans', + 'ibm-plex-sans', + [ + 'IBMPlexSans-Regular.ttf', + 'IBMPlexSans-Medium.ttf', + 'IBMPlexSans-SemiBold.ttf', + 'IBMPlexSans-Bold.ttf', + ], + ), + sourceSans3( + 'source-sans-3', + 'Source Sans 3', + 'Source Sans 3', + 'source-sans-3', + [ + 'SourceSans3-Regular.ttf', + 'SourceSans3-Medium.ttf', + 'SourceSans3-Semibold.ttf', + 'SourceSans3-Bold.ttf', + ], + ), + atkinsonHyperlegible( + 'atkinson-hyperlegible-next', + 'Atkinson Hyperlegible Next', + 'Atkinson Hyperlegible Next', + 'atkinson-hyperlegible-next', + [ + 'AtkinsonHyperlegibleNext-Regular.ttf', + 'AtkinsonHyperlegibleNext-Medium.ttf', + 'AtkinsonHyperlegibleNext-SemiBold.ttf', + 'AtkinsonHyperlegibleNext-Bold.ttf', + ], + ); + + const AppFont(this.id, this.label, this.family, this._directory, this._files); + + /// The stored value. Stable across releases; never the display label. + final String id; + + /// The picker's name for the option. + final String label; + + /// The registered family name; null keeps the platform typography. + final String? family; + + final String? _directory; + final List _files; + + /// Every weight's asset (400, 500, 600 and 700, in that order). + List get assets => [ + for (final file in _files) 'fonts/$_directory/$file', + ]; + + /// The regular weight alone: enough to preview the option's name. + String? get previewAsset => _files.isEmpty ? null : assets.first; + + /// The licence shipped beside the files. + String? get licenseAsset => + _directory == null ? null : 'fonts/$_directory/OFL.txt'; + + /// The option a stored id names; an unknown or missing id is the default. + static AppFont fromId(String? id) => AppFont.values.firstWhere( + (font) => font.id == id, + orElse: () => AppFont.system, + ); +} diff --git a/app/lib/ui/theme/app_font_controller.dart b/app/lib/ui/theme/app_font_controller.dart new file mode 100644 index 00000000..3109282f --- /dev/null +++ b/app/lib/ui/theme/app_font_controller.dart @@ -0,0 +1,156 @@ +import 'dart:async'; + +import 'package:flutter/services.dart'; +import 'package:flutter/widgets.dart'; + +import 'app_font.dart'; +import 'app_font_store.dart'; + +/// Registers [assets] under [family]. Production reads them from the bundle; +/// tests substitute a recorder. +typedef AppFontAssetLoader = + Future Function(String family, List assets); + +/// Loads bundled font files on demand with a [FontLoader]. On the web each +/// asset is fetched only now, when its family is first previewed or chosen. +Future loadBundledFontAssets(String family, List assets) async { + final loader = FontLoader(family); + for (final asset in assets) { + loader.addFont(rootBundle.load(asset)); + } + await loader.load(); +} + +/// The device's interface font: the stored choice, the family the theme +/// actually uses, and the on-demand loading between the two. +/// +/// The theme keeps the previous family (at first the platform's) until the +/// chosen files are registered, then switches once; text never lays out in a +/// family that has not loaded, so nothing reflows more than once. +class AppFontController extends ChangeNotifier { + AppFontController({ + this._store = const SharedPreferencesAppFontStore(), + this._loader = loadBundledFontAssets, + }); + + /// The app's own controller: one per running app, shared by the theme + /// hosts and the Settings picker. + static final AppFontController instance = AppFontController(); + + final AppFontStore _store; + final AppFontAssetLoader _loader; + final Map> _assetLoads = >{}; + final Set _loadedAssets = {}; + + AppFont _selected = AppFont.system; + AppFont _applied = AppFont.system; + bool _saveFailed = false; + AppFont? _loadFailed; + + /// The option the picker shows as chosen. + AppFont get selected => _selected; + + /// The option the theme uses now. + AppFont get applied => _applied; + + /// The theme's family; null keeps the platform typography. + String? get family => _applied.family; + + /// The chosen family's files are still downloading. + bool get loading => _selected != _applied && _loadFailed != _selected; + + /// The last choice could not be stored on this device. + bool get saveFailed => _saveFailed; + + /// The chosen family's files could not be loaded; the theme kept [applied]. + AppFont? get loadFailed => _loadFailed; + + /// Whether [font]'s regular weight is registered, so its name can be drawn + /// in its own face. + bool canPreview(AppFont font) => + font.previewAsset == null || _loadedAssets.contains(font.previewAsset); + + /// Reads the stored choice and starts loading it. With [wait], returns once + /// it is applied or after [wait], whichever is first; a slower load still + /// applies when it finishes. + Future restore({Duration? wait}) async { + final font = await _store.load(); + if (font == AppFont.system || font == _selected) return; + _selected = font; + notifyListeners(); + final applying = _apply(font); + if (wait == null) return applying; + await Future.any(>[ + applying, + Future.delayed(wait), + ]); + } + + /// Chooses [font], stores it, and applies it once its files are loaded. + Future select(AppFont font) async { + if (font == _selected && _loadFailed != font) return; + _selected = font; + _loadFailed = null; + notifyListeners(); + final saved = await _store.save(font); + if (saved == _saveFailed) { + _saveFailed = !saved; + notifyListeners(); + } + await _apply(font); + } + + /// Loads [font]'s regular weight for its picker row. Failures are quiet: the + /// row keeps the current face and choosing it reports the problem. + Future preview(AppFont font) async { + final asset = font.previewAsset; + final family = font.family; + if (asset == null || family == null || _loadedAssets.contains(asset)) { + return; + } + try { + await _ensure(family, [asset]); + } catch (_) { + return; + } + notifyListeners(); + } + + Future _apply(AppFont font) async { + final family = font.family; + if (family != null) { + try { + await _ensure(family, font.assets); + } catch (_) { + if (_selected == font) { + _loadFailed = font; + notifyListeners(); + } + return; + } + } + // A later choice supersedes this one. + if (_selected != font || _applied == font) return; + _applied = font; + notifyListeners(); + } + + /// Registers the assets not yet loaded or loading, in one [FontLoader], and + /// waits for all of [assets]. A failed asset can be retried. + Future _ensure(String family, List assets) { + final missing = assets.where((a) => !_assetLoads.containsKey(a)).toList(); + if (missing.isNotEmpty) { + final load = _loader(family, missing); + for (final asset in missing) { + _assetLoads[asset] = load.then( + (_) => _loadedAssets.add(asset), + onError: (Object error, StackTrace stack) { + _assetLoads.remove(asset); + Error.throwWithStackTrace(error, stack); + }, + ); + } + } + return Future.wait(assets.map((a) => _assetLoads[a]!)); + } +} diff --git a/app/lib/ui/theme/app_font_licenses.dart b/app/lib/ui/theme/app_font_licenses.dart new file mode 100644 index 00000000..e7b5e631 --- /dev/null +++ b/app/lib/ui/theme/app_font_licenses.dart @@ -0,0 +1,23 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; + +import 'app_font.dart'; + +bool _registered = false; + +/// Adds each bundled interface font's SIL Open Font License to the +/// [LicenseRegistry]. The texts are read from the bundle only when a licence +/// page asks for them. +void registerAppFontLicenses() { + if (_registered) return; + _registered = true; + LicenseRegistry.addLicense(() async* { + for (final font in AppFont.values) { + final asset = font.licenseAsset; + if (asset == null) continue; + yield LicenseEntryWithLineBreaks([ + font.label, + ], await rootBundle.loadString(asset)); + } + }); +} diff --git a/app/lib/ui/theme/app_font_store.dart b/app/lib/ui/theme/app_font_store.dart new file mode 100644 index 00000000..6a2ec375 --- /dev/null +++ b/app/lib/ui/theme/app_font_store.dart @@ -0,0 +1,60 @@ +import 'package:shared_preferences/shared_preferences.dart'; + +import 'app_font.dart'; + +/// The device's font choice. It lives in this device's (or browser's) own +/// preferences and is never sent to a server, so a phone and a browser signed +/// in to the same server keep their own choice. +const String appFontKey = 'pipelines.next.font.v1'; + +/// The persistence seam for the font choice. +abstract interface class AppFontStore { + Future load(); + + /// Whether the choice is actually stored. + Future save(AppFont font); +} + +/// `shared_preferences` storage. An unavailable or unreadable store means the +/// default; a failed save returns false rather than throwing. +class SharedPreferencesAppFontStore implements AppFontStore { + const SharedPreferencesAppFontStore(); + + @override + Future load() async { + try { + final preferences = await SharedPreferences.getInstance(); + return AppFont.fromId(preferences.getString(appFontKey)); + } catch (_) { + return AppFont.system; + } + } + + @override + Future save(AppFont font) async { + try { + final preferences = await SharedPreferences.getInstance(); + return await preferences.setString(appFontKey, font.id); + } catch (_) { + return false; + } + } +} + +/// An in-memory store for tests and fixtures. +class MemoryAppFontStore implements AppFontStore { + MemoryAppFontStore([this.font = AppFont.system]); + + AppFont font; + bool failSaves = false; + + @override + Future load() async => font; + + @override + Future save(AppFont font) async { + if (failSaves) return false; + this.font = font; + return true; + } +} diff --git a/app/lib/ui/theme/app_theme.dart b/app/lib/ui/theme/app_theme.dart index 2e2ca090..40ad5587 100644 --- a/app/lib/ui/theme/app_theme.dart +++ b/app/lib/ui/theme/app_theme.dart @@ -26,11 +26,20 @@ abstract final class AppColors { const _seed = Color(0xFF6750A4); -ThemeData appLightTheme() => ThemeData( - platform: kIsWeb ? TargetPlatform.android : null, - colorScheme: ColorScheme.fromSeed(seedColor: _seed), - pageTransitionsTheme: appPageTransitions, -); +/// [fontFamily] replaces the platform family throughout the text themes +/// (Material's, and Cupertino's for Cupertino widgets on a Material host), +/// keeping every size, weight and the text scaling; null keeps the system +/// typography. +ThemeData appLightTheme({String? fontFamily}) { + final scheme = ColorScheme.fromSeed(seedColor: _seed); + return ThemeData( + platform: kIsWeb ? TargetPlatform.android : null, + colorScheme: scheme, + pageTransitionsTheme: appPageTransitions, + fontFamily: fontFamily, + cupertinoOverrideTheme: _cupertinoOverride(fontFamily, scheme), + ); +} /// Android and iOS keep their native transitions and interactive back gestures. /// Desktop hosts use the short horizontal slide. @@ -80,7 +89,7 @@ class FastSlidePageTransitionsBuilder extends PageTransitionsBuilder { } /// Material's dark scheme with gray surfaces stepping up from the page. -ThemeData appDarkTheme() { +ThemeData appDarkTheme({String? fontFamily}) { final scheme = ColorScheme.fromSeed( seedColor: _seed, @@ -100,6 +109,8 @@ ThemeData appDarkTheme() { colorScheme: scheme, scaffoldBackgroundColor: scheme.surface, pageTransitionsTheme: appPageTransitions, + fontFamily: fontFamily, + cupertinoOverrideTheme: _cupertinoOverride(fontFamily, scheme), ); } @@ -107,3 +118,45 @@ const appCupertinoTheme = CupertinoThemeData( scaffoldBackgroundColor: AppColors.background, barBackgroundColor: AppColors.bar, ); + +/// [appCupertinoTheme] with every Cupertino text role in [fontFamily]; null +/// keeps the system typography. +CupertinoThemeData appCupertinoThemeWith({String? fontFamily}) => + fontFamily == null + ? appCupertinoTheme + : appCupertinoTheme.copyWith(textTheme: appCupertinoTextTheme(fontFamily)); + +/// The stock Cupertino text roles (sizes, weights, colours) in [fontFamily]. +/// Action roles take [primaryColor], as the stock roles do. +CupertinoTextThemeData appCupertinoTextTheme( + String fontFamily, { + Color primaryColor = CupertinoColors.systemBlue, +}) { + final base = CupertinoTextThemeData(primaryColor: primaryColor); + TextStyle face(TextStyle style) => style.copyWith(fontFamily: fontFamily); + return CupertinoTextThemeData( + textStyle: face(base.textStyle), + actionTextStyle: face(base.actionTextStyle), + actionSmallTextStyle: face(base.actionSmallTextStyle), + tabLabelTextStyle: face(base.tabLabelTextStyle), + navTitleTextStyle: face(base.navTitleTextStyle), + navLargeTitleTextStyle: face(base.navLargeTitleTextStyle), + navActionTextStyle: face(base.navActionTextStyle), + pickerTextStyle: face(base.pickerTextStyle), + dateTimePickerTextStyle: face(base.dateTimePickerTextStyle), + ); +} + +/// Cupertino widgets on a Material host derive their theme from Material's; +/// only the family is overridden, with Material's primary for actions. +NoDefaultCupertinoThemeData? _cupertinoOverride( + String? fontFamily, + ColorScheme scheme, +) => fontFamily == null + ? null + : CupertinoThemeData( + textTheme: appCupertinoTextTheme( + fontFamily, + primaryColor: scheme.primary, + ), + ); 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/interface-font-choice.md b/app/notes/interface-font-choice.md new file mode 100644 index 00000000..3ab07c94 --- /dev/null +++ b/app/notes/interface-font-choice.md @@ -0,0 +1,44 @@ +# Interface font choice (per device) + +Settings > Font offers System default (the default everywhere: Roboto on +Android and the web, SF in the iOS app), Inter, IBM Plex Sans, Source Sans 3 +and Atkinson Hyperlegible Next. Each option is named in its own face. + +- `lib/ui/theme/app_font.dart`: the options, stable stored ids and assets. +- `lib/ui/theme/app_font_store.dart`: `shared_preferences` key + `pipelines.next.font.v1`, so the choice belongs to this device or browser + and is never sent to the server. +- `lib/ui/theme/app_font_controller.dart`: `AppFontController.instance` + loads the chosen family with a `FontLoader` and only then switches the + theme (one switch, no reflow loop). Startup waits up to 1.5 s for a stored + font beside the session load, then draws in the system font and applies + the font when it lands. A failed load keeps the current face and says so. +- `lib/ui/theme/app_theme.dart`: `appLightTheme`/`appDarkTheme({fontFamily})` + and `appCupertinoThemeWith({fontFamily})` replace only the family in every + text role; sizes, weights and text scaling are unchanged. Explicit + `appMonospaceFamily` (RobotoMono) styles are untouched. +- `MyApp` and the onboarding host rebuild from the controller, so a choice + applies immediately on Material and Cupertino hosts. +- `lib/ui/theme/app_font_licenses.dart`: each `fonts//OFL.txt` is + registered with `LicenseRegistry`. The app has no licence page of its own + yet, so nothing shows them in the UI. + +## Web download cost + +The families are plain `assets:`, not pubspec `fonts:` (the web engine fetches +every `fonts:` family at boot). Browser resource log on the fixture build: +startup fetches no optional font; opening Settings fetches each family's +Regular weight for the previews (65–431 KB each); choosing one fetches its +other three weights. A stored choice fetches only its four files at startup. + +Build size (release web): `main.dart.js` 4,214,247 -> 4,232,251 bytes (+18 KB, ++5 KB gzip); `build/web` 43.6 -> 48.0 MB because the four families' 16 TTFs +(4.45 MB, 2.09 MB gzip) ship as on-demand assets. The APK grows by about the +same compressed amount (not measured). + +## Known gaps + +- Chat markdown code blocks were already in the proportional UI face, not + RobotoMono; they now follow the chosen font too. +- On the web Cupertino fixture, "System default" names an unregistered + `CupertinoSystemText` family, which the browser engine substitutes. diff --git a/app/notes/run-tokens.md b/app/notes/run-tokens.md new file mode 100644 index 00000000..96041afb --- /dev/null +++ b/app/notes/run-tokens.md @@ -0,0 +1,48 @@ +# Run token usage beside size + +Runs that spend 20M+ tokens now show it the way they show disk size. + +## Server (payload) + +- `RunSummary.usage` (`protocol.RunUsage`) moved up from `RunDetail`, so + `list_runs` rows carry the run total too. `get_run` still sends `usage` at + the same JSON key, now through the embedded summary. +- The list fills it with one grouped `step_usage` aggregate per 100-run batch + (`internal/pipeline/run_usage.go`), using the same column sums as the + history-wide stats (`usageSumColumns` in `stats.go`). It is joined to + `run_steps`, so orphaned usage rows (reported: about 650 rows, 1.47B tokens, + left by an Oct 19 history wipe) no longer count in stats or per-run totals. + The rows are not deleted. If a wiped run id was reused, an orphan row that + matches a new run's (run_id, sequence) still cannot be told apart. +- `StageUsage` now keeps `tokens_reasoning`, `cache_read` and `cache_write` + with `cache_known` (the runner dropped them before). They are JSON keys in + `usage_json`, so old rows need no migration: they lack `cache_known`, so + their cache share is unknown, not zero. Run totals add `cache_known_stages` + and `cache_known` (every token-reporting stage recorded the split). + +## App + +- `lib/features/runs/domain/run_tokens.dart`: parsing and formatting. + `22.4M tokens`, `≥9.1M tokens` when `tokens_known_stages < stages`. Nothing + is estimated, and a row without `usage` (an older server) shows nothing. +- List row: tokens after the size, in both the full and the compact subtitle. +- Sort: `Most tokens` (`most_tokens`) after `Largest first`, flat like it. + Runs without a reported total sort last. +- Summary: `Size on disk`, `Tokens` (`22.4M · 2.1M fresh, 20.3M cached · + 1.8M in, 240K out`; fresh is total minus cache reads, shown only when + `cache_known`) and `Cost`. +- Stage rows: the Timeline row subtitle adds `· 20.1M tokens`. The visit facts + add `Tokens` and `Cost`. +- Cost is shown only when a positive `cost_usd` was reported. Subscription + models report `cost_known` with no amount, which shows as `Not reported`. + The stats page's `Reported cost` uses the same rule. +- `usage` is no longer treated as detail-only in + `RunsController.acceptRunDetail`, so opening a run refreshes its row's total. + +## Evidence + +Fixture: `tool/probe_usage_fixture.dart`, opt-in `?usage=1` on the capture +entry (run 7: 340 MB, 22.4M tokens, mostly cached, $1.42; run 6: partial +≥9.1M). Captures were taken with `tool/capture` at 560x1000 and 1400x900: +the list, the sort sheet, Most tokens, the run 7 Summary and Timeline, the +build and plan visits, and the no-usage (old-server) list and Summary. 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/pubspec.yaml b/app/pubspec.yaml index a1e6e95e..90b2201a 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -75,6 +75,13 @@ flutter: assets: - assets/harnesses/claude.png - assets/harnesses/opencode.png + # Optional interface fonts. Plain assets, not `fonts:`: the web engine + # downloads every `fonts:` family at startup, while these load on demand + # (lib/ui/theme/app_font_controller.dart). Licences: fonts/README.md. + - fonts/inter/ + - fonts/ibm-plex-sans/ + - fonts/source-sans-3/ + - fonts/atkinson-hyperlegible-next/ fonts: - family: RobotoMono 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/test/features/chat/ui/transcript/chat_markdown_presentation_test.dart b/app/test/features/chat/ui/transcript/chat_markdown_presentation_test.dart index 6bb18e29..b2fefcc2 100644 --- a/app/test/features/chat/ui/transcript/chat_markdown_presentation_test.dart +++ b/app/test/features/chat/ui/transcript/chat_markdown_presentation_test.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/semantics.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart'; +import 'package:pipelines_app/ui/text/monospace.dart'; import 'chat_markdown_test_support.dart'; @@ -126,7 +127,7 @@ void main() { expect(code.style?.backgroundColor, fill, reason: '$cupertino'); expect(plain.style?.backgroundColor, isNull, reason: '$cupertino'); expect(code.style?.fontSize, isNull, reason: '$cupertino'); - expect(code.style?.fontFamily, isNull, reason: '$cupertino'); + expect(code.style?.fontFamily, appMonospaceFamily, reason: '$cupertino'); } }); diff --git a/app/test/features/runs/domain/run_tokens_test.dart b/app/test/features/runs/domain/run_tokens_test.dart new file mode 100644 index 00000000..7877e796 --- /dev/null +++ b/app/test/features/runs/domain/run_tokens_test.dart @@ -0,0 +1,191 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/runs/domain/run_models.dart'; +import 'package:pipelines_app/features/runs/domain/run_tokens.dart'; +import 'package:pipelines_app/features/runs/ui/runs_filter_options.dart'; + +JsonMap _run(int id, {Map? usage, String? created}) => { + 'id': id, + 'created_at': created ?? '2026-10-2${id % 10}T12:00:00Z', + 'usage': ?usage, +}; + +Map _usage( + int total, { + int stages = 1, + int? tokenStages, + bool split = false, + int input = 0, + int output = 0, + int costStages = 0, + double? cost, +}) => { + 'stages': stages, + 'tokens_total': total, + 'tokens_known_stages': tokenStages ?? stages, + 'split_known': split, + if (input > 0) 'tokens_in': input, + if (output > 0) 'tokens_out': output, + 'cost_known_stages': costStages, + 'cost_usd': ?cost, +}; + +void main() { + test('token counts are compact decimal multiples', () { + expect(formatTokenCount(0), '0'); + expect(formatTokenCount(950), '950'); + expect(formatTokenCount(1000), '1K'); + expect(formatTokenCount(1234), '1.2K'); + expect(formatTokenCount(340000), '340K'); + expect(formatTokenCount(999600), '1M'); + expect(formatTokenCount(22400015), '22.4M'); + expect(formatTokenCount(1300000000), '1.3B'); + }); + + test('a reported run total is labelled like the size', () { + final usage = runTokenUsage(_run(1, usage: _usage(22400015)))!; + expect(usage.partial, isFalse); + expect(tokenUsageLabel(usage), '22.4M tokens'); + }); + + test('a partial total is a lower bound, never an estimate', () { + final usage = runTokenUsage( + _run(1, usage: _usage(9, stages: 2, tokenStages: 1)), + )!; + expect(usage.partial, isTrue); + expect(tokenUsageLabel(usage), '≥9 tokens'); + expect(tokenUsageDetail(usage), '≥9 · 1 of 2 stages reported'); + }); + + test('absent or unreported usage shows nothing', () { + expect(runTokenUsage(_run(1)), isNull); + expect(runTokenUsage({'id': 1, 'usage': 'bogus'}), isNull); + // Only a cost was reported: no token figure is invented. + expect( + runTokenUsage(_run(1, usage: _usage(0, tokenStages: 0, costStages: 1))), + isNull, + ); + }); + + test('the detail adds the split and the reported cost', () { + final usage = runTokenUsage( + _run( + 1, + usage: _usage( + 22400015, + stages: 2, + split: true, + input: 20000010, + output: 2400005, + costStages: 2, + cost: 1.75, + ), + ), + )!; + expect(tokenUsageDetail(usage), '22.4M · 20M in, 2.4M out'); + expect(tokenUsageCost(usage), '\$1.75'); + + final partialCost = runTokenUsage( + _run(1, usage: _usage(9, stages: 2, costStages: 1, cost: 0.1)), + )!; + expect(tokenUsageCost(partialCost), '≥\$0.10'); + expect( + tokenUsageCost(runTokenUsage(_run(1, usage: _usage(9)))!), + 'Not reported', + ); + expect( + tokenUsageCost( + runTokenUsage(_run(1, usage: _usage(9, costStages: 1, cost: 0.0042)))!, + ), + '\$0.0042', + ); + }); + + test('the detail splits fresh from cached when every stage reported it', () { + final usage = runTokenUsage( + _run( + 1, + usage: { + ..._usage(22400000, split: true, input: 900000, output: 150000), + 'cache_known': true, + 'cache_read': 21300000, + 'cache_write': 50000, + }, + ), + )!; + expect(tokenUsageLabel(usage), '22.4M tokens'); + expect(usage.fresh, 1100000); + expect( + tokenUsageDetail(usage), + '22.4M · 1.1M fresh, 21.3M cached · 900K in, 150K out', + ); + // An older row (or a partial cache split) has no cache figure: just the + // total and what else it reported. + final old = runTokenUsage( + _run( + 1, + usage: _usage(22400000, split: true, input: 900000, output: 150000), + ), + )!; + expect(old.fresh, isNull); + expect(tokenUsageDetail(old), '22.4M · 900K in, 150K out'); + }); + + test('a zero or missing "known" cost is not reported, never \$0', () { + // Subscription models report cost_known with no positive cost_usd. + for (final usage in [ + _usage(9, costStages: 1), + _usage(9, costStages: 1, cost: 0), + ]) { + final tokens = runTokenUsage(_run(1, usage: usage))!; + expect(tokens.cost, isNull); + expect(tokenUsageCost(tokens), 'Not reported'); + } + final stage = stageTokenUsage({ + 'usage': {'tokens_total': 9, 'tokens_known': true, 'cost_known': true}, + })!; + expect(tokenUsageCost(stage), 'Not reported'); + }); + + test('a stage reports its own tokens only when it counted them', () { + final stage = stageTokenUsage({ + 'usage': { + 'tokens_total': 1500, + 'tokens_known': true, + 'split_known': true, + 'tokens_in': 1200, + 'tokens_out': 300, + 'cost_known': true, + 'cost_usd': 0.02, + }, + })!; + expect(tokenUsageDetail(stage), '1.5K · 1.2K in, 300 out'); + expect(tokenUsageCost(stage), '\$0.02'); + expect( + stageTokenUsage({ + 'usage': {'cost_known': true}, + }), + isNull, + ); + expect(stageTokenUsage({}), isNull); + }); + + test('Most tokens sorts reported totals first, largest first', () { + final runs = [ + _run(1, usage: _usage(5)), + _run(2), + _run(3, usage: _usage(22400000)), + _run(4, usage: _usage(9, stages: 2, tokenStages: 1)), + _run(5), + ]; + final sorted = sortRuns(runs, sort: 'most_tokens'); + expect(sorted.map((run) => run['id']), [3, 4, 1, 5, 2]); + }); + + test('Most tokens is offered beside Largest first and lists flat', () { + final keys = runSorts.map((option) => option.$1).toList(); + expect(keys.indexOf('most_tokens'), keys.indexOf('largest_first') + 1); + expect(runSortLabel('most_tokens'), 'Most tokens'); + expect(flatRunSorts, contains('most_tokens')); + }); +} diff --git a/app/test/features/runs/ui/run_detail_summary_fields_test.dart b/app/test/features/runs/ui/run_detail_summary_fields_test.dart index d38fd4b9..d083a9ca 100644 --- a/app/test/features/runs/ui/run_detail_summary_fields_test.dart +++ b/app/test/features/runs/ui/run_detail_summary_fields_test.dart @@ -24,6 +24,7 @@ JsonMap recordedRunFixture() => { 'retry_of': 12, 'diagnostic': 'Traceback: boom', 'completed_steps': 2, + 'size_bytes': 5 * 1024 * 1024, 'input': {'prompt': 'ship it'}, 'output': {'result': 'ok'}, 'steps': [ @@ -64,6 +65,9 @@ const recordedLabels = [ 'Started', 'Finished', 'Duration', + 'Size on disk', + 'Tokens', + 'Cost', 'Retry of', 'Diagnostic', ]; @@ -91,6 +95,12 @@ void main() { formatLocalTimestamp('2026-10-19T09:32:05Z'), ); expect(rows.firstWhere((row) => row.label == 'Duration').value, '2m 5s'); + expect(rows.firstWhere((row) => row.label == 'Size on disk').value, '5 MB'); + expect( + rows.firstWhere((row) => row.label == 'Tokens').value, + '123 · 100 in, 23 out', + ); + expect(rows.firstWhere((row) => row.label == 'Cost').value, '\$0.50'); expect(rows.firstWhere((row) => row.label == 'Retry of').value, 'run 12'); final diagnostic = rows.firstWhere((row) => row.label == 'Diagnostic'); expect(diagnostic.value, 'Traceback: boom'); diff --git a/app/test/features/runs/ui/run_list_row_test.dart b/app/test/features/runs/ui/run_list_row_test.dart index 6a7346e8..40130ff9 100644 --- a/app/test/features/runs/ui/run_list_row_test.dart +++ b/app/test/features/runs/ui/run_list_row_test.dart @@ -86,6 +86,48 @@ void main() { expect(find.text('Pipeline unavailable · 2m 5s · 3 steps'), findsOneWidget); }); + testWidgets('shows reported tokens beside the size, and nothing without', ( + tester, + ) async { + Future pump(JsonMap run) => tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: RunListRow(run: run, now: DateTime.utc(2026, 10, 22)), + ), + ), + ); + final sized = {..._finished, 'size_bytes': 340 * 1024 * 1024}; + await pump({ + ...sized, + 'usage': { + 'stages': 3, + 'tokens_known_stages': 3, + 'tokens_total': 22400015, + }, + }); + expect( + find.text('release · 2m 5s · 3 steps · 340 MB · 22.4M tokens'), + findsOneWidget, + ); + + await pump({ + ...sized, + 'usage': { + 'stages': 3, + 'tokens_known_stages': 2, + 'tokens_total': 22400015, + }, + }); + expect( + find.text('release · 2m 5s · 3 steps · 340 MB · ≥22.4M tokens'), + findsOneWidget, + ); + + // An older server's list carries no usage: the row is unchanged. + await pump(sized); + expect(find.text('release · 2m 5s · 3 steps · 340 MB'), findsOneWidget); + }); + test('the run action sheet is titled with the run', () { expect(runMoreMenuTitle(_finished), 'Ship the release notes'); expect(runMoreMenuTitle({'id': 5, 'name': 'nightly'}), 'nightly'); diff --git a/app/test/features/runs/ui/run_stage_tokens_test.dart b/app/test/features/runs/ui/run_stage_tokens_test.dart new file mode 100644 index 00000000..ae6e12ac --- /dev/null +++ b/app/test/features/runs/ui/run_stage_tokens_test.dart @@ -0,0 +1,46 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/runs/ui/run_detail_content.dart'; +import 'package:pipelines_app/features/runs/ui/run_detail_visit_facts.dart'; + +final _reported = { + 'stage': 'build', + 'sequence': 1, + 'status': 'completed', + 'outcome': 'success', + 'target': 'review', + 'usage': { + 'tokens_total': 1500000, + 'tokens_known': true, + 'split_known': true, + 'tokens_in': 1200000, + 'tokens_out': 300000, + 'cost_known': true, + 'cost_usd': 0.42, + }, +}; + +final _unreported = { + 'stage': 'gate', + 'sequence': 2, + 'status': 'completed', +}; + +void main() { + test('a visit lists its reported tokens and cost as facts', () { + final facts = detailVisitFacts(_reported); + expect( + facts, + contains((label: 'Tokens', value: '1.5M · 1.2M in, 300K out')), + ); + expect(facts, contains((label: 'Cost', value: '\$0.42'))); + final none = detailVisitFacts(_unreported); + expect(none.any((fact) => fact.label == 'Tokens'), isFalse); + expect(none.any((fact) => fact.label == 'Cost'), isFalse); + }); + + test('a stage row carries its compact token total', () { + final visits = detailTimeline([_reported, _unreported]); + expect(visits[0].tokens, '1.5M tokens'); + expect(visits[1].tokens, isNull); + }); +} diff --git a/app/test/features/settings/settings_font_section_test.dart b/app/test/features/settings/settings_font_section_test.dart new file mode 100644 index 00000000..fd8aedd3 --- /dev/null +++ b/app/test/features/settings/settings_font_section_test.dart @@ -0,0 +1,135 @@ +import 'dart:async'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/settings/settings_font_section.dart'; +import 'package:pipelines_app/ui/theme/app_font.dart'; +import 'package:pipelines_app/ui/theme/app_font_controller.dart'; +import 'package:pipelines_app/ui/theme/app_font_store.dart'; + +class _Harness { + _Harness({this.hold = false}); + + final bool hold; + final store = MemoryAppFontStore(); + final loads = []; + final held = >[]; + + late final fonts = AppFontController( + store: store, + loader: (family, assets) { + loads.addAll(assets); + if (!hold || assets.length == 1) return Future.value(); + final done = Completer(); + held.add(done); + return done.future; + }, + ); +} + +Future _pump( + WidgetTester tester, + _Harness harness, { + bool cupertino = false, +}) async { + final section = SingleChildScrollView( + child: SettingsFontSection(controller: harness.fonts), + ); + await tester.pumpWidget( + cupertino + ? CupertinoApp(home: CupertinoPageScaffold(child: section)) + : MaterialApp(home: Scaffold(body: section)), + ); + await tester.pumpAndSettle(); +} + +String? _familyOf(WidgetTester tester, AppFont font) => tester + .widget( + find.descendant( + of: find.byKey(settingsFontKey(font)), + matching: find.text(font.label), + ), + ) + .style + ?.fontFamily; + +void main() { + testWidgets('Material: every option is a radio named in its own face', ( + tester, + ) async { + final harness = _Harness(); + await _pump(tester, harness); + + expect(find.text(fontSectionTitle), findsOneWidget); + for (final font in AppFont.values) { + expect(find.byKey(settingsFontKey(font)), findsOneWidget); + expect(_familyOf(tester, font), font.family ?? 'Roboto'); + } + // Showing the picker fetched one regular weight per option, no more. + expect(harness.loads, [ + for (final font in AppFont.values) + if (font.previewAsset != null) font.previewAsset, + ]); + final group = tester.widget>( + find.byType(RadioGroup), + ); + expect(group.groupValue, AppFont.system); + }); + + testWidgets('Material: choosing a font stores it and shows it loading', ( + tester, + ) async { + final harness = _Harness(hold: true); + await _pump(tester, harness); + + await tester.tap(find.text('Source Sans 3')); + await tester.pump(); + expect(harness.store.font, AppFont.sourceSans3); + expect( + tester + .widget>(find.byType(RadioGroup)) + .groupValue, + AppFont.sourceSans3, + ); + expect(find.text(fontLoadingText), findsOneWidget); + expect(harness.fonts.family, isNull); + + harness.held.single.complete(); + await tester.pumpAndSettle(); + expect(find.text(fontLoadingText), findsNothing); + expect(harness.fonts.family, 'Source Sans 3'); + }); + + testWidgets('Cupertino: the chosen row carries the checkmark', ( + tester, + ) async { + final harness = _Harness(); + await _pump(tester, harness, cupertino: true); + + expect(find.byType(RadioListTile), findsNothing); + expect(_familyOf(tester, AppFont.system), 'CupertinoSystemText'); + Finder check(AppFont font) => find.descendant( + of: find.byKey(settingsFontKey(font)), + matching: find.byIcon(CupertinoIcons.check_mark), + ); + expect(check(AppFont.system), findsOneWidget); + + await tester.tap(find.text('Inter')); + await tester.pumpAndSettle(); + expect(check(AppFont.system), findsNothing); + expect(check(AppFont.inter), findsOneWidget); + expect(harness.store.font, AppFont.inter); + expect(harness.fonts.family, 'Inter'); + }); + + testWidgets('a failed save is said under the group', (tester) async { + final harness = _Harness(); + harness.store.failSaves = true; + await _pump(tester, harness); + + await tester.tap(find.text('Inter')); + await tester.pumpAndSettle(); + expect(find.text(fontSaveFailedText), findsOneWidget); + }); +} diff --git a/app/test/ui/theme/app_font_controller_test.dart b/app/test/ui/theme/app_font_controller_test.dart new file mode 100644 index 00000000..865ad00f --- /dev/null +++ b/app/test/ui/theme/app_font_controller_test.dart @@ -0,0 +1,156 @@ +import 'dart:async'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/ui/theme/app_font.dart'; +import 'package:pipelines_app/ui/theme/app_font_controller.dart'; +import 'package:pipelines_app/ui/theme/app_font_store.dart'; + +/// Records each load and completes it when the test says so. +class _Loads { + final calls = <(String, List)>[]; + final pending = >[]; + bool immediate = true; + + Future call(String family, List assets) { + calls.add((family, assets)); + final done = Completer(); + pending.add(done); + if (immediate) done.complete(); + return done.future; + } +} + +void main() { + test('starts on the system default with no download', () async { + final loads = _Loads(); + final fonts = AppFontController( + store: MemoryAppFontStore(), + loader: loads.call, + ); + await fonts.restore(); + expect(fonts.selected, AppFont.system); + expect(fonts.family, isNull); + expect(loads.calls, isEmpty); + }); + + test('restore loads only the stored family, then applies it', () async { + final loads = _Loads()..immediate = false; + final fonts = AppFontController( + store: MemoryAppFontStore(AppFont.inter), + loader: loads.call, + ); + final restoring = fonts.restore(); + await pumpEventQueue(); + expect(fonts.selected, AppFont.inter); + expect(fonts.family, isNull, reason: 'system face until loaded'); + expect(fonts.loading, isTrue); + expect(loads.calls.single.$1, 'Inter'); + expect(loads.calls.single.$2, AppFont.inter.assets); + loads.pending.single.complete(); + await restoring; + expect(fonts.family, 'Inter'); + expect(fonts.loading, isFalse); + }); + + test( + 'restore with a wait returns at the deadline and applies later', + () async { + final loads = _Loads()..immediate = false; + final fonts = AppFontController( + store: MemoryAppFontStore(AppFont.sourceSans3), + loader: loads.call, + ); + await fonts.restore(wait: const Duration(milliseconds: 10)); + expect(fonts.family, isNull); + loads.pending.single.complete(); + await pumpEventQueue(); + expect(fonts.family, 'Source Sans 3'); + }, + ); + + test( + 'select stores the choice and applies it; system needs no load', + () async { + final store = MemoryAppFontStore(); + final loads = _Loads(); + final fonts = AppFontController(store: store, loader: loads.call); + await fonts.select(AppFont.atkinsonHyperlegible); + expect(store.font, AppFont.atkinsonHyperlegible); + expect(fonts.family, 'Atkinson Hyperlegible Next'); + await fonts.select(AppFont.system); + expect(store.font, AppFont.system); + expect(fonts.family, isNull); + expect(loads.calls, hasLength(1)); + }, + ); + + test('a newer choice wins over a slower earlier load', () async { + final loads = _Loads()..immediate = false; + final fonts = AppFontController( + store: MemoryAppFontStore(), + loader: loads.call, + ); + final first = fonts.select(AppFont.inter); + await pumpEventQueue(); + final second = fonts.select(AppFont.ibmPlexSans); + await pumpEventQueue(); + loads.pending[1].complete(); + await second; + expect(fonts.family, 'IBM Plex Sans'); + loads.pending[0].complete(); + await first; + expect(fonts.family, 'IBM Plex Sans'); + }); + + test( + 'preview loads the regular weight once; choosing loads the rest', + () async { + final loads = _Loads(); + final fonts = AppFontController( + store: MemoryAppFontStore(), + loader: loads.call, + ); + expect(fonts.canPreview(AppFont.inter), isFalse); + await fonts.preview(AppFont.inter); + await fonts.preview(AppFont.inter); + await fonts.preview(AppFont.system); + expect(fonts.canPreview(AppFont.inter), isTrue); + expect(fonts.canPreview(AppFont.system), isTrue); + expect(loads.calls.single.$1, 'Inter'); + expect(loads.calls.single.$2, ['fonts/inter/Inter-Regular.ttf']); + await fonts.select(AppFont.inter); + expect(loads.calls.last.$2, AppFont.inter.assets.skip(1).toList()); + }, + ); + + test( + 'a failed load keeps the current face, says so, and can retry', + () async { + var fail = true; + final fonts = AppFontController( + store: MemoryAppFontStore(), + loader: (family, assets) async { + if (fail) throw StateError('offline'); + }, + ); + await fonts.select(AppFont.inter); + expect(fonts.family, isNull); + expect(fonts.loadFailed, AppFont.inter); + expect(fonts.loading, isFalse); + fail = false; + await fonts.select(AppFont.inter); + expect(fonts.loadFailed, isNull); + expect(fonts.family, 'Inter'); + }, + ); + + test('a failed save is reported but the font still applies', () async { + final fonts = AppFontController( + store: MemoryAppFontStore()..failSaves = true, + loader: _Loads().call, + ); + await fonts.select(AppFont.inter); + expect(fonts.saveFailed, isTrue); + expect(fonts.family, 'Inter'); + }); +} diff --git a/app/test/ui/theme/app_font_store_test.dart b/app/test/ui/theme/app_font_store_test.dart new file mode 100644 index 00000000..7e6f46cf --- /dev/null +++ b/app/test/ui/theme/app_font_store_test.dart @@ -0,0 +1,69 @@ +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/ui/theme/app_font.dart'; +import 'package:pipelines_app/ui/theme/app_font_store.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + setUp(() => SharedPreferences.setMockInitialValues({})); + + test('nothing stored is the system default', () async { + expect(await const SharedPreferencesAppFontStore().load(), AppFont.system); + }); + + test( + 'a choice round-trips under its stable id, on the device only', + () async { + const store = SharedPreferencesAppFontStore(); + expect(await store.save(AppFont.ibmPlexSans), isTrue); + final preferences = await SharedPreferences.getInstance(); + expect(preferences.getKeys(), {appFontKey}); + expect(preferences.getString(appFontKey), 'ibm-plex-sans'); + expect(await store.load(), AppFont.ibmPlexSans); + + expect(await store.save(AppFont.system), isTrue); + expect(await store.load(), AppFont.system); + }, + ); + + test('an unknown stored id falls back to the system default', () async { + SharedPreferences.setMockInitialValues({ + appFontKey: 'comic-sans', + }); + expect(await const SharedPreferencesAppFontStore().load(), AppFont.system); + }); + + test('every bundled option has its four weights and its licence', () { + for (final font in AppFont.values) { + if (font == AppFont.system) { + expect(font.family, isNull); + expect(font.assets, isEmpty); + continue; + } + expect(font.assets, hasLength(4)); + expect(font.previewAsset, font.assets.first); + for (final asset in [...font.assets, font.licenseAsset!]) { + expect(File(asset).existsSync(), isTrue, reason: asset); + } + expect( + File(font.licenseAsset!).readAsStringSync(), + contains('SIL OPEN FONT LICENSE Version 1.1'), + ); + } + }); + + test('the optional fonts are assets, not startup fonts', () { + final pubspec = File('pubspec.yaml').readAsStringSync(); + final fonts = pubspec.substring(pubspec.indexOf('\n fonts:')); + for (final font in AppFont.values.where((f) => f.family != null)) { + expect(fonts, isNot(contains('family: ${font.family}'))); + expect( + pubspec, + contains(' - ${font.licenseAsset!.replaceAll('OFL.txt', '')}\n'), + ); + } + }); +} diff --git a/app/test/ui/theme/app_font_theme_test.dart b/app/test/ui/theme/app_font_theme_test.dart new file mode 100644 index 00000000..4f9e3ea5 --- /dev/null +++ b/app/test/ui/theme/app_font_theme_test.dart @@ -0,0 +1,95 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/application/app.dart'; +import 'package:pipelines_app/shell/destination.dart'; +import 'package:pipelines_app/ui/theme/app_font.dart'; +import 'package:pipelines_app/ui/theme/app_font_controller.dart'; +import 'package:pipelines_app/ui/theme/app_font_store.dart'; +import 'package:pipelines_app/ui/theme/app_theme.dart'; + +void main() { + test('Material themes take the family and keep sizes and weights', () { + for (final build in [ + appLightTheme, + appDarkTheme, + ]) { + final system = build().textTheme; + final inter = build(fontFamily: 'Inter').textTheme; + expect(build().textTheme.bodyMedium?.fontFamily, isNot('Inter')); + for (final (a, b) in <(TextStyle?, TextStyle?)>[ + (system.bodyMedium, inter.bodyMedium), + (system.titleMedium, inter.titleMedium), + (system.labelLarge, inter.labelLarge), + (system.headlineSmall, inter.headlineSmall), + ]) { + expect(b?.fontFamily, 'Inter'); + expect(b?.fontSize, a?.fontSize); + expect(b?.fontWeight, a?.fontWeight); + } + expect( + build( + fontFamily: 'Inter', + ).cupertinoOverrideTheme?.textTheme?.textStyle.fontFamily, + 'Inter', + ); + expect(build().cupertinoOverrideTheme, isNull); + } + }); + + test('the Cupertino theme takes the family in every text role', () { + expect(appCupertinoThemeWith(), same(appCupertinoTheme)); + final text = appCupertinoThemeWith(fontFamily: 'Inter').textTheme; + const stock = CupertinoTextThemeData(); + for (final (a, b) in <(TextStyle, TextStyle)>[ + (stock.textStyle, text.textStyle), + (stock.navTitleTextStyle, text.navTitleTextStyle), + (stock.navLargeTitleTextStyle, text.navLargeTitleTextStyle), + (stock.tabLabelTextStyle, text.tabLabelTextStyle), + (stock.actionTextStyle, text.actionTextStyle), + (stock.pickerTextStyle, text.pickerTextStyle), + ]) { + expect(b.fontFamily, 'Inter'); + expect(b.fontSize, a.fontSize); + expect(b.fontWeight, a.fontWeight); + } + expect( + appCupertinoThemeWith(fontFamily: 'Inter').scaffoldBackgroundColor, + AppColors.background, + ); + }); + + for (final cupertino in [false, true]) { + testWidgets('the ${cupertino ? 'Cupertino' : 'Material'} host switches ' + 'font live, without a restart', (tester) async { + final fonts = AppFontController( + store: MemoryAppFontStore(), + loader: (family, assets) async {}, + ); + String? family; + await tester.pumpWidget( + MyApp( + cupertino: cupertino, + fonts: fonts, + destination: AppDestination.settings, + destinationBodies: { + AppDestination.settings: (context) => Builder( + builder: (context) { + family = DefaultTextStyle.of(context).style.fontFamily; + return const Text('body'); + }, + ), + }, + ), + ); + final before = family; + expect(before, isNot('IBM Plex Sans')); + await fonts.select(AppFont.ibmPlexSans); + await tester.pumpAndSettle(); + expect(family, 'IBM Plex Sans'); + await fonts.select(AppFont.system); + await tester.pumpAndSettle(); + expect(family, before); + }); + } +} 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: