Skip to content

Commit 9a4acbb

Browse files
committed
fix(kimi): attach reasoning_content on tool-call messages for Kimi coding endpoint
Moonshot's dedicated Kimi coding endpoint (https://api.kimi.com/coding/v1, model kimi-for-coding) enables thinking server-side and rejects any assistant tool-call message that lacks reasoning_content with 400 "thinking is enabled but reasoning_content is missing in assistant tool call message" (issue 1jehuang#322). Root cause: the endpoint's model id (kimi-for-coding) is not matched by is_kimi_model (which only covers moonshotai/ and kimi-k2 variants), and the profile runs as a direct OpenAI-compatible provider (supports_provider_features = false). Both conditions gated reasoning_content out, so the request builder emitted bare assistant tool-call messages and the endpoint rejected the whole request. Fix: detect the dedicated Kimi coding endpoint (by profile id, api base, or kimi* model name) and treat it as reasoning-required so reasoning_content is attached to assistant tool-call messages. Crucially this does NOT add the OpenRouter-specific top-level thinking field, since the endpoint manages thinking itself. Adds a regression test capturing the request body and asserting the assistant tool-call message carries a non-empty reasoning_content. Fixes 1jehuang#322
1 parent c9d3d3d commit 9a4acbb

3 files changed

Lines changed: 128 additions & 4 deletions

File tree

‎crates/jcode-base/src/provider/openrouter.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,20 @@ impl OpenRouterProvider {
11721172
jcode_provider_openrouter::is_kimi_model(model)
11731173
}
11741174

1175+
/// Return true when this request targets Moonshot's dedicated Kimi coding
1176+
/// endpoint (`https://api.kimi.com/coding/v1`, default model
1177+
/// `kimi-for-coding`). That endpoint enables thinking server-side and
1178+
/// rejects any assistant tool-call message that lacks `reasoning_content`
1179+
/// (issue #322). The endpoint's own model id (`kimi-for-coding`) is not
1180+
/// caught by `is_kimi_model`, so detect it by profile/api-base/model.
1181+
fn is_kimi_coding_endpoint(&self, model: &str) -> bool {
1182+
self.profile_id
1183+
.as_deref()
1184+
.is_some_and(|id| id.eq_ignore_ascii_case("kimi"))
1185+
|| is_kimi_coding_api_base(&self.api_base)
1186+
|| is_kimi_model_name(model)
1187+
}
1188+
11751189
/// Parse thinking override from env. Values: "enabled"/"disabled"/"auto".
11761190
/// Returns Some(true)=force enable, Some(false)=force disable, None=auto.
11771191
fn thinking_override() -> Option<bool> {

‎crates/jcode-base/src/provider/openrouter_provider_impl.rs‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,27 @@ impl Provider for OpenRouterProvider {
1414
let model = self.model.read().await.clone();
1515
let reasoning_effort = self.reasoning_effort();
1616
let thinking_override = Self::thinking_override();
17+
// Moonshot's dedicated Kimi coding endpoint enables thinking server-side
18+
// by default and rejects any assistant tool-call message that lacks
19+
// `reasoning_content`, even though its model id (`kimi-for-coding`) is
20+
// not a moonshotai/kimi-k2 model and the profile runs without OpenRouter
21+
// provider features (issue #322). We must attach `reasoning_content` to
22+
// those messages, but must NOT add the OpenRouter-specific top-level
23+
// `thinking` field (the endpoint already manages thinking itself), so
24+
// this is kept separate from `thinking_enabled`.
25+
let kimi_coding_endpoint = self.is_kimi_coding_endpoint(&model);
1726
let thinking_enabled = thinking_override.or_else(|| {
1827
if Self::is_kimi_model(&model) {
1928
Some(true)
2029
} else {
2130
None
2231
}
2332
});
24-
let allow_reasoning = self.supports_provider_features && thinking_enabled != Some(false);
25-
let include_reasoning_content =
26-
thinking_enabled == Some(true) || (allow_reasoning && Self::is_kimi_model(&model));
33+
let allow_reasoning = (self.supports_provider_features || kimi_coding_endpoint)
34+
&& thinking_enabled != Some(false);
35+
let include_reasoning_content = thinking_enabled == Some(true)
36+
|| (allow_reasoning && Self::is_kimi_model(&model))
37+
|| kimi_coding_endpoint;
2738

2839
// Some OpenAI-compatible providers (e.g. Mistral) strictly enforce the
2940
// OpenAI schema and reject the non-standard `reasoning_content` message

‎crates/jcode-base/src/provider/openrouter_tests.rs‎

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ fn interrupted_reasoning_only_assistant_message_is_not_sent_empty() {
458458
"assistant message must carry content or tool_calls (issue #321); got: {msg}"
459459
);
460460
}
461-
eprintln!("DBG321 BODY = {}", serde_json::to_string_pretty(&body).unwrap());
462461
}
463462

464463
/// Companion to issue #321: when the provider *does* support reasoning replay
@@ -547,6 +546,106 @@ fn interrupted_reasoning_only_assistant_message_keeps_reasoning_with_content() {
547546
);
548547
}
549548

549+
/// Regression for issue #322: the dedicated Kimi coding endpoint
550+
/// (`https://api.kimi.com/coding/v1`, model `kimi-for-coding`) enables thinking
551+
/// server-side and rejects any assistant tool-call message that lacks
552+
/// `reasoning_content` with 400 "thinking is enabled but reasoning_content is
553+
/// missing in assistant tool call message". When an assistant turn produced a
554+
/// tool call without an accompanying reasoning block (the common case once the
555+
/// thinking stream is not persisted), the request builder must still attach a
556+
/// `reasoning_content` field to that assistant message so the endpoint accepts
557+
/// the request.
558+
#[test]
559+
fn kimi_for_coding_tool_call_message_includes_reasoning_content() {
560+
let _lock = ENV_LOCK.lock();
561+
let _thinking = EnvVarGuard::remove("JCODE_OPENROUTER_THINKING");
562+
let (api_base, request_rx) = spawn_single_response_chat_server();
563+
let provider = OpenRouterProvider {
564+
api_base,
565+
// The dedicated Kimi coding endpoint is a direct OpenAI-compatible
566+
// profile (no OpenRouter provider routing features).
567+
profile_id: Some("kimi".to_string()),
568+
supports_provider_features: false,
569+
supports_model_catalog: false,
570+
model: Arc::new(RwLock::new("kimi-for-coding".to_string())),
571+
..make_custom_compatible_provider()
572+
};
573+
574+
let messages = vec![
575+
Message {
576+
role: Role::User,
577+
content: vec![ContentBlock::Text {
578+
text: "list the files".to_string(),
579+
cache_control: None,
580+
}],
581+
timestamp: None,
582+
tool_duration_ms: None,
583+
},
584+
// Assistant turn that emitted a tool call but whose hidden reasoning was
585+
// not persisted (so there is no Reasoning block to replay).
586+
Message {
587+
role: Role::Assistant,
588+
content: vec![ContentBlock::ToolUse {
589+
id: "call_1".to_string(),
590+
name: "bash".to_string(),
591+
input: serde_json::json!({"command": "ls"}),
592+
thought_signature: None,
593+
}],
594+
timestamp: None,
595+
tool_duration_ms: None,
596+
},
597+
Message {
598+
role: Role::User,
599+
content: vec![ContentBlock::ToolResult {
600+
tool_use_id: "call_1".to_string(),
601+
content: "a.txt\nb.txt".to_string(),
602+
is_error: None,
603+
}],
604+
timestamp: None,
605+
tool_duration_ms: None,
606+
},
607+
];
608+
609+
let rt = tokio::runtime::Builder::new_current_thread()
610+
.enable_all()
611+
.build()
612+
.expect("runtime");
613+
rt.block_on(async {
614+
let mut stream = provider
615+
.complete(&messages, &[], "", None)
616+
.await
617+
.expect("fake chat request should start");
618+
while let Some(event) = stream.next().await {
619+
if event.is_err() {
620+
break;
621+
}
622+
}
623+
});
624+
625+
let request = request_rx
626+
.recv_timeout(Duration::from_secs(2))
627+
.expect("capture fake provider request");
628+
let body = parse_captured_request_body(&request);
629+
let api_messages = body
630+
.get("messages")
631+
.and_then(|m| m.as_array())
632+
.expect("request should contain messages array");
633+
634+
let assistant = api_messages
635+
.iter()
636+
.find(|msg| {
637+
msg.get("role").and_then(|v| v.as_str()) == Some("assistant")
638+
&& msg.get("tool_calls").is_some()
639+
})
640+
.expect("request should retain the assistant tool-call turn");
641+
642+
let reasoning = assistant.get("reasoning_content");
643+
assert!(
644+
reasoning.is_some_and(|value| value.as_str().is_some_and(|s| !s.is_empty())),
645+
"Kimi coding endpoint requires reasoning_content on assistant tool-call messages (issue #322); got: {assistant}"
646+
);
647+
}
648+
550649
#[test]
551650
fn default_named_openai_compatible_provider_uses_direct_compatible_request_path() {
552651
let _lock = ENV_LOCK.lock();

0 commit comments

Comments
 (0)