-
Notifications
You must be signed in to change notification settings - Fork 425
Support Kimi Code API for Claude Code routing #951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Spherrrical
wants to merge
3
commits into
main
Choose a base branch
from
musa/kimi-code-api-8e77
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| use crate::apis::anthropic::MessagesRequest; | ||
| use crate::apis::openai::ChatCompletionsRequest; | ||
| use crate::apis::openai::{is_kimi_code_model, ChatCompletionsRequest}; | ||
| use log::warn; | ||
|
|
||
| use crate::apis::amazon_bedrock::{ConverseRequest, ConverseStreamRequest}; | ||
| use crate::apis::openai_responses::ResponsesAPIRequest; | ||
|
|
@@ -90,6 +91,24 @@ impl ProviderRequestType { | |
| } | ||
| } | ||
|
|
||
| if matches!( | ||
| upstream_api, | ||
| SupportedUpstreamAPIs::OpenAIChatCompletions(_) | ||
| ) { | ||
| if let Self::ChatCompletionsRequest(req) = self { | ||
| if is_kimi_code_model(req.model()) { | ||
| req.normalize_for_kimi_code_api(); | ||
| } | ||
| } else if let Self::MessagesRequest(req) = self { | ||
| if is_kimi_code_model(req.model.as_str()) && req.thinking.is_some() { | ||
| warn!( | ||
| "kimi-for-coding: stripping unsupported thinking config from upstream request" | ||
| ); | ||
| req.thinking = None; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here if thinking is set then we should emit a warn |
||
| } | ||
| } | ||
| } | ||
|
|
||
| // ChatGPT requires instructions, store=false, and input as a list | ||
| if provider_id == ProviderId::ChatGPT { | ||
| if let Self::ResponsesAPIRequest(req) = self { | ||
|
|
@@ -879,6 +898,42 @@ mod tests { | |
| assert!(req.web_search_options.is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_normalize_for_upstream_kimi_code_strips_unsupported_chat_fields() { | ||
| use crate::apis::openai::{Message, MessageContent, OpenAIApi, Role, StreamOptions}; | ||
|
|
||
| let mut request = ProviderRequestType::ChatCompletionsRequest(ChatCompletionsRequest { | ||
| model: "kimi-for-coding".to_string(), | ||
| messages: vec![Message { | ||
| role: Role::User, | ||
| content: Some(MessageContent::Text("hello".to_string())), | ||
| name: None, | ||
| tool_calls: None, | ||
| tool_call_id: None, | ||
| }], | ||
| stream_options: Some(StreamOptions { | ||
| include_usage: Some(true), | ||
| }), | ||
| reasoning_effort: Some("high".to_string()), | ||
| web_search_options: Some(serde_json::json!({"search_context_size":"medium"})), | ||
| ..Default::default() | ||
| }); | ||
|
|
||
| request | ||
| .normalize_for_upstream( | ||
| ProviderId::Moonshotai, | ||
| &SupportedUpstreamAPIs::OpenAIChatCompletions(OpenAIApi::ChatCompletions), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let ProviderRequestType::ChatCompletionsRequest(req) = request else { | ||
| panic!("expected chat request"); | ||
| }; | ||
| assert!(req.stream_options.is_none()); | ||
| assert!(req.reasoning_effort.is_none()); | ||
| assert!(req.web_search_options.is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_normalize_for_upstream_non_xai_keeps_chat_web_search_options() { | ||
| use crate::apis::openai::{Message, MessageContent, OpenAIApi, Role}; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if they are set? Would we silently ignore these fields? I think we should emit warn if any of these fields are set so not to cause any unwanted behavior.