blib/lib/Langertha/Engine/DeepSeek.pm

filter: Statementstmt 100% 50 / 50 Branchbran 92.3% 24 / 26 Conditioncond 78.9% 15 / 19 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 12 / 12 Podpod 33.3% 1 / 3 totaltotal 90.0% 108 / 120 CC 21 SCAR 19.6
CC
21
Cov
94%
reasoning_kwargs_for
13.0
_is_deepseek_v3
6.2
__ANON__
3.0
CRAP 7.1
« blib/lib/Langertha/Reasoning/BudgetPolicy.pm ↑ Summary blib/lib/Langertha/Virtual/Rules.pm »
1 package Langertha::Engine::DeepSeek;
2 # ABSTRACT: DeepSeek API
3 18 our $VERSION = '0.505';
4 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Langertha::Reasoning;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 18 extends 'Langertha::Engine::OpenAIBase';
9  
10 # StreamUsage: stream_options.include_usage is in the create-chat-completion
11 # reference; the last chunk carries the usage either way (ADR 0038).
12 18 with 'Langertha::Role::Tools',
13 'Langertha::Role::StreamUsage';
14  
15  
16 67 ▶sub _build_supported_operations {[qw(
Subroutine _build_supported_operations: called CC=1 SCAR=0.0
17 createChatCompletion
18 )]}
19  
20 has '+url' => (
21 lazy => 1,
22 28 ▶ default => sub { 'https://api.deepseek.com' },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 18 );
24  
25 1 ▶sub _build_api_key {
Subroutine _build_api_key: called CC=2 SCAR=7.2
26 1 my ( $self ) = @_;
27 return $ENV{LANGERTHA_DEEPSEEK_API_KEY}
28 1 ▶ || croak "".(ref $self)." requires LANGERTHA_DEEPSEEK_API_KEY or api_key set";
Condition50%1/2
$ENV{'LANGERTHA_DEEPSEEK_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_DEEPSEEK_API_KEY or api_key set')
l!l
10
MC/DC0%0/1
$ENV{'LANGERTHA_DEEPSEEK_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_DEEPSEEK_API_KEY or api_key set')
$ENV{'LANGERTHA_DEEPSEEK_API_KEY'}
Truth table50%1/2
A
A= $ENV{'LANGERTHA_DEEPSEEK_API_KEY'}
Aresult
11
00
29 }
30  
31 21 ▶sub default_model { 'deepseek-flash' }
Subroutine default_model: called CC=1 SCAR=0.0
Pod documented
32  
33 # image_input (k266, k280, ADR 0019 k266 Update): deepseek-flash is
34 # DeepSeek-V4.1-Flash, natively multimodal since the 2026-09-10 release (news
35 # 260910; pricing table "Vision: yes"). The legacy ids deepseek-v4-flash and
36 # deepseek-v4-flash-vision-exp are served by V4.1-Flash too (pricing note,
37 # /guides/vision). deepseek-v4-pro (V4-Pro-0813) is "Vision: not supported";
38 # its temporary routing to V4.1-Flash since 2026-09-14 is not a capability
39 # and is deliberately not claimed. Other ids are unchecked, so the catch-all
40 # first row clears the flag (llm-advisor, docs only, read 2026-09-25).
41 202 ▶sub model_capability_corrections {
Subroutine model_capability_corrections: called CC=1 SCAR=0.0
Pod undocumented
42 return (
43 202 qr/\A/ => { image_input => 0 },
44 'deepseek-flash' => { image_input => 1 },
45 qr/\Adeepseek-v4-flash(?:-vision-exp)?\z/ => { image_input => 1 },
46 );
47 }
48  
49 # DeepSeek's response_format.type enum is [text, json_object] only — there is
50 # no json_schema on the standard endpoint (api-docs.deepseek.com, verified
51 # 2026-09-01; schema enforcement instead needs a forced tool with strict:true
52 # on the /beta base URL). Clear the json_schema flag so chat_f routes a
53 # json_schema request through the forced-tool path rather than shipping a
54 # response_format the wire rejects; json_object stays.
55 # parallel_tool_calls is not in the chat/completions schema, and DeepSeek's own
56 # Responses guide calls it "Ignored (parallel tool calling is always enabled)":
57 # clear parallel_tool_use so a parallel_tool_use=0 is dropped with a carp
58 # instead of sent and ignored (karr k242, docs only).
59 202 ▶around engine_capabilities => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
60 202 my ( $orig, $self, @rest ) = @_;
61 202 my $caps = $self->$orig(@rest);
62 202 delete @{$caps}{ qw( response_format_json_schema parallel_tool_use ) };
63 202 return $caps;
64 18 };
65  
66 # Reasoning effort diverges by DeepSeek model within the shared openai wire
67 # format: the current V4 generation (deepseek-flash, deepseek-v4-pro, and the
68 # temporarily-routed deepseek-v4-* aliases) takes a flat reasoning_effort
69 # string; the legacy V3.2 line used a thinking:{type:enabled} toggle instead.
70 # Match the V3.2 family by explicit prefix — anchored, so `deepseek-v3`,
71 # `deepseek-v3.2`, `deepseek-v3.2-exp` (etc.) hit the legacy toggle, and
72 # retired aliases / unknown future ids default to V4 (the safe current
73 # default). A bare `/v3/i` regex was rejected: it matched too widely and
74 # could collide with future V3.x / V3.x-y naming. (V3.2 mapping flagged for
75 # live re-verify.)
76 112 ▶sub _is_deepseek_v3 {
Subroutine _is_deepseek_v3: called CC=6 SCAR=18.2
77 112 my ( $model ) = @_;
78 112 ▶ return 0 unless defined $model && length $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model and length $model 0 112 112
Condition33%1/3
defined $model and length $model
!ll&&!rl&&r
00112
MC/DC0%0/2
defined $model and length $model
defined $model length $model
Truth table33%1/3
A and B
A= defined $modelB= length $model
ABresult
0X0
100
111
79 112 ▶ return 1 if $model eq 'deepseek-v3' || $model eq 'deepseek-v3.2';
Branch: 1 branch
BranchTrueFalseTotal
if $model eq "deepseek-v3" or $model eq "deepseek-v3.2" 38 74 112
Condition100%3/3
$model eq "deepseek-v3" or $model eq "deepseek-v3.2"
l!l&&r!l&&!r
13774
MC/DC100%2/2
$model eq "deepseek-v3" or $model eq "deepseek-v3.2"
$model eq "deepseek-v3" $model eq "deepseek-v3.2"
Truth table100%3/3
A or B
A= $model eq "deepseek-v3"B= $model eq "deepseek-v3.2"
ABresult
1X1
011
000
80 74 ▶ return 1 if $model =~ /\Adeepseek-v3(?:\.\d+)?(?:-[A-Za-z0-9._-]+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $model =~ /\Adeepseek-v3(?:\.\d+)?(?:-[A-Za-z0-9._-]+)?\z/ 1 73 74
81 73 return 0;
82 }
83  
84 # Effort set per api-docs.deepseek.com/api/create-chat-completion (verified
85 # 2026-09-14): the chat-completion endpoint serving deepseek-flash and
86 # deepseek-v4-pro accepts reasoning_effort none|low|high|max (server default
87 # high) with NO per-model difference; none disables thinking. The former
88 # deepseek-v4-pro-only high|max clamp (drop low) was reversed by DeepSeek —
89 # V4 Pro service continues unchanged after 2026-09-14 — and is gone. Unknown
90 # future V4 ids get the same flat set.
91 #
92 # This override replaces Role::ReasoningEffort::reasoning_kwargs_for and so
93 # bypasses its supports()-gate (karr k204, ADR 0009): harmless while DeepSeek
94 # never clears reasoning_effort -- if it ever does, add the same gate here.
95 110 ▶sub reasoning_kwargs_for {
Subroutine reasoning_kwargs_for: called CC=13 SCAR=25.7
Pod undocumented
96 110 my ( $self, %args ) = @_;
97 # A per-request reasoning_effort control (chat_f, karr #46) beats the
98 # engine attribute; the rest of the model-gated placement is unchanged. The
99 # shared resolver also turns an off switch (thinking_enabled => 0, karr
100 # k382) into 'none'.
101 110 my %reasoning = $self->_reasoning_controls_for(%args);
102 # No Langertha::Reasoning is built here, so its BUILD check would not run;
103 # check explicitly, so a layer contradicting itself (effort high +
104 # thinking_enabled 0) croaks with the message every other engine gives
105 # (karr k382).
106 110 Langertha::Reasoning->assert_switch_agrees( $reasoning{thinking_enabled}, $reasoning{effort} );
107 106 my $e = $reasoning{effort};
108 106 ▶ my $model = $self->can('chat_model') ? ( $self->chat_model // '' ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->can('chat_model') ? : 106 0 106
Condition50%1/2
$self->chat_model // ""
l!l
1060
MC/DC0%0/1
$self->chat_model // ""
$self->chat_model
Truth table50%1/2
A
A= $self->chat_model
Aresult
11
00
109 # The V3.x toggle: `thinking.type` is enabled | disabled, "disabled" uses
110 # non-thinking mode (api-docs.deepseek.com/api/create-chat-completion, read
111 # 2026-11-04, karr k382). Effort none -- and so thinking_enabled => 0 -- turns
112 # it off; it used to send enabled for none as well. A bare
113 # thinking_enabled => 1 is the on-switch.
114 106 ▶ if ( _is_deepseek_v3($model) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_deepseek_v3($model)) 37 69 106
115 37 ▶ return ( thinking => { type => $e eq 'none' ? 'disabled' : 'enabled' } )
Branch: 2 branches
BranchTrueFalseTotal
if defined $e 27 10 37
$e eq 'none' ? : 7 20 27
116 if defined $e;
117 10 ▶ return $reasoning{thinking_enabled} ? ( thinking => { type => 'enabled' } ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$reasoning{'thinking_enabled'} ? : 2 8 10
118 }
119 # V4: the flat effort string has no level-free on-switch.
120 69 ▶ unless ( defined $e ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $e) 21 48 69
121 $self->_warn_thinking_enabled_dropped( $self->reasoning_wire_format )
122 21 ▶ if $reasoning{thinking_enabled};
Branch: 1 branch
BranchTrueFalseTotal
if $reasoning{"thinking_enabled"} 1 20 21
123 21 return ();
124 }
125 48 ▶ return () unless $e eq 'none' || $e eq 'low' || $e eq 'high' || $e eq 'max';
Branch: 1 branch
BranchTrueFalseTotal
unless $e eq "none" or $e eq "low" or $e eq "high" or $e eq "max" 12 36 48
Condition100%9/9
$e eq "none" or $e eq "low" or $e eq "high" or $e eq "max"
l!l&&r!l&&!r
31512
$e eq "none" or $e eq "low" or $e eq "high"
l!l&&r!l&&!r
151617
$e eq "none" or $e eq "low"
l!l&&r!l&&!r
7833
MC/DC100%4/4
$e eq "none" or $e eq "low" or $e eq "high" or $e eq "max"
$e eq "none" $e eq "low" $e eq "high" $e eq "max"
Truth table100%5/5
A or B or C or D
A= $e eq "none"B= $e eq "low"C= $e eq "high"D= $e eq "max"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
126 36 return ( reasoning_effort => $e );
127 }
128  
129 # The thinking switch follows chat_model too, and every DeepSeek id resolves
130 # the same reasoning profile, so a per-request model that crosses the V3 line
131 # flips it unnamed otherwise (karr k362, Role::Chat::_warn_model_override).
132 8 ▶around _model_scoped_wire_decisions => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
133 8 my ( $orig, $self, $features, @rest ) = @_;
134 8 my %decision = $self->$orig( $features, @rest );
135 $decision{'thinking switch'} = _is_deepseek_v3( $self->chat_model ) ? 1 : 0
136 8 ▶ if $features->{reasoning};
Branch: 2 branches
BranchTrueFalseTotal
if $$features{"reasoning"} 6 2 8
_is_deepseek_v3($self->chat_model) ? : 2 4 6
137 8 return %decision;
138 18 };
139  
140 18 __PACKAGE__->meta->make_immutable;
141  
142  
143 18 1;
144  
145 __END__
« blib/lib/Langertha/Reasoning/BudgetPolicy.pm ↑ Summary blib/lib/Langertha/Virtual/Rules.pm »