@astrale-os/shell 0.1.1 → 0.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 (284) hide show
  1. package/LICENSE +202 -0
  2. package/dist/application/apps.d.ts +18 -0
  3. package/dist/application/apps.d.ts.map +1 -0
  4. package/dist/application/apps.js +35 -0
  5. package/dist/application/namespaces.d.ts +21 -0
  6. package/dist/application/namespaces.d.ts.map +1 -0
  7. package/dist/application/namespaces.js +12 -0
  8. package/dist/application/session/open.d.ts +19 -0
  9. package/dist/application/session/open.d.ts.map +1 -0
  10. package/dist/application/session/open.js +46 -0
  11. package/dist/application/session/session.d.ts +41 -0
  12. package/dist/application/session/session.d.ts.map +1 -0
  13. package/dist/application/session/session.js +9 -0
  14. package/dist/application/session/token.d.ts +24 -0
  15. package/dist/application/session/token.d.ts.map +1 -0
  16. package/dist/application/session/token.js +51 -0
  17. package/dist/application/spaces.d.ts +17 -0
  18. package/dist/application/spaces.d.ts.map +1 -0
  19. package/dist/application/spaces.js +30 -0
  20. package/dist/application/user.d.ts +20 -0
  21. package/dist/application/user.d.ts.map +1 -0
  22. package/dist/application/user.js +26 -0
  23. package/dist/application/view.d.ts +20 -0
  24. package/dist/application/view.d.ts.map +1 -0
  25. package/dist/application/view.js +18 -0
  26. package/dist/{application/channels/children-channel.d.ts → channel/children.d.ts} +4 -4
  27. package/dist/channel/children.d.ts.map +1 -0
  28. package/dist/channel/listeners.d.ts +8 -0
  29. package/dist/channel/listeners.d.ts.map +1 -0
  30. package/dist/{adapters/message-port.transport.d.ts → channel/message-port.d.ts} +2 -2
  31. package/dist/channel/message-port.d.ts.map +1 -0
  32. package/dist/{application/channels → channel}/observer.d.ts +2 -1
  33. package/dist/channel/observer.d.ts.map +1 -0
  34. package/dist/{application/channels/parent-channel.d.ts → channel/parent.d.ts} +4 -4
  35. package/dist/channel/parent.d.ts.map +1 -0
  36. package/dist/{application/channels/parent-channel.js → channel/parent.js} +1 -1
  37. package/dist/channel/transport.d.ts +8 -0
  38. package/dist/channel/transport.d.ts.map +1 -0
  39. package/dist/index.d.ts +56 -41
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +23 -20
  42. package/dist/{domain/intent → intent}/middleware/capabilities.d.ts +2 -2
  43. package/dist/intent/middleware/capabilities.d.ts.map +1 -0
  44. package/dist/intent/middleware/logging.d.ts.map +1 -0
  45. package/dist/{domain/intent → intent}/middleware/rate-limit.d.ts +1 -1
  46. package/dist/intent/middleware/rate-limit.d.ts.map +1 -0
  47. package/dist/{application/intent/pipeline-runner.d.ts → intent/pipeline.d.ts} +3 -3
  48. package/dist/intent/pipeline.d.ts.map +1 -0
  49. package/dist/intent/policy.d.ts +40 -0
  50. package/dist/intent/policy.d.ts.map +1 -0
  51. package/dist/intent/policy.js +15 -0
  52. package/dist/{application/intent → intent}/request.d.ts +5 -14
  53. package/dist/intent/request.d.ts.map +1 -0
  54. package/dist/{application/intent → intent}/request.js +6 -20
  55. package/dist/intent/router.d.ts +31 -0
  56. package/dist/intent/router.d.ts.map +1 -0
  57. package/dist/{application/intent → intent}/router.js +1 -15
  58. package/dist/kernel/client.d.ts +33 -0
  59. package/dist/kernel/client.d.ts.map +1 -0
  60. package/dist/kernel/credential.d.ts +35 -0
  61. package/dist/kernel/credential.d.ts.map +1 -0
  62. package/dist/kernel/credential.js +43 -0
  63. package/dist/kernel/delegated.d.ts +17 -0
  64. package/dist/kernel/delegated.d.ts.map +1 -0
  65. package/dist/kernel/delegated.js +29 -0
  66. package/dist/{application/windowing/token-refresh.d.ts → kernel/delegation/refresh.d.ts} +2 -2
  67. package/dist/kernel/delegation/refresh.d.ts.map +1 -0
  68. package/dist/{application/windowing/token-refresh.js → kernel/delegation/refresh.js} +1 -1
  69. package/dist/{domain/identity/delegation.d.ts → kernel/delegation/token.d.ts} +1 -1
  70. package/dist/kernel/delegation/token.d.ts.map +1 -0
  71. package/dist/{adapters/kernel-client.adapter.d.ts → kernel/session.d.ts} +15 -12
  72. package/dist/kernel/session.d.ts.map +1 -0
  73. package/dist/{adapters/kernel-client.adapter.js → kernel/session.js} +10 -15
  74. package/dist/message/ctrl.d.ts +34 -0
  75. package/dist/message/ctrl.d.ts.map +1 -0
  76. package/dist/message/error.d.ts +9 -0
  77. package/dist/message/error.d.ts.map +1 -0
  78. package/dist/{application/windowing/protocol.d.ts → message/init.d.ts} +1 -1
  79. package/dist/message/init.d.ts.map +1 -0
  80. package/dist/{domain/message/registries.d.ts → message/intent.d.ts} +45 -37
  81. package/dist/message/intent.d.ts.map +1 -0
  82. package/dist/message/message.d.ts +10 -0
  83. package/dist/message/message.d.ts.map +1 -0
  84. package/dist/shell/access.d.ts +10 -0
  85. package/dist/shell/access.d.ts.map +1 -0
  86. package/dist/shell/access.js +19 -0
  87. package/dist/shell/build.d.ts +28 -0
  88. package/dist/shell/build.d.ts.map +1 -0
  89. package/dist/shell/build.js +58 -0
  90. package/dist/shell/config.d.ts +29 -0
  91. package/dist/shell/config.d.ts.map +1 -0
  92. package/dist/shell/create.d.ts +4 -0
  93. package/dist/shell/create.d.ts.map +1 -0
  94. package/dist/shell/create.js +5 -0
  95. package/dist/{application/windowing/child-init.d.ts → shell/sandboxed/boot.d.ts} +5 -5
  96. package/dist/shell/sandboxed/boot.d.ts.map +1 -0
  97. package/dist/{application/windowing/child-init.js → shell/sandboxed/boot.js} +5 -6
  98. package/dist/shell/sandboxed/guarded.d.ts +13 -0
  99. package/dist/shell/sandboxed/guarded.d.ts.map +1 -0
  100. package/dist/shell/sandboxed/guarded.js +28 -0
  101. package/dist/shell/sandboxed/pending.d.ts +14 -0
  102. package/dist/shell/sandboxed/pending.d.ts.map +1 -0
  103. package/dist/shell/sandboxed/pending.js +30 -0
  104. package/dist/shell/sandboxed/sandboxed.d.ts +4 -0
  105. package/dist/shell/sandboxed/sandboxed.d.ts.map +1 -0
  106. package/dist/shell/sandboxed/sandboxed.js +100 -0
  107. package/dist/shell/shell.d.ts +36 -0
  108. package/dist/shell/shell.d.ts.map +1 -0
  109. package/dist/shell/standalone.d.ts +4 -0
  110. package/dist/shell/standalone.d.ts.map +1 -0
  111. package/dist/shell/standalone.js +85 -0
  112. package/dist/{domain/window/config.d.ts → window/capabilities.d.ts} +3 -3
  113. package/dist/window/capabilities.d.ts.map +1 -0
  114. package/dist/{ports/shell-adapter.d.ts → window/embed/adapter.d.ts} +3 -2
  115. package/dist/window/embed/adapter.d.ts.map +1 -0
  116. package/dist/{adapters/iframe.shell-adapter.d.ts → window/embed/iframe.d.ts} +2 -2
  117. package/dist/window/embed/iframe.d.ts.map +1 -0
  118. package/dist/{application/windowing/lifecycle-controller.d.ts → window/lifecycle/states.d.ts} +4 -3
  119. package/dist/window/lifecycle/states.d.ts.map +1 -0
  120. package/dist/{application/windowing/lifecycle-controller.js → window/lifecycle/states.js} +1 -1
  121. package/dist/{domain/window/lifecycle.d.ts → window/lifecycle/transition.d.ts} +3 -3
  122. package/dist/window/lifecycle/transition.d.ts.map +1 -0
  123. package/dist/{domain/window/lifecycle.js → window/lifecycle/transition.js} +1 -1
  124. package/dist/window/mount/close.d.ts +27 -0
  125. package/dist/window/mount/close.d.ts.map +1 -0
  126. package/dist/window/mount/close.js +74 -0
  127. package/dist/window/mount/credential.d.ts +41 -0
  128. package/dist/window/mount/credential.d.ts.map +1 -0
  129. package/dist/window/mount/credential.js +74 -0
  130. package/dist/window/mount/handshake.d.ts.map +1 -0
  131. package/dist/{application/windowing → window/mount}/handshake.js +1 -1
  132. package/dist/{application/windowing/mount-controller.d.ts → window/mount/mount.d.ts} +7 -7
  133. package/dist/window/mount/mount.d.ts.map +1 -0
  134. package/dist/window/mount/mount.js +143 -0
  135. package/dist/window/mount/params.d.ts +73 -0
  136. package/dist/window/mount/params.d.ts.map +1 -0
  137. package/dist/window/mount/params.js +1 -0
  138. package/dist/window/mount/plain.d.ts +20 -0
  139. package/dist/window/mount/plain.d.ts.map +1 -0
  140. package/dist/window/mount/plain.js +46 -0
  141. package/dist/{domain/window → window}/window.d.ts +1 -1
  142. package/dist/window/window.d.ts.map +1 -0
  143. package/dist/window/window.js +1 -0
  144. package/package.json +15 -7
  145. package/src/application/apps.ts +60 -0
  146. package/src/application/namespaces.ts +38 -0
  147. package/src/application/session/open.ts +73 -0
  148. package/src/application/session/session.ts +46 -0
  149. package/src/application/session/token.ts +87 -0
  150. package/src/application/spaces.ts +52 -0
  151. package/src/application/user.ts +54 -0
  152. package/src/application/view.ts +42 -0
  153. package/src/{application/channels/children-channel.ts → channel/children.ts} +3 -3
  154. package/src/{application/channels/types.ts → channel/listeners.ts} +2 -7
  155. package/src/{adapters/message-port.transport.ts → channel/message-port.ts} +2 -2
  156. package/src/{application/channels → channel}/observer.ts +2 -1
  157. package/src/{application/channels/parent-channel.ts → channel/parent.ts} +4 -4
  158. package/src/{ports → channel}/transport.ts +2 -1
  159. package/src/index.ts +80 -88
  160. package/src/{domain/intent → intent}/middleware/capabilities.ts +2 -3
  161. package/src/{domain/intent → intent}/middleware/logging.ts +1 -1
  162. package/src/{domain/intent → intent}/middleware/rate-limit.ts +1 -1
  163. package/src/{application/intent/pipeline-runner.ts → intent/pipeline.ts} +3 -2
  164. package/src/intent/policy.ts +46 -0
  165. package/src/{application/intent → intent}/request.ts +14 -34
  166. package/src/{application/intent → intent}/router.ts +12 -49
  167. package/src/kernel/client.ts +33 -0
  168. package/src/kernel/credential.ts +78 -0
  169. package/src/kernel/delegated.ts +49 -0
  170. package/src/{application/windowing/token-refresh.ts → kernel/delegation/refresh.ts} +2 -2
  171. package/src/{adapters/kernel-client.adapter.ts → kernel/session.ts} +26 -33
  172. package/src/message/ctrl.ts +25 -0
  173. package/src/{domain/message/errors.ts → message/error.ts} +8 -0
  174. package/src/message/intent.ts +98 -0
  175. package/src/message/message.ts +11 -0
  176. package/src/shell/access.ts +26 -0
  177. package/src/shell/build.ts +95 -0
  178. package/src/shell/config.ts +32 -0
  179. package/src/shell/create.ts +9 -0
  180. package/src/{application/windowing/child-init.ts → shell/sandboxed/boot.ts} +10 -11
  181. package/src/shell/sandboxed/guarded.ts +32 -0
  182. package/src/shell/sandboxed/pending.ts +52 -0
  183. package/src/shell/sandboxed/sandboxed.ts +131 -0
  184. package/src/shell/shell.ts +37 -0
  185. package/src/shell/standalone.ts +97 -0
  186. package/src/{domain/window/config.ts → window/capabilities.ts} +2 -2
  187. package/src/{ports/shell-adapter.ts → window/embed/adapter.ts} +2 -1
  188. package/src/{adapters/iframe.shell-adapter.ts → window/embed/iframe.ts} +2 -2
  189. package/src/{application/windowing/lifecycle-controller.ts → window/lifecycle/states.ts} +3 -2
  190. package/src/{domain/window/lifecycle.ts → window/lifecycle/transition.ts} +2 -2
  191. package/src/window/mount/close.ts +111 -0
  192. package/src/window/mount/credential.ts +101 -0
  193. package/src/{application/windowing → window/mount}/handshake.ts +3 -2
  194. package/src/window/mount/mount.ts +185 -0
  195. package/src/window/mount/params.ts +64 -0
  196. package/src/window/mount/plain.ts +68 -0
  197. package/src/{domain/window → window}/window.ts +1 -1
  198. package/dist/adapters/iframe.shell-adapter.d.ts.map +0 -1
  199. package/dist/adapters/kernel-client.adapter.d.ts.map +0 -1
  200. package/dist/adapters/message-port.transport.d.ts.map +0 -1
  201. package/dist/application/channels/children-channel.d.ts.map +0 -1
  202. package/dist/application/channels/observer.d.ts.map +0 -1
  203. package/dist/application/channels/parent-channel.d.ts.map +0 -1
  204. package/dist/application/channels/types.d.ts +0 -12
  205. package/dist/application/channels/types.d.ts.map +0 -1
  206. package/dist/application/intent/pipeline-runner.d.ts.map +0 -1
  207. package/dist/application/intent/request.d.ts.map +0 -1
  208. package/dist/application/intent/router.d.ts +0 -56
  209. package/dist/application/intent/router.d.ts.map +0 -1
  210. package/dist/application/kernel-facade.d.ts +0 -33
  211. package/dist/application/kernel-facade.d.ts.map +0 -1
  212. package/dist/application/kernel-facade.js +0 -77
  213. package/dist/application/shell.d.ts +0 -100
  214. package/dist/application/shell.d.ts.map +0 -1
  215. package/dist/application/shell.js +0 -256
  216. package/dist/application/windowing/child-init.d.ts.map +0 -1
  217. package/dist/application/windowing/handshake.d.ts.map +0 -1
  218. package/dist/application/windowing/lifecycle-controller.d.ts.map +0 -1
  219. package/dist/application/windowing/mount-controller.d.ts.map +0 -1
  220. package/dist/application/windowing/mount-controller.js +0 -277
  221. package/dist/application/windowing/protocol.d.ts.map +0 -1
  222. package/dist/application/windowing/token-refresh.d.ts.map +0 -1
  223. package/dist/domain/identity/delegation.d.ts.map +0 -1
  224. package/dist/domain/identity/path.d.ts +0 -11
  225. package/dist/domain/identity/path.d.ts.map +0 -1
  226. package/dist/domain/identity/path.js +0 -13
  227. package/dist/domain/identity/resolve-self.d.ts +0 -21
  228. package/dist/domain/identity/resolve-self.d.ts.map +0 -1
  229. package/dist/domain/identity/resolve-self.js +0 -36
  230. package/dist/domain/intent/middleware/capabilities.d.ts.map +0 -1
  231. package/dist/domain/intent/middleware/logging.d.ts.map +0 -1
  232. package/dist/domain/intent/middleware/rate-limit.d.ts.map +0 -1
  233. package/dist/domain/intent/pipeline.d.ts +0 -3
  234. package/dist/domain/intent/pipeline.d.ts.map +0 -1
  235. package/dist/domain/intent/router.d.ts +0 -6
  236. package/dist/domain/intent/router.d.ts.map +0 -1
  237. package/dist/domain/message/errors.d.ts +0 -2
  238. package/dist/domain/message/errors.d.ts.map +0 -1
  239. package/dist/domain/message/registries.d.ts.map +0 -1
  240. package/dist/domain/message/types.d.ts +0 -30
  241. package/dist/domain/message/types.d.ts.map +0 -1
  242. package/dist/domain/window/config.d.ts.map +0 -1
  243. package/dist/domain/window/lifecycle.d.ts.map +0 -1
  244. package/dist/domain/window/window.d.ts.map +0 -1
  245. package/dist/ports/kernel-client.d.ts +0 -33
  246. package/dist/ports/kernel-client.d.ts.map +0 -1
  247. package/dist/ports/shell-adapter.d.ts.map +0 -1
  248. package/dist/ports/transport.d.ts +0 -7
  249. package/dist/ports/transport.d.ts.map +0 -1
  250. package/src/application/kernel-facade.ts +0 -143
  251. package/src/application/shell.ts +0 -433
  252. package/src/application/windowing/mount-controller.ts +0 -335
  253. package/src/domain/identity/path.ts +0 -14
  254. package/src/domain/identity/resolve-self.ts +0 -38
  255. package/src/domain/intent/pipeline.ts +0 -3
  256. package/src/domain/intent/router.ts +0 -6
  257. package/src/domain/message/registries.ts +0 -76
  258. package/src/domain/message/types.ts +0 -32
  259. package/src/ports/kernel-client.ts +0 -32
  260. /package/dist/{application/channels/children-channel.js → channel/children.js} +0 -0
  261. /package/dist/{application/channels/types.js → channel/listeners.js} +0 -0
  262. /package/dist/{adapters/message-port.transport.js → channel/message-port.js} +0 -0
  263. /package/dist/{application/channels → channel}/observer.js +0 -0
  264. /package/dist/{ports → channel}/transport.js +0 -0
  265. /package/dist/{domain/intent → intent}/middleware/capabilities.js +0 -0
  266. /package/dist/{domain/intent → intent}/middleware/logging.d.ts +0 -0
  267. /package/dist/{domain/intent → intent}/middleware/logging.js +0 -0
  268. /package/dist/{domain/intent → intent}/middleware/rate-limit.js +0 -0
  269. /package/dist/{application/intent/pipeline-runner.js → intent/pipeline.js} +0 -0
  270. /package/dist/{domain/intent/pipeline.js → kernel/client.js} +0 -0
  271. /package/dist/{domain/identity/delegation.js → kernel/delegation/token.js} +0 -0
  272. /package/dist/{domain/intent/router.js → message/ctrl.js} +0 -0
  273. /package/dist/{domain/message/errors.js → message/error.js} +0 -0
  274. /package/dist/{application/windowing/protocol.js → message/init.js} +0 -0
  275. /package/dist/{domain/message/registries.js → message/intent.js} +0 -0
  276. /package/dist/{domain/message/types.js → message/message.js} +0 -0
  277. /package/dist/{domain/window → shell}/config.js +0 -0
  278. /package/dist/{domain/window/window.js → shell/shell.js} +0 -0
  279. /package/dist/{ports/kernel-client.js → window/capabilities.js} +0 -0
  280. /package/dist/{ports/shell-adapter.js → window/embed/adapter.js} +0 -0
  281. /package/dist/{adapters/iframe.shell-adapter.js → window/embed/iframe.js} +0 -0
  282. /package/dist/{application/windowing → window/mount}/handshake.d.ts +0 -0
  283. /package/src/{domain/identity/delegation.ts → kernel/delegation/token.ts} +0 -0
  284. /package/src/{application/windowing/protocol.ts → message/init.ts} +0 -0
@@ -1,335 +0,0 @@
1
- import type { Window } from '../../domain/window/window'
2
- import type { KernelClient } from '../../ports/kernel-client'
3
- import type { ShellAdapter, WindowHandle } from '../../ports/shell-adapter'
4
- import type { ChildrenChannelController } from '../channels/children-channel'
5
- import type { MountParams, MountedWindow, CloseResult } from '../shell'
6
- import type { LifecycleController } from './lifecycle-controller'
7
-
8
- import { createMessagePortTransport } from '../../adapters/message-port.transport'
9
- import { runParentHandshake } from './handshake'
10
- import { createTokenRefresher, type TokenRefresher } from './token-refresh'
11
-
12
- const DEFAULT_HANDSHAKE_TIMEOUT = 5_000
13
- const DEFAULT_DELEGATION_TTL = 3_600
14
- const DEFAULT_CLOSE_TIMEOUT = 3_000
15
- const PARENT_SENDER_ID = '<parent>'
16
-
17
- export type MountControllerConfig = {
18
- adapter: ShellAdapter
19
- kernel: KernelClient
20
- kernelUrl: string
21
- mintIdentity?: string
22
- childrenChannel: ChildrenChannelController
23
- windows: Map<string, Window>
24
- lifecycle: LifecycleController
25
- }
26
-
27
- export interface MountController {
28
- mount(params: MountParams): Promise<MountedWindow>
29
- close(windowId: string, opts?: { force?: boolean; timeoutMs?: number }): Promise<CloseResult>
30
- dispose(): Promise<void>
31
- }
32
-
33
- type Entry = {
34
- readonly handle: WindowHandle
35
- readonly dispose: () => void
36
- readonly refresher: TokenRefresher
37
- /** Plain (no-handshake) mount: no transport, no token, no close ack. */
38
- readonly plain?: boolean
39
- }
40
-
41
- /** No-op refresher for plain mounts (no token to refresh). */
42
- const NOOP_REFRESHER: TokenRefresher = {
43
- start: () => {},
44
- stop: () => {},
45
- forceRefreshNow: () => Promise.resolve(),
46
- }
47
-
48
- export function createMountController(config: MountControllerConfig): MountController {
49
- const { adapter, kernel, kernelUrl, mintIdentity, childrenChannel, windows, lifecycle } = config
50
- // The child iframe is always a DIFFERENT origin from this parent window, so a
51
- // relative kernelUrl (e.g. the SPA's same-origin `/api/`) handed across the
52
- // handshake would resolve against the iframe's OWN origin and POST to the
53
- // wrong host. Absolutize it against the parent's location once so every child
54
- // calls THIS origin's kernel. (The parent's own calls keep using the relative
55
- // form via its kernel client / fetch.)
56
- const childKernelUrl = new URL(kernelUrl, window.location.href).href
57
- const entries = new Map<string, Entry>()
58
- let seq = 0
59
- let disposed = false
60
-
61
- function nextWindowId(functionId: string) {
62
- seq += 1
63
- const slug = functionId.replace(/[^a-z0-9]/gi, '').slice(-8) || 'fn'
64
- return `win-${slug}-${seq}`
65
- }
66
-
67
- async function mint(ttlSeconds: number) {
68
- const credential = await kernel.mintDelegation()
69
- return { credential, expiresAt: Date.now() + ttlSeconds * 1_000 }
70
- }
71
-
72
- function originOf(url: string): string {
73
- try {
74
- return new URL(url, window.location.href).origin
75
- } catch {
76
- throw new Error(`Invalid mount url: ${url}`)
77
- }
78
- }
79
-
80
- // Plain-iframe mount path — for hosted views that don't speak the shell
81
- // protocol. No handshake, no transport, no delegation token, no refresher;
82
- // the iframe just renders. `close()` skips the ack request (no channel) and
83
- // falls straight to dispose.
84
- async function mountPlain(params: MountParams): Promise<MountedWindow> {
85
- const windowId = nextWindowId(params.functionId)
86
- const { handle, dispose: disposeHandle } = adapter.mount({
87
- functionId: params.functionId,
88
- isolation: 'sandboxed',
89
- capabilities: params.capabilities,
90
- sandbox: params.sandbox,
91
- })
92
- if (!(handle.element instanceof HTMLIFrameElement)) {
93
- disposeHandle()
94
- throw new Error('Shell adapter did not produce an HTMLIFrameElement')
95
- }
96
- params.host.appendChild(handle.element)
97
- handle.element.src = params.url
98
-
99
- windows.set(windowId, {
100
- windowId,
101
- functionId: params.functionId,
102
- targetNodeId: params.targetNodeId,
103
- isolation: 'sandboxed',
104
- state: 'active',
105
- delegationToken: null,
106
- capabilities: params.capabilities,
107
- })
108
- lifecycle.register(windowId, 'active')
109
- entries.set(windowId, {
110
- handle,
111
- dispose: disposeHandle,
112
- refresher: NOOP_REFRESHER,
113
- plain: true,
114
- })
115
-
116
- return {
117
- windowId,
118
- get window() {
119
- return windows.get(windowId)!
120
- },
121
- handle: { element: handle.element },
122
- close: (opts) => controller.close(windowId, opts),
123
- }
124
- }
125
-
126
- const controller: MountController = {
127
- async mount(params) {
128
- if (disposed) throw new Error('MountController is disposed')
129
-
130
- // Plain (no-handshake) path: hosted views that don't speak the shell
131
- // protocol. Skip mint/handshake/transport/refresher; the iframe still
132
- // renders and `close()` falls through to dispose.
133
- if (params.handshake === 'none') return mountPlain(params)
134
-
135
- const ttlSeconds = params.delegationTtlSeconds ?? DEFAULT_DELEGATION_TTL
136
- const handshakeTimeout = params.handshakeTimeoutMs ?? DEFAULT_HANDSHAKE_TIMEOUT
137
- const expectedOrigin = originOf(params.url)
138
- const windowId = nextWindowId(params.functionId)
139
-
140
- // Mint the token and create the iframe in parallel — the iframe
141
- // doesn't need the credential until the handshake fires.
142
- const [{ credential, expiresAt }, mounted] = await Promise.all([
143
- mint(ttlSeconds),
144
- Promise.resolve(
145
- adapter.mount({
146
- functionId: params.functionId,
147
- isolation: 'sandboxed',
148
- capabilities: params.capabilities,
149
- sandbox: params.sandbox,
150
- }),
151
- ),
152
- ])
153
- const { handle, dispose: disposeHandle } = mounted
154
-
155
- if (!(handle.element instanceof HTMLIFrameElement)) {
156
- disposeHandle()
157
- throw new Error('Shell adapter did not produce an HTMLIFrameElement')
158
- }
159
-
160
- params.host.appendChild(handle.element)
161
- handle.element.src = params.url
162
-
163
- let handshakeResult
164
- try {
165
- handshakeResult = await runParentHandshake({
166
- iframe: handle.element,
167
- expectedOrigin,
168
- windowId,
169
- delegationToken: credential,
170
- tokenExpiresAt: expiresAt,
171
- kernelUrl: childKernelUrl,
172
- functionId: params.functionId,
173
- targetNodeId: params.targetNodeId,
174
- mintIdentity,
175
- timeoutMs: handshakeTimeout,
176
- })
177
- } catch (err) {
178
- disposeHandle()
179
- throw err
180
- }
181
-
182
- const transport = createMessagePortTransport({
183
- port: handshakeResult.port,
184
- onClose: () => {
185
- if (!entries.has(windowId)) return
186
- lifecycle.markCrashed(windowId)
187
- void controller.close(windowId, { force: true }).catch(() => {})
188
- },
189
- })
190
-
191
- childrenChannel.attach(windowId, transport)
192
- windows.set(windowId, {
193
- windowId,
194
- functionId: params.functionId,
195
- targetNodeId: params.targetNodeId,
196
- isolation: 'sandboxed',
197
- state: 'active',
198
- delegationToken: credential,
199
- capabilities: params.capabilities,
200
- })
201
- lifecycle.register(windowId, 'active')
202
-
203
- // The live expiry advances on every refresh. The Window store only keeps
204
- // the credential, so track expiry here — otherwise `token()` keeps
205
- // returning the original expiry and `schedule()` collapses its lead time
206
- // to 0, busy-looping `mint()` forever after the first refresh.
207
- let currentExpiresAt = expiresAt
208
- const refresher = createTokenRefresher({
209
- functionId: params.functionId,
210
- token: () => {
211
- const stored = windows.get(windowId)
212
- return {
213
- credential: stored?.delegationToken ?? credential,
214
- expiresAt: currentExpiresAt,
215
- issuedAt: 0,
216
- scope: { functionId: params.functionId },
217
- }
218
- },
219
- setToken: (next) => {
220
- currentExpiresAt = next.expiresAt
221
- const stored = windows.get(windowId)
222
- if (stored) {
223
- windows.set(windowId, { ...stored, delegationToken: next.credential })
224
- }
225
- childrenChannel.send(windowId, {
226
- type: 'ctrl',
227
- version: 1,
228
- action: 'tokenRefresh',
229
- data: { delegationToken: next.credential, tokenExpiresAt: next.expiresAt },
230
- })
231
- },
232
- mint: () => mint(ttlSeconds),
233
- onFailure: () => {
234
- void controller.close(windowId, { force: false }).catch(() => {})
235
- },
236
- })
237
- refresher.start()
238
-
239
- entries.set(windowId, { handle, dispose: disposeHandle, refresher })
240
-
241
- return {
242
- windowId,
243
- get window() {
244
- return windows.get(windowId)!
245
- },
246
- handle: { element: handle.element },
247
- close: (opts) => controller.close(windowId, opts),
248
- }
249
- },
250
-
251
- async close(windowId, opts) {
252
- const entry = entries.get(windowId)
253
- if (!entry) return { kind: 'noop' }
254
-
255
- const timeoutMs = opts?.timeoutMs ?? DEFAULT_CLOSE_TIMEOUT
256
-
257
- // Plain mounts have no channel to ack a close; always force.
258
- if (!opts?.force && !entry.plain) {
259
- // Move the window into `closing` so the state machine accepts the
260
- // subsequent `closeRefuse`/`forceClose` transition (both are only valid
261
- // from `closing`).
262
- lifecycle.apply(windowId, { kind: 'requestClose' })
263
- const accepted = await requestCloseAck(childrenChannel, windowId, timeoutMs)
264
- if (accepted.kind === 'refused') {
265
- lifecycle.apply(windowId, { kind: 'closeRefuse' })
266
- return { kind: 'refused', reason: accepted.reason }
267
- }
268
- }
269
-
270
- lifecycle.apply(windowId, { kind: 'forceClose' })
271
- entry.refresher.stop()
272
- // Drop the entry before detaching: `detach` synchronously closes the
273
- // transport, whose `onClose` re-enters `close()`. Removing the entry first
274
- // makes that re-entrant call's `entries.has(windowId)` guard short-circuit,
275
- // so `entry.dispose()` runs exactly once (adapters need not be idempotent).
276
- entries.delete(windowId)
277
- childrenChannel.detach(windowId)
278
- windows.delete(windowId)
279
- entry.dispose()
280
- lifecycle.remove(windowId)
281
- return { kind: 'closed' }
282
- },
283
-
284
- async dispose() {
285
- if (disposed) return
286
- disposed = true
287
- const ids = [...entries.keys()]
288
- await Promise.all(ids.map((id) => controller.close(id, { force: true })))
289
- },
290
- }
291
-
292
- return controller
293
- }
294
-
295
- type CloseAck = { kind: 'ack' } | { kind: 'refused'; reason?: string } | { kind: 'timeout' }
296
-
297
- async function requestCloseAck(
298
- children: ChildrenChannelController,
299
- windowId: string,
300
- timeoutMs: number,
301
- ): Promise<CloseAck> {
302
- return new Promise((resolve) => {
303
- let settled = false
304
- const off = children.on('intent', (from, message) => {
305
- if (from !== windowId) return
306
- if (message.envelope.name === 'closeAck') {
307
- settled = true
308
- off()
309
- clearTimeout(timer)
310
- resolve({ kind: 'ack' })
311
- } else if (message.envelope.name === 'closeRefuse') {
312
- settled = true
313
- off()
314
- clearTimeout(timer)
315
- const reason = (message.envelope.payload as { reason?: string }).reason
316
- resolve({ kind: 'refused', reason })
317
- }
318
- })
319
- const timer = setTimeout(() => {
320
- if (settled) return
321
- off()
322
- resolve({ kind: 'timeout' })
323
- }, timeoutMs)
324
-
325
- children.send(windowId, {
326
- type: 'intent',
327
- version: 1,
328
- envelope: {
329
- name: 'willClose',
330
- payload: {},
331
- sender: { windowId: PARENT_SENDER_ID },
332
- },
333
- })
334
- })
335
- }
@@ -1,14 +0,0 @@
1
- /**
2
- * Built-in kernel root identity, used as the default mint subject when no
3
- * explicit `mintIdentity` is configured.
4
- */
5
- export const SYSTEM_IDENTITY = '__system__'
6
-
7
- /**
8
- * Address a method on an identity or path: `@<id>::method` for a bare node id,
9
- * `<path>::method` for an absolute path (one starting with `/`).
10
- */
11
- export function identityMethod(identity: string, method: string): string {
12
- const target = identity.startsWith('/') ? identity : `@${identity}`
13
- return `${target}::${method}`
14
- }
@@ -1,38 +0,0 @@
1
- import { SYSTEM_IDENTITY } from './path'
2
-
3
- /** Kernel path that returns the AUTHENTICATED caller's own identity node. */
4
- export const WHOAMI_PATH = '/:kernel.astrale.ai:interface.Identity:whoami'
5
-
6
- /**
7
- * Resolve the mint SUBJECT — the identity node a delegation/credential should be
8
- * minted for. It MUST be the principal the kernel actually authenticates this
9
- * caller as, otherwise the minted token impersonates someone else.
10
- *
11
- * Why a kernel round-trip and not the local credential: a cookie-fronted SPA
12
- * holds an EMPTY credential string (the session token lives in an httpOnly
13
- * cookie it can't read), so the subject cannot be derived locally. We ask the
14
- * kernel via `whoami`; that request carries the cookie, so the router unseals it
15
- * and the kernel answers with the logged-in user's node. Without this the shell
16
- * defaulted every mint to `__system__`, so a View iframe mounted by a logged-in
17
- * user acted as the kernel ROOT instead of as that user.
18
- *
19
- * `fallback` (the configured `mintIdentity`, default `__system__`) is used when
20
- * whoami can't be resolved — a genuinely system/dev caller, or an unauthenticated
21
- * boot. The result is cached: the principal is stable for a session.
22
- */
23
- export function createSelfResolver(
24
- fallback: string = SYSTEM_IDENTITY,
25
- ): (whoami: () => Promise<unknown>) => Promise<string> {
26
- let cached: string | undefined
27
- return async (whoami) => {
28
- if (cached !== undefined) return cached
29
- try {
30
- const me = (await whoami()) as { id?: unknown } | null
31
- const id = me && typeof me.id === 'string' && me.id.length > 0 ? me.id : undefined
32
- cached = id ?? fallback
33
- } catch {
34
- cached = fallback
35
- }
36
- return cached
37
- }
38
- }
@@ -1,3 +0,0 @@
1
- import type { IntentMessage } from '../message/types'
2
-
3
- export type IntentMiddleware = (message: IntentMessage, next: () => Promise<void>) => Promise<void>
@@ -1,6 +0,0 @@
1
- import type { IntentRegistry } from '../message/registries'
2
- import type { IntentMessage } from '../message/types'
3
-
4
- export interface IntentRouter {
5
- dispatch<K extends keyof IntentRegistry>(message: IntentMessage<K>): Promise<void>
6
- }
@@ -1,76 +0,0 @@
1
- export interface ViewRef {
2
- readonly viewId: string
3
- readonly functionId: string
4
- readonly label?: string
5
- }
6
-
7
- /**
8
- * Intent registry — extensible via declaration merging.
9
- *
10
- * Consumers extend via:
11
- * declare module '@astrale-os/shell' {
12
- * interface IntentRegistry { ... }
13
- * }
14
- */
15
- export interface IntentRegistry {
16
- open: { payload: { nodeId: string; viewId?: string }; response: { windowId: string } }
17
- close: { payload: { windowId: string }; response: void }
18
- focus: { payload: { nodeId: string }; response: void }
19
- willClose: { payload: Record<string, never>; response: void }
20
- closeAck: { payload: Record<string, never>; response: void }
21
- closeRefuse: { payload: { reason: string }; response: void }
22
- pickView: { payload: { views: ViewRef[] }; response: { viewId: string } }
23
- receive: { payload: { data: unknown; sourceIntent: string }; response: void }
24
- /**
25
- * Hot-swap — parent pushes a new target node to an already-mounted view
26
- * without remounting. The child reads `targetNodeId` at handshake time
27
- * for the initial value; `setTarget` replaces it for the lifetime of the
28
- * window.
29
- */
30
- setTarget: { payload: { nodeId: string }; response: void }
31
- /**
32
- * Child → parent: a view asks the user to grant `perms` (a permission
33
- * bitmask) on `node` to `identity` (the beneficiary subject). Both `node`
34
- * and `identity` are bare node ids (e.g. the view's `targetNodeId`, or
35
- * `whoami().id` for "grant to myself") — the root shell addresses them as
36
- * `@<id>`. The root shell (GUI) surfaces a modal, computes the delta vs.
37
- * what the subject already holds, and — on approval — grants only the
38
- * missing bits under the GUI user's own credential. `reason` is shown to
39
- * the user. `response.perms` is the bitmask actually granted (0 if
40
- * declined / nothing was missing).
41
- *
42
- * Reaches a handler only from a view mounted as a DIRECT child of the root
43
- * shell — the default intent policy does not bubble past one hop.
44
- */
45
- requestAccess: {
46
- payload: { node: string; identity: string; perms: number; reason?: string }
47
- response: { granted: boolean; perms: number }
48
- }
49
- /**
50
- * Generic correlated reply channel. The default intent transport is
51
- * fire-and-forget; `requestIntent()` sends an intent carrying a
52
- * `correlationId` and awaits an `intentReply` whose payload echoes it back
53
- * with the handler's `result`. Routed to the resolver purely by
54
- * `correlationId` — it intentionally matches no local handler.
55
- */
56
- intentReply: { payload: { correlationId: string; result: unknown }; response: void }
57
- }
58
-
59
- /**
60
- * Control message registry — extensible via declaration merging.
61
- */
62
- export interface CtrlRegistry {
63
- handshake: {
64
- data: {
65
- delegationToken: string
66
- tokenExpiresAt: number
67
- windowId: string
68
- kernelUrl: string
69
- functionId: string
70
- targetNodeId?: string
71
- mintIdentity?: string
72
- }
73
- }
74
- handshakeAck: { data: { windowId: string } }
75
- tokenRefresh: { data: { delegationToken: string; tokenExpiresAt: number } }
76
- }
@@ -1,32 +0,0 @@
1
- import type { ErrorCode } from './errors'
2
- import type { CtrlRegistry, IntentRegistry } from './registries'
3
-
4
- export type IntentEnvelope<K extends keyof IntentRegistry = keyof IntentRegistry> = {
5
- readonly name: K
6
- readonly payload: IntentRegistry[K]['payload']
7
- readonly sender: { readonly windowId: string }
8
- readonly correlationId?: string
9
- }
10
-
11
- export type IntentMessage<K extends keyof IntentRegistry = keyof IntentRegistry> = {
12
- readonly type: 'intent'
13
- readonly version: 1
14
- readonly envelope: IntentEnvelope<K>
15
- }
16
-
17
- export type CtrlMessage<K extends keyof CtrlRegistry = keyof CtrlRegistry> = {
18
- readonly type: 'ctrl'
19
- readonly version: 1
20
- readonly action: K
21
- readonly data: CtrlRegistry[K]['data']
22
- }
23
-
24
- export type ErrorMessage = {
25
- readonly type: 'error'
26
- readonly version: 1
27
- readonly code: ErrorCode
28
- readonly message: string
29
- readonly context?: unknown
30
- }
31
-
32
- export type ShellMessage = IntentMessage | CtrlMessage | ErrorMessage
@@ -1,32 +0,0 @@
1
- export interface KernelClient {
2
- call(path: string, params: unknown): Promise<unknown>
3
- /**
4
- * Produce a credential usable by a child shell.
5
- *
6
- * v1: returns a fresh identity credential scoped to the current principal.
7
- * Future (SDK v2): will accept `{ functionId }` to scope the delegation
8
- * to a specific Function — tracked in `.dev/todo/shell-sdk-v2-delegation.md`.
9
- */
10
- mintDelegation(): Promise<string>
11
- /**
12
- * Return the JWT this shell currently uses to authenticate `call`. Iframes
13
- * use it to make their own authenticated HTTP requests to the kernel —
14
- * e.g. when a third-party SDK like Vercel AI's `useChat` issues a raw
15
- * `fetch()` that can't run through `call()`. Callers form the Authorization
16
- * header themselves: `Authorization: \`Bearer ${shell.kernel.authToken()}\``.
17
- *
18
- * The value is the *current* token; the parent shell auto-refreshes it
19
- * before expiry, so always read fresh on each request rather than caching.
20
- */
21
- authToken(): string
22
- /**
23
- * URL of the kernel this shell talks to. Pair with `authToken()` when an
24
- * iframe needs to make a raw `fetch()` from a third-party SDK (e.g.
25
- * Vercel AI's `useChat`): the kernel-issued JWT's `aud` matches the
26
- * kernel's own issuer, so requests must target the kernel directly. If
27
- * a sandboxed iframe POSTs to its own `window.location.origin` (the
28
- * worker hosting the iframe), the worker's audience check rejects the
29
- * cred with "Credential audience mismatch".
30
- */
31
- readonly url: string
32
- }
File without changes
File without changes