Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/in-memory-file-byte-sizes.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 10 additions & 3 deletions packages/core/src/workspace/filesystem/backends/in-memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
55 changes: 53 additions & 2 deletions packages/core/src/workspace/filesystem/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down