fix: Claude code rate limit error per new Claude changes

This commit is contained in:
2026-05-27 14:06:17 -06:00
parent 8a53b7934b
commit 097d8936e3
+21 -27
View File
@@ -114,41 +114,35 @@ async fn prepare_chat_completions(
/// ///
/// This behavior was discovered 2026-03-17. /// This behavior was discovered 2026-03-17.
/// ///
/// So this function injects the Claude Code system prompt into the request /// The prefix must be in its **own** top-level system block. Concatenating it
/// body to make it a valid request. /// with role / session content into a single block causes Anthropic to reject
/// the request with `rate_limit_error`. Any pre-existing system content is
/// preserved as additional blocks after the prefix.
fn inject_oauth_system_prompt(body: &mut Value) { fn inject_oauth_system_prompt(body: &mut Value) {
let existing_text = match body.get("system") { let existing_blocks: Vec<Value> = match body.get("system") {
Some(Value::String(s)) => { Some(Value::String(s)) => {
if s.starts_with(CLAUDE_CODE_PREFIX) { if s.is_empty() {
return; Vec::new()
} else {
vec![json!({ "type": "text", "text": s })]
} }
(!s.is_empty()).then(|| s.clone())
} }
Some(Value::Array(blocks)) => { Some(Value::Array(blocks)) => blocks.clone(),
let already_injected = blocks.iter().any(|b| { _ => Vec::new(),
b.get("text") };
.and_then(|t| t.as_str())
.map(|t| t.starts_with(CLAUDE_CODE_PREFIX)) let already_injected = existing_blocks
.unwrap_or(false) .first()
}); .and_then(|b| b.get("text").and_then(|t| t.as_str()))
.map(|t| t == CLAUDE_CODE_PREFIX)
.unwrap_or(false);
if already_injected { if already_injected {
return; return;
} }
let joined: Vec<String> = blocks
.iter()
.filter_map(|b| b.get("text").and_then(|t| t.as_str()).map(String::from))
.collect();
(!joined.is_empty()).then(|| joined.join("\n\n"))
}
_ => None,
};
let merged = match existing_text { let mut system = vec![json!({ "type": "text", "text": CLAUDE_CODE_PREFIX })];
Some(rest) => format!("{}\n\n{}", CLAUDE_CODE_PREFIX, rest), system.extend(existing_blocks);
None => CLAUDE_CODE_PREFIX.to_string(), body["system"] = Value::Array(system);
};
body["system"] = json!([{ "type": "text", "text": merged }]);
} }
pub async fn claude_chat_completions( pub async fn claude_chat_completions(