fix(app): reactive file tree

This commit is contained in:
adamelmore
2026-01-26 16:46:01 -06:00
parent b07d7cdb71
commit 021d9d105e
4 changed files with 35 additions and 7 deletions

View File

@@ -571,6 +571,18 @@ export const { use: useFile, provider: FileProvider } = createSimpleContext({
} }
const kind = event.properties.event const kind = event.properties.event
if (kind === "change") {
const dir = (() => {
if (path === "") return ""
const node = tree.node[path]
if (node?.type !== "directory") return
return path
})()
if (dir === undefined) return
if (!tree.dir[dir]?.loaded) return
listDir(dir, { force: true })
return
}
if (kind !== "add" && kind !== "unlink") return if (kind !== "add" && kind !== "unlink") return
const parent = path.split("/").slice(0, -1).join("/") const parent = path.split("/").slice(0, -1).join("/")

View File

@@ -185,7 +185,7 @@ export const ApplyPatchTool = Tool.define("apply_patch", {
}) })
// Apply the changes // Apply the changes
const changedFiles: string[] = [] const updates: Array<{ file: string; event: "add" | "change" | "unlink" }> = []
for (const change of fileChanges) { for (const change of fileChanges) {
const edited = change.type === "delete" ? undefined : (change.movePath ?? change.filePath) const edited = change.type === "delete" ? undefined : (change.movePath ?? change.filePath)
@@ -194,12 +194,12 @@ export const ApplyPatchTool = Tool.define("apply_patch", {
// Create parent directories (recursive: true is safe on existing/root dirs) // Create parent directories (recursive: true is safe on existing/root dirs)
await fs.mkdir(path.dirname(change.filePath), { recursive: true }) await fs.mkdir(path.dirname(change.filePath), { recursive: true })
await fs.writeFile(change.filePath, change.newContent, "utf-8") await fs.writeFile(change.filePath, change.newContent, "utf-8")
changedFiles.push(change.filePath) updates.push({ file: change.filePath, event: "add" })
break break
case "update": case "update":
await fs.writeFile(change.filePath, change.newContent, "utf-8") await fs.writeFile(change.filePath, change.newContent, "utf-8")
changedFiles.push(change.filePath) updates.push({ file: change.filePath, event: "change" })
break break
case "move": case "move":
@@ -208,13 +208,14 @@ export const ApplyPatchTool = Tool.define("apply_patch", {
await fs.mkdir(path.dirname(change.movePath), { recursive: true }) await fs.mkdir(path.dirname(change.movePath), { recursive: true })
await fs.writeFile(change.movePath, change.newContent, "utf-8") await fs.writeFile(change.movePath, change.newContent, "utf-8")
await fs.unlink(change.filePath) await fs.unlink(change.filePath)
changedFiles.push(change.movePath) updates.push({ file: change.filePath, event: "unlink" })
updates.push({ file: change.movePath, event: "add" })
} }
break break
case "delete": case "delete":
await fs.unlink(change.filePath) await fs.unlink(change.filePath)
changedFiles.push(change.filePath) updates.push({ file: change.filePath, event: "unlink" })
break break
} }
@@ -226,8 +227,8 @@ export const ApplyPatchTool = Tool.define("apply_patch", {
} }
// Publish file change events // Publish file change events
for (const filePath of changedFiles) { for (const update of updates) {
await Bus.publish(FileWatcher.Event.Updated, { file: filePath, event: "change" }) await Bus.publish(FileWatcher.Event.Updated, update)
} }
// Notify LSP of file changes and collect diagnostics // Notify LSP of file changes and collect diagnostics

View File

@@ -10,6 +10,7 @@ import { LSP } from "../lsp"
import { createTwoFilesPatch, diffLines } from "diff" import { createTwoFilesPatch, diffLines } from "diff"
import DESCRIPTION from "./edit.txt" import DESCRIPTION from "./edit.txt"
import { File } from "../file" import { File } from "../file"
import { FileWatcher } from "../file/watcher"
import { Bus } from "../bus" import { Bus } from "../bus"
import { FileTime } from "../file/time" import { FileTime } from "../file/time"
import { Filesystem } from "../util/filesystem" import { Filesystem } from "../util/filesystem"
@@ -48,6 +49,7 @@ export const EditTool = Tool.define("edit", {
let contentNew = "" let contentNew = ""
await FileTime.withLock(filePath, async () => { await FileTime.withLock(filePath, async () => {
if (params.oldString === "") { if (params.oldString === "") {
const existed = await Bun.file(filePath).exists()
contentNew = params.newString contentNew = params.newString
diff = trimDiff(createTwoFilesPatch(filePath, filePath, contentOld, contentNew)) diff = trimDiff(createTwoFilesPatch(filePath, filePath, contentOld, contentNew))
await ctx.ask({ await ctx.ask({
@@ -63,6 +65,10 @@ export const EditTool = Tool.define("edit", {
await Bus.publish(File.Event.Edited, { await Bus.publish(File.Event.Edited, {
file: filePath, file: filePath,
}) })
await Bus.publish(FileWatcher.Event.Updated, {
file: filePath,
event: existed ? "change" : "add",
})
FileTime.read(ctx.sessionID, filePath) FileTime.read(ctx.sessionID, filePath)
return return
} }
@@ -92,6 +98,10 @@ export const EditTool = Tool.define("edit", {
await Bus.publish(File.Event.Edited, { await Bus.publish(File.Event.Edited, {
file: filePath, file: filePath,
}) })
await Bus.publish(FileWatcher.Event.Updated, {
file: filePath,
event: "change",
})
contentNew = await file.text() contentNew = await file.text()
diff = trimDiff( diff = trimDiff(
createTwoFilesPatch(filePath, filePath, normalizeLineEndings(contentOld), normalizeLineEndings(contentNew)), createTwoFilesPatch(filePath, filePath, normalizeLineEndings(contentOld), normalizeLineEndings(contentNew)),

View File

@@ -6,6 +6,7 @@ import { createTwoFilesPatch } from "diff"
import DESCRIPTION from "./write.txt" import DESCRIPTION from "./write.txt"
import { Bus } from "../bus" import { Bus } from "../bus"
import { File } from "../file" import { File } from "../file"
import { FileWatcher } from "../file/watcher"
import { FileTime } from "../file/time" import { FileTime } from "../file/time"
import { Filesystem } from "../util/filesystem" import { Filesystem } from "../util/filesystem"
import { Instance } from "../project/instance" import { Instance } from "../project/instance"
@@ -45,6 +46,10 @@ export const WriteTool = Tool.define("write", {
await Bus.publish(File.Event.Edited, { await Bus.publish(File.Event.Edited, {
file: filepath, file: filepath,
}) })
await Bus.publish(FileWatcher.Event.Updated, {
file: filepath,
event: exists ? "change" : "add",
})
FileTime.read(ctx.sessionID, filepath) FileTime.read(ctx.sessionID, filepath)
let output = "Wrote file successfully." let output = "Wrote file successfully."