@agent-native/dispatch 0.43.0-nightly-20261120212328 → 0.43.7

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 (296) hide show
  1. package/CHANGELOG.md +194 -174
  2. package/dist/actions/ask_app.d.ts +3 -0
  3. package/dist/actions/ask_app.js +7 -2
  4. package/dist/actions/ask_app.js.map +1 -1
  5. package/dist/actions/ask_app_status.d.ts +3 -0
  6. package/dist/actions/ask_app_status.js +5 -1
  7. package/dist/actions/ask_app_status.js.map +1 -1
  8. package/dist/actions/call_app_action.d.ts +1 -0
  9. package/dist/actions/call_app_action.js +21 -5
  10. package/dist/actions/call_app_action.js.map +1 -1
  11. package/dist/actions/create_embed_session.d.ts +3 -2
  12. package/dist/actions/create_embed_session.js +4 -2
  13. package/dist/actions/create_embed_session.js.map +1 -1
  14. package/dist/actions/create_workspace_app_embed_session.d.ts +3 -2
  15. package/dist/actions/create_workspace_app_embed_session.js +3 -1
  16. package/dist/actions/create_workspace_app_embed_session.js.map +1 -1
  17. package/dist/actions/delete-destination.d.ts +12 -12
  18. package/dist/actions/execute-action-suggestion.d.ts +44 -0
  19. package/dist/actions/execute-action-suggestion.d.ts.map +1 -0
  20. package/dist/actions/execute-action-suggestion.js +148 -0
  21. package/dist/actions/execute-action-suggestion.js.map +1 -0
  22. package/dist/actions/import-agent.d.ts +13 -13
  23. package/dist/actions/index.d.ts.map +1 -1
  24. package/dist/actions/index.js +5 -1
  25. package/dist/actions/index.js.map +1 -1
  26. package/dist/actions/list-connected-agents.js +3 -2
  27. package/dist/actions/list-connected-agents.js.map +1 -1
  28. package/dist/actions/list-workspace-connections.d.ts +1 -0
  29. package/dist/actions/list-workspace-connections.d.ts.map +1 -1
  30. package/dist/actions/list_app_actions.js +1 -1
  31. package/dist/actions/list_app_actions.js.map +1 -1
  32. package/dist/actions/list_apps.d.ts +2 -0
  33. package/dist/actions/list_apps.js +11 -3
  34. package/dist/actions/list_apps.js.map +1 -1
  35. package/dist/actions/navigate.js +1 -0
  36. package/dist/actions/navigate.js.map +1 -1
  37. package/dist/actions/open_app.d.ts +4 -0
  38. package/dist/actions/open_app.js +14 -6
  39. package/dist/actions/open_app.js.map +1 -1
  40. package/dist/actions/propose-action-suggestions.d.ts +45 -0
  41. package/dist/actions/propose-action-suggestions.d.ts.map +1 -0
  42. package/dist/actions/propose-action-suggestions.js +32 -0
  43. package/dist/actions/propose-action-suggestions.js.map +1 -0
  44. package/dist/actions/provider-api-register.d.ts +12 -12
  45. package/dist/actions/read_view.js +1 -1
  46. package/dist/actions/read_view.js.map +1 -1
  47. package/dist/actions/remember-pointer.js +1 -1
  48. package/dist/actions/remember-pointer.js.map +1 -1
  49. package/dist/actions/resolve-pointer.js +1 -1
  50. package/dist/actions/resolve-pointer.js.map +1 -1
  51. package/dist/actions/route-app-intent.d.ts +6 -0
  52. package/dist/actions/route-app-intent.js +79 -5
  53. package/dist/actions/route-app-intent.js.map +1 -1
  54. package/dist/actions/send-code-agent-remote-command.js +4 -0
  55. package/dist/actions/send-code-agent-remote-command.js.map +1 -1
  56. package/dist/actions/start-workspace-app-creation.js +1 -1
  57. package/dist/actions/start-workspace-app-creation.js.map +1 -1
  58. package/dist/actions/upsert-destination.d.ts +12 -12
  59. package/dist/actions/view-screen.d.ts +12 -1
  60. package/dist/actions/view-screen.js +53 -5
  61. package/dist/actions/view-screen.js.map +1 -1
  62. package/dist/components/chat-action-suggestions.d.ts +8 -0
  63. package/dist/components/chat-action-suggestions.d.ts.map +1 -0
  64. package/dist/components/chat-action-suggestions.js +191 -0
  65. package/dist/components/chat-action-suggestions.js.map +1 -0
  66. package/dist/components/chat-home-apps.d.ts.map +1 -1
  67. package/dist/components/chat-home-apps.js +1 -1
  68. package/dist/components/chat-home-apps.js.map +1 -1
  69. package/dist/components/google-broker-settings-group.d.ts +2 -0
  70. package/dist/components/google-broker-settings-group.d.ts.map +1 -0
  71. package/dist/components/google-broker-settings-group.js +53 -0
  72. package/dist/components/google-broker-settings-group.js.map +1 -0
  73. package/dist/components/layout/Layout.d.ts +4 -1
  74. package/dist/components/layout/Layout.d.ts.map +1 -1
  75. package/dist/components/layout/Layout.js +246 -69
  76. package/dist/components/layout/Layout.js.map +1 -1
  77. package/dist/components/messaging-setup-panel.d.ts.map +1 -1
  78. package/dist/components/messaging-setup-panel.js +47 -15
  79. package/dist/components/messaging-setup-panel.js.map +1 -1
  80. package/dist/components/workspace-app-host.d.ts +27 -4
  81. package/dist/components/workspace-app-host.d.ts.map +1 -1
  82. package/dist/components/workspace-app-host.js +355 -74
  83. package/dist/components/workspace-app-host.js.map +1 -1
  84. package/dist/config.d.ts +5 -0
  85. package/dist/config.d.ts.map +1 -1
  86. package/dist/config.js.map +1 -1
  87. package/dist/db/google-broker-admission.d.ts +36 -0
  88. package/dist/db/google-broker-admission.d.ts.map +1 -0
  89. package/dist/db/google-broker-admission.js +129 -0
  90. package/dist/db/google-broker-admission.js.map +1 -0
  91. package/dist/db/migrations.d.ts +1 -1
  92. package/dist/db/migrations.d.ts.map +1 -1
  93. package/dist/db/migrations.js +42 -0
  94. package/dist/db/migrations.js.map +1 -1
  95. package/dist/lib/action-suggestions.d.ts +20 -0
  96. package/dist/lib/action-suggestions.d.ts.map +1 -0
  97. package/dist/lib/action-suggestions.js +219 -0
  98. package/dist/lib/action-suggestions.js.map +1 -0
  99. package/dist/lib/chat-first-app-route.d.ts +11 -5
  100. package/dist/lib/chat-first-app-route.d.ts.map +1 -1
  101. package/dist/lib/chat-first-app-route.js +9 -5
  102. package/dist/lib/chat-first-app-route.js.map +1 -1
  103. package/dist/lib/chat-first-pane.d.ts +25 -1
  104. package/dist/lib/chat-first-pane.d.ts.map +1 -1
  105. package/dist/lib/chat-first-pane.js +67 -1
  106. package/dist/lib/chat-first-pane.js.map +1 -1
  107. package/dist/lib/data-intent.d.ts +3 -0
  108. package/dist/lib/data-intent.d.ts.map +1 -0
  109. package/dist/lib/data-intent.js +14 -0
  110. package/dist/lib/data-intent.js.map +1 -0
  111. package/dist/lib/dispatch-app-identity.d.ts +10 -0
  112. package/dist/lib/dispatch-app-identity.d.ts.map +1 -0
  113. package/dist/lib/dispatch-app-identity.js +18 -0
  114. package/dist/lib/dispatch-app-identity.js.map +1 -0
  115. package/dist/lib/other-apps.d.ts +15 -0
  116. package/dist/lib/other-apps.d.ts.map +1 -1
  117. package/dist/lib/other-apps.js +31 -0
  118. package/dist/lib/other-apps.js.map +1 -1
  119. package/dist/lib/workspace-app-frame-view.d.ts +15 -0
  120. package/dist/lib/workspace-app-frame-view.d.ts.map +1 -0
  121. package/dist/lib/workspace-app-frame-view.js +72 -0
  122. package/dist/lib/workspace-app-frame-view.js.map +1 -0
  123. package/dist/lib/workspace-apps.d.ts +5 -0
  124. package/dist/lib/workspace-apps.d.ts.map +1 -1
  125. package/dist/lib/workspace-apps.js +85 -23
  126. package/dist/lib/workspace-apps.js.map +1 -1
  127. package/dist/lib/workspace-apps.test.js +6 -0
  128. package/dist/lib/workspace-apps.test.js.map +1 -1
  129. package/dist/routes/pages/_index.d.ts +2 -2
  130. package/dist/routes/pages/chat.d.ts.map +1 -1
  131. package/dist/routes/pages/chat.js +33 -2
  132. package/dist/routes/pages/chat.js.map +1 -1
  133. package/dist/routes/pages/settings-page.d.ts +1 -1
  134. package/dist/routes/pages/settings-page.d.ts.map +1 -1
  135. package/dist/routes/pages/settings-page.js +3 -2
  136. package/dist/routes/pages/settings-page.js.map +1 -1
  137. package/dist/server/index.d.ts +4 -1
  138. package/dist/server/index.d.ts.map +1 -1
  139. package/dist/server/index.js +5 -5
  140. package/dist/server/index.js.map +1 -1
  141. package/dist/server/lib/action-suggestions.d.ts +41 -0
  142. package/dist/server/lib/action-suggestions.d.ts.map +1 -0
  143. package/dist/server/lib/action-suggestions.js +94 -0
  144. package/dist/server/lib/action-suggestions.js.map +1 -0
  145. package/dist/server/lib/app-action-result.d.ts +5 -0
  146. package/dist/server/lib/app-action-result.d.ts.map +1 -1
  147. package/dist/server/lib/app-action-result.js +7 -0
  148. package/dist/server/lib/app-action-result.js.map +1 -1
  149. package/dist/server/lib/app-creation-store.d.ts.map +1 -1
  150. package/dist/server/lib/app-creation-store.js +63 -13
  151. package/dist/server/lib/app-creation-store.js.map +1 -1
  152. package/dist/server/lib/curated-workspace-templates.d.ts.map +1 -1
  153. package/dist/server/lib/curated-workspace-templates.js +3 -0
  154. package/dist/server/lib/curated-workspace-templates.js.map +1 -1
  155. package/dist/server/lib/dispatch-config.d.ts +5 -0
  156. package/dist/server/lib/dispatch-config.d.ts.map +1 -0
  157. package/dist/server/lib/dispatch-config.js +11 -0
  158. package/dist/server/lib/dispatch-config.js.map +1 -0
  159. package/dist/server/lib/dispatch-pane-context.d.ts +11 -0
  160. package/dist/server/lib/dispatch-pane-context.d.ts.map +1 -0
  161. package/dist/server/lib/dispatch-pane-context.js +35 -0
  162. package/dist/server/lib/dispatch-pane-context.js.map +1 -0
  163. package/dist/server/lib/google-broker-provider.d.ts +41 -0
  164. package/dist/server/lib/google-broker-provider.d.ts.map +1 -0
  165. package/dist/server/lib/google-broker-provider.js +96 -0
  166. package/dist/server/lib/google-broker-provider.js.map +1 -0
  167. package/dist/server/lib/google-broker-rpc.d.ts +26 -0
  168. package/dist/server/lib/google-broker-rpc.d.ts.map +1 -0
  169. package/dist/server/lib/google-broker-rpc.js +52 -0
  170. package/dist/server/lib/google-broker-rpc.js.map +1 -0
  171. package/dist/server/lib/hosted-apps-settings.d.ts.map +1 -1
  172. package/dist/server/lib/hosted-apps-settings.js +4 -0
  173. package/dist/server/lib/hosted-apps-settings.js.map +1 -1
  174. package/dist/server/lib/mcp-app-discovery.d.ts +17 -0
  175. package/dist/server/lib/mcp-app-discovery.d.ts.map +1 -0
  176. package/dist/server/lib/mcp-app-discovery.js +231 -0
  177. package/dist/server/lib/mcp-app-discovery.js.map +1 -0
  178. package/dist/server/lib/mcp-gateway.d.ts +64 -21
  179. package/dist/server/lib/mcp-gateway.d.ts.map +1 -1
  180. package/dist/server/lib/mcp-gateway.js +730 -88
  181. package/dist/server/lib/mcp-gateway.js.map +1 -1
  182. package/dist/server/plugins/agent-chat.js +9 -7
  183. package/dist/server/plugins/agent-chat.js.map +1 -1
  184. package/dist/server/plugins/auth.d.ts.map +1 -1
  185. package/dist/server/plugins/auth.js +1 -0
  186. package/dist/server/plugins/auth.js.map +1 -1
  187. package/dist/server/plugins/db.d.ts.map +1 -1
  188. package/dist/server/plugins/db.js +0 -46
  189. package/dist/server/plugins/db.js.map +1 -1
  190. package/dist/server/plugins/integrations.d.ts.map +1 -1
  191. package/dist/server/plugins/integrations.js +4 -1
  192. package/dist/server/plugins/integrations.js.map +1 -1
  193. package/dist/shared/chat-first-pane.d.ts +24 -0
  194. package/dist/shared/chat-first-pane.d.ts.map +1 -1
  195. package/dist/shared/chat-first-pane.js +34 -0
  196. package/dist/shared/chat-first-pane.js.map +1 -1
  197. package/dist/shared/labs.d.ts +1 -0
  198. package/dist/shared/labs.d.ts.map +1 -1
  199. package/dist/shared/labs.js +7 -0
  200. package/dist/shared/labs.js.map +1 -1
  201. package/package.json +7 -6
  202. package/src/actions/action-suggestions.spec.ts +342 -0
  203. package/src/actions/app_actions.spec.ts +103 -0
  204. package/src/actions/ask_app.ts +9 -2
  205. package/src/actions/ask_app_status.ts +12 -1
  206. package/src/actions/call_app_action.ts +28 -8
  207. package/src/actions/create_embed_session.ts +8 -2
  208. package/src/actions/create_workspace_app_embed_session.ts +6 -1
  209. package/src/actions/execute-action-suggestion.ts +177 -0
  210. package/src/actions/index.spec.ts +6 -3
  211. package/src/actions/index.ts +5 -1
  212. package/src/actions/list-connected-agents.spec.ts +26 -1
  213. package/src/actions/list-connected-agents.ts +7 -4
  214. package/src/actions/list_app_actions.ts +1 -1
  215. package/src/actions/list_apps.spec.ts +58 -22
  216. package/src/actions/list_apps.ts +12 -5
  217. package/src/actions/navigate.ts +1 -0
  218. package/src/actions/open_app.spec.ts +17 -1
  219. package/src/actions/open_app.ts +21 -8
  220. package/src/actions/propose-action-suggestions.ts +42 -0
  221. package/src/actions/read_view.ts +1 -1
  222. package/src/actions/remember-pointer.ts +1 -1
  223. package/src/actions/resolve-pointer.ts +1 -1
  224. package/src/actions/route-app-intent.spec.ts +234 -5
  225. package/src/actions/route-app-intent.ts +89 -4
  226. package/src/actions/send-code-agent-remote-command.spec.ts +40 -0
  227. package/src/actions/send-code-agent-remote-command.ts +5 -0
  228. package/src/actions/start-workspace-app-creation.ts +1 -1
  229. package/src/actions/view-screen.bundle.spec.ts +15 -2
  230. package/src/actions/view-screen.spec.ts +81 -3
  231. package/src/actions/view-screen.ts +60 -4
  232. package/src/components/chat-action-suggestions.spec.tsx +362 -0
  233. package/src/components/chat-action-suggestions.tsx +432 -0
  234. package/src/components/chat-home-apps.spec.tsx +19 -0
  235. package/src/components/chat-home-apps.tsx +0 -1
  236. package/src/components/google-broker-settings-group.tsx +186 -0
  237. package/src/components/layout/Layout.spec.tsx +419 -28
  238. package/src/components/layout/Layout.tsx +304 -66
  239. package/src/components/messaging-setup-panel.spec.tsx +278 -4
  240. package/src/components/messaging-setup-panel.tsx +82 -19
  241. package/src/components/workspace-app-card.spec.tsx +79 -10
  242. package/src/components/workspace-app-chat-rail.spec.tsx +81 -3
  243. package/src/components/workspace-app-host.spec.tsx +269 -16
  244. package/src/components/workspace-app-host.tsx +464 -87
  245. package/src/config.ts +5 -0
  246. package/src/db/google-broker-admission.ts +160 -0
  247. package/src/db/migrations.spec.ts +165 -2
  248. package/src/db/migrations.ts +43 -1
  249. package/src/lib/action-suggestions.ts +267 -0
  250. package/src/lib/chat-first-app-route.spec.ts +15 -2
  251. package/src/lib/chat-first-app-route.ts +19 -7
  252. package/src/lib/chat-first-pane.spec.ts +71 -0
  253. package/src/lib/chat-first-pane.ts +101 -4
  254. package/src/lib/data-intent.spec.ts +21 -0
  255. package/src/lib/data-intent.ts +13 -0
  256. package/src/lib/dispatch-app-identity.ts +40 -0
  257. package/src/lib/other-apps.spec.ts +110 -1
  258. package/src/lib/other-apps.ts +41 -0
  259. package/src/lib/workspace-app-frame-view.spec.ts +13 -0
  260. package/src/lib/workspace-app-frame-view.ts +98 -0
  261. package/src/lib/workspace-apps.spec.ts +101 -0
  262. package/src/lib/workspace-apps.test.ts +18 -0
  263. package/src/lib/workspace-apps.ts +102 -24
  264. package/src/routes/pages/chat.spec.tsx +92 -1
  265. package/src/routes/pages/chat.tsx +41 -2
  266. package/src/routes/pages/settings-page.tsx +22 -17
  267. package/src/routes/pages/settings.spec.tsx +4 -0
  268. package/src/server/index.ts +6 -6
  269. package/src/server/lib/action-suggestions.ts +122 -0
  270. package/src/server/lib/app-action-result.ts +8 -0
  271. package/src/server/lib/app-creation-store.spec.ts +251 -0
  272. package/src/server/lib/app-creation-store.ts +72 -12
  273. package/src/server/lib/community-apps.spec.ts +19 -1
  274. package/src/server/lib/curated-workspace-templates.spec.ts +12 -0
  275. package/src/server/lib/curated-workspace-templates.ts +2 -0
  276. package/src/server/lib/dispatch-config.ts +15 -0
  277. package/src/server/lib/dispatch-pane-context.spec.ts +54 -0
  278. package/src/server/lib/dispatch-pane-context.ts +54 -0
  279. package/src/server/lib/google-broker-provider.ts +121 -0
  280. package/src/server/lib/google-broker-rpc.spec.ts +95 -0
  281. package/src/server/lib/google-broker-rpc.ts +107 -0
  282. package/src/server/lib/hosted-apps-settings.ts +4 -0
  283. package/src/server/lib/mcp-app-discovery.ts +318 -0
  284. package/src/server/lib/mcp-gateway.spec.ts +2252 -524
  285. package/src/server/lib/mcp-gateway.ts +1044 -181
  286. package/src/server/plugins/agent-chat.direct-mcp.spec.ts +84 -5
  287. package/src/server/plugins/agent-chat.ts +9 -7
  288. package/src/server/plugins/auth.spec.ts +5 -1
  289. package/src/server/plugins/auth.ts +1 -0
  290. package/src/server/plugins/db.ts +0 -51
  291. package/src/server/plugins/integrations.ts +4 -1
  292. package/src/shared/chat-first-pane.spec.ts +36 -0
  293. package/src/shared/chat-first-pane.ts +55 -0
  294. package/src/shared/labs.ts +8 -0
  295. package/src/server/plugins/agent-chat.spec.ts +0 -206
  296. package/src/server/plugins/integrations.spec.ts +0 -60
@@ -20,6 +20,7 @@ import { useTheme } from "next-themes";
20
20
  import {
21
21
  useCallback,
22
22
  useEffect,
23
+ useEffectEvent,
23
24
  useMemo,
24
25
  useRef,
25
26
  useState,
@@ -28,6 +29,12 @@ import {
28
29
  import { Link } from "react-router";
29
30
 
30
31
  import { isEmbedSessionExpiredMessage } from "../lib/embed-session-recovery";
32
+ import {
33
+ requestWorkspaceAppFrameView,
34
+ workspaceAppFramePath,
35
+ workspaceAppFramePathAtMount,
36
+ type WorkspaceAppFrameView,
37
+ } from "../lib/workspace-app-frame-view";
31
38
  import {
32
39
  isWorkspaceAppHostPageUrl,
33
40
  isWorkspaceSsoApp,
@@ -40,6 +47,10 @@ import {
40
47
  workspaceAppTargetPath,
41
48
  type WorkspaceAppSummary,
42
49
  } from "../lib/workspace-apps";
50
+ import {
51
+ DISPATCH_CHILD_BROWSER_TAB_PARAM,
52
+ isDispatchChildBrowserTabId,
53
+ } from "../shared/chat-first-pane";
43
54
  import { DISPATCH_WORKSPACE_SSO_FLAG } from "../shared/feature-flags";
44
55
  import { workspaceAppChatProxyPath } from "../shared/workspace-app-chat";
45
56
  import { ActionQueryError } from "./action-query-error";
@@ -56,6 +67,7 @@ const MAX_FRAME_CHAT_CONTEXT_LENGTH = 700_000;
56
67
 
57
68
  interface EmbedSessionResult {
58
69
  startUrl: string;
70
+ childBrowserTabId?: string;
59
71
  }
60
72
 
61
73
  interface EmbedSessionInput {
@@ -217,6 +229,52 @@ export interface WorkspaceAppChatRailProps {
217
229
  copy?: ChatFirstCopy;
218
230
  agentPageHref?: string;
219
231
  onFullscreenRequest?: () => void;
232
+ browserTabId?: string;
233
+ }
234
+
235
+ const CHILD_TAB_POLL_MS = 500;
236
+ const CHILD_TAB_POLL_ATTEMPTS = 40;
237
+
238
+ /**
239
+ * The tab id the app's chat requests must carry is the embedded frame's, not
240
+ * Dispatch's: apps share one database, so a `navigate:<tabId>` command written
241
+ * under Dispatch's own id is consumed by Dispatch's navigation state. Until the
242
+ * frame has adopted its claim the rail gets an id nobody consumes.
243
+ */
244
+ export function useWorkspaceAppChildBrowserTab(): {
245
+ browserTabId: string;
246
+ onViewReaderChange: (read: WorkspaceAppViewReader) => () => void;
247
+ } {
248
+ const [unclaimedId] = useState(() => crypto.randomUUID());
249
+ const [claimedId, setClaimedId] = useState<string | null>(null);
250
+ const onViewReaderChange = useCallback((read: WorkspaceAppViewReader) => {
251
+ let attempts = 0;
252
+ let stopped = false;
253
+ let timer: ReturnType<typeof setTimeout> | undefined;
254
+ const retry = () => {
255
+ if (!stopped && attempts < CHILD_TAB_POLL_ATTEMPTS) {
256
+ timer = setTimeout(poll, CHILD_TAB_POLL_MS);
257
+ }
258
+ };
259
+ const poll = () => {
260
+ attempts += 1;
261
+ read(AbortSignal.timeout(CHILD_TAB_POLL_MS * 4))
262
+ .then((view) => {
263
+ if (stopped) return;
264
+ if (view?.childBrowserTabId) setClaimedId(view.childBrowserTabId);
265
+ else retry();
266
+ })
267
+ // coercion-ok: an unreadable frame stays unclaimed and the rail keeps its inert id.
268
+ .catch(retry);
269
+ };
270
+ poll();
271
+ return () => {
272
+ stopped = true;
273
+ clearTimeout(timer);
274
+ setClaimedId(null);
275
+ };
276
+ }, []);
277
+ return { browserTabId: claimedId ?? unclaimedId, onViewReaderChange };
220
278
  }
221
279
 
222
280
  export function WorkspaceAppChatRail({
@@ -226,6 +284,7 @@ export function WorkspaceAppChatRail({
226
284
  copy = defaultChatFirstCopy,
227
285
  agentPageHref,
228
286
  onFullscreenRequest,
287
+ browserTabId,
229
288
  }: WorkspaceAppChatRailProps) {
230
289
  const t = useT();
231
290
  const appChat = useWorkspaceAppChatApi(appId);
@@ -292,6 +351,7 @@ export function WorkspaceAppChatRail({
292
351
  isolateHistoryByScope
293
352
  apiUrl={appChat.apiUrl}
294
353
  agentChatSurface="app"
354
+ {...(browserTabId ? { browserTabId } : {})}
295
355
  showTabBar
296
356
  suppressInlineOpenApp
297
357
  dynamicSuggestions={false}
@@ -305,6 +365,63 @@ export function WorkspaceAppChatRail({
305
365
  );
306
366
  }
307
367
 
368
+ function navigateSameOriginWorkspaceApp(
369
+ frame: Window,
370
+ mountPath: string | null | undefined,
371
+ url: URL,
372
+ ): boolean {
373
+ const base = mountPath?.replace(/\/+$/, "");
374
+ if (!base || url.origin !== window.location.origin) return false;
375
+ try {
376
+ const current = new URL(frame.location.href);
377
+ if (
378
+ current.origin !== url.origin ||
379
+ (current.pathname !== base && !current.pathname.startsWith(`${base}/`))
380
+ ) {
381
+ return false;
382
+ }
383
+ const nextPath = workspaceAppFramePath(url);
384
+ if (workspaceAppFramePath(current) === nextPath) return true;
385
+ const index = frame.history.state?.idx;
386
+ const state = {
387
+ usr: null,
388
+ key: crypto.randomUUID(),
389
+ idx: typeof index === "number" ? index + 1 : 0,
390
+ };
391
+ frame.history.pushState(state, "", nextPath);
392
+ const FramePopStateEvent = (frame as Window & typeof globalThis)
393
+ .PopStateEvent;
394
+ frame.dispatchEvent(new FramePopStateEvent("popstate", { state }));
395
+ return true;
396
+ } catch {
397
+ // coercion-ok: a redirected iframe rejects direct access and falls back to the guest bridge.
398
+ return false;
399
+ }
400
+ }
401
+
402
+ function readSameOriginWorkspaceAppView(
403
+ frame: Window,
404
+ mount: URL,
405
+ ): (WorkspaceAppFrameView & { pendingTabClaim: boolean }) | null {
406
+ try {
407
+ const location = new URL(frame.location.href);
408
+ const path = workspaceAppFramePathAtMount(location, mount);
409
+ if (!path) return null;
410
+ const selectedText = frame.getSelection()?.toString().trim();
411
+ return {
412
+ path,
413
+ title: frame.document.title,
414
+ ...(selectedText ? { selectedText } : {}),
415
+ pendingTabClaim: location.searchParams.has(
416
+ DISPATCH_CHILD_BROWSER_TAB_PARAM,
417
+ ),
418
+ };
419
+ } catch {
420
+ // coercion-ok: null means the frame view is unavailable after the browser denies access.
421
+ return null;
422
+ }
423
+ }
424
+
308
425
  export interface WorkspaceAppFrameApp {
309
426
  id: string;
310
427
  name: string;
@@ -314,12 +431,22 @@ export interface WorkspaceAppFrameApp {
314
431
  isDispatch?: boolean;
315
432
  }
316
433
 
434
+ export type WorkspaceAppViewReader = (signal: AbortSignal) => Promise<
435
+ | (WorkspaceAppFrameView & {
436
+ origin?: string;
437
+ childBrowserTabId?: string;
438
+ })
439
+ | null
440
+ >;
441
+
317
442
  interface WorkspaceAppFrameProps {
318
443
  app: WorkspaceAppFrameApp;
319
444
  navigateToTopWindow?: (href: string) => boolean | void;
320
445
  embedPath?: string;
321
446
  initialPath?: string;
322
447
  onChildRouteChange?: (path: string) => void;
448
+ onViewReaderChange?: (read: WorkspaceAppViewReader) => () => void;
449
+ claimBrowserTab?: boolean;
323
450
  chatSidebar?: boolean;
324
451
  copy?: ChatFirstCopy;
325
452
  }
@@ -330,6 +457,8 @@ export function WorkspaceAppFrame({
330
457
  embedPath,
331
458
  initialPath,
332
459
  onChildRouteChange,
460
+ onViewReaderChange,
461
+ claimBrowserTab = false,
333
462
  chatSidebar = false,
334
463
  copy = defaultChatFirstCopy,
335
464
  }: WorkspaceAppFrameProps) {
@@ -341,6 +470,33 @@ export function WorkspaceAppFrame({
341
470
  document.documentElement.classList.contains("dark")
342
471
  ? "dark"
343
472
  : "light";
473
+ const isExplicitEmbeddedHostRoute =
474
+ typeof window !== "undefined" &&
475
+ new URLSearchParams(window.location.search).get("embedded") === "1";
476
+ const openInTopWindow =
477
+ !isExplicitEmbeddedHostRoute && shouldOpenWorkspaceAppInTopWindow();
478
+ const requestedPath = embedPath ?? initialPath;
479
+ const [launchPath, setLaunchPath] = useState(requestedPath);
480
+ const [completedNavigation, setCompletedNavigation] = useState({
481
+ requestedPath,
482
+ accepted: true,
483
+ });
484
+ const requestedPathAccepted =
485
+ completedNavigation.accepted &&
486
+ completedNavigation.requestedPath === requestedPath;
487
+ const [loadedDocument, setLoadedDocument] = useState<{ url: string } | null>(
488
+ null,
489
+ );
490
+ const pendingNavigation = useRef<{
491
+ controller: AbortController;
492
+ path: string;
493
+ requestedPath: string | undefined;
494
+ } | null>(null);
495
+ const observedPath = useRef<string | null>(null);
496
+ const framePath = openInTopWindow ? requestedPath : launchPath;
497
+ const childBrowserTabClaim = useRef<{ startUrl: string; id: string } | null>(
498
+ null,
499
+ );
344
500
  const [embedUrl, setEmbedUrl] = useState<string | null>(null);
345
501
  const [embedError, setEmbedError] = useState<Error | null>(null);
346
502
  const [isDirectFallback, setIsDirectFallback] = useState(false);
@@ -360,8 +516,12 @@ export function WorkspaceAppFrame({
360
516
  "*",
361
517
  );
362
518
  postThemeToFrame();
519
+ pendingNavigation.current?.controller.abort();
520
+ pendingNavigation.current = null;
521
+ observedPath.current = null;
522
+ setLoadedDocument(embedUrl ? { url: embedUrl } : null);
363
523
  if (isDirectFallback) setEmbedError(null);
364
- }, [isDirectFallback, postThemeToFrame]);
524
+ }, [embedUrl, isDirectFallback, postThemeToFrame]);
365
525
  const workspaceSsoEnabled = useFeatureFlag(DISPATCH_WORKSPACE_SSO_FLAG.key);
366
526
  const useWorkspaceSso = workspaceSsoEnabled && isWorkspaceSsoApp(app);
367
527
  const createEmbedSession = useActionMutation<
@@ -391,24 +551,23 @@ export function WorkspaceAppFrame({
391
551
  url: appUrl,
392
552
  isDispatch: app.isDispatch ?? isDispatchWorkspaceAppId(app.id),
393
553
  });
394
- const topWindowHref = useMemo(() => {
395
- if (embedPath !== undefined) {
396
- return workspaceAppDirectHref(
397
- { path: app.path ?? null, url: appUrl },
398
- embedPath,
399
- );
400
- }
401
- if (initialPath !== undefined) {
402
- return workspaceAppDirectHref(
403
- { path: app.path ?? null, url: appUrl },
404
- initialPath,
405
- );
406
- }
407
-
408
- return appHref;
409
- }, [app.path, appHref, appUrl, embedPath, initialPath]);
410
- const openInTopWindow = shouldOpenWorkspaceAppInTopWindow();
411
- const topWindowSsoAttemptKey = `${app.id}\u0000${app.path ?? ""}\u0000${app.url ?? ""}\u0000${embedPath ?? ""}\u0000${initialPath ?? ""}\u0000${embedAttempt}`;
554
+ const topWindowHref = useMemo(
555
+ () =>
556
+ requestedPath === undefined
557
+ ? appHref
558
+ : workspaceAppDirectHref(
559
+ { path: app.path ?? null, url: appUrl },
560
+ requestedPath,
561
+ ),
562
+ [app.path, appHref, appUrl, requestedPath],
563
+ );
564
+ const topWindowSsoAttemptKey = JSON.stringify([
565
+ app.id,
566
+ app.path,
567
+ app.url,
568
+ framePath,
569
+ embedAttempt,
570
+ ]);
412
571
  const topWindowSsoAttemptedRef = useRef<string | null>(null);
413
572
  // A same-origin workspace app already shares the workspace session cookie, so
414
573
  // framing it through an embed session makes it worse: an ordinary embed
@@ -421,7 +580,7 @@ export function WorkspaceAppFrame({
421
580
  // Keep the embedded presentation without the credential.
422
581
  const hostPageDirectUrl = useMemo(() => {
423
582
  if (!isHostPageUrl) return null;
424
- const targetPath = embedPath ?? initialPath;
583
+ const targetPath = framePath;
425
584
  const href =
426
585
  targetPath !== undefined
427
586
  ? workspaceAppDirectHref(
@@ -433,14 +592,11 @@ export function WorkspaceAppFrame({
433
592
  // batching, and the plain app URL is what the existing direct fallback
434
593
  // already loads when a mint fails.
435
594
  return href || null;
436
- }, [app.path, appHref, appUrl, embedPath, initialPath, isHostPageUrl]);
595
+ }, [app.path, appHref, appUrl, framePath, isHostPageUrl]);
437
596
 
438
597
  const embedInput = useMemo<EmbedSessionInput | null>(() => {
439
- if (embedPath !== undefined) {
440
- return buildChatFirstEmbedSessionInput(app.id, embedPath);
441
- }
442
- if (initialPath !== undefined) {
443
- return buildChatFirstEmbedSessionInput(app.id, initialPath);
598
+ if (framePath !== undefined) {
599
+ return buildChatFirstEmbedSessionInput(app.id, framePath);
444
600
  }
445
601
  if (!appHref) return null;
446
602
  if (isHostPageUrl) {
@@ -454,18 +610,9 @@ export function WorkspaceAppFrame({
454
610
  ...(appUrl?.trim() ? { url: appHref } : { path: appHref }),
455
611
  chrome: "minimal",
456
612
  };
457
- }, [
458
- app.homePath,
459
- app.id,
460
- appHref,
461
- appUrl,
462
- embedPath,
463
- initialPath,
464
- isHostPageUrl,
465
- ]);
613
+ }, [app.homePath, app.id, appHref, appUrl, framePath, isHostPageUrl]);
466
614
 
467
615
  useEffect(() => {
468
- if (hostPageDirectUrl) return;
469
616
  if (openInTopWindow && useWorkspaceSso && embedInput) {
470
617
  setTopWindowNavigationFailed(false);
471
618
  return;
@@ -495,12 +642,24 @@ export function WorkspaceAppFrame({
495
642
  ]);
496
643
 
497
644
  useEffect(() => {
498
- if (hostPageDirectUrl) {
499
- setEmbedUrl(hostPageDirectUrl);
645
+ setLoadedDocument(null);
646
+ observedPath.current = null;
647
+ childBrowserTabClaim.current = null;
648
+ if (hostPageDirectUrl && (!openInTopWindow || topWindowNavigationFailed)) {
649
+ if (claimBrowserTab) {
650
+ const url = new URL(hostPageDirectUrl, window.location.href);
651
+ const id = crypto.randomUUID();
652
+ url.searchParams.set(DISPATCH_CHILD_BROWSER_TAB_PARAM, id);
653
+ childBrowserTabClaim.current = { startUrl: url.href, id };
654
+ setEmbedUrl(url.href);
655
+ } else {
656
+ setEmbedUrl(hostPageDirectUrl);
657
+ }
500
658
  setEmbedError(null);
501
659
  setIsDirectFallback(false);
502
660
  return;
503
661
  }
662
+ if (hostPageDirectUrl) return;
504
663
  const useTopWindowSso = openInTopWindow && useWorkspaceSso && !!embedInput;
505
664
  if (
506
665
  !embedInput ||
@@ -541,6 +700,15 @@ export function WorkspaceAppFrame({
541
700
  setEmbedUrl(didNavigate ? null : result.startUrl);
542
701
  return;
543
702
  }
703
+ if (
704
+ claimBrowserTab &&
705
+ isDispatchChildBrowserTabId(result.childBrowserTabId)
706
+ ) {
707
+ childBrowserTabClaim.current = {
708
+ startUrl: result.startUrl,
709
+ id: result.childBrowserTabId,
710
+ };
711
+ }
544
712
  setEmbedUrl(result.startUrl);
545
713
  })
546
714
  .catch((cause: unknown) => {
@@ -555,10 +723,10 @@ export function WorkspaceAppFrame({
555
723
  }
556
724
  setIsDirectFallback(true);
557
725
  const fallbackHref =
558
- initialPath !== undefined || embedPath !== undefined
726
+ framePath !== undefined
559
727
  ? workspaceAppDirectHref(
560
728
  { path: app.path ?? "", url: appUrl },
561
- initialPath ?? embedPath ?? "/",
729
+ framePath,
562
730
  )
563
731
  : appHref;
564
732
  setEmbedUrl(fallbackHref);
@@ -572,12 +740,12 @@ export function WorkspaceAppFrame({
572
740
  app.path,
573
741
  app.url,
574
742
  appHref,
743
+ claimBrowserTab,
575
744
  createEmbedSession.mutateAsync,
576
745
  createWorkspaceSsoEmbedSession.mutateAsync,
577
746
  embedInput,
578
- embedPath,
747
+ framePath,
579
748
  hostPageDirectUrl,
580
- initialPath,
581
749
  embedAttempt,
582
750
  openInTopWindow,
583
751
  navigateToTopWindow,
@@ -586,6 +754,120 @@ export function WorkspaceAppFrame({
586
754
  useWorkspaceSso,
587
755
  ]);
588
756
 
757
+ const reloadFrame = useCallback(() => {
758
+ pendingNavigation.current?.controller.abort();
759
+ pendingNavigation.current = null;
760
+ setLoadedDocument(null);
761
+ setLaunchPath(requestedPath);
762
+ setCompletedNavigation({ requestedPath, accepted: true });
763
+ setEmbedAttempt((attempt) => attempt + 1);
764
+ }, [requestedPath]);
765
+
766
+ useEffect(
767
+ () => () => {
768
+ pendingNavigation.current?.controller.abort();
769
+ pendingNavigation.current = null;
770
+ },
771
+ [embedUrl],
772
+ );
773
+
774
+ const finishNavigation = useEffectEvent(
775
+ (route: string | undefined, accepted: boolean) => {
776
+ if (!accepted && route === requestedPath) {
777
+ reloadFrame();
778
+ return;
779
+ }
780
+ setCompletedNavigation({ requestedPath: route, accepted });
781
+ },
782
+ );
783
+
784
+ useEffect(() => {
785
+ const frame = embedFrameRef.current;
786
+ const target = frame?.contentWindow;
787
+ if (
788
+ openInTopWindow ||
789
+ !embedUrl ||
790
+ loadedDocument?.url !== embedUrl ||
791
+ !target ||
792
+ requestedPathAccepted ||
793
+ pendingNavigation.current
794
+ ) {
795
+ return;
796
+ }
797
+
798
+ const href =
799
+ requestedPath === undefined
800
+ ? appHref
801
+ : workspaceAppDirectHref(
802
+ { path: app.path ?? null, url: appUrl },
803
+ requestedPath,
804
+ );
805
+ if (!href) {
806
+ finishNavigation(requestedPath, false);
807
+ return;
808
+ }
809
+ const url = new URL(href, window.location.href);
810
+ const origin = new URL(embedUrl, window.location.href).origin;
811
+ if (url.origin !== origin) {
812
+ finishNavigation(requestedPath, false);
813
+ return;
814
+ }
815
+
816
+ const path = workspaceAppFramePath(url);
817
+ if (observedPath.current === path) {
818
+ finishNavigation(requestedPath, true);
819
+ return;
820
+ }
821
+ if (
822
+ hostPageDirectUrl &&
823
+ navigateSameOriginWorkspaceApp(target, app.path, url)
824
+ ) {
825
+ finishNavigation(requestedPath, true);
826
+ return;
827
+ }
828
+
829
+ const controller = new AbortController();
830
+ const navigation = { controller, path, requestedPath };
831
+ pendingNavigation.current = navigation;
832
+ void requestWorkspaceAppFrameView(
833
+ target,
834
+ origin,
835
+ { operation: "navigate", path },
836
+ controller.signal,
837
+ ).then((receipt) => {
838
+ if (pendingNavigation.current !== navigation) return;
839
+ pendingNavigation.current = null;
840
+ if (frame !== embedFrameRef.current) return;
841
+ const accepted = receipt?.path === navigation.path;
842
+ if (!accepted || observedPath.current !== navigation.path) {
843
+ observedPath.current = null;
844
+ }
845
+ finishNavigation(navigation.requestedPath, accepted);
846
+ });
847
+ }, [
848
+ app.path,
849
+ appHref,
850
+ appUrl,
851
+ completedNavigation,
852
+ embedUrl,
853
+ hostPageDirectUrl,
854
+ loadedDocument,
855
+ requestedPathAccepted,
856
+ openInTopWindow,
857
+ requestedPath,
858
+ ]);
859
+
860
+ const relayChildRoute = useEffectEvent((childPath: string, route: string) => {
861
+ const pending = pendingNavigation.current;
862
+ if (
863
+ pending
864
+ ? pending.requestedPath === requestedPath && pending.path === childPath
865
+ : requestedPathAccepted
866
+ ) {
867
+ onChildRouteChange?.(route);
868
+ }
869
+ });
870
+
589
871
  useEffect(() => {
590
872
  const handleEmbedSessionExpired = (event: MessageEvent) => {
591
873
  if (
@@ -593,16 +875,16 @@ export function WorkspaceAppFrame({
593
875
  ) {
594
876
  return;
595
877
  }
596
- setEmbedAttempt((attempt) => attempt + 1);
878
+ reloadFrame();
597
879
  };
598
880
 
599
881
  window.addEventListener("message", handleEmbedSessionExpired);
600
882
  return () =>
601
883
  window.removeEventListener("message", handleEmbedSessionExpired);
602
- }, [embedUrl]);
884
+ }, [embedUrl, reloadFrame]);
603
885
 
604
886
  useEffect(() => {
605
- if (!onChildRouteChange || !embedUrl) return;
887
+ if (!embedUrl) return;
606
888
 
607
889
  const frame = embedFrameRef.current;
608
890
  if (!frame) return;
@@ -627,21 +909,95 @@ export function WorkspaceAppFrame({
627
909
  } | null;
628
910
  if (
629
911
  message?.type !== AGENT_NATIVE_WORKSPACE_APP_ROUTE_MESSAGE_TYPE ||
630
- typeof message.path !== "string"
912
+ typeof message.path !== "string" ||
913
+ !message.path.startsWith("/") ||
914
+ message.path.startsWith("//") ||
915
+ /[\u0000-\u001f\u007f]/.test(message.path)
631
916
  ) {
632
917
  return;
633
918
  }
634
919
 
920
+ const childUrl = new URL(message.path, expectedOrigin);
921
+ if (childUrl.origin !== expectedOrigin) return;
922
+ const childPath = workspaceAppFramePath(childUrl);
635
923
  const route = workspaceAppRouteForChildPath(
636
924
  { id: app.id, path: app.path ?? "", url: appUrl },
637
- message.path,
925
+ childPath,
638
926
  );
639
- if (route) onChildRouteChange(route);
927
+ if (!route) return;
928
+ observedPath.current = childPath;
929
+ relayChildRoute(childPath, route);
640
930
  };
641
931
 
642
932
  window.addEventListener("message", handleWorkspaceAppRoute);
643
933
  return () => window.removeEventListener("message", handleWorkspaceAppRoute);
644
- }, [app.id, app.path, appUrl, embedUrl, onChildRouteChange]);
934
+ }, [app.id, app.path, appUrl, embedUrl]);
935
+
936
+ useEffect(() => {
937
+ if (!onViewReaderChange || !embedUrl || loadedDocument?.url !== embedUrl) {
938
+ return;
939
+ }
940
+ const frame = embedFrameRef.current;
941
+ const target = frame?.contentWindow;
942
+ const mountHref = workspaceAppDirectHref(
943
+ { path: app.path ?? null, url: appUrl },
944
+ "/",
945
+ );
946
+ if (!target || !mountHref) return;
947
+
948
+ const origin = new URL(embedUrl, window.location.href).origin;
949
+ const mount = new URL(mountHref, window.location.href);
950
+ if (mount.origin !== origin) return;
951
+
952
+ let current = true;
953
+ const canRead = (signal: AbortSignal) =>
954
+ current &&
955
+ !signal.aborted &&
956
+ frame === embedFrameRef.current &&
957
+ !pendingNavigation.current;
958
+ const unregister = onViewReaderChange(async (signal) => {
959
+ if (!canRead(signal)) return null;
960
+ const read =
961
+ origin === window.location.origin
962
+ ? readSameOriginWorkspaceAppView(target, mount)
963
+ : await requestWorkspaceAppFrameView(
964
+ target,
965
+ origin,
966
+ { operation: "read" },
967
+ signal,
968
+ );
969
+ if (!read || !canRead(signal)) return null;
970
+ if (/[\u0000-\u001f\u007f]/.test(read.path)) return null;
971
+ const renderedUrl = new URL(read.path, origin);
972
+ const path = workspaceAppFramePathAtMount(renderedUrl, mount);
973
+ const claim = childBrowserTabClaim.current;
974
+ if (
975
+ !path ||
976
+ path.length > 2_000 ||
977
+ (observedPath.current && observedPath.current !== path)
978
+ ) {
979
+ return null;
980
+ }
981
+ return {
982
+ path,
983
+ ...(read.title === undefined
984
+ ? {}
985
+ : { title: read.title.slice(0, 256) }),
986
+ ...(read.selectedText === undefined
987
+ ? {}
988
+ : { selectedText: read.selectedText.slice(0, 4_000) }),
989
+ ...(claim?.startUrl === embedUrl &&
990
+ (!("pendingTabClaim" in read) || read.pendingTabClaim === false) &&
991
+ !renderedUrl.searchParams.has(DISPATCH_CHILD_BROWSER_TAB_PARAM)
992
+ ? { childBrowserTabId: claim.id, origin }
993
+ : {}),
994
+ };
995
+ });
996
+ return () => {
997
+ current = false;
998
+ unregister();
999
+ };
1000
+ }, [app.path, appUrl, embedUrl, loadedDocument, onViewReaderChange]);
645
1001
 
646
1002
  useEffect(() => {
647
1003
  if (!embedUrl) return;
@@ -748,19 +1104,17 @@ export function WorkspaceAppFrame({
748
1104
  }
749
1105
  embedUrl={embedUrl}
750
1106
  errorMessage={embedError?.message}
751
- onRetry={
752
- embedInput ? () => setEmbedAttempt((attempt) => attempt + 1) : undefined
753
- }
1107
+ onRetry={embedInput ? reloadFrame : undefined}
754
1108
  renderEmbed={({ url, title }) => (
755
1109
  <iframe
756
- key={url + ":" + embedAttempt}
1110
+ key={embedAttempt}
757
1111
  data-dispatch-workspace-app-frame
758
1112
  src={url}
759
1113
  title={title ?? app.name}
760
1114
  ref={embedFrameRef}
761
1115
  onLoad={handleFrameLoad}
762
1116
  referrerPolicy="no-referrer"
763
- allow="clipboard-read; clipboard-write"
1117
+ allow="clipboard-read; clipboard-write; fullscreen"
764
1118
  className="h-full w-full border-0 bg-background"
765
1119
  />
766
1120
  )}
@@ -782,11 +1136,15 @@ export function WorkspaceAppHost({
782
1136
  navigateToTopWindow = navigateToWorkspaceApp,
783
1137
  initialPath,
784
1138
  onChildRouteChange,
1139
+ onViewReaderChange,
1140
+ claimBrowserTab,
785
1141
  }: {
786
1142
  appId?: string;
787
1143
  navigateToTopWindow?: (href: string) => boolean | void;
788
1144
  initialPath?: string;
789
1145
  onChildRouteChange?: (path: string) => void;
1146
+ onViewReaderChange?: (read: WorkspaceAppViewReader) => () => void;
1147
+ claimBrowserTab?: boolean;
790
1148
  }) {
791
1149
  const t = useT();
792
1150
  const workspaceAppsQuery = useActionQuery<WorkspaceAppSummary[]>(
@@ -914,10 +1272,13 @@ export function WorkspaceAppHost({
914
1272
  >
915
1273
  <div className="min-h-0 flex-1 bg-muted/20">
916
1274
  <WorkspaceAppFrame
1275
+ key={app.id}
917
1276
  app={app}
918
1277
  navigateToTopWindow={navigateToTopWindow}
919
1278
  initialPath={initialPath}
920
1279
  onChildRouteChange={onChildRouteChange}
1280
+ onViewReaderChange={onViewReaderChange}
1281
+ claimBrowserTab={claimBrowserTab}
921
1282
  />
922
1283
  </div>
923
1284
  </div>
@@ -926,10 +1287,42 @@ export function WorkspaceAppHost({
926
1287
 
927
1288
  const MAX_KEEP_ALIVE_APPS = 3;
928
1289
 
1290
+ export function useWarmWorkspaceAppIds(
1291
+ scope: string,
1292
+ activeQualifiedAppId: string | null,
1293
+ availableQualifiedAppIds?: readonly string[],
1294
+ ): readonly string[] {
1295
+ const [pool, setPool] = useState<{ scope: string; ids: readonly string[] }>(
1296
+ () => ({ scope, ids: [] }),
1297
+ );
1298
+ const retained = pool.scope === scope ? pool.ids : [];
1299
+ const available = availableQualifiedAppIds
1300
+ ? retained.filter((id) => availableQualifiedAppIds.includes(id))
1301
+ : retained;
1302
+ const next = activeQualifiedAppId
1303
+ ? [
1304
+ activeQualifiedAppId,
1305
+ ...available.filter((id) => id !== activeQualifiedAppId),
1306
+ ].slice(0, MAX_KEEP_ALIVE_APPS)
1307
+ : available;
1308
+
1309
+ if (
1310
+ pool.scope === scope &&
1311
+ next.length === pool.ids.length &&
1312
+ next.every((id, index) => id === pool.ids[index])
1313
+ ) {
1314
+ return pool.ids;
1315
+ }
1316
+ setPool({ scope, ids: next });
1317
+ return next;
1318
+ }
1319
+
929
1320
  export function WorkspaceAppKeepAlive({
930
1321
  activeAppId,
931
1322
  initialPath,
932
1323
  onChildRouteChange,
1324
+ onViewReaderChange,
1325
+ claimBrowserTab,
933
1326
  }: {
934
1327
  activeAppId: string | null;
935
1328
  /**
@@ -939,42 +1332,26 @@ export function WorkspaceAppKeepAlive({
939
1332
  */
940
1333
  initialPath?: string;
941
1334
  onChildRouteChange?: (path: string) => void;
1335
+ onViewReaderChange?: (read: WorkspaceAppViewReader) => () => void;
1336
+ claimBrowserTab?: boolean;
942
1337
  }) {
943
- const [visitedAppIds, setVisitedAppIds] = useState<string[]>(() =>
944
- activeAppId ? [activeAppId] : [],
945
- );
946
- // Remember the route each app was last shown at. Handing the deep path only to
947
- // the active app would reset a neighbour to its home the moment another app
948
- // becomes active, which is exactly what keep-alive exists to prevent.
949
- const [pathByAppId, setPathByAppId] = useState<Record<string, string>>(() =>
950
- activeAppId && initialPath !== undefined
951
- ? { [activeAppId]: initialPath }
952
- : {},
953
- );
1338
+ const warmAppIds = useWarmWorkspaceAppIds("standalone", activeAppId);
1339
+ const renderedAppIds = useMemo(() => [...warmAppIds].sort(), [warmAppIds]);
1340
+ const [pathByAppId, setPathByAppId] = useState<
1341
+ Record<string, string | undefined>
1342
+ >({});
954
1343
 
955
1344
  useEffect(() => {
956
1345
  if (!activeAppId) return;
957
- setVisitedAppIds((current) =>
958
- [activeAppId, ...current.filter((appId) => appId !== activeAppId)].slice(
959
- 0,
960
- MAX_KEEP_ALIVE_APPS,
961
- ),
962
- );
963
- }, [activeAppId]);
964
-
965
- useEffect(() => {
966
- if (!activeAppId || initialPath === undefined) return;
967
1346
  setPathByAppId((current) =>
968
- current[activeAppId] === initialPath
969
- ? current
970
- : { ...current, [activeAppId]: initialPath },
1347
+ Object.fromEntries(
1348
+ warmAppIds.map((id) => [
1349
+ id,
1350
+ id === activeAppId ? initialPath : current[id],
1351
+ ]),
1352
+ ),
971
1353
  );
972
- }, [activeAppId, initialPath]);
973
-
974
- const renderedAppIds =
975
- activeAppId && !visitedAppIds.includes(activeAppId)
976
- ? [activeAppId, ...visitedAppIds]
977
- : visitedAppIds;
1354
+ }, [activeAppId, initialPath, warmAppIds]);
978
1355
 
979
1356
  return (
980
1357
  <div
@@ -992,10 +1369,10 @@ export function WorkspaceAppKeepAlive({
992
1369
  >
993
1370
  <WorkspaceAppHost
994
1371
  appId={appId}
995
- {...(Object.hasOwn(pathByAppId, appId)
996
- ? { initialPath: pathByAppId[appId] }
997
- : {})}
1372
+ initialPath={active ? initialPath : pathByAppId[appId]}
998
1373
  {...(active && onChildRouteChange ? { onChildRouteChange } : {})}
1374
+ {...(active && onViewReaderChange ? { onViewReaderChange } : {})}
1375
+ {...(active && claimBrowserTab ? { claimBrowserTab } : {})}
999
1376
  />
1000
1377
  </div>
1001
1378
  );