@fro.bot/space-bus 0.14.0 → 0.15.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/dist/core.d.ts +36 -1
- package/dist/core.js +119 -28
- package/dist/format.d.ts +1 -0
- package/dist/format.js +7 -1
- package/dist/index.js +92 -29
- package/dist/mcp.js +96 -31
- package/package.json +3 -3
package/dist/core.d.ts
CHANGED
|
@@ -3,9 +3,25 @@ export type { SessionState, SessionStateInfo } from "./contract";
|
|
|
3
3
|
type Ok<T> = {
|
|
4
4
|
ok: true;
|
|
5
5
|
} & T;
|
|
6
|
+
/**
|
|
7
|
+
* Typed partial-failure handle for dispatch() call sites only — omitted
|
|
8
|
+
* entirely on every other core function's errors. "indeterminate" means the
|
|
9
|
+
* failed request may already have mutated OpenCode state (a create/prompt/
|
|
10
|
+
* reply POST that failed, was lost, or returned an unparseable body after
|
|
11
|
+
* the mutating call was made); "not_sent" means the failure is definitely
|
|
12
|
+
* pre-mutation (target resolution, pending-question verification). Fields
|
|
13
|
+
* are included only when known — never present-but-undefined.
|
|
14
|
+
*/
|
|
15
|
+
export type DispatchFailure = {
|
|
16
|
+
phase: "not_sent" | "indeterminate";
|
|
17
|
+
project: string;
|
|
18
|
+
sessionId?: string;
|
|
19
|
+
messageId?: string;
|
|
20
|
+
};
|
|
6
21
|
type Err = {
|
|
7
22
|
ok: false;
|
|
8
23
|
error: string;
|
|
24
|
+
dispatchFailure?: DispatchFailure;
|
|
9
25
|
};
|
|
10
26
|
export type Result<T> = Ok<T> | Err;
|
|
11
27
|
/** Options every exported core function takes: a per-call BusContext. */
|
|
@@ -32,12 +48,23 @@ export type DispatchResult = {
|
|
|
32
48
|
} & ({
|
|
33
49
|
mode: "new";
|
|
34
50
|
directory: string;
|
|
51
|
+
messageId?: string;
|
|
35
52
|
} | {
|
|
36
|
-
mode: "
|
|
53
|
+
mode: "follow-up";
|
|
54
|
+
messageId?: string;
|
|
55
|
+
} | {
|
|
56
|
+
mode: "question-reply";
|
|
37
57
|
} | {
|
|
38
58
|
mode: "blocked";
|
|
39
59
|
requestId: string;
|
|
40
60
|
});
|
|
61
|
+
/**
|
|
62
|
+
* Generates an OpenCode v1 user-message id (`msg_` + a 12-char lowercase-hex
|
|
63
|
+
* ascending timestamp/counter prefix + 14 random base62 chars) using only
|
|
64
|
+
* Web Crypto / global browser-safe APIs — no Node builtins — so it's usable
|
|
65
|
+
* from the browser-safe `/core` subpath.
|
|
66
|
+
*/
|
|
67
|
+
export declare function createDispatchMessageId(): string;
|
|
41
68
|
export type DispatchArgs = {
|
|
42
69
|
prompt: string;
|
|
43
70
|
title?: string;
|
|
@@ -51,6 +78,13 @@ export type DispatchArgs = {
|
|
|
51
78
|
* reinterpret prompt text as a question answer.
|
|
52
79
|
*/
|
|
53
80
|
onPendingQuestion?: "question-reply" | "blocked";
|
|
81
|
+
/**
|
|
82
|
+
* Caller-supplied OpenCode v1 user message id to correlate the dispatched
|
|
83
|
+
* prompt with the message OpenCode creates for it. Validated by
|
|
84
|
+
* toDispatchArgs against the msg_ + alphanumeric shape; never generated
|
|
85
|
+
* here — generation is the caller's job via createDispatchMessageId().
|
|
86
|
+
*/
|
|
87
|
+
messageId?: string;
|
|
54
88
|
} & ({
|
|
55
89
|
project: string;
|
|
56
90
|
sessionId?: undefined;
|
|
@@ -64,6 +98,7 @@ export declare function toDispatchArgs(input: {
|
|
|
64
98
|
project?: string;
|
|
65
99
|
sessionId?: string;
|
|
66
100
|
onPendingQuestion?: "question-reply" | "blocked";
|
|
101
|
+
messageId?: string;
|
|
67
102
|
}): Result<DispatchArgs>;
|
|
68
103
|
export declare function dispatch(args: DispatchArgs, opts: CoreOpts): Promise<Result<DispatchResult>>;
|
|
69
104
|
export type SessionStatusResult = {
|
package/dist/core.js
CHANGED
|
@@ -168,6 +168,15 @@ function findProject(projects, name) {
|
|
|
168
168
|
function err(error) {
|
|
169
169
|
return { ok: false, error };
|
|
170
170
|
}
|
|
171
|
+
function dispatchErr(error, failure) {
|
|
172
|
+
const dispatchFailure = {
|
|
173
|
+
phase: failure.phase,
|
|
174
|
+
project: failure.project,
|
|
175
|
+
...failure.sessionId !== undefined ? { sessionId: failure.sessionId } : {},
|
|
176
|
+
...failure.messageId !== undefined ? { messageId: failure.messageId } : {}
|
|
177
|
+
};
|
|
178
|
+
return { ok: false, error, dispatchFailure };
|
|
179
|
+
}
|
|
171
180
|
function toBase64(s) {
|
|
172
181
|
const bytes = new TextEncoder().encode(s);
|
|
173
182
|
let bin = "";
|
|
@@ -367,7 +376,7 @@ async function roster(opts) {
|
|
|
367
376
|
}));
|
|
368
377
|
return { ok: true, projects: results };
|
|
369
378
|
}
|
|
370
|
-
async function dispatchNew(baseUrl, credentials, projects, project, prompt, title) {
|
|
379
|
+
async function dispatchNew(baseUrl, credentials, projects, project, prompt, title, messageId) {
|
|
371
380
|
const resolved = resolveProjectOrErr(projects, project);
|
|
372
381
|
if (!resolved.ok)
|
|
373
382
|
return resolved;
|
|
@@ -378,22 +387,73 @@ async function dispatchNew(baseUrl, credentials, projects, project, prompt, titl
|
|
|
378
387
|
body: JSON.stringify({ title: sessionTitle })
|
|
379
388
|
});
|
|
380
389
|
if (!createRes.res.ok) {
|
|
381
|
-
return
|
|
390
|
+
return dispatchErr(`space-bus: failed to create session in "${project}" (${createRes.res.status}): ${createRes.bodyText}`, { phase: "indeterminate", project, messageId });
|
|
382
391
|
}
|
|
383
392
|
let session;
|
|
384
393
|
try {
|
|
385
394
|
session = sessionSchema.parse(JSON.parse(createRes.bodyText));
|
|
386
395
|
} catch (e) {
|
|
387
|
-
return
|
|
396
|
+
return dispatchErr(`space-bus: unexpected /session response shape: ${e.message}`, { phase: "indeterminate", project, messageId });
|
|
388
397
|
}
|
|
389
398
|
const promptRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(session.id)}/prompt_async`, {
|
|
390
399
|
method: "POST",
|
|
391
|
-
body: JSON.stringify({
|
|
400
|
+
body: JSON.stringify({
|
|
401
|
+
parts: [{ type: "text", text: prompt }],
|
|
402
|
+
...messageId !== undefined ? { messageID: messageId } : {}
|
|
403
|
+
})
|
|
392
404
|
});
|
|
393
405
|
if (promptRes.res.status !== 204) {
|
|
394
|
-
return
|
|
406
|
+
return dispatchErr(`space-bus: dispatch to "${project}" failed sending prompt (${promptRes.res.status}): ${promptRes.bodyText}`, { phase: "indeterminate", project, sessionId: session.id, messageId });
|
|
407
|
+
}
|
|
408
|
+
return {
|
|
409
|
+
ok: true,
|
|
410
|
+
sessionId: session.id,
|
|
411
|
+
project,
|
|
412
|
+
directory,
|
|
413
|
+
...messageId !== undefined ? { messageId } : {}
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
var BASE62_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
417
|
+
var lastTimestamp = 0;
|
|
418
|
+
var counter = 0;
|
|
419
|
+
function nextAscendingId() {
|
|
420
|
+
const timestamp = Date.now();
|
|
421
|
+
if (timestamp !== lastTimestamp) {
|
|
422
|
+
lastTimestamp = timestamp;
|
|
423
|
+
counter = 0;
|
|
424
|
+
}
|
|
425
|
+
counter++;
|
|
426
|
+
return BigInt(timestamp) * 0x1000n + BigInt(counter);
|
|
427
|
+
}
|
|
428
|
+
function encode48BitHex(value) {
|
|
429
|
+
const bytes = new Uint8Array(6);
|
|
430
|
+
let v = value & 0xffffffffffffn;
|
|
431
|
+
for (let i = 5;i >= 0; i--) {
|
|
432
|
+
bytes[i] = Number(v & 0xffn);
|
|
433
|
+
v >>= 8n;
|
|
434
|
+
}
|
|
435
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
436
|
+
}
|
|
437
|
+
function randomBase62(length) {
|
|
438
|
+
const bytes = new Uint8Array(length);
|
|
439
|
+
crypto.getRandomValues(bytes);
|
|
440
|
+
let out = "";
|
|
441
|
+
for (const b of bytes) {
|
|
442
|
+
out += BASE62_ALPHABET[b % BASE62_ALPHABET.length];
|
|
443
|
+
}
|
|
444
|
+
return out;
|
|
445
|
+
}
|
|
446
|
+
function createDispatchMessageId() {
|
|
447
|
+
const hex = encode48BitHex(nextAscendingId());
|
|
448
|
+
return `msg_${hex}${randomBase62(14)}`;
|
|
449
|
+
}
|
|
450
|
+
var MESSAGE_ID_PATTERN = /^msg_[0-9a-f]{12}[0-9A-Za-z]{14}$/;
|
|
451
|
+
var INVALID_MESSAGE_ID_ERROR = "space-bus: messageId must be msg_ followed by exactly 12 hex characters and 14 alphanumeric characters";
|
|
452
|
+
function validateMessageId(messageId) {
|
|
453
|
+
if (!MESSAGE_ID_PATTERN.test(messageId)) {
|
|
454
|
+
return err(INVALID_MESSAGE_ID_ERROR);
|
|
395
455
|
}
|
|
396
|
-
return { ok: true,
|
|
456
|
+
return { ok: true, messageId };
|
|
397
457
|
}
|
|
398
458
|
function toDispatchArgs(input) {
|
|
399
459
|
if (input.onPendingQuestion !== undefined && input.onPendingQuestion !== "question-reply" && input.onPendingQuestion !== "blocked") {
|
|
@@ -402,6 +462,13 @@ function toDispatchArgs(input) {
|
|
|
402
462
|
if (input.sessionId !== undefined && input.sessionId === "") {
|
|
403
463
|
return err("space-bus: sessionId must be a non-empty string");
|
|
404
464
|
}
|
|
465
|
+
let messageId;
|
|
466
|
+
if (input.messageId !== undefined) {
|
|
467
|
+
const validated = validateMessageId(input.messageId);
|
|
468
|
+
if (!validated.ok)
|
|
469
|
+
return validated;
|
|
470
|
+
messageId = validated.messageId;
|
|
471
|
+
}
|
|
405
472
|
if (!input.sessionId) {
|
|
406
473
|
if (!input.project) {
|
|
407
474
|
return err("space-bus: project is required when starting a new session");
|
|
@@ -411,7 +478,8 @@ function toDispatchArgs(input) {
|
|
|
411
478
|
prompt: input.prompt,
|
|
412
479
|
title: input.title,
|
|
413
480
|
project: input.project,
|
|
414
|
-
onPendingQuestion: input.onPendingQuestion
|
|
481
|
+
onPendingQuestion: input.onPendingQuestion,
|
|
482
|
+
...messageId !== undefined ? { messageId } : {}
|
|
415
483
|
};
|
|
416
484
|
}
|
|
417
485
|
return {
|
|
@@ -420,7 +488,21 @@ function toDispatchArgs(input) {
|
|
|
420
488
|
title: input.title,
|
|
421
489
|
sessionId: input.sessionId,
|
|
422
490
|
project: input.project,
|
|
423
|
-
onPendingQuestion: input.onPendingQuestion
|
|
491
|
+
onPendingQuestion: input.onPendingQuestion,
|
|
492
|
+
...messageId !== undefined ? { messageId } : {}
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
async function dispatchNewBranch(baseUrl, credentials, projects, args) {
|
|
496
|
+
const r = await dispatchNew(baseUrl, credentials, projects, args.project, args.prompt, args.title, args.messageId);
|
|
497
|
+
if (!r.ok)
|
|
498
|
+
return r;
|
|
499
|
+
return {
|
|
500
|
+
ok: true,
|
|
501
|
+
sessionId: r.sessionId,
|
|
502
|
+
project: r.project,
|
|
503
|
+
mode: "new",
|
|
504
|
+
directory: r.directory,
|
|
505
|
+
...r.messageId !== undefined ? { messageId: r.messageId } : {}
|
|
424
506
|
};
|
|
425
507
|
}
|
|
426
508
|
async function dispatch(args, opts) {
|
|
@@ -428,6 +510,11 @@ async function dispatch(args, opts) {
|
|
|
428
510
|
if (!ctx.ok)
|
|
429
511
|
return ctx;
|
|
430
512
|
const { baseUrl, projects, credentials } = ctx;
|
|
513
|
+
if (args.messageId !== undefined) {
|
|
514
|
+
const validated = validateMessageId(args.messageId);
|
|
515
|
+
if (!validated.ok)
|
|
516
|
+
return validated;
|
|
517
|
+
}
|
|
431
518
|
if (args.sessionId !== undefined && args.sessionId === "") {
|
|
432
519
|
return err("space-bus: sessionId must be a non-empty string");
|
|
433
520
|
}
|
|
@@ -435,16 +522,10 @@ async function dispatch(args, opts) {
|
|
|
435
522
|
if (!args.project) {
|
|
436
523
|
return err("space-bus: project is required when starting a new session");
|
|
437
524
|
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
ok: true,
|
|
443
|
-
sessionId: r.sessionId,
|
|
444
|
-
project: r.project,
|
|
445
|
-
mode: "new",
|
|
446
|
-
directory: r.directory
|
|
447
|
-
};
|
|
525
|
+
return dispatchNewBranch(baseUrl, credentials, projects, {
|
|
526
|
+
...args,
|
|
527
|
+
project: args.project
|
|
528
|
+
});
|
|
448
529
|
}
|
|
449
530
|
const loc = await findSessionDirectory(baseUrl, credentials, projects, args.sessionId);
|
|
450
531
|
if (!loc.ok)
|
|
@@ -453,7 +534,7 @@ async function dispatch(args, opts) {
|
|
|
453
534
|
if (args.project && args.project !== project) {
|
|
454
535
|
return err(`space-bus: session ${args.sessionId} belongs to project "${project}", not "${args.project}" — refusing to steer the wrong session`);
|
|
455
536
|
}
|
|
456
|
-
return steerSession(baseUrl, credentials, args.sessionId, args.prompt, directory, project, args.onPendingQuestion ?? "question-reply");
|
|
537
|
+
return steerSession(baseUrl, credentials, args.sessionId, args.prompt, directory, project, args.onPendingQuestion ?? "question-reply", args.messageId);
|
|
457
538
|
}
|
|
458
539
|
async function findSessionDirectory(baseUrl, credentials, projects, sessionId) {
|
|
459
540
|
for (const p of projects) {
|
|
@@ -558,11 +639,11 @@ async function status(sessionId, opts) {
|
|
|
558
639
|
resultAvailable: state === "complete"
|
|
559
640
|
};
|
|
560
641
|
}
|
|
561
|
-
async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, onPendingQuestion) {
|
|
642
|
+
async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, project, onPendingQuestion) {
|
|
562
643
|
const questionsRes = await api(baseUrl, credentials, directory, "/question");
|
|
563
644
|
if (!questionsRes.res.ok) {
|
|
564
645
|
if (onPendingQuestion === "blocked") {
|
|
565
|
-
return
|
|
646
|
+
return dispatchErr(`space-bus: could not verify pending-question state for session ${sessionId} (/question ${questionsRes.res.status}) — refusing to dispatch under the blocked policy`, { phase: "not_sent", project, sessionId });
|
|
566
647
|
}
|
|
567
648
|
return { ok: true, pending: undefined };
|
|
568
649
|
}
|
|
@@ -571,7 +652,7 @@ async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, s
|
|
|
571
652
|
pendingList = questionListSchema.parse(JSON.parse(questionsRes.bodyText));
|
|
572
653
|
} catch (e) {
|
|
573
654
|
if (onPendingQuestion === "blocked") {
|
|
574
|
-
return
|
|
655
|
+
return dispatchErr(`space-bus: could not parse pending-question state for session ${sessionId}: ${e.message} — refusing to dispatch under the blocked policy`, { phase: "not_sent", project, sessionId });
|
|
575
656
|
}
|
|
576
657
|
return { ok: true, pending: undefined };
|
|
577
658
|
}
|
|
@@ -580,8 +661,8 @@ async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, s
|
|
|
580
661
|
pending: pendingList.find((q) => q.sessionID === sessionId)
|
|
581
662
|
};
|
|
582
663
|
}
|
|
583
|
-
async function steerSession(baseUrl, credentials, sessionId, message, directory, project, onPendingQuestion) {
|
|
584
|
-
const pendingRes = await resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, onPendingQuestion);
|
|
664
|
+
async function steerSession(baseUrl, credentials, sessionId, message, directory, project, onPendingQuestion, messageId) {
|
|
665
|
+
const pendingRes = await resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, project, onPendingQuestion);
|
|
585
666
|
if (!pendingRes.ok)
|
|
586
667
|
return pendingRes;
|
|
587
668
|
const { pending } = pendingRes;
|
|
@@ -600,18 +681,27 @@ async function steerSession(baseUrl, credentials, sessionId, message, directory,
|
|
|
600
681
|
body: JSON.stringify({ answers: [[message]] })
|
|
601
682
|
});
|
|
602
683
|
if (!replyRes.res.ok) {
|
|
603
|
-
return
|
|
684
|
+
return dispatchErr(`space-bus: failed to reply to question ${pending.id} for session ${sessionId} (${replyRes.res.status}): ${replyRes.bodyText}`, { phase: "indeterminate", project, sessionId });
|
|
604
685
|
}
|
|
605
686
|
return { ok: true, sessionId, project, mode: "question-reply" };
|
|
606
687
|
}
|
|
607
688
|
const promptRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(sessionId)}/prompt_async`, {
|
|
608
689
|
method: "POST",
|
|
609
|
-
body: JSON.stringify({
|
|
690
|
+
body: JSON.stringify({
|
|
691
|
+
parts: [{ type: "text", text: message }],
|
|
692
|
+
...messageId !== undefined ? { messageID: messageId } : {}
|
|
693
|
+
})
|
|
610
694
|
});
|
|
611
695
|
if (promptRes.res.status !== 204) {
|
|
612
|
-
return
|
|
696
|
+
return dispatchErr(`space-bus: follow-up prompt to session ${sessionId} failed (${promptRes.res.status}): ${promptRes.bodyText}`, { phase: "indeterminate", project, sessionId, messageId });
|
|
613
697
|
}
|
|
614
|
-
return {
|
|
698
|
+
return {
|
|
699
|
+
ok: true,
|
|
700
|
+
sessionId,
|
|
701
|
+
project,
|
|
702
|
+
mode: "follow-up",
|
|
703
|
+
...messageId !== undefined ? { messageId } : {}
|
|
704
|
+
};
|
|
615
705
|
}
|
|
616
706
|
async function result(sessionId, opts) {
|
|
617
707
|
const ctx = validateContext(opts.context);
|
|
@@ -1029,5 +1119,6 @@ export {
|
|
|
1029
1119
|
messages,
|
|
1030
1120
|
dispatch,
|
|
1031
1121
|
deriveSessionState,
|
|
1122
|
+
createDispatchMessageId,
|
|
1032
1123
|
answerQuestion
|
|
1033
1124
|
};
|
package/dist/format.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export type DispatchMetadata = {
|
|
|
26
26
|
sessionId: string;
|
|
27
27
|
project: string;
|
|
28
28
|
mode: DispatchResult["mode"];
|
|
29
|
+
messageId?: string;
|
|
29
30
|
};
|
|
30
31
|
export declare function dispatchMetadata(r: DispatchResult): DispatchMetadata;
|
|
31
32
|
export declare function formatStatus(r: SessionStatusResult): string;
|
package/dist/format.js
CHANGED
|
@@ -26,7 +26,13 @@ function formatDispatch(r) {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
function dispatchMetadata(r) {
|
|
29
|
-
|
|
29
|
+
const messageId = (r.mode === "new" || r.mode === "follow-up") && r.messageId !== undefined ? r.messageId : undefined;
|
|
30
|
+
return {
|
|
31
|
+
sessionId: r.sessionId,
|
|
32
|
+
project: r.project,
|
|
33
|
+
mode: r.mode,
|
|
34
|
+
...messageId !== undefined ? { messageId } : {}
|
|
35
|
+
};
|
|
30
36
|
}
|
|
31
37
|
function formatStatus(r) {
|
|
32
38
|
const todoLines = r.todos.length ? r.todos.map((t) => ` - [${t.status}] ${t.content} (${t.priority})`).join(`
|
package/dist/index.js
CHANGED
|
@@ -1484,7 +1484,13 @@ function formatDispatch(r) {
|
|
|
1484
1484
|
}
|
|
1485
1485
|
}
|
|
1486
1486
|
function dispatchMetadata(r) {
|
|
1487
|
-
|
|
1487
|
+
const messageId = (r.mode === "new" || r.mode === "follow-up") && r.messageId !== undefined ? r.messageId : undefined;
|
|
1488
|
+
return {
|
|
1489
|
+
sessionId: r.sessionId,
|
|
1490
|
+
project: r.project,
|
|
1491
|
+
mode: r.mode,
|
|
1492
|
+
...messageId !== undefined ? { messageId } : {}
|
|
1493
|
+
};
|
|
1488
1494
|
}
|
|
1489
1495
|
function formatStatus(r) {
|
|
1490
1496
|
const todoLines = r.todos.length ? r.todos.map((t) => ` - [${t.status}] ${t.content} (${t.priority})`).join(`
|
|
@@ -2104,6 +2110,15 @@ function findProject(projects, name) {
|
|
|
2104
2110
|
function err(error) {
|
|
2105
2111
|
return { ok: false, error };
|
|
2106
2112
|
}
|
|
2113
|
+
function dispatchErr(error, failure) {
|
|
2114
|
+
const dispatchFailure = {
|
|
2115
|
+
phase: failure.phase,
|
|
2116
|
+
project: failure.project,
|
|
2117
|
+
...failure.sessionId !== undefined ? { sessionId: failure.sessionId } : {},
|
|
2118
|
+
...failure.messageId !== undefined ? { messageId: failure.messageId } : {}
|
|
2119
|
+
};
|
|
2120
|
+
return { ok: false, error, dispatchFailure };
|
|
2121
|
+
}
|
|
2107
2122
|
function toBase64(s) {
|
|
2108
2123
|
const bytes = new TextEncoder().encode(s);
|
|
2109
2124
|
let bin = "";
|
|
@@ -2303,7 +2318,7 @@ async function roster(opts) {
|
|
|
2303
2318
|
}));
|
|
2304
2319
|
return { ok: true, projects: results };
|
|
2305
2320
|
}
|
|
2306
|
-
async function dispatchNew(baseUrl, credentials, projects, project, prompt, title) {
|
|
2321
|
+
async function dispatchNew(baseUrl, credentials, projects, project, prompt, title, messageId) {
|
|
2307
2322
|
const resolved = resolveProjectOrErr(projects, project);
|
|
2308
2323
|
if (!resolved.ok)
|
|
2309
2324
|
return resolved;
|
|
@@ -2314,22 +2329,39 @@ async function dispatchNew(baseUrl, credentials, projects, project, prompt, titl
|
|
|
2314
2329
|
body: JSON.stringify({ title: sessionTitle })
|
|
2315
2330
|
});
|
|
2316
2331
|
if (!createRes.res.ok) {
|
|
2317
|
-
return
|
|
2332
|
+
return dispatchErr(`space-bus: failed to create session in "${project}" (${createRes.res.status}): ${createRes.bodyText}`, { phase: "indeterminate", project, messageId });
|
|
2318
2333
|
}
|
|
2319
2334
|
let session;
|
|
2320
2335
|
try {
|
|
2321
2336
|
session = sessionSchema.parse(JSON.parse(createRes.bodyText));
|
|
2322
2337
|
} catch (e) {
|
|
2323
|
-
return
|
|
2338
|
+
return dispatchErr(`space-bus: unexpected /session response shape: ${e.message}`, { phase: "indeterminate", project, messageId });
|
|
2324
2339
|
}
|
|
2325
2340
|
const promptRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(session.id)}/prompt_async`, {
|
|
2326
2341
|
method: "POST",
|
|
2327
|
-
body: JSON.stringify({
|
|
2342
|
+
body: JSON.stringify({
|
|
2343
|
+
parts: [{ type: "text", text: prompt }],
|
|
2344
|
+
...messageId !== undefined ? { messageID: messageId } : {}
|
|
2345
|
+
})
|
|
2328
2346
|
});
|
|
2329
2347
|
if (promptRes.res.status !== 204) {
|
|
2330
|
-
return
|
|
2348
|
+
return dispatchErr(`space-bus: dispatch to "${project}" failed sending prompt (${promptRes.res.status}): ${promptRes.bodyText}`, { phase: "indeterminate", project, sessionId: session.id, messageId });
|
|
2349
|
+
}
|
|
2350
|
+
return {
|
|
2351
|
+
ok: true,
|
|
2352
|
+
sessionId: session.id,
|
|
2353
|
+
project,
|
|
2354
|
+
directory,
|
|
2355
|
+
...messageId !== undefined ? { messageId } : {}
|
|
2356
|
+
};
|
|
2357
|
+
}
|
|
2358
|
+
var MESSAGE_ID_PATTERN = /^msg_[0-9a-f]{12}[0-9A-Za-z]{14}$/;
|
|
2359
|
+
var INVALID_MESSAGE_ID_ERROR = "space-bus: messageId must be msg_ followed by exactly 12 hex characters and 14 alphanumeric characters";
|
|
2360
|
+
function validateMessageId(messageId) {
|
|
2361
|
+
if (!MESSAGE_ID_PATTERN.test(messageId)) {
|
|
2362
|
+
return err(INVALID_MESSAGE_ID_ERROR);
|
|
2331
2363
|
}
|
|
2332
|
-
return { ok: true,
|
|
2364
|
+
return { ok: true, messageId };
|
|
2333
2365
|
}
|
|
2334
2366
|
function toDispatchArgs(input) {
|
|
2335
2367
|
if (input.onPendingQuestion !== undefined && input.onPendingQuestion !== "question-reply" && input.onPendingQuestion !== "blocked") {
|
|
@@ -2338,6 +2370,13 @@ function toDispatchArgs(input) {
|
|
|
2338
2370
|
if (input.sessionId !== undefined && input.sessionId === "") {
|
|
2339
2371
|
return err("space-bus: sessionId must be a non-empty string");
|
|
2340
2372
|
}
|
|
2373
|
+
let messageId;
|
|
2374
|
+
if (input.messageId !== undefined) {
|
|
2375
|
+
const validated = validateMessageId(input.messageId);
|
|
2376
|
+
if (!validated.ok)
|
|
2377
|
+
return validated;
|
|
2378
|
+
messageId = validated.messageId;
|
|
2379
|
+
}
|
|
2341
2380
|
if (!input.sessionId) {
|
|
2342
2381
|
if (!input.project) {
|
|
2343
2382
|
return err("space-bus: project is required when starting a new session");
|
|
@@ -2347,7 +2386,8 @@ function toDispatchArgs(input) {
|
|
|
2347
2386
|
prompt: input.prompt,
|
|
2348
2387
|
title: input.title,
|
|
2349
2388
|
project: input.project,
|
|
2350
|
-
onPendingQuestion: input.onPendingQuestion
|
|
2389
|
+
onPendingQuestion: input.onPendingQuestion,
|
|
2390
|
+
...messageId !== undefined ? { messageId } : {}
|
|
2351
2391
|
};
|
|
2352
2392
|
}
|
|
2353
2393
|
return {
|
|
@@ -2356,7 +2396,21 @@ function toDispatchArgs(input) {
|
|
|
2356
2396
|
title: input.title,
|
|
2357
2397
|
sessionId: input.sessionId,
|
|
2358
2398
|
project: input.project,
|
|
2359
|
-
onPendingQuestion: input.onPendingQuestion
|
|
2399
|
+
onPendingQuestion: input.onPendingQuestion,
|
|
2400
|
+
...messageId !== undefined ? { messageId } : {}
|
|
2401
|
+
};
|
|
2402
|
+
}
|
|
2403
|
+
async function dispatchNewBranch(baseUrl, credentials, projects, args) {
|
|
2404
|
+
const r = await dispatchNew(baseUrl, credentials, projects, args.project, args.prompt, args.title, args.messageId);
|
|
2405
|
+
if (!r.ok)
|
|
2406
|
+
return r;
|
|
2407
|
+
return {
|
|
2408
|
+
ok: true,
|
|
2409
|
+
sessionId: r.sessionId,
|
|
2410
|
+
project: r.project,
|
|
2411
|
+
mode: "new",
|
|
2412
|
+
directory: r.directory,
|
|
2413
|
+
...r.messageId !== undefined ? { messageId: r.messageId } : {}
|
|
2360
2414
|
};
|
|
2361
2415
|
}
|
|
2362
2416
|
async function dispatch(args, opts) {
|
|
@@ -2364,6 +2418,11 @@ async function dispatch(args, opts) {
|
|
|
2364
2418
|
if (!ctx.ok)
|
|
2365
2419
|
return ctx;
|
|
2366
2420
|
const { baseUrl, projects, credentials } = ctx;
|
|
2421
|
+
if (args.messageId !== undefined) {
|
|
2422
|
+
const validated = validateMessageId(args.messageId);
|
|
2423
|
+
if (!validated.ok)
|
|
2424
|
+
return validated;
|
|
2425
|
+
}
|
|
2367
2426
|
if (args.sessionId !== undefined && args.sessionId === "") {
|
|
2368
2427
|
return err("space-bus: sessionId must be a non-empty string");
|
|
2369
2428
|
}
|
|
@@ -2371,16 +2430,10 @@ async function dispatch(args, opts) {
|
|
|
2371
2430
|
if (!args.project) {
|
|
2372
2431
|
return err("space-bus: project is required when starting a new session");
|
|
2373
2432
|
}
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
ok: true,
|
|
2379
|
-
sessionId: r.sessionId,
|
|
2380
|
-
project: r.project,
|
|
2381
|
-
mode: "new",
|
|
2382
|
-
directory: r.directory
|
|
2383
|
-
};
|
|
2433
|
+
return dispatchNewBranch(baseUrl, credentials, projects, {
|
|
2434
|
+
...args,
|
|
2435
|
+
project: args.project
|
|
2436
|
+
});
|
|
2384
2437
|
}
|
|
2385
2438
|
const loc = await findSessionDirectory(baseUrl, credentials, projects, args.sessionId);
|
|
2386
2439
|
if (!loc.ok)
|
|
@@ -2389,7 +2442,7 @@ async function dispatch(args, opts) {
|
|
|
2389
2442
|
if (args.project && args.project !== project) {
|
|
2390
2443
|
return err(`space-bus: session ${args.sessionId} belongs to project "${project}", not "${args.project}" — refusing to steer the wrong session`);
|
|
2391
2444
|
}
|
|
2392
|
-
return steerSession(baseUrl, credentials, args.sessionId, args.prompt, directory, project, args.onPendingQuestion ?? "question-reply");
|
|
2445
|
+
return steerSession(baseUrl, credentials, args.sessionId, args.prompt, directory, project, args.onPendingQuestion ?? "question-reply", args.messageId);
|
|
2393
2446
|
}
|
|
2394
2447
|
async function findSessionDirectory(baseUrl, credentials, projects, sessionId) {
|
|
2395
2448
|
for (const p of projects) {
|
|
@@ -2494,11 +2547,11 @@ async function status(sessionId, opts) {
|
|
|
2494
2547
|
resultAvailable: state === "complete"
|
|
2495
2548
|
};
|
|
2496
2549
|
}
|
|
2497
|
-
async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, onPendingQuestion) {
|
|
2550
|
+
async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, project, onPendingQuestion) {
|
|
2498
2551
|
const questionsRes = await api(baseUrl, credentials, directory, "/question");
|
|
2499
2552
|
if (!questionsRes.res.ok) {
|
|
2500
2553
|
if (onPendingQuestion === "blocked") {
|
|
2501
|
-
return
|
|
2554
|
+
return dispatchErr(`space-bus: could not verify pending-question state for session ${sessionId} (/question ${questionsRes.res.status}) — refusing to dispatch under the blocked policy`, { phase: "not_sent", project, sessionId });
|
|
2502
2555
|
}
|
|
2503
2556
|
return { ok: true, pending: undefined };
|
|
2504
2557
|
}
|
|
@@ -2507,7 +2560,7 @@ async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, s
|
|
|
2507
2560
|
pendingList = questionListSchema.parse(JSON.parse(questionsRes.bodyText));
|
|
2508
2561
|
} catch (e) {
|
|
2509
2562
|
if (onPendingQuestion === "blocked") {
|
|
2510
|
-
return
|
|
2563
|
+
return dispatchErr(`space-bus: could not parse pending-question state for session ${sessionId}: ${e.message} — refusing to dispatch under the blocked policy`, { phase: "not_sent", project, sessionId });
|
|
2511
2564
|
}
|
|
2512
2565
|
return { ok: true, pending: undefined };
|
|
2513
2566
|
}
|
|
@@ -2516,8 +2569,8 @@ async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, s
|
|
|
2516
2569
|
pending: pendingList.find((q) => q.sessionID === sessionId)
|
|
2517
2570
|
};
|
|
2518
2571
|
}
|
|
2519
|
-
async function steerSession(baseUrl, credentials, sessionId, message, directory, project, onPendingQuestion) {
|
|
2520
|
-
const pendingRes = await resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, onPendingQuestion);
|
|
2572
|
+
async function steerSession(baseUrl, credentials, sessionId, message, directory, project, onPendingQuestion, messageId) {
|
|
2573
|
+
const pendingRes = await resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, project, onPendingQuestion);
|
|
2521
2574
|
if (!pendingRes.ok)
|
|
2522
2575
|
return pendingRes;
|
|
2523
2576
|
const { pending } = pendingRes;
|
|
@@ -2536,18 +2589,27 @@ async function steerSession(baseUrl, credentials, sessionId, message, directory,
|
|
|
2536
2589
|
body: JSON.stringify({ answers: [[message]] })
|
|
2537
2590
|
});
|
|
2538
2591
|
if (!replyRes.res.ok) {
|
|
2539
|
-
return
|
|
2592
|
+
return dispatchErr(`space-bus: failed to reply to question ${pending.id} for session ${sessionId} (${replyRes.res.status}): ${replyRes.bodyText}`, { phase: "indeterminate", project, sessionId });
|
|
2540
2593
|
}
|
|
2541
2594
|
return { ok: true, sessionId, project, mode: "question-reply" };
|
|
2542
2595
|
}
|
|
2543
2596
|
const promptRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(sessionId)}/prompt_async`, {
|
|
2544
2597
|
method: "POST",
|
|
2545
|
-
body: JSON.stringify({
|
|
2598
|
+
body: JSON.stringify({
|
|
2599
|
+
parts: [{ type: "text", text: message }],
|
|
2600
|
+
...messageId !== undefined ? { messageID: messageId } : {}
|
|
2601
|
+
})
|
|
2546
2602
|
});
|
|
2547
2603
|
if (promptRes.res.status !== 204) {
|
|
2548
|
-
return
|
|
2604
|
+
return dispatchErr(`space-bus: follow-up prompt to session ${sessionId} failed (${promptRes.res.status}): ${promptRes.bodyText}`, { phase: "indeterminate", project, sessionId, messageId });
|
|
2549
2605
|
}
|
|
2550
|
-
return {
|
|
2606
|
+
return {
|
|
2607
|
+
ok: true,
|
|
2608
|
+
sessionId,
|
|
2609
|
+
project,
|
|
2610
|
+
mode: "follow-up",
|
|
2611
|
+
...messageId !== undefined ? { messageId } : {}
|
|
2612
|
+
};
|
|
2551
2613
|
}
|
|
2552
2614
|
async function result(sessionId, opts) {
|
|
2553
2615
|
const ctx = validateContext(opts.context);
|
|
@@ -2837,6 +2899,7 @@ function makeBusTask(defaultDirectory) {
|
|
|
2837
2899
|
prompt: tool5.schema.string().describe("The prompt to send to the delegated agent"),
|
|
2838
2900
|
title: tool5.schema.string().optional().describe("Optional session title (only used when starting a new session)"),
|
|
2839
2901
|
sessionId: tool5.schema.string().optional().describe("Existing session ID to steer instead of starting a new session"),
|
|
2902
|
+
messageId: tool5.schema.string().optional().describe("Optional caller-supplied id to correlate this prompt with the message OpenCode creates for it (msg_ + 12 hex + 14 alphanumeric chars). Omit to dispatch without correlation."),
|
|
2840
2903
|
roster: tool5.schema.string().optional().describe("Registry roster name to target. Resolution precedence: this param > workspace directory (see bus_registry to list)")
|
|
2841
2904
|
},
|
|
2842
2905
|
async execute(args, ctx) {
|
package/dist/mcp.js
CHANGED
|
@@ -1464,6 +1464,15 @@ function findProject(projects, name) {
|
|
|
1464
1464
|
function err(error) {
|
|
1465
1465
|
return { ok: false, error };
|
|
1466
1466
|
}
|
|
1467
|
+
function dispatchErr(error, failure) {
|
|
1468
|
+
const dispatchFailure = {
|
|
1469
|
+
phase: failure.phase,
|
|
1470
|
+
project: failure.project,
|
|
1471
|
+
...failure.sessionId !== undefined ? { sessionId: failure.sessionId } : {},
|
|
1472
|
+
...failure.messageId !== undefined ? { messageId: failure.messageId } : {}
|
|
1473
|
+
};
|
|
1474
|
+
return { ok: false, error, dispatchFailure };
|
|
1475
|
+
}
|
|
1467
1476
|
function toBase64(s) {
|
|
1468
1477
|
const bytes = new TextEncoder().encode(s);
|
|
1469
1478
|
let bin = "";
|
|
@@ -1663,7 +1672,7 @@ async function roster(opts) {
|
|
|
1663
1672
|
}));
|
|
1664
1673
|
return { ok: true, projects: results };
|
|
1665
1674
|
}
|
|
1666
|
-
async function dispatchNew(baseUrl, credentials, projects, project, prompt, title) {
|
|
1675
|
+
async function dispatchNew(baseUrl, credentials, projects, project, prompt, title, messageId) {
|
|
1667
1676
|
const resolved = resolveProjectOrErr(projects, project);
|
|
1668
1677
|
if (!resolved.ok)
|
|
1669
1678
|
return resolved;
|
|
@@ -1674,22 +1683,39 @@ async function dispatchNew(baseUrl, credentials, projects, project, prompt, titl
|
|
|
1674
1683
|
body: JSON.stringify({ title: sessionTitle })
|
|
1675
1684
|
});
|
|
1676
1685
|
if (!createRes.res.ok) {
|
|
1677
|
-
return
|
|
1686
|
+
return dispatchErr(`space-bus: failed to create session in "${project}" (${createRes.res.status}): ${createRes.bodyText}`, { phase: "indeterminate", project, messageId });
|
|
1678
1687
|
}
|
|
1679
1688
|
let session;
|
|
1680
1689
|
try {
|
|
1681
1690
|
session = sessionSchema.parse(JSON.parse(createRes.bodyText));
|
|
1682
1691
|
} catch (e) {
|
|
1683
|
-
return
|
|
1692
|
+
return dispatchErr(`space-bus: unexpected /session response shape: ${e.message}`, { phase: "indeterminate", project, messageId });
|
|
1684
1693
|
}
|
|
1685
1694
|
const promptRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(session.id)}/prompt_async`, {
|
|
1686
1695
|
method: "POST",
|
|
1687
|
-
body: JSON.stringify({
|
|
1696
|
+
body: JSON.stringify({
|
|
1697
|
+
parts: [{ type: "text", text: prompt }],
|
|
1698
|
+
...messageId !== undefined ? { messageID: messageId } : {}
|
|
1699
|
+
})
|
|
1688
1700
|
});
|
|
1689
1701
|
if (promptRes.res.status !== 204) {
|
|
1690
|
-
return
|
|
1702
|
+
return dispatchErr(`space-bus: dispatch to "${project}" failed sending prompt (${promptRes.res.status}): ${promptRes.bodyText}`, { phase: "indeterminate", project, sessionId: session.id, messageId });
|
|
1703
|
+
}
|
|
1704
|
+
return {
|
|
1705
|
+
ok: true,
|
|
1706
|
+
sessionId: session.id,
|
|
1707
|
+
project,
|
|
1708
|
+
directory,
|
|
1709
|
+
...messageId !== undefined ? { messageId } : {}
|
|
1710
|
+
};
|
|
1711
|
+
}
|
|
1712
|
+
var MESSAGE_ID_PATTERN = /^msg_[0-9a-f]{12}[0-9A-Za-z]{14}$/;
|
|
1713
|
+
var INVALID_MESSAGE_ID_ERROR = "space-bus: messageId must be msg_ followed by exactly 12 hex characters and 14 alphanumeric characters";
|
|
1714
|
+
function validateMessageId(messageId) {
|
|
1715
|
+
if (!MESSAGE_ID_PATTERN.test(messageId)) {
|
|
1716
|
+
return err(INVALID_MESSAGE_ID_ERROR);
|
|
1691
1717
|
}
|
|
1692
|
-
return { ok: true,
|
|
1718
|
+
return { ok: true, messageId };
|
|
1693
1719
|
}
|
|
1694
1720
|
function toDispatchArgs(input) {
|
|
1695
1721
|
if (input.onPendingQuestion !== undefined && input.onPendingQuestion !== "question-reply" && input.onPendingQuestion !== "blocked") {
|
|
@@ -1698,6 +1724,13 @@ function toDispatchArgs(input) {
|
|
|
1698
1724
|
if (input.sessionId !== undefined && input.sessionId === "") {
|
|
1699
1725
|
return err("space-bus: sessionId must be a non-empty string");
|
|
1700
1726
|
}
|
|
1727
|
+
let messageId;
|
|
1728
|
+
if (input.messageId !== undefined) {
|
|
1729
|
+
const validated = validateMessageId(input.messageId);
|
|
1730
|
+
if (!validated.ok)
|
|
1731
|
+
return validated;
|
|
1732
|
+
messageId = validated.messageId;
|
|
1733
|
+
}
|
|
1701
1734
|
if (!input.sessionId) {
|
|
1702
1735
|
if (!input.project) {
|
|
1703
1736
|
return err("space-bus: project is required when starting a new session");
|
|
@@ -1707,7 +1740,8 @@ function toDispatchArgs(input) {
|
|
|
1707
1740
|
prompt: input.prompt,
|
|
1708
1741
|
title: input.title,
|
|
1709
1742
|
project: input.project,
|
|
1710
|
-
onPendingQuestion: input.onPendingQuestion
|
|
1743
|
+
onPendingQuestion: input.onPendingQuestion,
|
|
1744
|
+
...messageId !== undefined ? { messageId } : {}
|
|
1711
1745
|
};
|
|
1712
1746
|
}
|
|
1713
1747
|
return {
|
|
@@ -1716,7 +1750,21 @@ function toDispatchArgs(input) {
|
|
|
1716
1750
|
title: input.title,
|
|
1717
1751
|
sessionId: input.sessionId,
|
|
1718
1752
|
project: input.project,
|
|
1719
|
-
onPendingQuestion: input.onPendingQuestion
|
|
1753
|
+
onPendingQuestion: input.onPendingQuestion,
|
|
1754
|
+
...messageId !== undefined ? { messageId } : {}
|
|
1755
|
+
};
|
|
1756
|
+
}
|
|
1757
|
+
async function dispatchNewBranch(baseUrl, credentials, projects, args) {
|
|
1758
|
+
const r = await dispatchNew(baseUrl, credentials, projects, args.project, args.prompt, args.title, args.messageId);
|
|
1759
|
+
if (!r.ok)
|
|
1760
|
+
return r;
|
|
1761
|
+
return {
|
|
1762
|
+
ok: true,
|
|
1763
|
+
sessionId: r.sessionId,
|
|
1764
|
+
project: r.project,
|
|
1765
|
+
mode: "new",
|
|
1766
|
+
directory: r.directory,
|
|
1767
|
+
...r.messageId !== undefined ? { messageId: r.messageId } : {}
|
|
1720
1768
|
};
|
|
1721
1769
|
}
|
|
1722
1770
|
async function dispatch(args, opts) {
|
|
@@ -1724,6 +1772,11 @@ async function dispatch(args, opts) {
|
|
|
1724
1772
|
if (!ctx.ok)
|
|
1725
1773
|
return ctx;
|
|
1726
1774
|
const { baseUrl, projects, credentials } = ctx;
|
|
1775
|
+
if (args.messageId !== undefined) {
|
|
1776
|
+
const validated = validateMessageId(args.messageId);
|
|
1777
|
+
if (!validated.ok)
|
|
1778
|
+
return validated;
|
|
1779
|
+
}
|
|
1727
1780
|
if (args.sessionId !== undefined && args.sessionId === "") {
|
|
1728
1781
|
return err("space-bus: sessionId must be a non-empty string");
|
|
1729
1782
|
}
|
|
@@ -1731,16 +1784,10 @@ async function dispatch(args, opts) {
|
|
|
1731
1784
|
if (!args.project) {
|
|
1732
1785
|
return err("space-bus: project is required when starting a new session");
|
|
1733
1786
|
}
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
ok: true,
|
|
1739
|
-
sessionId: r.sessionId,
|
|
1740
|
-
project: r.project,
|
|
1741
|
-
mode: "new",
|
|
1742
|
-
directory: r.directory
|
|
1743
|
-
};
|
|
1787
|
+
return dispatchNewBranch(baseUrl, credentials, projects, {
|
|
1788
|
+
...args,
|
|
1789
|
+
project: args.project
|
|
1790
|
+
});
|
|
1744
1791
|
}
|
|
1745
1792
|
const loc = await findSessionDirectory(baseUrl, credentials, projects, args.sessionId);
|
|
1746
1793
|
if (!loc.ok)
|
|
@@ -1749,7 +1796,7 @@ async function dispatch(args, opts) {
|
|
|
1749
1796
|
if (args.project && args.project !== project) {
|
|
1750
1797
|
return err(`space-bus: session ${args.sessionId} belongs to project "${project}", not "${args.project}" — refusing to steer the wrong session`);
|
|
1751
1798
|
}
|
|
1752
|
-
return steerSession(baseUrl, credentials, args.sessionId, args.prompt, directory, project, args.onPendingQuestion ?? "question-reply");
|
|
1799
|
+
return steerSession(baseUrl, credentials, args.sessionId, args.prompt, directory, project, args.onPendingQuestion ?? "question-reply", args.messageId);
|
|
1753
1800
|
}
|
|
1754
1801
|
async function findSessionDirectory(baseUrl, credentials, projects, sessionId) {
|
|
1755
1802
|
for (const p of projects) {
|
|
@@ -1854,11 +1901,11 @@ async function status(sessionId, opts) {
|
|
|
1854
1901
|
resultAvailable: state === "complete"
|
|
1855
1902
|
};
|
|
1856
1903
|
}
|
|
1857
|
-
async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, onPendingQuestion) {
|
|
1904
|
+
async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, project, onPendingQuestion) {
|
|
1858
1905
|
const questionsRes = await api(baseUrl, credentials, directory, "/question");
|
|
1859
1906
|
if (!questionsRes.res.ok) {
|
|
1860
1907
|
if (onPendingQuestion === "blocked") {
|
|
1861
|
-
return
|
|
1908
|
+
return dispatchErr(`space-bus: could not verify pending-question state for session ${sessionId} (/question ${questionsRes.res.status}) — refusing to dispatch under the blocked policy`, { phase: "not_sent", project, sessionId });
|
|
1862
1909
|
}
|
|
1863
1910
|
return { ok: true, pending: undefined };
|
|
1864
1911
|
}
|
|
@@ -1867,7 +1914,7 @@ async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, s
|
|
|
1867
1914
|
pendingList = questionListSchema.parse(JSON.parse(questionsRes.bodyText));
|
|
1868
1915
|
} catch (e) {
|
|
1869
1916
|
if (onPendingQuestion === "blocked") {
|
|
1870
|
-
return
|
|
1917
|
+
return dispatchErr(`space-bus: could not parse pending-question state for session ${sessionId}: ${e.message} — refusing to dispatch under the blocked policy`, { phase: "not_sent", project, sessionId });
|
|
1871
1918
|
}
|
|
1872
1919
|
return { ok: true, pending: undefined };
|
|
1873
1920
|
}
|
|
@@ -1876,8 +1923,8 @@ async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, s
|
|
|
1876
1923
|
pending: pendingList.find((q) => q.sessionID === sessionId)
|
|
1877
1924
|
};
|
|
1878
1925
|
}
|
|
1879
|
-
async function steerSession(baseUrl, credentials, sessionId, message, directory, project, onPendingQuestion) {
|
|
1880
|
-
const pendingRes = await resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, onPendingQuestion);
|
|
1926
|
+
async function steerSession(baseUrl, credentials, sessionId, message, directory, project, onPendingQuestion, messageId) {
|
|
1927
|
+
const pendingRes = await resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, project, onPendingQuestion);
|
|
1881
1928
|
if (!pendingRes.ok)
|
|
1882
1929
|
return pendingRes;
|
|
1883
1930
|
const { pending } = pendingRes;
|
|
@@ -1896,18 +1943,27 @@ async function steerSession(baseUrl, credentials, sessionId, message, directory,
|
|
|
1896
1943
|
body: JSON.stringify({ answers: [[message]] })
|
|
1897
1944
|
});
|
|
1898
1945
|
if (!replyRes.res.ok) {
|
|
1899
|
-
return
|
|
1946
|
+
return dispatchErr(`space-bus: failed to reply to question ${pending.id} for session ${sessionId} (${replyRes.res.status}): ${replyRes.bodyText}`, { phase: "indeterminate", project, sessionId });
|
|
1900
1947
|
}
|
|
1901
1948
|
return { ok: true, sessionId, project, mode: "question-reply" };
|
|
1902
1949
|
}
|
|
1903
1950
|
const promptRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(sessionId)}/prompt_async`, {
|
|
1904
1951
|
method: "POST",
|
|
1905
|
-
body: JSON.stringify({
|
|
1952
|
+
body: JSON.stringify({
|
|
1953
|
+
parts: [{ type: "text", text: message }],
|
|
1954
|
+
...messageId !== undefined ? { messageID: messageId } : {}
|
|
1955
|
+
})
|
|
1906
1956
|
});
|
|
1907
1957
|
if (promptRes.res.status !== 204) {
|
|
1908
|
-
return
|
|
1958
|
+
return dispatchErr(`space-bus: follow-up prompt to session ${sessionId} failed (${promptRes.res.status}): ${promptRes.bodyText}`, { phase: "indeterminate", project, sessionId, messageId });
|
|
1909
1959
|
}
|
|
1910
|
-
return {
|
|
1960
|
+
return {
|
|
1961
|
+
ok: true,
|
|
1962
|
+
sessionId,
|
|
1963
|
+
project,
|
|
1964
|
+
mode: "follow-up",
|
|
1965
|
+
...messageId !== undefined ? { messageId } : {}
|
|
1966
|
+
};
|
|
1911
1967
|
}
|
|
1912
1968
|
async function result(sessionId, opts) {
|
|
1913
1969
|
const ctx = validateContext(opts.context);
|
|
@@ -2105,7 +2161,13 @@ function formatDispatch(r) {
|
|
|
2105
2161
|
}
|
|
2106
2162
|
}
|
|
2107
2163
|
function dispatchMetadata(r) {
|
|
2108
|
-
|
|
2164
|
+
const messageId = (r.mode === "new" || r.mode === "follow-up") && r.messageId !== undefined ? r.messageId : undefined;
|
|
2165
|
+
return {
|
|
2166
|
+
sessionId: r.sessionId,
|
|
2167
|
+
project: r.project,
|
|
2168
|
+
mode: r.mode,
|
|
2169
|
+
...messageId !== undefined ? { messageId } : {}
|
|
2170
|
+
};
|
|
2109
2171
|
}
|
|
2110
2172
|
function formatStatus(r) {
|
|
2111
2173
|
const todoLines = r.todos.length ? r.todos.map((t) => ` - [${t.status}] ${t.content} (${t.priority})`).join(`
|
|
@@ -2843,6 +2905,7 @@ function makeBusTask(defaultDirectory) {
|
|
|
2843
2905
|
prompt: tool5.schema.string().describe("The prompt to send to the delegated agent"),
|
|
2844
2906
|
title: tool5.schema.string().optional().describe("Optional session title (only used when starting a new session)"),
|
|
2845
2907
|
sessionId: tool5.schema.string().optional().describe("Existing session ID to steer instead of starting a new session"),
|
|
2908
|
+
messageId: tool5.schema.string().optional().describe("Optional caller-supplied id to correlate this prompt with the message OpenCode creates for it (msg_ + 12 hex + 14 alphanumeric chars). Omit to dispatch without correlation."),
|
|
2846
2909
|
roster: tool5.schema.string().optional().describe("Registry roster name to target. Resolution precedence: this param > workspace directory (see bus_registry to list)")
|
|
2847
2910
|
},
|
|
2848
2911
|
async execute(args, ctx) {
|
|
@@ -2952,7 +3015,7 @@ async function mcpLoadContext(rosterName, ensure = ensureServer) {
|
|
|
2952
3015
|
}
|
|
2953
3016
|
throw ambientError;
|
|
2954
3017
|
}
|
|
2955
|
-
var version = "0.
|
|
3018
|
+
var version = "0.15.0";
|
|
2956
3019
|
var server = new McpServer({
|
|
2957
3020
|
name: "space-bus",
|
|
2958
3021
|
version
|
|
@@ -2992,12 +3055,14 @@ server.registerTool("bus_task", {
|
|
|
2992
3055
|
prompt: z5.string().describe("The prompt to send to the delegated agent"),
|
|
2993
3056
|
title: z5.string().optional().describe("Optional session title (only used when starting a new session)"),
|
|
2994
3057
|
sessionId: z5.string().optional().describe("Existing session ID to steer instead of starting a new session"),
|
|
3058
|
+
messageId: z5.string().optional().describe("Optional caller-supplied id to correlate this prompt with the message OpenCode creates for it (msg_ + 12 hex + 14 alphanumeric chars). Omit to dispatch without correlation."),
|
|
2995
3059
|
roster: ROSTER_PARAM_SCHEMA
|
|
2996
3060
|
},
|
|
2997
3061
|
outputSchema: {
|
|
2998
3062
|
sessionId: z5.string().describe("The dispatched or steered session ID"),
|
|
2999
3063
|
project: z5.string().describe("Manifest project name that owns the session"),
|
|
3000
|
-
mode: z5.enum(["new", "question-reply", "follow-up"]).describe("How the prompt was delivered")
|
|
3064
|
+
mode: z5.enum(["new", "question-reply", "follow-up"]).describe("How the prompt was delivered"),
|
|
3065
|
+
messageId: z5.string().optional().describe("Echoes the supplied messageId for new/follow-up dispatches; omitted otherwise")
|
|
3001
3066
|
}
|
|
3002
3067
|
}, async (args) => {
|
|
3003
3068
|
const dispatchArgs = toDispatchArgs(args);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fro.bot/space-bus",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Space Bus — workspace agent bus for OpenCode. One control agent tasking per-project agents over the OpenCode server API, with an MCP facade for Claude Desktop.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -81,8 +81,8 @@
|
|
|
81
81
|
},
|
|
82
82
|
"devDependencies": {
|
|
83
83
|
"@biomejs/biome": "2.5.2",
|
|
84
|
-
"@changesets/cli": "2.31.
|
|
85
|
-
"@opencode-ai/plugin": "1.
|
|
84
|
+
"@changesets/cli": "2.31.1",
|
|
85
|
+
"@opencode-ai/plugin": "1.18.2",
|
|
86
86
|
"@types/bun": "1.3.14",
|
|
87
87
|
"typescript": "5.9.3"
|
|
88
88
|
}
|