@fro.bot/space-bus 0.2.1 → 0.4.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.
- package/README.md +28 -2
- package/dist/config.d.ts +19 -0
- package/dist/config.js +90 -0
- package/dist/contract.d.ts +1814 -0
- package/dist/contract.js +100 -0
- package/dist/core.d.ts +29 -13
- package/dist/core.js +612 -0
- package/dist/format.d.ts +14 -0
- package/dist/format.js +63 -0
- package/dist/index.js +295 -135
- package/dist/mcp.js +349 -143
- package/package.json +18 -2
package/dist/mcp.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// src/mcp.ts
|
|
3
|
-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
4
|
-
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
|
-
import { z as z3 } from "zod";
|
|
6
|
-
|
|
7
|
-
// src/core.ts
|
|
8
|
-
import { existsSync as existsSync2 } from "node:fs";
|
|
9
|
-
import { z as z2 } from "zod";
|
|
10
|
-
|
|
11
2
|
// src/config.ts
|
|
12
3
|
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
13
4
|
import { homedir } from "node:os";
|
|
@@ -69,53 +60,29 @@ function getRoster(directory) {
|
|
|
69
60
|
return parsed.data;
|
|
70
61
|
}
|
|
71
62
|
function getProjects(manifest) {
|
|
72
|
-
return manifest.projects.map((p) =>
|
|
73
|
-
|
|
74
|
-
expandedPath:
|
|
75
|
-
})
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// src/core.ts
|
|
79
|
-
function findProject(projects, name) {
|
|
80
|
-
return projects.find((p) => p.name === name);
|
|
81
|
-
}
|
|
82
|
-
function err(error) {
|
|
83
|
-
return { ok: false, error };
|
|
63
|
+
return manifest.projects.map((p) => {
|
|
64
|
+
const expandedPath = expandHome(p.path);
|
|
65
|
+
return { ...p, expandedPath, exists: existsSync(expandedPath) };
|
|
66
|
+
});
|
|
84
67
|
}
|
|
85
|
-
function
|
|
68
|
+
function getCredentials() {
|
|
86
69
|
const password = process.env["OPENCODE_SERVER_PASSWORD"];
|
|
87
70
|
if (!password)
|
|
88
71
|
return {};
|
|
89
72
|
const username = process.env["OPENCODE_SERVER_USERNAME"] ?? "opencode";
|
|
90
|
-
|
|
91
|
-
return { Authorization: `Basic ${token}` };
|
|
73
|
+
return { username, password };
|
|
92
74
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
...authHeader(),
|
|
101
|
-
...init?.headers
|
|
102
|
-
},
|
|
103
|
-
redirect: "error",
|
|
104
|
-
signal: AbortSignal.timeout(30000)
|
|
105
|
-
});
|
|
106
|
-
const bodyText = await res.text().catch(() => "<unreadable body>");
|
|
107
|
-
return { res, bodyText };
|
|
108
|
-
} catch (e) {
|
|
109
|
-
const message = e.message;
|
|
110
|
-
return {
|
|
111
|
-
res: new Response(null, {
|
|
112
|
-
status: 599,
|
|
113
|
-
statusText: "space-bus: request failed"
|
|
114
|
-
}),
|
|
115
|
-
bodyText: `space-bus: request failed: ${message}`
|
|
116
|
-
};
|
|
117
|
-
}
|
|
75
|
+
function loadContext(directory) {
|
|
76
|
+
const manifest = getRoster(directory);
|
|
77
|
+
const projects = getProjects(manifest);
|
|
78
|
+
return {
|
|
79
|
+
roster: { server: manifest.server, projects },
|
|
80
|
+
credentials: getCredentials()
|
|
81
|
+
};
|
|
118
82
|
}
|
|
83
|
+
|
|
84
|
+
// src/contract.ts
|
|
85
|
+
import { z as z2 } from "zod";
|
|
119
86
|
var sessionSchema = z2.object({
|
|
120
87
|
id: z2.string(),
|
|
121
88
|
directory: z2.string().optional(),
|
|
@@ -153,8 +120,92 @@ var sessionSummarySchema = z2.object({
|
|
|
153
120
|
diffs: z2.array(turnDiffEntrySchema).optional()
|
|
154
121
|
}).passthrough().optional()
|
|
155
122
|
}).passthrough();
|
|
156
|
-
|
|
157
|
-
|
|
123
|
+
var messagePartSchema = z2.object({ type: z2.string(), text: z2.string().optional() }).passthrough();
|
|
124
|
+
var messageEnvelopeSchema = z2.object({
|
|
125
|
+
info: z2.object({ role: z2.string() }).passthrough(),
|
|
126
|
+
parts: z2.array(messagePartSchema)
|
|
127
|
+
}).passthrough();
|
|
128
|
+
var messageListSchema = z2.array(messageEnvelopeSchema);
|
|
129
|
+
var pendingQuestionEntrySchema = z2.object({
|
|
130
|
+
id: z2.string(),
|
|
131
|
+
sessionID: z2.string(),
|
|
132
|
+
questions: z2.array(z2.object({
|
|
133
|
+
question: z2.string().optional(),
|
|
134
|
+
options: z2.array(z2.object({ label: z2.string().optional() }).passthrough()).optional()
|
|
135
|
+
}).passthrough()).optional()
|
|
136
|
+
}).passthrough();
|
|
137
|
+
var pendingQuestionListSchema = z2.array(pendingQuestionEntrySchema);
|
|
138
|
+
var questionEntrySchema = z2.object({ id: z2.string(), sessionID: z2.string() }).passthrough();
|
|
139
|
+
var questionListSchema = z2.array(questionEntrySchema);
|
|
140
|
+
var projectSchema = z2.object({
|
|
141
|
+
name: z2.string(),
|
|
142
|
+
path: z2.string(),
|
|
143
|
+
description: z2.string(),
|
|
144
|
+
expandedPath: z2.string(),
|
|
145
|
+
exists: z2.boolean()
|
|
146
|
+
}).passthrough();
|
|
147
|
+
var rosterSchema = z2.object({
|
|
148
|
+
server: z2.object({ baseUrl: z2.string().url() }),
|
|
149
|
+
projects: z2.array(projectSchema)
|
|
150
|
+
});
|
|
151
|
+
var credentialsSchema = z2.object({
|
|
152
|
+
username: z2.string().optional(),
|
|
153
|
+
password: z2.string().optional()
|
|
154
|
+
});
|
|
155
|
+
var busContextSchema = z2.object({
|
|
156
|
+
roster: rosterSchema,
|
|
157
|
+
credentials: credentialsSchema.optional()
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// src/core.ts
|
|
161
|
+
function findProject(projects, name) {
|
|
162
|
+
return projects.find((p) => p.name === name);
|
|
163
|
+
}
|
|
164
|
+
function err(error) {
|
|
165
|
+
return { ok: false, error };
|
|
166
|
+
}
|
|
167
|
+
function toBase64(s) {
|
|
168
|
+
const bytes = new TextEncoder().encode(s);
|
|
169
|
+
let bin = "";
|
|
170
|
+
for (const b of bytes)
|
|
171
|
+
bin += String.fromCharCode(b);
|
|
172
|
+
return btoa(bin);
|
|
173
|
+
}
|
|
174
|
+
function authHeader(credentials) {
|
|
175
|
+
if (!credentials.password)
|
|
176
|
+
return {};
|
|
177
|
+
const username = credentials.username ?? "opencode";
|
|
178
|
+
const token = toBase64(`${username}:${credentials.password}`);
|
|
179
|
+
return { Authorization: `Basic ${token}` };
|
|
180
|
+
}
|
|
181
|
+
async function api(baseUrl, credentials, directory, path, init) {
|
|
182
|
+
try {
|
|
183
|
+
const res = await fetch(`${baseUrl}${path}`, {
|
|
184
|
+
...init,
|
|
185
|
+
headers: {
|
|
186
|
+
"content-type": "application/json",
|
|
187
|
+
"x-opencode-directory": directory,
|
|
188
|
+
...authHeader(credentials),
|
|
189
|
+
...init?.headers
|
|
190
|
+
},
|
|
191
|
+
redirect: "error",
|
|
192
|
+
signal: AbortSignal.timeout(30000)
|
|
193
|
+
});
|
|
194
|
+
const bodyText = await res.text().catch(() => "<unreadable body>");
|
|
195
|
+
return { res, bodyText };
|
|
196
|
+
} catch (e) {
|
|
197
|
+
const message = e.message;
|
|
198
|
+
return {
|
|
199
|
+
res: new Response(null, {
|
|
200
|
+
status: 599,
|
|
201
|
+
statusText: "space-bus: request failed"
|
|
202
|
+
}),
|
|
203
|
+
bodyText: `space-bus: request failed: ${message}`
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
async function fetchTurnDiffs(baseUrl, credentials, directory, sessionId) {
|
|
208
|
+
const { res, bodyText } = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(sessionId)}/message?limit=100`);
|
|
158
209
|
if (!res.ok)
|
|
159
210
|
return [];
|
|
160
211
|
let messages;
|
|
@@ -177,8 +228,8 @@ async function fetchTurnDiffs(baseUrl, directory, sessionId) {
|
|
|
177
228
|
}
|
|
178
229
|
return Array.from(byFile.values());
|
|
179
230
|
}
|
|
180
|
-
async function fetchDiffWithFallback(baseUrl, directory, sessionId) {
|
|
181
|
-
const diffRes = await api(baseUrl, directory, `/session/${encodeURIComponent(sessionId)}/diff`);
|
|
231
|
+
async function fetchDiffWithFallback(baseUrl, credentials, directory, sessionId) {
|
|
232
|
+
const diffRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(sessionId)}/diff`);
|
|
182
233
|
let diff = [];
|
|
183
234
|
try {
|
|
184
235
|
diff = diffRes.res.ok ? diffSchema.parse(JSON.parse(diffRes.bodyText)) : [];
|
|
@@ -189,7 +240,7 @@ async function fetchDiffWithFallback(baseUrl, directory, sessionId) {
|
|
|
189
240
|
return { diff, diffSource: "session" };
|
|
190
241
|
}
|
|
191
242
|
try {
|
|
192
|
-
const sessionRes = await api(baseUrl, directory, `/session/${encodeURIComponent(sessionId)}`);
|
|
243
|
+
const sessionRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(sessionId)}`);
|
|
193
244
|
if (sessionRes.res.ok) {
|
|
194
245
|
const parsed = sessionSummarySchema.parse(JSON.parse(sessionRes.bodyText));
|
|
195
246
|
const summaryDiffs = parsed.summary?.diffs;
|
|
@@ -199,13 +250,13 @@ async function fetchDiffWithFallback(baseUrl, directory, sessionId) {
|
|
|
199
250
|
}
|
|
200
251
|
} catch {}
|
|
201
252
|
try {
|
|
202
|
-
const turnDiffs = await fetchTurnDiffs(baseUrl, directory, sessionId);
|
|
253
|
+
const turnDiffs = await fetchTurnDiffs(baseUrl, credentials, directory, sessionId);
|
|
203
254
|
if (turnDiffs.length > 0) {
|
|
204
255
|
return { diff: turnDiffs, diffSource: "turns" };
|
|
205
256
|
}
|
|
206
257
|
} catch {}
|
|
207
258
|
try {
|
|
208
|
-
const vcsRes = await api(baseUrl, directory, "/vcs/status");
|
|
259
|
+
const vcsRes = await api(baseUrl, credentials, directory, "/vcs/status");
|
|
209
260
|
if (vcsRes.res.ok) {
|
|
210
261
|
const vcsStatus = vcsStatusSchema.parse(JSON.parse(vcsRes.bodyText));
|
|
211
262
|
if (vcsStatus.length > 0) {
|
|
@@ -223,20 +274,28 @@ async function fetchDiffWithFallback(baseUrl, directory, sessionId) {
|
|
|
223
274
|
} catch {}
|
|
224
275
|
return { diff, diffSource: "session" };
|
|
225
276
|
}
|
|
226
|
-
var
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
})
|
|
231
|
-
|
|
232
|
-
|
|
277
|
+
var ALLOWED_HOSTS = new Set(["127.0.0.1", "::1", "[::1]", "localhost"]);
|
|
278
|
+
function validateContext(context) {
|
|
279
|
+
const parsed = busContextSchema.safeParse(context);
|
|
280
|
+
if (!parsed.success) {
|
|
281
|
+
return err(`space-bus: invalid context: ${parsed.error.message}`);
|
|
282
|
+
}
|
|
283
|
+
const { roster, credentials } = parsed.data;
|
|
284
|
+
let hostname;
|
|
233
285
|
try {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
return
|
|
237
|
-
} catch (e) {
|
|
238
|
-
return err(e.message);
|
|
286
|
+
hostname = new URL(roster.server.baseUrl).hostname;
|
|
287
|
+
} catch {
|
|
288
|
+
return err("space-bus: context roster server.baseUrl is not a valid URL");
|
|
239
289
|
}
|
|
290
|
+
if (!ALLOWED_HOSTS.has(hostname)) {
|
|
291
|
+
return err(`space-bus: context roster server.baseUrl must point to localhost (got ${hostname}) — refusing to send credentials off-machine`);
|
|
292
|
+
}
|
|
293
|
+
return {
|
|
294
|
+
ok: true,
|
|
295
|
+
baseUrl: roster.server.baseUrl,
|
|
296
|
+
projects: roster.projects,
|
|
297
|
+
credentials: credentials ?? {}
|
|
298
|
+
};
|
|
240
299
|
}
|
|
241
300
|
function resolveProjectOrErr(projects, name) {
|
|
242
301
|
const project = findProject(projects, name);
|
|
@@ -244,19 +303,18 @@ function resolveProjectOrErr(projects, name) {
|
|
|
244
303
|
const valid = projects.map((p) => p.name).join(", ");
|
|
245
304
|
return err(`space-bus: unknown project "${name}". Valid projects: ${valid}`);
|
|
246
305
|
}
|
|
247
|
-
if (!
|
|
306
|
+
if (!project.exists) {
|
|
248
307
|
return err(`space-bus: project "${name}" path does not exist on disk: ${project.expandedPath}`);
|
|
249
308
|
}
|
|
250
309
|
return { ok: true, project };
|
|
251
310
|
}
|
|
252
311
|
async function roster(opts) {
|
|
253
|
-
const ctx =
|
|
312
|
+
const ctx = validateContext(opts.context);
|
|
254
313
|
if (!ctx.ok)
|
|
255
314
|
return ctx;
|
|
256
|
-
const { baseUrl, projects } = ctx;
|
|
315
|
+
const { baseUrl, projects, credentials } = ctx;
|
|
257
316
|
const results = await Promise.all(projects.map(async (p) => {
|
|
258
|
-
|
|
259
|
-
if (!pathExists) {
|
|
317
|
+
if (!p.exists) {
|
|
260
318
|
return {
|
|
261
319
|
name: p.name,
|
|
262
320
|
path: p.expandedPath,
|
|
@@ -266,8 +324,8 @@ async function roster(opts) {
|
|
|
266
324
|
}
|
|
267
325
|
try {
|
|
268
326
|
const [statusRes, listRes] = await Promise.all([
|
|
269
|
-
api(baseUrl, p.expandedPath, "/session/status"),
|
|
270
|
-
api(baseUrl, p.expandedPath, "/session?limit=101")
|
|
327
|
+
api(baseUrl, credentials, p.expandedPath, "/session/status"),
|
|
328
|
+
api(baseUrl, credentials, p.expandedPath, "/session?limit=101")
|
|
271
329
|
]);
|
|
272
330
|
if (!statusRes.res.ok || !listRes.res.ok) {
|
|
273
331
|
return {
|
|
@@ -303,13 +361,13 @@ async function roster(opts) {
|
|
|
303
361
|
}));
|
|
304
362
|
return { ok: true, projects: results };
|
|
305
363
|
}
|
|
306
|
-
async function dispatchNew(baseUrl, projects, project, prompt, title) {
|
|
364
|
+
async function dispatchNew(baseUrl, credentials, projects, project, prompt, title) {
|
|
307
365
|
const resolved = resolveProjectOrErr(projects, project);
|
|
308
366
|
if (!resolved.ok)
|
|
309
367
|
return resolved;
|
|
310
368
|
const directory = resolved.project.expandedPath;
|
|
311
369
|
const sessionTitle = title ?? `bus: ${prompt.slice(0, 60)}`;
|
|
312
|
-
const createRes = await api(baseUrl, directory, "/session", {
|
|
370
|
+
const createRes = await api(baseUrl, credentials, directory, "/session", {
|
|
313
371
|
method: "POST",
|
|
314
372
|
body: JSON.stringify({ title: sessionTitle })
|
|
315
373
|
});
|
|
@@ -322,7 +380,7 @@ async function dispatchNew(baseUrl, projects, project, prompt, title) {
|
|
|
322
380
|
} catch (e) {
|
|
323
381
|
return err(`space-bus: unexpected /session response shape: ${e.message}`);
|
|
324
382
|
}
|
|
325
|
-
const promptRes = await api(baseUrl, directory, `/session/${encodeURIComponent(session.id)}/prompt_async`, {
|
|
383
|
+
const promptRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(session.id)}/prompt_async`, {
|
|
326
384
|
method: "POST",
|
|
327
385
|
body: JSON.stringify({ parts: [{ type: "text", text: prompt }] })
|
|
328
386
|
});
|
|
@@ -343,7 +401,6 @@ function toDispatchArgs(input) {
|
|
|
343
401
|
ok: true,
|
|
344
402
|
prompt: input.prompt,
|
|
345
403
|
title: input.title,
|
|
346
|
-
directory: input.directory,
|
|
347
404
|
project: input.project
|
|
348
405
|
};
|
|
349
406
|
}
|
|
@@ -351,16 +408,15 @@ function toDispatchArgs(input) {
|
|
|
351
408
|
ok: true,
|
|
352
409
|
prompt: input.prompt,
|
|
353
410
|
title: input.title,
|
|
354
|
-
directory: input.directory,
|
|
355
411
|
sessionId: input.sessionId,
|
|
356
412
|
project: input.project
|
|
357
413
|
};
|
|
358
414
|
}
|
|
359
|
-
async function dispatch(args) {
|
|
360
|
-
const ctx =
|
|
415
|
+
async function dispatch(args, opts) {
|
|
416
|
+
const ctx = validateContext(opts.context);
|
|
361
417
|
if (!ctx.ok)
|
|
362
418
|
return ctx;
|
|
363
|
-
const { baseUrl, projects } = ctx;
|
|
419
|
+
const { baseUrl, projects, credentials } = ctx;
|
|
364
420
|
if (args.sessionId !== undefined && args.sessionId === "") {
|
|
365
421
|
return err("space-bus: sessionId must be a non-empty string");
|
|
366
422
|
}
|
|
@@ -368,7 +424,7 @@ async function dispatch(args) {
|
|
|
368
424
|
if (!args.project) {
|
|
369
425
|
return err("space-bus: project is required when starting a new session");
|
|
370
426
|
}
|
|
371
|
-
const r = await dispatchNew(baseUrl, projects, args.project, args.prompt, args.title);
|
|
427
|
+
const r = await dispatchNew(baseUrl, credentials, projects, args.project, args.prompt, args.title);
|
|
372
428
|
if (!r.ok)
|
|
373
429
|
return r;
|
|
374
430
|
return {
|
|
@@ -379,20 +435,20 @@ async function dispatch(args) {
|
|
|
379
435
|
directory: r.directory
|
|
380
436
|
};
|
|
381
437
|
}
|
|
382
|
-
const loc = await findSessionDirectory(baseUrl, projects, args.sessionId);
|
|
438
|
+
const loc = await findSessionDirectory(baseUrl, credentials, projects, args.sessionId);
|
|
383
439
|
if (!loc.ok)
|
|
384
440
|
return loc;
|
|
385
441
|
const { directory, project } = loc;
|
|
386
442
|
if (args.project && args.project !== project) {
|
|
387
443
|
return err(`space-bus: session ${args.sessionId} belongs to project "${project}", not "${args.project}" — refusing to steer the wrong session`);
|
|
388
444
|
}
|
|
389
|
-
return steerSession(baseUrl, args.sessionId, args.prompt, directory, project);
|
|
445
|
+
return steerSession(baseUrl, credentials, args.sessionId, args.prompt, directory, project);
|
|
390
446
|
}
|
|
391
|
-
async function findSessionDirectory(baseUrl, projects, sessionId) {
|
|
447
|
+
async function findSessionDirectory(baseUrl, credentials, projects, sessionId) {
|
|
392
448
|
for (const p of projects) {
|
|
393
|
-
if (!
|
|
449
|
+
if (!p.exists)
|
|
394
450
|
continue;
|
|
395
|
-
const { res, bodyText } = await api(baseUrl, p.expandedPath, `/session/${encodeURIComponent(sessionId)}`);
|
|
451
|
+
const { res, bodyText } = await api(baseUrl, credentials, p.expandedPath, `/session/${encodeURIComponent(sessionId)}`);
|
|
396
452
|
if (!res.ok)
|
|
397
453
|
continue;
|
|
398
454
|
let session;
|
|
@@ -409,48 +465,43 @@ async function findSessionDirectory(baseUrl, projects, sessionId) {
|
|
|
409
465
|
}
|
|
410
466
|
return err(`space-bus: no manifest project has a session with id ${sessionId}`);
|
|
411
467
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
var pendingQuestionListSchema = z2.array(pendingQuestionEntrySchema);
|
|
421
|
-
async function fetchPendingQuestion(baseUrl, directory, sessionId) {
|
|
468
|
+
function formatQuestionEntry(entry) {
|
|
469
|
+
const firstQuestion = entry.questions?.[0];
|
|
470
|
+
const text = firstQuestion?.question ?? "";
|
|
471
|
+
const preview = text.length > 140 ? `${text.slice(0, 140)}…` : text;
|
|
472
|
+
const options = (firstQuestion?.options ?? []).map((o) => o.label ?? "").filter((l) => l.length > 0);
|
|
473
|
+
return { sessionId: entry.sessionID, preview, options };
|
|
474
|
+
}
|
|
475
|
+
async function fetchPendingQuestion(baseUrl, credentials, directory, sessionId) {
|
|
422
476
|
try {
|
|
423
|
-
const { res, bodyText } = await api(baseUrl, directory, "/question");
|
|
477
|
+
const { res, bodyText } = await api(baseUrl, credentials, directory, "/question");
|
|
424
478
|
if (!res.ok)
|
|
425
479
|
return;
|
|
426
480
|
const entries = pendingQuestionListSchema.parse(JSON.parse(bodyText));
|
|
427
481
|
const entry = entries.find((e) => e.sessionID === sessionId);
|
|
428
482
|
if (!entry)
|
|
429
483
|
return;
|
|
430
|
-
const
|
|
431
|
-
const text = firstQuestion?.question ?? "";
|
|
432
|
-
const preview = text.length > 140 ? `${text.slice(0, 140)}…` : text;
|
|
433
|
-
const options = (firstQuestion?.options ?? []).map((o) => o.label ?? "").filter((l) => l.length > 0);
|
|
484
|
+
const { preview, options } = formatQuestionEntry(entry);
|
|
434
485
|
return { preview, options };
|
|
435
486
|
} catch {
|
|
436
487
|
return;
|
|
437
488
|
}
|
|
438
489
|
}
|
|
439
490
|
async function status(sessionId, opts) {
|
|
440
|
-
const ctx =
|
|
491
|
+
const ctx = validateContext(opts.context);
|
|
441
492
|
if (!ctx.ok)
|
|
442
493
|
return ctx;
|
|
443
|
-
const { baseUrl, projects } = ctx;
|
|
444
|
-
const loc = await findSessionDirectory(baseUrl, projects, sessionId);
|
|
494
|
+
const { baseUrl, projects, credentials } = ctx;
|
|
495
|
+
const loc = await findSessionDirectory(baseUrl, credentials, projects, sessionId);
|
|
445
496
|
if (!loc.ok)
|
|
446
497
|
return loc;
|
|
447
498
|
const { directory, project } = loc;
|
|
448
499
|
const [sessionRes, statusMapRes, todoRes, diffResult, pendingQuestion] = await Promise.all([
|
|
449
|
-
api(baseUrl, directory, `/session/${encodeURIComponent(sessionId)}`),
|
|
450
|
-
api(baseUrl, directory, "/session/status"),
|
|
451
|
-
api(baseUrl, directory, `/session/${encodeURIComponent(sessionId)}/todo`),
|
|
452
|
-
fetchDiffWithFallback(baseUrl, directory, sessionId),
|
|
453
|
-
fetchPendingQuestion(baseUrl, directory, sessionId)
|
|
500
|
+
api(baseUrl, credentials, directory, `/session/${encodeURIComponent(sessionId)}`),
|
|
501
|
+
api(baseUrl, credentials, directory, "/session/status"),
|
|
502
|
+
api(baseUrl, credentials, directory, `/session/${encodeURIComponent(sessionId)}/todo`),
|
|
503
|
+
fetchDiffWithFallback(baseUrl, credentials, directory, sessionId),
|
|
504
|
+
fetchPendingQuestion(baseUrl, credentials, directory, sessionId)
|
|
454
505
|
]);
|
|
455
506
|
if (!sessionRes.res.ok) {
|
|
456
507
|
return err(`space-bus: failed to fetch session ${sessionId} (${sessionRes.res.status}): ${sessionRes.bodyText}`);
|
|
@@ -482,10 +533,8 @@ async function status(sessionId, opts) {
|
|
|
482
533
|
pendingQuestion
|
|
483
534
|
};
|
|
484
535
|
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
async function steerSession(baseUrl, sessionId, message, directory, project) {
|
|
488
|
-
const questionsRes = await api(baseUrl, directory, "/question");
|
|
536
|
+
async function steerSession(baseUrl, credentials, sessionId, message, directory, project) {
|
|
537
|
+
const questionsRes = await api(baseUrl, credentials, directory, "/question");
|
|
489
538
|
if (questionsRes.res.ok) {
|
|
490
539
|
let questions;
|
|
491
540
|
try {
|
|
@@ -495,7 +544,7 @@ async function steerSession(baseUrl, sessionId, message, directory, project) {
|
|
|
495
544
|
}
|
|
496
545
|
const pending = questions.find((q) => q.sessionID === sessionId);
|
|
497
546
|
if (pending) {
|
|
498
|
-
const replyRes = await api(baseUrl, directory, `/question/${encodeURIComponent(pending.id)}/reply`, {
|
|
547
|
+
const replyRes = await api(baseUrl, credentials, directory, `/question/${encodeURIComponent(pending.id)}/reply`, {
|
|
499
548
|
method: "POST",
|
|
500
549
|
body: JSON.stringify({ answers: [[message]] })
|
|
501
550
|
});
|
|
@@ -505,7 +554,7 @@ async function steerSession(baseUrl, sessionId, message, directory, project) {
|
|
|
505
554
|
return { ok: true, sessionId, project, mode: "question-reply" };
|
|
506
555
|
}
|
|
507
556
|
}
|
|
508
|
-
const promptRes = await api(baseUrl, directory, `/session/${encodeURIComponent(sessionId)}/prompt_async`, {
|
|
557
|
+
const promptRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(sessionId)}/prompt_async`, {
|
|
509
558
|
method: "POST",
|
|
510
559
|
body: JSON.stringify({ parts: [{ type: "text", text: message }] })
|
|
511
560
|
});
|
|
@@ -515,15 +564,15 @@ async function steerSession(baseUrl, sessionId, message, directory, project) {
|
|
|
515
564
|
return { ok: true, sessionId, project, mode: "follow-up" };
|
|
516
565
|
}
|
|
517
566
|
async function result(sessionId, opts) {
|
|
518
|
-
const ctx =
|
|
567
|
+
const ctx = validateContext(opts.context);
|
|
519
568
|
if (!ctx.ok)
|
|
520
569
|
return ctx;
|
|
521
|
-
const { baseUrl, projects } = ctx;
|
|
522
|
-
const loc = await findSessionDirectory(baseUrl, projects, sessionId);
|
|
570
|
+
const { baseUrl, projects, credentials } = ctx;
|
|
571
|
+
const loc = await findSessionDirectory(baseUrl, credentials, projects, sessionId);
|
|
523
572
|
if (!loc.ok)
|
|
524
573
|
return loc;
|
|
525
574
|
const { directory, project } = loc;
|
|
526
|
-
const statusMapRes = await api(baseUrl, directory, "/session/status");
|
|
575
|
+
const statusMapRes = await api(baseUrl, credentials, directory, "/session/status");
|
|
527
576
|
if (statusMapRes.res.ok) {
|
|
528
577
|
try {
|
|
529
578
|
const statusMap = sessionStatusMapSchema.parse(JSON.parse(statusMapRes.bodyText));
|
|
@@ -534,8 +583,8 @@ async function result(sessionId, opts) {
|
|
|
534
583
|
} catch {}
|
|
535
584
|
}
|
|
536
585
|
const [messageRes, diffResult] = await Promise.all([
|
|
537
|
-
api(baseUrl, directory, `/session/${encodeURIComponent(sessionId)}/message?limit=50`),
|
|
538
|
-
fetchDiffWithFallback(baseUrl, directory, sessionId)
|
|
586
|
+
api(baseUrl, credentials, directory, `/session/${encodeURIComponent(sessionId)}/message?limit=50`),
|
|
587
|
+
fetchDiffWithFallback(baseUrl, credentials, directory, sessionId)
|
|
539
588
|
]);
|
|
540
589
|
if (!messageRes.res.ok) {
|
|
541
590
|
return err(`space-bus: failed to fetch messages for ${sessionId} (${messageRes.res.status}): ${messageRes.bodyText}`);
|
|
@@ -552,6 +601,90 @@ async function result(sessionId, opts) {
|
|
|
552
601
|
`).trim() : "";
|
|
553
602
|
return { ok: true, sessionId, project, text, diff, diffSource };
|
|
554
603
|
}
|
|
604
|
+
async function fetchSnapshotProject(baseUrl, credentials, project) {
|
|
605
|
+
const directory = project.expandedPath;
|
|
606
|
+
try {
|
|
607
|
+
const [statusRes, listRes, questionRes] = await Promise.all([
|
|
608
|
+
api(baseUrl, credentials, directory, "/session/status"),
|
|
609
|
+
api(baseUrl, credentials, directory, "/session?limit=101"),
|
|
610
|
+
api(baseUrl, credentials, directory, "/question")
|
|
611
|
+
]);
|
|
612
|
+
if (!statusRes.res.ok || !listRes.res.ok) {
|
|
613
|
+
return {
|
|
614
|
+
name: project.name,
|
|
615
|
+
path: directory,
|
|
616
|
+
exists: true,
|
|
617
|
+
description: project.description,
|
|
618
|
+
error: `status=${statusRes.res.status}/${listRes.res.status}`
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
const statusMap = sessionStatusMapSchema.parse(JSON.parse(statusRes.bodyText));
|
|
622
|
+
const sessions = sessionListSchema.parse(JSON.parse(listRes.bodyText));
|
|
623
|
+
const busyCount = Object.values(statusMap).filter((s) => s.type === "busy" || s.type === "retry").length;
|
|
624
|
+
const capped = sessions.length > 100;
|
|
625
|
+
let pendingQuestions;
|
|
626
|
+
if (questionRes.res.ok) {
|
|
627
|
+
try {
|
|
628
|
+
const entries = pendingQuestionListSchema.parse(JSON.parse(questionRes.bodyText));
|
|
629
|
+
pendingQuestions = entries.map((e) => formatQuestionEntry(e));
|
|
630
|
+
} catch {
|
|
631
|
+
pendingQuestions = undefined;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
return {
|
|
635
|
+
name: project.name,
|
|
636
|
+
path: directory,
|
|
637
|
+
exists: true,
|
|
638
|
+
description: project.description,
|
|
639
|
+
busyCount,
|
|
640
|
+
sessionCount: capped ? 100 : sessions.length,
|
|
641
|
+
sessionCountCapped: capped,
|
|
642
|
+
pendingQuestions
|
|
643
|
+
};
|
|
644
|
+
} catch (e) {
|
|
645
|
+
return {
|
|
646
|
+
name: project.name,
|
|
647
|
+
path: directory,
|
|
648
|
+
exists: true,
|
|
649
|
+
description: project.description,
|
|
650
|
+
error: e.message
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
async function mapWithConcurrency(items, concurrency, fn) {
|
|
655
|
+
const results = new Array(items.length);
|
|
656
|
+
let next = 0;
|
|
657
|
+
const limit = Math.max(1, Math.min(Number.isFinite(concurrency) ? Math.floor(concurrency) : 4, items.length));
|
|
658
|
+
const workers = Array.from({ length: limit }, async () => {
|
|
659
|
+
while (true) {
|
|
660
|
+
const i = next++;
|
|
661
|
+
if (i >= items.length)
|
|
662
|
+
return;
|
|
663
|
+
results[i] = await fn(items[i]);
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
await Promise.all(workers);
|
|
667
|
+
return results;
|
|
668
|
+
}
|
|
669
|
+
async function snapshot(opts) {
|
|
670
|
+
const ctx = validateContext(opts.context);
|
|
671
|
+
if (!ctx.ok)
|
|
672
|
+
return ctx;
|
|
673
|
+
const { baseUrl, projects, credentials } = ctx;
|
|
674
|
+
const concurrency = opts.concurrency ?? 4;
|
|
675
|
+
const results = await mapWithConcurrency(projects, concurrency, async (p) => {
|
|
676
|
+
if (!p.exists) {
|
|
677
|
+
return {
|
|
678
|
+
name: p.name,
|
|
679
|
+
path: p.expandedPath,
|
|
680
|
+
exists: false,
|
|
681
|
+
description: p.description
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
return fetchSnapshotProject(baseUrl, credentials, p);
|
|
685
|
+
});
|
|
686
|
+
return { ok: true, projects: results };
|
|
687
|
+
}
|
|
555
688
|
|
|
556
689
|
// src/format.ts
|
|
557
690
|
function formatRoster(projects) {
|
|
@@ -575,6 +708,9 @@ function formatDispatch(r) {
|
|
|
575
708
|
return `Follow-up prompt sent to session ${r.sessionId} (${r.project}).`;
|
|
576
709
|
}
|
|
577
710
|
}
|
|
711
|
+
function dispatchMetadata(r) {
|
|
712
|
+
return { sessionId: r.sessionId, project: r.project, mode: r.mode };
|
|
713
|
+
}
|
|
578
714
|
function formatStatus(r) {
|
|
579
715
|
const todoLines = r.todos.length ? r.todos.map((t) => ` - [${t.status}] ${t.content} (${t.priority})`).join(`
|
|
580
716
|
`) : " (none)";
|
|
@@ -607,6 +743,11 @@ function formatResult(r) {
|
|
|
607
743
|
`);
|
|
608
744
|
}
|
|
609
745
|
|
|
746
|
+
// src/mcp.ts
|
|
747
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
748
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
749
|
+
import { z as z3 } from "zod";
|
|
750
|
+
|
|
610
751
|
// src/tools/bus_result.ts
|
|
611
752
|
import { tool } from "@opencode-ai/plugin";
|
|
612
753
|
var BUS_RESULT_DESCRIPTION = "Return a completed space-bus session's final assistant message and diff. Errors if the session is still running — use bus_status to check first.";
|
|
@@ -617,9 +758,14 @@ function makeBusResult(defaultDirectory) {
|
|
|
617
758
|
sessionId: tool.schema.string().describe("Session ID returned by bus_task")
|
|
618
759
|
},
|
|
619
760
|
async execute(args, ctx) {
|
|
620
|
-
const
|
|
621
|
-
|
|
622
|
-
|
|
761
|
+
const directory = ctx.directory ?? defaultDirectory;
|
|
762
|
+
let context;
|
|
763
|
+
try {
|
|
764
|
+
context = loadContext(directory);
|
|
765
|
+
} catch (e) {
|
|
766
|
+
throw new Error(e.message);
|
|
767
|
+
}
|
|
768
|
+
const r = await result(args.sessionId, { context });
|
|
623
769
|
if (!r.ok)
|
|
624
770
|
throw new Error(r.error);
|
|
625
771
|
return formatResult(r);
|
|
@@ -635,7 +781,14 @@ function makeBusRoster(defaultDirectory) {
|
|
|
635
781
|
description: BUS_ROSTER_DESCRIPTION,
|
|
636
782
|
args: {},
|
|
637
783
|
async execute(_args, ctx) {
|
|
638
|
-
const
|
|
784
|
+
const directory = ctx.directory ?? defaultDirectory;
|
|
785
|
+
let context;
|
|
786
|
+
try {
|
|
787
|
+
context = loadContext(directory);
|
|
788
|
+
} catch (e) {
|
|
789
|
+
throw new Error(e.message);
|
|
790
|
+
}
|
|
791
|
+
const r = await roster({ context });
|
|
639
792
|
if (!r.ok)
|
|
640
793
|
throw new Error(r.error);
|
|
641
794
|
return formatRoster(r.projects);
|
|
@@ -653,9 +806,14 @@ function makeBusStatus(defaultDirectory) {
|
|
|
653
806
|
sessionId: tool3.schema.string().describe("Session ID returned by bus_task")
|
|
654
807
|
},
|
|
655
808
|
async execute(args, ctx) {
|
|
656
|
-
const
|
|
657
|
-
|
|
658
|
-
|
|
809
|
+
const directory = ctx.directory ?? defaultDirectory;
|
|
810
|
+
let context;
|
|
811
|
+
try {
|
|
812
|
+
context = loadContext(directory);
|
|
813
|
+
} catch (e) {
|
|
814
|
+
throw new Error(e.message);
|
|
815
|
+
}
|
|
816
|
+
const r = await status(args.sessionId, { context });
|
|
659
817
|
if (!r.ok)
|
|
660
818
|
throw new Error(r.error);
|
|
661
819
|
return formatStatus(r);
|
|
@@ -676,22 +834,26 @@ function makeBusTask(defaultDirectory) {
|
|
|
676
834
|
sessionId: tool4.schema.string().optional().describe("Existing session ID to steer instead of starting a new session")
|
|
677
835
|
},
|
|
678
836
|
async execute(args, ctx) {
|
|
679
|
-
const dispatchArgs = toDispatchArgs(
|
|
680
|
-
...args,
|
|
681
|
-
directory: ctx.directory ?? defaultDirectory
|
|
682
|
-
});
|
|
837
|
+
const dispatchArgs = toDispatchArgs(args);
|
|
683
838
|
if (!dispatchArgs.ok)
|
|
684
839
|
throw new Error(dispatchArgs.error);
|
|
685
|
-
const
|
|
840
|
+
const directory = ctx.directory ?? defaultDirectory;
|
|
841
|
+
let context;
|
|
842
|
+
try {
|
|
843
|
+
context = loadContext(directory);
|
|
844
|
+
} catch (e) {
|
|
845
|
+
throw new Error(e.message);
|
|
846
|
+
}
|
|
847
|
+
const r = await dispatch(dispatchArgs, { context });
|
|
686
848
|
if (!r.ok)
|
|
687
849
|
throw new Error(r.error);
|
|
688
|
-
return formatDispatch(r);
|
|
850
|
+
return { output: formatDispatch(r), metadata: dispatchMetadata(r) };
|
|
689
851
|
}
|
|
690
852
|
});
|
|
691
853
|
}
|
|
692
854
|
|
|
693
855
|
// src/mcp.ts
|
|
694
|
-
var version = "0.
|
|
856
|
+
var version = "0.4.0";
|
|
695
857
|
var server = new McpServer({
|
|
696
858
|
name: "space-bus",
|
|
697
859
|
version
|
|
@@ -700,7 +862,16 @@ server.registerTool("bus_roster", {
|
|
|
700
862
|
description: BUS_ROSTER_DESCRIPTION,
|
|
701
863
|
inputSchema: {}
|
|
702
864
|
}, async () => {
|
|
703
|
-
|
|
865
|
+
let context;
|
|
866
|
+
try {
|
|
867
|
+
context = loadContext();
|
|
868
|
+
} catch (e) {
|
|
869
|
+
return {
|
|
870
|
+
content: [{ type: "text", text: e.message }],
|
|
871
|
+
isError: true
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
const r = await roster({ context });
|
|
704
875
|
if (!r.ok)
|
|
705
876
|
return { content: [{ type: "text", text: r.error }], isError: true };
|
|
706
877
|
return { content: [{ type: "text", text: formatRoster(r.projects) }] };
|
|
@@ -712,6 +883,11 @@ server.registerTool("bus_task", {
|
|
|
712
883
|
prompt: z3.string().describe("The prompt to send to the delegated agent"),
|
|
713
884
|
title: z3.string().optional().describe("Optional session title (only used when starting a new session)"),
|
|
714
885
|
sessionId: z3.string().optional().describe("Existing session ID to steer instead of starting a new session")
|
|
886
|
+
},
|
|
887
|
+
outputSchema: {
|
|
888
|
+
sessionId: z3.string().describe("The dispatched or steered session ID"),
|
|
889
|
+
project: z3.string().describe("Manifest project name that owns the session"),
|
|
890
|
+
mode: z3.enum(["new", "question-reply", "follow-up"]).describe("How the prompt was delivered")
|
|
715
891
|
}
|
|
716
892
|
}, async (args) => {
|
|
717
893
|
const dispatchArgs = toDispatchArgs(args);
|
|
@@ -720,10 +896,22 @@ server.registerTool("bus_task", {
|
|
|
720
896
|
content: [{ type: "text", text: dispatchArgs.error }],
|
|
721
897
|
isError: true
|
|
722
898
|
};
|
|
723
|
-
|
|
899
|
+
let context;
|
|
900
|
+
try {
|
|
901
|
+
context = loadContext();
|
|
902
|
+
} catch (e) {
|
|
903
|
+
return {
|
|
904
|
+
content: [{ type: "text", text: e.message }],
|
|
905
|
+
isError: true
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
const r = await dispatch(dispatchArgs, { context });
|
|
724
909
|
if (!r.ok)
|
|
725
910
|
return { content: [{ type: "text", text: r.error }], isError: true };
|
|
726
|
-
return {
|
|
911
|
+
return {
|
|
912
|
+
content: [{ type: "text", text: formatDispatch(r) }],
|
|
913
|
+
structuredContent: dispatchMetadata(r)
|
|
914
|
+
};
|
|
727
915
|
});
|
|
728
916
|
server.registerTool("bus_status", {
|
|
729
917
|
description: BUS_STATUS_DESCRIPTION,
|
|
@@ -731,7 +919,16 @@ server.registerTool("bus_status", {
|
|
|
731
919
|
sessionId: z3.string().describe("Session ID returned by bus_task")
|
|
732
920
|
}
|
|
733
921
|
}, async (args) => {
|
|
734
|
-
|
|
922
|
+
let context;
|
|
923
|
+
try {
|
|
924
|
+
context = loadContext();
|
|
925
|
+
} catch (e) {
|
|
926
|
+
return {
|
|
927
|
+
content: [{ type: "text", text: e.message }],
|
|
928
|
+
isError: true
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
const r = await status(args.sessionId, { context });
|
|
735
932
|
if (!r.ok)
|
|
736
933
|
return { content: [{ type: "text", text: r.error }], isError: true };
|
|
737
934
|
return { content: [{ type: "text", text: formatStatus(r) }] };
|
|
@@ -742,7 +939,16 @@ server.registerTool("bus_result", {
|
|
|
742
939
|
sessionId: z3.string().describe("Session ID returned by bus_task")
|
|
743
940
|
}
|
|
744
941
|
}, async (args) => {
|
|
745
|
-
|
|
942
|
+
let context;
|
|
943
|
+
try {
|
|
944
|
+
context = loadContext();
|
|
945
|
+
} catch (e) {
|
|
946
|
+
return {
|
|
947
|
+
content: [{ type: "text", text: e.message }],
|
|
948
|
+
isError: true
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
const r = await result(args.sessionId, { context });
|
|
746
952
|
if (!r.ok)
|
|
747
953
|
return { content: [{ type: "text", text: r.error }], isError: true };
|
|
748
954
|
return { content: [{ type: "text", text: formatResult(r) }] };
|