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

filter: Statementstmt 93.4% 114 / 122 Branchbran 79.6% 43 / 54 Conditioncond 46.1% 6 / 13 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 89.4% 17 / 19 Podpod 0.0% 0 / 7 totaltotal 81.5% 181 / 222 CC 33 SCAR 18.2
CC
33
Cov
86%
run_connect
11.2
main
10.1
_print_update
6.0
CRAP 6.2
« blib/lib/Langertha/Raider/CLI.pm ↑ Summary blib/lib/Langertha/Raider/HallTools.pm »
1 package Langertha::Raider::ACP::CLI;
2 56 our $VERSION = '0.503';
3 # ABSTRACT: Internal dispatcher of the raider acp subcommands (client side)
4  
5 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 56 ▶use Getopt::Long qw( GetOptions );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 56 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 56 ▶use Term::ANSIColor qw( colored );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 56 ▶use Term::ReadLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 56 ▶use Langertha::Raider::ACP::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 9 ▶sub _parse_endpoint {
Subroutine _parse_endpoint: called CC=4 SCAR=13.9
15 9 my ($ep) = @_;
16 9 ▶ die "endpoint required (HOST:PORT)\n" unless $ep;
Branch: 1 branch
BranchTrueFalseTotal
unless $ep 1 8 9
17 8 ▶ my ($host, $port) = $ep =~ /^(.*):(\d+)$/
Branch: 2 branches
BranchTrueFalseTotal
$ep =~ /^(.*):(\d+)$/ ? : 6 2 8
$ep =~ /^\d+$/ ? : 1 1 2
18 ? ($1, $2)
19 : ('127.0.0.1', $ep =~ /^\d+$/ ? $ep : die "bad endpoint '$ep' (want HOST:PORT or PORT)\n");
20 7 return ($host, $port);
21 }
22  
23 7 ▶sub _color_on { -t STDOUT ? colored([$_[0]], $_[1]) : $_[1] }
Subroutine _color_on: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
-t STDOUT ? : 0 7 7
24  
25 8 ▶sub main {
Subroutine main: called CC=8 SCAR=23.2
Pod undocumented
26 8 my ($class, @args) = @_;
27  
28 8 ▶ if (!@args || $args[0] eq '--help' || $args[0] eq '-h') {
Branch: 1 branch
BranchTrueFalseTotal
if (not @args or $args[0] eq "--help" or $args[0] eq "-h") 0 8 8
Condition33%2/6
not @args or $args[0] eq "--help" or $args[0] eq "-h"
l!l&&r!l&&!r
008
not @args or $args[0] eq "--help"
l!l&&r!l&&!r
008
MC/DC0%0/3
not @args or $args[0] eq "--help" or $args[0] eq "-h"
not @args $args[0] eq "--help" $args[0] eq "-h"
Truth table25%1/4
not(A) or B or C
A= not @argsB= $args[0] eq "--help"C= $args[0] eq "-h"
ABCresult
1XX1
01X1
0011
0000
29 0 print usage();
30 0 exit 0;
31 }
32  
33 8 my $cmd = shift @args;
34 8 ▶ if ($cmd eq 'ping') { return run_ping(@args); }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "ping") 2 6 8
35 6 ▶ if ($cmd eq 'prompt') { return run_prompt(@args); }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "prompt") 4 2 6
36 2 ▶ if ($cmd eq 'connect') { return run_connect(@args); }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "connect") 1 1 2
37 1 ▶ if ($cmd eq 'help') { print usage(); exit 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "help") 0 1 1
38 1 die "Unknown subcommand: $cmd\n\n" . usage();
39 }
40  
41 2 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
42 2 return <<"EOF";
43 Usage: raider acp <subcommand> HOST:PORT [options]
44  
45 Subcommands:
46 ping HOST:PORT Handshake only — print the server's
47 protocolVersion and agentCapabilities.
48 prompt HOST:PORT TEXT... One-shot: open a session, send TEXT,
49 print streaming updates, exit on stopReason.
50 connect HOST:PORT Open an interactive ACP session as a REPL.
51  
52 Options (prompt/connect):
53 --raider NAME Target raider slot on the remote hall.
54 --json Print raw JSON-RPC frames to stderr as they arrive.
55  
56 See raider hall start --acp-port N for the server side.
57 EOF
58 }
59  
60 2 ▶sub run_ping {
Subroutine run_ping: called CC=2 SCAR=6.9
Pod undocumented
61 2 my @args = @_;
62 2 ▶ my $ep = shift @args or die "Usage: raider acp ping HOST:PORT\n";
Branch: 1 branch
BranchTrueFalseTotal
my $ep = shift @args or die "Usage: raider acp ping HOST:PORT\n" 1 1 2
63 1 my ($host, $port) = _parse_endpoint($ep);
64  
65 1 my $c = Langertha::Raider::ACP::Client->new(host => $host, port => $port);
66 1 my $res = $c->initialize;
67 1 my $json = JSON::MaybeXS->new(pretty => 1, canonical => 1);
68 1 print $json->encode($res);
69 1 return 0;
70 }
71  
72 4 ▶sub run_prompt {
Subroutine run_prompt: called CC=6 SCAR=17.9
Pod undocumented
73 4 my @args = @_;
74 4 my %opt;
75 4 local @ARGV = @args;
76 4 Getopt::Long::GetOptions(\%opt, 'raider=s', 'json', 'help');
77 4 ▶ return print_prompt_help() if $opt{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 0 4 4
78  
79 4 ▶ my $ep = shift @ARGV or die "Usage: raider acp prompt HOST:PORT TEXT...\n";
Branch: 1 branch
BranchTrueFalseTotal
my $ep = shift @ARGV or die "Usage: raider acp prompt HOST:PORT TEXT...\n" 3 1 4
80 3 my ($host, $port) = _parse_endpoint($ep);
81 3 my $text = join ' ', @ARGV;
82 3 ▶ die "No prompt text given.\n" unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 1 2 3
83  
84 2 my $c = Langertha::Raider::ACP::Client->new(host => $host, port => $port);
85 2 $c->initialize;
86 2 ▶ my $sess = $c->new_session($opt{raider} ? { raiderName => $opt{raider} } : {});
Branch: 1 branch
BranchTrueFalseTotal
$opt{'raider'} ? : 1 1 2
87  
88 3 ▶ my $result = $c->prompt_stream($sess->{sessionId}, $text, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
89 3 my ($params) = @_;
90 3 _print_update($params, $opt{json});
91 2 });
92  
93 2 print "\n";
94 2 ▶ print "-- stopReason: " . ($result->{stopReason} // '(none)') . "\n";
Condition50%1/2
$$result{"stopReason"} // "(none)"
l!l
20
MC/DC0%0/1
$$result{"stopReason"} // "(none)"
$$result{"stopReason"}
Truth table50%1/2
A
A= $$result{"stopReason"}
Aresult
11
00
95 2 return 0;
96 }
97  
98 0 ▶sub print_prompt_help {
Subroutine print_prompt_help: not called CC=1 SCAR=6.9
Pod undocumented
99 0 print <<"EOF";
100 raider acp prompt HOST:PORT TEXT... [--raider NAME] [--json]
101  
102 One-shot ACP prompt. Connects, opens a session, sends TEXT, prints
103 incoming session/update notifications, exits on the final stopReason.
104 EOF
105 0 exit 0;
106 }
107  
108 1 ▶sub run_connect {
Subroutine run_connect: called CC=11 SCAR=24.2
Pod undocumented
109 1 my @args = @_;
110 1 my %opt;
111 1 local @ARGV = @args;
112 1 Getopt::Long::GetOptions(\%opt, 'raider=s', 'json', 'help');
113 1 ▶ return print_connect_help() if $opt{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 0 1 1
114  
115 1 ▶ my $ep = shift @ARGV or die "Usage: raider acp connect HOST:PORT\n";
Branch: 1 branch
BranchTrueFalseTotal
my $ep = shift @ARGV or die "Usage: raider acp connect HOST:PORT\n" 1 0 1
116 1 my ($host, $port) = _parse_endpoint($ep);
117  
118 1 my $c = Langertha::Raider::ACP::Client->new(host => $host, port => $port);
119 1 my $init = $c->initialize;
120 1 print _color_on('bright_blue', "connected to $host:$port\n");
121 print _color_on('bright_black',
122 "protocol v$init->{protocolVersion}"
123 1 ▶ . ($opt{raider} ? " — raider: $opt{raider}" : '')
Branch: 1 branch
BranchTrueFalseTotal
$opt{'raider'} ? : 0 1 1
124 . "\n\n");
125  
126 1 ▶ my $sess = $c->new_session($opt{raider} ? { raiderName => $opt{raider} } : {});
Branch: 1 branch
BranchTrueFalseTotal
$opt{'raider'} ? : 0 1 1
127 1 my $sid = $sess->{sessionId};
128  
129 # Read loop: use Term::ReadLine if available, else bare STDIN.
130 1 my $term = Term::ReadLine->new('raider-acp');
131 1 eval { $term->ornaments(0) };
132 1 my $prompt = _color_on('bright_black', "acp> ");
133  
134 1 ▶ while (defined(my $line = $term->readline($prompt))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $term->readline($prompt))) 4 0 4
135 4 $line =~ s/^\s+|\s+$//g;
136 4 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 4 4
137 4 ▶ last if $line =~ m{^(?:/quit|/exit|:q|quit|exit)$}i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ m[^(?:/quit|/exit|:q|quit|exit)$]i 1 3 4
138  
139 3 ▶ if ($line =~ m{^/cancel$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^/cancel$]) 1 2 3
140 1 eval { $c->cancel($sid) };
141 1 print _color_on(yellow => "[cancel sent]\n");
142 1 next;
143 }
144  
145 2 my $result = eval {
146 1 ▶ $c->prompt_stream($sid, $line, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
147 1 my ($params) = @_;
148 1 _print_update($params, $opt{json});
149 2 });
150 };
151 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 1 2
152 1 my $err = $@; chomp $err;
153 1 print _color_on(red => "error: $err\n");
154 1 next;
155 }
156 1 print "\n";
157 print _color_on('bright_black',
158 1 ▶ "-- stopReason: " . ($result->{stopReason} // '(none)') . "\n");
Condition50%1/2
$$result{"stopReason"} // "(none)"
l!l
10
MC/DC0%0/1
$$result{"stopReason"} // "(none)"
$$result{"stopReason"}
Truth table50%1/2
A
A= $$result{"stopReason"}
Aresult
11
00
159 }
160  
161 1 print _color_on('bright_black', "bye.\n");
162 1 return 0;
163 }
164  
165 0 ▶sub print_connect_help {
Subroutine print_connect_help: not called CC=1 SCAR=6.9
Pod undocumented
166 0 print <<"EOF";
167 raider acp connect HOST:PORT [--raider NAME] [--json]
168  
169 Open an interactive REPL backed by a remote ACP agent. Each input line
170 is sent as a session/prompt; session/update notifications stream to
171 the terminal. /cancel sends session/cancel; /quit exits.
172 EOF
173 0 exit 0;
174 }
175  
176 8 ▶sub _print_update {
Subroutine _print_update: called CC=6 SCAR=18.0
177 8 my ($params, $raw_json) = @_;
178 8 ▶ if ($raw_json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_json) 2 6 8
179 2 my $j = JSON::MaybeXS->new(canonical => 1);
180 2 print STDERR $j->encode($params) . "\n";
181 }
182 8 ▶ my $u = $params->{update} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $u = $$params{"update"} or return 7 1 8
183 # agent_message_chunk { content: { type: text, text: ... } }
184 7 my $content = $u->{content};
185 7 ▶ if (ref $content eq 'HASH' && defined $content->{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "HASH" and defined $$content{"text"}) 6 1 7
Condition66%2/3
ref $content eq "HASH" and defined $$content{"text"}
!ll&&!rl&&r
016
MC/DC50%1/2
ref $content eq "HASH" and defined $$content{"text"}
ref $content eq "HASH" defined $$content{"text"}
Truth table66%2/3
A and B
A= ref $content eq "HASH"B= defined $$content{"text"}
ABresult
0X0
100
111
186 6 print $content->{text};
187 6 ▶ *STDOUT->flush if *STDOUT->can('flush');
Branch: 1 branch
BranchTrueFalseTotal
if *STDOUT->can("flush") 6 0 6
188 }
189 }
190  
191 56 1;
192  
193 __END__
194  
195 =pod
196  
197 =encoding UTF-8
198  
199 =head1 NAME
200  
201 Langertha::Raider::ACP::CLI - Internal dispatcher of the raider acp subcommands (client side)
202  
203 =head1 VERSION
204  
205 version 0.503
206  
207 =head1 SYNOPSIS
208  
209 raider acp ping HOST:PORT
210 raider acp prompt HOST:PORT "list files"
211 raider acp connect HOST:PORT [--raider bjorn]
212  
213 =head1 DESCRIPTION
214  
215 B<Internal module.> Its interface may change without notice.
216  
217 Dispatches the C<raider acp> subcommands -- C<ping>, C<prompt> and
218 C<connect> -- to an ACP server such as L<Langertha::Raider::Hall::ACP>,
219 through L<Langertha::Raider::ACP::Client>. The endpoint is C<HOST:PORT>,
220 or just C<PORT> for C<127.0.0.1>.
221  
222 =head1 SEE ALSO
223  
224 L<Langertha::Raider::ACP::Client>, L<Langertha::Raider::Hall::ACP>.
225  
226 =head1 SUPPORT
227  
228 =head2 Issues
229  
230 Please report bugs and feature requests on GitHub at
231 L<https://github.com/Getty/langertha-raider/issues>.
232  
233 =head2 IRC
234  
235 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
236  
237 =head1 CONTRIBUTING
238  
239 Contributions are welcome! Please fork the repository and submit a pull request.
240  
241 =head1 AUTHOR
242  
243 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
244  
245 =head1 COPYRIGHT AND LICENSE
246  
247 This software is copyright (c) 2026 by Torsten Raudssus.
248  
249 This is free software; you can redistribute it and/or modify it under
250 the same terms as the Perl 5 programming language system itself.
251  
252 =cut
« blib/lib/Langertha/Raider/CLI.pm ↑ Summary blib/lib/Langertha/Raider/HallTools.pm »