refactor: migrate src/session/prompt.ts from Bun.file() to Filesystem/stat modules (#14128)
This commit is contained in:
@@ -2,6 +2,7 @@ import path from "path"
|
|||||||
import os from "os"
|
import os from "os"
|
||||||
import fs from "fs/promises"
|
import fs from "fs/promises"
|
||||||
import z from "zod"
|
import z from "zod"
|
||||||
|
import { Filesystem } from "../util/filesystem"
|
||||||
import { Identifier } from "../id/id"
|
import { Identifier } from "../id/id"
|
||||||
import { MessageV2 } from "./message-v2"
|
import { MessageV2 } from "./message-v2"
|
||||||
import { Log } from "../util/log"
|
import { Log } from "../util/log"
|
||||||
@@ -1082,11 +1083,9 @@ export namespace SessionPrompt {
|
|||||||
// have to normalize, symbol search returns absolute paths
|
// have to normalize, symbol search returns absolute paths
|
||||||
// Decode the pathname since URL constructor doesn't automatically decode it
|
// Decode the pathname since URL constructor doesn't automatically decode it
|
||||||
const filepath = fileURLToPath(part.url)
|
const filepath = fileURLToPath(part.url)
|
||||||
const stat = await Bun.file(filepath)
|
const s = Filesystem.stat(filepath)
|
||||||
.stat()
|
|
||||||
.catch(() => undefined)
|
|
||||||
|
|
||||||
if (stat?.isDirectory()) {
|
if (s?.isDirectory()) {
|
||||||
part.mime = "application/x-directory"
|
part.mime = "application/x-directory"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1233,14 +1232,13 @@ export namespace SessionPrompt {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
const file = Bun.file(filepath)
|
|
||||||
FileTime.read(input.sessionID, filepath)
|
FileTime.read(input.sessionID, filepath)
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
messageID: info.id,
|
messageID: info.id,
|
||||||
sessionID: input.sessionID,
|
sessionID: input.sessionID,
|
||||||
type: "text",
|
type: "text",
|
||||||
text: `Called the Read tool with the following input: {\"filePath\":\"${filepath}\"}`,
|
text: `Called the Read tool with the following input: {"filePath":"${filepath}"}`,
|
||||||
synthetic: true,
|
synthetic: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -1248,7 +1246,7 @@ export namespace SessionPrompt {
|
|||||||
messageID: info.id,
|
messageID: info.id,
|
||||||
sessionID: input.sessionID,
|
sessionID: input.sessionID,
|
||||||
type: "file",
|
type: "file",
|
||||||
url: `data:${part.mime};base64,` + Buffer.from(await file.bytes()).toString("base64"),
|
url: `data:${part.mime};base64,` + (await Filesystem.readBytes(filepath)).toString("base64"),
|
||||||
mime: part.mime,
|
mime: part.mime,
|
||||||
filename: part.filename!,
|
filename: part.filename!,
|
||||||
source: part.source,
|
source: part.source,
|
||||||
@@ -1354,7 +1352,7 @@ export namespace SessionPrompt {
|
|||||||
// Switching from plan mode to build mode
|
// Switching from plan mode to build mode
|
||||||
if (input.agent.name !== "plan" && assistantMessage?.info.agent === "plan") {
|
if (input.agent.name !== "plan" && assistantMessage?.info.agent === "plan") {
|
||||||
const plan = Session.plan(input.session)
|
const plan = Session.plan(input.session)
|
||||||
const exists = await Bun.file(plan).exists()
|
const exists = await Filesystem.exists(plan)
|
||||||
if (exists) {
|
if (exists) {
|
||||||
const part = await Session.updatePart({
|
const part = await Session.updatePart({
|
||||||
id: Identifier.ascending("part"),
|
id: Identifier.ascending("part"),
|
||||||
@@ -1373,7 +1371,7 @@ export namespace SessionPrompt {
|
|||||||
// Entering plan mode
|
// Entering plan mode
|
||||||
if (input.agent.name === "plan" && assistantMessage?.info.agent !== "plan") {
|
if (input.agent.name === "plan" && assistantMessage?.info.agent !== "plan") {
|
||||||
const plan = Session.plan(input.session)
|
const plan = Session.plan(input.session)
|
||||||
const exists = await Bun.file(plan).exists()
|
const exists = await Filesystem.exists(plan)
|
||||||
if (!exists) await fs.mkdir(path.dirname(plan), { recursive: true })
|
if (!exists) await fs.mkdir(path.dirname(plan), { recursive: true })
|
||||||
const part = await Session.updatePart({
|
const part = await Session.updatePart({
|
||||||
id: Identifier.ascending("part"),
|
id: Identifier.ascending("part"),
|
||||||
|
|||||||
Reference in New Issue
Block a user