blib/lib/Langertha/Raider/Plugin/Events.pm

filter: Statementstmt 100% 39 / 39 Branchbran 90.0% 9 / 10 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 93.2% 69 / 74 CC 10 SCAR 16.9
CC
10
Cov
95%
plugin_after_tool_call
9.1
plugin_before_tool_call
2.0
BEGIN
1.0
CRAP 5.4
« blib/lib/Langertha/Raider/EngineResolver.pm ↑ Summary blib/lib/Langertha/Raider/Provider/Change.pm »
1 package Langertha::Raider::Plugin::Events;
2 24 our $VERSION = '0.503';
3 # ABSTRACT: Report a raid's tool calls and results as machine-output events
4  
5 24 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 24 extends 'Langertha::Plugin';
11  
12  
13 24 has on_event => (
14 is => 'ro',
15 isa => 'CodeRef',
16 required => 1,
17 );
18  
19 24 has _calls => ( is => 'rw', init_arg => undef, default => 0 );
20  
21 110 ▶async sub plugin_before_raid {
Subroutine plugin_before_raid: called CC=1 SCAR=0.0
Pod documented
22 110 my ( $self, $messages ) = @_;
23 110 $self->_calls(0);
24 110 return $messages;
25 24 }
26  
27 164 ▶async sub plugin_before_tool_call {
Subroutine plugin_before_tool_call: called CC=2 SCAR=6.9
Pod documented
28 164 my ( $self, $name, $input ) = @_;
29 164 $self->_calls($self->_calls + 1);
30 164 ▶ $self->on_event->('tool.call', call => $self->_call_id, name => $name, arguments => $input // {},
Condition100%2/2
$input // {}
l!l
1622
MC/DC100%1/1
$input // {}
$input
Truth table100%2/2
A
A= $input
Aresult
11
00
31 status => 'dispatched');
32 164 return ( $name, $input );
33 24 }
34  
35 # Tool calls run one after another, so a result belongs to the last call.
36 326 ▶sub _call_id { 'c'.$_[0]->_calls }
Subroutine _call_id: called CC=1 SCAR=0.0
37  
38 162 ▶async sub plugin_after_tool_call {
Subroutine plugin_after_tool_call: called CC=9 SCAR=22.1
Pod documented
39 162 my ( $self, $name, $input, $result ) = @_;
40 my ( $text, $ok ) = ref $result eq 'HASH'
41 162 ▶ ? ( join('', map { $_->{text} // '' } grep { ref $_ eq 'HASH' }
Branch: 1 branch
BranchTrueFalseTotal
ref $$result{'content'} eq 'ARRAY' ? : 161 0 161
Condition50%1/2
$$_{"text"} // ""
l!l
1620
MC/DC0%0/1
$$_{"text"} // ""
$$_{"text"}
Truth table50%1/2
A
A= $$_{"text"}
Aresult
11
00
42 161 ▶ ref $result->{content} eq 'ARRAY' ? @{ $result->{content} } : ()),
Condition50%1/2
$result // ""
l!l
10
MC/DC0%0/1
$result // ""
$result
Truth table50%1/2
A
A= $result
Aresult
11
00
43 !$result->{isError} )
44 162 ▶ : ( $result // '', 1 );
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq 'HASH' ? : 161 1 162
45 $self->on_event->('tool.result',
46 call => $self->_call_id,
47 name => $name,
48 162 ▶ status => ref $result eq 'HASH' && $result->{cancelled} ? 'cancelled' : $ok ? 'succeeded' : 'failed',
Branch: 3 branches
BranchTrueFalseTotal
ref $result eq 'HASH' && $$result{'cancelled'} ? : 7 155 162
$ok ? : 88 67 155
$ok ? : 88 74 162
Condition100%3/3
ref $result eq "HASH" and $$result{"cancelled"}
!ll&&!rl&&r
11547
MC/DC100%2/2
ref $result eq "HASH" and $$result{"cancelled"}
ref $result eq "HASH" $$result{"cancelled"}
Truth table100%3/3
A and B
A= ref $result eq "HASH"B= $$result{"cancelled"}
ABresult
0X0
100
111
49 ok => $ok ? JSON::MaybeXS->true : JSON::MaybeXS->false,
50 size => length $text,
51 content => $text,
52 );
53 162 return $result;
54 24 }
55  
56 24 __PACKAGE__->meta->make_immutable;
57  
58 24 1;
59  
60 __END__
61  
62 =pod
63  
64 =encoding UTF-8
65  
66 =head1 NAME
67  
68 Langertha::Raider::Plugin::Events - Report a raid's tool calls and results as machine-output events
69  
70 =head1 VERSION
71  
72 version 0.503
73  
74 =head1 SYNOPSIS
75  
76 my $raider = Langertha::Raider->new(
77 engine => $engine,
78 plugins => [ '+Langertha::Raider::Plugin::Events' => {
79 on_event => sub { my ( $type, %payload ) = @_; ... },
80 } ],
81 );
82  
83 =head1 DESCRIPTION
84  
85 The source of the C<tool.call> and C<tool.result> events of a raid: the
86 output of C<raider --stream-json> and its siblings (ADR 0013) and the
87 session journal (ADR 0015) are both fed from here. Every tool call the raid
88 makes is handed to L</on_event> twice: before it runs, with its name and
89 arguments, and after, with the outcome and the whole text of its result --
90 a consumer that shows less (the stream) cuts it itself.
91  
92 Load it after any plugin that may skip or rewrite a tool call, so the
93 events describe the call that actually runs.
94  
95 =head2 on_event
96  
97 Code reference called as C<< $on_event->($type, %payload) >>. Required.
98  
99 =over
100  
101 =item C<tool.call> -- C<call> (the id of the call within the raid: C<c1>,
102 C<c2>, ...), C<name>, C<arguments> and C<status> (C<dispatched>).
103  
104 =item C<tool.result> -- C<call> (the id of its C<tool.call>), C<name>,
105 C<status> (C<cancelled> for a call cut off by L<Langertha::Raider/cancel>,
106 C<failed> when the tool reported an error, else C<succeeded>),
107 C<ok> (the same as a boolean), C<size> (characters of the text) and
108 C<content> (the whole text).
109  
110 =back
111  
112 =head1 SEE ALSO
113  
114 =over
115  
116 =item * L<Langertha::Raider::CLI::Machine>
117  
118 =item * L<Langertha::Plugin>
119  
120 =back
121  
122 =head1 SUPPORT
123  
124 =head2 Issues
125  
126 Please report bugs and feature requests on GitHub at
127 L<https://github.com/Getty/langertha-raider/issues>.
128  
129 =head2 IRC
130  
131 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
132  
133 =head1 CONTRIBUTING
134  
135 Contributions are welcome! Please fork the repository and submit a pull request.
136  
137 =head1 AUTHOR
138  
139 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
140  
141 =head1 COPYRIGHT AND LICENSE
142  
143 This software is copyright (c) 2026 by Torsten Raudssus.
144  
145 This is free software; you can redistribute it and/or modify it under
146 the same terms as the Perl 5 programming language system itself.
147  
148 =cut
« blib/lib/Langertha/Raider/EngineResolver.pm ↑ Summary blib/lib/Langertha/Raider/Provider/Change.pm »