diff --git a/extensions/cli/src/services/BackgroundJobManager.ts b/extensions/cli/src/services/BackgroundJobManager.ts index 720737746..fd0ea7c42 100644 --- a/extensions/cli/src/services/BackgroundJobManager.ts +++ b/extensions/cli/src/services/BackgroundJobManager.ts @@ -144,6 +144,9 @@ export class BackgroundJobManager { 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(); diff --git a/extensions/cli/src/slashCommands.ts b/extensions/cli/src/slashCommands.ts index d00b82bfb..07c659d59 100644 --- a/extensions/cli/src/slashCommands.ts +++ b/extensions/cli/src/slashCommands.ts @@ -9,7 +9,6 @@ import { import { getAllSlashCommands } from "./commands/commands.js"; import { handleInit } from "./commands/init.js"; import { handleInfoSlashCommand } from "./infoScreen.js"; -import { backgroundJobManager } from "./services/BackgroundJobManager.js"; import { reloadService, SERVICE_NAMES, services } from "./services/index.js"; import { getCurrentSession, updateSessionTitle } from "./session.js"; import { posthogService } from "./telemetry/posthogService.js"; @@ -171,33 +170,7 @@ function handleTitle(args: string[]) { } function handleJobs() { - const allJobs = backgroundJobManager.getAllJobs(); - if (allJobs.length === 0) { - return { - exit: false, - output: chalk.dim("No background jobs"), - }; - } - - const lines = [chalk.bold("Background Jobs:")]; - for (const job of allJobs) { - const statusColor = - job.status === "running" || job.status === "pending" - ? chalk.yellow - : job.status === "completed" - ? chalk.green - : chalk.red; - const duration = job.endTime - ? `${Math.round((job.endTime.getTime() - job.startTime.getTime()) / 1000)}s` - : `${Math.round((Date.now() - job.startTime.getTime()) / 1000)}s`; - lines.push( - ` ${statusColor(job.status.padEnd(10))} ${chalk.cyan(job.id)} ${chalk.dim(`(${duration})`)} ${job.command.substring(0, 40)}${job.command.length > 40 ? "..." : ""}`, - ); - } - return { - exit: false, - output: lines.join("\n"), - }; + return { openJobsSelector: true }; } const commandHandlers: Record = { diff --git a/extensions/cli/src/ui/JobsSelector.tsx b/extensions/cli/src/ui/JobsSelector.tsx new file mode 100644 index 000000000..c0ddfbca6 --- /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, + backgroundJobManager, +} from "../services/BackgroundJobManager.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(() => backgroundJobManager.getAllJobs()); + + // Refetch the job details every 1 second + useEffect(() => { + const interval = setInterval(() => { + if (viewMode === "detail" && selectedJob) { + const freshJob = backgroundJobManager.getJob(selectedJob.id); + if (freshJob) { + setSelectedJob(freshJob); + } + } else { + setJobs(backgroundJobManager.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 = backgroundJobManager.getJob(selectedJob.id); + if (job && (job.status === "running" || job.status === "pending")) { + backgroundJobManager.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 f28b20a5b..bedad8b38 100644 --- a/extensions/cli/src/ui/TUIChat.tsx +++ b/extensions/cli/src/ui/TUIChat.tsx @@ -283,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), diff --git a/extensions/cli/src/ui/components/ScreenContent.tsx b/extensions/cli/src/ui/components/ScreenContent.tsx index 694c8f215..aff68f1ce 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 6d43414dd..04c1689f5 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 a453b7066..6eb49924d 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 fe3dc8d05..814790684 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 2fe0b0b81..ac2ecc5c6 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; }