moi-computer 0.13.1-next.2 → 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.
- package/client/features/chat/sessions/use-selected-session.ts +0 -13
- package/client/features/views/api.ts +33 -8
- package/client/features/views/use-view-actions.ts +2 -23
- package/client/features/workspace/tab-resolution.ts +1 -8
- package/client/features/workspace/use-workspace-navigation.ts +4 -3
- package/client/features/workspace/workspace-content.tsx +2 -11
- package/client/features/workspace/workspace-preferences.ts +67 -10
- package/client/features/workspace/workspace-state.ts +40 -15
- package/client/store/ui.ts +1 -6
- package/client/store/workspace-session.ts +30 -0
- package/dist/chunk-0h4zypgf.js +1 -0
- package/dist/chunk-1tksq43z.js +72 -0
- package/dist/chunk-6gxf0xr8.js +5 -0
- package/dist/{chunk-ec5ennpv.js → chunk-a865c7x4.js} +1 -1
- package/dist/{chunk-pdt9hxfw.js → chunk-f44cs6mw.js} +2 -2
- package/dist/{chunk-44khk4sp.js → chunk-h3qag4a6.js} +2 -2
- package/dist/{chunk-ectsnw81.js → chunk-q2tpy0h7.js} +2 -2
- package/dist/{chunk-rmd0ctfx.js → chunk-r95acex1.js} +74 -74
- package/dist/chunk-w5h9grdr.js +1 -0
- package/dist/{chunk-n0fawy9x.js → chunk-xj1pjhng.js} +1 -1
- package/dist/index.html +1 -1
- package/lib/moi-context.ts +19 -5
- package/lib/types.ts +4 -5
- package/lib/workspace-layout.ts +3 -20
- package/package.json +2 -1
- package/server/AGENTS.md +1 -1
- package/server/README.md +21 -18
- package/server/api/index.ts +122 -118
- package/server/cli/commands/init.ts +7 -2
- package/server/cli/commands/remote.ts +128 -26
- package/server/cli/remote-control.ts +12 -9
- package/server/cli/remote-operations.ts +47 -44
- package/server/control.ts +9 -1
- package/server/onboarding/jobs.ts +1 -1
- package/server/remote/broker-client.ts +76 -0
- package/server/remote/broker.ts +81 -0
- package/server/remote/client.ts +212 -152
- package/server/remote/errors.ts +2 -0
- package/server/remote/routes.ts +121 -0
- package/server/scratchpad/document.ts +3 -2
- package/server/sessions/pending-views.ts +2 -1
- package/server/sessions/send-message.ts +102 -0
- package/server/sessions/view-sessions.ts +4 -94
- package/server/web.ts +2 -1
- package/server/workspace-state/file.ts +2 -0
- package/server/workspaces/api.ts +4 -8
- package/server/workspaces/tabs.ts +1 -2
- package/skill/SKILL.md +75 -34
- package/skill/references/SCRATCHPAD.md +4 -0
- package/skill/references/UI-COMPONENTS.md +1 -1
- package/dist/chunk-ba6mz56s.js +0 -72
- package/dist/chunk-en7w5ypg.js +0 -1
- package/dist/chunk-f695wcx5.js +0 -5
- package/dist/chunk-zv22wv0g.js +0 -1
|
@@ -70,19 +70,6 @@ function selectionChange(
|
|
|
70
70
|
return { sessionId, tabId, show: true, ifMatch: current }
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
// Show a chat in a tab before the server confirms it, without a request: for
|
|
74
|
-
// flows whose own request makes the server select it (view submission).
|
|
75
|
-
export function setSelectedSessionInCache(
|
|
76
|
-
queryClient: QueryClient,
|
|
77
|
-
workspaceId: string,
|
|
78
|
-
sessionId: string,
|
|
79
|
-
tabId: WorkspaceTabId
|
|
80
|
-
): void {
|
|
81
|
-
changeWorkspaceStateCache(queryClient, workspaceId, draft => {
|
|
82
|
-
draft.selected[tabId] = sessionId
|
|
83
|
-
})
|
|
84
|
-
}
|
|
85
|
-
|
|
86
73
|
// A harness replaced a temporary chat id with its real one.
|
|
87
74
|
export function renameSelectedSessionInCache(
|
|
88
75
|
queryClient: QueryClient,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { viewTabId } from '@/lib/workspace-tabs'
|
|
1
2
|
import { useWorkspaceLayoutCtx } from '@/client/features/workspace/workspace-layout-context'
|
|
2
3
|
import type { SpeedMode } from '@/lib/types'
|
|
3
4
|
import type {
|
|
@@ -13,7 +14,8 @@ import { jsonRequest, requestJson, requestVoid } from '@/client/api/http'
|
|
|
13
14
|
import { workspaceKeys } from '@/client/api/workspace-keys'
|
|
14
15
|
import {
|
|
15
16
|
getCachedWorkspaceState,
|
|
16
|
-
useWorkspaceState
|
|
17
|
+
useWorkspaceState,
|
|
18
|
+
useWorkspaceStateWrite
|
|
17
19
|
} from '@/client/features/workspace/workspace-state'
|
|
18
20
|
import { useWorkspaceEvent } from '@/client/runtime/use-workspace-events'
|
|
19
21
|
|
|
@@ -153,12 +155,35 @@ export type SubmitViewInput = {
|
|
|
153
155
|
}
|
|
154
156
|
export function useSubmitView(workspaceId: string) {
|
|
155
157
|
const { local } = useViewBase(workspaceId)
|
|
156
|
-
return
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
158
|
+
return useWorkspaceStateWrite<
|
|
159
|
+
SubmitViewInput,
|
|
160
|
+
{
|
|
161
|
+
viewId: string
|
|
162
|
+
sessionId: string
|
|
163
|
+
state: WorkspaceState
|
|
164
|
+
}
|
|
165
|
+
>(workspaceId, {
|
|
166
|
+
request: ({ viewId, ...input }) =>
|
|
167
|
+
requestJson(
|
|
168
|
+
`${local}/views/${viewId}/submit`,
|
|
169
|
+
jsonRequest('POST', input),
|
|
170
|
+
'Couldn’t start view chat'
|
|
171
|
+
),
|
|
172
|
+
optimistic: (draft, { viewId, sessionId, requirements }) => {
|
|
173
|
+
const tabId = viewTabId(viewId)
|
|
174
|
+
if (!draft.pinnedSessionId) {
|
|
175
|
+
draft.selected[tabId] = sessionId
|
|
176
|
+
draft.sessions[sessionId] = { ...draft.sessions[sessionId], tabId }
|
|
177
|
+
}
|
|
178
|
+
const view = draft.pendingViews[viewId]
|
|
179
|
+
if (view?.status === 'draft')
|
|
180
|
+
Object.assign(view, {
|
|
181
|
+
status: 'starting',
|
|
182
|
+
requirements,
|
|
183
|
+
buildSessionId: draft.pinnedSessionId ?? sessionId
|
|
184
|
+
})
|
|
185
|
+
},
|
|
186
|
+
errorTitle: 'Couldn’t start view chat'
|
|
187
|
+
})
|
|
163
188
|
}
|
|
164
189
|
export const useDiscardView = useDeleteView
|
|
@@ -12,13 +12,9 @@ import {
|
|
|
12
12
|
} from '@/client/features/chat/chat-send'
|
|
13
13
|
import {
|
|
14
14
|
renameSelectedSessionInCache,
|
|
15
|
-
setSelectedSessionInCache,
|
|
16
15
|
usePinnedSession
|
|
17
16
|
} from '@/client/features/chat/sessions/use-selected-session'
|
|
18
|
-
import {
|
|
19
|
-
changeWorkspaceStateCache,
|
|
20
|
-
getCachedWorkspaceState
|
|
21
|
-
} from '@/client/features/workspace/workspace-state'
|
|
17
|
+
import { getCachedWorkspaceState } from '@/client/features/workspace/workspace-state'
|
|
22
18
|
import { useWorkspaceHarness } from '@/client/features/workspace/api'
|
|
23
19
|
import { useWorkspaceLayoutCtx } from '@/client/features/workspace/workspace-layout-context'
|
|
24
20
|
import {
|
|
@@ -55,17 +51,13 @@ export function useViewActions() {
|
|
|
55
51
|
|
|
56
52
|
if (!pinnedSessionId)
|
|
57
53
|
startOptimisticSession({
|
|
58
|
-
tabId: viewTabId(pendingView.id),
|
|
59
54
|
queryClient,
|
|
60
55
|
workspaceId,
|
|
61
56
|
sessionId,
|
|
62
57
|
text,
|
|
63
58
|
filenames: attachments.map(attachment => attachment.label)
|
|
64
59
|
})
|
|
65
|
-
if (!pinnedSessionId)
|
|
66
|
-
moveChatDraft(workspaceId, draftId, sessionId)
|
|
67
|
-
setSelectedSessionInCache(queryClient, workspaceId, sessionId, viewTabId(pendingView.id))
|
|
68
|
-
}
|
|
60
|
+
if (!pinnedSessionId) moveChatDraft(workspaceId, draftId, sessionId)
|
|
69
61
|
const prepared = prepareOptimisticSend(
|
|
70
62
|
{ queryClient, workspaceId, sessionId, text },
|
|
71
63
|
attachments
|
|
@@ -81,12 +73,6 @@ export function useViewActions() {
|
|
|
81
73
|
config?.speedMode ?? defaults?.speedMode
|
|
82
74
|
)
|
|
83
75
|
|
|
84
|
-
changeWorkspaceStateCache(queryClient, workspaceId, draft => {
|
|
85
|
-
const view = draft.pendingViews[pendingView.id]
|
|
86
|
-
if (view?.status === 'draft')
|
|
87
|
-
Object.assign(view, { status: 'starting', requirements: text, buildSessionId: sessionId })
|
|
88
|
-
})
|
|
89
|
-
|
|
90
76
|
try {
|
|
91
77
|
const result = await submitMutation.mutateAsync({
|
|
92
78
|
viewId: pendingView.id,
|
|
@@ -105,13 +91,6 @@ export function useViewActions() {
|
|
|
105
91
|
})
|
|
106
92
|
moveChatDraft(workspaceId, sessionId, result.sessionId)
|
|
107
93
|
renameSelectedSessionInCache(queryClient, workspaceId, sessionId, result.sessionId)
|
|
108
|
-
if (!pinnedSessionId)
|
|
109
|
-
setSelectedSessionInCache(
|
|
110
|
-
queryClient,
|
|
111
|
-
workspaceId,
|
|
112
|
-
result.sessionId,
|
|
113
|
-
viewTabId(pendingView.id)
|
|
114
|
-
)
|
|
115
94
|
finishComposerSend(workspaceId, attachments)
|
|
116
95
|
const draftKey = composerDraftKey(workspaceId, result.sessionId)
|
|
117
96
|
const ui = useUiStore.getState()
|
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
// Pure tab-state derivation for the workspace screen. The URL is the live
|
|
2
|
-
// address for the active tab (`/workspace/:id/views/<id>`, etc.); personal state
|
|
3
|
-
// keeps the open set and active tab in sync across windows. These helpers
|
|
4
|
-
// turn (URL segment + layout + what actually exists) into the rendered state.
|
|
5
1
|
import type { ViewInfo, WorkspaceTabId, WorkspaceTabsState } from '@/lib/types'
|
|
6
2
|
import { viewIdFromTab } from '@/lib/workspace-tabs'
|
|
7
|
-
import { createDefaultWorkspaceTabs } from '@/lib/workspace-layout'
|
|
8
|
-
|
|
9
|
-
const DEFAULT_TABS = createDefaultWorkspaceTabs()
|
|
10
3
|
|
|
11
4
|
// Whether a tab id points at something that exists: static tabs always do,
|
|
12
5
|
// view tabs only while their pending or compiled view exists.
|
|
@@ -20,7 +13,7 @@ export function tabAvailable(tab: WorkspaceTabId, views: ViewInfo[]) {
|
|
|
20
13
|
// back to the defaults when nothing survives.
|
|
21
14
|
export function effectiveOpenTabs(tabs: WorkspaceTabsState, views: ViewInfo[]): WorkspaceTabId[] {
|
|
22
15
|
const open = tabs.open.filter(tab => tabAvailable(tab, views))
|
|
23
|
-
return open.length > 0 ? open :
|
|
16
|
+
return open.length > 0 ? open : ['overview']
|
|
24
17
|
}
|
|
25
18
|
|
|
26
19
|
// An explicit URL keeps its destination, including a missing view so the host
|
|
@@ -46,7 +46,7 @@ type UseWorkspaceNavigationOptions = {
|
|
|
46
46
|
export function useWorkspaceNavigation({ views, onOpenChat }: UseWorkspaceNavigationOptions) {
|
|
47
47
|
const queryClient = useQueryClient()
|
|
48
48
|
const { workspaceId, apiBase } = useWorkspaceLayoutCtx()
|
|
49
|
-
const [tabsState, saveTabs] = useWorkspaceTabs()
|
|
49
|
+
const [tabsState, saveTabs, saveActiveTab] = useWorkspaceTabs(views)
|
|
50
50
|
const latest = useRef({ views, tabsState, onOpenChat })
|
|
51
51
|
useLayoutEffect(() => {
|
|
52
52
|
latest.current = { views, tabsState, onOpenChat }
|
|
@@ -219,8 +219,9 @@ export function useWorkspaceNavigation({ views, onOpenChat }: UseWorkspaceNaviga
|
|
|
219
219
|
remembered.set(activeTab, `moi:/${activeTab}${search}`)
|
|
220
220
|
const current = latest.current.tabsState
|
|
221
221
|
const open = current.open.includes(activeTab) ? current.open : [...current.open, activeTab]
|
|
222
|
-
if (open !== current.open
|
|
223
|
-
|
|
222
|
+
if (open !== current.open) setTabs({ open, active: activeTab })
|
|
223
|
+
else if (current.active !== activeTab) saveActiveTab(activeTab)
|
|
224
|
+
}, [activeTab, isAvailableTab, latest, remembered, search, setTabs, saveActiveTab])
|
|
224
225
|
|
|
225
226
|
// React bubbling includes applet/chat portals. Native modified clicks and
|
|
226
227
|
// downloads keep a real href, so the browser can handle them normally.
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useFloatingChatOpen } from './workspace-preferences'
|
|
1
2
|
import { lazy, Suspense, useCallback, useEffect, useRef, useState } from 'react'
|
|
2
3
|
|
|
3
4
|
import { AnimatePresence } from 'motion/react'
|
|
@@ -126,7 +127,7 @@ export function WorkspaceContent({ widgets, views: allViews }: WorkspaceContentP
|
|
|
126
127
|
const frame = useWorkspaceFrame()
|
|
127
128
|
const { mode, canUseSplit, setMode } = frame
|
|
128
129
|
const [widgetMode, setWidgetMode] = useState<WidgetMode>('idle')
|
|
129
|
-
const [floatingChatOpen, setFloatingChatOpen] =
|
|
130
|
+
const [floatingChatOpen, setFloatingChatOpen] = useFloatingChatOpen()
|
|
130
131
|
const [chatFocusRequest, setChatFocusRequest] = useState(0)
|
|
131
132
|
const pendingViewRefs = useRef(new Map<string, PendingViewHandle>())
|
|
132
133
|
const sessionActivity = useLive(state => state.activity)
|
|
@@ -238,16 +239,6 @@ export function WorkspaceContent({ widgets, views: allViews }: WorkspaceContentP
|
|
|
238
239
|
openPopup: () => setFloatingChatOpen(true)
|
|
239
240
|
})
|
|
240
241
|
|
|
241
|
-
useEffect(() => {
|
|
242
|
-
const open = tabsState.open.filter(tab => tabAvailable(tab, allViews))
|
|
243
|
-
if (open.length === tabsState.open.length) return
|
|
244
|
-
const nextOpen = effectiveOpenTabs(tabsState, allViews)
|
|
245
|
-
setTabs({
|
|
246
|
-
open: nextOpen,
|
|
247
|
-
active: nextOpen.includes(tabsState.active) ? tabsState.active : nextOpen[0]
|
|
248
|
-
})
|
|
249
|
-
}, [allViews, setTabs, tabsState])
|
|
250
|
-
|
|
251
242
|
useEffect(() => {
|
|
252
243
|
if (!activeDraftViewId) return
|
|
253
244
|
if (mode === 'fullscreen') setFloatingChatOpen(true)
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
import { useCallback, useMemo } from 'react'
|
|
2
|
+
import { useQueryClient } from '@tanstack/react-query'
|
|
3
|
+
import { jsonRequest, requestJson } from '@/client/api/http'
|
|
2
4
|
import { useUiStore, defaultWorkspaceUi, type WorkspaceUi } from '@/client/store/ui'
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
+
import { useWorkspaceSessionStore } from '@/client/store/workspace-session'
|
|
6
|
+
import { normalizeOpenTabs } from '@/lib/workspace-layout'
|
|
7
|
+
import type { ViewInfo, WorkspaceState, WorkspaceTabId, WorkspaceTabsState } from '@/lib/types'
|
|
5
8
|
import { useWorkspaceLayoutCtx } from './workspace-layout-context'
|
|
9
|
+
import {
|
|
10
|
+
getCachedWorkspaceState,
|
|
11
|
+
useWorkspaceState,
|
|
12
|
+
useWorkspaceStateWrite
|
|
13
|
+
} from './workspace-state'
|
|
14
|
+
import { tabAvailable } from './tab-resolution'
|
|
15
|
+
import { viewTabId } from '@/lib/workspace-tabs'
|
|
6
16
|
|
|
7
17
|
// Per-browser preferences for one workspace, local or remote. See client/store/ui.ts.
|
|
8
18
|
export function useWorkspaceUi() {
|
|
@@ -18,17 +28,64 @@ export function useWorkspaceUi() {
|
|
|
18
28
|
return [ui, save] as const
|
|
19
29
|
}
|
|
20
30
|
|
|
21
|
-
export function
|
|
31
|
+
export function useFloatingChatOpen() {
|
|
22
32
|
const { workspaceId } = useWorkspaceLayoutCtx()
|
|
23
|
-
const
|
|
24
|
-
const
|
|
33
|
+
const open = useWorkspaceSessionStore(state => state.workspaces[workspaceId]?.chatOpen ?? false)
|
|
34
|
+
const save = useWorkspaceSessionStore(state => state.setWorkspaceSession)
|
|
35
|
+
const setOpen = useCallback(
|
|
36
|
+
(chatOpen: boolean) => save(workspaceId, { chatOpen }),
|
|
37
|
+
[workspaceId, save]
|
|
38
|
+
)
|
|
39
|
+
return [open, setOpen] as const
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function sameTabs(a: WorkspaceTabId[] | undefined, b: WorkspaceTabId[]) {
|
|
43
|
+
return a?.length === b.length && a.every((tab, index) => tab === b[index])
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Requires loaded personal state and views.
|
|
47
|
+
export function useWorkspaceTabs(views: ViewInfo[]) {
|
|
48
|
+
const { workspaceId } = useWorkspaceLayoutCtx()
|
|
49
|
+
const queryClient = useQueryClient()
|
|
50
|
+
const { data } = useWorkspaceState(workspaceId)
|
|
51
|
+
const active = useWorkspaceSessionStore(
|
|
52
|
+
state => state.workspaces[workspaceId]?.activeTab ?? 'overview'
|
|
53
|
+
)
|
|
54
|
+
const saveSession = useWorkspaceSessionStore(state => state.setWorkspaceSession)
|
|
55
|
+
const setActiveTab = useCallback(
|
|
56
|
+
(activeTab: WorkspaceTabId) => saveSession(workspaceId, { activeTab }),
|
|
57
|
+
[workspaceId, saveSession]
|
|
58
|
+
)
|
|
59
|
+
const { mutate } = useWorkspaceStateWrite<WorkspaceTabId[]>(workspaceId, {
|
|
60
|
+
request: openTabs =>
|
|
61
|
+
requestJson<WorkspaceState>(
|
|
62
|
+
`/api/workspaces/${workspaceId}/state/tabs`,
|
|
63
|
+
jsonRequest('PUT', { openTabs })
|
|
64
|
+
),
|
|
65
|
+
optimistic: (draft, openTabs) => {
|
|
66
|
+
draft.openTabs = openTabs
|
|
67
|
+
},
|
|
68
|
+
errorTitle: 'Couldn’t save workspace tabs'
|
|
69
|
+
})
|
|
70
|
+
// Deleted views leave the saved list on the next tab change.
|
|
71
|
+
const open = useMemo(() => {
|
|
72
|
+
const all: WorkspaceTabId[] = ['scratchpad', ...views.map(view => viewTabId(view.id))]
|
|
73
|
+
return [
|
|
74
|
+
'overview',
|
|
75
|
+
...normalizeOpenTabs(data?.openTabs ?? all).filter(tab => tabAvailable(tab, views))
|
|
76
|
+
] satisfies WorkspaceTabId[]
|
|
77
|
+
}, [data?.openTabs, views])
|
|
78
|
+
const tabs = useMemo(() => ({ open, active }), [open, active])
|
|
79
|
+
|
|
25
80
|
const setTabs = useCallback(
|
|
26
81
|
(tabs: WorkspaceTabsState) => {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
82
|
+
const openTabs = normalizeOpenTabs(tabs.open)
|
|
83
|
+
setActiveTab(tabs.active)
|
|
84
|
+
if (!sameTabs(getCachedWorkspaceState(queryClient, workspaceId)?.openTabs, openTabs))
|
|
85
|
+
mutate(openTabs)
|
|
86
|
+
return tabs
|
|
30
87
|
},
|
|
31
|
-
[workspaceId,
|
|
88
|
+
[workspaceId, setActiveTab, queryClient, mutate]
|
|
32
89
|
)
|
|
33
|
-
return [tabs, setTabs] as const
|
|
90
|
+
return [tabs, setTabs, setActiveTab] as const
|
|
34
91
|
}
|
|
@@ -24,6 +24,15 @@ function writesInFlight(queryClient: QueryClient, workspaceId: string): number {
|
|
|
24
24
|
return queryClient.isMutating({ mutationKey: writeKey(workspaceId) })
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
// The newest server update received while an optimistic write is visible.
|
|
28
|
+
const deferredStates = new WeakMap<QueryClient, Map<string, WorkspaceState>>()
|
|
29
|
+
|
|
30
|
+
function deferWorkspaceState(client: QueryClient, workspaceId: string, state: WorkspaceState) {
|
|
31
|
+
let states = deferredStates.get(client)
|
|
32
|
+
if (!states) deferredStates.set(client, (states = new Map()))
|
|
33
|
+
states.set(workspaceId, newer(states.get(workspaceId), state))
|
|
34
|
+
}
|
|
35
|
+
|
|
27
36
|
// Keep the newer copy. A reply or event can arrive after a newer one.
|
|
28
37
|
function newer(current: WorkspaceState | undefined, next: WorkspaceState): WorkspaceState {
|
|
29
38
|
return current && current.revision > next.revision ? current : next
|
|
@@ -40,7 +49,10 @@ export function workspaceStateOptions(queryClient: QueryClient, workspaceId: str
|
|
|
40
49
|
const current = queryClient.getQueryData<WorkspaceState>(queryKey)
|
|
41
50
|
// A write in flight already shows its change; its reply brings the
|
|
42
51
|
// server's copy.
|
|
43
|
-
if (current && writesInFlight(queryClient, workspaceId) > 0)
|
|
52
|
+
if (current && writesInFlight(queryClient, workspaceId) > 0) {
|
|
53
|
+
deferWorkspaceState(queryClient, workspaceId, next)
|
|
54
|
+
return current
|
|
55
|
+
}
|
|
44
56
|
return newer(current, next)
|
|
45
57
|
},
|
|
46
58
|
// Events keep the cache current, so new observers never refetch. A focus
|
|
@@ -87,27 +99,33 @@ export function receiveWorkspaceState(
|
|
|
87
99
|
workspaceId: string,
|
|
88
100
|
next: WorkspaceState
|
|
89
101
|
): void {
|
|
102
|
+
if (writesInFlight(queryClient, workspaceId) > 0) {
|
|
103
|
+
deferWorkspaceState(queryClient, workspaceId, next)
|
|
104
|
+
return
|
|
105
|
+
}
|
|
90
106
|
queryClient.setQueryData<WorkspaceState>(workspaceStateKey(workspaceId), current =>
|
|
91
107
|
newer(current, next)
|
|
92
108
|
)
|
|
93
109
|
}
|
|
94
110
|
|
|
95
|
-
type
|
|
96
|
-
|
|
111
|
+
type StateReply = WorkspaceState | { state: WorkspaceState }
|
|
112
|
+
|
|
113
|
+
type WorkspaceStateWriteOptions<Input, Result extends StateReply> = {
|
|
114
|
+
request: (input: Input) => Promise<Result>
|
|
97
115
|
optimistic?: (draft: WorkspaceState, input: Input) => void
|
|
98
|
-
onSuccess?: (state:
|
|
116
|
+
onSuccess?: (state: Result, input: Input) => void
|
|
99
117
|
errorTitle: string
|
|
100
118
|
}
|
|
101
119
|
|
|
102
120
|
// A write that answers with the new state. Writes to one workspace run in
|
|
103
|
-
// order; the
|
|
104
|
-
export function useWorkspaceStateWrite<Input>(
|
|
121
|
+
// order; the newest reply or event becomes the cache, and a failure refetches it.
|
|
122
|
+
export function useWorkspaceStateWrite<Input, Result extends StateReply = WorkspaceState>(
|
|
105
123
|
workspaceId: string,
|
|
106
|
-
{ request, optimistic, onSuccess, errorTitle }: WorkspaceStateWriteOptions<Input>
|
|
124
|
+
{ request, optimistic, onSuccess, errorTitle }: WorkspaceStateWriteOptions<Input, Result>
|
|
107
125
|
) {
|
|
108
126
|
const queryClient = useQueryClient()
|
|
109
127
|
const queryKey = workspaceStateKey(workspaceId)
|
|
110
|
-
return useMutation<
|
|
128
|
+
return useMutation<Result, Error, Input>({
|
|
111
129
|
mutationKey: writeKey(workspaceId),
|
|
112
130
|
scope: { id: `workspace-state:${workspaceId}` },
|
|
113
131
|
mutationFn: request,
|
|
@@ -116,15 +134,23 @@ export function useWorkspaceStateWrite<Input>(
|
|
|
116
134
|
await queryClient.cancelQueries({ queryKey, exact: true })
|
|
117
135
|
changeWorkspaceStateCache(queryClient, workspaceId, draft => optimistic(draft, input))
|
|
118
136
|
},
|
|
119
|
-
onSuccess: (
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
receiveWorkspaceState(queryClient, workspaceId, state)
|
|
123
|
-
onSuccess?.(state, input)
|
|
137
|
+
onSuccess: (result, input) => {
|
|
138
|
+
receiveWorkspaceState(queryClient, workspaceId, 'state' in result ? result.state : result)
|
|
139
|
+
onSuccess?.(result, input)
|
|
124
140
|
},
|
|
125
141
|
onError: error => {
|
|
126
142
|
toast.add({ title: errorTitle, description: error.message, type: 'error' })
|
|
127
|
-
|
|
143
|
+
},
|
|
144
|
+
onSettled: (_result, error) => {
|
|
145
|
+
// This mutation still counts as pending until onSettled returns.
|
|
146
|
+
if (writesInFlight(queryClient, workspaceId) === 1) {
|
|
147
|
+
const states = deferredStates.get(queryClient)
|
|
148
|
+
const latest = states?.get(workspaceId)
|
|
149
|
+
states?.delete(workspaceId)
|
|
150
|
+
if (latest)
|
|
151
|
+
queryClient.setQueryData<WorkspaceState>(queryKey, current => newer(current, latest))
|
|
152
|
+
}
|
|
153
|
+
if (error) void queryClient.invalidateQueries({ queryKey, exact: true })
|
|
128
154
|
}
|
|
129
155
|
})
|
|
130
156
|
}
|
|
@@ -136,7 +162,6 @@ export function useWorkspaceStateSync(): void {
|
|
|
136
162
|
useWorkspaceEvent(event => {
|
|
137
163
|
if (event.type !== 'state:changed') return
|
|
138
164
|
if (!getCachedWorkspaceState(queryClient, event.workspaceId)) return
|
|
139
|
-
if (writesInFlight(queryClient, event.workspaceId) > 0) return
|
|
140
165
|
receiveWorkspaceState(queryClient, event.workspaceId, event.state)
|
|
141
166
|
})
|
|
142
167
|
}
|
package/client/store/ui.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { LayoutMode
|
|
2
|
-
import { createDefaultWorkspaceTabs } from '@/lib/workspace-layout'
|
|
1
|
+
import type { LayoutMode } from '@/lib/types'
|
|
3
2
|
import { create } from 'zustand'
|
|
4
3
|
import { createJSONStorage, persist } from 'zustand/middleware'
|
|
5
4
|
import type { StateStorage } from 'zustand/middleware'
|
|
@@ -7,16 +6,12 @@ import type { StateStorage } from 'zustand/middleware'
|
|
|
7
6
|
// Per-browser UI preferences and onboarding markers, persisted to localStorage
|
|
8
7
|
// so they survive reloads. Server-backed per-user app state belongs in DATA_DIR.
|
|
9
8
|
export type WorkspaceUi = {
|
|
10
|
-
// Open tabs and the tab a bare `/workspace/:id` lands on. Each window follows
|
|
11
|
-
// its own URL; the last window to change tabs decides what is saved.
|
|
12
|
-
tabs: WorkspaceTabsState
|
|
13
9
|
layoutMode: LayoutMode
|
|
14
10
|
dockedChatWidth: number
|
|
15
11
|
popupChatWidth: number
|
|
16
12
|
}
|
|
17
13
|
|
|
18
14
|
export const defaultWorkspaceUi: WorkspaceUi = {
|
|
19
|
-
tabs: createDefaultWorkspaceTabs(),
|
|
20
15
|
layoutMode: 'split',
|
|
21
16
|
dockedChatWidth: 360,
|
|
22
17
|
popupChatWidth: 440
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { WorkspaceTabId } from '@/lib/types'
|
|
2
|
+
import { create } from 'zustand'
|
|
3
|
+
import { createJSONStorage, persist } from 'zustand/middleware'
|
|
4
|
+
|
|
5
|
+
type WorkspaceSession = {
|
|
6
|
+
activeTab?: WorkspaceTabId
|
|
7
|
+
chatOpen?: boolean
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
type WorkspaceSessionStore = {
|
|
11
|
+
workspaces: Record<string, WorkspaceSession>
|
|
12
|
+
setWorkspaceSession: (workspaceId: string, patch: WorkspaceSession) => void
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Per-browser-tab UI state that survives reloads.
|
|
16
|
+
export const useWorkspaceSessionStore = create<WorkspaceSessionStore>()(
|
|
17
|
+
persist(
|
|
18
|
+
set => ({
|
|
19
|
+
workspaces: {},
|
|
20
|
+
setWorkspaceSession: (workspaceId, patch) =>
|
|
21
|
+
set(state => ({
|
|
22
|
+
workspaces: {
|
|
23
|
+
...state.workspaces,
|
|
24
|
+
[workspaceId]: { ...state.workspaces[workspaceId], ...patch }
|
|
25
|
+
}
|
|
26
|
+
}))
|
|
27
|
+
}),
|
|
28
|
+
{ name: 'moi:workspace-session', storage: createJSONStorage(() => sessionStorage) }
|
|
29
|
+
)
|
|
30
|
+
)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Qn}from"/chunk-xj1pjhng.js";export{Qn as sanitizeSvg};
|