fix: merge required claude code system prompt into instructions
This commit is contained in:
+29
-24
@@ -117,33 +117,38 @@ async fn prepare_chat_completions(
|
|||||||
/// So this function injects the Claude Code system prompt into the request
|
/// So this function injects the Claude Code system prompt into the request
|
||||||
/// body to make it a valid request.
|
/// body to make it a valid request.
|
||||||
fn inject_oauth_system_prompt(body: &mut Value) {
|
fn inject_oauth_system_prompt(body: &mut Value) {
|
||||||
let prefix_block = json!({
|
let existing_text = match body.get("system") {
|
||||||
"type": "text",
|
Some(Value::String(s)) => {
|
||||||
"text": CLAUDE_CODE_PREFIX,
|
if s.starts_with(CLAUDE_CODE_PREFIX) {
|
||||||
});
|
return;
|
||||||
|
|
||||||
match body.get("system") {
|
|
||||||
Some(Value::String(existing)) => {
|
|
||||||
let existing_block = json!({
|
|
||||||
"type": "text",
|
|
||||||
"text": existing,
|
|
||||||
});
|
|
||||||
body["system"] = json!([prefix_block, existing_block]);
|
|
||||||
}
|
|
||||||
Some(Value::Array(_)) => {
|
|
||||||
if let Some(arr) = body["system"].as_array_mut() {
|
|
||||||
let already_injected = arr
|
|
||||||
.iter()
|
|
||||||
.any(|block| block["text"].as_str() == Some(CLAUDE_CODE_PREFIX));
|
|
||||||
if !already_injected {
|
|
||||||
arr.insert(0, prefix_block);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
(!s.is_empty()).then(|| s.clone())
|
||||||
}
|
}
|
||||||
_ => {
|
Some(Value::Array(blocks)) => {
|
||||||
body["system"] = json!([prefix_block]);
|
let already_injected = blocks.iter().any(|b| {
|
||||||
|
b.get("text")
|
||||||
|
.and_then(|t| t.as_str())
|
||||||
|
.map(|t| t.starts_with(CLAUDE_CODE_PREFIX))
|
||||||
|
.unwrap_or(false)
|
||||||
|
});
|
||||||
|
if already_injected {
|
||||||
|
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 {
|
||||||
|
Some(rest) => format!("{}\n\n{}", CLAUDE_CODE_PREFIX, rest),
|
||||||
|
None => CLAUDE_CODE_PREFIX.to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
body["system"] = json!([{ "type": "text", "text": merged }]);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn claude_chat_completions(
|
pub async fn claude_chat_completions(
|
||||||
|
|||||||
Reference in New Issue
Block a user