@notation/factory-cursor 0.2.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,9 +16,9 @@ const agentConfig = v.object({
16
16
  mode: v.optional(v.picklist(["plan", "ask"])),
17
17
  /** Relative to `.factory/`. Appended to the runner's prompt. */
18
18
  systemPrompt: v.optional(v.string()),
19
- /** Merged into `~/.cursor/cli-config.json` before each run. */
19
+ /** Merged into `/data/agents/<id>/.cursor/cli-config.json` before each run. */
20
20
  settings: v.optional(v.record(v.string(), v.unknown())),
21
- /** `~/.cursor/*` is written to `/data/agents/<id>/`, other `~/*` to `/data/home/`. */
21
+ /** `~/.cursor/*` is written to `/data/agents/<id>/.cursor/`, other `~/*` to `/data/home/`. Relative to `.factory/`. */
22
22
  files: v.optional(v.record(v.string(), v.string())),
23
23
  });
24
24
  export type AgentConfig = v.InferInput<typeof agentConfig>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notation/factory-cursor",
3
- "version": "0.2.0",
3
+ "version": "0.5.0",
4
4
  "author": "Daniel Grant",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -24,9 +24,9 @@
24
24
  "valibot": "^1.1.0"
25
25
  },
26
26
  "peerDependencies": {
27
- "@notation/factory": "^0.2.0"
27
+ "@notation/factory": "^0.5.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@notation/factory": "0.2.0"
30
+ "@notation/factory": "0.5.0"
31
31
  }
32
32
  }
package/runtime/runner.ts CHANGED
@@ -1,33 +1,52 @@
1
1
  import { mkdir, readFile, writeFile } from "node:fs/promises";
2
- import { homedir } from "node:os";
3
2
  import { dirname, join } from "node:path";
4
3
  import type { RuntimeRunner } from "@notation/factory";
5
4
 
5
+ /** One line of Cursor Agent's stream-json output. */
6
+ type StreamEvent = {
7
+ type?: string;
8
+ result?: string;
9
+ is_error?: boolean;
10
+ session_id?: string;
11
+ message?: { content?: { type: string; text?: string }[] };
12
+ };
13
+
14
+ const parseLine = (line: string): StreamEvent | undefined => {
15
+ if (!line.startsWith("{")) return undefined;
16
+ try {
17
+ return JSON.parse(line) as StreamEvent;
18
+ } catch {
19
+ return undefined;
20
+ }
21
+ };
22
+
6
23
  /** How the runner runs Cursor Agent. Containers import it with no packages, so it imports only Node's. */
7
24
  const cursor: RuntimeRunner = {
8
25
  command: "agent",
9
26
  name: "Cursor Agent",
10
- transcripts: join(homedir(), ".cursor", "chats"),
11
- args: async (run, { systemPrompt }) => {
27
+ configDir: ".cursor",
28
+ // The CLI reads its config and keeps its chats in CURSOR_CONFIG_DIR.
29
+ configEnv: (dir) => ({ CURSOR_CONFIG_DIR: dir }),
30
+ transcripts: "chats",
31
+ args: async (run, { configDir }) => {
12
32
  const { agent } = run;
13
- // The CLI has no public flag for a system prompt, so the runner's
14
- // prompt, then the agent's, come before the conversation's messages.
33
+ // stream-json writes a line for each event, so the runner can post the
34
+ // agent's messages as it writes them.
15
35
  const args = [
16
36
  "-p",
17
37
  "--output-format",
18
- "json",
38
+ "stream-json",
19
39
  "--force",
20
40
  "--trust",
21
41
  "--sandbox",
22
42
  "disabled",
23
43
  "--disable-auto-update",
24
- `${systemPrompt}\n\n${run.conversation.prompt}`,
25
44
  ];
26
45
  if (typeof agent.model === "string") args.push("--model", agent.model);
27
46
  if (agent.mode === "plan" || agent.mode === "ask") args.push("--mode", agent.mode);
28
47
  if (agent.settings && typeof agent.settings === "object") {
29
48
  // The CLI reads one config for the user, which a run merges for itself.
30
- const path = join(homedir(), ".cursor", "cli-config.json");
49
+ const path = join(configDir, "cli-config.json");
31
50
  await mkdir(dirname(path), { recursive: true });
32
51
  let current: Record<string, unknown> = {};
33
52
  try {
@@ -54,14 +73,32 @@ const cursor: RuntimeRunner = {
54
73
  }
55
74
  return args;
56
75
  },
76
+ // Print mode reads the prompt from stdin when it has no prompt argument.
77
+ // The CLI has no public flag for a system prompt, so the runner's prompt,
78
+ // then the agent's, come before the conversation's messages.
79
+ input: ({ prompt, systemPrompt }) => `${systemPrompt}\n\n${prompt}`,
57
80
  resume: (sessionId) => ["--resume", sessionId],
81
+ // A run that succeeds ends with a result event. A run that fails writes
82
+ // the error to stderr and exits non-zero, with no result event.
58
83
  parse: (stdout) => {
59
- try {
60
- const output = JSON.parse(stdout) as { result?: string; is_error?: boolean; session_id?: string };
61
- return { result: output.result, isError: output.is_error, sessionId: output.session_id };
62
- } catch {
63
- return undefined;
84
+ const lines = stdout.split("\n");
85
+ for (let i = lines.length - 1; i >= 0; i--) {
86
+ const event = parseLine(lines[i]!);
87
+ if (event?.type === "result") return { result: event.result, isError: event.is_error, sessionId: event.session_id };
64
88
  }
89
+ return undefined;
90
+ },
91
+ // The message before a tool call carries model_call_id. It is that
92
+ // message: the runner doesn't pass --stream-partial-output, so nothing
93
+ // earlier in the stream has the same text. A subagent's text is nested
94
+ // in the task tool's result, so it isn't an assistant message. A
95
+ // thinking event has no text the conversation should see.
96
+ message: (line) => {
97
+ const event = parseLine(line);
98
+ if (event?.type === "thinking") return "Thinking...";
99
+ if (event?.type !== "assistant") return undefined;
100
+ const text = (event.message?.content ?? []).flatMap((block) => (block.type === "text" && block.text ? [block.text] : []));
101
+ return text.length ? text.join("\n\n") : undefined;
65
102
  },
66
103
  };
67
104