diff --git a/packages/core/src/tools/shell.ts b/packages/core/src/tools/shell.ts index a2d3b611c57..6f735d5b1e5 100644 --- a/packages/core/src/tools/shell.ts +++ b/packages/core/src/tools/shell.ts @@ -32,7 +32,7 @@ import type { ShellOutputEvent, } from '../services/shellExecutionService.js'; import { ShellExecutionService } from '../services/shellExecutionService.js'; -import { formatMemoryUsage } from '../utils/formatters.js'; +import { formatBytes } from '../utils/formatters.js'; import type { AnsiOutput } from '../utils/terminalSerializer.js'; import { getCommandRoots, @@ -220,7 +220,7 @@ export class ShellToolInvocation extends BaseToolInvocation< break; case 'binary_progress': isBinaryStream = true; - cumulativeOutput = `[Receiving binary output... ${formatMemoryUsage( + cumulativeOutput = `[Receiving binary output... ${formatBytes( event.bytesReceived, )} received]`; if (Date.now() - lastUpdateTime > OUTPUT_UPDATE_INTERVAL_MS) { diff --git a/packages/core/src/utils/formatters.test.ts b/packages/core/src/utils/formatters.test.ts index 305b2d0adb1..cc771685e15 100644 --- a/packages/core/src/utils/formatters.test.ts +++ b/packages/core/src/utils/formatters.test.ts @@ -6,7 +6,7 @@ import { describe, it, expect } from 'vitest'; -import { bytesToMB, formatMemoryUsage } from './formatters.js'; +import { bytesToMB, formatBytes } from './formatters.js'; describe('bytesToMB', () => { it('converts bytes to megabytes', () => { @@ -16,16 +16,21 @@ describe('bytesToMB', () => { }); }); -describe('formatMemoryUsage', () => { +describe('formatBytes', () => { + it('formats values below one kilobyte in B', () => { + expect(formatBytes(512)).toBe('512 B'); + expect(formatBytes(0)).toBe('0 B'); + }); + it('formats values below one megabyte in KB', () => { - expect(formatMemoryUsage(512 * 1024)).toBe('512.0 KB'); + expect(formatBytes(512 * 1024)).toBe('512.0 KB'); }); it('formats values below one gigabyte in MB', () => { - expect(formatMemoryUsage(5 * 1024 * 1024)).toBe('5.0 MB'); + expect(formatBytes(5 * 1024 * 1024)).toBe('5.0 MB'); }); it('formats values of one gigabyte or larger in GB', () => { - expect(formatMemoryUsage(2 * 1024 * 1024 * 1024)).toBe('2.00 GB'); + expect(formatBytes(2 * 1024 * 1024 * 1024)).toBe('2.00 GB'); }); }); diff --git a/packages/core/src/utils/formatters.ts b/packages/core/src/utils/formatters.ts index 88946ae910b..89871786b38 100644 --- a/packages/core/src/utils/formatters.ts +++ b/packages/core/src/utils/formatters.ts @@ -6,13 +6,15 @@ export const bytesToMB = (bytes: number): number => bytes / (1024 * 1024); -export const formatMemoryUsage = (bytes: number): string => { - const gb = bytes / (1024 * 1024 * 1024); +export const formatBytes = (bytes: number): string => { + if (bytes < 1024) { + return `${bytes} B`; + } if (bytes < 1024 * 1024) { return `${(bytes / 1024).toFixed(1)} KB`; } if (bytes < 1024 * 1024 * 1024) { return `${bytesToMB(bytes).toFixed(1)} MB`; } - return `${gb.toFixed(2)} GB`; + return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`; };