da251c9b30
Rename BackgroundTask and attempt ID fields to camelCase across background-agent consumers while moving BackgroundManager construction to a single config object. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
import { getTimingConfig } from "../../tools/delegate-task/timing"
|
|
import type { BackgroundTaskStatus } from "./types"
|
|
|
|
type SessionWaitTerminalStatus = Extract<BackgroundTaskStatus, "error" | "cancelled" | "interrupt">
|
|
type AbortSignalLike = { aborted: boolean }
|
|
|
|
interface TaskReader {
|
|
getTask(taskID: string): { sessionId?: string; status?: BackgroundTaskStatus } | undefined
|
|
}
|
|
|
|
export interface WaitForTaskSessionIDOptions {
|
|
timeoutMs?: number
|
|
intervalMs?: number
|
|
signal?: AbortSignalLike
|
|
}
|
|
|
|
function isTerminalStatus(status: BackgroundTaskStatus | undefined): status is SessionWaitTerminalStatus {
|
|
return status === "error" || status === "cancelled" || status === "interrupt"
|
|
}
|
|
|
|
function waitForInterval(intervalMs: number): Promise<void> {
|
|
return new Promise(resolve => {
|
|
const scheduler = globalThis as { setTimeout: (handler: () => void, timeout?: number) => unknown }
|
|
scheduler.setTimeout(resolve, intervalMs)
|
|
})
|
|
}
|
|
|
|
export async function waitForTaskSessionID(
|
|
manager: TaskReader,
|
|
taskID: string,
|
|
options: WaitForTaskSessionIDOptions = {}
|
|
): Promise<string | undefined> {
|
|
const timing = getTimingConfig()
|
|
const timeoutMs = options.timeoutMs ?? timing.WAIT_FOR_SESSION_TIMEOUT_MS
|
|
const intervalMs = options.intervalMs ?? timing.WAIT_FOR_SESSION_INTERVAL_MS
|
|
|
|
if (options.signal?.aborted) {
|
|
return undefined
|
|
}
|
|
|
|
const initialTask = manager.getTask(taskID)
|
|
if (initialTask?.sessionId) {
|
|
return initialTask.sessionId
|
|
}
|
|
if (isTerminalStatus(initialTask?.status)) {
|
|
return undefined
|
|
}
|
|
|
|
const deadline = Date.now() + timeoutMs
|
|
|
|
while (Date.now() < deadline) {
|
|
if (options.signal?.aborted) {
|
|
return undefined
|
|
}
|
|
|
|
await waitForInterval(intervalMs)
|
|
|
|
const task = manager.getTask(taskID)
|
|
if (task?.sessionId) {
|
|
return task.sessionId
|
|
}
|
|
if (isTerminalStatus(task?.status)) {
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
return undefined
|
|
}
|