mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-25 13:45:03 +08:00
merge(main): integrate copilot provider support and streaming responses
Merge main into hyper header-case-preservation branch. Conflicts resolved: - forwarder.rs: keep ProxyResponse return type (branch core refactor) - useProviderActions.ts: combine fine-grained proxy reasons (HEAD) with Copilot provider detection (main), add proxyReasonCopilot - i18n (en/zh/ja): retain both reason keys and hint keys, add Copilot reason translations
This commit is contained in:
@@ -310,6 +310,8 @@ pub struct CopilotAuthManager {
|
||||
refresh_locks: Arc<RwLock<HashMap<String, Arc<Mutex<()>>>>>,
|
||||
/// Copilot Token 缓存(key = GitHub user ID,内存缓存,自动刷新)
|
||||
copilot_tokens: Arc<RwLock<HashMap<String, CopilotToken>>>,
|
||||
/// Copilot Models 缓存(key = GitHub user ID,仅进程内复用)
|
||||
copilot_models: Arc<RwLock<HashMap<String, Vec<CopilotModel>>>>,
|
||||
/// HTTP 客户端
|
||||
http_client: Client,
|
||||
/// 存储路径
|
||||
@@ -330,6 +332,7 @@ impl CopilotAuthManager {
|
||||
default_account_id: Arc::new(RwLock::new(None)),
|
||||
refresh_locks: Arc::new(RwLock::new(HashMap::new())),
|
||||
copilot_tokens: Arc::new(RwLock::new(HashMap::new())),
|
||||
copilot_models: Arc::new(RwLock::new(HashMap::new())),
|
||||
http_client: Client::new(),
|
||||
storage_path,
|
||||
pending_migration: Arc::new(RwLock::new(None)),
|
||||
@@ -375,6 +378,10 @@ impl CopilotAuthManager {
|
||||
let mut tokens = self.copilot_tokens.write().await;
|
||||
tokens.remove(account_id);
|
||||
}
|
||||
{
|
||||
let mut models = self.copilot_models.write().await;
|
||||
models.remove(account_id);
|
||||
}
|
||||
{
|
||||
let mut refresh_locks = self.refresh_locks.write().await;
|
||||
refresh_locks.remove(account_id);
|
||||
@@ -625,6 +632,27 @@ impl CopilotAuthManager {
|
||||
pub async fn fetch_models_for_account(
|
||||
&self,
|
||||
account_id: &str,
|
||||
) -> Result<Vec<CopilotModel>, CopilotAuthError> {
|
||||
self.ensure_migration_complete().await?;
|
||||
|
||||
{
|
||||
let models = self.copilot_models.read().await;
|
||||
if let Some(cached) = models.get(account_id) {
|
||||
return Ok(cached.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let models = self.fetch_models_for_account_uncached(account_id).await?;
|
||||
{
|
||||
let mut cache = self.copilot_models.write().await;
|
||||
cache.insert(account_id.to_string(), models.clone());
|
||||
}
|
||||
Ok(models)
|
||||
}
|
||||
|
||||
async fn fetch_models_for_account_uncached(
|
||||
&self,
|
||||
account_id: &str,
|
||||
) -> Result<Vec<CopilotModel>, CopilotAuthError> {
|
||||
let copilot_token = self.get_valid_token_for_account(account_id).await?;
|
||||
|
||||
@@ -673,6 +701,18 @@ impl CopilotAuthManager {
|
||||
Ok(models)
|
||||
}
|
||||
|
||||
pub async fn get_model_vendor_for_account(
|
||||
&self,
|
||||
account_id: &str,
|
||||
model_id: &str,
|
||||
) -> Result<Option<String>, CopilotAuthError> {
|
||||
let models = self.fetch_models_for_account(account_id).await?;
|
||||
Ok(models
|
||||
.into_iter()
|
||||
.find(|model| model.id == model_id)
|
||||
.map(|model| model.vendor))
|
||||
}
|
||||
|
||||
/// 获取 Copilot 可用模型列表(向后兼容:使用第一个账号)
|
||||
pub async fn fetch_models(&self) -> Result<Vec<CopilotModel>, CopilotAuthError> {
|
||||
match self.resolve_default_account_id().await {
|
||||
@@ -681,6 +721,16 @@ impl CopilotAuthManager {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_model_vendor(
|
||||
&self,
|
||||
model_id: &str,
|
||||
) -> Result<Option<String>, CopilotAuthError> {
|
||||
match self.resolve_default_account_id().await {
|
||||
Some(id) => self.get_model_vendor_for_account(&id, model_id).await,
|
||||
None => Err(CopilotAuthError::GitHubTokenInvalid),
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取指定账号的 Copilot 使用量信息
|
||||
pub async fn fetch_usage_for_account(
|
||||
&self,
|
||||
@@ -1136,6 +1186,7 @@ impl CopilotAuthManager {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn test_copilot_token_expiry() {
|
||||
@@ -1308,4 +1359,59 @@ mod tests {
|
||||
Some("67890".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_model_vendor_from_cache() {
|
||||
let temp_dir = tempdir().unwrap();
|
||||
let manager = CopilotAuthManager::new(temp_dir.path().to_path_buf());
|
||||
|
||||
{
|
||||
let mut default_account_id = manager.default_account_id.write().await;
|
||||
*default_account_id = Some("12345".to_string());
|
||||
}
|
||||
{
|
||||
let mut accounts = manager.accounts.write().await;
|
||||
accounts.insert(
|
||||
"12345".to_string(),
|
||||
GitHubAccountData {
|
||||
github_token: "gho_test".to_string(),
|
||||
user: GitHubUser {
|
||||
login: "alice".to_string(),
|
||||
id: 12345,
|
||||
avatar_url: None,
|
||||
},
|
||||
authenticated_at: 1700000000,
|
||||
},
|
||||
);
|
||||
}
|
||||
{
|
||||
let mut models = manager.copilot_models.write().await;
|
||||
models.insert(
|
||||
"12345".to_string(),
|
||||
vec![
|
||||
CopilotModel {
|
||||
id: "gpt-5.4".to_string(),
|
||||
name: "GPT-5.4".to_string(),
|
||||
vendor: "OpenAI".to_string(),
|
||||
model_picker_enabled: true,
|
||||
},
|
||||
CopilotModel {
|
||||
id: "claude-sonnet-4".to_string(),
|
||||
name: "Claude Sonnet 4".to_string(),
|
||||
vendor: "Anthropic".to_string(),
|
||||
model_picker_enabled: true,
|
||||
},
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
let vendor = manager
|
||||
.get_model_vendor_for_account("12345", "gpt-5.4")
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(vendor.as_deref(), Some("OpenAI"));
|
||||
|
||||
let default_vendor = manager.get_model_vendor("claude-sonnet-4").await.unwrap();
|
||||
assert_eq!(default_vendor.as_deref(), Some("Anthropic"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user