fix(coding-agent): speed up external editor launch (#6903)
* fix(coding-agent): speed up external editor launch Fixes #6774 * refactor(coding-agent): clarify external editor handling * refactor(coding-agent): simplify external editor result * refactor(coding-agent): model external editor lifecycle * fix(coding-agent): rename external editor test fake * fix(coding-agent): secure external editor temp files * fix(coding-agent): simplify external editor temp path * fix(coding-agent): restore system temp editor files
This commit is contained in:
@@ -3,10 +3,6 @@
|
||||
* Supports Ctrl+G for external editor.
|
||||
*/
|
||||
|
||||
import { spawn } from "node:child_process";
|
||||
import * as fs from "node:fs";
|
||||
import * as os from "node:os";
|
||||
import * as path from "node:path";
|
||||
import {
|
||||
Container,
|
||||
Editor,
|
||||
@@ -18,6 +14,7 @@ import {
|
||||
type TUI,
|
||||
} from "@earendil-works/pi-tui";
|
||||
import type { KeybindingsManager } from "../../../core/keybindings.ts";
|
||||
import { editInExternalEditor } from "../external-editor.ts";
|
||||
import { getEditorTheme, theme } from "../theme/theme.ts";
|
||||
import { DynamicBorder } from "./dynamic-border.ts";
|
||||
import { keyHint } from "./keybinding-hints.ts";
|
||||
@@ -28,7 +25,7 @@ export class ExtensionEditorComponent extends Container implements Focusable {
|
||||
private onCancelCallback: () => void;
|
||||
private tui: TUI;
|
||||
private keybindings: KeybindingsManager;
|
||||
private externalEditorCommand: string | undefined;
|
||||
private externalEditorCommand: string;
|
||||
|
||||
private _focused = false;
|
||||
get focused(): boolean {
|
||||
@@ -53,7 +50,11 @@ export class ExtensionEditorComponent extends Container implements Focusable {
|
||||
|
||||
this.tui = tui;
|
||||
this.keybindings = keybindings;
|
||||
this.externalEditorCommand = externalEditorCommand;
|
||||
this.externalEditorCommand =
|
||||
externalEditorCommand ||
|
||||
process.env.VISUAL ||
|
||||
process.env.EDITOR ||
|
||||
(process.platform === "win32" ? "notepad" : "nano");
|
||||
this.onSubmitCallback = onSubmit;
|
||||
this.onCancelCallback = onCancel;
|
||||
|
||||
@@ -79,14 +80,13 @@ export class ExtensionEditorComponent extends Container implements Focusable {
|
||||
this.addChild(new Spacer(1));
|
||||
|
||||
// Add hint
|
||||
const hasExternalEditor = !!this.getExternalEditorCommand();
|
||||
const hint =
|
||||
keyHint("tui.select.confirm", "submit") +
|
||||
" " +
|
||||
keyHint("tui.input.newLine", "newline") +
|
||||
" " +
|
||||
keyHint("tui.select.cancel", "cancel") +
|
||||
(hasExternalEditor ? ` ${keyHint("app.editor.external", "external editor")}` : "");
|
||||
` ${keyHint("app.editor.external", "external editor")}`;
|
||||
this.addChild(new Text(hint, 1, 0));
|
||||
|
||||
this.addChild(new Spacer(1));
|
||||
@@ -105,7 +105,7 @@ export class ExtensionEditorComponent extends Container implements Focusable {
|
||||
|
||||
// External editor (app keybinding)
|
||||
if (this.keybindings.matches(keyData, "app.editor.external")) {
|
||||
this.openExternalEditor();
|
||||
void this.handleOpenExternalEditor();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -113,54 +113,19 @@ export class ExtensionEditorComponent extends Container implements Focusable {
|
||||
this.editor.handleInput(keyData);
|
||||
}
|
||||
|
||||
private getExternalEditorCommand(): string | undefined {
|
||||
const editorCmd = this.externalEditorCommand || process.env.VISUAL || process.env.EDITOR;
|
||||
if (editorCmd) {
|
||||
return editorCmd;
|
||||
}
|
||||
return process.platform === "win32" ? "notepad" : "nano";
|
||||
}
|
||||
|
||||
private async openExternalEditor(): Promise<void> {
|
||||
const editorCmd = this.getExternalEditorCommand();
|
||||
if (!editorCmd) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentText = this.editor.getText();
|
||||
const tmpFile = path.join(os.tmpdir(), `pi-extension-editor-${Date.now()}.md`);
|
||||
|
||||
private async handleOpenExternalEditor(): Promise<void> {
|
||||
const content = this.editor.getText();
|
||||
this.tui.stop();
|
||||
try {
|
||||
fs.writeFileSync(tmpFile, currentText, "utf-8");
|
||||
this.tui.stop();
|
||||
|
||||
const [editor, ...editorArgs] = editorCmd.split(" ");
|
||||
process.stdout.write(`Launching external editor: ${editorCmd}\nPi will resume when the editor exits.\n`);
|
||||
|
||||
// Do not use spawnSync here. On Windows, synchronous child_process calls can keep
|
||||
// Node/libuv's console input read active after tui.stop() pauses stdin, racing
|
||||
// vim/nvim for the console input buffer until Ctrl+C cancels the pending read.
|
||||
const status = await new Promise<number | null>((resolve) => {
|
||||
const child = spawn(editor, [...editorArgs, tmpFile], {
|
||||
stdio: "inherit",
|
||||
shell: process.platform === "win32",
|
||||
});
|
||||
child.on("error", () => resolve(null));
|
||||
child.on("close", (code) => resolve(code));
|
||||
const result = await editInExternalEditor({
|
||||
command: this.externalEditorCommand,
|
||||
content,
|
||||
});
|
||||
|
||||
if (status === 0) {
|
||||
const newContent = fs.readFileSync(tmpFile, "utf-8").replace(/\n$/, "");
|
||||
this.editor.setText(newContent);
|
||||
if (result.status === "complete") {
|
||||
this.editor.setText(result.content);
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
fs.unlinkSync(tmpFile);
|
||||
} catch {
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
this.tui.start();
|
||||
// Force full re-render since external editor uses alternate screen
|
||||
this.tui.requestRender(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
export interface ExternalEditorOptions {
|
||||
command: string;
|
||||
content: string;
|
||||
}
|
||||
|
||||
export type ExternalEditorResult = { status: "complete"; content: string } | { status: "failed" };
|
||||
|
||||
export async function editInExternalEditor(options: ExternalEditorOptions): Promise<ExternalEditorResult> {
|
||||
const directory = mkdtempSync(join(tmpdir(), "pi-editor-"));
|
||||
const filePath = join(directory, "prompt.md");
|
||||
try {
|
||||
writeFileSync(filePath, options.content, "utf-8");
|
||||
const [editor, ...editorArgs] = options.command.split(" ");
|
||||
process.stdout.write(`Launching external editor: ${options.command}\nPi will resume when the editor exits.\n`);
|
||||
|
||||
// Do not use spawnSync here. On Windows, synchronous child_process calls can keep
|
||||
// Node/libuv's console input read active after the parent pauses stdin, racing
|
||||
// vim/nvim for the console input buffer until Ctrl+C cancels the pending read.
|
||||
const exitCode = await new Promise<number | null>((resolve) => {
|
||||
const child = spawn(editor, [...editorArgs, filePath], {
|
||||
stdio: "inherit",
|
||||
shell: process.platform === "win32",
|
||||
});
|
||||
child.on("error", () => resolve(null));
|
||||
child.on("close", (code) => resolve(code));
|
||||
});
|
||||
|
||||
if (exitCode !== 0) {
|
||||
return { status: "failed" };
|
||||
}
|
||||
|
||||
return { status: "complete", content: readFileSync(filePath, "utf-8").replace(/\n$/, "") };
|
||||
} finally {
|
||||
try {
|
||||
rmSync(directory, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Cleanup is best effort.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,6 +137,7 @@ import { TreeSelectorComponent } from "./components/tree-selector.ts";
|
||||
import { TrustSelectorComponent } from "./components/trust-selector.ts";
|
||||
import { UserMessageComponent } from "./components/user-message.ts";
|
||||
import { UserMessageSelectorComponent } from "./components/user-message-selector.ts";
|
||||
import { editInExternalEditor } from "./external-editor.ts";
|
||||
import { getModelSearchText } from "./model-search.ts";
|
||||
import {
|
||||
getAvailableThemes,
|
||||
@@ -2577,7 +2578,7 @@ export class InteractiveMode {
|
||||
this.defaultEditor.onAction("app.model.select", () => this.showModelSelector());
|
||||
this.defaultEditor.onAction("app.tools.expand", () => this.toggleToolOutputExpansion());
|
||||
this.defaultEditor.onAction("app.thinking.toggle", () => this.toggleThinkingBlockVisibility());
|
||||
this.defaultEditor.onAction("app.editor.external", () => this.openExternalEditor());
|
||||
this.defaultEditor.onAction("app.editor.external", () => void this.handleOpenExternalEditor());
|
||||
this.defaultEditor.onAction("app.message.copy", () => void this.handleCopyCommand());
|
||||
this.defaultEditor.onAction("app.message.followUp", () => this.handleFollowUp());
|
||||
this.defaultEditor.onAction("app.message.dequeue", () => this.handleDequeue());
|
||||
@@ -3809,57 +3810,20 @@ export class InteractiveMode {
|
||||
this.showStatus(`Thinking blocks: ${this.hideThinkingBlock ? "hidden" : "visible"}`);
|
||||
}
|
||||
|
||||
private async openExternalEditor(): Promise<void> {
|
||||
private async handleOpenExternalEditor(): Promise<void> {
|
||||
const editorCmd = this.settingsManager.getExternalEditorCommand();
|
||||
if (!editorCmd) {
|
||||
this.showWarning("No editor configured. Set externalEditor in settings.json or $VISUAL/$EDITOR.");
|
||||
return;
|
||||
}
|
||||
|
||||
const currentText = this.editor.getExpandedText?.() ?? this.editor.getText();
|
||||
const tmpFile = path.join(os.tmpdir(), `pi-editor-${Date.now()}.pi.md`);
|
||||
|
||||
const content = this.editor.getExpandedText?.() ?? this.editor.getText();
|
||||
this.ui.stop();
|
||||
try {
|
||||
// Write current content to temp file
|
||||
fs.writeFileSync(tmpFile, currentText, "utf-8");
|
||||
|
||||
// Stop TUI to release terminal
|
||||
this.ui.stop();
|
||||
|
||||
// Split by space to support editor arguments (e.g., "code --wait")
|
||||
const [editor, ...editorArgs] = editorCmd.split(" ");
|
||||
|
||||
process.stdout.write(`Launching external editor: ${editorCmd}\nPi will resume when the editor exits.\n`);
|
||||
|
||||
// Do not use spawnSync here. On Windows, synchronous child_process calls can keep
|
||||
// Node/libuv's console input read active after ui.stop() pauses stdin, racing
|
||||
// vim/nvim for the console input buffer until Ctrl+C cancels the pending read.
|
||||
const status = await new Promise<number | null>((resolve) => {
|
||||
const child = spawn(editor, [...editorArgs, tmpFile], {
|
||||
stdio: "inherit",
|
||||
shell: process.platform === "win32",
|
||||
});
|
||||
child.on("error", () => resolve(null));
|
||||
child.on("close", (code) => resolve(code));
|
||||
const result = await editInExternalEditor({
|
||||
command: editorCmd,
|
||||
content,
|
||||
});
|
||||
|
||||
// On successful exit (status 0), replace editor content
|
||||
if (status === 0) {
|
||||
const newContent = fs.readFileSync(tmpFile, "utf-8").replace(/\n$/, "");
|
||||
this.editor.setText(newContent);
|
||||
if (result.status === "complete") {
|
||||
this.editor.setText(result.content);
|
||||
}
|
||||
// On non-zero exit, keep original text (no action needed)
|
||||
} finally {
|
||||
// Clean up temp file
|
||||
try {
|
||||
fs.unlinkSync(tmpFile);
|
||||
} catch {
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
|
||||
// Restart TUI
|
||||
this.ui.start();
|
||||
// Force full re-render since external editor uses alternate screen
|
||||
this.ui.requestRender(true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user