Skip to content

Commit

Permalink
collab: Include max monthly spend preference in LLM token (#18955)
Browse files Browse the repository at this point in the history
This PR updates the LLM token claims to include the maximum monthly
spend.

Release Notes:

- N/A
  • Loading branch information
maxdeviant authored Oct 9, 2024
1 parent 99a6a3d commit 3db789e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 18 additions & 1 deletion crates/collab/src/llm/token.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{db::UserId, Config};
use crate::llm::DEFAULT_MAX_MONTHLY_SPEND;
use crate::{
db::{billing_preference, UserId},
Config,
};
use anyhow::{anyhow, Result};
use chrono::Utc;
use jsonwebtoken::{DecodingKey, EncodingKey, Header, Validation};
Expand All @@ -22,16 +26,24 @@ pub struct LlmTokenClaims {
// this change has been deployed).
#[serde(default)]
pub has_llm_subscription: Option<bool>,
// This field is temporarily optional so it can be added
// in a backwards-compatible way. We can make it required
// once all of the LLM tokens have cycled (~1 hour after
// this change has been deployed).
#[serde(default)]
pub max_monthly_spend_in_cents: Option<u32>,
pub plan: rpc::proto::Plan,
}

const LLM_TOKEN_LIFETIME: Duration = Duration::from_secs(60 * 60);

impl LlmTokenClaims {
#[allow(clippy::too_many_arguments)]
pub fn create(
user_id: UserId,
github_user_login: String,
is_staff: bool,
billing_preferences: Option<billing_preference::Model>,
has_llm_closed_beta_feature_flag: bool,
has_llm_subscription: bool,
plan: rpc::proto::Plan,
Expand All @@ -52,6 +64,11 @@ impl LlmTokenClaims {
is_staff,
has_llm_closed_beta_feature_flag,
has_llm_subscription: Some(has_llm_subscription),
max_monthly_spend_in_cents: Some(
billing_preferences.map_or(DEFAULT_MAX_MONTHLY_SPEND.0, |preferences| {
preferences.max_monthly_llm_usage_spending_in_cents as u32
}),
),
plan,
};

Expand Down
4 changes: 4 additions & 0 deletions crates/collab/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4917,10 +4917,14 @@ async fn get_llm_api_token(
if Utc::now().naive_utc() - account_created_at < MIN_ACCOUNT_AGE_FOR_LLM_USE {
Err(anyhow!("account too young"))?
}

let billing_preferences = db.get_billing_preferences(user.id).await?;

let token = LlmTokenClaims::create(
user.id,
user.github_login.clone(),
session.is_staff(),
billing_preferences,
has_llm_closed_beta_feature_flag,
session.has_llm_subscription(&db).await?,
session.current_plan(&db).await?,
Expand Down

0 comments on commit 3db789e

Please sign in to comment.