feat: align TaskOutput UX with Bash output (#1029)
This commit is contained in:
@@ -7,6 +7,8 @@ interface GetTaskOutputArgs {
|
||||
block?: boolean;
|
||||
timeout?: number;
|
||||
filter?: string;
|
||||
onOutput?: (chunk: string, stream: "stdout" | "stderr") => void;
|
||||
runningMessageWhenNonBlocking?: boolean;
|
||||
}
|
||||
|
||||
interface GetTaskOutputResult {
|
||||
@@ -14,6 +16,74 @@ interface GetTaskOutputResult {
|
||||
status?: "running" | "completed" | "failed";
|
||||
}
|
||||
|
||||
const POLL_INTERVAL_MS = 100;
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
function emitNewProcessOutput(
|
||||
proc: typeof backgroundProcesses extends Map<string, infer V> ? V : never,
|
||||
onOutput: (chunk: string, stream: "stdout" | "stderr") => void,
|
||||
indexes: { stdout: number; stderr: number },
|
||||
filter?: string,
|
||||
): { stdout: number; stderr: number } {
|
||||
const next = { ...indexes };
|
||||
|
||||
if (proc.stdout.length > next.stdout) {
|
||||
const newStdoutLines = proc.stdout.slice(next.stdout);
|
||||
const filtered = filter
|
||||
? newStdoutLines.filter((line) => line.includes(filter))
|
||||
: newStdoutLines;
|
||||
if (filtered.length > 0) {
|
||||
onOutput(`${filtered.join("\n")}\n`, "stdout");
|
||||
}
|
||||
next.stdout = proc.stdout.length;
|
||||
}
|
||||
|
||||
if (proc.stderr.length > next.stderr) {
|
||||
const newStderrLines = proc.stderr.slice(next.stderr);
|
||||
const filtered = filter
|
||||
? newStderrLines.filter((line) => line.includes(filter))
|
||||
: newStderrLines;
|
||||
if (filtered.length > 0) {
|
||||
onOutput(`${filtered.join("\n")}\n`, "stderr");
|
||||
}
|
||||
next.stderr = proc.stderr.length;
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
function emitNewBackgroundTaskOutput(
|
||||
task: typeof backgroundTasks extends Map<string, infer V> ? V : never,
|
||||
onOutput: (chunk: string, stream: "stdout" | "stderr") => void,
|
||||
cursor: { outputIndex: number; emittedError?: string },
|
||||
filter?: string,
|
||||
): { outputIndex: number; emittedError?: string } {
|
||||
const next = { ...cursor };
|
||||
|
||||
if (task.output.length > next.outputIndex) {
|
||||
const newOutputLines = task.output.slice(next.outputIndex);
|
||||
const filtered = filter
|
||||
? newOutputLines.filter((line) => line.includes(filter))
|
||||
: newOutputLines;
|
||||
if (filtered.length > 0) {
|
||||
onOutput(`${filtered.join("\n")}\n`, "stdout");
|
||||
}
|
||||
next.outputIndex = task.output.length;
|
||||
}
|
||||
|
||||
if (task.error && task.error !== next.emittedError) {
|
||||
if (!filter || task.error.includes(filter)) {
|
||||
onOutput(`[error] ${task.error}\n`, "stderr");
|
||||
}
|
||||
next.emittedError = task.error;
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
/**
|
||||
* Core implementation for retrieving task/process output.
|
||||
* Used by both BashOutput (legacy) and TaskOutput (new).
|
||||
@@ -22,18 +92,41 @@ interface GetTaskOutputResult {
|
||||
export async function getTaskOutput(
|
||||
args: GetTaskOutputArgs,
|
||||
): Promise<GetTaskOutputResult> {
|
||||
const { task_id, block = false, timeout = 30000, filter } = args;
|
||||
const {
|
||||
task_id,
|
||||
block = false,
|
||||
timeout = 30000,
|
||||
filter,
|
||||
onOutput,
|
||||
runningMessageWhenNonBlocking = false,
|
||||
} = args;
|
||||
|
||||
// Check backgroundProcesses first (for Bash background commands)
|
||||
const proc = backgroundProcesses.get(task_id);
|
||||
if (proc) {
|
||||
return getProcessOutput(task_id, proc, block, timeout, filter);
|
||||
return getProcessOutput(
|
||||
task_id,
|
||||
proc,
|
||||
block,
|
||||
timeout,
|
||||
filter,
|
||||
onOutput,
|
||||
runningMessageWhenNonBlocking,
|
||||
);
|
||||
}
|
||||
|
||||
// Check backgroundTasks (for Task background subagents)
|
||||
const task = backgroundTasks.get(task_id);
|
||||
if (task) {
|
||||
return getBackgroundTaskOutput(task_id, task, block, timeout, filter);
|
||||
return getBackgroundTaskOutput(
|
||||
task_id,
|
||||
task,
|
||||
block,
|
||||
timeout,
|
||||
filter,
|
||||
onOutput,
|
||||
runningMessageWhenNonBlocking,
|
||||
);
|
||||
}
|
||||
|
||||
return { message: `No background process found with ID: ${task_id}` };
|
||||
@@ -48,22 +141,37 @@ async function getProcessOutput(
|
||||
block: boolean,
|
||||
timeout: number,
|
||||
filter?: string,
|
||||
onOutput?: (chunk: string, stream: "stdout" | "stderr") => void,
|
||||
runningMessageWhenNonBlocking?: boolean,
|
||||
): Promise<GetTaskOutputResult> {
|
||||
// If blocking, wait for process to complete (or timeout)
|
||||
// If blocking, wait for process to complete (or timeout) while streaming deltas.
|
||||
if (block && proc.status === "running") {
|
||||
const startTime = Date.now();
|
||||
await new Promise<void>((resolve) => {
|
||||
const checkInterval = setInterval(() => {
|
||||
const currentProc = backgroundProcesses.get(task_id);
|
||||
if (!currentProc || currentProc.status !== "running") {
|
||||
clearInterval(checkInterval);
|
||||
resolve();
|
||||
} else if (Date.now() - startTime >= timeout) {
|
||||
clearInterval(checkInterval);
|
||||
resolve();
|
||||
}
|
||||
}, 100); // Check every 100ms
|
||||
});
|
||||
let cursor = { stdout: 0, stderr: 0 };
|
||||
|
||||
if (onOutput) {
|
||||
cursor = emitNewProcessOutput(proc, onOutput, cursor, filter);
|
||||
}
|
||||
|
||||
while (Date.now() - startTime < timeout) {
|
||||
const currentProc = backgroundProcesses.get(task_id);
|
||||
if (!currentProc) break;
|
||||
|
||||
if (onOutput) {
|
||||
cursor = emitNewProcessOutput(currentProc, onOutput, cursor, filter);
|
||||
}
|
||||
|
||||
if (currentProc.status !== "running") {
|
||||
break;
|
||||
}
|
||||
|
||||
await sleep(POLL_INTERVAL_MS);
|
||||
}
|
||||
|
||||
const finalProc = backgroundProcesses.get(task_id);
|
||||
if (finalProc && onOutput) {
|
||||
emitNewProcessOutput(finalProc, onOutput, cursor, filter);
|
||||
}
|
||||
}
|
||||
|
||||
// Re-fetch in case status changed while waiting
|
||||
@@ -72,6 +180,14 @@ async function getProcessOutput(
|
||||
return { message: `Process ${task_id} no longer exists` };
|
||||
}
|
||||
|
||||
if (
|
||||
!block &&
|
||||
runningMessageWhenNonBlocking &&
|
||||
currentProc.status === "running"
|
||||
) {
|
||||
return { message: "Task is still running...", status: "running" };
|
||||
}
|
||||
|
||||
const stdout = currentProc.stdout.join("\n");
|
||||
const stderr = currentProc.stderr.join("\n");
|
||||
let text = stdout;
|
||||
@@ -109,22 +225,44 @@ async function getBackgroundTaskOutput(
|
||||
block: boolean,
|
||||
timeout: number,
|
||||
filter?: string,
|
||||
onOutput?: (chunk: string, stream: "stdout" | "stderr") => void,
|
||||
runningMessageWhenNonBlocking?: boolean,
|
||||
): Promise<GetTaskOutputResult> {
|
||||
// If blocking, wait for task to complete (or timeout)
|
||||
// If blocking, wait for task to complete (or timeout) while streaming deltas.
|
||||
if (block && task.status === "running") {
|
||||
const startTime = Date.now();
|
||||
await new Promise<void>((resolve) => {
|
||||
const checkInterval = setInterval(() => {
|
||||
const currentTask = backgroundTasks.get(task_id);
|
||||
if (!currentTask || currentTask.status !== "running") {
|
||||
clearInterval(checkInterval);
|
||||
resolve();
|
||||
} else if (Date.now() - startTime >= timeout) {
|
||||
clearInterval(checkInterval);
|
||||
resolve();
|
||||
}
|
||||
}, 100); // Check every 100ms
|
||||
});
|
||||
let cursor: { outputIndex: number; emittedError?: string } = {
|
||||
outputIndex: 0,
|
||||
};
|
||||
|
||||
if (onOutput) {
|
||||
cursor = emitNewBackgroundTaskOutput(task, onOutput, cursor, filter);
|
||||
}
|
||||
|
||||
while (Date.now() - startTime < timeout) {
|
||||
const currentTask = backgroundTasks.get(task_id);
|
||||
if (!currentTask) break;
|
||||
|
||||
if (onOutput) {
|
||||
cursor = emitNewBackgroundTaskOutput(
|
||||
currentTask,
|
||||
onOutput,
|
||||
cursor,
|
||||
filter,
|
||||
);
|
||||
}
|
||||
|
||||
if (currentTask.status !== "running") {
|
||||
break;
|
||||
}
|
||||
|
||||
await sleep(POLL_INTERVAL_MS);
|
||||
}
|
||||
|
||||
const finalTask = backgroundTasks.get(task_id);
|
||||
if (finalTask && onOutput) {
|
||||
emitNewBackgroundTaskOutput(finalTask, onOutput, cursor, filter);
|
||||
}
|
||||
}
|
||||
|
||||
// Re-fetch in case status changed while waiting
|
||||
@@ -133,6 +271,14 @@ async function getBackgroundTaskOutput(
|
||||
return { message: `Task ${task_id} no longer exists` };
|
||||
}
|
||||
|
||||
if (
|
||||
!block &&
|
||||
runningMessageWhenNonBlocking &&
|
||||
currentTask.status === "running"
|
||||
) {
|
||||
return { message: "Task is still running...", status: "running" };
|
||||
}
|
||||
|
||||
let text = currentTask.output.join("\n");
|
||||
if (currentTask.error) {
|
||||
text = text
|
||||
|
||||
Reference in New Issue
Block a user