@haex-space/vault-sdk 4.1.0 → 4.3.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 (55) hide show
  1. package/README.md +69 -0
  2. package/dist/cli/index.js +2 -1
  3. package/dist/cli/index.js.map +1 -1
  4. package/dist/cli/index.mjs +2 -1
  5. package/dist/cli/index.mjs.map +1 -1
  6. package/dist/{client-Cx4ZyQFL.d.mts → client-CDox5_KH.d.mts} +263 -27
  7. package/dist/{client-CD5QZi7F.d.ts → client-pppDo5Q3.d.ts} +263 -27
  8. package/dist/haex_bundle_bg.wasm +0 -0
  9. package/dist/index.d.mts +45 -6
  10. package/dist/index.d.ts +45 -6
  11. package/dist/index.js +363 -14
  12. package/dist/index.js.map +1 -1
  13. package/dist/index.mjs +360 -15
  14. package/dist/index.mjs.map +1 -1
  15. package/dist/node.d.mts +1 -1
  16. package/dist/node.d.ts +1 -1
  17. package/dist/node.js +2 -1
  18. package/dist/node.js.map +1 -1
  19. package/dist/node.mjs +2 -1
  20. package/dist/node.mjs.map +1 -1
  21. package/dist/nuxt.js +10 -2
  22. package/dist/nuxt.js.map +1 -1
  23. package/dist/nuxt.mjs +10 -2
  24. package/dist/nuxt.mjs.map +1 -1
  25. package/dist/react.d.mts +2 -2
  26. package/dist/react.d.ts +2 -2
  27. package/dist/react.js +339 -14
  28. package/dist/react.js.map +1 -1
  29. package/dist/react.mjs +339 -14
  30. package/dist/react.mjs.map +1 -1
  31. package/dist/runtime/nuxt.plugin.client.d.mts +2 -2
  32. package/dist/runtime/nuxt.plugin.client.d.ts +2 -2
  33. package/dist/runtime/nuxt.plugin.client.js +329 -14
  34. package/dist/runtime/nuxt.plugin.client.js.map +1 -1
  35. package/dist/runtime/nuxt.plugin.client.mjs +329 -14
  36. package/dist/runtime/nuxt.plugin.client.mjs.map +1 -1
  37. package/dist/svelte.d.mts +2 -2
  38. package/dist/svelte.d.ts +2 -2
  39. package/dist/svelte.js +339 -14
  40. package/dist/svelte.js.map +1 -1
  41. package/dist/svelte.mjs +339 -14
  42. package/dist/svelte.mjs.map +1 -1
  43. package/dist/{types-BwFKGH70.d.mts → types-DoNvKiW_.d.mts} +75 -1
  44. package/dist/{types-BwFKGH70.d.ts → types-DoNvKiW_.d.ts} +75 -1
  45. package/dist/vite.js +8 -1
  46. package/dist/vite.js.map +1 -1
  47. package/dist/vite.mjs +8 -1
  48. package/dist/vite.mjs.map +1 -1
  49. package/dist/vue.d.mts +2 -2
  50. package/dist/vue.d.ts +2 -2
  51. package/dist/vue.js +339 -14
  52. package/dist/vue.js.map +1 -1
  53. package/dist/vue.mjs +339 -14
  54. package/dist/vue.mjs.map +1 -1
  55. package/package.json +1 -1
package/dist/vue.js CHANGED
@@ -310,7 +310,14 @@ var HAEXSPACE_MESSAGE_TYPES = {
310
310
  *
311
311
  * Payload: `{ type: PORT_READY }` — no data.
312
312
  */
313
- PORT_READY: "haexspace:port:ready"
313
+ PORT_READY: "haexspace:port:ready",
314
+ /**
315
+ * One MCP message of the tool connection (holzi spec 018), in both directions over the
316
+ * MessagePort: the host calls the extension's tools, `sdk.tools` answers.
317
+ *
318
+ * Payload: `{ type: MCP, message }`, where `message` is a JSON-RPC 2.0 object.
319
+ */
320
+ MCP: "haexspace:mcp"
314
321
  };
315
322
 
316
323
  // src/polyfills/debug.ts
@@ -648,6 +655,21 @@ var REMOTE_STORAGE_COMMANDS = {
648
655
  list: "extension_remote_storage_list"
649
656
  };
650
657
 
658
+ // src/commands/encryptedFolders.ts
659
+ init_cjs_shims();
660
+ var ENCRYPTED_FOLDER_COMMANDS = {
661
+ /** Let the user pick an encrypted folder of a storage and grant access to it */
662
+ choose: "extension_encrypted_folder_choose",
663
+ /** List the entries of a folder inside an encrypted folder */
664
+ list: "extension_encrypted_folder_list",
665
+ /** Download and decrypt a file */
666
+ download: "extension_encrypted_folder_download",
667
+ /** Encrypt and upload a file */
668
+ upload: "extension_encrypted_folder_upload",
669
+ /** Delete a file or folder */
670
+ delete: "extension_encrypted_folder_delete"
671
+ };
672
+
651
673
  // src/commands/localsend.ts
652
674
  init_cjs_shims();
653
675
  var LOCALSEND_COMMANDS = {
@@ -1373,26 +1395,32 @@ var BackendManagement = class {
1373
1395
  );
1374
1396
  }
1375
1397
  /**
1376
- * Add a new storage backend
1377
- * @param request - Backend configuration
1398
+ * Propose a new storage. The host asks the user; credentials are entered
1399
+ * only there. Resolves with the new storage, which the extension may then
1400
+ * read and write.
1401
+ * @param request - The proposal, without credentials
1378
1402
  * @returns Created backend info
1379
1403
  */
1380
1404
  async add(request) {
1381
1405
  return this.client.request(
1382
1406
  REMOTE_STORAGE_COMMANDS.addBackend,
1383
- { request }
1407
+ { request },
1408
+ // No deadline: the host waits for the user's answer in its dialog.
1409
+ { timeout: null }
1384
1410
  );
1385
1411
  }
1386
1412
  /**
1387
- * Update a storage backend
1388
- * Only provided fields are updated. Credentials are preserved if not provided.
1413
+ * Change a storage after the user confirms it in the host.
1414
+ * Only provided fields are updated; new credentials are entered in the host.
1389
1415
  * @param request - Update request with backendId and fields to update
1390
1416
  * @returns Updated backend info
1391
1417
  */
1392
1418
  async update(request) {
1393
1419
  return this.client.request(
1394
1420
  REMOTE_STORAGE_COMMANDS.updateBackend,
1395
- { request }
1421
+ { request },
1422
+ // No deadline: the host waits for the user's answer in its dialog.
1423
+ { timeout: null }
1396
1424
  );
1397
1425
  }
1398
1426
  /**
@@ -1400,17 +1428,100 @@ var BackendManagement = class {
1400
1428
  * @param backendId - Backend ID to remove
1401
1429
  */
1402
1430
  async remove(backendId) {
1403
- await this.client.request(REMOTE_STORAGE_COMMANDS.removeBackend, {
1404
- backendId
1405
- });
1431
+ await this.client.request(
1432
+ REMOTE_STORAGE_COMMANDS.removeBackend,
1433
+ { backendId },
1434
+ { timeout: null }
1435
+ );
1406
1436
  }
1407
1437
  /**
1408
1438
  * Test connection to a storage backend
1409
1439
  * @param backendId - Backend ID to test
1410
1440
  */
1411
1441
  async test(backendId) {
1412
- await this.client.request(REMOTE_STORAGE_COMMANDS.testBackend, {
1413
- backendId
1442
+ await this.client.request(
1443
+ REMOTE_STORAGE_COMMANDS.testBackend,
1444
+ { backendId },
1445
+ { timeout: null }
1446
+ );
1447
+ }
1448
+ };
1449
+
1450
+ // src/api/encryptedFolders.ts
1451
+ init_cjs_shims();
1452
+ init_vaultKey();
1453
+ var EncryptedFoldersAPI = class {
1454
+ constructor(client) {
1455
+ this.client = client;
1456
+ }
1457
+ /**
1458
+ * Let the user choose one of the storage's encrypted folders and grant
1459
+ * access to it. The host shows its own window and waits for the answer.
1460
+ * Needs the `remoteStorage` permission for the storage (else 1002 without
1461
+ * asking); 1002 as well when the user refuses.
1462
+ * @param storageId - Storage the folder lies in
1463
+ * @param access - Access the extension asks for
1464
+ * @returns The chosen folder, or `null` when the user closed the chooser without choosing
1465
+ */
1466
+ async choose(storageId, access) {
1467
+ const choice = await this.client.request(
1468
+ ENCRYPTED_FOLDER_COMMANDS.choose,
1469
+ { request: { storageId, access } },
1470
+ // No deadline: the host waits for the user's answer in its window.
1471
+ { timeout: null }
1472
+ );
1473
+ return choice ?? null;
1474
+ }
1475
+ /**
1476
+ * List a folder inside an encrypted folder (permission `read`).
1477
+ * @param storageId - Storage the folder lies in
1478
+ * @param folderId - Folder from {@link EncryptedFoldersAPI.choose}
1479
+ * @param path - Path relative to the encrypted folder; `''` (default) for its root
1480
+ * @returns The entries with their decrypted names
1481
+ */
1482
+ async list(storageId, folderId, path = "") {
1483
+ const result = await this.client.request(
1484
+ ENCRYPTED_FOLDER_COMMANDS.list,
1485
+ { request: { storageId, folderId, path } }
1486
+ );
1487
+ return result.entries;
1488
+ }
1489
+ /**
1490
+ * Download and decrypt a file (permission `read`).
1491
+ * @param storageId - Storage the folder lies in
1492
+ * @param folderId - Folder from {@link EncryptedFoldersAPI.choose}
1493
+ * @param path - Path of the file, relative to the encrypted folder
1494
+ * @returns The decrypted content
1495
+ */
1496
+ async download(storageId, folderId, path) {
1497
+ const base64 = await this.client.request(
1498
+ ENCRYPTED_FOLDER_COMMANDS.download,
1499
+ { request: { storageId, folderId, path } }
1500
+ );
1501
+ return base64ToArrayBuffer(base64);
1502
+ }
1503
+ /**
1504
+ * Encrypt and upload a file (permission `readWrite`). Missing parent
1505
+ * folders are created.
1506
+ * @param storageId - Storage the folder lies in
1507
+ * @param folderId - Folder from {@link EncryptedFoldersAPI.choose}
1508
+ * @param path - Path of the file, relative to the encrypted folder
1509
+ * @param data - Content of the file
1510
+ */
1511
+ async upload(storageId, folderId, path, data) {
1512
+ await this.client.request(ENCRYPTED_FOLDER_COMMANDS.upload, {
1513
+ request: { storageId, folderId, path, data: arrayBufferToBase64(data) }
1514
+ });
1515
+ }
1516
+ /**
1517
+ * Delete a file or folder (permission `readWrite`).
1518
+ * @param storageId - Storage the folder lies in
1519
+ * @param folderId - Folder from {@link EncryptedFoldersAPI.choose}
1520
+ * @param path - Path relative to the encrypted folder
1521
+ */
1522
+ async delete(storageId, folderId, path) {
1523
+ await this.client.request(ENCRYPTED_FOLDER_COMMANDS.delete, {
1524
+ request: { storageId, folderId, path }
1414
1525
  });
1415
1526
  }
1416
1527
  };
@@ -2021,6 +2132,202 @@ var DialogAPI = class {
2021
2132
  }
2022
2133
  };
2023
2134
 
2135
+ // src/api/tools/index.ts
2136
+ init_cjs_shims();
2137
+
2138
+ // src/api/tools/server.ts
2139
+ init_cjs_shims();
2140
+
2141
+ // src/api/tools/schema.ts
2142
+ init_cjs_shims();
2143
+ function fail(field, message) {
2144
+ return { ok: false, field, message };
2145
+ }
2146
+ function typeMatches(type, value) {
2147
+ switch (type) {
2148
+ case "object":
2149
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2150
+ case "array":
2151
+ return Array.isArray(value);
2152
+ case "integer":
2153
+ return Number.isInteger(value);
2154
+ case "number":
2155
+ return typeof value === "number" && Number.isFinite(value);
2156
+ case "string":
2157
+ return typeof value === "string";
2158
+ case "boolean":
2159
+ return typeof value === "boolean";
2160
+ default:
2161
+ return false;
2162
+ }
2163
+ }
2164
+ function validateAt(schema, value, field) {
2165
+ if (!typeMatches(schema.type, value)) return fail(field, `expected ${String(schema.type)}`);
2166
+ if (schema.enum && !schema.enum.includes(value)) {
2167
+ return fail(field, `expected one of ${schema.enum.join(", ")}`);
2168
+ }
2169
+ if (schema.type === "array" && schema.items) {
2170
+ const items = value;
2171
+ for (let i = 0; i < items.length; i++) {
2172
+ const result = validateAt(schema.items, items[i], `${field}[${i}]`);
2173
+ if (!result.ok) return result;
2174
+ }
2175
+ }
2176
+ if (schema.type === "object") {
2177
+ const record = value;
2178
+ const properties = schema.properties ?? {};
2179
+ const prefix = field === "" ? "" : `${field}.`;
2180
+ if (schema.properties) {
2181
+ for (const key of Object.keys(record)) {
2182
+ if (!Object.hasOwn(properties, key)) return fail(`${prefix}${key}`, "unknown property");
2183
+ }
2184
+ }
2185
+ for (const key of schema.required ?? []) {
2186
+ if (record[key] === void 0) return fail(`${prefix}${key}`, "required");
2187
+ }
2188
+ for (const [key, child] of Object.entries(properties)) {
2189
+ if (record[key] === void 0) continue;
2190
+ const result = validateAt(child, record[key], `${prefix}${key}`);
2191
+ if (!result.ok) return result;
2192
+ }
2193
+ }
2194
+ return { ok: true };
2195
+ }
2196
+ function validateInput(schema, value) {
2197
+ return validateAt(schema, value, "");
2198
+ }
2199
+
2200
+ // src/api/tools/server.ts
2201
+ var PROTOCOL_VERSION = "2025-11-25";
2202
+ var METHOD_NOT_FOUND = -32601;
2203
+ var INVALID_PARAMS = -32602;
2204
+ function textResult(text, isError) {
2205
+ return { content: [{ type: "text", text }], isError };
2206
+ }
2207
+ function asText(value) {
2208
+ if (typeof value === "string") return value;
2209
+ if (value === void 0) return "";
2210
+ return JSON.stringify(value);
2211
+ }
2212
+ function isId(value) {
2213
+ return typeof value === "string" || typeof value === "number";
2214
+ }
2215
+ var ToolsServer = class {
2216
+ constructor(options) {
2217
+ this.options = options;
2218
+ this.running = /* @__PURE__ */ new Map();
2219
+ }
2220
+ /**
2221
+ * Handles one JSON-RPC message from the host. A tool call runs on its own and answers when it is
2222
+ * done, so a long call never holds up `ping` or the cancellation of that very call.
2223
+ */
2224
+ async receive(message) {
2225
+ if (typeof message !== "object" || message === null) return;
2226
+ const { id, method, params } = message;
2227
+ if (typeof method !== "string") return;
2228
+ const args = typeof params === "object" && params !== null ? params : {};
2229
+ if (!isId(id)) {
2230
+ if (method === "notifications/cancelled" && isId(args.requestId)) {
2231
+ this.running.get(args.requestId)?.abort();
2232
+ }
2233
+ return;
2234
+ }
2235
+ switch (method) {
2236
+ case "initialize":
2237
+ return this.reply(id, {
2238
+ protocolVersion: PROTOCOL_VERSION,
2239
+ capabilities: { tools: {} },
2240
+ serverInfo: {
2241
+ name: this.options.manifest?.name ?? "",
2242
+ version: this.options.manifest?.version ?? ""
2243
+ }
2244
+ });
2245
+ case "ping":
2246
+ return this.reply(id, {});
2247
+ case "tools/list": {
2248
+ const tools = this.tools().map(({ name, title, description, inputSchema }) => ({
2249
+ name,
2250
+ title: title.en,
2251
+ description,
2252
+ inputSchema
2253
+ }));
2254
+ return this.reply(id, { tools });
2255
+ }
2256
+ case "tools/call":
2257
+ void this.call(id, args);
2258
+ return;
2259
+ default:
2260
+ return this.error(id, METHOD_NOT_FOUND, method);
2261
+ }
2262
+ }
2263
+ /** The declared tools that have a handler. */
2264
+ tools() {
2265
+ return (this.options.manifest?.tools ?? []).filter((tool) => this.options.handlers.has(tool.name));
2266
+ }
2267
+ async call(id, params) {
2268
+ const tool = this.tools().find((candidate) => candidate.name === params.name);
2269
+ const handler = tool && this.options.handlers.get(tool.name);
2270
+ if (!tool || !handler) return this.error(id, INVALID_PARAMS, `unknown tool ${String(params.name)}`);
2271
+ const input = params.arguments ?? {};
2272
+ const checked = validateInput(tool.inputSchema, input);
2273
+ if (!checked.ok) {
2274
+ return this.reply(id, textResult(`invalid input: ${checked.field}: ${checked.message}`, true));
2275
+ }
2276
+ const controller = new AbortController();
2277
+ this.running.set(id, controller);
2278
+ let result;
2279
+ try {
2280
+ const output = await handler(input, { signal: controller.signal });
2281
+ result = textResult(asText(output), false);
2282
+ } catch (error) {
2283
+ result = textResult(error instanceof Error ? error.message : String(error), true);
2284
+ } finally {
2285
+ this.running.delete(id);
2286
+ }
2287
+ if (!controller.signal.aborted) this.reply(id, result);
2288
+ }
2289
+ reply(id, result) {
2290
+ this.options.send({ jsonrpc: "2.0", id, result });
2291
+ }
2292
+ error(id, code, message) {
2293
+ this.options.send({ jsonrpc: "2.0", id, error: { code, message } });
2294
+ }
2295
+ };
2296
+
2297
+ // src/api/tools/index.ts
2298
+ var ToolsAPI = class {
2299
+ constructor(options) {
2300
+ this.options = options;
2301
+ this.handlers = /* @__PURE__ */ new Map();
2302
+ const manifest = options.manifest;
2303
+ this.server = new ToolsServer({
2304
+ get manifest() {
2305
+ return manifest();
2306
+ },
2307
+ handlers: this.handlers,
2308
+ send: options.send
2309
+ });
2310
+ }
2311
+ /**
2312
+ * Runs `handler` when the host calls the tool `name`. The tool must be declared in the manifest.
2313
+ * Returns a function that unregisters the handler; the host then no longer sees the tool.
2314
+ */
2315
+ register(name, handler) {
2316
+ const declared = this.options.manifest()?.tools?.some((tool) => tool.name === name) ?? false;
2317
+ if (!declared) {
2318
+ throw new Error(`The tool ${name} is not declared in the manifest's tools`);
2319
+ }
2320
+ this.handlers.set(name, handler);
2321
+ return () => {
2322
+ if (this.handlers.get(name) === handler) this.handlers.delete(name);
2323
+ };
2324
+ }
2325
+ /** Handles one MCP message from the host. Called by the SDK for `haexspace:mcp` port messages. */
2326
+ receive(message) {
2327
+ return this.server.receive(message);
2328
+ }
2329
+ };
2330
+
2024
2331
  // src/client/tableName.ts
2025
2332
  init_cjs_shims();
2026
2333
  function validatePublicKey(publicKey) {
@@ -2402,7 +2709,11 @@ function createMessageHandler(config, pendingRequests, extensionInfo2, onEvent)
2402
2709
  }
2403
2710
  };
2404
2711
  }
2405
- function processEvent(event, log, eventListeners, onContextChanged, onExternalRequest, onActionRequest) {
2712
+ function processEvent(event, log, eventListeners, onContextChanged, onExternalRequest, onActionRequest, onMcpMessage) {
2713
+ if (event.type === HAEXSPACE_MESSAGE_TYPES.MCP) {
2714
+ onMcpMessage?.(event.message);
2715
+ return;
2716
+ }
2406
2717
  if (event.type === HAEXTENSION_EVENTS.CONTEXT_CHANGED) {
2407
2718
  const contextEvent = event;
2408
2719
  onContextChanged(contextEvent.data.context);
@@ -2674,6 +2985,7 @@ var HaexVaultSdk = class {
2674
2985
  this.web = new WebAPI(this);
2675
2986
  this.permissions = new PermissionsAPI(this);
2676
2987
  this.remoteStorage = new RemoteStorageAPI(this);
2988
+ this.encryptedFolders = new EncryptedFoldersAPI(this);
2677
2989
  this.localsend = new LocalSendAPI(this);
2678
2990
  this.spaces = new SpacesAPI(this);
2679
2991
  this.shell = new ShellAPI(this);
@@ -2682,6 +2994,10 @@ var HaexVaultSdk = class {
2682
2994
  this.notifications = new NotificationsAPI(this);
2683
2995
  this.tab = new TabAPI(this);
2684
2996
  this.dialog = new DialogAPI(this);
2997
+ this.tools = new ToolsAPI({
2998
+ manifest: () => this.config.manifest,
2999
+ send: (message) => this.sendMcpMessage(message)
3000
+ });
2685
3001
  installConsoleForwarding(this.config.debug);
2686
3002
  this.on(HAEXTENSION_EVENTS.PERMISSION_RESOLVED, (event) => {
2687
3003
  const data = event.data;
@@ -2989,9 +3305,18 @@ var HaexVaultSdk = class {
2989
3305
  this.notifySubscribersInternal();
2990
3306
  },
2991
3307
  (extEvent) => this.handleExternalRequestInternal(extEvent.data),
2992
- (actionEvent) => this.handleActionRequestInternal(actionEvent.data)
3308
+ (actionEvent) => this.handleActionRequestInternal(actionEvent.data),
3309
+ (message) => void this.tools.receive(message)
2993
3310
  );
2994
3311
  }
3312
+ /** Sends one MCP message of the tool connection to the host on the port. */
3313
+ sendMcpMessage(message) {
3314
+ if (!this.hostPort) {
3315
+ this.log("Dropped an MCP message: no host port yet");
3316
+ return;
3317
+ }
3318
+ this.hostPort.postMessage({ type: HAEXSPACE_MESSAGE_TYPES.MCP, message });
3319
+ }
2995
3320
  async handleExternalRequestInternal(request) {
2996
3321
  await handleExternalRequest(request, this.externalRequestHandlers, this.respondToExternalRequest.bind(this), this.log.bind(this));
2997
3322
  }