| 1 | package Langertha::Engine::AnthropicBase; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Base class for Anthropic-compatible engines | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 16 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 16 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use Langertha::ToolChoice; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 16 | ▶ | use Langertha::Tool; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 16 | ▶ | use Langertha::Response; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 16 | ▶ | use Langertha::ToolCall; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 16 | extends 'Langertha::Engine::Remote'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 16 | with map { 'Langertha::Role::'.$_ } qw( | |||||||||||||||||||||||||||||||||||||||||
| 15 | Models | ||||||||||||||||||||||||||||||||||||||||||
| 16 | Chat | ||||||||||||||||||||||||||||||||||||||||||
| 17 | Temperature | ||||||||||||||||||||||||||||||||||||||||||
| 18 | ResponseSize | ||||||||||||||||||||||||||||||||||||||||||
| 19 | SystemPrompt | ||||||||||||||||||||||||||||||||||||||||||
| 20 | ResponseFormat | ||||||||||||||||||||||||||||||||||||||||||
| 21 | Streaming | ||||||||||||||||||||||||||||||||||||||||||
| 22 | Tools | ||||||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | ▶ | sub default_response_size { 1024 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_response_size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | sub content_format { 'anthropic' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
content_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 16 | has api_key => ( | |||||||||||||||||||||||||||||||||||||||||
| 31 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | sub _build_api_key { | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_api_key: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | return croak "".(ref $self)." requires api_key to be set"; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 16 | has api_version => ( | |||||||||||||||||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 8 | ▶ | sub _build_api_version { '2023-06-01' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_api_version: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 16 | has effort => ( | |||||||||||||||||||||||||||||||||||||||||
| 48 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 49 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 50 | predicate => 'has_effort', | ||||||||||||||||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 16 | has inference_geo => ( | |||||||||||||||||||||||||||||||||||||||||
| 55 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 56 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 57 | predicate => 'has_inference_geo', | ||||||||||||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | sub update_request { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 11 | ▶ | my ( $self, $request ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update_request: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 11 | $request->header('x-api-key', $self->api_key); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 11 | $request->header('content-type', 'application/json'); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 11 | $request->header('anthropic-version', $self->api_version); | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | sub default_model { croak "".(ref $_[0])." requires model to be set" } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | sub chat_request { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 6 | ▶ | my ( $self, $messages, %extra ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chat_request: called CC=10 SCAR=24.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | # Anthropic has no native response_format. Translate json_object / | ||||||||||||||||||||||||||||||||||||||||||
| 74 | # json_schema response_format hashes into a synthesized tool + forced | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # named tool_choice; the response parser will pull the structured | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # output out of the resulting tool_use block. | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 6 | $self->_translate_response_format(\%extra); | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 6 | $self->_normalize_tool_params(\%extra); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 6 | my @msgs; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | my $system = ""; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 6 | for my $message (@{$messages}) { | |||||||||||||||||||||||||||||||||||||||||
| 83 | 7 | ▶ | if ($message->{role} eq 'system') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | $system .= "\n\n" if length $system; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | $system .= $message->{content}; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | push @msgs, $message; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 6 | ▶ | if ($system and scalar @msgs == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$system and scalar @msgs == 0
MC/DC0%0/2
$system and scalar @msgs == 0
$system
scalar @msgs == 0
Truth table66%2/3
A and B
A= $systemB= scalar @msgs == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | push @msgs, { | |||||||||||||||||||||||||||||||||||||||||
| 92 | role => 'user', | ||||||||||||||||||||||||||||||||||||||||||
| 93 | content => $system, | ||||||||||||||||||||||||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $system = undef; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | return $self->generate_http_request( POST => $self->url.'/v1/messages', sub { $self->chat_response(shift) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 6 | ▶ | model => $self->chat_model, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | messages => \@msgs, | ||||||||||||||||||||||||||||||||||||||||||
| 100 | max_tokens => $self->get_response_size, # must be always set | ||||||||||||||||||||||||||||||||||||||||||
| 101 | $self->has_temperature ? ( temperature => $self->temperature ) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 102 | $self->has_effort ? ( effort => $self->effort ) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 103 | $self->has_inference_geo ? ( inference_geo => $self->inference_geo ) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 104 | $system ? ( system => $system ) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 105 | %extra, | ||||||||||||||||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | # Anthropic has no response_format; emulate via a synthetic tool plus | ||||||||||||||||||||||||||||||||||||||||||
| 110 | # a forced tool_choice. The response_call will detect the synthetic | ||||||||||||||||||||||||||||||||||||||||||
| 111 | # tool_use block and lift its input back into the response content. | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 16 | my $SYNTH_RF_TOOL_NAME = '__langertha_response_format__'; | |||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | sub _translate_response_format { | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 6 | ▶ | my ( $self, $extra ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_translate_response_format: called CC=11 SCAR=46.8
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 6 | ▶ | return unless $self->has_response_format; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my $rf = $self->response_format; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | return unless ref($rf) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | my $type = $rf->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$$rf{"type"} // ""
MC/DC0%0/1
$$rf{"type"} // ""
$$rf{"type"}
Truth table0%0/2
A
A= $$rf{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my ( $name, $schema, $description ); | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ( $type eq 'json_schema' && ref( $rf->{json_schema} ) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$type eq "json_schema" and ref $$rf{"json_schema"} eq "HASH"
MC/DC0%0/2
$type eq "json_schema" and ref $$rf{"json_schema"} eq "HASH"
$type eq "json_schema"
ref $$rf{"json_schema"} eq "HASH"
Truth table0%0/3
A and B
A= $type eq "json_schema"B= ref $$rf{"json_schema"} eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | my $js = $rf->{json_schema}; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | $name = $js->{name} // $SYNTH_RF_TOOL_NAME; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$$js{"name"} // $SYNTH_RF_TOOL_NAME
MC/DC0%0/2
$$js{"name"} // $SYNTH_RF_TOOL_NAME
$$js{"name"}
$SYNTH_RF_TOOL_NAME
Truth table0%0/3
A // B
A= $$js{"name"}B= $SYNTH_RF_TOOL_NAME
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | $schema = $js->{schema}; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $description = $js->{description}; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | elsif ( $type eq 'json_object' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | $name = $SYNTH_RF_TOOL_NAME; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | $schema = { type => 'object', additionalProperties => JSON->true }; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | return unless ref($schema) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | my $tool = Langertha::Tool->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | name => $name, | ||||||||||||||||||||||||||||||||||||||||||
| 139 | input_schema => $schema, | ||||||||||||||||||||||||||||||||||||||||||
| 140 | ( defined $description ? ( description => $description ) : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 141 | )->to_anthropic; | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | $extra->{tools} ||= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$$extra{"tools"} ||= []
MC/DC0%0/1
$$extra{"tools"} ||= []
$$extra{"tools"}
Truth table0%0/2
A
A= $$extra{"tools"}
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | push @{ $extra->{tools} }, $tool; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | $extra->{tool_choice} = { type => 'tool', name => $name }; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | # Normalize tool_choice (any accepted format -> Anthropic native) and fold | ||||||||||||||||||||||||||||||||||||||||||
| 149 | # parallel_tool_use into the tool_choice block as Anthropic expects. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | sub _normalize_tool_params { | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 7 | ▶ | my ( $self, $extra ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_tool_params: called CC=10 SCAR=37.2
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 7 | ▶ | if ( exists $extra->{tool_choice} && defined $extra->{tool_choice} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $$extra{"tool_choice"} and defined $$extra{"tool_choice"}
MC/DC0%0/2
exists $$extra{"tool_choice"} and defined $$extra{"tool_choice"}
exists $$extra{"tool_choice"}
defined $$extra{"tool_choice"}
Truth table33%1/3
A and B
A= exists $$extra{"tool_choice"}B= defined $$extra{"tool_choice"}
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ( my $tc = Langertha::ToolChoice->from_hash( $extra->{tool_choice} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | $extra->{tool_choice} = $tc->to_anthropic; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | return unless exists $extra->{tools} | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 7 | ▶ | && $self->can('has_parallel_tool_use') && $self->has_parallel_tool_use; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
exists $$extra{"tools"} and $self->can("has_parallel_tool_use") and $self->has_parallel_tool_use
exists $$extra{"tools"} and $self->can("has_parallel_tool_use")
MC/DC0%0/3
exists $$extra{"tools"} and $self->can("has_parallel_tool_use") and $self->has_parallel_tool_use
exists $$extra{"tools"}
$self->can("has_parallel_tool_use")
$self->has_parallel_tool_use
Truth table50%2/4
A and B and C
A= exists $$extra{"tools"}B= $self->can("has_parallel_tool_use")C= $self->has_parallel_tool_use
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | my $tc = $extra->{tool_choice}; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | $tc = { type => 'auto' } unless ref($tc) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | unless ( exists $tc->{disable_parallel_tool_use} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | $tc->{disable_parallel_tool_use} = $self->parallel_tool_use ? JSON->false : JSON->true; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | $extra->{tool_choice} = $tc; | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | sub chat_response { | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 5 | ▶ | my ( $self, $response ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chat_response: called CC=12 SCAR=27.4
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 5 | my $data = $self->parse_response($response); | |||||||||||||||||||||||||||||||||||||||||
| 173 | 5 | my @blocks = @{$data->{content}}; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 5 | ▶ | my $text = join('', map { $_->{text} // '' } grep { $_->{type} eq 'text' } @blocks); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$_{"text"} // ""
MC/DC0%0/1
$$_{"text"} // ""
$$_{"text"}
Truth table50%1/2
A
A= $$_{"text"}
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 5 | ▶ | my @thinking = map { $_->{thinking} // '' } grep { $_->{type} eq 'thinking' } @blocks; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$_{"thinking"} // ""
MC/DC0%0/1
$$_{"thinking"} // ""
$$_{"thinking"}
Truth table50%1/2
A
A= $$_{"thinking"}
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 5 | ▶ | my $thinking = @thinking ? join("\n", @thinking) : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 5 | my @tcs = Langertha::ToolCall->extract($data); | |||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | # If the caller asked for a response_format and we routed it through a | ||||||||||||||||||||||||||||||||||||||||||
| 180 | # synthesized tool, lift the tool_use input back into the content as | ||||||||||||||||||||||||||||||||||||||||||
| 181 | # JSON so callers can treat it like any other structured-output result. | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 5 | ▶ | if ( $self->has_response_format && @tcs ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->has_response_format and @tcs
MC/DC0%0/2
$self->has_response_format and @tcs
$self->has_response_format
@tcs
Truth table33%1/3
A and B
A= $self->has_response_formatB= @tcs
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | $text = $self->json->encode( $tcs[0]->arguments ); | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | return Langertha::Response->new( | ||||||||||||||||||||||||||||||||||||||||||
| 186 | content => $text, | ||||||||||||||||||||||||||||||||||||||||||
| 187 | raw => $data, | ||||||||||||||||||||||||||||||||||||||||||
| 188 | $data->{id} ? ( id => $data->{id} ) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 189 | $data->{model} ? ( model => $data->{model} ) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 190 | defined $data->{stop_reason} ? ( finish_reason => $data->{stop_reason} ) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 5 | ▶ | $data->{usage} ? ( usage => $data->{usage} ) : (), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | defined $thinking ? ( thinking => $thinking ) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 193 | @tcs ? ( tool_calls => [ @tcs ] ) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 194 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 2 | ▶ | sub stream_format { 'sse' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stream_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | sub chat_stream_request { | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 1 | ▶ | my ( $self, $messages, %extra ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chat_stream_request: called CC=10 SCAR=29.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 1 | $self->_normalize_tool_params(\%extra); | |||||||||||||||||||||||||||||||||||||||||
| 202 | 1 | my @msgs; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 1 | my $system = ""; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 1 | for my $message (@{$messages}) { | |||||||||||||||||||||||||||||||||||||||||
| 205 | 1 | ▶ | if ($message->{role} eq 'system') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | $system .= "\n\n" if length $system; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | $system .= $message->{content}; | |||||||||||||||||||||||||||||||||||||||||
| 208 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 1 | push @msgs, $message; | |||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 1 | ▶ | if ($system and scalar @msgs == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$system and scalar @msgs == 0
MC/DC0%0/2
$system and scalar @msgs == 0
$system
scalar @msgs == 0
Truth table33%1/3
A and B
A= $systemB= scalar @msgs == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | push @msgs, { | |||||||||||||||||||||||||||||||||||||||||
| 214 | role => 'user', | ||||||||||||||||||||||||||||||||||||||||||
| 215 | content => $system, | ||||||||||||||||||||||||||||||||||||||||||
| 216 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | $system = undef; | |||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | ▶ | return $self->generate_http_request( POST => $self->url.'/v1/messages', sub {}, | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 1 | ▶ | model => $self->chat_model, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | messages => \@msgs, | ||||||||||||||||||||||||||||||||||||||||||
| 222 | max_tokens => $self->get_response_size, | ||||||||||||||||||||||||||||||||||||||||||
| 223 | $self->has_temperature ? ( temperature => $self->temperature ) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 224 | $self->has_effort ? ( effort => $self->effort ) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 225 | $self->has_inference_geo ? ( inference_geo => $self->inference_geo ) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 226 | $system ? ( system => $system ) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 227 | stream => JSON->true, | ||||||||||||||||||||||||||||||||||||||||||
| 228 | %extra, | ||||||||||||||||||||||||||||||||||||||||||
| 229 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | sub parse_stream_chunk { | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 6 | ▶ | my ( $self, $data, $event ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_stream_chunk: called CC=10 SCAR=23.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 6 | require Langertha::Stream::Chunk; | |||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | # Anthropic uses event types: content_block_delta, message_delta, message_stop | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 6 | ▶ | my $type = $data->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$data{"type"} // ""
MC/DC0%0/1
$$data{"type"} // ""
$$data{"type"}
Truth table50%1/2
A
A= $$data{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 6 | ▶ | if ($type eq 'content_block_delta') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 2 | ▶ | my $delta = $data->{delta} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$data{'delta'} || {}
MC/DC0%0/1
$$data{'delta'} || {}
$$data{'delta'}
Truth table50%1/2
A
A= $$data{'delta'}
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | return Langertha::Stream::Chunk->new( | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 2 | ▶ | content => $delta->{text} // '', | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$delta{"text"} // ""
MC/DC0%0/1
$$delta{"text"} // ""
$$delta{"text"}
Truth table50%1/2
A
A= $$delta{"text"}
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | raw => $data, | ||||||||||||||||||||||||||||||||||||||||||
| 245 | is_final => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 246 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 4 | ▶ | if ($type eq 'message_delta') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 1 | ▶ | my $delta = $data->{delta} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$data{'delta'} || {}
MC/DC0%0/1
$$data{'delta'} || {}
$$data{'delta'}
Truth table50%1/2
A
A= $$data{'delta'}
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | return Langertha::Stream::Chunk->new( | ||||||||||||||||||||||||||||||||||||||||||
| 252 | content => '', | ||||||||||||||||||||||||||||||||||||||||||
| 253 | raw => $data, | ||||||||||||||||||||||||||||||||||||||||||
| 254 | is_final => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 255 | $delta->{stop_reason} ? (finish_reason => $delta->{stop_reason}) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 1 | ▶ | $data->{usage} ? (usage => $data->{usage}) : (), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | 3 | ▶ | if ($type eq 'message_stop') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 1 | return Langertha::Stream::Chunk->new( | |||||||||||||||||||||||||||||||||||||||||
| 262 | content => '', | ||||||||||||||||||||||||||||||||||||||||||
| 263 | raw => $data, | ||||||||||||||||||||||||||||||||||||||||||
| 264 | is_final => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 265 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | # Other event types (message_start, content_block_start, etc.) - skip | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 2 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | # Dynamic model listing with cursor pagination | ||||||||||||||||||||||||||||||||||||||||||
| 273 | sub list_models_request { | ||||||||||||||||||||||||||||||||||||||||||
| 274 | 4 | ▶ | my ($self, %params) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models_request: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 4 | my $url = $self->url.'/v1/models'; | |||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | # Add pagination params if provided | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 4 | ▶ | if (%params) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 2 | require URI; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 2 | my $uri = URI->new($url); | |||||||||||||||||||||||||||||||||||||||||
| 281 | 2 | $uri->query_form(%params); | |||||||||||||||||||||||||||||||||||||||||
| 282 | 2 | $url = $uri->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | return $self->generate_http_request( | ||||||||||||||||||||||||||||||||||||||||||
| 286 | GET => $url, | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 2 | ▶ | sub { $self->list_models_response(shift) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | sub list_models_response { | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 3 | ▶ | my ($self, $response) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models_response: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 3 | my $data = $self->parse_response($response); | |||||||||||||||||||||||||||||||||||||||||
| 294 | 3 | return $data; | |||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | sub _fetch_all_models { | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fetch_all_models: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 1 | my @all_models; | |||||||||||||||||||||||||||||||||||||||||
| 300 | my $after_id; | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 1 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 2 | ▶ | my $request = $self->list_models_request( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | $after_id ? (after_id => $after_id, limit => 100) : () | ||||||||||||||||||||||||||||||||||||||||||
| 305 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 2 | my $response = $self->user_agent->request($request); | |||||||||||||||||||||||||||||||||||||||||
| 307 | 2 | my $data = $request->response_call->($response); | |||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | 2 | push @all_models, @{$data->{data}}; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 2 | ▶ | $after_id = $data->{has_more} ? $data->{last_id} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | } while ($after_id); | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | 1 | return \@all_models; | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | sub list_models { | ||||||||||||||||||||||||||||||||||||||||||
| 317 | 1 | ▶ | my ($self, %opts) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models: called CC=6 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | # Check cache unless force_refresh requested | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 1 | ▶ | unless ($opts{force_refresh}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 1 | my $cache = $self->_models_cache; | |||||||||||||||||||||||||||||||||||||||||
| 322 | 1 | ▶ | if ($cache->{timestamp} && time - $cache->{timestamp} < $self->models_cache_ttl) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$$cache{"timestamp"} and time - $$cache{"timestamp"} < $self->models_cache_ttl
MC/DC0%0/2
$$cache{"timestamp"} and time - $$cache{"timestamp"} < $self->models_cache_ttl
$$cache{"timestamp"}
time - $$cache{"timestamp"} < $self->models_cache_ttl
Truth table33%1/3
A and B
A= $$cache{"timestamp"}B= time - $$cache{"timestamp"} < $self->models_cache_ttl
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | return $opts{full} ? $cache->{models} : $cache->{model_ids}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | # Fetch all pages from API | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 1 | my $models = $self->_fetch_all_models; | |||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | # Extract IDs and update cache | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 1 | my @model_ids = map { $_->{id} } @$models; | |||||||||||||||||||||||||||||||||||||||||
| 332 | 1 | $self->_models_cache({ | |||||||||||||||||||||||||||||||||||||||||
| 333 | timestamp => time, | ||||||||||||||||||||||||||||||||||||||||||
| 334 | models => $models, | ||||||||||||||||||||||||||||||||||||||||||
| 335 | model_ids => \@model_ids, | ||||||||||||||||||||||||||||||||||||||||||
| 336 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | 1 | ▶ | return $opts{full} ? $models : \@model_ids; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | # Tool calling support (MCP) | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | sub format_tools { | ||||||||||||||||||||||||||||||||||||||||||
| 345 | 1 | ▶ | my ( $self, $mcp_tools ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_tools: called CC=3 SCAR=13.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | return [map { | ||||||||||||||||||||||||||||||||||||||||||
| 347 | 1 | { | |||||||||||||||||||||||||||||||||||||||||
| 348 | name => $_->{name}, | ||||||||||||||||||||||||||||||||||||||||||
| 349 | description => $_->{description}, | ||||||||||||||||||||||||||||||||||||||||||
| 350 | input_schema => $_->{input_schema} // $_->{inputSchema} // $_->{parameters}, | ||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 2 | ▶ | } @$mcp_tools]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
($$_{"input_schema"} // $$_{"inputSchema"}) // $$_{"parameters"}
$$_{"input_schema"} // $$_{"inputSchema"}
MC/DC0%0/3
($$_{"input_schema"} // $$_{"inputSchema"}) // $$_{"parameters"}
$$_{"input_schema"}
$$_{"inputSchema"}
$$_{"parameters"}
Truth table25%1/4
A // B // C
A= $$_{"input_schema"}B= $$_{"inputSchema"}C= $$_{"parameters"}
| |||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | sub response_tool_calls { | ||||||||||||||||||||||||||||||||||||||||||
| 356 | 3 | ▶ | my ( $self, $data ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
response_tool_calls: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | 3 | ▶ | return [grep { $_->{type} eq 'tool_use' } @{$data->{content} // []}]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$data{"content"} // []
MC/DC0%0/1
$$data{"content"} // []
$$data{"content"}
Truth table50%1/2
A
A= $$data{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | sub extract_tool_call { | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | ▶ | my ( $self, $tc ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
extract_tool_call: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | return ( $tc->{name}, $tc->{input} ); | |||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | sub response_text_content { | ||||||||||||||||||||||||||||||||||||||||||
| 366 | 3 | ▶ | my ( $self, $data ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
response_text_content: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 367 | 5 | return join('', map { $_->{text} } | |||||||||||||||||||||||||||||||||||||||||
| 368 | 3 | ▶ | grep { $_->{type} eq 'text' } @{$data->{content} // []}) | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$data{"content"} // []
MC/DC0%0/1
$$data{"content"} // []
$$data{"content"}
Truth table50%1/2
A
A= $$data{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | sub format_tool_results { | ||||||||||||||||||||||||||||||||||||||||||
| 372 | 2 | ▶ | my ( $self, $data, $results ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_tool_results: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 374 | { role => 'assistant', content => $data->{content} }, | ||||||||||||||||||||||||||||||||||||||||||
| 375 | { role => 'user', content => [ | ||||||||||||||||||||||||||||||||||||||||||
| 376 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 377 | 2 | my $r = $_; | |||||||||||||||||||||||||||||||||||||||||
| 378 | { | ||||||||||||||||||||||||||||||||||||||||||
| 379 | type => 'tool_result', | ||||||||||||||||||||||||||||||||||||||||||
| 380 | tool_use_id => $r->{tool_call}{id}, | ||||||||||||||||||||||||||||||||||||||||||
| 381 | content => $r->{result}{content}, | ||||||||||||||||||||||||||||||||||||||||||
| 382 | 2 | ▶ | $r->{result}{isError} ? ( is_error => JSON->true ) : (), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||
| 384 | } @$results | ||||||||||||||||||||||||||||||||||||||||||
| 385 | ]}, | ||||||||||||||||||||||||||||||||||||||||||
| 386 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | sub _parse_rate_limit_headers { | ||||||||||||||||||||||||||||||||||||||||||
| 390 | 9 | ▶ | my ( $self, $http_response ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_rate_limit_headers: called CC=10 SCAR=24.5
| |||||||||||||||||||||||||||||||||||||||||||
| 391 | 9 | my %raw; | |||||||||||||||||||||||||||||||||||||||||
| 392 | 9 | for my $name (qw( | |||||||||||||||||||||||||||||||||||||||||
| 393 | anthropic-ratelimit-requests-limit | ||||||||||||||||||||||||||||||||||||||||||
| 394 | anthropic-ratelimit-requests-remaining | ||||||||||||||||||||||||||||||||||||||||||
| 395 | anthropic-ratelimit-requests-reset | ||||||||||||||||||||||||||||||||||||||||||
| 396 | anthropic-ratelimit-tokens-limit | ||||||||||||||||||||||||||||||||||||||||||
| 397 | anthropic-ratelimit-tokens-remaining | ||||||||||||||||||||||||||||||||||||||||||
| 398 | anthropic-ratelimit-tokens-reset | ||||||||||||||||||||||||||||||||||||||||||
| 399 | anthropic-ratelimit-input-tokens-limit | ||||||||||||||||||||||||||||||||||||||||||
| 400 | anthropic-ratelimit-input-tokens-remaining | ||||||||||||||||||||||||||||||||||||||||||
| 401 | anthropic-ratelimit-input-tokens-reset | ||||||||||||||||||||||||||||||||||||||||||
| 402 | anthropic-ratelimit-output-tokens-limit | ||||||||||||||||||||||||||||||||||||||||||
| 403 | anthropic-ratelimit-output-tokens-remaining | ||||||||||||||||||||||||||||||||||||||||||
| 404 | anthropic-ratelimit-output-tokens-reset | ||||||||||||||||||||||||||||||||||||||||||
| 405 | )) { | ||||||||||||||||||||||||||||||||||||||||||
| 406 | 108 | my $val = $http_response->header($name); | |||||||||||||||||||||||||||||||||||||||||
| 407 | 108 | ▶ | $raw{$name} = $val if defined $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||
| 409 | 9 | ▶ | return undef unless %raw; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | 1 | require Langertha::RateLimit; | |||||||||||||||||||||||||||||||||||||||||
| 411 | return Langertha::RateLimit->new( | ||||||||||||||||||||||||||||||||||||||||||
| 412 | ( defined $raw{'anthropic-ratelimit-requests-limit'} ? ( requests_limit => $raw{'anthropic-ratelimit-requests-limit'} + 0 ) : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 413 | ( defined $raw{'anthropic-ratelimit-requests-remaining'} ? ( requests_remaining => $raw{'anthropic-ratelimit-requests-remaining'} + 0 ) : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 414 | ( defined $raw{'anthropic-ratelimit-requests-reset'} ? ( requests_reset => $raw{'anthropic-ratelimit-requests-reset'} ) : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 415 | ( defined $raw{'anthropic-ratelimit-tokens-limit'} ? ( tokens_limit => $raw{'anthropic-ratelimit-tokens-limit'} + 0 ) : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 416 | ( defined $raw{'anthropic-ratelimit-tokens-remaining'} ? ( tokens_remaining => $raw{'anthropic-ratelimit-tokens-remaining'} + 0 ) : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 417 | 1 | ▶ | ( defined $raw{'anthropic-ratelimit-tokens-reset'} ? ( tokens_reset => $raw{'anthropic-ratelimit-tokens-reset'} ) : () ), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 418 | raw => \%raw, | ||||||||||||||||||||||||||||||||||||||||||
| 419 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | 16 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | 16 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | Langertha::Engine::AnthropicBase - Base class for Anthropic-compatible engines | ||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | version 0.502 | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | package My::AnthropicCompatible; | ||||||||||||||||||||||||||||||||||||||||||
| 445 | use Moose; | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | extends 'Langertha::Engine::AnthropicBase'; | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | has '+url' => ( default => sub { 'https://api.example.com' } ); | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | sub _build_api_key { $ENV{MY_API_KEY} || die "MY_API_KEY required" } | ||||||||||||||||||||||||||||||||||||||||||
| 452 | sub default_model { 'my-model-v1' } | ||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | Intermediate base class for engines speaking the Anthropic-compatible | ||||||||||||||||||||||||||||||||||||||||||
| 459 | C</v1/messages> format. Extends L<Langertha::Engine::Remote> and composes | ||||||||||||||||||||||||||||||||||||||||||
| 460 | models/chat/streaming plus Anthropic-style tool calling and response parsing. | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | Concrete engines extending this class include | ||||||||||||||||||||||||||||||||||||||||||
| 463 | L<Langertha::Engine::Anthropic>, L<Langertha::Engine::MiniMax>, and | ||||||||||||||||||||||||||||||||||||||||||
| 464 | L<Langertha::Engine::LMStudioAnthropic>. | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | B<THIS API IS WORK IN PROGRESS> | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | =head2 api_key | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | Anthropic-compatible API key sent as C<x-api-key>. Subclasses typically | ||||||||||||||||||||||||||||||||||||||||||
| 471 | override C<_build_api_key> to read a provider-specific environment variable. | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | =head2 api_version | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | The Anthropic API version header sent with every request. Defaults to | ||||||||||||||||||||||||||||||||||||||||||
| 476 | C<2023-06-01>. | ||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | =head2 effort | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | Controls the depth of thinking for reasoning models. Values: C<low>, C<medium>, | ||||||||||||||||||||||||||||||||||||||||||
| 481 | C<high>. When set, passed as the C<effort> parameter in the API request. | ||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | my $claude = Langertha::Engine::Anthropic->new( | ||||||||||||||||||||||||||||||||||||||||||
| 484 | api_key => $ENV{ANTHROPIC_API_KEY}, | ||||||||||||||||||||||||||||||||||||||||||
| 485 | model => 'claude-opus-4-6', | ||||||||||||||||||||||||||||||||||||||||||
| 486 | effort => 'high', | ||||||||||||||||||||||||||||||||||||||||||
| 487 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | =head2 inference_geo | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | Controls data residency for inference. Values: C<us>, C<eu>. When set, passed | ||||||||||||||||||||||||||||||||||||||||||
| 492 | as the C<inference_geo> parameter to keep processing in the specified region. | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | my $claude = Langertha::Engine::Anthropic->new( | ||||||||||||||||||||||||||||||||||||||||||
| 495 | api_key => $ENV{ANTHROPIC_API_KEY}, | ||||||||||||||||||||||||||||||||||||||||||
| 496 | inference_geo => 'eu', | ||||||||||||||||||||||||||||||||||||||||||
| 497 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | =head2 list_models | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | my $model_ids = $engine->list_models; | ||||||||||||||||||||||||||||||||||||||||||
| 502 | my $models = $engine->list_models(full => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 503 | my $models = $engine->list_models(force_refresh => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | Fetches available models from the Anthropic API using cursor pagination. | ||||||||||||||||||||||||||||||||||||||||||
| 506 | Returns an ArrayRef of model ID strings by default, or full model objects | ||||||||||||||||||||||||||||||||||||||||||
| 507 | when C<full => 1> is passed. Results are cached for C<models_cache_ttl> | ||||||||||||||||||||||||||||||||||||||||||
| 508 | seconds (default: 3600). Pass C<force_refresh => 1> to bypass the cache. | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | =head2 _parse_rate_limit_headers | ||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||
| 512 | Parses C<anthropic-ratelimit-*> headers from the HTTP response into a | ||||||||||||||||||||||||||||||||||||||||||
| 513 | L<Langertha::RateLimit> object. The C<raw> hash captures extras like | ||||||||||||||||||||||||||||||||||||||||||
| 514 | C<input-tokens-limit> and C<output-tokens-limit>. | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | =item * L<https://status.anthropic.com/> - Anthropic service status | ||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | =item * L<https://docs.anthropic.com/> - Official Anthropic documentation | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | =item * L<Langertha::Role::Chat> - Chat interface methods | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | =item * L<Langertha::Role::Tools> - MCP tool calling interface | ||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||
| 528 | =item * L<Langertha::Role::Streaming> - Streaming support (SSE format) | ||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||
| 530 | =item * L<Langertha::Engine::Gemini> - Another non-OpenAI-compatible engine | ||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||
| 538 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||
| 539 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||
| 541 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||
| 545 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||
| 547 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||
| 553 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||
| 555 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||
| 557 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 558 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||
| 560 | =cut | ||||||||||||||||||||||||||||||||||||||||||