Remove dependents of language_models
(#25511)
This PR removes the dependents of the `language_models` crate. The following types have been moved from `language_models` to `language_model` to facilitate this: - `LlmApiToken` - `RefreshLlmTokenListener` - `MaxMonthlySpendReachedError` - `PaymentRequiredError` With this change only `zed` now depends on `language_models`. Release Notes: - N/A
This commit is contained in:
parent
bbb8d63de0
commit
def342e35c
14 changed files with 117 additions and 124 deletions
|
@ -9,6 +9,7 @@ mod telemetry;
|
|||
pub mod fake_provider;
|
||||
|
||||
use anyhow::Result;
|
||||
use client::Client;
|
||||
use futures::FutureExt;
|
||||
use futures::{future::BoxFuture, stream::BoxStream, StreamExt, TryStreamExt as _};
|
||||
use gpui::{AnyElement, AnyView, App, AsyncApp, SharedString, Task, Window};
|
||||
|
@ -29,8 +30,9 @@ pub use crate::telemetry::*;
|
|||
|
||||
pub const ZED_CLOUD_PROVIDER_ID: &str = "zed.dev";
|
||||
|
||||
pub fn init(cx: &mut App) {
|
||||
pub fn init(client: Arc<Client>, cx: &mut App) {
|
||||
registry::init(cx);
|
||||
RefreshLlmTokenListener::register(client.clone(), cx);
|
||||
}
|
||||
|
||||
/// The availability of a [`LanguageModel`].
|
||||
|
|
|
@ -1,7 +1,17 @@
|
|||
use proto::Plan;
|
||||
use std::fmt;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use client::Client;
|
||||
use gpui::{
|
||||
App, AppContext as _, AsyncApp, Context, Entity, EventEmitter, Global, ReadGlobal as _,
|
||||
};
|
||||
use proto::{Plan, TypedEnvelope};
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use smol::lock::{RwLock, RwLockUpgradableReadGuard, RwLockWriteGuard};
|
||||
use strum::EnumIter;
|
||||
use thiserror::Error;
|
||||
use ui::IconName;
|
||||
|
||||
use crate::LanguageModelAvailability;
|
||||
|
@ -102,3 +112,92 @@ impl CloudModel {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub struct PaymentRequiredError;
|
||||
|
||||
impl fmt::Display for PaymentRequiredError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"Payment required to use this language model. Please upgrade your account."
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub struct MaxMonthlySpendReachedError;
|
||||
|
||||
impl fmt::Display for MaxMonthlySpendReachedError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"Maximum spending limit reached for this month. For more usage, increase your spending limit."
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct LlmApiToken(Arc<RwLock<Option<String>>>);
|
||||
|
||||
impl LlmApiToken {
|
||||
pub async fn acquire(&self, client: &Arc<Client>) -> Result<String> {
|
||||
let lock = self.0.upgradable_read().await;
|
||||
if let Some(token) = lock.as_ref() {
|
||||
Ok(token.to_string())
|
||||
} else {
|
||||
Self::fetch(RwLockUpgradableReadGuard::upgrade(lock).await, client).await
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn refresh(&self, client: &Arc<Client>) -> Result<String> {
|
||||
Self::fetch(self.0.write().await, client).await
|
||||
}
|
||||
|
||||
async fn fetch<'a>(
|
||||
mut lock: RwLockWriteGuard<'a, Option<String>>,
|
||||
client: &Arc<Client>,
|
||||
) -> Result<String> {
|
||||
let response = client.request(proto::GetLlmToken {}).await?;
|
||||
*lock = Some(response.token.clone());
|
||||
Ok(response.token.clone())
|
||||
}
|
||||
}
|
||||
|
||||
struct GlobalRefreshLlmTokenListener(Entity<RefreshLlmTokenListener>);
|
||||
|
||||
impl Global for GlobalRefreshLlmTokenListener {}
|
||||
|
||||
pub struct RefreshLlmTokenEvent;
|
||||
|
||||
pub struct RefreshLlmTokenListener {
|
||||
_llm_token_subscription: client::Subscription,
|
||||
}
|
||||
|
||||
impl EventEmitter<RefreshLlmTokenEvent> for RefreshLlmTokenListener {}
|
||||
|
||||
impl RefreshLlmTokenListener {
|
||||
pub fn register(client: Arc<Client>, cx: &mut App) {
|
||||
let listener = cx.new(|cx| RefreshLlmTokenListener::new(client, cx));
|
||||
cx.set_global(GlobalRefreshLlmTokenListener(listener));
|
||||
}
|
||||
|
||||
pub fn global(cx: &App) -> Entity<Self> {
|
||||
GlobalRefreshLlmTokenListener::global(cx).0.clone()
|
||||
}
|
||||
|
||||
fn new(client: Arc<Client>, cx: &mut Context<Self>) -> Self {
|
||||
Self {
|
||||
_llm_token_subscription: client
|
||||
.add_message_handler(cx.weak_entity(), Self::handle_refresh_llm_token),
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_refresh_llm_token(
|
||||
this: Entity<Self>,
|
||||
_: TypedEnvelope<proto::RefreshLlmToken>,
|
||||
mut cx: AsyncApp,
|
||||
) -> Result<()> {
|
||||
this.update(&mut cx, |_this, cx| cx.emit(RefreshLlmTokenEvent))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue