cloudaxe-opencode/packages/opencode/src/file/time.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-06-27 15:29:20 +00:00
import { App } from "../app/app"
2025-07-04 21:57:48 +00:00
import { Log } from "../util/log"
2025-05-19 23:29:38 +00:00
2025-06-27 15:29:20 +00:00
export namespace FileTime {
2025-07-04 21:57:48 +00:00
const log = Log.create({ service: "file.time" })
2025-06-03 00:24:32 +00:00
export const state = App.state("tool.filetimes", () => {
const read: {
[sessionID: string]: {
[path: string]: Date | undefined
}
} = {}
return {
read,
}
})
2025-05-19 23:29:38 +00:00
2025-06-03 00:24:32 +00:00
export function read(sessionID: string, file: string) {
2025-07-04 21:57:48 +00:00
log.info("read", { sessionID, file })
2025-06-03 00:24:32 +00:00
const { read } = state()
read[sessionID] = read[sessionID] || {}
read[sessionID][file] = new Date()
2025-05-19 23:29:38 +00:00
}
2025-06-03 00:24:32 +00:00
export function get(sessionID: string, file: string) {
return state().read[sessionID]?.[file]
2025-05-19 23:29:38 +00:00
}
2025-06-04 17:33:25 +00:00
export async function assert(sessionID: string, filepath: string) {
const time = get(sessionID, filepath)
if (!time) throw new Error(`You must read the file ${filepath} before overwriting it. Use the Read tool first`)
2025-06-04 17:33:25 +00:00
const stats = await Bun.file(filepath).stat()
if (stats.mtime.getTime() > time.getTime()) {
throw new Error(
`File ${filepath} has been modified since it was last read.\nLast modification: ${stats.mtime.toISOString()}\nLast read: ${time.toISOString()}\n\nPlease read the file again before modifying it.`,
)
}
}
2025-05-19 23:29:38 +00:00
}