diff --git a/packages/app/src/plugins/host-navigation-model.ts b/packages/app/src/plugins/host-navigation-model.ts index f651370a9d..d6700ccf2c 100644 --- a/packages/app/src/plugins/host-navigation-model.ts +++ b/packages/app/src/plugins/host-navigation-model.ts @@ -1,24 +1,39 @@ import type { PluginSurfaceProps } from "@getpaseo/plugin/client"; import type { NavigateToWorkspaceInput } from "@/stores/navigation-active-workspace-store"; +import { parseServerIdFromPathname } from "@/utils/host-routes"; import { isHttpUrl } from "@/utils/http-url"; interface HostNavigationOwner { browserAvailable: boolean; - openAgent(input: { serverId: string; agentId: string }): void; - openWorkspace(input: NavigateToWorkspaceInput): void; + isHostRegistered(serverId: string): boolean; + openAgent(input: { serverId: string; agentId: string }): string; + openWorkspace(input: NavigateToWorkspaceInput): string; resolveWorkspace(input: { serverId: string; workspaceId: string }): string | null; createBrowser(input: { initialUrl: string }): { browserId: string }; + focusHost(serverId: string): void; } export function createPluginHostNavigation( serverId: string, owner: HostNavigationOwner, ): NonNullable { + const focusRouteHost = (route: string) => { + const destinationServerId = parseServerIdFromPathname(route); + if (destinationServerId && owner.isHostRegistered(destinationServerId)) { + owner.focusHost(destinationServerId); + } + }; + return { - openAgent: ({ agentId, serverId: targetServerId }) => - owner.openAgent({ serverId: targetServerId ?? serverId, agentId }), - openWorkspace: ({ workspaceId, serverId: targetServerId }) => - owner.openWorkspace({ serverId: targetServerId ?? serverId, workspaceId }), + supportsFocusHost: true, + openAgent: ({ agentId, serverId: targetServerId, focusHost }) => { + const route = owner.openAgent({ serverId: targetServerId ?? serverId, agentId }); + if (focusHost) focusRouteHost(route); + }, + openWorkspace: ({ workspaceId, serverId: targetServerId, focusHost }) => { + const route = owner.openWorkspace({ serverId: targetServerId ?? serverId, workspaceId }); + if (focusHost) focusRouteHost(route); + }, openBrowser: owner.browserAvailable ? ({ url, workspaceId, serverId: targetServerId }) => { if (!isHttpUrl(url)) throw new Error("Only absolute HTTP(S) URLs are supported."); diff --git a/packages/app/src/plugins/host-navigation.test.ts b/packages/app/src/plugins/host-navigation.test.ts index 22538b3976..4da0e62df5 100644 --- a/packages/app/src/plugins/host-navigation.test.ts +++ b/packages/app/src/plugins/host-navigation.test.ts @@ -1,25 +1,58 @@ import { describe, expect, it } from "vitest"; +import { buildHostAgentDetailRoute, buildHostWorkspaceRoute } from "@/utils/host-routes"; import { createPluginHostNavigation } from "./host-navigation-model"; +import { useSidebarViewStore } from "../stores/sidebar-view-store"; describe("plugin host navigation", () => { function setup(electron = true) { const destinations: unknown[] = []; const browsers: string[] = []; + const focusedHosts: string[] = []; const workspaces = new Set(["selected:one", "remote:two"]); const navigation = createPluginHostNavigation("selected", { browserAvailable: electron, + isHostRegistered: (serverId: string) => serverId === "selected" || serverId === "remote", resolveWorkspace: ({ serverId, workspaceId }) => workspaces.has(`${serverId}:${workspaceId}`) ? workspaceId : null, - openAgent: (input) => destinations.push(input), - openWorkspace: (input) => destinations.push(input), + openAgent: (input) => { + destinations.push(input); + return buildHostAgentDetailRoute(input.serverId, input.agentId); + }, + openWorkspace: (input) => { + destinations.push(input); + return buildHostWorkspaceRoute(input.serverId, input.workspaceId); + }, + focusHost: (serverId) => { + focusedHosts.push(serverId); + useSidebarViewStore.getState().focusHost(serverId); + }, createBrowser: ({ initialUrl }) => { browsers.push(initialUrl); return { browserId: `browser-${browsers.length}` }; }, }); - return { navigation, destinations, browsers, workspaces }; + return { navigation, destinations, browsers, workspaces, focusedHosts }; } + it("focuses the destination host and clears filters that could hide its workspace", () => { + useSidebarViewStore.setState({ + groupMode: "status", + hostFilters: ["selected", "other"], + projectFilters: ["old-project"], + labelFilter: { labels: ["urgent"] }, + }); + const { navigation, destinations, focusedHosts } = setup(false); + navigation.openAgent({ serverId: "remote", agentId: "agent-2", focusHost: true }); + expect(focusedHosts).toEqual(["remote"]); + expect(destinations).toEqual([{ serverId: "remote", agentId: "agent-2" }]); + expect(useSidebarViewStore.getState()).toMatchObject({ + groupMode: "status", + hostFilters: ["remote"], + projectFilters: [], + labelFilter: { labels: [] }, + }); + }); + it("creates and focuses a local browser in the selected or explicit host workspace", () => { const { navigation, destinations, browsers } = setup(); navigation.openBrowser!({ url: "https://example.com/one", workspaceId: "one" }); @@ -43,6 +76,110 @@ describe("plugin host navigation", () => { ]); }); + it("can return from a remote workspace to the installation host without toggling it off", () => { + const { navigation, destinations, focusedHosts } = setup(); + navigation.openWorkspace({ serverId: "remote", workspaceId: "two", focusHost: true }); + navigation.openWorkspace({ workspaceId: "one", focusHost: true }); + navigation.openWorkspace({ workspaceId: "one", focusHost: true }); + expect(focusedHosts).toEqual(["remote", "selected", "selected"]); + expect(destinations).toEqual([ + { serverId: "remote", workspaceId: "two" }, + { serverId: "selected", workspaceId: "one" }, + { serverId: "selected", workspaceId: "one" }, + ]); + expect(useSidebarViewStore.getState().hostFilters).toEqual(["selected"]); + }); + + it("focuses the host in the returned route for padded host IDs", () => { + const { navigation, destinations, focusedHosts } = setup(); + navigation.openAgent({ serverId: " remote ", agentId: "agent-2", focusHost: true }); + navigation.openWorkspace({ serverId: " selected ", workspaceId: "one", focusHost: true }); + + expect(destinations).toEqual([ + { serverId: " remote ", agentId: "agent-2" }, + { serverId: " selected ", workspaceId: "one" }, + ]); + expect(focusedHosts).toEqual(["remote", "selected"]); + expect(useSidebarViewStore.getState().hostFilters).toEqual(["selected"]); + }); + + it("leaves existing sidebar filters alone unless explicitly requested", () => { + useSidebarViewStore.setState({ + hostFilters: ["remote"], + projectFilters: ["pinned-project"], + labelFilter: { labels: ["urgent"] }, + }); + const { navigation, destinations, focusedHosts } = setup(); + navigation.openAgent({ agentId: "agent-1", serverId: " remote " }); + navigation.openWorkspace({ workspaceId: "one", serverId: " selected ", focusHost: false }); + expect(destinations).toEqual([ + { serverId: " remote ", agentId: "agent-1" }, + { serverId: " selected ", workspaceId: "one" }, + ]); + expect(focusedHosts).toEqual([]); + expect(useSidebarViewStore.getState()).toMatchObject({ + hostFilters: ["remote"], + projectFilters: ["pinned-project"], + labelFilter: { labels: ["urgent"] }, + }); + expect(navigation.supportsFocusHost).toBe(true); + }); + + it("preserves sidebar filters when the navigation route has no destination", () => { + const filters = { + hostFilters: ["selected"], + projectFilters: ["pinned-project"], + labelFilter: { labels: ["urgent"] }, + }; + useSidebarViewStore.setState(filters); + const { navigation } = setup(); + navigation.openAgent({ serverId: "remote", agentId: " ", focusHost: true }); + navigation.openWorkspace({ serverId: "remote", workspaceId: "", focusHost: true }); + + expect(useSidebarViewStore.getState()).toMatchObject(filters); + }); + + it("preserves sidebar filters when the destination host has been removed", () => { + const filters = { + hostFilters: ["selected"], + projectFilters: ["pinned-project"], + labelFilter: { labels: ["urgent"] }, + }; + useSidebarViewStore.setState(filters); + const { navigation, destinations } = setup(); + navigation.openAgent({ serverId: "removed", agentId: "agent-2", focusHost: true }); + navigation.openWorkspace({ serverId: "removed", workspaceId: "two", focusHost: true }); + + expect(useSidebarViewStore.getState()).toMatchObject(filters); + expect(destinations).toEqual([ + { serverId: "removed", agentId: "agent-2" }, + { serverId: "removed", workspaceId: "two" }, + ]); + }); + + it("does not change the filter when navigation rejects the target", () => { + const focusedHosts: string[] = []; + const unavailable = () => { + throw new Error("Target unavailable"); + }; + const navigation = createPluginHostNavigation("selected", { + browserAvailable: false, + isHostRegistered: () => true, + resolveWorkspace: () => null, + openAgent: unavailable, + openWorkspace: unavailable, + createBrowser: unavailable, + focusHost: (serverId) => focusedHosts.push(serverId), + }); + expect(() => navigation.openAgent({ agentId: "missing", focusHost: true })).toThrow( + "Target unavailable", + ); + expect(() => navigation.openWorkspace({ workspaceId: "missing", focusHost: true })).toThrow( + "Target unavailable", + ); + expect(focusedHosts).toEqual([]); + }); + it("refuses unknown or removed workspaces before creating browser records", () => { const { navigation, destinations, browsers, workspaces } = setup(); expect(() => diff --git a/packages/app/src/plugins/host-navigation.ts b/packages/app/src/plugins/host-navigation.ts index 5f30f014aa..36049f4499 100644 --- a/packages/app/src/plugins/host-navigation.ts +++ b/packages/app/src/plugins/host-navigation.ts @@ -4,10 +4,12 @@ import { resolveWorkspaceMapKeyByIdentity } from "@/utils/workspace-identity"; import { useMemo } from "react"; import { navigateToWorkspace } from "@/stores/navigation-active-workspace-store"; import { navigateToAgent } from "@/utils/navigate-to-agent"; +import { getHostRuntimeStore } from "@/runtime/host-runtime"; import { getIsElectron } from "@/constants/platform"; import { createWorkspaceBrowser } from "@/desktop/browser/store"; import { createPluginHostNavigation } from "./host-navigation-model"; +import { useSidebarViewStore } from "@/stores/sidebar-view-store"; export function usePluginHostNavigation( serverId: string, @@ -16,9 +18,14 @@ export function usePluginHostNavigation( () => createPluginHostNavigation(serverId, { browserAvailable: getIsElectron(), + isHostRegistered: (targetServerId) => + getHostRuntimeStore() + .getHosts() + .some((host) => host.serverId === targetServerId), openAgent: navigateToAgent, openWorkspace: navigateToWorkspace, createBrowser: createWorkspaceBrowser, + focusHost: (targetServerId) => useSidebarViewStore.getState().focusHost(targetServerId), resolveWorkspace: ({ serverId: targetServerId, workspaceId }) => resolveWorkspaceMapKeyByIdentity({ workspaces: useSessionStore.getState().sessions[targetServerId]?.workspaces, diff --git a/packages/app/src/stores/sidebar-view-store.ts b/packages/app/src/stores/sidebar-view-store.ts index 1b53fdfe9d..2e94a0d887 100644 --- a/packages/app/src/stores/sidebar-view-store.ts +++ b/packages/app/src/stores/sidebar-view-store.ts @@ -69,6 +69,7 @@ interface SidebarViewStoreState { setWorkspaceSort: (sort: WorkspaceSort) => void; toggleHostFilter: (serverId: string) => void; clearHostFilters: () => void; + focusHost: (serverId: string) => void; toggleProjectFilter: (viewKey: string) => void; clearProjectFilters: () => void; toggleLabelFilter: (name: string) => void; @@ -205,6 +206,9 @@ export const useSidebarViewStore = create()( toggleHostFilter: (serverId) => set((state) => ({ hostFilters: toggleFilterEntry(state.hostFilters, serverId) })), clearHostFilters: () => set({ hostFilters: [] }), + // An explicit navigation target must not be hidden by another host's facets. + focusHost: (serverId) => + set({ hostFilters: [serverId], projectFilters: [], labelFilter: emptyLabelFilter() }), toggleProjectFilter: (viewKey) => set((state) => ({ projectFilters: toggleFilterEntry(state.projectFilters, viewKey) })), clearProjectFilters: () => set({ projectFilters: [] }), diff --git a/packages/plugin/src/client/contracts.ts b/packages/plugin/src/client/contracts.ts index 72dc1f954d..13aaa9b71f 100644 --- a/packages/plugin/src/client/contracts.ts +++ b/packages/plugin/src/client/contracts.ts @@ -49,16 +49,25 @@ export interface PluginHostProps { interface PluginNavigableHostProps extends PluginHostProps { /** Client-owned navigation. Undefined on older hosts; hide dependent affordances when absent. */ readonly navigation?: { + /** This client supports the focusHost option on agent/workspace navigation. */ + readonly supportsFocusHost?: boolean; /** Present only on Electron. The browser runs locally; serverId selects workspace ownership. */ readonly openBrowser?: (input: { readonly url: string; readonly workspaceId: string; readonly serverId?: string; }) => void; - readonly openAgent: (input: { readonly agentId: string; readonly serverId?: string }) => void; + readonly openAgent: (input: { + readonly agentId: string; + readonly serverId?: string; + /** Select only the destination host in the sidebar and clear project/label filters. */ + readonly focusHost?: boolean; + }) => void; readonly openWorkspace: (input: { readonly workspaceId: string; readonly serverId?: string; + /** Select only the destination host in the sidebar and clear project/label filters. */ + readonly focusHost?: boolean; }) => void; }; } diff --git a/public-docs/plugins/reference.md b/public-docs/plugins/reference.md index d95cd0ac7b..32a8932eb0 100644 --- a/public-docs/plugins/reference.md +++ b/public-docs/plugins/reference.md @@ -789,6 +789,16 @@ export default function contribute(client: PluginClientContext) { | `params` | The params the screen was opened with, as string keys and values. `{}` when none. | | `navigation` | Optional client navigation. `openAgent({ agentId, serverId? })` and `openWorkspace({ workspaceId, serverId? })` open targets on `serverId`, or on the selected host when omitted. `openBrowser({ url, workspaceId, serverId? })` is available only on Electron; see [links and browsers](#external-links-and-workspace-browsers). | +If `navigation?.supportsFocusHost` is true, pass `focusHost: true` to `openAgent` or `openWorkspace`. +Paseo selects only the destination host in the workspace sidebar, clears the project and label +filters, and keeps the grouping mode. Omit the option to preserve the existing filters. + +```ts +if (navigation?.supportsFocusHost) { + navigation.openAgent({ agentId, serverId, focusHost: true }); +} +``` + Paseo owns the route, header, close action, host picker, error boundary, and query client. The plugin owns the screen body. `addScreen({ id, title, Component })` registers a screen. `title` is the screen header's title: a