@haex-space/vault-sdk 4.2.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-BPwsH1so.d.ts → client-CDox5_KH.d.mts} +166 -2
  7. package/dist/{client-7bZyiRwE.d.mts → client-pppDo5Q3.d.ts} +166 -2
  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 +342 -3
  12. package/dist/index.js.map +1 -1
  13. package/dist/index.mjs +339 -4
  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 +318 -3
  28. package/dist/react.js.map +1 -1
  29. package/dist/react.mjs +318 -3
  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 +308 -3
  34. package/dist/runtime/nuxt.plugin.client.js.map +1 -1
  35. package/dist/runtime/nuxt.plugin.client.mjs +308 -3
  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 +318 -3
  40. package/dist/svelte.js.map +1 -1
  41. package/dist/svelte.mjs +318 -3
  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 +318 -3
  52. package/dist/vue.js.map +1 -1
  53. package/dist/vue.mjs +318 -3
  54. package/dist/vue.mjs.map +1 -1
  55. package/package.json +1 -1
@@ -211,6 +211,20 @@ var REMOTE_STORAGE_COMMANDS = {
211
211
  list: "extension_remote_storage_list"
212
212
  };
213
213
 
214
+ // src/commands/encryptedFolders.ts
215
+ var ENCRYPTED_FOLDER_COMMANDS = {
216
+ /** Let the user pick an encrypted folder of a storage and grant access to it */
217
+ choose: "extension_encrypted_folder_choose",
218
+ /** List the entries of a folder inside an encrypted folder */
219
+ list: "extension_encrypted_folder_list",
220
+ /** Download and decrypt a file */
221
+ download: "extension_encrypted_folder_download",
222
+ /** Encrypt and upload a file */
223
+ upload: "extension_encrypted_folder_upload",
224
+ /** Delete a file or folder */
225
+ delete: "extension_encrypted_folder_delete"
226
+ };
227
+
214
228
  // src/commands/localsend.ts
215
229
  var LOCALSEND_COMMANDS = {
216
230
  // Initialization
@@ -1002,6 +1016,83 @@ var BackendManagement = class {
1002
1016
  }
1003
1017
  };
1004
1018
 
1019
+ // src/api/encryptedFolders.ts
1020
+ var EncryptedFoldersAPI = class {
1021
+ constructor(client) {
1022
+ this.client = client;
1023
+ }
1024
+ /**
1025
+ * Let the user choose one of the storage's encrypted folders and grant
1026
+ * access to it. The host shows its own window and waits for the answer.
1027
+ * Needs the `remoteStorage` permission for the storage (else 1002 without
1028
+ * asking); 1002 as well when the user refuses.
1029
+ * @param storageId - Storage the folder lies in
1030
+ * @param access - Access the extension asks for
1031
+ * @returns The chosen folder, or `null` when the user closed the chooser without choosing
1032
+ */
1033
+ async choose(storageId, access) {
1034
+ const choice = await this.client.request(
1035
+ ENCRYPTED_FOLDER_COMMANDS.choose,
1036
+ { request: { storageId, access } },
1037
+ // No deadline: the host waits for the user's answer in its window.
1038
+ { timeout: null }
1039
+ );
1040
+ return choice ?? null;
1041
+ }
1042
+ /**
1043
+ * List a folder inside an encrypted folder (permission `read`).
1044
+ * @param storageId - Storage the folder lies in
1045
+ * @param folderId - Folder from {@link EncryptedFoldersAPI.choose}
1046
+ * @param path - Path relative to the encrypted folder; `''` (default) for its root
1047
+ * @returns The entries with their decrypted names
1048
+ */
1049
+ async list(storageId, folderId, path = "") {
1050
+ const result = await this.client.request(
1051
+ ENCRYPTED_FOLDER_COMMANDS.list,
1052
+ { request: { storageId, folderId, path } }
1053
+ );
1054
+ return result.entries;
1055
+ }
1056
+ /**
1057
+ * Download and decrypt a file (permission `read`).
1058
+ * @param storageId - Storage the folder lies in
1059
+ * @param folderId - Folder from {@link EncryptedFoldersAPI.choose}
1060
+ * @param path - Path of the file, relative to the encrypted folder
1061
+ * @returns The decrypted content
1062
+ */
1063
+ async download(storageId, folderId, path) {
1064
+ const base64 = await this.client.request(
1065
+ ENCRYPTED_FOLDER_COMMANDS.download,
1066
+ { request: { storageId, folderId, path } }
1067
+ );
1068
+ return base64ToArrayBuffer(base64);
1069
+ }
1070
+ /**
1071
+ * Encrypt and upload a file (permission `readWrite`). Missing parent
1072
+ * folders are created.
1073
+ * @param storageId - Storage the folder lies in
1074
+ * @param folderId - Folder from {@link EncryptedFoldersAPI.choose}
1075
+ * @param path - Path of the file, relative to the encrypted folder
1076
+ * @param data - Content of the file
1077
+ */
1078
+ async upload(storageId, folderId, path, data) {
1079
+ await this.client.request(ENCRYPTED_FOLDER_COMMANDS.upload, {
1080
+ request: { storageId, folderId, path, data: arrayBufferToBase64(data) }
1081
+ });
1082
+ }
1083
+ /**
1084
+ * Delete a file or folder (permission `readWrite`).
1085
+ * @param storageId - Storage the folder lies in
1086
+ * @param folderId - Folder from {@link EncryptedFoldersAPI.choose}
1087
+ * @param path - Path relative to the encrypted folder
1088
+ */
1089
+ async delete(storageId, folderId, path) {
1090
+ await this.client.request(ENCRYPTED_FOLDER_COMMANDS.delete, {
1091
+ request: { storageId, folderId, path }
1092
+ });
1093
+ }
1094
+ };
1095
+
1005
1096
  // src/api/localsend.ts
1006
1097
  var LOCALSEND_EVENTS = {
1007
1098
  /** New device discovered */
@@ -1600,6 +1691,195 @@ var DialogAPI = class {
1600
1691
  }
1601
1692
  };
1602
1693
 
1694
+ // src/api/tools/schema.ts
1695
+ function fail(field, message) {
1696
+ return { ok: false, field, message };
1697
+ }
1698
+ function typeMatches(type, value) {
1699
+ switch (type) {
1700
+ case "object":
1701
+ return typeof value === "object" && value !== null && !Array.isArray(value);
1702
+ case "array":
1703
+ return Array.isArray(value);
1704
+ case "integer":
1705
+ return Number.isInteger(value);
1706
+ case "number":
1707
+ return typeof value === "number" && Number.isFinite(value);
1708
+ case "string":
1709
+ return typeof value === "string";
1710
+ case "boolean":
1711
+ return typeof value === "boolean";
1712
+ default:
1713
+ return false;
1714
+ }
1715
+ }
1716
+ function validateAt(schema, value, field) {
1717
+ if (!typeMatches(schema.type, value)) return fail(field, `expected ${String(schema.type)}`);
1718
+ if (schema.enum && !schema.enum.includes(value)) {
1719
+ return fail(field, `expected one of ${schema.enum.join(", ")}`);
1720
+ }
1721
+ if (schema.type === "array" && schema.items) {
1722
+ const items = value;
1723
+ for (let i = 0; i < items.length; i++) {
1724
+ const result = validateAt(schema.items, items[i], `${field}[${i}]`);
1725
+ if (!result.ok) return result;
1726
+ }
1727
+ }
1728
+ if (schema.type === "object") {
1729
+ const record = value;
1730
+ const properties = schema.properties ?? {};
1731
+ const prefix = field === "" ? "" : `${field}.`;
1732
+ if (schema.properties) {
1733
+ for (const key of Object.keys(record)) {
1734
+ if (!Object.hasOwn(properties, key)) return fail(`${prefix}${key}`, "unknown property");
1735
+ }
1736
+ }
1737
+ for (const key of schema.required ?? []) {
1738
+ if (record[key] === void 0) return fail(`${prefix}${key}`, "required");
1739
+ }
1740
+ for (const [key, child] of Object.entries(properties)) {
1741
+ if (record[key] === void 0) continue;
1742
+ const result = validateAt(child, record[key], `${prefix}${key}`);
1743
+ if (!result.ok) return result;
1744
+ }
1745
+ }
1746
+ return { ok: true };
1747
+ }
1748
+ function validateInput(schema, value) {
1749
+ return validateAt(schema, value, "");
1750
+ }
1751
+
1752
+ // src/api/tools/server.ts
1753
+ var PROTOCOL_VERSION = "2025-11-25";
1754
+ var METHOD_NOT_FOUND = -32601;
1755
+ var INVALID_PARAMS = -32602;
1756
+ function textResult(text, isError) {
1757
+ return { content: [{ type: "text", text }], isError };
1758
+ }
1759
+ function asText(value) {
1760
+ if (typeof value === "string") return value;
1761
+ if (value === void 0) return "";
1762
+ return JSON.stringify(value);
1763
+ }
1764
+ function isId(value) {
1765
+ return typeof value === "string" || typeof value === "number";
1766
+ }
1767
+ var ToolsServer = class {
1768
+ constructor(options) {
1769
+ this.options = options;
1770
+ this.running = /* @__PURE__ */ new Map();
1771
+ }
1772
+ /**
1773
+ * Handles one JSON-RPC message from the host. A tool call runs on its own and answers when it is
1774
+ * done, so a long call never holds up `ping` or the cancellation of that very call.
1775
+ */
1776
+ async receive(message) {
1777
+ if (typeof message !== "object" || message === null) return;
1778
+ const { id, method, params } = message;
1779
+ if (typeof method !== "string") return;
1780
+ const args = typeof params === "object" && params !== null ? params : {};
1781
+ if (!isId(id)) {
1782
+ if (method === "notifications/cancelled" && isId(args.requestId)) {
1783
+ this.running.get(args.requestId)?.abort();
1784
+ }
1785
+ return;
1786
+ }
1787
+ switch (method) {
1788
+ case "initialize":
1789
+ return this.reply(id, {
1790
+ protocolVersion: PROTOCOL_VERSION,
1791
+ capabilities: { tools: {} },
1792
+ serverInfo: {
1793
+ name: this.options.manifest?.name ?? "",
1794
+ version: this.options.manifest?.version ?? ""
1795
+ }
1796
+ });
1797
+ case "ping":
1798
+ return this.reply(id, {});
1799
+ case "tools/list": {
1800
+ const tools = this.tools().map(({ name, title, description, inputSchema }) => ({
1801
+ name,
1802
+ title: title.en,
1803
+ description,
1804
+ inputSchema
1805
+ }));
1806
+ return this.reply(id, { tools });
1807
+ }
1808
+ case "tools/call":
1809
+ void this.call(id, args);
1810
+ return;
1811
+ default:
1812
+ return this.error(id, METHOD_NOT_FOUND, method);
1813
+ }
1814
+ }
1815
+ /** The declared tools that have a handler. */
1816
+ tools() {
1817
+ return (this.options.manifest?.tools ?? []).filter((tool) => this.options.handlers.has(tool.name));
1818
+ }
1819
+ async call(id, params) {
1820
+ const tool = this.tools().find((candidate) => candidate.name === params.name);
1821
+ const handler = tool && this.options.handlers.get(tool.name);
1822
+ if (!tool || !handler) return this.error(id, INVALID_PARAMS, `unknown tool ${String(params.name)}`);
1823
+ const input = params.arguments ?? {};
1824
+ const checked = validateInput(tool.inputSchema, input);
1825
+ if (!checked.ok) {
1826
+ return this.reply(id, textResult(`invalid input: ${checked.field}: ${checked.message}`, true));
1827
+ }
1828
+ const controller = new AbortController();
1829
+ this.running.set(id, controller);
1830
+ let result;
1831
+ try {
1832
+ const output = await handler(input, { signal: controller.signal });
1833
+ result = textResult(asText(output), false);
1834
+ } catch (error) {
1835
+ result = textResult(error instanceof Error ? error.message : String(error), true);
1836
+ } finally {
1837
+ this.running.delete(id);
1838
+ }
1839
+ if (!controller.signal.aborted) this.reply(id, result);
1840
+ }
1841
+ reply(id, result) {
1842
+ this.options.send({ jsonrpc: "2.0", id, result });
1843
+ }
1844
+ error(id, code, message) {
1845
+ this.options.send({ jsonrpc: "2.0", id, error: { code, message } });
1846
+ }
1847
+ };
1848
+
1849
+ // src/api/tools/index.ts
1850
+ var ToolsAPI = class {
1851
+ constructor(options) {
1852
+ this.options = options;
1853
+ this.handlers = /* @__PURE__ */ new Map();
1854
+ const manifest = options.manifest;
1855
+ this.server = new ToolsServer({
1856
+ get manifest() {
1857
+ return manifest();
1858
+ },
1859
+ handlers: this.handlers,
1860
+ send: options.send
1861
+ });
1862
+ }
1863
+ /**
1864
+ * Runs `handler` when the host calls the tool `name`. The tool must be declared in the manifest.
1865
+ * Returns a function that unregisters the handler; the host then no longer sees the tool.
1866
+ */
1867
+ register(name, handler) {
1868
+ const declared = this.options.manifest()?.tools?.some((tool) => tool.name === name) ?? false;
1869
+ if (!declared) {
1870
+ throw new Error(`The tool ${name} is not declared in the manifest's tools`);
1871
+ }
1872
+ this.handlers.set(name, handler);
1873
+ return () => {
1874
+ if (this.handlers.get(name) === handler) this.handlers.delete(name);
1875
+ };
1876
+ }
1877
+ /** Handles one MCP message from the host. Called by the SDK for `haexspace:mcp` port messages. */
1878
+ receive(message) {
1879
+ return this.server.receive(message);
1880
+ }
1881
+ };
1882
+
1603
1883
  // src/messages.ts
1604
1884
  var HAEXSPACE_MESSAGE_TYPES = {
1605
1885
  /** Debug message for development/troubleshooting */
@@ -1622,7 +1902,14 @@ var HAEXSPACE_MESSAGE_TYPES = {
1622
1902
  *
1623
1903
  * Payload: `{ type: PORT_READY }` — no data.
1624
1904
  */
1625
- PORT_READY: "haexspace:port:ready"
1905
+ PORT_READY: "haexspace:port:ready",
1906
+ /**
1907
+ * One MCP message of the tool connection (holzi spec 018), in both directions over the
1908
+ * MessagePort: the host calls the extension's tools, `sdk.tools` answers.
1909
+ *
1910
+ * Payload: `{ type: MCP, message }`, where `message` is a JSON-RPC 2.0 object.
1911
+ */
1912
+ MCP: "haexspace:mcp"
1626
1913
  };
1627
1914
 
1628
1915
  // src/polyfills/consoleForwarding.ts
@@ -2068,7 +2355,11 @@ function createMessageHandler(config, pendingRequests, extensionInfo, onEvent) {
2068
2355
  }
2069
2356
  };
2070
2357
  }
2071
- function processEvent(event, log, eventListeners, onContextChanged, onExternalRequest, onActionRequest) {
2358
+ function processEvent(event, log, eventListeners, onContextChanged, onExternalRequest, onActionRequest, onMcpMessage) {
2359
+ if (event.type === HAEXSPACE_MESSAGE_TYPES.MCP) {
2360
+ onMcpMessage?.(event.message);
2361
+ return;
2362
+ }
2072
2363
  if (event.type === HAEXTENSION_EVENTS.CONTEXT_CHANGED) {
2073
2364
  const contextEvent = event;
2074
2365
  onContextChanged(contextEvent.data.context);
@@ -2334,6 +2625,7 @@ var HaexVaultSdk = class {
2334
2625
  this.web = new WebAPI(this);
2335
2626
  this.permissions = new PermissionsAPI(this);
2336
2627
  this.remoteStorage = new RemoteStorageAPI(this);
2628
+ this.encryptedFolders = new EncryptedFoldersAPI(this);
2337
2629
  this.localsend = new LocalSendAPI(this);
2338
2630
  this.spaces = new SpacesAPI(this);
2339
2631
  this.shell = new ShellAPI(this);
@@ -2342,6 +2634,10 @@ var HaexVaultSdk = class {
2342
2634
  this.notifications = new NotificationsAPI(this);
2343
2635
  this.tab = new TabAPI(this);
2344
2636
  this.dialog = new DialogAPI(this);
2637
+ this.tools = new ToolsAPI({
2638
+ manifest: () => this.config.manifest,
2639
+ send: (message) => this.sendMcpMessage(message)
2640
+ });
2345
2641
  installConsoleForwarding(this.config.debug);
2346
2642
  this.on(HAEXTENSION_EVENTS.PERMISSION_RESOLVED, (event) => {
2347
2643
  const data = event.data;
@@ -2649,9 +2945,18 @@ var HaexVaultSdk = class {
2649
2945
  this.notifySubscribersInternal();
2650
2946
  },
2651
2947
  (extEvent) => this.handleExternalRequestInternal(extEvent.data),
2652
- (actionEvent) => this.handleActionRequestInternal(actionEvent.data)
2948
+ (actionEvent) => this.handleActionRequestInternal(actionEvent.data),
2949
+ (message) => void this.tools.receive(message)
2653
2950
  );
2654
2951
  }
2952
+ /** Sends one MCP message of the tool connection to the host on the port. */
2953
+ sendMcpMessage(message) {
2954
+ if (!this.hostPort) {
2955
+ this.log("Dropped an MCP message: no host port yet");
2956
+ return;
2957
+ }
2958
+ this.hostPort.postMessage({ type: HAEXSPACE_MESSAGE_TYPES.MCP, message });
2959
+ }
2655
2960
  async handleExternalRequestInternal(request) {
2656
2961
  await handleExternalRequest(request, this.externalRequestHandlers, this.respondToExternalRequest.bind(this), this.log.bind(this));
2657
2962
  }