mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-30 22:03:58 +00:00
Co-authored-by: GitHub Action <action@github.com> Co-authored-by: Liang-Shih Lin <liangshihlin@proton.me> Co-authored-by: Dominik Engelhardt <dominikengelhardt@ymail.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: adamdottv <2363879+adamdottv@users.noreply.github.com>
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import { App } from "../app/app"
|
|
import { Log } from "../util/log"
|
|
|
|
export namespace FileTime {
|
|
const log = Log.create({ service: "file.time" })
|
|
export const state = App.state("tool.filetimes", () => {
|
|
const read: {
|
|
[sessionID: string]: {
|
|
[path: string]: Date | undefined
|
|
}
|
|
} = {}
|
|
return {
|
|
read,
|
|
}
|
|
})
|
|
|
|
export function read(sessionID: string, file: string) {
|
|
log.info("read", { sessionID, file })
|
|
const { read } = state()
|
|
read[sessionID] = read[sessionID] || {}
|
|
read[sessionID][file] = new Date()
|
|
}
|
|
|
|
export function get(sessionID: string, file: string) {
|
|
return state().read[sessionID]?.[file]
|
|
}
|
|
|
|
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`)
|
|
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.`,
|
|
)
|
|
}
|
|
}
|
|
}
|