blib/lib/Langertha/Raider/ACP/Client.pm

filter: Statementstmt 77.4% 55 / 71 Branchbran 42.8% 12 / 28 Conditioncond 25.0% 7 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 86.6% 13 / 15 Podpod 83.3% 5 / 6 totaltotal 56.4% 92 / 163 CC 27 SCAR 34.2
CC
27
Cov
58%
prompt_stream
110.0
_call
9.9
new
6.8
CRAP 30.5
« blib/lib/Langertha/Raider/Hall/MCP.pm ↑ Summary blib/lib/Langertha/Raid.pm »
1 package Langertha::Raider::ACP::Client;
2 57 our $VERSION = '0.503';
3 # ABSTRACT: Minimal synchronous ACP client over TCP
4  
5 57 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 57 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 57 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 57 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 57 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 1 ▶sub new {
Subroutine new: called CC=5 SCAR=19.2
Pod documented
13 1 my ($class, %args) = @_;
14 my $sock = IO::Socket::IP->new(
15 PeerHost => $args{host} // '127.0.0.1',
16 PeerPort => $args{port} // die "port required",
17 1 ▶ Timeout => $args{timeout} // 10,
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::IP"->new("PeerHost", $args{"host"} // "127.0.0.1", "PeerPort", $args{"port"} // die("port required", "Timeout", $args{"timeout"} // 10)) or die qq[Cannot connect to $args{"host"}:$args{"port"}: $!] 1 0 1
Condition33%2/6
$args{"host"} // "127.0.0.1"
l!l
10
$args{"port"} // die("port required", "Timeout", $args{"timeout"} // 10)
l!l
10
$args{"timeout"} // 10
l!l
00
MC/DC0%0/1
$args{"host"} // "127.0.0.1"
$args{"host"}
MC/DC0%0/1
$args{"port"} // die("port required", "Timeout", $args{"timeout"} // 10)
$args{"port"}
MC/DC0%0/1
$args{"timeout"} // 10
$args{"timeout"}
Truth table50%1/2
A
A= $args{"host"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"port"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"timeout"}
Aresult
11
00
18 ) or die "Cannot connect to $args{host}:$args{port}: $!";
19 1 $sock->autoflush(1);
20 bless {
21 sock => $sock,
22 next_id => 1,
23 buf => '',
24 json => JSON::MaybeXS->new,
25 host => $args{host},
26 port => $args{port},
27 1 }, $class;
28 }
29  
30 3 ▶sub _send {
Subroutine _send: called CC=2 SCAR=7.0
31 3 my ($self, $msg) = @_;
32 3 my $bytes = $self->{json}->encode($msg) . "\n";
33 3 my $n = syswrite $self->{sock}, $bytes;
34 3 ▶ die "write to ACP server failed: $!" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 3 3
35 }
36  
37 3 ▶sub _read_line {
Subroutine _read_line: called CC=4 SCAR=14.1
38 3 my ($self) = @_;
39 3 ▶ while ($self->{buf} !~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$self{"buf"} =~ /\n/) 3 3 6
40 3 my $chunk;
41 3 my $n = sysread $self->{sock}, $chunk, 4096;
42 3 ▶ return undef unless defined $n && $n > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n > 0 0 3 3
Condition33%1/3
defined $n and $n > 0
!ll&&!rl&&r
003
MC/DC0%0/2
defined $n and $n > 0
defined $n $n > 0
Truth table33%1/3
A and B
A= defined $nB= $n > 0
ABresult
0X0
100
111
43 3 $self->{buf} .= $chunk;
44 }
45 3 $self->{buf} =~ s/^(.*?)\n//;
46 3 my $line = $1;
47 3 $line =~ s/\r$//;
48 3 return $line;
49 }
50  
51 3 ▶sub _call {
Subroutine _call: called CC=8 SCAR=22.9
52 3 my ($self, $method, $params) = @_;
53 3 my $id = $self->{next_id}++;
54 3 ▶ $self->_send({
Condition50%1/2
$params // {}
l!l
30
MC/DC0%0/1
$params // {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
55 jsonrpc => '2.0', id => $id, method => $method, params => $params // {},
56 });
57 3 ▶ while (defined(my $line = $self->_read_line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $self->_read_line)) 3 0 3
58 3 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 3 3
59 3 ▶ my $msg = eval { $self->{json}->decode($line) } // next;
Condition50%1/2
eval { // (next)
l!l
30
MC/DC0%0/1
eval { do { $$self{"json"}->decode($line) } } // (next)
eval { do { $$self{"json"}->decode($line) } }
Truth table50%1/2
A
A= eval { do { $$self{"json"}->decode($line) } }
Aresult
11
00
60 3 ▶ if (defined $msg->{id} && $msg->{id} eq $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$msg{"id"} and $$msg{"id"} eq $id) 3 0 3
Condition33%1/3
defined $$msg{"id"} and $$msg{"id"} eq $id
!ll&&!rl&&r
003
MC/DC0%0/2
defined $$msg{"id"} and $$msg{"id"} eq $id
defined $$msg{"id"} $$msg{"id"} eq $id
Truth table33%1/3
A and B
A= defined $$msg{"id"}B= $$msg{"id"} eq $id
ABresult
0X0
100
111
61 3 ▶ die "ACP error: $msg->{error}{message}\n" if $msg->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $$msg{"error"} 1 2 3
62 2 return $msg->{result};
63 }
64 # Any other frame (a notification or a response to a different id)
65 # gets swallowed in pure-call mode. prompt_stream handles this
66 # properly.
67 }
68 0 die "ACP connection closed while waiting for reply to $method\n";
69 }
70  
71 1 ▶sub initialize {
Subroutine initialize: called CC=1 SCAR=0.0
Pod documented
72 1 my ($self) = @_;
73 1 $self->_call('initialize', {});
74 }
75  
76 1 ▶sub new_session {
Subroutine new_session: called CC=2 SCAR=7.0
Pod documented
77 1 my ($self, $extra) = @_;
78 1 ▶ my %p = (cwd => Cwd::cwd(), %{ $extra // {} });
Condition50%1/2
$extra // {}
l!l
01
MC/DC0%0/1
$extra // {}
$extra
Truth table50%1/2
A
A= $extra
Aresult
11
00
79 1 $self->_call('session/new', \%p);
80 }
81  
82 0 ▶sub cancel {
Subroutine cancel: not called CC=1 SCAR=6.9
Pod documented
83 0 my ($self, $sid) = @_;
84 0 $self->_call('session/cancel', { sessionId => $sid });
85 }
86  
87 0 ▶sub prompt_stream {
Subroutine prompt_stream: not called CC=10 SCAR=47.0
Pod documented
88 0 my ($self, $sid, $text, $on_update) = @_;
89 0 my $id = $self->{next_id}++;
90 0 $self->_send({
91 jsonrpc => '2.0', id => $id, method => 'session/prompt',
92 params => {
93 sessionId => $sid,
94 prompt => [{ type => 'text', text => $text }],
95 },
96 });
97 0 ▶ while (defined(my $line = $self->_read_line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $self->_read_line)) 0 0 0
98 0 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 0 0
99 0 ▶ my $msg = eval { $self->{json}->decode($line) } // next;
Condition0%0/2
eval { // (next)
l!l
00
MC/DC0%0/1
eval { do { $$self{"json"}->decode($line) } } // (next)
eval { do { $$self{"json"}->decode($line) } }
Truth table0%0/2
A
A= eval { do { $$self{"json"}->decode($line) } }
Aresult
11
00
100 0 ▶ if (defined $msg->{id} && $msg->{id} eq $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$msg{"id"} and $$msg{"id"} eq $id) 0 0 0
Condition0%0/3
defined $$msg{"id"} and $$msg{"id"} eq $id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $$msg{"id"} and $$msg{"id"} eq $id
defined $$msg{"id"} $$msg{"id"} eq $id
Truth table0%0/3
A and B
A= defined $$msg{"id"}B= $$msg{"id"} eq $id
ABresult
0X0
100
111
101 0 ▶ die "ACP error: $msg->{error}{message}\n" if $msg->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $$msg{"error"} 0 0 0
102 0 return $msg->{result};
103 }
104 0 ▶ if (($msg->{method} // '') eq 'session/update' && $on_update) {
Branch: 1 branch
BranchTrueFalseTotal
if (($$msg{"method"} // "") eq "session/update" and $on_update) 0 0 0
Condition0%0/5
($$msg{"method"} // "") eq "session/update" and $on_update
!ll&&!rl&&r
000
$$msg{"method"} // ""
l!l
00
MC/DC0%0/2
($$msg{"method"} // "") eq "session/update" and $on_update
$$msg{"method"} $on_update
Truth table0%0/3
A and B
A= $$msg{"method"}B= $on_update
ABresult
0X0
100
111
105 0 $on_update->($msg->{params});
106 }
107 }
108 0 die "ACP connection closed during session/prompt\n";
109 }
110  
111 6 ▶sub close {
Subroutine close: called CC=2 SCAR=6.9
Pod undocumented
112 6 my ($self) = @_;
113 6 ▶ close $self->{sock} if $self->{sock};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"sock"} 1 5 6
114 6 delete $self->{sock};
115 }
116  
117 5 ▶sub DESTROY { $_[0]->close }
Subroutine DESTROY: called CC=1 SCAR=0.0
118  
119 57 1;
120  
121 __END__
122  
123 =pod
124  
125 =encoding UTF-8
126  
127 =head1 NAME
128  
129 Langertha::Raider::ACP::Client - Minimal synchronous ACP client over TCP
130  
131 =head1 VERSION
132  
133 version 0.503
134  
135 =head1 SYNOPSIS
136  
137 use Langertha::Raider::ACP::Client;
138 my $c = Langertha::Raider::ACP::Client->new(host => '127.0.0.1', port => 38421);
139 my $init = $c->initialize;
140 my $s = $c->new_session;
141 $c->prompt_stream($s->{sessionId}, "summarise Changes", sub {
142 my ($update) = @_;
143 # $update is the params of a session/update JSON-RPC notification
144 print $update->{update}{content}{text} // '';
145 });
146  
147 =head1 DESCRIPTION
148  
149 Line-framed JSON-RPC 2.0 over a blocking TCP socket. Matched against
150 L<Langertha::Raider::Hall::ACP> on the server side, but protocol-clean so it
151 speaks to any conforming ACP agent.
152  
153 Sync by design: this is a CLI client, not a daemon. The one non-trivial
154 piece is C<prompt_stream>, which multiplexes inbound C<session/update>
155 notifications with the pending C<session/prompt> response on the same
156 socket.
157  
158 =head2 new
159  
160 my $c = Langertha::Raider::ACP::Client->new(host => '127.0.0.1', port => 38421, timeout => 10);
161  
162 Opens the connection; C<port> is required, C<host> defaults to
163 C<127.0.0.1> and C<timeout> (seconds) to 10. Dies on failure.
164  
165 =head2 initialize
166  
167 Handshake. Returns the server's C<result> hash
168 (C<protocolVersion>, C<agentCapabilities>).
169  
170 =head2 new_session
171  
172 my $s = $c->new_session({ raiderName => 'bjorn' });
173  
174 Create a session. Adds the current C<cwd> automatically if the caller
175 didn't set one. Returns the C<result> hash (C<sessionId>, ...).
176  
177 =head2 prompt_stream
178  
179 my $result = $c->prompt_stream($session_id, $text, $on_update);
180  
181 Send a prompt, invoke C<$on_update-E<gt>($params)> for each
182 C<session/update> notification that arrives, and return the final
183 result hash (typically C<{ stopReason =E<gt> ... }>).
184  
185 =head2 cancel
186  
187 $c->cancel($session_id);
188  
189 Send C<session/cancel>. Returns the server's result.
190  
191 =head1 SEE ALSO
192  
193 L<Langertha::Raider::Hall::ACP>, L<Langertha::Raider::ACP::CLI>.
194  
195 =head1 SUPPORT
196  
197 =head2 Issues
198  
199 Please report bugs and feature requests on GitHub at
200 L<https://github.com/Getty/langertha-raider/issues>.
201  
202 =head2 IRC
203  
204 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
205  
206 =head1 CONTRIBUTING
207  
208 Contributions are welcome! Please fork the repository and submit a pull request.
209  
210 =head1 AUTHOR
211  
212 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
213  
214 =head1 COPYRIGHT AND LICENSE
215  
216 This software is copyright (c) 2026 by Torsten Raudssus.
217  
218 This is free software; you can redistribute it and/or modify it under
219 the same terms as the Perl 5 programming language system itself.
220  
221 =cut
« blib/lib/Langertha/Raider/Hall/MCP.pm ↑ Summary blib/lib/Langertha/Raid.pm »