| 1 | package Langertha::Raider::CLI::Machine; | ||||||||||||||
| 2 | # ABSTRACT: Internal writer of the raider CLI's machine output (JSON, MessagePack, YAML) | ||||||||||||||
| 3 | 55 | our $VERSION = '0.503'; | |||||||||||||
| 4 | 55 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 55 | ▶ | use Moose::Util::TypeConstraints qw( enum ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 55 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 55 | ▶ | use Data::MessagePack; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 55 | ▶ | use Encode qw( encode_utf8 ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 55 | ▶ | use IO::Handle; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 55 | ▶ | use JSON::MaybeXS (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 55 | ▶ | use Time::HiRes (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 55 | ▶ | use YAML::PP; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | |||||||||||||||
| 15 | |||||||||||||||
| 16 | 55 | has format => ( | |||||||||||||
| 17 | is => 'ro', | ||||||||||||||
| 18 | isa => enum([qw( json msgpack yaml )]), | ||||||||||||||
| 19 | required => 1, | ||||||||||||||
| 20 | ); | ||||||||||||||
| 21 | |||||||||||||||
| 22 | |||||||||||||||
| 23 | 55 | has stream => ( | |||||||||||||
| 24 | is => 'ro', | ||||||||||||||
| 25 | isa => 'Bool', | ||||||||||||||
| 26 | default => 0, | ||||||||||||||
| 27 | ); | ||||||||||||||
| 28 | |||||||||||||||
| 29 | |||||||||||||||
| 30 | 55 | has version => ( | |||||||||||||
| 31 | is => 'ro', | ||||||||||||||
| 32 | isa => 'Int', | ||||||||||||||
| 33 | default => 1, | ||||||||||||||
| 34 | ); | ||||||||||||||
| 35 | |||||||||||||||
| 36 | |||||||||||||||
| 37 | has out => ( | ||||||||||||||
| 38 | is => 'ro', | ||||||||||||||
| 39 | 11 | ▶ | default => sub { \*STDOUT }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | 55 | ); | |||||||||||||
| 41 | |||||||||||||||
| 42 | |||||||||||||||
| 43 | has clock => ( | ||||||||||||||
| 44 | is => 'ro', | ||||||||||||||
| 45 | isa => 'CodeRef', | ||||||||||||||
| 46 | 69 | ▶ | default => sub { \&Time::HiRes::time }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 47 | 55 | ); | |||||||||||||
| 48 | |||||||||||||||
| 49 | |||||||||||||||
| 50 | 55 | has max_content_length => ( | |||||||||||||
| 51 | is => 'ro', | ||||||||||||||
| 52 | isa => 'Int', | ||||||||||||||
| 53 | default => 1000, | ||||||||||||||
| 54 | ); | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 55 | has _seq => ( | |||||||||||||
| 57 | traits => ['Counter'], | ||||||||||||||
| 58 | is => 'ro', | ||||||||||||||
| 59 | isa => 'Int', | ||||||||||||||
| 60 | default => 0, | ||||||||||||||
| 61 | handles => { _next_seq => 'inc' }, | ||||||||||||||
| 62 | ); | ||||||||||||||
| 63 | |||||||||||||||
| 64 | |||||||||||||||
| 65 | 69 | ▶ | sub versions { (1) } | ||||||||||||
|
Subroutine
versions: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 66 | |||||||||||||||
| 67 | |||||||||||||||
| 68 | 41 | ▶ | sub document { | ||||||||||||
|
Subroutine
document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 69 | 41 | my ( $self, $status, %fields ) = @_; | |||||||||||||
| 70 | 41 | return { %fields, version => $self->version, status => $status }; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | |||||||||||||||
| 74 | 191 | ▶ | sub event { | ||||||||||||
|
Subroutine
event: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 75 | 191 | my ( $self, $type, %payload ) = @_; | |||||||||||||
| 76 | 191 | ▶ | return unless $self->stream; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 74 | ▶ | if ($type eq 'tool.result') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | 12 | ▶ | my $content = $payload{content} // ''; | ||||||||||||
|
Condition50%1/2
$payload{"content"} // ""
MC/DC0%0/1
$payload{"content"} // ""
$payload{"content"}
Truth table50%1/2
A
A= $payload{"content"}
| |||||||||||||||
| 79 | 12 | my $max = $self->max_content_length; | |||||||||||||
| 80 | 12 | $payload{content} = substr($content, 0, $max); | |||||||||||||
| 81 | 12 | ▶ | $payload{truncated} = length $content > $max ? JSON::MaybeXS->true : JSON::MaybeXS->false; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | } | ||||||||||||||
| 83 | $self->write({ | ||||||||||||||
| 84 | 74 | %payload, | |||||||||||||
| 85 | version => $self->version, | ||||||||||||||
| 86 | type => $type, | ||||||||||||||
| 87 | seq => $self->_next_seq, | ||||||||||||||
| 88 | time => $self->clock->(), | ||||||||||||||
| 89 | }); | ||||||||||||||
| 90 | 74 | return; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | |||||||||||||||
| 94 | 40 | ▶ | sub finish { | ||||||||||||
|
Subroutine
finish: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 95 | 40 | my ( $self, $doc ) = @_; | |||||||||||||
| 96 | 40 | ▶ | return $self->event('run.finished', %$doc) if $self->stream; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | 28 | $self->write($doc); | |||||||||||||
| 98 | 28 | return; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | |||||||||||||||
| 102 | 117 | ▶ | sub write { | ||||||||||||
|
Subroutine
write: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 103 | 117 | my ( $self, $data ) = @_; | |||||||||||||
| 104 | 117 | my $out = $self->out; | |||||||||||||
| 105 | 117 | binmode $out, ':raw'; | |||||||||||||
| 106 | 117 | print {$out} $self->encode($data); | |||||||||||||
| 107 | 117 | $out->flush; | |||||||||||||
| 108 | 117 | return; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | |||||||||||||||
| 112 | 128 | ▶ | sub encode { | ||||||||||||
|
Subroutine
encode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 113 | 128 | my ( $self, $data ) = @_; | |||||||||||||
| 114 | 128 | my $method = '_encode_'.$self->format; | |||||||||||||
| 115 | 128 | return $self->$method($self->normalize($data)); | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | |||||||||||||||
| 119 | 128 | ▶ | sub normalize { | ||||||||||||
|
Subroutine
normalize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 120 | 128 | my ( $self, $data ) = @_; | |||||||||||||
| 121 | 128 | my $json = JSON::MaybeXS->new(canonical => 1, convert_blessed => 1, allow_nonref => 1); | |||||||||||||
| 122 | 128 | return $json->decode($json->encode($data)); | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 79 | ▶ | sub _encode_json { | ||||||||||||
|
Subroutine
_encode_json: called CC=3 SCAR=11.0
| |||||||||||||||
| 126 | 79 | my ( $self, $data ) = @_; | |||||||||||||
| 127 | 79 | ▶ | my $json = JSON::MaybeXS->new(utf8 => 1, canonical => 1, $self->stream ? () : ( pretty => 1 )); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | 79 | ▶ | return $self->stream ? $json->encode($data)."\n" : $json->encode($data); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 29 | ▶ | sub _encode_msgpack { | ||||||||||||
|
Subroutine
_encode_msgpack: called CC=1 SCAR=0.0
| |||||||||||||||
| 132 | 29 | my ( $self, $data ) = @_; | |||||||||||||
| 133 | 29 | return Data::MessagePack->new->canonical(1)->utf8(1)->pack($self->_msgpack_booleans($data)); | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 20 | ▶ | sub _encode_yaml { | ||||||||||||
|
Subroutine
_encode_yaml: called CC=1 SCAR=0.0
| |||||||||||||||
| 137 | 20 | my ( $self, $data ) = @_; | |||||||||||||
| 138 | 20 | return encode_utf8(YAML::PP->new(boolean => 'JSON::PP', header => 1)->dump_string($data)); | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | # Data::MessagePack refuses JSON::PP::Boolean; swap in its own booleans. | ||||||||||||||
| 142 | 250 | ▶ | sub _msgpack_booleans { | ||||||||||||
|
Subroutine
_msgpack_booleans: called CC=5 SCAR=16.2
| |||||||||||||||
| 143 | 250 | my ( $self, $data ) = @_; | |||||||||||||
| 144 | 250 | ▶ | return { map { $_ => $self->_msgpack_booleans($data->{$_}) } keys %$data } if ref $data eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 145 | 206 | ▶ | return [ map { $self->_msgpack_booleans($_) } @$data ] if ref $data eq 'ARRAY'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | 206 | ▶ | return $data ? Data::MessagePack::true() : Data::MessagePack::false() if JSON::MaybeXS::is_bool($data); | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 147 | 196 | return $data; | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | |||||||||||||||
| 150 | 55 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 151 | |||||||||||||||
| 152 | 55 | 1; | |||||||||||||
| 153 | |||||||||||||||
| 154 | __END__ | ||||||||||||||
| 155 | |||||||||||||||
| 156 | =pod | ||||||||||||||
| 157 | |||||||||||||||
| 158 | =encoding UTF-8 | ||||||||||||||
| 159 | |||||||||||||||
| 160 | =head1 NAME | ||||||||||||||
| 161 | |||||||||||||||
| 162 | Langertha::Raider::CLI::Machine - Internal writer of the raider CLI's machine output (JSON, MessagePack, YAML) | ||||||||||||||
| 163 | |||||||||||||||
| 164 | =head1 VERSION | ||||||||||||||
| 165 | |||||||||||||||
| 166 | version 0.503 | ||||||||||||||
| 167 | |||||||||||||||
| 168 | =head1 SYNOPSIS | ||||||||||||||
| 169 | |||||||||||||||
| 170 | # Internal to Langertha-Raider -- no API promise. | ||||||||||||||
| 171 | my $machine = Langertha::Raider::CLI::Machine->new(format => 'json', stream => 1); | ||||||||||||||
| 172 | |||||||||||||||
| 173 | $machine->event('run.started', engine => 'openai', model => 'gpt-4o-mini'); | ||||||||||||||
| 174 | $machine->finish($machine->document(completed => response => 'hi', elapsed => 1)); | ||||||||||||||
| 175 | |||||||||||||||
| 176 | =head1 DESCRIPTION | ||||||||||||||
| 177 | |||||||||||||||
| 178 | B<Internal module.> Its interface may change without notice. | ||||||||||||||
| 179 | |||||||||||||||
| 180 | The machine output of F<raider> (ADR 0013): one format-independent model | ||||||||||||||
| 181 | -- the run's document and, when streaming, the events leading up to it -- | ||||||||||||||
| 182 | written in one of three encodings. The model is built as plain Perl data; | ||||||||||||||
| 183 | L</encode> turns it into octets for the chosen L</format>. The reference of | ||||||||||||||
| 184 | the format itself is the POD of F<raider>. | ||||||||||||||
| 185 | |||||||||||||||
| 186 | Before encoding, the data is normalized through JSON, so a value is a | ||||||||||||||
| 187 | number, a string or a boolean exactly as the JSON encoding would write it -- | ||||||||||||||
| 188 | the same fields and types in every encoding. | ||||||||||||||
| 189 | |||||||||||||||
| 190 | =head2 format | ||||||||||||||
| 191 | |||||||||||||||
| 192 | C<json>, C<msgpack> or C<yaml>. Required. | ||||||||||||||
| 193 | |||||||||||||||
| 194 | =head2 stream | ||||||||||||||
| 195 | |||||||||||||||
| 196 | True for the C<--stream-*> flags: L</event> writes every event, and | ||||||||||||||
| 197 | L</finish> writes the document as the C<run.finished> event. False writes | ||||||||||||||
| 198 | only the document, once, at L</finish>. | ||||||||||||||
| 199 | |||||||||||||||
| 200 | =head2 version | ||||||||||||||
| 201 | |||||||||||||||
| 202 | The format version written into the document and every event. Only C<1> | ||||||||||||||
| 203 | exists; see L</versions>. | ||||||||||||||
| 204 | |||||||||||||||
| 205 | =head2 out | ||||||||||||||
| 206 | |||||||||||||||
| 207 | Filehandle the octets are written to. Defaults to C<STDOUT>. It is switched | ||||||||||||||
| 208 | to C<:raw> before each write. | ||||||||||||||
| 209 | |||||||||||||||
| 210 | =head2 clock | ||||||||||||||
| 211 | |||||||||||||||
| 212 | Code reference returning the current time as epoch seconds, for the C<time> | ||||||||||||||
| 213 | of an event. Defaults to L<Time::HiRes/time>. | ||||||||||||||
| 214 | |||||||||||||||
| 215 | =head2 max_content_length | ||||||||||||||
| 216 | |||||||||||||||
| 217 | How many characters of a C<tool.result>'s text an event carries in | ||||||||||||||
| 218 | C<content>; a longer text is cut and the event flagged C<truncated>. | ||||||||||||||
| 219 | Defaults to C<1000>. | ||||||||||||||
| 220 | |||||||||||||||
| 221 | =head2 versions | ||||||||||||||
| 222 | |||||||||||||||
| 223 | my @known = Langertha::Raider::CLI::Machine->versions; # (1) | ||||||||||||||
| 224 | |||||||||||||||
| 225 | The format versions this raider can write. | ||||||||||||||
| 226 | |||||||||||||||
| 227 | =head2 document | ||||||||||||||
| 228 | |||||||||||||||
| 229 | my $doc = $machine->document(completed => response => $text, metrics => $m, elapsed => $s); | ||||||||||||||
| 230 | my $doc = $machine->document(failed => error => $message, elapsed => $s); | ||||||||||||||
| 231 | |||||||||||||||
| 232 | The document of a finished run: C<version>, C<status> and the given fields. | ||||||||||||||
| 233 | |||||||||||||||
| 234 | =head2 event | ||||||||||||||
| 235 | |||||||||||||||
| 236 | $machine->event('tool.call', name => 'bash', arguments => { command => 'ls' }); | ||||||||||||||
| 237 | |||||||||||||||
| 238 | Writes one event -- the payload plus C<version>, C<type>, C<seq> and C<time> | ||||||||||||||
| 239 | -- when L</stream> is on; does nothing otherwise. The C<content> of a | ||||||||||||||
| 240 | C<tool.result> is cut to L</max_content_length> characters, with | ||||||||||||||
| 241 | C<truncated> telling whether it was. | ||||||||||||||
| 242 | |||||||||||||||
| 243 | =head2 finish | ||||||||||||||
| 244 | |||||||||||||||
| 245 | $machine->finish($doc); | ||||||||||||||
| 246 | |||||||||||||||
| 247 | Writes the run's document: as the C<run.finished> event when streaming (its | ||||||||||||||
| 248 | payload is the document), otherwise as the one document of the run. | ||||||||||||||
| 249 | |||||||||||||||
| 250 | =head2 write | ||||||||||||||
| 251 | |||||||||||||||
| 252 | Encodes the data with L</encode> and writes it to L</out>, flushed. | ||||||||||||||
| 253 | |||||||||||||||
| 254 | =head2 encode | ||||||||||||||
| 255 | |||||||||||||||
| 256 | my $octets = $machine->encode($data); | ||||||||||||||
| 257 | |||||||||||||||
| 258 | The data normalized and encoded for L</format>: a pretty-printed canonical | ||||||||||||||
| 259 | JSON document or one compact line per event, a MessagePack object with text | ||||||||||||||
| 260 | as UTF-8 C<str>, or a YAML document starting with C<--->. | ||||||||||||||
| 261 | |||||||||||||||
| 262 | =head2 normalize | ||||||||||||||
| 263 | |||||||||||||||
| 264 | The data after a round trip through JSON: numbers, strings and booleans | ||||||||||||||
| 265 | (L<JSON::PP::Boolean>) as JSON sees them, text as characters. | ||||||||||||||
| 266 | |||||||||||||||
| 267 | =head1 SEE ALSO | ||||||||||||||
| 268 | |||||||||||||||
| 269 | =over | ||||||||||||||
| 270 | |||||||||||||||
| 271 | =item * L<raider> | ||||||||||||||
| 272 | |||||||||||||||
| 273 | =item * L<Langertha::Raider::CLI::Runner> | ||||||||||||||
| 274 | |||||||||||||||
| 275 | =back | ||||||||||||||
| 276 | |||||||||||||||
| 277 | =head1 SUPPORT | ||||||||||||||
| 278 | |||||||||||||||
| 279 | =head2 Issues | ||||||||||||||
| 280 | |||||||||||||||
| 281 | Please report bugs and feature requests on GitHub at | ||||||||||||||
| 282 | L<https://github.com/Getty/langertha-raider/issues>. | ||||||||||||||
| 283 | |||||||||||||||
| 284 | =head2 IRC | ||||||||||||||
| 285 | |||||||||||||||
| 286 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||
| 287 | |||||||||||||||
| 288 | =head1 CONTRIBUTING | ||||||||||||||
| 289 | |||||||||||||||
| 290 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||
| 291 | |||||||||||||||
| 292 | =head1 AUTHOR | ||||||||||||||
| 293 | |||||||||||||||
| 294 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||
| 295 | |||||||||||||||
| 296 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 297 | |||||||||||||||
| 298 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||
| 299 | |||||||||||||||
| 300 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 301 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 302 | |||||||||||||||
| 303 | =cut | ||||||||||||||