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

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 (82) hide show
  1. package/client/app/app-provider.tsx +14 -20
  2. package/client/app/app-router.tsx +5 -8
  3. package/client/app/routes/workspace-route.tsx +28 -11
  4. package/client/app/shell/sidebar-layout.tsx +5 -10
  5. package/client/features/app-settings/api.ts +3 -3
  6. package/client/features/applets/applet-cache.ts +1 -2
  7. package/client/features/applets/use-applet.ts +18 -20
  8. package/client/features/chat/chat-store.ts +1 -1
  9. package/client/features/chat/connection/chat-connection.ts +25 -68
  10. package/client/features/chat/connection/chat-frames.ts +20 -32
  11. package/client/features/chat/sessions/api.ts +2 -2
  12. package/client/features/collab/presence/client.ts +63 -83
  13. package/client/features/collab/presence/provider.tsx +8 -2
  14. package/client/features/dev/chat-states-page.tsx +1 -1
  15. package/client/features/dev/harness-debug-page.tsx +2 -2
  16. package/client/features/scratchpad/scratchpad.tsx +29 -20
  17. package/client/features/views/api.ts +5 -6
  18. package/client/features/workspace/api.ts +6 -11
  19. package/client/features/workspace/harness-transition.ts +3 -5
  20. package/client/features/workspace/remote.ts +9 -0
  21. package/client/features/workspace/settings/use-env-vars.ts +4 -4
  22. package/client/features/workspace/use-workspace-navigation.ts +1 -1
  23. package/client/features/workspace/use-workspace-socket.ts +51 -0
  24. package/client/features/workspace/workspace-content.tsx +3 -7
  25. package/client/features/workspace/workspace-layout-context.tsx +6 -13
  26. package/client/features/workspace/workspace-state.ts +17 -6
  27. package/client/runtime/app-context.ts +2 -2
  28. package/client/runtime/app-events.ts +75 -0
  29. package/client/runtime/reconnecting-socket.ts +115 -0
  30. package/client/runtime/{use-workspace-events.ts → use-client-events.ts} +9 -10
  31. package/dist/{chunk-f44cs6mw.js → chunk-0t8m1ehd.js} +2 -2
  32. package/dist/chunk-851bx8ss.js +72 -0
  33. package/dist/{chunk-xj1pjhng.js → chunk-c8g5x288.js} +1 -1
  34. package/dist/chunk-cty37f3v.js +1 -0
  35. package/dist/chunk-dy6f6h3e.js +5 -0
  36. package/dist/chunk-j6928pwm.js +1 -0
  37. package/dist/{chunk-r95acex1.js → chunk-k32585ne.js} +70 -70
  38. package/dist/chunk-qtkv7hp8.js +2 -0
  39. package/dist/chunk-rh58dc0y.js +2 -0
  40. package/dist/{chunk-a865c7x4.js → chunk-wjbejzek.js} +1 -1
  41. package/dist/index.html +1 -1
  42. package/lib/events.ts +61 -0
  43. package/lib/ids.ts +2 -8
  44. package/lib/types.ts +12 -15
  45. package/package.json +1 -1
  46. package/server/AGENTS.md +3 -3
  47. package/server/README.md +29 -15
  48. package/server/api/index.ts +54 -35
  49. package/server/applets/views.ts +15 -14
  50. package/server/applets/widgets.ts +4 -3
  51. package/server/cli/commands/refresh.ts +2 -5
  52. package/server/control.ts +24 -33
  53. package/server/events.ts +14 -13
  54. package/server/harness/README.md +7 -7
  55. package/server/harness/acp/session.ts +9 -5
  56. package/server/harness/auth.ts +7 -30
  57. package/server/harness/claude-code/session.ts +8 -4
  58. package/server/harness/codex/NOTES.md +1 -1
  59. package/server/harness/codex/session.ts +18 -10
  60. package/server/harness/hermes/NOTES.md +3 -3
  61. package/server/harness/openclaw/session.ts +9 -5
  62. package/server/onboarding/jobs.ts +7 -7
  63. package/server/operations/commands.ts +1 -1
  64. package/server/scratchpad/executor.ts +2 -2
  65. package/server/scratchpad/relay.ts +3 -3
  66. package/server/sessions/archive.ts +1 -1
  67. package/server/sessions/lifecycle.ts +1 -1
  68. package/server/sessions/pending-views.ts +17 -11
  69. package/server/web.ts +40 -30
  70. package/server/workspace-state/index.ts +15 -4
  71. package/server/workspaces/config.ts +14 -13
  72. package/server/workspaces/env-apply.ts +1 -1
  73. package/server/workspaces/layout.ts +40 -7
  74. package/server/workspaces/shared.ts +1 -1
  75. package/client/features/scratchpad/scratch-executor.ts +0 -20
  76. package/client/runtime/workspace-events.ts +0 -168
  77. package/dist/chunk-0h4zypgf.js +0 -1
  78. package/dist/chunk-1tksq43z.js +0 -72
  79. package/dist/chunk-6gxf0xr8.js +0 -5
  80. package/dist/chunk-h3qag4a6.js +0 -2
  81. package/dist/chunk-q2tpy0h7.js +0 -2
  82. package/dist/chunk-w5h9grdr.js +0 -1
@@ -3,12 +3,12 @@ import { useMutation, useQuery, useQueryClient, type QueryClient } from '@tansta
3
3
  import { requestJson } from '@/client/api/http'
4
4
  import { workspaceKeys } from '@/client/api/workspace-keys'
5
5
  import { toast } from '@/client/components/ui/toast'
6
- import { useWorkspaceEvent } from '@/client/runtime/use-workspace-events'
7
- import type { WorkspaceState } from '@/lib/types'
6
+ import { useClientEvent } from '@/client/runtime/use-client-events'
7
+ import type { CompiledView, WorkspaceState } from '@/lib/types'
8
8
 
9
9
  // Personal state for one workspace: tabs, pin, selected chats, chat
10
10
  // settings and fork history, and views being built. One cache, kept current by
11
- // write replies and `state:changed` events instead of refetches.
11
+ // write replies and `personal-state:changed` events instead of refetches.
12
12
 
13
13
  export function workspaceStateKey(workspaceId: string) {
14
14
  return workspaceKeys.state(workspaceId)
@@ -159,9 +159,20 @@ export function useWorkspaceStateWrite<Input, Result extends StateReply = Worksp
159
159
  // changes, so cached states stay current even while their workspace is closed.
160
160
  export function useWorkspaceStateSync(): void {
161
161
  const queryClient = useQueryClient()
162
- useWorkspaceEvent(event => {
163
- if (event.type !== 'state:changed') return
164
- if (!getCachedWorkspaceState(queryClient, event.workspaceId)) return
162
+ useClientEvent(event => {
163
+ if (event.type !== 'personal-state:changed') return
164
+ const current = getCachedWorkspaceState(queryClient, event.workspaceId)
165
+ if (!current || current.revision > event.state.revision) return
166
+ // The personal socket can deliver completion before the owner sends its views list.
167
+ const key = workspaceKeys.views(event.workspaceId)
168
+ const views = queryClient.getQueryData<CompiledView[]>(key)
169
+ const missing = event.completedViews?.filter(
170
+ view => current.pendingViews[view.id] && !views?.some(old => old.id === view.id)
171
+ )
172
+ if (views && missing?.length) {
173
+ void queryClient.cancelQueries({ queryKey: key })
174
+ queryClient.setQueryData(key, [...views, ...missing])
175
+ }
165
176
  receiveWorkspaceState(queryClient, event.workspaceId, event.state)
166
177
  })
167
178
  }
@@ -1,6 +1,6 @@
1
1
  import { createContext, useContext } from 'react'
2
2
 
3
- import type { WorkspaceEventsClient } from './workspace-events'
3
+ import type { AppEventsClient } from './app-events'
4
4
 
5
5
  // App-wide state that lives exactly as long as the app: created by
6
6
  // `AppProvider` (app/app-provider.tsx) when the app mounts and shut down when
@@ -8,7 +8,7 @@ import type { WorkspaceEventsClient } from './workspace-events'
8
8
  // app itself.
9
9
  export type AppContextValue = {
10
10
  // The page's workspace events socket.
11
- events: WorkspaceEventsClient
11
+ events: AppEventsClient
12
12
  }
13
13
 
14
14
  export const AppContext = createContext<AppContextValue | undefined>(undefined)
@@ -0,0 +1,75 @@
1
+ import type { ClientEvent } from '@/lib/events'
2
+
3
+ import { ReconnectingSocket } from './reconnecting-socket'
4
+ export type { ClientEvent } from '@/lib/events'
5
+
6
+ export type ClientEventHandler = (event: ClientEvent) => void
7
+
8
+ export type AppEventsClientOptions = {
9
+ reconnectBaseDelayMs?: number
10
+ }
11
+
12
+ // The app's event bus, fed by the machine's `/api/events` socket. Workspace
13
+ // sockets emit into it too, so listeners see one stream. `AppProvider` opens
14
+ // the socket when the app mounts and closes it when the app unmounts.
15
+ export class AppEventsClient {
16
+ private readonly listeners = new Set<ClientEventHandler>()
17
+ private readonly socket: ReconnectingSocket
18
+ private release: (() => void) | undefined
19
+ private shownWorkspace: string | undefined
20
+
21
+ constructor(url: string, options: AppEventsClientOptions = {}) {
22
+ this.socket = new ReconnectingSocket(() => url, { baseDelayMs: options.reconnectBaseDelayMs })
23
+ this.socket.subscribe({
24
+ open: () => {
25
+ this.sendNavigationPresence()
26
+ this.emit({ type: 'app:connected' })
27
+ },
28
+ message: message => this.emit(message as ClientEvent)
29
+ })
30
+ }
31
+
32
+ connect() {
33
+ this.release ??= this.socket.start()
34
+ }
35
+
36
+ close() {
37
+ this.release?.()
38
+ this.release = undefined
39
+ }
40
+
41
+ subscribe(handler: ClientEventHandler): () => void {
42
+ this.listeners.add(handler)
43
+ return () => {
44
+ this.listeners.delete(handler)
45
+ }
46
+ }
47
+
48
+ emit(event: ClientEvent) {
49
+ for (const handler of this.listeners) handler(event)
50
+ }
51
+
52
+ send(message: object) {
53
+ this.socket.send(message)
54
+ }
55
+
56
+ // The workspace this tab shows, for the server's navigation relay. Sent now
57
+ // and again on every reconnect.
58
+ get navigationWorkspace(): string | undefined {
59
+ return this.shownWorkspace
60
+ }
61
+
62
+ setNavigationWorkspace(workspaceId: string | undefined) {
63
+ this.shownWorkspace = workspaceId
64
+ this.sendNavigationPresence()
65
+ }
66
+
67
+ sendNavigationPresence() {
68
+ this.send({
69
+ type: 'navigation:presence',
70
+ // The wire format uses `null` for "no workspace".
71
+ workspaceId: this.shownWorkspace ?? null,
72
+ focused: document.visibilityState === 'visible' && document.hasFocus()
73
+ })
74
+ }
75
+ }
@@ -0,0 +1,115 @@
1
+ export type SocketListener = {
2
+ open?: () => void
3
+ message?: (message: { type?: unknown }) => void
4
+ close?: () => void
5
+ }
6
+
7
+ export type ReconnectingSocketOptions = {
8
+ // Ping the server and drop a connection that has gone silent.
9
+ ping?: boolean
10
+ // Delay before reopening after a drop. It doubles on repeated drops, up to
11
+ // ten seconds, and resets once a socket opens.
12
+ baseDelayMs?: number
13
+ }
14
+
15
+ // The one WebSocket wrapper for the app, harness and workspace connections.
16
+ // It stays open while anything holds it and reopens on its own after a drop.
17
+ export class ReconnectingSocket {
18
+ private readonly listeners = new Set<SocketListener>()
19
+ private socket: WebSocket | undefined
20
+ private holders = 0
21
+ private attempts = 0
22
+ private lastMessageAt = 0
23
+ private retry: ReturnType<typeof setTimeout> | undefined
24
+ private heartbeat: ReturnType<typeof setInterval> | undefined
25
+
26
+ // The address is read on every connect.
27
+ constructor(
28
+ private readonly url: () => string,
29
+ private readonly options: ReconnectingSocketOptions = {}
30
+ ) {}
31
+
32
+ get isOpen(): boolean {
33
+ return this.socket?.readyState === WebSocket.OPEN
34
+ }
35
+
36
+ subscribe(listener: SocketListener): () => void {
37
+ this.listeners.add(listener)
38
+ return () => {
39
+ this.listeners.delete(listener)
40
+ }
41
+ }
42
+
43
+ start(): () => void {
44
+ let held = true
45
+ if (this.holders++ === 0) this.open()
46
+ return () => {
47
+ if (!held) return
48
+ held = false
49
+ if (--this.holders === 0) this.drop()
50
+ }
51
+ }
52
+
53
+ // Replace the connection, so the server forgets what this one had joined.
54
+ reconnect() {
55
+ this.drop()
56
+ if (this.holders) this.retry = setTimeout(() => this.open(), 0)
57
+ }
58
+
59
+ send(message: object) {
60
+ if (!this.isOpen) return
61
+ try {
62
+ this.socket?.send(JSON.stringify(message))
63
+ } catch {
64
+ this.socket?.close()
65
+ }
66
+ }
67
+
68
+ private open() {
69
+ if (this.socket || !this.holders) return
70
+ const socket = new WebSocket(this.url())
71
+ this.socket = socket
72
+ socket.onopen = () => {
73
+ if (socket !== this.socket) return
74
+ this.attempts = 0
75
+ this.lastMessageAt = Date.now()
76
+ if (this.options.ping)
77
+ this.heartbeat = setInterval(() => {
78
+ if (Date.now() - this.lastMessageAt > 45_000) socket.close()
79
+ else this.send({ type: 'ping' })
80
+ }, 15_000)
81
+ for (const listener of this.listeners) listener.open?.()
82
+ }
83
+ socket.onmessage = event => {
84
+ if (socket !== this.socket) return
85
+ this.lastMessageAt = Date.now()
86
+ let message: { type?: unknown }
87
+ try {
88
+ message = JSON.parse(String(event.data))
89
+ } catch {
90
+ return
91
+ }
92
+ for (const listener of this.listeners) listener.message?.(message)
93
+ }
94
+ socket.onerror = () => socket.close()
95
+ socket.onclose = () => {
96
+ if (socket !== this.socket) return
97
+ clearInterval(this.heartbeat)
98
+ this.socket = undefined
99
+ for (const listener of this.listeners) listener.close?.()
100
+ const maxDelay = Math.min((this.options.baseDelayMs ?? 500) * 2 ** this.attempts++, 10_000)
101
+ const delay = maxDelay * (0.5 + Math.random() * 0.5)
102
+ this.retry = setTimeout(() => this.open(), delay)
103
+ }
104
+ }
105
+
106
+ private drop() {
107
+ clearTimeout(this.retry)
108
+ clearInterval(this.heartbeat)
109
+ const socket = this.socket
110
+ if (!socket) return
111
+ this.socket = undefined
112
+ socket.close()
113
+ for (const listener of this.listeners) listener.close?.()
114
+ }
115
+ }
@@ -3,23 +3,22 @@ import { useEffect } from 'react'
3
3
  import { useLatestRef } from '@/client/lib/use-latest-ref'
4
4
 
5
5
  import { useAppContext } from './app-context'
6
- import type { WorkspaceEventHandler } from './workspace-events'
6
+ import type { ClientEventHandler } from './app-events'
7
7
 
8
- export type { WorkspaceEvent, WorkspaceEventHandler } from './workspace-events'
8
+ export type { ClientEvent, ClientEventHandler } from './app-events'
9
9
 
10
- // Listen to the app's workspace events. Subscribing never opens or closes the
10
+ // Listen to every client event. Subscribing never opens or closes the
11
11
  // socket — `AppProvider` owns that — so a route switch that briefly leaves
12
12
  // nothing subscribed costs nothing. A no-op outside `AppProvider`.
13
- export function useWorkspaceEvent(handler: WorkspaceEventHandler) {
13
+ export function useClientEvent(handler: ClientEventHandler) {
14
14
  const events = useAppContext()?.events
15
15
  const handlerRef = useLatestRef(handler)
16
16
  useEffect(() => events?.subscribe(event => handlerRef.current(event)), [events, handlerRef])
17
17
  }
18
18
 
19
- // Listen to events for one workspace. Events that name no workspace (such as
20
- // `moi refresh`) reach every open workspace.
21
- export function useWorkspaceScopedEvent(workspaceId: string, handler: WorkspaceEventHandler) {
22
- useWorkspaceEvent(event => {
19
+ // Listen to one workspace plus app-wide events.
20
+ export function useWorkspaceEvent(workspaceId: string, handler: ClientEventHandler) {
21
+ useClientEvent(event => {
23
22
  if (
24
23
  'workspaceId' in event &&
25
24
  event.workspaceId !== undefined &&
@@ -51,8 +50,8 @@ export function useNavigationClient(
51
50
  }
52
51
  }, [events, workspaceId])
53
52
 
54
- useWorkspaceEvent(async event => {
55
- if (!events || event.type !== 'navigation:request' || event.workspaceId !== workspaceId) return
53
+ useWorkspaceEvent(workspaceId, async event => {
54
+ if (!events || event.type !== 'navigation:request') return
56
55
  try {
57
56
  if (events.navigationWorkspace !== workspaceId) {
58
57
  throw new Error('The browser switched workspaces.')
@@ -1,2 +1,2 @@
1
- class Ao extends Error{status;constructor(e,t){super(t);this.status=e;this.name="HttpError"}}async function o(e,t){let s=await e.text();return new Ao(e.status,s||t)}async function h(e,t,s="Request failed"){let n=await fetch(e,t);if(!n.ok)throw await o(n,s);return n.json()}async function he(e,t,s="Request failed"){let n=await fetch(e,t);if(!n.ok)throw await o(n,s)}function S(e,t){return{method:e,headers:{"Content-Type":"application/json"},body:t===void 0?void 0:JSON.stringify(t)}}
2
- export{Ao,h,he,S};
1
+ class Lo extends Error{status;constructor(e,t){super(t);this.status=e;this.name="HttpError"}}async function o(e,t){let s=await e.text();return new Lo(e.status,s||t)}async function h(e,t,s="Request failed"){let n=await fetch(e,t);if(!n.ok)throw await o(n,s);return n.json()}async function he(e,t,s="Request failed"){let n=await fetch(e,t);if(!n.ok)throw await o(n,s)}function S(e,t){return{method:e,headers:{"Content-Type":"application/json"},body:t===void 0?void 0:JSON.stringify(t)}}
2
+ export{Lo,h,he,S};