pub(crate) mod config;
pub(crate) mod error;
use anyhow::Result;
use rand::Rng;
pub async fn get_mcp_url(category: &str) -> Result<String> {
let Some(list) = config::load_mcp_config() else {
return Err(anyhow::anyhow!(
"未找到 MCP 配置缓存,请先运行 `{} init`",
env!("CARGO_BIN_NAME")
));
};
let target = list
.iter()
.find(|item| item.biz_type.as_deref() == Some(category))
.ok_or_else(|| anyhow::anyhow!("当前企业暂不支持 {category} 命令"))?;
target
.url
.clone()
.ok_or_else(|| anyhow::anyhow!("MCP 配置中 {category} 的 url 为空"))
}
pub fn gen_req_id(prefix: &str) -> String {
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let random = generate_random_hex(8);
format!("{prefix}_{timestamp}_{random}")
}
fn generate_random_hex(length: usize) -> String {
let byte_len = length.div_ceil(2);
let bytes: Vec<u8> = (0..byte_len).map(|_| rand::rng().random::<u8>()).collect();
let hex = hex::encode(bytes);
hex[..length].to_string()
}