use gpui_kit::{AnyElement, App, AppContext as _};
use subspace_agent::SignInOffer;
use subspace_core::credentials::gh_sign_in::{DEVICE_URL, GhSignIn};
use subspace_dashboard::Tokens;
use subspace_dashboard::github_sign_in::{self, SignInEvent};
use super::{ChatPanel, ClipboardItem, Context, services};
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum SignIn {
Offered {
offer: SignInOffer,
failure: Option<String>,
},
Starting { login: String },
Waiting { login: String, code: String },
}
impl SignIn {
pub(super) fn offered(offer: SignInOffer) -> Self {
Self::Offered {
offer,
failure: None,
}
}
pub(super) fn in_app(&self) -> bool {
matches!(
self,
Self::Offered {
offer: SignInOffer::InApp { .. },
..
}
)
}
pub(super) fn in_progress(&self) -> bool {
!matches!(self, Self::Offered { .. })
}
pub(super) fn note(&self) -> Option<String> {
match self {
Self::Offered { failure, .. } => failure.clone(),
Self::Starting { .. } => Some("Starting GitHub sign-in…".into()),
Self::Waiting { code, .. } => Some(format!(
"Enter code {code} at {DEVICE_URL}. The code is on the clipboard. Subspace \
reconnects when GitHub confirms the sign-in."
)),
}
}
}
pub(super) fn subscriptions(cx: &mut Context<ChatPanel>) -> Vec<gpui_kit::Subscription> {
github_sign_in::shared(cx)
.map(|sign_in| {
vec![
cx.observe(&sign_in, |_, _, cx| cx.notify()),
cx.subscribe(&sign_in, |chat, _, event, cx| {
chat.on_sign_in_event(event, cx)
}),
]
})
.unwrap_or_default()
}
impl ChatPanel {
pub(super) fn sign_in(&mut self, cx: &mut Context<Self>) {
match &self.sign_in {
Some(SignIn::Offered {
offer: SignInOffer::InApp { .. },
..
}) => {
if let Some(shared) = github_sign_in::shared(cx) {
shared.update(cx, |sign_in, cx| sign_in.start(cx));
}
}
Some(SignIn::Offered {
offer: SignInOffer::GhRefresh { login },
..
}) => {
let login = login.clone();
self.gh_sign_in(login, cx);
}
_ => {}
}
}
pub(super) fn signing_in(&self, cx: &App) -> bool {
let Some(sign_in) = &self.sign_in else {
return false;
};
sign_in.in_progress()
|| (sign_in.in_app()
&& github_sign_in::shared(cx).is_some_and(|shared| shared.read(cx).in_progress()))
}
pub(super) fn in_app_status(
&self,
tokens: &Tokens,
cx: &mut Context<Self>,
) -> Option<AnyElement> {
let shared = github_sign_in::shared(cx)
.filter(|_| self.sign_in.as_ref().is_some_and(SignIn::in_app))?;
let phase = shared.read(cx).phase().clone();
let cancel = move |_: &gpui_kit::ClickEvent, _: &mut gpui_kit::Window, cx: &mut App| {
shared.update(cx, |sign_in, cx| sign_in.cancel(cx));
};
github_sign_in::status("chat-sign-in", &phase, tokens, cancel, cx)
}
fn on_sign_in_event(&mut self, event: &SignInEvent, cx: &mut Context<Self>) {
let offered = self.sign_in.as_ref().is_some_and(SignIn::in_app);
if offered && matches!(event, SignInEvent::SignedIn(_)) {
self.restart_runtime(cx);
}
}
fn gh_sign_in(&mut self, login: String, cx: &mut Context<Self>) {
self.sign_in = Some(SignIn::Starting {
login: login.clone(),
});
cx.notify();
cx.spawn(async move |chat, cx| {
let started = cx.background_spawn(async { GhSignIn::start() }).await;
let signing = match started {
Ok(signing) => signing,
Err(error) => {
let _ = chat.update(cx, |chat, cx| chat.sign_in_failed(login, error.0, cx));
return;
}
};
let code = signing.code().to_owned();
let _ = chat.update(cx, |chat, cx| chat.show_code(login.clone(), code, cx));
let finished = cx.background_spawn(async move { signing.finish() }).await;
let _ = chat.update(cx, |chat, cx| match finished {
Ok(()) => chat.signed_in(&login, cx),
Err(error) => chat.sign_in_failed(login, error.0, cx),
});
})
.detach();
}
fn show_code(&mut self, login: String, code: String, cx: &mut Context<Self>) {
cx.write_to_clipboard(ClipboardItem::new_string(code.clone()));
crate::links::open(DEVICE_URL, cx);
self.sign_in = Some(SignIn::Waiting { login, code });
cx.notify();
}
fn sign_in_failed(&mut self, login: String, failure: String, cx: &mut Context<Self>) {
self.sign_in = Some(SignIn::Offered {
offer: SignInOffer::GhRefresh { login },
failure: Some(failure),
});
cx.notify();
}
fn signed_in(&mut self, login: &str, cx: &mut Context<Self>) {
if let Some(services) = services(cx) {
services.credentials.forget_gh_token(login);
}
self.restart_runtime(cx);
}
}
#[cfg(test)]
mod tests {
use subspace_agent::SignInOffer;
use super::SignIn;
#[test]
fn the_note_gives_the_code_while_gh_waits_and_the_reason_after_a_failure() {
let offered = SignIn::offered(SignInOffer::GhRefresh {
login: "octocat".into(),
});
assert!(!offered.in_progress());
assert!(!offered.in_app());
assert_eq!(offered.note(), None);
let waiting = SignIn::Waiting {
login: "octocat".into(),
code: "AB12-CD34".into(),
};
assert!(waiting.in_progress());
assert!(
waiting
.note()
.is_some_and(|note| note.contains("AB12-CD34") && note.contains("login/device"))
);
let failed = SignIn::Offered {
offer: SignInOffer::GhRefresh {
login: "octocat".into(),
},
failure: Some("GitHub sign-in did not finish: gh failed.".into()),
};
assert!(!failed.in_progress(), "a failed sign-in can be tried again");
assert_eq!(
failed.note().as_deref(),
Some("GitHub sign-in did not finish: gh failed.")
);
}
#[test]
fn a_sign_in_in_subspace_has_no_gh_note() {
let offered = SignIn::offered(SignInOffer::InApp { login: None });
assert!(offered.in_app());
assert!(!offered.in_progress());
assert_eq!(offered.note(), None);
}
mod panel {
use std::sync::Arc;
use gpui_kit::TestAppContext;
use gpui_kit::test::TestWindowExt as _;
use subspace_agent::{Command, Event, SignInOffer};
use subspace_core::Secret;
use subspace_dashboard::github_sign_in::{self, Phase, SignInEvent};
use subspace_github_auth::{AuthError, Authenticator, Started};
use super::super::ChatPanel;
use crate::prefs;
struct Unused;
impl Authenticator for Unused {
fn start(&self) -> Result<Started, AuthError> {
Err(AuthError::Cancelled)
}
fn revoke(&self, _: &Secret) {}
}
#[gpui_kit::test]
fn a_missing_account_offers_the_subspace_sign_in_and_reconnects_after_it(
cx: &mut TestAppContext,
) {
let directory = tempfile::tempdir().unwrap();
let (handle, mut commands) = subspace_agent::AgentHandle::test_offline(
directory.path().into(),
vec![
Event::SignInRequired(SignInOffer::InApp { login: None }),
Event::Error("No GitHub account found.".into()),
],
);
let shared = cx.update(|cx| {
cx.set_reduce_motion(true);
gpui_kit::init(cx);
prefs::init_file(None, cx);
github_sign_in::install(Arc::new(Unused), cx)
});
let (chat, cx) = cx.add_window_view(|window, cx| {
ChatPanel::with_engine(subspace_core::config::ChatEngine::Copilot, window, cx)
});
cx.update(|_, cx| {
chat.update(cx, |chat, cx| {
chat.connected(Ok(handle));
chat.poll(cx);
assert!(
chat.sign_in
.as_ref()
.is_some_and(super::super::SignIn::in_app)
);
})
});
cx.update(|_, cx| shared.update(cx, |sign_in, cx| sign_in.show(Phase::Browser, cx)));
cx.run_until_parked();
cx.update(|window, cx| {
window.draw(cx).clear(cx);
assert!(window.find("chat-sign-in-cancel").visible());
assert!(chat.read(cx).signing_in(cx));
});
cx.update(|_, cx| {
shared.update(cx, |_, cx| {
cx.emit(SignInEvent::SignedIn("octo-dev".into()))
});
});
cx.run_until_parked();
assert!(matches!(commands.try_recv(), Ok(Command::Shutdown)));
}
}
}