Skip to content
Closed
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
28 changes: 25 additions & 3 deletions packages/opencode/src/file/time.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Instance } from "../project/instance"
import { Log } from "../util/log"
import { Storage } from "../storage/storage"

export namespace FileTime {
const log = Log.create({ service: "file.time" })
Expand All @@ -19,14 +20,35 @@ export namespace FileTime {
const { read } = state()
read[sessionID] = read[sessionID] || {}
read[sessionID][file] = new Date()

Storage.write(["filetime", sessionID, Buffer.from(file).toString("base64url")], {
time: read[sessionID][file]!.toISOString(),
path: file,
}).catch(() => {})
}

export function get(sessionID: string, file: string) {
return state().read[sessionID]?.[file]
export async function get(sessionID: string, file: string) {
const memTime = state().read[sessionID]?.[file]
if (memTime) return memTime

const stored = await Storage.read<{ time: string; path: string }>([
"filetime",
sessionID,
Buffer.from(file).toString("base64url"),
]).catch(() => undefined)

if (stored?.time) {
const date = new Date(stored.time)
const { read } = state()
read[sessionID] = read[sessionID] || {}
read[sessionID][file] = date
return date
}
return undefined
}

export async function assert(sessionID: string, filepath: string) {
const time = get(sessionID, filepath)
const time = await get(sessionID, filepath)
if (!time) throw new Error(`You must read the file ${filepath} before overwriting it. Use the Read tool first`)
const stats = await Bun.file(filepath).stat()
if (stats.mtime.getTime() > time.getTime()) {
Expand Down