mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-20 16:57:46 +00:00
update shell output at an interval to reduce flicker (#614)
This commit is contained in:
@@ -25,6 +25,8 @@ export interface ShellToolParams {
|
||||
}
|
||||
import { spawn } from 'child_process';
|
||||
|
||||
const OUTPUT_UPDATE_INTERVAL_MS = 1000;
|
||||
|
||||
export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
|
||||
static Name: string = 'execute_bash_command';
|
||||
private whitelist: Set<string> = new Set();
|
||||
@@ -124,7 +126,7 @@ export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
|
||||
async execute(
|
||||
params: ShellToolParams,
|
||||
abortSignal: AbortSignal,
|
||||
onOutputChunk?: (chunk: string) => void,
|
||||
updateOutput?: (output: string) => void,
|
||||
): Promise<ToolResult> {
|
||||
const validationError = this.validateToolParams(params);
|
||||
if (validationError) {
|
||||
@@ -155,6 +157,19 @@ export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
|
||||
let exited = false;
|
||||
let stdout = '';
|
||||
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) => {
|
||||
// continue to consume post-exit for background processes
|
||||
// removing listeners can overflow OS buffer and block subprocesses
|
||||
@@ -162,10 +177,7 @@ export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
|
||||
if (!exited) {
|
||||
const str = data.toString();
|
||||
stdout += str;
|
||||
output += str;
|
||||
if (onOutputChunk) {
|
||||
onOutputChunk(str);
|
||||
}
|
||||
appendOutput(str);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -174,10 +186,7 @@ export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
|
||||
if (!exited) {
|
||||
const str = data.toString();
|
||||
stderr += str;
|
||||
output += str;
|
||||
if (onOutputChunk) {
|
||||
onOutputChunk(str);
|
||||
}
|
||||
appendOutput(str);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ export interface Tool<
|
||||
execute(
|
||||
params: TParams,
|
||||
signal: AbortSignal,
|
||||
onOutputChunk?: (chunk: string) => void,
|
||||
updateOutput?: (output: string) => void,
|
||||
): Promise<TResult>;
|
||||
}
|
||||
|
||||
@@ -154,7 +154,7 @@ export abstract class BaseTool<
|
||||
abstract execute(
|
||||
params: TParams,
|
||||
signal: AbortSignal,
|
||||
onOutputChunk?: (chunk: string) => void,
|
||||
updateOutput?: (output: string) => void,
|
||||
): Promise<TResult>;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user