Skip to content

Commit ac0c172

Browse files
committed
More progress
1 parent 85aa329 commit ac0c172

File tree

9 files changed

+374
-268
lines changed

9 files changed

+374
-268
lines changed

benchmark/apps/cli/src/index.ts

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ const runExercise = async ({ run, task, server }: { run: Run; task: Task; server
154154
}
155155

156156
let isTaskFinished = false
157+
let isTaskAborted = false
157158

158159
client.on(IpcMessageType.Disconnect, () => {
159160
console.log(`[cli#runExercise | ${language} / ${exercise}] disconnect`)
@@ -216,39 +217,59 @@ const runExercise = async ({ run, task, server }: { run: Run; task: Task; server
216217
}
217218

218219
if (eventName === RooCodeEventName.TaskAborted) {
219-
isTaskFinished = true
220+
isTaskAborted = true
220221
}
221222
})
222223

223-
console.log(`[cli#runExercise | ${language} / ${exercise}] StartNewTask (${taskSocketPath})`)
224-
225224
client.sendMessage({
226-
type: IpcMessageType.TaskCommand,
225+
type: IpcMessageType.VSCodeCommand,
227226
origin: IpcOrigin.Client,
228227
clientId: client.clientId!,
229-
data: {
230-
commandName: TaskCommandName.StartNewTask,
231-
data: {
232-
configuration: {
233-
...rooCodeDefaults,
234-
openRouterApiKey: process.env.OPENROUTER_API_KEY!,
235-
...run.settings,
236-
},
237-
text: prompt,
238-
newTab: true,
239-
},
240-
},
228+
data: "workbench.action.closeWindow",
241229
})
242230

231+
// client.sendMessage({
232+
// type: IpcMessageType.TaskCommand,
233+
// origin: IpcOrigin.Client,
234+
// clientId: client.clientId!,
235+
// data: {
236+
// commandName: TaskCommandName.StartNewTask,
237+
// data: {
238+
// configuration: {
239+
// ...rooCodeDefaults,
240+
// openRouterApiKey: process.env.OPENROUTER_API_KEY!,
241+
// ...run.settings,
242+
// },
243+
// text: prompt,
244+
// newTab: true,
245+
// },
246+
// },
247+
// })
248+
249+
console.log(`[cli#runExercise | ${language} / ${exercise}] StartNewTask`)
250+
243251
try {
244-
await pWaitFor(() => isTaskFinished, { interval: 1_000, timeout: 300 * 1_000 })
245-
client.disconnect()
246-
return true
252+
await pWaitFor(() => isTaskFinished || isTaskAborted, { interval: 1_000, timeout: 300 * 1_000 })
247253
} catch (error) {
248254
console.error(error)
255+
}
256+
257+
try {
258+
client.sendMessage({
259+
type: IpcMessageType.VSCodeCommand,
260+
origin: IpcOrigin.Client,
261+
clientId: client.clientId!,
262+
data: "workbench.action.closeWindow",
263+
})
264+
265+
console.log(`[cli#runExercise | ${language} / ${exercise}] VSCodeCommand (workbench.action.closeWindow)`)
266+
249267
client.disconnect()
250-
return false
268+
} catch (error) {
269+
console.error(error)
251270
}
271+
272+
return isTaskFinished
252273
}
253274

254275
const runUnitTest = async ({ task }: { task: Task }) => {
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import EventEmitter from "node:events"
2+
import * as crypto from "node:crypto"
3+
4+
import ipc from "node-ipc"
5+
6+
import { IpcOrigin, IpcMessageType, IpcMessage, ipcMessageSchema, TaskCommand, TaskEvent } from "@benchmark/types"
7+
8+
export type IpcClientEvents = {
9+
[IpcMessageType.Connect]: []
10+
[IpcMessageType.Disconnect]: []
11+
[IpcMessageType.Ack]: [clientId: string]
12+
[IpcMessageType.TaskCommand]: [data: TaskCommand]
13+
[IpcMessageType.TaskEvent]: [data: TaskEvent]
14+
}
15+
16+
export class IpcClient extends EventEmitter<IpcClientEvents> {
17+
private readonly _socketPath: string
18+
private readonly _id: string
19+
private readonly _log: (...args: unknown[]) => void
20+
private _isConnected = false
21+
private _clientId?: string
22+
23+
constructor(socketPath: string, log = console.log) {
24+
super()
25+
26+
this._socketPath = socketPath
27+
this._id = `benchmark-${crypto.randomBytes(6).toString("hex")}`
28+
this._log = log
29+
30+
ipc.config.silent = true
31+
32+
ipc.connectTo(this._id, this.socketPath, () => {
33+
ipc.of[this._id]?.on("connect", () => this.onConnect())
34+
ipc.of[this._id]?.on("disconnect", () => this.onDisconnect())
35+
ipc.of[this._id]?.on("message", (data) => this.onMessage(data))
36+
})
37+
}
38+
39+
private onConnect() {
40+
if (this._isConnected) {
41+
return
42+
}
43+
44+
this.log("[client#onConnect]")
45+
this._isConnected = true
46+
this.emit(IpcMessageType.Connect)
47+
}
48+
49+
private onDisconnect() {
50+
if (!this._isConnected) {
51+
return
52+
}
53+
54+
this.log("[client#onDisconnect]")
55+
this._isConnected = false
56+
this.emit(IpcMessageType.Disconnect)
57+
}
58+
59+
private onMessage(data: unknown) {
60+
if (typeof data !== "object") {
61+
this._log("[client#onMessage] invalid data", data)
62+
return
63+
}
64+
65+
const result = ipcMessageSchema.safeParse(data)
66+
67+
if (!result.success) {
68+
this.log("[client#onMessage] invalid payload", data)
69+
return
70+
}
71+
72+
const payload = result.data
73+
74+
if (payload.origin === IpcOrigin.Server) {
75+
switch (payload.type) {
76+
case IpcMessageType.Ack:
77+
this._clientId = payload.data.clientId
78+
this.emit(IpcMessageType.Ack, payload.data.clientId)
79+
break
80+
case IpcMessageType.TaskEvent:
81+
this.emit(IpcMessageType.TaskEvent, payload.data)
82+
break
83+
}
84+
}
85+
}
86+
87+
private log(...args: unknown[]) {
88+
this._log(...args)
89+
}
90+
91+
public sendMessage(message: IpcMessage) {
92+
ipc.of[this._id]?.emit("message", message)
93+
}
94+
95+
public disconnect() {
96+
try {
97+
ipc.disconnect(this._id)
98+
// @TODO: Should we set _disconnect here?
99+
} catch (error) {
100+
this.log("[client#disconnect] error disconnecting", error)
101+
}
102+
}
103+
104+
public get socketPath() {
105+
return this._socketPath
106+
}
107+
108+
public get clientId() {
109+
return this._clientId
110+
}
111+
112+
public get isConnected() {
113+
return this._isConnected
114+
}
115+
116+
public get isReady() {
117+
return this._isConnected && this._clientId !== undefined
118+
}
119+
}

0 commit comments

Comments
 (0)