diff --git a/.changeset/in-memory-file-byte-sizes.md b/.changeset/in-memory-file-byte-sizes.md new file mode 100644 index 000000000..603a8bd9d --- /dev/null +++ b/.changeset/in-memory-file-byte-sizes.md @@ -0,0 +1,5 @@ +--- +"@voltagent/core": patch +--- + +Report UTF-8 byte sizes for in-memory workspace files in stat, directory listings, and glob results, matching the disk backend for non-ASCII content. diff --git a/packages/core/src/workspace/filesystem/backends/in-memory.ts b/packages/core/src/workspace/filesystem/backends/in-memory.ts index b0a88406c..4f388829f 100644 --- a/packages/core/src/workspace/filesystem/backends/in-memory.ts +++ b/packages/core/src/workspace/filesystem/backends/in-memory.ts @@ -38,6 +38,13 @@ export class InMemoryFilesystemBackend implements FilesystemBackend { return this.directories; } + private getFileSize(fileData: FileData): number { + const content = fileDataToString(fileData); + return typeof Buffer !== "undefined" + ? Buffer.byteLength(content, "utf8") + : new TextEncoder().encode(content).byteLength; + } + private normalizeDirPath(path: string): string { const trimmed = path.trim(); if (!trimmed) { @@ -81,7 +88,7 @@ export class InMemoryFilesystemBackend implements FilesystemBackend { continue; } - const size = fd.content.join("\n").length; + const size = this.getFileSize(fd); infos.push({ path: filePath, is_dir: false, @@ -153,7 +160,7 @@ export class InMemoryFilesystemBackend implements FilesystemBackend { const fileData = files[filePath]; if (fileData) { - const size = fileData.content.join("\n").length; + const size = this.getFileSize(fileData); return { path: filePath, is_dir: false, @@ -285,7 +292,7 @@ export class InMemoryFilesystemBackend implements FilesystemBackend { const infos: FileInfo[] = []; for (const filePath of paths) { const fd = files[filePath]; - const size = fd ? fd.content.join("\n").length : 0; + const size = fd ? this.getFileSize(fd) : 0; infos.push({ path: filePath, is_dir: false, diff --git a/packages/core/src/workspace/filesystem/index.spec.ts b/packages/core/src/workspace/filesystem/index.spec.ts index 173949219..388864235 100644 --- a/packages/core/src/workspace/filesystem/index.spec.ts +++ b/packages/core/src/workspace/filesystem/index.spec.ts @@ -1,7 +1,58 @@ -import { describe, expect, it } from "vitest"; -import { WorkspaceFilesystem } from "./index"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { describe, expect, it, vi } from "vitest"; +import { NodeFilesystemBackend, WorkspaceFilesystem } from "./index"; describe("WorkspaceFilesystem", () => { + it("reports byte sizes when Buffer is unavailable", async () => { + const filesystem = new WorkspaceFilesystem(); + await filesystem.write("/content.txt", "你好"); + vi.stubGlobal("Buffer", undefined); + try { + expect((await filesystem.stat("/content.txt"))?.size).toBe(6); + expect((await filesystem.lsInfo("/"))[0].size).toBe(6); + expect((await filesystem.globInfo("*.txt", "/"))[0].size).toBe(6); + } finally { + vi.unstubAllGlobals(); + } + }); + + describe.each([ + ["ASCII", "hello", 5], + ["empty", "", 0], + ["Chinese", "你好", 6], + ["emoji and newline", "a\n😀", 6], + ["combining mark", "e\u0301", 3], + ] as const)("UTF-8 byte sizes for %s files", (_label, content, expectedSize) => { + it.each(["stat", "lsInfo", "globInfo"] as const)( + "reports byte sizes via %s", + async (operation) => { + const root = await mkdtemp(path.join(tmpdir(), "va-byte-size-")); + try { + const memory = new WorkspaceFilesystem(); + const disk = new WorkspaceFilesystem({ + backend: new NodeFilesystemBackend({ rootDir: root }), + }); + for (const filesystem of [disk, memory]) { + const result = await filesystem.write("/content.txt", content); + expect(result.error).toBeUndefined(); + expect((await filesystem.readRaw("/content.txt")).content.join("\n")).toBe(content); + const info = + operation === "stat" + ? await filesystem.stat("/content.txt") + : operation === "lsInfo" + ? (await filesystem.lsInfo("/"))[0] + : (await filesystem.globInfo("*.txt", "/"))[0]; + expect(info?.size).toBe(expectedSize); + } + } finally { + await rm(root, { recursive: true, force: true }); + } + }, + ); + }); + it("writes, reads, edits, and deletes files", async () => { const filesystem = new WorkspaceFilesystem();