diff --git a/apps/detail-photocraft/Cargo.lock b/apps/detail-photocraft/Cargo.lock index ed65b2f1..887d6621 100644 --- a/apps/detail-photocraft/Cargo.lock +++ b/apps/detail-photocraft/Cargo.lock @@ -4997,6 +4997,7 @@ dependencies = [ "futures-lite", "image", "keyring", + "flate2", "log", "muda", "objc2 0.6.5", diff --git a/apps/detail-photocraft/apps/photocraft/Cargo.toml b/apps/detail-photocraft/apps/photocraft/Cargo.toml index b4f38313..b00f0cc0 100644 --- a/apps/detail-photocraft/apps/photocraft/Cargo.toml +++ b/apps/detail-photocraft/apps/photocraft/Cargo.toml @@ -52,6 +52,7 @@ base64 = "0.22" sha2 = "0.10" url = "2" uuid = { version = "1", features = ["v4"] } +flate2 = "1" reqwest = { version = "0.13", default-features = false, features = ["blocking", "json", "rustls", "multipart"] } regex = "1" image = { workspace = true } diff --git a/apps/detail-photocraft/apps/photocraft/src/jiansun_backend.rs b/apps/detail-photocraft/apps/photocraft/src/jiansun_backend.rs index 840e4b3a..1c795aee 100644 --- a/apps/detail-photocraft/apps/photocraft/src/jiansun_backend.rs +++ b/apps/detail-photocraft/apps/photocraft/src/jiansun_backend.rs @@ -676,20 +676,44 @@ impl Backend { Ok(aliases) } fn public_model_catalog(&self) -> R { + self.public_model_catalog_with_timings().map(|(value, _)| value) + } + fn public_model_catalog_with_timings(&self) -> R<(Value, Value)> { + const LIMIT: u64 = 16 * 1024 * 1024; + let start = std::time::Instant::now(); #[cfg(debug_assertions)] if let Some(path) = std::env::var_os("PHOTOCRAFT_LOCAL_MODEL_CATALOG") { let mut bytes = Vec::new(); - fs::File::open(path).and_then(|file| file.take(16 * 1024 * 1024 + 1).read_to_end(&mut bytes)) + fs::File::open(path).and_then(|file| file.take(LIMIT + 1).read_to_end(&mut bytes)) .map_err(|_| "本地模型目录无法读取")?; - if bytes.len() > 16 * 1024 * 1024 { + if bytes.len() as u64 > LIMIT { return Err("本地模型目录过大".into()); } - return serde_json::from_slice(&bytes).map_err(|_| "本地模型目录格式无效".into()); + let catalog = serde_json::from_slice(&bytes).map_err(|_| "本地模型目录格式无效")?; + return Ok((catalog, json!({"source":"local","elapsedMs":start.elapsed().as_millis()}))); } - let response = self.http.get(format!("{UPSTREAM}/api/v2/catalog/models")).send().map_err(|_| "98k公开模型目录暂不可用")?; - let (status, catalog) = body(response, 16 * 1024 * 1024)?; + let response = self.http.get(format!("{UPSTREAM}/api/v2/catalog/models")) + .header(reqwest::header::ACCEPT_ENCODING, "gzip") + .send().map_err(|_| "98k公开模型目录暂不可用")?; + let headers_ms = start.elapsed().as_millis(); + let status = response.status().as_u16(); if status != 200 { return Err(format!("98k公开模型目录暂不可用(HTTP {status})")); } - Ok(catalog) + let content_length = response.content_length(); + let gzip = response.headers().get(reqwest::header::CONTENT_ENCODING) + .is_some_and(|value| value.as_bytes().eq_ignore_ascii_case(b"gzip")); + let mut bytes = Vec::new(); + let read = if gzip { + flate2::read::GzDecoder::new(response).take(LIMIT + 1).read_to_end(&mut bytes) + } else { + response.take(LIMIT + 1).read_to_end(&mut bytes) + }; + read.map_err(|_| "98k公开模型目录传输中断")?; + let body_ms = start.elapsed().as_millis() - headers_ms; + if bytes.len() as u64 > LIMIT { return Err("服务响应超过客户端限制".into()); } + let catalog = serde_json::from_slice(&bytes).map_err(|_| "98k公开模型目录格式无效")?; + Ok((catalog, json!({"source":"https","encoding":if gzip {"gzip"} else {"identity"}, + "wireBytes":content_length,"decodedBytes":bytes.len(),"headersMs":headers_ms,"bodyMs":body_ms, + "decodeJsonMs":start.elapsed().as_millis() - headers_ms - body_ms}))) } fn image_model_id(&self, choice: &str) -> R { if Uuid::parse_str(choice).is_ok() { diff --git a/apps/detail-photocraft/apps/photocraft/src/jiansun_pi.rs b/apps/detail-photocraft/apps/photocraft/src/jiansun_pi.rs index a478ac29..285772d8 100644 --- a/apps/detail-photocraft/apps/photocraft/src/jiansun_pi.rs +++ b/apps/detail-photocraft/apps/photocraft/src/jiansun_pi.rs @@ -563,7 +563,7 @@ fn selected(backend: &mut Backend, role: &str) -> R<(Value, Value, Value, Value) }); let catalog = workers.spawn(|| { let start = std::time::Instant::now(); - let result = backend.public_model_catalog(); + let result = backend.public_model_catalog_with_timings(); (result, start.elapsed().as_millis()) }); let start = std::time::Instant::now(); @@ -576,7 +576,7 @@ fn selected(backend: &mut Backend, role: &str) -> R<(Value, Value, Value, Value) let (authorized, key_ms) = authorized; let authorized = authorized?; let (catalog, catalog_ms) = catalog.map_err(|_| "98k公开模型目录暂不可用")?; - let catalog = catalog?; + let (catalog, catalog_phases) = catalog?; let model = if role == "detail" { let aliases = backend.legacy_text_choices(&settings); let published = Backend::text_models(&catalog, &aliases)?; @@ -587,7 +587,7 @@ fn selected(backend: &mut Backend, role: &str) -> R<(Value, Value, Value, Value) if !authorized.contains(&id) { return Err("个人Key未开放此模型".into()); } backend.text_choice_from_catalog(choice, &id, &catalog, true)? }; - let timings = json!({"portalMs":session_ms,"keyMs":key_ms,"catalogMs":catalog_ms}); + let timings = json!({"portalMs":session_ms,"keyMs":key_ms,"catalogMs":catalog_ms,"catalogPhases":catalog_phases}); let invocation = pi_runtime::invocation_from(&catalog, &model)?; Ok((settings, model, invocation, timings)) }