@johnhenry/browsermesh-core 0.1.1 → 0.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnhenry/browsermesh-core",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "Identity, crypto, peer management, and trust primitives for BrowserMesh",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
@@ -20,7 +20,13 @@
20
20
  "directory": "packages/browsermesh-core"
21
21
  },
22
22
  "peerDependencies": {
23
- "@johnhenry/browsermesh-primitives": ">=0.0.1 <1.0.0"
23
+ "@johnhenry/browsermesh-primitives": ">=0.0.1 <1.0.0",
24
+ "@johnhenry/browsermesh-apps": ">=0.4.0 <1.0.0"
25
+ },
26
+ "peerDependenciesMeta": {
27
+ "@johnhenry/browsermesh-apps": {
28
+ "optional": true
29
+ }
24
30
  },
25
31
  "scripts": {
26
32
  "test": "node --import ./test/_setup-globals.mjs --test test/*.test.mjs"
package/src/compat.mjs CHANGED
@@ -8,6 +8,12 @@
8
8
  * assign `this.name = ...` in the constructor, which threw
9
9
  * "Cannot set property name of #<Tool> which has only a getter" for
10
10
  * every real subclass, since none of them define a setter.
11
+ *
12
+ * Also exports BrowserToolRegistry, BrowserTool's counterpart: a
13
+ * name-keyed collection of constructed tool instances used to dispatch
14
+ * LLM-requested tool calls (see `registerIdentityTools`/
15
+ * `registerMeshPeerTools` in this package, and `registerMeshTools` in
16
+ * browsermesh-apps, all of which call `registry.register(...)`).
11
17
  */
12
18
  export class BrowserTool {
13
19
  /** @returns {object} ToolSpec-compatible object */
@@ -27,3 +33,94 @@ export class BrowserTool {
27
33
 
28
34
  async execute(_params) { throw new Error('Not implemented'); }
29
35
  }
36
+
37
+ /**
38
+ * Returns true if `tool` looks like a BrowserTool: it exposes a working
39
+ * `.spec` getter (with a non-empty string `.name`) and an `.execute`
40
+ * method. Duck-typed rather than `instanceof BrowserTool` on purpose —
41
+ * see the module header: multiple packages in this monorepo (e.g.
42
+ * browsermesh-apps) vendor their own copy of the `BrowserTool` shim for
43
+ * standalone use, so a tool built against one package's `BrowserTool`
44
+ * class is not `instanceof` another package's class even though it has
45
+ * the identical shape. Duck-typing lets `BrowserToolRegistry` accept any
46
+ * of them.
47
+ * @param {unknown} tool
48
+ * @returns {boolean}
49
+ */
50
+ function looksLikeBrowserTool(tool) {
51
+ if (!tool || (typeof tool !== 'object' && typeof tool !== 'function')) return false;
52
+ if (typeof tool.execute !== 'function') return false;
53
+ let spec;
54
+ try {
55
+ spec = tool.spec;
56
+ } catch {
57
+ return false;
58
+ }
59
+ return !!spec && typeof spec.name === 'string' && spec.name.length > 0;
60
+ }
61
+
62
+ /**
63
+ * BrowserToolRegistry -- holds constructed BrowserTool instances, keyed by
64
+ * `.name`, and exposes the `.spec` list an `llmFn` (see the agent-runtime
65
+ * plan) needs for tool-calling.
66
+ *
67
+ * `register()` duck-types its argument (see `looksLikeBrowserTool` above)
68
+ * rather than requiring `instanceof BrowserTool`, for the same
69
+ * cross-package "standalone use" reason `BrowserTool` itself avoids hard
70
+ * class-identity assumptions.
71
+ *
72
+ * Registering a second tool under a name that's already registered
73
+ * throws (it does not silently overwrite) — a name collision usually
74
+ * means two unrelated tools accidentally share a name, and silently
75
+ * dropping one would be a confusing, hard-to-debug failure mode. Call
76
+ * `unregister(name)` first if replacing a tool is genuinely intended.
77
+ */
78
+ export class BrowserToolRegistry {
79
+ #tools = new Map();
80
+
81
+ /**
82
+ * @param {BrowserTool | { spec: object, execute: Function }} tool
83
+ * @returns {BrowserTool} the same tool, for chaining
84
+ */
85
+ register(tool) {
86
+ if (!looksLikeBrowserTool(tool)) {
87
+ throw new TypeError(
88
+ 'BrowserToolRegistry.register(tool): tool must implement the BrowserTool shape ' +
89
+ '(a `.spec` getter and an `.execute()` method) — got ' +
90
+ (tool && tool.constructor ? tool.constructor.name : String(tool))
91
+ );
92
+ }
93
+ const { name } = tool;
94
+ if (this.#tools.has(name)) {
95
+ throw new Error(`BrowserToolRegistry.register(tool): a tool named "${name}" is already registered; call unregister("${name}") first to replace it`);
96
+ }
97
+ this.#tools.set(name, tool);
98
+ return tool;
99
+ }
100
+
101
+ /**
102
+ * @param {string} name
103
+ * @returns {BrowserTool | undefined}
104
+ */
105
+ get(name) {
106
+ return this.#tools.get(name);
107
+ }
108
+
109
+ /** @returns {BrowserTool[]} all registered tool instances */
110
+ list() {
111
+ return [...this.#tools.values()];
112
+ }
113
+
114
+ /** @returns {object[]} the `.spec` of every registered tool */
115
+ listSpecs() {
116
+ return this.list().map((tool) => tool.spec);
117
+ }
118
+
119
+ /**
120
+ * @param {string} name
121
+ * @returns {boolean} true if a tool with that name was registered and removed
122
+ */
123
+ unregister(name) {
124
+ return this.#tools.delete(name);
125
+ }
126
+ }
package/src/index.mjs CHANGED
@@ -12,4 +12,4 @@ export * from './trust.mjs';
12
12
  export * from './hardening.mjs';
13
13
  export * from './identity-base.mjs';
14
14
  export * from './identity-wallet.mjs';
15
- export { BrowserTool } from './compat.mjs';
15
+ export { BrowserTool, BrowserToolRegistry } from './compat.mjs';
@@ -209,13 +209,18 @@ export class MeshSchedulerSubmitTool extends BrowserTool {
209
209
  if (!sched) return { success: false, output: '', error: 'Mesh scheduler not initialized.' }
210
210
  try {
211
211
  // ScheduledTask lives in the sibling browsermesh-apps package, which
212
- // itself depends on browsermesh-core (peerDependency) — core can't
213
- // depend back on apps without a cycle, so this reaches across via a
214
- // relative path that only resolves inside this monorepo, pre-existing
215
- // from before this package was re-extracted. Contained by the
216
- // surrounding try/catch: if apps isn't present, this one tool fails
217
- // gracefully rather than crashing the module.
218
- const { ScheduledTask } = await import('../../browsermesh-apps/src/scheduler.mjs')
212
+ // itself depends on browsermesh-core (peerDependency) -- core can't
213
+ // depend back on apps without a build-time cycle. Reached via a
214
+ // lazy import of the real package NAME (not a monorepo-relative
215
+ // path, which only ever resolved inside this workspace and broke
216
+ // for every standalone consumer via esm.sh) -- matches this
217
+ // family's established pattern for an optional cross-package reach
218
+ // (see @johnhenry/andbox's lazy import in browsermesh-apps'
219
+ // serverless-executor-andbox.mjs). Declared as an optional peer
220
+ // dependency below; contained by the surrounding try/catch so a
221
+ // consumer without apps installed still fails this one tool
222
+ // gracefully instead of crashing the module.
223
+ const { ScheduledTask } = await import('@johnhenry/browsermesh-apps')
219
224
  const task = new ScheduledTask({
220
225
  id: crypto.randomUUID(),
221
226
  type,