diff --git a/crates/maa-cli/src/run/preset/copilot.rs b/crates/maa-cli/src/run/preset/copilot.rs index 8f513750..b32a86eb 100644 --- a/crates/maa-cli/src/run/preset/copilot.rs +++ b/crates/maa-cli/src/run/preset/copilot.rs @@ -1,6 +1,7 @@ use std::{ fs, path::{Path, PathBuf}, + sync::Once, }; use anyhow::{Context, Result, bail}; @@ -27,6 +28,8 @@ const COPILOT_API: &str = "http://127.0.0.1:18080/copilot/get/"; #[cfg(test)] const COPILOT_SET_API: &str = "http://127.0.0.1:18080/set/get?id="; +static LEGACY_URI_WARNING: Once = Once::new(); + /// Raid mode for copilot stages. #[cfg_attr(test, derive(Debug, PartialEq, Eq))] #[derive(Clone, Copy, Default, clap::ValueEnum)] @@ -375,21 +378,34 @@ enum CopilotFile { impl CopilotFile { fn from_uri(uri: &str) -> Result { + fn parse_code(code: &str) -> Result { + code.parse::().context("Invalid code") + } + let trimmed = uri.trim(); - if let Some(code_str) = trimmed.strip_prefix("maa://") { - if let Some(code_str) = code_str.strip_suffix('s') { - Ok(CopilotFile::RemoteSet( - code_str.parse::().context("Invalid code")?, - )) + + if let Some(code) = trimmed.strip_prefix("prts://") { + if let Some(code) = code.strip_prefix('s') { + Ok(Self::RemoteSet(parse_code(code)?)) + } else { + Ok(Self::Remote(parse_code(code)?)) + } + } else if let Some(code) = trimmed.strip_prefix("maa://") { + LEGACY_URI_WARNING.call_once(|| { + warn!( + "The maa:// URI format is deprecated; use prts:// or prts://s instead" + ); + }); + + if let Some(code) = code.strip_suffix('s') { + Ok(Self::RemoteSet(parse_code(code)?)) } else { - Ok(CopilotFile::Remote( - code_str.parse::().context("Invalid code")?, - )) + Ok(Self::Remote(parse_code(code)?)) } - } else if let Some(code) = trimmed.strip_prefix("file://") { - Ok(CopilotFile::Local(PathBuf::from(code))) + } else if let Some(path) = trimmed.strip_prefix("file://") { + Ok(Self::Local(PathBuf::from(path))) } else { - Ok(CopilotFile::Local(PathBuf::from(trimmed))) + Ok(Self::Local(PathBuf::from(trimmed))) } } @@ -1484,23 +1500,37 @@ found"}"#, #[test] fn invalid_code() { + assert!(CopilotFile::from_uri("prts://xyz").is_err()); + assert!(CopilotFile::from_uri("prts://s").is_err()); assert!(CopilotFile::from_uri("maa://xyz").is_err()); } #[test] fn remote_set() { assert_eq!( - CopilotFile::from_uri("maa://20001s").unwrap(), + CopilotFile::from_uri("prts://s20001").unwrap(), CopilotFile::RemoteSet(20001) ); } #[test] fn remote() { + assert_eq!( + CopilotFile::from_uri("prts://30001").unwrap(), + CopilotFile::Remote(30001) + ); + } + + #[test] + fn legacy_remote_remains_supported() { assert_eq!( CopilotFile::from_uri("maa://30001").unwrap(), CopilotFile::Remote(30001) ); + assert_eq!( + CopilotFile::from_uri("maa://20001s").unwrap(), + CopilotFile::RemoteSet(20001) + ); } #[test] @@ -1522,7 +1552,7 @@ found"}"#, #[test] fn with_whitespace() { assert_eq!( - CopilotFile::from_uri(" maa://30001 ").unwrap(), + CopilotFile::from_uri(" prts://30001 ").unwrap(), CopilotFile::Remote(30001) );