From 14c0989411a408c680404b7313382b54dee8ca07 Mon Sep 17 00:00:00 2001 From: Dax Date: Wed, 18 Feb 2026 19:29:11 -0500 Subject: [PATCH] refactor: migrate src/tool/read.ts from Bun.file() to Filesystem module (#14118) --- packages/opencode/src/tool/read.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/opencode/src/tool/read.ts b/packages/opencode/src/tool/read.ts index 80ca95900..c981ac16e 100644 --- a/packages/opencode/src/tool/read.ts +++ b/packages/opencode/src/tool/read.ts @@ -10,6 +10,7 @@ import DESCRIPTION from "./read.txt" import { Instance } from "../project/instance" import { assertExternalDirectory } from "./external-directory" import { InstructionPrompt } from "../session/instruction" +import { Filesystem } from "../util/filesystem" const DEFAULT_READ_LIMIT = 2000 const MAX_LINE_LENGTH = 2000 @@ -34,8 +35,7 @@ export const ReadTool = Tool.define("read", { } const title = path.relative(Instance.worktree, filepath) - const file = Bun.file(filepath) - const stat = await file.stat().catch(() => undefined) + const stat = Filesystem.stat(filepath) await assertExternalDirectory(ctx, filepath, { bypass: Boolean(ctx.extra?.["bypassCwdCheck"]), @@ -118,11 +118,10 @@ export const ReadTool = Tool.define("read", { const instructions = await InstructionPrompt.resolve(ctx.messages, filepath, ctx.messageID) // Exclude SVG (XML-based) and vnd.fastbidsheet (.fbs extension, commonly FlatBuffers schema files) - const isImage = - file.type.startsWith("image/") && file.type !== "image/svg+xml" && file.type !== "image/vnd.fastbidsheet" - const isPdf = file.type === "application/pdf" + const mime = Filesystem.mimeType(filepath) + const isImage = mime.startsWith("image/") && mime !== "image/svg+xml" && mime !== "image/vnd.fastbidsheet" + const isPdf = mime === "application/pdf" if (isImage || isPdf) { - const mime = file.type const msg = `${isImage ? "Image" : "PDF"} read successfully` return { title, @@ -136,13 +135,13 @@ export const ReadTool = Tool.define("read", { { type: "file", mime, - url: `data:${mime};base64,${Buffer.from(await file.bytes()).toString("base64")}`, + url: `data:${mime};base64,${Buffer.from(await Filesystem.readBytes(filepath)).toString("base64")}`, }, ], } } - const isBinary = await isBinaryFile(filepath, stat.size) + const isBinary = await isBinaryFile(filepath, Number(stat.size)) if (isBinary) throw new Error(`Cannot read binary file: ${filepath}`) const stream = createReadStream(filepath, { encoding: "utf8" })