Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
use anyhow::{Result, anyhow};
use credentials_provider::CredentialsProvider;
use futures::FutureExt as _;
use futures::future::{BoxFuture, Shared};
use gpui::{App, AsyncApp, Context, Entity, SharedString, Task, Window};
use http_client::HttpClient;
use language_model::{
AuthenticateError, CompactionResult, FastModeConfirmation, IconOrSvg, InlineDescription,
LanguageModel, LanguageModelClient, LanguageModelCompletionError,
LanguageModelCompletionStream, LanguageModelProvider, LanguageModelProviderId,
LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest, ModelRateLimiters,
ProviderSettingsView, unavailable_error,
};
use openai_subscribed::{ChatGptModel, PROVIDER_ID, PROVIDER_NAME, State, language_model};
use std::sync::Arc;
use ui::{ConfiguredApiCard, prelude::*};
const SUBSCRIPTION_DESCRIPTION: &str =
"Sign in with your ChatGPT Plus or Pro subscription to use OpenAI models in Zed's agent.";
pub struct OpenAiSubscribedProvider {
state: Entity<State>,
request_limiters: ModelRateLimiters,
}
impl OpenAiSubscribedProvider {
pub fn new(
http_client: Arc<dyn HttpClient>,
credentials_provider: Arc<dyn CredentialsProvider>,
cx: &mut App,
) -> Self {
let state = cx.new(|cx| State::new(http_client, credentials_provider, cx));
Self {
state,
request_limiters: ModelRateLimiters::default(),
}
}
/// The current configuration of `model`, if this provider still offers it.
fn config(
&self,
model: &LanguageModel,
cx: &App,
) -> Result<ChatGptModel, LanguageModelCompletionError> {
self.state
.read(cx)
.available_models()
.iter()
.find(|config| config.id() == model.id.0.as_ref())
.cloned()
.ok_or_else(|| unavailable_error(model))
}
}
impl LanguageModelProviderState for OpenAiSubscribedProvider {
type ObservableEntity = State;
fn observable_entity(&self) -> Option<Entity<Self::ObservableEntity>> {
Some(self.state.clone())
}
}
impl LanguageModelProvider for OpenAiSubscribedProvider {
fn id(&self) -> LanguageModelProviderId {
PROVIDER_ID
}
fn name(&self) -> LanguageModelProviderName {
PROVIDER_NAME
}
fn icon(&self) -> IconOrSvg {
IconOrSvg::Icon(IconName::AiOpenAiGptSub)
}
fn default_model(&self, cx: &App) -> Option<LanguageModel> {
self.state
.read(cx)
.default_model()
.map(|model| language_model(&model))
}
fn default_fast_model(&self, cx: &App) -> Option<LanguageModel> {
self.state
.read(cx)
.default_fast_model()
.map(|model| language_model(&model))
}
fn provided_models(&self, cx: &App) -> Vec<LanguageModel> {
self.state
.read(cx)
.available_models()
.iter()
.map(language_model)
.collect()
}
fn recommended_models(&self, cx: &App) -> Vec<LanguageModel> {
self.default_model(cx).into_iter().collect()
}
fn is_authenticated(&self, cx: &App) -> bool {
self.state.read(cx).is_authenticated()
}
fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
if self.is_authenticated(cx) {
return Task::ready(Ok(()));
}
let load_task: Option<Shared<_>> = self.state.read(cx).load_task();
if let Some(load_task) = load_task {
let weak_state = self.state.downgrade();
cx.spawn(async move |cx| {
load_task
.await
.map_err(|error| AuthenticateError::Other(anyhow!("{error:#}")))?;
let is_auth = weak_state
.read_with(&*cx, |state, _| state.is_authenticated())
.map_err(AuthenticateError::Other)?;
if is_auth {
Ok(())
} else {
Err(AuthenticateError::CredentialsNotFound)
}
})
} else {
Task::ready(Err(AuthenticateError::CredentialsNotFound))
}
}
fn settings_view(&self, cx: &mut App) -> Option<ProviderSettingsView> {
let is_authenticated = self.state.read(cx).is_authenticated();
let title = if is_authenticated {
None
} else {
Some("Configure ChatGPT".into())
};
let description = if is_authenticated {
None
} else {
Some(InlineDescription::Text(SUBSCRIPTION_DESCRIPTION.into()))
};
Some(ProviderSettingsView::Inline(
language_model::InlineProviderSettings {
title,
description,
create_view: Arc::new({
let state = self.state.clone();
move |_window, cx| {
cx.new(|_cx| ConfigurationView {
state: state.clone(),
compact: true,
})
.into()
}
}),
},
))
}
fn authentication_error_message(&self) -> SharedString {
"Your ChatGPT subscription session is invalid or has expired. \
Sign in again via Settings > AI > LLM Providers to continue."
.into()
}
fn missing_credentials_error_message(&self) -> SharedString {
"You are not signed in to your ChatGPT account. \
Sign in via Settings > AI > LLM Providers to continue."
.into()
}
fn fast_mode_confirmation(&self, _cx: &App) -> Option<FastModeConfirmation> {
Some(FastModeConfirmation {
title: "Enable Fast Mode for OpenAI?".into(),
message: "Fast mode sends requests using OpenAI's Priority processing tier, which \
targets significantly lower latency than the standard tier and is billed at a \
premium per-token rate."
.into(),
})
}
}
impl LanguageModelClient for OpenAiSubscribedProvider {
fn stream_completion(
&self,
model: &LanguageModel,
request: LanguageModelRequest,
cx: &AsyncApp,
) -> BoxFuture<'static, Result<LanguageModelCompletionStream, LanguageModelCompletionError>>
{
let config = match cx.update(|cx| self.config(model, cx)) {
Ok(config) => config,
Err(error) => return async move { Err(error) }.boxed(),
};
let request_limiter = self.request_limiters.for_model(&model.id);
openai_subscribed::stream_completion(&config, &self.state, &request_limiter, request, cx)
}
fn compact(
&self,
model: &LanguageModel,
request: LanguageModelRequest,
cx: &AsyncApp,
) -> BoxFuture<'static, Result<CompactionResult, LanguageModelCompletionError>> {
let config = match cx.update(|cx| self.config(model, cx)) {
Ok(config) => config,
Err(error) => return async move { Err(error) }.boxed(),
};
let request_limiter = self.request_limiters.for_model(&model.id);
openai_subscribed::compact(&config, &self.state, &request_limiter, request, cx)
}
}
struct ConfigurationView {
state: Entity<State>,
/// When `true`, the description is rendered elsewhere (the settings row's
/// left column), so it's omitted here to avoid duplication.
compact: bool,
}
impl Render for ConfigurationView {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let state = self.state.read(cx);
if state.is_authenticated() {
let label = state
.email()
.map(|e| format!("Signed in as {e}"))
.unwrap_or_else(|| "Signed in".to_string());
let model_catalog_error = state.model_catalog_error();
let state_entity = self.state.clone();
return v_flex()
.gap_2()
.child(
ConfiguredApiCard::new("openai-subscribed-sign-out", SharedString::from(label))
.button_label("Sign Out")
.on_click(cx.listener(move |_this, _, _window, cx| {
state_entity
.update(cx, |state, cx| state.sign_out(cx))
.detach_and_log_err(cx);
})),
)
.when_some(model_catalog_error, |this, error| {
this.child(
h_flex()
.gap_1()
.justify_center()
.child(
Icon::new(IconName::XCircle)
.color(Color::Error)
.size(IconSize::Small),
)
.child(Label::new(error).color(Color::Muted)),
)
})
.into_any_element();
}
let last_auth_error = state.last_auth_error();
let provider_state = self.state.clone();
let cancel_provider_state = self.state.clone();
let is_signing_in = state.is_signing_in();
let is_sign_in_cancellable = state.is_sign_in_cancellable();
let button_label = if is_signing_in {
"Signing in…"
} else {
"Sign In"
};
v_flex()
.gap_2()
.when(!self.compact, |this| {
this.child(Label::new(SUBSCRIPTION_DESCRIPTION))
})
.child(
h_flex()
.gap_2()
.child(
Button::new("sign-in", button_label)
.when(!self.compact, |this| this.full_width())
.style(ButtonStyle::Outlined)
.size(ButtonSize::Medium)
.loading(is_signing_in)
.disabled(is_signing_in)
.on_click(move |_, _window, cx| {
provider_state.update(cx, |state, cx| state.sign_in(cx));
}),
)
.when(is_sign_in_cancellable, |this| {
this.child(
Button::new("cancel-sign-in", "Cancel")
.style(ButtonStyle::Subtle)
.size(ButtonSize::Medium)
.on_click(move |_, _window, cx| {
cancel_provider_state
.update(cx, |state, cx| state.cancel_sign_in(cx));
}),
)
}),
)
.when_some(last_auth_error, |this, error| {
this.child(
h_flex()
.gap_1()
.justify_center()
.child(
Icon::new(IconName::XCircle)
.color(Color::Error)
.size(IconSize::Small),
)
.child(Label::new(error).color(Color::Muted)),
)
})
.into_any_element()
}
}
#[cfg(test)]
mod tests {
use super::*;
use gpui::{AsyncApp, TestAppContext};
use http_client::FakeHttpClient;
use parking_lot::Mutex;
use std::future::Future;
use std::pin::Pin;
#[gpui::test]
async fn test_authenticate_awaits_initial_load(cx: &mut TestAppContext) {
let creds_json = serde_json::json!({
"access_token": "fresh_access",
"refresh_token": "fresh_refresh",
"expires_at_ms": u64::MAX,
"account_id": null,
"email": null,
});
let creds_provider = Arc::new(FakeCredentialsProvider::new());
creds_provider.storage.lock().replace((
"Bearer".to_string(),
serde_json::to_vec(&creds_json).unwrap(),
));
let http_client = FakeHttpClient::create(|_| async {
Ok(http_client::Response::builder()
.status(200)
.body(http_client::AsyncBody::from(
serde_json::json!({
"models": [{
"slug": "gpt-account-default",
"display_name": "Account Default",
"default_reasoning_level": "medium",
"supported_reasoning_levels": [],
"visibility": "list",
"priority": 0,
"additional_speed_tiers": [],
"service_tiers": [],
"context_window": 128_000,
"max_context_window": null,
"input_modalities": ["text"]
}]
})
.to_string(),
))?)
});
let provider =
cx.update(|cx| OpenAiSubscribedProvider::new(http_client, creds_provider, cx));
// Before load completes, authenticate should still await the load.
let auth_task = cx.update(|cx| provider.authenticate(cx));
// Drive the load to completion.
cx.run_until_parked();
let result = auth_task.await;
assert!(
result.is_ok(),
"authenticate should succeed after load completes with valid credentials"
);
cx.update(|cx| {
let models = provider.provided_models(cx);
assert_eq!(models.len(), 1);
assert_eq!(
models.first().map(|model| model.id().0.to_string()),
Some("gpt-account-default".to_string())
);
assert_eq!(
provider
.default_model(cx)
.map(|model| model.id().0.to_string()),
Some("gpt-account-default".to_string())
);
});
}
struct FakeCredentialsProvider {
storage: Mutex<Option<(String, Vec<u8>)>>,
}
impl FakeCredentialsProvider {
fn new() -> Self {
Self {
storage: Mutex::new(None),
}
}
}
impl CredentialsProvider for FakeCredentialsProvider {
fn read_credentials<'a>(
&'a self,
_url: &'a str,
_cx: &'a AsyncApp,
) -> Pin<Box<dyn Future<Output = Result<Option<(String, Vec<u8>)>>> + 'a>> {
Box::pin(async { Ok(self.storage.lock().clone()) })
}
fn write_credentials<'a>(
&'a self,
_url: &'a str,
username: &'a str,
password: &'a [u8],
_cx: &'a AsyncApp,
) -> Pin<Box<dyn Future<Output = Result<()>> + 'a>> {
self.storage
.lock()
.replace((username.to_string(), password.to_vec()));
Box::pin(async { Ok(()) })
}
fn delete_credentials<'a>(
&'a self,
_url: &'a str,
_cx: &'a AsyncApp,
) -> Pin<Box<dyn Future<Output = Result<()>> + 'a>> {
*self.storage.lock() = None;
Box::pin(async { Ok(()) })
}
}
}