mirror of
https://github.com/sst/opencode.git
synced 2025-12-23 10:11:41 +00:00
Merge 81b88133a3 into 83397ebde2
This commit is contained in:
commit
ef8763b5ea
1 changed files with 25 additions and 3 deletions
|
|
@ -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" })
|
||||
|
|
@ -25,10 +26,31 @@ 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 withLock<T>(filepath: string, fn: () => Promise<T>): Promise<T> {
|
||||
|
|
@ -52,7 +74,7 @@ export namespace FileTime {
|
|||
}
|
||||
|
||||
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()) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue