pi-web icon indicating copy to clipboard operation
pi-web copied to clipboard

Feature: Support `?cwd=` URL query parameter to set initial working directory

Open dust617 opened this issue 4 months ago • 1 comments

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:

  1. The UI shows the old session's cwd (e.g. D:\PI-web-desktop)
  2. But the backend's global cwd has been changed via POST /api/cwd/validate to the new directory
  3. 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:

  1. Initialize newSessionCwd state from useSearchParams().get("cwd") instead of null
  2. Skip the "restore latest session" auto-selection logic (the user explicitly wants a new session in the given directory)
  3. 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/project could 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! 🙏

dust617 avatar Jul 21 '26 18:07 dust617

I wanted to ask the same by supporting this from command line and or environment variable. I think this is a must.

digregoriovalerio avatar Jul 22 '26 08:07 digregoriovalerio