@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/dist/index.js CHANGED
@@ -72,7 +72,11 @@ var messagePartSchema = z.looseObject({
72
72
  text: z.string().optional()
73
73
  });
74
74
  var messageEnvelopeSchema = z.looseObject({
75
- info: z.looseObject({ role: z.string() }),
75
+ info: z.looseObject({
76
+ id: z.string().optional(),
77
+ role: z.string(),
78
+ time: z.looseObject({ created: z.number().optional() }).optional()
79
+ }),
76
80
  parts: z.array(messagePartSchema)
77
81
  });
78
82
  var messageListSchema = z.array(messageEnvelopeSchema);
@@ -81,7 +85,13 @@ var pendingQuestionEntrySchema = z.looseObject({
81
85
  sessionID: z.string(),
82
86
  questions: z.array(z.looseObject({
83
87
  question: z.string().optional(),
84
- options: z.array(z.looseObject({ label: z.string().optional() })).optional()
88
+ header: z.string().optional(),
89
+ multiple: z.boolean().optional(),
90
+ custom: z.boolean().optional(),
91
+ options: z.array(z.looseObject({
92
+ label: z.string().optional(),
93
+ description: z.string().optional()
94
+ })).optional()
85
95
  })).optional()
86
96
  });
87
97
  var pendingQuestionListSchema = z.array(pendingQuestionEntrySchema);
@@ -842,17 +852,23 @@ var manifestSchema = z3.object({
842
852
  function expandHome(path) {
843
853
  return path.startsWith("~") ? join3(homedir3(), path.slice(1)) : path;
844
854
  }
845
- function resolveRosterPath(directory) {
855
+ function resolveExplicitOverride(override, sourceLabel) {
856
+ if (override.includes("://")) {
857
+ throw new Error(`space-bus: ${sourceLabel} must be an absolute path or start with ~ (got a URL: ${override})`);
858
+ }
859
+ const expanded = expandHome(override);
860
+ if (!expanded.startsWith("/")) {
861
+ throw new Error(`space-bus: ${sourceLabel} must be an absolute path or start with ~ (got: ${override})`);
862
+ }
863
+ return existsSync(expanded) ? realpathSync2(expanded) : expanded;
864
+ }
865
+ function resolveRosterPath(directory, explicitOverride) {
866
+ if (explicitOverride) {
867
+ return resolveExplicitOverride(explicitOverride, "--config");
868
+ }
846
869
  const override = process.env["SPACE_BUS_CONFIG"];
847
870
  if (override) {
848
- if (override.includes("://")) {
849
- throw new Error(`space-bus: SPACE_BUS_CONFIG must be an absolute path or start with ~ (got a URL: ${override})`);
850
- }
851
- const expanded = expandHome(override);
852
- if (!expanded.startsWith("/")) {
853
- throw new Error(`space-bus: SPACE_BUS_CONFIG must be an absolute path or start with ~ (got: ${override})`);
854
- }
855
- return existsSync(expanded) ? realpathSync2(expanded) : expanded;
871
+ return resolveExplicitOverride(override, "SPACE_BUS_CONFIG");
856
872
  }
857
873
  if (directory) {
858
874
  const candidate = join3(directory, "spacebus.json");
@@ -1463,10 +1479,18 @@ function formatDispatch(r) {
1463
1479
  return `Replied to pending question in session ${r.sessionId} (${r.project}).`;
1464
1480
  case "follow-up":
1465
1481
  return `Follow-up prompt sent to session ${r.sessionId} (${r.project}).`;
1482
+ case "blocked":
1483
+ return `Blocked: session ${r.sessionId} (${r.project}) has a pending question (${r.requestId}) — no reply or prompt sent.`;
1466
1484
  }
1467
1485
  }
1468
1486
  function dispatchMetadata(r) {
1469
- return { sessionId: r.sessionId, project: r.project, mode: r.mode };
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
+ };
1470
1494
  }
1471
1495
  function formatStatus(r) {
1472
1496
  const todoLines = r.todos.length ? r.todos.map((t) => ` - [${t.status}] ${t.content} (${t.priority})`).join(`
@@ -2086,6 +2110,15 @@ function findProject(projects, name) {
2086
2110
  function err(error) {
2087
2111
  return { ok: false, error };
2088
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
+ }
2089
2122
  function toBase64(s) {
2090
2123
  const bytes = new TextEncoder().encode(s);
2091
2124
  let bin = "";
@@ -2285,7 +2318,7 @@ async function roster(opts) {
2285
2318
  }));
2286
2319
  return { ok: true, projects: results };
2287
2320
  }
2288
- async function dispatchNew(baseUrl, credentials, projects, project, prompt, title) {
2321
+ async function dispatchNew(baseUrl, credentials, projects, project, prompt, title, messageId) {
2289
2322
  const resolved = resolveProjectOrErr(projects, project);
2290
2323
  if (!resolved.ok)
2291
2324
  return resolved;
@@ -2296,27 +2329,54 @@ async function dispatchNew(baseUrl, credentials, projects, project, prompt, titl
2296
2329
  body: JSON.stringify({ title: sessionTitle })
2297
2330
  });
2298
2331
  if (!createRes.res.ok) {
2299
- return err(`space-bus: failed to create session in "${project}" (${createRes.res.status}): ${createRes.bodyText}`);
2332
+ return dispatchErr(`space-bus: failed to create session in "${project}" (${createRes.res.status}): ${createRes.bodyText}`, { phase: "indeterminate", project, messageId });
2300
2333
  }
2301
2334
  let session;
2302
2335
  try {
2303
2336
  session = sessionSchema.parse(JSON.parse(createRes.bodyText));
2304
2337
  } catch (e) {
2305
- return err(`space-bus: unexpected /session response shape: ${e.message}`);
2338
+ return dispatchErr(`space-bus: unexpected /session response shape: ${e.message}`, { phase: "indeterminate", project, messageId });
2306
2339
  }
2307
2340
  const promptRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(session.id)}/prompt_async`, {
2308
2341
  method: "POST",
2309
- body: JSON.stringify({ parts: [{ type: "text", text: prompt }] })
2342
+ body: JSON.stringify({
2343
+ parts: [{ type: "text", text: prompt }],
2344
+ ...messageId !== undefined ? { messageID: messageId } : {}
2345
+ })
2310
2346
  });
2311
2347
  if (promptRes.res.status !== 204) {
2312
- return err(`space-bus: dispatch to "${project}" failed sending prompt (${promptRes.res.status}): ${promptRes.bodyText}`);
2348
+ return dispatchErr(`space-bus: dispatch to "${project}" failed sending prompt (${promptRes.res.status}): ${promptRes.bodyText}`, { phase: "indeterminate", project, sessionId: session.id, messageId });
2313
2349
  }
2314
- return { ok: true, sessionId: session.id, project, directory };
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);
2363
+ }
2364
+ return { ok: true, messageId };
2315
2365
  }
2316
2366
  function toDispatchArgs(input) {
2367
+ if (input.onPendingQuestion !== undefined && input.onPendingQuestion !== "question-reply" && input.onPendingQuestion !== "blocked") {
2368
+ return err(`space-bus: onPendingQuestion must be "question-reply" or "blocked", got ${JSON.stringify(input.onPendingQuestion)}`);
2369
+ }
2317
2370
  if (input.sessionId !== undefined && input.sessionId === "") {
2318
2371
  return err("space-bus: sessionId must be a non-empty string");
2319
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
+ }
2320
2380
  if (!input.sessionId) {
2321
2381
  if (!input.project) {
2322
2382
  return err("space-bus: project is required when starting a new session");
@@ -2325,7 +2385,9 @@ function toDispatchArgs(input) {
2325
2385
  ok: true,
2326
2386
  prompt: input.prompt,
2327
2387
  title: input.title,
2328
- project: input.project
2388
+ project: input.project,
2389
+ onPendingQuestion: input.onPendingQuestion,
2390
+ ...messageId !== undefined ? { messageId } : {}
2329
2391
  };
2330
2392
  }
2331
2393
  return {
@@ -2333,7 +2395,22 @@ function toDispatchArgs(input) {
2333
2395
  prompt: input.prompt,
2334
2396
  title: input.title,
2335
2397
  sessionId: input.sessionId,
2336
- project: input.project
2398
+ project: input.project,
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 } : {}
2337
2414
  };
2338
2415
  }
2339
2416
  async function dispatch(args, opts) {
@@ -2341,6 +2418,11 @@ async function dispatch(args, opts) {
2341
2418
  if (!ctx.ok)
2342
2419
  return ctx;
2343
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
+ }
2344
2426
  if (args.sessionId !== undefined && args.sessionId === "") {
2345
2427
  return err("space-bus: sessionId must be a non-empty string");
2346
2428
  }
@@ -2348,16 +2430,10 @@ async function dispatch(args, opts) {
2348
2430
  if (!args.project) {
2349
2431
  return err("space-bus: project is required when starting a new session");
2350
2432
  }
2351
- const r = await dispatchNew(baseUrl, credentials, projects, args.project, args.prompt, args.title);
2352
- if (!r.ok)
2353
- return r;
2354
- return {
2355
- ok: true,
2356
- sessionId: r.sessionId,
2357
- project: r.project,
2358
- mode: "new",
2359
- directory: r.directory
2360
- };
2433
+ return dispatchNewBranch(baseUrl, credentials, projects, {
2434
+ ...args,
2435
+ project: args.project
2436
+ });
2361
2437
  }
2362
2438
  const loc = await findSessionDirectory(baseUrl, credentials, projects, args.sessionId);
2363
2439
  if (!loc.ok)
@@ -2366,7 +2442,7 @@ async function dispatch(args, opts) {
2366
2442
  if (args.project && args.project !== project) {
2367
2443
  return err(`space-bus: session ${args.sessionId} belongs to project "${project}", not "${args.project}" — refusing to steer the wrong session`);
2368
2444
  }
2369
- return steerSession(baseUrl, credentials, args.sessionId, args.prompt, directory, project);
2445
+ return steerSession(baseUrl, credentials, args.sessionId, args.prompt, directory, project, args.onPendingQuestion ?? "question-reply", args.messageId);
2370
2446
  }
2371
2447
  async function findSessionDirectory(baseUrl, credentials, projects, sessionId) {
2372
2448
  for (const p of projects) {
@@ -2471,35 +2547,69 @@ async function status(sessionId, opts) {
2471
2547
  resultAvailable: state === "complete"
2472
2548
  };
2473
2549
  }
2474
- async function steerSession(baseUrl, credentials, sessionId, message, directory, project) {
2550
+ async function resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, project, onPendingQuestion) {
2475
2551
  const questionsRes = await api(baseUrl, credentials, directory, "/question");
2476
- if (questionsRes.res.ok) {
2477
- let questions;
2478
- try {
2479
- questions = questionListSchema.parse(JSON.parse(questionsRes.bodyText));
2480
- } catch {
2481
- questions = [];
2552
+ if (!questionsRes.res.ok) {
2553
+ if (onPendingQuestion === "blocked") {
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 });
2482
2555
  }
2483
- const pending = questions.find((q) => q.sessionID === sessionId);
2484
- if (pending) {
2485
- const replyRes = await api(baseUrl, credentials, directory, `/question/${encodeURIComponent(pending.id)}/reply`, {
2486
- method: "POST",
2487
- body: JSON.stringify({ answers: [[message]] })
2488
- });
2489
- if (!replyRes.res.ok) {
2490
- return err(`space-bus: failed to reply to question ${pending.id} for session ${sessionId} (${replyRes.res.status}): ${replyRes.bodyText}`);
2491
- }
2492
- return { ok: true, sessionId, project, mode: "question-reply" };
2556
+ return { ok: true, pending: undefined };
2557
+ }
2558
+ let pendingList;
2559
+ try {
2560
+ pendingList = questionListSchema.parse(JSON.parse(questionsRes.bodyText));
2561
+ } catch (e) {
2562
+ if (onPendingQuestion === "blocked") {
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 });
2493
2564
  }
2565
+ return { ok: true, pending: undefined };
2566
+ }
2567
+ return {
2568
+ ok: true,
2569
+ pending: pendingList.find((q) => q.sessionID === sessionId)
2570
+ };
2571
+ }
2572
+ async function steerSession(baseUrl, credentials, sessionId, message, directory, project, onPendingQuestion, messageId) {
2573
+ const pendingRes = await resolvePendingQuestionForSteer(baseUrl, credentials, directory, sessionId, project, onPendingQuestion);
2574
+ if (!pendingRes.ok)
2575
+ return pendingRes;
2576
+ const { pending } = pendingRes;
2577
+ if (pending && onPendingQuestion === "blocked") {
2578
+ return {
2579
+ ok: true,
2580
+ sessionId,
2581
+ project,
2582
+ mode: "blocked",
2583
+ requestId: pending.id
2584
+ };
2585
+ }
2586
+ if (pending) {
2587
+ const replyRes = await api(baseUrl, credentials, directory, `/question/${encodeURIComponent(pending.id)}/reply`, {
2588
+ method: "POST",
2589
+ body: JSON.stringify({ answers: [[message]] })
2590
+ });
2591
+ if (!replyRes.res.ok) {
2592
+ return dispatchErr(`space-bus: failed to reply to question ${pending.id} for session ${sessionId} (${replyRes.res.status}): ${replyRes.bodyText}`, { phase: "indeterminate", project, sessionId });
2593
+ }
2594
+ return { ok: true, sessionId, project, mode: "question-reply" };
2494
2595
  }
2495
2596
  const promptRes = await api(baseUrl, credentials, directory, `/session/${encodeURIComponent(sessionId)}/prompt_async`, {
2496
2597
  method: "POST",
2497
- body: JSON.stringify({ parts: [{ type: "text", text: message }] })
2598
+ body: JSON.stringify({
2599
+ parts: [{ type: "text", text: message }],
2600
+ ...messageId !== undefined ? { messageID: messageId } : {}
2601
+ })
2498
2602
  });
2499
2603
  if (promptRes.res.status !== 204) {
2500
- return err(`space-bus: follow-up prompt to session ${sessionId} failed (${promptRes.res.status}): ${promptRes.bodyText}`);
2604
+ return dispatchErr(`space-bus: follow-up prompt to session ${sessionId} failed (${promptRes.res.status}): ${promptRes.bodyText}`, { phase: "indeterminate", project, sessionId, messageId });
2501
2605
  }
2502
- return { ok: true, sessionId, project, mode: "follow-up" };
2606
+ return {
2607
+ ok: true,
2608
+ sessionId,
2609
+ project,
2610
+ mode: "follow-up",
2611
+ ...messageId !== undefined ? { messageId } : {}
2612
+ };
2503
2613
  }
2504
2614
  async function result(sessionId, opts) {
2505
2615
  const ctx = validateContext(opts.context);
@@ -2789,6 +2899,7 @@ function makeBusTask(defaultDirectory) {
2789
2899
  prompt: tool5.schema.string().describe("The prompt to send to the delegated agent"),
2790
2900
  title: tool5.schema.string().optional().describe("Optional session title (only used when starting a new session)"),
2791
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."),
2792
2903
  roster: tool5.schema.string().optional().describe("Registry roster name to target. Resolution precedence: this param > workspace directory (see bus_registry to list)")
2793
2904
  },
2794
2905
  async execute(args, ctx) {