fix(background-agent): address Oracle review feedback
- Remove unused storage.ts (dead code, runtime inconsistency) - Change persist() to sync void (debounce semantics clarity) - Add type guards in handleEvent() for event safety - Remove unused 'pending' from BackgroundTaskStatus 🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
@@ -1,3 +1,2 @@
|
||||
export * from "./types"
|
||||
export { BackgroundManager } from "./manager"
|
||||
export * from "./storage"
|
||||
|
||||
@@ -122,6 +122,7 @@ export class BackgroundManager {
|
||||
const props = event.properties
|
||||
|
||||
if (event.type === "message.part.updated") {
|
||||
if (!props || typeof props !== "object" || !("sessionID" in props)) return
|
||||
const partInfo = props as unknown as MessagePartInfo
|
||||
const sessionID = partInfo?.sessionID
|
||||
if (!sessionID) return
|
||||
@@ -145,11 +146,10 @@ export class BackgroundManager {
|
||||
|
||||
if (event.type === "session.updated") {
|
||||
const info = props?.info as SessionInfo | undefined
|
||||
const sessionID = info?.id
|
||||
if (!info || typeof info.id !== "string") return
|
||||
const sessionID = info.id
|
||||
const status = info?.status
|
||||
|
||||
if (!sessionID) return
|
||||
|
||||
const task = this.findBySession(sessionID)
|
||||
if (!task) return
|
||||
|
||||
@@ -163,9 +163,8 @@ export class BackgroundManager {
|
||||
|
||||
if (event.type === "session.deleted") {
|
||||
const info = props?.info as SessionInfo | undefined
|
||||
const sessionID = info?.id
|
||||
|
||||
if (!sessionID) return
|
||||
if (!info || typeof info.id !== "string") return
|
||||
const sessionID = info.id
|
||||
|
||||
const task = this.findBySession(sessionID)
|
||||
if (!task) return
|
||||
@@ -208,29 +207,27 @@ export class BackgroundManager {
|
||||
}
|
||||
}
|
||||
|
||||
async persist(): Promise<void> {
|
||||
persist(): void {
|
||||
if (this.persistTimer) {
|
||||
clearTimeout(this.persistTimer)
|
||||
}
|
||||
|
||||
this.persistTimer = setTimeout(async () => {
|
||||
try {
|
||||
const data = Array.from(this.tasks.values())
|
||||
const serialized = data.map((task) => ({
|
||||
...task,
|
||||
startedAt: task.startedAt.toISOString(),
|
||||
completedAt: task.completedAt?.toISOString(),
|
||||
progress: task.progress
|
||||
? {
|
||||
...task.progress,
|
||||
lastUpdate: task.progress.lastUpdate.toISOString(),
|
||||
}
|
||||
: undefined,
|
||||
}))
|
||||
await Bun.write(this.storePath, JSON.stringify(serialized, null, 2))
|
||||
} catch {
|
||||
this.persistTimer = setTimeout(() => {
|
||||
const data = Array.from(this.tasks.values())
|
||||
const serialized = data.map((task) => ({
|
||||
...task,
|
||||
startedAt: task.startedAt.toISOString(),
|
||||
completedAt: task.completedAt?.toISOString(),
|
||||
progress: task.progress
|
||||
? {
|
||||
...task.progress,
|
||||
lastUpdate: task.progress.lastUpdate.toISOString(),
|
||||
}
|
||||
: undefined,
|
||||
}))
|
||||
Bun.write(this.storePath, JSON.stringify(serialized, null, 2)).catch(() => {
|
||||
void 0
|
||||
}
|
||||
})
|
||||
}, 500)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
import { writeFile, readFile } from "fs/promises"
|
||||
import type { BackgroundTask } from "./types"
|
||||
|
||||
export async function saveToFile(
|
||||
path: string,
|
||||
tasks: BackgroundTask[]
|
||||
): Promise<void> {
|
||||
await writeFile(path, JSON.stringify(tasks, null, 2), "utf-8")
|
||||
}
|
||||
|
||||
export async function loadFromFile(path: string): Promise<BackgroundTask[]> {
|
||||
try {
|
||||
const content = await readFile(path, "utf-8")
|
||||
return JSON.parse(content)
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
||||
return []
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
export type BackgroundTaskStatus =
|
||||
| "pending"
|
||||
| "running"
|
||||
| "completed"
|
||||
| "error"
|
||||
|
||||
@@ -207,7 +207,7 @@ Only running tasks can be cancelled.`
|
||||
task.status = "cancelled"
|
||||
task.completedAt = new Date()
|
||||
|
||||
await manager.persist()
|
||||
manager.persist()
|
||||
|
||||
return `✅ Task cancelled successfully
|
||||
|
||||
|
||||
Reference in New Issue
Block a user