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.503';
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__
152  
153 =pod
154  
155 =encoding UTF-8
156  
157 =head1 NAME
158  
159 Langertha::ImageGen - Image generation abstraction wrapping an engine with optional overrides
160  
161 =head1 VERSION
162  
163 version 0.503
164  
165 =head1 SYNOPSIS
166  
167 use Langertha::Engine::OpenAI;
168 use Langertha::ImageGen;
169  
170 my $engine = Langertha::Engine::OpenAI->new(
171 api_key => $ENV{OPENAI_API_KEY},
172 );
173  
174 my $image_gen = Langertha::ImageGen->new(
175 engine => $engine,
176 model => 'gpt-image-2',
177 size => '1024x1024',
178 quality => 'high',
179 plugins => ['Langfuse'],
180 );
181  
182 my $result = $image_gen->simple_image('A cat riding a bicycle through Paris');
183  
184 =head1 DESCRIPTION
185  
186 C<Langertha::ImageGen> wraps any engine that consumes
187 L<Langertha::Role::ImageGeneration> and adds optional overrides for
188 model, size, and quality, plus plugin lifecycle hooks via
189 L<Langertha::Role::PluginHost>.
190  
191 Use this class when you need multiple image generation configurations
192 from the same engine instance, or when you want plugin observability
193 (e.g. L<Langertha::Plugin::Langfuse>) without modifying the engine.
194  
195 =head2 engine
196  
197 The LLM engine to delegate image generation requests to. Must consume
198 L<Langertha::Role::ImageGeneration>.
199  
200 =head2 model
201  
202 Optional model name override. When set, overrides the engine's
203 C<image_model> via C<%extra> pass-through.
204  
205 =head2 size
206  
207 Optional image size (e.g. C<'1024x1024'>, C<'1536x1024'>).
208  
209 =head2 quality
210  
211 Optional quality setting (e.g. C<'low'>, C<'medium'>, C<'high'> for GPT image models).
212  
213 =head2 simple_image
214  
215 my $result = $image_gen->simple_image('A cat in space');
216  
217 Returns the image generation result for C<$prompt>. If C<model>,
218 C<size>, or C<quality> overrides are set, uses them via C<%extra>;
219 otherwise delegates to the engine's C<simple_image>. Plugin hooks
220 C<plugin_before_image_gen> and C<plugin_after_image_gen> are fired.
221  
222 =head2 simple_image_f
223  
224 my $result = await $image_gen->simple_image_f('A cat in space');
225  
226 Async variant of L</simple_image>: the same result, overrides and plugin
227 hooks, with the hooks awaited and the request sent through the engine's
228 async backend (see L<Langertha::Role::ImageGeneration/simple_image_f>).
229  
230 =head2 simple_image_result
231  
232 my $result = $image_gen->simple_image_result('A cat in space');
233 my $images = $result->value;
234 say $result->usage->output_tokens if $result->has_usage;
235  
236 Like L</simple_image>, with the same overrides and plugin hooks, but returns
237 the L<Langertha::CallResult> of
238 L<Langertha::Role::ImageGeneration/simple_image_result>: C<value> is the
239 image result after the after-hooks, and C<usage>, C<rate_limit>, C<model>
240 and C<total_seconds> are the call's. C<plugin_after_image_gen> gets that
241 C<CallResult> as an extra third argument (see L<Langertha::Plugin>). When a
242 hook returns a different value, the result is a new C<CallResult> with that
243 value and the other attributes copied.
244  
245 =head2 simple_image_result_f
246  
247 my $result = await $image_gen->simple_image_result_f('A cat in space');
248  
249 Async variant of L</simple_image_result>: the same result, overrides and
250 hooks, sent through the engine's async backend like L</simple_image_f>.
251  
252 =head1 SEE ALSO
253  
254 =over
255  
256 =item * L<Langertha::Role::PluginHost> - Plugin system consumed by this class
257  
258 =item * L<Langertha::Role::ImageGeneration> - Image generation role required by the engine
259  
260 =item * L<Langertha::Plugin::Langfuse> - Observability plugin for image generation calls
261  
262 =item * L<Langertha::Chat> - Chat counterpart to this class
263  
264 =item * L<Langertha::Embedder> - Embedding counterpart to this class
265  
266 =back
267  
268 =head1 SUPPORT
269  
270 =head2 Issues
271  
272 Please report bugs and feature requests on GitHub at
273 L<https://github.com/Getty/langertha/issues>.
274  
275 =head2 IRC
276  
277 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
278  
279 =head1 CONTRIBUTING
280  
281 Contributions are welcome! Please fork the repository and submit a pull request.
282  
283 =head1 AUTHOR
284  
285 Torsten Raudssus <getty@cpan.org>
286  
287 =head1 COPYRIGHT AND LICENSE
288  
289 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
290  
291 This is free software; you can redistribute it and/or modify it under
292 the same terms as the Perl 5 programming language system itself.
293  
294 =cut
« blib/lib/Langertha/Role/RuntimeKnobs.pm ↑ Summary blib/lib/Langertha/Embedder.pm »