diff --git a/app/lib/features/chat/conversation/chat_thread_controller.dart b/app/lib/features/chat/conversation/chat_thread_controller.dart index 6c469ee4..31147fc9 100644 --- a/app/lib/features/chat/conversation/chat_thread_controller.dart +++ b/app/lib/features/chat/conversation/chat_thread_controller.dart @@ -6,6 +6,7 @@ import '../../../core/api_gateway.dart'; import '../data/chat_repository.dart'; import '../domain/chat_account_usage.dart'; import '../domain/chat_sessions.dart'; +import '../domain/chat_shown_items.dart'; import '../domain/chat_transcript.dart'; import 'chat_server_queue.dart'; import 'chat_thread_cards.dart'; @@ -65,6 +66,7 @@ class ChatThreadController extends ChangeNotifier { void Function(JsonMap queue)? onQueue; List _messages = const []; + List _shown = const []; bool _running = false; bool _loaded = false; bool _loading = false; @@ -108,6 +110,10 @@ class ChatThreadController extends ChangeNotifier { List get messages => _messages; + /// What the model showed through the Pipelines tools ([chatShownItemsOf]), + /// as the server retains it, oldest first. + List get shown => _shown; + /// The prompts the server still holds for this session, in delivery order. List get queuedPrompts => _queue.prompts; @@ -302,6 +308,7 @@ class ChatThreadController extends ChangeNotifier { _subscribed = true; _subscriptionError = null; _applyQueue(reply['chat_queue']); + _applyShown(reply['chat_shown'], replace: true); } catch (error) { // History stays usable, but the UI must report that live updates failed. if (_disposed) return; @@ -330,8 +337,20 @@ class ChatThreadController extends ChangeNotifier { _notify(); } + /// Adopts the subscribe reply's retained list, or one pushed item. + void _applyShown(Object? body, {bool replace = false}) { + final items = chatShownItemsOf(body, sessionId); + if (items == null) return; + _shown = mergeChatShownItems(_shown, items, replace: replace); + _notify(); + } + void _onFrame(JsonMap envelope) { if (_disposed) return; + if (envelope['type'] == 'chat_shown') { + _applyShown(envelope['chat_shown']); + return; + } if (envelope['type'] == 'chat_queue') { _applyQueue(envelope['chat_queue']); return; diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index 0836adfe..03c9b169 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -14,6 +14,9 @@ import '../domain/chat_activity_detail.dart'; import '../domain/chat_notice.dart'; import '../domain/chat_attachment_picker.dart'; import '../domain/chat_colors.dart'; +import '../../../core/transport/pipelines_client.dart' + show configuredGatewayEndpoint; +import '../domain/chat_shown_items.dart'; import '../domain/chat_transcript.dart'; import '../ui/sessions/chat_adaptive.dart'; import '../ui/sessions/chat_color_picker.dart'; @@ -23,6 +26,7 @@ import '../ui/transcript/chat_markdown_view.dart'; import '../ui/transcript/chat_message_view.dart'; import '../ui/transcript/chat_transcript_selection.dart'; import '../ui/transcript/chat_injected_rows.dart'; +import '../ui/transcript/shown/chat_shown_card.dart'; import '../ui/transcript/chat_task_notices_group.dart'; import '../../artifacts/actions/artifact_file_bridge.dart'; import '../domain/chat_session_handoff.dart'; @@ -979,20 +983,26 @@ class _ChatThreadViewState extends State { // 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, + withChatShownItems( + transcriptRows( + messages, + trailing: cards, + running: + _controller.running && + !_reconnecting && + !_staleTranscript && + !cards.any(chatCardAwaitingOperator), + awaitingResponse: + _controller.awaitingResponse && + !_reconnecting && + !_staleTranscript && + !cards.any(chatCardAwaitingOperator), + liveMessageId: _controller.liveAssistantMessageId, + ), + // Placed only against a loaded transcript, so an entry never + // leads an empty one and then jumps down when history arrives. + _controller.loaded ? _controller.shown : const [], + hasMore: _controller.hasMore, ), ), ); @@ -1440,6 +1450,12 @@ class _ChatThreadViewState extends State { return ChatAgentReportCard(row: row, textBuilder: chatMarkdownText); } if (kind == 'system_notice') return ChatSystemNoticeLine(row: row); + if (kind == chatShownRowKind) { + return ChatShownCard( + row: row, + endpoint: configuredGatewayEndpoint(widget.gateway), + ); + } if (kind == 'task_notices') return ChatTaskNoticesGroup(row: row); if (kind == 'questions') return ChatQuestionsRow(row: row); if (kind == 'permission' || kind == 'question') { diff --git a/app/lib/features/chat/domain/chat_shown_items.dart b/app/lib/features/chat/domain/chat_shown_items.dart new file mode 100644 index 00000000..3539e1ab --- /dev/null +++ b/app/lib/features/chat/domain/chat_shown_items.dart @@ -0,0 +1,157 @@ +import '../../../core/api_gateway.dart'; +import 'chat_transcript.dart'; + +/// Entries a chat's model showed the operator through the Pipelines tools +/// (`show_file`, `show_image`, `show_html`, `notify`). +/// +/// The server keeps them per chat, outside the harness transcript: the +/// `chat_subscribe` reply carries the retained list (oldest first) and a +/// pushed `chat_shown` frame carries each new or updated item. Items are keyed +/// by `id`; a later copy replaces the held one in place. + +/// The four recorded kinds. +const chatShownKinds = {'file', 'image', 'html', 'notify'}; + +/// The transcript row kind an item is drawn as. +const String chatShownRowKind = 'shown'; + +/// The items of a `chat_shown` body for [sessionId], or null when the body is +/// absent, malformed or for another chat. Items of unknown kind or without an +/// id are dropped rather than drawn as something they are not. +List? chatShownItemsOf(Object? body, String sessionId) { + if (body is! Map || '${body['session_id'] ?? ''}' != sessionId) return null; + final items = body['items']; + if (items is! List) return const []; + return [ + for (final item in items.whereType()) + if ('${item['id'] ?? ''}'.isNotEmpty && + chatShownKinds.contains(item['kind'])) + Map.from(item), + ]; +} + +/// [held] with [incoming] merged by id: a known item is replaced where it is, +/// a new one is appended. [replace] adopts [incoming] as the whole list (the +/// subscribe reply is the server's complete retained set). +List mergeChatShownItems( + List held, + List incoming, { + bool replace = false, +}) { + if (replace) return List.unmodifiable(incoming); + final byId = {for (final item in incoming) '${item['id']}': item}; + final merged = [ + for (final item in held) byId.remove('${item['id']}') ?? item, + ...byId.values, + ]; + return List.unmodifiable(merged); +} + +/// The tool names a harness reports for the Pipelines tools: Claude Code +/// `mcp__pipelines__show_file`, OpenCode `pipelines_show_file`, Codex +/// `pipelines/show_file`. +final RegExp _pipelinesTool = RegExp( + r'^(?:mcp__pipelines__|pipelines_|pipelines/)' + r'(?:show_file|show_image|show_html|notify)$', +); + +/// Whether [part] is a call of a Pipelines tool whose result the transcript +/// draws as its own entry. A failed call stays a normal tool row: its error is +/// the only record of what went wrong. +bool chatPipelinesToolRepresented(JsonMap part) { + if ('${part['type'] ?? ''}' != 'tool') return false; + if (!_pipelinesTool.hasMatch('${part['name'] ?? ''}'.trim())) return false; + final state = part['state']; + return !(state is Map && '${state['status'] ?? ''}' == 'error'); +} + +int? _createdAt(JsonMap value) => chatMessageCreatedAt(value); + +/// A transcript row's recorded time, or null for a row without one. +int? _rowTime(JsonMap row) { + final own = _createdAt(row); + if (own != null) return own; + final message = row['message']; + return message is Map ? _createdAt(Map.from(message)) : null; +} + +/// [rows] (from [transcriptRows]) with the Pipelines tool calls removed from +/// answer turns and each shown item placed by its time. +/// +/// An item goes before the first timed row recorded after it, else after the +/// last timed row (so it never follows the pending cards or the response +/// shell that come after the transcript). While older history pages remain +/// ([hasMore]), an item older than the oldest loaded row stays out until that +/// page loads, so it is never drawn above messages that precede it. +List withChatShownItems( + List rows, + List items, { + required bool hasMore, +}) { + final out = []; + for (final row in rows) { + final parts = row['parts']; + if (row['kind'] != 'assistant' || parts is! List) { + out.add(row); + continue; + } + final kept = [ + for (final part in parts.whereType()) + if (!chatPipelinesToolRepresented(Map.from(part))) + part, + ]; + if (kept.length == parts.length) { + out.add(row); + } else if (kept.isNotEmpty || + row['streaming'] == true || + row['activityRecords'] != null) { + out.add({...row, 'parts': kept}); + } + } + if (items.isEmpty) return out; + final timed = [ + for (final item in items) + if (_createdAt(item) != null) item, + ]..sort((a, b) => _createdAt(a)!.compareTo(_createdAt(b)!)); + int? oldest; + var lastTimed = -1; + for (var index = 0; index < out.length; index++) { + final time = _rowTime(out[index]); + if (time == null) continue; + oldest ??= time; + lastTimed = index; + } + final result = []; + var next = 0; + JsonMap rowOf(JsonMap item) => { + 'kind': chatShownRowKind, + 'key': 'chat-shown-${item['id']}', + 'messageIds': ['shown:${item['id']}'], + 'item': item, + }; + if (hasMore) { + while (next < timed.length && + (oldest == null || _createdAt(timed[next])! < oldest)) { + next++; + } + } + for (var index = 0; index < out.length; index++) { + final time = _rowTime(out[index]); + if (time != null) { + while (next < timed.length && _createdAt(timed[next])! < time) { + result.add(rowOf(timed[next++])); + } + } + result.add(out[index]); + if (index == lastTimed) { + while (next < timed.length) { + result.add(rowOf(timed[next++])); + } + } + } + // A transcript with no timed row yet: the items lead. + if (lastTimed < 0) { + return [for (; next < timed.length; next++) rowOf(timed[next]), ...result]; + } + return result; +} diff --git a/app/lib/features/chat/ui/transcript/shown/chat_shown_card.dart b/app/lib/features/chat/ui/transcript/shown/chat_shown_card.dart new file mode 100644 index 00000000..ade376f6 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/shown/chat_shown_card.dart @@ -0,0 +1,50 @@ +import 'package:flutter/widgets.dart'; + +import '../../../../../core/api_gateway.dart'; +import '../../../../artifacts/ui/artifact_html_view.dart'; +import '../../../files/files_viewer.dart'; +import 'chat_shown_file_card.dart'; +import 'chat_shown_html_card.dart'; +import 'chat_shown_link_image.dart'; +import 'chat_shown_notify_row.dart'; + +/// One transcript row of kind `shown` ([withChatShownItems]): what the model +/// showed through a Pipelines tool, drawn by its recorded kind. [endpoint] is +/// the configured endpoint of the chat's own machine, so a file is read from +/// the machine that recorded it. +class ChatShownCard extends StatelessWidget { + const ChatShownCard({ + super.key, + required this.row, + required this.endpoint, + this.viewer = const FilesViewer(), + this.htmlBuilder = defaultArtifactHtml, + }); + + final JsonMap row; + final String endpoint; + final FilesViewer viewer; + final ArtifactHtmlBuilder htmlBuilder; + + @override + Widget build(BuildContext context) { + final raw = row['item']; + if (raw is! Map) return const SizedBox.shrink(); + final item = Map.from(raw); + final path = '${item['path'] ?? ''}'.trim(); + return switch ('${item['kind']}') { + 'notify' => ChatShownNotifyRow(item: item), + 'html' => ChatShownHtmlCard(item: item, htmlBuilder: htmlBuilder), + 'image' when path.isEmpty => ChatShownLinkImage( + item: item, + openExternal: viewer.openExternal, + ), + 'file' || 'image' => ChatShownFileCard( + item: item, + endpoint: endpoint, + viewer: viewer, + ), + _ => const SizedBox.shrink(), + }; + } +} diff --git a/app/lib/features/chat/ui/transcript/shown/chat_shown_file_card.dart b/app/lib/features/chat/ui/transcript/shown/chat_shown_file_card.dart new file mode 100644 index 00000000..7122e5de --- /dev/null +++ b/app/lib/features/chat/ui/transcript/shown/chat_shown_file_card.dart @@ -0,0 +1,161 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; + +import '../../../../../core/api_gateway.dart'; +import '../../../../artifacts/domain/artifact_kind.dart'; +import '../../../../artifacts/ui/artifact_viewer_fetch.dart'; +import '../../../data/directory_listing.dart'; +import '../../../files/files_icons.dart'; +import '../../../files/files_models.dart'; +import '../../../files/files_preview.dart'; +import '../../../files/files_thumbnail_image.dart'; +import '../../../files/files_urls.dart'; +import '../../../files/files_viewer.dart'; +import 'chat_shown_file_lines.dart'; +import 'chat_shown_surface.dart'; + +/// The file a `file` (or path `image`) entry names on the chat's machine. +DirectoryEntry chatShownEntry(JsonMap item) { + final path = '${item['path'] ?? ''}'; + var name = '${item['name'] ?? ''}'.trim(); + if (name.isEmpty) { + name = path.split('/').lastWhere((s) => s.isNotEmpty, orElse: () => path); + } + final size = item['size']; + return DirectoryEntry( + name: name, + path: path, + isDir: false, + isGitRepo: false, + hidden: false, + size: size is num ? size.toInt() : null, + ); +} + +/// The largest logical height an inline shown image takes. +const double chatShownImageMaxHeight = 280; + +/// A file the model showed: an image inline as a picture, any other file as a +/// card with its kind glyph, name and size, and the first lines of a text +/// document. Activating either opens the Files viewer (preview and download) +/// over the chat machine's `/files` endpoint ([endpoint]). +class ChatShownFileCard extends StatelessWidget { + const ChatShownFileCard({ + super.key, + required this.item, + required this.endpoint, + this.viewer = const FilesViewer(), + }); + + final JsonMap item; + final String endpoint; + final FilesViewer viewer; + + @override + Widget build(BuildContext context) { + final entry = chatShownEntry(item); + final urls = resolveFileUrls(endpoint, entry.path); + final image = isThumbnailFile(entry.name) && urls != null; + final kind = artifactKindLabel( + artifactKind(fileContentType(entry.name), entry.name), + ); + final title = chatShownTitle(item, entry.name); + final subtitle = [ + if (title != entry.name) entry.name, + kind, + fileSizeLabel(entry.size), + ].join(' · '); + void open() => viewer.open(context, endpoint: endpoint, entry: entry); + final heading = Padding( + padding: const EdgeInsets.all(12), + child: ChatShownHeading( + title: title, + subtitle: subtitle, + leading: image + ? null + : Icon( + fileIcon(entry), + size: 32, + color: chatShownSecondary(context), + ), + ), + ); + final body = Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + if (image) + _ChatShownPicture( + view: urls.view, + name: entry.name, + fetch: viewer.fetch, + ), + heading, + if (!image && urls != null) + ChatShownFileLines( + entry: entry, + view: urls.view, + fetch: viewer.fetch, + ), + ], + ); + final cupertino = chatShownCupertino(context); + return ChatShownSurface( + label: image ? 'Shown image $title' : 'Shown file $title', + child: Semantics( + button: true, + onTap: open, + child: cupertino + ? CupertinoButton( + padding: EdgeInsets.zero, + onPressed: open, + child: DefaultTextStyle.merge( + style: CupertinoTheme.of(context).textTheme.textStyle, + child: body, + ), + ) + : InkWell(onTap: open, child: body), + ), + ); + } +} + +/// An image file's own bytes from the files endpoint, decoded at the width it +/// is drawn, with the image glyph while loading and when it fails. +class _ChatShownPicture extends StatelessWidget { + const _ChatShownPicture({ + required this.view, + required this.name, + required this.fetch, + }); + + final Uri view; + final String name; + final ArtifactFetch fetch; + + @override + Widget build(BuildContext context) { + final ratio = MediaQuery.devicePixelRatioOf(context).clamp(1, 3); + final ImageProvider source = kIsWeb + ? NetworkImage(view.toString()) + : FileThumbnailImage(view, fetch: fetch); + final glyph = SizedBox( + height: 96, + child: Center( + child: Icon(Icons.image_outlined, color: chatShownSecondary(context)), + ), + ); + return ConstrainedBox( + constraints: const BoxConstraints(maxHeight: chatShownImageMaxHeight), + child: Image( + image: ResizeImage.resizeIfNeeded((560 * ratio).round(), null, source), + fit: BoxFit.contain, + semanticLabel: name, + loadingBuilder: (_, child, progress) => + progress == null ? child : glyph, + errorBuilder: (_, _, _) => glyph, + ), + ); + } +} diff --git a/app/lib/features/chat/ui/transcript/shown/chat_shown_file_lines.dart b/app/lib/features/chat/ui/transcript/shown/chat_shown_file_lines.dart new file mode 100644 index 00000000..40b8ad34 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/shown/chat_shown_file_lines.dart @@ -0,0 +1,105 @@ +import 'dart:convert'; + +import 'package:flutter/material.dart'; + +import '../../../../artifacts/domain/artifact_kind.dart'; +import '../../../../artifacts/ui/artifact_viewer_fetch.dart'; +import '../../../data/directory_listing.dart'; +import '../../../files/files_preview.dart'; +import 'chat_shown_surface.dart'; + +/// Text documents up to this size preview their first lines inline; larger +/// ones (and documents of unknown size) show only their card. +const int chatShownLinesMaxBytes = 256 << 10; + +/// How many leading lines the inline preview shows. +const int chatShownPreviewLines = 6; + +/// Whether [entry] is a text document the card previews by its first lines: +/// plain text, markdown and tables (the formats the Files viewer reads as +/// text). +bool chatShownPreviewsLines(DirectoryEntry entry) { + final size = entry.size; + if (size == null || size <= 0 || size > chatShownLinesMaxBytes) return false; + return switch (filePreviewFormat(fileContentType(entry.name), entry.name)) { + ArtifactPreviewFormat.text || + ArtifactPreviewFormat.markdown || + ArtifactPreviewFormat.table => true, + _ => false, + }; +} + +/// The first [chatShownPreviewLines] lines of a decoded document. +String chatShownLeadingLines(List bytes) { + final text = utf8.decode(bytes, allowMalformed: true); + final lines = const LineSplitter().convert(text); + return lines.take(chatShownPreviewLines).join('\n').trimRight(); +} + +/// The first lines of a small text document, fetched once over the same +/// scoped fetch the viewer uses. Nothing is drawn for a document that is not +/// previewable or whose fetch fails: the card still names and opens it. +class ChatShownFileLines extends StatefulWidget { + const ChatShownFileLines({ + super.key, + required this.entry, + required this.view, + required this.fetch, + }); + + final DirectoryEntry entry; + final Uri view; + final ArtifactFetch fetch; + + @override + State createState() => _ChatShownFileLinesState(); +} + +class _ChatShownFileLinesState extends State { + Future? _lines; + + @override + void initState() { + super.initState(); + if (chatShownPreviewsLines(widget.entry)) _lines = _load(); + } + + Future _load() async { + try { + final result = await widget.fetch(widget.view); + if (!result.ok) return null; + final text = chatShownLeadingLines(result.bytes); + return text.isEmpty ? null : text; + } catch (_) { + return null; + } + } + + @override + Widget build(BuildContext context) { + final lines = _lines; + if (lines == null) return const SizedBox.shrink(); + return FutureBuilder( + future: lines, + builder: (context, snapshot) { + final text = snapshot.data; + if (text == null) return const SizedBox.shrink(); + final style = Theme.of(context).textTheme.bodySmall?.copyWith( + fontFamily: 'monospace', + color: chatShownSecondary(context), + ); + return Padding( + key: const Key('chat-shown-file-lines'), + padding: const EdgeInsetsDirectional.fromSTEB(12, 0, 12, 12), + child: Text( + text, + style: style, + maxLines: chatShownPreviewLines, + overflow: TextOverflow.ellipsis, + softWrap: false, + ), + ); + }, + ); + } +} diff --git a/app/lib/features/chat/ui/transcript/shown/chat_shown_html_card.dart b/app/lib/features/chat/ui/transcript/shown/chat_shown_html_card.dart new file mode 100644 index 00000000..1336b521 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/shown/chat_shown_html_card.dart @@ -0,0 +1,188 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../../../../core/api_gateway.dart'; +import '../../../../../ui/adaptive/host.dart'; +import '../../../../artifacts/ui/artifact_fullscreen.dart'; +import '../../../../artifacts/ui/artifact_html_view.dart'; +import '../../../../artifacts/ui/artifact_viewer_scaffold.dart'; +import '../../../files/files_models.dart'; +import 'chat_shown_surface.dart'; + +/// The inline frame's height when the model gave no hint. +const double chatShownHtmlDefaultHeight = 320; + +/// The inline frame's height: the model's hint held between 120 and 600. +double chatShownHtmlHeight(JsonMap item) { + final hint = item['height_hint']; + if (hint is! num || hint <= 0) return chatShownHtmlDefaultHeight; + return hint.toDouble().clamp(120, 600); +} + +/// A view the model authored (HTML or SVG), drawn inline by the same inert +/// renderer as an HTML artifact preview: on the web an iframe whose sandbox +/// is only `allow-popups allow-popups-to-escape-sandbox` (no scripts, no +/// same origin, no forms, no navigation of the app); on Android and iOS a web +/// view with JavaScript off that refuses navigation. Open shows it full +/// screen. A body the server no longer retains says so instead. +class ChatShownHtmlCard extends StatelessWidget { + const ChatShownHtmlCard({ + super.key, + required this.item, + this.htmlBuilder = defaultArtifactHtml, + }); + + final JsonMap item; + final ArtifactHtmlBuilder htmlBuilder; + + @override + Widget build(BuildContext context) { + final title = chatShownTitle(item, 'View'); + final html = '${item['html'] ?? ''}'; + final expired = item['expired'] == true || html.isEmpty; + final bytes = item['html_bytes']; + final size = bytes is num ? fileSizeLabel(bytes.toInt()) : null; + final cupertino = chatShownCupertino(context); + final open = expired + ? null + : () => Navigator.of(context, rootNavigator: true).push( + artifactFullscreenRoute( + cupertino: usesCupertinoHost(context), + builder: (_) => ChatShownHtmlPage( + title: title, + html: html, + htmlBuilder: htmlBuilder, + ), + ), + ); + final action = open == null + ? null + : cupertino + ? CupertinoButton( + key: const Key('chat-shown-html-open'), + padding: const EdgeInsets.symmetric(horizontal: 8), + onPressed: open, + child: const Text('Open'), + ) + : TextButton( + key: const Key('chat-shown-html-open'), + onPressed: open, + child: const Text('Open'), + ); + final heading = Padding( + padding: const EdgeInsetsDirectional.fromSTEB(12, 8, 4, 8), + child: ChatShownHeading( + title: title, + subtitle: expired + ? 'This view is no longer retained' + : ['View', ?size].join(' · '), + leading: Icon( + cupertino ? CupertinoIcons.chart_bar_square : Icons.web_asset, + color: chatShownSecondary(context), + ), + trailing: action, + ), + ); + return ChatShownSurface( + label: 'Shown view $title', + maxWidth: 720, + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + heading, + if (!expired) + // Inset from the card's edge: a web platform view is not clipped + // by the card's rounded outline and would draw over it. + Padding( + padding: const EdgeInsetsDirectional.fromSTEB(8, 0, 8, 8), + child: SizedBox( + height: chatShownHtmlHeight(item), + child: MediaQuery.removePadding( + context: context, + removeTop: true, + removeBottom: true, + child: _SettledFrame( + builder: (context) => htmlBuilder(context, html), + ), + ), + ), + ), + ], + ), + ); + } +} + +/// Builds [builder] from the row's second frame on. The transcript's lazy +/// list can lay a row out and drop it within one frame; a platform view +/// (the web frame) laid out that way fails its post-frame position read, so +/// the frame is only created for a row that is still mounted a frame later. +class _SettledFrame extends StatefulWidget { + const _SettledFrame({required this.builder}); + + final WidgetBuilder builder; + + @override + State<_SettledFrame> createState() => _SettledFrameState(); +} + +class _SettledFrameState extends State<_SettledFrame> { + bool _settled = false; + + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) setState(() => _settled = true); + }); + } + + @override + Widget build(BuildContext context) => + _settled ? widget.builder(context) : const SizedBox.expand(); +} + +/// The full-screen view of a shown HTML entry: the platform's Close, the +/// title, and on the web the zoom actions the sandboxed frame needs. +class ChatShownHtmlPage extends StatefulWidget { + const ChatShownHtmlPage({ + super.key, + required this.title, + required this.html, + this.htmlBuilder = defaultArtifactHtml, + }); + + final String title; + final String html; + final ArtifactHtmlBuilder htmlBuilder; + + @override + State createState() => _ChatShownHtmlPageState(); +} + +class _ChatShownHtmlPageState extends State { + final ValueNotifier _zoom = ValueNotifier(1); + + @override + void dispose() { + _zoom.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) => ValueListenableBuilder( + valueListenable: _zoom, + builder: (context, _, _) => ArtifactViewerScaffold( + title: widget.title, + subtitle: null, + actions: artifactHtmlZoomButtons ? artifactHtmlZoomActions(_zoom) : [], + body: ArtifactFullscreenScope( + htmlZoom: _zoom, + child: Builder( + builder: (context) => widget.htmlBuilder(context, widget.html), + ), + ), + ), + ); +} diff --git a/app/lib/features/chat/ui/transcript/shown/chat_shown_link_image.dart b/app/lib/features/chat/ui/transcript/shown/chat_shown_link_image.dart new file mode 100644 index 00000000..fd7c7848 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/shown/chat_shown_link_image.dart @@ -0,0 +1,100 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../../../../core/api_gateway.dart'; +import '../../../../artifacts/ui/artifact_external_open.dart'; +import 'chat_shown_file_card.dart'; +import 'chat_shown_surface.dart'; + +/// The https address of a web `image` entry, or null for any other scheme. +Uri? chatShownImageUrl(JsonMap item) { + final uri = Uri.tryParse('${item['url'] ?? ''}'.trim()); + if (uri == null || uri.scheme != 'https' || uri.host.isEmpty) return null; + return uri; +} + +/// A web image the model showed: the picture inline with its address under +/// it. The picture is fetched without credentials (the default network image +/// fetch; on the web that needs the host to allow cross-origin reads, and a +/// refused fetch shows the glyph and the link). Activating it opens the +/// address outside the app. +class ChatShownLinkImage extends StatelessWidget { + const ChatShownLinkImage({ + super.key, + required this.item, + this.openExternal = openArtifactExternally, + }); + + final JsonMap item; + final ArtifactExternalOpen openExternal; + + @override + Widget build(BuildContext context) { + final url = chatShownImageUrl(item); + final address = '${item['url'] ?? ''}'.trim(); + final title = chatShownTitle(item, url?.host ?? 'Image'); + final alt = '${item['alt'] ?? ''}'.trim(); + final glyph = SizedBox( + height: 96, + child: Center( + child: Icon( + Icons.broken_image_outlined, + color: chatShownSecondary(context), + ), + ), + ); + final picture = url == null + ? glyph + : ConstrainedBox( + constraints: const BoxConstraints( + maxHeight: chatShownImageMaxHeight, + ), + child: Image.network( + url.toString(), + fit: BoxFit.contain, + semanticLabel: alt.isEmpty ? title : alt, + loadingBuilder: (_, child, progress) => + progress == null ? child : glyph, + errorBuilder: (_, _, _) => glyph, + ), + ); + final body = Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + picture, + Padding( + padding: const EdgeInsets.all(12), + child: ChatShownHeading( + title: title, + subtitle: url == null ? 'Not an https address: $address' : address, + ), + ), + ], + ); + void open() { + if (url != null) openExternal(url); + } + + return ChatShownSurface( + label: 'Shown image $title', + child: url == null + ? body + : Semantics( + button: true, + link: true, + onTap: open, + child: chatShownCupertino(context) + ? CupertinoButton( + padding: EdgeInsets.zero, + onPressed: open, + child: DefaultTextStyle.merge( + style: CupertinoTheme.of(context).textTheme.textStyle, + child: body, + ), + ) + : InkWell(onTap: open, child: body), + ), + ); + } +} diff --git a/app/lib/features/chat/ui/transcript/shown/chat_shown_notify_row.dart b/app/lib/features/chat/ui/transcript/shown/chat_shown_notify_row.dart new file mode 100644 index 00000000..3c6dcc56 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/shown/chat_shown_notify_row.dart @@ -0,0 +1,84 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../../../../core/api_gateway.dart'; +import 'chat_shown_surface.dart'; + +/// What a `notify` entry says: the phone notification was accepted for +/// delivery, or why it was not sent. +String chatShownNotifyText(JsonMap item) { + final title = chatShownTitle(item, 'notification'); + if (item['delivered'] == true) return 'Notified your phone: $title'; + final detail = '${item['detail'] ?? ''}'.trim(); + return detail.isEmpty + ? 'Phone notification not sent: $title' + : 'Phone notification not sent: $title — $detail'; +} + +/// One secondary line in the agent's column for a `notify` entry, with the +/// notification's message under it. +class ChatShownNotifyRow extends StatelessWidget { + const ChatShownNotifyRow({super.key, required this.item}); + + final JsonMap item; + + @override + Widget build(BuildContext context) { + final cupertino = chatShownCupertino(context); + final delivered = item['delivered'] == true; + final secondary = chatShownSecondary(context); + final color = delivered + ? secondary + : (cupertino + ? CupertinoColors.systemRed.resolveFrom(context) + : Theme.of(context).colorScheme.error); + final base = cupertino + ? CupertinoTheme.of(context).textTheme.textStyle + : Theme.of(context).textTheme.bodySmall; + final text = chatShownNotifyText(item); + final message = '${item['message'] ?? ''}'.trim(); + final icon = delivered + ? (cupertino ? CupertinoIcons.bell : Icons.notifications_outlined) + : (cupertino + ? CupertinoIcons.bell_slash + : Icons.notifications_off_outlined); + return Semantics( + container: true, + label: text, + value: message, + child: ExcludeSemantics( + child: Padding( + padding: const EdgeInsets.symmetric(vertical: 6), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsetsDirectional.only(end: 8), + child: Icon( + icon, + size: base?.fontSize == null ? 16 : base!.fontSize! * 1.2, + color: color, + ), + ), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text(text, style: base?.copyWith(color: color)), + if (message.isNotEmpty) + Text( + message, + maxLines: 2, + overflow: TextOverflow.ellipsis, + style: base?.copyWith(color: secondary), + ), + ], + ), + ), + ], + ), + ), + ), + ); + } +} diff --git a/app/lib/features/chat/ui/transcript/shown/chat_shown_surface.dart b/app/lib/features/chat/ui/transcript/shown/chat_shown_surface.dart new file mode 100644 index 00000000..2f4fbdde --- /dev/null +++ b/app/lib/features/chat/ui/transcript/shown/chat_shown_surface.dart @@ -0,0 +1,142 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../../../../core/api_gateway.dart'; +import '../../sessions/chat_adaptive.dart'; + +/// Whether the transcript draws Cupertino controls. +bool chatShownCupertino(BuildContext context) => + chatControlsStyleOf(context) == ChatControlsStyle.cupertino; + +/// The secondary text colour of either host. +Color chatShownSecondary(BuildContext context) => chatShownCupertino(context) + ? CupertinoColors.secondaryLabel.resolveFrom(context) + : Theme.of(context).colorScheme.onSurfaceVariant; + +/// The item's title, else [fallback]. +String chatShownTitle(JsonMap item, String fallback) { + final title = '${item['title'] ?? ''}'.trim(); + return title.isEmpty ? fallback : title; +} + +/// The outlined card a shown entry sits in, on the agent's side of the +/// transcript, at most [maxWidth] wide. +class ChatShownSurface extends StatelessWidget { + const ChatShownSurface({ + super.key, + required this.label, + required this.child, + this.maxWidth = 560, + }); + + /// The entry's accessible name ("Shown file", "Shown view", ...). + final String label; + final Widget child; + final double maxWidth; + + @override + Widget build(BuildContext context) { + final cupertino = chatShownCupertino(context); + const radius = BorderRadius.all(Radius.circular(12)); + final card = cupertino + ? DecoratedBox( + decoration: BoxDecoration( + color: CupertinoColors.secondarySystemGroupedBackground + .resolveFrom(context), + border: Border.all( + color: CupertinoColors.separator.resolveFrom(context), + ), + borderRadius: radius, + ), + child: ClipRRect(borderRadius: radius, child: child), + ) + : Card.outlined( + margin: EdgeInsets.zero, + clipBehavior: Clip.antiAlias, + child: child, + ); + return Semantics( + container: true, + explicitChildNodes: true, + label: label, + child: Padding( + padding: const EdgeInsets.symmetric(vertical: 6), + child: Align( + alignment: AlignmentDirectional.centerStart, + child: ConstrainedBox( + constraints: BoxConstraints(maxWidth: maxWidth), + child: card, + ), + ), + ), + ); + } +} + +/// A title line with an optional subtitle, leading glyph and trailing action. +class ChatShownHeading extends StatelessWidget { + const ChatShownHeading({ + super.key, + required this.title, + this.subtitle, + this.leading, + this.trailing, + }); + + final String title; + final String? subtitle; + final Widget? leading; + final Widget? trailing; + + @override + Widget build(BuildContext context) { + final cupertino = chatShownCupertino(context); + final titleStyle = cupertino + ? CupertinoTheme.of( + context, + ).textTheme.textStyle.copyWith(fontWeight: FontWeight.w600) + : Theme.of(context).textTheme.titleSmall; + // Cupertino has no caption role; the body style at a smaller size keeps + // the line secondary while still following text scaling. + final cupertinoBody = CupertinoTheme.of(context).textTheme.textStyle; + final subtitleStyle = + (cupertino + ? cupertinoBody.copyWith( + fontSize: (cupertinoBody.fontSize ?? 17) * 0.8, + ) + : Theme.of(context).textTheme.bodySmall) + ?.copyWith(color: chatShownSecondary(context)); + final sub = subtitle; + return Row( + children: [ + if (leading != null) + Padding( + padding: const EdgeInsetsDirectional.only(end: 12), + child: leading, + ), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + Text( + title, + style: titleStyle, + maxLines: 2, + overflow: TextOverflow.ellipsis, + ), + if (sub != null && sub.isNotEmpty) + Text( + sub, + style: subtitleStyle, + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + ], + ), + ), + ?trailing, + ], + ); + } +} diff --git a/app/notes/images/chat-pipelines-tools/cards-cupertino-560.png b/app/notes/images/chat-pipelines-tools/cards-cupertino-560.png new file mode 100644 index 00000000..e3ded793 Binary files /dev/null and b/app/notes/images/chat-pipelines-tools/cards-cupertino-560.png differ diff --git a/app/notes/images/chat-pipelines-tools/cards-dark-880.png b/app/notes/images/chat-pipelines-tools/cards-dark-880.png new file mode 100644 index 00000000..76751698 Binary files /dev/null and b/app/notes/images/chat-pipelines-tools/cards-dark-880.png differ diff --git a/app/notes/images/chat-pipelines-tools/cards-light-880.png b/app/notes/images/chat-pipelines-tools/cards-light-880.png new file mode 100644 index 00000000..32bb33cb Binary files /dev/null and b/app/notes/images/chat-pipelines-tools/cards-light-880.png differ diff --git a/app/notes/images/chat-pipelines-tools/html-open-light-880.png b/app/notes/images/chat-pipelines-tools/html-open-light-880.png new file mode 100644 index 00000000..d1be5d94 Binary files /dev/null and b/app/notes/images/chat-pipelines-tools/html-open-light-880.png differ diff --git a/app/notes/images/chat-pipelines-tools/image-open-dark-880.png b/app/notes/images/chat-pipelines-tools/image-open-dark-880.png new file mode 100644 index 00000000..a3378c15 Binary files /dev/null and b/app/notes/images/chat-pipelines-tools/image-open-dark-880.png differ diff --git a/app/test/features/chat/conversation/chat_shown_controller_test.dart b/app/test/features/chat/conversation/chat_shown_controller_test.dart new file mode 100644 index 00000000..a52647e3 --- /dev/null +++ b/app/test/features/chat/conversation/chat_shown_controller_test.dart @@ -0,0 +1,88 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_thread_controller.dart'; + +import 'chat_thread_fixtures.dart'; + +void main() { + late FakeChatGateway gateway; + late ChatThreadController controller; + + setUp(() { + gateway = FakeChatGateway(); + gateway.history = (_) async => chatPage([]); + controller = ChatThreadController(gateway, 's'); + }); + + tearDown(() { + controller.dispose(); + gateway.close(); + }); + + test('takes the retained list from the subscribe reply', () async { + gateway.subscribe = () async => { + 'chat_shown': { + 'session_id': 's', + 'items': [ + {'id': 'a', 'kind': 'file', 'created_at': 1, 'path': '/h/a.png'}, + {'id': 'b', 'kind': 'notify', 'created_at': 2, 'title': 'Done'}, + ], + }, + }; + await controller.load(); + expect(controller.shown.map((item) => item['id']), ['a', 'b']); + }); + + test('a pushed frame adds or replaces one item of this chat only', () async { + gateway.subscribe = () async => { + 'chat_shown': { + 'session_id': 's', + 'items': [ + {'id': 'a', 'kind': 'notify', 'created_at': 1, 'delivered': false}, + ], + }, + }; + await controller.load(); + var notified = 0; + controller.transcriptChanges.addListener(() => notified++); + gateway.emitRaw({ + 'type': 'chat_shown', + 'chat_shown': { + 'session_id': 'other', + 'items': [ + {'id': 'x', 'kind': 'notify', 'created_at': 3}, + ], + }, + }); + gateway.emitRaw({ + 'type': 'chat_shown', + 'chat_shown': { + 'session_id': 's', + 'items': [ + {'id': 'a', 'kind': 'notify', 'created_at': 1, 'delivered': true}, + {'id': 'c', 'kind': 'html', 'created_at': 4, 'html': '

x

'}, + ], + }, + }); + await pumpEventQueue(); + expect(controller.shown.map((item) => item['id']), ['a', 'c']); + expect(controller.shown.first['delivered'], isTrue); + expect(notified, 1); + }); + + test('a resync re-subscribes and adopts the server list', () async { + var round = 0; + gateway.subscribe = () async => { + 'chat_shown': { + 'session_id': 's', + 'items': [ + {'id': 'r$round', 'kind': 'notify', 'created_at': 1}, + ], + }, + }; + gateway.onSubscribe = () => round++; + await controller.load(); + expect(controller.shown.single['id'], 'r1'); + await controller.resync(); + expect(controller.shown.single['id'], 'r2'); + }); +} diff --git a/app/test/features/chat/domain/chat_shown_items_test.dart b/app/test/features/chat/domain/chat_shown_items_test.dart new file mode 100644 index 00000000..5f4321c5 --- /dev/null +++ b/app/test/features/chat/domain/chat_shown_items_test.dart @@ -0,0 +1,165 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/domain/chat_shown_items.dart'; +import 'package:pipelines_app/features/chat/domain/chat_transcript.dart'; + +JsonMap _item(String id, int at, {String kind = 'notify'}) => { + 'id': id, + 'kind': kind, + 'created_at': at, + 'title': id, +}; + +JsonMap _message(String id, String role, int at, {List? parts}) => { + 'id': id, + 'role': role, + 'created_at': at, + 'parts': + parts ?? + [ + {'type': 'text', 'text': id}, + ], +}; + +List _shape(List rows) => [ + for (final row in rows) + row['kind'] == 'shown' + ? 'shown:${(row['item'] as Map)['id']}' + : '${row['kind']}:${row['key']}', +]; + +void main() { + group('chatShownItemsOf', () { + test('reads only this session and known kinds with ids', () { + final body = { + 'session_id': 's', + 'items': [ + _item('a', 1), + {'id': '', 'kind': 'file'}, + {'id': 'x', 'kind': 'future'}, + 'junk', + ], + }; + expect(chatShownItemsOf(body, 's')!.map((i) => i['id']), ['a']); + expect(chatShownItemsOf(body, 'other'), isNull); + expect(chatShownItemsOf(null, 's'), isNull); + }); + }); + + test('merge replaces by id in place and appends new items', () { + final held = [_item('a', 1), _item('b', 2)]; + final updated = {..._item('a', 1), 'title': 'A2'}; + final merged = mergeChatShownItems(held, [updated, _item('c', 3)]); + expect(merged.map((i) => i['id']), ['a', 'b', 'c']); + expect(merged.first['title'], 'A2'); + expect( + mergeChatShownItems(held, [_item('z', 9)], replace: true).single['id'], + 'z', + ); + }); + + group('withChatShownItems', () { + final messages = [ + _message('u1', 'user', 100), + _message('a1', 'assistant', 110), + _message('u2', 'user', 200), + _message('a2', 'assistant', 210), + ]; + + test('interleaves items by time before trailing cards', () { + final rows = transcriptRows( + messages, + trailing: [ + {'kind': 'permission', 'key': 'p'}, + ], + ); + final out = withChatShownItems(rows, [ + _item('late', 500), + _item('mid', 150), + ], hasMore: false); + expect(_shape(out), [ + 'user:u1', + 'assistant:a1', + 'shown:mid', + 'user:u2', + 'assistant:a2', + 'shown:late', + 'permission:p', + ]); + final shown = out.firstWhere((row) => row['kind'] == 'shown'); + expect(shown['messageIds'], ['shown:mid']); + }); + + test('older items wait for their history page while more remains', () { + final rows = transcriptRows(messages); + final early = [_item('early', 50), _item('mid', 150)]; + expect( + _shape(withChatShownItems(rows, early, hasMore: true)), + isNot(contains('shown:early')), + ); + expect( + _shape(withChatShownItems(rows, early, hasMore: false)).first, + 'shown:early', + ); + }); + + test('items lead an empty transcript only when no history remains', () { + expect(_shape(withChatShownItems([], [_item('a', 1)], hasMore: false)), [ + 'shown:a', + ]); + expect(withChatShownItems([], [_item('a', 1)], hasMore: true), isEmpty); + }); + + test('hides represented Pipelines tool calls, keeps others and errors', () { + JsonMap tool(String name, {String status = 'completed'}) => { + 'type': 'tool', + 'id': name, + 'name': name, + 'state': {'status': status}, + }; + final turn = _message( + 'a', + 'assistant', + 10, + parts: [ + {'type': 'text', 'text': 'Here it is'}, + tool('mcp__pipelines__show_file'), + tool('pipelines_show_html'), + tool('pipelines/notify'), + tool('pipelines_show_image', status: 'error'), + tool('bash'), + tool('pipelines_other'), + ], + ); + final out = withChatShownItems( + transcriptRows([turn]), + [], + hasMore: false, + ); + final names = [ + for (final part in out.single['parts'] as List) + if (part['type'] == 'tool') part['name'], + ]; + expect(names, ['pipelines_show_image', 'bash', 'pipelines_other']); + }); + + test('a turn holding only a represented call is not drawn', () { + final turn = _message( + 'a', + 'assistant', + 10, + parts: [ + { + 'type': 'tool', + 'name': 'pipelines_notify', + 'state': {'status': 'completed'}, + }, + ], + ); + final out = withChatShownItems(transcriptRows([turn]), [ + _item('n', 20), + ], hasMore: false); + expect(_shape(out), ['shown:n']); + }); + }); +} diff --git a/app/test/features/chat/ui/transcript/shown/chat_shown_card_test.dart b/app/test/features/chat/ui/transcript/shown/chat_shown_card_test.dart new file mode 100644 index 00000000..2765ae5e --- /dev/null +++ b/app/test/features/chat/ui/transcript/shown/chat_shown_card_test.dart @@ -0,0 +1,215 @@ +import 'dart:convert'; + +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/artifacts/ui/artifact_fullscreen.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/chat/files/files_urls.dart'; +import 'package:pipelines_app/features/chat/files/files_viewer.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/shown/chat_shown_card.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/shown/chat_shown_html_card.dart'; + +const _png = + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=='; + +Widget _stubHtml(BuildContext context, String html) => Container( + key: const Key('stub-html'), + child: Text( + ArtifactFullscreenScope.maybeOf(context) == null + ? 'inline $html' + : 'full $html', + ), +); + +void main() { + late FileUrlResolver original; + final fetched = []; + final opened = []; + setUp(() { + original = resolveFileUrls; + resolveFileUrls = (endpoint, path) => fileUrls(endpoint, path); + fetched.clear(); + opened.clear(); + }); + tearDown(() => resolveFileUrls = original); + + final viewer = FilesViewer( + fetch: (uri) async { + fetched.add(uri); + final path = uri.queryParameters['path'] ?? ''; + return ArtifactFetchResult( + status: 200, + bytes: path.endsWith('.png') + ? base64Decode(_png) + : utf8.encode('# Release notes\nline two\nline three\n'), + ); + }, + openExternal: (uri) async { + opened.add(uri); + return true; + }, + ); + + Future pump(WidgetTester tester, JsonMap item) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SingleChildScrollView( + child: ChatShownCard( + row: {'kind': 'shown', 'item': item}, + endpoint: 'ws://peer.example.test/peers/abc/connect', + viewer: viewer, + htmlBuilder: _stubHtml, + ), + ), + ), + ), + ); + await tester.pumpAndSettle(); + } + + testWidgets('an image file draws inline and opens the files viewer', ( + tester, + ) async { + await pump(tester, { + 'id': 'i', + 'kind': 'file', + 'path': '/home/demo/chart.png', + 'name': 'chart.png', + 'size': 2048, + 'title': 'Latency chart', + }); + expect(find.text('Latency chart'), findsOneWidget); + expect(find.text('chart.png · Image · 2.0 KiB'), findsOneWidget); + expect(find.bySemanticsLabel('chart.png'), findsOneWidget); + await tester.tap(find.text('Latency chart')); + await tester.pumpAndSettle(); + expect(find.byType(ArtifactViewerPage), findsOneWidget); + expect(fetched.last.path, '/peers/abc/files'); + expect(fetched.last.queryParameters['path'], '/home/demo/chart.png'); + }); + + testWidgets('a document card shows icon, size and its first lines', ( + tester, + ) async { + await pump(tester, { + 'id': 'd', + 'kind': 'file', + 'path': '/home/demo/NOTES.md', + 'name': 'NOTES.md', + 'size': 40, + }); + expect(find.text('NOTES.md'), findsOneWidget); + expect(find.byIcon(Icons.description_outlined), findsOneWidget); + expect(find.byKey(const Key('chat-shown-file-lines')), findsOneWidget); + expect(find.textContaining('# Release notes'), findsOneWidget); + expect(fetched.single.queryParameters['path'], '/home/demo/NOTES.md'); + await tester.tap(find.text('NOTES.md')); + await tester.pumpAndSettle(); + expect(find.byType(ArtifactViewerPage), findsOneWidget); + }); + + testWidgets('a PDF card opens the host handler', (tester) async { + await pump(tester, { + 'id': 'p', + 'kind': 'file', + 'path': '/home/demo/report.pdf', + 'size': 100000, + }); + expect(find.byIcon(Icons.picture_as_pdf_outlined), findsOneWidget); + expect(find.byKey(const Key('chat-shown-file-lines')), findsNothing); + await tester.tap(find.text('report.pdf')); + await tester.pumpAndSettle(); + expect(opened.single.queryParameters['path'], '/home/demo/report.pdf'); + }); + + testWidgets('a web image shows its address and opens it outside', ( + tester, + ) async { + await pump(tester, { + 'id': 'w', + 'kind': 'image', + 'url': 'https://example.test/cat.png', + 'alt': 'A cat', + }); + expect(find.text('https://example.test/cat.png'), findsOneWidget); + await tester.tap(find.text('example.test')); + await tester.pumpAndSettle(); + expect(opened.single.toString(), 'https://example.test/cat.png'); + }); + + testWidgets('a non-https image address is never fetched or opened', ( + tester, + ) async { + await pump(tester, { + 'id': 'w', + 'kind': 'image', + 'url': 'http://example.test/cat.png', + }); + expect(find.byType(Image), findsNothing); + expect(find.textContaining('Not an https address'), findsOneWidget); + }); + + testWidgets('an HTML view renders inline at its hint and opens full screen', ( + tester, + ) async { + await pump(tester, { + 'id': 'h', + 'kind': 'html', + 'title': 'Pipeline graph', + 'html': '', + 'html_bytes': 11, + 'height_hint': 2000, + }); + expect(find.text('inline '), findsOneWidget); + final box = tester.getSize( + find + .ancestor( + of: find.byKey(const Key('stub-html')), + matching: find.byType(SizedBox), + ) + .first, + ); + expect(box.height, 600); + await tester.tap(find.byKey(const Key('chat-shown-html-open'))); + await tester.pumpAndSettle(); + expect(find.byType(ChatShownHtmlPage), findsOneWidget); + expect(find.text('full '), findsOneWidget); + }); + + testWidgets('an expired HTML view says so and offers no Open', ( + tester, + ) async { + await pump(tester, {'id': 'h', 'kind': 'html', 'expired': true}); + expect(find.text('This view is no longer retained'), findsOneWidget); + expect(find.byKey(const Key('chat-shown-html-open')), findsNothing); + expect(find.byKey(const Key('stub-html')), findsNothing); + }); + + testWidgets('notify rows say whether the phone was notified', (tester) async { + await pump(tester, { + 'id': 'n', + 'kind': 'notify', + 'title': 'Build done', + 'message': 'All green', + 'delivered': true, + }); + expect(find.text('Notified your phone: Build done'), findsOneWidget); + expect(find.text('All green'), findsOneWidget); + await pump(tester, { + 'id': 'n2', + 'kind': 'notify', + 'title': 'Build done', + 'delivered': false, + 'detail': 'notifications are not set up', + }); + expect( + find.text( + 'Phone notification not sent: Build done — notifications are not set up', + ), + findsOneWidget, + ); + }); +} diff --git a/app/tool/perf/chat_shown_fixture.dart b/app/tool/perf/chat_shown_fixture.dart new file mode 100644 index 00000000..78b14d48 --- /dev/null +++ b/app/tool/perf/chat_shown_fixture.dart @@ -0,0 +1,72 @@ +// `?shown=1` (with `?chats=N×tamps=1`, and `?files=1` so the files +// resolve to tool/capture/fixtures/files copied under capture-files/): the +// subscribe reply carries one of each Pipelines tool entry, after the last answer of the +// bench transcript's messages (perf-m0..19 at 09:00..09:19 UTC). +import 'package:pipelines_app/core/api_gateway.dart'; + +int _at(int minute, int second) => + DateTime.utc(2026, 11, 5, 9, minute, second).millisecondsSinceEpoch; + +const String _diagram = ''' + + + + + + Build + + Test + + Release + + + +
Stage timings +

Build 2m 10s · Test 4m 02s · Release 38s

+'''; + +/// The retained entries the fixture's subscribe reply carries. +JsonMap chatShownFixture(Object? sessionId) => { + 'session_id': sessionId, + 'items': [ + { + 'id': 'shown-image', + 'kind': 'file', + 'created_at': _at(19, 10), + 'title': 'Release design', + 'path': '/srv/probe/release/design.png', + 'name': 'design.png', + 'size': 18432, + 'mime': 'image/png', + }, + { + 'id': 'shown-doc', + 'kind': 'file', + 'created_at': _at(19, 20), + 'path': '/srv/probe/release/release.md', + 'name': 'release.md', + 'size': 640, + 'mime': 'text/markdown', + }, + { + 'id': 'shown-html', + 'kind': 'html', + 'created_at': _at(19, 30), + 'title': 'Pipeline stages', + 'html': _diagram, + 'html_bytes': _diagram.length, + 'height_hint': 220, + }, + { + 'id': 'shown-notify', + 'kind': 'notify', + 'created_at': _at(19, 40), + 'title': 'Release ready', + 'message': 'All three stages passed; the release is ready to publish.', + 'priority': 'default', + 'delivered': true, + }, + ], +}; diff --git a/app/tool/perf/perf_fixture.dart b/app/tool/perf/perf_fixture.dart index cdde9334..74273223 100644 --- a/app/tool/perf/perf_fixture.dart +++ b/app/tool/perf/perf_fixture.dart @@ -22,6 +22,7 @@ import 'package:pipelines_app/core/api_gateway.dart'; import 'chat_new_session_fixture.dart'; import 'chat_question_fixture.dart'; +import 'chat_shown_fixture.dart'; import 'chat_scroll_fixture.dart'; Map _params = const {}; @@ -264,7 +265,14 @@ JsonMap? perfChatReply(JsonMap request) { case 'chat_new_session': return perfChatNewSessionReply(request, _chatRow); case 'chat_subscribe': - return {'id': id, 'type': 'chat_subscribed'}; + return { + 'id': id, + 'type': 'chat_subscribed', + if (_params['shown'] == '1') + 'chat_shown': chatShownFixture( + (request['chat_subscribe'] as Map?)?['session_id'], + ), + }; case 'chat_prompt': // Accepted, as the server does; no reply follows, so a capture can // watch the sent message itself.