localmcp-cli 0.0.0-stage → 0.0.11
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.
- package/LICENSE +21 -0
- package/README.md +104 -2
- package/bin/cli.js +27 -0
- package/package.json +28 -4
- package/src/agents/hub.js +663 -0
- package/src/agents/providers.js +840 -0
- package/src/agents/store.js +524 -0
- package/src/cli/options.js +177 -0
- package/src/cli/runtime.js +126 -0
- package/src/cli/service.js +29 -0
- package/src/cli/tunnel.js +242 -0
- package/src/core/audit.js +105 -0
- package/src/core/auth.js +326 -0
- package/src/core/bounded-log.js +39 -0
- package/src/core/config.js +319 -0
- package/src/core/context-discovery.js +245 -0
- package/src/core/mesh.js +103 -0
- package/src/core/protocol.js +112 -0
- package/src/core/updater-client.js +165 -0
- package/src/server.js +260 -0
- package/src/tools/agents.js +149 -0
- package/src/tools/apply-patch.js +454 -0
- package/src/tools/diagnostics.js +158 -0
- package/src/tools/filesystem.js +1122 -0
- package/src/tools/gui-windows.js +268 -0
- package/src/tools/registry.js +119 -0
- package/src/tools/system.js +591 -0
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
import { AgentStore, DEFAULT_TASK_TTL_MS, TERMINAL_TASK_STATUSES } from "./store.js";
|
|
2
|
+
import { createDefaultProviders, isPidAlive, isUuid } from "./providers.js";
|
|
3
|
+
|
|
4
|
+
const ENGINE_NAMES = new Set(["antigravity", "codex", "claude", "gemini", "dsh", "workbuddy"]);
|
|
5
|
+
|
|
6
|
+
export class AgentHub {
|
|
7
|
+
constructor({ baseDir, resolveAllowedCwd, defaultCwd, audit, providers } = {}) {
|
|
8
|
+
this.store = new AgentStore(baseDir);
|
|
9
|
+
this.providers = providers || createDefaultProviders();
|
|
10
|
+
this.resolveAllowedCwd = resolveAllowedCwd || (async (p) => p || defaultCwd || process.cwd());
|
|
11
|
+
this.defaultCwd = defaultCwd || process.cwd();
|
|
12
|
+
this.audit = audit || (async () => {});
|
|
13
|
+
this.reconcileStaleTasks();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
reconcileStaleTasks() {
|
|
17
|
+
const working = this.store.listTasks({ limit: 100, status: "working" });
|
|
18
|
+
for (const task of working) {
|
|
19
|
+
if (task.engine !== "antigravity" && task.engine !== "custom" && task.pid && !isPidAlive(task.pid)) {
|
|
20
|
+
this.store.updateTask(task.taskId, {
|
|
21
|
+
status: "failed",
|
|
22
|
+
statusMessage: "Task process no longer running after server restart",
|
|
23
|
+
pid: null,
|
|
24
|
+
error: { code: -32000, message: "Background CLI process terminated while server was offline." },
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
_withPiggyback(payload, fromAgent, skipPiggyback = false) {
|
|
31
|
+
if (!fromAgent) return payload;
|
|
32
|
+
this.store.touchAgent(fromAgent);
|
|
33
|
+
if (skipPiggyback) return payload;
|
|
34
|
+
const inbox = this.store.getUnreadSummary(fromAgent);
|
|
35
|
+
if (!inbox) return payload;
|
|
36
|
+
return { ...payload, inboxPiggyback: inbox };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
probeEngines() {
|
|
40
|
+
const out = [];
|
|
41
|
+
for (const provider of this.providers.values()) {
|
|
42
|
+
out.push(provider.probe());
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
pickAutoEngine() {
|
|
48
|
+
const probes = this.probeEngines();
|
|
49
|
+
const ready = probes.find((p) => p.available);
|
|
50
|
+
return ready ? ready.engine : "";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async listAgents({ from_agent, engine, include_sessions = true, limit = 12 } = {}) {
|
|
54
|
+
const engines = this.probeEngines();
|
|
55
|
+
const registered = this.store.listRegistered({ engine });
|
|
56
|
+
let discoveredSessions = [];
|
|
57
|
+
if (include_sessions && (!engine || engine === "antigravity")) {
|
|
58
|
+
const ag = this.providers.get("antigravity");
|
|
59
|
+
if (ag?.listSessions) {
|
|
60
|
+
discoveredSessions = ag.listSessions({ limit });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const recentTasks = this.store.listTasks({ limit: Math.min(limit, 20) }).map((t) => ({
|
|
64
|
+
taskId: t.taskId,
|
|
65
|
+
status: t.status,
|
|
66
|
+
engine: t.engine,
|
|
67
|
+
targetAgent: t.targetAgent || undefined,
|
|
68
|
+
sessionId: t.sessionId || undefined,
|
|
69
|
+
title: t.title || undefined,
|
|
70
|
+
lastUpdatedAt: t.lastUpdatedAt,
|
|
71
|
+
}));
|
|
72
|
+
|
|
73
|
+
return this._withPiggyback(
|
|
74
|
+
{
|
|
75
|
+
ok: true,
|
|
76
|
+
engines,
|
|
77
|
+
registered,
|
|
78
|
+
discoveredSessions,
|
|
79
|
+
recentTasks,
|
|
80
|
+
},
|
|
81
|
+
from_agent
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async registerAgent({
|
|
86
|
+
agent_id,
|
|
87
|
+
aliases,
|
|
88
|
+
engine,
|
|
89
|
+
role,
|
|
90
|
+
workspace,
|
|
91
|
+
delivery_mode,
|
|
92
|
+
target_id,
|
|
93
|
+
status,
|
|
94
|
+
ttl_seconds,
|
|
95
|
+
from_agent,
|
|
96
|
+
}) {
|
|
97
|
+
let resolvedWorkspace = "";
|
|
98
|
+
if (workspace) {
|
|
99
|
+
resolvedWorkspace = await this.resolveAllowedCwd(workspace);
|
|
100
|
+
}
|
|
101
|
+
const inferredDelivery =
|
|
102
|
+
delivery_mode ||
|
|
103
|
+
(engine === "antigravity" || (target_id && isUuid(target_id)) ? "push" : "inbox");
|
|
104
|
+
|
|
105
|
+
const record = this.store.registerAgent({
|
|
106
|
+
agentId: agent_id,
|
|
107
|
+
aliases,
|
|
108
|
+
engine: engine || (inferredDelivery === "push" && isUuid(target_id) ? "antigravity" : "custom"),
|
|
109
|
+
role,
|
|
110
|
+
workspace: resolvedWorkspace,
|
|
111
|
+
deliveryMode: inferredDelivery,
|
|
112
|
+
targetId: target_id,
|
|
113
|
+
status,
|
|
114
|
+
ttlSeconds: ttl_seconds,
|
|
115
|
+
implicit: false,
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
await this.audit({
|
|
119
|
+
tool: "agent_register",
|
|
120
|
+
agentId: record.agentId,
|
|
121
|
+
engine: record.engine,
|
|
122
|
+
deliveryMode: record.deliveryMode,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const caller = from_agent || record.agentId;
|
|
126
|
+
return this._withPiggyback(
|
|
127
|
+
{
|
|
128
|
+
ok: true,
|
|
129
|
+
agent: {
|
|
130
|
+
...record,
|
|
131
|
+
supportsPush:
|
|
132
|
+
record.deliveryMode === "push" && Boolean(record.targetId || record.engine === "antigravity"),
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
caller
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async pushTask(provider, task, params) {
|
|
140
|
+
try {
|
|
141
|
+
return await provider.pushMessage(params);
|
|
142
|
+
} catch {
|
|
143
|
+
this.store.updateTask(task.taskId, { status: "failed", statusMessage: "Provider delivery failed", pid: null });
|
|
144
|
+
throw new Error(`Provider delivery failed for task ${task.taskId}.`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async dispatchTask({
|
|
149
|
+
target = "",
|
|
150
|
+
engine = "",
|
|
151
|
+
prompt,
|
|
152
|
+
cwd,
|
|
153
|
+
title = "",
|
|
154
|
+
model = "",
|
|
155
|
+
profile = "",
|
|
156
|
+
wait_seconds = 0,
|
|
157
|
+
from_agent = "",
|
|
158
|
+
ttl_seconds,
|
|
159
|
+
}) {
|
|
160
|
+
if (!String(prompt || "").trim()) {
|
|
161
|
+
throw new Error("prompt is required.");
|
|
162
|
+
}
|
|
163
|
+
if (from_agent) {
|
|
164
|
+
this.store.touchAgent(from_agent);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const resolvedCwd = await this.resolveAllowedCwd(cwd);
|
|
168
|
+
const ttlMs = ttl_seconds ? Math.max(60, Number(ttl_seconds)) * 1000 : DEFAULT_TASK_TTL_MS;
|
|
169
|
+
const cleanTarget = String(target || "").trim();
|
|
170
|
+
const cleanEngine = String(engine || "").trim().toLowerCase();
|
|
171
|
+
|
|
172
|
+
const registeredTarget = cleanTarget ? this.store.resolveAgent(cleanTarget) : null;
|
|
173
|
+
if (registeredTarget?.workspace) await this.resolveAllowedCwd(registeredTarget.workspace);
|
|
174
|
+
const targetIsEngine = ENGINE_NAMES.has(cleanTarget.toLowerCase()) || cleanTarget.toLowerCase() === "auto";
|
|
175
|
+
const targetIsRawUuid = !registeredTarget && isUuid(cleanTarget);
|
|
176
|
+
|
|
177
|
+
let chosenEngine = "";
|
|
178
|
+
let task = null;
|
|
179
|
+
let delivery = "direct";
|
|
180
|
+
|
|
181
|
+
if (registeredTarget) {
|
|
182
|
+
const canPushProvider =
|
|
183
|
+
registeredTarget.deliveryMode === "push" &&
|
|
184
|
+
this.providers.has(registeredTarget.engine) &&
|
|
185
|
+
this.providers.get(registeredTarget.engine).probe().available;
|
|
186
|
+
|
|
187
|
+
if (canPushProvider && registeredTarget.targetId) {
|
|
188
|
+
const provider = this.providers.get(registeredTarget.engine);
|
|
189
|
+
chosenEngine = registeredTarget.engine;
|
|
190
|
+
task = this.store.createTask({
|
|
191
|
+
engine: chosenEngine,
|
|
192
|
+
targetAgent: registeredTarget.agentId,
|
|
193
|
+
fromAgent: from_agent,
|
|
194
|
+
sessionId: registeredTarget.targetId,
|
|
195
|
+
title: title || `Task for ${registeredTarget.agentId}`,
|
|
196
|
+
prompt,
|
|
197
|
+
cwd: registeredTarget.workspace || resolvedCwd,
|
|
198
|
+
ttlMs,
|
|
199
|
+
remoteStopSupported: chosenEngine !== "antigravity",
|
|
200
|
+
});
|
|
201
|
+
const pushRes = await this.pushTask(provider, task, {
|
|
202
|
+
targetId: registeredTarget.targetId,
|
|
203
|
+
title: title || `Task ${task.taskId}`,
|
|
204
|
+
message: `[Task ${task.taskId}${from_agent ? ` from ${from_agent}` : ""}]\n${prompt}`,
|
|
205
|
+
cwd: registeredTarget.workspace || resolvedCwd,
|
|
206
|
+
taskId: task.taskId,
|
|
207
|
+
store: this.store,
|
|
208
|
+
});
|
|
209
|
+
if (pushRes?.pid) {
|
|
210
|
+
task = this.store.updateTask(task.taskId, { pid: pushRes.pid });
|
|
211
|
+
}
|
|
212
|
+
this.store.setAgentStatus(registeredTarget.agentId, "busy");
|
|
213
|
+
delivery = "push";
|
|
214
|
+
} else if (canPushProvider && !registeredTarget.targetId) {
|
|
215
|
+
chosenEngine = registeredTarget.engine;
|
|
216
|
+
} else {
|
|
217
|
+
chosenEngine = registeredTarget.engine || "custom";
|
|
218
|
+
task = this.store.createTask({
|
|
219
|
+
engine: chosenEngine,
|
|
220
|
+
targetAgent: registeredTarget.agentId,
|
|
221
|
+
fromAgent: from_agent,
|
|
222
|
+
title: title || `Task for ${registeredTarget.agentId}`,
|
|
223
|
+
prompt,
|
|
224
|
+
cwd: registeredTarget.workspace || resolvedCwd,
|
|
225
|
+
status: "working",
|
|
226
|
+
statusMessage: `Queued in inbox for ${registeredTarget.agentId}`,
|
|
227
|
+
ttlMs,
|
|
228
|
+
remoteStopSupported: false,
|
|
229
|
+
});
|
|
230
|
+
this.store.enqueueMessage({
|
|
231
|
+
toAgent: registeredTarget.agentId,
|
|
232
|
+
fromAgent: from_agent || "orchestrator",
|
|
233
|
+
title: title || `Task ${task.taskId}`,
|
|
234
|
+
content: prompt,
|
|
235
|
+
taskId: task.taskId,
|
|
236
|
+
delivery: "inbox",
|
|
237
|
+
});
|
|
238
|
+
this.store.setAgentStatus(registeredTarget.agentId, "busy");
|
|
239
|
+
delivery = "inbox";
|
|
240
|
+
}
|
|
241
|
+
} else if (targetIsRawUuid) {
|
|
242
|
+
const ag = this.providers.get("antigravity");
|
|
243
|
+
if (!ag?.probe().available) {
|
|
244
|
+
throw new Error("Antigravity daemon is not running; cannot dispatch to conversation UUID.");
|
|
245
|
+
}
|
|
246
|
+
chosenEngine = "antigravity";
|
|
247
|
+
task = this.store.createTask({
|
|
248
|
+
engine: "antigravity",
|
|
249
|
+
targetAgent: `ag:${cleanTarget.slice(0, 8)}`,
|
|
250
|
+
fromAgent: from_agent,
|
|
251
|
+
sessionId: cleanTarget,
|
|
252
|
+
title: title || "Follow-up task",
|
|
253
|
+
prompt,
|
|
254
|
+
cwd: resolvedCwd,
|
|
255
|
+
ttlMs,
|
|
256
|
+
remoteStopSupported: false,
|
|
257
|
+
});
|
|
258
|
+
await this.pushTask(ag, task, {
|
|
259
|
+
targetId: cleanTarget,
|
|
260
|
+
title: title || `Task ${task.taskId}`,
|
|
261
|
+
message: `[Task ${task.taskId}${from_agent ? ` from ${from_agent}` : ""}]\n${prompt}`,
|
|
262
|
+
});
|
|
263
|
+
delivery = "push";
|
|
264
|
+
} else if (cleanTarget && !targetIsEngine && !cleanEngine) {
|
|
265
|
+
chosenEngine = "custom";
|
|
266
|
+
this.store.registerAgent({
|
|
267
|
+
agentId: cleanTarget,
|
|
268
|
+
engine: "custom",
|
|
269
|
+
deliveryMode: "inbox",
|
|
270
|
+
implicit: true,
|
|
271
|
+
});
|
|
272
|
+
task = this.store.createTask({
|
|
273
|
+
engine: "custom",
|
|
274
|
+
targetAgent: cleanTarget,
|
|
275
|
+
fromAgent: from_agent,
|
|
276
|
+
title: title || `Task for ${cleanTarget}`,
|
|
277
|
+
prompt,
|
|
278
|
+
cwd: resolvedCwd,
|
|
279
|
+
status: "working",
|
|
280
|
+
statusMessage: `Queued in inbox for ${cleanTarget}`,
|
|
281
|
+
ttlMs,
|
|
282
|
+
remoteStopSupported: false,
|
|
283
|
+
});
|
|
284
|
+
this.store.enqueueMessage({
|
|
285
|
+
toAgent: cleanTarget,
|
|
286
|
+
fromAgent: from_agent || "orchestrator",
|
|
287
|
+
title: title || `Task ${task.taskId}`,
|
|
288
|
+
content: prompt,
|
|
289
|
+
taskId: task.taskId,
|
|
290
|
+
delivery: "inbox",
|
|
291
|
+
});
|
|
292
|
+
delivery = "inbox";
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (!task) {
|
|
296
|
+
const requestedEngine =
|
|
297
|
+
cleanEngine || (targetIsEngine ? cleanTarget.toLowerCase() : "") || chosenEngine || "auto";
|
|
298
|
+
const resolvedEngine = requestedEngine === "auto" ? this.pickAutoEngine() : requestedEngine;
|
|
299
|
+
if (!resolvedEngine || !this.providers.has(resolvedEngine)) {
|
|
300
|
+
const available = this.probeEngines()
|
|
301
|
+
.filter((e) => e.available)
|
|
302
|
+
.map((e) => e.engine);
|
|
303
|
+
throw new Error(
|
|
304
|
+
`No available local agent engine matched '${requestedEngine}'. Available engines: ${
|
|
305
|
+
available.length ? available.join(", ") : "none"
|
|
306
|
+
}.`
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
const provider = this.providers.get(resolvedEngine);
|
|
310
|
+
if (!provider.probe().available) {
|
|
311
|
+
throw new Error(`Engine '${resolvedEngine}' is not available: ${provider.probe().detail}`);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
task = this.store.createTask({
|
|
315
|
+
engine: resolvedEngine,
|
|
316
|
+
targetAgent: registeredTarget?.agentId || resolvedEngine,
|
|
317
|
+
fromAgent: from_agent,
|
|
318
|
+
title: title || `${resolvedEngine} task`,
|
|
319
|
+
prompt,
|
|
320
|
+
cwd: registeredTarget?.workspace || resolvedCwd,
|
|
321
|
+
ttlMs,
|
|
322
|
+
remoteStopSupported: resolvedEngine !== "antigravity",
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
try {
|
|
326
|
+
const launched = await provider.dispatch({
|
|
327
|
+
prompt,
|
|
328
|
+
cwd: task.cwd,
|
|
329
|
+
title: task.title,
|
|
330
|
+
model,
|
|
331
|
+
profile,
|
|
332
|
+
taskId: task.taskId,
|
|
333
|
+
store: this.store,
|
|
334
|
+
});
|
|
335
|
+
if (launched.initialOutput) {
|
|
336
|
+
this.store.appendTaskOutput(task.taskId, `${launched.initialOutput}\n`);
|
|
337
|
+
}
|
|
338
|
+
task = this.store.updateTask(task.taskId, {
|
|
339
|
+
sessionId: launched.sessionId || task.sessionId,
|
|
340
|
+
pid: launched.pid ?? task.pid,
|
|
341
|
+
status: launched.status || "working",
|
|
342
|
+
statusMessage: launched.statusMessage || task.statusMessage,
|
|
343
|
+
remoteStopSupported:
|
|
344
|
+
launched.remoteStopSupported !== undefined ? launched.remoteStopSupported : task.remoteStopSupported,
|
|
345
|
+
});
|
|
346
|
+
if (registeredTarget && task.sessionId) {
|
|
347
|
+
this.store.registerAgent({
|
|
348
|
+
agentId: registeredTarget.agentId,
|
|
349
|
+
targetId: task.sessionId,
|
|
350
|
+
deliveryMode: "push",
|
|
351
|
+
status: "busy",
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
} catch (err) {
|
|
355
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
356
|
+
task = this.store.updateTask(task.taskId, {
|
|
357
|
+
status: "failed",
|
|
358
|
+
statusMessage: message,
|
|
359
|
+
error: { code: -32000, message },
|
|
360
|
+
});
|
|
361
|
+
throw err;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const waitMs = Math.min(Math.max(Number(wait_seconds) || 0, 0), 20) * 1000;
|
|
366
|
+
if (waitMs > 0 && !TERMINAL_TASK_STATUSES.has(task.status)) {
|
|
367
|
+
const deadline = Date.now() + waitMs;
|
|
368
|
+
while (Date.now() < deadline) {
|
|
369
|
+
await new Promise((r) => setTimeout(r, 400));
|
|
370
|
+
const refreshed = await this._refreshTask(task.taskId);
|
|
371
|
+
if (refreshed) task = refreshed;
|
|
372
|
+
if (TERMINAL_TASK_STATUSES.has(task.status) || task.status === "input_required") {
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
await this.audit({
|
|
379
|
+
tool: "agent_dispatch",
|
|
380
|
+
taskId: task.taskId,
|
|
381
|
+
engine: task.engine,
|
|
382
|
+
targetAgent: task.targetAgent,
|
|
383
|
+
delivery,
|
|
384
|
+
status: task.status,
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
return this._withPiggyback(
|
|
388
|
+
{
|
|
389
|
+
ok: true,
|
|
390
|
+
delivery,
|
|
391
|
+
task: this._formatTaskView(task),
|
|
392
|
+
},
|
|
393
|
+
from_agent
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
async _refreshTask(taskId) {
|
|
398
|
+
let task = this.store.getTask(taskId);
|
|
399
|
+
if (!task) return null;
|
|
400
|
+
if (TERMINAL_TASK_STATUSES.has(task.status)) return task;
|
|
401
|
+
|
|
402
|
+
const provider = this.providers.get(task.engine);
|
|
403
|
+
if (provider?.inspect) {
|
|
404
|
+
const inspected = await provider.inspect(task).catch(() => null);
|
|
405
|
+
if (inspected) {
|
|
406
|
+
task = this.store.updateTask(task.taskId, inspected) || task;
|
|
407
|
+
if (TERMINAL_TASK_STATUSES.has(task.status) && task.targetAgent) {
|
|
408
|
+
this.store.setAgentStatus(task.targetAgent, "idle");
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return task;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
_formatTaskView(task) {
|
|
416
|
+
const view = {
|
|
417
|
+
taskId: task.taskId,
|
|
418
|
+
status: task.status,
|
|
419
|
+
statusMessage: task.statusMessage,
|
|
420
|
+
createdAt: task.createdAt,
|
|
421
|
+
lastUpdatedAt: task.lastUpdatedAt,
|
|
422
|
+
ttlMs: task.ttlMs,
|
|
423
|
+
...(task.pollIntervalMs ? { pollIntervalMs: task.pollIntervalMs } : {}),
|
|
424
|
+
engine: task.engine,
|
|
425
|
+
targetAgent: task.targetAgent || undefined,
|
|
426
|
+
sessionId: task.sessionId || undefined,
|
|
427
|
+
fromAgent: task.fromAgent || undefined,
|
|
428
|
+
title: task.title || undefined,
|
|
429
|
+
remoteStopSupported: task.remoteStopSupported,
|
|
430
|
+
};
|
|
431
|
+
if (task.status === "completed" && task.result !== undefined) {
|
|
432
|
+
view.result = task.result;
|
|
433
|
+
}
|
|
434
|
+
if (task.status === "failed" && task.error !== undefined) {
|
|
435
|
+
view.error = task.error;
|
|
436
|
+
}
|
|
437
|
+
if (task.status === "input_required" && task.inputRequests !== undefined) {
|
|
438
|
+
view.inputRequests = task.inputRequests;
|
|
439
|
+
}
|
|
440
|
+
return view;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
async getOrUpdateResult({
|
|
444
|
+
task_id,
|
|
445
|
+
action = "get",
|
|
446
|
+
message = "",
|
|
447
|
+
result_text = "",
|
|
448
|
+
output_cursor,
|
|
449
|
+
max_chars = 4000,
|
|
450
|
+
from_agent = "",
|
|
451
|
+
}) {
|
|
452
|
+
let task = this.store.getTask(task_id);
|
|
453
|
+
if (!task) {
|
|
454
|
+
throw new Error(`Task not found or expired: ${task_id}`);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (action === "cancel") {
|
|
458
|
+
const provider = this.providers.get(task.engine);
|
|
459
|
+
let cancelMeta = { processKilled: false, remoteStopSupported: Boolean(task.remoteStopSupported) };
|
|
460
|
+
if (!TERMINAL_TASK_STATUSES.has(task.status) && provider?.cancel) {
|
|
461
|
+
cancelMeta = await provider.cancel(task, message).catch(() => cancelMeta);
|
|
462
|
+
}
|
|
463
|
+
task = this.store.updateTask(task.taskId, {
|
|
464
|
+
status: "cancelled",
|
|
465
|
+
statusMessage: message || "Cancelled by caller",
|
|
466
|
+
pid: null,
|
|
467
|
+
remoteStopSupported: cancelMeta.remoteStopSupported,
|
|
468
|
+
});
|
|
469
|
+
if (task.targetAgent) {
|
|
470
|
+
this.store.setAgentStatus(task.targetAgent, "idle");
|
|
471
|
+
}
|
|
472
|
+
await this.audit({ tool: "agent_result", action: "cancel", taskId: task.taskId, status: task.status });
|
|
473
|
+
return this._withPiggyback(
|
|
474
|
+
{
|
|
475
|
+
ok: true,
|
|
476
|
+
action: "cancel",
|
|
477
|
+
processKilled: cancelMeta.processKilled,
|
|
478
|
+
remoteStopSupported: cancelMeta.remoteStopSupported,
|
|
479
|
+
task: this._formatTaskView(task),
|
|
480
|
+
},
|
|
481
|
+
from_agent
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
if (action === "complete" || action === "fail" || action === "input_required") {
|
|
486
|
+
if (TERMINAL_TASK_STATUSES.has(task.status)) {
|
|
487
|
+
throw new Error(`Task ${task.taskId} is already in terminal state '${task.status}'.`);
|
|
488
|
+
}
|
|
489
|
+
if (result_text) {
|
|
490
|
+
this.store.appendTaskOutput(task.taskId, `${result_text}\n`);
|
|
491
|
+
}
|
|
492
|
+
const patch =
|
|
493
|
+
action === "complete"
|
|
494
|
+
? {
|
|
495
|
+
status: "completed",
|
|
496
|
+
statusMessage: message || "Completed by target agent",
|
|
497
|
+
result: { summary: result_text || message || "Completed" },
|
|
498
|
+
}
|
|
499
|
+
: action === "fail"
|
|
500
|
+
? {
|
|
501
|
+
status: "failed",
|
|
502
|
+
statusMessage: message || result_text || "Failed",
|
|
503
|
+
error: { code: -32000, message: message || result_text || "Task failed" },
|
|
504
|
+
}
|
|
505
|
+
: {
|
|
506
|
+
status: "input_required",
|
|
507
|
+
statusMessage: message || "Waiting for input",
|
|
508
|
+
inputRequests: {
|
|
509
|
+
input_1: { kind: "message", prompt: message || result_text || "Input required" },
|
|
510
|
+
},
|
|
511
|
+
};
|
|
512
|
+
task = this.store.updateTask(task.taskId, patch);
|
|
513
|
+
if (TERMINAL_TASK_STATUSES.has(task.status) && task.targetAgent) {
|
|
514
|
+
this.store.setAgentStatus(task.targetAgent, "idle");
|
|
515
|
+
}
|
|
516
|
+
if (task.fromAgent && task.fromAgent !== from_agent) {
|
|
517
|
+
this.store.enqueueMessage({
|
|
518
|
+
toAgent: task.fromAgent,
|
|
519
|
+
fromAgent: from_agent || task.targetAgent || "agent",
|
|
520
|
+
title: `Task ${task.taskId} ${task.status}`,
|
|
521
|
+
content: (result_text || message || task.statusMessage || "").slice(0, 1000),
|
|
522
|
+
taskId: task.taskId,
|
|
523
|
+
delivery: "inbox",
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
await this.audit({ tool: "agent_result", action, taskId: task.taskId, status: task.status });
|
|
527
|
+
return this._withPiggyback({ ok: true, action, task: this._formatTaskView(task) }, from_agent);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
task = (await this._refreshTask(task.taskId)) || task;
|
|
531
|
+
const output = this.store.readTaskOutput(task.taskId, {
|
|
532
|
+
cursor: output_cursor !== undefined ? output_cursor : null,
|
|
533
|
+
maxChars: max_chars,
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
return this._withPiggyback(
|
|
537
|
+
{
|
|
538
|
+
ok: true,
|
|
539
|
+
task: this._formatTaskView(task),
|
|
540
|
+
output,
|
|
541
|
+
},
|
|
542
|
+
from_agent
|
|
543
|
+
);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
async notifyOrInbox({
|
|
547
|
+
action,
|
|
548
|
+
to_agent = "",
|
|
549
|
+
message = "",
|
|
550
|
+
title = "",
|
|
551
|
+
task_id = "",
|
|
552
|
+
from_agent = "",
|
|
553
|
+
mark_read = true,
|
|
554
|
+
include_read = false,
|
|
555
|
+
limit = 20,
|
|
556
|
+
}) {
|
|
557
|
+
const mode = action || (to_agent && message ? "send" : "inbox");
|
|
558
|
+
if (mode === "inbox") {
|
|
559
|
+
const owner = from_agent || to_agent;
|
|
560
|
+
if (!owner) {
|
|
561
|
+
throw new Error("from_agent (or to_agent) is required when reading inbox.");
|
|
562
|
+
}
|
|
563
|
+
this.store.touchAgent(owner);
|
|
564
|
+
const inboxResult = this.store.readInbox(owner, {
|
|
565
|
+
markRead: mark_read !== false,
|
|
566
|
+
includeRead: Boolean(include_read),
|
|
567
|
+
limit,
|
|
568
|
+
});
|
|
569
|
+
return { ok: true, action: "inbox", ...inboxResult };
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
const cleanTo = String(to_agent || "").trim();
|
|
573
|
+
if (!cleanTo || !String(message || "").trim()) {
|
|
574
|
+
throw new Error("to_agent and message are required when sending a notification.");
|
|
575
|
+
}
|
|
576
|
+
if (from_agent) {
|
|
577
|
+
this.store.touchAgent(from_agent);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
const reg = this.store.resolveAgent(cleanTo);
|
|
581
|
+
const rawUuid = !reg && isUuid(cleanTo) ? cleanTo : "";
|
|
582
|
+
let delivery = "inbox";
|
|
583
|
+
let pushError = "";
|
|
584
|
+
let pushOutput = "";
|
|
585
|
+
|
|
586
|
+
const pushEngine = reg?.deliveryMode === "push" ? reg.engine : rawUuid ? "antigravity" : "";
|
|
587
|
+
const pushTargetId = reg?.targetId || rawUuid;
|
|
588
|
+
|
|
589
|
+
if (pushEngine && pushTargetId && this.providers.has(pushEngine)) {
|
|
590
|
+
const provider = this.providers.get(pushEngine);
|
|
591
|
+
if (provider.probe().available && provider.pushMessage) {
|
|
592
|
+
try {
|
|
593
|
+
const pushRes = await provider.pushMessage({
|
|
594
|
+
targetId: pushTargetId,
|
|
595
|
+
title: title || (from_agent ? `Message from ${from_agent}` : "Agent Notification"),
|
|
596
|
+
message: from_agent ? `[From ${from_agent}] ${message}` : message,
|
|
597
|
+
cwd: reg?.workspace || this.defaultCwd,
|
|
598
|
+
taskId: task_id || undefined,
|
|
599
|
+
store: this.store,
|
|
600
|
+
});
|
|
601
|
+
delivery = "push";
|
|
602
|
+
pushOutput = pushRes?.output || "";
|
|
603
|
+
} catch (err) {
|
|
604
|
+
pushError = err instanceof Error ? err.message : String(err);
|
|
605
|
+
delivery = "inbox_fallback";
|
|
606
|
+
}
|
|
607
|
+
} else {
|
|
608
|
+
pushError = `Engine '${pushEngine}' unavailable for push`;
|
|
609
|
+
delivery = "inbox_fallback";
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
let inboxEntry = null;
|
|
614
|
+
if (delivery !== "push") {
|
|
615
|
+
inboxEntry = this.store.enqueueMessage({
|
|
616
|
+
toAgent: reg ? reg.agentId : cleanTo,
|
|
617
|
+
fromAgent: from_agent || "external",
|
|
618
|
+
title: title || "Notification",
|
|
619
|
+
content: message,
|
|
620
|
+
taskId: task_id,
|
|
621
|
+
delivery,
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
let updatedTask = null;
|
|
626
|
+
if (task_id) {
|
|
627
|
+
const existingTask = this.store.getTask(task_id);
|
|
628
|
+
if (existingTask && existingTask.status === "input_required" && existingTask.targetAgent === (reg?.agentId || cleanTo)) {
|
|
629
|
+
updatedTask = this.store.updateTask(task_id, {
|
|
630
|
+
status: "working",
|
|
631
|
+
statusMessage: `Input supplied by ${from_agent || "caller"}`,
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
await this.audit({
|
|
637
|
+
tool: "agent_notify",
|
|
638
|
+
action: "send",
|
|
639
|
+
toAgent: reg ? reg.agentId : cleanTo,
|
|
640
|
+
fromAgent: from_agent || undefined,
|
|
641
|
+
delivery,
|
|
642
|
+
taskId: task_id || undefined,
|
|
643
|
+
});
|
|
644
|
+
|
|
645
|
+
return this._withPiggyback(
|
|
646
|
+
{
|
|
647
|
+
ok: true,
|
|
648
|
+
action: "send",
|
|
649
|
+
toAgent: reg ? reg.agentId : cleanTo,
|
|
650
|
+
delivery,
|
|
651
|
+
...(pushOutput ? { pushOutput } : {}),
|
|
652
|
+
...(pushError ? { pushFallbackReason: pushError } : {}),
|
|
653
|
+
...(inboxEntry ? { messageId: inboxEntry.messageId } : {}),
|
|
654
|
+
...(updatedTask ? { task: this._formatTaskView(updatedTask) } : {}),
|
|
655
|
+
},
|
|
656
|
+
from_agent
|
|
657
|
+
);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
export function createAgentHub(options) {
|
|
662
|
+
return new AgentHub(options);
|
|
663
|
+
}
|