nanocodex 0.4.0 → 0.6.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 (196) hide show
  1. package/README.md +944 -78
  2. package/actions/events.mjs +14 -25
  3. package/actions/index.d.mts +1 -0
  4. package/actions/index.mjs +11 -1
  5. package/actions/session.d.mts +29 -1
  6. package/actions/session.mjs +22 -0
  7. package/actions/turn.d.mts +15 -6
  8. package/actions/turn.mjs +10 -0
  9. package/actions/voice.d.mts +26 -0
  10. package/actions/voice.mjs +54 -0
  11. package/browser/Agent.d.mts +41 -24
  12. package/browser/Agent.mjs +7 -84
  13. package/browser/ChatGptSubscription.d.mts +7 -0
  14. package/browser/ChatGptSubscription.mjs +19 -0
  15. package/browser/InlineAgent.mjs +267 -0
  16. package/browser/Transport.d.mts +80 -0
  17. package/browser/Transport.mjs +63 -0
  18. package/browser/Voice.d.mts +86 -0
  19. package/browser/Voice.mjs +316 -0
  20. package/browser/VoiceSession.mjs +763 -0
  21. package/browser/WorkerAgent.d.mts +65 -0
  22. package/browser/WorkerAgent.mjs +1467 -0
  23. package/browser/agent.worker.mjs +3 -0
  24. package/browser/config.d.mts +36 -0
  25. package/browser/config.mjs +439 -0
  26. package/browser/engine.mjs +13 -0
  27. package/browser/harness.mjs +55 -0
  28. package/browser/host.d.mts +33 -5
  29. package/browser/host.mjs +320 -26
  30. package/browser/hostManagedWebSocket.d.mts +14 -0
  31. package/browser/hostManagedWebSocket.mjs +110 -0
  32. package/browser/index.d.mts +64 -1
  33. package/browser/index.mjs +22 -1
  34. package/browser/indexeddb-durability-store.mjs +210 -0
  35. package/browser/workspace.d.mts +16 -0
  36. package/browser/workspace.mjs +107 -0
  37. package/cloud/Client.d.mts +83 -0
  38. package/cloud/Client.mjs +231 -0
  39. package/cloud/Decorator.d.mts +31 -0
  40. package/cloud/Decorator.mjs +37 -0
  41. package/cloud/Dialog.d.mts +113 -0
  42. package/cloud/Dialog.mjs +366 -0
  43. package/cloud/Errors.d.mts +22 -0
  44. package/cloud/Errors.mjs +39 -0
  45. package/cloud/Principal.d.mts +26 -0
  46. package/cloud/Principal.mjs +88 -0
  47. package/cloud/RemoteProvider.mjs +88 -0
  48. package/cloud/Transport.d.mts +47 -0
  49. package/cloud/Transport.mjs +454 -0
  50. package/cloud/actions/account.d.mts +9 -0
  51. package/cloud/actions/account.mjs +19 -0
  52. package/cloud/actions/agent.d.mts +18 -0
  53. package/cloud/actions/agent.mjs +429 -0
  54. package/cloud/actions/connection.d.mts +133 -0
  55. package/cloud/actions/connection.mjs +580 -0
  56. package/cloud/actions/grant.d.mts +10 -0
  57. package/cloud/actions/grant.mjs +12 -0
  58. package/cloud/actions/index.d.mts +7 -0
  59. package/cloud/actions/index.mjs +7 -0
  60. package/cloud/actions/machineUsd.d.mts +23 -0
  61. package/cloud/actions/machineUsd.mjs +37 -0
  62. package/cloud/actions/model.d.mts +12 -0
  63. package/cloud/actions/model.mjs +50 -0
  64. package/cloud/actions/mpp.d.mts +28 -0
  65. package/cloud/actions/mpp.mjs +39 -0
  66. package/cloud/index.d.mts +30 -0
  67. package/cloud/index.mjs +9 -0
  68. package/cloud/internal.mjs +533 -0
  69. package/cloud/mercator.mjs +108 -0
  70. package/cloud/server/HostPrincipal.d.mts +34 -0
  71. package/cloud/server/HostPrincipal.mjs +202 -0
  72. package/cloud/server/index.d.mts +1 -0
  73. package/cloud/server/index.mjs +1 -0
  74. package/cloud/types.d.mts +212 -0
  75. package/cloudflare/Agent.d.mts +136 -0
  76. package/cloudflare/Agent.mjs +784 -0
  77. package/cloudflare/egress-subject.mjs +19 -0
  78. package/cloudflare/egress.d.mts +41 -0
  79. package/cloudflare/egress.mjs +119 -0
  80. package/cloudflare/event-socket.mjs +316 -0
  81. package/cloudflare/index.d.mts +7 -0
  82. package/cloudflare/index.mjs +6 -0
  83. package/host/Agent.d.mts +55 -0
  84. package/host/Agent.mjs +1 -0
  85. package/host/index.d.mts +71 -0
  86. package/host/index.mjs +10 -0
  87. package/index.d.mts +46 -0
  88. package/index.mjs +11 -0
  89. package/internal.mjs +673 -61
  90. package/managed/Agent.d.mts +459 -0
  91. package/managed/Agent.mjs +1824 -0
  92. package/managed/ManagedError.d.mts +9 -0
  93. package/managed/ManagedError.mjs +8 -0
  94. package/managed/README.md +244 -0
  95. package/managed/Voice.mjs +209 -0
  96. package/managed/index.d.mts +22 -0
  97. package/managed/index.mjs +2 -0
  98. package/managed/internal.mjs +119 -0
  99. package/node/Agent.d.mts +35 -8
  100. package/node/Agent.mjs +116 -25
  101. package/node/ChatGptSubscription.d.mts +9 -0
  102. package/node/ChatGptSubscription.mjs +17 -0
  103. package/node/Transport.d.mts +41 -0
  104. package/node/Transport.mjs +46 -0
  105. package/node/host.mjs +99 -17
  106. package/node/index.d.mts +45 -1
  107. package/node/index.mjs +16 -1
  108. package/node/workspace.d.mts +10 -0
  109. package/node/workspace.mjs +198 -0
  110. package/package.json +145 -11
  111. package/pkg-node/nanocodex.d.ts +523 -11
  112. package/pkg-node/nanocodex.js +2038 -179
  113. package/pkg-node/nanocodex_bg.wasm.d.ts +100 -29
  114. package/pkg-web/.nanocodex-bindgen-stamp +6 -0
  115. package/pkg-web/nanocodex-build.json +1 -0
  116. package/pkg-web/nanocodex.d.ts +623 -40
  117. package/pkg-web/nanocodex.js +2031 -178
  118. package/pkg-web/nanocodex_bg.js +2667 -0
  119. package/pkg-web/nanocodex_bg.wasm +0 -0
  120. package/pkg-web/nanocodex_bg.wasm.d.ts +100 -29
  121. package/pkg-web/nanocodex_worker.js +9 -0
  122. package/runtime/chatgpt-subscription.mjs +212 -0
  123. package/runtime/cloudflare-durability-store.d.mts +26 -0
  124. package/runtime/cloudflare-durability-store.mjs +14 -0
  125. package/runtime/code-evaluator.worker.mjs +137 -0
  126. package/runtime/code-runtime.mjs +1 -243
  127. package/runtime/durability-store.d.mts +85 -0
  128. package/runtime/durability-store.mjs +752 -0
  129. package/runtime/durability.mjs +174 -0
  130. package/runtime/managed-transport.mjs +461 -0
  131. package/runtime/mcp-runtime.mjs +534 -0
  132. package/runtime/postgres-durability-store.d.mts +64 -0
  133. package/runtime/postgres-durability-store.mjs +581 -0
  134. package/runtime/quickjs-evaluator.d.mts +17 -0
  135. package/runtime/quickjs-evaluator.mjs +236 -0
  136. package/runtime/response-controls.mjs +42 -0
  137. package/runtime/response-lanes.mjs +72 -0
  138. package/runtime/responses-transport.mjs +22 -0
  139. package/runtime/subagents.d.mts +107 -0
  140. package/runtime/subagents.mjs +54 -0
  141. package/runtime/subscription-store.d.mts +8 -0
  142. package/runtime/subscription-store.mjs +42 -0
  143. package/runtime/tempo-provider.d.mts +125 -0
  144. package/runtime/tempo-provider.mjs +264 -0
  145. package/runtime/tool-configuration.mjs +1 -0
  146. package/runtime/tool-router.mjs +1 -0
  147. package/runtime/utf8.mjs +1 -0
  148. package/runtime/worker-evaluator.mjs +162 -0
  149. package/runtime/workspace.d.mts +1 -0
  150. package/runtime/workspace.mjs +1 -0
  151. package/tools/Tools.d.mts +56 -0
  152. package/tools/Tools.mjs +136 -0
  153. package/tools/artifact.d.mts +1 -0
  154. package/tools/artifact.mjs +1 -0
  155. package/tools/attachment.mjs +1 -0
  156. package/tools/bash.d.mts +1 -0
  157. package/tools/bash.mjs +1 -0
  158. package/tools/browser/accountInfo.mjs +795 -0
  159. package/tools/browser/browserBuffer.mjs +18 -0
  160. package/tools/browser/browserCompiler.mjs +155 -0
  161. package/tools/browser/browserEgress.mjs +161 -0
  162. package/tools/browser/browserPython.mjs +133 -0
  163. package/tools/browser/browserShell.mjs +1075 -0
  164. package/tools/browser/browserSprintf.mjs +4 -0
  165. package/tools/browser/browserSsh.mjs +201 -0
  166. package/tools/browser/browserZlib.mjs +136 -0
  167. package/tools/browser/compiler.worker.mjs +160 -0
  168. package/tools/browser/devTunnelsSshBrowser.mjs +13 -0
  169. package/tools/browser/index.d.mts +209 -0
  170. package/tools/browser/index.mjs +174 -0
  171. package/tools/browser/opfsGit.mjs +248 -0
  172. package/tools/browser/python.worker.mjs +114 -0
  173. package/tools/browser/threadGit.mjs +197 -0
  174. package/tools/browser/unsupportedNodeRsa.mjs +5 -0
  175. package/tools/browser/workspace.mjs +83 -0
  176. package/tools/dataset.d.mts +1 -0
  177. package/tools/dataset.mjs +1 -0
  178. package/tools/datasetContract.mjs +1 -0
  179. package/tools/datasetEngine.mjs +1 -0
  180. package/tools/hostedCatalog.d.mts +1 -0
  181. package/tools/hostedCatalog.mjs +1 -0
  182. package/tools/index.d.mts +49 -0
  183. package/tools/index.mjs +19 -0
  184. package/tools/namedTool.mjs +1 -0
  185. package/tools/repository-workspace.d.mts +1 -0
  186. package/tools/repository-workspace.mjs +1 -0
  187. package/tools/ssh.d.mts +1 -0
  188. package/tools/ssh.mjs +1 -0
  189. package/tools/standard.mjs +1 -0
  190. package/tools/standardDescriptions.mjs +1 -0
  191. package/types.d.mts +431 -16
  192. package/worker/ChatGptSubscription.d.mts +9 -0
  193. package/worker/ChatGptSubscription.mjs +29 -0
  194. package/worker/index.d.mts +18 -0
  195. package/worker/index.mjs +5 -0
  196. package/pkg-node/nanocodex_bg.wasm +0 -0
@@ -0,0 +1,31 @@
1
+ import type { logout } from "./actions/account.mjs";
2
+ import type { create as createAgent } from "./actions/agent.mjs";
3
+ import type { connect, disconnect, reconnect } from "./actions/connection.mjs";
4
+ import type { revoke } from "./actions/grant.mjs";
5
+ import type { fund, getConfig } from "./actions/machineUsd.mjs";
6
+ import type { transport as modelTransport } from "./actions/model.mjs";
7
+ import type { charge, getBalance } from "./actions/mpp.mjs";
8
+ import type { Client } from "./Client.mjs";
9
+ import type { Connection, HostConnection } from "./types.mjs";
10
+
11
+ export type ConnectActions<connection extends Connection | HostConnection = Connection> = {
12
+ account: { logout(): logout.ReturnType };
13
+ agent: { create(options: createAgent.Options): Promise<createAgent.ReturnType> };
14
+ connection: {
15
+ connect(options: connect.Options): Promise<connection>;
16
+ disconnect(options?: disconnect.Options | undefined): disconnect.ReturnType;
17
+ reconnect(options?: reconnect.Options | undefined): Promise<connection | undefined>;
18
+ };
19
+ grant: { revoke(options: revoke.Options): revoke.ReturnType };
20
+ machineUsd: {
21
+ fund(options: fund.Options): fund.ReturnType;
22
+ getConfig(options?: getConfig.Options | undefined): getConfig.ReturnType;
23
+ };
24
+ model: { transport(options: modelTransport.Options): modelTransport.ReturnType };
25
+ mpp: {
26
+ charge(options: charge.Options): charge.ReturnType;
27
+ getBalance(options: getBalance.Options): getBalance.ReturnType;
28
+ };
29
+ };
30
+
31
+ export function connectActions(): (client: Client) => ConnectActions;
@@ -0,0 +1,37 @@
1
+ import * as account from "./actions/account.mjs";
2
+ import * as agent from "./actions/agent.mjs";
3
+ import * as connection from "./actions/connection.mjs";
4
+ import * as grant from "./actions/grant.mjs";
5
+ import * as machineUsd from "./actions/machineUsd.mjs";
6
+ import * as model from "./actions/model.mjs";
7
+ import * as mpp from "./actions/mpp.mjs";
8
+
9
+ export function connectActions() {
10
+ return (client) => ({
11
+ account: {
12
+ logout: () => account.logout(client),
13
+ },
14
+ agent: {
15
+ create: (options) => agent.create(client, options),
16
+ },
17
+ connection: {
18
+ connect: (options) => connection.connect(client, options),
19
+ disconnect: (options) => connection.disconnect(client, options),
20
+ reconnect: (options) => connection.reconnect(client, options),
21
+ },
22
+ grant: {
23
+ revoke: (options) => grant.revoke(client, options),
24
+ },
25
+ machineUsd: {
26
+ fund: (options) => machineUsd.fund(client, options),
27
+ getConfig: (options) => machineUsd.getConfig(client, options),
28
+ },
29
+ model: {
30
+ transport: (options) => model.transport(client, options),
31
+ },
32
+ mpp: {
33
+ charge: (options) => mpp.charge(client, options),
34
+ getBalance: (options) => mpp.getBalance(client, options),
35
+ },
36
+ });
37
+ }
@@ -0,0 +1,113 @@
1
+ export const DEFAULT_HOST: "https://nanocodex.gakonst.workers.dev/connect-dialog/";
2
+
3
+ export type Request = ConnectionRequest | FundingRequest;
4
+
5
+ export type ConnectionRequest = Readonly<{
6
+ id: string;
7
+ type: "connect";
8
+ app: Readonly<{ id: string; name: string; origin: string }>;
9
+ accountAddress: `0x${string}`;
10
+ auth: Readonly<{
11
+ /** EIP-4361 message whose exact bytes are bound by the access-key witness. */
12
+ message: string;
13
+ /** Connect capabilities echoed into the SIWE Resources field. */
14
+ resources: readonly string[];
15
+ }>;
16
+ permission: Readonly<{
17
+ id: string;
18
+ title: string;
19
+ description: string;
20
+ connectors: readonly Readonly<{ id: string; name: string; detail: string }>[];
21
+ }>;
22
+ accessKey: Readonly<{
23
+ address: `0x${string}`;
24
+ chainId: bigint;
25
+ keyId: `0x${string}`;
26
+ publicKey: `0x${string}`;
27
+ keyType: "secp256k1" | "p256" | "webAuthn";
28
+ witness: `0x${string}`;
29
+ expiry: number;
30
+ limits: readonly Readonly<{
31
+ token: `0x${string}`;
32
+ limit: bigint;
33
+ period?: number | undefined;
34
+ }>[];
35
+ scopes: readonly Readonly<{
36
+ address: `0x${string}`;
37
+ selector?: `0x${string}` | string | undefined;
38
+ recipients?: readonly `0x${string}`[] | undefined;
39
+ }>[];
40
+ }>;
41
+ mpp?: Readonly<{
42
+ token: `0x${string}`;
43
+ symbol: string;
44
+ limit: bigint;
45
+ period: number;
46
+ maxPerRequest: bigint;
47
+ /** Exact recipient when the delegated payment authority is recipient-bound. */
48
+ recipient?: `0x${string}` | undefined;
49
+ }> | undefined;
50
+ }>;
51
+
52
+ export type FundingRequest = Readonly<{
53
+ id: string;
54
+ type: "machineUsdFund";
55
+ accountAddress?: `0x${string}` | undefined;
56
+ grantId: `0x${string}`;
57
+ chainId: number;
58
+ apiUrl: string;
59
+ stripePublishableKey: string;
60
+ tokenAddress: `0x${string}`;
61
+ usdAmountCents: number;
62
+ }>;
63
+
64
+ export type Instance = Readonly<{
65
+ host: string;
66
+ open(request: Request): Promise<unknown>;
67
+ /** Internal WATA iframe target used by the bundled Accounts provider. */
68
+ walletTarget?(options: Readonly<{ host?: string | undefined }>): Window | null | undefined;
69
+ /** Resolves after the bundled Accounts iframe has completed navigation. */
70
+ waitForWallet?(): Promise<void>;
71
+ showWallet?(): void;
72
+ hideWallet?(): void;
73
+ /** Replaces the cached hosted wallet after account logout. */
74
+ resetWallet?(): Promise<void>;
75
+ getRequest?(): Request | undefined;
76
+ subscribe?(listener: () => void): () => void;
77
+ respond?(result: unknown): void;
78
+ reject?(error?: unknown): void;
79
+ }>;
80
+
81
+ export type Dialog<type extends string = string> = Readonly<{
82
+ key: string;
83
+ name: string;
84
+ type: type;
85
+ setup(options: Readonly<{ appId: string }>): Instance;
86
+ }>;
87
+
88
+ export function from<const type extends string>(parameters: Readonly<{
89
+ key: string;
90
+ name: string;
91
+ type: type;
92
+ setup(options: Readonly<{ appId: string }>): Instance;
93
+ }>): Dialog<type>;
94
+
95
+ export function iframe(options?: Readonly<{
96
+ host?: string | undefined;
97
+ key?: string | undefined;
98
+ name?: string | undefined;
99
+ }>): Dialog<"iframe">;
100
+
101
+ export function popup(options?: Readonly<{
102
+ host?: string | undefined;
103
+ key?: string | undefined;
104
+ name?: string | undefined;
105
+ target?: string | undefined;
106
+ features?: string | undefined;
107
+ }>): Dialog<"popup">;
108
+
109
+ export function memory(options?: Readonly<{
110
+ host?: string | undefined;
111
+ key?: string | undefined;
112
+ name?: string | undefined;
113
+ }>): Dialog<"memory">;
@@ -0,0 +1,366 @@
1
+ import { DialogBusyError, UserRejectedRequestError } from "./Errors.mjs";
2
+
3
+ export const DEFAULT_HOST = "https://nanocodex.gakonst.workers.dev/connect-dialog/";
4
+
5
+ const iframeInstances = new Map();
6
+ const popupInstances = new Map();
7
+
8
+ export function from(parameters) {
9
+ if (!parameters || typeof parameters !== "object") {
10
+ throw new TypeError("Dialog.from requires parameters");
11
+ }
12
+ if (typeof parameters.setup !== "function") {
13
+ throw new TypeError("Dialog.from requires setup");
14
+ }
15
+ return Object.freeze({
16
+ key: requiredString(parameters.key, "dialog key"),
17
+ name: requiredString(parameters.name, "dialog name"),
18
+ type: requiredString(parameters.type, "dialog type"),
19
+ setup: parameters.setup,
20
+ });
21
+ }
22
+
23
+ export function iframe(options = {}) {
24
+ const host = new URL(options.host ?? DEFAULT_HOST).toString();
25
+ return from({
26
+ key: options.key ?? "nanocodex-iframe",
27
+ name: options.name ?? "Nanocodex Connect",
28
+ type: "iframe",
29
+ setup({ appId }) {
30
+ if (typeof document === "undefined" || typeof window === "undefined") {
31
+ return {
32
+ host,
33
+ async open() {
34
+ throw new Error("The Nanocodex iframe dialog requires a browser");
35
+ },
36
+ };
37
+ }
38
+ const source = dialogUrl(host, "iframe", appId);
39
+ let instance = iframeInstances.get(source);
40
+ if (!instance) {
41
+ instance = createIframeInstance(source);
42
+ iframeInstances.set(source, instance);
43
+ }
44
+ return instance;
45
+ },
46
+ });
47
+ }
48
+
49
+ export function popup(options = {}) {
50
+ const host = new URL(options.host ?? DEFAULT_HOST).toString();
51
+ return from({
52
+ key: options.key ?? "nanocodex-popup",
53
+ name: options.name ?? "Nanocodex Connect",
54
+ type: "popup",
55
+ setup({ appId }) {
56
+ if (typeof window === "undefined") {
57
+ return {
58
+ host,
59
+ async open() {
60
+ throw new Error("The Nanocodex popup dialog requires a browser");
61
+ },
62
+ };
63
+ }
64
+ const source = dialogUrl(host, "popup", appId);
65
+ let instance = popupInstances.get(source);
66
+ if (!instance) {
67
+ instance = createPopupInstance(source, options);
68
+ popupInstances.set(source, instance);
69
+ }
70
+ return instance;
71
+ },
72
+ });
73
+ }
74
+
75
+ export function memory(options = {}) {
76
+ return from({
77
+ key: options.key ?? "nanocodex-memory",
78
+ name: options.name ?? "Nanocodex Connect playground",
79
+ type: "memory",
80
+ setup() {
81
+ const listeners = new Set();
82
+ let pending;
83
+ let snapshot;
84
+
85
+ function publish(value) {
86
+ snapshot = value;
87
+ for (const listener of [...listeners]) listener();
88
+ }
89
+
90
+ return {
91
+ host: options.host ?? "https://connect.nanocodex.xyz",
92
+ open(request) {
93
+ if (pending) return Promise.reject(new DialogBusyError());
94
+ return new Promise((resolve, reject) => {
95
+ pending = { reject, request, resolve };
96
+ publish(Object.freeze(request));
97
+ });
98
+ },
99
+ getRequest() {
100
+ return snapshot;
101
+ },
102
+ subscribe(listener) {
103
+ if (typeof listener !== "function") {
104
+ throw new TypeError("dialog subscription requires a listener");
105
+ }
106
+ listeners.add(listener);
107
+ return () => listeners.delete(listener);
108
+ },
109
+ respond(result) {
110
+ if (!pending) throw new Error("The Nanocodex dialog has no pending request");
111
+ const current = pending;
112
+ pending = undefined;
113
+ publish(undefined);
114
+ current.resolve(result);
115
+ },
116
+ reject(error = new UserRejectedRequestError()) {
117
+ if (!pending) throw new Error("The Nanocodex dialog has no pending request");
118
+ const current = pending;
119
+ pending = undefined;
120
+ publish(undefined);
121
+ current.reject(error);
122
+ },
123
+ };
124
+ },
125
+ });
126
+ }
127
+
128
+ function createIframeInstance(host) {
129
+ let active;
130
+ let frame;
131
+ let modal;
132
+ let ready;
133
+ let walletFrame;
134
+ let walletHost;
135
+ let walletModal;
136
+ let walletReady;
137
+ let walletStale = false;
138
+ let walletVisible = false;
139
+
140
+ function mount() {
141
+ if (frame && modal) return;
142
+ modal = document.createElement("dialog");
143
+ modal.setAttribute("aria-label", "Nanocodex Connect permissions");
144
+ modal.style.cssText = "border:0;padding:0;background:transparent;max-width:none;max-height:none";
145
+ modal.addEventListener("cancel", (event) => {
146
+ event.preventDefault();
147
+ rejectActive(new UserRejectedRequestError());
148
+ });
149
+ modal.addEventListener("close", () => {
150
+ if (active) rejectActive(new UserRejectedRequestError());
151
+ });
152
+ frame = document.createElement("iframe");
153
+ frame.dataset.testid = "nanocodex-connect-dialog";
154
+ frame.title = "Nanocodex Connect permissions";
155
+ frame.src = host;
156
+ frame.tabIndex = -1;
157
+ frame.setAttribute("inert", "");
158
+ const dialogOrigin = new URL(host).origin;
159
+ frame.allow = [
160
+ `publickey-credentials-get ${dialogOrigin}`,
161
+ `publickey-credentials-create ${dialogOrigin}`,
162
+ "payment",
163
+ ].join("; ");
164
+ frame.style.cssText = "border:0;width:min(440px,calc(100vw - 24px));height:min(720px,calc(100vh - 24px));background:#161616";
165
+ frame.style.display = "none";
166
+ modal.append(frame);
167
+ document.body.append(modal);
168
+ ready = new Promise((resolve) => frame.addEventListener("load", resolve, { once: true }));
169
+ }
170
+
171
+ function rejectActive(error) {
172
+ if (!active) return;
173
+ const current = active;
174
+ active = undefined;
175
+ if (modal?.open) modal.close();
176
+ frame.tabIndex = -1;
177
+ frame.setAttribute("inert", "");
178
+ frame.style.display = "none";
179
+ current.reject(error);
180
+ }
181
+
182
+ function onMessage(event) {
183
+ if (!active || event.origin !== new URL(host).origin || event.source !== frame?.contentWindow) return;
184
+ const message = event.data;
185
+ if (!message || message.type !== "nanocodex:response" || message.id !== active.request.id) return;
186
+ const current = active;
187
+ active = undefined;
188
+ modal?.close();
189
+ frame.tabIndex = -1;
190
+ frame.setAttribute("inert", "");
191
+ frame.style.display = "none";
192
+ if (message.error) {
193
+ current.reject(new UserRejectedRequestError(message.error.message));
194
+ return;
195
+ }
196
+ current.resolve(message.result);
197
+ }
198
+
199
+ window.addEventListener("message", onMessage);
200
+
201
+ function mountWallet(nextHost) {
202
+ const url = new URL(nextHost ?? host);
203
+ if (!url.searchParams.has("origin")) url.searchParams.set("origin", window.location.origin);
204
+ url.searchParams.set("mode", "iframe");
205
+ const source = url.toString();
206
+ if (walletFrame && walletModal && walletHost === source) return;
207
+ walletModal?.remove();
208
+ walletModal = document.createElement("dialog");
209
+ walletModal.dataset.nanocodexConnectWallet = "";
210
+ walletModal.setAttribute("aria-label", "Nanocodex Connect");
211
+ walletModal.style.cssText = "border:0;outline:0;padding:0;background:transparent;max-width:none;max-height:none;width:100vw;height:100vh";
212
+ walletModal.addEventListener("cancel", (event) => event.preventDefault());
213
+ const style = document.createElement("style");
214
+ style.textContent = "dialog[data-nanocodex-connect-wallet]::backdrop{background:transparent}";
215
+ walletFrame = document.createElement("iframe");
216
+ walletFrame.dataset.testid = "nanocodex-connect-wallet";
217
+ walletFrame.title = "Nanocodex Connect";
218
+ walletReady = new Promise((resolve) => {
219
+ walletFrame.addEventListener("load", resolve, { once: true });
220
+ });
221
+ walletFrame.src = source;
222
+ const dialogOrigin = url.origin;
223
+ walletFrame.allow = [
224
+ `publickey-credentials-get ${dialogOrigin}`,
225
+ `publickey-credentials-create ${dialogOrigin}`,
226
+ "clipboard-write",
227
+ "payment",
228
+ ].join("; ");
229
+ walletFrame.style.cssText = "border:0;position:fixed;inset:0;width:100%;height:100%;background:transparent;color-scheme:light dark";
230
+ setWalletInteractive(walletVisible);
231
+ walletModal.append(style, walletFrame);
232
+ document.body.append(walletModal);
233
+ walletHost = source;
234
+ walletStale = false;
235
+ if (walletVisible) walletModal.showModal();
236
+ }
237
+
238
+ async function refreshStaleWallet() {
239
+ if (!walletStale) return;
240
+ walletModal?.remove();
241
+ walletFrame = undefined;
242
+ walletHost = undefined;
243
+ walletModal = undefined;
244
+ walletReady = undefined;
245
+ mountWallet(host);
246
+ await walletReady;
247
+ }
248
+
249
+ function showWallet() {
250
+ walletVisible = true;
251
+ setWalletInteractive(true);
252
+ if (walletModal && !walletModal.open) walletModal.showModal();
253
+ walletFrame?.focus();
254
+ }
255
+
256
+ function hideWallet() {
257
+ walletVisible = false;
258
+ if (walletModal?.open) walletModal.close();
259
+ setWalletInteractive(false);
260
+ }
261
+
262
+ async function resetWallet() {
263
+ walletVisible = false;
264
+ if (walletModal?.open) walletModal.close();
265
+ setWalletInteractive(false);
266
+ // The hosted account is still finishing its own logout when the Wata
267
+ // response reaches the parent. Keep that document alive until the next
268
+ // connect, then replace it before creating the next Provider session.
269
+ walletStale = true;
270
+ }
271
+
272
+ function setWalletInteractive(visible) {
273
+ if (!walletFrame) return;
274
+ walletFrame.tabIndex = visible ? 0 : -1;
275
+ walletFrame.style.display = visible ? "block" : "none";
276
+ if (visible) walletFrame.removeAttribute("inert");
277
+ else walletFrame.setAttribute("inert", "");
278
+ }
279
+
280
+ // Keep the hosted wallet warm from client creation. The iframe remains
281
+ // hidden and inert until a request is made, but its Accounts/WebAuthn
282
+ // runtime can load while the embedding app is already useful.
283
+ mountWallet(host);
284
+
285
+ return {
286
+ host,
287
+ hideWallet,
288
+ resetWallet,
289
+ async open(request) {
290
+ if (active) throw new DialogBusyError();
291
+ mount();
292
+ await ready;
293
+ return new Promise((resolve, reject) => {
294
+ active = { reject, request, resolve };
295
+ frame.removeAttribute("inert");
296
+ frame.tabIndex = 0;
297
+ frame.style.display = "block";
298
+ modal.showModal();
299
+ frame.contentWindow.postMessage({
300
+ type: "nanocodex:request",
301
+ id: request.id,
302
+ request,
303
+ }, new URL(host).origin);
304
+ });
305
+ },
306
+ showWallet,
307
+ async waitForWallet() {
308
+ await refreshStaleWallet();
309
+ await walletReady;
310
+ },
311
+ walletTarget(options = {}) {
312
+ mountWallet(options.host);
313
+ if (walletVisible && walletModal && !walletModal.open) walletModal.showModal();
314
+ return walletFrame?.contentWindow;
315
+ },
316
+ };
317
+ }
318
+
319
+ function createPopupInstance(host, options) {
320
+ const source = host;
321
+ const targetName = options.target ?? "nanocodex-connect";
322
+ const features = options.features ?? "popup=yes,width=440,height=720,resizable=yes,scrollbars=yes";
323
+ let walletWindow;
324
+
325
+ function showWallet() {
326
+ if (!walletWindow || walletWindow.closed) {
327
+ walletWindow = window.open(source, targetName, features);
328
+ }
329
+ if (!walletWindow) {
330
+ throw new Error("Nanocodex Connect was blocked. Allow the popup and try again.");
331
+ }
332
+ walletWindow.focus?.();
333
+ }
334
+
335
+ function hideWallet() {
336
+ if (walletWindow && !walletWindow.closed) walletWindow.close();
337
+ walletWindow = undefined;
338
+ }
339
+
340
+ return {
341
+ host: source,
342
+ async open() {
343
+ throw new Error("The Nanocodex popup supports account authorization only");
344
+ },
345
+ showWallet,
346
+ hideWallet,
347
+ walletTarget() {
348
+ return walletWindow && !walletWindow.closed ? walletWindow : undefined;
349
+ },
350
+ };
351
+ }
352
+
353
+ function dialogUrl(host, mode, appId) {
354
+ const url = new URL(host);
355
+ url.searchParams.set("app_id", appId);
356
+ url.searchParams.set("origin", window.location.origin);
357
+ url.searchParams.set("mode", mode);
358
+ return url.toString();
359
+ }
360
+
361
+ function requiredString(value, label) {
362
+ if (typeof value !== "string" || value.length === 0) {
363
+ throw new TypeError(`${label} must be a non-empty string`);
364
+ }
365
+ return value;
366
+ }
@@ -0,0 +1,22 @@
1
+ export declare class BaseError extends Error {
2
+ readonly code: string;
3
+ readonly status: number | undefined;
4
+ readonly metaMessages: readonly string[];
5
+ constructor(message: string, options?: Readonly<{
6
+ cause?: unknown;
7
+ code?: string | undefined;
8
+ status?: number | undefined;
9
+ metaMessages?: readonly string[] | undefined;
10
+ }>);
11
+ }
12
+
13
+ export declare class HttpError extends BaseError {
14
+ constructor(status: number, message: string, options?: Readonly<{
15
+ cause?: unknown;
16
+ code?: string | undefined;
17
+ }>);
18
+ }
19
+
20
+ export declare class InvalidResponseError extends BaseError {}
21
+ export declare class UserRejectedRequestError extends BaseError {}
22
+ export declare class DialogBusyError extends BaseError {}
@@ -0,0 +1,39 @@
1
+ export class BaseError extends Error {
2
+ constructor(message, options = {}) {
3
+ super(message, options.cause === undefined ? undefined : { cause: options.cause });
4
+ this.name = "Connect.BaseError";
5
+ this.code = options.code ?? "connect_error";
6
+ this.status = options.status;
7
+ this.metaMessages = Object.freeze([...(options.metaMessages ?? [])]);
8
+ }
9
+ }
10
+
11
+ export class HttpError extends BaseError {
12
+ constructor(status, message, options = {}) {
13
+ super(message, { ...options, code: options.code ?? "http_error", status });
14
+ this.name = "Connect.HttpError";
15
+ }
16
+ }
17
+
18
+ export class InvalidResponseError extends BaseError {
19
+ constructor(message, options = {}) {
20
+ super(message, { ...options, code: "invalid_response" });
21
+ this.name = "Connect.InvalidResponseError";
22
+ }
23
+ }
24
+
25
+ export class UserRejectedRequestError extends BaseError {
26
+ constructor(message = "The Nanocodex request was rejected.", options = {}) {
27
+ super(message, { ...options, code: "user_rejected_request" });
28
+ this.name = "Connect.UserRejectedRequestError";
29
+ }
30
+ }
31
+
32
+ export class DialogBusyError extends BaseError {
33
+ constructor() {
34
+ super("The Nanocodex dialog is already handling a request.", {
35
+ code: "dialog_busy",
36
+ });
37
+ this.name = "Connect.DialogBusyError";
38
+ }
39
+ }
@@ -0,0 +1,26 @@
1
+ export type Exchange = Readonly<{ token: string; expiresAt: number }>;
2
+
3
+ export type Instance = Readonly<{
4
+ create(options: Readonly<{
5
+ resources: readonly string[];
6
+ signal?: AbortSignal | undefined;
7
+ }>): Promise<Exchange>;
8
+ }>;
9
+
10
+ export type Principal<type extends string = string> = Readonly<{
11
+ key: string;
12
+ name: string;
13
+ type: type;
14
+ setup(context: Readonly<{
15
+ appId: string;
16
+ appOrigin: string | undefined;
17
+ }>): Instance;
18
+ }>;
19
+
20
+ export function host(options?: Readonly<{
21
+ /** Same-origin application route that verifies the current host login. */
22
+ url?: string | URL | undefined;
23
+ fetch?: typeof globalThis.fetch | undefined;
24
+ key?: string | undefined;
25
+ name?: string | undefined;
26
+ }>): Principal<"host">;