-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcursor-agent.ts
More file actions
58 lines (47 loc) · 1.51 KB
/
cursor-agent.ts
File metadata and controls
58 lines (47 loc) · 1.51 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
import { logger, task, tasks } from "@trigger.dev/sdk";
import { mkdirSync } from "fs";
import type { CursorEvent } from "@/lib/cursor-events";
import { spawnCursorAgent } from "../extensions/cursor-cli";
import { cursorStream } from "./cursor-stream";
export type CursorAgentPayload = {
prompt: string;
model?: string;
};
export type STREAMS = {
"cursor-events": CursorEvent;
};
export const cursorAgentTask = task({
id: "cursor-agent",
machine: "medium-2x",
maxDuration: 300,
run: async (payload: CursorAgentPayload) => {
const workspace = `/tmp/workspace-${Date.now()}`;
mkdirSync(workspace, { recursive: true });
const model = payload.model ?? "sonnet-4.5";
logger.info("Spawning cursor-agent", { workspace, model });
const agent = spawnCursorAgent(
[
"-p",
"--force",
"--output-format",
"stream-json",
"--model",
model,
payload.prompt,
],
{ cwd: workspace, env: { CURSOR_API_KEY: process.env.CURSOR_API_KEY } },
);
tasks.onCancel(() => {
logger.info("Task cancelled, killing cursor-agent");
agent.kill();
});
const { waitUntilComplete } = cursorStream.pipe(agent.stream);
const { exitCode, stderr } = await agent.waitUntilExit();
await waitUntilComplete();
if (exitCode !== 0) {
logger.error("cursor-agent failed", { exitCode, stderr });
throw new Error(stderr || `cursor-agent exited with code ${exitCode}`);
}
return { exitCode, prompt: payload.prompt };
},
});