Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
86 lines (75 loc) · 2.16 KB
/
Copy pathutils.ts
File metadata and controls
86 lines (75 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { RooCodeEventName, type ClineMessage, type RooCodeAPI } from "@roo-code/types"
export const isCompletedAsk = (message: ClineMessage) => message.type === "ask" && message.partial !== true
type WaitForOptions = {
timeout?: number
interval?: number
}
export const waitFor = (
condition: (() => Promise<boolean>) | (() => boolean),
{ timeout = 30_000, interval = 250 }: WaitForOptions = {},
) => {
return new Promise<void>((resolve, reject) => {
let settled = false
let intervalId: NodeJS.Timeout | undefined
const timeoutId = setTimeout(() => {
settled = true
if (intervalId) clearTimeout(intervalId)
reject(new Error(`Timeout after ${Math.floor(timeout / 1000)}s`))
}, timeout)
const cleanup = () => {
clearTimeout(timeoutId)
if (intervalId) clearTimeout(intervalId)
}
const check = async () => {
try {
const isSatisfied = await condition()
if (settled) return
if (isSatisfied) {
settled = true
cleanup()
resolve()
} else {
intervalId = setTimeout(() => void check(), interval)
}
} catch (error) {
if (settled) return
settled = true
cleanup()
reject(error)
}
}
void check()
})
}
type WaitUntilAbortedOptions = WaitForOptions & {
api: RooCodeAPI
taskId: string
}
export const waitUntilAborted = async ({ api, taskId, ...options }: WaitUntilAbortedOptions) => {
const set = new Set<string>()
api.on(RooCodeEventName.TaskAborted, (taskId) => set.add(taskId))
await waitFor(() => set.has(taskId), options)
}
type WaitUntilCompletedOptions = WaitForOptions & {
api: RooCodeAPI
taskId?: string
start?: () => Promise<string>
}
export const waitUntilCompleted = async ({
api,
taskId: passedTaskId,
start,
...options
}: WaitUntilCompletedOptions): Promise<string> => {
const completed = new Set<string>()
const handler = (id: string) => completed.add(id)
api.on(RooCodeEventName.TaskCompleted, handler)
try {
const taskId = passedTaskId ?? (await start!())
await waitFor(() => completed.has(taskId), options)
return taskId
} finally {
api.off(RooCodeEventName.TaskCompleted, handler)
}
}
export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))