diff --git a/PLAN.md b/PLAN.md index c3ff3ed..afb2b94 100644 --- a/PLAN.md +++ b/PLAN.md @@ -27,6 +27,20 @@ --- +## 0.3.3 工具调用展示(分支 `flutter`) + +| 项 | 结果 | +| --- | --- | +| 为什么要改 | 旧版每个 reasoning/tool part 只显示一行「已思考 ▸ / ✓ shell ▸」,既没有概要也不能整组收起,长回合里几乎不可读 | +| 分组依据 | 服务端每个 step 各写一条 assistant 消息,所以按**消息**分组会得到「5 组」;现在按**回合**(连续的 assistant 消息)分组,`text` 分隔出新的组。与 dsh-Go 一致:一组一个「已完成 · N 次工具调用」表头 | +| 表头 | 左侧状态图标(进行中转圈 / 完成对勾 / 失败警告),「已完成 · N 次工具调用」/「执行中 · …」/「有步骤失败 · …」;只有一个思考步骤时不显示表头,直接显示「思考」行;纯思考的组显示「思考过程」 | +| 每行 | 26px 圆角图标 + 名称 + 「· 概要」+ 展开箭头;图标按工具类型(思考=psychology、运行=terminal、读取=description、写入/编辑/补丁、搜索、抓取、任务、技能…),未知工具用工具名 | +| 概要 | bash=命令、read/write/edit=文件路径、grep/glob=模式、webfetch=URL、task=描述、skill=id,其余取 description/path/command/query/url/pattern/name/id,再退化为紧凑 JSON | +| 展开 | 单行展开后显示推理全文或工具的 input/输出(等宽、可选中);图片引用仍以 🖼 行列出,点击预览 | +| 版本 | **0.3.3(code 303)** | + +--- + ## 0.3.2 修复(分支 `flutter`) | 项 | 结果 | diff --git a/README.md b/README.md index ff687b3..ad9e266 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,14 @@ Android first. This branch (`flutter`) is a framework evaluation: the same feature set as the earlier Asuka/Go and MyGo attempts, reimplemented in Flutter to compare mobile UI maturity. -Status: 0.3.2 — servers (passwordless / password login, HTTP/HTTPS and +Status: 0.3.3 — servers (passwordless / password login, HTTP/HTTPS and SOCKS5/socks5h proxy, self-signed certificates and pinning), session list with search and live -events, chat with streaming Markdown and full history paging, tool rows with -image previews, a context usage ring with a detail sheet, model/agent -switching (Build/Plan only, current one checked), copy / fork / revert, -attachments (photos/files), permissions. Android 7.0 (API 24)+, plus a Linux -desktop target used for local testing. +events, chat with streaming Markdown and full history paging, tool steps +grouped per turn (icon, one-line summary, foldable run) with image previews, a +context usage ring with a detail sheet, model/agent switching (Build/Plan only, +current one checked), copy / fork / revert, attachments (photos/files), +permissions. Android 7.0 (API 24)+, plus a Linux desktop target used for local +testing. ## What is here diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index c5a7ec6..1d6d8e5 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -34,6 +34,10 @@ class _ChatScreenState extends State { List<_Attach> _attachments = []; bool _attaching = false; + /// Assistant turns show their reasoning/tool parts as a run of steps; the + /// header of each run folds it away (key: message id and start part). + final Set _collapsedSteps = {}; + @override void initState() { super.initState(); @@ -173,19 +177,15 @@ class _ChatScreenState extends State { out.add(_Row('useractions', m.id, -1)); break; case 'assistant': - if (m.content.isEmpty && m.completed == 0) { - out.add(_Row('thinking', m.id, -1)); - continue; - } - for (var i = 0; i < m.content.length; i++) { - final t = m.content[i].type; - if (t == 'text' || t == 'reasoning' || t == 'tool') { - out.add(_Row(t, m.id, i)); - } - } - if (m.completed > 0 && _turnEndsAt(mi)) { - out.add(_Row('footer', m.id, -1)); + // A turn is one or more assistant messages: the server writes a + // message per step, so the whole turn is shown as one run. + var end = mi; + while (end + 1 < chat.msgs.length && + chat.msgs[end + 1].type == 'assistant') { + end++; } + _turnRows(out, mi, end); + mi = end; break; case 'shell': out.add(_Row('shell', m.id, -1)); @@ -198,6 +198,77 @@ class _ChatScreenState extends State { return out; } + void _turnRows(List<_Row> out, int from, int to) { + final pending = <_Step>[]; + void flush() { + if (pending.isEmpty) return; + final first = pending.first; + // A lone thought needs no foldable header above it. + if (pending.length == 1 && first.part.type == 'reasoning') { + out.add(_Row('step', first.msgId, first.index)); + pending.clear(); + return; + } + out.add(_Row('steps', first.msgId, first.index)); + if (!_groupCollapsed(first.msgId, first.index)) { + for (final step in pending) { + out.add(_Row('step', step.msgId, step.index)); + } + } + pending.clear(); + } + + for (var i = from; i <= to; i++) { + final m = chat.msgs[i]; + if (m.content.isEmpty && m.completed == 0) { + flush(); + out.add(_Row('thinking', m.id, -1)); + continue; + } + for (var p = 0; p < m.content.length; p++) { + final part = m.content[p]; + if (part.type == 'reasoning' || part.type == 'tool') { + pending.add(_Step(m.id, p, part)); + } else if (part.type == 'text') { + flush(); + out.add(_Row('text', m.id, p)); + } + } + } + flush(); + if (chat.msgs[to].completed > 0 && _turnEndsAt(to)) { + out.add(_Row('footer', chat.msgs[to].id, -1)); + } + } + + bool _groupCollapsed(String id, int start) => + _collapsedSteps.contains('$id/$start'); + + /// The steps of the run that begins at [msgId]/[index]: consecutive + /// reasoning/tool parts, continuing into the following assistant messages. + List<_Step> _collectGroup(String msgId, int index) { + final out = <_Step>[]; + var i = chat.msgs.indexWhere((m) => m.id == msgId); + var p = index; + while (i >= 0 && i < chat.msgs.length) { + final m = chat.msgs[i]; + if (p >= m.content.length) { + if (i + 1 < chat.msgs.length && + chat.msgs[i + 1].type == 'assistant') { + i++; + p = 0; + continue; + } + break; + } + final part = m.content[p]; + if (part.type != 'reasoning' && part.type != 'tool') break; + out.add(_Step(m.id, p, part)); + p++; + } + return out; + } + bool _turnEndsAt(int i) { for (var j = i + 1; j < chat.msgs.length; j++) { if (chat.msgs[j].type == 'assistant') return false; @@ -219,10 +290,10 @@ class _ChatScreenState extends State { ); case 'text': return _textRow(r.msgId, r.part); - case 'reasoning': - return _reasoningRow(r.msgId, r.part); - case 'tool': - return _toolRow(r.msgId, r.part); + case 'steps': + return _stepsHeader(r.msgId, r.part); + case 'step': + return _stepRow(r.msgId, r.part); case 'footer': return _footerRow(r.msgId); case 'shell': @@ -341,109 +412,332 @@ class _ChatScreenState extends State { ); } - Widget _reasoningRow(String id, int part) { - final m = chat.byId[id]!; - final key = 'reasoning/$id/$part'; - final open = chat.expanded.contains(key); - final text = part < m.content.length ? m.content[part].text : ''; - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - InkWell( - onTap: () => setState(() { - open ? chat.expanded.remove(key) : chat.expanded.add(key); - }), - child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 2), - child: Text('已思考 ${open ? '▾' : '▸'}', - style: const TextStyle(color: Colors.grey, fontSize: 13)), - ), + /// "已完成 · 3 次工具调用", with a chevron that folds the whole run. + Widget _stepsHeader(String id, int start) { + final theme = Theme.of(context); + final steps = _collectGroup(id, start); + final tools = steps.where((s) => s.part.type == 'tool').length; + final collapsed = _groupCollapsed(id, start); + final failed = steps.any((s) => s.part.state?.status == 'error'); + final running = steps.any((s) { + final m = chat.byId[s.msgId]; + final st = s.part.state?.status ?? ''; + return (m != null && m.completed == 0) || + st == 'running' || + st == 'streaming' || + st == 'pending'; + }); + final label = tools == 0 + ? (running ? '思考中…' : '思考过程') + : failed + ? '有步骤失败 · $tools 次工具调用' + : running + ? '执行中 · $tools 次工具调用' + : '已完成 · $tools 次工具调用'; + return GestureDetector( + behavior: HitTestBehavior.opaque, + onTap: () => setState(() { + final key = '$id/$start'; + collapsed ? _collapsedSteps.remove(key) : _collapsedSteps.add(key); + }), + child: Padding( + padding: const EdgeInsets.fromLTRB(14, 10, 12, 2), + child: Row( + children: [ + if (running) + const SizedBox( + width: 15, + height: 15, + child: CircularProgressIndicator(strokeWidth: 1.6)) + else + Icon(failed ? Icons.error_outline : Icons.check_circle_outline, + size: 16, + color: failed ? theme.colorScheme.error : theme.hintColor), + const SizedBox(width: 8), + Expanded( + child: Text(label, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: TextStyle(fontSize: 13, color: theme.hintColor)), + ), + Icon(collapsed ? Icons.expand_more : Icons.expand_less, + size: 18, color: theme.hintColor), + ], ), - if (open && text.isNotEmpty) - Padding( - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 2), - child: Text(text, - style: const TextStyle(color: Colors.grey, fontSize: 13, height: 1.4)), - ), - ], + ), ); } - Widget _toolRow(String id, int part) { + /// One step: a rounded icon, a label, the one-line summary, and details + /// when opened. + Widget _stepRow(String id, int part) { final m = chat.byId[id]!; if (part < 0 || part >= m.content.length) return const SizedBox.shrink(); final p = m.content[part]; - final key = 'tool/$id/$part'; - final open = chat.expanded.contains(key); + final theme = Theme.of(context); + final reasoning = p.type == 'reasoning'; final status = p.state?.status ?? ''; - final icon = status == 'completed' ? '✓' - : status == 'error' ? '✕' - : status == 'running' || status == 'streaming' ? '◌' : '●'; - final refs = toolImages(p, chat.dir); - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - InkWell( - onTap: () { - setState(() { - open ? chat.expanded.remove(key) : chat.expanded.add(key); - }); - if (!open) chat.ensureToolLoaded(id); - }, - child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 2), - child: Text('$icon ${p.name ?? ''} ${open ? '▾' : '▸'}', - maxLines: 1, - overflow: TextOverflow.ellipsis, - style: const TextStyle(color: Colors.grey, fontSize: 13)), - ), - ), - for (final ref in refs) - Padding( - padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 1), - child: InkWell( - onTap: () => _openPreview(ref), + final busy = status == 'running' || status == 'streaming' || status == 'pending'; + final key = 'step/$id/$part'; + final open = chat.expanded.contains(key); + final label = _stepLabel(p); + final summary = _stepSummary(p); + final refs = reasoning ? const <_ImageRef>[] : toolImages(p, chat.dir); + return Container( + margin: const EdgeInsets.only(left: 22), + decoration: BoxDecoration( + border: Border(left: BorderSide(color: theme.dividerColor, width: 1)), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + InkWell( + onTap: () { + setState(() { + open ? chat.expanded.remove(key) : chat.expanded.add(key); + }); + if (!open && !reasoning) chat.ensureToolLoaded(id); + }, + child: Padding( + padding: const EdgeInsets.fromLTRB(10, 3, 10, 3), child: Row( children: [ - const Text('🖼', style: TextStyle(fontSize: 12)), - const SizedBox(width: 6), - Flexible( - child: Text(ref.label, - maxLines: 1, - overflow: TextOverflow.ellipsis, - style: TextStyle(fontSize: 12, color: Theme.of(context).colorScheme.primary)), + Container( + width: 26, + height: 26, + decoration: BoxDecoration( + color: status == 'error' + ? theme.colorScheme.errorContainer + : theme.colorScheme.surfaceContainerHighest, + borderRadius: BorderRadius.circular(7), + ), + child: busy + ? const Padding( + padding: EdgeInsets.all(6), + child: CircularProgressIndicator(strokeWidth: 1.6), + ) + : Icon(_stepIcon(p), + size: 15, + color: status == 'error' + ? theme.colorScheme.onErrorContainer + : theme.colorScheme.onSurfaceVariant), ), + const SizedBox(width: 10), + Text(label, + style: TextStyle( + fontSize: 13.5, color: theme.colorScheme.onSurface)), + if (summary.isEmpty) + const Spacer() + else ...[ + const SizedBox(width: 6), + Text('·', style: TextStyle(fontSize: 13, color: theme.hintColor)), + const SizedBox(width: 6), + Expanded( + child: Text(summary, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: TextStyle(fontSize: 13, color: theme.hintColor)), + ), + ], + Icon(open ? Icons.expand_less : Icons.expand_more, + size: 16, color: theme.hintColor), ], ), ), ), - if (open) - Padding( - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 2), - child: Container( - width: double.infinity, - padding: const EdgeInsets.all(8), - decoration: BoxDecoration( - color: Theme.of(context).colorScheme.surfaceContainerHighest, - borderRadius: BorderRadius.circular(8), - ), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - if (p.state != null && (p.state!.input).isNotEmpty) - Text(const JsonEncoder.withIndent(' ').convert(p.state!.input), - style: const TextStyle(fontSize: 12, color: Colors.grey)), - if (toolOutput(p).isNotEmpty) - Text(toolOutput(p), - style: const TextStyle(fontSize: 12, color: Colors.grey, height: 1.4)), - ], + for (final ref in refs) + Padding( + padding: const EdgeInsets.fromLTRB(10, 0, 10, 2), + child: InkWell( + onTap: () => _openPreview(ref), + child: Row( + children: [ + const Text('🖼', style: TextStyle(fontSize: 12)), + const SizedBox(width: 6), + Flexible( + child: Text(ref.label, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: TextStyle( + fontSize: 12, color: theme.colorScheme.primary)), + ), + ], + ), ), ), - ), - ], + if (open) _stepDetails(p, reasoning), + ], + ), ); } + Widget _stepDetails(ContentPart p, bool reasoning) { + final theme = Theme.of(context); + if (reasoning) { + if (p.text.isEmpty) return const SizedBox.shrink(); + return Padding( + padding: const EdgeInsets.fromLTRB(10, 2, 10, 8), + child: SelectableText(p.text, + style: TextStyle( + fontSize: 12.5, height: 1.45, color: theme.hintColor)), + ); + } + final input = p.state?.input ?? const {}; + final output = toolOutput(p); + return Padding( + padding: const EdgeInsets.fromLTRB(10, 2, 10, 8), + child: Container( + width: double.infinity, + padding: const EdgeInsets.all(8), + decoration: BoxDecoration( + color: theme.colorScheme.surfaceContainerHighest, + borderRadius: BorderRadius.circular(8), + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + if (input.isNotEmpty) + SelectableText(const JsonEncoder.withIndent(' ').convert(input), + style: const TextStyle( + fontSize: 12, + fontFamily: 'monospace', + height: 1.4)), + if (input.isNotEmpty && output.isNotEmpty) + const SizedBox(height: 6), + if (output.isNotEmpty) + SelectableText(output, + style: TextStyle( + fontSize: 12, + height: 1.4, + color: p.state?.error != null + ? theme.colorScheme.error + : theme.colorScheme.onSurfaceVariant)), + ], + ), + ), + ); + } + + static IconData _stepIcon(ContentPart p) { + if (p.type == 'reasoning') return Icons.psychology_outlined; + switch (p.name ?? '') { + case 'bash': + case 'shell': + case 'execute': + return Icons.terminal; + case 'read': + return Icons.description_outlined; + case 'write': + return Icons.note_add_outlined; + case 'edit': + return Icons.edit_outlined; + case 'patch': + case 'apply_patch': + return Icons.difference_outlined; + case 'grep': + return Icons.search; + case 'glob': + case 'find': + return Icons.travel_explore; + case 'list': + return Icons.format_list_bulleted; + case 'webfetch': + case 'web_fetch': + return Icons.language; + case 'todowrite': + return Icons.checklist_rtl; + case 'task': + return Icons.smart_toy_outlined; + case 'skill': + return Icons.extension_outlined; + default: + return Icons.handyman_outlined; + } + } + + static String _stepLabel(ContentPart p) { + if (p.type == 'reasoning') return '思考'; + switch (p.name ?? '') { + case 'bash': + case 'shell': + case 'execute': + return '运行'; + case 'read': + return '读取'; + case 'write': + return '写入'; + case 'edit': + return '编辑'; + case 'patch': + case 'apply_patch': + return '补丁'; + case 'grep': + case 'glob': + case 'find': + return '搜索'; + case 'list': + return '列出'; + case 'webfetch': + case 'web_fetch': + return '抓取'; + case 'todowrite': + return '任务'; + case 'task': + return '子任务'; + case 'skill': + return '技能'; + default: + return p.name == null || p.name!.isEmpty ? '工具' : p.name!; + } + } + + /// One line that says what the step is about, so a folded run stays useful. + static String _stepSummary(ContentPart p) { + if (p.type == 'reasoning') return _firstLine(p.text); + final input = p.state?.input ?? const {}; + String one(Object? v) => + (v ?? '').toString().replaceAll(RegExp(r'\s+'), ' ').trim(); + switch (p.name ?? '') { + case 'bash': + case 'shell': + case 'execute': + return one(input['command']); + case 'read': + case 'write': + case 'edit': + return one(input['filePath'] ?? input['path'] ?? input['file']); + case 'grep': + case 'glob': + case 'find': + return one(input['pattern']); + case 'webfetch': + case 'web_fetch': + return one(input['url']); + case 'task': + return one(input['description'] ?? input['prompt']); + case 'skill': + return one(input['id'] ?? input['name']); + default: + for (final k in const [ + 'description', 'filePath', 'path', 'command', 'query', 'url', + 'pattern', 'name', 'id', + ]) { + final v = one(input[k]); + if (v.isNotEmpty) return v; + } + if (input.isEmpty) return ''; + return one(const JsonEncoder().convert(input)); + } + } + + static String _firstLine(String text) { + for (final line in const LineSplitter().convert(text)) { + final t = line.replaceAll(RegExp(r'^[\s#>*\-]+'), '').trim(); + if (t.isNotEmpty) return t; + } + return ''; + } + Widget _footerRow(String id) { final m = chat.byId[id]!; final parts = []; @@ -1055,6 +1349,15 @@ class _Row { _Row(this.kind, this.msgId, this.part); } +/// One reasoning or tool part, addressed across the assistant messages of a +/// turn. +class _Step { + final String msgId; + final int index; + final ContentPart part; + const _Step(this.msgId, this.index, this.part); +} + class _Attach { final String name; final String mime; diff --git a/pubspec.yaml b/pubspec.yaml index 032440c..7914a68 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: openchamber description: "A lightweight OpenChamber client for phones." publish_to: 'none' -version: 0.3.2+302 +version: 0.3.3+303 environment: sdk: ^3.14.0