@johnhenry/browsermesh-apps 0.2.0 → 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.
Files changed (50) hide show
  1. package/README.md +232 -1
  2. package/package.json +7 -3
  3. package/src/agent-runtime.mjs +342 -0
  4. package/src/chunk-replication.mjs +751 -0
  5. package/src/cloud-storage-backend.mjs +932 -0
  6. package/src/cloud-storage.mjs +859 -0
  7. package/src/compat.mjs +98 -0
  8. package/src/grant-log.mjs +840 -0
  9. package/src/index.mjs +34 -2
  10. package/src/key-distribution.mjs +525 -0
  11. package/src/manifest-sync.mjs +363 -0
  12. package/src/mesh-agent-swarm.mjs +376 -0
  13. package/src/mesh-bootstrap.mjs +1131 -12
  14. package/src/mesh-compute.mjs +397 -0
  15. package/src/mesh-dht.mjs +182 -0
  16. package/src/mesh-fetch.mjs +246 -0
  17. package/src/mesh-hardening.mjs +226 -0
  18. package/src/mesh-health.mjs +282 -0
  19. package/src/mesh-keepalive.mjs +353 -0
  20. package/src/mesh-kv.mjs +673 -0
  21. package/src/mesh-orchestrator-tools.mjs +251 -0
  22. package/src/mesh-orchestrator.mjs +343 -0
  23. package/src/mesh-rpc.mjs +300 -0
  24. package/src/mesh-service.mjs +403 -0
  25. package/src/mesh-swarm.mjs +677 -0
  26. package/src/mesh-timestamp.mjs +265 -0
  27. package/src/mesh-torrent.mjs +683 -0
  28. package/src/mesh-verification.mjs +367 -0
  29. package/src/mesh-websocket.mjs +722 -0
  30. package/src/observability-bridge.mjs +453 -0
  31. package/src/orchestrator.mjs +18 -5
  32. package/src/peer-agent-swarm.mjs +12 -1
  33. package/src/peer-chat.mjs +230 -61
  34. package/src/peer-compute.mjs +66 -3
  35. package/src/peer-escrow.mjs +350 -1
  36. package/src/peer-files.mjs +362 -168
  37. package/src/peer-ipfs.mjs +205 -10
  38. package/src/peer-node.mjs +33 -2
  39. package/src/peer-routing.mjs +369 -5
  40. package/src/peer-terminal.mjs +400 -130
  41. package/src/serverless-executor-andbox.mjs +136 -0
  42. package/src/serverless-fetch.mjs +70 -0
  43. package/src/serverless-functions.mjs +67 -0
  44. package/src/serverless-peer-select.mjs +69 -0
  45. package/src/serverless-proxy.mjs +127 -0
  46. package/src/serverless-router.mjs +115 -0
  47. package/src/serverless-sites.mjs +116 -0
  48. package/src/serverless-static.mjs +202 -0
  49. package/src/serverless-wire.mjs +87 -0
  50. package/src/peer-session.mjs +0 -937
package/src/compat.mjs CHANGED
@@ -9,6 +9,13 @@
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 (same bug
11
11
  * found and fixed in browsermesh-core's compat.mjs).
12
+ *
13
+ * Also exports BrowserToolRegistry, BrowserTool's counterpart: a
14
+ * name-keyed collection of constructed tool instances used to dispatch
15
+ * LLM-requested tool calls (see `registerMeshTools` in tools.mjs, which
16
+ * calls `registry.register(...)`). Mirrors browsermesh-core's
17
+ * compat.mjs's BrowserToolRegistry exactly, kept as a separate copy for
18
+ * the same standalone-use reason BrowserTool itself is duplicated here.
12
19
  */
13
20
  export class BrowserTool {
14
21
  /** @returns {object} ToolSpec-compatible object */
@@ -28,3 +35,94 @@ export class BrowserTool {
28
35
 
29
36
  async execute(_params) { throw new Error('Not implemented'); }
30
37
  }
38
+
39
+ /**
40
+ * Returns true if `tool` looks like a BrowserTool: it exposes a working
41
+ * `.spec` getter (with a non-empty string `.name`) and an `.execute`
42
+ * method. Duck-typed rather than `instanceof BrowserTool` on purpose —
43
+ * see the module header: this package vendors its own copy of the
44
+ * `BrowserTool` shim (same as browsermesh-core's compat.mjs) for
45
+ * standalone use, so a tool built against one package's `BrowserTool`
46
+ * class is not `instanceof` another package's class even though it has
47
+ * the identical shape. Duck-typing lets `BrowserToolRegistry` accept any
48
+ * of them.
49
+ * @param {unknown} tool
50
+ * @returns {boolean}
51
+ */
52
+ function looksLikeBrowserTool(tool) {
53
+ if (!tool || (typeof tool !== 'object' && typeof tool !== 'function')) return false;
54
+ if (typeof tool.execute !== 'function') return false;
55
+ let spec;
56
+ try {
57
+ spec = tool.spec;
58
+ } catch {
59
+ return false;
60
+ }
61
+ return !!spec && typeof spec.name === 'string' && spec.name.length > 0;
62
+ }
63
+
64
+ /**
65
+ * BrowserToolRegistry -- holds constructed BrowserTool instances, keyed by
66
+ * `.name`, and exposes the `.spec` list an `llmFn` (see the agent-runtime
67
+ * plan) needs for tool-calling.
68
+ *
69
+ * `register()` duck-types its argument (see `looksLikeBrowserTool` above)
70
+ * rather than requiring `instanceof BrowserTool`, for the same
71
+ * cross-package "standalone use" reason `BrowserTool` itself avoids hard
72
+ * class-identity assumptions.
73
+ *
74
+ * Registering a second tool under a name that's already registered
75
+ * throws (it does not silently overwrite) — a name collision usually
76
+ * means two unrelated tools accidentally share a name, and silently
77
+ * dropping one would be a confusing, hard-to-debug failure mode. Call
78
+ * `unregister(name)` first if replacing a tool is genuinely intended.
79
+ */
80
+ export class BrowserToolRegistry {
81
+ #tools = new Map();
82
+
83
+ /**
84
+ * @param {BrowserTool | { spec: object, execute: Function }} tool
85
+ * @returns {BrowserTool} the same tool, for chaining
86
+ */
87
+ register(tool) {
88
+ if (!looksLikeBrowserTool(tool)) {
89
+ throw new TypeError(
90
+ 'BrowserToolRegistry.register(tool): tool must implement the BrowserTool shape ' +
91
+ '(a `.spec` getter and an `.execute()` method) — got ' +
92
+ (tool && tool.constructor ? tool.constructor.name : String(tool))
93
+ );
94
+ }
95
+ const { name } = tool;
96
+ if (this.#tools.has(name)) {
97
+ throw new Error(`BrowserToolRegistry.register(tool): a tool named "${name}" is already registered; call unregister("${name}") first to replace it`);
98
+ }
99
+ this.#tools.set(name, tool);
100
+ return tool;
101
+ }
102
+
103
+ /**
104
+ * @param {string} name
105
+ * @returns {BrowserTool | undefined}
106
+ */
107
+ get(name) {
108
+ return this.#tools.get(name);
109
+ }
110
+
111
+ /** @returns {BrowserTool[]} all registered tool instances */
112
+ list() {
113
+ return [...this.#tools.values()];
114
+ }
115
+
116
+ /** @returns {object[]} the `.spec` of every registered tool */
117
+ listSpecs() {
118
+ return this.list().map((tool) => tool.spec);
119
+ }
120
+
121
+ /**
122
+ * @param {string} name
123
+ * @returns {boolean} true if a tool with that name was registered and removed
124
+ */
125
+ unregister(name) {
126
+ return this.#tools.delete(name);
127
+ }
128
+ }