1 line
20 KiB
JSON
1 line
20 KiB
JSON
{"repo": "2eeyuan/ai-video-studio", "files": {"ai-video-studio/src-tauri/src/sidecar/mod.rs": {"fd39030dd732165f5f8b7f5e8a252f78b89281d4": {"t": 1785746254.9227607, "n": 9393, "h": "ac3d339a61118fea", "c": "use serde::{Deserialize, Serialize};\nuse std::process::{Child, Command};\nuse std::sync::Mutex;\n#[allow(unused_imports)]\nuse log;\n\npub struct SidecarManager {\n child: Mutex<Option<Child>>,\n port: u16,\n}\n\n#[derive(Debug, Serialize, Deserialize)]\npub struct ScriptResponse {\n pub script: String,\n pub keywords: Vec<String>,\n}\n\n#[derive(Debug, Serialize, Deserialize)]\npub struct HealthResponse {\n pub status: String,\n pub version: String,\n}\n\nimpl SidecarManager {\n pub fn new(port: u16) -> Self {\n Self {\n child: Mutex::new(None),\n port,\n }\n }\n\n pub fn start(&self, python_path: &str, sidecar_dir: &str) -> Result<(), String> {\n let mut child = self.child.lock().unwrap();\n if child.is_some() {\n return Ok(());\n }\n\n let cmd = Command::new(python_path)\n .arg(\"main.py\")\n .current_dir(sidecar_dir)\n .env(\"SIDECAR_PORT\", self.port.to_string())\n .env(\"NO_PROXY\", \"127.0.0.1,localhost,api.xiaomimimo.com,token-plan-cn.xiaomimimo.com,api.openai.com,api.deepseek.com\")\n .env(\"no_proxy\", \"127.0.0.1,localhost,api.xiaomimimo.com,token-plan-cn.xiaomimimo.com,api.openai.com,api.deepseek.com\")\n .spawn()\n .map_err(|e| format!(\"Failed to start sidecar: {}\", e))?;\n\n *child = Some(cmd);\n Ok(())\n }\n\n pub fn stop(&self) {\n let mut child = self.child.lock().unwrap();\n if let Some(mut c) = child.take() {\n let _ = c.kill();\n }\n }\n\n pub fn is_running(&self) -> bool {\n let mut child = self.child.lock().unwrap();\n if let Some(c) = child.as_mut() {\n match c.try_wait() {\n Ok(Some(_)) => {\n *child = None;\n false\n }\n Ok(None) => true,\n Err(_) => {\n *child = None;\n false\n }\n }\n } else {\n false\n }\n }\n\n pub fn base_url(&self) -> String {\n format!(\"http://127.0.0.1:{}\", self.port)\n }\n}\n\npub async fn health_check(port: u16) -> Result<HealthResponse, String> {\n let url = format!(\"http://127.0.0.1:{}/health\", port);\n let resp = reqwest::get(&url)\n .await\n .map_err(|e| format!(\"Health check failed: {}\", e))?;\n resp.json::<HealthResponse>()\n .await\n .map_err(|e| format!(\"Failed to parse health response: {}\", e))\n}\n\npub async fn generate_script(\n port: u16,\n subject: &str,\n language: &str,\n paragraph_number: u8,\n custom_prompt: &str,\n) -> Result<ScriptResponse, String> {\n let url = format!(\"http://127.0.0.1:{}/generate/script\", port);\n let client = reqwest::Client::new();\n let resp = client\n .post(&url)\n .json(&serde_json::json!({\n \"subject\": subject,\n \"language\": language,\n \"paragraph_number\": paragraph_number,\n \"custom_prompt\": custom_prompt,\n }))\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Sidecar error ({}): {}\", status, body));\n }\n\n resp.json::<ScriptResponse>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n\npub async fn generate_terms(\n port: u16,\n subject: &str,\n script: &str,\n) -> Result<Vec<String>, String> {\n let url = format!(\"http://127.0.0.1:{}/generate/terms\", port);\n let client = reqwest::Client::new();\n let resp = client\n .post(&url)\n .json(&serde_json::json!({\n \"subject\": subject,\n \"script\": script,\n }))\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Sidecar error ({}): {}\", status, body));\n }\n\n let result: serde_json::Value = resp.json()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))?;\n\n result[\"keywords\"]\n .as_array()\n .map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())\n .ok_or_else(|| \"Invalid response format\".to_string())\n}\n\npub async fn start_pipeline(\n port: u16,\n task_id: &str,\n subject: &str,\n script: &str,\n keywords: &str,\n shots: Option<&serde_json::Value>,\n) -> Result<(), String> {\n let url = format!(\"http://127.0.0.1:{}/pipeline/start\", port);\n let client = reqwest::Client::new();\n let mut body = serde_json::json!({\n \"task_id\": task_id,\n \"subject\": subject,\n \"script\": script,\n \"keywords\": keywords,\n });\n if let Some(shots_val) = shots {\n body[\"shots\"] = shots_val.clone();\n }\n let resp = client\n .post(&url)\n .json(&body)\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Sidecar error ({}): {}\", status, body));\n }\n\n Ok(())\n}\n\npub async fn get_pipeline_status(port: u16, task_id: &str) -> Result<serde_json::Value, String> {\n let url = format!(\"http://127.0.0.1:{}/pipeline/{}/status\", port, task_id);\n let resp = reqwest::get(&url)\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n return Err(format!(\"Task not found: {}\", task_id));\n }\n\n resp.json::<serde_json::Value>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n\n// --- Agent API calls ---\n\npub async fn agent_start(port: u16, task_id: &str, subject: &str) -> Result<serde_json::Value, String> {\n let url = format!(\"http://127.0.0.1:{}/agent/start\", port);\n let client = reqwest::Client::new();\n let resp = client\n .post(&url)\n .json(&serde_json::json!({ \"task_id\": task_id, \"subject\": subject }))\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Agent error ({}): {}\", status, body));\n }\n\n resp.json::<serde_json::Value>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n\npub async fn agent_submit_direction(port: u16, task_id: &str, direction_id: i32) -> Result<serde_json::Value, String> {\n let url = format!(\"http://127.0.0.1:{}/agent/direction\", port);\n let client = reqwest::Client::new();\n let resp = client\n .post(&url)\n .json(&serde_json::json!({ \"task_id\": task_id, \"direction_id\": direction_id }))\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Agent error ({}): {}\", status, body));\n }\n\n resp.json::<serde_json::Value>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n\npub async fn agent_submit_script(port: u16, task_id: &str, script: Option<serde_json::Value>) -> Result<serde_json::Value, String> {\n let url = format!(\"http://127.0.0.1:{}/agent/script\", port);\n let client = reqwest::Client::new();\n let resp = client\n .post(&url)\n .json(&serde_json::json!({ \"task_id\": task_id, \"script\": script }))\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Agent error ({}): {}\", status, body));\n }\n\n resp.json::<serde_json::Value>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n\npub async fn agent_submit_storyboard(port: u16, task_id: &str) -> Result<serde_json::Value, String> {\n let url = format!(\"http://127.0.0.1:{}/agent/storyboard\", port);\n let client = reqwest::Client::new();\n let resp = client\n .post(&url)\n .json(&serde_json::json!({ \"task_id\": task_id }))\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Agent error ({}): {}\", status, body));\n }\n\n resp.json::<serde_json::Value>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n\npub async fn agent_status(port: u16, task_id: &str) -> Result<serde_json::Value, String> {\n let url = format!(\"http://127.0.0.1:{}/agent/{}\", port, task_id);\n let resp = reqwest::get(&url)\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n return Err(format!(\"Session not found: {}\", task_id));\n }\n\n resp.json::<serde_json::Value>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n"}, "2d68a6e81bb1b4a06940967055a4fca4f1645c0d": {"t": 1785761128.0455885, "n": 9393, "h": "ac3d339a61118fea", "c": "use serde::{Deserialize, Serialize};\nuse std::process::{Child, Command};\nuse std::sync::Mutex;\n#[allow(unused_imports)]\nuse log;\n\npub struct SidecarManager {\n child: Mutex<Option<Child>>,\n port: u16,\n}\n\n#[derive(Debug, Serialize, Deserialize)]\npub struct ScriptResponse {\n pub script: String,\n pub keywords: Vec<String>,\n}\n\n#[derive(Debug, Serialize, Deserialize)]\npub struct HealthResponse {\n pub status: String,\n pub version: String,\n}\n\nimpl SidecarManager {\n pub fn new(port: u16) -> Self {\n Self {\n child: Mutex::new(None),\n port,\n }\n }\n\n pub fn start(&self, python_path: &str, sidecar_dir: &str) -> Result<(), String> {\n let mut child = self.child.lock().unwrap();\n if child.is_some() {\n return Ok(());\n }\n\n let cmd = Command::new(python_path)\n .arg(\"main.py\")\n .current_dir(sidecar_dir)\n .env(\"SIDECAR_PORT\", self.port.to_string())\n .env(\"NO_PROXY\", \"127.0.0.1,localhost,api.xiaomimimo.com,token-plan-cn.xiaomimimo.com,api.openai.com,api.deepseek.com\")\n .env(\"no_proxy\", \"127.0.0.1,localhost,api.xiaomimimo.com,token-plan-cn.xiaomimimo.com,api.openai.com,api.deepseek.com\")\n .spawn()\n .map_err(|e| format!(\"Failed to start sidecar: {}\", e))?;\n\n *child = Some(cmd);\n Ok(())\n }\n\n pub fn stop(&self) {\n let mut child = self.child.lock().unwrap();\n if let Some(mut c) = child.take() {\n let _ = c.kill();\n }\n }\n\n pub fn is_running(&self) -> bool {\n let mut child = self.child.lock().unwrap();\n if let Some(c) = child.as_mut() {\n match c.try_wait() {\n Ok(Some(_)) => {\n *child = None;\n false\n }\n Ok(None) => true,\n Err(_) => {\n *child = None;\n false\n }\n }\n } else {\n false\n }\n }\n\n pub fn base_url(&self) -> String {\n format!(\"http://127.0.0.1:{}\", self.port)\n }\n}\n\npub async fn health_check(port: u16) -> Result<HealthResponse, String> {\n let url = format!(\"http://127.0.0.1:{}/health\", port);\n let resp = reqwest::get(&url)\n .await\n .map_err(|e| format!(\"Health check failed: {}\", e))?;\n resp.json::<HealthResponse>()\n .await\n .map_err(|e| format!(\"Failed to parse health response: {}\", e))\n}\n\npub async fn generate_script(\n port: u16,\n subject: &str,\n language: &str,\n paragraph_number: u8,\n custom_prompt: &str,\n) -> Result<ScriptResponse, String> {\n let url = format!(\"http://127.0.0.1:{}/generate/script\", port);\n let client = reqwest::Client::new();\n let resp = client\n .post(&url)\n .json(&serde_json::json!({\n \"subject\": subject,\n \"language\": language,\n \"paragraph_number\": paragraph_number,\n \"custom_prompt\": custom_prompt,\n }))\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Sidecar error ({}): {}\", status, body));\n }\n\n resp.json::<ScriptResponse>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n\npub async fn generate_terms(\n port: u16,\n subject: &str,\n script: &str,\n) -> Result<Vec<String>, String> {\n let url = format!(\"http://127.0.0.1:{}/generate/terms\", port);\n let client = reqwest::Client::new();\n let resp = client\n .post(&url)\n .json(&serde_json::json!({\n \"subject\": subject,\n \"script\": script,\n }))\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Sidecar error ({}): {}\", status, body));\n }\n\n let result: serde_json::Value = resp.json()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))?;\n\n result[\"keywords\"]\n .as_array()\n .map(|arr| arr.iter().filter_map(|v| v.as_str().map(String::from)).collect())\n .ok_or_else(|| \"Invalid response format\".to_string())\n}\n\npub async fn start_pipeline(\n port: u16,\n task_id: &str,\n subject: &str,\n script: &str,\n keywords: &str,\n shots: Option<&serde_json::Value>,\n) -> Result<(), String> {\n let url = format!(\"http://127.0.0.1:{}/pipeline/start\", port);\n let client = reqwest::Client::new();\n let mut body = serde_json::json!({\n \"task_id\": task_id,\n \"subject\": subject,\n \"script\": script,\n \"keywords\": keywords,\n });\n if let Some(shots_val) = shots {\n body[\"shots\"] = shots_val.clone();\n }\n let resp = client\n .post(&url)\n .json(&body)\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Sidecar error ({}): {}\", status, body));\n }\n\n Ok(())\n}\n\npub async fn get_pipeline_status(port: u16, task_id: &str) -> Result<serde_json::Value, String> {\n let url = format!(\"http://127.0.0.1:{}/pipeline/{}/status\", port, task_id);\n let resp = reqwest::get(&url)\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n return Err(format!(\"Task not found: {}\", task_id));\n }\n\n resp.json::<serde_json::Value>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n\n// --- Agent API calls ---\n\npub async fn agent_start(port: u16, task_id: &str, subject: &str) -> Result<serde_json::Value, String> {\n let url = format!(\"http://127.0.0.1:{}/agent/start\", port);\n let client = reqwest::Client::new();\n let resp = client\n .post(&url)\n .json(&serde_json::json!({ \"task_id\": task_id, \"subject\": subject }))\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Agent error ({}): {}\", status, body));\n }\n\n resp.json::<serde_json::Value>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n\npub async fn agent_submit_direction(port: u16, task_id: &str, direction_id: i32) -> Result<serde_json::Value, String> {\n let url = format!(\"http://127.0.0.1:{}/agent/direction\", port);\n let client = reqwest::Client::new();\n let resp = client\n .post(&url)\n .json(&serde_json::json!({ \"task_id\": task_id, \"direction_id\": direction_id }))\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Agent error ({}): {}\", status, body));\n }\n\n resp.json::<serde_json::Value>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n\npub async fn agent_submit_script(port: u16, task_id: &str, script: Option<serde_json::Value>) -> Result<serde_json::Value, String> {\n let url = format!(\"http://127.0.0.1:{}/agent/script\", port);\n let client = reqwest::Client::new();\n let resp = client\n .post(&url)\n .json(&serde_json::json!({ \"task_id\": task_id, \"script\": script }))\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Agent error ({}): {}\", status, body));\n }\n\n resp.json::<serde_json::Value>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n\npub async fn agent_submit_storyboard(port: u16, task_id: &str) -> Result<serde_json::Value, String> {\n let url = format!(\"http://127.0.0.1:{}/agent/storyboard\", port);\n let client = reqwest::Client::new();\n let resp = client\n .post(&url)\n .json(&serde_json::json!({ \"task_id\": task_id }))\n .send()\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n let status = resp.status();\n let body = resp.text().await.unwrap_or_default();\n return Err(format!(\"Agent error ({}): {}\", status, body));\n }\n\n resp.json::<serde_json::Value>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n\npub async fn agent_status(port: u16, task_id: &str) -> Result<serde_json::Value, String> {\n let url = format!(\"http://127.0.0.1:{}/agent/{}\", port, task_id);\n let resp = reqwest::get(&url)\n .await\n .map_err(|e| format!(\"Request failed: {}\", e))?;\n\n if !resp.status().is_success() {\n return Err(format!(\"Session not found: {}\", task_id));\n }\n\n resp.json::<serde_json::Value>()\n .await\n .map_err(|e| format!(\"Failed to parse response: {}\", e))\n}\n"}}}} |