| 1 | package Langertha::Engine::MoonshotAnthropic; | ||||||||||||||||||||
| 2 | # ABSTRACT: Moonshot AI Kimi API via Anthropic-compatible endpoint | ||||||||||||||||||||
| 3 | 14 | our $VERSION = '0.505'; | |||||||||||||||||||
| 4 | 14 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 14 | ▶ | use Carp qw( croak ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 14 | extends 'Langertha::Engine::AnthropicBase'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 14 | with 'Langertha::Role::StaticModels'; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | # AnthropicBase->chat_request appends '/v1/messages' to url; the default must | ||||||||||||||||||||
| 13 | # therefore stop at '/anthropic' so the composed endpoint is a single | ||||||||||||||||||||
| 14 | # '/anthropic/v1/messages' (a '/anthropic/v1' default would double-stack to | ||||||||||||||||||||
| 15 | # '/anthropic/v1/v1/messages' -> HTTP 404). | ||||||||||||||||||||
| 16 | has '+url' => ( | ||||||||||||||||||||
| 17 | lazy => 1, | ||||||||||||||||||||
| 18 | 123 | ▶ | default => sub { 'https://api.moonshot.ai/anthropic' }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 14 | ); | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 0 | ▶ | sub _build_api_key { | ||||||||||||||||||
|
Subroutine
_build_api_key: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 22 | 0 | my ( $self ) = @_; | |||||||||||||||||||
| 23 | return $ENV{LANGERTHA_MOONSHOT_API_KEY} | ||||||||||||||||||||
| 24 | 0 | ▶ | || croak "".(ref $self)." requires LANGERTHA_MOONSHOT_API_KEY or api_key set"; | ||||||||||||||||||
|
Condition0%0/2
$ENV{'LANGERTHA_MOONSHOT_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_MOONSHOT_API_KEY or api_key set')
MC/DC0%0/1
$ENV{'LANGERTHA_MOONSHOT_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_MOONSHOT_API_KEY or api_key set')
$ENV{'LANGERTHA_MOONSHOT_API_KEY'}
Truth table0%0/2
A
A= $ENV{'LANGERTHA_MOONSHOT_API_KEY'}
| |||||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 32 | ▶ | sub default_model { 'kimi-k3' } | ||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 1 | ▶ | sub api_key_env { 'LANGERTHA_MOONSHOT_API_KEY' } | ||||||||||||||||||
|
Subroutine
api_key_env: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 13 | ▶ | sub default_response_size { 4096 } | ||||||||||||||||||
|
Subroutine
default_response_size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # Kimi counts reasoning_content against max_tokens and recommends | ||||||||||||||||||||
| 34 | # max_tokens >= 16000 while thinking is on (karr k225; platform.kimi.ai, advisor | ||||||||||||||||||||
| 35 | # 2026-09-25 on k219, docs only). kimi-k3 and kimi-k2.7-code(-highspeed) always | ||||||||||||||||||||
| 36 | # think, kimi-k2.6 thinks by default, so they default to 16000 instead of 4096. | ||||||||||||||||||||
| 37 | # Only when no response_size is set; max_tokens is a ceiling, billing follows | ||||||||||||||||||||
| 38 | # the tokens produced (ADR 0019 k225 Update). Same rows as | ||||||||||||||||||||
| 39 | # Engine::Moonshot: the same models think on this face (k215). | ||||||||||||||||||||
| 40 | 186 | ▶ | sub model_response_size_defaults { | ||||||||||||||||||
|
Subroutine
model_response_size_defaults: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 41 | return ( | ||||||||||||||||||||
| 42 | 186 | qr/\Akimi-k3(?!\d)/ => 16000, | |||||||||||||||||||
| 43 | qr/\Akimi-k2\.(?:6|7-code(?:-highspeed)?)\z/ => 16000, | ||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 0 | ▶ | sub _build_static_models {[ | ||||||||||||||||||
|
Subroutine
_build_static_models: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 48 | 0 | { id => 'kimi-k3' }, | |||||||||||||||||||
| 49 | { id => 'kimi-k2.7-code' }, | ||||||||||||||||||||
| 50 | { id => 'kimi-k2.7-code-highspeed' }, | ||||||||||||||||||||
| 51 | { id => 'kimi-k2.6' }, | ||||||||||||||||||||
| 52 | ]} | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # Temperature is fixed server-side on every current Kimi id (karr k214; see | ||||||||||||||||||||
| 55 | # Engine::Moonshot for the sources). Kimi's Messages-API schema has no | ||||||||||||||||||||
| 56 | # temperature property at all, and whether /anthropic 400s on one or ignores it | ||||||||||||||||||||
| 57 | # is undocumented; the value is fixed either way, so omitting it is always safe | ||||||||||||||||||||
| 58 | # (ADR 0025 k214 Update). Same rows as the OpenAI face. | ||||||||||||||||||||
| 59 | # | ||||||||||||||||||||
| 60 | # Reasoning on K2.x (karr k215, docs/api/messages.md + the Claude Code guide, | ||||||||||||||||||||
| 61 | # advisor 2026-09-25, docs only): output_config.effort is K3-only, so the K2 | ||||||||||||||||||||
| 62 | # line takes no effort -- the flag is cleared for every K2 id. The two | ||||||||||||||||||||
| 63 | # documented ids opt back in, because this face parses a thinking toggle for | ||||||||||||||||||||
| 64 | # them (kimi-k2.6 enabled|disabled, kimi-k2.7-code enabled only); their | ||||||||||||||||||||
| 65 | # Reasoning::Profile rows send it instead of an effort (ADR 0023 k215 Update). | ||||||||||||||||||||
| 66 | # | ||||||||||||||||||||
| 67 | # image_input (k266, k359, ADR 0019): the same rows as Engine::Moonshot -- this | ||||||||||||||||||||
| 68 | # face serves the same Kimi models, and its Messages schema carries image | ||||||||||||||||||||
| 69 | # blocks (docs/api/messages.md: tool_result content is "a string or an array | ||||||||||||||||||||
| 70 | # of text / image blocks"; llm-advisor 2026-09-30, docs only). The catch-all | ||||||||||||||||||||
| 71 | # first row clears the flag for unchecked ids. Until k359 the flag was deleted | ||||||||||||||||||||
| 72 | # engine-wide here; it now also picks the tool-result image form, and an | ||||||||||||||||||||
| 73 | # engine-wide no-claim would have kept kimi-k3 from seeing a tool's image. | ||||||||||||||||||||
| 74 | 497 | ▶ | sub model_capability_corrections { | ||||||||||||||||||
|
Subroutine
model_capability_corrections: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 75 | return ( | ||||||||||||||||||||
| 76 | 497 | qr/\A/ => { image_input => 0 }, | |||||||||||||||||||
| 77 | qr/\Akimi-k3(?!\d)/ => { temperature => 0 }, | ||||||||||||||||||||
| 78 | qr/\Akimi-k2(?!\d)/ => { temperature => 0, reasoning_effort => 0 }, | ||||||||||||||||||||
| 79 | qr/\Akimi-k2\.(?:6|7-code(?:-highspeed)?)\z/ => { reasoning_effort => 1 }, | ||||||||||||||||||||
| 80 | qr/\Akimi-k(?:3|2\.6|2\.7-code)(?!\d)/ => { image_input => 1 }, | ||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | # Source blocks in a tool_result (karr k364, k366): Kimi's Messages OpenAPI | ||||||||||||||||||||
| 85 | # schema (platform.kimi.ai/docs/api/messages.md, llm-advisor 2026-09-30, docs | ||||||||||||||||||||
| 86 | # only, not live) lists tool_result content as string | [text | image] -- no | ||||||||||||||||||||
| 87 | # document, no search_result -- and Kimi checks block types strictly (a 400 | ||||||||||||||||||||
| 88 | # "Input tag 'document' found using 'type' does not match any of the expected | ||||||||||||||||||||
| 89 | # tags" is reported in github.com/MoonshotAI/Kimi-K2.5/issues/27). A text | ||||||||||||||||||||
| 90 | # resource goes out as a text block, a PDF as the placeholder, a native | ||||||||||||||||||||
| 91 | # document or search_result as its text, rather than a rejected tool-loop turn. | ||||||||||||||||||||
| 92 | 502 | ▶ | sub _tool_result_source_blocks_on_wire { 0 } | ||||||||||||||||||
|
Subroutine
_tool_result_source_blocks_on_wire: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | # Kimi's Messages API documents native output_config.format {type: json_schema, | ||||||||||||||||||||
| 95 | # schema} for kimi-k3 (docs/api/messages.md, advisor 2026-09-25, docs only; | ||||||||||||||||||||
| 96 | # karr k218), so K3 takes the first-party native path (ADR 0005 k218 Update) | ||||||||||||||||||||
| 97 | # instead of the shim's synthetic tool + forced named tool_choice. K2.x is | ||||||||||||||||||||
| 98 | # undocumented on this face and keeps the synthetic tool. Per model, so the | ||||||||||||||||||||
| 99 | # endpoint predicate _native_structured_output stays 0: this is still a shim, | ||||||||||||||||||||
| 100 | # and its manifest dialect stays anthropic-compat. | ||||||||||||||||||||
| 101 | 212 | ▶ | sub _native_structured_output_for_model { | ||||||||||||||||||
|
Subroutine
_native_structured_output_for_model: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 102 | 212 | my ( $self ) = @_; | |||||||||||||||||||
| 103 | 212 | ▶ | return ( $self->chat_model // '' ) =~ /\Akimi-k3(?!\d)/ ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->chat_model // ""
MC/DC0%0/1
$self->chat_model // ""
$self->chat_model
Truth table50%1/2
A
A= $self->chat_model
| |||||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | # This endpoint speaks the `thinking` on/off toggle (ADR 0023 k209 Update): the | ||||||||||||||||||||
| 107 | # thinking-toggle Reasoning::Profile rows serialize as the toggle only on an | ||||||||||||||||||||
| 108 | # engine that opts in here; the same model id elsewhere keeps its effort wire. | ||||||||||||||||||||
| 109 | 126 | ▶ | sub _reasoning_thinking_toggle { 1 } | ||||||||||||||||||
|
Subroutine
_reasoning_thinking_toggle: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 14 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 14 | 1; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||