Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(tui): enable /memory command and fix open tab on headless Linux

haowenzhong15faad049865b
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/memory.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/memory.ts
index 1e2a04bdb3..fe7f4b9f60 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/memory.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/memory.ts
@@ -859,20 +859,25 @@ async function openMemoryDir(
return;
}
- // 优先调系统文件管理器打开;不支持时显示可复制路径 + 平台命令提示
- if (ctx.openFolder) {
- ctx.openFolder(selectedValue);
+ // 优先调系统文件管理器打开;不支持时(无 GUI 或未注入回调)显示可复制路径提示
+ const opened = ctx.openFolder?.(selectedValue);
+ if (opened) {
ctx.addItem(addInfo(ctx.sessionId, `Opened memory folder: ${selectedValue}`, "m"));
} else {
- const cmd = process.platform === "win32"
- ? `explorer "${selectedValue}"`
- : process.platform === "darwin"
- ? `open "${selectedValue}"`
- : `xdg-open "${selectedValue}"`;
+ // 无 GUI explorer(如无头 Linux 服务器)或未注入 openFolder 回调:
+ // 显示可复制路径 + 平台命令,避免误导用户以为文件夹已打开。
+ let cmd: string;
+ if (process.platform === "win32") {
+ cmd = `explorer "${selectedValue}"`;
+ } else if (process.platform === "darwin") {
+ cmd = `open "${selectedValue}"`;
+ } else {
+ cmd = `xdg-open "${selectedValue}"`;
+ }
ctx.addItem(
addInfo(
ctx.sessionId,
- `Cannot open directly. Path: ${selectedValue}\nOpen with: ${cmd}`,
+ `No GUI explorer detected. Path: ${selectedValue}\nOpen with: ${cmd}`,
"i",
),
);
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts
index 009578f93f..1e099cb0a3 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts
@@ -106,8 +106,11 @@ export interface CommandContext {
) => void;
enterStatusView?: (tab?: StatusViewTab) => void;
openInEditor?: (filePath: string) => void;
- /** Open a folder in system file explorer (Windows: explorer, macOS: open -R, Linux: xdg-open) */
- openFolder?: (folderPath: string) => void;
+ /** Open a folder in system file explorer (Windows: explorer, macOS: open -R, Linux: xdg-open).
+ * Returns true if an explorer was launched; false if no GUI explorer is
+ * available (e.g. headless Linux server), so the caller can fall back to
+ * a path hint instead of claiming the folder was opened. */
+ openFolder?: (folderPath: string) => boolean;
/** Enter FileViewer mode to view large content (e.g., formatted logs) */
enterFileViewer?: (content: string, title: string, source: string) => void;
/** Enter DiffViewer mode to browse git/turn diffs interactively */
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/utils/editor.ts b/jiuwenswarm/channels/tui/frontend/src/core/utils/editor.ts
index ca60612787..529f3a5a80 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/utils/editor.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/utils/editor.ts
@@ -260,9 +260,13 @@ function spawnFallbackAsync(filePath: string, onExit?: () => void): void {
* Open a folder in the system file explorer (not an editor).
* - Windows: explorer
* - macOS: open -R (reveals in Finder)
- * - Linux: xdg-open
+ * - Linux: xdg-open (requires a GUI/Display; falls back gracefully on headless servers)
+ *
+ * Returns true if an explorer was (likely) launched, false if the platform
+ * has no GUI explorer available (e.g. a headless Linux server). Callers can
+ * use the false return to show a copyable path hint instead.
*/
-export function openFolderInExplorer(folderPath: string): void {
+export function openFolderInExplorer(folderPath: string): boolean {
const spawnOptions: SpawnSyncOptions = { stdio: "inherit" };
// Ensure folder exists before opening (explorer opens Documents if path doesn't exist)
@@ -276,9 +280,23 @@ export function openFolderInExplorer(folderPath: string): void {
if (process.platform === "win32") {
spawnSync(`explorer "${folderPath}"`, { ...spawnOptions, shell: true });
+ return true;
} else if (process.platform === "darwin") {
spawnSync("open", ["-R", folderPath], spawnOptions);
+ return true;
} else {
- spawnSync("xdg-open", [folderPath], spawnOptions);
+ // Headless Linux server: no xdg-open, no DISPLAY → don't block on a
+ // spawnSync that will just hang or error out. Tell the caller to fall
+ // back to a path hint.
+ const hasDisplay = !!process.env.DISPLAY || !!process.env.WAYLAND_DISPLAY;
+ if (!hasDisplay) {
+ return false;
+ }
+ try {
+ const res = spawnSync("xdg-open", [folderPath], spawnOptions);
+ return res.status === 0 || res.status === null;
+ } catch {
+ return false;
+ }
}
}
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
index c8482ca996..67394868c4 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
@@ -3150,7 +3150,7 @@ export class AppScreen implements Component, Focusable {
openInExternalEditor(this.tui, filePath);
},
openFolder: (folderPath: string) => {
- openFolderInExplorer(folderPath);
+ return openFolderInExplorer(folderPath);
},
enterFileViewer: (content, title, source) => {
this.enterFileViewer(content, title, source);
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/memory-view.ts b/jiuwenswarm/channels/tui/frontend/src/ui/memory-view.ts
index d2ffe7fbb2..de18d8938c 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/memory-view.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/memory-view.ts
@@ -72,6 +72,8 @@ export class MemoryViewController {
private showFullPath = false;
/** 底部通知行:显示最新一次操作结果,新覆盖旧 */
private statusMessage: string | null = null;
+ /** 上一次 open 的目录绝对路径;Ctrl+O 切换时据此重算 statusMessage 的显示格式 */
+ private lastOpenedPath: string | null = null;
constructor(
private appState: CliPiAppState,
@@ -141,6 +143,7 @@ export class MemoryViewController {
async open(tab?: MemoryViewTab): Promise<void> {
this.showFullPath = false;
this.statusMessage = null;
+ this.lastOpenedPath = null;
const ctx = this.appState.getCommandContext();
const fullMode = ctx.mode ?? "code.normal";
const projectDir = ctx.getCurrentProjectDir() || process.cwd();
@@ -223,6 +226,7 @@ export class MemoryViewController {
case "memory:toggleFullPath":
this.showFullPath = !this.showFullPath;
this.rebuildTabList();
+ this.reformatLastOpenedStatus();
return true;
default:
break;
@@ -289,10 +293,32 @@ export class MemoryViewController {
this.state.tab = tabs[next];
this.showFullPath = false;
this.statusMessage = null;
+ this.lastOpenedPath = null;
this.rebuildTabList();
this.tui.requestRender();
}
+ /**
+ * Ctrl+O 切换 full-path 显示后,重算底部 statusMessage 里上一次 open
+ * 操作的路径显示格式,使提示行与列表项的格式保持一致。
+ * 仅在 open tab 有 lastOpenedPath 时生效,其他场景静默跳过。
+ */
+ private reformatLastOpenedStatus(): void {
+ if (!this.state || !this.lastOpenedPath) return;
+ const projectDir = this.state.projectDir ?? "";
+ const gitRoot = this.state.gitRoot ?? null;
+ const displayPath = this.showFullPath
+ ? this.lastOpenedPath.replace(/\\/g, "/")
+ : getDisplayPath(this.lastOpenedPath, projectDir, gitRoot);
+ // 保留原有 "Opened:" / "No GUI explorer detected. ..." 前缀,只替换显示路径
+ if (this.statusMessage?.startsWith("Opened: ")) {
+ this.statusMessage = `Opened: ${displayPath}`;
+ } else if (this.statusMessage?.startsWith("No GUI explorer detected. Path: ")) {
+ this.statusMessage = `No GUI explorer detected. Path: ${displayPath}`;
+ }
+ this.tui.requestRender();
+ }
+
private rebuildTabList(): void {
if (!this.state) return;
const s = this.state;
@@ -440,7 +466,26 @@ export class MemoryViewController {
return;
}
if (tab === "open" && item.value && item.value !== "__display__") {
- openFolderInExplorer(item.value);
+ const opened = openFolderInExplorer(item.value);
+ // Remember the absolute path so Ctrl+O can reformat the statusMessage
+ // later without re-opening the folder.
+ this.lastOpenedPath = item.value;
+ // Display path follows the showFullPath toggle (Ctrl+O), matching
+ // the format used by buildOpenItems so the hint stays consistent
+ // with what the user sees in the list.
+ const projectDir = this.state?.projectDir ?? "";
+ const gitRoot = this.state?.gitRoot ?? null;
+ const displayPath = this.showFullPath
+ ? item.value.replace(/\\/g, "/")
+ : getDisplayPath(item.value, projectDir, gitRoot);
+ if (opened) {
+ this.statusMessage = `Opened: ${displayPath}`;
+ } else {
+ // No GUI explorer (e.g. headless Linux server): show a copyable
+ // path hint instead of silently failing.
+ this.statusMessage = `No GUI explorer detected. Path: ${displayPath}`;
+ }
+ this.tui.requestRender();
return;
}
// status 只读,无操作