moi-computer 0.12.1-next.3 → 0.13.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 (236) hide show
  1. package/bunfig.toml +1 -5
  2. package/client/api/app-config.ts +1 -2
  3. package/client/api/app-ui-keys.ts +3 -1
  4. package/client/api/http.ts +4 -1
  5. package/client/api/workspace-keys.ts +3 -3
  6. package/client/app/app-provider.tsx +22 -2
  7. package/client/app/app-router.tsx +4 -0
  8. package/client/app/routes/home-route.tsx +14 -0
  9. package/client/app/routes/onboarding-route.tsx +11 -0
  10. package/client/app/routes/settings-route.tsx +1 -1
  11. package/client/app/routes/workspace-route.tsx +21 -19
  12. package/client/components/shared/screen-shell.tsx +23 -0
  13. package/client/components/shared/ultrafast-icon.tsx +61 -0
  14. package/client/components/shared/workspace-icon.tsx +2 -2
  15. package/client/features/account/account-test-utils.tsx +4 -3
  16. package/client/features/app-settings/api.ts +33 -0
  17. package/client/features/app-settings/general-settings.tsx +39 -0
  18. package/client/features/{settings → app-settings}/settings-page.tsx +5 -1
  19. package/client/features/chat/applet-chat-intents.ts +4 -20
  20. package/client/features/chat/chat-send.ts +17 -13
  21. package/client/features/chat/composer/model-order.ts +8 -4
  22. package/client/features/chat/composer/model-picker.tsx +88 -34
  23. package/client/features/chat/composer/use-workspace-composer-state.tsx +11 -3
  24. package/client/features/chat/composer/use-workspace-skill-updates.ts +1 -1
  25. package/client/features/chat/connection/chat-frames.ts +3 -15
  26. package/client/features/chat/messages/chat-empty-state.tsx +1 -4
  27. package/client/features/chat/messages/fork-history.ts +5 -5
  28. package/client/features/chat/messages/tool-group/subagent-card.tsx +1 -0
  29. package/client/features/chat/messages/turn-view.tsx +3 -12
  30. package/client/features/chat/messages/user-message-bubble.tsx +63 -0
  31. package/client/features/chat/sessions/api.ts +121 -80
  32. package/client/features/chat/sessions/browser-tab-state.ts +14 -12
  33. package/client/features/chat/sessions/chat-selector.tsx +5 -6
  34. package/client/features/chat/sessions/use-selected-session.ts +172 -257
  35. package/client/features/chat/use-chat.ts +14 -19
  36. package/client/features/collab/components/user-avatar.tsx +4 -10
  37. package/client/features/collab/workspace-users-menu.tsx +1 -1
  38. package/client/features/connectors/api.ts +12 -4
  39. package/client/features/connectors/mcp-server-cards.tsx +17 -7
  40. package/client/features/connectors/read-connectors.ts +35 -0
  41. package/client/features/demo/demo-dialog.tsx +11 -3
  42. package/client/features/dev/collab/local-user-settings.tsx +1 -1
  43. package/client/features/dev/dev-index-page.tsx +5 -0
  44. package/client/features/dev/dev-routes.tsx +2 -0
  45. package/client/features/dev/harness-debug-page.tsx +3 -3
  46. package/client/features/dev/onboarding-preview-page.tsx +76 -0
  47. package/client/features/dev/workspace-setup-mocks.ts +16 -14
  48. package/client/features/dev/workspace-setup-preview.tsx +11 -17
  49. package/client/features/harness/api.ts +45 -15
  50. package/client/features/harness/device-login-steps.tsx +9 -3
  51. package/client/features/harness/harness-login-panel.tsx +228 -0
  52. package/client/features/harness/harness-presentation.ts +15 -0
  53. package/client/features/harness/harness-selector.tsx +119 -0
  54. package/client/features/harness/use-harness-login.ts +77 -0
  55. package/client/features/home/home-page.tsx +7 -7
  56. package/client/features/onboarding/agent-setup-step.tsx +57 -0
  57. package/client/features/onboarding/api.ts +42 -0
  58. package/client/features/onboarding/onboarding-idea-disk.tsx +129 -0
  59. package/client/features/onboarding/onboarding-page.tsx +32 -0
  60. package/client/features/onboarding/onboarding-screen.tsx +20 -0
  61. package/client/features/onboarding/onboarding-status.tsx +24 -0
  62. package/client/features/onboarding/workspace-setup-step.tsx +239 -0
  63. package/client/features/overview/default/registry.tsx +2 -2
  64. package/client/features/overview/default/views-widget.tsx +6 -5
  65. package/client/features/overview/overview.tsx +9 -11
  66. package/client/features/overview/widget-canvas.tsx +1 -1
  67. package/client/features/overview/workspace-name.tsx +2 -2
  68. package/client/features/views/api.ts +69 -16
  69. package/client/features/views/pending-view-screen.tsx +30 -9
  70. package/client/features/views/use-view-actions.ts +19 -20
  71. package/client/features/views/view-presentation.ts +2 -2
  72. package/client/features/views/view-submission.ts +28 -41
  73. package/client/features/workspace/add/api.ts +3 -23
  74. package/client/features/workspace/add/create-workspace-dialog.tsx +6 -5
  75. package/client/features/workspace/add/import-workspace-dialog.tsx +3 -3
  76. package/client/features/workspace/add/workspace-agent-options.ts +11 -12
  77. package/client/features/workspace/add/workspace-agent-selector.tsx +3 -132
  78. package/client/features/workspace/api.ts +32 -36
  79. package/client/features/workspace/harness-transition.ts +89 -0
  80. package/client/features/{settings → workspace/settings}/api.ts +1 -34
  81. package/client/features/{settings → workspace/settings}/connectors-settings.tsx +31 -10
  82. package/client/features/workspace/settings/general-settings.tsx +180 -0
  83. package/client/features/{settings → workspace/settings}/use-env-vars.ts +1 -1
  84. package/client/features/{settings → workspace/settings}/workspace-icon-picker.tsx +3 -3
  85. package/client/features/workspace/setup/workspace-login-state.tsx +37 -156
  86. package/client/features/workspace/setup/workspace-recovery-states.tsx +0 -12
  87. package/client/features/workspace/setup/workspace-setup.tsx +34 -14
  88. package/client/features/workspace/setup/workspace-welcome-state.tsx +2 -2
  89. package/client/features/workspace/use-workspace-navigation.ts +25 -9
  90. package/client/features/workspace/workspace-content.tsx +11 -8
  91. package/client/features/workspace/workspace-frame.tsx +2 -1
  92. package/client/features/workspace/workspace-layout-context.tsx +5 -2
  93. package/client/features/workspace/workspace-menu.tsx +12 -8
  94. package/client/features/workspace/workspace-pending-screen.tsx +10 -10
  95. package/client/features/workspace/workspace-presentation.tsx +5 -13
  96. package/client/features/workspace/workspace-state.ts +169 -0
  97. package/client/features/workspace/workspace-tabs.tsx +22 -2
  98. package/client/index.css +2 -0
  99. package/client/index.tsx +3 -3
  100. package/client/lib/flags.ts +1 -1
  101. package/client/lib/{app-icon-registry.ts → identity-icon-registry.ts} +10 -14
  102. package/client/runtime/workspace-events.ts +21 -4
  103. package/client/runtime/workspace-theme.ts +9 -0
  104. package/client/tailwind-config.ts +43 -0
  105. package/dist/chunk-2y59vrxg.js +5 -0
  106. package/dist/chunk-58dyt9bk.js +2 -0
  107. package/dist/{chunk-tqd3pkg8.css → chunk-60q353jq.css} +1 -1
  108. package/dist/chunk-bncc7heg.js +1 -0
  109. package/dist/{chunk-11cab5dq.css → chunk-cq2r11fg.css} +1 -1
  110. package/dist/chunk-cqt3s31j.js +546 -0
  111. package/dist/chunk-dcmks9sm.js +75 -0
  112. package/dist/chunk-md45evr2.css +1 -0
  113. package/dist/chunk-n0nab26d.js +1 -0
  114. package/dist/chunk-nf2wkvf9.js +14 -0
  115. package/dist/chunk-nmzhgyp9.js +2 -0
  116. package/dist/chunk-q37knr3m.js +2 -0
  117. package/dist/chunk-s90m4qkz.js +1 -0
  118. package/dist/chunk-wjvd4byz.js +2 -0
  119. package/dist/index.html +1 -1
  120. package/lib/harness-instructions.ts +1 -1
  121. package/lib/{app-icons.ts → identity-icons.ts} +6 -7
  122. package/lib/onboarding.ts +41 -0
  123. package/lib/speed-mode.ts +5 -0
  124. package/lib/types.ts +75 -64
  125. package/lib/view-build-directives.ts +1 -2
  126. package/lib/workspace-icon.ts +2 -2
  127. package/lib/workspace-layout.ts +1 -1
  128. package/lib/workspace-name.ts +15 -12
  129. package/package.json +11 -8
  130. package/server/README.md +27 -25
  131. package/server/api/harnesses.ts +31 -8
  132. package/server/api/index.ts +189 -127
  133. package/server/api/onboarding.ts +46 -0
  134. package/server/app-config.ts +16 -24
  135. package/server/app-settings.ts +6 -2
  136. package/server/applets/config.ts +5 -5
  137. package/server/applets/views.ts +19 -17
  138. package/server/cli/commands/init.ts +3 -3
  139. package/server/cli/commands/skill.ts +16 -17
  140. package/server/cli/commands/start.ts +8 -34
  141. package/server/cli/server.ts +18 -6
  142. package/server/collab/config.ts +1 -2
  143. package/server/collab/manager.ts +4 -1
  144. package/server/collab/skill/index.ts +1 -1
  145. package/server/control.ts +3 -3
  146. package/server/data-dir.ts +1 -1
  147. package/server/harness/README.md +24 -8
  148. package/server/harness/acp/session.ts +8 -4
  149. package/server/harness/auth.ts +14 -17
  150. package/server/harness/claude-code/NOTES.md +4 -3
  151. package/server/harness/claude-code/index.ts +5 -6
  152. package/server/harness/claude-code/mcp.ts +17 -0
  153. package/server/harness/claude-code/models.ts +2 -20
  154. package/server/harness/claude-code/run-action.ts +123 -0
  155. package/server/harness/claude-code/session-title.ts +5 -7
  156. package/server/harness/claude-code/session.ts +24 -16
  157. package/server/harness/codex/NOTES.md +22 -10
  158. package/server/harness/codex/adapter.ts +14 -17
  159. package/server/harness/codex/client.ts +8 -5
  160. package/server/harness/codex/connectors.ts +92 -0
  161. package/server/harness/codex/index.ts +4 -1
  162. package/server/harness/codex/pagination.ts +8 -5
  163. package/server/harness/codex/run-action.ts +93 -0
  164. package/server/harness/codex/session-title.ts +6 -8
  165. package/server/harness/codex/session.ts +11 -11
  166. package/server/harness/codex/transport.ts +13 -6
  167. package/server/harness/command.ts +45 -0
  168. package/server/harness/executable.ts +5 -0
  169. package/server/harness/fork.ts +6 -4
  170. package/server/harness/install.ts +49 -0
  171. package/server/harness/openclaw/session.ts +1 -2
  172. package/server/harness/run-action.ts +85 -0
  173. package/server/harness/session-title.ts +0 -46
  174. package/server/harness/types.ts +30 -3
  175. package/server/onboarding/brief.ts +29 -0
  176. package/server/onboarding/ideas.ts +48 -0
  177. package/server/onboarding/jobs.ts +291 -0
  178. package/server/onboarding/prompt.md +49 -0
  179. package/server/sessions/archive.ts +5 -14
  180. package/server/sessions/config.ts +17 -16
  181. package/server/sessions/lifecycle.ts +15 -13
  182. package/server/sessions/pending-views.ts +79 -108
  183. package/server/sessions/selected.ts +75 -161
  184. package/server/sessions/store.ts +40 -113
  185. package/server/sessions/view-sessions.ts +128 -111
  186. package/server/skills/locations.ts +82 -0
  187. package/server/skills/lock.ts +29 -0
  188. package/server/skills/template.ts +36 -25
  189. package/server/skills/update.ts +52 -5
  190. package/server/skills/version.ts +17 -22
  191. package/server/skills/workspace.ts +121 -0
  192. package/server/web.ts +22 -8
  193. package/server/workspace-state/file.ts +177 -0
  194. package/server/workspace-state/index.ts +162 -0
  195. package/server/workspace-state/legacy-migration.ts +79 -0
  196. package/server/workspace-state/lock.ts +42 -0
  197. package/server/workspace-state/types.ts +16 -0
  198. package/server/workspaces/change-harness.ts +88 -0
  199. package/server/workspaces/harness-transition.ts +29 -0
  200. package/server/workspaces/init.ts +29 -6
  201. package/server/workspaces/layout.ts +3 -0
  202. package/server/workspaces/registry.ts +27 -1
  203. package/{workspace/.claude/skills/moi-workspace → skill}/SKILL.md +7 -5
  204. package/{workspace/.claude/skills/moi-workspace → skill}/references/COLLAB.md +4 -8
  205. package/skill/references/DESIGN.md +167 -0
  206. package/{workspace/.claude/skills/moi-workspace → skill}/references/SCRATCHPAD.md +2 -2
  207. package/{workspace/.claude/skills/moi-workspace → skill}/references/UI-COMPONENTS.md +3 -0
  208. package/ui-components/badge.tsx +1 -1
  209. package/client/accept-hmr-dependencies.ts +0 -22
  210. package/client/features/settings/general-settings.tsx +0 -103
  211. package/client/features/workspace/setup/workspace-setup-panel.tsx +0 -18
  212. package/dist/chunk-20ynagd4.js +0 -2
  213. package/dist/chunk-42tde9a6.js +0 -1
  214. package/dist/chunk-9w543s0r.js +0 -2
  215. package/dist/chunk-a5372n61.js +0 -546
  216. package/dist/chunk-aja9y1vn.js +0 -68
  217. package/dist/chunk-fvcvrh3d.css +0 -1
  218. package/dist/chunk-gtfjw3nx.js +0 -1
  219. package/dist/chunk-j39y2aky.js +0 -14
  220. package/dist/chunk-pcnahct1.js +0 -5
  221. package/dist/chunk-pja30km3.js +0 -1
  222. package/dist/chunk-rm894bc2.js +0 -2
  223. package/dist/chunk-tevvtfbk.js +0 -10
  224. package/dist/chunk-tgt9zd0r.js +0 -2
  225. package/dist/chunk-tv5k5z00.js +0 -1
  226. package/dist/chunk-zq2ae5dx.js +0 -2
  227. package/lib/experimental.ts +0 -14
  228. package/workspace/.claude/skills/moi-workspace/references/DESIGN.md +0 -157
  229. /package/client/features/{settings → app-settings}/profile-settings.tsx +0 -0
  230. /package/client/features/{settings → app-settings}/settings-section.tsx +0 -0
  231. /package/client/features/{settings → app-settings}/users-settings.tsx +0 -0
  232. /package/client/features/{settings → workspace/settings}/environment-settings.tsx +0 -0
  233. /package/client/features/{settings → workspace/settings}/icon-picker-options.ts +0 -0
  234. /package/client/features/{settings → workspace/settings}/settings-layout.tsx +0 -0
  235. /package/client/features/{settings → workspace/settings}/workspace-settings-dialog.tsx +0 -0
  236. /package/{workspace/.claude/skills/moi-workspace → skill}/references/INTENTS.md +0 -0
@@ -1,39 +1,69 @@
1
- import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
1
+ import { useQuery, useQueryClient } from '@tanstack/react-query'
2
2
  import type { QueryClient } from '@tanstack/react-query'
3
- import { useCallback } from 'react'
3
+ import { useCallback, useMemo } from 'react'
4
4
 
5
- import { jsonRequest, requestJson, requestVoid } from '@/client/api/http'
6
- import { WORKSPACE_RESOURCE_OPTIONS } from '@/client/api/query-options'
5
+ import { jsonRequest, requestJson } from '@/client/api/http'
7
6
  import { workspaceKeys } from '@/client/api/workspace-keys'
8
7
  import { sessionViewOptions } from '@/client/features/chat/sessions/session-view'
9
8
  import { readSelectedSession, writeSelectedSession } from './browser-tab-state'
10
9
  import {
11
- selectedSessionKey,
12
- selectedSessionOptions,
10
+ browserTabSelectionKey,
11
+ browserTabSelectionOptions,
13
12
  usePinnedSession,
14
13
  useSelectedSession
15
14
  } from './use-selected-session'
16
15
  import { useCollabEnabled } from '@/client/features/collab'
17
16
  import { useLatestRef } from '@/client/lib/use-latest-ref'
18
17
  import { useWorkspaceId } from '@/client/features/workspace/workspace-context'
18
+ import {
19
+ changeWorkspaceStateCache,
20
+ clearSessionInState,
21
+ useWorkspaceState,
22
+ useWorkspaceStateWrite,
23
+ workspaceStateOptions
24
+ } from '@/client/features/workspace/workspace-state'
19
25
  import type {
20
26
  SessionConfig,
21
27
  SessionInfo,
22
- SessionRecord,
23
- WorkspaceSessionSelection,
28
+ SessionSummary,
29
+ WorkspaceSessionState,
30
+ WorkspaceState,
24
31
  WorkspaceTabId
25
32
  } from '@/lib/types'
33
+ import type { BrowserTabSelection } from './browser-tab-state'
26
34
 
35
+ // The agent's chat list. Reading it makes the harness scan transcripts, so
36
+ // mounts within 30 s reuse it; harness `sessions_changed` frames refetch it,
37
+ // and focus picks up chats made outside Moi.
27
38
  export function workspaceSessionsOptions(workspaceId: string) {
28
39
  return {
29
40
  queryKey: workspaceKeys.sessions(workspaceId),
30
- queryFn: () => requestJson<SessionInfo[]>(`/api/workspaces/${workspaceId}/sessions`),
31
- ...WORKSPACE_RESOURCE_OPTIONS
41
+ queryFn: () => requestJson<SessionSummary[]>(`/api/workspaces/${workspaceId}/sessions`),
42
+ staleTime: 30_000,
43
+ gcTime: 5 * 60_000,
44
+ refetchOnMount: true,
45
+ refetchOnWindowFocus: true
32
46
  }
33
47
  }
34
48
 
35
- export function useWorkspaceSessions(workspaceId: string) {
36
- return useQuery(workspaceSessionsOptions(workspaceId))
49
+ const selectSessionEntries = (state: WorkspaceState) => state.sessions
50
+
51
+ // List rows joined with the tab that owns each chat. Undefined until both
52
+ // have loaded, so view chats never flash in the overview.
53
+ export function useWorkspaceSessions(workspaceId: string): { data: SessionInfo[] | undefined } {
54
+ const list = useQuery(workspaceSessionsOptions(workspaceId)).data
55
+ const entries = useWorkspaceState(workspaceId, selectSessionEntries).data
56
+ const data = useMemo(
57
+ () =>
58
+ list && entries
59
+ ? list.map(session => {
60
+ const tabId = entries[session.sessionId]?.tabId
61
+ return tabId ? { ...session, tabId } : session
62
+ })
63
+ : undefined,
64
+ [list, entries]
65
+ )
66
+ return { data }
37
67
  }
38
68
 
39
69
  // Select the linked chat on the destination tab, unpinning a different chat first.
@@ -42,47 +72,58 @@ export function useSelectLinkedChat() {
42
72
  const queryClient = useQueryClient()
43
73
  const collabEnabled = useCollabEnabled()
44
74
  const [, , selectSessionForTab] = useSelectedSession()
45
- const { pinAsync } = usePinnedSession()
46
- const stateRef = useLatestRef({ collabEnabled, selectSessionForTab, pinAsync })
75
+ const { unpinAsync } = usePinnedSession()
76
+ const stateRef = useLatestRef({ collabEnabled, selectSessionForTab, unpinAsync })
47
77
  // Stable so selection updates do not restart a pending chat link.
48
78
  return useCallback(
49
- async (sessionId: string, tab: WorkspaceTabId, signal: AbortSignal): Promise<boolean> => {
50
- const { collabEnabled, selectSessionForTab, pinAsync } = stateRef.current
79
+ async (
80
+ sessionId: string,
81
+ tab: WorkspaceTabId,
82
+ signal: AbortSignal
83
+ ): Promise<WorkspaceTabId | undefined> => {
84
+ const { collabEnabled, selectSessionForTab, unpinAsync } = stateRef.current
51
85
  signal.throwIfAborted()
52
86
  let sessions =
53
- queryClient.getQueryData<SessionInfo[]>(workspaceKeys.sessions(workspaceId)) ?? []
87
+ queryClient.getQueryData<SessionSummary[]>(workspaceKeys.sessions(workspaceId)) ?? []
54
88
  if (!sessions.some(session => session.sessionId === sessionId))
55
- sessions = await queryClient.fetchQuery(workspaceSessionsOptions(workspaceId))
89
+ // The chat may be newer than a cached list that is still fresh.
90
+ sessions = await queryClient.fetchQuery({
91
+ ...workspaceSessionsOptions(workspaceId),
92
+ staleTime: 0
93
+ })
56
94
  signal.throwIfAborted()
57
- if (!sessions.some(session => session.sessionId === sessionId)) return false
58
- await queryClient.ensureQueryData(selectedSessionOptions(workspaceId))
59
- if (collabEnabled)
60
- await queryClient.ensureQueryData(selectedSessionOptions(workspaceId, 'browser-tab'))
95
+ if (!sessions.some(session => session.sessionId === sessionId)) return undefined
96
+ const state = await queryClient.ensureQueryData(
97
+ workspaceStateOptions(queryClient, workspaceId)
98
+ )
99
+ if (collabEnabled) await queryClient.ensureQueryData(browserTabSelectionOptions(workspaceId))
61
100
  signal.throwIfAborted()
62
- const pinned = queryClient.getQueryData<WorkspaceSessionSelection>(
63
- selectedSessionKey(workspaceId)
64
- )?.pinned
101
+ const pinned = state.pinnedSessionId
102
+ const destination =
103
+ pinned === sessionId ? tab : (state.sessions[sessionId]?.tabId ?? 'overview')
65
104
  if (pinned && pinned !== sessionId) {
66
- await pinAsync(null)
105
+ await unpinAsync()
67
106
  signal.throwIfAborted()
68
107
  }
69
108
  if (pinned !== sessionId) {
70
- await selectSessionForTab(sessionId, tab)
109
+ await selectSessionForTab(sessionId, destination)
71
110
  signal.throwIfAborted()
72
111
  }
73
- return true
112
+ return destination
74
113
  },
75
114
  [queryClient, stateRef, workspaceId]
76
115
  )
77
116
  }
78
117
 
79
- export function removeArchivedSession(
80
- sessions: SessionInfo[] | undefined,
118
+ export function removeArchivedSession<T extends SessionSummary>(
119
+ sessions: T[] | undefined,
81
120
  sessionId: string
82
- ): SessionInfo[] | undefined {
121
+ ): T[] | undefined {
83
122
  return sessions?.filter(session => session.sessionId !== sessionId)
84
123
  }
85
124
 
125
+ // Drop an archived chat everywhere this browser keeps it. The server's state
126
+ // arrives with the archive reply or a `state:changed` event.
86
127
  export function applyArchivedSession(
87
128
  queryClient: QueryClient,
88
129
  workspaceId: string,
@@ -90,47 +131,39 @@ export function applyArchivedSession(
90
131
  ) {
91
132
  const sessionsKey = workspaceKeys.sessions(workspaceId)
92
133
  void queryClient.cancelQueries({ queryKey: sessionsKey })
93
- queryClient.setQueryData<SessionInfo[]>(sessionsKey, current =>
134
+ queryClient.setQueryData<SessionSummary[]>(sessionsKey, current =>
94
135
  removeArchivedSession(current, sessionId)
95
136
  )
96
137
  queryClient.removeQueries({ queryKey: workspaceKeys.events(workspaceId, sessionId) })
97
- queryClient.removeQueries({ queryKey: workspaceKeys.session(workspaceId, sessionId) })
98
- for (const scope of ['shared', 'browser-tab'] as const) {
99
- const key = selectedSessionKey(workspaceId, scope)
100
- const current =
101
- queryClient.getQueryData<WorkspaceSessionSelection>(key) ??
102
- (scope === 'browser-tab' ? readSelectedSession(workspaceId) : undefined)
103
- if (!current) continue
104
- let changed = current.pinned === sessionId
105
- const selected = Object.fromEntries(Object.entries(current.selected))
106
- for (const [tabId, id] of Object.entries(selected)) {
107
- if (id !== sessionId) continue
108
- changed = true
109
- // An explicit empty local choice prevents falling back to the server's chat.
110
- if (scope === 'browser-tab') selected[tabId] = null
111
- else delete selected[tabId]
112
- }
113
- if (!changed) continue
114
- const next = { selected, pinned: current.pinned === sessionId ? null : current.pinned }
115
- queryClient.setQueryData(key, next)
116
- if (scope === 'browser-tab') writeSelectedSession(workspaceId, next)
117
- }
118
- void queryClient.invalidateQueries({ queryKey: selectedSessionKey(workspaceId), exact: true })
138
+ changeWorkspaceStateCache(queryClient, workspaceId, draft =>
139
+ clearSessionInState(draft, sessionId)
140
+ )
141
+ const localKey = browserTabSelectionKey(workspaceId)
142
+ const local =
143
+ queryClient.getQueryData<BrowserTabSelection>(localKey) ?? readSelectedSession(workspaceId)
144
+ if (!Object.values(local.selected).includes(sessionId)) return
145
+ // An explicit empty local choice prevents falling back to the server's chat.
146
+ const selected = Object.fromEntries(
147
+ Object.entries(local.selected).map(([tabId, id]) => [tabId, id === sessionId ? null : id])
148
+ )
149
+ queryClient.setQueryData<BrowserTabSelection>(localKey, { selected })
150
+ writeSelectedSession(workspaceId, { selected })
119
151
  }
120
152
 
121
153
  export function useArchiveWorkspaceSession(workspaceId: string) {
122
154
  const queryClient = useQueryClient()
123
- return useMutation<void, Error, string>({
124
- mutationFn: sessionId =>
125
- requestVoid(
126
- `/api/workspaces/${workspaceId}/sessions/${sessionId}/archive`,
155
+ return useWorkspaceStateWrite<string>(workspaceId, {
156
+ request: sessionId =>
157
+ requestJson<WorkspaceState>(
158
+ `/api/workspaces/${workspaceId}/sessions/${encodeURIComponent(sessionId)}/archive`,
127
159
  { method: 'POST' },
128
160
  'Couldn’t archive chat'
129
161
  ),
130
- onSuccess: (_, sessionId) => {
162
+ onSuccess: (_state, sessionId) => {
131
163
  applyArchivedSession(queryClient, workspaceId, sessionId)
132
- queryClient.invalidateQueries({ queryKey: workspaceKeys.previews })
133
- }
164
+ void queryClient.invalidateQueries({ queryKey: workspaceKeys.previews })
165
+ },
166
+ errorTitle: 'Couldn’t archive chat'
134
167
  })
135
168
  }
136
169
 
@@ -141,29 +174,37 @@ export function useSessionView(workspaceId: string, sessionId: string | null) {
141
174
  })
142
175
  }
143
176
 
144
- export function useSession(workspaceId: string, sessionId: string | null) {
145
- return useQuery<SessionRecord>({
146
- queryKey: workspaceKeys.session(workspaceId, sessionId ?? ''),
147
- queryFn: () => requestJson(`/api/workspaces/${workspaceId}/sessions/${sessionId}`),
148
- enabled: Boolean(sessionId),
149
- staleTime: Infinity,
150
- gcTime: 5 * 60_000,
151
- refetchOnMount: false,
152
- refetchOnWindowFocus: false
153
- })
177
+ const NO_SESSION: WorkspaceSessionState = {}
178
+
179
+ // Moi's record of one chat: tab, settings, fork history. Undefined until the
180
+ // workspace state loads; a chat without a record reads as empty.
181
+ export function useSessionState(workspaceId: string, sessionId: string | null) {
182
+ return useWorkspaceState(workspaceId, state =>
183
+ sessionId ? (state.sessions[sessionId] ?? NO_SESSION) : NO_SESSION
184
+ )
154
185
  }
155
186
 
187
+ // null clears a setting; undefined leaves it unchanged.
188
+ export type SessionConfigPatch = { [Key in keyof SessionConfig]?: SessionConfig[Key] | null }
189
+
156
190
  export function useSaveSessionConfig(workspaceId: string) {
157
- const queryClient = useQueryClient()
158
- return useMutation<SessionRecord, Error, { sessionId: string; patch: SessionConfig }>({
159
- mutationFn: ({ patch, sessionId }) =>
160
- requestJson(
161
- `/api/workspaces/${workspaceId}/sessions/${sessionId}/config`,
162
- jsonRequest('PUT', patch),
191
+ return useWorkspaceStateWrite<{ sessionId: string; patch: SessionConfigPatch }>(workspaceId, {
192
+ request: ({ sessionId, patch }) =>
193
+ requestJson<WorkspaceState>(
194
+ `/api/workspaces/${workspaceId}/sessions/${encodeURIComponent(sessionId)}/config`,
195
+ jsonRequest('PATCH', patch),
163
196
  'Failed to save chat settings'
164
197
  ),
165
- onSuccess: (next, { sessionId }) => {
166
- queryClient.setQueryData<SessionRecord>(workspaceKeys.session(workspaceId, sessionId), next)
167
- }
198
+ optimistic: (draft, { sessionId, patch }) => {
199
+ const session = (draft.sessions[sessionId] ??= {})
200
+ const config: SessionConfig = { ...session.config }
201
+ for (const key of ['model', 'effort', 'speedMode'] as const) {
202
+ const value = patch[key]
203
+ if (value === null) delete config[key]
204
+ else if (value !== undefined) Object.assign(config, { [key]: value })
205
+ }
206
+ session.config = config
207
+ },
208
+ errorTitle: 'Couldn’t save chat settings'
168
209
  })
169
210
  }
@@ -1,16 +1,22 @@
1
- import type { WorkspaceSessionSelection } from '@/lib/types'
2
-
3
- const keyFor = (workspaceId: string) => `moi:collab:${workspaceId}:session`
4
- const emptySelection = (): WorkspaceSessionSelection => ({ selected: {}, pinned: null })
1
+ import type { WorkspaceTabId } from '@/lib/types'
5
2
 
6
3
  // Collab keeps each browser tab's unpinned chat choice, including a separate
7
4
  // selection for every workspace tab. The workspace pin remains server-owned.
8
- export function readSelectedSession(workspaceId: string): WorkspaceSessionSelection {
5
+ export type BrowserTabSelection = {
6
+ // A browser tab can explicitly choose an empty chat (null). An absent key
7
+ // inherits the server selection, including CLI-created view handoffs.
8
+ selected: Partial<Record<WorkspaceTabId, string | null>>
9
+ }
10
+
11
+ const keyFor = (workspaceId: string) => `moi:collab:${workspaceId}:session`
12
+ const emptySelection = (): BrowserTabSelection => ({ selected: {} })
13
+
14
+ export function readSelectedSession(workspaceId: string): BrowserTabSelection {
9
15
  try {
10
16
  const saved = sessionStorage.getItem(keyFor(workspaceId))
11
17
  if (!saved) return emptySelection()
12
18
  // The first Collab build stored only the overview chat ID.
13
- if (!saved.startsWith('{')) return { selected: { overview: saved }, pinned: null }
19
+ if (!saved.startsWith('{')) return { selected: { overview: saved } }
14
20
  const value: unknown = JSON.parse(saved)
15
21
  if (!value || typeof value !== 'object' || !('selected' in value)) return emptySelection()
16
22
  const selected = (value as { selected: unknown }).selected
@@ -21,18 +27,14 @@ export function readSelectedSession(workspaceId: string): WorkspaceSessionSelect
21
27
  Object.entries(selected).filter(
22
28
  ([, sessionId]) => typeof sessionId === 'string' || sessionId === null
23
29
  )
24
- ),
25
- pinned: null
30
+ )
26
31
  }
27
32
  } catch {
28
33
  return emptySelection()
29
34
  }
30
35
  }
31
36
 
32
- export function writeSelectedSession(
33
- workspaceId: string,
34
- selection: WorkspaceSessionSelection
35
- ): void {
37
+ export function writeSelectedSession(workspaceId: string, selection: BrowserTabSelection): void {
36
38
  try {
37
39
  sessionStorage.setItem(keyFor(workspaceId), JSON.stringify({ selected: selection.selected }))
38
40
  } catch {
@@ -2,7 +2,7 @@ import { useRef, useState } from 'react'
2
2
  import { IconArchive, IconEdit, IconHistory, IconPin, IconPinFilled } from '@tabler/icons-react'
3
3
 
4
4
  import { useArchiveWorkspaceSession, useWorkspaceSessions } from './api'
5
- import { useWorkspaceAgent } from '@/client/features/workspace/api'
5
+ import { useWorkspaceHarness } from '@/client/features/workspace/api'
6
6
  import { useWorkspaceId } from '@/client/features/workspace/workspace-context'
7
7
  import {
8
8
  useSelectedSession,
@@ -14,7 +14,6 @@ import { isSessionRunning, liveStore, useLive } from '@/client/features/chat/cha
14
14
  import type { SessionInfo, WorkspaceTabId } from '@/lib/types'
15
15
 
16
16
  import { Button } from '@/client/components/ui/button'
17
- import { toast } from '@/client/components/ui/toast'
18
17
  import { Tooltip, TooltipContent, TooltipTrigger } from '@/client/components/ui/tooltip'
19
18
  import {
20
19
  DropdownMenu,
@@ -89,7 +88,7 @@ export function ChatSessionItem({
89
88
  try {
90
89
  await onArchive(session.sessionId)
91
90
  } catch {
92
- toast.add({ title: 'Couldn’t archive chat', type: 'error' })
91
+ // The archive request already reported the failure.
93
92
  } finally {
94
93
  pendingRef.current = false
95
94
  setPending(false)
@@ -248,7 +247,7 @@ export function groupSessionsForTab(
248
247
 
249
248
  export function ChatSelector({ className }: ChatSelectorProps) {
250
249
  const workspaceId = useWorkspaceId()
251
- const { pinnedSessionId, loaded, pin } = usePinnedSession()
250
+ const { pinnedSessionId, loaded, pin, unpin } = usePinnedSession()
252
251
  const [selectedSessionId] = useSelectedSession()
253
252
  const { data: sessions = [] } = useWorkspaceSessions(workspaceId)
254
253
  if (!loaded) return null
@@ -271,7 +270,7 @@ export function ChatSelector({ className }: ChatSelectorProps) {
271
270
  variant="ghost"
272
271
  size="icon-sm"
273
272
  className={className}
274
- onClick={() => pin(null)}
273
+ onClick={unpin}
275
274
  aria-label="Unpin chat"
276
275
  aria-pressed={true}
277
276
  >
@@ -316,7 +315,7 @@ function SessionSelector({ className, sessions }: SessionSelectorProps) {
316
315
  const tabId = useCurrentTabId()
317
316
  const [selectedSessionId, selectSession] = useSelectedSession()
318
317
  const [confirmingSessionId, setConfirmingSessionId] = useState<string | null>(null)
319
- const canArchive = useWorkspaceAgent(workspaceId).data?.supportsArchiving === true
318
+ const canArchive = useWorkspaceHarness(workspaceId).data?.supportsArchiving === true
320
319
  const archiveSession = useArchiveWorkspaceSession(workspaceId)
321
320
  const sessionGroups = groupSessionsForTab(sessions, tabId)
322
321