moi-computer 0.11.1-dev.1c52199 → 0.11.1-dev.be1ed10
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/api/app-config.ts +1 -2
- package/client/app/routes/WorkspaceRoute.tsx +19 -27
- package/client/features/applets/AppletMount.tsx +1 -4
- package/client/features/applets/applet-runtime.ts +2 -13
- package/client/features/chat/applet-chat-intents.ts +6 -15
- package/client/features/chat/sessions/useSelectedSession.ts +20 -64
- package/client/features/chat/useChat.ts +0 -4
- package/client/features/dev/DevIndexPage.tsx +0 -5
- package/client/features/dev/DevRoutes.tsx +1 -14
- package/client/features/views/ViewManager.tsx +1 -4
- package/client/features/views/api.ts +2 -2
- package/client/features/views/useViewBuilderActions.ts +0 -3
- package/client/features/workspace/WorkspaceLayoutContext.tsx +3 -18
- package/client/features/workspace/WorkspaceScreen.tsx +11 -19
- package/client/features/workspace/api.ts +1 -3
- package/client/features/workspace/moi-context.ts +2 -7
- package/client/features/workspace/useWorkspaceNavigation.ts +3 -12
- package/client/index.tsx +3 -4
- package/dist/chunk-691pqsj6.js +2 -0
- package/dist/chunk-7a5nm4k9.css +1 -0
- package/dist/chunk-amb8y04y.js +1 -0
- package/dist/chunk-cf3pgy7j.js +546 -0
- package/dist/{chunk-wnmf38ta.js → chunk-mzrgr577.js} +1 -1
- package/dist/chunk-q6a0g26x.js +1 -0
- package/dist/chunk-twgdemkc.js +74 -0
- package/dist/chunk-x90k0f5c.js +2 -0
- package/dist/chunk-yq00csvd.js +2 -0
- package/dist/index.html +1 -1
- package/lib/moi-context.ts +0 -7
- package/lib/types.ts +6 -12
- package/package.json +1 -3
- package/server/api.ts +2 -23
- package/server/app-config.ts +34 -127
- package/server/applet-typecheck.ts +1 -6
- package/server/applets/build-applet.ts +0 -10
- package/server/cli.ts +12 -57
- package/server/layout.ts +3 -16
- package/server/skills-template.ts +2 -14
- package/server/web.ts +17 -62
- package/ui-components/avatar.tsx +4 -5
- package/ui-components/docs/avatar.md +5 -5
- package/client/features/collab/DevCollabIdentity.tsx +0 -170
- package/client/features/collab/DevCollabKit.tsx +0 -501
- package/client/features/collab/DevCollabPage.tsx +0 -25
- package/client/features/collab/WorkspaceCollabControls.tsx +0 -313
- package/client/features/collab/client.ts +0 -146
- package/client/features/collab/components.tsx +0 -307
- package/client/features/collab/dev-persona.ts +0 -66
- package/client/features/collab/engine.ts +0 -96
- package/client/features/collab/entry.tsx +0 -35
- package/client/features/collab/facehash-avatar.ts +0 -132
- package/client/features/collab/fake-engine.ts +0 -130
- package/client/features/collab/hooks.tsx +0 -253
- package/client/features/collab/identity.ts +0 -305
- package/client/features/collab/index.ts +0 -45
- package/client/features/collab/people.ts +0 -102
- package/client/features/collab/personal-state.ts +0 -61
- package/client/features/collab/presence-publisher.ts +0 -29
- package/client/features/collab/presence-target.ts +0 -10
- package/client/features/collab/primitives.tsx +0 -398
- package/client/features/collab/proxy-identity.ts +0 -24
- package/client/features/collab/store.ts +0 -156
- package/client/features/workspace/layout-save.ts +0 -13
- package/dist/chunk-1ftessc8.js +0 -2
- package/dist/chunk-3802vh2p.js +0 -2
- package/dist/chunk-542a2m9m.js +0 -1
- package/dist/chunk-5745d83v.js +0 -2
- package/dist/chunk-5zxdsjym.js +0 -1
- package/dist/chunk-7r4z77ry.js +0 -2
- package/dist/chunk-9cm0wjah.js +0 -79
- package/dist/chunk-cvgdm1hq.js +0 -2
- package/dist/chunk-gptm4jpd.js +0 -546
- package/dist/chunk-xaehhr1j.css +0 -1
- package/dist/chunk-xpt063fx.js +0 -1
- package/lib/collab/colors.ts +0 -24
- package/lib/collab/protocol.ts +0 -83
- package/lib/collab/types.ts +0 -63
- package/lib/experimental.ts +0 -14
- package/server/chat-selection.ts +0 -23
- package/server/collab/applet-module.ts +0 -50
- package/server/collab/cloudflare-access.ts +0 -77
- package/server/collab/config.ts +0 -18
- package/server/collab/ipc.ts +0 -10
- package/server/collab/manager.ts +0 -407
- package/server/collab/service.ts +0 -137
- package/server/collab/skill/collab-env.d.ts +0 -92
- package/server/collab/skill/index.ts +0 -59
- package/server/collab/worker.ts +0 -52
- package/workspace/.claude/skills/moi-workspace/references/COLLABORATIVE.md +0 -151
package/server/collab/manager.ts
DELETED
|
@@ -1,407 +0,0 @@
|
|
|
1
|
-
import { realpath } from 'node:fs/promises'
|
|
2
|
-
import { tmpdir } from 'node:os'
|
|
3
|
-
import { join, resolve } from 'node:path'
|
|
4
|
-
|
|
5
|
-
import { COLLAB_MAX_MESSAGE_BYTES, isCollabClientMessage } from '@/lib/collab/protocol'
|
|
6
|
-
import type { CollabClientMessage, CollabIdentity, CollabServerMessage } from '@/lib/collab/types'
|
|
7
|
-
|
|
8
|
-
import { isCollabEnabled } from './config'
|
|
9
|
-
import type { ParentMessage, WorkerMessage } from './ipc'
|
|
10
|
-
|
|
11
|
-
export type CollabSocket = {
|
|
12
|
-
send: (message: string) => number
|
|
13
|
-
close: (code?: number, reason?: string) => void
|
|
14
|
-
getBufferedAmount?: () => number
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
type Binding = {
|
|
18
|
-
socket: CollabSocket
|
|
19
|
-
workspacePaths: Set<string>
|
|
20
|
-
connectionId: string
|
|
21
|
-
closed: boolean
|
|
22
|
-
lastSeen: number
|
|
23
|
-
joined: boolean
|
|
24
|
-
// Set when a proxy (Cloudflare Access) verified who opened this socket.
|
|
25
|
-
identity?: CollabIdentity
|
|
26
|
-
slot?: Slot
|
|
27
|
-
queue: Promise<void>
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
type Slot = {
|
|
31
|
-
workspacePath: string
|
|
32
|
-
workspacePaths: Set<string>
|
|
33
|
-
generation: string
|
|
34
|
-
child: ReturnType<typeof Bun.spawn>
|
|
35
|
-
ready: Promise<void>
|
|
36
|
-
resolveReady: () => void
|
|
37
|
-
rejectReady: (error: Error) => void
|
|
38
|
-
readyTimer: ReturnType<typeof setTimeout>
|
|
39
|
-
stopping: boolean
|
|
40
|
-
clients: Map<string, Binding>
|
|
41
|
-
idleTimer?: ReturnType<typeof setTimeout>
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
type ManagerOptions = {
|
|
45
|
-
enabled?: (workspacePath: string) => Promise<boolean>
|
|
46
|
-
workerPath?: string
|
|
47
|
-
startupTimeoutMs?: number
|
|
48
|
-
idleTimeoutMs?: number
|
|
49
|
-
livenessTimeoutMs?: number
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export class CollabRuntimeError extends Error {
|
|
53
|
-
constructor(
|
|
54
|
-
public code: string,
|
|
55
|
-
message: string
|
|
56
|
-
) {
|
|
57
|
-
super(message)
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// A proxy-verified socket speaks only as the verified viewer. Another id, such
|
|
62
|
-
// as an outer host's own identity, is refused rather than rewritten, so peers
|
|
63
|
-
// never see someone other than the person the browser shows.
|
|
64
|
-
function verifiedMessage(
|
|
65
|
-
message: CollabClientMessage,
|
|
66
|
-
identity: CollabIdentity | undefined
|
|
67
|
-
): CollabClientMessage | null {
|
|
68
|
-
if (!identity || (message.type !== 'join' && message.type !== 'identity')) return message
|
|
69
|
-
return message.identity.id === identity.id ? { ...message, identity } : null
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// One owner map per moi server. No LRU: active workspaces cannot be evicted.
|
|
73
|
-
export class CollabManager {
|
|
74
|
-
private slots = new Map<string, Slot>()
|
|
75
|
-
private bindings = new Map<CollabSocket, Binding>()
|
|
76
|
-
private failures = new Map<string, { count: number; retryAfter: number }>()
|
|
77
|
-
private livenessTimer: ReturnType<typeof setInterval> | null = null
|
|
78
|
-
private closed = false
|
|
79
|
-
|
|
80
|
-
constructor(private options: ManagerOptions = {}) {}
|
|
81
|
-
|
|
82
|
-
private emit(binding: Binding, message: CollabServerMessage) {
|
|
83
|
-
if (binding.closed) return
|
|
84
|
-
const buffered = binding.socket.getBufferedAmount?.() ?? 0
|
|
85
|
-
if (buffered > 1024 * 1024) {
|
|
86
|
-
// A final leave or focus-clear snapshot might never be followed by another
|
|
87
|
-
// update. Rejoining must repair state even if the buffer drains before ping.
|
|
88
|
-
this.disconnect(binding, 'Collab updates need a fresh connection')
|
|
89
|
-
return
|
|
90
|
-
}
|
|
91
|
-
try {
|
|
92
|
-
if (binding.socket.send(JSON.stringify(message)) === 0)
|
|
93
|
-
this.disconnect(binding, 'Connection lost')
|
|
94
|
-
} catch {
|
|
95
|
-
this.disconnect(binding, 'Connection lost')
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private send(slot: Slot, message: ParentMessage) {
|
|
100
|
-
if (slot.stopping) throw new CollabRuntimeError('restarting', 'Collab is restarting')
|
|
101
|
-
slot.child.send(message)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
private async getSlot(workspacePath: string, binding: Binding): Promise<Slot> {
|
|
105
|
-
if (this.closed || binding.closed)
|
|
106
|
-
throw new CollabRuntimeError('unavailable', 'Collab connection closed')
|
|
107
|
-
const canonicalPath = await realpath(workspacePath)
|
|
108
|
-
binding.workspacePaths.add(canonicalPath)
|
|
109
|
-
if (binding.closed) throw new CollabRuntimeError('unavailable', 'Collab connection closed')
|
|
110
|
-
if (!(await (this.options.enabled ?? isCollabEnabled)(canonicalPath))) {
|
|
111
|
-
throw new CollabRuntimeError('unavailable', 'Start moi with --experimental-collab')
|
|
112
|
-
}
|
|
113
|
-
if (this.closed || binding.closed)
|
|
114
|
-
throw new CollabRuntimeError('unavailable', 'Collab connection closed')
|
|
115
|
-
const current = this.slots.get(canonicalPath)
|
|
116
|
-
if (current) {
|
|
117
|
-
if (current.stopping) {
|
|
118
|
-
await current.child.exited
|
|
119
|
-
if (this.slots.get(canonicalPath) === current) this.slots.delete(canonicalPath)
|
|
120
|
-
return this.getSlot(canonicalPath, binding)
|
|
121
|
-
}
|
|
122
|
-
if (current.idleTimer) clearTimeout(current.idleTimer)
|
|
123
|
-
current.idleTimer = undefined
|
|
124
|
-
current.workspacePaths.add(resolve(workspacePath))
|
|
125
|
-
return current
|
|
126
|
-
}
|
|
127
|
-
const failure = this.failures.get(canonicalPath)
|
|
128
|
-
if (failure && failure.retryAfter > Date.now()) {
|
|
129
|
-
throw new CollabRuntimeError('restarting', 'Collab is recovering; reconnect shortly')
|
|
130
|
-
}
|
|
131
|
-
const slot = this.spawn(canonicalPath)
|
|
132
|
-
slot.workspacePaths.add(resolve(workspacePath))
|
|
133
|
-
return slot
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
private spawn(workspacePath: string): Slot {
|
|
137
|
-
let resolveReady: () => void = () => {}
|
|
138
|
-
let rejectReady: (error: Error) => void = () => {}
|
|
139
|
-
const ready = new Promise<void>((resolve, reject) => {
|
|
140
|
-
resolveReady = resolve
|
|
141
|
-
rejectReady = reject
|
|
142
|
-
})
|
|
143
|
-
// Startup failure can precede a caller's await; callers still observe the original rejection.
|
|
144
|
-
void ready.catch(() => {})
|
|
145
|
-
const generation = crypto.randomUUID()
|
|
146
|
-
const slot: Slot = {
|
|
147
|
-
workspacePath,
|
|
148
|
-
workspacePaths: new Set([workspacePath]),
|
|
149
|
-
generation,
|
|
150
|
-
child: undefined as never,
|
|
151
|
-
ready,
|
|
152
|
-
resolveReady,
|
|
153
|
-
rejectReady,
|
|
154
|
-
readyTimer: setTimeout(() => {
|
|
155
|
-
slot.rejectReady(new CollabRuntimeError('startup_timeout', 'Collab did not start in time'))
|
|
156
|
-
this.stopSlot(slot, 'Collab startup timed out')
|
|
157
|
-
}, this.options.startupTimeoutMs ?? 10_000),
|
|
158
|
-
stopping: false,
|
|
159
|
-
clients: new Map()
|
|
160
|
-
}
|
|
161
|
-
try {
|
|
162
|
-
slot.child = Bun.spawn(
|
|
163
|
-
[process.execPath, this.options.workerPath ?? join(import.meta.dir, 'worker.ts')],
|
|
164
|
-
{
|
|
165
|
-
cwd: tmpdir(),
|
|
166
|
-
env: {
|
|
167
|
-
PATH: process.env.PATH,
|
|
168
|
-
MOI_COLLAB_WORKSPACE: workspacePath,
|
|
169
|
-
MOI_COLLAB_GENERATION: generation
|
|
170
|
-
},
|
|
171
|
-
stdout: 'inherit',
|
|
172
|
-
stderr: 'inherit',
|
|
173
|
-
ipc: (raw: unknown) => this.receive(slot, raw as WorkerMessage),
|
|
174
|
-
onExit: () => this.exited(slot)
|
|
175
|
-
}
|
|
176
|
-
)
|
|
177
|
-
this.slots.set(workspacePath, slot)
|
|
178
|
-
} catch (error) {
|
|
179
|
-
clearTimeout(slot.readyTimer)
|
|
180
|
-
rejectReady(error instanceof Error ? error : new Error('Collab could not start'))
|
|
181
|
-
throw error
|
|
182
|
-
}
|
|
183
|
-
return slot
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
private receive(slot: Slot, message: WorkerMessage) {
|
|
187
|
-
if (slot.stopping || message.generation !== slot.generation) return
|
|
188
|
-
if (message.type === 'ready') {
|
|
189
|
-
clearTimeout(slot.readyTimer)
|
|
190
|
-
slot.resolveReady()
|
|
191
|
-
return
|
|
192
|
-
}
|
|
193
|
-
if (message.type === 'client') {
|
|
194
|
-
const binding = slot.clients.get(message.connectionId)
|
|
195
|
-
if (binding) this.emit(binding, message.message)
|
|
196
|
-
return
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
private exited(slot: Slot) {
|
|
201
|
-
const expected = slot.stopping
|
|
202
|
-
slot.stopping = true
|
|
203
|
-
clearTimeout(slot.readyTimer)
|
|
204
|
-
if (slot.idleTimer) clearTimeout(slot.idleTimer)
|
|
205
|
-
const error = new CollabRuntimeError(
|
|
206
|
-
'worker_exited',
|
|
207
|
-
'Collab restarted; reconnect to restore presence'
|
|
208
|
-
)
|
|
209
|
-
slot.rejectReady(error)
|
|
210
|
-
for (const binding of [...slot.clients.values()]) this.disconnect(binding, error.message)
|
|
211
|
-
if (this.slots.get(slot.workspacePath) === slot) this.slots.delete(slot.workspacePath)
|
|
212
|
-
if (!expected) {
|
|
213
|
-
const count = (this.failures.get(slot.workspacePath)?.count ?? 0) + 1
|
|
214
|
-
this.failures.set(slot.workspacePath, {
|
|
215
|
-
count,
|
|
216
|
-
retryAfter: Date.now() + Math.min(250 * 2 ** Math.min(count - 1, 7), 30_000)
|
|
217
|
-
})
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
private scheduleIdle(slot: Slot) {
|
|
222
|
-
if (slot.stopping || slot.clients.size || slot.idleTimer) return
|
|
223
|
-
slot.idleTimer = setTimeout(
|
|
224
|
-
() => this.stopSlot(slot, 'Collab workspace is idle'),
|
|
225
|
-
this.options.idleTimeoutMs ?? 60_000
|
|
226
|
-
)
|
|
227
|
-
slot.idleTimer.unref()
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
private stopSlot(slot: Slot, reason: string) {
|
|
231
|
-
if (slot.stopping) return
|
|
232
|
-
slot.stopping = true
|
|
233
|
-
clearTimeout(slot.readyTimer)
|
|
234
|
-
slot.rejectReady(new CollabRuntimeError('restarting', reason))
|
|
235
|
-
for (const binding of [...slot.clients.values()]) this.disconnect(binding, reason)
|
|
236
|
-
try {
|
|
237
|
-
slot.child.send({ type: 'shutdown' } satisfies ParentMessage)
|
|
238
|
-
} catch {}
|
|
239
|
-
const deadline = setTimeout(() => slot.child.kill('SIGKILL'), 2000)
|
|
240
|
-
deadline.unref()
|
|
241
|
-
void slot.child.exited.finally(() => clearTimeout(deadline))
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
private disconnect(binding: Binding, reason: string) {
|
|
245
|
-
if (binding.closed) return
|
|
246
|
-
this.close(binding.socket)
|
|
247
|
-
binding.socket.close(1012, reason.slice(0, 120))
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
open(socket: CollabSocket, workspacePath: string, identity?: CollabIdentity) {
|
|
251
|
-
const binding: Binding = {
|
|
252
|
-
socket,
|
|
253
|
-
workspacePaths: new Set([resolve(workspacePath)]),
|
|
254
|
-
connectionId: crypto.randomUUID(),
|
|
255
|
-
closed: false,
|
|
256
|
-
lastSeen: Date.now(),
|
|
257
|
-
joined: false,
|
|
258
|
-
identity,
|
|
259
|
-
queue: Promise.resolve()
|
|
260
|
-
}
|
|
261
|
-
this.bindings.set(socket, binding)
|
|
262
|
-
binding.queue = this.getSlot(workspacePath, binding)
|
|
263
|
-
.then(async slot => {
|
|
264
|
-
if (binding.closed) {
|
|
265
|
-
this.scheduleIdle(slot)
|
|
266
|
-
return
|
|
267
|
-
}
|
|
268
|
-
binding.slot = slot
|
|
269
|
-
slot.clients.set(binding.connectionId, binding)
|
|
270
|
-
await slot.ready
|
|
271
|
-
})
|
|
272
|
-
.catch(error => {
|
|
273
|
-
this.emit(binding, {
|
|
274
|
-
type: 'error',
|
|
275
|
-
code: 'unavailable',
|
|
276
|
-
message: error instanceof Error ? error.message : 'Collab unavailable'
|
|
277
|
-
})
|
|
278
|
-
this.disconnect(binding, 'Collab unavailable')
|
|
279
|
-
})
|
|
280
|
-
this.livenessTimer ??= setInterval(() => {
|
|
281
|
-
const now = Date.now()
|
|
282
|
-
for (const client of this.bindings.values()) {
|
|
283
|
-
const timeout = client.joined ? (this.options.livenessTimeoutMs ?? 60_000) : 15_000
|
|
284
|
-
if (now - client.lastSeen > timeout) this.disconnect(client, 'Collab connection timed out')
|
|
285
|
-
}
|
|
286
|
-
}, 5000)
|
|
287
|
-
this.livenessTimer.unref()
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
message(socket: CollabSocket, raw: string | Uint8Array) {
|
|
291
|
-
const binding = this.bindings.get(socket)
|
|
292
|
-
if (!binding || binding.closed) return
|
|
293
|
-
const size = typeof raw === 'string' ? new TextEncoder().encode(raw).length : raw.byteLength
|
|
294
|
-
if (size > COLLAB_MAX_MESSAGE_BYTES) {
|
|
295
|
-
this.disconnect(binding, 'Collab message is too large')
|
|
296
|
-
return
|
|
297
|
-
}
|
|
298
|
-
let message: unknown
|
|
299
|
-
try {
|
|
300
|
-
message = JSON.parse(typeof raw === 'string' ? raw : new TextDecoder().decode(raw))
|
|
301
|
-
} catch {}
|
|
302
|
-
if (!isCollabClientMessage(message)) {
|
|
303
|
-
this.emit(binding, {
|
|
304
|
-
type: 'error',
|
|
305
|
-
code: 'invalid_message',
|
|
306
|
-
message: 'Invalid collab message'
|
|
307
|
-
})
|
|
308
|
-
return
|
|
309
|
-
}
|
|
310
|
-
binding.lastSeen = Date.now()
|
|
311
|
-
const validated = verifiedMessage(message, binding.identity)
|
|
312
|
-
if (!validated) {
|
|
313
|
-
this.emit(binding, {
|
|
314
|
-
type: 'error',
|
|
315
|
-
code: 'identity_mismatch',
|
|
316
|
-
message: 'Presence here uses your Cloudflare Access identity'
|
|
317
|
-
})
|
|
318
|
-
this.disconnect(binding, 'Identity does not match Cloudflare Access')
|
|
319
|
-
return
|
|
320
|
-
}
|
|
321
|
-
binding.queue = binding.queue
|
|
322
|
-
.then(() => {
|
|
323
|
-
if (binding.closed || !binding.slot) return
|
|
324
|
-
if (validated.type === 'join') binding.joined = true
|
|
325
|
-
this.send(binding.slot, {
|
|
326
|
-
type: 'client',
|
|
327
|
-
connectionId: binding.connectionId,
|
|
328
|
-
message: validated
|
|
329
|
-
})
|
|
330
|
-
})
|
|
331
|
-
.catch(() => this.disconnect(binding, 'Collab restarted'))
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
close(socket: CollabSocket) {
|
|
335
|
-
const binding = this.bindings.get(socket)
|
|
336
|
-
if (!binding) return
|
|
337
|
-
binding.closed = true
|
|
338
|
-
this.bindings.delete(socket)
|
|
339
|
-
if (!this.bindings.size && this.livenessTimer) {
|
|
340
|
-
clearInterval(this.livenessTimer)
|
|
341
|
-
this.livenessTimer = null
|
|
342
|
-
}
|
|
343
|
-
const slot = binding.slot
|
|
344
|
-
if (!slot) return
|
|
345
|
-
slot.clients.delete(binding.connectionId)
|
|
346
|
-
if (!slot.stopping) {
|
|
347
|
-
try {
|
|
348
|
-
this.send(slot, { type: 'leave', connectionId: binding.connectionId })
|
|
349
|
-
} catch {}
|
|
350
|
-
}
|
|
351
|
-
this.scheduleIdle(slot)
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
async stopWorkspace(workspacePath: string) {
|
|
355
|
-
// Remember aliases so removing a missing directory or symlink can still stop its room.
|
|
356
|
-
const knownPath = resolve(workspacePath)
|
|
357
|
-
let slot = [...this.slots.values()].find(slot => slot.workspacePaths.has(knownPath))
|
|
358
|
-
const disconnectPending = (path: string) => {
|
|
359
|
-
for (const binding of this.bindings.values()) {
|
|
360
|
-
if (binding.workspacePaths.has(path)) {
|
|
361
|
-
this.disconnect(binding, 'Collab stopped for this workspace')
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
disconnectPending(knownPath)
|
|
366
|
-
if (slot) disconnectPending(slot.workspacePath)
|
|
367
|
-
if (!slot) {
|
|
368
|
-
try {
|
|
369
|
-
const canonicalPath = await realpath(workspacePath)
|
|
370
|
-
disconnectPending(canonicalPath)
|
|
371
|
-
slot = this.slots.get(canonicalPath)
|
|
372
|
-
} catch (error) {
|
|
373
|
-
if (
|
|
374
|
-
error instanceof Error &&
|
|
375
|
-
'code' in error &&
|
|
376
|
-
(error.code === 'ENOENT' || error.code === 'ENOTDIR')
|
|
377
|
-
)
|
|
378
|
-
return
|
|
379
|
-
throw error
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
if (!slot) return
|
|
383
|
-
this.stopSlot(slot, 'Collab stopped for this workspace')
|
|
384
|
-
await slot.child.exited
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
async shutdown() {
|
|
388
|
-
this.closed = true
|
|
389
|
-
if (this.livenessTimer) clearInterval(this.livenessTimer)
|
|
390
|
-
this.livenessTimer = null
|
|
391
|
-
const slots = [...this.slots.values()]
|
|
392
|
-
for (const slot of slots) this.stopSlot(slot, 'moi is shutting down')
|
|
393
|
-
await Promise.allSettled(slots.map(slot => slot.child.exited))
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
debugSnapshot() {
|
|
397
|
-
return [...this.slots.values()].map(slot => ({
|
|
398
|
-
workspacePath: slot.workspacePath,
|
|
399
|
-
pid: slot.child.pid,
|
|
400
|
-
generation: slot.generation,
|
|
401
|
-
connections: slot.clients.size,
|
|
402
|
-
stopping: slot.stopping
|
|
403
|
-
}))
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
export const collabManager = new CollabManager()
|
package/server/collab/service.ts
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
COLLAB_MAX_CONNECTIONS,
|
|
3
|
-
COLLAB_MAX_REGISTRATIONS,
|
|
4
|
-
COLLAB_PROTOCOL_VERSION
|
|
5
|
-
} from '@/lib/collab/protocol'
|
|
6
|
-
import type {
|
|
7
|
-
CollabClientMessage,
|
|
8
|
-
CollabIdentity,
|
|
9
|
-
CollabParticipant,
|
|
10
|
-
CollabServerMessage
|
|
11
|
-
} from '@/lib/collab/types'
|
|
12
|
-
|
|
13
|
-
type Emit = (connectionId: string, message: CollabServerMessage) => void
|
|
14
|
-
type Client = {
|
|
15
|
-
identity: CollabIdentity
|
|
16
|
-
participant: CollabParticipant
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function cleanIdentity(identity: CollabIdentity): CollabIdentity {
|
|
20
|
-
return {
|
|
21
|
-
id: identity.id,
|
|
22
|
-
...(identity.name !== undefined ? { name: identity.name } : {}),
|
|
23
|
-
color: identity.color,
|
|
24
|
-
...(identity.avatar !== undefined ? { avatar: identity.avatar } : {}),
|
|
25
|
-
...(identity.email !== undefined ? { email: identity.email } : {})
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// One process owns this ephemeral room. No workspace data is read or written.
|
|
30
|
-
export class CollabService {
|
|
31
|
-
private clients = new Map<string, Client>()
|
|
32
|
-
private presenceTimer: ReturnType<typeof setTimeout> | null = null
|
|
33
|
-
|
|
34
|
-
constructor(private emit: Emit) {}
|
|
35
|
-
|
|
36
|
-
private snapshot() {
|
|
37
|
-
const users = new Map<string, CollabIdentity>()
|
|
38
|
-
const participants: CollabParticipant[] = []
|
|
39
|
-
for (const { identity, participant } of this.clients.values()) {
|
|
40
|
-
users.set(identity.id, identity)
|
|
41
|
-
participants.push(participant)
|
|
42
|
-
}
|
|
43
|
-
return { participants, users: [...users.values()] }
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
private publishParticipants() {
|
|
47
|
-
if (this.presenceTimer) clearTimeout(this.presenceTimer)
|
|
48
|
-
this.presenceTimer = null
|
|
49
|
-
const message: CollabServerMessage = { type: 'participants', ...this.snapshot() }
|
|
50
|
-
for (const id of this.clients.keys()) this.emit(id, message)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
private scheduleParticipants() {
|
|
54
|
-
this.presenceTimer ??= setTimeout(() => this.publishParticipants(), 50)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
receive(connectionId: string, message: CollabClientMessage) {
|
|
58
|
-
if (message.type === 'join') {
|
|
59
|
-
if (this.clients.has(connectionId)) throw new Error('This connection already joined')
|
|
60
|
-
if (this.clients.size >= COLLAB_MAX_CONNECTIONS)
|
|
61
|
-
throw new Error('This workspace has too many connections')
|
|
62
|
-
const identity = cleanIdentity(message.identity)
|
|
63
|
-
// Keep one current profile across a user's tabs, without sharing their presence.
|
|
64
|
-
for (const client of this.clients.values()) {
|
|
65
|
-
if (client.identity.id === identity.id) client.identity = identity
|
|
66
|
-
}
|
|
67
|
-
this.clients.set(connectionId, {
|
|
68
|
-
identity,
|
|
69
|
-
participant: {
|
|
70
|
-
connectionId,
|
|
71
|
-
userId: identity.id,
|
|
72
|
-
location: message.location ?? null,
|
|
73
|
-
presence: []
|
|
74
|
-
}
|
|
75
|
-
})
|
|
76
|
-
this.emit(connectionId, {
|
|
77
|
-
type: 'welcome',
|
|
78
|
-
version: COLLAB_PROTOCOL_VERSION,
|
|
79
|
-
connectionId,
|
|
80
|
-
...this.snapshot()
|
|
81
|
-
})
|
|
82
|
-
this.publishParticipants()
|
|
83
|
-
return
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const client = this.clients.get(connectionId)
|
|
87
|
-
if (!client) throw new Error('Join the workspace before sending collab messages')
|
|
88
|
-
const { participant } = client
|
|
89
|
-
switch (message.type) {
|
|
90
|
-
case 'identity': {
|
|
91
|
-
if (message.identity.id !== participant.userId)
|
|
92
|
-
throw new Error('Reconnect to change identity')
|
|
93
|
-
const identity = cleanIdentity(message.identity)
|
|
94
|
-
for (const other of this.clients.values()) {
|
|
95
|
-
if (other.identity.id === identity.id) other.identity = identity
|
|
96
|
-
}
|
|
97
|
-
this.publishParticipants()
|
|
98
|
-
return
|
|
99
|
-
}
|
|
100
|
-
case 'location':
|
|
101
|
-
participant.location = message.location
|
|
102
|
-
this.scheduleParticipants()
|
|
103
|
-
return
|
|
104
|
-
case 'presence:set': {
|
|
105
|
-
const presence = participant.presence
|
|
106
|
-
const index = presence.findIndex(item => item.registrationId === message.registrationId)
|
|
107
|
-
if (index < 0 && presence.length >= COLLAB_MAX_REGISTRATIONS)
|
|
108
|
-
throw new Error('Too many presence registrations')
|
|
109
|
-
const { registrationId, surface, channel, value } = message
|
|
110
|
-
const registration = { registrationId, surface, channel, value }
|
|
111
|
-
if (index >= 0) presence[index] = registration
|
|
112
|
-
else presence.push(registration)
|
|
113
|
-
this.scheduleParticipants()
|
|
114
|
-
return
|
|
115
|
-
}
|
|
116
|
-
case 'presence:delete':
|
|
117
|
-
participant.presence = participant.presence.filter(
|
|
118
|
-
item => item.registrationId !== message.registrationId
|
|
119
|
-
)
|
|
120
|
-
this.scheduleParticipants()
|
|
121
|
-
return
|
|
122
|
-
case 'ping':
|
|
123
|
-
this.emit(connectionId, { type: 'pong' })
|
|
124
|
-
return
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
leave(connectionId: string) {
|
|
129
|
-
if (this.clients.delete(connectionId)) this.publishParticipants()
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
close() {
|
|
133
|
-
if (this.presenceTimer) clearTimeout(this.presenceTimer)
|
|
134
|
-
this.presenceTimer = null
|
|
135
|
-
this.clients.clear()
|
|
136
|
-
}
|
|
137
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
// Generated by `moi init --experimental-collab`; do not edit this file.
|
|
2
|
-
declare module 'moi/collab' {
|
|
3
|
-
import type { ReactElement, ReactNode } from 'react'
|
|
4
|
-
|
|
5
|
-
export type CollabJsonValue =
|
|
6
|
-
| null
|
|
7
|
-
| boolean
|
|
8
|
-
| number
|
|
9
|
-
| string
|
|
10
|
-
| CollabJsonValue[]
|
|
11
|
-
| { [key: string]: CollabJsonValue }
|
|
12
|
-
|
|
13
|
-
export type CollabIdentity = {
|
|
14
|
-
id: string
|
|
15
|
-
// May be omitted or empty; display with email or id as a fallback.
|
|
16
|
-
name?: string
|
|
17
|
-
color: string
|
|
18
|
-
avatar?: string
|
|
19
|
-
email?: string
|
|
20
|
-
}
|
|
21
|
-
export type UserStatus = 'active' | 'away' | 'offline'
|
|
22
|
-
export type CollabUser = CollabIdentity & { status: UserStatus }
|
|
23
|
-
export type PeersOptions = { scope?: 'page' | 'workspace'; status?: 'active' | 'away' }
|
|
24
|
-
export type WorkspaceUsersOptions = { status?: UserStatus }
|
|
25
|
-
|
|
26
|
-
// Peers are connected users, deduplicated across tabs, excluding your own user.
|
|
27
|
-
export function useMe(): CollabUser | null
|
|
28
|
-
export function useUser(id: string): CollabUser | null
|
|
29
|
-
export function usePeers(options?: PeersOptions): CollabUser[]
|
|
30
|
-
// Full workspace directory, including your own user and offline members.
|
|
31
|
-
export function useWorkspaceUsers(options?: WorkspaceUsersOptions): CollabUser[]
|
|
32
|
-
// Unavailable uses live/dev profiles; loading differs from a ready empty directory.
|
|
33
|
-
export type WorkspaceUsersStatus = 'unavailable' | 'loading' | 'ready'
|
|
34
|
-
export function useWorkspaceUsersStatus(): WorkspaceUsersStatus
|
|
35
|
-
|
|
36
|
-
export type PresenceValue<T> = { connectionId: string; userId: string; value: T }
|
|
37
|
-
// Reading never registers or publishes presence. Values belong to connections.
|
|
38
|
-
export function usePresence<T extends CollabJsonValue>(channel: string): PresenceValue<T>[]
|
|
39
|
-
// Publishes the current value reactively, releasing it on hide or unmount.
|
|
40
|
-
export function usePublishPresence<T extends CollabJsonValue>(channel: string, value: T): void
|
|
41
|
-
|
|
42
|
-
export type ActivityProps = { scope?: 'page' | 'workspace'; className?: string }
|
|
43
|
-
export function Activity(props: ActivityProps): ReactElement
|
|
44
|
-
export type CursorsProps = { surface?: string; children: ReactNode; className?: string }
|
|
45
|
-
export function Cursors(props: CursorsProps): ReactElement
|
|
46
|
-
|
|
47
|
-
// Stable local IDs compose with enclosing PresenceGroup IDs across browsers.
|
|
48
|
-
// Each focus wrapper accepts one element (not a fragment) and tracks its descendants.
|
|
49
|
-
export type PresenceFrameProps = {
|
|
50
|
-
id: string
|
|
51
|
-
children: ReactElement
|
|
52
|
-
className?: string
|
|
53
|
-
}
|
|
54
|
-
export function PresenceFrame(props: PresenceFrameProps): ReactElement
|
|
55
|
-
export type PresenceGutterProps = {
|
|
56
|
-
id: string
|
|
57
|
-
children: ReactElement
|
|
58
|
-
className?: string
|
|
59
|
-
}
|
|
60
|
-
export function PresenceGutter(props: PresenceGutterProps): ReactElement
|
|
61
|
-
// Adds a namespace and a local gutter animation group; no DOM or publication.
|
|
62
|
-
export type PresenceGroupProps = { id: string; children: ReactNode }
|
|
63
|
-
export function PresenceGroup(props: PresenceGroupProps): ReactElement
|
|
64
|
-
export type SelectionProps = {
|
|
65
|
-
id: string
|
|
66
|
-
selected: boolean
|
|
67
|
-
children: ReactNode
|
|
68
|
-
className?: string
|
|
69
|
-
}
|
|
70
|
-
export function Selection(props: SelectionProps): ReactElement
|
|
71
|
-
|
|
72
|
-
export type UserSize = 'xs' | 'sm' | 'md' | 'lg'
|
|
73
|
-
export type UserProps = {
|
|
74
|
-
id: string
|
|
75
|
-
size?: UserSize
|
|
76
|
-
avatarOnly?: boolean
|
|
77
|
-
you?: boolean
|
|
78
|
-
detail?: ReactNode
|
|
79
|
-
showStatus?: boolean
|
|
80
|
-
label?: string
|
|
81
|
-
className?: string
|
|
82
|
-
}
|
|
83
|
-
export function User(props: UserProps): ReactElement
|
|
84
|
-
export type FacepileProps = {
|
|
85
|
-
ids: readonly string[]
|
|
86
|
-
max?: number
|
|
87
|
-
size?: 'xs' | 'sm' | 'md'
|
|
88
|
-
showStatus?: boolean
|
|
89
|
-
className?: string
|
|
90
|
-
}
|
|
91
|
-
export function Facepile(props: FacepileProps): ReactElement
|
|
92
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { mkdir, rm } from 'node:fs/promises'
|
|
2
|
-
import { dirname, join } from 'node:path'
|
|
3
|
-
|
|
4
|
-
import type { WorkspaceType } from '@/lib/types'
|
|
5
|
-
|
|
6
|
-
import { COLLAB_REFERENCE_SOURCE_PATH } from '../../skills-template'
|
|
7
|
-
import { skillsDirFor } from '../../workspace-init'
|
|
8
|
-
|
|
9
|
-
const TYPES_SOURCE_PATH = join(import.meta.dir, 'collab-env.d.ts')
|
|
10
|
-
|
|
11
|
-
export type CollabSkillPaths = { referencePath: string; typesPath: string }
|
|
12
|
-
|
|
13
|
-
export function collabSkillReferencePath(workspacePath: string, type?: WorkspaceType): string {
|
|
14
|
-
return join(skillsDirFor(workspacePath, type), 'moi-workspace', 'references', 'COLLABORATIVE.md')
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function targetPaths(workspacePath: string, type?: WorkspaceType): CollabSkillPaths {
|
|
18
|
-
return {
|
|
19
|
-
referencePath: collabSkillReferencePath(workspacePath, type),
|
|
20
|
-
typesPath: join(workspacePath, '.moi', 'collab-env.d.ts')
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async function writeChanged(path: string, contents: string): Promise<void> {
|
|
25
|
-
const file = Bun.file(path)
|
|
26
|
-
if ((await file.exists()) && (await file.text()) === contents) return
|
|
27
|
-
await mkdir(dirname(path), { recursive: true })
|
|
28
|
-
await Bun.write(path, contents)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Only an explicit `moi init --experimental-collab` installs the guide and types.
|
|
32
|
-
export async function installCollabSkill(
|
|
33
|
-
workspacePath: string,
|
|
34
|
-
type?: WorkspaceType
|
|
35
|
-
): Promise<CollabSkillPaths> {
|
|
36
|
-
const source = Bun.file(COLLAB_REFERENCE_SOURCE_PATH)
|
|
37
|
-
if (!(await source.exists())) {
|
|
38
|
-
throw new Error(
|
|
39
|
-
'The collaboration guide is unavailable in this build: COLLABORATIVE.md is missing.'
|
|
40
|
-
)
|
|
41
|
-
}
|
|
42
|
-
const [reference, declarations] = await Promise.all([
|
|
43
|
-
source.text(),
|
|
44
|
-
Bun.file(TYPES_SOURCE_PATH).text()
|
|
45
|
-
])
|
|
46
|
-
const paths = targetPaths(workspacePath, type)
|
|
47
|
-
await writeChanged(paths.referencePath, reference)
|
|
48
|
-
await writeChanged(paths.typesPath, declarations)
|
|
49
|
-
return paths
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export async function removeCollabSkill(
|
|
53
|
-
workspacePath: string,
|
|
54
|
-
type?: WorkspaceType
|
|
55
|
-
): Promise<void> {
|
|
56
|
-
const paths = targetPaths(workspacePath, type)
|
|
57
|
-
await rm(paths.referencePath, { force: true })
|
|
58
|
-
await rm(paths.typesPath, { force: true })
|
|
59
|
-
}
|