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
@@ -50,6 +50,8 @@ class AgUiService {
Completer<void>? _activeSseDoneCompleter;
String? _threadId;
String? _activeThreadIdForRun;
String? _activeRunId;
bool _hasMoreHistory = false;
AgUiService({EventCallback? onEvent, required IApiClient apiClient})
@@ -83,11 +85,20 @@ class AgUiService {
throw StateError('Missing runId in /agent/runs response');
}
_threadId = threadId;
await _streamEventsFromApi(
threadId,
expectedRunId: runId,
streamToken: streamToken,
);
_activeThreadIdForRun = threadId;
_activeRunId = runId;
try {
await _streamEventsFromApi(
threadId,
expectedRunId: runId,
streamToken: streamToken,
);
} finally {
if (_activeThreadIdForRun == threadId && _activeRunId == runId) {
_activeThreadIdForRun = null;
_activeRunId = null;
}
}
return SendMessageResult(
uploadedAttachments: runInputPayload.uploadedAttachments,
);
@@ -151,6 +162,19 @@ class AgUiService {
}
Future<void> cancelCurrentRun() async {
final activeThreadId = _activeThreadIdForRun;
final activeRunId = _activeRunId;
if (activeThreadId != null && activeRunId != null) {
final encodedRunId = Uri.encodeQueryComponent(activeRunId);
await _apiClient.post<Map<String, dynamic>>(
'/api/v1/agent/runs/$activeThreadId/cancel?runId=$encodedRunId',
);
_activeThreadIdForRun = null;
_activeRunId = null;
_activeStreamToken += 1;
await _cancelActiveSseSubscription();
return;
}
_activeStreamToken += 1;
await _cancelActiveSseSubscription();
}