Skip to content

Commit 7974d2b

Browse files
committed
fix: extract primary key when multiple API keys are configured
When multiple API keys are stored as comma-separated string (e.g., "key1,key2"), the model fetcher and aionrs agent now extract the first valid key instead of passing the entire string as a single API key. - Add primary_key() helper in fetchers.rs to extract first valid key - Use primary_key in fetch_for_platform for all platform fetchers - Extract first valid key in aionrs agent initialization
1 parent c4d1b40 commit 7974d2b

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

crates/aionui-ai-agent/src/manager/aionrs/agent.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,18 @@ impl AionrsAgentManager {
6666

6767
let cli_args = CliArgs {
6868
provider: Some(config_extra.provider.clone()),
69-
api_key: Some(config_extra.api_key.clone()),
69+
// When multiple keys are stored as comma-separated string,
70+
// use the first valid key for the aionrs engine.
71+
// Full key rotation on failure is handled at a higher layer.
72+
api_key: Some(
73+
config_extra
74+
.api_key
75+
.split([',', '\n'])
76+
.map(|s| s.trim())
77+
.find(|s| !s.is_empty())
78+
.unwrap_or(&config_extra.api_key)
79+
.to_owned(),
80+
),
7081
base_url: config_extra.base_url.clone(),
7182
model: Some(config_extra.model.clone()),
7283
max_tokens: Some(config_extra.max_tokens),

crates/aionui-system/src/model_fetcher/fetchers.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,32 @@ use super::FetchConfig;
99

1010
const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
1111

12+
/// Extract the first API key from a potentially comma/newline-separated string.
13+
/// If the input contains multiple keys (e.g., "key1,key2"), only the first one
14+
/// is used for fetching the model list.
15+
fn primary_key(api_key: &str) -> &str {
16+
api_key
17+
.split([',', '\n'])
18+
.map(|s| s.trim())
19+
.find(|s| !s.is_empty())
20+
.unwrap_or(api_key)
21+
}
22+
1223
/// Dispatch to the appropriate platform-specific fetcher.
1324
pub(crate) async fn fetch_for_platform(
1425
client: &reqwest::Client,
1526
config: &FetchConfig,
1627
) -> Result<Vec<ModelInfo>, AppError> {
28+
let key = primary_key(&config.api_key);
1729
match config.platform.as_str() {
18-
"anthropic" | "claude" => fetch_anthropic(client, &config.base_url, &config.api_key).await,
19-
"gemini" => fetch_gemini(client, &config.base_url, &config.api_key).await,
30+
"anthropic" | "claude" => fetch_anthropic(client, &config.base_url, key).await,
31+
"gemini" => fetch_gemini(client, &config.base_url, key).await,
2032
"bedrock" => fetch_bedrock(config).await,
2133
"vertex-ai" => Ok(vertex_ai_models()),
22-
"new-api" => fetch_new_api(client, &config.base_url, &config.api_key).await,
34+
"new-api" => fetch_new_api(client, &config.base_url, key).await,
2335
"minimax" => Ok(minimax_models()),
24-
"dashscope-coding" => fetch_dashscope_coding(client, &config.base_url, &config.api_key).await,
25-
_ => fetch_openai_compatible(client, &config.base_url, &config.api_key).await,
36+
"dashscope-coding" => fetch_dashscope_coding(client, &config.base_url, key).await,
37+
_ => fetch_openai_compatible(client, &config.base_url, key).await,
2638
}
2739
}
2840

0 commit comments

Comments
 (0)