feat(desktop): restore new-session deep link and add open-session

This commit is contained in:
jlongster 2026-10-20 01:39:10 +00:00
parent 6e9c85390b
commit c344adedcb
5 changed files with 111 additions and 13 deletions

View file

@ -32,7 +32,8 @@ const runtime = Layer.effect(
const windows = yield* makeMainWindows()
const createWindow = windows.create
const restoreWindows = windows.restore
const pendingDeepLinks: string[] = []
// Windows and Linux pass the link that launched the app as an argument; later ones arrive via second-instance.
const pendingDeepLinks = process.argv.filter((arg) => arg.startsWith("opencode://"))
let shutdownReady = false
const prepareToRestart = shutdown.run.pipe(Effect.ensuring(Effect.sync(() => (shutdownReady = true))))

View file

@ -12,17 +12,20 @@ import {
useCurrentRoute,
useLanguage,
useExtensionServers,
useGlobal,
useServers,
useTabs,
type LayoutRoute,
} from "@opencode/app/desktop"
import { useTheme } from "@opencode/ui/theme/context"
import type { BaseRouterProps } from "@solidjs/router"
import { createEffect, createMemo, createResource, lazy, Show, Suspense } from "solid-js"
import { createEffect, createMemo, createResource, lazy, onCleanup, Show, Suspense } from "solid-js"
import { createStore } from "solid-js/store"
import type { ElectronAPI } from "./api-types"
import { DesktopFirstLaunchOnboarding } from "./onboarding"
import { createDesktopPlatform } from "./platform"
import { bindDesktopMenu } from "./platform/menu"
import { bindDeepLinks } from "./startup/deep-links"
import { createSidecarResolver, initializationData, sidecarHttp } from "./startup/initialization"
import { preloadStoredLocale } from "./startup/locale"
import { LoadingSplash } from "./startup/splash"
@ -122,6 +125,7 @@ export function DesktopApp(props: { api: ElectronAPI; version: string }) {
onReady={() => setStartup("onboardingReady", true)}
/>
<DesktopEffects api={props.api} />
<DesktopDeepLinks />
<Suspense fallback={null}>
<Show when={initializationData(sidecar)} keyed>
{(server) => <MigrationStatus server={server} />}
@ -198,3 +202,36 @@ function DesktopEffects(props: { api: ElectronAPI }) {
return null
}
// Deep links open on the local server once the window's tabs have loaded, so a new draft is not lost to hydration.
function DesktopDeepLinks() {
const servers = useServers()
const global = useGlobal()
const tabs = useTabs()
createEffect(() => {
const conn = servers.list.find(ServerConnection.builtin)
if (!conn || !tabs.ready()) return
const server = ServerConnection.key(conn)
bindDeepLinks((link) => {
if (link.type === "open-session") {
const tab = tabs.addSessionTab({ server, sessionId: link.sessionID })
if (tab.type !== "session") return
tabs.rememberSessionRoute(tab, link.sessionID)
tabs.select(tab)
return
}
const projects = global.ensureServerCtx(conn).projects
projects.open(link.directory)
projects.touch(link.directory)
void tabs.newDraft({ server, directory: link.directory }, link.prompt)
})
onCleanup(() => bindDeepLinks(undefined))
})
return null
}

View file

@ -7,8 +7,5 @@ declare global {
interface Window {
electron: ElectronNative
__OPENCODE__?: {
deepLinks?: string[]
}
}
}

View file

@ -0,0 +1,29 @@
import { describe, expect, test } from "bun:test"
import { parseDeepLink } from "./deep-links"
describe("parseDeepLink", () => {
test.each([
["opencode://new-session?directory=/tmp/demo", { type: "new-session", directory: "/tmp/demo", prompt: undefined }],
[
"opencode://new-session?directory=/tmp/demo&prompt=hello%20world",
{ type: "new-session", directory: "/tmp/demo", prompt: "hello world" },
],
["opencode://new-session?directory=C%3A%5Crepo", { type: "new-session", directory: "C:\\repo", prompt: undefined }],
["opencode://open-session?id=ses_123", { type: "open-session", sessionID: "ses_123" }],
])("opens %s", (input, expected) => {
expect(parseDeepLink(input)).toEqual(expected)
})
test.each([
"opencode://new-session",
"opencode://new-session?directory=",
"opencode://open-session",
"opencode://open-session?id=",
"opencode://open-project?directory=/tmp/demo",
"opencode://console/authorized?window=a",
"https://new-session?directory=/tmp/demo",
"not a url",
])("ignores %s", (input) => {
expect(parseDeepLink(input)).toBeUndefined()
})
})

View file

@ -1,15 +1,49 @@
import type { ElectronAPI } from "../api-types"
const deepLinkEvent = "opencode:deep-link"
export type DeepLink =
| { type: "new-session"; directory: string; prompt?: string }
| { type: "open-session"; sessionID: string }
const pending: DeepLink[] = []
let open: ((link: DeepLink) => void) | undefined
export function startDeepLinks(api: ElectronAPI) {
void api.consumeInitialDeepLinks().then(emitDeepLinks)
api.onDeepLink(emitDeepLinks)
void api.consumeInitialDeepLinks().then(receive)
api.onDeepLink(receive)
}
function emitDeepLinks(urls: string[]) {
if (urls.length === 0) return
window.__OPENCODE__ ??= {}
window.__OPENCODE__.deepLinks = [...(window.__OPENCODE__.deepLinks ?? []), ...urls]
window.dispatchEvent(new CustomEvent(deepLinkEvent, { detail: { urls } }))
// Links can arrive before the app can open them, such as the one that launched it; they wait here until it binds.
export function bindDeepLinks(next: ((link: DeepLink) => void) | undefined) {
open = next
if (next) pending.splice(0).forEach(next)
}
function receive(urls: string[]) {
urls.flatMap((url) => parseDeepLink(url) ?? []).forEach((link) => (open ? open(link) : pending.push(link)))
}
// `opencode://new-session?directory=<path>&prompt=<text>` and `opencode://open-session?id=<session id>`.
export function parseDeepLink(input: string): DeepLink | undefined {
if (!URL.canParse(input)) return
const url = new URL(input)
if (url.protocol !== "opencode:") return
if (url.hostname === "new-session") {
const directory = url.searchParams.get("directory")
if (!directory) return
return { type: "new-session", directory, prompt: url.searchParams.get("prompt") || undefined }
}
if (url.hostname === "open-session") {
const sessionID = url.searchParams.get("id")
if (!sessionID) return
return { type: "open-session", sessionID }
}
}