Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions src/providers/kimi/translate/model_allowlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ static ALIAS_TARGETS: once_cell::sync::Lazy<HashMap<&'static str, &'static str>>
m.insert("fable", KIMI_DEFAULT_MODEL);
m.insert("claude-fable-5", KIMI_DEFAULT_MODEL);
m.insert("kimi-for-coding", KIMI_DEFAULT_MODEL);
m.insert("kimi-k3", "k3");
m.insert("k3", "k3");
m
});

const ALLOWED_MODELS: &[&str] = &["kimi-for-coding", "k3"];

pub fn resolve_model(model: &str) -> String {
ALIAS_TARGETS
.get(model)
Expand All @@ -28,13 +32,18 @@ pub fn resolve_model(model: &str) -> String {
.to_string()
}

pub fn is_k3(model: &str) -> bool {
model == "k3"
}

pub fn assert_allowed_model(model: &str) -> Result<(), ModelNotAllowedError> {
if model != KIMI_DEFAULT_MODEL {
return Err(ModelNotAllowedError {
if ALLOWED_MODELS.contains(&model) {
Ok(())
} else {
Err(ModelNotAllowedError {
model: model.to_string(),
});
})
}
Ok(())
}

#[derive(Debug)]
Expand Down Expand Up @@ -81,11 +90,32 @@ mod tests {
assert_eq!(resolve_model("kimi-for-coding"), KIMI_DEFAULT_MODEL);
}

#[test]
fn resolve_kimi_k3_to_k3() {
assert_eq!(resolve_model("kimi-k3"), "k3");
}

#[test]
fn resolve_k3_to_k3() {
assert_eq!(resolve_model("k3"), "k3");
}

#[test]
fn k3_detected() {
assert!(is_k3("k3"));
assert!(!is_k3("kimi-for-coding"));
}

#[test]
fn assert_allowed_accepts_default() {
assert!(assert_allowed_model(KIMI_DEFAULT_MODEL).is_ok());
}

#[test]
fn assert_allowed_accepts_k3() {
assert!(assert_allowed_model("k3").is_ok());
}

#[test]
fn assert_allowed_rejects_other() {
assert!(assert_allowed_model("kimi-k2.6").is_err());
Expand Down
5 changes: 3 additions & 2 deletions src/providers/kimi/translate/reducer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ struct StreamError {

#[allow(dead_code)]
struct ToolSlot {
tc_index: usize,
block_index: usize,
id: String,
name: String,
Expand Down Expand Up @@ -270,7 +271,7 @@ pub fn reduce_upstream_bytes(input: &[u8]) -> Result<Vec<ReducerEvent>, Upstream
}

for tc in tool_calls {
let existing_pos = tool_slots.iter().position(|s| s.block_index == tc.index);
let existing_pos = tool_slots.iter().position(|s| s.tc_index == tc.index);
let block_index = if let Some(pos) = existing_pos {
tool_slots[pos].block_index
} else {
Expand All @@ -281,13 +282,13 @@ pub fn reduce_upstream_bytes(input: &[u8]) -> Result<Vec<ReducerEvent>, Upstream
.and_then(|f| f.name.clone())
.unwrap_or_default();
if id.is_empty() || name.is_empty() {
// Defensive: skip out-of-order fragments
continue;
}
saw_tool_calls = true;
let bi = next_block_index;
next_block_index += 1;
tool_slots.push(ToolSlot {
tc_index: tc.index,
block_index: bi,
id: id.clone(),
name: name.clone(),
Expand Down
Loading