Skip to content

feat(managed-kimi-code): support Anthropic-compatible protocol - #1170

Merged
sailist merged 3 commits into
MoonshotAI:mainfrom
sailist:feat/anthropic
Jun 28, 2026
Merged

feat(managed-kimi-code): support Anthropic-compatible protocol#1170
sailist merged 3 commits into
MoonshotAI:mainfrom
sailist:feat/anthropic

Conversation

@sailist

@sailist sailist commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator

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 413 context-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 /models protocol: 'anthropic') could not be used, because the managed provider was hardcoded to type: '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:

  • Switch the managed provider to type: 'anthropic' when any provisioned model declares protocol: 'anthropic', deriving the Anthropic baseUrl by stripping the trailing /v1 from the kimi baseUrl (the Anthropic SDK appends /v1/messages itself).
  • Add base64 video content blocks to the kosong Anthropic provider (Kimi's non-standard wire extension), replacing the previous "video omitted" placeholder for video_url parts.
  • Add downgradeUnsupportedMedia in KosongLLM to 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.
  • Forward the prompt cache key as Anthropic metadata.user_id for session affinity, mirroring OpenAI's prompt_cache_key.

2. Recover from provider 413 context overflows

Problem: When the provider rejects a request with a 413 context-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:

  • Track the provider-observed effective context limit after an overflow.
  • Compact history against the reduced limit before retrying the turn.
  • Treat large plain 413 responses as recoverable context overflow.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Jun 28, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e64d7c4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread packages/oauth/src/managed-kimi-code.ts Outdated
type: 'kimi',
baseUrl,
type: protocol,
baseUrl: protocol === 'anthropic' ? anthropicBaseUrl(baseUrl) : baseUrl,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment thread packages/oauth/src/managed-kimi-code.ts Outdated
config.providers[KIMI_CODE_PROVIDER_NAME] = {
type: 'kimi',
baseUrl,
type: protocol,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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
@pkg-pr-new

pkg-pr-new Bot commented Jun 28, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@e64d7c4
npx https://pkg.pr.new/@moonshot-ai/kimi-code@e64d7c4

commit: e64d7c4

@sailist

sailist commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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
@sailist

sailist commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +238 to +241
const effectiveType = modelProtocol === 'anthropic' ? 'anthropic' : provider.type;
switch (effectiveType) {
case 'anthropic': {
const baseUrl = providerValue(provider.baseUrl, provider.env, 'ANTHROPIC_BASE_URL');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@sailist
sailist merged commit cf558cd into MoonshotAI:main Jun 28, 2026
9 of 10 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 28, 2026
Moixia pushed a commit to Moixia/idea that referenced this pull request Jun 29, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant