Langertha/Raid.pm

filter: Statementstmt 87.2% 82 / 94 Branchbran 43.7% 14 / 32 Conditioncond 34.7% 8 / 23 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 87.5% 14 / 16 Podpod 50.0% 1 / 2 totaltotal 67.2% 121 / 180 CC 28 SCAR 34.2
CC
28
Cov
70%
_normalize_result
100.0
_run_steps_sequentially_f
7.0
_validate_steps
4.3
CRAP 30.5
« Langertha/Engine/Gemini.pm ↑ Summary Langertha/Engine/OpenAIResponses.pm »
1 package Langertha::Raid;
2 # ABSTRACT: Base class for orchestrating Runnable steps
3 2 our $VERSION = '0.502';
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Langertha::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Langertha::RunContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 with 'Langertha::Role::Runnable';
13  
14  
15 has steps => (
16 is => 'ro',
17 isa => 'ArrayRef',
18 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 2 );
20  
21  
22 2 has name => (
23 is => 'ro',
24 isa => 'Str',
25 predicate => 'has_name',
26 );
27  
28  
29 sub BUILD {
30 16 ▶ my ( $self ) = @_;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
31 16 $self->_validate_steps($self->steps);
32 }
33  
34 sub _validate_steps {
35 16 ▶ my ( $self, $steps ) = @_;
Subroutine _validate_steps: called CC=4 SCAR=14.6
36 16 for my $idx (0..$#{$steps}) {
37 26 my $step = $steps->[$idx];
38 26 ▶ croak "Invalid step at index $idx: expected Runnable object"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($step) and $step->can("run_f") 0 26 26
Condition33%1/3
builtin::blessed($step) and $step->can("run_f")
!ll&&!rl&&r
0026
MC/DC0%0/2
builtin::blessed($step) and $step->can("run_f")
builtin::blessed($step) $step->can("run_f")
Truth table33%1/3
A and B
A= builtin::blessed($step)B= $step->can("run_f")
ABresult
0X0
100
111
39 unless blessed($step) && $step->can('run_f');
40 }
41 16 return;
42 }
43  
44  
45 sub _coerce_context {
46 17 ▶ my ( $self, $ctx ) = @_;
Subroutine _coerce_context: called CC=3 SCAR=14.2
47 17 ▶ return $ctx if blessed($ctx) && $ctx->isa('Langertha::RunContext');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($ctx) and $ctx->isa("Langertha::RunContext") 17 0 17
Condition33%1/3
builtin::blessed($ctx) and $ctx->isa("Langertha::RunContext")
!ll&&!rl&&r
0017
MC/DC0%0/2
builtin::blessed($ctx) and $ctx->isa("Langertha::RunContext")
builtin::blessed($ctx) $ctx->isa("Langertha::RunContext")
Truth table33%1/3
A and B
A= builtin::blessed($ctx)B= $ctx->isa("Langertha::RunContext")
ABresult
0X0
100
111
48 0 return Langertha::RunContext->new(input => $ctx);
49 }
50  
51  
52 sub _step_label {
53 30 ▶ my ( $self, $step, $idx ) = @_;
Subroutine _step_label: called CC=3 SCAR=11.0
54 30 ▶ return $step->name if $step->can('name') && defined $step->name;
Branch: 1 branch
BranchTrueFalseTotal
if $step->can("name") and defined $step->name 23 7 30
Condition100%3/3
$step->can("name") and defined $step->name
!ll&&!rl&&r
3423
MC/DC100%2/2
$step->can("name") and defined $step->name
$step->can("name") defined $step->name
Truth table100%3/3
A and B
A= $step->can("name")B= defined $step->name
ABresult
0X0
100
111
55 7 return ref($step) . "[$idx]";
56 }
57  
58 sub _normalize_result {
59 27 ▶ my ( $self, $raw ) = @_;
Subroutine _normalize_result: called CC=12 SCAR=46.0
60  
61 27 ▶ if (blessed($raw) && $raw->isa('Langertha::Result')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($raw) and $raw->isa("Langertha::Result")) 27 0 27
Condition33%1/3
builtin::blessed($raw) and $raw->isa("Langertha::Result")
!ll&&!rl&&r
0027
MC/DC0%0/2
builtin::blessed($raw) and $raw->isa("Langertha::Result")
builtin::blessed($raw) $raw->isa("Langertha::Result")
Truth table33%1/3
A and B
A= builtin::blessed($raw)B= $raw->isa("Langertha::Result")
ABresult
0X0
100
111
62 27 return $raw;
63 }
64  
65 0 ▶ if (!ref($raw)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $raw) 0 0 0
66 0 return Langertha::Result->final($raw);
67 }
68  
69 0 ▶ if (ref($raw) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $raw eq "HASH") 0 0 0
70 my %args = (
71 type => ($raw->{type} // 'final'),
72 (exists $raw->{text} ? (text => $raw->{text}) : ()),
73 (exists $raw->{content} ? (content => $raw->{content}) : ()),
74 (exists $raw->{options} ? (options => $raw->{options}) : ()),
75 0 ▶ (exists $raw->{data} ? (data => $raw->{data}) : ()),
Branch: 4 branches
BranchTrueFalseTotal
exists $$raw{'text'} ? : 0 0 0
exists $$raw{'content'} ? : 0 0 0
exists $$raw{'options'} ? : 0 0 0
exists $$raw{'data'} ? : 0 0 0
Condition0%0/2
$$raw{"type"} // "final"
l!l
00
MC/DC0%0/1
$$raw{"type"} // "final"
$$raw{"type"}
Truth table0%0/2
A
A= $$raw{"type"}
Aresult
11
00
76 );
77 0 return Langertha::Result->new(%args);
78 }
79  
80 0 ▶ croak "Unsupported step result type from ".(blessed($raw) || ref($raw) || 'scalar');
Condition0%0/5
builtin::blessed($raw) || ref $raw || 'scalar'
l!l
00
builtin::blessed($raw) or ref $raw
l!l&&r!l&&!r
000
MC/DC0%0/2
builtin::blessed($raw) || ref $raw || 'scalar'
builtin::blessed($raw) ref $raw
Truth table0%0/3
A or B
A= builtin::blessed($raw)B= ref $raw
ABresult
1X1
011
000
81 }
82  
83  
84 sub _with_context_result {
85 26 ▶ my ( $self, $result, $ctx ) = @_;
Subroutine _with_context_result: called CC=2 SCAR=9.2
86 26 ▶ return $result->with_context($ctx)
Branch: 1 branch
BranchTrueFalseTotal
if $result->can("with_context") 26 0 26
87 if $result->can('with_context');
88  
89 return Langertha::Result->new(
90 0 %{$result->as_hash},
91 context => $ctx,
92 );
93 }
94  
95 sub _apply_final_result_to_context {
96 19 ▶ my ( $self, $ctx, $result, %extra ) = @_;
Subroutine _apply_final_result_to_context: called CC=3 SCAR=11.1
97 19 ▶ if ($result->has_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->has_text) 19 0 19
98 19 $ctx->input($result->text);
99 19 $ctx->state->{last_output} = $result->text;
100 }
101 19 $ctx->state->{last_result_type} = $result->type;
102 19 $ctx->state->{last_result} = $result->as_hash;
103 19 ▶ if ($extra{step_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra{"step_name"}) 19 0 19
104 19 $ctx->metadata->{last_step_name} = $extra{step_name};
105 }
106 19 return $ctx;
107 }
108  
109 16 ▶async sub _run_steps_sequentially_f {
Subroutine _run_steps_sequentially_f: called CC=7 SCAR=19.5
110 16 my ( $self, $ctx, %opts ) = @_;
111 16 my @steps = @{$self->steps};
112  
113 16 ▶ my $last = Langertha::Result->final($ctx->input // '');
Condition50%1/2
$ctx->input // ""
l!l
160
MC/DC0%0/1
$ctx->input // ""
$ctx->input
Truth table50%1/2
A
A= $ctx->input
Aresult
11
00
114  
115 16 for my $idx (0..$#steps) {
116 21 my $step = $steps[$idx];
117 21 my $step_name = $self->_step_label($step, $idx);
118 21 my $raw;
119  
120 21 my $ok = eval {
121 21 $raw = await $step->run_f($ctx);
122 19 1;
123 };
124  
125 21 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 19 21
126 2 ▶ my $err = $@ || 'Unknown step error';
Condition50%1/2
$@ || 'Unknown step error'
l!l
20
MC/DC0%0/1
$@ || 'Unknown step error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
127 2 chomp $err;
128 2 my $abort = Langertha::Result->abort("Step '$step_name' failed: $err");
129 2 $ctx->add_trace({
130 node => ref($self),
131 event => 'step_error',
132 step_index => $idx,
133 step_name => $step_name,
134 result_type => 'abort',
135 });
136 2 $ctx->state->{last_result_type} = 'abort';
137 2 return $self->_with_context_result($abort, $ctx);
138 }
139  
140 19 my $result = $self->_normalize_result($raw);
141 $ctx->add_trace({
142 node => ref($self),
143 event => 'step_result',
144 step_index => $idx,
145 step_name => $step_name,
146 result_type => $result->type,
147 19 ▶ ($opts{loop_iteration} ? (loop_iteration => $opts{loop_iteration}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$opts{'loop_iteration'} ? : 8 11 19
148 });
149  
150 19 ▶ if ($result->is_final) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_final) 17 2 19
151 17 $self->_apply_final_result_to_context($ctx, $result, step_name => $step_name);
152 17 $last = $result;
153 17 next;
154 }
155  
156 2 $ctx->state->{last_result_type} = $result->type;
157 2 $ctx->state->{last_result} = $result->as_hash;
158 2 return $self->_with_context_result($result, $ctx);
159 }
160  
161 12 return $self->_with_context_result($last, $ctx);
162 2 }
163  
164  
165 0 ▶async sub run_f {
Subroutine run_f: not called CC=1 SCAR=6.9
Pod documented
166 0 croak ref($_[0])." is abstract; use a Raid subclass";
167 2 }
168  
169  
170 2 __PACKAGE__->meta->make_immutable;
171  
172 2 1;
173  
174 __END__
175  
176 =pod
177  
178 =encoding UTF-8
179  
180 =head1 NAME
181  
182 Langertha::Raid - Base class for orchestrating Runnable steps
183  
184 =head1 VERSION
185  
186 version 0.502
187  
188 =head1 SYNOPSIS
189  
190 my $raid = Langertha::Raid::Sequential->new(
191 steps => [ $raider_a, $raider_b ],
192 );
193  
194 my $ctx = Langertha::RunContext->new(input => 'start');
195 my $result = await $raid->run_f($ctx);
196  
197 =head1 DESCRIPTION
198  
199 Abstract orchestration base class for runnable workflows. Holds child steps
200 that implement C<run_f($ctx)> and provides shared helpers for context
201 coercion, step validation, result normalization, and sequential step driving.
202  
203 =head2 steps
204  
205 Ordered child nodes. Each step must implement C<run_f>.
206  
207 =head2 name
208  
209 Optional human-friendly node name used in traces and diagnostics.
210  
211 =head2 _validate_steps
212  
213 Internal step validation used at construction.
214  
215 =head2 _coerce_context
216  
217 Internal helper that normalizes scalar/array input into L<Langertha::RunContext>.
218  
219 =head2 _normalize_result
220  
221 Internal helper to normalize scalar/hash/object returns into L<Langertha::Result>.
222  
223 =head2 _run_steps_sequentially_f
224  
225 Internal sequential executor used by sequential/loop orchestrators.
226 Stops early and propagates C<question>, C<pause>, and C<abort>.
227  
228 =head2 run_f
229  
230 Abstract execution method required by C<Langertha::Role::Runnable>.
231 Concrete subclasses must implement it.
232  
233 =head1 SUPPORT
234  
235 =head2 Issues
236  
237 Please report bugs and feature requests on GitHub at
238 L<https://github.com/Getty/langertha/issues>.
239  
240 =head2 IRC
241  
242 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
243  
244 =head1 CONTRIBUTING
245  
246 Contributions are welcome! Please fork the repository and submit a pull request.
247  
248 =head1 AUTHOR
249  
250 Torsten Raudssus <getty@cpan.org>
251  
252 =head1 COPYRIGHT AND LICENSE
253  
254 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
255  
256 This is free software; you can redistribute it and/or modify it under
257 the same terms as the Perl 5 programming language system itself.
258  
259 =cut
« Langertha/Engine/Gemini.pm ↑ Summary Langertha/Engine/OpenAIResponses.pm »