AnyEvent-MCP

 view release on metacpan or  search on metacpan

lib/AnyEvent/MCP/Helper.pm  view on Meta::CPAN

=head1 NAME

AnyEvent::MCP::Helper - asynchronous JSON exchange with an external helper

=head1 SYNOPSIS

   use AnyEvent::MCP::Helper;
   my $job = AnyEvent::MCP::Helper->new (
      helper => ["my-helper", "--option"], timeout => 300,
   );
   $job->ask ($input, sub {
      my ($success, $result) = @_;
      ...
   });

=head1 DESCRIPTION

Runs an executable (or an argument array, never a shell command), writes one
UTF-8 JSON value to its stdin and closes that input. A successful helper
writes one JSON value to stdout and exits zero. Diagnostics use stderr.

C<ask ($input, $done)> calls C<$done-E<gt>($success, $result)> once. On failure,
C<$result> is an error string. One exchange may be active per object.
C<timeout> defaults to 300 seconds and C<max_size> to 1 MiB of output.
C<cancel ($reason)> terminates the helper and completes with failure.
The independent child watcher reaps the helper after cancellation too.

This module handles processes and bytes, not the meaning of the JSON.
It is the transport for local form and permission helpers: an MCP server
or multibroker can ask a human without blocking its AnyEvent loop.
Ordinary server authors should normally use
L<AnyEvent::MCP::Server::Permissions> or the form facilities in
L<AnyEvent::MCP::Permissions::Ask>, not launch helpers directly.
L<AnyEvent::MCP::Permissions::Ask> provides the permission interface;
C<anyevent-mcp-form-elicitation> implements the standard form request/response contract.

=cut

package AnyEvent::MCP::Helper;

use common::sense;

use JSON::XS ();
use Scalar::Util qw(weaken);

use AnyEvent ();
use AnyEvent::Util ();
use AnyEvent::Handle ();
use AnyEvent::MCP ();

our @ISA = qw(AnyEvent::MCP::Base);

sub ask {
   my ($self, $prepared, $done) = @_;

   die "Helper is already active\n" if $self->{on_done};
   $self->{finished} = 0;
   $self->{on_done} = $done;
   delete @$self{qw(status eof)};
   my $helper = $self->{helper} or die "No helper configured\n";
   my $input = JSON::XS::encode_json $prepared;
   my $output = "";
   my $ok = eval {
      my ($parent, $child) = AnyEvent::Util::portable_socketpair;
      die "Cannot create helper socket: $!\n" unless defined $parent && defined $child;
      $self->{cv} = AnyEvent::Util::run_cmd (
         ref $helper eq "ARRAY" ? $helper : [$helper],
         "<" => $child, ">" => $child, '$$' => \$self->{pid}, close_all => 1,
      );
      close $child;
      weaken $self;
      $self->{handle} = AnyEvent::Handle->new (
         fh       => $parent,
         on_error => sub { $self->cancel ("Helper socket: $_[2]") if $self },
         on_read  => sub {
            return unless $self && !$self->{finished};
            $output .= $_[0]{rbuf};
            $_[0]{rbuf} = "";
            $self->cancel ("Helper output exceeds size limit")
               if length $output > ($self->{max_size} || 1024 * 1024);



( run in 1.367 second using v1.01-cache-2.11-cpan-60884a8df76 )