feat: 支持 agent 运行取消功能

This commit is contained in:
qzl
2026-03-25 18:33:25 +08:00
parent 599c597e69
commit 96fc4a1e77
21 changed files with 778 additions and 85 deletions
@@ -123,10 +123,16 @@ class ChatBloc extends Cubit<ChatState> {
);
case AgUiEventType.runError:
final errorEvent = event as RunErrorEvent;
final isCanceledByUser = errorEvent.code == 'RUN_CANCELED';
emit(
_resetRunState(
error: errorEvent.message,
).copyWith(items: _markActiveToolCallsFailed(state.items)),
error: isCanceledByUser ? null : errorEvent.message,
).copyWith(
items: _markActiveToolCallsFailed(
state.items,
reason: isCanceledByUser ? '本次运行已取消' : '本次运行已失败',
),
),
);
case AgUiEventType.stepStarted:
_handleStepStarted(event as StepStartedEvent);
@@ -286,7 +292,10 @@ class ChatBloc extends Cubit<ChatState> {
return items.where((item) => item is! ToolCallItem).toList();
}
List<ChatListItem> _markActiveToolCallsFailed(List<ChatListItem> items) {
List<ChatListItem> _markActiveToolCallsFailed(
List<ChatListItem> items, {
required String reason,
}) {
return items.map((item) {
if (item is! ToolCallItem) {
return item;
@@ -297,10 +306,7 @@ class ChatBloc extends Cubit<ChatState> {
if (item.status == ToolCallStatus.completed) {
return item;
}
return item.copyWith(
status: ToolCallStatus.error,
errorMessage: '本次运行已失败',
);
return item.copyWith(status: ToolCallStatus.error, errorMessage: reason);
}).toList();
}