blib/lib/Langertha/Knarr/Request.pm

filter: Statementstmt 85.5% 65 / 76 Branchbran 85.7% 24 / 28 Conditioncond 72.0% 36 / 50 MC/DCmcdc 73.3% 22 / 30 Subroutinesub 91.6% 22 / 24 Podpod 100% 4 / 4 totaltotal 81.6% 173 / 212 CC 32 SCAR 25.3
CC
32
Cov
81%
apply_grant
30.0
chat_f_args
22.0
forward_header
6.0
CRAP 12.6
« blib/lib/Langertha/Knarr/CLI/Cmd/Models.pm ↑ Summary blib/lib/Langertha/Knarr/Stream.pm »
1 package Langertha::Knarr::Request;
2 # ABSTRACT: Normalized chat request shared across all Knarr protocols
3 21 our $VERSION = '1.103';
4 21 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 has model => (
8 is => 'ro',
9 isa => 'Maybe[Str]',
10 4 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
11 21 writer => '_set_model',
12 );
13  
14 has grant => (
15 is => 'ro',
16 isa => 'Maybe[HashRef]',
17 162 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 21 writer => '_set_grant',
19 );
20  
21 has messages => (
22 is => 'ro',
23 isa => 'ArrayRef[HashRef]',
24 28 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 21 );
26  
27 21 has stream => (
28 is => 'ro',
29 isa => 'Bool',
30 default => 0,
31 );
32  
33 has temperature => (
34 is => 'ro',
35 isa => 'Maybe[Num]',
36 82 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 21 );
38  
39 has max_tokens => (
40 is => 'ro',
41 isa => 'Maybe[Int]',
42 110 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 21 );
44  
45 has reasoning_effort => (
46 is => 'ro',
47 isa => 'Maybe[Str]',
48 85 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 21 );
50  
51 has seed => (
52 is => 'ro',
53 isa => 'Maybe[Int]',
54 119 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
55 21 );
56  
57 has parallel_tool_use => (
58 is => 'ro',
59 isa => 'Maybe[Bool]',
60 146 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
61 21 );
62  
63 has prompt_cache_key => (
64 is => 'ro',
65 isa => 'Maybe[Str]',
66 147 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
67 21 );
68  
69 has tools => (
70 is => 'ro',
71 isa => 'Maybe[ArrayRef]',
72 81 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
73 21 );
74  
75 has tool_choice => (
76 is => 'ro',
77 109 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 21 );
79  
80 has response_format => (
81 is => 'ro',
82 114 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 21 );
84  
85 has system => (
86 is => 'ro',
87 isa => 'Maybe[Str]',
88 128 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 21 );
90  
91 has session_id => (
92 is => 'rw',
93 isa => 'Maybe[Str]',
94 149 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
95 21 );
96  
97 21 has protocol => (
98 is => 'ro',
99 isa => 'Str',
100 required => 1,
101 );
102  
103 has raw => (
104 is => 'ro',
105 isa => 'HashRef',
106 87 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
107 21 );
108  
109 has extra => (
110 is => 'ro',
111 isa => 'HashRef',
112 87 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
113 21 );
114  
115  
116 9 ▶sub forward_header_pairs {
Subroutine forward_header_pairs: called CC=3 SCAR=12.3
Pod documented
117 9 my ($self) = @_;
118 9 my $fwd = $self->extra->{forward_headers};
119 0 ▶ return map { [ @$_ ] } @$fwd if ref $fwd eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $fwd eq "ARRAY" 0 9 9
120 0 ▶ return map { [ $_, $fwd->{$_} ] } sort keys %$fwd if ref $fwd eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $fwd eq "HASH" 0 9 9
121 9 return;
122 }
123  
124  
125 0 ▶sub apply_grant {
Subroutine apply_grant: not called CC=5 SCAR=34.0
Pod documented
126 0 my ($self, $grant) = @_;
127 0 $self->_set_grant($grant);
128 0 my $model = $self->model;
129 $self->_set_model( $grant->{model} )
130 0 ▶ if !( defined $model && length $model ) && $grant && defined $grant->{model};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $model && length $model and $grant and defined $grant->{"model"} 0 0 0
Condition0%0/9
not defined $model && length $model and $grant and defined $grant->{"model"}
!ll&&!rl&&r
000
not defined $model && length $model and $grant
!ll&&!rl&&r
000
defined $model && length $model
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $model && length $model and $grant and defined $grant->{"model"}
defined $model length $model $grant defined $grant->{"model"}
Truth table0%0/7
not(A && B) and C and D
A= defined $modelB= length $modelC= $grantD= defined $grant->{"model"}
ABCDresult
11XX0
0X0X0
100X0
0X100
10100
0X111
10111
131 0 return $self;
132 }
133  
134  
135 0 ▶sub forward_header {
Subroutine forward_header: not called CC=2 SCAR=17.9
Pod documented
136 0 my ($self, $name) = @_;
137 0 return map { $_->[1] }
138 0 ▶ grep { lc $_->[0] eq lc $name && defined $_->[1] } $self->forward_header_pairs;
Condition0%0/3
lc $_->[0] eq lc $name && defined $_->[1]
!ll&&!rl&&r
000
MC/DC0%0/2
lc $_->[0] eq lc $name && defined $_->[1]
lc $_->[0] eq lc $name defined $_->[1]
Truth table0%0/3
A && B
A= lc $_->[0] eq lc $nameB= defined $_->[1]
ABresult
0X0
100
111
139 }
140  
141  
142 # Which capability flag a given response_format value requires. Only an
143 # explicit json_schema type needs the schema flag; json_object, Ollama's
144 # bare 'json' string and any raw-schema HashRef fall back to the object
145 # flag, which is the weaker of the two.
146 7 ▶sub _response_format_cap {
Subroutine _response_format_cap: called CC=4 SCAR=13.9
147 7 my ($rf) = @_;
148 return 'response_format_json_schema'
149 7 ▶ if ref($rf) eq 'HASH' && ( $rf->{type} // '' ) eq 'json_schema';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rf eq "HASH" and ($rf->{"type"} // "") eq "json_schema" 2 5 7
Condition80%4/5
ref $rf eq "HASH" and ($rf->{"type"} // "") eq "json_schema"
!ll&&!rl&&r
232
$rf->{"type"} // ""
l!l
50
MC/DC50%1/2
ref $rf eq "HASH" and ($rf->{"type"} // "") eq "json_schema"
ref $rf eq "HASH" $rf->{"type"}
Truth table66%2/3
A and B
A= ref $rf eq "HASH"B= $rf->{"type"}
ABresult
0X0
100
111
150 5 return 'response_format_json_object';
151 }
152  
153 32 ▶sub chat_f_args {
Subroutine chat_f_args: called CC=22 SCAR=30.9
Pod documented
154 32 my ($self, $engine) = @_;
155 my $supports = $engine && $engine->can('supports')
156 75 ▶ ? sub { $engine->supports($_[0]) }
Subroutine __ANON__: called CC=1 SCAR=0.0
157 32 ▶ : sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$engine && $engine->can('supports') ? : 25 7 32
Condition66%2/3
$engine and $engine->can("supports")
!ll&&!rl&&r
0725
MC/DC50%1/2
$engine and $engine->can("supports")
$engine $engine->can("supports")
Truth table66%2/3
A and B
A= $engineB= $engine->can("supports")
ABresult
0X0
100
111
158 32 my $rf = $self->response_format;
159 32 ▶ my $tools_ok = $supports->('tools_native') || $supports->('tools_hermes');
Condition100%3/3
&$supports('tools_native') || &$supports('tools_hermes')
l!l&&r!l&&!r
12119
MC/DC100%2/2
&$supports('tools_native') || &$supports('tools_hermes')
&$supports('tools_native') &$supports('tools_hermes')
Truth table100%3/3
A || B
A= &$supports('tools_native')B= &$supports('tools_hermes')
ABresult
1X1
011
000
160 32 my @args = ( messages => $self->messages );
161 32 ▶ push @args, tools => $self->tools if $self->tools && $tools_ok;
Branch: 1 branch
BranchTrueFalseTotal
if $self->tools and $tools_ok 4 28 32
Condition100%3/3
$self->tools and $tools_ok
!ll&&!rl&&r
2624
MC/DC100%2/2
$self->tools and $tools_ok
$self->tools $tools_ok
Truth table100%3/3
A and B
A= $self->toolsB= $tools_ok
ABresult
0X0
100
111
162 32 ▶ push @args, tool_choice => $self->tool_choice if defined $self->tool_choice && $tools_ok;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->tool_choice and $tools_ok 4 28 32
Condition100%3/3
defined $self->tool_choice and $tools_ok
!ll&&!rl&&r
2624
MC/DC100%2/2
defined $self->tool_choice and $tools_ok
defined $self->tool_choice $tools_ok
Truth table100%3/3
A and B
A= defined $self->tool_choiceB= $tools_ok
ABresult
0X0
100
111
163 32 ▶ push @args, response_format => $rf if defined $rf && $supports->( _response_format_cap($rf) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rf and &$supports(_response_format_cap($rf)) 3 29 32
Condition100%3/3
defined $rf and &$supports(_response_format_cap($rf))
!ll&&!rl&&r
2543
MC/DC100%2/2
defined $rf and &$supports(_response_format_cap($rf))
defined $rf &$supports(_response_format_cap($rf))
Truth table100%3/3
A and B
A= defined $rfB= &$supports(_response_format_cap($rf))
ABresult
0X0
100
111
164 32 ▶ push @args, temperature => $self->temperature if defined $self->temperature && $supports->('temperature');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->temperature and &$supports("temperature") 4 28 32
Condition100%3/3
defined $self->temperature and &$supports("temperature")
!ll&&!rl&&r
2714
MC/DC100%2/2
defined $self->temperature and &$supports("temperature")
defined $self->temperature &$supports("temperature")
Truth table100%3/3
A and B
A= defined $self->temperatureB= &$supports("temperature")
ABresult
0X0
100
111
165 32 ▶ push @args, max_tokens => $self->max_tokens if defined $self->max_tokens && $supports->('response_size');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->max_tokens and &$supports("response_size") 4 28 32
Condition100%3/3
defined $self->max_tokens and &$supports("response_size")
!ll&&!rl&&r
2534
MC/DC100%2/2
defined $self->max_tokens and &$supports("response_size")
defined $self->max_tokens &$supports("response_size")
Truth table100%3/3
A and B
A= defined $self->max_tokensB= &$supports("response_size")
ABresult
0X0
100
111
166 32 ▶ push @args, reasoning_effort => $self->reasoning_effort if defined $self->reasoning_effort && $supports->('reasoning_effort');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->reasoning_effort and &$supports("reasoning_effort") 2 30 32
Condition100%3/3
defined $self->reasoning_effort and &$supports("reasoning_effort")
!ll&&!rl&&r
2822
MC/DC100%2/2
defined $self->reasoning_effort and &$supports("reasoning_effort")
defined $self->reasoning_effort &$supports("reasoning_effort")
Truth table100%3/3
A and B
A= defined $self->reasoning_effortB= &$supports("reasoning_effort")
ABresult
0X0
100
111
167 32 ▶ push @args, seed => $self->seed if defined $self->seed && $supports->('seed');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->seed and &$supports("seed") 1 31 32
Condition100%3/3
defined $self->seed and &$supports("seed")
!ll&&!rl&&r
3011
MC/DC100%2/2
defined $self->seed and &$supports("seed")
defined $self->seed &$supports("seed")
Truth table100%3/3
A and B
A= defined $self->seedB= &$supports("seed")
ABresult
0X0
100
111
168 32 ▶ push @args, parallel_tool_use => $self->parallel_tool_use if defined $self->parallel_tool_use && $supports->('parallel_tool_use');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->parallel_tool_use and &$supports("parallel_tool_use") 2 30 32
Condition100%3/3
defined $self->parallel_tool_use and &$supports("parallel_tool_use")
!ll&&!rl&&r
2912
MC/DC100%2/2
defined $self->parallel_tool_use and &$supports("parallel_tool_use")
defined $self->parallel_tool_use &$supports("parallel_tool_use")
Truth table100%3/3
A and B
A= defined $self->parallel_tool_useB= &$supports("parallel_tool_use")
ABresult
0X0
100
111
169 32 ▶ push @args, prompt_cache_key => $self->prompt_cache_key if defined $self->prompt_cache_key && $supports->('prompt_cache_key');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->prompt_cache_key and &$supports("prompt_cache_key") 1 31 32
Condition100%3/3
defined $self->prompt_cache_key and &$supports("prompt_cache_key")
!ll&&!rl&&r
3011
MC/DC100%2/2
defined $self->prompt_cache_key and &$supports("prompt_cache_key")
defined $self->prompt_cache_key &$supports("prompt_cache_key")
Truth table100%3/3
A and B
A= defined $self->prompt_cache_keyB= &$supports("prompt_cache_key")
ABresult
0X0
100
111
170 32 return @args;
171 }
172  
173 21 __PACKAGE__->meta->make_immutable;
174 21 1;
175  
176 __END__
« blib/lib/Langertha/Knarr/CLI/Cmd/Models.pm ↑ Summary blib/lib/Langertha/Knarr/Stream.pm »