feat(comfy-cloud): job→ComfyUI history 翻译函数
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
//! Comfy Cloud 工作流执行适配器 service 层。
|
||||
pub mod site;
|
||||
pub mod translate;
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
use serde_json::{json, Value};
|
||||
|
||||
/// 把 Comfy Cloud 的 job status(+ 完成时的 jobs detail outputs)翻译成
|
||||
/// ComfyUI 原生 /history/{id} 的形状。
|
||||
///
|
||||
/// - status pending/in_progress → 返回空对象 {}(comfykit 据此继续轮询)
|
||||
/// - status completed → { "{id}": { "status": {completed:true, status_str:"success"}, "outputs": {...} } }
|
||||
/// - status failed/cancelled → { "{id}": { "status": {completed:false, status_str:"error"} } }
|
||||
pub fn comfy_cloud_job_to_history(
|
||||
prompt_id: &str,
|
||||
status: &str,
|
||||
outputs: Option<&Value>,
|
||||
) -> Value {
|
||||
match status {
|
||||
"completed" => json!({
|
||||
prompt_id: {
|
||||
"status": { "completed": true, "status_str": "success" },
|
||||
"outputs": outputs.cloned().unwrap_or_else(|| json!({})),
|
||||
}
|
||||
}),
|
||||
"failed" | "cancelled" => json!({
|
||||
prompt_id: {
|
||||
"status": { "completed": false, "status_str": "error" },
|
||||
"outputs": {},
|
||||
}
|
||||
}),
|
||||
_ => json!({}),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn in_progress_returns_empty() {
|
||||
assert_eq!(comfy_cloud_job_to_history("p1", "in_progress", None), json!({}));
|
||||
assert_eq!(comfy_cloud_job_to_history("p1", "pending", None), json!({}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completed_wraps_outputs() {
|
||||
let outputs = json!({ "9": { "images": [{ "filename": "a.png", "subfolder": "", "type": "output" }] } });
|
||||
let h = comfy_cloud_job_to_history("p1", "completed", Some(&outputs));
|
||||
assert_eq!(h["p1"]["status"]["completed"], true);
|
||||
assert_eq!(h["p1"]["outputs"]["9"]["images"][0]["filename"], "a.png");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_marks_error() {
|
||||
let h = comfy_cloud_job_to_history("p1", "failed", None);
|
||||
assert_eq!(h["p1"]["status"]["status_str"], "error");
|
||||
assert_eq!(h["p1"]["status"]["completed"], false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user