@lamplitisles/dsh-nanocodex 0.1.0-beta.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 (276) hide show
  1. package/LICENSE +65 -0
  2. package/NOTICE +2 -0
  3. package/README.md +182 -0
  4. package/THIRD_PARTY_NOTICES.md +29 -0
  5. package/cordis.patch.yml +52 -0
  6. package/dist/index.d.ts +435 -0
  7. package/dist/index.js +3405 -0
  8. package/dist/standard-preset.d.ts +7 -0
  9. package/dist/standard-preset.js +25 -0
  10. package/engine-release.json +14 -0
  11. package/package.json +119 -0
  12. package/presets/standard/agent.cordis.yml +6 -0
  13. package/presets/standard/preset.yml +2 -0
  14. package/vendor/nanocodex/README.md +1344 -0
  15. package/vendor/nanocodex/actions/events.d.mts +10 -0
  16. package/vendor/nanocodex/actions/events.mjs +163 -0
  17. package/vendor/nanocodex/actions/index.d.mts +13 -0
  18. package/vendor/nanocodex/actions/index.mjs +35 -0
  19. package/vendor/nanocodex/actions/session.d.mts +66 -0
  20. package/vendor/nanocodex/actions/session.mjs +59 -0
  21. package/vendor/nanocodex/actions/turn.d.mts +59 -0
  22. package/vendor/nanocodex/actions/turn.mjs +37 -0
  23. package/vendor/nanocodex/actions/voice.d.mts +22 -0
  24. package/vendor/nanocodex/actions/voice.mjs +50 -0
  25. package/vendor/nanocodex/browser/Agent.d.mts +55 -0
  26. package/vendor/nanocodex/browser/Agent.mjs +11 -0
  27. package/vendor/nanocodex/browser/ChatGptSubscription.d.mts +7 -0
  28. package/vendor/nanocodex/browser/ChatGptSubscription.mjs +19 -0
  29. package/vendor/nanocodex/browser/InlineAgent.mjs +266 -0
  30. package/vendor/nanocodex/browser/Transport.d.mts +74 -0
  31. package/vendor/nanocodex/browser/Transport.mjs +61 -0
  32. package/vendor/nanocodex/browser/Voice.d.mts +71 -0
  33. package/vendor/nanocodex/browser/Voice.mjs +291 -0
  34. package/vendor/nanocodex/browser/VoiceSession.mjs +688 -0
  35. package/vendor/nanocodex/browser/WorkerAgent.d.mts +65 -0
  36. package/vendor/nanocodex/browser/WorkerAgent.mjs +1472 -0
  37. package/vendor/nanocodex/browser/agent.worker.mjs +3 -0
  38. package/vendor/nanocodex/browser/config.d.mts +36 -0
  39. package/vendor/nanocodex/browser/config.mjs +439 -0
  40. package/vendor/nanocodex/browser/engine.mjs +13 -0
  41. package/vendor/nanocodex/browser/harness.mjs +55 -0
  42. package/vendor/nanocodex/browser/host.d.mts +83 -0
  43. package/vendor/nanocodex/browser/host.mjs +555 -0
  44. package/vendor/nanocodex/browser/hostManagedWebSocket.d.mts +14 -0
  45. package/vendor/nanocodex/browser/hostManagedWebSocket.mjs +110 -0
  46. package/vendor/nanocodex/browser/index.d.mts +92 -0
  47. package/vendor/nanocodex/browser/index.mjs +23 -0
  48. package/vendor/nanocodex/browser/indexeddb-durability-store.mjs +197 -0
  49. package/vendor/nanocodex/browser/workspace.d.mts +16 -0
  50. package/vendor/nanocodex/browser/workspace.mjs +107 -0
  51. package/vendor/nanocodex/cloud/Client.d.mts +83 -0
  52. package/vendor/nanocodex/cloud/Client.mjs +231 -0
  53. package/vendor/nanocodex/cloud/Decorator.d.mts +31 -0
  54. package/vendor/nanocodex/cloud/Decorator.mjs +37 -0
  55. package/vendor/nanocodex/cloud/Dialog.d.mts +113 -0
  56. package/vendor/nanocodex/cloud/Dialog.mjs +366 -0
  57. package/vendor/nanocodex/cloud/Errors.d.mts +22 -0
  58. package/vendor/nanocodex/cloud/Errors.mjs +39 -0
  59. package/vendor/nanocodex/cloud/Principal.d.mts +26 -0
  60. package/vendor/nanocodex/cloud/Principal.mjs +88 -0
  61. package/vendor/nanocodex/cloud/RemoteProvider.mjs +88 -0
  62. package/vendor/nanocodex/cloud/Transport.d.mts +47 -0
  63. package/vendor/nanocodex/cloud/Transport.mjs +454 -0
  64. package/vendor/nanocodex/cloud/actions/account.d.mts +9 -0
  65. package/vendor/nanocodex/cloud/actions/account.mjs +19 -0
  66. package/vendor/nanocodex/cloud/actions/agent.d.mts +18 -0
  67. package/vendor/nanocodex/cloud/actions/agent.mjs +428 -0
  68. package/vendor/nanocodex/cloud/actions/connection.d.mts +133 -0
  69. package/vendor/nanocodex/cloud/actions/connection.mjs +580 -0
  70. package/vendor/nanocodex/cloud/actions/grant.d.mts +10 -0
  71. package/vendor/nanocodex/cloud/actions/grant.mjs +12 -0
  72. package/vendor/nanocodex/cloud/actions/index.d.mts +7 -0
  73. package/vendor/nanocodex/cloud/actions/index.mjs +7 -0
  74. package/vendor/nanocodex/cloud/actions/machineUsd.d.mts +23 -0
  75. package/vendor/nanocodex/cloud/actions/machineUsd.mjs +37 -0
  76. package/vendor/nanocodex/cloud/actions/model.d.mts +12 -0
  77. package/vendor/nanocodex/cloud/actions/model.mjs +50 -0
  78. package/vendor/nanocodex/cloud/actions/mpp.d.mts +28 -0
  79. package/vendor/nanocodex/cloud/actions/mpp.mjs +39 -0
  80. package/vendor/nanocodex/cloud/index.d.mts +30 -0
  81. package/vendor/nanocodex/cloud/index.mjs +9 -0
  82. package/vendor/nanocodex/cloud/internal.mjs +533 -0
  83. package/vendor/nanocodex/cloud/mercator.mjs +108 -0
  84. package/vendor/nanocodex/cloud/server/HostPrincipal.d.mts +34 -0
  85. package/vendor/nanocodex/cloud/server/HostPrincipal.mjs +202 -0
  86. package/vendor/nanocodex/cloud/server/index.d.mts +1 -0
  87. package/vendor/nanocodex/cloud/server/index.mjs +1 -0
  88. package/vendor/nanocodex/cloud/types.d.mts +211 -0
  89. package/vendor/nanocodex/cloudflare/Agent.d.mts +133 -0
  90. package/vendor/nanocodex/cloudflare/Agent.mjs +811 -0
  91. package/vendor/nanocodex/cloudflare/egress-subject.mjs +19 -0
  92. package/vendor/nanocodex/cloudflare/egress.d.mts +41 -0
  93. package/vendor/nanocodex/cloudflare/egress.mjs +119 -0
  94. package/vendor/nanocodex/cloudflare/event-socket.mjs +316 -0
  95. package/vendor/nanocodex/cloudflare/index.d.mts +7 -0
  96. package/vendor/nanocodex/cloudflare/index.mjs +6 -0
  97. package/vendor/nanocodex/host/Agent.d.mts +55 -0
  98. package/vendor/nanocodex/host/Agent.mjs +1 -0
  99. package/vendor/nanocodex/host/index.d.mts +82 -0
  100. package/vendor/nanocodex/host/index.mjs +10 -0
  101. package/vendor/nanocodex/index.d.mts +81 -0
  102. package/vendor/nanocodex/index.mjs +12 -0
  103. package/vendor/nanocodex/internal.mjs +1275 -0
  104. package/vendor/nanocodex/managed/Agent.d.mts +380 -0
  105. package/vendor/nanocodex/managed/Agent.mjs +1694 -0
  106. package/vendor/nanocodex/managed/ManagedError.d.mts +9 -0
  107. package/vendor/nanocodex/managed/ManagedError.mjs +8 -0
  108. package/vendor/nanocodex/managed/README.md +241 -0
  109. package/vendor/nanocodex/managed/Voice.mjs +202 -0
  110. package/vendor/nanocodex/managed/index.d.mts +20 -0
  111. package/vendor/nanocodex/managed/index.mjs +2 -0
  112. package/vendor/nanocodex/managed/internal.mjs +119 -0
  113. package/vendor/nanocodex/node/Agent.d.mts +50 -0
  114. package/vendor/nanocodex/node/Agent.mjs +203 -0
  115. package/vendor/nanocodex/node/ChatGptSubscription.d.mts +9 -0
  116. package/vendor/nanocodex/node/ChatGptSubscription.mjs +17 -0
  117. package/vendor/nanocodex/node/Transport.d.mts +44 -0
  118. package/vendor/nanocodex/node/Transport.mjs +46 -0
  119. package/vendor/nanocodex/node/host.mjs +731 -0
  120. package/vendor/nanocodex/node/index.d.mts +73 -0
  121. package/vendor/nanocodex/node/index.mjs +17 -0
  122. package/vendor/nanocodex/node/workspace.d.mts +10 -0
  123. package/vendor/nanocodex/node/workspace.mjs +198 -0
  124. package/vendor/nanocodex/package.json +200 -0
  125. package/vendor/nanocodex/pkg-node/nanocodex.d.ts +675 -0
  126. package/vendor/nanocodex/pkg-node/nanocodex.js +2628 -0
  127. package/vendor/nanocodex/pkg-node/nanocodex_bg.wasm.d.ts +105 -0
  128. package/vendor/nanocodex/pkg-node/package.json +1 -0
  129. package/vendor/nanocodex/pkg-web/.nanocodex-bindgen-stamp +6 -0
  130. package/vendor/nanocodex/pkg-web/nanocodex-build.json +1 -0
  131. package/vendor/nanocodex/pkg-web/nanocodex.d.ts +805 -0
  132. package/vendor/nanocodex/pkg-web/nanocodex.js +2714 -0
  133. package/vendor/nanocodex/pkg-web/nanocodex_bg.js +2615 -0
  134. package/vendor/nanocodex/pkg-web/nanocodex_bg.wasm +0 -0
  135. package/vendor/nanocodex/pkg-web/nanocodex_bg.wasm.d.ts +105 -0
  136. package/vendor/nanocodex/pkg-web/nanocodex_worker.js +9 -0
  137. package/vendor/nanocodex/pkg-web/package.json +1 -0
  138. package/vendor/nanocodex/runtime/chatgpt-subscription.mjs +212 -0
  139. package/vendor/nanocodex/runtime/cloudflare-durability-store.d.mts +26 -0
  140. package/vendor/nanocodex/runtime/cloudflare-durability-store.mjs +168 -0
  141. package/vendor/nanocodex/runtime/code-evaluator.worker.mjs +137 -0
  142. package/vendor/nanocodex/runtime/code-runtime.mjs +1 -0
  143. package/vendor/nanocodex/runtime/durability-store.d.mts +85 -0
  144. package/vendor/nanocodex/runtime/durability-store.mjs +631 -0
  145. package/vendor/nanocodex/runtime/durability.mjs +153 -0
  146. package/vendor/nanocodex/runtime/managed-transport.mjs +460 -0
  147. package/vendor/nanocodex/runtime/mcp-runtime.mjs +534 -0
  148. package/vendor/nanocodex/runtime/postgres-durability-store.d.mts +64 -0
  149. package/vendor/nanocodex/runtime/postgres-durability-store.mjs +532 -0
  150. package/vendor/nanocodex/runtime/quickjs-evaluator.d.mts +17 -0
  151. package/vendor/nanocodex/runtime/quickjs-evaluator.mjs +236 -0
  152. package/vendor/nanocodex/runtime/responses-transport.mjs +22 -0
  153. package/vendor/nanocodex/runtime/subagents.d.mts +107 -0
  154. package/vendor/nanocodex/runtime/subagents.mjs +54 -0
  155. package/vendor/nanocodex/runtime/subscription-store.d.mts +8 -0
  156. package/vendor/nanocodex/runtime/subscription-store.mjs +42 -0
  157. package/vendor/nanocodex/runtime/tempo-provider.d.mts +125 -0
  158. package/vendor/nanocodex/runtime/tempo-provider.mjs +264 -0
  159. package/vendor/nanocodex/runtime/tool-configuration.mjs +1 -0
  160. package/vendor/nanocodex/runtime/tool-router.mjs +1 -0
  161. package/vendor/nanocodex/runtime/utf8.mjs +1 -0
  162. package/vendor/nanocodex/runtime/worker-evaluator.mjs +162 -0
  163. package/vendor/nanocodex/runtime/workspace.d.mts +1 -0
  164. package/vendor/nanocodex/runtime/workspace.mjs +1 -0
  165. package/vendor/nanocodex/tools/Tools.d.mts +56 -0
  166. package/vendor/nanocodex/tools/Tools.mjs +136 -0
  167. package/vendor/nanocodex/tools/artifact.d.mts +1 -0
  168. package/vendor/nanocodex/tools/artifact.mjs +1 -0
  169. package/vendor/nanocodex/tools/attachment.mjs +1 -0
  170. package/vendor/nanocodex/tools/bash.d.mts +1 -0
  171. package/vendor/nanocodex/tools/bash.mjs +1 -0
  172. package/vendor/nanocodex/tools/browser/accountInfo.mjs +795 -0
  173. package/vendor/nanocodex/tools/browser/browserBuffer.mjs +18 -0
  174. package/vendor/nanocodex/tools/browser/browserCompiler.mjs +155 -0
  175. package/vendor/nanocodex/tools/browser/browserEgress.mjs +161 -0
  176. package/vendor/nanocodex/tools/browser/browserPython.mjs +133 -0
  177. package/vendor/nanocodex/tools/browser/browserShell.mjs +1075 -0
  178. package/vendor/nanocodex/tools/browser/browserSprintf.mjs +4 -0
  179. package/vendor/nanocodex/tools/browser/browserSsh.mjs +201 -0
  180. package/vendor/nanocodex/tools/browser/browserZlib.mjs +136 -0
  181. package/vendor/nanocodex/tools/browser/compiler.worker.mjs +160 -0
  182. package/vendor/nanocodex/tools/browser/devTunnelsSshBrowser.mjs +13 -0
  183. package/vendor/nanocodex/tools/browser/index.d.mts +209 -0
  184. package/vendor/nanocodex/tools/browser/index.mjs +174 -0
  185. package/vendor/nanocodex/tools/browser/opfsGit.mjs +248 -0
  186. package/vendor/nanocodex/tools/browser/python.worker.mjs +114 -0
  187. package/vendor/nanocodex/tools/browser/threadGit.mjs +197 -0
  188. package/vendor/nanocodex/tools/browser/unsupportedNodeRsa.mjs +5 -0
  189. package/vendor/nanocodex/tools/browser/workspace.mjs +83 -0
  190. package/vendor/nanocodex/tools/dataset.d.mts +1 -0
  191. package/vendor/nanocodex/tools/dataset.mjs +1 -0
  192. package/vendor/nanocodex/tools/datasetContract.mjs +1 -0
  193. package/vendor/nanocodex/tools/datasetEngine.mjs +1 -0
  194. package/vendor/nanocodex/tools/hostedCatalog.d.mts +1 -0
  195. package/vendor/nanocodex/tools/hostedCatalog.mjs +1 -0
  196. package/vendor/nanocodex/tools/index.d.mts +49 -0
  197. package/vendor/nanocodex/tools/index.mjs +19 -0
  198. package/vendor/nanocodex/tools/namedTool.mjs +1 -0
  199. package/vendor/nanocodex/tools/repository-workspace.d.mts +1 -0
  200. package/vendor/nanocodex/tools/repository-workspace.mjs +1 -0
  201. package/vendor/nanocodex/tools/ssh.d.mts +1 -0
  202. package/vendor/nanocodex/tools/ssh.mjs +1 -0
  203. package/vendor/nanocodex/tools/standard.mjs +1 -0
  204. package/vendor/nanocodex/tools/standardDescriptions.mjs +1 -0
  205. package/vendor/nanocodex/types.d.mts +663 -0
  206. package/vendor/nanocodex/wasm.d.mts +4 -0
  207. package/vendor/nanocodex/worker/ChatGptSubscription.d.mts +9 -0
  208. package/vendor/nanocodex/worker/ChatGptSubscription.mjs +29 -0
  209. package/vendor/nanocodex/worker/index.d.mts +18 -0
  210. package/vendor/nanocodex/worker/index.mjs +5 -0
  211. package/vendor/nanocodex-tools/README.md +65 -0
  212. package/vendor/nanocodex-tools/dist/hosted/app-tool-catalog.d.ts +7 -0
  213. package/vendor/nanocodex-tools/dist/hosted/app-tool-catalog.js +14 -0
  214. package/vendor/nanocodex-tools/dist/hosted/broker-core.d.ts +172 -0
  215. package/vendor/nanocodex-tools/dist/hosted/broker-core.js +1278 -0
  216. package/vendor/nanocodex-tools/dist/hosted/index.d.ts +3 -0
  217. package/vendor/nanocodex-tools/dist/hosted/index.js +3 -0
  218. package/vendor/nanocodex-tools/dist/hosted/protocol.d.ts +120 -0
  219. package/vendor/nanocodex-tools/dist/hosted/protocol.js +415 -0
  220. package/vendor/nanocodex-tools/dist/index.d.ts +19 -0
  221. package/vendor/nanocodex-tools/dist/index.js +14 -0
  222. package/vendor/nanocodex-tools/dist/memory.d.ts +204 -0
  223. package/vendor/nanocodex-tools/dist/memory.js +411 -0
  224. package/vendor/nanocodex-tools/dist/namespace.d.ts +87 -0
  225. package/vendor/nanocodex-tools/dist/namespace.js +338 -0
  226. package/vendor/nanocodex-tools/dist/repository-archive.d.ts +4 -0
  227. package/vendor/nanocodex-tools/dist/repository-archive.js +104 -0
  228. package/vendor/nanocodex-tools/dist/runtime.d.ts +30 -0
  229. package/vendor/nanocodex-tools/dist/runtime.js +37 -0
  230. package/vendor/nanocodex-tools/dist/session.d.ts +167 -0
  231. package/vendor/nanocodex-tools/dist/session.js +286 -0
  232. package/vendor/nanocodex-tools/dist/shell.d.ts +51 -0
  233. package/vendor/nanocodex-tools/dist/shell.js +553 -0
  234. package/vendor/nanocodex-tools/dist/ssh.d.ts +29 -0
  235. package/vendor/nanocodex-tools/dist/ssh.js +66 -0
  236. package/vendor/nanocodex-tools/dist/workspace.d.ts +45 -0
  237. package/vendor/nanocodex-tools/dist/workspace.js +237 -0
  238. package/vendor/nanocodex-tools/package.json +122 -0
  239. package/vendor/nanocodex-tools/runtime/code-runtime.mjs +817 -0
  240. package/vendor/nanocodex-tools/runtime/tool-configuration.mjs +50 -0
  241. package/vendor/nanocodex-tools/runtime/tool-router.mjs +730 -0
  242. package/vendor/nanocodex-tools/runtime/utf8.d.mts +2 -0
  243. package/vendor/nanocodex-tools/runtime/utf8.mjs +25 -0
  244. package/vendor/nanocodex-tools/tools/artifact.d.mts +69 -0
  245. package/vendor/nanocodex-tools/tools/artifact.mjs +270 -0
  246. package/vendor/nanocodex-tools/tools/attachment.mjs +603 -0
  247. package/vendor/nanocodex-tools/tools/bash.d.mts +90 -0
  248. package/vendor/nanocodex-tools/tools/bash.mjs +742 -0
  249. package/vendor/nanocodex-tools/tools/dataset.d.mts +9 -0
  250. package/vendor/nanocodex-tools/tools/dataset.mjs +47 -0
  251. package/vendor/nanocodex-tools/tools/datasetContract.mjs +54 -0
  252. package/vendor/nanocodex-tools/tools/datasetEngine.mjs +1167 -0
  253. package/vendor/nanocodex-tools/tools/execution-contract.d.mts +5 -0
  254. package/vendor/nanocodex-tools/tools/execution-contract.mjs +63 -0
  255. package/vendor/nanocodex-tools/tools/hostedCatalog.d.mts +20 -0
  256. package/vendor/nanocodex-tools/tools/hostedCatalog.mjs +67 -0
  257. package/vendor/nanocodex-tools/tools/hostedMachine.d.mts +10 -0
  258. package/vendor/nanocodex-tools/tools/hostedMachine.mjs +56 -0
  259. package/vendor/nanocodex-tools/tools/just-bash-browser.d.mts +1 -0
  260. package/vendor/nanocodex-tools/tools/just-bash-browser.mjs +1 -0
  261. package/vendor/nanocodex-tools/tools/namedTool.d.mts +3 -0
  262. package/vendor/nanocodex-tools/tools/namedTool.mjs +3 -0
  263. package/vendor/nanocodex-tools/tools/nodeProcess.d.mts +10 -0
  264. package/vendor/nanocodex-tools/tools/nodeProcess.mjs +314 -0
  265. package/vendor/nanocodex-tools/tools/processOutput.mjs +56 -0
  266. package/vendor/nanocodex-tools/tools/repository-workspace.d.mts +55 -0
  267. package/vendor/nanocodex-tools/tools/repository-workspace.mjs +397 -0
  268. package/vendor/nanocodex-tools/tools/ssh.d.mts +56 -0
  269. package/vendor/nanocodex-tools/tools/ssh.mjs +503 -0
  270. package/vendor/nanocodex-tools/tools/standard.mjs +458 -0
  271. package/vendor/nanocodex-tools/tools/standardDescriptions.mjs +3 -0
  272. package/vendor/nanocodex-tools/tools/types.d.mts +83 -0
  273. package/vendor/nanocodex-tools/tools/workspace.d.mts +25 -0
  274. package/vendor/nanocodex-tools/tools/workspace.mjs +282 -0
  275. package/vendor/nanocodex-tools/tools/x.d.mts +30 -0
  276. package/vendor/nanocodex-tools/tools/x.mjs +91 -0
@@ -0,0 +1,202 @@
1
+ const APP_ID = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
2
+ const OPAQUE_CLAIM = /^[^\u0000-\u001f\u007f]{1,512}$/;
3
+ const EXCHANGE_TOKEN = /^[A-Za-z0-9_-]{43}$/;
4
+ const EXCHANGE_RESOURCE_PREFIX = "urn:nanocodex:host-principal:exchange:";
5
+
6
+ export function create(parameters) {
7
+ if (!parameters || typeof parameters !== "object") {
8
+ throw new TypeError("HostPrincipal.create requires parameters");
9
+ }
10
+ if (!APP_ID.test(parameters.appId)) {
11
+ throw new TypeError("HostPrincipal.create requires a valid appId");
12
+ }
13
+ if (typeof parameters.secret !== "string" || !/^\S{32,512}$/.test(parameters.secret)) {
14
+ throw new TypeError("HostPrincipal.create requires a 32 to 512 character project secret");
15
+ }
16
+ const appOrigin = publicOrigin(parameters.appOrigin, "HostPrincipal.create appOrigin");
17
+ const baseUrl = serviceBaseUrl(parameters.baseUrl ?? "https://api.nanocodex.xyz");
18
+ const fetchFn = parameters.fetch ?? globalThis.fetch;
19
+ if (typeof fetchFn !== "function") throw new TypeError("HostPrincipal.create requires fetch");
20
+
21
+ const client = {
22
+ async create(options) {
23
+ const claims = principalClaims(options);
24
+ const resources = principalResources(options?.resources);
25
+ const expiresIn = exchangeExpiry(options?.expiresIn);
26
+ const response = await fetchFn(new URL("/v1/host-principal/exchanges", baseUrl), {
27
+ method: "POST",
28
+ headers: serverHeaders(parameters.appId, parameters.secret),
29
+ body: JSON.stringify({
30
+ app_origin: appOrigin,
31
+ issuer: claims.issuer,
32
+ tenant: claims.tenant,
33
+ subject: claims.subject,
34
+ session_id: claims.sessionId,
35
+ resources,
36
+ ...(expiresIn === undefined ? {} : { expires_in: expiresIn }),
37
+ }),
38
+ signal: options?.signal,
39
+ });
40
+ const body = await response.json().catch(() => undefined);
41
+ if (!response.ok) throw serviceError(response, body, "creation");
42
+ return exchangeFromWire(body);
43
+ },
44
+ handler(options) {
45
+ if (!options || typeof options.authenticate !== "function") {
46
+ throw new TypeError("host principal handler requires authenticate");
47
+ }
48
+ return async function hostPrincipalExchange(request) {
49
+ if (!request || request.method !== "POST") {
50
+ return errorResponse(405, "Method not allowed.", { allow: "POST" });
51
+ }
52
+ const origin = request.headers?.get?.("origin");
53
+ const fetchSite = request.headers?.get?.("sec-fetch-site");
54
+ if (origin !== appOrigin || (fetchSite && fetchSite !== "same-origin")) {
55
+ return errorResponse(403, "Origin not allowed.");
56
+ }
57
+ let requestBody;
58
+ try {
59
+ requestBody = await request.json();
60
+ } catch {
61
+ return errorResponse(400, "Invalid host principal request.");
62
+ }
63
+ if (!requestBody || typeof requestBody !== "object" || Array.isArray(requestBody)
64
+ || Object.keys(requestBody).some((key) => key !== "resources")) {
65
+ return errorResponse(400, "Invalid host principal request.");
66
+ }
67
+ let resources;
68
+ try {
69
+ resources = principalResources(requestBody.resources);
70
+ } catch (error) {
71
+ return errorResponse(400, error.message);
72
+ }
73
+ const identity = await options.authenticate(request);
74
+ if (!identity) return errorResponse(401, "Authentication required.");
75
+ const exchange = await client.create({ ...identity, resources });
76
+ return Response.json({ token: exchange.token, expires_at: exchange.expiresAt }, {
77
+ headers: { "cache-control": "no-store" },
78
+ });
79
+ };
80
+ },
81
+ async revoke(options) {
82
+ const claims = principalClaims(options);
83
+ const response = await fetchFn(new URL("/v1/host-principal/sessions", baseUrl), {
84
+ method: "DELETE",
85
+ headers: serverHeaders(parameters.appId, parameters.secret),
86
+ body: JSON.stringify({
87
+ app_origin: appOrigin,
88
+ issuer: claims.issuer,
89
+ tenant: claims.tenant,
90
+ subject: claims.subject,
91
+ session_id: claims.sessionId,
92
+ }),
93
+ signal: options?.signal,
94
+ });
95
+ if (!response.ok) {
96
+ const body = await response.json().catch(() => undefined);
97
+ throw serviceError(response, body, "revocation");
98
+ }
99
+ await response.body?.cancel();
100
+ },
101
+ };
102
+ return Object.freeze(client);
103
+ }
104
+
105
+ function principalClaims(value) {
106
+ if (!value || typeof value !== "object" || Array.isArray(value)
107
+ || !boundedOpaqueClaim(value.issuer) || !boundedOpaqueClaim(value.tenant)
108
+ || !boundedOpaqueClaim(value.subject) || !boundedOpaqueClaim(value.sessionId)) {
109
+ throw new TypeError("host principal requires bounded opaque issuer, tenant, subject, and sessionId");
110
+ }
111
+ return {
112
+ issuer: value.issuer,
113
+ tenant: value.tenant,
114
+ subject: value.subject,
115
+ sessionId: value.sessionId,
116
+ };
117
+ }
118
+
119
+ function boundedOpaqueClaim(value) {
120
+ return typeof value === "string" && OPAQUE_CLAIM.test(value);
121
+ }
122
+
123
+ function principalResources(value) {
124
+ if (!Array.isArray(value) || value.length === 0 || value.length > 64
125
+ || value.some((resource) => typeof resource !== "string" || resource.length === 0
126
+ || resource.length > 512 || resource.startsWith(EXCHANGE_RESOURCE_PREFIX)
127
+ || forbiddenAuthority(resource))) {
128
+ throw new TypeError("host principal resources must be 1 through 64 bounded non-exchange strings");
129
+ }
130
+ return Object.freeze([...value]);
131
+ }
132
+
133
+ function forbiddenAuthority(resource) {
134
+ const value = resource.toLowerCase();
135
+ return value.startsWith("urn:nanocodex:mpp:")
136
+ || value.startsWith("urn:nanocodex:access-key:")
137
+ || value.startsWith("urn:nanocodex:access_key:")
138
+ || value === "urn:nanocodex:authorize-access-key";
139
+ }
140
+
141
+ function exchangeExpiry(value) {
142
+ if (value === undefined) return undefined;
143
+ if (!Number.isSafeInteger(value) || value < 30 || value > 300) {
144
+ throw new TypeError("host principal expiresIn must be between 30 and 300 seconds");
145
+ }
146
+ return value;
147
+ }
148
+
149
+ function exchangeFromWire(value) {
150
+ const now = Math.floor(Date.now() / 1_000);
151
+ if (!value || typeof value !== "object" || Array.isArray(value)
152
+ || !EXCHANGE_TOKEN.test(value.token)
153
+ || !Number.isSafeInteger(value.expires_at)
154
+ || value.expires_at <= now || value.expires_at > now + 300) {
155
+ throw new Error("Nanocodex returned an invalid host principal exchange.");
156
+ }
157
+ return Object.freeze({ token: value.token, expiresAt: value.expires_at });
158
+ }
159
+
160
+ function serverHeaders(appId, secret) {
161
+ return {
162
+ accept: "application/json",
163
+ authorization: `Bearer ${secret}`,
164
+ "content-type": "application/json",
165
+ "x-nanocodex-app-id": appId,
166
+ };
167
+ }
168
+
169
+ function serviceError(response, body, operation) {
170
+ const error = new Error(
171
+ body?.error?.message ?? `Nanocodex host principal ${operation} failed with ${response.status}.`,
172
+ );
173
+ error.status = response.status;
174
+ return error;
175
+ }
176
+
177
+ function errorResponse(status, message, headers = {}) {
178
+ return Response.json({ error: { message } }, {
179
+ status,
180
+ headers: { ...headers, "cache-control": "no-store" },
181
+ });
182
+ }
183
+
184
+ function serviceBaseUrl(value) {
185
+ const url = new URL(value);
186
+ const origin = publicOrigin(url.origin, "HostPrincipal.create baseUrl");
187
+ if (url.username || url.password || url.hash) {
188
+ throw new TypeError("HostPrincipal.create baseUrl cannot contain credentials or a fragment");
189
+ }
190
+ return new URL(origin);
191
+ }
192
+
193
+ function publicOrigin(value, label) {
194
+ if (typeof value !== "string") throw new TypeError(`${label} must be an exact origin`);
195
+ const url = new URL(value);
196
+ const loopback = ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname.toLowerCase());
197
+ if (url.origin !== value || (url.protocol !== "https:" && !(url.protocol === "http:" && loopback))
198
+ || url.username || url.password) {
199
+ throw new TypeError(`${label} must be an exact HTTPS or loopback origin`);
200
+ }
201
+ return url.origin;
202
+ }
@@ -0,0 +1 @@
1
+ export * as HostPrincipal from "./HostPrincipal.mjs";
@@ -0,0 +1 @@
1
+ export * as HostPrincipal from "./HostPrincipal.mjs";
@@ -0,0 +1,211 @@
1
+ export type Hex = `0x${string}`;
2
+
3
+ export type CloudAccount =
4
+ | "github"
5
+ | "gmail"
6
+ | "gdrive"
7
+ | "gcalendar"
8
+ | "gtasks"
9
+ | "gdocs"
10
+ | "gsheets"
11
+ | "gslides"
12
+ | "gcontacts"
13
+ | "slack"
14
+ | "x"
15
+ | "chatgpt";
16
+
17
+ /** Secret-free account identity returned by a connector status reader. */
18
+ export type ConnectorConnection = Readonly<{
19
+ id: string;
20
+ label: string;
21
+ accountId?: string | undefined;
22
+ capabilities?: readonly CloudAccount[] | undefined;
23
+ }>;
24
+
25
+ /** Provider-neutral status for a connector service capability. */
26
+ export type ConnectorStatus = Readonly<{
27
+ connected: boolean;
28
+ connections?: readonly ConnectorConnection[] | undefined;
29
+ }>;
30
+
31
+ /** Exact approved connector connection IDs keyed by service capability. */
32
+ export type ConnectorConnectionSelection = Readonly<
33
+ Partial<Record<Exclude<CloudAccount, "chatgpt">, readonly string[]>>
34
+ >;
35
+
36
+ export type AccessKey = Readonly<{
37
+ address: Hex;
38
+ chainId: bigint;
39
+ keyId: Hex;
40
+ /** Public key material is present only when the caller supplied an external key. */
41
+ publicKey?: Hex | undefined;
42
+ keyType: "secp256k1" | "p256" | "webAuthn";
43
+ limits: readonly Readonly<{ token: Hex; limit: bigint; period?: number | undefined }>[];
44
+ scopes: readonly Readonly<{
45
+ address: Hex;
46
+ selector?: Hex | string | undefined;
47
+ recipients?: readonly Hex[] | undefined;
48
+ }>[];
49
+ witness: Hex;
50
+ expiry: number;
51
+ /** Canonical RLP-encoded, root-signed TIP-1053 authorization. */
52
+ authorization?: Hex | undefined;
53
+ }>;
54
+
55
+ export type AgentTurnResult = Readonly<{
56
+ turnId: string;
57
+ finalMessage: string;
58
+ provider: string;
59
+ capabilitiesUsed: readonly string[];
60
+ usage: import("../types.mjs").TurnUsage | null;
61
+ usageError?: string | undefined;
62
+ cursor?: string | undefined;
63
+ }>;
64
+
65
+ export type AgentVisibility = Readonly<{
66
+ finalMessages: boolean;
67
+ actionSummaries: boolean;
68
+ conversationHistory: boolean;
69
+ rawTraces: boolean;
70
+ }>;
71
+
72
+ /** Secret-free metadata for one account-owned hosted MCP connection. */
73
+ export type McpConnection = Readonly<{
74
+ /** Exact opaque 43-character hosted connection ID. */
75
+ id: string;
76
+ /** Account-visible display name; endpoints and credentials remain broker-owned. */
77
+ name: string;
78
+ }>;
79
+
80
+ export type AgentTurn = Readonly<{
81
+ idempotencyKey: string;
82
+ accepted(): Promise<string>;
83
+ state(): Promise<import("../managed/Agent.mjs").TurnView>;
84
+ steer(options: Readonly<{ input: import("../types.mjs").PromptInput }>): Promise<Readonly<{
85
+ turn_id: string;
86
+ state: "steering";
87
+ }>>;
88
+ result(options?: import("../managed/Agent.mjs").TurnResultOptions): Promise<AgentTurnResult>;
89
+ cancel(): Promise<import("../managed/Agent.mjs").TurnView | Readonly<{
90
+ turn_id: string;
91
+ state: "cancelling";
92
+ }>>;
93
+ }>;
94
+
95
+ export type ConnectAgent = Readonly<{
96
+ id: string;
97
+ /** Alias matching the canonical Nanocodex Agent surface. */
98
+ sessionId: string;
99
+ type: "connect";
100
+ provider: string;
101
+ events: Readonly<{
102
+ page(options?: import("../managed/Agent.mjs").EventHistoryOptions): Promise<import("../managed/Agent.mjs").EventHistoryPage>;
103
+ watch(options?: import("../managed/Agent.mjs").WatchEventsOptions): AsyncIterableIterator<import("../managed/Agent.mjs").Event>;
104
+ }>;
105
+ mercator: Readonly<{
106
+ enabled: true;
107
+ readonly channelId: Hex | undefined;
108
+ readonly cumulative: bigint;
109
+ readonly opened: boolean;
110
+ }>;
111
+ turn: Readonly<{
112
+ prompt(options: import("../managed/Agent.mjs").PromptOptions): AgentTurn;
113
+ }>;
114
+ state(): Promise<import("../managed/Agent.mjs").State>;
115
+ session: Readonly<{ shutdown(): Promise<void> }>;
116
+ }>;
117
+
118
+ export type Grant = Readonly<{
119
+ id: Hex;
120
+ permission: string;
121
+ status: "active" | "revoked" | "expired";
122
+ expiresAt: number;
123
+ /** App-generated signed selector for this exact durable conversation. */
124
+ conversationId?: string | undefined;
125
+ capabilities: readonly string[];
126
+ visibility: AgentVisibility;
127
+ /** Secret-free cloud account providers bound to this grant. */
128
+ connectors: readonly CloudAccount[];
129
+ /** Exact account connection subset. Absent only when reading a legacy grant. */
130
+ connectorConnections?: ConnectorConnectionSelection | undefined;
131
+ /** Exact secret-free hosted MCP connections bound to this grant. */
132
+ mcpConnections: readonly McpConnection[];
133
+ /** Exact signed app-local reverse-tool catalog, when present. */
134
+ appToolCatalogDigest?: Hex | undefined;
135
+ }>;
136
+
137
+ export type MppPermission = Readonly<{
138
+ token: Hex;
139
+ symbol: string;
140
+ balance: bigint;
141
+ balanceStatus: "pending" | "ready";
142
+ settlementToken: Hex;
143
+ settlementSymbol: string;
144
+ settlementBalance: bigint;
145
+ spent: bigint;
146
+ limit: bigint;
147
+ period: number;
148
+ maxPerRequest: bigint;
149
+ }>;
150
+
151
+ type ConnectionBase = Readonly<{
152
+ agentId: string;
153
+ grant: Grant;
154
+ }>;
155
+
156
+ export type WalletConnection = ConnectionBase & Readonly<{
157
+ accountAddress: Hex;
158
+ principal?: undefined;
159
+ }> & (
160
+ | Readonly<{
161
+ authorization: "access_key";
162
+ accessKey: AccessKey;
163
+ mpp: MppPermission;
164
+ }>
165
+ | Readonly<{
166
+ authorization: "hosted";
167
+ accessKey?: undefined;
168
+ mpp?: undefined;
169
+ }>
170
+ );
171
+
172
+ export type HostConnection = ConnectionBase & Readonly<{
173
+ accountAddress?: undefined;
174
+ principal: Readonly<{ kind: "host"; id: string }>;
175
+ authorization: "hosted";
176
+ accessKey?: undefined;
177
+ mpp?: undefined;
178
+ }>;
179
+
180
+ /** The stable passkey/wallet connection contract. */
181
+ export type Connection = WalletConnection;
182
+
183
+ export type MachineUsdConfig = Readonly<{
184
+ chainId: number;
185
+ minUsdAmountCents: number;
186
+ maxUsdAmountCents: number;
187
+ onrampEnabled: boolean;
188
+ stripePublishableKey: string;
189
+ tokenAddress: Hex;
190
+ }>;
191
+
192
+ export type MachineUsdFunding = Readonly<{
193
+ order: Readonly<{
194
+ id: string;
195
+ status: string;
196
+ usdAmountCents: number;
197
+ machineUsdAmount: bigint;
198
+ issuanceTransactionHash: Hex;
199
+ }>;
200
+ connection: Connection;
201
+ }>;
202
+
203
+ export type MppCharge = Readonly<{
204
+ receipt: Readonly<{
205
+ id: string;
206
+ amount: bigint;
207
+ origin: string;
208
+ transactionHash: Hex;
209
+ }>;
210
+ connection: Connection;
211
+ }>;
@@ -0,0 +1,133 @@
1
+ import type {
2
+ Agent as BaseAgent,
3
+ AgentActions,
4
+ AgentOptions,
5
+ AgentEvent,
6
+ DefaultAgent,
7
+ ToolConfiguration,
8
+ } from "../types.mjs";
9
+ import type { CloudflareDurableObjectStorage } from "../runtime/cloudflare-durability-store.mjs";
10
+ import type { Tool as SubagentTool } from "../runtime/subagents.mjs";
11
+ import type {
12
+ DurabilityExportPageRequest,
13
+ DurabilityPortableStateArchive,
14
+ DurabilityPortableStatePage,
15
+ DurabilityStoredState,
16
+ } from "../types.mjs";
17
+
18
+ export type DurableObjectContext = Readonly<{
19
+ storage: CloudflareDurableObjectStorage;
20
+ acceptWebSocket(socket: WebSocket, tags?: string[]): void;
21
+ getWebSockets(tag?: string): WebSocket[];
22
+ }>;
23
+
24
+ /** The owning Cloudflare Durable Object instance. Runtime fields remain adapter-private. */
25
+ export type DurableObjectOwner = object;
26
+
27
+ /** Rust-owned first-prompt retrieval policy; the host owns authenticated execution. */
28
+ export type BootstrapPlan = Readonly<{
29
+ query: string;
30
+ voice_bootstrap: boolean;
31
+ calls: readonly Readonly<{ name: "find_session" | "memory"; arguments: unknown }>[];
32
+ }>;
33
+ export function bootstrapPlan(input: string): Promise<BootstrapPlan>;
34
+
35
+ export type EventFrame = Readonly<{
36
+ cursor: string;
37
+ event: AgentEvent;
38
+ }> | Readonly<{
39
+ type: "replay_paused";
40
+ cursor: string;
41
+ latest_cursor: string;
42
+ }>;
43
+
44
+ type CloudflareAgentActions = Omit<AgentActions, "events" | "turn"> & Readonly<{
45
+ events: AgentActions["events"] & Readonly<{
46
+ /** Accepts a read-only hibernatable event socket; reconnect from the last event or replay pause cursor. */
47
+ connect(request: Request): Response;
48
+ }>;
49
+ turn: AgentActions["turn"] & Readonly<{
50
+ /** Atomically steers the active turn or starts a new independently awaitable turn. */
51
+ route(options: { input: string }): Promise<import("../types.mjs").Turn | undefined>;
52
+ }>;
53
+ }>;
54
+
55
+ /** A durable Agent whose Cloudflare event socket survives typed extensions. */
56
+ export type Agent<extended extends object = {}> =
57
+ Omit<BaseAgent<CloudflareAgentActions & extended>, "extend"> & Readonly<{
58
+ extend<const extension extends object>(
59
+ decorator: (agent: Agent<extended>) => extension,
60
+ ): Agent<extended & extension>;
61
+ }>;
62
+
63
+ /** Removes the package-owned durable history for one Cloudflare Agent. */
64
+ export function destroy(owner: DurableObjectOwner): void;
65
+
66
+ /** Fences and exports this inactive Cloudflare Agent's provider-neutral state. */
67
+ export function exportDurabilityState(
68
+ owner: DurableObjectOwner,
69
+ ): Promise<DurabilityPortableStateArchive>;
70
+
71
+ /** Fences once and exports one resumable page of an exact revision range. */
72
+ export function exportDurabilityState(
73
+ owner: DurableObjectOwner,
74
+ request: DurabilityExportPageRequest,
75
+ ): Promise<DurabilityPortableStatePage>;
76
+
77
+ /** Imports provider-neutral state into a pristine Cloudflare Agent owner. */
78
+ export function importDurabilityState(
79
+ owner: DurableObjectOwner,
80
+ archive: DurabilityPortableStateArchive,
81
+ ): Promise<DurabilityStoredState>;
82
+
83
+ /** Prunes old terminal receipts before constructing the full Agent runtime. */
84
+ export function pruneDurableReceipts(
85
+ owner: DurableObjectOwner,
86
+ options?: Readonly<{ terminalReceiptRetention?: number | undefined }>,
87
+ ): Promise<void>;
88
+
89
+ /** Atomically steers an active Cloudflare Agent turn or starts a new turn. */
90
+ export function route(
91
+ agent: Agent,
92
+ options: { input: string },
93
+ ): Promise<import("../types.mjs").Turn | undefined>;
94
+
95
+ /** Creates one durable Agent from its owning Durable Object instance. */
96
+ export function create(owner: create.Owner, options?: create.Options): Promise<create.ReturnType>;
97
+ export declare namespace create {
98
+ type Owner = DurableObjectOwner;
99
+ type Options = Readonly<{
100
+ /** Stable portable state identity. It cannot change after first construction or import. */
101
+ durabilityId?: string | undefined;
102
+ /**
103
+ * `durable` retains the adapter's resumable event socket. `caller` leaves
104
+ * event retention to the embedding Durable Object and disables connect().
105
+ */
106
+ eventPersistence?: "durable" | "caller" | undefined;
107
+ instructions?: string | undefined;
108
+ /** Appends host instructions while retaining the selected model's prompt. */
109
+ additionalInstructions?: string | undefined;
110
+ /**
111
+ * Bounds terminal receipts retained in the hot Rust state checkpoint.
112
+ * The caller must preserve older exact-ID results before selecting this.
113
+ */
114
+ terminalReceiptRetention?: number | undefined;
115
+ /** Live child execution is in-memory; interrupted descriptors survive reconstruction. */
116
+ tools?: ToolConfiguration<SubagentTool> | undefined;
117
+ }>;
118
+ type ReturnType = Agent;
119
+ }
120
+
121
+ /** Creates one non-durable Rust/WASM Agent in the current Cloudflare isolate. */
122
+ export function createEphemeral(
123
+ owner: createEphemeral.Owner,
124
+ options?: createEphemeral.Options,
125
+ ): Promise<createEphemeral.ReturnType>;
126
+ export declare namespace createEphemeral {
127
+ type Owner = DurableObjectOwner;
128
+ type Options = Readonly<AgentOptions & {
129
+ /** Caller-owned tools exposed directly to the model. */
130
+ tools?: ToolConfiguration<SubagentTool> | undefined;
131
+ }>;
132
+ type ReturnType = DefaultAgent;
133
+ }