@kolosal-ai/rivet 0.3.0 → 0.4.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.
@@ -1,327 +0,0 @@
1
- import { N as NodeId, V as Vec2, R as Rect } from './types-C9Vn1rzy.js';
2
-
3
- /**
4
- * Locks — nodes a peer has claimed, and what this client may still do to them.
5
- *
6
- * A lock is policy where presence is description. `Peer.holding` says "they
7
- * have their pointer on this"; a lock says "hands off", and rivet acts on it.
8
- * The two render identically on purpose — to whoever is looking, they mean the
9
- * same thing — but only a lock refuses anything.
10
- *
11
- * Locks come in whole, as the `lockedNodes` prop, because the transport already
12
- * has to hold them: a lock outlives the tab that took it (that's the point of
13
- * taking one), so the server owns the lifetime and rivet only reads it. Nothing
14
- * here enters the graph, the change stream, or history — a lock is no more a
15
- * fact about the document than a cursor is.
16
- *
17
- * What a lock refuses is a default, not a rule. {@link LOCK_DEFAULT_REFUSED} is
18
- * rivet's opinion; `canInteractWithLocked` is the consumer's, and it wins on
19
- * every intent it's asked about.
20
- */
21
-
22
- /**
23
- * A thing this client might try to do to a locked node.
24
- *
25
- * Granular because the answer genuinely differs per app: a design tool wants a
26
- * locked node inspectable, a form builder wants it untouchable, and a pipeline
27
- * editor usually wants to keep wiring around one while a colleague renames it.
28
- */
29
- type LockIntent =
30
- /** Click it, or sweep it up in a marquee. */
31
- "select"
32
- /** Move it — pointer drag, group drag, keyboard grab or arrow nudge. */
33
- | "drag"
34
- /** Change its box through {@link NodeResizer}. */
35
- | "resize"
36
- /** Remove it as part of deleting the selection. */
37
- | "delete"
38
- /** Start or land an edge on one of its handles. */
39
- | "connect";
40
- /** What {@link CanInteractWithLocked} is asked about. */
41
- type LockInteraction = {
42
- nodeId: NodeId;
43
- /** The peer holding the lock — the `lockedNodes` value for this node. */
44
- holderId: string;
45
- intent: LockIntent;
46
- };
47
- /**
48
- * Consumer override for rivet's default refusals, consulted once per attempt.
49
- * Return `true` to allow the intent, `false` to refuse it — the default is
50
- * replaced, not consulted, so a handler owns the whole policy.
51
- */
52
- type CanInteractWithLocked = (event: LockInteraction) => boolean;
53
- /** A node's lock as {@link useNodeLock} reports it. */
54
- type NodeLock = {
55
- locked: boolean;
56
- /** The peer holding it, or `undefined` when the node isn't locked. */
57
- holderId?: string;
58
- };
59
- /**
60
- * The intents rivet refuses on a locked node when no policy is given.
61
- *
62
- * `connect` is deliberately absent. A lock claims the node, not the graph
63
- * around it: an edge is its own row with its own id, and refusing to wire one
64
- * would make a locked node an island for as long as somebody holds it — a far
65
- * heavier lock than "someone is editing this" warrants. Consumers who want that
66
- * can say so through {@link CanInteractWithLocked}.
67
- */
68
- declare const LOCK_DEFAULT_REFUSED: ReadonlySet<LockIntent>;
69
- /**
70
- * The lock registry — one per graph, reachable as `store.locks`.
71
- *
72
- * Reads are hot: every marquee frame and every mover loop asks
73
- * {@link LockRegistry.allows}, so it stays a map lookup plus at most one
74
- * consumer call, with no allocation on the common (nothing locked) path.
75
- */
76
- type LockRegistry = {
77
- /**
78
- * Replace the lock table (the `lockedNodes` prop). Returns the nodes that
79
- * weren't locked before and are now — the store scans those for conflicts
80
- * with a live local gesture.
81
- */
82
- setLocks: (locks: Record<NodeId, string> | undefined) => NodeId[];
83
- /** Install the consumer's policy, or `null` to fall back to the defaults. */
84
- setPolicy: (policy: CanInteractWithLocked | null) => void;
85
- isLocked: (id: NodeId) => boolean;
86
- /** Who holds this node's lock, or `null` when it isn't locked. */
87
- getHolder: (id: NodeId) => string | null;
88
- /** The whole table. The renderer walks it once a frame. */
89
- getLocks: () => ReadonlyMap<NodeId, string>;
90
- /** How many nodes are locked — the render loop's early-out. */
91
- size: () => number;
92
- /**
93
- * Whether this client may act on a node. Always `true` for an unlocked node,
94
- * so callers can ask unconditionally.
95
- */
96
- allows: (id: NodeId, intent: LockIntent) => boolean;
97
- /** Bumped on every table change, for `useSyncExternalStore`. */
98
- getVersion: () => number;
99
- /** Subscribe to the table changing — locks move at human rate, so this is React-safe. */
100
- subscribe: (listener: () => void) => () => void;
101
- };
102
-
103
- /**
104
- * Presence — who else is in the document, and where they are.
105
- *
106
- * Presence is ephemeral by construction. Nothing here enters the graph, the
107
- * change stream, or history: a peer's cursor is not a fact about the document,
108
- * it's a fact about a person, and it stops being true the moment they move. So
109
- * the registry is imperative, painted straight from the render loop, and thrown
110
- * away when the tab closes.
111
- *
112
- * Two doors write to it, at deliberately different speeds:
113
- *
114
- * - the `peers` prop — the roster. Identity (name, colour) and the slow
115
- * fields (selection, holding). React-rate.
116
- * - {@link PresenceRegistry.setPeerCursor} / `setPeerNodeTransform` — the hot
117
- * path off your ephemeral channel, called as fast as frames arrive. These
118
- * never touch React: they mark the canvas dirty and nothing else. A cursor
119
- * dirties only the cursor layer; a transform moves a real node, so it
120
- * dirties the frame.
121
- *
122
- * A peer the roster has never mentioned is still legitimate — a cursor frame
123
- * can beat the join event, and dropping it would blink the cursor. Such a peer
124
- * lives until its cursor clears (see {@link PresenceRegistry.setPeerCursor}),
125
- * and its colour is derived from its id, so the record can be rebuilt any
126
- * number of times without the colour ever flickering.
127
- */
128
-
129
- /**
130
- * A participant other than this client, as your transport describes them.
131
- *
132
- * Everything is optional but `id`: presence degrades field by field, and a peer
133
- * you know nothing about except that they exist is still worth an avatar.
134
- */
135
- type Peer = {
136
- id: string;
137
- /** Display name, drawn beside their cursor. Unnamed peers get no label. */
138
- name?: string;
139
- /** Any CSS colour. Defaults to a stable colour derived from {@link Peer.id}. */
140
- color?: string;
141
- /**
142
- * Cursor in **world** coordinates — peers pan and zoom independently, so a
143
- * screen point means nothing to anyone else. `null` when their pointer is
144
- * off the canvas. Only a seed: once {@link PresenceRegistry.setPeerCursor}
145
- * has been called for a peer, the imperative value owns the cursor.
146
- */
147
- cursor?: Vec2 | null;
148
- /** Nodes they have selected. Outlined in their colour. */
149
- selection?: NodeId[];
150
- /** Nodes under one of their live gestures (their `onNodeDragStart`). */
151
- holding?: NodeId[];
152
- };
153
- /**
154
- * A peer as the registry holds it: identity resolved, live overlays merged in.
155
- * What the renderer and {@link usePeers} both read.
156
- */
157
- type PeerRecord = {
158
- id: string;
159
- name?: string;
160
- /** Always set — {@link peerColor} fills in for a peer that declared none. */
161
- color: string;
162
- cursor: Vec2 | null;
163
- selection: readonly NodeId[];
164
- holding: readonly NodeId[];
165
- /**
166
- * Where this peer currently has each node they're dragging, in world space —
167
- * the frame-rate overlay from `setPeerNodeTransform`. The node renders there
168
- * until they let go; the graph keeps its own position until their commit
169
- * arrives as a real change.
170
- */
171
- transforms: ReadonlyMap<NodeId, Rect>;
172
- };
173
- /** Presence policy, set via the `presenceOptions` prop. */
174
- type PresenceOptions = {
175
- /**
176
- * Smallest gap between outbound `onLocalPresence` snapshots, in ms. Cursor
177
- * movement is coalesced to this rate; selection and gesture changes ignore it
178
- * and go out at once (a lock signal that waits is a lock signal that races).
179
- * Default `50`.
180
- */
181
- throttleMs?: number;
182
- /**
183
- * Give peer cursors their own canvas layer. Turn off to render your own from
184
- * {@link usePeers} — avatars, follow buttons, anything the canvas can't do —
185
- * and the layer is never created at all. Node outlines and peers' live boxes
186
- * are unaffected: they live with the geometry they annotate. Default `true`.
187
- */
188
- renderCursors?: boolean;
189
- };
190
- type ResolvedPresenceOptions = Required<PresenceOptions>;
191
- declare const DEFAULT_PRESENCE_OPTIONS: ResolvedPresenceOptions;
192
- /**
193
- * Default peer colours. Spaced around the wheel and mid-toned, so they read on
194
- * a light or dark canvas and stay distinguishable side by side.
195
- */
196
- declare const PEER_COLORS: readonly ["#6366f1", "#ec4899", "#f59e0b", "#10b981", "#3b82f6", "#8b5cf6", "#ef4444", "#14b8a6"];
197
- /**
198
- * A stable colour for a peer id. Deterministic, so the same person is the same
199
- * colour on every client and across a record being pruned and rebuilt — nobody
200
- * coordinates a palette, and nobody's cursor changes colour mid-session.
201
- */
202
- declare function peerColor(id: string): string;
203
- /**
204
- * The presence registry — one per graph, reachable as `store.presence`.
205
- *
206
- * Reads are snapshot-based: {@link getPeers} returns a cached array rebuilt
207
- * only when something actually changed, so the render loop can call it every
208
- * frame and `useSyncExternalStore` can compare identities.
209
- */
210
- type PresenceRegistry = {
211
- /**
212
- * Replace the roster (the `peers` prop). Peers that were in the previous
213
- * roster and aren't in this one are dropped outright — leaving the roster is
214
- * how a peer leaves. Peers this registry only knows imperatively are kept.
215
- */
216
- setPeers: (peers: Peer[]) => void;
217
- /**
218
- * Move a peer's cursor, in world coordinates. The hot path: call it as fast
219
- * as your channel delivers frames — it dirties the cursor layer and nothing
220
- * else, so the cost doesn't scale into the rest of the canvas.
221
- *
222
- * `null` parks the cursor. For a peer the roster never declared, that leaves
223
- * nothing to draw, so the record is dropped — the next frame recreates it
224
- * with the same colour.
225
- */
226
- setPeerCursor: (peerId: string, point: Vec2 | null) => void;
227
- /**
228
- * Where a peer currently has a node, in world space — their drag, mid-flight.
229
- * The node itself moves there: it's painted, hit-tested and wired at the peer's
230
- * live box, while the graph underneath is untouched until their commit lands.
231
- * `null` releases it, which is what their drop should do just before their
232
- * real change arrives.
233
- */
234
- setPeerNodeTransform: (peerId: string, nodeId: NodeId, rect: Rect | null) => void;
235
- /**
236
- * Every node a peer currently has under a gesture, at its *rendered* position
237
- * — the interpolated one, not the last frame that arrived. What the store
238
- * folds into world positions.
239
- */
240
- getNodeTransforms: () => ReadonlyMap<NodeId, Rect>;
241
- /**
242
- * Bumped whenever a rendered transform moves. Lets the store cache world
243
- * positions across frames where peers are idle.
244
- */
245
- getTransformVersion: () => number;
246
- /**
247
- * Advance the interpolation toward the values that arrived, and report
248
- * whether anything is still in motion (i.e. whether another frame is owed).
249
- *
250
- * Frames arrive at whatever rate a transport manages — 20Hz is normal, and
251
- * stepping straight to each one makes a cursor stutter and a dragged node
252
- * jump. Easing between them costs one lerp per peer and buys motion that
253
- * reads as somebody moving rather than as packets landing.
254
- */
255
- step: (now: number) => boolean;
256
- /** Forget a peer entirely — cursor, held nodes and all. Use it when they disconnect. */
257
- removePeer: (peerId: string) => void;
258
- /** Every known peer. A cached snapshot; identity only changes when the data does. */
259
- getPeers: () => readonly PeerRecord[];
260
- /** True when nobody else is here — the render loop's early-out. */
261
- isEmpty: () => boolean;
262
- /**
263
- * Subscribe to **roster** changes: peers arriving or leaving, and their
264
- * identity, selection or holding changing. Cursor and transform frames
265
- * deliberately don't notify — they'd re-render React at pointer rate, and
266
- * they're already painted on the canvas. Read them from {@link getPeers} on
267
- * the frame channel if you need them in the DOM.
268
- */
269
- subscribe: (listener: () => void) => () => void;
270
- };
271
-
272
- /**
273
- * The outbound half of presence: what this client tells everyone else.
274
- *
275
- * It publishes a *snapshot*, never an event stream. A snapshot is idempotent —
276
- * a dropped one costs nothing, a duplicate costs nothing, and a client that
277
- * reconnects mid-session is immediately correct rather than replaying history.
278
- * That's also how presence channels themselves model state, so this maps onto
279
- * any transport without a translation layer.
280
- *
281
- * Cursor movement is coalesced to `throttleMs`; selection and gesture changes
282
- * jump the queue, because they're the signals a peer acts on (a lock request
283
- * that waits 50ms is a lock request that can lose a race it should have won).
284
- */
285
-
286
- /** This client's presence, as handed to `onLocalPresence`. */
287
- type LocalPresence = {
288
- /**
289
- * Pointer position in **world** coordinates, or `null` when the pointer is
290
- * off the canvas. World, not screen: peers have their own viewports, and a
291
- * screen point means nothing on the other side.
292
- */
293
- cursor: Vec2 | null;
294
- /** Currently selected node ids. */
295
- selection: NodeId[];
296
- /** Nodes under a live local gesture — the same set as `onNodeDragStart`. */
297
- holding: NodeId[];
298
- };
299
-
300
- /**
301
- * Whether a node is locked, and by whom — for the chrome rivet can't draw for
302
- * you: a padlock in the node's header, a "Ada is editing" line, a disabled
303
- * form inside the node body.
304
- *
305
- * rivet already refuses the interactions it owns (see the `lockedNodes` prop),
306
- * so this is for *your* controls. Re-renders only when this node changes hands,
307
- * not when any lock in the document moves.
308
- *
309
- * ```tsx
310
- * function MyNode({ id }: NodeProps) {
311
- * const { locked, holderId } = useNodeLock(id)
312
- * return <input disabled={locked} title={locked ? `${holderId} is editing` : ""} />
313
- * }
314
- * ```
315
- */
316
- declare function useNodeLock(id: NodeId): NodeLock;
317
- /**
318
- * Whether this client may do `intent` to a node right now — the same question
319
- * every rivet input path asks, answered through the same policy.
320
- *
321
- * Use it to keep your own affordances honest: hide a delete button on a node a
322
- * peer has locked, rather than letting it be pressed and refused. Always `true`
323
- * for an unlocked node.
324
- */
325
- declare function useNodeLockAllows(id: NodeId, intent: LockIntent): boolean;
326
-
327
- export { type CanInteractWithLocked as C, DEFAULT_PRESENCE_OPTIONS as D, LOCK_DEFAULT_REFUSED as L, type NodeLock as N, type PeerRecord as P, type ResolvedPresenceOptions as R, type LocalPresence as a, type LockIntent as b, type LockInteraction as c, type LockRegistry as d, PEER_COLORS as e, type Peer as f, type PresenceOptions as g, type PresenceRegistry as h, useNodeLockAllows as i, peerColor as p, useNodeLock as u };