blib/lib/Langertha/ImageGen.pm

filter: Statementstmt 100% 85 / 85 Branchbran 92.3% 24 / 26 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 93.8% 138 / 147 CC 20 SCAR 11.5
CC
20
Cov
95%
simple_image
5.0
_with_hooked_value
4.2
_extra
4.0
CRAP 3.2
« blib/lib/Langertha/Role/RuntimeKnobs.pm ↑ Summary blib/lib/Langertha/Embedder.pm »
1 package Langertha::ImageGen;
2 # ABSTRACT: Image generation abstraction wrapping an engine with optional overrides
3 6 our $VERSION = '0.505';
4 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 with 'Langertha::Role::PluginHost';
11  
12  
13 6 has engine => (
14 is => 'ro',
15 required => 1,
16 );
17  
18 6 has model => (
19 is => 'ro',
20 isa => 'Str',
21 predicate => 'has_model',
22 );
23  
24 6 has size => (
25 is => 'ro',
26 isa => 'Str',
27 predicate => 'has_size',
28 );
29  
30 6 has quality => (
31 is => 'ro',
32 isa => 'Str',
33 predicate => 'has_quality',
34 );
35  
36  
37 11 ▶sub _extra {
Subroutine _extra: called CC=4 SCAR=13.9
38 11 my ( $self ) = @_;
39 return (
40 11 ▶ ($self->has_model ? (model => $self->model) : ()),
Branch: 3 branches
BranchTrueFalseTotal
$self->has_model ? : 6 5 11
$self->has_size ? : 7 4 11
$self->has_quality ? : 4 7 11
41 ($self->has_size ? (size => $self->size) : ()),
42 ($self->has_quality ? (quality => $self->quality) : ()),
43 );
44 }
45  
46 14 ▶sub _assert_image_engine {
Subroutine _assert_image_engine: called CC=2 SCAR=6.9
47 14 my ( $self ) = @_;
48 14 my $engine = $self->engine;
49 14 ▶ croak ref($engine) . " does not support image generation"
Branch: 1 branch
BranchTrueFalseTotal
unless $engine->does("Langertha::Role::ImageGeneration") 1 13 14
50 unless $engine->does('Langertha::Role::ImageGeneration');
51 13 return $engine;
52 }
53  
54 # --- Plugin hook runners (async) ---
55  
56 13 ▶async sub _run_plugin_before_image_gen {
Subroutine _run_plugin_before_image_gen: called CC=3 SCAR=11.0
57 13 my ( $self, $prompt ) = @_;
58 13 for my $plugin (@{$self->_plugin_instances}) {
59 7 ▶ if ($plugin->can('plugin_before_image_gen')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("plugin_before_image_gen")) 5 2 7
60 5 $prompt = await $plugin->plugin_before_image_gen($prompt);
61 }
62 }
63 13 return $prompt;
64 6 }
65  
66 # $call_result (optional) is the Langertha::CallResult of the *_result path;
67 # every hook gets the same one, while the images are piped hook to hook.
68 13 ▶async sub _run_plugin_after_image_gen {
Subroutine _run_plugin_after_image_gen: called CC=4 SCAR=13.9
69 13 my ( $self, $prompt, $result, $call_result ) = @_;
70 13 for my $plugin (@{$self->_plugin_instances}) {
71 7 ▶ if ($plugin->can('plugin_after_image_gen')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("plugin_after_image_gen")) 6 1 7
72 6 ▶ $result = await $plugin->plugin_after_image_gen($prompt, $result,
Branch: 1 branch
BranchTrueFalseTotal
defined $call_result ? : 3 3 6
73 defined $call_result ? ($call_result) : ());
74 }
75 }
76 13 return $result;
77 6 }
78  
79 # The engine's CallResult when the hooks returned its own images, else a new
80 # one carrying the hooks' value (CallResult is immutable).
81 4 ▶sub _with_hooked_value {
Subroutine _with_hooked_value: called CC=4 SCAR=14.5
82 4 my ( $call_result, $value ) = @_;
83 4 my $old = $call_result->value;
84 4 ▶ return $call_result if ref $value && ref $old && refaddr($value) == refaddr($old);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and ref $old and builtin::refaddr($value) == builtin::refaddr($old) 3 1 4
Condition50%3/6
ref $value and ref $old and builtin::refaddr($value) == builtin::refaddr($old)
!ll&&!rl&&r
013
ref $value and ref $old
!ll&&!rl&&r
004
MC/DC33%1/3
ref $value and ref $old and builtin::refaddr($value) == builtin::refaddr($old)
ref $value ref $old builtin::refaddr($value) == builtin::refaddr($old)
Truth table50%2/4
A and B and C
A= ref $valueB= ref $oldC= builtin::refaddr($value) == builtin::refaddr($old)
ABCresult
0XX0
10X0
1100
1111
85 1 return $call_result->with_value($value);
86 }
87  
88 9 ▶sub simple_image {
Subroutine simple_image: called CC=5 SCAR=16.1
Pod documented
89 9 my ( $self, $prompt ) = @_;
90 9 ▶ $log->debugf("[ImageGen] simple_image via %s, model=%s",
Branch: 1 branch
BranchTrueFalseTotal
$self->has_model ? : 2 7 9
91 ref $self->engine, $self->has_model ? $self->model : 'default');
92 9 my $engine = $self->_assert_image_engine;
93  
94 8 $prompt = $self->_run_plugin_before_image_gen($prompt)->get;
95  
96 8 my $result;
97 8 ▶ if ($self->has_model || $self->has_size || $self->has_quality) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_model or $self->has_size or $self->has_quality) { } 4 4 8
Condition83%5/6
$self->has_model or $self->has_size or $self->has_quality
l!l&&r!l&&!r
404
$self->has_model or $self->has_size
l!l&&r!l&&!r
224
MC/DC66%2/3
$self->has_model or $self->has_size or $self->has_quality
$self->has_model $self->has_size $self->has_quality
Truth table75%3/4
A or B or C
A= $self->has_modelB= $self->has_sizeC= $self->has_quality
ABCresult
1XX1
01X1
0011
0000
98 4 my $request = $engine->image_request($prompt, $self->_extra);
99 4 my $response = $engine->user_agent->request($request);
100 4 $result = $request->response_call->($response);
101 } else {
102 4 $result = $engine->simple_image($prompt);
103 }
104  
105 8 $result = $self->_run_plugin_after_image_gen($prompt, $result)->get;
106  
107 8 return $result;
108 }
109  
110  
111 1 ▶async sub simple_image_f {
Subroutine simple_image_f: called CC=2 SCAR=7.0
Pod documented
112 1 my ( $self, $prompt ) = @_;
113 1 ▶ $log->debugf("[ImageGen] simple_image_f via %s, model=%s",
Branch: 1 branch
BranchTrueFalseTotal
$self->has_model ? : 1 0 1
114 ref $self->engine, $self->has_model ? $self->model : 'default');
115 1 my $engine = $self->_assert_image_engine;
116 1 $prompt = await $self->_run_plugin_before_image_gen($prompt);
117 1 my $result = await $engine->simple_image_f($prompt, $self->_extra);
118 1 return await $self->_run_plugin_after_image_gen($prompt, $result);
119 6 }
120  
121  
122 1 ▶sub simple_image_result {
Subroutine simple_image_result: called CC=2 SCAR=7.0
Pod documented
123 1 my ( $self, $prompt ) = @_;
124 1 ▶ $log->debugf("[ImageGen] simple_image_result via %s, model=%s",
Branch: 1 branch
BranchTrueFalseTotal
$self->has_model ? : 0 1 1
125 ref $self->engine, $self->has_model ? $self->model : 'default');
126 1 my $engine = $self->_assert_image_engine;
127 1 $prompt = $self->_run_plugin_before_image_gen($prompt)->get;
128 1 my $call_result = $engine->simple_image_result($prompt, $self->_extra);
129 1 my $images = $self->_run_plugin_after_image_gen($prompt, $call_result->value, $call_result)->get;
130 1 return _with_hooked_value($call_result, $images);
131 }
132  
133  
134 3 ▶async sub simple_image_result_f {
Subroutine simple_image_result_f: called CC=2 SCAR=6.9
Pod documented
135 3 my ( $self, $prompt ) = @_;
136 3 ▶ $log->debugf("[ImageGen] simple_image_result_f via %s, model=%s",
Branch: 1 branch
BranchTrueFalseTotal
$self->has_model ? : 2 1 3
137 ref $self->engine, $self->has_model ? $self->model : 'default');
138 3 my $engine = $self->_assert_image_engine;
139 3 $prompt = await $self->_run_plugin_before_image_gen($prompt);
140 3 my $call_result = await $engine->simple_image_result_f($prompt, $self->_extra);
141 3 my $images = await $self->_run_plugin_after_image_gen($prompt, $call_result->value, $call_result);
142 3 return _with_hooked_value($call_result, $images);
143 6 }
144  
145  
146  
147 6 __PACKAGE__->meta->make_immutable;
148  
149 6 1;
150  
151 __END__
« blib/lib/Langertha/Role/RuntimeKnobs.pm ↑ Summary blib/lib/Langertha/Embedder.pm »