blib/lib/Langertha/Raider/HallTools.pm

filter: Statementstmt 100% 62 / 62 Branchbran 96.8% 31 / 32 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 97.5% 117 / 120 CC 21 SCAR 17.3
CC
21
Cov
98%
build_hall_tools_server
9.0
__ANON__
8.0
_call
4.0
CRAP 5.6
« blib/lib/Langertha/Raider/ACP/CLI.pm ↑ Summary blib/lib/Langertha/Raider/EngineResolver.pm »
1 package Langertha::Raider::HallTools;
2 81 our $VERSION = '0.503';
3 # ABSTRACT: MCP::Server factory with hall-side tools (telegram_reply, hall_status, hall_spawn)
4  
5 81 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 81 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 81 ▶use MCP::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 81 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 81 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 81 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 81 our @EXPORT_OK = qw( build_hall_tools_server );
13  
14  
15 13 ▶sub _call {
Subroutine _call: called CC=4 SCAR=13.9
16 13 my ($sock_path, $cmd, %payload) = @_;
17  
18 13 ▶ my $s = IO::Socket::UNIX->new(Peer => $sock_path)
Branch: 1 branch
BranchTrueFalseTotal
my $s = "IO::Socket::UNIX"->new("Peer", $sock_path) or return {"error", "cannot connect to hall socket ${sock_path}: $!"} 10 3 13
19 or return { error => "cannot connect to hall socket $sock_path: $!" };
20 10 $s->autoflush(1);
21  
22 10 my $frame = JSON::MaybeXS->new->encode({
23 type => 'command',
24 payload => { cmd => $cmd, %payload },
25 });
26 10 print $s "$frame\n";
27  
28 10 my $line = <$s>;
29 10 close $s;
30 10 ▶ return { error => 'no response from hall' } unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 2 8 10
31 8 chomp $line;
32 8 my $resp = eval { JSON::MaybeXS->new->decode($line) };
33 8 ▶ return { error => "invalid hall response: $@" } if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 7 8
34 7 return $resp;
35 }
36  
37 17 ▶sub build_hall_tools_server {
Subroutine build_hall_tools_server: called CC=9 SCAR=22.0
Pod documented
38 17 my (%args) = @_;
39 my $sock = $args{socket}
40 17 ▶ or die "build_hall_tools_server: socket param required";
Branch: 1 branch
BranchTrueFalseTotal
my $sock = $args{"socket"} or die "build_hall_tools_server: socket param required" 16 1 17
41 my $bound = $args{telegram}
42 // ( $ENV{RAIDER_HALL_TELEGRAM_BOT} && defined $ENV{RAIDER_HALL_TELEGRAM_CHAT_ID}
43 ? {
44 bot => $ENV{RAIDER_HALL_TELEGRAM_BOT},
45 chat_id => $ENV{RAIDER_HALL_TELEGRAM_CHAT_ID},
46 defined $ENV{RAIDER_HALL_TELEGRAM_THREAD_ID}
47 16 ▶ ? ( message_thread_id => $ENV{RAIDER_HALL_TELEGRAM_THREAD_ID} ) : (),
Branch: 2 branches
BranchTrueFalseTotal
$ENV{'RAIDER_HALL_TELEGRAM_BOT'} && defined $ENV{'RAIDER_HALL_TELEGRAM_CHAT_ID'} ? : 2 11 13
defined $ENV{'RAIDER_HALL_TELEGRAM_THREAD_ID'} ? : 1 1 2
Condition83%5/6
$args{"telegram"} // ($ENV{"RAIDER_HALL_TELEGRAM_BOT"} && defined $ENV{"RAIDER_HALL_TELEGRAM_CHAT_ID"} ? {"bot", $ENV{"RAIDER_HALL_TELEGRAM_BOT"}, "chat_id", $ENV{"RAIDER_HALL_TELEGRAM_CHAT_ID"}, defined $ENV{"RAIDER_HALL_TELEGRAM_THREAD_ID"} ? ("message_thread_id", $ENV{"RAIDER_HALL_TELEGRAM_THREAD_ID"}) : ()} : undef)
l!l&&r!l&&!r
3211
$ENV{"RAIDER_HALL_TELEGRAM_BOT"} and defined $ENV{"RAIDER_HALL_TELEGRAM_CHAT_ID"}
!ll&&!rl&&r
1102
MC/DC66%2/3
$args{"telegram"} // ($ENV{"RAIDER_HALL_TELEGRAM_BOT"} && defined $ENV{"RAIDER_HALL_TELEGRAM_CHAT_ID"} ? {"bot", $ENV{"RAIDER_HALL_TELEGRAM_BOT"}, "chat_id", $ENV{"RAIDER_HALL_TELEGRAM_CHAT_ID"}, defined $ENV{"RAIDER_HALL_TELEGRAM_THREAD_ID"} ? ("message_thread_id", $ENV{"RAIDER_HALL_TELEGRAM_THREAD_ID"}) : ()} : undef)
$args{"telegram"} $ENV{"RAIDER_HALL_TELEGRAM_BOT"} defined $ENV{"RAIDER_HALL_TELEGRAM_CHAT_ID"}
Truth table75%3/4
A // B and C
A= $args{"telegram"}B= $ENV{"RAIDER_HALL_TELEGRAM_BOT"}C= defined $ENV{"RAIDER_HALL_TELEGRAM_CHAT_ID"}
ABCresult
1XX1
0111
00X0
0100
48 }
49 : undef );
50  
51 16 my $server = MCP::Server->new(name => 'raider-hall', version => '1.0');
52  
53 $server->tool(
54 name => 'telegram_reply',
55 description => $bound
56 ? 'Reply to the Telegram chat that sent you this mission. Only text is needed.'
57 : 'Send a Telegram message from the hall. Use this to reply to a user that reached you through the hall\'s telegram.in event.',
58 input_schema => {
59 type => 'object',
60 properties => {
61 $bound ? () : (
62 bot => { type => 'string', description => 'Bot name as configured in .raider-hall.yml' },
63 chat_id => { type => 'integer', description => 'Telegram chat id' },
64 message_thread_id => { type => 'integer', description => 'Forum topic to answer in (optional)' },
65 ),
66 text => { type => 'string', description => 'Message body (Markdown)' },
67 },
68 required => $bound ? ['text'] : [qw(bot chat_id text)],
69 },
70 9 ▶ code => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
71 9 my ($tool, $in) = @_;
72 9 my %target = map { $_ => $in->{$_} } qw( bot chat_id );
73 9 ▶ if ($bound) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bound) 7 2 9
74 7 for my $key (qw( bot chat_id )) {
75 return $tool->text_result('Error: this run can only reply to '
76 .$bound->{bot}.' chat '.$bound->{chat_id}, 1)
77 13 ▶ if defined $target{$key} && $target{$key} ne $bound->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $target{$key} and $target{$key} ne $$bound{$key} 2 11 13
Condition100%3/3
defined $target{$key} and $target{$key} ne $$bound{$key}
!ll&&!rl&&r
922
MC/DC100%2/2
defined $target{$key} and $target{$key} ne $$bound{$key}
defined $target{$key} $target{$key} ne $$bound{$key}
Truth table100%3/3
A and B
A= defined $target{$key}B= $target{$key} ne $$bound{$key}
ABresult
0X0
100
111
78 11 $target{$key} = $bound->{$key};
79 }
80 }
81 # The forum topic comes with the target: bound, from the hall only.
82 7 ▶ my $thread = $bound ? $bound->{message_thread_id} : $in->{message_thread_id};
Branch: 1 branch
BranchTrueFalseTotal
$bound ? : 5 2 7
83 7 ▶ $target{message_thread_id} = $thread if defined $thread;
Branch: 1 branch
BranchTrueFalseTotal
if defined $thread 3 4 7
84 7 my $r = _call($sock, 'telegram_reply', %target, text => $in->{text});
85 7 ▶ return $tool->text_result("Error: $r->{error}", 1) if $r->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $$r{"error"} 0 7 7
86 7 return $tool->text_result('sent');
87 },
88 16 ▶ );
Branch: 3 branches
BranchTrueFalseTotal
$bound ? : 5 11 16
$bound ? : 5 11 16
$bound ? : 5 11 16
89  
90 $server->tool(
91 name => 'hall_status',
92 description => 'Ask the hall how many raiders are currently running.',
93 input_schema => { type => 'object', properties => {} },
94 2 ▶ code => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
95 2 my ($tool, $in) = @_;
96 2 my $r = _call($sock, 'status');
97 2 ▶ return $tool->text_result("Error: $r->{error}", 1) if $r->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $$r{"error"} 1 1 2
98 1 return $tool->text_result(JSON::MaybeXS->new(canonical => 1)->encode($r));
99 },
100 16 );
101  
102 $server->tool(
103 name => 'hall_spawn',
104 description => 'Spawn another raider in the same hall (respects 1name singleton queueing).',
105 input_schema => {
106 type => 'object',
107 properties => {
108 name => { type => 'string', description => 'Raider slot name (e.g. "bjorn" or "1bjorn")' },
109 mission => { type => 'string', description => 'One-shot task' },
110 },
111 required => [qw(name mission)],
112 },
113 2 ▶ code => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
114 2 my ($tool, $in) = @_;
115 2 my $r = _call($sock, 'spawn', name => $in->{name}, mission => $in->{mission});
116 2 ▶ return $tool->text_result("Error: $r->{error}", 1) if $r->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $$r{"error"} 1 1 2
117 1 return $tool->text_result(JSON::MaybeXS->new(canonical => 1)->encode($r));
118 },
119 16 );
120  
121 16 return $server;
122 }
123  
124 81 1;
125  
126 __END__
127  
128 =pod
129  
130 =encoding UTF-8
131  
132 =head1 NAME
133  
134 Langertha::Raider::HallTools - MCP::Server factory with hall-side tools (telegram_reply, hall_status, hall_spawn)
135  
136 =head1 VERSION
137  
138 version 0.503
139  
140 =head1 DESCRIPTION
141  
142 The Hall tools of L<raider>: F<raider> mounts them when a
143 L<Langertha::Raider::Hall> started it (L</RAIDER_HALL_SOCKET> names a
144 socket).
145  
146 =head2 build_hall_tools_server
147  
148 my $server = Langertha::Raider::HallTools::build_hall_tools_server(
149 socket => $ENV{RAIDER_HALL_SOCKET},
150 );
151  
152 Returns an L<MCP::Server> exposing tools that let a raider running inside
153 a hall talk back to its hall daemon:
154  
155 =over
156  
157 =item * C<telegram_reply(bot, chat_id, text)>
158  
159 When the raider was spawned for a Telegram message, the hall sets
160 C<RAIDER_HALL_TELEGRAM_BOT> and C<RAIDER_HALL_TELEGRAM_CHAT_ID> (or pass
161 C<< telegram => { bot => ..., chat_id => ... } >>). The tool is then bound to
162 that chat: only C<text> is required, and a different C<bot> or C<chat_id>
163 is refused. A message from a forum topic also sets
164 C<RAIDER_HALL_TELEGRAM_THREAD_ID> (C<message_thread_id> in the hash), and
165 the reply goes into that topic. Unbound, C<message_thread_id> is an
166 optional argument.
167  
168 =item * C<hall_status()>
169  
170 =item * C<hall_spawn(name, mission)>
171  
172 =back
173  
174 Each tool opens a short-lived UNIX-socket command connection to the
175 hall, sends a single JSON frame, reads the reply, and returns it as
176 text. Errors surface as text_result with isError=1.
177  
178 =head1 ENVIRONMENT
179  
180 The Hall sets these for the raiders it starts; the Telegram ones only for
181 a Telegram mission, and without a C<telegram> argument the function reads
182 them.
183  
184 =head2 RAIDER_HALL_SOCKET
185  
186 The Hall's control socket. F<raider> mounts the tools when it names a
187 socket and passes it as C<socket>; the function itself does not read it.
188  
189 =head2 RAIDER_HALL_TELEGRAM_BOT
190  
191 The bot of the Telegram message the raider was started for; with
192 L</RAIDER_HALL_TELEGRAM_CHAT_ID> it binds C<telegram_reply> to that chat.
193  
194 =head2 RAIDER_HALL_TELEGRAM_CHAT_ID
195  
196 The chat of that message.
197  
198 =head2 RAIDER_HALL_TELEGRAM_THREAD_ID
199  
200 The forum topic of that message, when it came from one.
201  
202 =head1 SEE ALSO
203  
204 =over
205  
206 =item * L<Langertha::Raider::Hall>
207  
208 =item * L<MCP::Server>
209  
210 =back
211  
212 =head1 SUPPORT
213  
214 =head2 Issues
215  
216 Please report bugs and feature requests on GitHub at
217 L<https://github.com/Getty/langertha-raider/issues>.
218  
219 =head2 IRC
220  
221 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
222  
223 =head1 CONTRIBUTING
224  
225 Contributions are welcome! Please fork the repository and submit a pull request.
226  
227 =head1 AUTHOR
228  
229 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
230  
231 =head1 COPYRIGHT AND LICENSE
232  
233 This software is copyright (c) 2026 by Torsten Raudssus.
234  
235 This is free software; you can redistribute it and/or modify it under
236 the same terms as the Perl 5 programming language system itself.
237  
238 =cut
« blib/lib/Langertha/Raider/ACP/CLI.pm ↑ Summary blib/lib/Langertha/Raider/EngineResolver.pm »