diff --git a/extensions/cli/src/commands/commands.ts b/extensions/cli/src/commands/commands.ts index e04849c587d..0c85f2228c1 100644 --- a/extensions/cli/src/commands/commands.ts +++ b/extensions/cli/src/commands/commands.ts @@ -2,11 +2,11 @@ import { type AssistantConfig } from "@continuedev/sdk"; // Export command functions export { chat } from "./chat.js"; -export { review } from "./review.js"; export { login } from "./login.js"; export { logout } from "./logout.js"; export { listSessionsCommand } from "./ls.js"; export { remote } from "./remote.js"; +export { review } from "./review.js"; export { serve } from "./serve.js"; export interface SlashCommand { @@ -101,6 +101,11 @@ export const SYSTEM_SLASH_COMMANDS: SystemCommand[] = [ description: "Exit the chat", category: "system", }, + { + name: "jobs", + description: "List background jobs", + category: "system", + }, ]; // Remote mode specific commands diff --git a/extensions/cli/src/services/BackgroundJobService.ts b/extensions/cli/src/services/BackgroundJobService.ts new file mode 100644 index 00000000000..e4350231e04 --- /dev/null +++ b/extensions/cli/src/services/BackgroundJobService.ts @@ -0,0 +1,218 @@ +import { ChildProcess, spawn } from "child_process"; + +import { logger } from "../util/logger.js"; + +export type BackgroundJobStatus = + | "pending" + | "running" + | "completed" + | "failed" + | "cancelled"; + +export interface BackgroundJob { + id: string; + status: BackgroundJobStatus; + command: string; + output: string; + exitCode: number | null; + startTime: Date; + endTime: Date | null; + error?: string; +} + +const MAX_CONCURRENT_JOBS = 5; +const MAX_OUTPUT_LINES = 1000; + +/** + * Service for managing background job execution and lifecycle + * Handles spawning, tracking, and cleanup of background processes + */ +export class BackgroundJobService { + private jobs: Map = new Map(); + private processes: Map = new Map(); + private jobCounter = 0; + + createJob(command: string): BackgroundJob | null { + const runningCount = this.getRunningJobCount(); + if (runningCount >= MAX_CONCURRENT_JOBS) { + logger.warn( + `Cannot create background job: limit of ${MAX_CONCURRENT_JOBS} reached`, + ); + return null; + } + + const id = `bg-${++this.jobCounter}-${Date.now()}`; + const job: BackgroundJob = { + id, + status: "pending", + command, + output: "", + exitCode: null, + startTime: new Date(), + endTime: null, + }; + + this.jobs.set(id, job); + return job; + } + + startJob(jobId: string, shell: string, args: string[]): ChildProcess | null { + const job = this.jobs.get(jobId); + if (!job) { + logger.error(`Cannot start job ${jobId}: job not found`); + return null; + } + + job.status = "running"; + + const child = spawn(shell, args, { stdio: "pipe" }); + this.processes.set(jobId, child); + + child.stdout?.on("data", (data: Buffer) => { + this.appendOutput(jobId, data.toString()); + }); + + child.stderr?.on("data", (data: Buffer) => { + this.appendOutput(jobId, data.toString()); + }); + + child.on("close", (code: number | null) => { + this.completeJob(jobId, code ?? 0); + }); + + child.on("error", (error: Error) => { + this.failJob(jobId, error.message); + }); + + return child; + } + + createJobWithProcess( + command: string, + child: ChildProcess, + existingOutput: string = "", + ): BackgroundJob | null { + const runningCount = this.getRunningJobCount(); + if (runningCount >= MAX_CONCURRENT_JOBS) { + logger.warn( + `Cannot create background job: limit of ${MAX_CONCURRENT_JOBS} reached`, + ); + return null; + } + + const id = `bg-${++this.jobCounter}-${Date.now()}`; + const job: BackgroundJob = { + id, + status: "running", + command, + output: existingOutput, + exitCode: null, + startTime: new Date(), + endTime: null, + }; + + this.jobs.set(id, job); + this.processes.set(id, child); + + child.stdout?.on("data", (data: Buffer) => { + this.appendOutput(id, data.toString()); + }); + + child.stderr?.on("data", (data: Buffer) => { + this.appendOutput(id, data.toString()); + }); + + child.on("close", (code: number | null) => { + this.completeJob(id, code ?? 0); + }); + + child.on("error", (error: Error) => { + this.failJob(id, error.message); + }); + + return job; + } + + // todo: improve write efficiency with ring buffer or similar + appendOutput(jobId: string, data: string): void { + const job = this.jobs.get(jobId); + if (job) { + job.output += data; + const lines = job.output.split("\n"); + if (lines.length > MAX_OUTPUT_LINES) { + job.output = lines.slice(-MAX_OUTPUT_LINES).join("\n"); + } + } + } + + completeJob(jobId: string, exitCode: number): void { + const job = this.jobs.get(jobId); + if (job) { + if (job.status === "cancelled") { + return; + } + job.status = exitCode === 0 ? "completed" : "failed"; + job.exitCode = exitCode; + job.endTime = new Date(); + this.processes.delete(jobId); + } + } + + failJob(jobId: string, error: string): void { + const job = this.jobs.get(jobId); + if (job) { + job.status = "failed"; + job.error = error; + job.endTime = new Date(); + this.processes.delete(jobId); + } + } + + cancelJob(jobId: string): boolean { + const job = this.jobs.get(jobId); + const process = this.processes.get(jobId); + + if (!job) return false; + + if (process) { + process.kill(); + this.processes.delete(jobId); + } + + job.status = "cancelled"; + job.endTime = new Date(); + return true; + } + + getJob(jobId: string): BackgroundJob | undefined { + return this.jobs.get(jobId); + } + + getRunningJobs(): BackgroundJob[] { + return Array.from(this.jobs.values()).filter( + (job) => job.status === "running" || job.status === "pending", + ); + } + + getAllJobs(): BackgroundJob[] { + return Array.from(this.jobs.values()); + } + + getRunningJobCount(): number { + return this.getRunningJobs().length; + } + + killAllJobs(): void { + for (const [jobId, process] of this.processes) { + process.kill(); + const job = this.jobs.get(jobId); + if (job) { + job.status = "cancelled"; + job.endTime = new Date(); + } + } + this.processes.clear(); + } +} + +export const backgroundJobService = new BackgroundJobService(); diff --git a/extensions/cli/src/services/index.ts b/extensions/cli/src/services/index.ts index 56a5d4e938a..778c8625a94 100644 --- a/extensions/cli/src/services/index.ts +++ b/extensions/cli/src/services/index.ts @@ -387,6 +387,7 @@ export const services = { toolPermissions: toolPermissionService, artifactUpload: artifactUploadService, gitAiIntegration: gitAiIntegrationService, + backgroundJobs: backgroundJobService, } as const; export type ServicesType = typeof services; diff --git a/extensions/cli/src/services/types.ts b/extensions/cli/src/services/types.ts index b9804bcd26b..6011bbd5c34 100644 --- a/extensions/cli/src/services/types.ts +++ b/extensions/cli/src/services/types.ts @@ -130,6 +130,10 @@ export interface ArtifactUploadServiceState { lastError: string | null; } +export type { + BackgroundJob, + BackgroundJobStatus, +} from "./BackgroundJobService.js"; export type { ChatHistoryState } from "./ChatHistoryService.js"; export type { FileIndexServiceState } from "./FileIndexService.js"; export type { GitAiIntegrationServiceState } from "./GitAiIntegrationService.js"; @@ -153,6 +157,7 @@ export const SERVICE_NAMES = { AGENT_FILE: "agentFile", ARTIFACT_UPLOAD: "artifactUpload", GIT_AI_INTEGRATION: "gitAiIntegration", + BACKGROUND_JOBS: "backgroundJobs", } as const; /** diff --git a/extensions/cli/src/slashCommands.ts b/extensions/cli/src/slashCommands.ts index c28390d0cec..07c659d5939 100644 --- a/extensions/cli/src/slashCommands.ts +++ b/extensions/cli/src/slashCommands.ts @@ -169,6 +169,10 @@ function handleTitle(args: string[]) { } } +function handleJobs() { + return { openJobsSelector: true }; +} + const commandHandlers: Record = { help: handleHelp, clear: () => { @@ -203,6 +207,7 @@ const commandHandlers: Record = { update: () => { return { openUpdateSelector: true }; }, + jobs: handleJobs, }; export async function handleSlashCommands( diff --git a/extensions/cli/src/tools/checkBackgroundJob.ts b/extensions/cli/src/tools/checkBackgroundJob.ts new file mode 100644 index 00000000000..08968b91fb8 --- /dev/null +++ b/extensions/cli/src/tools/checkBackgroundJob.ts @@ -0,0 +1,64 @@ +import { services } from "../services/index.js"; + +import { Tool } from "./types.js"; + +export const checkBackgroundJobTool: Tool = { + name: "CheckBackgroundJob", + displayName: "Check Background Job", + description: `Check the status and output of a background job. +Returns the current status, exit code (if finished), and all available output. +If the job is still running, returns partial output with "running" status.`, + parameters: { + type: "object", + required: ["job_id"], + properties: { + job_id: { + type: "string", + description: + "The ID of the background job to check (e.g., bg-1-1234567890)", + }, + }, + }, + readonly: true, + isBuiltIn: true, + run: async ({ job_id }: { job_id: string }): Promise => { + const job = services.backgroundJobs.getJob(job_id); + + if (!job) { + return JSON.stringify({ + error: `Job ${job_id} not found`, + available_jobs: services.backgroundJobs.getAllJobs().map((j) => ({ + id: j.id, + status: j.status, + command: + j.command.substring(0, 50) + (j.command.length > 50 ? "..." : ""), + })), + }); + } + + const result: Record = { + job_id: job.id, + status: job.status, + command: job.command, + output: job.output, + started_at: job.startTime.toISOString(), + }; + + if (job.endTime) { + result.ended_at = job.endTime.toISOString(); + result.duration_seconds = Math.round( + (job.endTime.getTime() - job.startTime.getTime()) / 1000, + ); + } + + if (job.exitCode !== null) { + result.exit_code = job.exitCode; + } + + if (job.error) { + result.error = job.error; + } + + return JSON.stringify(result, null, 2); + }, +}; diff --git a/extensions/cli/src/tools/index.tsx b/extensions/cli/src/tools/index.tsx index 20d37d6d365..eb10d870d62 100644 --- a/extensions/cli/src/tools/index.tsx +++ b/extensions/cli/src/tools/index.tsx @@ -19,6 +19,7 @@ import { telemetryService } from "../telemetry/telemetryService.js"; import { logger } from "../util/logger.js"; import { ALL_BUILT_IN_TOOLS } from "./allBuiltIns.js"; +import { checkBackgroundJobTool } from "./checkBackgroundJob.js"; import { editTool } from "./edit.js"; import { exitTool } from "./exit.js"; import { fetchTool } from "./fetch.js"; @@ -68,6 +69,7 @@ const BASE_BUILTIN_TOOLS: Tool[] = [ runTerminalCommandTool, fetchTool, writeChecklistTool, + checkBackgroundJobTool, ]; const BUILTIN_SEARCH_TOOLS: Tool[] = [searchCodeTool]; diff --git a/extensions/cli/src/tools/runTerminalCommand.ts b/extensions/cli/src/tools/runTerminalCommand.ts index 5b7f08b30fc..b6d8bb10f7f 100644 --- a/extensions/cli/src/tools/runTerminalCommand.ts +++ b/extensions/cli/src/tools/runTerminalCommand.ts @@ -1,4 +1,4 @@ -import { spawn } from "child_process"; +import { ChildProcess, spawn } from "child_process"; import fs from "fs"; import { @@ -6,11 +6,15 @@ import { type ToolPolicy, } from "@continuedev/terminal-security"; +import { backgroundJobService } from "../services/BackgroundJobService.js"; +import { services } from "../services/index.js"; import { telemetryService } from "../telemetry/telemetryService.js"; import { isGitCommitCommand, isPullRequestCommand, } from "../telemetry/utils.js"; +import { backgroundSignalManager } from "../util/backgroundSignalManager.js"; +import { emitBashToolEnded, emitBashToolStarted } from "../util/cli.js"; import { parseEnvNumber, truncateOutputFromStart, @@ -82,6 +86,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 = backgroundJobService.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 = backgroundJobService.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", @@ -151,7 +184,9 @@ IMPORTANT: To edit files, use Edit/MultiEdit tools instead of bash commands (sed const maxChars = Math.floor(baseMaxChars / parallelCount); const maxLines = Math.floor(baseMaxLines / parallelCount); - return new Promise((resolve, reject) => { + emitBashToolStarted(); + + const terminalOutput: string = await new Promise((resolve, reject) => { // Use same shell logic as core implementation const { shell, args } = getShellCommand(command); const child = spawn(shell, args); @@ -187,6 +222,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 = backgroundJobService.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); @@ -209,17 +272,33 @@ IMPORTANT: To edit files, use Edit/MultiEdit tools instead of bash commands (sed }, TIMEOUT_MS); }; + const showCurrentOutput = () => { + if (!context?.toolCallId) return; + try { + const currentOutput = stdout + (stderr ? `\nStderr: ${stderr}` : ""); + services.chatHistory.addToolResult( + context.toolCallId, + currentOutput, + "calling", + ); + } catch { + // Ignore errors during streaming updates + } + }; + // Start the initial timeout resetTimeout(); child.stdout.on("data", (data) => { stdout += data.toString(); resetTimeout(); + showCurrentOutput(); }); child.stderr.on("data", (data) => { stderr += data.toString(); resetTimeout(); + showCurrentOutput(); }); child.on("close", (code) => { @@ -230,6 +309,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,8 +351,13 @@ 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}`); }); }); + + emitBashToolEnded(); + + return terminalOutput; }, }; diff --git a/extensions/cli/src/ui/JobsSelector.tsx b/extensions/cli/src/ui/JobsSelector.tsx new file mode 100644 index 00000000000..f39136dde8f --- /dev/null +++ b/extensions/cli/src/ui/JobsSelector.tsx @@ -0,0 +1,201 @@ +import { Box, Text, useInput } from "ink"; +import React, { useEffect, useState } from "react"; + +import { + BackgroundJob, + backgroundJobService, +} from "../services/BackgroundJobService.js"; +import { truncateOutputFromStart } from "../util/truncateOutput.js"; + +import { defaultBoxStyles } from "./styles.js"; + +interface JobsSelectorProps { + onCancel: () => void; +} + +type ViewMode = "list" | "detail"; + +function getStatusColor(status: BackgroundJob["status"]): string { + if (status === "running" || status === "pending") return "yellow"; + if (status === "completed") return "green"; + return "red"; +} + +function formatDuration(job: BackgroundJob): string { + const endTime = job.endTime ? job.endTime.getTime() : Date.now(); + return `${Math.round((endTime - job.startTime.getTime()) / 1000)}s`; +} + +export function JobsSelector({ onCancel }: JobsSelectorProps) { + const [selectedIndex, setSelectedIndex] = useState(0); + const [viewMode, setViewMode] = useState("list"); + const [selectedJob, setSelectedJob] = useState(null); + const [jobs, setJobs] = useState(() => backgroundJobService.getAllJobs()); + + // Refetch the job details every 1 second + useEffect(() => { + const interval = setInterval(() => { + if (viewMode === "detail" && selectedJob) { + const freshJob = backgroundJobService.getJob(selectedJob.id); + if (freshJob) { + setSelectedJob(freshJob); + } + } else { + setJobs(backgroundJobService.getAllJobs()); + } + }, 1000); + + return () => clearInterval(interval); + }, []); + + useInput((input, key) => { + // Handle key input when viewing job details + if (viewMode === "detail") { + // Go back to list view + if (key.escape || input === "b") { + setViewMode("list"); + setSelectedJob(null); + return; + } + // Cancel the selected job if it's still active + if (input === "x" && selectedJob) { + const job = backgroundJobService.getJob(selectedJob.id); + if (job && (job.status === "running" || job.status === "pending")) { + backgroundJobService.cancelJob(selectedJob.id); + } + return; + } + return; + } + + // Handle key input when viewing job list + if (key.escape || (key.ctrl && input === "c")) { + onCancel(); + return; + } + + // Navigate up and down the job list + if (key.upArrow) { + setSelectedIndex((prev) => (prev > 0 ? prev - 1 : jobs.length - 1)); + return; + } + if (key.downArrow) { + setSelectedIndex((prev) => (prev < jobs.length - 1 ? prev + 1 : 0)); + return; + } + + // Select a job to view its details + if (key.return && jobs[selectedIndex]) { + setSelectedJob(jobs[selectedIndex]); + setViewMode("detail"); + return; + } + }); + + if (jobs.length === 0) { + return ( + + + Background Jobs + + + No background jobs + + Press Esc to go back + + ); + } + + if (viewMode === "detail" && selectedJob) { + const freshJob = selectedJob; + const { output: lastOutput } = truncateOutputFromStart(freshJob.output, { + maxLines: 10, + maxChars: 2000, + }); + + return ( + + + Job Details + + Press Esc/b to go back, x to cancel job + + + + ID: + {freshJob.id} + + + + Status: + {freshJob.status} + + + + Duration: + {formatDuration(freshJob)} + + + + Command: + {freshJob.command} + + + {freshJob.exitCode !== null && ( + + Exit Code: + + {freshJob.exitCode} + + + )} + + {freshJob.error && ( + + Error: + {freshJob.error} + + )} + + + Last 10 lines of output: + + {lastOutput || "(no output)"} + + + ); + } + + // List screen + return ( + + + Background Jobs ({jobs.length}) + + + ↑/↓ to navigate, Enter to view details, Esc to exit + + + + {jobs.map((job, index) => { + const isSelected = index === selectedIndex; + const prefix = isSelected ? "❯ " : " "; + + return ( + + {prefix} + + {job.status.padEnd(10)} + + {job.id} + ({formatDuration(job)}) + + {job.command.substring(0, 30)} + {job.command.length > 30 ? "..." : ""} + + + ); + })} + + ); +} diff --git a/extensions/cli/src/ui/TUIChat.tsx b/extensions/cli/src/ui/TUIChat.tsx index b20d4db122d..bedad8b3874 100644 --- a/extensions/cli/src/ui/TUIChat.tsx +++ b/extensions/cli/src/ui/TUIChat.tsx @@ -20,6 +20,7 @@ import { UpdateServiceState, } from "../services/types.js"; import { getTotalSessionCost } from "../session.js"; +import { bashToolEvents } from "../util/cli.js"; import { logger } from "../util/logger.js"; import { ActionStatus } from "./components/ActionStatus.js"; @@ -282,6 +283,7 @@ const TUIChat: React.FC = ({ onShowMCPSelector: () => navigateTo("mcp"), onShowUpdateSelector: () => navigateTo("update"), onShowSessionSelector: () => navigateTo("session"), + onShowJobsSelector: () => navigateTo("jobs"), onReload: handleReload, onClear: handleClear, onRefreshStatic: () => setStaticRefreshTrigger((prev) => prev + 1), @@ -341,6 +343,22 @@ const TUIChat: React.FC = ({ // Fetch organization name based on auth state const organizationName = useOrganizationName(services.auth?.organizationId); + // Track if a Bash tool is currently running via events + const [isBashToolRunning, setIsBashToolRunning] = useState(false); + + useEffect(() => { + const handleStarted = () => setIsBashToolRunning(true); + const handleEnded = () => setIsBashToolRunning(false); + + bashToolEvents.on("started", handleStarted); + bashToolEvents.on("ended", handleEnded); + + return () => { + bashToolEvents.off("started", handleStarted); + bashToolEvents.off("ended", handleEnded); + }; + }, []); + return ( {/* Chat history - takes up all available space above input */} @@ -379,6 +397,9 @@ const TUIChat: React.FC = ({ startTime={responseStartTime || 0} message="" showSpinner={true} + additionalHint={ + isBashToolRunning ? "ctrl+b to background" : undefined + } /> {/* Compaction Status */} diff --git a/extensions/cli/src/ui/components/ActionStatus.tsx b/extensions/cli/src/ui/components/ActionStatus.tsx index a12b3d6497c..84f17b313bc 100644 --- a/extensions/cli/src/ui/components/ActionStatus.tsx +++ b/extensions/cli/src/ui/components/ActionStatus.tsx @@ -11,6 +11,7 @@ interface ActionStatusProps { showSpinner?: boolean; color?: string; loadingColor?: string; + additionalHint?: string; } const ActionStatus: React.FC = ({ @@ -20,6 +21,7 @@ const ActionStatus: React.FC = ({ showSpinner = false, color = "dim", loadingColor = "green", + additionalHint, }) => { if (!visible) return null; @@ -29,7 +31,9 @@ const ActionStatus: React.FC = ({ {message} ( - • esc to interrupt ) + • esc to interrupt + {additionalHint && • {additionalHint}} + ) ); }; diff --git a/extensions/cli/src/ui/components/ScreenContent.tsx b/extensions/cli/src/ui/components/ScreenContent.tsx index 694c8f215c6..aff68f1ceb0 100644 --- a/extensions/cli/src/ui/components/ScreenContent.tsx +++ b/extensions/cli/src/ui/components/ScreenContent.tsx @@ -10,6 +10,7 @@ import { DiffViewer } from "../DiffViewer.js"; import { EditMessageSelector } from "../EditMessageSelector.js"; import { FreeTrialTransitionUI } from "../FreeTrialTransitionUI.js"; import type { ActivePermissionRequest } from "../hooks/useChat.types.js"; +import { JobsSelector } from "../JobsSelector.js"; import { MCPSelector } from "../MCPSelector.js"; import { ModelSelector } from "../ModelSelector.js"; import type { ConfigOption, ModelOption } from "../types/selectorTypes.js"; @@ -156,6 +157,11 @@ export const ScreenContent: React.FC = ({ ); } + // Jobs selector + if (isScreenActive("jobs")) { + return ; + } + // Free trial transition UI if (isScreenActive("free-trial")) { return ; diff --git a/extensions/cli/src/ui/context/NavigationContext.tsx b/extensions/cli/src/ui/context/NavigationContext.tsx index 6d43414dd93..04c1689f50d 100644 --- a/extensions/cli/src/ui/context/NavigationContext.tsx +++ b/extensions/cli/src/ui/context/NavigationContext.tsx @@ -21,6 +21,7 @@ export type NavigationScreen = | "diff" // Full-screen diff overlay | "update" // Update selector | "edit" // Edit message selector + | "jobs" // Background Jobs selector | "session"; // Session selector interface NavigationState { diff --git a/extensions/cli/src/ui/hooks/useChat.helpers.ts b/extensions/cli/src/ui/hooks/useChat.helpers.ts index a453b70668f..6eb49924d7e 100644 --- a/extensions/cli/src/ui/hooks/useChat.helpers.ts +++ b/extensions/cli/src/ui/hooks/useChat.helpers.ts @@ -51,6 +51,7 @@ interface ProcessSlashCommandResultOptions { onShowUpdateSelector?: () => void; onShowMCPSelector?: () => void; onShowSessionSelector?: () => void; + onShowJobsSelector?: () => void; onClear?: () => void; } @@ -66,6 +67,7 @@ export function processSlashCommandResult({ onShowModelSelector, onShowMCPSelector, onShowSessionSelector, + onShowJobsSelector, onClear, }: ProcessSlashCommandResultOptions): string | null { if (result.exit) { @@ -97,6 +99,11 @@ export function processSlashCommandResult({ return null; } + if (result.openJobsSelector && onShowJobsSelector) { + onShowJobsSelector(); + return null; + } + if (result.clear) { const systemMessage = chatHistory.find( (item) => item.message.role === "system", diff --git a/extensions/cli/src/ui/hooks/useChat.ts b/extensions/cli/src/ui/hooks/useChat.ts index fe3dc8d05b8..8147906840e 100644 --- a/extensions/cli/src/ui/hooks/useChat.ts +++ b/extensions/cli/src/ui/hooks/useChat.ts @@ -58,6 +58,7 @@ export function useChat({ onShowUpdateSelector, onShowMCPSelector, onShowSessionSelector, + onShowJobsSelector, onClear, onRefreshStatic, isRemoteMode = false, @@ -426,6 +427,7 @@ export function useChat({ onShowModelSelector, onShowMCPSelector, onShowSessionSelector, + onShowJobsSelector, onShowUpdateSelector, onClear, }); diff --git a/extensions/cli/src/ui/hooks/useChat.types.ts b/extensions/cli/src/ui/hooks/useChat.types.ts index 2fe0b0b81dc..ac2ecc5c657 100644 --- a/extensions/cli/src/ui/hooks/useChat.types.ts +++ b/extensions/cli/src/ui/hooks/useChat.types.ts @@ -18,6 +18,7 @@ export interface UseChatProps { onShowUpdateSelector: () => void; onShowModelSelector?: () => void; onShowSessionSelector?: () => void; + onShowJobsSelector?: () => void; onReload?: () => Promise; onClear?: () => void; onRefreshStatic?: () => void; @@ -63,6 +64,7 @@ export interface SlashCommandResult { openMcpSelector?: boolean; openUpdateSelector?: boolean; openSessionSelector?: boolean; + openJobsSelector?: boolean; compact?: boolean; diffContent?: string; } diff --git a/extensions/cli/src/ui/hooks/useUserInput.ts b/extensions/cli/src/ui/hooks/useUserInput.ts index 334d23fc304..229ac44f153 100644 --- a/extensions/cli/src/ui/hooks/useUserInput.ts +++ b/extensions/cli/src/ui/hooks/useUserInput.ts @@ -1,4 +1,5 @@ import type { PermissionMode } from "../../permissions/types.js"; +import { backgroundSignalManager } from "../../util/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) => { diff --git a/extensions/cli/src/util/backgroundSignalManager.ts b/extensions/cli/src/util/backgroundSignalManager.ts new file mode 100644 index 00000000000..1bfd4233374 --- /dev/null +++ b/extensions/cli/src/util/backgroundSignalManager.ts @@ -0,0 +1,10 @@ +import { EventEmitter } from "events"; + +// Event emitter to notify that running terminal command should be moved to background +class BackgroundSignalManager extends EventEmitter { + signalBackground(): void { + this.emit("backgroundRequested"); + } +} + +export const backgroundSignalManager = new BackgroundSignalManager(); diff --git a/extensions/cli/src/util/cli.ts b/extensions/cli/src/util/cli.ts index b8e359a1d7f..563b9405777 100644 --- a/extensions/cli/src/util/cli.ts +++ b/extensions/cli/src/util/cli.ts @@ -94,3 +94,13 @@ export function hasSuppliedPrompt(): boolean { } export const escapeEvents = new EventEmitter(); + +export const bashToolEvents = new EventEmitter(); + +export function emitBashToolStarted(): void { + bashToolEvents.emit("started"); +} + +export function emitBashToolEnded(): void { + bashToolEvents.emit("ended"); +} diff --git a/extensions/cli/src/util/exit.ts b/extensions/cli/src/util/exit.ts index a378cd8de64..17012d8228e 100644 --- a/extensions/cli/src/util/exit.ts +++ b/extensions/cli/src/util/exit.ts @@ -1,6 +1,7 @@ import type { ChatHistoryItem } from "core/index.js"; import { sentryService } from "../sentry.js"; +import { backgroundJobService } from "../services/BackgroundJobService.js"; import { getSessionUsage } from "../session.js"; import { telemetryService } from "../telemetry/telemetryService.js"; @@ -194,6 +195,16 @@ export async function gracefulExit(code: number = 0): Promise { // Display session usage breakdown in verbose mode displaySessionUsage(); + try { + const runningJobs = backgroundJobService.getRunningJobCount(); + if (runningJobs > 0) { + logger.debug(`Killing ${runningJobs} background job(s) on exit`); + backgroundJobService.killAllJobs(); + } + } catch (err) { + logger.debug("Background job cleanup error (ignored)", err as any); + } + try { // Flush metrics (forceFlush + shutdown inside service) await telemetryService.shutdown();