@notation/factory-claude 0.2.2 → 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.
@@ -7,9 +7,9 @@ const agentConfig = v.object({
7
7
  effort: v.optional(v.picklist(["low", "medium", "high", "xhigh", "max"])),
8
8
  /** Relative to `.factory/`. Appended to the runner's prompt. */
9
9
  systemPrompt: v.optional(v.string()),
10
- /** Merged into `/data/agents/<id>/settings.json`. */
10
+ /** Passed with `--settings`, over the agent's `/data/agents/<id>/.claude/settings.json`. */
11
11
  settings: v.optional(v.record(v.string(), v.unknown())),
12
- /** `~/.claude/*` is written to `/data/agents/<id>/`, other `~/*` to `/data/home/`. */
12
+ /** `~/.claude/*` is written to `/data/agents/<id>/.claude/`, other `~/*` to `/data/home/`. Relative to `.factory/`. */
13
13
  files: v.optional(v.record(v.string(), v.string())),
14
14
  });
15
15
  export type AgentConfig = v.InferInput<typeof agentConfig>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notation/factory-claude",
3
- "version": "0.2.2",
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.2"
27
+ "@notation/factory": "^0.5.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@notation/factory": "0.2.2"
30
+ "@notation/factory": "0.5.0"
31
31
  }
32
32
  }
package/runtime/runner.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { mkdtempSync, writeFileSync } from "node:fs";
2
- import { homedir, tmpdir } from "node:os";
2
+ import { tmpdir } from "node:os";
3
3
  import { join } from "node:path";
4
4
  import type { RuntimeRunner } from "@notation/factory";
5
5
 
@@ -14,14 +14,45 @@ function envFile(): Record<string, string> {
14
14
  return { CLAUDE_ENV_FILE: file };
15
15
  }
16
16
 
17
+ /** One line of Claude Code's stream-json output. */
18
+ type StreamEvent = {
19
+ type?: string;
20
+ result?: string;
21
+ is_error?: boolean;
22
+ session_id?: string;
23
+ parent_tool_use_id?: string | null;
24
+ message?: { content?: { type: string; text?: string }[] };
25
+ };
26
+
27
+ const parseLine = (line: string): StreamEvent | undefined => {
28
+ if (!line.startsWith("{")) return undefined;
29
+ try {
30
+ return JSON.parse(line) as StreamEvent;
31
+ } catch {
32
+ return undefined;
33
+ }
34
+ };
35
+
17
36
  /** How the runner runs Claude Code. Containers import it with no packages, so it imports only Node's. */
18
37
  const claude: RuntimeRunner = {
19
38
  command: "claude",
20
39
  name: "Claude Code",
21
- transcripts: join(homedir(), ".claude", "projects"),
40
+ configDir: ".claude",
41
+ configEnv: (dir) => ({ CLAUDE_CONFIG_DIR: dir }),
42
+ homeFiles: [".claude.json"],
43
+ transcripts: "projects",
22
44
  args: async (run, { systemPrompt }) => {
23
45
  const { agent } = run;
24
- const args = ["-p", run.conversation.prompt, "--output-format", "json", "--dangerously-skip-permissions"];
46
+ // Print mode reads the prompt from stdin. stream-json writes a line for
47
+ // each event, so the runner can post the agent's messages as it writes
48
+ // them. It needs --verbose in print mode.
49
+ const args = [
50
+ "-p",
51
+ "--output-format",
52
+ "stream-json",
53
+ "--verbose",
54
+ "--dangerously-skip-permissions",
55
+ ];
25
56
  if (typeof agent.model === "string") args.push("--model", agent.model);
26
57
  if (typeof agent.effort === "string") args.push("--effort", agent.effort);
27
58
  args.push("--append-system-prompt", systemPrompt);
@@ -30,12 +61,23 @@ const claude: RuntimeRunner = {
30
61
  },
31
62
  resume: (sessionId) => ["--resume", sessionId],
32
63
  parse: (stdout) => {
33
- try {
34
- const output = JSON.parse(stdout) as { result?: string; is_error?: boolean; session_id?: string };
35
- return { result: output.result, isError: output.is_error, sessionId: output.session_id };
36
- } catch {
37
- return undefined;
64
+ const lines = stdout.split("\n");
65
+ for (let i = lines.length - 1; i >= 0; i--) {
66
+ const event = parseLine(lines[i]!);
67
+ if (event?.type === "result") return { result: event.result, isError: event.is_error, sessionId: event.session_id };
38
68
  }
69
+ return undefined;
70
+ },
71
+ // A message with a parent tool use is a subagent's, which the conversation
72
+ // doesn't see. The conversation sees that the agent is thinking, but not
73
+ // its thoughts.
74
+ message: (line) => {
75
+ const event = parseLine(line);
76
+ if (event?.type !== "assistant" || event.parent_tool_use_id) return undefined;
77
+ const content = event.message?.content ?? [];
78
+ const text = content.flatMap((block) => (block.type === "text" && block.text ? [block.text] : []));
79
+ if (text.length) return text.join("\n\n");
80
+ return content.some((block) => block.type === "thinking" || block.type === "redacted_thinking") ? "Thinking..." : undefined;
39
81
  },
40
82
  env: { DISABLE_AUTOUPDATER: "1", ...envFile() },
41
83
  };