Skip to content

feat: open and reveal paths in the OS file manager - #951

Closed
planb788 wants to merge 1 commit into
agegr:mainfrom
planb788:feat/open-folder
Closed

planb788 wants to merge 1 commit into
agegr:mainfrom
planb788:feat/open-folder

Conversation

@planb788

@planb788 planb788 commented Sep 23, 2026 •

Copy link
Copy Markdown

What

Adds POST /api/open-folder, which opens a path in the OS file manager
(Explorer / Finder / xdg-open), and wires it into three places in the UI.

Why

Pi Web can already preview a file in a tab, but a browser cannot navigate to a
file:// URL, so there was no way to jump from the app to the folder on disk.

Note that this is not a browser capability: #206 was closed with "the web has
no reveal capability", which is true for a plain page — but Pi Web is a local
server, so a route handler can launch the file manager itself. The path is
gated by the same allow-list as /api/files, and the request has to come from
this machine (see below).

How

Route — app/api/open-folder/route.ts

  • POST { path }; stat first (400 when missing), then authorizes against the
    same allow-list as /api/files and /api/terminal
    (getAllowedFileRoots + isExistingFilePathAllowed, 403 when outside).
  • Only requests addressed to this machine's loopback interface are accepted
    (403 otherwise), so a phone or another computer on the LAN cannot raise
    windows on the host. Host is client-controlled, so this is a usability
    guard, not access control — the real boundaries stay the loopback bind and
    the path allow-list.
  • Spawns without a shell, detached, stdio: "ignore", and resolves on the
    spawn event so a launch failure becomes a 500 instead of a false ok.
  • A directory opens as-is; a file is revealed with its entry selected.

Commands — lib/open-folder.ts

  • openFolderCommand: explorer.exe <dir> / open <dir> / xdg-open <dir>
  • revealFileCommand: explorer.exe /select,<file> / open -R <file> /
    xdg-open <parent> (xdg-open has no portable "select this entry" flag)
  • isLoopbackHost: host classification for the guard above.

UI

  • components/SessionSidebar.tsx — folder button in the explorer toolbar, next
    to the terminal button, opening the selected project folder.
  • components/FileViewer.tsx + components/RevealInFileManagerButton.tsx —
    reveal button in all five viewer headers (hidden for deleted diffs). The icon
    is the file tree's own folder icon.
  • components/MarkdownBody.tsx + lib/file-links.ts — an inline-code span that
    is unambiguously a path becomes a chip. Clicking the path keeps the existing
    in-app preview; a directory (which /api/files cannot serve) falls back to
    opening the folder. The folder icon in the chip reveals it on disk.

Relationship to existing issues and PRs

Notes

  • windowsHide must stay false: SW_HIDE inherited from STARTUPINFO
    suppresses the Explorer window even though explorer.exe is a GUI app. This
    was verified by enumerating top-level windows before/after the spawn.
  • Inline-code detection is deliberately narrow, because inline code is far more
    often prose than a path: a single token that is either rooted (drive letter,
    UNC, /…) or slash-separated with a file-looking tail. npm test,
    application/json, read/write, Node.js, v0.9.2, --flag and URLs stay
    plain code, and a relative path that would escape the message cwd is ignored.
  • i18n keys added to en, zh-CN, zh-TW.
  • The demo in demo/ is intentionally not updated here: it keeps its own
    copies with Pages-specific changes. Happy to sync it in a follow-up PR.

Tests

  • lib/open-folder.test.mjs — pure command builder for all platforms, plus
    loopback host classification (127.0.0.1, localhost, [::1], ::1 vs.
    LAN IPs and lookalike domains such as 127.0.0.1.evil.com).
  • app/api/open-folder/route.test.mjs — validation paths only (400 / 403,
    including a non-loopback request); tests never spawn a real file manager.
  • lib/path-actions.test.mjs — probe + reveal against a stubbed fetch.
  • lib/file-links.test.mjs — inline-code detection, including prose negatives
    and cwd-escape cases.
  • components/MarkdownBody.test.mjs — chip rendering, prose stays code, code
    nested in a link is untouched.

tsc --noEmit and eslint . are clean. The full suite matches main apart from
the pre-existing Windows-only failures (symlink / pty / git) that are present
without this change.

Manually verified on Windows: opening a folder, revealing a file (window opens
with the entry selected), a path outside the allow-list returning 403, and a
spoofed non-loopback Host returning 403 without opening anything.

Add POST /api/open-folder, which authorizes the target against the same
file-access allow-list as /api/files and /api/terminal, then launches
explorer.exe / open / xdg-open without a shell. A directory opens as-is;
a file is revealed with its entry selected (explorer.exe /select, on
Windows, open -R on macOS).

Wire it up in three places:
- a folder button in the sidebar explorer toolbar, next to the terminal
  button, opening the selected project folder
- a folder button in every file-viewer header, revealing the open file
- inline-code paths in replies become chips: clicking the path keeps
  opening the in-app viewer (a directory falls back to opening the
  folder, which cannot be previewed), and the folder icon reveals the
  path on disk

windowsHide stays false on Windows: SW_HIDE inherited from STARTUPINFO
suppresses the Explorer window even though explorer.exe is a GUI app.
@Light1002

Copy link
Copy Markdown

感谢这个 PR —— #859 的诉求基本都覆盖了,细节也做得扎实(复用 /api/files 的 allow-list、windowsHide/SW_HIDE 那个坑的实测、配套测试、三语文案)。

我这边还有一个场景没被覆盖:文件树本身没有入口。

  • 工具栏那个按钮只到工作区根目录;
  • viewer 头部的 reveal 按钮只对已经打开成 tab 的文件生效;
  • 于是树里一个深层目录(比如 out/report/)仍然开不了 —— 除非先点开它里面的某个文件。

我的用法正是"在树里看到这个目录/文件,就想直接交给资源管理器"(用原生程序改文件、拖拽、复制真实路径)。

树行上其实已经有现成的模式可以复用:mention chip 和下载图标都是 hover 显示、绝对定位在行右端(components/FileExplorer.tsx:395,hovered && !node.isDir)。照着加第三个图标就够了,而且它对目录同样适用 —— 注意下载图标是 !node.isDir 才显示,reveal 图标不该这么限制。

顺便两个小点:

  1. loopback 判定读的是 Host 头,而 Host 是客户端可控的:如果 Pi Web 挂在会重写 Host 的同机反向代理后面,远端浏览器也会被放行,从而真的在主机上弹窗。你已经说明这是可用性门闩而非访问控制,那 tooltip 的措辞就更重要了 —— 建议写清"在运行 Pi Web 的那台机器上打开",免得远程/手机访问的用户以为会打开自己那台的窗口。
  2. 我在资源管理器里改完文件之后,文件树/viewer 需要刷新。这里到底是 watch 还是手动刷新,值得在 tooltip 或文档里说明一下。

再次感谢。

Kano-u added a commit to Kano-u/pi-web that referenced this pull request Sep 29, 2026

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks @planb788, the write-up is excellent, including the windowsHide/SW_HIDE detail. We've decided to go with the narrower #907 for this (a single "open workspace folder" button in the explorer header), so I'm closing this one.

The two extra surfaces are where it goes beyond what we want Pi Web to carry:

  • Path chips in chat Markdown. Auto-detecting paths in inline code is a heuristic over model output, and the click can launch a desktop program on the server. On macOS, open <dir> on an .app bundle launches the app instead of showing the folder. We'd rather not tie that to text the model wrote.
  • Reveal buttons in all five viewer headers. They're useful, but it's a lot of UI for an action that only works when the browser runs on the same machine.

If #907 lands and a "reveal this file" action still feels missing in practice, a small follow-up on top of it can be discussed in an issue.


Generated by Claude Code

@agegr agegr closed this Sep 30, 2026
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
…#907 agegr#941 agegr#946 agegr#961 agegr#968 agegr#969 agegr#1007 agegr#1008 agegr#1009 agegr#1010)

上游已把我们提前摘取的 agegr#884 / agegr#871 / agegr#996 / agegr#961 原样合并(agegr#961 逐字节相同),
agegr#967 与 agegr#963 由上游的 agegr#1009 / agegr#1008 接管。

冲突解决:
- demo/ 对齐上游 fe288c0,撤销 agegr#963 对 demo 的两处改动(`@/lib/chat-phase-label`
  在 demo/lib 下不存在,会打断 Pages 构建)。
- AGENTS.md:保留我们的 default-project 条目,open-folder 换成上游的
  open-in-explorer。
- MessageView.tsx / SessionSidebar.tsx / FileExplorer.tsx:以上游为准,
  DismissButton 采用上游的独立文件,保留我们的思考卡片与文件管理改动。
- SessionSidebar 移除引用已删函数的旧 agegr#951 工作区按钮(agegr#951 的其他入口另行收尾)。
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
上游只接受「资源管理器头部一个按钮」的窄版(agegr#907 / a0c00f3,含 agegr#1010 的收尾),
并以「超出薄前端范围」关闭了我们的宽版 agegr#951,因此移除 agegr#951 的两个额外入口:

- 聊天回复里行内代码路径的 chip(MarkdownBody.tsx 还原上游版本,
  lib/file-links.ts、MarkdownBody.test.mjs、file-links.test.mjs 与
  app/globals.css 的样式一并还原)
- 文件查看器头部的「在资源管理器中显示」按钮

同时删除只服务于它们的模块:/api/open-folder、lib/open-folder.ts、
lib/path-actions.ts、hooks/useLocalFileManager.ts、lib/loopback-host.ts,
以及两条只被它们使用的 i18n 文案。上游的 /api/open-in-explorer +
lib/open-in-file-manager.ts(含平台文案、远程/不支持时的置灰与错误提示)
保持不变。
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
agegr#967 / agegr#963 已由上游的 agegr#1009 / agegr#1008 落地,agegr#961 上游原样合并(逐字节相同),
agegr#951 已收敛到上游的 agegr#907,agegr#960 上下游都拒绝——这些都与上游一致,按台账规则删行。
agegr#899 上游关闭但本地保留,状态注明为永久分叉。
@planb788
planb788 deleted the feat/open-folder branch October 3, 2026 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants