Feature: Support `?cwd=` URL query parameter to set initial working directory
Background
I'm building a Windows desktop shell (pi-web-desktop) that wraps pi-web in Electron and adds right-click "Open Pi Web here" context menu integration in Windows Explorer.
When the user right-clicks a folder and selects "Open Pi Web here", the Electron app launches pi-web and needs to tell the frontend: "start in this directory as a new session".
Problem
Currently there's no way to pass the desired working directory via URL. The frontend always restores the most recent session on load, which means:
- The UI shows the old session's cwd (e.g.
D:\PI-web-desktop) - But the backend's global cwd has been changed via
POST /api/cwd/validateto the new directory - This creates a confusing mismatch: the sidebar says one directory, but tools operate in another
The /api/cwd/validate endpoint changes the global/default cwd, but it does not affect an already-restored historical session. And pre-creating an empty session via /api/agent/new doesn't work reliably because empty sessions don't persist until the first message, so the session header still shows the wrong cwd.
Proposed Solution
Support a ?cwd=<encoded-path> query parameter on the main page URL. When present:
- Initialize
newSessionCwdstate fromuseSearchParams().get("cwd")instead ofnull - Skip the "restore latest session" auto-selection logic (the user explicitly wants a new session in the given directory)
- The sidebar should show the target directory as the selected cwd, and the Explorer panel should list files from that directory
This is a minimal, non-breaking change — the parameter is optional and only affects initial state. Without it, behavior is identical to today.
My Current Workaround
I'm patching two minified build artifacts to inject this behavior:
Client bundle (page-*.js):
// Before:
[k,j]=(0,o.useState)(null)
// After:
[k,j]=(0,o.useState)(()=>d??null) // d = useSearchParams().get("cwd")
SSR bundle (page.js):
// Before:
[p,q]=(0,af.useState)(null)
// After:
[p,q]=(0,af.useState)(()=>j.get("cwd"))
This works but is fragile — it breaks on every pi-web update because the minified variable names change.
Use Cases Beyond Desktop
- Bookmarking a specific project directory:
http://localhost:30141/?cwd=/home/user/my-project - CLI integration:
pi-web --open /path/to/projectcould navigate to/?cwd=... - IDE extensions that want to open pi-web pointed at the current workspace
- Sharing links that include the intended working directory
Reference Implementation
Full working desktop app: https://github.com/dust617/pi-web-desktop
The app uses requestSingleInstanceLock with additionalData for cross-instance project handoff, calls /api/cwd/validate to set the backend cwd, then navigates to /?cwd=<path> so the frontend enters the correct new-session state.
Would love to see this as a first-class feature so I can stop patching minified bundles! 🙏
I wanted to ask the same by supporting this from command line and or environment variable. I think this is a must.