diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 0b0116be549cb..f4323951ab380 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -150,6 +150,8 @@ import { import { isMarkdownFileLinkLabel } from "@t3tools/shared/markdownLinks"; import { isElectron } from "../env"; import { readLocalApi } from "../localApi"; +import { appAtomRegistry } from "../rpc/atomRegistry"; +import { hostEnvironmentIdAtom } from "../state/primaryEnvironment"; import { cn } from "../lib/utils"; import { resolveRemoteOpenState, useRemoteOpenResolution } from "../remoteOpen"; import { useRightPanelStore } from "../rightPanelStore"; @@ -610,6 +612,7 @@ function useChatMarkdownState({ sshTarget: null, remoteOpenTargets: undefined, isDesktopRenderer: window.desktopBridge !== undefined, + hostEnvironmentId: appAtomRegistry.get(hostEnvironmentIdAtom), }).mode !== "local-exec" ) { description = `The file was not found on ${target.label} at ${error.checkedPath ?? error.resource.path}.`; diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 7de02cc3941ad..c1f68878e9644 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -1,3 +1,4 @@ +import { shouldShowOpenInPicker } from "./chat/OpenInPicker.logic"; import { boundAttachmentIds, buildExpiredTerminalContextToastCopy, @@ -1216,3 +1217,25 @@ describe("deriveAvailableRevertTurnCounts", () => { expect(deriveAvailableRevertTurnCounts(null).size).toBe(0); }); }); + +describe("Open in for the host environment", () => { + it("shows both local environments when the primary has a separate WSL home", () => { + const primaryEnvironmentId = EnvironmentId.make("wsl"); + const hostEnvironmentId = EnvironmentId.make("host"); + for (const [activeThreadEnvironmentId, visible] of [ + [primaryEnvironmentId, true], + [hostEnvironmentId, true], + [EnvironmentId.make("other"), false], + ] as const) { + expect( + shouldShowOpenInPicker({ + activeProjectName: "Project", + activeThreadEnvironmentId, + primaryEnvironmentId, + hostEnvironmentId, + remoteOpenMode: "local-exec", + }), + ).toBe(visible); + } + }); +}); diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index f4561188df0cc..42df913652151 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -232,11 +232,8 @@ import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../termina import { useKnownTerminalSessions, useThreadRunningTerminalIds } from "../state/terminalSessions"; import { useEnvironmentQueryRefresh } from "../state/query"; import { useEnvironmentScope } from "~/state/session"; -import { - primaryServerAvailableEditorsAtom, - primaryServerKeybindingsAtom, - serverEnvironment, -} from "../state/server"; +import { primaryServerKeybindingsAtom, serverEnvironment } from "../state/server"; +import { hostEnvironmentIdAtom } from "../state/primaryEnvironment"; import { threadEnvironment } from "../state/threads"; import { resolveProviderSkillsForCwd } from "@t3tools/client-runtime/providerSkills"; import { chatGitStatusAtom, NO_CHAT_GIT_STATUS_ATOM, vcsEnvironment } from "../state/vcs"; @@ -1172,7 +1169,6 @@ export default function ChatView(props: ChatViewProps) { useRightPanelStore.getState().reconcileFileSurfaces(activeThreadRef, activeProject !== null); }, [activeEnvironmentBootstrapComplete, activeProject, activeThreadRef]); - const primaryEnvironmentId = primaryEnvironment?.environmentId ?? null; const activeEnvironment = activeThread == null ? null : (environmentById.get(activeThread.environmentId) ?? null); const activeEnvironmentUnavailable = @@ -1627,17 +1623,21 @@ export default function ChatView(props: ChatViewProps) { const scriptKeybindings = useAtomValue(serverEnvironment.configValueAtom(environmentId))?.keybindings ?? DEFAULT_RESOLVED_KEYBINDINGS; - const availableEditors = useAtomValue(primaryServerAvailableEditorsAtom); - const remoteOpenState = useRemoteOpenState(activeThread?.environmentId ?? environmentId); + const openInEnvironmentId = activeThread?.environmentId ?? environmentId; + const availableEditors = + useAtomValue(serverEnvironment.configValueAtom(openInEnvironmentId))?.availableEditors ?? []; + const hostEnvironmentId = useAtomValue(hostEnvironmentIdAtom); + const remoteOpenState = useRemoteOpenState(openInEnvironmentId); const showOpenInPicker = shouldShowOpenInPicker({ activeProjectName: activeProject?.title, - activeThreadEnvironmentId: activeThread?.environmentId ?? environmentId, - primaryEnvironmentId, + activeThreadEnvironmentId: openInEnvironmentId, + primaryEnvironmentId: primaryEnvironment?.environmentId ?? null, + hostEnvironmentId, remoteOpenMode: remoteOpenState.mode, }); useOpenFavoriteEditorShortcut({ enabled: showOpenInPicker && !backgroundThread, - environmentId: activeThread?.environmentId ?? environmentId, + environmentId: openInEnvironmentId, keybindings, availableEditors, openInCwd: gitCwd, diff --git a/apps/web/src/components/chat/OpenInPicker.logic.test.ts b/apps/web/src/components/chat/OpenInPicker.logic.test.ts new file mode 100644 index 0000000000000..10595e2f8cdbe --- /dev/null +++ b/apps/web/src/components/chat/OpenInPicker.logic.test.ts @@ -0,0 +1,26 @@ +import { EnvironmentId } from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { shouldShowOpenInPicker } from "./OpenInPicker.logic"; + +describe("shouldShowOpenInPicker", () => { + it("shows both local environments when the primary has a separate WSL home", () => { + const primaryEnvironmentId = EnvironmentId.make("wsl"); + const hostEnvironmentId = EnvironmentId.make("host"); + for (const [activeThreadEnvironmentId, visible] of [ + [primaryEnvironmentId, true], + [hostEnvironmentId, true], + [EnvironmentId.make("other"), false], + ] as const) { + expect( + shouldShowOpenInPicker({ + activeProjectName: "Project", + activeThreadEnvironmentId, + primaryEnvironmentId, + hostEnvironmentId, + remoteOpenMode: "local-exec", + }), + ).toBe(visible); + } + }); +}); diff --git a/apps/web/src/components/chat/OpenInPicker.logic.ts b/apps/web/src/components/chat/OpenInPicker.logic.ts index f5c9e17c392c5..5978a5986030a 100644 --- a/apps/web/src/components/chat/OpenInPicker.logic.ts +++ b/apps/web/src/components/chat/OpenInPicker.logic.ts @@ -6,12 +6,13 @@ export function shouldShowOpenInPicker(input: { readonly activeProjectName: string | undefined; readonly activeThreadEnvironmentId: EnvironmentId; readonly primaryEnvironmentId: EnvironmentId | null; + readonly hostEnvironmentId: string | null; readonly remoteOpenMode: RemoteOpenMode; }): boolean { if (!input.activeProjectName) return false; if ( - input.primaryEnvironmentId !== null && - input.activeThreadEnvironmentId === input.primaryEnvironmentId + input.activeThreadEnvironmentId === input.primaryEnvironmentId || + input.activeThreadEnvironmentId === input.hostEnvironmentId ) { return true; } diff --git a/apps/web/src/components/files/FilePreviewPanel.tsx b/apps/web/src/components/files/FilePreviewPanel.tsx index 1455fc849a7f1..0311ce254ff4f 100644 --- a/apps/web/src/components/files/FilePreviewPanel.tsx +++ b/apps/web/src/components/files/FilePreviewPanel.tsx @@ -71,6 +71,7 @@ import { buildFileReviewComment } from "~/reviewCommentContext"; import { assetEnvironment } from "~/state/assets"; import { usePreviewAvailable } from "~/browser/previewRuntime"; import { useEnvironmentHttpBaseUrl, usePrimaryEnvironmentId } from "~/state/environments"; +import { hostEnvironmentIdAtom } from "~/state/primaryEnvironment"; import { previewEnvironment } from "~/state/preview"; import { serverEnvironment } from "~/state/server"; import { useEnvironmentScope } from "~/state/session"; @@ -1025,6 +1026,7 @@ export default function FilePreviewPanel({ const wordWrap = wordWrapOverride ?? defaultWordWrap; const canOperatePreview = useEnvironmentScope(environmentId, AuthPreviewOperateScope); const primaryEnvironmentId = usePrimaryEnvironmentId(); + const hostEnvironmentId = useAtomValue(hostEnvironmentIdAtom); const remoteOpenState = useRemoteOpenState(environmentId); const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId)); const environmentHttpBaseUrl = useEnvironmentHttpBaseUrl(environmentId); @@ -1281,7 +1283,9 @@ export default function FilePreviewPanel({ {absolutePath && - (environmentId === primaryEnvironmentId || remoteOpenState.mode !== "local-exec") ? ( + (environmentId === primaryEnvironmentId || + environmentId === hostEnvironmentId || + remoteOpenState.mode !== "local-exec") ? ( { sshTarget: null, isDesktopRenderer: false, remoteOpenTargets: TAILSCALE_TARGETS, + hostEnvironmentId: null, }), ).toEqual({ mode: "local-exec" }); }); @@ -42,6 +43,7 @@ describe("resolveRemoteOpenState", () => { sshTarget: null, isDesktopRenderer: false, remoteOpenTargets: TAILSCALE_TARGETS, + hostEnvironmentId: null, }), ).toEqual({ mode: "remote-links", @@ -57,10 +59,40 @@ describe("resolveRemoteOpenState", () => { sshTarget: null, isDesktopRenderer: true, remoteOpenTargets: TAILSCALE_TARGETS, + hostEnvironmentId: null, }), ).toEqual({ mode: "local-exec" }); }); + it("keeps exec behavior for the environment that owns the desktop's T3 home", () => { + // With Local environment off, a background service on this machine can own the + // desktop's home and still be reached through T3 Connect. + expect( + resolveRemoteOpenState({ + target: new RelayConnectionTarget({ environmentId, label: "sol" }), + sshTarget: null, + isDesktopRenderer: true, + remoteOpenTargets: TAILSCALE_TARGETS, + hostEnvironmentId: environmentId, + }), + ).toEqual({ mode: "local-exec" }); + }); + + it("uses deep links for other environments when the desktop home has its own", () => { + expect( + resolveRemoteOpenState({ + target: new RelayConnectionTarget({ environmentId, label: "sol" }), + sshTarget: null, + isDesktopRenderer: true, + remoteOpenTargets: TAILSCALE_TARGETS, + hostEnvironmentId: EnvironmentId.make("environment-host"), + }), + ).toEqual({ + mode: "remote-links", + host: { kind: "tailscale", host: "sol.tail1234.ts.net" }, + }); + }); + it("leaves a bare desktop SSH alias for the editor to resolve through SSH config", () => { const state = resolveRemoteOpenState({ target: new SshConnectionTarget({ environmentId, label: "sol", connectionId: "ssh-1" }), @@ -69,6 +101,7 @@ describe("resolveRemoteOpenState", () => { remoteOpenTargets: [ { kind: "tailscale", host: "sol.tail1234.ts.net", username: "server-user" }, ], + hostEnvironmentId: null, }); expect(state).toEqual({ mode: "remote-links", host: { kind: "ssh-alias", host: "sol" } }); if (state.mode !== "remote-links") throw new Error("Expected a remote link"); @@ -97,6 +130,7 @@ describe("resolveRemoteOpenState", () => { sshTarget, isDesktopRenderer: true, remoteOpenTargets: TAILSCALE_TARGETS, + hostEnvironmentId: null, }); expect(state.mode).toBe("remote-links"); if (state.mode !== "remote-links") throw new Error("Expected a remote link"); @@ -121,6 +155,7 @@ describe("resolveRemoteOpenState", () => { remoteOpenTargets: [ { kind: "tailscale", host: "server.tail1234.ts.net", username: "admin-user" }, ], + hostEnvironmentId: null, }); expect(state.mode).toBe("remote-links"); if (state.mode !== "remote-links") throw new Error("Expected a remote link"); @@ -137,6 +172,7 @@ describe("resolveRemoteOpenState", () => { sshTarget: null, isDesktopRenderer: false, remoteOpenTargets, + hostEnvironmentId: null, }), ).toEqual({ mode: "remote-unavailable" }); } diff --git a/apps/web/src/remoteOpen.ts b/apps/web/src/remoteOpen.ts index 741f6f3c60cde..5624169abd696 100644 --- a/apps/web/src/remoteOpen.ts +++ b/apps/web/src/remoteOpen.ts @@ -14,6 +14,7 @@ import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal"; import { isLoopbackHostname } from "~/environments/primary/target"; import { useLocalStorage } from "~/hooks/useLocalStorage"; import { environmentPresentations } from "~/state/presentation"; +import { hostEnvironmentIdAtom } from "~/state/primaryEnvironment"; export interface RemoteOpenHost { readonly kind: "ssh-alias" | RemoteOpenTarget["kind"]; @@ -53,9 +54,10 @@ export function resolveRemoteOpenState(input: { readonly sshTarget: { readonly alias: string; readonly username: string | null } | null; readonly remoteOpenTargets: ReadonlyArray | undefined; readonly isDesktopRenderer: boolean; + readonly hostEnvironmentId: string | null; }): RemoteOpenState { const { target } = input; - if (target === null) { + if (target === null || target.environmentId === input.hostEnvironmentId) { return LOCAL_EXEC; } if (target._tag === "PrimaryConnectionTarget") { @@ -120,6 +122,7 @@ const remoteOpenResolutionAtom = Atom.family((environmentId: EnvironmentId) => profile !== null && profile._tag === "SshConnectionProfile" ? profile.target : null, remoteOpenTargets: presentation?.serverConfig?.remoteOpenTargets, isDesktopRenderer: window.desktopBridge !== undefined, + hostEnvironmentId: get(hostEnvironmentIdAtom), }), isResolved: true, }; diff --git a/apps/web/src/state/server.ts b/apps/web/src/state/server.ts index 8be2ccd51f03e..b759f456e3208 100644 --- a/apps/web/src/state/server.ts +++ b/apps/web/src/state/server.ts @@ -1,6 +1,5 @@ import { DEFAULT_SERVER_SETTINGS, - type EditorId, type EnvironmentId, type EnvironmentTheme, type ServerConfig, @@ -53,7 +52,6 @@ interface PrimaryServerState { readonly welcome: ServerLifecycleWelcomePayload | null; } -const EMPTY_AVAILABLE_EDITORS: ReadonlyArray = []; export const EMPTY_SERVER_PROVIDERS: ReadonlyArray = []; const EMPTY_PRIMARY_SERVER_STATE: PrimaryServerState = { config: null, @@ -166,11 +164,6 @@ export const primaryServerKeybindingsAtom = createShortcutKeybindingsAtom({ environmentsAtom: environmentConnectionsAtom, }).pipe(Atom.withLabel("web-primary-server-keybindings")); -export const primaryServerAvailableEditorsAtom = Atom.make( - (get): ReadonlyArray => - get(primaryServerConfigAtom)?.availableEditors ?? EMPTY_AVAILABLE_EDITORS, -).pipe(Atom.withLabel("web-primary-server-available-editors")); - const EMPTY_ENVIRONMENT_THEMES: ReadonlyArray = []; const themeServerConfigAtom = Atom.make((get): ServerConfig | null => { diff --git a/docs/user/remote-access.md b/docs/user/remote-access.md index a510ff0008c55..9635a8af2a0a7 100644 --- a/docs/user/remote-access.md +++ b/docs/user/remote-access.md @@ -465,6 +465,9 @@ desktop app, open **Settings → Environments** and switch off **Local environment**. T3 Code restarts without a local server: no local agents or terminals run, WSL backends stay off, and other devices can no longer connect to this computer. Your projects, history, and saved connections are kept, and you keep working through pairing, T3 Connect, or SSH. +If a [background service](./background-service.md) on this computer uses the same T3 home, the +app still treats it as this computer: **Open in** and file reveal for its projects run here, +whichever connection you reach it through. Switch **Local environment** back on in the same place to restart with your previous local settings.