update shell output at an interval to reduce flicker (#614)

This commit is contained in:
Olcan 2025-05-30 00:02:30 -07:00 committed by GitHub
parent 2582c20e2a
commit b0aeeb53b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 22 deletions

View File

@ -16,6 +16,8 @@ import path from 'path';
import os from 'os'; import os from 'os';
import fs from 'fs'; import fs from 'fs';
const OUTPUT_UPDATE_INTERVAL_MS = 1000;
/** /**
* Hook to process shell commands (e.g., !ls, $pwd). * Hook to process shell commands (e.g., !ls, $pwd).
* Executes the command in the target directory and adds output/errors to history. * Executes the command in the target directory and adds output/errors to history.
@ -122,16 +124,20 @@ export const useShellCommandProcessor = (
let exited = false; let exited = false;
let output = ''; let output = '';
let lastUpdateTime = Date.now();
const handleOutput = (data: string) => { const handleOutput = (data: string) => {
// continue to consume post-exit for background processes // continue to consume post-exit for background processes
// removing listeners can overflow OS buffer and block subprocesses // removing listeners can overflow OS buffer and block subprocesses
// destroying (e.g. child.stdout.destroy()) can terminate subprocesses via SIGPIPE // destroying (e.g. child.stdout.destroy()) can terminate subprocesses via SIGPIPE
if (!exited) { if (!exited) {
output += data; output += data;
if (Date.now() - lastUpdateTime > OUTPUT_UPDATE_INTERVAL_MS) {
setPendingHistoryItem({ setPendingHistoryItem({
type: 'info', type: 'info',
text: output, text: output,
}); });
lastUpdateTime = Date.now();
}
} }
}; };
child.stdout.on('data', handleOutput); child.stdout.on('data', handleOutput);

View File

@ -288,11 +288,9 @@ export function useToolScheduler(
const callId = t.request.callId; const callId = t.request.callId;
setToolCalls(setStatus(t.request.callId, 'executing')); setToolCalls(setStatus(t.request.callId, 'executing'));
let accumulatedOutput = ''; const updateOutput =
const onOutputChunk =
t.tool.name === 'execute_bash_command' t.tool.name === 'execute_bash_command'
? (chunk: string) => { ? (output: string) => {
accumulatedOutput += chunk;
setPendingHistoryItem( setPendingHistoryItem(
(prevItem: HistoryItemWithoutId | null) => { (prevItem: HistoryItemWithoutId | null) => {
if (prevItem?.type === 'tool_group') { if (prevItem?.type === 'tool_group') {
@ -304,7 +302,7 @@ export function useToolScheduler(
toolDisplay.status === ToolCallStatus.Executing toolDisplay.status === ToolCallStatus.Executing
? { ? {
...toolDisplay, ...toolDisplay,
resultDisplay: accumulatedOutput, resultDisplay: output,
} }
: toolDisplay, : toolDisplay,
), ),
@ -319,7 +317,7 @@ export function useToolScheduler(
setToolCalls((prevToolCalls) => setToolCalls((prevToolCalls) =>
prevToolCalls.map((tc) => prevToolCalls.map((tc) =>
tc.request.callId === callId && tc.status === 'executing' tc.request.callId === callId && tc.status === 'executing'
? { ...tc, liveOutput: accumulatedOutput } ? { ...tc, liveOutput: output }
: tc, : tc,
), ),
); );
@ -327,7 +325,7 @@ export function useToolScheduler(
: undefined; : undefined;
t.tool t.tool
.execute(t.request.args, signal, onOutputChunk) .execute(t.request.args, signal, updateOutput)
.then((result: ToolResult) => { .then((result: ToolResult) => {
if (signal.aborted) { if (signal.aborted) {
// TODO(jacobr): avoid stringifying the LLM content. // TODO(jacobr): avoid stringifying the LLM content.

View File

@ -25,6 +25,8 @@ export interface ShellToolParams {
} }
import { spawn } from 'child_process'; import { spawn } from 'child_process';
const OUTPUT_UPDATE_INTERVAL_MS = 1000;
export class ShellTool extends BaseTool<ShellToolParams, ToolResult> { export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
static Name: string = 'execute_bash_command'; static Name: string = 'execute_bash_command';
private whitelist: Set<string> = new Set(); private whitelist: Set<string> = new Set();
@ -124,7 +126,7 @@ export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
async execute( async execute(
params: ShellToolParams, params: ShellToolParams,
abortSignal: AbortSignal, abortSignal: AbortSignal,
onOutputChunk?: (chunk: string) => void, updateOutput?: (output: string) => void,
): Promise<ToolResult> { ): Promise<ToolResult> {
const validationError = this.validateToolParams(params); const validationError = this.validateToolParams(params);
if (validationError) { if (validationError) {
@ -155,6 +157,19 @@ export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
let exited = false; let exited = false;
let stdout = ''; let stdout = '';
let output = ''; let output = '';
let lastUpdateTime = Date.now();
const appendOutput = (str: string) => {
output += str;
if (
updateOutput &&
Date.now() - lastUpdateTime > OUTPUT_UPDATE_INTERVAL_MS
) {
updateOutput(output);
lastUpdateTime = Date.now();
}
};
shell.stdout.on('data', (data: Buffer) => { shell.stdout.on('data', (data: Buffer) => {
// continue to consume post-exit for background processes // continue to consume post-exit for background processes
// removing listeners can overflow OS buffer and block subprocesses // removing listeners can overflow OS buffer and block subprocesses
@ -162,10 +177,7 @@ export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
if (!exited) { if (!exited) {
const str = data.toString(); const str = data.toString();
stdout += str; stdout += str;
output += str; appendOutput(str);
if (onOutputChunk) {
onOutputChunk(str);
}
} }
}); });
@ -174,10 +186,7 @@ export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
if (!exited) { if (!exited) {
const str = data.toString(); const str = data.toString();
stderr += str; stderr += str;
output += str; appendOutput(str);
if (onOutputChunk) {
onOutputChunk(str);
}
} }
}); });

View File

@ -68,7 +68,7 @@ export interface Tool<
execute( execute(
params: TParams, params: TParams,
signal: AbortSignal, signal: AbortSignal,
onOutputChunk?: (chunk: string) => void, updateOutput?: (output: string) => void,
): Promise<TResult>; ): Promise<TResult>;
} }
@ -154,7 +154,7 @@ export abstract class BaseTool<
abstract execute( abstract execute(
params: TParams, params: TParams,
signal: AbortSignal, signal: AbortSignal,
onOutputChunk?: (chunk: string) => void, updateOutput?: (output: string) => void,
): Promise<TResult>; ): Promise<TResult>;
} }