Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions crates/maa-cli/src/run/preset/copilot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fs,
path::{Path, PathBuf},
sync::Once,
};

use anyhow::{Context, Result, bail};
Expand All @@ -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)]
Expand Down Expand Up @@ -375,21 +378,34 @@ enum CopilotFile {

impl CopilotFile {
fn from_uri(uri: &str) -> Result<Self> {
fn parse_code(code: &str) -> Result<u64> {
code.parse::<u64>().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::<u64>().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://<id> or prts://s<id> instead"
);
});

if let Some(code) = code.strip_suffix('s') {
Ok(Self::RemoteSet(parse_code(code)?))
} else {
Ok(CopilotFile::Remote(
code_str.parse::<u64>().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)))
}
}

Expand Down Expand Up @@ -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());
}
Comment thread
wangl-cc marked this conversation as resolved.

#[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]
Expand All @@ -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)
);

Expand Down