moi-computer 0.13.1-next.1 → 0.13.1-next.3

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 (205) hide show
  1. package/client/api/app-config.ts +12 -1
  2. package/client/api/http.ts +11 -1
  3. package/client/app/app-router.tsx +3 -2
  4. package/client/app/routes/home-route.tsx +1 -1
  5. package/client/app/routes/workspace-registration.tsx +68 -0
  6. package/client/app/routes/workspace-route.tsx +33 -6
  7. package/client/app/shell/sidebar-layout.tsx +10 -5
  8. package/client/components/shared/workspace-icon.tsx +8 -17
  9. package/client/features/account/account-test-utils.tsx +15 -9
  10. package/client/features/account/api.ts +6 -6
  11. package/client/features/account/profile-actions.ts +5 -7
  12. package/client/features/app-settings/settings-page.tsx +2 -1
  13. package/client/features/applets/api.ts +6 -6
  14. package/client/features/applets/applet-cache.ts +6 -5
  15. package/client/features/applets/applet-log.ts +2 -1
  16. package/client/features/applets/applet-runtime.ts +2 -1
  17. package/client/features/applets/use-applet.ts +73 -18
  18. package/client/features/applets/widget-shell.tsx +4 -2
  19. package/client/features/chat/applet-chat-intents.ts +5 -13
  20. package/client/features/chat/chat-popup.tsx +8 -4
  21. package/client/features/chat/chat-send.ts +2 -2
  22. package/client/features/chat/composer/model-order.ts +4 -4
  23. package/client/features/chat/composer/model-picker.tsx +12 -10
  24. package/client/features/chat/composer/use-workspace-composer-state.tsx +1 -1
  25. package/client/features/chat/sessions/api.ts +11 -28
  26. package/client/features/chat/sessions/use-selected-session.ts +14 -109
  27. package/client/features/chat/use-chat.ts +8 -12
  28. package/client/features/collab/members/api.ts +4 -10
  29. package/client/features/collab/members/hooks.ts +32 -38
  30. package/client/features/collab/members/store.ts +96 -71
  31. package/client/features/collab/members/types.ts +5 -3
  32. package/client/features/collab/presence/client.ts +2 -3
  33. package/client/features/collab/presence/hooks.tsx +1 -1
  34. package/client/features/collab/proxy-user.ts +1 -3
  35. package/client/features/collab/sharing/workspace-members.tsx +0 -8
  36. package/client/features/dev/chat-states-page.tsx +3 -1
  37. package/client/features/dev/harness-debug-page.tsx +7 -7
  38. package/client/features/dev/workspace-setup-mocks.ts +1 -1
  39. package/client/features/dev/workspace-setup-preview.tsx +6 -4
  40. package/client/features/harness/api.ts +13 -10
  41. package/client/features/harness/harness-login-panel.tsx +9 -5
  42. package/client/features/harness/harness-presentation.ts +4 -4
  43. package/client/features/harness/harness-selector.tsx +13 -13
  44. package/client/features/harness/use-harness-login.ts +5 -5
  45. package/client/features/home/api.ts +38 -2
  46. package/client/features/home/home-page.tsx +43 -13
  47. package/client/features/home/shared-workspaces.tsx +70 -0
  48. package/client/features/home/workspace-invitations.tsx +101 -0
  49. package/client/features/identity/host-api.ts +3 -2
  50. package/client/features/identity/store.ts +1 -7
  51. package/client/features/onboarding/agent-setup-step.tsx +8 -8
  52. package/client/features/onboarding/onboarding-page.tsx +3 -2
  53. package/client/features/onboarding/workspace-setup-step.tsx +1 -1
  54. package/client/features/overview/api.ts +3 -5
  55. package/client/features/overview/default/registry.tsx +1 -1
  56. package/client/features/overview/default/views-widget.tsx +2 -2
  57. package/client/features/overview/hidden-panel.tsx +7 -5
  58. package/client/features/overview/overview.tsx +18 -16
  59. package/client/features/overview/workspace-name.tsx +2 -2
  60. package/client/features/scratchpad/scratchpad.tsx +9 -7
  61. package/client/features/scratchpad/use-scratchpad-snapshot.tsx +13 -5
  62. package/client/features/views/api.ts +66 -23
  63. package/client/features/views/use-view-actions.ts +10 -41
  64. package/client/features/views/view-manager.tsx +1 -1
  65. package/client/features/views/view-submission.ts +2 -21
  66. package/client/features/workspace/add/api.ts +5 -4
  67. package/client/features/workspace/add/create-workspace-dialog.tsx +12 -9
  68. package/client/features/workspace/add/import-workspace-dialog.tsx +7 -7
  69. package/client/features/workspace/add/use-workspace-import.ts +10 -10
  70. package/client/features/workspace/add/workspace-agent-options.ts +21 -20
  71. package/client/features/workspace/add/workspace-agent-selector.tsx +11 -11
  72. package/client/features/workspace/api.ts +60 -13
  73. package/client/features/workspace/harness-transition.ts +7 -13
  74. package/client/features/workspace/remote.ts +25 -0
  75. package/client/features/workspace/settings/api.ts +7 -12
  76. package/client/features/workspace/settings/connectors-settings.tsx +3 -3
  77. package/client/features/workspace/settings/general-settings.tsx +8 -8
  78. package/client/features/workspace/setup/workspace-login-state.tsx +4 -4
  79. package/client/features/workspace/setup/workspace-recovery-states.tsx +43 -14
  80. package/client/features/workspace/setup/workspace-setup.tsx +6 -6
  81. package/client/features/workspace/tab-resolution.ts +1 -8
  82. package/client/features/workspace/use-workspace-navigation.ts +11 -7
  83. package/client/features/workspace/workspace-content.tsx +24 -31
  84. package/client/features/workspace/workspace-frame.tsx +13 -26
  85. package/client/features/workspace/workspace-layout-context.tsx +45 -12
  86. package/client/features/workspace/workspace-menu.tsx +2 -2
  87. package/client/features/workspace/workspace-pane.tsx +22 -0
  88. package/client/features/workspace/workspace-pending-screen.tsx +1 -1
  89. package/client/features/workspace/workspace-preferences.ts +91 -0
  90. package/client/features/workspace/workspace-presentation.tsx +12 -16
  91. package/client/features/workspace/workspace-screen.tsx +2 -2
  92. package/client/features/workspace/workspace-state.ts +42 -16
  93. package/client/features/workspace/workspace-tabs.tsx +1 -1
  94. package/client/features/workspace/workspace-test-utils.ts +23 -0
  95. package/client/runtime/use-workspace-events.ts +14 -0
  96. package/client/runtime/workspace-events.ts +9 -9
  97. package/client/store/ui.ts +24 -9
  98. package/client/store/workspace-session.ts +30 -0
  99. package/dist/chunk-0h4zypgf.js +1 -0
  100. package/dist/chunk-1tksq43z.js +72 -0
  101. package/dist/chunk-6gxf0xr8.js +5 -0
  102. package/dist/chunk-a865c7x4.js +1 -0
  103. package/dist/chunk-f44cs6mw.js +2 -0
  104. package/dist/chunk-h3qag4a6.js +2 -0
  105. package/dist/chunk-kffcah9w.css +1 -0
  106. package/dist/chunk-q2tpy0h7.js +2 -0
  107. package/dist/chunk-r95acex1.js +546 -0
  108. package/dist/chunk-w5h9grdr.js +1 -0
  109. package/dist/{chunk-th91xfnp.js → chunk-xj1pjhng.js} +1 -1
  110. package/dist/index.html +1 -1
  111. package/lib/harness-instructions.ts +4 -4
  112. package/lib/harness-types.ts +13 -0
  113. package/lib/moi-context.ts +19 -5
  114. package/lib/types.ts +38 -54
  115. package/lib/workspace-config.ts +48 -0
  116. package/lib/workspace-layout.ts +5 -24
  117. package/package.json +5 -2
  118. package/server/AGENTS.md +1 -1
  119. package/server/README.md +36 -31
  120. package/server/api/harnesses.ts +5 -5
  121. package/server/api/index.ts +268 -162
  122. package/server/applets/index.ts +15 -9
  123. package/server/applets/views.ts +2 -17
  124. package/server/applets/widgets.ts +3 -2
  125. package/server/cli/commands/config.ts +8 -20
  126. package/server/cli/commands/hermes.ts +2 -2
  127. package/server/cli/commands/init.ts +102 -54
  128. package/server/cli/commands/openclaw.ts +2 -2
  129. package/server/cli/commands/refresh.ts +10 -1
  130. package/server/cli/commands/remote.ts +330 -0
  131. package/server/cli/commands/skill.ts +4 -4
  132. package/server/cli/commands/tabs.ts +5 -12
  133. package/server/cli/control.ts +35 -4
  134. package/server/cli/index.ts +10 -6
  135. package/server/cli/remote-command.ts +77 -0
  136. package/server/cli/remote-control.ts +73 -0
  137. package/server/cli/remote-operations.ts +247 -0
  138. package/server/collab/config.ts +3 -3
  139. package/server/collab/skill/index.ts +3 -3
  140. package/server/control.ts +47 -12
  141. package/server/harness/acp/session.ts +4 -4
  142. package/server/harness/auth.ts +26 -31
  143. package/server/harness/claude-code/index.ts +1 -1
  144. package/server/harness/codex/discovery.ts +1 -1
  145. package/server/harness/codex/session.ts +1 -1
  146. package/server/harness/detect.ts +14 -19
  147. package/server/harness/executable.ts +10 -10
  148. package/server/harness/hermes/NOTES.md +2 -2
  149. package/server/harness/hermes/index.ts +1 -1
  150. package/server/harness/install.ts +11 -11
  151. package/server/harness/openclaw/index.ts +1 -1
  152. package/server/harness/registry.ts +7 -6
  153. package/server/harness/types.ts +4 -4
  154. package/server/onboarding/jobs.ts +2 -2
  155. package/server/operations/commands.ts +46 -0
  156. package/server/operations/files.ts +148 -0
  157. package/server/operations/server.ts +206 -0
  158. package/server/remote/agent.ts +103 -0
  159. package/server/remote/broker-client.ts +76 -0
  160. package/server/remote/broker.ts +81 -0
  161. package/server/remote/client.ts +725 -0
  162. package/server/remote/errors.ts +2 -0
  163. package/server/remote/routes.ts +121 -0
  164. package/server/remote/workspace.ts +34 -0
  165. package/server/scratchpad/document.ts +33 -12
  166. package/server/scratchpad/executor.ts +16 -7
  167. package/server/sessions/config.ts +8 -5
  168. package/server/sessions/pending-views.ts +17 -0
  169. package/server/sessions/send-message.ts +102 -0
  170. package/server/sessions/view-sessions.ts +9 -101
  171. package/server/skills/locations.ts +4 -4
  172. package/server/skills/update.ts +11 -11
  173. package/server/skills/version.ts +7 -7
  174. package/server/skills/workspace.ts +13 -11
  175. package/server/web.ts +13 -9
  176. package/server/workspace-state/file.ts +4 -0
  177. package/server/workspace-state/index.ts +0 -4
  178. package/server/workspaces/api.ts +24 -0
  179. package/server/workspaces/change-harness.ts +15 -26
  180. package/server/workspaces/import.ts +4 -4
  181. package/server/workspaces/init.ts +15 -13
  182. package/server/workspaces/layout.ts +60 -66
  183. package/server/workspaces/registry.ts +43 -18
  184. package/server/workspaces/shared.ts +74 -0
  185. package/server/workspaces/tabs.ts +5 -12
  186. package/skill/SKILL.md +80 -19
  187. package/skill/references/SCRATCHPAD.md +4 -0
  188. package/skill/references/UI-COMPONENTS.md +1 -1
  189. package/client/api/app-ui-keys.ts +0 -6
  190. package/client/features/chat/sessions/browser-tab-state.ts +0 -43
  191. package/client/features/collab/members/keys.ts +0 -6
  192. package/client/features/workspace/browser-tab-state.ts +0 -51
  193. package/client/features/workspace/layout-save.ts +0 -13
  194. package/dist/chunk-3smkkcfw.js +0 -546
  195. package/dist/chunk-47h9c2qy.js +0 -2
  196. package/dist/chunk-8qed2gqp.js +0 -75
  197. package/dist/chunk-8zy3vesm.js +0 -1
  198. package/dist/chunk-a47zy276.js +0 -2
  199. package/dist/chunk-g4c1nte9.css +0 -1
  200. package/dist/chunk-kcactdr4.js +0 -2
  201. package/dist/chunk-m8bhx6tw.js +0 -1
  202. package/dist/chunk-pf19zcrn.js +0 -2
  203. package/dist/chunk-ygzxah0e.js +0 -1
  204. package/lib/workspace-types.ts +0 -13
  205. package/server/workspace-state/legacy-migration.ts +0 -79
@@ -1,15 +1,8 @@
1
- import { useQuery, useQueryClient, type QueryClient } from '@tanstack/react-query'
1
+ import { useQueryClient, type QueryClient } from '@tanstack/react-query'
2
2
  import { useCallback } from 'react'
3
3
  import { useParams } from 'wouter'
4
4
 
5
- import { appUiKeys } from '@/client/api/app-ui-keys'
6
5
  import { jsonRequest, requestJson } from '@/client/api/http'
7
- import {
8
- readSelectedSession,
9
- writeSelectedSession,
10
- type BrowserTabSelection
11
- } from '@/client/features/chat/sessions/browser-tab-state'
12
- import { useAppConfig } from '@/client/api/app-config'
13
6
  import { useWorkspaceId } from '@/client/features/workspace/workspace-context'
14
7
  import {
15
8
  changeWorkspaceStateCache,
@@ -20,7 +13,7 @@ import {
20
13
  } from '@/client/features/workspace/workspace-state'
21
14
  import { useUiStore } from '@/client/store/ui'
22
15
  import { composerDraftKey } from '@/lib/session-drafts'
23
- import type { SelectedSessionScope, WorkspaceState, WorkspaceTabId } from '@/lib/types'
16
+ import type { WorkspaceState, WorkspaceTabId } from '@/lib/types'
24
17
  import { isWorkspaceTabId } from '@/lib/workspace-tabs'
25
18
 
26
19
  type SetSelectedSession = (sessionId: string | null) => void
@@ -40,69 +33,22 @@ type SelectionWrite = {
40
33
  ifMatch?: string | null
41
34
  }
42
35
 
43
- export function browserTabSelectionKey(workspaceId: string) {
44
- return appUiKeys.browserTabSelection(workspaceId)
45
- }
46
-
47
- // sessionStorage is the source; the query only lets React observe it. Dropped
48
- // on unmount so the next visit reads storage again.
49
- export function browserTabSelectionOptions(workspaceId: string) {
50
- return {
51
- queryKey: browserTabSelectionKey(workspaceId),
52
- queryFn: () => Promise.resolve(readSelectedSession(workspaceId)),
53
- staleTime: Infinity,
54
- gcTime: 0,
55
- refetchOnMount: false,
56
- refetchOnWindowFocus: false
57
- }
58
- }
59
-
60
- // The chat a tab shows: the pin, then this browser tab's own choice (collab),
61
- // then the workspace's. Undefined until the state has loaded.
36
+ // The personal pin takes precedence over the chat selected in this workspace tab.
62
37
  export function selectedSessionForTab(
63
38
  state: WorkspaceState | undefined,
64
- tabId: WorkspaceTabId,
65
- local?: BrowserTabSelection
39
+ tabId: WorkspaceTabId
66
40
  ): string | null | undefined {
67
41
  if (!state) return undefined
68
42
  if (state.pinnedSessionId) return state.pinnedSessionId
69
- if (local && tabId in local.selected) return local.selected[tabId] ?? null
70
43
  return state.selected[tabId] ?? null
71
44
  }
72
45
 
73
- function cachedBrowserTabSelection(
74
- queryClient: QueryClient,
75
- workspaceId: string
76
- ): BrowserTabSelection {
77
- return (
78
- queryClient.getQueryData<BrowserTabSelection>(browserTabSelectionKey(workspaceId)) ??
79
- readSelectedSession(workspaceId)
80
- )
81
- }
82
-
83
- // For handlers outside render, which must see a change before React does.
84
46
  export function cachedSelectedSession(
85
47
  queryClient: QueryClient,
86
48
  workspaceId: string,
87
- tabId: WorkspaceTabId,
88
- scope: SelectedSessionScope
89
- ): string | null | undefined {
90
- return selectedSessionForTab(
91
- getCachedWorkspaceState(queryClient, workspaceId),
92
- tabId,
93
- scope === 'browser-tab' ? cachedBrowserTabSelection(queryClient, workspaceId) : undefined
94
- )
95
- }
96
-
97
- function setBrowserTabSelection(
98
- queryClient: QueryClient,
99
- workspaceId: string,
100
- change: (selected: BrowserTabSelection['selected']) => void
101
- ): void {
102
- const selected = { ...cachedBrowserTabSelection(queryClient, workspaceId).selected }
103
- change(selected)
104
- queryClient.setQueryData<BrowserTabSelection>(browserTabSelectionKey(workspaceId), { selected })
105
- writeSelectedSession(workspaceId, { selected })
49
+ tabId: WorkspaceTabId
50
+ ) {
51
+ return selectedSessionForTab(getCachedWorkspaceState(queryClient, workspaceId), tabId)
106
52
  }
107
53
 
108
54
  function applySelectionWrite(draft: WorkspaceState, input: SelectionWrite): void {
@@ -111,47 +57,19 @@ function applySelectionWrite(draft: WorkspaceState, input: SelectionWrite): void
111
57
  }
112
58
 
113
59
  // The request that points `tabId` at `sessionId` (null: a new chat), or
114
- // undefined when nothing needs saving. Collab saves locally right here.
60
+ // undefined when nothing needs saving.
115
61
  function selectionChange(
116
62
  queryClient: QueryClient,
117
63
  workspaceId: string,
118
64
  sessionId: string | null,
119
- tabId: WorkspaceTabId,
120
- scope: SelectedSessionScope
65
+ tabId: WorkspaceTabId
121
66
  ): SelectionWrite | undefined {
122
- if (scope === 'browser-tab') {
123
- const local = cachedBrowserTabSelection(queryClient, workspaceId)
124
- if (tabId in local.selected && (local.selected[tabId] ?? null) === sessionId) return
125
- setBrowserTabSelection(queryClient, workspaceId, selected => {
126
- selected[tabId] = sessionId
127
- })
128
- return
129
- }
130
67
  const current = getCachedWorkspaceState(queryClient, workspaceId)?.selected[tabId] ?? null
131
68
  if (current === sessionId) return
132
69
  if (sessionId === null) return current ? { sessionId: current, tabId, show: false } : undefined
133
70
  return { sessionId, tabId, show: true, ifMatch: current }
134
71
  }
135
72
 
136
- // Show a chat in a tab before the server confirms it, without a request: for
137
- // flows whose own request makes the server select it (view submission).
138
- export function setSelectedSessionInCache(
139
- queryClient: QueryClient,
140
- workspaceId: string,
141
- sessionId: string,
142
- tabId: WorkspaceTabId,
143
- scope: SelectedSessionScope
144
- ): void {
145
- if (scope === 'browser-tab')
146
- setBrowserTabSelection(queryClient, workspaceId, selected => {
147
- selected[tabId] = sessionId
148
- })
149
- else
150
- changeWorkspaceStateCache(queryClient, workspaceId, draft => {
151
- draft.selected[tabId] = sessionId
152
- })
153
- }
154
-
155
73
  // A harness replaced a temporary chat id with its real one.
156
74
  export function renameSelectedSessionInCache(
157
75
  queryClient: QueryClient,
@@ -165,10 +83,6 @@ export function renameSelectedSessionInCache(
165
83
  changeWorkspaceStateCache(queryClient, workspaceId, draft =>
166
84
  renameSessionInState(draft, from, to)
167
85
  )
168
- setBrowserTabSelection(queryClient, workspaceId, selected => {
169
- for (const [tabId, sessionId] of Object.entries(selected) as [WorkspaceTabId, string | null][])
170
- if (sessionId === from) selected[tabId] = to
171
- })
172
86
  }
173
87
 
174
88
  export function useCurrentTabId(): WorkspaceTabId {
@@ -193,40 +107,31 @@ export function useSelectedSession(explicitTabId?: WorkspaceTabId): SelectedSess
193
107
  const routeTabId = useCurrentTabId()
194
108
  const tabId = explicitTabId ?? routeTabId
195
109
  const workspaceId = useWorkspaceId()
196
- const cloud = useAppConfig().environment === 'cloud'
197
- const scope: SelectedSessionScope = cloud ? 'browser-tab' : 'shared'
198
110
  const queryClient = useQueryClient()
199
111
  const state = useWorkspaceState(workspaceId).data
200
- const local = useQuery({
201
- ...browserTabSelectionOptions(workspaceId),
202
- enabled: scope === 'browser-tab'
203
- }).data
204
112
  const { mutate: save, mutateAsync: saveAsync } = useSelectionWrite(workspaceId)
205
113
 
206
114
  const setSelectedSessionId = useCallback<SetSelectedSession>(
207
115
  sessionId => {
208
116
  // A pinned chat shows in every tab until it is unpinned.
209
117
  if (getCachedWorkspaceState(queryClient, workspaceId)?.pinnedSessionId) return
210
- const input = selectionChange(queryClient, workspaceId, sessionId, tabId, scope)
118
+ const input = selectionChange(queryClient, workspaceId, sessionId, tabId)
211
119
  if (input) save(input)
212
120
  },
213
- [queryClient, save, workspaceId, tabId, scope]
121
+ [queryClient, save, workspaceId, tabId]
214
122
  )
215
123
 
216
124
  const selectSessionForTab = useCallback(
217
125
  async (sessionId: string, destination: WorkspaceTabId) => {
218
- const input = selectionChange(queryClient, workspaceId, sessionId, destination, scope)
126
+ const input = selectionChange(queryClient, workspaceId, sessionId, destination)
219
127
  if (!input) return
220
128
  const saved = await saveAsync(input)
221
129
  if (saved.selected[destination] !== sessionId) throw new Error('Couldn’t save selected chat')
222
130
  },
223
- [queryClient, saveAsync, scope, workspaceId]
131
+ [queryClient, saveAsync, workspaceId]
224
132
  )
225
133
 
226
- const selectedSessionId =
227
- scope === 'browser-tab' && local === undefined
228
- ? undefined
229
- : selectedSessionForTab(state, tabId, scope === 'browser-tab' ? local : undefined)
134
+ const selectedSessionId = selectedSessionForTab(state, tabId)
230
135
  return [selectedSessionId, setSelectedSessionId, selectSessionForTab]
231
136
  }
232
137
 
@@ -1,3 +1,4 @@
1
+ import { useWorkspaceState } from '@/client/features/workspace/workspace-state'
1
2
  import { useCallback, useMemo } from 'react'
2
3
 
3
4
  import { useQueryClient } from '@tanstack/react-query'
@@ -6,13 +7,11 @@ import { workspaceKeys } from '@/client/api/workspace-keys'
6
7
  import { useSessionState, useSessionView } from '@/client/features/chat/sessions/api'
7
8
  import { useWorkspaceHarness } from '@/client/features/workspace/api'
8
9
  import { useSelectedSession } from '@/client/features/chat/sessions/use-selected-session'
9
- import { useAppConfig } from '@/client/api/app-config'
10
10
  import {
11
11
  type WorkspaceTabAddress,
12
12
  useMoiUserMessageContext
13
13
  } from '@/client/features/workspace/moi-context'
14
14
  import { useWorkspaceId } from '@/client/features/workspace/workspace-context'
15
- import { useWorkspaceLayoutCtx } from '@/client/features/workspace/workspace-layout-context'
16
15
  import { sendMessage } from '@/client/features/chat/connection/chat-connection'
17
16
  import {
18
17
  type ChatSendOptions,
@@ -51,9 +50,7 @@ const EMPTY: ViewState = emptyViewState()
51
50
  export function useChat(address: WorkspaceTabAddress) {
52
51
  const workspaceId = useWorkspaceId()
53
52
  const qc = useQueryClient()
54
- const { layout } = useWorkspaceLayoutCtx()
55
53
  const [selectedSession, selectSession] = useSelectedSession(address.activeTab)
56
- const cloud = useAppConfig().environment === 'cloud'
57
54
  const modelsData = useWorkspaceHarness(workspaceId).data
58
55
  // A newly forked/native session can precede the provider's session listing.
59
56
  // Archive explicitly clears selection; a stale catalog must not do so.
@@ -75,6 +72,7 @@ export function useChat(address: WorkspaceTabAddress) {
75
72
  const viewQuery = useSessionView(workspaceId, selectedSessionId)
76
73
  const { refetch } = viewQuery
77
74
  // Tab, settings and fork history, from the workspace state.
75
+ const defaults = useWorkspaceState(workspaceId).data?.chatDefaults
78
76
  const session = useSessionState(workspaceId, selectedSessionId)
79
77
  const { refetch: refetchSession } = session
80
78
  const retryLoad = useCallback(async () => {
@@ -156,9 +154,9 @@ export function useChat(address: WorkspaceTabAddress) {
156
154
  // the picker's concrete model even for an implicit or stale selection;
157
155
  // otherwise its config or resumed thread can choose a different model.
158
156
  // Validate effort and Fast mode against that same resolved row.
159
- const pickedModel = sessionConfig?.model ?? layout.selectedModel
160
- const pickedEffort = sessionConfig?.effort ?? layout.selectedEffort
161
- const pickedSpeedMode = sessionConfig?.speedMode ?? layout.selectedSpeedMode
157
+ const pickedModel = sessionConfig?.model ?? defaults?.model
158
+ const pickedEffort = sessionConfig?.effort ?? defaults?.effort
159
+ const pickedSpeedMode = sessionConfig?.speedMode ?? defaults?.speedMode
162
160
  const { model, effort, speedMode, stream } = resolveChatRunOptions(
163
161
  modelsData,
164
162
  pickedModel,
@@ -171,7 +169,6 @@ export function useChat(address: WorkspaceTabAddress) {
171
169
  content: text,
172
170
  sessionId: sid,
173
171
  isNew,
174
- selectedSessionScope: cloud ? 'browser-tab' : 'shared',
175
172
  optimisticId,
176
173
  model,
177
174
  effort,
@@ -191,15 +188,14 @@ export function useChat(address: WorkspaceTabAddress) {
191
188
  selectedSessionId,
192
189
  workspaceId,
193
190
  qc,
194
- layout.selectedModel,
195
- layout.selectedEffort,
196
- layout.selectedSpeedMode,
191
+ defaults?.model,
192
+ defaults?.effort,
193
+ defaults?.speedMode,
197
194
  buildMoiContext,
198
195
  sessionConfig?.model,
199
196
  sessionConfig?.effort,
200
197
  sessionConfig?.speedMode,
201
198
  selectSession,
202
- cloud,
203
199
  modelsData
204
200
  ]
205
201
  )
@@ -1,23 +1,17 @@
1
+ import { getWorkspaceApiBase } from '@/client/features/workspace/remote'
1
2
  import { cloudRequest } from '../../account/api'
2
3
  import { jsonRequest } from '@/client/api/http'
3
- import { parseWorkspaceMembers, validWorkspaceId } from './types'
4
4
 
5
- const workspaceMembersPath = (id: string) =>
6
- `workspaces/${encodeURIComponent(validWorkspaceId(id))}`
7
- export async function fetchWorkspaceMembers(id: string, signal?: AbortSignal) {
8
- const response = await cloudRequest(`${workspaceMembersPath(id)}/members`, { signal })
9
- return parseWorkspaceMembers(await response.json())
10
- }
11
5
  export const inviteWorkspaceMember = (id: string, email: string, workspaceName?: string) =>
12
6
  cloudRequest(
13
- `${workspaceMembersPath(id)}/invitations`,
7
+ `${getWorkspaceApiBase(id)}/invitations`,
14
8
  jsonRequest('POST', { email, workspaceName })
15
9
  )
16
10
  export const removeWorkspaceMember = (id: string, membershipId: string) =>
17
- cloudRequest(`${workspaceMembersPath(id)}/members/${encodeURIComponent(membershipId)}`, {
11
+ cloudRequest(`${getWorkspaceApiBase(id)}/members/${encodeURIComponent(membershipId)}`, {
18
12
  method: 'DELETE'
19
13
  })
20
14
  export const revokeWorkspaceInvitation = (id: string, invitationId: string) =>
21
- cloudRequest(`${workspaceMembersPath(id)}/invitations/${encodeURIComponent(invitationId)}`, {
15
+ cloudRequest(`${getWorkspaceApiBase(id)}/invitations/${encodeURIComponent(invitationId)}`, {
22
16
  method: 'DELETE'
23
17
  })
@@ -1,16 +1,12 @@
1
- import { useMutation, useMutationState, useQuery, useQueryClient } from '@tanstack/react-query'
1
+ import { workspaceKeys } from '@/client/api/workspace-keys'
2
+ import type { WorkspaceLayoutResponse } from '@/lib/types'
3
+ import { useSyncExternalStore } from 'react'
4
+ import { useMutation, useMutationState, useQueryClient } from '@tanstack/react-query'
2
5
  import { useWorkspaces } from '../../home/api'
3
6
  import { getAccountGeneration } from '../../identity/store'
4
7
  import { useIdentity } from '../../identity/use-identity'
5
- import { ownerOnlyWorkspaceMembers } from './store'
6
- import { memberKeys } from './keys'
7
- import { identityKey } from '../../identity/types'
8
- import {
9
- fetchWorkspaceMembers,
10
- inviteWorkspaceMember,
11
- removeWorkspaceMember,
12
- revokeWorkspaceInvitation
13
- } from './api'
8
+ import { getWorkspaceMembers, subscribeWorkspaceDirectory, updateWorkspaceMembers } from './store'
9
+ import { inviteWorkspaceMember, removeWorkspaceMember, revokeWorkspaceInvitation } from './api'
14
10
  import type { WorkspaceMembers } from './types'
15
11
 
16
12
  export type MembershipAction =
@@ -38,25 +34,13 @@ export function applyPendingMembership(
38
34
  }
39
35
  return { members, invitations }
40
36
  }
37
+ // Member lists are published by the cloud host; this only reads what it last sent.
41
38
  export function useWorkspaceMembers(workspaceId: string | undefined) {
42
- const identity = useIdentity()
43
- const accountId = identity ? identityKey(identity) : undefined
44
- const enabled = !!workspaceId && identity?.type === 'user' && !identity.isAnonymous
45
- const accountGeneration = getAccountGeneration()
46
- const mutationKey = ['workspace-members-action', accountGeneration, workspaceId]
47
- const query = useQuery({
48
- queryKey: memberKeys.workspace(accountId, workspaceId ?? ''),
49
- initialData: ownerOnlyWorkspaceMembers,
50
- queryFn: async ({ signal }) => {
51
- const snapshot = await fetchWorkspaceMembers(workspaceId!, signal)
52
- if (signal.aborted || accountGeneration !== getAccountGeneration())
53
- throw new Error('Identity changed')
54
- return snapshot
55
- },
56
- enabled,
57
- retry: false,
58
- staleTime: 30_000
59
- })
39
+ // The owner-only list is built from the identity, so follow it too.
40
+ useIdentity()
41
+ const mutationKey = ['workspace-members-action', getAccountGeneration(), workspaceId]
42
+ const read = () => (workspaceId ? getWorkspaceMembers(workspaceId) : undefined)
43
+ const published = useSyncExternalStore(subscribeWorkspaceDirectory, read, read)
60
44
  const pending = useMutationState({
61
45
  filters: { mutationKey, status: 'pending', exact: true },
62
46
  select: mutation => ({
@@ -64,14 +48,20 @@ export function useWorkspaceMembers(workspaceId: string | undefined) {
64
48
  action: mutation.state.variables as MembershipAction
65
49
  })
66
50
  })
67
- const data = applyPendingMembership(query.data, pending)
68
- return { ...query, data, members: data?.members, invitations: data?.invitations }
51
+ const data = applyPendingMembership(published, pending)
52
+ return {
53
+ data,
54
+ members: data?.members,
55
+ invitations: data?.invitations,
56
+ // Unknown until the host publishes its first snapshot for this account.
57
+ isPending: !published,
58
+ isSuccess: !!published
59
+ }
69
60
  }
70
61
  export function useMembershipAction(workspaceId: string) {
71
62
  const workspaces = useWorkspaces()
72
63
 
73
64
  const identity = useIdentity()
74
- const accountId = identity ? identityKey(identity) : undefined
75
65
  const accountGeneration = getAccountGeneration()
76
66
  const client = useQueryClient()
77
67
  return useMutation({
@@ -85,19 +75,23 @@ export function useMembershipAction(workspaceId: string) {
85
75
  )
86
76
  throw new Error('Sign in to manage workspace members')
87
77
  if (action.type === 'invite') {
88
- const name = workspaces.data?.find(workspace => workspace.id === workspaceId)?.name
78
+ const layout = client.getQueryData<WorkspaceLayoutResponse>(
79
+ workspaceKeys.layout(workspaceId)
80
+ )
81
+ const name = layout?.isRemote
82
+ ? layout.name
83
+ : workspaces.data?.find(workspace => workspace.id === workspaceId)?.name
89
84
  return inviteWorkspaceMember(workspaceId, action.email, name?.trim() || undefined)
90
85
  }
91
86
  if (action.type === 'remove') return removeWorkspaceMember(workspaceId, action.membershipId)
92
87
  return revokeWorkspaceInvitation(workspaceId, action.invitationId)
93
88
  },
94
- onSuccess: async () => {
89
+ // The optimistic overlay ends here, so keep its result until the host's next snapshot.
90
+ onSuccess: (_response, action) => {
95
91
  if (accountGeneration !== getAccountGeneration()) return
96
- // Keep the optimistic overlay until the canonical snapshot has caught up.
97
- await client.invalidateQueries({
98
- queryKey: memberKeys.workspace(accountId, workspaceId),
99
- exact: true
100
- })
92
+ updateWorkspaceMembers(workspaceId, current =>
93
+ applyPendingMembership(current, [{ id: Date.now(), action }])
94
+ )
101
95
  }
102
96
  })
103
97
  }
@@ -1,40 +1,53 @@
1
- import { memberKeys } from './keys'
1
+ import { replaceEqualDeep } from '@tanstack/query-core'
2
2
  import { queryClient } from '@/client/api/query-client'
3
+ import { workspaceKeys } from '@/client/api/workspace-keys'
4
+ import { getWorkspaceAddress } from '@/client/features/workspace/remote'
3
5
  import { getIdentity, getAccountGeneration } from '../../identity/store'
4
6
  import type { Identity } from '../../identity/types'
5
7
  import { collabIdentityId, identityKey } from '../../identity/types'
6
8
  import { normalizeUserProfile } from '../host-state'
7
9
  import type { UserProfile } from '@/lib/collab/types'
8
- import { parseWorkspaceMembers, validWorkspaceId } from './types'
10
+ import { parseWorkspaceMembers, validWorkspaceAddress } from './types'
9
11
  import type { WorkspaceMembers, WorkspaceMembersSnapshot } from './types'
10
12
 
11
- // Host-published memberships remain authoritative until replaced or cleared,
12
- // including for workspaces that have no mounted React observer yet.
13
- queryClient.setQueryDefaults(memberKeys.all, { gcTime: Infinity })
13
+ // The cloud host publishes every member list; Moi never fetches one. Lists are
14
+ // keyed by workspace address: `workspaceId` on the account's own machine,
15
+ // `ownerId:workspaceId` for a workspace someone shared with it.
16
+ type Published = {
17
+ accountGeneration: number
18
+ lists: Map<string, WorkspaceMembers>
19
+ // The host has sent a full snapshot, so a missing list has a meaning.
20
+ complete: boolean
21
+ }
22
+ let published: Published | undefined
23
+ // Lists belong to one account; a switch starts over.
24
+ function current(): Published {
25
+ const accountGeneration = getAccountGeneration()
26
+ if (published?.accountGeneration !== accountGeneration)
27
+ published = { accountGeneration, lists: new Map(), complete: false }
28
+ return published
29
+ }
14
30
 
15
31
  // Revisions track membership writes so older cloud snapshots cannot replace them.
16
32
  let membersRevision = 0
17
- let membersLoadedForAccountGeneration: number | undefined
18
- const collectionListeners = new Set<() => void>()
19
- const ownerSnapshots = new WeakMap<Identity, WorkspaceMembers>()
20
- queryClient.getQueryCache().subscribe(event => {
21
- if (event.query.queryKey[0] !== memberKeys.all[0]) return
22
- if (event.type === 'removed' || (event.type === 'updated' && event.action.type === 'success')) {
23
- membersRevision++
24
- }
25
- })
33
+ const listeners = new Set<() => void>()
34
+ function changed() {
35
+ membersRevision++
36
+ listeners.forEach(listener => listener())
37
+ }
26
38
  export const getWorkspaceMembersRevision = () => membersRevision
39
+
40
+ const ownerSnapshots = new WeakMap<Identity, WorkspaceMembers>()
41
+ const NO_ACCESS: WorkspaceMembers = { members: [], invitations: [] }
27
42
  const directoryCache = new WeakMap<WorkspaceMembers, readonly UserProfile[]>()
28
- // Cloud routing currently opens the owner's machine. No sharing record means
29
- // owner-only; Moi's own workspace registry decides whether that workspace exists.
30
- export function ownerOnlyWorkspaceMembers(): WorkspaceMembers | undefined {
43
+
44
+ // No sharing record means owner-only on the account's own machine, and no
45
+ // access to someone else's. Unknown until the host has published its first snapshot.
46
+ function unlistedWorkspaceMembers(address: string): WorkspaceMembers | undefined {
47
+ if (!current().complete) return undefined
48
+ if (address.includes(':')) return NO_ACCESS
31
49
  const owner = getIdentity()
32
- if (
33
- membersLoadedForAccountGeneration !== getAccountGeneration() ||
34
- owner?.type !== 'user' ||
35
- owner.isAnonymous
36
- )
37
- return undefined
50
+ if (owner?.type !== 'user' || owner.isAnonymous) return undefined
38
51
  let snapshot = ownerSnapshots.get(owner)
39
52
  if (!snapshot) {
40
53
  snapshot = { members: [owner], invitations: [] }
@@ -42,60 +55,70 @@ export function ownerOnlyWorkspaceMembers(): WorkspaceMembers | undefined {
42
55
  }
43
56
  return snapshot
44
57
  }
45
- export function setWorkspaceMembers(workspaceId: string, value: WorkspaceMembers): void {
46
- const identity = getIdentity()
47
- const queryKey = memberKeys.workspace(identity ? identityKey(identity) : undefined, workspaceId)
48
- validWorkspaceId(workspaceId)
49
- const data = parseWorkspaceMembers(value)
50
- // A push supersedes any older in-flight GET for this snapshot.
51
- void queryClient.cancelQueries({ queryKey, exact: true })
52
- queryClient.setQueryData(queryKey, data)
58
+ // An unchanged list keeps its identity, so a repeated snapshot re-renders nothing.
59
+ function store(address: string, data: WorkspaceMembers) {
60
+ const { lists } = current()
61
+ lists.set(address, replaceEqualDeep(lists.get(address), data))
62
+ }
63
+ export function setWorkspaceMembers(address: string, value: WorkspaceMembers): void {
64
+ store(validWorkspaceAddress(address), parseWorkspaceMembers(value))
65
+ changed()
53
66
  }
54
67
  export function setAllWorkspaceMembers(value: WorkspaceMembersSnapshot[]): void {
55
68
  if (!Array.isArray(value)) throw new Error('Invalid workspace members')
56
- const snapshots = value.map(workspace => ({
57
- id: validWorkspaceId(workspace.id),
58
- data: parseWorkspaceMembers(workspace)
59
- }))
60
- const ids = new Set(snapshots.map(workspace => workspace.id))
61
- if (ids.size !== snapshots.length) throw new Error('Duplicate workspace id')
62
- membersLoadedForAccountGeneration = getAccountGeneration()
63
- const identity = getIdentity()
64
- const accountId = identity ? identityKey(identity) : undefined
65
- const prefix = memberKeys.account(accountId)
66
- void queryClient.cancelQueries({ queryKey: prefix })
67
- for (const query of queryClient.getQueryCache().findAll({ queryKey: prefix })) {
68
- if (!ids.has(query.queryKey[2] as string)) forgetWorkspaceMembers(query.queryKey)
69
- }
70
- for (const workspace of snapshots)
71
- queryClient.setQueryData(memberKeys.workspace(accountId, workspace.id), workspace.data)
72
- membersRevision++
73
- collectionListeners.forEach(listener => listener())
69
+ const snapshots = value.map(
70
+ workspace => [validWorkspaceAddress(workspace.id), parseWorkspaceMembers(workspace)] as const
71
+ )
72
+ const addresses = new Set(snapshots.map(([address]) => address))
73
+ if (addresses.size !== snapshots.length) throw new Error('Duplicate workspace id')
74
+ const { lists } = current()
75
+ for (const address of lists.keys()) if (!addresses.has(address)) lists.delete(address)
76
+ for (const [address, data] of snapshots) store(address, data)
77
+ current().complete = true
78
+ changed()
74
79
  }
75
- export function removeWorkspaceMembers(workspaceId: string) {
76
- const identity = getIdentity()
77
- const queryKey = memberKeys.workspace(identity ? identityKey(identity) : undefined, workspaceId)
78
- validWorkspaceId(workspaceId)
79
- void queryClient.cancelQueries({ queryKey, exact: true })
80
- forgetWorkspaceMembers(queryKey)
80
+ export function removeWorkspaceMembers(address: string) {
81
+ current().lists.delete(validWorkspaceAddress(address))
82
+ changed()
81
83
  }
82
84
  export function clearWorkspaceMembers() {
83
- void queryClient.cancelQueries({ queryKey: memberKeys.all })
84
- for (const query of queryClient.getQueryCache().findAll({ queryKey: memberKeys.all }))
85
- forgetWorkspaceMembers(query.queryKey)
85
+ current().lists.clear()
86
+ changed()
86
87
  }
87
- function forgetWorkspaceMembers(queryKey: readonly unknown[]) {
88
- // Removing an observed query leaves its current UI snapshot intact. Publish
89
- // the replacement so mounted views and the presence engine update together.
90
- queryClient.setQueryData<WorkspaceMembers>(
91
- queryKey,
92
- ownerOnlyWorkspaceMembers() ?? { members: [], invitations: [] }
93
- )
88
+
89
+ // A confirmed change shows at once; the host's next snapshot replaces it.
90
+ export function updateWorkspaceMembers(
91
+ workspaceId: string,
92
+ update: (current: WorkspaceMembers) => WorkspaceMembers | undefined
93
+ ): void {
94
+ const address = getWorkspaceAddress(workspaceId)
95
+ const before = current().lists.get(address) ?? unlistedWorkspaceMembers(address)
96
+ const after = before && update(before)
97
+ if (!after) return
98
+ store(address, after)
99
+ changed()
100
+ }
101
+ // A saved profile reaches the member lists it appears in before the host's event does.
102
+ export function refreshMemberProfile(identity: Identity): void {
103
+ const key = identityKey(identity)
104
+ for (const [address, list] of current().lists)
105
+ store(address, {
106
+ ...list,
107
+ members: list.members.map(member =>
108
+ identityKey(member) === key
109
+ ? { ...identity, ...(member.membershipId && { membershipId: member.membershipId }) }
110
+ : member
111
+ )
112
+ })
113
+ changed()
114
+ }
115
+ // What the host last published for this workspace; undefined until it has published anything.
116
+ export function getWorkspaceMembers(workspaceId: string): WorkspaceMembers | undefined {
117
+ const address = getWorkspaceAddress(workspaceId)
118
+ return current().lists.get(address) ?? unlistedWorkspaceMembers(address)
94
119
  }
95
120
  export function getWorkspaceDirectory(workspaceId: string): readonly UserProfile[] | undefined {
96
- const identity = getIdentity()
97
- const queryKey = memberKeys.workspace(identity ? identityKey(identity) : undefined, workspaceId)
98
- const data = queryClient.getQueryData<WorkspaceMembers>(queryKey) ?? ownerOnlyWorkspaceMembers()
121
+ const data = getWorkspaceMembers(workspaceId)
99
122
  if (!data) return undefined
100
123
  let profiles = directoryCache.get(data)
101
124
  if (!profiles) {
@@ -112,12 +135,14 @@ export function getWorkspaceDirectory(workspaceId: string): readonly UserProfile
112
135
  return profiles
113
136
  }
114
137
  export function subscribeWorkspaceDirectory(listener: () => void) {
115
- collectionListeners.add(listener)
138
+ listeners.add(listener)
139
+ // Workspace info decides which address a workspace's list is published under.
116
140
  const unsubscribe = queryClient.getQueryCache().subscribe(event => {
117
- if (event.query.queryKey[0] === memberKeys.all[0]) listener()
141
+ const [scope, kind] = event.query.queryKey
142
+ if (scope === workspaceKeys.all[0] && kind === 'layout') listener()
118
143
  })
119
144
  return () => {
120
- collectionListeners.delete(listener)
145
+ listeners.delete(listener)
121
146
  unsubscribe()
122
147
  }
123
148
  }
@@ -47,7 +47,9 @@ export function parseWorkspaceMembers(value: unknown): WorkspaceMembers {
47
47
  throw new Error('Duplicate workspace member or invitation')
48
48
  return { members, invitations }
49
49
  }
50
- export function validWorkspaceId(id: string): string {
51
- if (!/^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(id)) throw new Error('Invalid workspace id')
52
- return id
50
+ // `workspaceId`, or `ownerId:workspaceId` for a workspace on someone else's machine.
51
+ export function validWorkspaceAddress(address: string): string {
52
+ if (!/^(?:[A-Za-z0-9_-]{1,64}:)?[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(address))
53
+ throw new Error('Invalid workspace id')
54
+ return address
53
55
  }
@@ -1,3 +1,4 @@
1
+ import { getWorkspaceApiBase } from '@/client/features/workspace/remote'
1
2
  import { getWorkspaceDirectory, subscribeWorkspaceDirectory } from '../members/store'
2
3
  import { wsUrl } from '@/client/lib/ws-url'
3
4
  import { COLLAB_PROTOCOL_VERSION } from '@/lib/collab/protocol'
@@ -160,9 +161,7 @@ export class CollabClient implements CollabEngineApi {
160
161
  if (this.socket) return
161
162
  clearTimeout(this.retry)
162
163
  this.store.connecting()
163
- const socket = new WebSocket(
164
- wsUrl(`/api/workspaces/${encodeURIComponent(this.workspaceId)}/collab/ws`)
165
- )
164
+ const socket = new WebSocket(wsUrl(`${getWorkspaceApiBase(this.workspaceId)}/collab`))
166
165
  this.socket = socket
167
166
  socket.onopen = () => {
168
167
  if (socket !== this.socket) return