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
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Fragment,
|
|
3
|
-
createContext,
|
|
4
|
-
isValidElement,
|
|
5
|
-
useContext,
|
|
6
|
-
useId,
|
|
7
|
-
useLayoutEffect,
|
|
8
|
-
useMemo,
|
|
9
|
-
useRef
|
|
10
|
-
} from 'react'
|
|
11
|
-
import type { FocusEvent, PointerEvent, ReactElement, ReactNode, RefObject } from 'react'
|
|
12
|
-
|
|
13
|
-
import { IconCursorText, IconPointer } from '@tabler/icons-react'
|
|
14
|
-
import { LayoutGroup, MotionConfig } from 'motion/react'
|
|
15
|
-
|
|
16
|
-
import { cn } from '@/client/lib/cn'
|
|
17
|
-
import type { CollabJsonValue } from '@/lib/collab/types'
|
|
18
|
-
|
|
19
|
-
import {
|
|
20
|
-
presenceChannels,
|
|
21
|
-
useMe,
|
|
22
|
-
usePeers,
|
|
23
|
-
usePresenceChannel,
|
|
24
|
-
usePresencePublisher,
|
|
25
|
-
usePublishPresenceChannel
|
|
26
|
-
} from './hooks'
|
|
27
|
-
import { Cursor, Facepile, PresenceFramePrimitive, PresenceGutterPrimitive } from './primitives'
|
|
28
|
-
import { presenceTarget } from './presence-target'
|
|
29
|
-
|
|
30
|
-
export { Facepile, User } from './primitives'
|
|
31
|
-
|
|
32
|
-
// Built-in indicators have no registration while unfocused, unselected, or absent.
|
|
33
|
-
const hasPresence = (value: CollabJsonValue) => value !== false && value !== null
|
|
34
|
-
|
|
35
|
-
export type ActivityProps = { scope?: 'page' | 'workspace'; className?: string }
|
|
36
|
-
export function Activity({ scope = 'page', className }: ActivityProps) {
|
|
37
|
-
const peers = usePeers({ scope })
|
|
38
|
-
const me = useMe()
|
|
39
|
-
const users = me ? [me, ...peers] : peers
|
|
40
|
-
return <Facepile ids={users.map(user => user.id)} max={users.length} className={className} />
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
type PointerPosition = { x: number; y: number; target?: string; targetX?: number; targetY?: number }
|
|
44
|
-
function pointerPosition(value: CollabJsonValue): PointerPosition | null {
|
|
45
|
-
if (!value || typeof value !== 'object' || Array.isArray(value)) return null
|
|
46
|
-
if (typeof value.x !== 'number' || typeof value.y !== 'number') return null
|
|
47
|
-
if (!Number.isFinite(value.x) || !Number.isFinite(value.y)) return null
|
|
48
|
-
return {
|
|
49
|
-
x: value.x,
|
|
50
|
-
y: value.y,
|
|
51
|
-
...(typeof value.target === 'string' ? { target: value.target } : {}),
|
|
52
|
-
...(typeof value.targetX === 'number' && Number.isFinite(value.targetX)
|
|
53
|
-
? { targetX: value.targetX }
|
|
54
|
-
: {}),
|
|
55
|
-
...(typeof value.targetY === 'number' && Number.isFinite(value.targetY)
|
|
56
|
-
? { targetY: value.targetY }
|
|
57
|
-
: {})
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export type CursorsProps = { surface?: string; children: ReactNode; className?: string }
|
|
62
|
-
export function Cursors({ surface = 'default', children, className }: CursorsProps) {
|
|
63
|
-
const root = useRef<HTMLDivElement>(null)
|
|
64
|
-
const channel = presenceChannels.cursor(surface)
|
|
65
|
-
const cursors = usePresenceChannel<CollabJsonValue>(channel)
|
|
66
|
-
const publish = usePresencePublisher<CollabJsonValue>(channel, null, hasPresence)
|
|
67
|
-
const byConnection = new Map<string, { id: string; point: PointerPosition }>()
|
|
68
|
-
for (const { connectionId, userId, value } of cursors) {
|
|
69
|
-
const point = pointerPosition(value)
|
|
70
|
-
if (point) byConnection.set(connectionId, { id: userId, point })
|
|
71
|
-
}
|
|
72
|
-
const move = (event: PointerEvent<HTMLDivElement>) => {
|
|
73
|
-
if (event.pointerType === 'touch') return
|
|
74
|
-
const element = root.current
|
|
75
|
-
if (
|
|
76
|
-
!element ||
|
|
77
|
-
!(event.target instanceof Element) ||
|
|
78
|
-
event.target.closest('[data-collab-cursors]') !== element
|
|
79
|
-
)
|
|
80
|
-
return
|
|
81
|
-
const bounds = element.getBoundingClientRect()
|
|
82
|
-
const target =
|
|
83
|
-
event.target instanceof Element
|
|
84
|
-
? event.target.closest<HTMLElement>('[data-collab-target]')
|
|
85
|
-
: null
|
|
86
|
-
const anchor = target && element.contains(target) ? target : null
|
|
87
|
-
const rect = anchor?.getBoundingClientRect()
|
|
88
|
-
publish({
|
|
89
|
-
x: event.clientX - bounds.left + element.scrollLeft,
|
|
90
|
-
y: event.clientY - bounds.top + element.scrollTop,
|
|
91
|
-
...(anchor && rect
|
|
92
|
-
? {
|
|
93
|
-
target: anchor.dataset.collabTarget ?? '',
|
|
94
|
-
targetX: rect.width ? (event.clientX - rect.left) / rect.width : 0,
|
|
95
|
-
targetY: rect.height ? (event.clientY - rect.top) / rect.height : 0
|
|
96
|
-
}
|
|
97
|
-
: {})
|
|
98
|
-
})
|
|
99
|
-
}
|
|
100
|
-
return (
|
|
101
|
-
<div
|
|
102
|
-
ref={root}
|
|
103
|
-
data-collab-cursors={surface}
|
|
104
|
-
className={cn('relative', className)}
|
|
105
|
-
onPointerMove={move}
|
|
106
|
-
onPointerLeave={event => {
|
|
107
|
-
if (
|
|
108
|
-
event.target instanceof Element &&
|
|
109
|
-
event.target.closest('[data-collab-cursors]') === event.currentTarget
|
|
110
|
-
)
|
|
111
|
-
publish(null)
|
|
112
|
-
}}
|
|
113
|
-
>
|
|
114
|
-
{children}
|
|
115
|
-
<div className="pointer-events-none absolute inset-0 overflow-hidden" aria-hidden="true">
|
|
116
|
-
{[...byConnection].map(([connectionId, { id, point }]) => (
|
|
117
|
-
<RemoteCursor key={connectionId} root={root} point={point} id={id} />
|
|
118
|
-
))}
|
|
119
|
-
</div>
|
|
120
|
-
</div>
|
|
121
|
-
)
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
type RemoteCursorProps = {
|
|
125
|
-
root: RefObject<HTMLDivElement | null>
|
|
126
|
-
point: PointerPosition
|
|
127
|
-
id: string
|
|
128
|
-
}
|
|
129
|
-
// Semantic anchors follow their own data through scrolling and reordering.
|
|
130
|
-
// A cursor on a different record's modal has no matching anchor and stays hidden.
|
|
131
|
-
function RemoteCursor({ root, point, id }: RemoteCursorProps) {
|
|
132
|
-
const marker = useRef<HTMLSpanElement>(null)
|
|
133
|
-
useLayoutEffect(() => {
|
|
134
|
-
const element = root.current
|
|
135
|
-
const node = marker.current
|
|
136
|
-
if (!element || !node) return
|
|
137
|
-
const position = () => {
|
|
138
|
-
let x = point.x - element.scrollLeft
|
|
139
|
-
let y = point.y - element.scrollTop
|
|
140
|
-
if (point.target) {
|
|
141
|
-
const target = [...element.querySelectorAll<HTMLElement>('[data-collab-target]')].find(
|
|
142
|
-
candidate => candidate.dataset.collabTarget === point.target
|
|
143
|
-
)
|
|
144
|
-
if (!target) {
|
|
145
|
-
node.hidden = true
|
|
146
|
-
return
|
|
147
|
-
}
|
|
148
|
-
const bounds = element.getBoundingClientRect()
|
|
149
|
-
const rect = target.getBoundingClientRect()
|
|
150
|
-
x = rect.left - bounds.left + rect.width * (point.targetX ?? 0)
|
|
151
|
-
y = rect.top - bounds.top + rect.height * (point.targetY ?? 0)
|
|
152
|
-
}
|
|
153
|
-
node.hidden = false
|
|
154
|
-
node.style.transform = `translate(${x}px, ${y}px)`
|
|
155
|
-
}
|
|
156
|
-
position()
|
|
157
|
-
const observer = new ResizeObserver(position)
|
|
158
|
-
observer.observe(element)
|
|
159
|
-
const changes = new MutationObserver(position)
|
|
160
|
-
changes.observe(element, {
|
|
161
|
-
childList: true,
|
|
162
|
-
subtree: true,
|
|
163
|
-
attributes: true,
|
|
164
|
-
attributeFilter: ['data-collab-target']
|
|
165
|
-
})
|
|
166
|
-
element.addEventListener('scroll', position, true)
|
|
167
|
-
window.addEventListener('resize', position)
|
|
168
|
-
return () => {
|
|
169
|
-
observer.disconnect()
|
|
170
|
-
changes.disconnect()
|
|
171
|
-
element.removeEventListener('scroll', position, true)
|
|
172
|
-
window.removeEventListener('resize', position)
|
|
173
|
-
}
|
|
174
|
-
}, [root, point])
|
|
175
|
-
return <Cursor ref={marker} id={id} />
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const PresenceGroupContext = createContext<readonly string[]>([])
|
|
179
|
-
export type PresenceGroupProps = { id: string; children: ReactNode }
|
|
180
|
-
export function PresenceGroup({ id, children }: PresenceGroupProps) {
|
|
181
|
-
const parent = useContext(PresenceGroupContext)
|
|
182
|
-
const scope = useMemo(() => {
|
|
183
|
-
presenceTarget(id)
|
|
184
|
-
return [...parent, id]
|
|
185
|
-
}, [parent, id])
|
|
186
|
-
// The shared path identifies data across browsers; the generated ID keeps
|
|
187
|
-
// avatar animations inside this particular rendered group.
|
|
188
|
-
const animationId = useId()
|
|
189
|
-
return (
|
|
190
|
-
<MotionConfig reducedMotion="user">
|
|
191
|
-
<LayoutGroup id={animationId}>
|
|
192
|
-
<PresenceGroupContext value={scope}>{children}</PresenceGroupContext>
|
|
193
|
-
</LayoutGroup>
|
|
194
|
-
</MotionConfig>
|
|
195
|
-
)
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function usePresenceTarget(id: string): string {
|
|
199
|
-
return presenceTarget(...useContext(PresenceGroupContext), id)
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
function presenceChild(children: ReactNode): ReactElement {
|
|
203
|
-
if (!isValidElement(children) || children.type === Fragment) {
|
|
204
|
-
throw new Error(
|
|
205
|
-
'PresenceFrame and PresenceGutter require exactly one child element. ' +
|
|
206
|
-
'Wrap a list with PresenceGroup and give each item its own wrapper; fragments are not supported.'
|
|
207
|
-
)
|
|
208
|
-
}
|
|
209
|
-
return children
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
function useTargetPresence(target: string) {
|
|
213
|
-
const root = useRef<HTMLDivElement>(null)
|
|
214
|
-
const channel = presenceChannels.field(target)
|
|
215
|
-
const others = usePresenceChannel<boolean>(channel)
|
|
216
|
-
const publish = usePresencePublisher<boolean>(channel, false, hasPresence)
|
|
217
|
-
const ownsFocus = (element: EventTarget | null, owner: HTMLDivElement | null) =>
|
|
218
|
-
owner !== null &&
|
|
219
|
-
element instanceof Element &&
|
|
220
|
-
element.closest('[data-presence-target]') === owner
|
|
221
|
-
useLayoutEffect(() => {
|
|
222
|
-
publish(ownsFocus(document.activeElement, root.current))
|
|
223
|
-
}, [target, publish])
|
|
224
|
-
const users = new Map<string, { id: string; connectionId: string }>()
|
|
225
|
-
for (const entry of others) {
|
|
226
|
-
if (entry.value === true && !users.has(entry.userId)) {
|
|
227
|
-
users.set(entry.userId, { id: entry.userId, connectionId: entry.connectionId })
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
return {
|
|
231
|
-
users: [...users.values()],
|
|
232
|
-
props: {
|
|
233
|
-
ref: root,
|
|
234
|
-
'data-collab-target': target,
|
|
235
|
-
'data-presence-target': target,
|
|
236
|
-
onFocusCapture: (event: FocusEvent<HTMLDivElement>) =>
|
|
237
|
-
publish(ownsFocus(event.target, event.currentTarget)),
|
|
238
|
-
onBlurCapture: (event: FocusEvent<HTMLDivElement>) =>
|
|
239
|
-
publish(ownsFocus(event.relatedTarget, event.currentTarget))
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
export type PresenceFrameProps = {
|
|
245
|
-
id: string
|
|
246
|
-
children: ReactElement
|
|
247
|
-
className?: string
|
|
248
|
-
}
|
|
249
|
-
export function PresenceFrame({ id, children, className }: PresenceFrameProps) {
|
|
250
|
-
const target = usePresenceTarget(id)
|
|
251
|
-
const presence = useTargetPresence(target)
|
|
252
|
-
return (
|
|
253
|
-
<PresenceFramePrimitive
|
|
254
|
-
{...presence.props}
|
|
255
|
-
ids={presence.users.map(user => user.id)}
|
|
256
|
-
icon={<IconCursorText size={12} stroke={1.75} />}
|
|
257
|
-
className={className}
|
|
258
|
-
>
|
|
259
|
-
{presenceChild(children)}
|
|
260
|
-
</PresenceFramePrimitive>
|
|
261
|
-
)
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
export type PresenceGutterProps = {
|
|
265
|
-
id: string
|
|
266
|
-
children: ReactElement
|
|
267
|
-
className?: string
|
|
268
|
-
}
|
|
269
|
-
export function PresenceGutter({ id, children, className }: PresenceGutterProps) {
|
|
270
|
-
const target = usePresenceTarget(id)
|
|
271
|
-
const presence = useTargetPresence(target)
|
|
272
|
-
const grouped = useContext(PresenceGroupContext).length > 0
|
|
273
|
-
return (
|
|
274
|
-
<PresenceGutterPrimitive
|
|
275
|
-
{...presence.props}
|
|
276
|
-
users={presence.users}
|
|
277
|
-
animate={grouped}
|
|
278
|
-
className={className}
|
|
279
|
-
>
|
|
280
|
-
{presenceChild(children)}
|
|
281
|
-
</PresenceGutterPrimitive>
|
|
282
|
-
)
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
export type SelectionProps = {
|
|
286
|
-
id: string
|
|
287
|
-
selected: boolean
|
|
288
|
-
children: ReactNode
|
|
289
|
-
className?: string
|
|
290
|
-
}
|
|
291
|
-
export function Selection({ id, selected, children, className }: SelectionProps) {
|
|
292
|
-
const target = usePresenceTarget(id)
|
|
293
|
-
const channel = presenceChannels.selection(target)
|
|
294
|
-
usePublishPresenceChannel(channel, selected, hasPresence)
|
|
295
|
-
const others = usePresenceChannel<boolean>(channel)
|
|
296
|
-
const ids = [...new Set(others.filter(other => other.value === true).map(other => other.userId))]
|
|
297
|
-
return (
|
|
298
|
-
<PresenceFramePrimitive
|
|
299
|
-
data-collab-target={target}
|
|
300
|
-
ids={ids}
|
|
301
|
-
icon={<IconPointer size={12} stroke={1.75} />}
|
|
302
|
-
className={className}
|
|
303
|
-
>
|
|
304
|
-
{children}
|
|
305
|
-
</PresenceFramePrimitive>
|
|
306
|
-
)
|
|
307
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { PERSONA_COLORS } from '@/lib/collab/colors'
|
|
2
|
-
import type { CollabIdentity } from '@/lib/collab/types'
|
|
3
|
-
|
|
4
|
-
import { facehashDataUrl } from './facehash-avatar'
|
|
5
|
-
import { userDisplayName } from './people'
|
|
6
|
-
|
|
7
|
-
// Test profiles are activated only by explicit setup on /dev/collab.
|
|
8
|
-
|
|
9
|
-
export const PERSONA_NAMES = [
|
|
10
|
-
'Ada',
|
|
11
|
-
'Alan',
|
|
12
|
-
'Grace',
|
|
13
|
-
'Linus',
|
|
14
|
-
'Margaret',
|
|
15
|
-
'Dennis',
|
|
16
|
-
'Barbara',
|
|
17
|
-
'Ken',
|
|
18
|
-
'Radia',
|
|
19
|
-
'Hedy',
|
|
20
|
-
'Edsger',
|
|
21
|
-
'Frances',
|
|
22
|
-
'Donald',
|
|
23
|
-
'Katherine',
|
|
24
|
-
'Niklaus',
|
|
25
|
-
'Sophie',
|
|
26
|
-
'Adele',
|
|
27
|
-
'Jean',
|
|
28
|
-
'Bjarne',
|
|
29
|
-
'Guido',
|
|
30
|
-
'Yukihiro',
|
|
31
|
-
'Joe',
|
|
32
|
-
'Vint',
|
|
33
|
-
'Tim',
|
|
34
|
-
'Mary',
|
|
35
|
-
'Kathleen',
|
|
36
|
-
'Brian',
|
|
37
|
-
'Leslie'
|
|
38
|
-
]
|
|
39
|
-
|
|
40
|
-
export type Persona = { name: string; color: string }
|
|
41
|
-
|
|
42
|
-
function pick<T>(items: readonly T[], except?: T): T {
|
|
43
|
-
const pool = items.filter(item => item !== except)
|
|
44
|
-
return pool[Math.floor(Math.random() * pool.length)] ?? items[0]
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function randomPersona(current?: Persona): Persona {
|
|
48
|
-
return {
|
|
49
|
-
name: pick(PERSONA_NAMES, current?.name),
|
|
50
|
-
color: pick(
|
|
51
|
-
PERSONA_COLORS.map(([, hex]) => hex),
|
|
52
|
-
current?.color
|
|
53
|
-
)
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function personaIdentity(id: string, persona: Persona): CollabIdentity {
|
|
58
|
-
const identity = { id, name: persona.name, color: persona.color }
|
|
59
|
-
const avatar = facehashDataUrl(userDisplayName(identity), persona.color)
|
|
60
|
-
return { ...identity, ...(avatar ? { avatar } : {}) }
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// The id stays with the tab through renames, so peers keep seeing one person.
|
|
64
|
-
export function createDevIdentity(): CollabIdentity {
|
|
65
|
-
return personaIdentity(`dev-${crypto.randomUUID().slice(0, 8)}`, randomPersona())
|
|
66
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import type { CollabIdentity, CollabLocation, CollabPresenceRegistration } from '@/lib/collab/types'
|
|
2
|
-
|
|
3
|
-
import { CollabClient } from './client'
|
|
4
|
-
import {
|
|
5
|
-
getIdentity,
|
|
6
|
-
getWorkspaceDirectory,
|
|
7
|
-
getWorkspaceUsers,
|
|
8
|
-
subscribeIdentityStore,
|
|
9
|
-
subscribeWorkspaceUsersStore
|
|
10
|
-
} from './identity'
|
|
11
|
-
import type { WorkspaceDirectory } from './identity'
|
|
12
|
-
import { DISCONNECTED_STATE } from './store'
|
|
13
|
-
import type { CollabConnectionState, CollabPresenceEntry } from './store'
|
|
14
|
-
|
|
15
|
-
type Unsubscribe = () => void
|
|
16
|
-
|
|
17
|
-
// A workspace exposes one engine to React. The playground implements this same
|
|
18
|
-
// contract; sockets, timers, and registration storage stay behind it.
|
|
19
|
-
export type CollabEngineApi = {
|
|
20
|
-
readonly workspaceId: string
|
|
21
|
-
readonly enabled: boolean
|
|
22
|
-
start: () => Unsubscribe
|
|
23
|
-
getSnapshot: () => CollabConnectionState
|
|
24
|
-
getPeopleSnapshot: () => CollabConnectionState
|
|
25
|
-
getPresenceSnapshot: (surface: string, channel: string) => readonly CollabPresenceEntry[]
|
|
26
|
-
subscribe: (listener: () => void) => Unsubscribe
|
|
27
|
-
getIdentity: () => CollabIdentity | null
|
|
28
|
-
subscribeIdentity: (listener: () => void) => Unsubscribe
|
|
29
|
-
getWorkspaceUsers: () => readonly CollabIdentity[] | null
|
|
30
|
-
getWorkspaceDirectory: () => WorkspaceDirectory
|
|
31
|
-
subscribeWorkspaceUsers: (listener: () => void) => Unsubscribe
|
|
32
|
-
getLocation: () => CollabLocation | null
|
|
33
|
-
setLocation: (location: CollabLocation | null) => void
|
|
34
|
-
setPresence: (registration: CollabPresenceRegistration) => void
|
|
35
|
-
deletePresence: (registrationId: string) => void
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const noop = () => {}
|
|
39
|
-
const EMPTY_PRESENCE: readonly CollabPresenceEntry[] = []
|
|
40
|
-
const UNAVAILABLE_DIRECTORY: WorkspaceDirectory = { status: 'unavailable', users: [] }
|
|
41
|
-
|
|
42
|
-
export const NO_ENGINE: CollabEngineApi = {
|
|
43
|
-
workspaceId: '',
|
|
44
|
-
enabled: false,
|
|
45
|
-
start: () => noop,
|
|
46
|
-
getSnapshot: () => DISCONNECTED_STATE,
|
|
47
|
-
getPeopleSnapshot: () => DISCONNECTED_STATE,
|
|
48
|
-
getPresenceSnapshot: () => EMPTY_PRESENCE,
|
|
49
|
-
subscribe: () => noop,
|
|
50
|
-
getIdentity,
|
|
51
|
-
subscribeIdentity: subscribeIdentityStore,
|
|
52
|
-
getWorkspaceUsers: () => null,
|
|
53
|
-
getWorkspaceDirectory: () => UNAVAILABLE_DIRECTORY,
|
|
54
|
-
subscribeWorkspaceUsers: () => noop,
|
|
55
|
-
getLocation: () => null,
|
|
56
|
-
setLocation: noop,
|
|
57
|
-
setPresence: noop,
|
|
58
|
-
deletePresence: noop
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export class CollabEngine implements CollabEngineApi {
|
|
62
|
-
private readonly client: CollabClient
|
|
63
|
-
|
|
64
|
-
constructor(
|
|
65
|
-
readonly workspaceId: string,
|
|
66
|
-
readonly enabled = true
|
|
67
|
-
) {
|
|
68
|
-
this.client = new CollabClient(workspaceId)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
start = (): Unsubscribe => (this.enabled ? this.client.start() : noop)
|
|
72
|
-
getSnapshot = (): CollabConnectionState =>
|
|
73
|
-
this.enabled ? this.client.store.getSnapshot() : DISCONNECTED_STATE
|
|
74
|
-
getPeopleSnapshot = (): CollabConnectionState =>
|
|
75
|
-
this.enabled ? this.client.store.getPeopleSnapshot() : DISCONNECTED_STATE
|
|
76
|
-
getPresenceSnapshot = (surface: string, channel: string): readonly CollabPresenceEntry[] =>
|
|
77
|
-
this.enabled ? this.client.store.getPresenceSnapshot(surface, channel) : EMPTY_PRESENCE
|
|
78
|
-
subscribe = (listener: () => void): Unsubscribe =>
|
|
79
|
-
this.enabled ? this.client.store.subscribe(listener) : noop
|
|
80
|
-
getIdentity = getIdentity
|
|
81
|
-
subscribeIdentity = subscribeIdentityStore
|
|
82
|
-
getWorkspaceUsers = (): readonly CollabIdentity[] | null => getWorkspaceUsers(this.workspaceId)
|
|
83
|
-
getWorkspaceDirectory = (): WorkspaceDirectory => getWorkspaceDirectory(this.workspaceId)
|
|
84
|
-
subscribeWorkspaceUsers = (listener: () => void): Unsubscribe =>
|
|
85
|
-
subscribeWorkspaceUsersStore(this.workspaceId, listener)
|
|
86
|
-
getLocation = (): CollabLocation | null => (this.enabled ? this.client.store.getLocation() : null)
|
|
87
|
-
setLocation = (location: CollabLocation | null): void => {
|
|
88
|
-
this.client.store.setLocation(location)
|
|
89
|
-
}
|
|
90
|
-
setPresence = (registration: CollabPresenceRegistration): void => {
|
|
91
|
-
if (this.enabled) this.client.store.setPresence(registration)
|
|
92
|
-
}
|
|
93
|
-
deletePresence = (registrationId: string): void => {
|
|
94
|
-
if (this.enabled) this.client.store.deletePresence(registrationId)
|
|
95
|
-
}
|
|
96
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
// The API remains available when presence is disabled, so the same applet can
|
|
2
|
-
// render in either mode. The provider starts its transport only when enabled.
|
|
3
|
-
import { useSyncExternalStore } from 'react'
|
|
4
|
-
import type { WorkspaceTabId } from '@/lib/types'
|
|
5
|
-
import { createAppletCollabApi } from './index'
|
|
6
|
-
import { useCollabEngine } from './hooks'
|
|
7
|
-
import { WorkspaceCollabControls } from './WorkspaceCollabControls'
|
|
8
|
-
import type { CollabTabInfo } from './WorkspaceCollabControls'
|
|
9
|
-
|
|
10
|
-
export type { CollabTabInfo } from './WorkspaceCollabControls'
|
|
11
|
-
export { AppletScope, CollabProvider } from './hooks'
|
|
12
|
-
|
|
13
|
-
export const getAppletCollabApi = createAppletCollabApi
|
|
14
|
-
|
|
15
|
-
// Identity controls personal navigation and host UI independently of storage.
|
|
16
|
-
export function useCollabIdentityEnabled(): boolean {
|
|
17
|
-
const engine = useCollabEngine()
|
|
18
|
-
const identity = useSyncExternalStore(
|
|
19
|
-
engine.subscribeIdentity,
|
|
20
|
-
engine.getIdentity,
|
|
21
|
-
engine.getIdentity
|
|
22
|
-
)
|
|
23
|
-
return engine.enabled && identity !== null
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export type CollabControlsProps = {
|
|
27
|
-
workspaceId: string
|
|
28
|
-
// Resolves a participant's tab to the label and icon the tab strip uses.
|
|
29
|
-
describeTab: (tab: WorkspaceTabId) => CollabTabInfo | null
|
|
30
|
-
onOpenTab: (tab: WorkspaceTabId) => void
|
|
31
|
-
}
|
|
32
|
-
export function CollabControls(props: CollabControlsProps) {
|
|
33
|
-
const enabled = useCollabIdentityEnabled()
|
|
34
|
-
return enabled ? <WorkspaceCollabControls {...props} /> : null
|
|
35
|
-
}
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { stringHash } from 'facehash'
|
|
2
|
-
|
|
3
|
-
import { COLLAB_MAX_AVATAR_BYTES } from '@/lib/collab/protocol'
|
|
4
|
-
|
|
5
|
-
// Rasterizes a facehash face (https://facehash.dev) into a small PNG data URL,
|
|
6
|
-
// so a test avatar travels inside the identity instead of pointing at a URL.
|
|
7
|
-
// The library paints its faces with React and CSS, which a canvas cannot
|
|
8
|
-
// consume, so the eye shapes and their selection are mirrored here: a name gets
|
|
9
|
-
// the same face it gets from the library. This is the library's solid variant;
|
|
10
|
-
// its gradient sheen alone costs more than the protocol's avatar budget.
|
|
11
|
-
|
|
12
|
-
type Face = { width: number; height: number; draw: (ctx: CanvasRenderingContext2D) => void }
|
|
13
|
-
|
|
14
|
-
function circle(ctx: CanvasRenderingContext2D, cx: number, cy: number, r: number): void {
|
|
15
|
-
ctx.beginPath()
|
|
16
|
-
ctx.arc(cx, cy, r, 0, Math.PI * 2)
|
|
17
|
-
ctx.fill()
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function pill(ctx: CanvasRenderingContext2D, x: number, y: number, w: number, h: number): void {
|
|
21
|
-
ctx.beginPath()
|
|
22
|
-
ctx.roundRect(x, y, w, h, 3.5)
|
|
23
|
-
ctx.fill()
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const CURVED_EYE =
|
|
27
|
-
'M0 5.1c0-.1 0-.2 0-.3.1-.5.3-1 .7-1.3.1 0 .1-.1.2-.1C2.4 2.2 6 0 10.5 0S18.6 2.2 20.2 3.3c.1 0 .1.1.1.1.4.3.7.9.7 1.3v.3c0 1 0 1.4 0 1.7-.2 1.3-1.2 1.9-2.5 1.6-.2 0-.7-.3-1.8-.8C15 6.7 12.8 6 10.5 6s-4.5.7-6.3 1.5c-1 .5-1.5.7-1.8.8-1.3.3-2.3-.3-2.5-1.6v-1.7z'
|
|
28
|
-
|
|
29
|
-
// Same order as the library: round, cross, line, curved.
|
|
30
|
-
const FACES: Face[] = [
|
|
31
|
-
{
|
|
32
|
-
width: 63,
|
|
33
|
-
height: 15,
|
|
34
|
-
draw: ctx => {
|
|
35
|
-
circle(ctx, 7.2, 7.2, 7.2)
|
|
36
|
-
circle(ctx, 55.2, 7.2, 7.2)
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
width: 71,
|
|
41
|
-
height: 23,
|
|
42
|
-
draw: ctx => {
|
|
43
|
-
pill(ctx, 8, 0, 7, 23)
|
|
44
|
-
pill(ctx, 0, 8, 23, 7)
|
|
45
|
-
pill(ctx, 55.2, 0, 7, 23)
|
|
46
|
-
pill(ctx, 47.3, 8, 23, 7)
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
width: 82,
|
|
51
|
-
height: 8,
|
|
52
|
-
draw: ctx => {
|
|
53
|
-
pill(ctx, 0.07, 0.16, 6.9, 6.9)
|
|
54
|
-
pill(ctx, 7.9, 0.16, 20.7, 6.9)
|
|
55
|
-
pill(ctx, 74.7, 0.16, 6.9, 6.9)
|
|
56
|
-
pill(ctx, 53.1, 0.16, 20.7, 6.9)
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
width: 63,
|
|
61
|
-
height: 9,
|
|
62
|
-
draw: ctx => {
|
|
63
|
-
const eye = new Path2D(CURVED_EYE)
|
|
64
|
-
ctx.fill(eye)
|
|
65
|
-
ctx.translate(42, 0)
|
|
66
|
-
ctx.fill(eye)
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
]
|
|
70
|
-
|
|
71
|
-
// The library tilts the face in 3D; its image renderer flattens that into a
|
|
72
|
-
// small nudge, which is what a raster copy can reproduce.
|
|
73
|
-
const TILTS = [
|
|
74
|
-
[-1, 1],
|
|
75
|
-
[1, 1],
|
|
76
|
-
[1, 0],
|
|
77
|
-
[0, 1],
|
|
78
|
-
[-1, 0],
|
|
79
|
-
[0, 0],
|
|
80
|
-
[0, -1],
|
|
81
|
-
[-1, -1],
|
|
82
|
-
[1, -1]
|
|
83
|
-
] as const
|
|
84
|
-
|
|
85
|
-
// Crisp at the largest host avatar on a 2x display, and a few kilobytes as PNG.
|
|
86
|
-
const SIZE = 96
|
|
87
|
-
|
|
88
|
-
function drawFace(ctx: CanvasRenderingContext2D, name: string, color: string, size: number): void {
|
|
89
|
-
const hash = stringHash(name)
|
|
90
|
-
const face = FACES[hash % FACES.length] ?? FACES[0]
|
|
91
|
-
const [tiltX, tiltY] = TILTS[hash % TILTS.length] ?? [0, 0]
|
|
92
|
-
const dx = tiltY * size * 0.05
|
|
93
|
-
const dy = -tiltX * size * 0.05
|
|
94
|
-
|
|
95
|
-
ctx.fillStyle = color
|
|
96
|
-
ctx.fillRect(0, 0, size, size)
|
|
97
|
-
|
|
98
|
-
const eyesWidth = size * 0.6
|
|
99
|
-
const eyesHeight = (eyesWidth * face.height) / face.width
|
|
100
|
-
const gap = size * 0.08
|
|
101
|
-
const fontSize = size * 0.26
|
|
102
|
-
const top = (size - (eyesHeight + gap + fontSize)) / 2
|
|
103
|
-
|
|
104
|
-
ctx.save()
|
|
105
|
-
ctx.translate((size - eyesWidth) / 2 + dx, top + dy)
|
|
106
|
-
ctx.scale(eyesWidth / face.width, eyesWidth / face.width)
|
|
107
|
-
ctx.fillStyle = '#000'
|
|
108
|
-
face.draw(ctx)
|
|
109
|
-
ctx.restore()
|
|
110
|
-
|
|
111
|
-
ctx.fillStyle = '#000'
|
|
112
|
-
ctx.font = `700 ${fontSize}px ui-monospace, Menlo, monospace`
|
|
113
|
-
ctx.textAlign = 'center'
|
|
114
|
-
ctx.textBaseline = 'middle'
|
|
115
|
-
ctx.fillText(
|
|
116
|
-
name.charAt(0).toUpperCase(),
|
|
117
|
-
size / 2 + dx,
|
|
118
|
-
top + eyesHeight + gap + fontSize / 2 + dy
|
|
119
|
-
)
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function facehashDataUrl(name: string, color: string): string | undefined {
|
|
123
|
-
if (typeof document === 'undefined') return undefined
|
|
124
|
-
const canvas = document.createElement('canvas')
|
|
125
|
-
canvas.width = SIZE
|
|
126
|
-
canvas.height = SIZE
|
|
127
|
-
const ctx = canvas.getContext('2d')
|
|
128
|
-
if (!ctx) return undefined
|
|
129
|
-
drawFace(ctx, name, color, SIZE)
|
|
130
|
-
const url = canvas.toDataURL('image/png')
|
|
131
|
-
return url.length <= COLLAB_MAX_AVATAR_BYTES ? url : undefined
|
|
132
|
-
}
|