move process to background
with ctrl+b
This commit is contained in:
parent
5a479ca427
commit
561f85e95c
3 changed files with 82 additions and 1 deletions
9
extensions/cli/src/services/BackgroundSignalManager.ts
Normal file
9
extensions/cli/src/services/BackgroundSignalManager.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { EventEmitter } from "events";
|
||||
|
||||
export class BackgroundSignalManager extends EventEmitter {
|
||||
signalBackground(): void {
|
||||
this.emit("backgroundRequested");
|
||||
}
|
||||
}
|
||||
|
||||
export const backgroundSignalManager = new BackgroundSignalManager();
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { spawn } from "child_process";
|
||||
import { ChildProcess, spawn } from "child_process";
|
||||
import fs from "fs";
|
||||
|
||||
import {
|
||||
|
|
@ -6,6 +6,8 @@ import {
|
|||
type ToolPolicy,
|
||||
} from "@continuedev/terminal-security";
|
||||
|
||||
import { backgroundJobManager } from "../services/BackgroundJobManager.js";
|
||||
import { backgroundSignalManager } from "../services/BackgroundSignalManager.js";
|
||||
import { telemetryService } from "../telemetry/telemetryService.js";
|
||||
import {
|
||||
isGitCommitCommand,
|
||||
|
|
@ -82,6 +84,35 @@ function getShellCommand(command: string): { shell: string; args: string[] } {
|
|||
return { shell: userShell, args: ["-l", "-c", command] };
|
||||
}
|
||||
|
||||
export function runCommandInBackground(command: string): {
|
||||
success: boolean;
|
||||
jobId?: string;
|
||||
error?: string;
|
||||
} {
|
||||
const job = backgroundJobManager.createJob(command);
|
||||
if (!job) {
|
||||
return {
|
||||
success: false,
|
||||
error: "Cannot create background job: limit of 5 concurrent jobs reached",
|
||||
};
|
||||
}
|
||||
|
||||
const { shell, args } = getShellCommand(command);
|
||||
const child = backgroundJobManager.startJob(job.id, shell, args);
|
||||
|
||||
if (!child) {
|
||||
return {
|
||||
success: false,
|
||||
error: `Failed to start background job ${job.id}`,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
jobId: job.id,
|
||||
};
|
||||
}
|
||||
|
||||
export const runTerminalCommandTool: Tool = {
|
||||
name: "Bash",
|
||||
displayName: "Bash",
|
||||
|
|
@ -187,6 +218,34 @@ IMPORTANT: To edit files, use Edit/MultiEdit tools instead of bash commands (sed
|
|||
return output;
|
||||
};
|
||||
|
||||
const moveToBackground = () => {
|
||||
if (isResolved) return;
|
||||
isResolved = true;
|
||||
|
||||
if (timeoutId) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
backgroundSignalManager.off("backgroundRequested", moveToBackground);
|
||||
|
||||
const job = backgroundJobManager.createJobWithProcess(
|
||||
command,
|
||||
child as ChildProcess,
|
||||
stdout,
|
||||
);
|
||||
|
||||
if (job) {
|
||||
resolve(
|
||||
`Command moved to background. Job ID: ${job.id}\nOutput so far:\n${stdout}\nUse CheckBackgroundJob("${job.id}") to check status.`,
|
||||
);
|
||||
} else {
|
||||
resolve(
|
||||
`Failed to move to background (job limit reached). Command continues in foreground.\nOutput so far: ${stdout}`,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
backgroundSignalManager.on("backgroundRequested", moveToBackground);
|
||||
|
||||
const resetTimeout = () => {
|
||||
if (timeoutId) {
|
||||
clearTimeout(timeoutId);
|
||||
|
|
@ -230,6 +289,11 @@ IMPORTANT: To edit files, use Edit/MultiEdit tools instead of bash commands (sed
|
|||
clearTimeout(timeoutId);
|
||||
}
|
||||
|
||||
backgroundSignalManager.removeListener(
|
||||
"backgroundRequested",
|
||||
moveToBackground,
|
||||
);
|
||||
|
||||
// Only reject on non-zero exit code if there's also stderr
|
||||
if (code !== 0 && stderr) {
|
||||
reject(`Error (exit code ${code}): ${stderr}`);
|
||||
|
|
@ -267,6 +331,7 @@ IMPORTANT: To edit files, use Edit/MultiEdit tools instead of bash commands (sed
|
|||
if (timeoutId) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
backgroundSignalManager.off("backgroundRequested", moveToBackground);
|
||||
reject(`Error: ${error.message}`);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import type { PermissionMode } from "../../permissions/types.js";
|
||||
import { backgroundSignalManager } from "../../services/BackgroundSignalManager.js";
|
||||
import {
|
||||
checkClipboardForImage,
|
||||
getClipboardImage,
|
||||
|
|
@ -85,6 +86,12 @@ export function handleControlKeys(options: ControlKeysOptions): boolean {
|
|||
return true;
|
||||
}
|
||||
|
||||
// Handle Ctrl+B to send current tool execution to background
|
||||
if (key.ctrl && input === "b") {
|
||||
backgroundSignalManager.signalBackground();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Handle Shift+Tab to cycle through modes
|
||||
if (key.tab && key.shift && !showSlashCommands && !showFileSearch) {
|
||||
cycleModes().catch((error) => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue