feat(managed-kimi-code): support Anthropic-compatible protocol - #1170
Conversation
🦋 Changeset detectedLatest commit: e64d7c4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 481123d354
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| type: 'kimi', | ||
| baseUrl, | ||
| type: protocol, | ||
| baseUrl: protocol === 'anthropic' ? anthropicBaseUrl(baseUrl) : baseUrl, |
There was a problem hiding this comment.
Keep the managed REST base URL unstripped
When /models returns protocol: 'anthropic', this persists the Anthropic SDK base (https://.../coding) into the managed provider, but other managed-code paths still interpret provider.baseUrl as the REST base (.../coding/v1): resolveKimiCodeRuntimeAuth derives the OAuth storage key from it, and managedKimiCodeEnvForPlugins forwards it as KIMI_CODE_BASE_URL to the datasource plugin, which calls ${base}/tools. That makes non-default-base logins look up a different token after provisioning, and even the default-base plugin session call /coding/tools instead of /coding/v1/tools; derive the Anthropic transport base at provider resolution rather than overwriting the managed REST base.
Useful? React with 👍 / 👎.
| config.providers[KIMI_CODE_PROVIDER_NAME] = { | ||
| type: 'kimi', | ||
| baseUrl, | ||
| type: protocol, |
There was a problem hiding this comment.
Keep Anthropic managed providers refreshable
After this assignment flips the managed provider from kimi to anthropic, model-refresh paths stop considering it: ModelCatalogService.refreshOAuthProviderModels returns unless provider.type === 'kimi', and the TUI refresh utility has the same managedProvider.type === 'kimi' guard. Users on the new protocol can refresh once from an old config, but subsequent refreshes never fetch updated managed models; the refresh checks need to accept the managed OAuth provider regardless of transport type.
Useful? React with 👍 / 👎.
- track provider-observed effective context limit after overflow - compact with the reduced limit before retrying the turn - treat large plain 413 responses as recoverable context overflow - add CLI patch changeset
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b51fbc776f
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| // Session affinity: Anthropic's analog of OpenAI `prompt_cache_key` is | ||
| // `metadata.user_id` on the Messages API (cache-affinity / end-user id). | ||
| ...(promptCacheKey !== undefined ? { metadata: { user_id: promptCacheKey } } : {}), | ||
| ...defaultHeadersField(provider.customHeaders), |
There was a problem hiding this comment.
Forward Kimi identity headers for anthropic-protocol models
When a managed Kimi model declares protocol: 'anthropic', this branch builds an Anthropic provider but drops kimiRequestHeaders, forwarding only provider.customHeaders. The Kimi wire path below still merges those headers, and CoreProcessService explicitly documents that managed Kimi-for-Coding requests need the synthesized User-Agent/X-Msh-* identity headers or the endpoint rejects them with 40340; with the new per-model protocol, normal TUI/daemon sessions for these managed models will hit the Anthropic transport without those required headers unless the user manually duplicates them in config.
Useful? React with 👍 / 👎.
- switch managed provider to anthropic when models declare anthropic protocol - add base64 video content blocks to the kosong anthropic provider - downgrade unsupported media parts to text placeholders by capability - pass prompt cache key as Anthropic metadata.user_id for session affinity
…etry - turn_started now carries `type` (configured provider wire type) and `protocol` (effective transport, i.e. alias.protocol ?? provider.type) - new video_upload event reports mime type, size, latency and success/failure, plus type/protocol/model context - ResolvedRuntimeProvider gains `type` and `protocol` fields
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e64d7c4ab9
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const effectiveType = modelProtocol === 'anthropic' ? 'anthropic' : provider.type; | ||
| switch (effectiveType) { | ||
| case 'anthropic': { | ||
| const baseUrl = providerValue(provider.baseUrl, provider.env, 'ANTHROPIC_BASE_URL'); |
There was a problem hiding this comment.
Preserve Kimi headers for protocol-overridden requests
When a managed model has protocol: 'anthropic', applyManagedKimiCodeConfig still stores the provider as type: 'kimi' and only annotates the model alias. Routing that Kimi provider through the Anthropic branch here means the request no longer uses the Kimi branch that merges kimiRequestHeaders, so the new managed Anthropic-compatible sessions drop the CLI User-Agent and X-Msh-* identity headers that existing Kimi requests intentionally send. This also makes Kimi env-derived provider settings easy to bypass; derive Kimi base URL/headers from the original provider before swapping only the transport.
Useful? React with 👍 / 👎.
…hotAI#1170) * fix(agent-core): recover from context overflow 413 - track provider-observed effective context limit after overflow - compact with the reduced limit before retrying the turn - treat large plain 413 responses as recoverable context overflow - add CLI patch changeset * feat(managed-kimi-code): support Anthropic-compatible protocol - switch managed provider to anthropic when models declare anthropic protocol - add base64 video content blocks to the kosong anthropic provider - downgrade unsupported media parts to text placeholders by capability - pass prompt cache key as Anthropic metadata.user_id for session affinity * feat(agent-core): add protocol/type to request and video upload telemetry - turn_started now carries `type` (configured provider wire type) and `protocol` (effective transport, i.e. alias.protocol ?? provider.type) - new video_upload event reports mime type, size, latency and success/failure, plus type/protocol/model context - ResolvedRuntimeProvider gains `type` and `protocol` fields
Summary
This PR bundles two related provider-side improvements for Kimi Code: support for the Anthropic-compatible protocol on managed Kimi Code (including video input and capability-aware media downgrade), and recovery from provider
413context-overflow errors by compacting before retrying the turn.1. Anthropic-compatible protocol for managed Kimi Code
Problem: Managed Kimi Code models that speak the Anthropic-compatible protocol (advertised via
/modelsprotocol: 'anthropic') could not be used, because the managed provider was hardcoded totype: 'kimi'. On top of that, the kosong Anthropic provider had no representation for video input, and models lacking image/video/audio capability were still handed media parts they cannot process.What was done:
type: 'anthropic'when any provisioned model declaresprotocol: 'anthropic', deriving the AnthropicbaseUrlby stripping the trailing/v1from the kimi baseUrl (the Anthropic SDK appends/v1/messagesitself).videocontent blocks to the kosong Anthropic provider (Kimi's non-standard wire extension), replacing the previous "video omitted" placeholder forvideo_urlparts.downgradeUnsupportedMediainKosongLLMto replace image/video/audio parts with explicit text placeholders when the active model lacks the corresponding capability, so unsupported attachments are acknowledged rather than silently sent.metadata.user_idfor session affinity, mirroring OpenAI'sprompt_cache_key.2. Recover from provider 413 context overflows
Problem: When the provider rejects a request with a
413context-overflow error, the turn failed outright instead of recovering. Large plain-text (non-JSON) 413 responses were also not recognized as context overflow at all.What was done:
Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.