blib/lib/Langertha/Engine/AKI.pm

filter: Statementstmt 80.2% 65 / 81 Branchbran 80.0% 48 / 60 Conditioncond 27.2% 3 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 86.3% 19 / 22 Podpod 50.0% 6 / 12 totaltotal 73.4% 141 / 192 CC 36 SCAR 23.0
CC
36
Cov
76%
list_models
30.0
chat_response
17.0
chat_request
8.3
CRAP 10.0
« blib/lib/Langertha/Runtime/Metrics/OTLP.pm ↑ Summary blib/lib/Langertha/Request/SyncHTTP.pm »
1 package Langertha::Engine::AKI;
2 # ABSTRACT: AKI.IO native 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 carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 18 extends 'Langertha::Engine::Remote';
9  
10 18 with map { 'Langertha::Role::'.$_ } qw(
11 Models
12 Temperature
13 SystemPrompt
14 Chat
15 Tools
16 HermesTools
17 );
18  
19 29 ▶sub _build_tool_wire_format { 'hermes' }
Subroutine _build_tool_wire_format: called CC=1 SCAR=0.0
20  
21 # The native /api/call body has no tools field, so Hermes (tools in the system
22 # prompt) is the only tool wire it can carry. Another tag would claim
23 # tools_native (HermesTools keys on the resolved tag, k251) and then drop the
24 # tools from the request -- fail loud instead. -- karr k254
25 32 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
26 32 my ( $self ) = @_;
27 32 my $fmt = $self->tool_wire_format;
28 32 ▶ croak "".(ref $self)." cannot use tool_wire_format '$fmt': the native API only"
Branch: 1 branch
BranchTrueFalseTotal
unless $fmt eq "hermes" 2 30 32
29 ." speaks 'hermes'; use Langertha::Engine::AKIOpenAI (\$aki->openai) for native tools"
30 unless $fmt eq 'hermes';
31 30 return;
32 }
33  
34  
35 18 has api_key => (
36 is => 'ro',
37 lazy_build => 1,
38 );
39 0 ▶sub _build_api_key {
Subroutine _build_api_key: not called CC=2 SCAR=17.9
40 0 my ( $self ) = @_;
41 return $ENV{LANGERTHA_AKI_API_KEY}
42 0 ▶ || croak "".(ref $self)." requires LANGERTHA_AKI_API_KEY or api_key set";
Condition0%0/2
$ENV{'LANGERTHA_AKI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_AKI_API_KEY or api_key set')
l!l
00
MC/DC0%0/1
$ENV{'LANGERTHA_AKI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_AKI_API_KEY or api_key set')
$ENV{'LANGERTHA_AKI_API_KEY'}
Truth table0%0/2
A
A= $ENV{'LANGERTHA_AKI_API_KEY'}
Aresult
11
00
43 }
44  
45  
46 has '+url' => (
47 lazy => 1,
48 9 ▶ default => sub { 'https://aki.io' },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 18 );
50  
51 # AKI's native /api/endpoints listing carries `minimax_m3` (endpoint_details
52 # reports "MiniMax M3 428B", category chat) — AKI.IO's current-generation
53 # MiniMax and the chosen default now that llama3_8b_chat is marked end-of-life
54 # 2026-09-30. Verified live against the native endpoint listing on 2026-09-10.
55 # MiniMax is a per-account gated endpoint: a key without the entitlement is
56 # answered with "Client not authorized for endpoint minimax_m3!" (not a silent
57 # fallback), so check $response->model / the error when it matters. -- karr k132
58 5 ▶sub default_model { 'minimax_m3' }
Subroutine default_model: called CC=1 SCAR=0.0
Pod documented
59  
60  
61 16 ▶sub hermes_extract_content {
Subroutine hermes_extract_content: called CC=1 SCAR=0.0
Pod undocumented
62 16 my ( $self, $data ) = @_;
63 16 return $data->{text};
64 }
65  
66 18 has top_k => (
67 is => 'ro',
68 isa => 'Num',
69 predicate => 'has_top_k',
70 );
71  
72  
73 18 has top_p => (
74 is => 'ro',
75 isa => 'Num',
76 predicate => 'has_top_p',
77 );
78  
79  
80 18 has max_gen_tokens => (
81 is => 'ro',
82 isa => 'Int',
83 predicate => 'has_max_gen_tokens',
84 );
85  
86  
87 # Dynamic model listing
88  
89 1 ▶sub list_models_request {
Subroutine list_models_request: called CC=1 SCAR=0.0
Pod undocumented
90 1 my ($self) = @_;
91 return $self->generate_http_request(
92 GET => $self->url.'/api/endpoints?key='.$self->api_key,
93 1 ▶ sub { $self->list_models_response(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
94 1 );
95 }
96  
97 1 ▶sub list_models_response {
Subroutine list_models_response: called CC=1 SCAR=0.0
Pod undocumented
98 1 my ($self, $response) = @_;
99 1 my $data = $self->parse_response($response);
100 1 return $data->{endpoints};
101 }
102  
103 0 ▶sub list_models {
Subroutine list_models: not called CC=5 SCAR=34.0
Pod documented
104 0 my ($self, %opts) = @_;
105  
106 # Check cache unless force_refresh requested
107 0 ▶ unless ($opts{force_refresh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"force_refresh"}) 0 0 0
108 0 my $cache = $self->_models_cache;
109 0 ▶ if ($cache->{timestamp} && time - $cache->{timestamp} < $self->models_cache_ttl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->{"timestamp"} and time - $cache->{"timestamp"} < $self->models_cache_ttl) 0 0 0
Condition0%0/3
$cache->{"timestamp"} and time - $cache->{"timestamp"} < $self->models_cache_ttl
!ll&&!rl&&r
000
MC/DC0%0/2
$cache->{"timestamp"} and time - $cache->{"timestamp"} < $self->models_cache_ttl
$cache->{"timestamp"} time - $cache->{"timestamp"} < $self->models_cache_ttl
Truth table0%0/3
A and B
A= $cache->{"timestamp"}B= time - $cache->{"timestamp"} < $self->models_cache_ttl
ABresult
0X0
100
111
110 0 ▶ return $opts{full} ? $cache->{models} : $cache->{model_ids};
Branch: 1 branch
BranchTrueFalseTotal
$opts{'full'} ? : 0 0 0
111 }
112 }
113  
114 # Fetch from API
115 0 my $request = $self->list_models_request;
116 0 my $response = $self->user_agent->request($request);
117 0 my $endpoints = $request->response_call->($response);
118  
119 # Update cache
120 0 $self->_models_cache({
121 timestamp => time,
122 models => $endpoints,
123 model_ids => $endpoints,
124 });
125  
126 0 return $endpoints;
127 }
128  
129  
130 1 ▶sub endpoint_details_request {
Subroutine endpoint_details_request: called CC=1 SCAR=0.0
Pod undocumented
131 1 my ($self, $endpoint_name) = @_;
132 return $self->generate_http_request(
133 GET => $self->url.'/api/endpoints/'.$endpoint_name.'?key='.$self->api_key,
134 1 ▶ sub { $self->endpoint_details_response(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
135 1 );
136 }
137  
138 1 ▶sub endpoint_details_response {
Subroutine endpoint_details_response: called CC=1 SCAR=0.0
Pod undocumented
139 1 my ($self, $response) = @_;
140 1 return $self->parse_response($response);
141 }
142  
143 0 ▶sub endpoint_details {
Subroutine endpoint_details: not called CC=1 SCAR=6.9
Pod documented
144 0 my ($self, $endpoint_name) = @_;
145 0 my $request = $self->endpoint_details_request($endpoint_name);
146 0 my $response = $self->user_agent->request($request);
147 0 return $request->response_call->($response);
148 }
149  
150  
151 # Chat
152  
153 10 ▶sub chat_request {
Subroutine chat_request: called CC=8 SCAR=21.1
Pod documented
154 10 my ( $self, $messages, %extra ) = @_;
155  
156 # Canonical per-request controls (chat_f, karr #46) beat the engine
157 # attributes on a per-key basis; the rest of %extra passes straight through.
158 # AKI's native wire only honors temperature and max_gen_tokens; the other
159 # canonical controls (response_format, seed, reasoning_effort, ...) have no
160 # native placement here and are consumed without being emitted.
161 10 ▶ my $controls = delete $extra{controls} // {};
Condition50%1/2
delete $extra{"controls"} // {}
l!l
010
MC/DC0%0/1
delete $extra{"controls"} // {}
delete $extra{"controls"}
Truth table50%1/2
A
A= delete $extra{"controls"}
Aresult
11
00
162  
163 # The model is the endpoint; a per-request model names it there, never in
164 # the body (karr k357).
165 10 my $model = $self->_url_model( \%extra );
166 return $self->generate_http_request(
167 POST => $self->url.'/api/call/'.$model,
168 3 ▶ sub { $self->chat_response(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
169 key => $self->api_key,
170 chat_context => $self->encode_json_text($messages),
171 exists $controls->{temperature}
172 ? ( temperature => $controls->{temperature} )
173 : ( $self->has_temperature ? ( temperature => $self->temperature ) : () ),
174 $self->has_top_k ? ( top_k => $self->top_k ) : (),
175 $self->has_top_p ? ( top_p => $self->top_p ) : (),
176 exists $controls->{max_tokens}
177 ? ( max_gen_tokens => $controls->{max_tokens} )
178 10 ▶ : ( $self->has_max_gen_tokens ? ( max_gen_tokens => $self->max_gen_tokens ) : () ),
Branch: 6 branches
BranchTrueFalseTotal
exists $controls->{'temperature'} ? : 0 10 10
$self->has_temperature ? : 2 8 10
$self->has_top_k ? : 1 9 10
$self->has_top_p ? : 1 9 10
exists $controls->{'max_tokens'} ? : 0 10 10
$self->has_max_gen_tokens ? : 1 9 10
179 wait_for_result => JSON->true,
180 %extra,
181 );
182 }
183  
184  
185 16 ▶sub chat_response {
Subroutine chat_response: called CC=17 SCAR=28.4
186 16 my ( $self, $response ) = @_;
187 16 my $data = $self->parse_response($response);
188 croak "".(ref $self)." API error: ".($data->{error} || 'unknown')
189 13 ▶ unless $data->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"success"} 1 12 13
Condition50%1/2
$data->{'error'} || 'unknown'
l!l
10
MC/DC0%0/1
$data->{'error'} || 'unknown'
$data->{'error'}
Truth table50%1/2
A
A= $data->{'error'}
Aresult
11
00
190 12 require Langertha::Response;
191 # The native endpoint composes Role::HermesTools (tool_wire_format 'hermes'),
192 # so tool calls ride as <tool_call> tags in the model text. Route them onto
193 # Response.tool_calls (ADR 0003) via the tag-aware role helpers rather than
194 # leaving them buried in content; strip the tags from content when a call is
195 # present. -- karr k123
196 12 my $tool_calls = $self->_raw_tool_calls($data);
197  
198 # Native usage: the AKI wire names its token counts differently from the
199 # OpenAI/Anthropic shapes, so normalize to the keys Langertha::Usage->from_hash
200 # understands. num_cached_tokens is the same quantity as cached_tokens; it rides
201 # in the usage hash so Response->usage->cached_tokens carries it and
202 # Response->cached_tokens is lifted off that Usage (karr k197). It counts
203 # prefix-cache *reads* (a subset of prompt_length, vLLM 16-token blocks, often
204 # non-zero even on a first call); native and /v1 report the same numbers for
205 # identical messages (live-verified 2026-09-25). Trust $response->model for which
206 # model answered. -- karr k126, k197
207 my $usage = {
208 defined $data->{prompt_length} ? ( prompt_tokens => $data->{prompt_length} ) : (),
209 defined $data->{num_generated_tokens} ? ( completion_tokens => $data->{num_generated_tokens} ) : (),
210 12 ▶ defined $data->{num_cached_tokens} ? ( cached_tokens => $data->{num_cached_tokens} ) : (),
Branch: 3 branches
BranchTrueFalseTotal
defined $data->{'prompt_length'} ? : 3 9 12
defined $data->{'num_generated_tokens'} ? : 3 9 12
defined $data->{'num_cached_tokens'} ? : 4 8 12
211 };
212 12 ▶ undef $usage unless %$usage;
Branch: 1 branch
BranchTrueFalseTotal
unless %$usage 8 4 12
213  
214 # Timing: AKI reports durations already in SECONDS. Emit the engine-agnostic
215 # total_seconds (ADR 0011) plus the native compute_seconds, and deliberately do
216 # NOT emit a raw total_duration key -- Engine::Ollama uses that same key in
217 # nanoseconds, so an AKI seconds value under it collides by a factor of 1e9.
218 # -- karr k126
219 my $timing = {
220 defined $data->{total_duration} ? ( total_seconds => $data->{total_duration} ) : (),
221 12 ▶ defined $data->{compute_duration} ? ( compute_seconds => $data->{compute_duration} ) : (),
Branch: 2 branches
BranchTrueFalseTotal
defined $data->{'total_duration'} ? : 6 6 12
defined $data->{'compute_duration'} ? : 4 8 12
222 };
223 12 ▶ undef $timing unless %$timing;
Branch: 1 branch
BranchTrueFalseTotal
unless %$timing 6 6 12
224  
225 return Langertha::Response->new(
226 content => ( @$tool_calls ? $self->_raw_text_content($data) : ( $data->{text} // '' ) ),
227 raw => $data,
228 $data->{job_id} ? ( id => $data->{job_id} ) : (),
229 12 ▶ $data->{model_name} ? ( model => $data->{model_name} ) : (),
Branch: 6 branches
BranchTrueFalseTotal
@$tool_calls ? : 4 8 12
$data->{'job_id'} ? : 4 8 12
$data->{'model_name'} ? : 7 5 12
$usage ? : 4 8 12
$timing ? : 6 6 12
@$tool_calls ? : 4 8 12
Condition50%1/2
$data->{"text"} // ""
l!l
80
MC/DC0%0/1
$data->{"text"} // ""
$data->{"text"}
Truth table50%1/2
A
A= $data->{"text"}
Aresult
11
00
230 $usage ? ( usage => $usage ) : (),
231 $timing ? ( timing => $timing ) : (),
232 @$tool_calls ? ( tool_calls => $tool_calls ) : (),
233 );
234 }
235  
236  
237 2 ▶sub openai {
Subroutine openai: called CC=4 SCAR=14.1
Pod documented
238 2 my ( $self, %args ) = @_;
239 2 require Langertha::Engine::AKIOpenAI;
240 2 ▶ unless (exists $args{model}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"model"}) 1 1 2
241 1 carp "".(ref $self)."->openai: native model name cannot be mapped to /v1 model name automatically, using AKIOpenAI default model";
242 }
243 2 ▶ return Langertha::Engine::AKIOpenAI->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->has_system_prompt ? : 2 0 2
$self->has_temperature ? : 2 0 2
244 api_key => $self->api_key,
245 $self->has_system_prompt ? ( system_prompt => $self->system_prompt ) : (),
246 $self->has_temperature ? ( temperature => $self->temperature ) : (),
247 %args,
248 );
249 }
250  
251  
252 3 ▶sub anthropic {
Subroutine anthropic: called CC=4 SCAR=14.1
Pod documented
253 3 my ( $self, %args ) = @_;
254 3 require Langertha::Engine::AKIAnthropic;
255 3 ▶ unless (exists $args{model}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"model"}) 1 2 3
256 1 carp "".(ref $self)."->anthropic: native model name cannot be mapped to /anthropic model name automatically, using AKIAnthropic default model";
257 }
258 3 ▶ return Langertha::Engine::AKIAnthropic->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->has_system_prompt ? : 3 0 3
$self->has_temperature ? : 3 0 3
259 api_key => $self->api_key,
260 $self->has_system_prompt ? ( system_prompt => $self->system_prompt ) : (),
261 $self->has_temperature ? ( temperature => $self->temperature ) : (),
262 %args,
263 );
264 }
265  
266  
267 18 __PACKAGE__->meta->make_immutable;
268  
269  
270 18 1;
271  
272 __END__
« blib/lib/Langertha/Runtime/Metrics/OTLP.pm ↑ Summary blib/lib/Langertha/Request/SyncHTTP.pm »