diff --git a/packages/cli-rs/crates/tui/src/app.rs b/packages/cli-rs/crates/tui/src/app.rs index b87e51667ac..44fecaf931d 100644 --- a/packages/cli-rs/crates/tui/src/app.rs +++ b/packages/cli-rs/crates/tui/src/app.rs @@ -424,6 +424,7 @@ pub fn mount_with(app: &App, mut deps: Deps, args: Args) -> Rc { crate::dialogs::settings::install(&tui); crate::dialogs::mcp::install(&tui); crate::dialogs::theme_list::install(&tui); + crate::dialogs::status::install(&tui); crate::features::devtools::mount(&tui, &shell); crate::dialogs::model::install(&tui); crate::plugins::host::install(&tui); diff --git a/packages/cli-rs/crates/tui/src/dialogs/status.rs b/packages/cli-rs/crates/tui/src/dialogs/status.rs index a165463bdac..2ab20283e58 100644 --- a/packages/cli-rs/crates/tui/src/dialogs/status.rs +++ b/packages/cli-rs/crates/tui/src/dialogs/status.rs @@ -1 +1,213 @@ -//! Status dialog (E.18b). +//! Status dialog (E.18b): `packages/tui/src/component/dialog-status.tsx`, `opencode.status` (`/status`, +//! `s`). It lists the default location's MCP servers and follows the store while it is open. + +use std::cell::Cell; +use std::rc::{Rc, Weak}; + +use opencode_api::generated::{McpServer, McpServerStatus}; +use opencode_store::Catalog; +use opencode_ui::dialog::{DialogCx, DialogOptions, HeaderProps, header, layout}; +use opencode_ui::keymap::{Bind, GLOBAL_MODE, KeymapLayer, Slash, TuiCommand}; +use opencode_ui::theme::Theme; +use opentui::prelude::*; + +use crate::app::Tui; + +/// Registers `opencode.status` ("View status", `/status`) as `app.tsx:1054-1061` does. +pub fn install(tui: &Rc) { + let weak = tui.weak(); + let command = TuiCommand::new("opencode.status", move |_, _| { + if let Some(tui) = weak.upgrade() { + show(&tui); + } + }) + .bind(Bind::Off) + .title("View status") + .group("System") + .palette() + .slash(Slash { + name: "status".to_owned(), + aliases: Vec::new(), + arguments: false, + }); + let layers = [ + KeymapLayer { + mode: Some(GLOBAL_MODE.to_owned()), + commands: vec![command], + ..KeymapLayer::default() + }, + // `appBindingCommands`: `s` by default. + KeymapLayer { + bindings: vec!["opencode.status".to_owned()], + ..KeymapLayer::default() + }, + ]; + for layer in layers { + // The layers live as long as the keymap, which the TUI owns. + if let Err(error) = tui.keymap.layer(layer) { + log::error!("status keymap layer: {error}"); + } + } +} + +/// `DialogStatus`, in place of any open dialog. +pub fn show(tui: &Tui) { + let weak = tui.weak(); + tui.dialogs.replace( + move |cx| { + let Some(tui) = weak.upgrade() else { + return View::new(&cx.app); + }; + let body = View::new(&cx.app); + draw(&body, cx, &servers(&tui)); + let status = Rc::new(Status { + tui: tui.weak(), + cx: Rc::downgrade(cx), + body: body.clone(), + seen: Cell::new(tui.data.read(|state| state.revision())), + open: Cell::new(true), + }); + let closed = status.clone(); + cx.on_close(opentui::keymap::Disposer::new(move || { + closed.open.set(false) + })); + status.load(&tui); + layout( + cx, + &header( + cx, + HeaderProps { + title: "Status".to_owned(), + ..HeaderProps::default() + }, + ), + &body, + None, + ) + }, + None, + DialogOptions::default(), + ); +} + +struct Status { + tui: Weak, + cx: Weak, + body: View, + seen: Cell, + open: Cell, +} + +impl Status { + /// Reads the list when the store has none (TS loads it with the location at startup), then follows the store. + fn load(self: &Rc, tui: &Tui) { + let location = tui.data.read(|state| state.default_location().clone()); + let loaded = tui.data.read(|state| { + state + .location(Some(&location)) + .is_some_and(|data| data.mcp_server.is_some()) + }); + if !loaded { + tui.app.spawn( + tui.data.catalog_sync(Catalog::McpServer, Some(&location)), + |_, result| { + if let Err(error) = result { + log::warn!("mcp list: {error}"); + } + }, + ); + } + self.watch(tui.data.subscribe()); + } + + fn watch(self: &Rc, mut revisions: tokio::sync::watch::Receiver) { + let Some(tui) = self.tui.upgrade() else { + return; + }; + let status = self.clone(); + tui.app.spawn( + async move { revisions.changed().await.is_ok().then_some(revisions) }, + move |_, revisions| { + let (Some(tui), Some(cx), Some(revisions)) = + (status.tui.upgrade(), status.cx.upgrade(), revisions) + else { + return; + }; + if !status.open.get() { + return; + } + let location = tui.data.read(|state| state.default_location().clone()); + let (revision, changes) = tui.data.changes_since(status.seen.get()); + status.seen.set(revision); + if changes.location(&location) { + draw(&status.body, &cx, &servers(&tui)); + } + status.watch(revisions); + }, + ); + } +} + +/// `data.location.mcp.server.list() ?? []` for the default location, in the server's order. +fn servers(tui: &Tui) -> Vec { + tui.data.read(|state| { + let location = state.default_location().clone(); + state + .location(Some(&location)) + .and_then(|data| data.mcp_server.clone()) + .unwrap_or_default() + }) +} + +fn draw(body: &View, cx: &DialogCx, servers: &[McpServer]) { + let app = &cx.app; + let theme = &cx.theme; + body.clear(); + if servers.is_empty() { + body.add(&Text::new(app, "No MCP servers").fg(theme.text.base)); + return; + } + let plural = if servers.len() == 1 { "" } else { "s" }; + body.add(&Text::new(app, format!("{} MCP server{plural}", servers.len())).fg(theme.text.base)); + for server in servers { + let row = View::new(app).row().gap(1); + row.add( + &Text::new(app, "•") + .fg(color(theme, &server.status)) + .shrink(0.0), + ); + let text = Text::new(app, "").fg(theme.text.base).wrap(WrapMode::Word); + text.set_text(vec![ + Span::styled(server.name.clone(), Style::new().fg(theme.text.base).bold()), + Span::styled(" ", Style::new().fg(theme.text.base)), + Span::styled(describe(&server.status), Style::new().fg(theme.text.muted)), + ]); + row.add(&text); + body.add(&row); + } +} + +fn color(theme: &Theme, status: &McpServerStatus) -> Color { + match status { + McpServerStatus::Connected(_) => theme.text.feedback.success.base, + McpServerStatus::Failed(_) => theme.text.feedback.error.base, + McpServerStatus::NeedsAuth(_) => theme.text.feedback.warning.base, + _ => theme.text.muted, + } +} + +/// The muted text after the name; an unknown status shows its name, as the TS `Switch` fallback does. +fn describe(status: &McpServerStatus) -> String { + match status { + McpServerStatus::Connected(_) => "Connected".to_owned(), + McpServerStatus::Failed(failed) => failed.error.clone(), + McpServerStatus::Disabled(_) => "Disabled in configuration".to_owned(), + McpServerStatus::NeedsAuth(auth) => format!("Needs authentication: {}", auth.error), + McpServerStatus::Pending(_) => "pending".to_owned(), + McpServerStatus::Unknown(value) => value + .get("status") + .and_then(serde_json::Value::as_str) + .unwrap_or_default() + .to_owned(), + } +} diff --git a/packages/cli-rs/crates/tui/tests/system.rs b/packages/cli-rs/crates/tui/tests/system.rs index 9bc1c9d9eea..f3be61e28be 100644 --- a/packages/cli-rs/crates/tui/tests/system.rs +++ b/packages/cli-rs/crates/tui/tests/system.rs @@ -15,11 +15,13 @@ use opencode_api::client::Client; use opencode_api::transport::{BoxFuture, Request, Response, Transport}; use opencode_local::cli_config::{Info, Resolved, merge}; use opencode_parity::{Fixture, compare, fixtures_dir}; -use opencode_tui::dialogs::{settings, theme_list}; +use opencode_tui::dialogs::{settings, status, theme_list}; use opencode_tui::{Deps, Route, Tui}; use opentui::testing::TestApp; use serde_json::{Value, json}; +const DIRECTORY: &str = "/tmp/opencode/app"; + type Answer = Box Option<(u16, Value)> + Send + Sync>; /// Answers with `answer` (method, path, body), else an empty session list, else 404; logs every request. @@ -101,7 +103,7 @@ fn mount_with(app: &TestApp, info: Info, answer: Answer) -> Mounted { app, Deps { client, - directory: "/project".into(), + directory: DIRECTORY.into(), config: Resolved::new(info, |_| None), roots: None, route: Route::Home, @@ -270,3 +272,43 @@ fn settings_theme_opens_the_list_and_comes_back() { assert_eq!(configured_theme(&tui).as_deref(), Some("opencode2")); assert_eq!(theme(&tui), "opencode2"); } + +// Status ------------------------------------------------------------------------------------------------------ + +fn location() -> Value { + json!({ "directory": DIRECTORY, "project": { "id": "proj_app", "directory": DIRECTORY, "canonical": DIRECTORY } }) +} + +fn servers() -> Value { + json!([ + { "name": "linear", "status": { "status": "failed", "error": "MCP error -32000: Connection closed" } }, + { "name": "github", "status": { "status": "connected" } }, + { "name": "sentry", "status": { "status": "needs_auth", "error": "Authentication required" } }, + { "name": "docs", "status": { "status": "disabled" } }, + { "name": "figma", "status": { "status": "pending" } }, + ]) +} + +fn mcp(list: Value) -> Answer { + Box::new(move |_, path, _| match path { + "/api/location" => Some((200, location())), + "/api/mcp" => Some((200, json!({ "location": location(), "data": list.clone() }))), + _ => None, + }) +} + +#[test] +fn status_matches_typescript() { + let app = TestApp::new(80, 30); + let mounted = mount_with(&app, info(json!({})), mcp(servers())); + mounted.tui.keymap.dispatch("opencode.status", None); + wait_for(&app, "5 MCP servers"); + assert_dialog(&app, "E.18b/status", "Status"); + assert_eq!(mounted.requests("/api/mcp").len(), 1); + + let app = TestApp::new(80, 30); + let tui = mount_with(&app, info(json!({})), mcp(json!([]))).tui; + status::show(&tui); + wait_for(&app, "No MCP servers"); + assert_dialog(&app, "E.18b/status-empty", "Status"); +} diff --git a/packages/cli-rs/parity/component/E.18b-system.tsx b/packages/cli-rs/parity/component/E.18b-system.tsx index 2ca110e77e0..c30454a27d4 100644 --- a/packages/cli-rs/parity/component/E.18b-system.tsx +++ b/packages/cli-rs/parity/component/E.18b-system.tsx @@ -1,16 +1,106 @@ /** @jsxImportSource @opentui/solid */ // P-COMP fixtures for the system dialogs (E.18b): crates/tui/tests/system.rs opens the Rust dialogs over the whole -// TUI and compares the dialog panel. +// TUI with the same responses (keep `SERVERS` and the other data below in step with it) and compares the dialog +// panel. +import { onMount, type JSX } from "solid-js" +import { DialogStatus } from "../../../tui/src/component/dialog-status" import { DialogThemeList } from "../../../tui/src/component/dialog-theme-list" +import { ConfigProvider } from "../../../tui/src/config" +import { ClientProvider } from "../../../tui/src/context/client" +import { DataProvider, useData } from "../../../tui/src/context/data" +import { Keymap } from "../../../tui/src/context/keymap" +import { LocationProvider } from "../../../tui/src/context/location" +import { RouteProvider } from "../../../tui/src/context/route" +import { ThemeProvider } from "../../../tui/src/context/theme" +import { DialogProvider, useDialog } from "../../../tui/src/ui/dialog" +import { ToastProvider } from "../../../tui/src/ui/toast" +import { emptyThemeSource } from "../../../tui/test/fixture/fixture" +import { createApi, createEventStream, createFetch, json } from "../../../tui/test/fixture/tui-client" +import { TestTuiContexts } from "../../../tui/test/fixture/tui-environment" +import { createTuiResolvedConfig } from "../../../tui/test/fixture/tui-runtime" +import { withNewDesigns } from "../lib/app" import { renderComponent } from "../lib/component" -const themes = { - width: 80, - height: 30, - dialog: () => , - ready: (f: string) => f.includes("esc"), +const directory = "/tmp/opencode/app" +const location = { directory, project: { id: "proj_app", directory, canonical: directory } } + +const SERVERS = [ + { name: "linear", status: { status: "failed", error: "MCP error -32000: Connection closed" } }, + { name: "github", status: { status: "connected" } }, + { name: "sentry", status: { status: "needs_auth", error: "Authentication required" } }, + { name: "docs", status: { status: "disabled" } }, + { name: "figma", status: { status: "pending" } }, +] + +let servers: unknown[] = SERVERS + +const calls = createFetch((url) => { + if (url.pathname === "/api/location") return json(location) + if (url.pathname === "/api/mcp") return json({ location, data: servers }) + return undefined +}, createEventStream()) + +type Act = Parameters[0]["act"] + +function Probe(props: { open: () => JSX.Element; mcp: boolean }) { + const data = useData() + const dialog = useDialog() + onMount(() => { + // The TUI reads the MCP list with the location at startup; the Rust dialog reads it when the store has none. + if (props.mcp) void data.location.mcp.server.sync().then(() => dialog.replace(props.open)) + else dialog.replace(props.open) + }) + return null } -await renderComponent({ ...themes, fixture: "E.18b/themes" }) -// One move previews the next theme, so the dialog draws in it. -await renderComponent({ ...themes, fixture: "E.18b/themes-down", act: (setup) => setup.mockInput.pressArrow("down") }) -await renderComponent({ ...themes, fixture: "E.18b/themes-filter", act: (setup) => setup.mockInput.typeText("tokyo") }) + +async function render( + fixture: string, + open: () => JSX.Element, + ready: (frame: string) => boolean, + options: { act?: Act; mcp?: boolean; width?: number; height?: number } = {}, +) { + await renderComponent({ + fixture, + width: options.width ?? 80, + height: options.height ?? 30, + bare: () => ( + + + + + + + + + + + + + + + + + + + + + + ), + ready, + act: options.act, + }) +} + +// Themes: one move previews the next theme, so the dialog draws in it. +const themes = () => +const esc = (f: string) => f.includes("esc") +await render("E.18b/themes", themes, esc) +await render("E.18b/themes-down", themes, esc, { act: (setup) => setup.mockInput.pressArrow("down") }) +await render("E.18b/themes-filter", themes, esc, { act: (setup) => setup.mockInput.typeText("tokyo") }) + +// Status. +const status = () => +await render("E.18b/status", status, (f) => f.includes("MCP servers"), { mcp: true }) +servers = [] +await render("E.18b/status-empty", status, (f) => f.includes("No MCP servers"), { mcp: true }) +servers = SERVERS diff --git a/packages/cli-rs/parity/fixtures/E.18b/status-empty.json b/packages/cli-rs/parity/fixtures/E.18b/status-empty.json new file mode 100644 index 00000000000..5499b7cf6c4 --- /dev/null +++ b/packages/cli-rs/parity/fixtures/E.18b/status-empty.json @@ -0,0 +1,74 @@ +{ + "width": 80, + "height": 30, + "rows": [ + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " Status esc ", + " ", + " No MCP servers ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " " + ], + "styles": [ + "#ffffffff #040404ff 0", + "#ffffffff #141414ff 0", + "#eeeeeeff #141414ff 0", + "#808080ff #141414ff 0" + ], + "runs": [ + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,10],[1,60],[0,10]], + [[0,10],[1,3],[2,6],[1,45],[3,3],[1,3],[0,10]], + [[0,10],[1,60],[0,10]], + [[0,10],[1,3],[2,14],[1,43],[0,10]], + [[0,10],[1,60],[0,10]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]] + ] +} diff --git a/packages/cli-rs/parity/fixtures/E.18b/status.json b/packages/cli-rs/parity/fixtures/E.18b/status.json new file mode 100644 index 00000000000..ed8d6fd9479 --- /dev/null +++ b/packages/cli-rs/parity/fixtures/E.18b/status.json @@ -0,0 +1,78 @@ +{ + "width": 80, + "height": 30, + "rows": [ + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " Status esc ", + " ", + " 5 MCP servers ", + " • linear MCP error -32000: Connection closed ", + " • github Connected ", + " • sentry Needs authentication: Authentication required ", + " • docs Disabled in configuration ", + " • figma pending ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " " + ], + "styles": [ + "#ffffffff #040404ff 0", + "#ffffffff #141414ff 0", + "#eeeeeeff #141414ff 0", + "#808080ff #141414ff 0", + "#e06c75ff #141414ff 0", + "#eeeeeeff #141414ff 1", + "#7fd88fff #141414ff 0", + "#f5a742ff #141414ff 0" + ], + "runs": [ + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,10],[1,60],[0,10]], + [[0,10],[1,3],[2,6],[1,45],[3,3],[1,3],[0,10]], + [[0,10],[1,60],[0,10]], + [[0,10],[1,3],[2,13],[1,44],[0,10]], + [[0,10],[1,3],[4,1],[1,1],[5,6],[2,1],[3,35],[1,13],[0,10]], + [[0,10],[1,3],[6,1],[1,1],[5,6],[2,1],[3,9],[1,39],[0,10]], + [[0,10],[1,3],[7,1],[1,1],[5,6],[2,1],[3,45],[1,3],[0,10]], + [[0,10],[1,3],[3,1],[1,1],[5,4],[2,1],[3,25],[1,25],[0,10]], + [[0,10],[1,3],[3,1],[1,1],[5,5],[2,1],[3,7],[1,42],[0,10]], + [[0,10],[1,60],[0,10]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]], + [[0,80]] + ] +}