@johnhenry/browsermesh-core 0.1.1 → 0.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnhenry/browsermesh-core",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Identity, crypto, peer management, and trust primitives for BrowserMesh",
5
5
  "type": "module",
6
6
  "main": "./src/index.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';