feat: fold subagent trees and open them from Agent Task rows
Subagent sessions now render as a collapsed tree in the sidebar: parents with workers get an expand/collapse chevron, expanded child rows sit behind a guide line, and the parent keeps its worker count. The parent of the open subagent expands once so its child is visible. Subagent tool rows in the parent chat (subagent or task) are labeled 子代理, show the description and open the child session on tap; a child outside the loaded page is fetched by id first. Subagent chats are read-only by default with the first-party notice; allowPromptingSubagentSessions permits ordinary subagents, while a Lead's operator-child worker stays read-only until takeover. 0.4.10 (410).
This commit is contained in:
1 parent
cebf09048e
commit
9cbd97d532
10 files changed
+271
-23
No files matched your search
+104
-11
@@ -135,6 +135,44 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// A subagent is view-only unless the server allows prompting subagents;
|
||||
/// a lead's operator worker stays view-only until it is taken over.
|
||||
bool get _promptReadOnly =>
|
||||
widget.app.promptReadOnlyFor(widget.session);
|
||||
|
||||
Widget _readOnlyBanner(ThemeData theme) {
|
||||
final scheme = theme.colorScheme;
|
||||
final openchamber = widget.session.metadata['openchamber'];
|
||||
final operator =
|
||||
openchamber is Map && openchamber['kind'] == 'operator-child';
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
padding: EdgeInsets.fromLTRB(
|
||||
16, 12, 16, 12 + widget.keyboardInset),
|
||||
decoration: BoxDecoration(
|
||||
color: scheme.surfaceContainerHighest,
|
||||
border: Border(
|
||||
top: BorderSide(color: theme.dividerColor, width: 0.5),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.lock_outline, size: 16, color: theme.hintColor),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
operator
|
||||
? '此会话由 Lead 管理,接管后才能继续。'
|
||||
: '无法向子智能体会话发送提示。',
|
||||
style: TextStyle(
|
||||
fontSize: 12.5, color: scheme.onSurfaceVariant),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// The session that started this one, when it is a subagent.
|
||||
Session? get _parentSession {
|
||||
final parentID = widget.session.parentID;
|
||||
@@ -216,7 +254,10 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
_permissions(),
|
||||
_forms(),
|
||||
if (chat.running) _activityStrip(),
|
||||
_composer(widget.keyboardInset),
|
||||
if (_promptReadOnly)
|
||||
_readOnlyBanner(theme)
|
||||
else
|
||||
_composer(widget.keyboardInset),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -812,6 +853,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
final refs = reasoning || image != null
|
||||
? const <_ImageRef>[]
|
||||
: toolImages(p, chat.dir);
|
||||
final subagentId = _subagentSessionId(p);
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(left: 22),
|
||||
decoration: BoxDecoration(
|
||||
@@ -821,14 +863,19 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
InkWell(
|
||||
onTap: image != null
|
||||
? () => _openPreview(_ImageRef(image, summary.split('/').last))
|
||||
: () {
|
||||
setState(() {
|
||||
open ? chat.expanded.remove(key) : chat.expanded.add(key);
|
||||
});
|
||||
if (!open && !reasoning) chat.ensureToolLoaded(id);
|
||||
},
|
||||
onTap: subagentId != null
|
||||
? () => _openSubagent(subagentId)
|
||||
: image != null
|
||||
? () =>
|
||||
_openPreview(_ImageRef(image, summary.split('/').last))
|
||||
: () {
|
||||
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(
|
||||
@@ -874,7 +921,10 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
: theme.hintColor)),
|
||||
),
|
||||
],
|
||||
if (image != null)
|
||||
if (subagentId != null)
|
||||
Icon(Icons.chevron_right,
|
||||
size: 18, color: theme.hintColor)
|
||||
else if (image != null)
|
||||
Icon(Icons.open_in_full,
|
||||
size: 14, color: theme.colorScheme.primary)
|
||||
else
|
||||
@@ -919,6 +969,45 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
'png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'avif',
|
||||
};
|
||||
|
||||
/// The child session a subagent tool call started: the server puts it in the
|
||||
/// tool metadata, and older results carry `sessionID: ses_…` in the text.
|
||||
static String? _subagentSessionId(ContentPart p) {
|
||||
if (p.type != 'tool') return null;
|
||||
if (p.name != 'subagent' && p.name != 'task') return null;
|
||||
final meta = p.state?.metadata;
|
||||
if (meta != null) {
|
||||
for (final key in const [
|
||||
'sessionID',
|
||||
'sessionId',
|
||||
'childSessionID',
|
||||
'childSessionId',
|
||||
]) {
|
||||
final value = meta[key];
|
||||
if (value is String && value.isNotEmpty) return value;
|
||||
}
|
||||
}
|
||||
final pattern =
|
||||
RegExp(r'(?:sessionID|sessionId)["\s:=]+(ses_[A-Za-z0-9]+)');
|
||||
final texts = <String>[
|
||||
for (final part in p.state?.content ?? const <ContentPart>[]) part.text,
|
||||
if (p.state?.error != null) p.state!.error!,
|
||||
];
|
||||
for (final text in texts) {
|
||||
final match = pattern.firstMatch(text);
|
||||
if (match != null) return match.group(1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<void> _openSubagent(String id) async {
|
||||
final ok = await widget.app.openSubagent(id);
|
||||
if (ok || !mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(widget.app.lastError ?? '无法打开子代理会话')),
|
||||
);
|
||||
widget.app.clearLastError();
|
||||
}
|
||||
|
||||
/// The file a read/write/edit step points at, when it is an image.
|
||||
String? _imageArg(ContentPart p) {
|
||||
if (p.type != 'tool') return null;
|
||||
@@ -954,6 +1043,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
case 'web_fetch':
|
||||
return 'url';
|
||||
case 'task':
|
||||
case 'subagent':
|
||||
return 'description';
|
||||
case 'skill':
|
||||
return 'id';
|
||||
@@ -1064,6 +1154,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
case 'todowrite':
|
||||
return Icons.checklist_rtl;
|
||||
case 'task':
|
||||
case 'subagent':
|
||||
return Icons.smart_toy_outlined;
|
||||
case 'skill':
|
||||
return Icons.extension_outlined;
|
||||
@@ -1100,7 +1191,8 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
case 'todowrite':
|
||||
return '任务';
|
||||
case 'task':
|
||||
return '子任务';
|
||||
case 'subagent':
|
||||
return '子代理';
|
||||
case 'skill':
|
||||
return '技能';
|
||||
default:
|
||||
@@ -1131,6 +1223,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
case 'web_fetch':
|
||||
return one(input['url']);
|
||||
case 'task':
|
||||
case 'subagent':
|
||||
return one(input['description'] ?? input['prompt']);
|
||||
case 'skill':
|
||||
return one(input['id'] ?? input['name']);
|
||||
|
||||
@@ -32,6 +32,11 @@ class _SessionListPaneState extends State<SessionListPane> {
|
||||
final Set<String> _collapsed = {};
|
||||
bool _showArchived = false;
|
||||
|
||||
/// Parents whose subagents are expanded. Collapsed is the default; the
|
||||
/// parent of the open child expands once so its child is visible.
|
||||
final Set<String> _expandedChildren = {};
|
||||
String? _activeChildParent;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -63,6 +68,7 @@ class _SessionListPaneState extends State<SessionListPane> {
|
||||
return ListenableBuilder(
|
||||
listenable: _app,
|
||||
builder: (context, _) {
|
||||
_syncActiveParent();
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
return SafeArea(
|
||||
child: Column(
|
||||
@@ -499,11 +505,33 @@ class _SessionListPaneState extends State<SessionListPane> {
|
||||
]..sort((a, b) => a.created.compareTo(b.created));
|
||||
}
|
||||
|
||||
List<Widget> _rowsWithChildren(Session session, {bool archived = false}) => [
|
||||
_row(session, archived: archived),
|
||||
for (final child in _childrenOf(session, archived: archived))
|
||||
_row(child, archived: archived, child: true),
|
||||
];
|
||||
List<Widget> _rowsWithChildren(Session session, {bool archived = false}) {
|
||||
final rows = <Widget>[_row(session, archived: archived)];
|
||||
if (!_expandedChildren.contains(session.id)) return rows;
|
||||
final theme = Theme.of(context);
|
||||
for (final child in _childrenOf(session, archived: archived)) {
|
||||
rows.add(Container(
|
||||
margin: const EdgeInsets.only(left: 24),
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
left: BorderSide(color: theme.dividerColor, width: 1),
|
||||
),
|
||||
),
|
||||
child: _row(child, archived: archived, child: true),
|
||||
));
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
/// Expands the parent of the open subagent once, so the sidebar shows where
|
||||
/// the child is. Collapsing it by hand stays collapsed until another child
|
||||
/// is opened.
|
||||
void _syncActiveParent() {
|
||||
final parent = _app.chat?.session.parentID;
|
||||
if (parent == _activeChildParent) return;
|
||||
_activeChildParent = parent;
|
||||
if (parent != null) _expandedChildren.add(parent);
|
||||
}
|
||||
|
||||
Widget _row(Session session,
|
||||
{bool archived = false, bool child = false}) {
|
||||
@@ -512,6 +540,8 @@ class _SessionListPaneState extends State<SessionListPane> {
|
||||
final running = _isActive(session);
|
||||
final selected =
|
||||
!_app.draftOpen && _app.chat?.session.id == session.id;
|
||||
final children = child ? const <Session>[] : _childrenOf(session, archived: archived);
|
||||
final expanded = _expandedChildren.contains(session.id);
|
||||
return Slidable(
|
||||
key: ValueKey('${archived ? 'a' : 's'}-${session.id}'),
|
||||
endActionPane: ActionPane(
|
||||
@@ -551,7 +581,31 @@ class _SessionListPaneState extends State<SessionListPane> {
|
||||
leading: child
|
||||
? Icon(Icons.smart_toy_outlined,
|
||||
size: 16, color: scheme.onSurfaceVariant)
|
||||
: null,
|
||||
: children.isEmpty
|
||||
? null
|
||||
: SizedBox(
|
||||
width: 32,
|
||||
height: 32,
|
||||
child: IconButton(
|
||||
icon: Icon(
|
||||
expanded
|
||||
? Icons.expand_more
|
||||
: Icons.chevron_right,
|
||||
size: 18),
|
||||
tooltip: expanded ? '收起子代理' : '展开子代理',
|
||||
padding: EdgeInsets.zero,
|
||||
visualDensity: VisualDensity.compact,
|
||||
constraints:
|
||||
const BoxConstraints(minWidth: 32, minHeight: 32),
|
||||
onPressed: () => setState(() {
|
||||
if (expanded) {
|
||||
_expandedChildren.remove(session.id);
|
||||
} else {
|
||||
_expandedChildren.add(session.id);
|
||||
}
|
||||
}),
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
session.title.isEmpty ? '(无标题会话)' : session.title,
|
||||
maxLines: 1,
|
||||
|
||||
Reference in new issue
Block a user