cancelTask method

Future<Task> cancelTask(
  1. String taskId
)

Requests the cancellation of an ongoing task using tasks/cancel.

The server will attempt to cancel the task identified by taskId. Success is not guaranteed, as the task might have already completed or may not support cancellation.

Returns the updated Task state after the cancellation request. Throws an A2AException if the server returns a JSON-RPC error.

Implementation

Future<Task> cancelTask(String taskId) async {
  _log?.info('Canceling task: $taskId');
  final Map<String, Object?> response = await _transport.send({
    'jsonrpc': '2.0',
    'method': 'tasks/cancel',
    'params': {'id': taskId},
    'id': _requestId++,
  });
  _log?.fine('Received response from tasks/cancel: $response');
  if (response.containsKey('error')) {
    throw _exceptionFrom(response['error'] as Map<String, Object?>);
  }
  return Task.fromJson(response['result'] as Map<String, Object?>);
}