Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(bridge): web folder/file picker (server-directory browser) for dialog.select*

enes7fa0beca6f5e
diff --git a/src/renderer/bridge/dialog-picker.test.ts b/src/renderer/bridge/dialog-picker.test.ts
new file mode 100644
index 000000000..8f846f4ee
--- /dev/null
+++ b/src/renderer/bridge/dialog-picker.test.ts
@@ -0,0 +1,49 @@
+import { describe, it, expect, vi } from 'vitest'
+import { nextEntries } from './dialog-picker'
+import type { DirEntry } from '../../shared/types'
+
+// The real `DirEntry` (src/shared/types.ts) is `{ name, dir, ignored? }` — it uses `dir`
+// (not `isDirectory`) and carries NO absolute path. So `nextEntries` filters on `.dir` and
+// synthesizes each row's absolute path from the current dir (`currentDir + '/' + name`).
+
+describe('dialog-picker navigation helper', () => {
+ const list = vi.fn(
+ async (p: string): Promise<DirEntry[]> =>
+ p === '/home/u'
+ ? [
+ { name: 'proj', dir: true },
+ { name: 'note.txt', dir: false }
+ ]
+ : []
+ )
+
+ it('lists dirs and (file mode) files, with a parent when not at root', async () => {
+ const folderView = await nextEntries('/home/u', 'folder', list)
+ expect(folderView.parent).toBe('/home')
+ expect(folderView.rows.map((r) => r.name)).toEqual(['proj']) // folder mode hides files
+
+ const fileView = await nextEntries('/home/u', 'file', list)
+ expect(fileView.rows.map((r) => r.name)).toEqual(['proj', 'note.txt'])
+ })
+
+ it('resolves each row to an absolute path under the current dir', async () => {
+ const view = await nextEntries('/home/u', 'file', list)
+ expect(view.rows.map((r) => r.path)).toEqual(['/home/u/proj', '/home/u/note.txt'])
+ })
+
+ it('joins correctly at the filesystem root (no double slash)', async () => {
+ const rootList = vi.fn(async (): Promise<DirEntry[]> => [{ name: 'etc', dir: true }])
+ const view = await nextEntries('/', 'folder', rootList)
+ expect(view.rows.map((r) => r.path)).toEqual(['/etc'])
+ })
+
+ it('parent is null at filesystem root', async () => {
+ const view = await nextEntries('/', 'folder', vi.fn(async () => []))
+ expect(view.parent).toBeNull()
+ })
+
+ it('normalizes a trailing slash when computing the parent', async () => {
+ const view = await nextEntries('/home/u/', 'folder', vi.fn(async () => []))
+ expect(view.parent).toBe('/home')
+ })
+})
diff --git a/src/renderer/bridge/dialog-picker.tsx b/src/renderer/bridge/dialog-picker.tsx
new file mode 100644
index 000000000..781b19e42
--- /dev/null
+++ b/src/renderer/bridge/dialog-picker.tsx
@@ -0,0 +1,201 @@
+// Web replacement for the Electron native folder/file dialog (Server Edition).
+//
+// The desktop build opens a native OS picker via the preload's `dialog` namespace; the browser
+// build has no such thing. Instead we browse the *server's* filesystem in-app over `fs.list`
+// (Task 4) and let the user drill into directories, then either "Use this folder" (folder mode)
+// or click a file (file mode). The chosen ABSOLUTE path becomes a project cwd / opened file.
+//
+// `nextEntries` is the pure navigation core (unit-tested without the DOM). The modal, the
+// `openDirectoryPicker` promise wrapper, and `mountPickerRoot` wire it into `ws-bridge`.
+
+import { useCallback, useEffect, useState } from 'react'
+import { createRoot, type Root } from 'react-dom/client'
+import type { DirEntry } from '../../shared/types'
+
+export type PickerMode = 'folder' | 'file'
+
+/** A directory entry plus its resolved absolute path (the real `DirEntry` carries only a name). */
+export type PickerRow = DirEntry & { path: string }
+
+type ListFn = (dirPath: string) => Promise<DirEntry[]>
+
+/** Strip a trailing slash from a path (but keep the root `/` as-is). */
+function stripTrailingSlash(dir: string): string {
+ return dir.length > 1 ? dir.replace(/\/+$/, '') : dir
+}
+
+/** Join an absolute dir and a child name into an absolute path (no double slash at the root). */
+function joinPath(dir: string, name: string): string {
+ const base = stripTrailingSlash(dir)
+ return base === '/' ? `/${name}` : `${base}/${name}`
+}
+
+/**
+ * Pure navigation step: list `dir`, keep subdirectories (both modes) and — in `file` mode —
+ * files too, resolve each row's absolute path, and compute the parent dir (`null` at the
+ * filesystem root `/`). The modal calls this on every navigation.
+ */
+export async function nextEntries(
+ dir: string,
+ mode: PickerMode,
+ list: ListFn
+): Promise<{ parent: string | null; rows: PickerRow[] }> {
+ const current = stripTrailingSlash(dir)
+ const entries = await list(dir)
+ const visible = mode === 'folder' ? entries.filter((e) => e.dir) : entries
+ const rows: PickerRow[] = visible.map((e) => ({ ...e, path: joinPath(current, e.name) }))
+
+ let parent: string | null = null
+ if (current !== '/') {
+ const cut = current.lastIndexOf('/')
+ parent = cut <= 0 ? '/' : current.slice(0, cut)
+ }
+ return { parent, rows }
+}
+
+interface PickerProps {
+ mode: PickerMode
+ startDir: string
+ list: ListFn
+ onDone: (result: string | null) => void
+}
+
+function DirectoryPicker({ mode, startDir, list, onDone }: PickerProps): React.ReactElement {
+ const [dir, setDir] = useState(startDir)
+ const [rows, setRows] = useState<PickerRow[]>([])
+ const [parent, setParent] = useState<string | null>(null)
+ const [error, setError] = useState<string | null>(null)
+ const [loading, setLoading] = useState(true)
+
+ useEffect(() => {
+ let cancelled = false
+ setLoading(true)
+ setError(null)
+ nextEntries(dir, mode, list)
+ .then((view) => {
+ if (cancelled) return
+ setRows(view.rows)
+ setParent(view.parent)
+ })
+ .catch(() => {
+ if (!cancelled) setError('Could not read this folder')
+ })
+ .finally(() => {
+ if (!cancelled) setLoading(false)
+ })
+ return () => {
+ cancelled = true
+ }
+ }, [dir, mode, list])
+
+ useEffect(() => {
+ const onKey = (e: KeyboardEvent): void => {
+ if (e.key === 'Escape') {
+ e.preventDefault()
+ onDone(null)
+ }
+ }
+ window.addEventListener('keydown', onKey)
+ return () => window.removeEventListener('keydown', onKey)
+ }, [onDone])
+
+ const openRow = useCallback(
+ (row: PickerRow) => {
+ if (row.dir) setDir(row.path)
+ else if (mode === 'file') onDone(row.path)
+ },
+ [mode, onDone]
+ )
+
+ return (
+ <div className="confirm-overlay" onClick={() => onDone(null)}>
+ <div
+ className="confirm dir-picker"
+ onClick={(e) => e.stopPropagation()}
+ role="dialog"
+ aria-label={mode === 'folder' ? 'Choose a folder' : 'Choose a file'}
+ >
+ <div className="dir-picker__head">
+ <button
+ className="dir-picker__up"
+ onClick={() => parent && setDir(parent)}
+ disabled={parent === null}
+ title="Up one level"
+ >
+ ↑
+ </button>
+ <span className="dir-picker__path" title={dir}>
+ {dir}
+ </span>
+ </div>
+
+ <div className="dir-picker__list">
+ {loading && <div className="dir-picker__empty">Loading…</div>}
+ {!loading && error && <div className="dir-picker__empty">{error}</div>}
+ {!loading && !error && rows.length === 0 && (
+ <div className="dir-picker__empty">
+ {mode === 'folder' ? 'No subfolders here' : 'Empty folder'}
+ </div>
+ )}
+ {!loading &&
+ !error &&
+ rows.map((row) => (
+ <button
+ key={row.path}
+ className={`dir-picker__row${row.dir ? ' is-dir' : ''}`}
+ onClick={() => openRow(row)}
+ >
+ <span className="dir-picker__icon">{row.dir ? '📁' : '📄'}</span>
+ <span className="dir-picker__name">{row.name}</span>
+ </button>
+ ))}
+ </div>
+
+ <div className="confirm__actions">
+ <button className="confirm__btn" onClick={() => onDone(null)}>
+ Cancel
+ </button>
+ {mode === 'folder' && (
+ <button className="confirm__btn primary" onClick={() => onDone(stripTrailingSlash(dir))}>
+ Use this folder
+ </button>
+ )}
+ </div>
+ </div>
+ </div>
+ )
+}
+
+// ── Mount plumbing ──────────────────────────────────────────────────────────────────────────
+let pickerRoot: Root | null = null
+
+/** Create the React root container the picker renders into. Idempotent; call once from the bridge. */
+export function mountPickerRoot(): void {
+ if (pickerRoot || typeof document === 'undefined') return
+ const container = document.createElement('div')
+ container.id = 'nt-dialog-picker-root'
+ document.body.appendChild(container)
+ pickerRoot = createRoot(container)
+}
+
+/**
+ * Open the in-app server-directory browser and resolve with the chosen ABSOLUTE path
+ * (folder mode → the current dir; file mode → the clicked file) or `null` on cancel/close.
+ * Never rejects — cancel resolves `null`, mirroring the native dialog's contract.
+ */
+export function openDirectoryPicker(opts: {
+ mode: PickerMode
+ startDir: string
+ list: ListFn
+}): Promise<string | null> {
+ if (!pickerRoot) mountPickerRoot()
+ return new Promise<string | null>((resolve) => {
+ const finish = (result: string | null): void => {
+ pickerRoot?.render(null)
+ resolve(result)
+ }
+ pickerRoot?.render(
+ <DirectoryPicker mode={opts.mode} startDir={opts.startDir} list={opts.list} onDone={finish} />
+ )
+ })
+}
diff --git a/src/renderer/bridge/stubs.ts b/src/renderer/bridge/stubs.ts
index 284aac969..56d2685ce 100644
--- a/src/renderer/bridge/stubs.ts
+++ b/src/renderer/bridge/stubs.ts
@@ -1,16 +1,16 @@
// Graceful non-terminal stub surface for the browser (Server Edition) build.
//
-// The real namespaces (`pty`, `workspace`, `settings`, and the fs/git/files/context group from
-// `buildFilesApi`) are provided by the ws-bridge; everything else here degrades benignly so the
-// renderer boots without a full Electron preload. The exact per-member behavior is the boot-path
-// contract encoded in the Task 7 brief (derived from a full renderer-boot audit): every `on*`
-// subscription MUST return a callable no-op unsubscribe (the renderer uses the return value as a
-// React effect cleanup — a missing member or a non-function return is a mount crash), promise
-// members that the boot path awaits resolve to a benign value, and everything else rejects with a
-// coded error.
+// The real namespaces (`pty`, `workspace`, `settings`, the fs/git/files/context group from
+// `buildFilesApi`, and `dialog` from the in-app `dialog-picker`) are provided by the ws-bridge;
+// everything else here degrades benignly so the renderer boots without a full Electron preload.
+// The exact per-member behavior is the boot-path contract encoded in the Task 7 brief (derived
+// from a full renderer-boot audit): every `on*` subscription MUST return a callable no-op
+// unsubscribe (the renderer uses the return value as a React effect cleanup — a missing member or
+// a non-function return is a mount crash), promise members that the boot path awaits resolve to a
+// benign value, and everything else rejects with a coded error.
//
// The object is `satisfies Omit<NodeTerminalApi, 'pty' | 'workspace' | 'settings' | 'fs' | 'git'
-// | 'files' | 'context'>`, so the TypeScript compiler is the completeness test: if
+// | 'files' | 'context' | 'dialog'>`, so the TypeScript compiler is the completeness test: if
// `NodeTerminalApi` gains a member, this file fails to typecheck until the stub is declared.
import type { ClaudeUsage, NodeTerminalApi, NotifyPayload, UpdatePolicy } from '../../shared/types'
@@ -39,14 +39,9 @@ const pnoop = (): Promise<void> => Promise.resolve()
export function buildStubApi(): Omit<
NodeTerminalApi,
- 'pty' | 'workspace' | 'settings' | 'fs' | 'git' | 'files' | 'context'
+ 'pty' | 'workspace' | 'settings' | 'fs' | 'git' | 'files' | 'context' | 'dialog'
> {
const api = {
- dialog: {
- // Web folder/file pickers are Phase 3.
- selectFolder: U('dialog.selectFolder'),
- selectFile: U('dialog.selectFile')
- },
ssh: {
list: U('ssh.list'),
save: U('ssh.save'),
@@ -208,7 +203,7 @@ export function buildStubApi(): Omit<
sendAgentControlResult: noop
} satisfies Omit<
NodeTerminalApi,
- 'pty' | 'workspace' | 'settings' | 'fs' | 'git' | 'files' | 'context'
+ 'pty' | 'workspace' | 'settings' | 'fs' | 'git' | 'files' | 'context' | 'dialog'
>
return api
diff --git a/src/renderer/bridge/ws-bridge.ts b/src/renderer/bridge/ws-bridge.ts
index ca2221bf5..03aac6317 100644
--- a/src/renderer/bridge/ws-bridge.ts
+++ b/src/renderer/bridge/ws-bridge.ts
@@ -22,6 +22,7 @@ import type {
WorkspaceApi
} from '../../shared/types'
import { buildStubApi } from './stubs'
+import { mountPickerRoot, openDirectoryPicker } from './dialog-picker'
type Listener = (...args: unknown[]) => void
@@ -393,7 +394,17 @@ export async function installWsBridge(): Promise<void> {
const api: NodeTerminalApi = {
...buildStubApi(),
...buildRealApi(client),
- ...buildFilesApi(client)
+ ...buildFilesApi(client),
+ // Web replacement for the Electron native dialog: an in-app server-directory browser over
+ // fs.list (the stub's E_UNSUPPORTED reject is dropped in favor of this real picker).
+ dialog: (() => {
+ mountPickerRoot()
+ const startDir = '/' // navigable up/down from root; the picker remembers nothing across calls in v1
+ return {
+ selectFolder: () => openDirectoryPicker({ mode: 'folder', startDir, list: api.fs.list }),
+ selectFile: () => openDirectoryPicker({ mode: 'file', startDir, list: api.fs.list })
+ }
+ })()
}
;(window as unknown as { nodeTerminal: NodeTerminalApi }).nodeTerminal = api
}
diff --git a/src/renderer/styles.css b/src/renderer/styles.css
index 8a6e30d0d..d2dbf567f 100644
--- a/src/renderer/styles.css
+++ b/src/renderer/styles.css
@@ -3238,6 +3238,100 @@ body,
font-weight: 600;
}
+/* ---- Web folder/file picker (Server Edition; reuses the .confirm shell) ---- */
+.confirm.dir-picker {
+ width: 460px;
+ display: flex;
+ flex-direction: column;
+ gap: 12px;
+}
+
+.dir-picker__head {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+}
+
+.dir-picker__up {
+ flex: none;
+ width: 28px;
+ height: 28px;
+ background: var(--panel-header);
+ border: 1px solid var(--border);
+ border-radius: 8px;
+ color: var(--text);
+ font-size: 14px;
+ cursor: pointer;
+}
+
+.dir-picker__up:hover:not(:disabled) {
+ background: rgba(255, 255, 255, 0.1);
+}
+
+.dir-picker__up:disabled {
+ opacity: 0.4;
+ cursor: not-allowed;
+}
+
+.dir-picker__path {
+ flex: 1;
+ min-width: 0;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ direction: rtl;
+ text-align: left;
+ font-size: 12px;
+ color: var(--muted);
+ font-family: var(--mono, monospace);
+}
+
+.dir-picker__list {
+ height: 300px;
+ overflow-y: auto;
+ border: 1px solid var(--border);
+ border-radius: 10px;
+ background: rgba(255, 255, 255, 0.03);
+ padding: 4px;
+}
+
+.dir-picker__empty {
+ padding: 20px;
+ text-align: center;
+ font-size: 12px;
+ color: var(--muted);
+}
+
+.dir-picker__row {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ width: 100%;
+ padding: 7px 9px;
+ background: transparent;
+ border: none;
+ border-radius: 7px;
+ color: var(--text);
+ font-size: 13px;
+ text-align: left;
+ cursor: pointer;
+}
+
+.dir-picker__row:hover {
+ background: rgba(255, 255, 255, 0.08);
+}
+
+.dir-picker__icon {
+ flex: none;
+ font-size: 13px;
+}
+
+.dir-picker__name {
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
/* ---- Shortcuts panel ---- */
.sc-overlay {
position: fixed;