refactor(proxy): replace panic-prone unwrap/expect with safe patterns

Harden the proxy module against panics by removing optimistic
unwrap()/expect() calls in favor of pattern matching and graceful
fallbacks:

- copilot_optimizer/cache_injector: bind Value::Array/String directly
  instead of is_array()+as_array().unwrap(); use is_none_or and in-place
  string mutation
- hyper_client: gate the raw-write path with if-let + filter instead of
  has_cases + unwrap()
- gemini_shadow: recover poisoned RwLock via into_inner() rather than
  panicking, with warn logging
- streaming_codex_chat: replace expect("tool state exists") with
  let-else (return/continue)
- merge_tool_results: early-return when messages is absent
- sse: fall back to from_utf8_lossy on the UTF-8 boundary slice

No behavior change on the happy path; all proxy tests pass.
This commit is contained in:
Jason
2026-05-22 00:08:37 +08:00
parent 279b9eabde
commit c12d20efd0
7 changed files with 76 additions and 67 deletions
+2 -5
View File
@@ -75,15 +75,12 @@ pub fn optimize(body: &mut Value, config: &OptimizerConfig) {
/// 追加 beta 标识到 anthropic_beta 数组(去重)
fn append_beta(body: &mut Value, beta: &str) {
match body.get("anthropic_beta") {
match body.get_mut("anthropic_beta") {
Some(Value::Array(arr)) => {
if arr.iter().any(|v| v.as_str() == Some(beta)) {
return;
}
body["anthropic_beta"]
.as_array_mut()
.unwrap()
.push(json!(beta));
arr.push(json!(beta));
}
Some(Value::Null) | None => {
body["anthropic_beta"] = json!([beta]);