@fro.bot/space-bus 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  [![npm version](https://img.shields.io/npm/v/@fro.bot/space-bus?style=for-the-badge&labelColor=0D0216&color=00BCD4)](https://www.npmjs.com/package/@fro.bot/space-bus) [![Build Status](https://img.shields.io/github/actions/workflow/status/fro-bot/space-bus/ci.yaml?style=for-the-badge&label=Build&labelColor=0D0216&color=00BCD4)](https://github.com/fro-bot/space-bus/actions) [![License](https://img.shields.io/badge/License-MIT-FFC107?style=for-the-badge&labelColor=0D0216&color=FFC107)](LICENSE)
10
10
 
11
- [What it is](#what-it-is) · [Install](#install) · [Configure](#configure) · [Tools](#tools) · [Claude Desktop](#claude-desktop) · [Development](#development)
11
+ [What it is](#what-it-is) · [Install](#install) · [Configure](#configure) · [Tools](#tools) · [Library surface](#library-surface) · [Claude Desktop](#claude-desktop) · [Development](#development)
12
12
 
13
13
  </div>
14
14
 
@@ -63,10 +63,36 @@ Set `SPACE_BUS_CONFIG` to override roster discovery — it must be an absolute p
63
63
  ## Tools
64
64
 
65
65
  - `bus_roster` — List the space-bus manifest projects with live session status per project.
66
- - `bus_task` — Dispatch a prompt to an agent in the given space-bus manifest project, or steer an existing session by passing `sessionId` (answers its pending question, else sends a follow-up prompt). Returns immediately; does not wait for completion.
66
+ - `bus_task` — Dispatch a prompt to an agent in the given space-bus manifest project, or steer an existing session by passing `sessionId` (answers its pending question, else sends a follow-up prompt). Returns immediately; does not wait for completion. Alongside the formatted text, results carry structured `{sessionId, project, mode}` metadata — plugin `ToolResult.metadata` on the tool surface, MCP `structuredContent` on the stdio surface — for callers that want the dispatch outcome without parsing the string.
67
67
  - `bus_status` — Report a space-bus session's status plus a summary of its latest todo and diff. Also reports when the session is blocked on an interactive question awaiting a reply.
68
68
  - `bus_result` — Return a completed space-bus session's final assistant message and diff. Errors if the session is still running — use `bus_status` to check first.
69
69
 
70
+ ## Library surface
71
+
72
+ Experimental subpath exports expose the bus's semantics directly — the same functions the four tools run on — 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.
73
+
74
+ - `@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.
75
+ - `@fro.bot/space-bus/config` — Node-only. `loadContext(directory?)` reads `spacebus.json` (honoring `SPACE_BUS_CONFIG` the same as the plugin) and returns a ready-to-use `BusContext`, with per-project `exists` flags and env-derived credentials attached. Build a fresh context per call; it's per-call/short-lived by contract, not meant to be cached across filesystem changes.
76
+ - `@fro.bot/space-bus/contract` — the zod schemas (and inferred types) behind the OpenCode API and `BusContext`, for consumers hitting the server directly and wanting the same shapes.
77
+ - `@fro.bot/space-bus/format` — the pure formatters the tools use to render output, for tool-identical text.
78
+
79
+ Node example (`/config` + `/core`):
80
+
81
+ ```ts
82
+ import { loadContext } from "@fro.bot/space-bus/config";
83
+ import { snapshot } from "@fro.bot/space-bus/core";
84
+
85
+ const context = loadContext(); // resolves spacebus.json from SPACE_BUS_CONFIG or a directory you pass — no cwd fallback
86
+ const result = await snapshot({ context });
87
+ if (result.ok) {
88
+ for (const project of result.projects) {
89
+ console.log(project.name, project.exists);
90
+ }
91
+ }
92
+ ```
93
+
94
+ Browser consumers: `/core`, `/contract`, and `/format` are browser-safe (their module graphs are bundle-tested in CI to exclude `node:*` imports and never reach `/config`). `/config` is Node-only — browser code can't call `loadContext`, so credentials must be injected explicitly by whatever server-side process builds the `BusContext` (never read ambiently by core). The localhost guard travels with the context: it's re-checked at core's single validation gate on every call, so a context built from a non-local `baseUrl` is rejected there, not just at config's load time.
95
+
70
96
  ## Claude Desktop
71
97
 
72
98
  ```json
package/dist/config.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { z } from "zod";
2
+ import type { BusContext } from "./contract";
2
3
  export declare const manifestSchema: z.ZodObject<{
3
4
  server: z.ZodObject<{
4
5
  baseUrl: z.ZodString;
@@ -60,5 +61,23 @@ export declare function resolveRosterPath(directory?: string): string;
60
61
  export declare function getRoster(directory?: string): Manifest;
61
62
  export type Project = Manifest["projects"][number] & {
62
63
  expandedPath: string;
64
+ exists: boolean;
63
65
  };
64
66
  export declare function getProjects(manifest: Manifest): Project[];
67
+ /**
68
+ * Reads env-derived credentials for the Node path. Core never reads
69
+ * `process.env` directly — this is the one place that boundary crosses.
70
+ */
71
+ export declare function getCredentials(): {
72
+ username?: string;
73
+ password?: string;
74
+ };
75
+ /**
76
+ * Node-side loader producing a `BusContext` (see contract.ts) for a given
77
+ * workspace directory: roster with `exists`-flagged projects, plus
78
+ * env-derived credentials. Per-call/short-lived by contract — build a fresh
79
+ * one per call, never cache across filesystem changes. Throws on
80
+ * missing/invalid roster (same as `getRoster`); callers convert to a
81
+ * discriminated-union Result at the core boundary.
82
+ */
83
+ export declare function loadContext(directory?: string): BusContext;
package/dist/config.js ADDED
@@ -0,0 +1,90 @@
1
+ // src/config.ts
2
+ import { existsSync, readFileSync, realpathSync } from "node:fs";
3
+ import { homedir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { z } from "zod";
6
+ var manifestSchema = z.object({
7
+ server: z.object({ baseUrl: z.string().url() }),
8
+ projects: z.array(z.object({
9
+ name: z.string(),
10
+ path: z.string(),
11
+ description: z.string()
12
+ }))
13
+ });
14
+ function expandHome(path) {
15
+ return path.startsWith("~") ? join(homedir(), path.slice(1)) : path;
16
+ }
17
+ function resolveRosterPath(directory) {
18
+ const override = process.env["SPACE_BUS_CONFIG"];
19
+ if (override) {
20
+ if (override.includes("://")) {
21
+ throw new Error(`space-bus: SPACE_BUS_CONFIG must be an absolute path or start with ~ (got a URL: ${override})`);
22
+ }
23
+ const expanded = expandHome(override);
24
+ if (!expanded.startsWith("/")) {
25
+ throw new Error(`space-bus: SPACE_BUS_CONFIG must be an absolute path or start with ~ (got: ${override})`);
26
+ }
27
+ return existsSync(expanded) ? realpathSync(expanded) : expanded;
28
+ }
29
+ if (directory) {
30
+ const candidate = join(directory, "spacebus.json");
31
+ return existsSync(candidate) ? realpathSync(candidate) : candidate;
32
+ }
33
+ throw new Error("space-bus: cannot locate spacebus.json — pass a directory containing spacebus.json, or set SPACE_BUS_CONFIG to an absolute path (or ~-prefixed path) to the roster file");
34
+ }
35
+ function getRoster(directory) {
36
+ const manifestPath = resolveRosterPath(directory);
37
+ let raw;
38
+ try {
39
+ raw = readFileSync(manifestPath, "utf8");
40
+ } catch (err) {
41
+ throw new Error(`space-bus: cannot read roster at ${manifestPath}: ${err.message} (set SPACE_BUS_CONFIG or ensure spacebus.json exists in the workspace directory)`);
42
+ }
43
+ let json;
44
+ try {
45
+ json = JSON.parse(raw);
46
+ } catch (err) {
47
+ throw new Error(`space-bus: roster at ${manifestPath} is not valid JSON: ${err.message}`);
48
+ }
49
+ const parsed = manifestSchema.safeParse(json);
50
+ if (!parsed.success) {
51
+ throw new Error(`space-bus: roster at ${manifestPath} failed schema validation: ${parsed.error.message}`);
52
+ }
53
+ const url = new URL(parsed.data.server.baseUrl);
54
+ const hostname = url.hostname;
55
+ const allowedHosts = new Set(["127.0.0.1", "::1", "[::1]", "localhost"]);
56
+ if (!allowedHosts.has(hostname)) {
57
+ throw new Error(`space-bus: spacebus.json baseUrl must point to localhost (got ${hostname}) — refusing to send credentials off-machine`);
58
+ }
59
+ return parsed.data;
60
+ }
61
+ function getProjects(manifest) {
62
+ return manifest.projects.map((p) => {
63
+ const expandedPath = expandHome(p.path);
64
+ return { ...p, expandedPath, exists: existsSync(expandedPath) };
65
+ });
66
+ }
67
+ function getCredentials() {
68
+ const password = process.env["OPENCODE_SERVER_PASSWORD"];
69
+ if (!password)
70
+ return {};
71
+ const username = process.env["OPENCODE_SERVER_USERNAME"] ?? "opencode";
72
+ return { username, password };
73
+ }
74
+ function loadContext(directory) {
75
+ const manifest = getRoster(directory);
76
+ const projects = getProjects(manifest);
77
+ return {
78
+ roster: { server: manifest.server, projects },
79
+ credentials: getCredentials()
80
+ };
81
+ }
82
+ export {
83
+ resolveRosterPath,
84
+ manifestSchema,
85
+ loadContext,
86
+ getRoster,
87
+ getProjects,
88
+ getCredentials,
89
+ expandHome
90
+ };