fix(core): recover corrupted models cache (#30947)
This commit is contained in:
parent
a261b55e43
commit
0ee7cfa1fe
4 changed files with 69 additions and 6 deletions
|
|
@ -84,7 +84,10 @@ export namespace FSUtil {
|
|||
|
||||
const readJson = Effect.fn("FileSystem.readJson")(function* (path: string) {
|
||||
const text = yield* fs.readFileString(path)
|
||||
return JSON.parse(text)
|
||||
return yield* Effect.try({
|
||||
try: () => JSON.parse(text),
|
||||
catch: (cause) => new FileSystemError({ method: "readJson", cause }),
|
||||
})
|
||||
})
|
||||
|
||||
const writeJson = Effect.fn("FileSystem.writeJson")(function* (path: string, data: unknown, mode?: number) {
|
||||
|
|
|
|||
|
|
@ -162,7 +162,16 @@ export const layer = Layer.effect(
|
|||
})
|
||||
|
||||
const loadFromDisk = fs.readJson(Flag.OPENCODE_MODELS_PATH ?? filepath).pipe(
|
||||
Effect.catch(() => Effect.succeed(undefined)),
|
||||
Effect.catch((error) => {
|
||||
if (
|
||||
Flag.OPENCODE_MODELS_PATH === undefined &&
|
||||
error._tag === "FileSystemError" &&
|
||||
error.method === "readJson"
|
||||
) {
|
||||
return fs.remove(filepath, { force: true }).pipe(Effect.ignore, Effect.as(undefined))
|
||||
}
|
||||
return Effect.succeed(undefined)
|
||||
}),
|
||||
Effect.map((v) => v as Record<string, Provider> | undefined),
|
||||
)
|
||||
|
||||
|
|
@ -172,7 +181,16 @@ export const layer = Layer.effect(
|
|||
|
||||
const fetchAndWrite = Effect.fn("ModelsDev.fetchAndWrite")(function* () {
|
||||
const text = yield* fetchApi()
|
||||
yield* fs.writeWithDirs(filepath, text)
|
||||
const tempfile = `${filepath}.${process.pid}.${Date.now()}.tmp`
|
||||
yield* fs.writeWithDirs(tempfile, text).pipe(
|
||||
Effect.andThen(fs.rename(tempfile, filepath)),
|
||||
Effect.catch((error) =>
|
||||
Effect.gen(function* () {
|
||||
yield* fs.remove(tempfile, { force: true }).pipe(Effect.ignore)
|
||||
return yield* Effect.fail(error)
|
||||
}),
|
||||
),
|
||||
)
|
||||
return text
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue