@fro.bot/space-bus 0.14.0 → 0.15.1
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 -2
- package/dist/attach.js +2 -2
- package/dist/cli.js +9 -9
- package/dist/config.js +14 -17
- package/dist/contract.js +30 -30
- package/dist/core.d.ts +36 -1
- package/dist/core.js +129 -38
- package/dist/format.d.ts +1 -0
- package/dist/format.js +13 -7
- package/dist/index.js +1615 -1555
- package/dist/mcp.js +1154 -1089
- package/dist/registry-entry.js +189 -190
- package/dist/server.js +18 -19
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -69,7 +69,7 @@ Set `SPACE_BUS_CONFIG` to override roster discovery — it must be an absolute p
|
|
|
69
69
|
- `bus_wait` — Block until any of the given sessions needs attention (completes, blocks on a question, fails, or is not found) or a bounded timeout elapses. Returns each watched session's normalized state and which session(s) woke the wait; on timeout returns the current snapshot, not an error.
|
|
70
70
|
- `bus_registry` — Manage the roster registry: list registered rosters, create/register/unregister rosters, set the default, select an active roster for this connector session (`use`, MCP only), and add/remove/update projects on a registered roster. `set-default` also becomes the MCP ambient-resolution fallback (see below) once `SPACE_BUS_CONFIG` is unset.
|
|
71
71
|
|
|
72
|
-
All
|
|
72
|
+
All `bus_*` tools except `bus_registry` accept an optional `roster` param (a registry name — see `bus_registry`) to target a roster other than the ambient one, and every tool result opens with a `roster: <name-or-path>` line naming the roster it resolved against, on both surfaces. Resolution precedence:
|
|
73
73
|
|
|
74
74
|
- **Plugin surface:** explicit `roster` param > workspace directory (`ctx.directory`).
|
|
75
75
|
- **MCP surface:** explicit `roster` param > connector-session active roster (`bus_registry use`) > `SPACE_BUS_CONFIG` (ambient) > registry default (`bus_registry set-default`), consulted only when `SPACE_BUS_CONFIG` is unset > an actionable error listing the available rosters.
|
|
@@ -136,7 +136,7 @@ Semantics:
|
|
|
136
136
|
|
|
137
137
|
## Library surface
|
|
138
138
|
|
|
139
|
-
Experimental subpath exports expose the bus's internals directly — the functions the
|
|
139
|
+
Experimental subpath exports expose the bus's internals directly — the functions the `bus_*` tools run on, plus the managed-server lifecycle and the attach resolver — for renderers and other consumers that want structured state instead of formatted strings. Subpaths are experimental: shapes may change in minor releases; pin the version if you adopt them.
|
|
140
140
|
|
|
141
141
|
- `@fro.bot/space-bus/core` — browser-safe; no Node builtins, no ambient env reads. Every exported function takes a `context: BusContext` (`{ roster, credentials? }`) that you build yourself and pass in — core never resolves it for you. Includes `snapshot()`, a one-call composite of roster + per-project status + pending questions with bounded fan-out. Session-content primitives:
|
|
142
142
|
- `messages(sessionId, { context, limit? })` — bounded full-message read. Resolves session ownership from the roster (never a caller-supplied directory), fetches `GET /session/:id/message?limit=N` (default 20, hard maximum 200 — `limit` must be a finite positive integer at or under the maximum; `0`, negative, fractional, `NaN`, `Infinity`, and over-maximum values are rejected before any fetch). Returns `{ sessionId, project, messages: [{ id?, role, createdAt?, parts: [{ type, text? }] }] }` in the order the server returns them — `id`/`createdAt` are the message's stable identity/ordering metadata (from the envelope's `info.id`/`info.time.created`), and only those explicit fields plus parsed `parts` cross the boundary; unknown envelope fields are dropped. No directory/credential fields in the result.
|
|
@@ -148,6 +148,9 @@ Experimental subpath exports expose the bus's internals directly — the functio
|
|
|
148
148
|
- `@fro.bot/space-bus/format` — the pure formatters the tools use to render output, for tool-identical text.
|
|
149
149
|
- `@fro.bot/space-bus/managed-server` — Node-only. The managed-server lifecycle (`ensureServer`/`serverStatus`/`stopServer`) for consumers driving spawn/attach/stop directly — see [Managed server](#managed-server).
|
|
150
150
|
- `@fro.bot/space-bus/attach` — browser-safe. `resolveManagedServer(workspaceDir, seams)` resolves a managed roster's running endpoint by reading the discovery file through injected filesystem/env/crypto seams — re-checking the localhost guard and probing liveness — so an external attacher such as a Tauri webview can attach without any `node:*` imports or reimplementing the discovery contract. Returns `{ baseUrl, credentials, alive }` or an actionable error.
|
|
151
|
+
- `@fro.bot/space-bus/registry` — Node-only, experimental. The roster registry and roster-edit substrate behind `bus_registry` (`readRegistry`/`registerRoster`/`unregisterRoster`/`setDefaultRoster`/`resolveRosterByName`, plus roster creation and project add/remove/update), for consumers that want to manage rosters programmatically without the tool or MCP surfaces.
|
|
152
|
+
|
|
153
|
+
`@fro.bot/space-bus/server` is not a library entry: OpenCode's plugin loader reserves that subpath for the plugin itself, so it resolves to the plugin entry. Use `/managed-server` for the lifecycle API.
|
|
151
154
|
|
|
152
155
|
Node example (`/config` + `/core`):
|
|
153
156
|
|
package/dist/attach.js
CHANGED
package/dist/cli.js
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
4
4
|
|
|
5
|
+
// src/cli.ts
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
|
|
5
8
|
// src/config.ts
|
|
6
9
|
import { existsSync, readFileSync as readFileSync3, realpathSync as realpathSync2 } from "node:fs";
|
|
7
10
|
import { homedir as homedir3 } from "node:os";
|
|
@@ -718,7 +721,7 @@ function registerRoster(name, rosterPath) {
|
|
|
718
721
|
if (!readResult.ok)
|
|
719
722
|
return readResult;
|
|
720
723
|
const registry = readResult.registry;
|
|
721
|
-
const collision = registry.rosters.find((
|
|
724
|
+
const collision = registry.rosters.find((entry) => entry.name.toLowerCase() === name.toLowerCase());
|
|
722
725
|
if (collision) {
|
|
723
726
|
return {
|
|
724
727
|
ok: false,
|
|
@@ -1059,8 +1062,8 @@ async function waitForDiscoveryOrFail(rosterPath, budgetMs) {
|
|
|
1059
1062
|
const handle = attachServer(rosterPath);
|
|
1060
1063
|
if (handle)
|
|
1061
1064
|
return handle;
|
|
1062
|
-
const
|
|
1063
|
-
const winnerAlive =
|
|
1065
|
+
const lockInfo = readLockFile(lockPath);
|
|
1066
|
+
const winnerAlive = lockInfo !== null && verifyIdentity(lockInfo.pid, lockInfo.startTime);
|
|
1064
1067
|
if (!winnerAlive) {
|
|
1065
1068
|
throw new Error(`space-bus: the process starting the managed server for roster ${rosterPath} failed before it could finish (lock released without a discovery file) — check its log for details`);
|
|
1066
1069
|
}
|
|
@@ -1452,9 +1455,6 @@ async function waitForGroupDeath(pid, budgetMs) {
|
|
|
1452
1455
|
return !groupAlive();
|
|
1453
1456
|
}
|
|
1454
1457
|
|
|
1455
|
-
// src/cli.ts
|
|
1456
|
-
import { fileURLToPath } from "node:url";
|
|
1457
|
-
|
|
1458
1458
|
// src/service.ts
|
|
1459
1459
|
import {
|
|
1460
1460
|
chmodSync as chmodSync3,
|
|
@@ -2171,7 +2171,7 @@ function printJson(json, data, plain) {
|
|
|
2171
2171
|
async function runServe(args, deps = {}) {
|
|
2172
2172
|
const doEnsureServer = deps.ensureServer ?? ensureServer;
|
|
2173
2173
|
const doStopServer = deps.stopServer ?? stopServer;
|
|
2174
|
-
const doSuperviseServer = deps.superviseServer ?? ((
|
|
2174
|
+
const doSuperviseServer = deps.superviseServer ?? ((rosterPath, handle, shouldStop, interrupt) => superviseServer(rosterPath, handle, { shouldStop, interrupt }));
|
|
2175
2175
|
const rosterPath = resolveRoster(args.config);
|
|
2176
2176
|
const handle = await doEnsureServer(rosterPath);
|
|
2177
2177
|
const data = {
|
|
@@ -2387,6 +2387,6 @@ if (__require.main == __require.module) {
|
|
|
2387
2387
|
});
|
|
2388
2388
|
}
|
|
2389
2389
|
export {
|
|
2390
|
-
|
|
2391
|
-
|
|
2390
|
+
runServe,
|
|
2391
|
+
runService
|
|
2392
2392
|
};
|
package/dist/config.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { createRequire } from "node:module";
|
|
2
|
-
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
3
|
-
|
|
4
1
|
// src/config.ts
|
|
5
2
|
import { existsSync, readFileSync as readFileSync3, realpathSync as realpathSync2 } from "node:fs";
|
|
6
3
|
import { homedir as homedir3 } from "node:os";
|
|
@@ -717,7 +714,7 @@ function registerRoster(name, rosterPath) {
|
|
|
717
714
|
if (!readResult.ok)
|
|
718
715
|
return readResult;
|
|
719
716
|
const registry = readResult.registry;
|
|
720
|
-
const collision = registry.rosters.find((
|
|
717
|
+
const collision = registry.rosters.find((entry) => entry.name.toLowerCase() === name.toLowerCase());
|
|
721
718
|
if (collision) {
|
|
722
719
|
return {
|
|
723
720
|
ok: false,
|
|
@@ -964,18 +961,18 @@ function isManagedRoster(directory) {
|
|
|
964
961
|
return manifest.server.managed !== undefined;
|
|
965
962
|
}
|
|
966
963
|
export {
|
|
967
|
-
|
|
968
|
-
resolveRosterPath,
|
|
969
|
-
manifestSchema,
|
|
970
|
-
managedServerConfigSchema,
|
|
971
|
-
loadContextForRosterPath,
|
|
972
|
-
loadContextForRoster,
|
|
973
|
-
loadContext,
|
|
974
|
-
isManagedRosterAtPath,
|
|
975
|
-
isManagedRoster,
|
|
976
|
-
getRosterAtPath,
|
|
977
|
-
getRoster,
|
|
978
|
-
getProjects,
|
|
964
|
+
expandHome,
|
|
979
965
|
getCredentials,
|
|
980
|
-
|
|
966
|
+
getProjects,
|
|
967
|
+
getRoster,
|
|
968
|
+
getRosterAtPath,
|
|
969
|
+
isManagedRoster,
|
|
970
|
+
isManagedRosterAtPath,
|
|
971
|
+
loadContext,
|
|
972
|
+
loadContextForRoster,
|
|
973
|
+
loadContextForRosterPath,
|
|
974
|
+
managedServerConfigSchema,
|
|
975
|
+
manifestSchema,
|
|
976
|
+
resolveRosterPath,
|
|
977
|
+
serverConfigSchema
|
|
981
978
|
};
|
package/dist/contract.js
CHANGED
|
@@ -168,35 +168,35 @@ var discoveryFileSchema = z.object({
|
|
|
168
168
|
rosterPath: z.string().optional()
|
|
169
169
|
});
|
|
170
170
|
export {
|
|
171
|
-
|
|
172
|
-
vcsStatusEntrySchema,
|
|
173
|
-
turnMessageSchema,
|
|
174
|
-
turnMessageListSchema,
|
|
175
|
-
turnDiffEntrySchema,
|
|
176
|
-
todoSchema,
|
|
177
|
-
sessionSummarySchema,
|
|
178
|
-
sessionStatusMapSchema,
|
|
179
|
-
sessionStateSchema,
|
|
180
|
-
sessionStateInfoSchema,
|
|
181
|
-
sessionSchema,
|
|
182
|
-
sessionListSchema,
|
|
183
|
-
rosterSchema,
|
|
184
|
-
registryFileSchema,
|
|
185
|
-
registryEntrySchema,
|
|
186
|
-
questionListSchema,
|
|
187
|
-
questionEntrySchema,
|
|
188
|
-
projectSchema,
|
|
189
|
-
pendingQuestionListSchema,
|
|
190
|
-
pendingQuestionEntrySchema,
|
|
191
|
-
messagePartSchema,
|
|
192
|
-
messageListSchema,
|
|
193
|
-
messageEnvelopeSchema,
|
|
194
|
-
managedSpawnConfigSchema,
|
|
195
|
-
loopbackOk,
|
|
196
|
-
discoveryFileSchema,
|
|
197
|
-
diffSchema,
|
|
198
|
-
diffEntrySchema,
|
|
199
|
-
credentialsSchema,
|
|
171
|
+
LOOPBACK_HOSTS,
|
|
200
172
|
busContextSchema,
|
|
201
|
-
|
|
173
|
+
credentialsSchema,
|
|
174
|
+
diffEntrySchema,
|
|
175
|
+
diffSchema,
|
|
176
|
+
discoveryFileSchema,
|
|
177
|
+
loopbackOk,
|
|
178
|
+
managedSpawnConfigSchema,
|
|
179
|
+
messageEnvelopeSchema,
|
|
180
|
+
messageListSchema,
|
|
181
|
+
messagePartSchema,
|
|
182
|
+
pendingQuestionEntrySchema,
|
|
183
|
+
pendingQuestionListSchema,
|
|
184
|
+
projectSchema,
|
|
185
|
+
questionEntrySchema,
|
|
186
|
+
questionListSchema,
|
|
187
|
+
registryEntrySchema,
|
|
188
|
+
registryFileSchema,
|
|
189
|
+
rosterSchema,
|
|
190
|
+
sessionListSchema,
|
|
191
|
+
sessionSchema,
|
|
192
|
+
sessionStateInfoSchema,
|
|
193
|
+
sessionStateSchema,
|
|
194
|
+
sessionStatusMapSchema,
|
|
195
|
+
sessionSummarySchema,
|
|
196
|
+
todoSchema,
|
|
197
|
+
turnDiffEntrySchema,
|
|
198
|
+
turnMessageListSchema,
|
|
199
|
+
turnMessageSchema,
|
|
200
|
+
vcsStatusEntrySchema,
|
|
201
|
+
vcsStatusSchema
|
|
202
202
|
};
|
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];
|
|
395
443
|
}
|
|
396
|
-
return
|
|
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 };
|
|
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);
|
|
@@ -1019,15 +1109,16 @@ async function answerQuestion(args, opts) {
|
|
|
1019
1109
|
return { ok: true, sessionId: args.sessionId, requestId: args.requestId };
|
|
1020
1110
|
}
|
|
1021
1111
|
export {
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
status,
|
|
1025
|
-
snapshot,
|
|
1026
|
-
roster,
|
|
1027
|
-
result,
|
|
1028
|
-
questions,
|
|
1029
|
-
messages,
|
|
1030
|
-
dispatch,
|
|
1112
|
+
answerQuestion,
|
|
1113
|
+
createDispatchMessageId,
|
|
1031
1114
|
deriveSessionState,
|
|
1032
|
-
|
|
1115
|
+
dispatch,
|
|
1116
|
+
messages,
|
|
1117
|
+
questions,
|
|
1118
|
+
result,
|
|
1119
|
+
roster,
|
|
1120
|
+
snapshot,
|
|
1121
|
+
status,
|
|
1122
|
+
toDispatchArgs,
|
|
1123
|
+
wait
|
|
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(`
|
|
@@ -79,11 +85,11 @@ function formatResult(r) {
|
|
|
79
85
|
`);
|
|
80
86
|
}
|
|
81
87
|
export {
|
|
82
|
-
|
|
83
|
-
formatStatus,
|
|
84
|
-
formatRosterHeader,
|
|
85
|
-
formatRoster,
|
|
86
|
-
formatResult,
|
|
88
|
+
dispatchMetadata,
|
|
87
89
|
formatDispatch,
|
|
88
|
-
|
|
90
|
+
formatResult,
|
|
91
|
+
formatRoster,
|
|
92
|
+
formatRosterHeader,
|
|
93
|
+
formatStatus,
|
|
94
|
+
formatWait
|
|
89
95
|
};
|