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']);
|
||||
|
||||
Reference in new issue
Block a user