@matthugh1/conductor-cli 0.2.0 → 0.2.3

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.
@@ -0,0 +1,232 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ assembleAutonomousPrompt
4
+ } from "./chunk-6AA726KG.js";
5
+
6
+ // ../../src/core/agent-spawner.ts
7
+ import { spawn } from "child_process";
8
+ import { readFile } from "fs/promises";
9
+ import { join } from "path";
10
+ import { createInterface } from "readline";
11
+ async function readClaudeMd(projectRoot) {
12
+ try {
13
+ return await readFile(join(projectRoot, "CLAUDE.md"), "utf8");
14
+ } catch {
15
+ return "";
16
+ }
17
+ }
18
+ function parseStreamJsonLine(raw) {
19
+ let event;
20
+ try {
21
+ event = JSON.parse(raw);
22
+ } catch {
23
+ return raw;
24
+ }
25
+ const type = event.type;
26
+ if (type === "assistant") {
27
+ const msg = event.message;
28
+ const content = msg?.content;
29
+ if (!content) return null;
30
+ const parts = [];
31
+ for (const block of content) {
32
+ if (block.type === "text" && typeof block.text === "string") {
33
+ parts.push(block.text);
34
+ } else if (block.type === "tool_use") {
35
+ const name = block.name;
36
+ parts.push(`[tool] ${name}`);
37
+ }
38
+ }
39
+ return parts.length > 0 ? parts.join("\n") : null;
40
+ }
41
+ if (type === "tool_result") {
42
+ const name = event.tool_name ?? "tool";
43
+ const content = event.content;
44
+ if (content && content.length > 200) {
45
+ return `[${name}] ${content.slice(0, 200)}...`;
46
+ }
47
+ return content ? `[${name}] ${content}` : `[${name}] done`;
48
+ }
49
+ if (type === "result") {
50
+ const subtype = event.subtype;
51
+ const result = event.result;
52
+ if (subtype === "success" && result) {
53
+ return `[result] ${result.slice(0, 500)}`;
54
+ }
55
+ if (subtype === "error") {
56
+ return `[error] ${event.error ?? "unknown error"}`;
57
+ }
58
+ return null;
59
+ }
60
+ if (type === "system" || type === "rate_limit_event") {
61
+ return null;
62
+ }
63
+ return null;
64
+ }
65
+ function spawnAgent(opts) {
66
+ let child = null;
67
+ let cancelled = false;
68
+ let pid = null;
69
+ const cancel = () => {
70
+ cancelled = true;
71
+ if (child !== null && child.exitCode === null) {
72
+ child.kill("SIGTERM");
73
+ }
74
+ };
75
+ const done = runAgent(opts, (c) => {
76
+ child = c;
77
+ pid = c.pid ?? null;
78
+ }, () => cancelled);
79
+ return { done, cancel, get pid() {
80
+ return pid;
81
+ } };
82
+ }
83
+ async function runAgent(opts, onChild, isCancelled) {
84
+ const startTime = Date.now();
85
+ let lineCount = 0;
86
+ let prompt;
87
+ if (opts.assembledPrompt) {
88
+ prompt = opts.assembledPrompt;
89
+ } else {
90
+ const claudeMd = await readClaudeMd(opts.projectRoot);
91
+ prompt = assembleAutonomousPrompt({
92
+ claudeMd,
93
+ projectName: opts.projectRoot,
94
+ item: opts.item
95
+ });
96
+ }
97
+ const args = ["-p", prompt, "--output-format", "stream-json", "--verbose"];
98
+ if (opts.modelOverride) {
99
+ args.push("--model", opts.modelOverride);
100
+ }
101
+ if (opts.skipPermissions !== false) {
102
+ args.push("--dangerously-skip-permissions");
103
+ }
104
+ const child = spawn("claude", args, {
105
+ cwd: opts.projectRoot,
106
+ stdio: ["ignore", "pipe", "pipe"],
107
+ env: { ...process.env }
108
+ });
109
+ onChild(child);
110
+ if (child.pid === void 0) {
111
+ return {
112
+ outcome: "failed",
113
+ exitCode: null,
114
+ lineCount: 0,
115
+ durationMs: Date.now() - startTime,
116
+ errorMessage: "Failed to spawn claude process"
117
+ };
118
+ }
119
+ if (child.stdout !== null) {
120
+ const rl = createInterface({ input: child.stdout });
121
+ rl.on("line", (raw) => {
122
+ const text = parseStreamJsonLine(raw);
123
+ if (text === null) return;
124
+ lineCount++;
125
+ const currentLine = lineCount;
126
+ opts.onLine?.("stdout", text);
127
+ opts.client.appendOutputLine(opts.runId, {
128
+ lineNumber: currentLine,
129
+ stream: "stdout",
130
+ content: text
131
+ }).catch(() => {
132
+ });
133
+ });
134
+ }
135
+ if (child.stderr !== null) {
136
+ const rl = createInterface({ input: child.stderr });
137
+ rl.on("line", (line) => {
138
+ lineCount++;
139
+ const currentLine = lineCount;
140
+ opts.onLine?.("stderr", line);
141
+ opts.client.appendOutputLine(opts.runId, {
142
+ lineNumber: currentLine,
143
+ stream: "stderr",
144
+ content: line
145
+ }).catch(() => {
146
+ });
147
+ });
148
+ }
149
+ const exitCode = await waitWithTimeout(
150
+ child,
151
+ opts.timeoutMs,
152
+ opts.client,
153
+ opts.projectId,
154
+ opts.deliverableId,
155
+ isCancelled
156
+ );
157
+ const durationMs = Date.now() - startTime;
158
+ if (isCancelled()) {
159
+ return { outcome: "cancelled", exitCode, lineCount, durationMs };
160
+ }
161
+ if (exitCode === null) {
162
+ child.kill("SIGTERM");
163
+ await new Promise((r) => setTimeout(r, 2e3));
164
+ if (child.exitCode === null) {
165
+ child.kill("SIGKILL");
166
+ }
167
+ return {
168
+ outcome: "timeout",
169
+ exitCode: null,
170
+ lineCount,
171
+ durationMs,
172
+ errorMessage: `Task timed out after ${Math.round(opts.timeoutMs / 1e3)}s`
173
+ };
174
+ }
175
+ if (exitCode === 0) {
176
+ return { outcome: "completed", exitCode, lineCount, durationMs };
177
+ }
178
+ return {
179
+ outcome: "failed",
180
+ exitCode,
181
+ lineCount,
182
+ durationMs,
183
+ errorMessage: `Agent exited with code ${exitCode}`
184
+ };
185
+ }
186
+ async function waitWithTimeout(child, timeoutMs, client, projectId, deliverableId, isCancelled) {
187
+ return new Promise((resolve) => {
188
+ let resolved = false;
189
+ let elapsedMs = 0;
190
+ const TICK_MS = 3e3;
191
+ const finish = (code) => {
192
+ if (!resolved) {
193
+ resolved = true;
194
+ clearInterval(timer);
195
+ resolve(code);
196
+ }
197
+ };
198
+ child.on("close", (code) => {
199
+ finish(code ?? 1);
200
+ });
201
+ child.on("error", () => {
202
+ finish(1);
203
+ });
204
+ const timer = setInterval(async () => {
205
+ if (resolved || isCancelled()) {
206
+ clearInterval(timer);
207
+ if (isCancelled() && !resolved) {
208
+ finish(null);
209
+ }
210
+ return;
211
+ }
212
+ if (deliverableId) {
213
+ try {
214
+ const paused = await client.hasPendingDecisions(projectId, deliverableId);
215
+ if (!paused) {
216
+ elapsedMs += TICK_MS;
217
+ }
218
+ } catch {
219
+ elapsedMs += TICK_MS;
220
+ }
221
+ } else {
222
+ elapsedMs += TICK_MS;
223
+ }
224
+ if (elapsedMs >= timeoutMs) {
225
+ finish(null);
226
+ }
227
+ }, TICK_MS);
228
+ });
229
+ }
230
+ export {
231
+ spawnAgent
232
+ };