diff --git a/apps/desktop/src/plugin_host/bridge/servers.rs b/apps/desktop/src/plugin_host/bridge/servers.rs index ac5cf45ae..31762dfb6 100644 --- a/apps/desktop/src/plugin_host/bridge/servers.rs +++ b/apps/desktop/src/plugin_host/bridge/servers.rs @@ -170,6 +170,19 @@ pub fn authenticate(app: &mut App, key: &str, tx: &mut Tx) { run_actions(app, key, effects.unwrap_or_default(), tx); } +/// Re-authenticates or reconnects a signed-out plugin server `key`: runs `authenticate` when its +/// machine is waiting on auth, otherwise tries the source's `reconnect` then `connect` handler. +/// Returns `true` when a plugin handler was invoked. +pub fn reauthenticate(app: &mut App, key: &str, tx: &mut Tx) -> bool { + sync_actions(app); + if can_authenticate(key) { + authenticate(app, key, tx); + return true; + } + super::server_sources::call(app, key, "reconnect", None, tx) + || super::server_sources::call(app, key, "connect", None, tx) +} + /// Whether plugin server `key` waits for a sign-in it can start, or one is on its way. pub fn can_authenticate(key: &str) -> bool { ACTIONS.with_borrow(|a| a.get(key).is_some_and(|s| s.machine.can_authenticate())) diff --git a/apps/desktop/src/shell/view/home/servers.rs b/apps/desktop/src/shell/view/home/servers.rs index 07eca8e70..7a623b704 100644 --- a/apps/desktop/src/shell/view/home/servers.rs +++ b/apps/desktop/src/shell/view/home/servers.rs @@ -238,7 +238,7 @@ fn authenticate(s: &mut App, key: &ServerKey, tx: &mut Tx) -> bool { } // A plugin's server signs in through its plugin (`ServerConnection.authenticate`). ServerConn::Plugin { .. } => crate::plugin_host::connect_server(s, key.as_str(), tx), - ServerConn::Sidecar { .. } => {} + ServerConn::Sidecar { .. } => crate::view::server_unavailable::recover_signed_out(s, key, tx), } true } diff --git a/apps/desktop/src/shell/view/server_unavailable.rs b/apps/desktop/src/shell/view/server_unavailable.rs index ec29d1480..b1ea669f8 100644 --- a/apps/desktop/src/shell/view/server_unavailable.rs +++ b/apps/desktop/src/shell/view/server_unavailable.rs @@ -95,37 +95,134 @@ pub fn unavailable(p: &Pal, reason: Reason, server: &ServerKey, close: Option<&s frame(key, children) } -/// `ServerSignedOutPanel`: the lock, signed out, and Sign in again for a server added by address -/// (its Edit server dialog takes a new password, as Home's Authenticate does). +/// How the user recovers from a signed-out server in [`signed_out`], [`signed_out_status`], and +/// Home's server auth action so no server kind ends at a bare non-interactive label. +#[derive(Clone, Debug)] +enum Recovery { + /// A server saved in `servers.json`: open Edit server to update its password or pairing link. + EditSaved(String), + /// The browser page's own server (`apps/web`): drop the saved password and show sign-in again. + PageSignIn, + /// A plugin-contributed server: run the plugin's authenticate/reconnect handler, falling back + /// to Settings › Servers if none exists. + Plugin(ServerKey), + /// The app's managed local sidecar (`ServerKey::sidecar()`): re-discover `service.json` and + /// reconnect/restart the local background service. + ReconnectLocal, + /// An explicit `--server` / `OPENCODE_SERVER_URL` or unlisted server not in `servers.json`: + /// nudge health and open Settings › Servers so the user can inspect or add it. + OpenServerSettings(ServerKey), +} + +impl Recovery { + fn for_server(server: &ServerKey) -> Self { + if let Some(url) = saved_url(server) { + return Self::EditSaved(url); + } + if server.is_sidecar() { + if crate::page_sign_in::can_sign_out() { + return Self::PageSignIn; + } + if is_managed_sidecar() { + return Self::ReconnectLocal; + } + return Self::OpenServerSettings(server.clone()); + } + if is_plugin_server(server) { + return Self::Plugin(server.clone()); + } + Self::OpenServerSettings(server.clone()) + } + + fn label(&self) -> &'static str { + match self { + Self::EditSaved(_) | Self::PageSignIn | Self::Plugin(_) => opencode_i18n::t!("session.error.signedOut.signIn"), + Self::ReconnectLocal | Self::OpenServerSettings(_) => opencode_i18n::t!("project.start.reconnect"), + } + } + + fn run(&self, s: &mut App, tx: &mut Tx) { + match self { + Self::EditSaved(url) => crate::servers::dialog::open(s, Some(url), tx), + Self::PageSignIn => crate::page_sign_in::sign_out(), + Self::Plugin(key) => { + tx.instant(); + if !crate::plugin_host::servers::reauthenticate(s, key.as_str(), tx) { + opencode_kit::client::Server::wake_reconnects(); + crate::servers::health::nudge(s, key, tx); + s.open_settings_page(crate::settings::pages::Page::Servers, tx); + } + } + Self::ReconnectLocal => { + tx.instant(); + opencode_kit::client::Server::wake_reconnects(); + crate::servers::health::nudge(s, &ServerKey::sidecar(), tx); + if crate::launch::offline(s) { + crate::launch::retry(s, tx); + } + } + Self::OpenServerSettings(key) => { + tx.instant(); + opencode_kit::client::Server::wake_reconnects(); + crate::servers::health::nudge(s, key, tx); + s.open_settings_page(crate::settings::pages::Page::Servers, tx); + } + } + } +} + +/// Runs the appropriate sign-in or reconnect recovery action for a signed-out `server`. +pub fn recover_signed_out(s: &mut App, server: &ServerKey, tx: &mut Tx) { + Recovery::for_server(server).run(s, tx); +} + +fn is_managed_sidecar() -> bool { + #[cfg(not(target_arch = "wasm32"))] + { + crate::servers::sidecar::managed() + } + #[cfg(target_arch = "wasm32")] + { + false + } +} + +fn is_plugin_server(key: &ServerKey) -> bool { + crate::servers::runtime::listed() + .iter() + .any(|l| l.key == *key && matches!(l.conn, opencode_servers::ServerConn::Plugin { .. })) + || crate::plugin_host::servers::can_authenticate(key.as_str()) +} + +/// `ServerSignedOutPanel`: the lock, signed out, and an actionable recovery button (Sign in again +/// for saved/plugin/page servers, Reconnect for the local sidecar or unlisted servers). pub fn signed_out(p: &Pal, server: &ServerKey) -> El { let name = crate::servers::runtime::label(server); - let mut children = vec![ + let action = Recovery::for_server(server); + let label = action.label(); + let children = vec![ icon(&ic::LOCK, 20.0).color(p.icon_muted), heading(p, opencode_i18n::t!("session.error.signedOut").to_owned(), opencode_i18n::t!("session.error.signedOut.description", server = name).to_owned()), + crate::widgets::button(label, rune_kit::ButtonVariant::Neutral) + .on_press_tx(move |s: &mut App, tx: &mut Tx| action.run(s, tx)) + .build(crate::palette::kit(p)) + .key("session-signed-out-sign-in"), ]; - if let Some(url) = saved_url(server) { - children.push( - crate::widgets::button(opencode_i18n::t!("session.error.signedOut.signIn"), rune_kit::ButtonVariant::Neutral) - .on_press_tx(move |s: &mut App, tx: &mut Tx| crate::servers::dialog::open(s, Some(&url), tx)) - .build(crate::palette::kit(p)) - .key("session-signed-out-sign-in"), - ); - } frame("session-signed-out", children) } /// The session header's word for a signed-out server whose session stays on screen: what -/// [`signed_out`] says, in one line, with its Sign in again as a link. +/// [`signed_out`] says, in one line, with its recovery action as a clickable link. pub fn signed_out_status(p: &Pal, server: &ServerKey) -> El { let label = text(opencode_i18n::t!("session.error.signedOut").to_owned()).font(crate::view::UI_SMALL).color(p.faint); - let link = saved_url(server).map(|url| { - text(opencode_i18n::t!("session.error.signedOut.signIn").to_owned()) - .key("connection-sign-in") - .font(crate::view::UI_SMALL) - .color(p.text) - .on_press_tx(move |s: &mut App, tx: &mut Tx| crate::servers::dialog::open(s, Some(&url), tx)) - }); - rune::row().key("connection-status").gap(6.0).center_y().child(label).children(link) + let action = Recovery::for_server(server); + let link = text(action.label().to_owned()) + .key("connection-sign-in") + .role("button") + .font(crate::view::UI_SMALL) + .color(p.text) + .on_press_tx(move |s: &mut App, tx: &mut Tx| action.run(s, tx)); + rune::row().key("connection-status").gap(6.0).center_y().child(label).child(link) } /// The `servers.json` address of server `key`, when it was added by address. diff --git a/apps/desktop/src/workspace/lifecycle/reconnect.rs b/apps/desktop/src/workspace/lifecycle/reconnect.rs index 21fd087d9..a72ec824c 100644 --- a/apps/desktop/src/workspace/lifecycle/reconnect.rs +++ b/apps/desktop/src/workspace/lifecycle/reconnect.rs @@ -179,9 +179,13 @@ pub fn step_link(s: &mut App, server: &str, event: opencode_kit::servers::link:: use opencode_kit::servers::link::Effect; let mut state = s.services.server_links.state(server); let was = state.dropped(); + let was_signed_out = matches!(state, Link::SignedOut); let opened = matches!(event, Event::StreamUp); + let stream_down = matches!(event, Event::StreamDown { .. }); let effects = state.step(event); let dropped = state.dropped(); + let signed_out = matches!(state, Link::SignedOut); + let up = !dropped && !matches!(state, Link::SignedOut | Link::Incompatible); if state == Link::Online { s.services.server_links.states.remove(server); } else { @@ -189,9 +193,15 @@ pub fn step_link(s: &mut App, server: &str, event: opencode_kit::servers::link:: } // The stream opening says the server's alive for as long as it stays up (its heartbeat), so // the health poll leaves it be; its dropping is the first word that the server went, and the - // poll asks at once and again every 10 s. + // poll asks at once and again every 10 s. A 401/403 on the event stream nudges health right + // away too, so a managed sidecar re-reads service.json / reconnects and remote servers update + // their auth state immediately. + let key = opencode_kit::servers::ServerKey::new(server); if opened || was != dropped { - crate::servers::health::streaming(s, &opencode_kit::servers::ServerKey::new(server), !dropped, tx); + crate::servers::health::streaming(s, &key, up, tx); + } + if stream_down && signed_out && !was_signed_out { + crate::servers::health::nudge(s, &key, tx); } let mut changed = false; for effect in effects { diff --git a/apps/desktop/src/workspace/servers/health.rs b/apps/desktop/src/workspace/servers/health.rs index 1477c41df..22aa4d91e 100644 --- a/apps/desktop/src/workspace/servers/health.rs +++ b/apps/desktop/src/workspace/servers/health.rs @@ -370,7 +370,10 @@ fn probe(s: &App, key: &ServerKey, attempt: Attempt) -> Option { }; let (server, result) = (current.clone(), probe_http(¤t, &conn)); #[cfg(not(target_arch = "wasm32"))] - let (server, result) = if result == ProbeResult::Unreachable && discover_sidecar && super::sidecar::managed() { + let (server, result) = if matches!(result, ProbeResult::Unreachable | ProbeResult::Unauthorized) + && discover_sidecar + && super::sidecar::managed() + { match super::sidecar::reconnect() { Ok(restarted) => { let res = probe_http(&restarted, &conn);