diff --git a/app/lib/features/chat/domain/chat_tool_calls.dart b/app/lib/features/chat/domain/chat_tool_calls.dart new file mode 100644 index 00000000..36c92a00 --- /dev/null +++ b/app/lib/features/chat/domain/chat_tool_calls.dart @@ -0,0 +1,119 @@ +import 'dart:convert'; + +import '../../../core/api_gateway.dart'; + +/// Tool calls in an agent turn, ported from the web client's `src/chat.js` +/// (`toolLabel`, `toolCallsLabel`, `toolDetail`, `toolCallRow`, ...): the turn +/// shows one summary chip below its answer, and each call opens to its own +/// formatted input and output instead of raw JSON. + +bool isChatToolPart(JsonMap part) => '${part['type'] ?? ''}' == 'tool'; + +JsonMap _state(JsonMap part) => part['state'] is Map + ? JsonMap.from(part['state'] as Map) + : const {}; + +/// A call's name, or `tool` when unnamed. +String chatToolLabel(JsonMap part) { + final name = '${part['name'] ?? ''}'.trim(); + return name.isEmpty ? 'tool' : name; +} + +/// The turn's chip: `1 tool` / `N tools`. +String chatToolCallsLabel(int count) => count == 1 ? '1 tool' : '$count tools'; + +bool _runningStatus(Object? status) => + '$status' == 'streaming' || '$status' == 'running'; + +/// Still in flight: `streaming` or `running`. +bool chatToolRunning(JsonMap part) => + isChatToolPart(part) && _runningStatus(_state(part)['status']); + +/// Ended in an error state. +bool chatToolFailed(JsonMap part) => + '${_state(part)['status'] ?? ''}' == 'error'; + +/// Wall-clock duration, or null without a completion time. +int? chatToolDurationMillis(JsonMap part) { + final time = part['time']; + if (time is! Map) return null; + num read(Object? v) => v is num ? v : (num.tryParse('$v') ?? 0); + final ran = read(time['ran']); + final start = ran > 0 ? ran : read(time['created']); + final completed = read(time['completed']); + if (start <= 0 || completed <= 0 || completed < start) return null; + return (completed - start).toInt(); +} + +/// `340ms` / `1.2s` / `2m 3s`. +String formatChatToolDuration(int millis) { + if (millis < 1000) return '${millis}ms'; + if (millis < 60000) return '${(millis / 1000).toStringAsFixed(1)}s'; + return '${millis ~/ 60000}m ${(millis % 60000) ~/ 1000}s'; +} + +/// The input keys that carry a human-facing command or description, in order. +const chatToolDetailKeys = [ + 'description', + 'command', + 'filePath', + 'file_path', + 'path', + 'pattern', + 'query', + 'url', +]; + +const int chatToolDetailMaxLength = 72; + +/// A one-line command/description from the call's input, elided past 72 +/// characters; null when the input names none. +String? chatToolDetail(JsonMap part) { + final input = _state(part)['input']; + if (input is! Map) return null; + for (final key in chatToolDetailKeys) { + final value = input[key]; + if (value is! String) continue; + final text = value.trim().replaceAll(RegExp(r'\s+'), ' '); + if (text.isEmpty) continue; + return text.length <= chatToolDetailMaxLength + ? text + : '${text.substring(0, chatToolDetailMaxLength - 1).trimRight()}…'; + } + return null; +} + +/// INPUT: pretty JSON for an object, the raw string while it streams. +String chatToolInputText(JsonMap part) { + final input = _state(part)['input']; + if (input == null) return ''; + if (input is String) return input; + if (input is Map || input is List) { + return const JsonEncoder.withIndent(' ').convert(input); + } + return '$input'; +} + +/// OUTPUT: the text content; `(no output)` once settled with none. +String chatToolOutputText(JsonMap part) { + final state = _state(part); + final content = state['content']; + final text = content is List + ? content + .whereType() + .where((item) => item['type'] == 'text') + .map((item) => '${item['text'] ?? ''}') + .where((value) => value.trim().isNotEmpty) + .join('\n') + : ''; + if (text.trim().isNotEmpty) return text; + if (_runningStatus(state['status'])) return ''; + return '(no output)'; +} + +/// ERROR: the server's message, else the output text. +String chatToolErrorText(JsonMap part) { + final error = _state(part)['error']; + final message = error is Map ? '${error['message'] ?? ''}' : ''; + return message.trim().isNotEmpty ? message : chatToolOutputText(part); +} diff --git a/app/lib/features/chat/ui/transcript/chat_message_view.dart b/app/lib/features/chat/ui/transcript/chat_message_view.dart index a6114062..e7f71a30 100644 --- a/app/lib/features/chat/ui/transcript/chat_message_view.dart +++ b/app/lib/features/chat/ui/transcript/chat_message_view.dart @@ -5,6 +5,8 @@ import 'package:flutter/material.dart'; import '../../../../core/api_gateway.dart'; import '../sessions/chat_adaptive.dart'; +import '../../domain/chat_tool_calls.dart'; +import 'chat_tool_calls_view.dart'; import 'native_disclosure.dart'; /// Builds an assistant text part; the default keeps the server's text exactly. @@ -77,7 +79,7 @@ class ChatMessageView extends StatelessWidget { ? (constraints.maxWidth * 0.85).clamp(0, 560).toDouble() : 560, ), - child: bubble, + child: user ? bubble : _agentTurn(context, content, bubble), ), ), ), @@ -186,6 +188,38 @@ class ChatMessageView extends StatelessWidget { return widgets; } + /// The agent turn as the web lays it out: its reasoning as a quiet line + /// above the answer, the answer in the bubble, and one tool chip below. + Widget _agentTurn(BuildContext context, List content, Widget bubble) { + final parts = _partsOf(row); + final reasoning = [ + for (final part in parts) + if (part['type'] == 'reasoning' && + '${part['text'] ?? ''}'.trim().isNotEmpty) + '${part['text']}', + ]; + final tools = [ + for (final part in parts) + if (isChatToolPart(part)) part, + ]; + return Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + if (reasoning.isNotEmpty) + NativeDisclosure( + key: const Key('chat-reasoning'), + label: reasoning.length == 1 + ? 'Reasoning' + : 'Reasoning · ${reasoning.length} steps', + childBuilder: (context) => SelectableText(reasoning.join('\n\n')), + ), + if (content.isNotEmpty) bubble, + if (tools.isNotEmpty) ChatToolCallsChip(calls: tools), + ], + ); + } + List _agentParts(BuildContext context) { final widgets = []; final parts = _partsOf(row); @@ -227,19 +261,8 @@ class ChatMessageView extends StatelessWidget { if (!hasText) return type.isEmpty ? _unrecognised(part) : null; return textBuilder?.call(context, text) ?? SelectableText(text); } - if (type == 'reasoning') { - if (!hasText) return null; - return NativeDisclosure( - label: 'Reasoning', - childBuilder: (context) => SelectableText(text), - ); - } - if (type == 'tool') { - return NativeDisclosure( - label: _toolLabel(part), - childBuilder: (context) => _raw(part), - ); - } + // Reasoning and tool calls are drawn outside the bubble (_agentTurn). + if (type == 'reasoning' || type == 'tool') return null; if (type == 'file') { if (_isFile(part)) return _attachment(context, part); return hasText ? SelectableText(text) : null; @@ -300,11 +323,6 @@ bool _isFile(JsonMap part) => part['type'] == 'file' && '${part['name'] ?? ''}${part['uri'] ?? ''}'.trim().isNotEmpty; -String _toolLabel(JsonMap part) { - final name = '${part['name'] ?? ''}'.trim(); - return name.isEmpty ? 'tool' : name; -} - /// The web's attachment name rule: its own name, else the URI basename, else /// `file`, so an attachment never shows an empty label. String _attachmentDisplayName(Object? name, Object? uri) { diff --git a/app/lib/features/chat/ui/transcript/chat_tool_calls_view.dart b/app/lib/features/chat/ui/transcript/chat_tool_calls_view.dart new file mode 100644 index 00000000..378d9734 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/chat_tool_calls_view.dart @@ -0,0 +1,211 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../../../core/api_gateway.dart'; +import '../../../../ui/text/monospace.dart'; +import '../../domain/chat_tool_calls.dart'; +import '../sessions/chat_adaptive.dart'; +import 'native_disclosure.dart'; + +/// A turn's one tool summary below its answer, as the web's chip: `N tools`, +/// spinning while a call runs and tinted when one failed. It opens a sheet with +/// one row per call. +class ChatToolCallsChip extends StatelessWidget { + const ChatToolCallsChip({super.key, required this.calls}); + + final List calls; + + @override + Widget build(BuildContext context) { + final cupertino = + chatControlsStyleOf(context) == ChatControlsStyle.cupertino; + final running = calls.any(chatToolRunning); + final failed = calls.any(chatToolFailed); + final color = failed + ? (cupertino + ? CupertinoColors.systemRed.resolveFrom(context) + : Theme.of(context).colorScheme.error) + : (cupertino + ? CupertinoColors.secondaryLabel.resolveFrom(context) + : Theme.of(context).colorScheme.onSurfaceVariant); + final label = chatToolCallsLabel(calls.length); + final leading = running + ? (cupertino + ? const CupertinoActivityIndicator(radius: 7) + : const SizedBox.square( + dimension: 12, + child: CircularProgressIndicator(strokeWidth: 2), + )) + : Icon( + cupertino ? CupertinoIcons.wrench : Icons.build_outlined, + size: 14, + color: color, + ); + final content = Row( + mainAxisSize: MainAxisSize.min, + children: [ + leading, + const SizedBox(width: 6), + Text(label, style: TextStyle(color: color, fontSize: 13)), + Icon( + cupertino ? CupertinoIcons.chevron_up : Icons.expand_less, + size: 14, + color: color, + ), + ], + ); + return Semantics( + button: true, + // The child's own action is excluded with its label, so the node + // carries the activation for assistive technology. + onTap: () => _open(context, cupertino), + label: calls.length == 1 + ? '1 tool call, expand for details' + : '${calls.length} tool calls, expand for details', + excludeSemantics: true, + child: Padding( + padding: const EdgeInsets.only(top: 2), + child: cupertino + ? CupertinoButton( + key: const Key('chat-tool-chip'), + padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 4), + minimumSize: Size.zero, + onPressed: () => _open(context, cupertino), + child: content, + ) + : InkWell( + key: const Key('chat-tool-chip'), + borderRadius: BorderRadius.circular(8), + onTap: () => _open(context, cupertino), + child: Padding( + padding: const EdgeInsets.symmetric( + horizontal: 4, + vertical: 6, + ), + child: content, + ), + ), + ), + ); + } + + Future _open(BuildContext context, bool cupertino) { + final sheet = _ToolCallsSheet(calls: calls); + if (cupertino) { + return showCupertinoModalPopup( + context: context, + builder: (_) => CupertinoPopupSurface(child: SafeArea(child: sheet)), + ); + } + return showModalBottomSheet( + context: context, + isScrollControlled: true, + showDragHandle: true, + useSafeArea: true, + builder: (_) => sheet, + ); + } +} + +class _ToolCallsSheet extends StatelessWidget { + const _ToolCallsSheet({required this.calls}); + + final List calls; + + @override + Widget build(BuildContext context) { + final cupertino = + chatControlsStyleOf(context) == ChatControlsStyle.cupertino; + final style = cupertino + ? CupertinoTheme.of(context).textTheme.textStyle + : Theme.of(context).textTheme.bodyMedium; + return DefaultTextStyle.merge( + style: style, + child: ConstrainedBox( + constraints: BoxConstraints( + maxHeight: MediaQuery.sizeOf(context).height * 0.8, + ), + child: ListView( + key: const Key('chat-tool-sheet'), + shrinkWrap: true, + padding: const EdgeInsets.fromLTRB(16, 8, 16, 16), + children: [ + Padding( + padding: const EdgeInsets.only(bottom: 8), + child: Text( + chatToolCallsLabel(calls.length), + style: const TextStyle(fontWeight: FontWeight.w600), + ), + ), + for (final call in calls) _ToolCallRow(call: call), + ], + ), + ), + ); + } +} + +/// One call: status glyph, name, its command/description and duration; it +/// opens to the formatted INPUT and OUTPUT (or ERROR). +class _ToolCallRow extends StatelessWidget { + const _ToolCallRow({required this.call}); + + final JsonMap call; + + @override + Widget build(BuildContext context) { + final cupertino = + chatControlsStyleOf(context) == ChatControlsStyle.cupertino; + final detail = chatToolDetail(call); + final duration = chatToolDurationMillis(call); + final failed = chatToolFailed(call); + final running = chatToolRunning(call); + final title = [ + chatToolLabel(call), + ?detail, + if (duration != null) formatChatToolDuration(duration), + if (failed) 'failed', + if (running) 'running', + ].join(' · '); + return NativeDisclosure( + label: title, + childBuilder: (context) { + final mono = const TextStyle( + fontFamily: appMonospaceFamily, + fontSize: 12, + ); + final input = chatToolInputText(call); + final output = failed + ? chatToolErrorText(call) + : chatToolOutputText(call); + Widget section(String heading, String body) => Padding( + padding: const EdgeInsets.only(bottom: 8), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + heading, + style: TextStyle( + fontSize: 11, + fontWeight: FontWeight.w600, + color: cupertino + ? CupertinoColors.secondaryLabel.resolveFrom(context) + : Theme.of(context).colorScheme.onSurfaceVariant, + ), + ), + const SizedBox(height: 2), + SelectableText(body, style: mono), + ], + ), + ); + return Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + if (input.isNotEmpty) section('INPUT', input), + if (output.isNotEmpty) section(failed ? 'ERROR' : 'OUTPUT', output), + ], + ); + }, + ); + } +} diff --git a/app/test/application/chat_registration_test.dart b/app/test/application/chat_registration_test.dart index 440979ef..81afc495 100644 --- a/app/test/application/chat_registration_test.dart +++ b/app/test/application/chat_registration_test.dart @@ -202,7 +202,7 @@ void main() { }); expect(find.text('The stored reply stays readable.'), findsOneWidget); expect(find.text('Reasoning'), findsOneWidget); - expect(find.text('shell'), findsOneWidget); + expect(find.text('1 tool'), findsOneWidget); expect(find.text('Kept behind a native disclosure.'), findsNothing); // One shell bar only: the embedded list/thread adds no second one. diff --git a/app/test/features/chat/conversation/chat_thread_view_test.dart b/app/test/features/chat/conversation/chat_thread_view_test.dart index 9f7ce183..255a9428 100644 --- a/app/test/features/chat/conversation/chat_thread_view_test.dart +++ b/app/test/features/chat/conversation/chat_thread_view_test.dart @@ -283,22 +283,28 @@ void main() { ); await tester.pumpAndSettle(); - // Before paging the turn holds two messages, so two disclosures. - expect(find.text('Reasoning'), findsNWidgets(2)); - await tester.tap(find.text('Reasoning').first); + // The turn's reasoning is one quiet block above its answer, as on + // the web; it counts the steps it holds. + expect(find.text('Reasoning · 2 steps'), findsOneWidget); + await tester.tap(find.text('Reasoning · 2 steps')); await tester.pumpAndSettle(); - expect(find.text('NEWEST folded reasoning body.'), findsOneWidget); + expect( + find.textContaining('NEWEST folded reasoning body.'), + findsOneWidget, + ); await tester.tap(find.text('Load older')); await tester.pumpAndSettle(); - // All four assistant messages now fold into the same turn. The - // newest block stays open; no older block may open. - expect(find.text('Reasoning'), findsNWidgets(4)); - expect(find.text('NEWEST folded reasoning body.'), findsOneWidget); - expect(find.text('FOURTH folded reasoning body.'), findsNothing); - expect(find.text('SECOND folded reasoning body.'), findsNothing); - expect(find.text('OLDEST folded reasoning body.'), findsNothing); + // All four assistant messages now fold into the same turn, and the + // open block stays open while it grows. + expect(find.text('Reasoning · 4 steps'), findsOneWidget); + for (final body in [ + 'NEWEST folded reasoning body.', + 'OLDEST folded reasoning body.', + ]) { + expect(find.textContaining(body), findsOneWidget, reason: body); + } expect( find.text('Load older'), keepsControl ? findsOneWidget : findsNothing, diff --git a/app/test/features/chat/domain/chat_tool_calls_test.dart b/app/test/features/chat/domain/chat_tool_calls_test.dart new file mode 100644 index 00000000..c003418e --- /dev/null +++ b/app/test/features/chat/domain/chat_tool_calls_test.dart @@ -0,0 +1,90 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/domain/chat_tool_calls.dart'; + +void main() { + final shell = { + 'type': 'tool', + 'name': 'shell', + 'state': { + 'status': 'completed', + 'input': {'command': 'ls -la\n /srv'}, + 'content': [ + {'type': 'text', 'text': 'a.txt'}, + ], + }, + 'time': {'ran': 1000, 'completed': 2200}, + }; + + test('labels and counts follow the web', () { + expect(chatToolLabel(shell), 'shell'); + expect(chatToolLabel({'type': 'tool'}), 'tool'); + expect(chatToolCallsLabel(1), '1 tool'); + expect(chatToolCallsLabel(3), '3 tools'); + }); + + test('the detail is the first command-like input, one line, elided', () { + expect(chatToolDetail(shell), 'ls -la /srv'); + final long = { + 'type': 'tool', + 'state': { + 'input': {'description': 'x' * 100}, + }, + }; + expect(chatToolDetail(long)!.length, 72); + expect(chatToolDetail(long), endsWith('…')); + expect( + chatToolDetail({ + 'type': 'tool', + 'state': {'input': '{"par'}, + }), + isNull, + ); + }); + + test('duration, running and failure states', () { + expect(chatToolDurationMillis(shell), 1200); + expect(formatChatToolDuration(340), '340ms'); + expect(formatChatToolDuration(1200), '1.2s'); + expect(formatChatToolDuration(123000), '2m 3s'); + expect(chatToolRunning(shell), isFalse); + expect( + chatToolRunning({ + 'type': 'tool', + 'state': {'status': 'running'}, + }), + isTrue, + ); + expect( + chatToolFailed({ + 'state': {'status': 'error'}, + }), + isTrue, + ); + }); + + test('input is pretty JSON and output is the text content', () { + expect(chatToolInputText(shell), '{\n "command": "ls -la\\n /srv"\n}'); + expect(chatToolOutputText(shell), 'a.txt'); + expect( + chatToolOutputText({ + 'state': {'status': 'completed'}, + }), + '(no output)', + ); + expect( + chatToolOutputText({ + 'state': {'status': 'running'}, + }), + '', + ); + expect( + chatToolErrorText({ + 'state': { + 'status': 'error', + 'error': {'message': 'denied'}, + }, + }), + 'denied', + ); + }); +} diff --git a/app/test/features/chat/ui/transcript/chat_message_view_test.dart b/app/test/features/chat/ui/transcript/chat_message_view_test.dart index a74c2165..6c41ec29 100644 --- a/app/test/features/chat/ui/transcript/chat_message_view_test.dart +++ b/app/test/features/chat/ui/transcript/chat_message_view_test.dart @@ -124,14 +124,16 @@ void main() { expect(find.textContaining('"payload": 7'), findsNothing); expect(find.text('plain future text'), findsOneWidget); + // As the web: reasoning is a quiet line above the answer, the answer + // keeps its own order in the bubble, and the tool calls are one chip below. double top(String text) => tester.getTopLeft(find.text(text)).dy; - expect(top('First answer'), lessThan(top('Reasoning'))); - expect(top('Reasoning'), lessThan(top('shell'))); - expect(top('shell'), lessThan(top('Second answer'))); + expect(top('Reasoning'), lessThan(top('First answer'))); + expect(top('First answer'), lessThan(top('Second answer'))); expect( top('Second answer'), lessThan(top('Unrecognised part: future_part')), ); + expect(top('Unrecognised part: future_part'), lessThan(top('1 tool'))); await tester.ensureVisible(find.text('Reasoning')); await tester.pumpAndSettle(); @@ -139,11 +141,16 @@ void main() { await tester.pumpAndSettle(); expect(find.text('private reasoning'), findsOneWidget); - await tester.ensureVisible(find.text('shell')); + // The chip opens the calls; a call opens to its formatted input. + await tester.ensureVisible(find.text('1 tool')); + await tester.tap(find.text('1 tool')); await tester.pumpAndSettle(); - await tester.tap(find.text('shell')); + await tester.tap(find.text('shell · ls')); await tester.pumpAndSettle(); + expect(find.text('INPUT'), findsOneWidget); expect(find.textContaining('"command": "ls"'), findsOneWidget); + await tester.tapAt(const Offset(10, 10)); + await tester.pumpAndSettle(); await tester.ensureVisible(find.text('Unrecognised part: future_part')); await tester.pumpAndSettle(); @@ -247,9 +254,12 @@ void main() { await tester.pumpAndSettle(); expect(find.text('thought'), findsOneWidget); - await tester.tap(find.text('read')); + await tester.tap(find.text('1 tool')); await tester.pumpAndSettle(); - expect(find.textContaining('"status": "error"'), findsOneWidget); + await tester.tap(find.text('read · failed')); + await tester.pumpAndSettle(); + expect(find.text('ERROR'), findsOneWidget); + expect(find.text('(no output)'), findsOneWidget); }); testWidgets('Cupertino disclosure announces its expanded state', ( diff --git a/app/tool/chat_message_probe.mjs b/app/tool/chat_message_probe.mjs index ccc77d2f..ad1eaec8 100644 --- a/app/tool/chat_message_probe.mjs +++ b/app/tool/chat_message_probe.mjs @@ -191,6 +191,17 @@ try { } return node; }; + // A real pointer tap at the node's centre, for controls inside a modal sheet. + const tap = async (name) => { + const node = await findNode(name); + if (!node) throw new Error(`Could not tap ${JSON.stringify(name)}`); + const x = node.x + node.w / 2; + const y = node.y + node.h / 2; + for (const type of ['mousePressed', 'mouseReleased']) { + await send('Input.dispatchMouseEvent', { type, x, y, button: 'left', clickCount: 1 }, session); + } + await delay(450); + }; const click = async (name) => { const clicked = await evaluate(`(() => { const wanted = ${JSON.stringify(name)}; @@ -302,7 +313,7 @@ try { 'User message', 'Assistant message', 'Reasoning', - 'shell', + '1 tool call, expand for details', 'Unrecognised part: future_part', 'Ada phone', ]) { @@ -352,21 +363,24 @@ try { `Cupertino reasoning did not announce aria-expanded=true: ${JSON.stringify(openReasoningNode)}`, ); } - await assertBodyEdge(openReasoning, 'Reasoning', 'shell'); - - await click('shell'); - const openTool = await capture(`${style}-tool-expanded`); - for (const text of ['command', '-la', 'completed']) { - await assertOcr(openTool, text); - } - await assertBodyEdge(openTool, 'shell', 'Second'); - await click('Unrecognised part: future_part'); const openUnknown = await capture(`${style}-unknown-expanded`); for (const text of ['future_part', 'payload', 'kept', 'count']) { await assertOcr(openUnknown, text); } - await assertBodyEdge(openUnknown, 'Unrecognised', null); + + // As the web: the turn's tool calls are one chip below the answer that + // opens a sheet; a call there opens to its formatted input. + await click('1 tool call, expand for details'); + await waitFor(async () => Boolean(await findNode('shell · ls -la')), 'tool sheet'); + // Let the sheet finish its entrance before the row is activated. + await delay(800); + await tap('shell · ls -la'); + await delay(600); + const openTool = await capture(`${style}-tool-expanded`); + for (const text of ['INPUT', 'command', '-la']) { + await assertOcr(openTool, text); + } const report = JSON.stringify(evidence, null, 2); await writeFile(join(out, `${style}-evidence.json`), report);