Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
456 lines (423 loc) · 15.6 KB
/
Copy pathindex.js
File metadata and controls
456 lines (423 loc) · 15.6 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
"use strict";
const fs = require("fs");
const path = require("path");
const { createInterface } = require("readline");
const { version } = require("./package.json");
const {
orchestrate,
requestedGroupName,
stripGroupDirective,
nextGroupName,
rewriteSessionId,
mergeMcpServers,
log,
} = require("./orchestrator");
const {
AGENT_GROUPS,
DEFAULT_GROUP,
MCP_SERVERS,
AGENT_TIMEOUT_MS,
} = require("./config");
const { groupAllowsMutation } = require("./policy");
// ─── ACP framing ──────────────────────────────────────────────────────────────
const send = (msg) => process.stdout.write(JSON.stringify(msg) + "\n");
const reply = (id, result) => send({ jsonrpc: "2.0", id, result });
const maybeReply = (id, result) => {
if (id !== undefined && id !== null) reply(id, result);
};
const notifyText = (sessionId, text) =>
send({
jsonrpc: "2.0",
method: "session/update",
params: {
sessionId,
update: {
type: "agent_message_chunk",
sessionUpdate: "agent_message_chunk",
content: { type: "text", text },
},
},
});
const availableGroupCommands = () =>
Object.values(AGENT_GROUPS).map((group) => ({
name: group.name,
description:
group.description || `Use the "${group.name}" orchestrator group.`,
input: { hint: "task or instructions" },
}));
const notifyAvailableCommands = (sessionId) =>
send({
jsonrpc: "2.0",
method: "session/update",
params: {
sessionId,
update: {
type: "available_commands_update",
sessionUpdate: "available_commands_update",
availableCommands: availableGroupCommands(),
},
},
});
const scheduleAvailableCommands = (sessionId) => {
// Zed inserts the session into its foreground session map after session/new
// resolves. Immediate post-response notifications can race that insertion
// and be dropped as "unknown session", so publish after a short delay and
// repeat once for older/slower clients.
for (const delayMs of [100, 1000]) {
setTimeout(() => {
if (sessions.has(sessionId)) notifyAvailableCommands(sessionId);
}, delayMs);
}
};
// ─── Zed RPC (proxied child requests) ────────────────────────────────────────
const pendingZedRequests = new Map();
let zedRequestId = 1;
let zedClientCapabilities = {};
function rejectPendingZedRequests(err) {
for (const { reject, timer } of pendingZedRequests.values()) {
if (timer) clearTimeout(timer);
reject(err);
}
pendingZedRequests.clear();
}
function rpcToZed(method, params, timeoutMs = AGENT_TIMEOUT_MS) {
return new Promise((resolve, reject) => {
const id = `orch-req-${zedRequestId++}`;
const timer =
timeoutMs > 0
? setTimeout(() => {
pendingZedRequests.delete(id);
const err = new Error(
`TIMEOUT:${timeoutMs}ms — ${method} did not respond`,
);
err.code = -32000;
reject(err);
}, timeoutMs)
: null;
pendingZedRequests.set(id, { resolve, reject, timer });
send({ jsonrpc: "2.0", id, method, params });
});
}
// ─── Session table ────────────────────────────────────────────────────────────
// sessionId → { workDir, mcpServers, groupName, ctx? }
const sessions = new Map();
function persistSession(sessionId, session) {
if (!session || !session.workDir) return;
try {
const sessionDir = path.join(
session.workDir,
".plan",
"orchestrator",
"sessions",
);
fs.mkdirSync(sessionDir, { recursive: true });
const safeId = sessionId.replace(/[^a-zA-Z0-9-]/g, "");
const filePath = path.join(sessionDir, `${safeId}.json`);
const dataToSave = {
sessionId,
workDir: session.workDir,
mcpServers: session.mcpServers,
groupName: session.groupName,
promptCounter: session.promptCounter,
createdAt: session.createdAt || Date.now(),
};
fs.writeFileSync(filePath, JSON.stringify(dataToSave, null, 2), "utf8");
} catch (err) {
log(`Failed to persist session ${sessionId}: ${err.message}`);
}
}
function loadPersistedSession(workDir, sessionId) {
try {
const safeId = sessionId.replace(/[^a-zA-Z0-9-]/g, "");
const filePath = path.join(
workDir,
".plan",
"orchestrator",
"sessions",
`${safeId}.json`,
);
if (fs.existsSync(filePath)) {
const data = JSON.parse(fs.readFileSync(filePath, "utf8"));
// Basic validation
if (data && data.sessionId === sessionId && data.workDir) {
return data;
}
}
} catch (err) {
log(`Failed to load persisted session ${sessionId}: ${err.message}`);
}
return null;
}
// ─── Message loop ─────────────────────────────────────────────────────────────
const rl = createInterface({ input: process.stdin });
process.stdin.resume();
rl.on("line", async (line) => {
let msg;
try {
msg = JSON.parse(line.trim());
} catch {
return;
}
// Responses to our outbound Zed requests
if (msg.method == null && msg.id != null) {
const pending = pendingZedRequests.get(msg.id);
if (!pending) return;
pendingZedRequests.delete(msg.id);
if (pending.timer) clearTimeout(pending.timer);
if (msg.error) {
const err = new Error(msg.error.message || "RPC error");
err.code = Number.isInteger(msg.error.code)
? msg.error.code
: -32000;
if (msg.error.data !== undefined) err.data = msg.error.data;
pending.reject(err);
} else {
pending.resolve(msg.result);
}
return;
}
if (msg.method === "initialize") {
zedClientCapabilities = msg.params?.clientCapabilities || {};
reply(msg.id, {
protocolVersion: 1,
agentInfo: { name: "zed-orchestrator", version },
agentCapabilities: {
promptCapabilities: { image: true, embeddedContext: true },
mcpCapabilities: { http: true, sse: false },
sessionCapabilities: { close: {} },
},
authMethods: [],
});
} else if (msg.method === "session/new") {
const sessionId = `orch-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`;
const session = {
workDir:
msg.params?.cwd ||
msg.params?.workingDirectory ||
process.cwd(),
mcpServers: msg.params?.mcpServers || [],
groupName: DEFAULT_GROUP,
promptCounter: 0,
createdAt: Date.now(),
};
sessions.set(sessionId, session);
persistSession(sessionId, session);
log(`Session created: ${sessionId}, group=${DEFAULT_GROUP}`);
reply(msg.id, { sessionId });
scheduleAvailableCommands(sessionId);
} else if (msg.method === "session/resume") {
const sessionId = msg.params?.sessionId;
if (!sessionId) {
send({
jsonrpc: "2.0",
id: msg.id,
error: { code: -32602, message: "Missing sessionId" },
});
return;
}
if (!sessions.has(sessionId)) {
// Zed sends the project root as `cwd` — use it so we look in the right directory.
const loadedData = loadPersistedSession(msg.params?.cwd, sessionId);
if (loadedData) {
sessions.set(sessionId, {
workDir: loadedData.workDir,
mcpServers: loadedData.mcpServers || [],
groupName: loadedData.groupName || DEFAULT_GROUP,
promptCounter: loadedData.promptCounter || 0,
createdAt: loadedData.createdAt || Date.now(),
});
log(
`Session resumed: ${sessionId}, group=${loadedData.groupName}`,
);
} else {
send({
jsonrpc: "2.0",
id: msg.id,
// -32002 = ResourceNotFound in ACP protocol.
// Do NOT use -32000: that is AuthRequired in ACP and
// causes Zed to show "Authentication required" instead.
error: { code: -32002, message: "Session not found" },
});
return;
}
}
reply(msg.id, {});
scheduleAvailableCommands(sessionId);
} else if (msg.method === "session/prompt") {
const { sessionId, prompt } = msg.params;
const session = sessions.get(sessionId);
if (!session) {
send({
jsonrpc: "2.0",
id: msg.id,
error: {
code: -32002, // ResourceNotFound
message: `Unknown session: ${sessionId}`,
},
});
return;
}
if (session.ctx) {
send({
jsonrpc: "2.0",
id: msg.id,
error: {
code: -32603, // InternalError
message: "Session busy: a prompt is already in progress",
},
});
return;
}
let task = prompt || [];
const { workDir } = session;
const requestedGroup = requestedGroupName(task);
if (requestedGroup) {
if (!AGENT_GROUPS[requestedGroup]) {
send({
jsonrpc: "2.0",
id: msg.id,
error: {
code: -32602,
message: `Unknown orchestrator group: ${requestedGroup}`,
},
});
return;
}
session.groupName = requestedGroup;
task = stripGroupDirective(task);
}
const group = AGENT_GROUPS[session.groupName];
const activeGroupName = group.name;
const promptId = ++session.promptCounter;
const runId = `${sessionId}-prompt-${String(promptId).padStart(4, "0")}`;
const ctx = {
cancelled: false,
clients: new Set(),
rpcToZed,
latestApprovedPlans: session.latestApprovedPlans || {},
};
session.ctx = ctx;
log(
`Task received (${task.length} block(s)), group=${group.name}, orchestrating…`,
);
let finalResult;
try {
const outcome = await orchestrate(
sessionId,
runId,
promptId,
task,
workDir,
group,
ctx,
session.mcpServers,
notifyText,
zedClientCapabilities,
);
finalResult = outcome.text;
if (outcome.approved && !ctx.cancelled) {
const next = nextGroupName(group.name);
const nextGroup = next ? AGENT_GROUPS[next] : null;
if (nextGroup && !groupAllowsMutation(nextGroup)) {
session.groupName = next;
notifyText(
sessionId,
`\n> Group "${group.name}" approved. Next prompt will use group "${next}".\n`,
);
} else if (nextGroup) {
notifyText(
sessionId,
`\n> Group "${group.name}" approved. Use /${next} explicitly for that one prompt.\n`,
);
}
}
} catch (err) {
log(`fatal error: ${err.message}\n${err.stack}`);
finalResult = `Orchestrator fatal error: ${err.message}`;
} finally {
if (ctx.latestApprovedPlans)
session.latestApprovedPlans = ctx.latestApprovedPlans;
const activeGroup = AGENT_GROUPS[activeGroupName];
if (
activeGroup?.persist === false ||
activeGroup?.strategy === "single_writer"
) {
session.groupName = DEFAULT_GROUP;
}
session.ctx = null;
persistSession(sessionId, session);
}
if (finalResult) {
notifyText(sessionId, `\n${finalResult}\n`);
}
reply(msg.id, { stopReason: ctx.cancelled ? "cancelled" : "end_turn" });
} else if (msg.method === "session/cancel") {
const { sessionId } = msg.params || {};
const session = sessions.get(sessionId);
if (session?.ctx) {
session.ctx.cancelled = true;
for (const c of session.ctx.clients) c.kill();
log(`Session cancelled: ${sessionId}`);
}
maybeReply(msg.id, {});
} else if (
msg.method === "session/release" ||
msg.method === "session/close"
) {
const { sessionId } = msg.params || {};
const session = sessions.get(sessionId);
if (session?.ctx) {
session.ctx.cancelled = true;
for (const c of session.ctx.clients) c.kill();
}
sessions.delete(sessionId);
maybeReply(msg.id, {});
} else if (msg.method && msg.id != null) {
send({
jsonrpc: "2.0",
id: msg.id,
error: { code: -32601, message: "Method not found" },
});
}
});
// ─── Shutdown ─────────────────────────────────────────────────────────────────
let shuttingDown = false;
async function shutdown(signal) {
if (shuttingDown) return;
shuttingDown = true;
log(`Received ${signal}, shutting down…`);
rejectPendingZedRequests(
new Error(`orchestrator shutting down: ${signal}`),
);
const clients = new Set();
for (const session of sessions.values()) {
if (session.ctx) {
session.ctx.cancelled = true;
for (const c of session.ctx.clients) clients.add(c);
}
}
for (const c of clients) c.kill();
await Promise.allSettled([...clients].map((c) => c.waitForExit?.(1500)));
for (const c of clients) c.forceKill?.();
await Promise.allSettled([...clients].map((c) => c.waitForExit?.(250)));
process.exit(0);
}
process.on("SIGTERM", () => {
void shutdown("SIGTERM");
});
process.on("SIGINT", () => {
void shutdown("SIGINT");
});
rl.on("close", () => {
void shutdown("stdin-closed");
});
process.stdin.on("end", () => {
void shutdown("stdin-end");
});
process.on("uncaughtException", (err) =>
log(`uncaughtException: ${err.message}\n${err.stack}`),
);
process.on("unhandledRejection", (err) =>
log(`unhandledRejection: ${err?.message || err}`),
);
log(`Started. defaultGroup=${DEFAULT_GROUP}`);