moi-computer 0.12.0 → 0.12.1-next.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 (140) hide show
  1. package/README.md +1 -1
  2. package/client/AGENTS.md +2 -2
  3. package/client/api/app-ui-keys.ts +1 -1
  4. package/client/api/workspace-keys.ts +1 -3
  5. package/client/app/routes/WorkspaceRoute.tsx +2 -8
  6. package/client/features/applets/applet-runtime.ts +11 -13
  7. package/client/features/chat/ChatPanel.tsx +70 -49
  8. package/client/features/chat/ChatPopup.tsx +81 -8
  9. package/client/features/chat/applet-chat-intents.ts +31 -12
  10. package/client/features/chat/chat-send.ts +33 -1
  11. package/client/features/chat/chat-store.ts +10 -0
  12. package/client/features/chat/composer/ChatComposer.tsx +24 -27
  13. package/client/features/chat/composer/ModelPicker.tsx +3 -3
  14. package/client/features/chat/composer/useWorkspaceComposerState.tsx +3 -3
  15. package/client/features/chat/connection/chat-frames.ts +14 -7
  16. package/client/features/chat/messages/ChatEmptyState.tsx +33 -19
  17. package/client/features/chat/messages/MarkdownContent.tsx +3 -3
  18. package/client/features/chat/messages/fork-history.ts +14 -0
  19. package/client/features/chat/sessions/ChatSelector.tsx +138 -79
  20. package/client/features/chat/sessions/api.ts +109 -19
  21. package/client/features/chat/sessions/browser-tab-state.ts +30 -7
  22. package/client/features/chat/sessions/useSelectedSession.ts +217 -51
  23. package/client/features/chat/useChat.ts +43 -45
  24. package/client/features/collab/client.ts +10 -11
  25. package/client/features/collab/hooks.tsx +6 -11
  26. package/client/features/collab/host-state.ts +57 -128
  27. package/client/features/collab/provider.tsx +3 -3
  28. package/client/features/collab/testing/fake-engine.ts +6 -27
  29. package/client/features/collab/workspace-users-menu.tsx +3 -7
  30. package/client/features/drawings/{useViewBuilderSketch.ts → useViewSketch.ts} +7 -7
  31. package/client/features/home/HomePage.tsx +2 -2
  32. package/client/features/overview/Overview.tsx +3 -6
  33. package/client/features/overview/default/ViewsWidget.tsx +5 -12
  34. package/client/features/overview/default/registry.tsx +2 -4
  35. package/client/features/views/PendingViewScreen.tsx +279 -0
  36. package/client/features/views/ViewManager.tsx +10 -10
  37. package/client/features/views/api.ts +63 -130
  38. package/client/features/views/useViewActions.ts +151 -0
  39. package/client/features/views/useViewDrafts.ts +79 -0
  40. package/client/features/views/view-presentation.ts +4 -5
  41. package/client/features/views/view-submission.ts +88 -0
  42. package/client/features/workspace/WorkspaceMenu.tsx +63 -0
  43. package/client/features/workspace/WorkspaceScreen.tsx +180 -375
  44. package/client/features/workspace/browser-tab-state.ts +16 -9
  45. package/client/features/workspace/moi-context.ts +13 -15
  46. package/client/features/workspace/tab-resolution.ts +16 -29
  47. package/client/features/workspace/useWorkspaceNavigation.ts +146 -104
  48. package/client/index.tsx +5 -1
  49. package/client/runtime/useWorkspaceEvents.ts +7 -6
  50. package/client/store/ui.ts +25 -29
  51. package/dist/chunk-1zhnhy00.js +1 -0
  52. package/dist/chunk-27501vke.js +1 -0
  53. package/dist/{chunk-jf6czgrz.js → chunk-9xtv6djt.js} +2 -2
  54. package/dist/chunk-am1x230f.js +2 -0
  55. package/dist/{chunk-nez8sdta.js → chunk-darpsw65.js} +1 -1
  56. package/dist/{chunk-97eg9mjn.js → chunk-gps4qc38.js} +1 -1
  57. package/dist/{chunk-3a48fyf7.js → chunk-hzwr8dp8.js} +4 -4
  58. package/dist/{chunk-efdyz6xp.js → chunk-j905va8r.js} +1 -1
  59. package/dist/chunk-n33w9y38.js +10 -0
  60. package/dist/chunk-r63ks44z.js +68 -0
  61. package/dist/{chunk-vfpsc75k.js → chunk-vqpkqtdr.js} +1 -1
  62. package/dist/chunk-wd46nk26.css +1 -0
  63. package/dist/chunk-xgt7vxt9.js +1 -0
  64. package/dist/{chunk-z3byfve5.js → chunk-yvzhwtfa.js} +2 -2
  65. package/dist/index.html +1 -1
  66. package/lib/ids.ts +2 -5
  67. package/lib/moi-context.ts +100 -73
  68. package/lib/navigation.ts +48 -87
  69. package/lib/session-drafts.ts +10 -0
  70. package/lib/types.ts +64 -31
  71. package/lib/view-build-directives.ts +12 -0
  72. package/lib/workspace-layout.ts +1 -1
  73. package/lib/workspace-tabs.ts +12 -7
  74. package/package.json +3 -1
  75. package/server/agent-launcher.ts +27 -0
  76. package/server/api.ts +119 -209
  77. package/server/app-config.ts +21 -2
  78. package/server/applets/build-worker.ts +2 -1
  79. package/server/applets/view-source.ts +7 -1
  80. package/server/cli-env.ts +72 -50
  81. package/server/cli.ts +101 -64
  82. package/server/control.ts +82 -63
  83. package/server/data-dir.ts +13 -0
  84. package/server/functions.ts +2 -1
  85. package/server/harness/README.md +8 -0
  86. package/server/harness/acp/adapter.ts +2 -2
  87. package/server/harness/acp/client.ts +2 -1
  88. package/server/harness/acp/discovery.ts +3 -3
  89. package/server/harness/acp/session.ts +6 -9
  90. package/server/harness/claude-code/adapter.ts +13 -8
  91. package/server/harness/claude-code/auth.ts +3 -2
  92. package/server/harness/claude-code/cli.ts +3 -1
  93. package/server/harness/claude-code/git-worktree.ts +5 -2
  94. package/server/harness/claude-code/index.ts +5 -0
  95. package/server/harness/claude-code/mcp.ts +2 -0
  96. package/server/harness/claude-code/models.ts +2 -0
  97. package/server/harness/claude-code/session.ts +5 -34
  98. package/server/harness/claude-code/sessions.ts +8 -12
  99. package/server/harness/codex/NOTES.md +23 -4
  100. package/server/harness/codex/adapter.ts +5 -3
  101. package/server/harness/codex/client.ts +13 -21
  102. package/server/harness/codex/index.ts +18 -0
  103. package/server/harness/codex/session.ts +6 -14
  104. package/server/harness/codex/thread-list.ts +74 -0
  105. package/server/harness/fork.ts +12 -0
  106. package/server/harness/openclaw/adapter.ts +3 -3
  107. package/server/harness/openclaw/session.ts +2 -30
  108. package/server/harness/session-title.ts +12 -3
  109. package/server/harness/types.ts +8 -3
  110. package/server/moi-scaffold.ts +2 -1
  111. package/server/pending-views.ts +164 -0
  112. package/server/registry.ts +14 -4
  113. package/server/selected-session.ts +91 -44
  114. package/server/service.ts +16 -2
  115. package/server/session-archive.ts +32 -0
  116. package/server/session-config.ts +16 -111
  117. package/server/session-lifecycle.ts +17 -0
  118. package/server/session-store.ts +127 -0
  119. package/server/status.ts +3 -1
  120. package/server/tabs.ts +7 -10
  121. package/server/view-sessions.ts +267 -0
  122. package/server/views.ts +30 -41
  123. package/server/web.ts +47 -45
  124. package/server/workspace-env.ts +27 -0
  125. package/server/workspace-init.ts +6 -2
  126. package/workspace/.claude/skills/moi-workspace/SKILL.md +49 -20
  127. package/workspace/.claude/skills/moi-workspace/references/COLLAB.md +2 -5
  128. package/client/features/views/ViewBuilder.tsx +0 -239
  129. package/client/features/views/useViewBuilderActions.ts +0 -103
  130. package/client/features/views/useViewBuilderDrafts.ts +0 -68
  131. package/dist/chunk-2jncyq65.js +0 -1
  132. package/dist/chunk-e2q5ken7.js +0 -68
  133. package/dist/chunk-h2kj1ysj.js +0 -1
  134. package/dist/chunk-nzwgm8vc.css +0 -1
  135. package/dist/chunk-ph0h6h86.js +0 -1
  136. package/dist/chunk-ryg9z3kn.js +0 -10
  137. package/dist/chunk-tpn177bt.js +0 -2
  138. package/lib/view-builder-directives.ts +0 -16
  139. package/server/chat-selection.ts +0 -22
  140. package/server/view-builders.ts +0 -453
@@ -1,18 +1,17 @@
1
1
  import { useRef, useState } from 'react'
2
- import { IconArchive, IconChevronDown, IconEdit } from '@tabler/icons-react'
2
+ import { IconArchive, IconEdit, IconHistory, IconPin, IconPinFilled } from '@tabler/icons-react'
3
3
 
4
4
  import { useArchiveWorkspaceSession, useWorkspaceSessions } from './api'
5
5
  import { useWorkspaceAgent } from '@/client/features/workspace/api'
6
6
  import { useWorkspaceId } from '@/client/features/workspace/WorkspaceContext'
7
- import { useSelectedSession } from '@/client/features/chat/sessions/useSelectedSession'
8
- import { cn } from '@/client/lib/cn'
9
7
  import {
10
- hasRunningBackgroundSession,
11
- isSessionRunning,
12
- liveStore,
13
- useLive
14
- } from '@/client/features/chat/chat-store'
15
- import type { SessionInfo } from '@/lib/types'
8
+ useSelectedSession,
9
+ useCurrentTabId,
10
+ usePinnedSession
11
+ } from '@/client/features/chat/sessions/useSelectedSession'
12
+ import { cn } from '@/client/lib/cn'
13
+ import { isSessionRunning, liveStore, useLive } from '@/client/features/chat/chat-store'
14
+ import type { SessionInfo, WorkspaceTabId } from '@/lib/types'
16
15
 
17
16
  import { Button } from '@/client/components/ui/button'
18
17
  import { toast } from '@/client/components/ui/toast'
@@ -23,17 +22,16 @@ import {
23
22
  DropdownMenuGroup,
24
23
  DropdownMenuItem,
25
24
  DropdownMenuLabel,
26
- DropdownMenuSeparator,
27
25
  DropdownMenuTrigger
28
26
  } from '@/client/components/ui/dropdown-menu'
29
27
  import { Spinner } from '@/client/components/ui/spinner'
30
28
 
31
29
  type ChatSelectorProps = {
32
- isViewBuilder?: boolean
30
+ className?: string
33
31
  }
34
32
 
35
- type ChatHeaderLabelProps = {
36
- label: string
33
+ type SessionSelectorProps = ChatSelectorProps & {
34
+ sessions: SessionInfo[]
37
35
  }
38
36
 
39
37
  type ChatSessionGroup = {
@@ -42,10 +40,6 @@ type ChatSessionGroup = {
42
40
  sessions: SessionInfo[]
43
41
  }
44
42
 
45
- function ChatHeaderLabel({ label }: ChatHeaderLabelProps) {
46
- return <div className="flex h-7 items-center px-2.5 text-sm font-medium">{label}</div>
47
- }
48
-
49
43
  // One tiny text badge per row at most: a cron/subagent flavor wins; otherwise
50
44
  // an external origin shows its provider lowercase (e.g. "irc", "telegram").
51
45
  // Plain app chats get none.
@@ -239,33 +233,94 @@ export function groupSessionsByDate(sessions: SessionInfo[], now = new Date()):
239
233
  return [...groups.values()]
240
234
  }
241
235
 
242
- export function ChatSelector({ isViewBuilder = false }: ChatSelectorProps) {
243
- if (isViewBuilder) {
244
- return <ChatHeaderLabel label="Build a new view" />
236
+ export function groupSessionsForTab(
237
+ sessions: SessionInfo[],
238
+ tabId: WorkspaceTabId,
239
+ now = new Date()
240
+ ): ChatSessionGroup[] {
241
+ return groupSessionsByDate(
242
+ sessions.filter(session =>
243
+ tabId === 'overview' ? !session.tabId?.startsWith('views/') : session.tabId === tabId
244
+ ),
245
+ now
246
+ )
247
+ }
248
+
249
+ export function ChatSelector({ className }: ChatSelectorProps) {
250
+ const workspaceId = useWorkspaceId()
251
+ const { pinnedSessionId, loaded, pin } = usePinnedSession()
252
+ const [selectedSessionId] = useSelectedSession()
253
+ const { data: sessions = [] } = useWorkspaceSessions(workspaceId)
254
+ if (!loaded) return null
255
+
256
+ if (pinnedSessionId) {
257
+ const title = sessions.find(session => session.sessionId === pinnedSessionId)?.summary
258
+ if (!title) return null
259
+ return (
260
+ <div
261
+ className={cn(
262
+ '-my-0.5 -ml-0.5 box-content h-7 max-w-64 rounded-lg py-0.5 pr-0.5 pl-3',
263
+ 'flex min-w-0 items-center gap-1 bg-accent text-sm font-medium'
264
+ )}
265
+ >
266
+ <span className="min-w-0 flex-1 truncate">{title}</span>
267
+ <Tooltip key="unpin">
268
+ <TooltipTrigger
269
+ render={
270
+ <Button
271
+ variant="ghost"
272
+ size="icon-sm"
273
+ className={className}
274
+ onClick={() => pin(null)}
275
+ aria-label="Unpin chat"
276
+ aria-pressed={true}
277
+ >
278
+ <IconPinFilled stroke={1.75} />
279
+ </Button>
280
+ }
281
+ />
282
+ <TooltipContent>Unpin chat</TooltipContent>
283
+ </Tooltip>
284
+ </div>
285
+ )
245
286
  }
246
287
 
247
- return <SessionSelector />
288
+ return (
289
+ <div className="flex w-full min-w-0 items-center justify-between gap-1">
290
+ <SessionSelector className={className} sessions={sessions} />
291
+ {selectedSessionId && (
292
+ <Tooltip key="pin">
293
+ <TooltipTrigger
294
+ render={
295
+ <Button
296
+ variant="ghost"
297
+ size="icon-sm"
298
+ className={className}
299
+ onClick={() => pin(selectedSessionId)}
300
+ aria-label="Pin chat to workspace"
301
+ aria-pressed={false}
302
+ >
303
+ <IconPin stroke={1.75} />
304
+ </Button>
305
+ }
306
+ />
307
+ <TooltipContent>Pin chat to workspace</TooltipContent>
308
+ </Tooltip>
309
+ )}
310
+ </div>
311
+ )
248
312
  }
249
313
 
250
- function SessionSelector() {
314
+ function SessionSelector({ className, sessions }: SessionSelectorProps) {
251
315
  const workspaceId = useWorkspaceId()
252
- const [selectedSession, selectSession] = useSelectedSession()
253
- const selectedSessionId = selectedSession ?? null
316
+ const tabId = useCurrentTabId()
317
+ const [selectedSessionId, selectSession] = useSelectedSession()
254
318
  const [confirmingSessionId, setConfirmingSessionId] = useState<string | null>(null)
255
- const { data: sessions = [] } = useWorkspaceSessions(workspaceId)
256
319
  const canArchive = useWorkspaceAgent(workspaceId).data?.supportsArchiving === true
257
320
  const archiveSession = useArchiveWorkspaceSession(workspaceId)
258
- const hasRunningBackgroundChat = useLive(state =>
259
- hasRunningBackgroundSession(state.activity, workspaceId, selectedSessionId)
260
- )
321
+ const sessionGroups = groupSessionsForTab(sessions, tabId)
261
322
 
262
- if (sessions.length === 0) {
263
- return <ChatHeaderLabel label="New chat" />
264
- }
265
-
266
- const active = sessions.find(s => s.sessionId === selectedSessionId)
267
- const label = active?.summary ?? 'New chat'
268
- const sessionGroups = groupSessionsByDate(sessions)
323
+ if (sessionGroups.length === 0 && !selectedSessionId) return null
269
324
 
270
325
  function handleMenuOpenChange(open: boolean) {
271
326
  if (!open) setConfirmingSessionId(null)
@@ -282,59 +337,63 @@ function SessionSelector() {
282
337
  }
283
338
  }
284
339
 
285
- return (
286
- <DropdownMenu onOpenChange={handleMenuOpenChange}>
287
- <DropdownMenuTrigger
340
+ return selectedSessionId ? (
341
+ <Tooltip>
342
+ <TooltipTrigger
288
343
  render={
289
- <Button className="group max-w-full min-w-0 shrink" variant="ghost" size="sm">
290
- <span className="max-w-64 min-w-0 truncate">{label}</span>
291
- {hasRunningBackgroundChat ? (
292
- <Spinner className="size-4!" stroke={2} />
293
- ) : (
294
- <IconChevronDown
295
- className="text-muted-foreground group-hover:text-foreground"
296
- stroke={1.75}
297
- />
298
- )}
344
+ <Button
345
+ variant="ghost"
346
+ size="icon-sm"
347
+ aria-label="New chat"
348
+ onClick={() => selectSession(null)}
349
+ className={className}
350
+ >
351
+ <IconEdit stroke={1.75} />
299
352
  </Button>
300
353
  }
301
354
  />
355
+ <TooltipContent>New chat</TooltipContent>
356
+ </Tooltip>
357
+ ) : (
358
+ <DropdownMenu onOpenChange={handleMenuOpenChange}>
359
+ <Tooltip>
360
+ <DropdownMenuTrigger
361
+ render={
362
+ <TooltipTrigger
363
+ render={
364
+ <Button variant="ghost" size="icon-sm" aria-label="History" className={className}>
365
+ <IconHistory stroke={1.75} />
366
+ </Button>
367
+ }
368
+ />
369
+ }
370
+ />
371
+ <TooltipContent>History</TooltipContent>
372
+ </Tooltip>
302
373
  <DropdownMenuContent
303
374
  align="start"
304
375
  className="flex max-h-100 w-max max-w-72 min-w-40 flex-col overflow-hidden"
305
376
  >
306
- <DropdownMenuItem
307
- className="shrink-0 gap-1 font-medium text-muted-foreground! **:text-muted-foreground!"
308
- onClick={() => selectSession(null)}
309
- >
310
- <IconEdit size={16} stroke={1.75} />
311
- New chat
312
- </DropdownMenuItem>
313
- {sessionGroups.length > 0 && (
314
- <>
315
- <DropdownMenuSeparator className="shrink-0" />
316
- <div className="no-scrollbar flex min-h-0 flex-1 scroll-fade flex-col gap-1 overflow-y-auto overscroll-contain [--scroll-fade-reveal:8px]">
317
- {sessionGroups.map(group => (
318
- <DropdownMenuGroup key={group.key}>
319
- <DropdownMenuLabel>{group.label}</DropdownMenuLabel>
320
- {group.sessions.map(session => (
321
- <ChatSessionItem
322
- key={session.sessionId}
323
- session={session}
324
- active={selectedSessionId === session.sessionId}
325
- canArchive={canArchive}
326
- confirmingArchive={confirmingSessionId === session.sessionId}
327
- onSelect={selectSession}
328
- onArchive={handleArchive}
329
- onRequestArchive={setConfirmingSessionId}
330
- workspaceId={workspaceId}
331
- />
332
- ))}
333
- </DropdownMenuGroup>
377
+ <div className="no-scrollbar flex min-h-0 flex-1 scroll-fade flex-col gap-1 overflow-y-auto overscroll-contain [--scroll-fade-reveal:8px]">
378
+ {sessionGroups.map(group => (
379
+ <DropdownMenuGroup key={group.key}>
380
+ <DropdownMenuLabel>{group.label}</DropdownMenuLabel>
381
+ {group.sessions.map(session => (
382
+ <ChatSessionItem
383
+ key={session.sessionId}
384
+ session={session}
385
+ active={selectedSessionId === session.sessionId}
386
+ canArchive={canArchive}
387
+ confirmingArchive={confirmingSessionId === session.sessionId}
388
+ onSelect={selectSession}
389
+ onArchive={handleArchive}
390
+ onRequestArchive={setConfirmingSessionId}
391
+ workspaceId={workspaceId}
392
+ />
334
393
  ))}
335
- </div>
336
- </>
337
- )}
394
+ </DropdownMenuGroup>
395
+ ))}
396
+ </div>
338
397
  </DropdownMenuContent>
339
398
  </DropdownMenu>
340
399
  )
@@ -1,17 +1,79 @@
1
1
  import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
2
+ import type { QueryClient } from '@tanstack/react-query'
3
+ import { useCallback } from 'react'
2
4
 
3
5
  import { jsonRequest, requestJson, requestVoid } from '@/client/api/http'
4
6
  import { WORKSPACE_RESOURCE_OPTIONS } from '@/client/api/query-options'
5
7
  import { workspaceKeys } from '@/client/api/workspace-keys'
6
8
  import { sessionViewOptions } from '@/client/features/chat/sessions/session-view'
7
- import type { SessionConfig, SessionInfo } from '@/lib/types'
9
+ import { readSelectedSession, writeSelectedSession } from './browser-tab-state'
10
+ import {
11
+ selectedSessionKey,
12
+ selectedSessionOptions,
13
+ usePinnedSession,
14
+ useSelectedSession
15
+ } from './useSelectedSession'
16
+ import { useCollabEnabled } from '@/client/features/collab'
17
+ import { useLatestRef } from '@/client/lib/use-latest-ref'
18
+ import { useWorkspaceId } from '@/client/features/workspace/WorkspaceContext'
19
+ import type {
20
+ SessionConfig,
21
+ SessionInfo,
22
+ SessionRecord,
23
+ WorkspaceSessionSelection,
24
+ WorkspaceTabId
25
+ } from '@/lib/types'
8
26
 
9
- export function useWorkspaceSessions(workspaceId: string) {
10
- return useQuery<SessionInfo[]>({
27
+ export function workspaceSessionsOptions(workspaceId: string) {
28
+ return {
11
29
  queryKey: workspaceKeys.sessions(workspaceId),
12
- queryFn: () => requestJson(`/api/workspaces/${workspaceId}/sessions`),
30
+ queryFn: () => requestJson<SessionInfo[]>(`/api/workspaces/${workspaceId}/sessions`),
13
31
  ...WORKSPACE_RESOURCE_OPTIONS
14
- })
32
+ }
33
+ }
34
+
35
+ export function useWorkspaceSessions(workspaceId: string) {
36
+ return useQuery(workspaceSessionsOptions(workspaceId))
37
+ }
38
+
39
+ // Select the linked chat on the destination tab, unpinning a different chat first.
40
+ export function useSelectLinkedChat() {
41
+ const workspaceId = useWorkspaceId()
42
+ const queryClient = useQueryClient()
43
+ const collabEnabled = useCollabEnabled()
44
+ const [, , selectSessionForTab] = useSelectedSession()
45
+ const { pinAsync } = usePinnedSession()
46
+ const stateRef = useLatestRef({ collabEnabled, selectSessionForTab, pinAsync })
47
+ // Stable so selection updates do not restart a pending chat link.
48
+ return useCallback(
49
+ async (sessionId: string, tab: WorkspaceTabId, signal: AbortSignal): Promise<boolean> => {
50
+ const { collabEnabled, selectSessionForTab, pinAsync } = stateRef.current
51
+ signal.throwIfAborted()
52
+ let sessions =
53
+ queryClient.getQueryData<SessionInfo[]>(workspaceKeys.sessions(workspaceId)) ?? []
54
+ if (!sessions.some(session => session.sessionId === sessionId))
55
+ sessions = await queryClient.fetchQuery(workspaceSessionsOptions(workspaceId))
56
+ 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'))
61
+ signal.throwIfAborted()
62
+ const pinned = queryClient.getQueryData<WorkspaceSessionSelection>(
63
+ selectedSessionKey(workspaceId)
64
+ )?.pinned
65
+ if (pinned && pinned !== sessionId) {
66
+ await pinAsync(null)
67
+ signal.throwIfAborted()
68
+ }
69
+ if (pinned !== sessionId) {
70
+ await selectSessionForTab(sessionId, tab)
71
+ signal.throwIfAborted()
72
+ }
73
+ return true
74
+ },
75
+ [queryClient, stateRef, workspaceId]
76
+ )
15
77
  }
16
78
 
17
79
  export function removeArchivedSession(
@@ -21,6 +83,41 @@ export function removeArchivedSession(
21
83
  return sessions?.filter(session => session.sessionId !== sessionId)
22
84
  }
23
85
 
86
+ export function applyArchivedSession(
87
+ queryClient: QueryClient,
88
+ workspaceId: string,
89
+ sessionId: string
90
+ ) {
91
+ const sessionsKey = workspaceKeys.sessions(workspaceId)
92
+ void queryClient.cancelQueries({ queryKey: sessionsKey })
93
+ queryClient.setQueryData<SessionInfo[]>(sessionsKey, current =>
94
+ removeArchivedSession(current, sessionId)
95
+ )
96
+ 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 })
119
+ }
120
+
24
121
  export function useArchiveWorkspaceSession(workspaceId: string) {
25
122
  const queryClient = useQueryClient()
26
123
  return useMutation<void, Error, string>({
@@ -31,11 +128,7 @@ export function useArchiveWorkspaceSession(workspaceId: string) {
31
128
  'Couldn’t archive chat'
32
129
  ),
33
130
  onSuccess: (_, sessionId) => {
34
- queryClient.setQueryData<SessionInfo[]>(workspaceKeys.sessions(workspaceId), current =>
35
- removeArchivedSession(current, sessionId)
36
- )
37
- queryClient.removeQueries({ queryKey: workspaceKeys.events(workspaceId, sessionId) })
38
- queryClient.removeQueries({ queryKey: workspaceKeys.sessionConfig(workspaceId, sessionId) })
131
+ applyArchivedSession(queryClient, workspaceId, sessionId)
39
132
  queryClient.invalidateQueries({ queryKey: workspaceKeys.preview(workspaceId) })
40
133
  }
41
134
  })
@@ -48,10 +141,10 @@ export function useSessionView(workspaceId: string, sessionId: string | null) {
48
141
  })
49
142
  }
50
143
 
51
- export function useSessionConfig(workspaceId: string, sessionId: string | null) {
52
- return useQuery<SessionConfig>({
53
- queryKey: workspaceKeys.sessionConfig(workspaceId, sessionId ?? ''),
54
- queryFn: () => requestJson(`/api/workspaces/${workspaceId}/sessions/${sessionId}/config`),
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}`),
55
148
  enabled: Boolean(sessionId),
56
149
  staleTime: Infinity,
57
150
  gcTime: 5 * 60_000,
@@ -62,7 +155,7 @@ export function useSessionConfig(workspaceId: string, sessionId: string | null)
62
155
 
63
156
  export function useSaveSessionConfig(workspaceId: string) {
64
157
  const queryClient = useQueryClient()
65
- return useMutation<SessionConfig, Error, { sessionId: string; patch: SessionConfig }>({
158
+ return useMutation<SessionRecord, Error, { sessionId: string; patch: SessionConfig }>({
66
159
  mutationFn: ({ patch, sessionId }) =>
67
160
  requestJson(
68
161
  `/api/workspaces/${workspaceId}/sessions/${sessionId}/config`,
@@ -70,10 +163,7 @@ export function useSaveSessionConfig(workspaceId: string) {
70
163
  'Failed to save chat settings'
71
164
  ),
72
165
  onSuccess: (next, { sessionId }) => {
73
- queryClient.setQueryData<SessionConfig>(
74
- workspaceKeys.sessionConfig(workspaceId, sessionId),
75
- next
76
- )
166
+ queryClient.setQueryData<SessionRecord>(workspaceKeys.session(workspaceId, sessionId), next)
77
167
  }
78
168
  })
79
169
  }
@@ -1,18 +1,41 @@
1
+ import type { WorkspaceSessionSelection } from '@/lib/types'
2
+
1
3
  const keyFor = (workspaceId: string) => `moi:collab:${workspaceId}:session`
4
+ const emptySelection = (): WorkspaceSessionSelection => ({ selected: {}, pinned: null })
2
5
 
3
- export function readSelectedSession(workspaceId: string): string | null {
6
+ // Collab keeps each browser tab's unpinned chat choice, including a separate
7
+ // selection for every workspace tab. The workspace pin remains server-owned.
8
+ export function readSelectedSession(workspaceId: string): WorkspaceSessionSelection {
4
9
  try {
5
- return sessionStorage.getItem(keyFor(workspaceId))
10
+ const saved = sessionStorage.getItem(keyFor(workspaceId))
11
+ if (!saved) return emptySelection()
12
+ // The first Collab build stored only the overview chat ID.
13
+ if (!saved.startsWith('{')) return { selected: { overview: saved }, pinned: null }
14
+ const value: unknown = JSON.parse(saved)
15
+ if (!value || typeof value !== 'object' || !('selected' in value)) return emptySelection()
16
+ const selected = (value as { selected: unknown }).selected
17
+ if (!selected || typeof selected !== 'object' || Array.isArray(selected))
18
+ return emptySelection()
19
+ return {
20
+ selected: Object.fromEntries(
21
+ Object.entries(selected).filter(
22
+ ([, sessionId]) => typeof sessionId === 'string' || sessionId === null
23
+ )
24
+ ),
25
+ pinned: null
26
+ }
6
27
  } catch {
7
- return null
28
+ return emptySelection()
8
29
  }
9
30
  }
10
31
 
11
- export function writeSelectedSession(workspaceId: string, sessionId: string | null): void {
32
+ export function writeSelectedSession(
33
+ workspaceId: string,
34
+ selection: WorkspaceSessionSelection
35
+ ): void {
12
36
  try {
13
- if (sessionId === null) sessionStorage.removeItem(keyFor(workspaceId))
14
- else sessionStorage.setItem(keyFor(workspaceId), sessionId)
37
+ sessionStorage.setItem(keyFor(workspaceId), JSON.stringify({ selected: selection.selected }))
15
38
  } catch {
16
- /* A private browser can still keep the active query in memory. */
39
+ // The in-memory query still works when browser storage is denied.
17
40
  }
18
41
  }