refactor: migrate src/tool/read.ts from Bun.file() to Filesystem module (#14118)
This commit is contained in:
@@ -10,6 +10,7 @@ import DESCRIPTION from "./read.txt"
|
|||||||
import { Instance } from "../project/instance"
|
import { Instance } from "../project/instance"
|
||||||
import { assertExternalDirectory } from "./external-directory"
|
import { assertExternalDirectory } from "./external-directory"
|
||||||
import { InstructionPrompt } from "../session/instruction"
|
import { InstructionPrompt } from "../session/instruction"
|
||||||
|
import { Filesystem } from "../util/filesystem"
|
||||||
|
|
||||||
const DEFAULT_READ_LIMIT = 2000
|
const DEFAULT_READ_LIMIT = 2000
|
||||||
const MAX_LINE_LENGTH = 2000
|
const MAX_LINE_LENGTH = 2000
|
||||||
@@ -34,8 +35,7 @@ export const ReadTool = Tool.define("read", {
|
|||||||
}
|
}
|
||||||
const title = path.relative(Instance.worktree, filepath)
|
const title = path.relative(Instance.worktree, filepath)
|
||||||
|
|
||||||
const file = Bun.file(filepath)
|
const stat = Filesystem.stat(filepath)
|
||||||
const stat = await file.stat().catch(() => undefined)
|
|
||||||
|
|
||||||
await assertExternalDirectory(ctx, filepath, {
|
await assertExternalDirectory(ctx, filepath, {
|
||||||
bypass: Boolean(ctx.extra?.["bypassCwdCheck"]),
|
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)
|
const instructions = await InstructionPrompt.resolve(ctx.messages, filepath, ctx.messageID)
|
||||||
|
|
||||||
// Exclude SVG (XML-based) and vnd.fastbidsheet (.fbs extension, commonly FlatBuffers schema files)
|
// Exclude SVG (XML-based) and vnd.fastbidsheet (.fbs extension, commonly FlatBuffers schema files)
|
||||||
const isImage =
|
const mime = Filesystem.mimeType(filepath)
|
||||||
file.type.startsWith("image/") && file.type !== "image/svg+xml" && file.type !== "image/vnd.fastbidsheet"
|
const isImage = mime.startsWith("image/") && mime !== "image/svg+xml" && mime !== "image/vnd.fastbidsheet"
|
||||||
const isPdf = file.type === "application/pdf"
|
const isPdf = mime === "application/pdf"
|
||||||
if (isImage || isPdf) {
|
if (isImage || isPdf) {
|
||||||
const mime = file.type
|
|
||||||
const msg = `${isImage ? "Image" : "PDF"} read successfully`
|
const msg = `${isImage ? "Image" : "PDF"} read successfully`
|
||||||
return {
|
return {
|
||||||
title,
|
title,
|
||||||
@@ -136,13 +135,13 @@ export const ReadTool = Tool.define("read", {
|
|||||||
{
|
{
|
||||||
type: "file",
|
type: "file",
|
||||||
mime,
|
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}`)
|
if (isBinary) throw new Error(`Cannot read binary file: ${filepath}`)
|
||||||
|
|
||||||
const stream = createReadStream(filepath, { encoding: "utf8" })
|
const stream = createReadStream(filepath, { encoding: "utf8" })
|
||||||
|
|||||||
Reference in New Issue
Block a user