refactor: migrate router and credential core (types/pool/health/risk) to proxycast-core

This commit is contained in:
coso
2026-02-08 13:31:33 +08:00
parent b70a6e748d
commit bbed059a49
15 changed files with 68 additions and 40 deletions
@@ -0,0 +1,15 @@
//! 凭证池核心类型和独立逻辑
//!
//! 包含凭证类型定义、凭证池管理、健康检查和风控模块。
//! 负载均衡器(balancer)、配额管理(quota)和同步服务(sync)
//! 因依赖 infra crate 保留在主 crate 中。
pub mod health;
pub mod pool;
pub mod risk;
pub mod types;
pub use health::{HealthCheckConfig, HealthCheckResult, HealthChecker, HealthStatus};
pub use pool::{CredentialPool, PoolError, PoolStatus};
pub use risk::{CooldownConfig, RateLimitEvent, RateLimitStats, RiskController, RiskLevel};
pub use types::{Credential, CredentialData, CredentialStats, CredentialStatus};
@@ -14,7 +14,7 @@ pub struct CredentialPool {
/// 所属 Provider 类型
provider: ProviderType,
/// 凭证存储(id -> Credential)
pub(crate) credentials: DashMap<String, Credential>,
pub credentials: DashMap<String, Credential>,
/// 轮询索引(用于负载均衡)
round_robin_index: AtomicUsize,
}
+6
View File
@@ -35,6 +35,12 @@ pub mod session_files;
pub mod agent;
pub mod general_chat;
// 路由系统
pub mod router;
// 凭证池核心(types, pool, health, risk)
pub mod credential;
// 数据层
pub mod content;
pub mod database;
+22
View File
@@ -0,0 +1,22 @@
//! 路由系统模块
//!
//! 支持动态路由注册、命名空间路由解析、模型映射。
//!
//! 路由格式:
//! - `/{provider-name}/v1/messages` - Provider 命名空间路由
//! - `/{selector}/v1/messages` - 凭证选择器路由(向后兼容)
//! - `/v1/messages` - 默认路由
//! - `/api/provider/{provider}/v1/*` - Amp CLI 路由
//!
//! 模型映射:
//! - 支持模型别名映射(如 `gpt-4` -> `claude-sonnet-4-5-20250514`)
mod amp_router;
mod mapper;
mod provider_router;
mod route_registry;
mod rules;
pub use amp_router::AmpRouter;
pub use mapper::ModelMapper;
pub use rules::Router;
+3 -3
View File
@@ -2,13 +2,13 @@
//!
//! 提供轮询负载均衡策略,支持凭证冷却和自动恢复
use super::health::{HealthCheckConfig, HealthChecker};
use super::pool::{CredentialPool, PoolError};
use super::types::Credential;
use crate::proxy::ProxyClientFactory;
use crate::ProviderType;
use chrono::{DateTime, Duration, Utc};
use dashmap::DashMap;
use proxycast_core::credential::health::{HealthCheckConfig, HealthChecker};
use proxycast_core::credential::pool::{CredentialPool, PoolError};
use proxycast_core::credential::types::Credential;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicUsize, Ordering};
+19 -15
View File
@@ -4,32 +4,36 @@
//!
//! ## 模块结构
//!
//! - `types` - 凭证相关类型定义
//! - `pool` - 凭证池管理
//! - `balancer` - 负载均衡策略
//! - `health` - 健康检查
//! - `quota` - 配额管理
//! - `sync` - 数据库同步
//! - `risk` - 风控模块(限流检测、冷却期管理)
//! - `types` - 凭证相关类型定义(来自 proxycast-core)
//! - `pool` - 凭证池管理(来自 proxycast-core)
//! - `health` - 健康检查(来自 proxycast-core)
//! - `risk` - 风控模块(来自 proxycast-core)
//! - `balancer` - 负载均衡策略(本地)
//! - `quota` - 配额管理(本地)
//! - `sync` - 数据库同步(本地)
// 从 proxycast-core 重新导出核心类型模块
pub use proxycast_core::credential::{health, pool, risk, types};
// 本地模块(依赖 infra 或 Tauri)
mod balancer;
mod health;
mod pool;
mod quota;
pub mod risk;
mod sync;
mod types;
// 重新导出 core 类型
pub use proxycast_core::credential::{
CooldownConfig, Credential, CredentialData, CredentialPool, CredentialStats, CredentialStatus,
HealthCheckConfig, HealthCheckResult, HealthChecker, HealthStatus, PoolError, PoolStatus,
RateLimitEvent, RateLimitStats, RiskController, RiskLevel,
};
// 重新导出本地类型
pub use balancer::{BalanceStrategy, CooldownInfo, CredentialSelection, LoadBalancer};
pub use health::{HealthCheckConfig, HealthCheckResult, HealthChecker, HealthStatus};
pub use pool::{CredentialPool, PoolError, PoolStatus};
pub use quota::{
create_shared_quota_manager, start_quota_cleanup_task, AllCredentialsExhaustedError,
QuotaAutoSwitchResult, QuotaExceededRecord, QuotaManager,
};
pub use risk::{CooldownConfig, RateLimitEvent, RateLimitStats, RiskController, RiskLevel};
pub use sync::{CredentialSyncService, SyncError};
pub use types::{Credential, CredentialData, CredentialStats, CredentialStatus};
#[cfg(test)]
mod tests;
+2 -21
View File
@@ -1,22 +1,3 @@
//! 路由系统模块
//!
//! 支持动态路由注册、命名空间路由解析、模型映射。
//!
//! 路由格式:
//! - `/{provider-name}/v1/messages` - Provider 命名空间路由
//! - `/{selector}/v1/messages` - 凭证选择器路由(向后兼容)
//! - `/v1/messages` - 默认路由
//! - `/api/provider/{provider}/v1/*` - Amp CLI 路由
//!
//! 模型映射:
//! - 支持模型别名映射(如 `gpt-4` -> `claude-sonnet-4-5-20250514`)
//! 路由系统模块 - 从 proxycast-core 重新导出
mod amp_router;
mod mapper;
mod provider_router;
mod route_registry;
mod rules;
pub use amp_router::AmpRouter;
pub use mapper::ModelMapper;
pub use rules::Router;
pub use proxycast_core::router::*;