Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
cloud_api_client: Key the cached LlmApiToken by organization (#58915)
Tom Houlé881b4fb9bc2e
diff --git a/crates/cloud_api_client/src/llm_token.rs b/crates/cloud_api_client/src/llm_token.rsindex 87e238dcbe6..fd9832d54fb 100644--- a/crates/cloud_api_client/src/llm_token.rs+++ b/crates/cloud_api_client/src/llm_token.rs@@ -1,4 +1,4 @@-use std::sync::Arc;+use std::{fmt, sync::Arc};use async_lock::{RwLock, RwLockUpgradableReadGuard, RwLockWriteGuard};use cloud_api_types::OrganizationId;@@ -6,11 +6,27 @@ use cloud_api_types::OrganizationId;use crate::{ClientApiError, CloudApiClient};#[derive(Clone, Default)]-pub struct LlmApiToken(Arc<RwLock<Option<String>>>);+pub struct LlmApiToken(Arc<RwLock<Option<CachedLlmApiToken>>>);++struct CachedLlmApiToken {+ /// The organization ID the token was minted for.+ organization_id: OrganizationId,+ token: String,+}++impl fmt::Debug for CachedLlmApiToken {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ f.debug_struct("CachedLlmApiToken")+ .field("organization_id", &self.organization_id)+ .field("token", &"<redacted>")+ .finish()+ }+}impl LlmApiToken {- /// Returns the cached LLM token, fetching a fresh one only if none has- /// been cached yet. The returned token is not validated; callers must+ /// Returns the cached LLM token, fetching a fresh one if none has been+ /// cached yet or if the cached token was minted for a different+ /// organization. The returned token is not validated; callers must/// be prepared to refresh it (via [`LlmApiToken::refresh`]) if the/// server rejects it.pub async fn cached(@@ -20,7 +36,12 @@ impl LlmApiToken {organization_id: OrganizationId,) -> Result<String, ClientApiError> {let lock = self.0.upgradable_read().await;- if let Some(token) = lock.as_ref() {+ if let Some(CachedLlmApiToken {+ organization_id: cached_organization_id,+ token,+ }) = lock.as_ref()+ && *cached_organization_id == organization_id+ {Ok(token.to_string())} else {Self::fetch(@@ -62,15 +83,21 @@ impl LlmApiToken {}async fn fetch(- mut lock: RwLockWriteGuard<'_, Option<String>>,+ mut lock: RwLockWriteGuard<'_, Option<CachedLlmApiToken>>,client: &CloudApiClient,system_id: Option<String>,organization_id: OrganizationId,) -> Result<String, ClientApiError> {- let result = client.create_llm_token(system_id, organization_id).await;+ let result = client+ .create_llm_token(system_id, organization_id.clone())+ .await;match result {Ok(response) => {- *lock = Some(response.token.0.clone());+ *lock = Some(CachedLlmApiToken {+ organization_id,+ token: response.token.0.clone(),+ });+Ok(response.token.0)}Err(err) => {