| 1 | package Langertha::Engine::Moonshot; | ||||||||||||
| 2 | # ABSTRACT: Moonshot AI Kimi API (OpenAI-compatible) | ||||||||||||
| 3 | 16 | our $VERSION = '0.505'; | |||||||||||
| 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 | |||||||||||||
| 7 | 16 | extends 'Langertha::Engine::OpenAIBase'; | |||||||||||
| 8 | |||||||||||||
| 9 | # StreamUsage: the chat API reference names stream_options.include_usage | ||||||||||||
| 10 | # (platform.kimi.ai/docs/api/chat, ADR 0038). | ||||||||||||
| 11 | 16 | with map { 'Langertha::Role::'.$_ } qw( | |||||||||||
| 12 | StaticModels | ||||||||||||
| 13 | Tools | ||||||||||||
| 14 | StreamUsage | ||||||||||||
| 15 | ); | ||||||||||||
| 16 | |||||||||||||
| 17 | |||||||||||||
| 18 | 134 | ▶ | sub _build_supported_operations {[qw( | ||||||||||
|
Subroutine
_build_supported_operations: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | createChatCompletion | ||||||||||||
| 20 | )]} | ||||||||||||
| 21 | |||||||||||||
| 22 | has '+url' => ( | ||||||||||||
| 23 | lazy => 1, | ||||||||||||
| 24 | 111 | ▶ | default => sub { 'https://api.moonshot.ai/v1' }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 25 | 16 | ); | |||||||||||
| 26 | |||||||||||||
| 27 | 1 | ▶ | sub _build_api_key { | ||||||||||
|
Subroutine
_build_api_key: called CC=2 SCAR=7.2
| |||||||||||||
| 28 | 1 | my ( $self ) = @_; | |||||||||||
| 29 | return $ENV{LANGERTHA_MOONSHOT_API_KEY} | ||||||||||||
| 30 | 1 | ▶ | || croak "".(ref $self)." requires LANGERTHA_MOONSHOT_API_KEY or api_key set"; | ||||||||||
|
Condition50%1/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 table50%1/2
A
A= $ENV{'LANGERTHA_MOONSHOT_API_KEY'}
| |||||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | 31 | ▶ | sub default_model { 'kimi-k3' } | ||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 34 | |||||||||||||
| 35 | # Kimi vision takes base64 data URLs (or ms:// file ids), no remote image URLs | ||||||||||||
| 36 | # (platform.kimi.ai/docs/guide/use-kimi-vision-model), karr k267. | ||||||||||||
| 37 | 8 | ▶ | sub _content_inline_images_only { 1 } | ||||||||||
|
Subroutine
_content_inline_images_only: called CC=1 SCAR=0.0
| |||||||||||||
| 38 | |||||||||||||
| 39 | 39 | ▶ | sub default_response_size { 4096 } | ||||||||||
|
Subroutine
default_response_size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 40 | |||||||||||||
| 41 | # Kimi counts reasoning_content against max_tokens and recommends | ||||||||||||
| 42 | # max_tokens >= 16000 while thinking is on (karr k225; platform.kimi.ai, advisor | ||||||||||||
| 43 | # 2026-09-25 on k219, docs only). kimi-k3 and kimi-k2.7-code(-highspeed) always | ||||||||||||
| 44 | # think, kimi-k2.6 thinks by default, so they default to 16000 instead of 4096. | ||||||||||||
| 45 | # Only when no response_size is set; max_tokens is a ceiling, billing follows | ||||||||||||
| 46 | # the tokens produced (ADR 0019 k225 Update). | ||||||||||||
| 47 | 327 | ▶ | sub model_response_size_defaults { | ||||||||||
|
Subroutine
model_response_size_defaults: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 48 | return ( | ||||||||||||
| 49 | 327 | qr/\Akimi-k3(?!\d)/ => 16000, | |||||||||||
| 50 | qr/\Akimi-k2\.(?:6|7-code(?:-highspeed)?)\z/ => 16000, | ||||||||||||
| 51 | ); | ||||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | 1 | ▶ | sub _build_static_models {[ | ||||||||||
|
Subroutine
_build_static_models: called CC=1 SCAR=0.0
| |||||||||||||
| 55 | 1 | { id => 'kimi-k3' }, | |||||||||||
| 56 | { id => 'kimi-k2.7-code' }, | ||||||||||||
| 57 | { id => 'kimi-k2.7-code-highspeed' }, | ||||||||||||
| 58 | { id => 'kimi-k2.6' }, | ||||||||||||
| 59 | ]} | ||||||||||||
| 60 | |||||||||||||
| 61 | # Per-model tool_choice reality on Kimi's OpenAI-compatible endpoint | ||||||||||||
| 62 | # (ADR 0002 amendment, ADR 0019; platform.kimi.ai/docs/guide/use-tool-choice): | ||||||||||||
| 63 | # * kimi-k3 (the engine default) always thinks, and forcing a *specific* | ||||||||||||
| 64 | # tool is incompatible with thinking -> a named tool_choice returns 400. | ||||||||||||
| 65 | # Clear tool_choice_named there (auto/any/none stay). | ||||||||||||
| 66 | # * The K2.x line does not support `required` and errors if it is passed. | ||||||||||||
| 67 | # Canonical `any` serializes to wire `required` (Langertha::ToolChoice), | ||||||||||||
| 68 | # so clear tool_choice_any there (auto/none/named stay). | ||||||||||||
| 69 | # * Reasoning is per model too (karr k207, platform.kimi.ai models overview, | ||||||||||||
| 70 | # advisor 2026-09-25, docs only): kimi-k3 takes a top-level | ||||||||||||
| 71 | # reasoning_effort (low|high|max, server default max; the accepted set | ||||||||||||
| 72 | # lives in its Reasoning::Profile row), while the K2.x line takes only the | ||||||||||||
| 73 | # Kimi `thinking` object. Clear reasoning_effort there (dotted and | ||||||||||||
| 74 | # dash-form K2 ids alike, e.g. kimi-k2-thinking), and | ||||||||||||
| 75 | # Role::ReasoningEffort then sends no reasoning field (the k204 gate). | ||||||||||||
| 76 | # kimi-k2.6 alone is re-asserted (karr k219, exact id): it takes a | ||||||||||||
| 77 | # top-level thinking {type: enabled|disabled} (KimiK26ChatRequest schema, | ||||||||||||
| 78 | # kimi-k2-6-quickstart; advisor 2026-09-25, docs only, not live), which | ||||||||||||
| 79 | # the opted-in toggle (_reasoning_thinking_toggle below) serializes from | ||||||||||||
| 80 | # reasoning_effort. kimi-k2.7-code(-highspeed) stays cleared: `disabled` | ||||||||||||
| 81 | # is an error there and the guides say not to pass thinking at all, so | ||||||||||||
| 82 | # there is nothing to send. | ||||||||||||
| 83 | # * Temperature is fixed server-side on every current Kimi id (karr k214, | ||||||||||||
| 84 | # platform.kimi.ai models overview, advisor 2026-09-25, docs + third-party | ||||||||||||
| 85 | # 400 reports, not live): kimi-k3 and kimi-k2.7-code(-highspeed) take only | ||||||||||||
| 86 | # 1.0, kimi-k2.6 1.0 with thinking and only 0.6 without, anything else is a | ||||||||||||
| 87 | # 400. Not effort-dependent, and 1 is not safe either (k2.6 non-thinking), | ||||||||||||
| 88 | # so the flag is cleared and the field never goes out (ADR 0025 k214 Update). | ||||||||||||
| 89 | # * image_input (k266, llm-advisor, docs only, 2026-09-25): the vision models | ||||||||||||
| 90 | # kimi-k3, kimi-k2.6 and kimi-k2.7-code take image input (platform.kimi.ai | ||||||||||||
| 91 | # use-kimi-vision-model; base64 only, Role::Chat inlines URL images here, | ||||||||||||
| 92 | # k267); other ids are unchecked, so the catch-all first row clears the flag. | ||||||||||||
| 93 | # The rows are deliberately distinct per model — that is the discriminating | ||||||||||||
| 94 | # information the flat role-derived row could not carry. | ||||||||||||
| 95 | 620 | ▶ | sub model_capability_corrections { | ||||||||||
|
Subroutine
model_capability_corrections: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 96 | return ( | ||||||||||||
| 97 | 620 | qr/\A/ => { image_input => 0 }, | |||||||||||
| 98 | 'kimi-k3' => { tool_choice_named => 0 }, | ||||||||||||
| 99 | qr/\Akimi-k3(?!\d)/ => { temperature => 0 }, | ||||||||||||
| 100 | qr/\Akimi-k2(?!\d)/ => { tool_choice_any => 0, reasoning_effort => 0, temperature => 0 }, | ||||||||||||
| 101 | 'kimi-k2.6' => { reasoning_effort => 1 }, | ||||||||||||
| 102 | qr/\Akimi-k(?:3|2\.6|2\.7-code)(?!\d)/ => { image_input => 1 }, | ||||||||||||
| 103 | ); | ||||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | # Kimi's chat/completions schema (platform.kimi.ai/docs/api/chat) has no | ||||||||||||
| 107 | # parallel_tool_calls field; only its separate Responses API lists one. Clear | ||||||||||||
| 108 | # parallel_tool_use engine-wide so it is dropped with a carp rather than sent | ||||||||||||
| 109 | # unhonored (karr k242, docs only). | ||||||||||||
| 110 | 620 | ▶ | around engine_capabilities => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 111 | 620 | my ( $orig, $self, @rest ) = @_; | |||||||||||
| 112 | 620 | my $caps = $self->$orig(@rest); | |||||||||||
| 113 | 620 | delete $caps->{parallel_tool_use}; | |||||||||||
| 114 | 620 | return $caps; | |||||||||||
| 115 | 16 | }; | |||||||||||
| 116 | |||||||||||||
| 117 | # Kimi's chat/completions speaks the K2.x `thinking` on/off toggle as a | ||||||||||||
| 118 | # top-level object (karr k219; ADR 0023 k209 Update: the toggle is an | ||||||||||||
| 119 | # endpoint's opt-in). Only kimi-k2.6 reaches it: its Profile row maps none -> | ||||||||||||
| 120 | # {type: disabled}, any other level -> {type: enabled}, and never sends `keep`. | ||||||||||||
| 121 | # kimi-k3 has no toggle row and keeps its reasoning_effort (k207). | ||||||||||||
| 122 | 73 | ▶ | sub _reasoning_thinking_toggle { 1 } | ||||||||||
|
Subroutine
_reasoning_thinking_toggle: called CC=1 SCAR=0.0
| |||||||||||||
| 123 | |||||||||||||
| 124 | 16 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 125 | |||||||||||||
| 126 | |||||||||||||
| 127 | 16 | 1; | |||||||||||
| 128 | |||||||||||||
| 129 | __END__ | ||||||||||||