@@ -29,6 +29,7 @@ export interface ShellOutput {
2929 exitCode: number;
3030 success: boolean;
3131 timedOut: boolean;
32+ streamedOutput: boolean;
3233 }
3334
3435 const MAX_OUTPUT_BYTES = 10_000; // 10KB per stream
@@ -121,19 +122,21 @@ export const shellTool: ToolDef<ShellInput, ShellOutput> = {
121122 let stdout = "";
122123 let stderr = "";
123124 let exitCode = 1;
125+ const onOutput = input.onOutput;
126+ const hasOutputHandler = typeof onOutput === "function";
124127 let streamedAnyOutput = false;
125128 let streamedEndsWithNewline = true;
126129 const emitOutput = (chunk: string): void => {
127- if (!chunk) return;
130+ if (!chunk || !hasOutputHandler) return;
128131 streamedAnyOutput = true;
129132 streamedEndsWithNewline = /[\r\n]$/.test(chunk);
130- input.onOutput?.(chunk);
133+ onOutput(chunk);
131134 };
132135
133136 try {
134137 [stdout, stderr] = await Promise.all([
135- collectStream(proc.stdout, emitOutput),
136- collectStream(proc.stderr, emitOutput),
138+ collectStream(proc.stdout, hasOutputHandler ? emitOutput : undefined),
139+ collectStream(proc.stderr, hasOutputHandler ? emitOutput : undefined),
137140 ]);
138141 if (streamedAnyOutput && !streamedEndsWithNewline) {
139142 emitOutput("\n");
@@ -160,6 +163,7 @@ export const shellTool: ToolDef<ShellInput, ShellOutput> = {
160163 exitCode,
161164 success: exitCode === 0,
162165 timedOut,
166+ streamedOutput: streamedAnyOutput,
163167 };
164168 },
165169 };