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
@@ -0,0 +1,279 @@
1
+ import type { ReactNode, Ref } from 'react'
2
+ import { useImperativeHandle } from 'react'
3
+
4
+ import { Button } from '@/client/components/ui/button'
5
+ import { Spinner } from '@/client/components/ui/spinner'
6
+ import { DrawingLayer } from '@/client/features/drawings/DrawingLayer'
7
+ import { DrawingToolbar } from '@/client/features/drawings/DrawingToolbar'
8
+ import {
9
+ type DrawingControls,
10
+ useDrawingHistoryState
11
+ } from '@/client/features/drawings/useDrawingLayer'
12
+ import { type ViewSketchController, useViewSketch } from '@/client/features/drawings/useViewSketch'
13
+ import { cn } from '@/client/lib/cn'
14
+ import type { PendingView } from '@/lib/types'
15
+ import { draftSessionId } from '@/lib/session-drafts'
16
+ import { viewTabId } from '@/lib/workspace-tabs'
17
+ import { IconScribble } from '@tabler/icons-react'
18
+
19
+ export type PendingViewHandle = {
20
+ prepareSketchForSend: () => Promise<void>
21
+ resetSketch: () => Promise<void>
22
+ }
23
+
24
+ type PendingViewScreenProps = {
25
+ ref?: Ref<PendingViewHandle>
26
+ active: boolean
27
+ pendingView: PendingView
28
+ running: boolean
29
+ sketchSessionId?: string
30
+ chatDocked: boolean
31
+ workspaceId: string
32
+ onEditingStart: () => void
33
+ onContinueInChat: () => void
34
+ onOpenChat: () => void
35
+ onDiscard: () => void
36
+ }
37
+
38
+ export function PendingViewScreen({
39
+ ref,
40
+ active,
41
+ pendingView,
42
+ running,
43
+ sketchSessionId,
44
+ chatDocked,
45
+ workspaceId,
46
+ onEditingStart,
47
+ onContinueInChat,
48
+ onOpenChat,
49
+ onDiscard
50
+ }: PendingViewScreenProps) {
51
+ const displayStatus =
52
+ pendingView.status === 'submitted' ? (running ? 'building' : 'waiting') : pendingView.status
53
+ const sketch = useViewSketch({
54
+ active: active && pendingView.status === 'draft',
55
+ viewId: pendingView.id,
56
+ sessionId:
57
+ sketchSessionId ??
58
+ pendingView.executionSessionId ??
59
+ draftSessionId(viewTabId(pendingView.id)),
60
+ source: viewTabId(pendingView.id),
61
+ workspaceId,
62
+ onEditingStart,
63
+ onContinueInChat
64
+ })
65
+
66
+ useImperativeHandle(
67
+ ref,
68
+ () => ({
69
+ prepareSketchForSend: sketch.prepareForSend,
70
+ resetSketch: sketch.resetDocument
71
+ }),
72
+ [sketch.prepareForSend, sketch.resetDocument]
73
+ )
74
+
75
+ return (
76
+ <PendingViewScreenSketchSurface
77
+ active={active}
78
+ chatDocked={chatDocked}
79
+ error={pendingView.error}
80
+ sketch={sketch}
81
+ displayStatus={displayStatus}
82
+ onDiscard={onDiscard}
83
+ onOpenChat={onOpenChat}
84
+ />
85
+ )
86
+ }
87
+
88
+ function PendingViewScreenStarting() {
89
+ return (
90
+ <PendingViewScreenStatusLayout>
91
+ <div className="flex max-w-md flex-col items-center gap-4 p-8 text-center">
92
+ <Spinner className="size-6 text-muted-foreground" />
93
+ <p className="text-sm text-muted-foreground">Starting view chat…</p>
94
+ </div>
95
+ </PendingViewScreenStatusLayout>
96
+ )
97
+ }
98
+
99
+ type PendingViewScreenFailedProps = {
100
+ error?: string
101
+ onOpenChat: () => void
102
+ onDiscard: () => void
103
+ }
104
+
105
+ function PendingViewScreenFailed({ error, onOpenChat, onDiscard }: PendingViewScreenFailedProps) {
106
+ return (
107
+ <PendingViewScreenStatusLayout>
108
+ <div className="pointer-events-auto flex max-w-md flex-col items-center gap-4 bg-radial from-background from-50% to-transparent to-75% p-8 text-center">
109
+ <p className="text-sm text-muted-foreground">{error}</p>
110
+ <Button onClick={onOpenChat}>Open chat</Button>
111
+ <Button variant="secondary" onClick={onDiscard}>
112
+ Discard view
113
+ </Button>
114
+ </div>
115
+ </PendingViewScreenStatusLayout>
116
+ )
117
+ }
118
+
119
+ type PendingViewScreenSketchSurfaceProps = {
120
+ active: boolean
121
+ chatDocked: boolean
122
+ error?: string
123
+ sketch: ViewSketchController
124
+ displayStatus: 'draft' | 'starting' | 'building' | 'waiting' | 'failed'
125
+ onDiscard: () => void
126
+ onOpenChat: () => void
127
+ }
128
+
129
+ function PendingViewScreenSketchSurface({
130
+ active,
131
+ chatDocked,
132
+ error,
133
+ sketch,
134
+ displayStatus,
135
+ onDiscard,
136
+ onOpenChat
137
+ }: PendingViewScreenSketchSurfaceProps) {
138
+ const draft = displayStatus === 'draft'
139
+
140
+ return (
141
+ <div
142
+ className={cn(
143
+ 'relative min-h-0 flex-1 overflow-hidden bg-background',
144
+ active ? 'flex' : 'hidden'
145
+ )}
146
+ >
147
+ <PendingViewScreenSketchBackground chatDocked={chatDocked} sketch={sketch} draft={draft} />
148
+ <div
149
+ inert={draft ? undefined : true}
150
+ className={cn('absolute inset-0', !draft && 'pointer-events-none opacity-50')}
151
+ >
152
+ <DrawingLayer {...sketch.layerProps}>
153
+ {draft && (
154
+ <DrawingToolbar
155
+ controls={sketch.controls}
156
+ title="Sketch the view"
157
+ busy={sketch.continuing}
158
+ onComplete={() => void sketch.continueInChat()}
159
+ />
160
+ )}
161
+ </DrawingLayer>
162
+ </div>
163
+ <div
164
+ aria-hidden
165
+ className={cn(
166
+ 'pointer-events-none absolute inset-0 transition-shadow duration-500 ease-in-out',
167
+ displayStatus === 'building' &&
168
+ 'animate-glow texture-inset-shadow-50% motion-reduce:animate-none'
169
+ )}
170
+ />
171
+ {displayStatus === 'building' && (
172
+ <PendingViewScreenBuildingState controls={sketch.controls} />
173
+ )}
174
+ {displayStatus === 'starting' && <PendingViewScreenStarting />}
175
+ {displayStatus === 'waiting' && (
176
+ <PendingViewScreenWaitingState
177
+ error={error}
178
+ onDiscard={onDiscard}
179
+ onOpenChat={onOpenChat}
180
+ />
181
+ )}
182
+ {displayStatus === 'failed' && (
183
+ <PendingViewScreenFailed error={error} onOpenChat={onOpenChat} onDiscard={onDiscard} />
184
+ )}
185
+ </div>
186
+ )
187
+ }
188
+
189
+ type PendingViewScreenSketchBackgroundProps = {
190
+ chatDocked: boolean
191
+ sketch: ViewSketchController
192
+ draft: boolean
193
+ }
194
+
195
+ function PendingViewScreenSketchBackground({
196
+ chatDocked,
197
+ sketch,
198
+ draft
199
+ }: PendingViewScreenSketchBackgroundProps) {
200
+ const { controls, targetRef } = sketch
201
+ const { hasStrokes } = useDrawingHistoryState(controls)
202
+
203
+ return (
204
+ <div
205
+ ref={targetRef}
206
+ className="absolute inset-0 flex items-center justify-center texture-checker-20"
207
+ >
208
+ {draft && !controls.active && !hasStrokes && (
209
+ <div
210
+ className={cn(
211
+ 'mb-4 flex h-full flex-col items-center justify-center gap-2 p-12 text-center text-sm text-muted-foreground',
212
+ 'bg-radial from-background from-10% to-transparent',
213
+ chatDocked ? 'w-full' : 'mr-auto w-[calc(100%-var(--chat-max))]'
214
+ )}
215
+ >
216
+ <IconScribble size={56} stroke={0.5} />
217
+ <span className="max-w-32">Sketch how the view should look</span>
218
+ </div>
219
+ )}
220
+ </div>
221
+ )
222
+ }
223
+
224
+ type PendingViewScreenWaitingStateProps = {
225
+ error?: string
226
+ onDiscard: () => void
227
+ onOpenChat: () => void
228
+ }
229
+
230
+ function PendingViewScreenWaitingState({
231
+ error,
232
+ onDiscard,
233
+ onOpenChat
234
+ }: PendingViewScreenWaitingStateProps) {
235
+ return (
236
+ <PendingViewScreenStatusLayout>
237
+ <div className="pointer-events-auto flex max-w-md flex-col items-center gap-4 bg-radial from-background from-50% to-transparent to-75% p-12 text-center">
238
+ <div className="flex flex-col gap-1">
239
+ <h1 className="font-medium">The view needs your attention</h1>
240
+ <p className="text-sm text-muted-foreground">
241
+ {error ?? 'Open the chat to answer or ask the agent to continue'}
242
+ </p>
243
+ </div>
244
+ <div className="flex gap-2">
245
+ <Button onClick={onOpenChat}>Open chat</Button>
246
+ <Button variant="secondary" onClick={onDiscard}>
247
+ Discard view
248
+ </Button>
249
+ </div>
250
+ </div>
251
+ </PendingViewScreenStatusLayout>
252
+ )
253
+ }
254
+
255
+ type PendingViewScreenBuildingStateProps = {
256
+ controls: DrawingControls
257
+ }
258
+
259
+ function PendingViewScreenBuildingState({ controls }: PendingViewScreenBuildingStateProps) {
260
+ const { hasStrokes } = useDrawingHistoryState(controls)
261
+
262
+ return (
263
+ <PendingViewScreenStatusLayout>
264
+ {!hasStrokes && <Spinner className="size-6 text-muted-foreground" />}
265
+ </PendingViewScreenStatusLayout>
266
+ )
267
+ }
268
+
269
+ type PendingViewScreenStatusLayoutProps = {
270
+ children: ReactNode
271
+ }
272
+
273
+ function PendingViewScreenStatusLayout({ children }: PendingViewScreenStatusLayoutProps) {
274
+ return (
275
+ <div className="pointer-events-none absolute inset-0 flex items-center justify-center">
276
+ {children}
277
+ </div>
278
+ )
279
+ }
@@ -29,7 +29,7 @@ import { AppletPresenceProvider } from '@/client/features/collab'
29
29
  import { useWorkspaceId } from '@/client/features/workspace/WorkspaceContext'
30
30
  import { cn } from '@/client/lib/cn'
31
31
  import { useLatestRef } from '@/client/lib/use-latest-ref'
32
- import type { ViewInfo } from '@/lib/types'
32
+ import type { CompiledView } from '@/lib/types'
33
33
 
34
34
  import {
35
35
  nextEvictionDelay,
@@ -39,10 +39,10 @@ import {
39
39
  } from './view-residency'
40
40
 
41
41
  type ViewManagerProps = {
42
- views: ViewInfo[]
42
+ views: CompiledView[]
43
43
  // The view the active tab names, or null when another tab is on screen. The
44
44
  // manager stays mounted either way — that is what makes coming back from the
45
- // agent or Overview tab instant too.
45
+ // Overview or Scratchpad tab instant too.
46
46
  activeViewId: string | null
47
47
  // The active view's URL query params. Parked views retain their last values.
48
48
  params: Record<string, string>
@@ -83,7 +83,7 @@ export const ViewManager = memo(function ViewManager({
83
83
 
84
84
  // The views mounted right now: the active one, plus the recently-visited ones
85
85
  // parked offscreen. The policy (and its timing) lives in view-residency.ts.
86
- function useResidentViews(activeId: string | null, views: ViewInfo[]): ResidentView[] {
86
+ function useResidentViews(activeId: string | null, views: CompiledView[]): ResidentView[] {
87
87
  const [residents, setResidents] = useState<ResidentView[]>([])
88
88
  // A workspace refetch hands us a new array on every event; residency only
89
89
  // cares whether a view appeared or disappeared. So the effect keys off the id
@@ -119,7 +119,7 @@ function useResidentViews(activeId: string | null, views: ViewInfo[]): ResidentV
119
119
  }
120
120
 
121
121
  type ViewSlotProps = {
122
- view: ViewInfo
122
+ view: CompiledView
123
123
  active: boolean
124
124
  params: Record<string, string>
125
125
  }
@@ -191,8 +191,8 @@ function ViewSlot({ view, active, params }: ViewSlotProps) {
191
191
  }
192
192
 
193
193
  type ViewFrameProps = {
194
- view: ViewInfo
195
- build: ViewBuild
194
+ view: CompiledView
195
+ build: LoadedViewVersion
196
196
  params: Record<string, string>
197
197
  // Play the rebuild dissolve. Set on the incoming build only, and only while
198
198
  // the build it replaced is still rendered underneath it.
@@ -237,7 +237,7 @@ function ViewFrame({ view, build, params, entering, current = false }: ViewFrame
237
237
  )
238
238
  }
239
239
 
240
- type ViewBuild = {
240
+ type LoadedViewVersion = {
241
241
  Component: ComponentType<AppletComponentProps>
242
242
  version: number
243
243
  }
@@ -248,11 +248,11 @@ type LoadedBundle = {
248
248
  // spinner every time the agent edits it is worse than showing the previous
249
249
  // build for that moment. The splash is for a view with nothing to show yet —
250
250
  // the first time it is opened.
251
- current: ViewBuild | null
251
+ current: LoadedViewVersion | null
252
252
  // The build `current` just replaced, kept underneath for the length of the
253
253
  // dissolve so the new one fades in over the view instead of over an empty
254
254
  // panel. Null except during a rebuild swap on screen.
255
- outgoing: ViewBuild | null
255
+ outgoing: LoadedViewVersion | null
256
256
  }
257
257
 
258
258
  // How long the outgoing build stays underneath. Matches the `duration-200` the
@@ -1,117 +1,78 @@
1
+ import type { MessageAttachment, PendingView, SelectedSessionScope, ViewInfo } from '@/lib/types'
1
2
  import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
2
-
3
3
  import { jsonRequest, requestJson, requestVoid } from '@/client/api/http'
4
4
  import { WORKSPACE_RESOURCE_OPTIONS } from '@/client/api/query-options'
5
5
  import { workspaceKeys } from '@/client/api/workspace-keys'
6
- import { APP_ICON_IDS } from '@/client/lib/app-icon-registry'
7
6
  import { useWorkspaceEvent } from '@/client/runtime/useWorkspaceEvents'
8
- import type { MessageAttachment, SelectedSessionScope, ViewBuilder, ViewInfo } from '@/lib/types'
9
7
 
10
8
  export function useViews(workspaceId: string) {
9
+ const queryClient = useQueryClient()
10
+ useWorkspaceEvent(event => {
11
+ if (
12
+ (event.type === 'views:changed' || event.type === 'view:deleted') &&
13
+ event.workspaceId === workspaceId
14
+ ) {
15
+ void queryClient.invalidateQueries({ queryKey: workspaceKeys.views(workspaceId) })
16
+ if (event.type === 'view:deleted')
17
+ void queryClient.invalidateQueries({ queryKey: workspaceKeys.sessions(workspaceId) })
18
+ }
19
+ })
11
20
  return useQuery<ViewInfo[]>({
12
21
  queryKey: workspaceKeys.views(workspaceId),
13
- queryFn: async () => {
14
- const data = await requestJson<{ views: ViewInfo[] }>(`/api/workspaces/${workspaceId}/views`)
15
- return data.views
16
- },
22
+ queryFn: async () =>
23
+ (await requestJson<{ views: ViewInfo[] }>(`/api/workspaces/${workspaceId}/views`)).views,
17
24
  ...WORKSPACE_RESOURCE_OPTIONS
18
25
  })
19
26
  }
20
-
21
- export function useRenameView(workspaceId: string) {
22
- return useMutation<ViewInfo, Error, { viewId: string; title: string }>({
23
- mutationFn: ({ viewId, title }) =>
24
- requestJson(
25
- `/api/workspaces/${workspaceId}/views/${encodeURIComponent(viewId)}`,
26
- jsonRequest('PATCH', { title }),
27
- 'Failed to rename view'
28
- )
29
- })
30
- }
31
-
32
- export function useDeleteView(workspaceId: string) {
33
- return useMutation<void, Error, string>({
34
- mutationFn: viewId =>
35
- requestVoid(
36
- `/api/workspaces/${workspaceId}/views/${encodeURIComponent(viewId)}`,
37
- { method: 'DELETE' },
38
- 'Failed to delete view'
39
- )
27
+ function useViewMutation<T, Input>(workspaceId: string, mutationFn: (input: Input) => Promise<T>) {
28
+ const queryClient = useQueryClient()
29
+ return useMutation({
30
+ mutationFn,
31
+ onSuccess: () => queryClient.invalidateQueries({ queryKey: workspaceKeys.views(workspaceId) })
40
32
  })
41
33
  }
42
-
43
- function upsertBuilder(builders: ViewBuilder[] | undefined, builder: ViewBuilder): ViewBuilder[] {
44
- const current = builders ?? []
45
- const index = current.findIndex(candidate => candidate.id === builder.id)
46
- if (index === -1) return [...current, builder]
47
- return current.map(candidate =>
48
- candidate.id === builder.id && candidate.updatedAt <= builder.updatedAt ? builder : candidate
34
+ export function useRenameView(workspaceId: string) {
35
+ return useViewMutation(workspaceId, ({ viewId, title }: { viewId: string; title: string }) =>
36
+ requestJson<ViewInfo>(
37
+ `/api/workspaces/${workspaceId}/views/${encodeURIComponent(viewId)}`,
38
+ jsonRequest('PATCH', { title }),
39
+ 'Couldn’t rename view'
40
+ )
49
41
  )
50
42
  }
51
-
52
- export function useViewBuilders(workspaceId: string) {
53
- const queryClient = useQueryClient()
54
- useWorkspaceEvent(event => {
55
- if (event.type === 'view-builder:updated' && event.workspaceId === workspaceId) {
56
- queryClient.setQueryData<ViewBuilder[]>(workspaceKeys.viewBuilders(workspaceId), current =>
57
- upsertBuilder(current, event.builder)
58
- )
59
- } else if (event.type === 'view-builder:deleted' && event.workspaceId === workspaceId) {
60
- queryClient.setQueryData<ViewBuilder[]>(workspaceKeys.viewBuilders(workspaceId), current =>
61
- (current ?? []).filter(builder => builder.id !== event.builderId)
62
- )
63
- }
64
- })
65
-
66
- return useQuery<ViewBuilder[]>({
67
- queryKey: workspaceKeys.viewBuilders(workspaceId),
68
- queryFn: async () => {
69
- const data = await requestJson<{ builders: ViewBuilder[] }>(
70
- `/api/workspaces/${workspaceId}/view-builders`
71
- )
72
- return data.builders
73
- },
74
- ...WORKSPACE_RESOURCE_OPTIONS
75
- })
43
+ export function useDeleteView(workspaceId: string) {
44
+ return useViewMutation(workspaceId, (viewId: string) =>
45
+ requestVoid(
46
+ `/api/workspaces/${workspaceId}/views/${encodeURIComponent(viewId)}`,
47
+ { method: 'DELETE' },
48
+ 'Couldn’t delete view'
49
+ )
50
+ )
76
51
  }
77
-
78
- export function useCreateViewBuilder(workspaceId: string) {
79
- const queryClient = useQueryClient()
80
- return useMutation<ViewBuilder, Error>({
81
- mutationFn: () =>
82
- requestJson(
83
- `/api/workspaces/${workspaceId}/view-builders`,
84
- jsonRequest('POST'),
85
- 'Failed to create view builder'
86
- ),
87
- onSuccess: builder => {
88
- queryClient.setQueryData<ViewBuilder[]>(workspaceKeys.viewBuilders(workspaceId), current =>
89
- upsertBuilder(current, builder)
90
- )
91
- }
92
- })
52
+ export function useCreateView(workspaceId: string) {
53
+ return useViewMutation(workspaceId, () =>
54
+ requestJson<PendingView>(
55
+ `/api/workspaces/${workspaceId}/views`,
56
+ jsonRequest('POST'),
57
+ 'Couldn’t create view'
58
+ )
59
+ )
93
60
  }
94
-
95
- export function useSaveViewBuilder(workspaceId: string) {
96
- const queryClient = useQueryClient()
97
- return useMutation<ViewBuilder, Error, { builderId: string; requirements: string }>({
98
- mutationFn: ({ builderId, requirements }) =>
99
- requestJson(
100
- `/api/workspaces/${workspaceId}/view-builders/${builderId}`,
101
- jsonRequest('PATCH', { input: { requirements } }),
102
- 'Failed to save view requirements'
103
- ),
104
- onSuccess: builder => {
105
- queryClient.setQueryData<ViewBuilder[]>(workspaceKeys.viewBuilders(workspaceId), current =>
106
- upsertBuilder(current, builder)
61
+ export function useSaveViewDraft(workspaceId: string) {
62
+ return useViewMutation(
63
+ workspaceId,
64
+ ({ viewId, requirements }: { viewId: string; requirements: string }) =>
65
+ requestJson<PendingView>(
66
+ `/api/workspaces/${workspaceId}/views/${viewId}`,
67
+ jsonRequest('PATCH', { requirements }),
68
+ 'Couldn’t save view requirements'
107
69
  )
108
- }
109
- })
70
+ )
110
71
  }
111
-
112
- export type SubmitViewBuilderInput = {
113
- builderId: string
72
+ export type SubmitViewInput = {
73
+ viewId: string
114
74
  requirements: string
75
+ sessionId: string
115
76
  optimisticId: string
116
77
  selectedSessionScope?: SelectedSessionScope
117
78
  attachments?: MessageAttachment[]
@@ -120,41 +81,13 @@ export type SubmitViewBuilderInput = {
120
81
  fastMode?: boolean
121
82
  stream?: boolean
122
83
  }
123
-
124
- export function useSubmitViewBuilder(workspaceId: string) {
125
- const queryClient = useQueryClient()
126
- return useMutation<ViewBuilder, Error, SubmitViewBuilderInput>({
127
- mutationFn: ({ builderId, requirements, ...options }) =>
128
- requestJson(
129
- `/api/workspaces/${workspaceId}/view-builders/${builderId}/submit`,
130
- jsonRequest('POST', {
131
- input: { requirements },
132
- availableIcons: APP_ICON_IDS,
133
- ...options
134
- }),
135
- 'Failed to start view builder'
136
- ),
137
- onSuccess: builder => {
138
- queryClient.setQueryData<ViewBuilder[]>(workspaceKeys.viewBuilders(workspaceId), current =>
139
- upsertBuilder(current, builder)
140
- )
141
- }
142
- })
143
- }
144
-
145
- export function useDiscardViewBuilder(workspaceId: string) {
146
- const queryClient = useQueryClient()
147
- return useMutation<void, Error, string>({
148
- mutationFn: builderId =>
149
- requestVoid(
150
- `/api/workspaces/${workspaceId}/view-builders/${builderId}`,
151
- { method: 'DELETE' },
152
- 'Failed to discard view builder'
153
- ),
154
- onSuccess: (_result, builderId) => {
155
- queryClient.setQueryData<ViewBuilder[]>(workspaceKeys.viewBuilders(workspaceId), current =>
156
- (current ?? []).filter(builder => builder.id !== builderId)
157
- )
158
- }
159
- })
84
+ export function useSubmitView(workspaceId: string) {
85
+ return useViewMutation(workspaceId, ({ viewId, ...input }: SubmitViewInput) =>
86
+ requestJson<{ viewId: string; sessionId: string }>(
87
+ `/api/workspaces/${workspaceId}/views/${viewId}/submit`,
88
+ jsonRequest('POST', input),
89
+ 'Couldn’t start view chat'
90
+ )
91
+ )
160
92
  }
93
+ export const useDiscardView = useDeleteView