language_model: Refresh the LLM token upon receiving a UserUpdated message from Cloud (#35839)

This PR makes it so we refresh the LLM token upon receiving a
`UserUpdated` message from Cloud over the WebSocket connection.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2025-08-07 19:00:45 -04:00 committed by GitHub
parent d110459ef8
commit 50482a6bc2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 19 deletions

1
Cargo.lock generated
View file

@ -9127,6 +9127,7 @@ dependencies = [
"anyhow", "anyhow",
"base64 0.22.1", "base64 0.22.1",
"client", "client",
"cloud_api_types",
"cloud_llm_client", "cloud_llm_client",
"collections", "collections",
"futures 0.3.31", "futures 0.3.31",

View file

@ -193,7 +193,7 @@ pub fn init(client: &Arc<Client>, cx: &mut App) {
}); });
} }
pub type MessageToClientHandler = Box<dyn Fn(&MessageToClient, &App) + Send + Sync + 'static>; pub type MessageToClientHandler = Box<dyn Fn(&MessageToClient, &mut App) + Send + Sync + 'static>;
struct GlobalClient(Arc<Client>); struct GlobalClient(Arc<Client>);
@ -1684,7 +1684,7 @@ impl Client {
pub fn add_message_to_client_handler( pub fn add_message_to_client_handler(
self: &Arc<Client>, self: &Arc<Client>,
handler: impl Fn(&MessageToClient, &App) + Send + Sync + 'static, handler: impl Fn(&MessageToClient, &mut App) + Send + Sync + 'static,
) { ) {
self.message_to_client_handlers self.message_to_client_handlers
.lock() .lock()

View file

@ -20,6 +20,7 @@ anthropic = { workspace = true, features = ["schemars"] }
anyhow.workspace = true anyhow.workspace = true
base64.workspace = true base64.workspace = true
client.workspace = true client.workspace = true
cloud_api_types.workspace = true
cloud_llm_client.workspace = true cloud_llm_client.workspace = true
collections.workspace = true collections.workspace = true
futures.workspace = true futures.workspace = true

View file

@ -3,11 +3,9 @@ use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use client::Client; use client::Client;
use cloud_api_types::websocket_protocol::MessageToClient;
use cloud_llm_client::Plan; use cloud_llm_client::Plan;
use gpui::{ use gpui::{App, AppContext as _, Context, Entity, EventEmitter, Global, ReadGlobal as _};
App, AppContext as _, AsyncApp, Context, Entity, EventEmitter, Global, ReadGlobal as _,
};
use proto::TypedEnvelope;
use smol::lock::{RwLock, RwLockUpgradableReadGuard, RwLockWriteGuard}; use smol::lock::{RwLock, RwLockUpgradableReadGuard, RwLockWriteGuard};
use thiserror::Error; use thiserror::Error;
@ -82,9 +80,7 @@ impl Global for GlobalRefreshLlmTokenListener {}
pub struct RefreshLlmTokenEvent; pub struct RefreshLlmTokenEvent;
pub struct RefreshLlmTokenListener { pub struct RefreshLlmTokenListener;
_llm_token_subscription: client::Subscription,
}
impl EventEmitter<RefreshLlmTokenEvent> for RefreshLlmTokenListener {} impl EventEmitter<RefreshLlmTokenEvent> for RefreshLlmTokenListener {}
@ -99,17 +95,21 @@ impl RefreshLlmTokenListener {
} }
fn new(client: Arc<Client>, cx: &mut Context<Self>) -> Self { fn new(client: Arc<Client>, cx: &mut Context<Self>) -> Self {
Self { client.add_message_to_client_handler({
_llm_token_subscription: client let this = cx.entity();
.add_message_handler(cx.weak_entity(), Self::handle_refresh_llm_token), move |message, cx| {
} Self::handle_refresh_llm_token(this.clone(), message, cx);
}
});
Self
} }
async fn handle_refresh_llm_token( fn handle_refresh_llm_token(this: Entity<Self>, message: &MessageToClient, cx: &mut App) {
this: Entity<Self>, match message {
_: TypedEnvelope<proto::RefreshLlmToken>, MessageToClient::UserUpdated => {
mut cx: AsyncApp, this.update(cx, |_this, cx| cx.emit(RefreshLlmTokenEvent));
) -> Result<()> { }
this.update(&mut cx, |_this, cx| cx.emit(RefreshLlmTokenEvent)) }
} }
} }