@fro.bot/space-bus 0.13.1 → 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/README.md +5 -1
- package/dist/attach.js +12 -2
- package/dist/cli.js +28 -15
- package/dist/config.d.ts +9 -4
- package/dist/config.js +27 -11
- package/dist/contract.d.ts +16 -0
- package/dist/contract.js +12 -2
- package/dist/core.d.ts +116 -1
- package/dist/core.js +326 -42
- package/dist/format.d.ts +1 -0
- package/dist/format.js +9 -1
- package/dist/index.js +162 -51
- package/dist/mcp.js +166 -53
- package/dist/registry-entry.js +27 -11
- package/dist/server.js +27 -11
- package/package.json +3 -3
package/dist/core.js
CHANGED
|
@@ -63,7 +63,11 @@ var messagePartSchema = z.looseObject({
|
|
|
63
63
|
text: z.string().optional()
|
|
64
64
|
});
|
|
65
65
|
var messageEnvelopeSchema = z.looseObject({
|
|
66
|
-
info: z.looseObject({
|
|
66
|
+
info: z.looseObject({
|
|
67
|
+
id: z.string().optional(),
|
|
68
|
+
role: z.string(),
|
|
69
|
+
time: z.looseObject({ created: z.number().optional() }).optional()
|
|
70
|
+
}),
|
|
67
71
|
parts: z.array(messagePartSchema)
|
|
68
72
|
});
|
|
69
73
|
var messageListSchema = z.array(messageEnvelopeSchema);
|
|
@@ -72,7 +76,13 @@ var pendingQuestionEntrySchema = z.looseObject({
|
|
|
72
76
|
sessionID: z.string(),
|
|
73
77
|
questions: z.array(z.looseObject({
|
|
74
78
|
question: z.string().optional(),
|
|
75
|
-
|
|
79
|
+
header: z.string().optional(),
|
|
80
|
+
multiple: z.boolean().optional(),
|
|
81
|
+
custom: z.boolean().optional(),
|
|
82
|
+
options: z.array(z.looseObject({
|
|
83
|
+
label: z.string().optional(),
|
|
84
|
+
description: z.string().optional()
|
|
85
|
+
})).optional()
|
|
76
86
|
})).optional()
|
|
77
87
|
});
|
|
78
88
|
var pendingQuestionListSchema = z.array(pendingQuestionEntrySchema);
|
|
@@ -158,6 +168,15 @@ function findProject(projects, name) {
|
|
|
158
168
|
function err(error) {
|
|
159
169
|
return { ok: false, error };
|
|
160
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
|
+
}
|
|
161
180
|
function toBase64(s) {
|
|
162
181
|
const bytes = new TextEncoder().encode(s);
|
|
163
182
|
let bin = "";
|
|
@@ -357,7 +376,7 @@ async function roster(opts) {
|
|
|
357
376
|
}));
|
|
358
377
|
return { ok: true, projects: results };
|
|
359
378
|
}
|
|
360
|
-
async function dispatchNew(baseUrl, credentials, projects, project, prompt, title) {
|
|
379
|
+
async function dispatchNew(baseUrl, credentials, projects, project, prompt, title, messageId) {
|
|
361
380
|
const resolved = resolveProjectOrErr(projects, project);
|
|
362
381
|
if (!resolved.ok)
|
|
363
382
|
return resolved;
|
|
@@ -368,27 +387,88 @@ async function dispatchNew(baseUrl, credentials, projects, project, prompt, titl
|
|
|
368
387
|
body: JSON.stringify({ title: sessionTitle })
|
|
369
388
|
});
|
|
370
389
|
if (!createRes.res.ok) {
|
|
371
|
-
return
|
|
390
|
+
return dispatchErr(`space-bus: failed to create session in "${project}" (${createRes.res.status}): ${createRes.bodyText}`, { phase: "indeterminate", project, messageId });
|
|
372
391
|
}
|
|
373
392
|
let session;
|
|
374
393
|
try {
|
|
375
394
|
session = sessionSchema.parse(JSON.parse(createRes.bodyText));
|
|
376
395
|
} catch (e) {
|
|
377
|
-
return
|
|
396
|
+
return dispatchErr(`space-bus: unexpected /session response shape: ${e.message}`, { phase: "indeterminate", project, messageId });
|
|
378
397
|
}
|
|
379
398
|
const promptRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(session.id)}/prompt_async`, {
|
|
380
399
|
method: "POST",
|
|
381
|
-
body: JSON.stringify({
|
|
400
|
+
body: JSON.stringify({
|
|
401
|
+
parts: [{ type: "text", text: prompt }],
|
|
402
|
+
...messageId !== undefined ? { messageID: messageId } : {}
|
|
403
|
+
})
|
|
382
404
|
});
|
|
383
405
|
if (promptRes.res.status !== 204) {
|
|
384
|
-
return
|
|
406
|
+
return dispatchErr(`space-bus: dispatch to "${project}" failed sending prompt (${promptRes.res.status}): ${promptRes.bodyText}`, { phase: "indeterminate", project, sessionId: session.id, messageId });
|
|
385
407
|
}
|
|
386
|
-
return {
|
|
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);
|
|
455
|
+
}
|
|
456
|
+
return { ok: true, messageId };
|
|
387
457
|
}
|
|
388
458
|
function toDispatchArgs(input) {
|
|
459
|
+
if (input.onPendingQuestion !== undefined && input.onPendingQuestion !== "question-reply" && input.onPendingQuestion !== "blocked") {
|
|
460
|
+
return err(`space-bus: onPendingQuestion must be "question-reply" or "blocked", got ${JSON.stringify(input.onPendingQuestion)}`);
|
|
461
|
+
}
|
|
389
462
|
if (input.sessionId !== undefined && input.sessionId === "") {
|
|
390
463
|
return err("space-bus: sessionId must be a non-empty string");
|
|
391
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
|
+
}
|
|
392
472
|
if (!input.sessionId) {
|
|
393
473
|
if (!input.project) {
|
|
394
474
|
return err("space-bus: project is required when starting a new session");
|
|
@@ -397,7 +477,9 @@ function toDispatchArgs(input) {
|
|
|
397
477
|
ok: true,
|
|
398
478
|
prompt: input.prompt,
|
|
399
479
|
title: input.title,
|
|
400
|
-
project: input.project
|
|
480
|
+
project: input.project,
|
|
481
|
+
onPendingQuestion: input.onPendingQuestion,
|
|
482
|
+
...messageId !== undefined ? { messageId } : {}
|
|
401
483
|
};
|
|
402
484
|
}
|
|
403
485
|
return {
|
|
@@ -405,7 +487,22 @@ function toDispatchArgs(input) {
|
|
|
405
487
|
prompt: input.prompt,
|
|
406
488
|
title: input.title,
|
|
407
489
|
sessionId: input.sessionId,
|
|
408
|
-
project: input.project
|
|
490
|
+
project: input.project,
|
|
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 } : {}
|
|
409
506
|
};
|
|
410
507
|
}
|
|
411
508
|
async function dispatch(args, opts) {
|
|
@@ -413,6 +510,11 @@ async function dispatch(args, opts) {
|
|
|
413
510
|
if (!ctx.ok)
|
|
414
511
|
return ctx;
|
|
415
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
|
+
}
|
|
416
518
|
if (args.sessionId !== undefined && args.sessionId === "") {
|
|
417
519
|
return err("space-bus: sessionId must be a non-empty string");
|
|
418
520
|
}
|
|
@@ -420,16 +522,10 @@ async function dispatch(args, opts) {
|
|
|
420
522
|
if (!args.project) {
|
|
421
523
|
return err("space-bus: project is required when starting a new session");
|
|
422
524
|
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
ok: true,
|
|
428
|
-
sessionId: r.sessionId,
|
|
429
|
-
project: r.project,
|
|
430
|
-
mode: "new",
|
|
431
|
-
directory: r.directory
|
|
432
|
-
};
|
|
525
|
+
return dispatchNewBranch(baseUrl, credentials, projects, {
|
|
526
|
+
...args,
|
|
527
|
+
project: args.project
|
|
528
|
+
});
|
|
433
529
|
}
|
|
434
530
|
const loc = await findSessionDirectory(baseUrl, credentials, projects, args.sessionId);
|
|
435
531
|
if (!loc.ok)
|
|
@@ -438,7 +534,7 @@ async function dispatch(args, opts) {
|
|
|
438
534
|
if (args.project && args.project !== project) {
|
|
439
535
|
return err(`space-bus: session ${args.sessionId} belongs to project "${project}", not "${args.project}" — refusing to steer the wrong session`);
|
|
440
536
|
}
|
|
441
|
-
return steerSession(baseUrl, credentials, args.sessionId, args.prompt, directory, project);
|
|
537
|
+
return steerSession(baseUrl, credentials, args.sessionId, args.prompt, directory, project, args.onPendingQuestion ?? "question-reply", args.messageId);
|
|
442
538
|
}
|
|
443
539
|
async function findSessionDirectory(baseUrl, credentials, projects, sessionId) {
|
|
444
540
|
for (const p of projects) {
|
|
@@ -543,35 +639,69 @@ async function status(sessionId, opts) {
|
|
|
543
639
|
resultAvailable: state === "complete"
|
|
544
640
|
};
|
|
545
641
|
}
|
|
546
|
-
async function
|
|
642
|
+
async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, project, onPendingQuestion) {
|
|
547
643
|
const questionsRes = await api(baseUrl, credentials, directory, "/question");
|
|
548
|
-
if (questionsRes.res.ok) {
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
questions = questionListSchema.parse(JSON.parse(questionsRes.bodyText));
|
|
552
|
-
} catch {
|
|
553
|
-
questions = [];
|
|
644
|
+
if (!questionsRes.res.ok) {
|
|
645
|
+
if (onPendingQuestion === "blocked") {
|
|
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 });
|
|
554
647
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
648
|
+
return { ok: true, pending: undefined };
|
|
649
|
+
}
|
|
650
|
+
let pendingList;
|
|
651
|
+
try {
|
|
652
|
+
pendingList = questionListSchema.parse(JSON.parse(questionsRes.bodyText));
|
|
653
|
+
} catch (e) {
|
|
654
|
+
if (onPendingQuestion === "blocked") {
|
|
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 });
|
|
656
|
+
}
|
|
657
|
+
return { ok: true, pending: undefined };
|
|
658
|
+
}
|
|
659
|
+
return {
|
|
660
|
+
ok: true,
|
|
661
|
+
pending: pendingList.find((q) => q.sessionID === sessionId)
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
async function steerSession(baseUrl, credentials, sessionId, message, directory, project, onPendingQuestion, messageId) {
|
|
665
|
+
const pendingRes = await resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, project, onPendingQuestion);
|
|
666
|
+
if (!pendingRes.ok)
|
|
667
|
+
return pendingRes;
|
|
668
|
+
const { pending } = pendingRes;
|
|
669
|
+
if (pending && onPendingQuestion === "blocked") {
|
|
670
|
+
return {
|
|
671
|
+
ok: true,
|
|
672
|
+
sessionId,
|
|
673
|
+
project,
|
|
674
|
+
mode: "blocked",
|
|
675
|
+
requestId: pending.id
|
|
676
|
+
};
|
|
677
|
+
}
|
|
678
|
+
if (pending) {
|
|
679
|
+
const replyRes = await api(baseUrl, credentials, directory, `/question/${encodeURIComponent(pending.id)}/reply`, {
|
|
680
|
+
method: "POST",
|
|
681
|
+
body: JSON.stringify({ answers: [[message]] })
|
|
682
|
+
});
|
|
683
|
+
if (!replyRes.res.ok) {
|
|
684
|
+
return dispatchErr(`space-bus: failed to reply to question ${pending.id} for session ${sessionId} (${replyRes.res.status}): ${replyRes.bodyText}`, { phase: "indeterminate", project, sessionId });
|
|
565
685
|
}
|
|
686
|
+
return { ok: true, sessionId, project, mode: "question-reply" };
|
|
566
687
|
}
|
|
567
688
|
const promptRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(sessionId)}/prompt_async`, {
|
|
568
689
|
method: "POST",
|
|
569
|
-
body: JSON.stringify({
|
|
690
|
+
body: JSON.stringify({
|
|
691
|
+
parts: [{ type: "text", text: message }],
|
|
692
|
+
...messageId !== undefined ? { messageID: messageId } : {}
|
|
693
|
+
})
|
|
570
694
|
});
|
|
571
695
|
if (promptRes.res.status !== 204) {
|
|
572
|
-
return
|
|
696
|
+
return dispatchErr(`space-bus: follow-up prompt to session ${sessionId} failed (${promptRes.res.status}): ${promptRes.bodyText}`, { phase: "indeterminate", project, sessionId, messageId });
|
|
573
697
|
}
|
|
574
|
-
return {
|
|
698
|
+
return {
|
|
699
|
+
ok: true,
|
|
700
|
+
sessionId,
|
|
701
|
+
project,
|
|
702
|
+
mode: "follow-up",
|
|
703
|
+
...messageId !== undefined ? { messageId } : {}
|
|
704
|
+
};
|
|
575
705
|
}
|
|
576
706
|
async function result(sessionId, opts) {
|
|
577
707
|
const ctx = validateContext(opts.context);
|
|
@@ -828,6 +958,156 @@ function parsePendingQuestions(questionRes) {
|
|
|
828
958
|
return new Map;
|
|
829
959
|
}
|
|
830
960
|
}
|
|
961
|
+
var DEFAULT_MESSAGE_LIMIT = 20;
|
|
962
|
+
var MAX_MESSAGE_LIMIT = 200;
|
|
963
|
+
function isValidMessageLimit(limit) {
|
|
964
|
+
return Number.isFinite(limit) && Number.isInteger(limit) && limit > 0 && limit <= MAX_MESSAGE_LIMIT;
|
|
965
|
+
}
|
|
966
|
+
async function messages(sessionId, opts) {
|
|
967
|
+
const ctx = validateContext(opts.context);
|
|
968
|
+
if (!ctx.ok)
|
|
969
|
+
return ctx;
|
|
970
|
+
const { baseUrl, projects, credentials } = ctx;
|
|
971
|
+
const limit = opts.limit ?? DEFAULT_MESSAGE_LIMIT;
|
|
972
|
+
if (!isValidMessageLimit(limit)) {
|
|
973
|
+
return err(`space-bus: limit must be a finite positive integer no greater than ${MAX_MESSAGE_LIMIT}, got ${limit}`);
|
|
974
|
+
}
|
|
975
|
+
const loc = await findSessionDirectory(baseUrl, credentials, projects, sessionId);
|
|
976
|
+
if (!loc.ok)
|
|
977
|
+
return loc;
|
|
978
|
+
const { directory, project } = loc;
|
|
979
|
+
const { res, bodyText } = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(sessionId)}/message?limit=${limit}`);
|
|
980
|
+
if (!res.ok) {
|
|
981
|
+
return err(`space-bus: failed to fetch messages for ${sessionId} (${res.status})`);
|
|
982
|
+
}
|
|
983
|
+
let parsed;
|
|
984
|
+
try {
|
|
985
|
+
parsed = messageListSchema.parse(JSON.parse(bodyText));
|
|
986
|
+
} catch (e) {
|
|
987
|
+
return err(`space-bus: unexpected response shape for session ${sessionId} messages: ${e.message}`);
|
|
988
|
+
}
|
|
989
|
+
return {
|
|
990
|
+
ok: true,
|
|
991
|
+
sessionId,
|
|
992
|
+
project,
|
|
993
|
+
messages: parsed.map((m) => ({
|
|
994
|
+
id: m.info.id,
|
|
995
|
+
role: m.info.role,
|
|
996
|
+
createdAt: m.info.time?.created,
|
|
997
|
+
parts: m.parts.map((p) => ({ type: p.type, text: p.text }))
|
|
998
|
+
}))
|
|
999
|
+
};
|
|
1000
|
+
}
|
|
1001
|
+
function toPendingQuestionView(entry) {
|
|
1002
|
+
return {
|
|
1003
|
+
requestId: entry.id,
|
|
1004
|
+
sessionId: entry.sessionID,
|
|
1005
|
+
questions: (entry.questions ?? []).map((q) => ({
|
|
1006
|
+
header: q.header,
|
|
1007
|
+
question: q.question ?? "",
|
|
1008
|
+
multiple: q.multiple ?? false,
|
|
1009
|
+
custom: q.custom ?? false,
|
|
1010
|
+
options: (q.options ?? []).map((o) => ({
|
|
1011
|
+
label: o.label ?? "",
|
|
1012
|
+
description: o.description
|
|
1013
|
+
}))
|
|
1014
|
+
}))
|
|
1015
|
+
};
|
|
1016
|
+
}
|
|
1017
|
+
async function resolveQuestionsTarget(target, baseUrl, credentials, projects) {
|
|
1018
|
+
const hasProject = "project" in target && !!target.project;
|
|
1019
|
+
const hasSessionId = "sessionId" in target && !!target.sessionId;
|
|
1020
|
+
if (hasProject && hasSessionId) {
|
|
1021
|
+
return err("space-bus: questions target must specify exactly one of project or sessionId, not both");
|
|
1022
|
+
}
|
|
1023
|
+
if (!hasProject && !hasSessionId) {
|
|
1024
|
+
return err("space-bus: questions target must specify exactly one of project or sessionId");
|
|
1025
|
+
}
|
|
1026
|
+
if (hasProject) {
|
|
1027
|
+
const projRes = resolveProjectOrErr(projects, target.project);
|
|
1028
|
+
if (!projRes.ok)
|
|
1029
|
+
return projRes;
|
|
1030
|
+
return {
|
|
1031
|
+
ok: true,
|
|
1032
|
+
directory: projRes.project.expandedPath,
|
|
1033
|
+
sessionFilter: undefined
|
|
1034
|
+
};
|
|
1035
|
+
}
|
|
1036
|
+
const targetSessionId = target.sessionId;
|
|
1037
|
+
const loc = await findSessionDirectory(baseUrl, credentials, projects, targetSessionId);
|
|
1038
|
+
if (!loc.ok)
|
|
1039
|
+
return loc;
|
|
1040
|
+
return { ok: true, directory: loc.directory, sessionFilter: targetSessionId };
|
|
1041
|
+
}
|
|
1042
|
+
async function questions(target, opts) {
|
|
1043
|
+
const ctx = validateContext(opts.context);
|
|
1044
|
+
if (!ctx.ok)
|
|
1045
|
+
return ctx;
|
|
1046
|
+
const { baseUrl, projects, credentials } = ctx;
|
|
1047
|
+
const targetRes = await resolveQuestionsTarget(target, baseUrl, credentials, projects);
|
|
1048
|
+
if (!targetRes.ok)
|
|
1049
|
+
return targetRes;
|
|
1050
|
+
const { directory, sessionFilter } = targetRes;
|
|
1051
|
+
const { res, bodyText } = await api(baseUrl, credentials, directory, "/question");
|
|
1052
|
+
if (!res.ok) {
|
|
1053
|
+
return err(`space-bus: failed to fetch questions (${res.status})`);
|
|
1054
|
+
}
|
|
1055
|
+
let entries;
|
|
1056
|
+
try {
|
|
1057
|
+
entries = pendingQuestionListSchema.parse(JSON.parse(bodyText));
|
|
1058
|
+
} catch (e) {
|
|
1059
|
+
return err(`space-bus: unexpected response shape for questions: ${e.message}`);
|
|
1060
|
+
}
|
|
1061
|
+
const filtered = sessionFilter ? entries.filter((e) => e.sessionID === sessionFilter) : entries;
|
|
1062
|
+
return { ok: true, questions: filtered.map(toPendingQuestionView) };
|
|
1063
|
+
}
|
|
1064
|
+
function isValidAnswersShape(value) {
|
|
1065
|
+
if (!Array.isArray(value) || value.length === 0)
|
|
1066
|
+
return false;
|
|
1067
|
+
return value.every((row) => Array.isArray(row) && row.every((cell) => typeof cell === "string"));
|
|
1068
|
+
}
|
|
1069
|
+
async function answerQuestion(args, opts) {
|
|
1070
|
+
const ctx = validateContext(opts.context);
|
|
1071
|
+
if (!ctx.ok)
|
|
1072
|
+
return ctx;
|
|
1073
|
+
const { baseUrl, projects, credentials } = ctx;
|
|
1074
|
+
if (!isValidAnswersShape(args.answers)) {
|
|
1075
|
+
return err("space-bus: answers must be a non-empty array of string arrays (string[][])");
|
|
1076
|
+
}
|
|
1077
|
+
const loc = await findSessionDirectory(baseUrl, credentials, projects, args.sessionId);
|
|
1078
|
+
if (!loc.ok)
|
|
1079
|
+
return loc;
|
|
1080
|
+
const { directory } = loc;
|
|
1081
|
+
const questionsRes = await api(baseUrl, credentials, directory, "/question");
|
|
1082
|
+
if (!questionsRes.res.ok) {
|
|
1083
|
+
return err(`space-bus: failed to fetch questions for session ${args.sessionId} (${questionsRes.res.status})`);
|
|
1084
|
+
}
|
|
1085
|
+
let pendingList;
|
|
1086
|
+
try {
|
|
1087
|
+
pendingList = pendingQuestionListSchema.parse(JSON.parse(questionsRes.bodyText));
|
|
1088
|
+
} catch (e) {
|
|
1089
|
+
return err(`space-bus: unexpected response shape for questions: ${e.message}`);
|
|
1090
|
+
}
|
|
1091
|
+
const pending = pendingList.find((q) => q.id === args.requestId && q.sessionID === args.sessionId);
|
|
1092
|
+
if (!pending) {
|
|
1093
|
+
return err(`space-bus: request ${args.requestId} does not belong to a pending question on session ${args.sessionId} — refusing to answer`);
|
|
1094
|
+
}
|
|
1095
|
+
if (!pending.questions || pending.questions.length === 0) {
|
|
1096
|
+
return err(`space-bus: request ${args.requestId} for session ${args.sessionId} has no subquestion metadata — refusing to answer without a verified cardinality`);
|
|
1097
|
+
}
|
|
1098
|
+
const expectedRows = pending.questions.length;
|
|
1099
|
+
if (args.answers.length !== expectedRows) {
|
|
1100
|
+
return err(`space-bus: answers has ${args.answers.length} row(s) but request ${args.requestId} has ${expectedRows} subquestion(s) — refusing to answer with mismatched cardinality`);
|
|
1101
|
+
}
|
|
1102
|
+
const replyRes = await api(baseUrl, credentials, directory, `/question/${encodeURIComponent(args.requestId)}/reply`, {
|
|
1103
|
+
method: "POST",
|
|
1104
|
+
body: JSON.stringify({ answers: args.answers })
|
|
1105
|
+
});
|
|
1106
|
+
if (!replyRes.res.ok) {
|
|
1107
|
+
return err(`space-bus: failed to answer question ${args.requestId} for session ${args.sessionId} (${replyRes.res.status})`);
|
|
1108
|
+
}
|
|
1109
|
+
return { ok: true, sessionId: args.sessionId, requestId: args.requestId };
|
|
1110
|
+
}
|
|
831
1111
|
export {
|
|
832
1112
|
wait,
|
|
833
1113
|
toDispatchArgs,
|
|
@@ -835,6 +1115,10 @@ export {
|
|
|
835
1115
|
snapshot,
|
|
836
1116
|
roster,
|
|
837
1117
|
result,
|
|
1118
|
+
questions,
|
|
1119
|
+
messages,
|
|
838
1120
|
dispatch,
|
|
839
|
-
deriveSessionState
|
|
1121
|
+
deriveSessionState,
|
|
1122
|
+
createDispatchMessageId,
|
|
1123
|
+
answerQuestion
|
|
840
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
|
@@ -21,10 +21,18 @@ function formatDispatch(r) {
|
|
|
21
21
|
return `Replied to pending question in session ${r.sessionId} (${r.project}).`;
|
|
22
22
|
case "follow-up":
|
|
23
23
|
return `Follow-up prompt sent to session ${r.sessionId} (${r.project}).`;
|
|
24
|
+
case "blocked":
|
|
25
|
+
return `Blocked: session ${r.sessionId} (${r.project}) has a pending question (${r.requestId}) — no reply or prompt sent.`;
|
|
24
26
|
}
|
|
25
27
|
}
|
|
26
28
|
function dispatchMetadata(r) {
|
|
27
|
-
|
|
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
|
+
};
|
|
28
36
|
}
|
|
29
37
|
function formatStatus(r) {
|
|
30
38
|
const todoLines = r.todos.length ? r.todos.map((t) => ` - [${t.status}] ${t.content} (${t.priority})`).join(`
|