| 1 |
root |
1.1 |
=head1 NAME |
| 2 |
|
|
|
| 3 |
|
|
AnyEvent::MCP::Client::Request - a request for client input |
| 4 |
|
|
|
| 5 |
|
|
=head1 DESCRIPTION |
| 6 |
|
|
|
| 7 |
|
|
Passed to C<on_request> for modern multi-round-trip input or legacy incoming |
| 8 |
|
|
requests. Fields are directly accessible: C<client>, C<id>, C<method>, |
| 9 |
|
|
C<params>, C<operation> (the originating local call, when known), |
| 10 |
|
|
C<protocol_version>, C<finished> and C<cancelled>. Retain the request for asynchronous user input. |
| 11 |
|
|
|
| 12 |
|
|
=head1 METHODS |
| 13 |
|
|
|
| 14 |
|
|
=head2 $req->reply ($result) |
| 15 |
|
|
|
| 16 |
|
|
Completes the input request with a result hash reference. Duplicate replies |
| 17 |
|
|
die. Protocol field spelling, such as C<resultType>, is preserved. |
| 18 |
|
|
|
| 19 |
|
|
=head2 $req->error ($code, $message, $data) |
| 20 |
|
|
|
| 21 |
|
|
Reports a JSON-RPC error. C<$data> is optional. For modern input this fails |
| 22 |
|
|
the local operation; for legacy requests it sends an error response. |
| 23 |
|
|
|
| 24 |
|
|
=head2 $req->cancel |
| 25 |
|
|
|
| 26 |
|
|
Marks the request finished and cancelled, releases its completion callback |
| 27 |
|
|
and calls C<< $req->{on_cancel}->($req) >> when present. Repeated cancellation |
| 28 |
|
|
is harmless. Applications can use C<on_cancel> to close an outstanding prompt. |
| 29 |
|
|
|
| 30 |
|
|
=cut |
| 31 |
|
|
|
| 32 |
|
|
package AnyEvent::MCP::Client::Request; |
| 33 |
|
|
|
| 34 |
|
|
use common::sense; |
| 35 |
|
|
use AnyEvent::MCP (); |
| 36 |
|
|
use Scalar::Util qw(weaken); |
| 37 |
|
|
|
| 38 |
|
|
our @ISA = qw(AnyEvent::MCP::Base); |
| 39 |
|
|
|
| 40 |
|
|
sub new { |
| 41 |
|
|
my $class = shift; |
| 42 |
|
|
my $self = $class->SUPER::new (@_); |
| 43 |
|
|
weaken $self->{client}; |
| 44 |
|
|
$self |
| 45 |
|
|
} |
| 46 |
|
|
|
| 47 |
|
|
sub _finish { |
| 48 |
|
|
my ($self, $success, $result) = @_; |
| 49 |
|
|
|
| 50 |
|
|
die "Request already finished\n" if $self->{finished}; |
| 51 |
|
|
$self->{finished} = 1; |
| 52 |
|
|
delete $self->{on_cancel}; |
| 53 |
|
|
(delete $self->{on_reply})->($success, $result); |
| 54 |
|
|
} |
| 55 |
|
|
|
| 56 |
|
|
sub reply { |
| 57 |
|
|
my ($self, $result) = @_; |
| 58 |
|
|
$result = { resultType => "complete", %$result } if $self->{protocol_version} ge "2026"; |
| 59 |
|
|
$self->_finish (1, $result); |
| 60 |
|
|
} |
| 61 |
|
|
|
| 62 |
|
|
sub error { |
| 63 |
|
|
my ($self, $code, $message, $data) = @_; |
| 64 |
|
|
$self->_finish (0, { code => $code, message => $message, (defined $data ? (data => $data) : ()) }); |
| 65 |
|
|
} |
| 66 |
|
|
|
| 67 |
|
|
sub cancel { |
| 68 |
|
|
my ($self) = @_; |
| 69 |
|
|
return if $self->{finished}; |
| 70 |
|
|
$self->{finished} = $self->{cancelled} = 1; |
| 71 |
|
|
delete $self->{on_reply}; |
| 72 |
|
|
(delete $self->{on_cancel} || sub {})->($self); |
| 73 |
|
|
} |
| 74 |
|
|
|
| 75 |
|
|
1 |