feat: tool steps as foldable runs with icon, label and summary
The old rows ("已思考 ▸", "✓ shell ▸") said nothing about what a step did
and could only be expanded one at a time.
- A turn is one or more assistant messages (the server writes a message
per step), so grouping per message produced a header for every step.
Rows are now grouped per turn: one "已完成 · N 次工具调用" header, split
where the reply has text. Header shows running/complete/failed, and the
whole run folds away.
- Every step shows a rounded icon for its kind (thought, shell, read,
write, edit, patch, search, fetch, task, skill), its label and a
one-line summary: the command, the file path, the pattern, the URL, the
skill id, and so on.
- A step opens to its reasoning text or the tool's input and output;
image references stay listed and clickable.
- A lone thought needs no header; a reasoning-only run reads "思考过程".
- Version 0.3.3 (code 303).
This commit is contained in:
1 parent
61977d2b14
commit
2baa0a2730
4 files changed
+424
-106
No files matched your search
+402
-99
@@ -34,6 +34,10 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
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<String> _collapsedSteps = {};
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -173,19 +177,15 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
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<ChatScreen> {
|
||||
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<ChatScreen> {
|
||||
);
|
||||
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<ChatScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
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 <String, dynamic>{};
|
||||
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, dynamic>{};
|
||||
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 = <String>[];
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user