@nelsonlongxiang/dsh-open-a2a-net 0.5.14 → 0.6.24

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 (86) hide show
  1. package/README.md +243 -121
  2. package/README_zh.md +144 -84
  3. package/assets/nexusDist/assets/index-034h1cNc.js +3 -0
  4. package/assets/nexusDist/assets/index-PG9EieGX.css +1 -0
  5. package/assets/nexusDist/index.html +26 -0
  6. package/assets/stageDist/assets/index-Bj7S2pA1.css +1 -0
  7. package/assets/stageDist/assets/index-C-28k_o5.js +974 -0
  8. package/assets/stageDist/assets/interiors-CQGBjffT.png +0 -0
  9. package/assets/stageDist/assets/office-tileset-CHQT-uYL.png +0 -0
  10. package/assets/stageDist/index.html +13 -0
  11. package/lib/a2a-client.js +190 -13
  12. package/lib/canvas-store.js +218 -0
  13. package/lib/card.js +25 -0
  14. package/lib/client/A2aControl.js +45 -137
  15. package/lib/client/locales.js +16 -40
  16. package/lib/client.js +164 -467
  17. package/lib/client.js.map +1 -1
  18. package/lib/group-store.js +6 -0
  19. package/lib/idempotency-store.js +291 -0
  20. package/lib/index.js +4217 -259
  21. package/lib/joined-store.js +11 -0
  22. package/lib/layout-store.js +155 -0
  23. package/lib/peer-store.js +98 -13
  24. package/lib/receipt-ladder.js +35 -0
  25. package/lib/receipt.js +102 -0
  26. package/lib/self-suppress.js +62 -0
  27. package/lib/session-transcript.js +150 -0
  28. package/lib/skill/SKILL.md +77 -0
  29. package/lib/skill/references/discovery.md +38 -0
  30. package/lib/skill/references/membership.md +50 -0
  31. package/lib/skill/references/pitfalls.md +42 -0
  32. package/lib/skill/references/routing.md +43 -0
  33. package/lib/skill-registration.js +52 -0
  34. package/lib/stage-mount.js +32 -0
  35. package/lib/task-ledger.js +540 -0
  36. package/lib/team-store.js +122 -0
  37. package/lib/teams-bridge.js +18 -0
  38. package/lib/transport-caps.js +31 -0
  39. package/lib/types/a2a-client.d.ts +72 -1
  40. package/lib/types/canvas-store.d.ts +102 -0
  41. package/lib/types/client/A2aControl.d.ts +18 -28
  42. package/lib/types/client/locales.d.ts +1 -1
  43. package/lib/types/group-store.d.ts +6 -0
  44. package/lib/types/idempotency-store.d.ts +164 -0
  45. package/lib/types/index.d.ts +210 -6
  46. package/lib/types/joined-store.d.ts +9 -0
  47. package/lib/types/layout-store.d.ts +63 -0
  48. package/lib/types/peer-store.d.ts +45 -2
  49. package/lib/types/receipt-ladder.d.ts +25 -0
  50. package/lib/types/receipt.d.ts +51 -0
  51. package/lib/types/self-suppress.d.ts +41 -0
  52. package/lib/types/session-transcript.d.ts +37 -0
  53. package/lib/types/skill-registration.d.ts +3 -0
  54. package/lib/types/stage-mount.d.ts +29 -0
  55. package/lib/types/task-ledger.d.ts +308 -0
  56. package/lib/types/team-store.d.ts +55 -0
  57. package/lib/types/teams-bridge.d.ts +112 -0
  58. package/lib/types/transport-caps.d.ts +29 -0
  59. package/lib/types/types.d.ts +43 -0
  60. package/nexus-stage/package-lock.json +2262 -0
  61. package/nexus-stage/package.json +21 -0
  62. package/nexus-stage/scripts/__pycache__/mockhost.cpython-312.pyc +0 -0
  63. package/nexus-stage/scripts/mockhost.py +253 -0
  64. package/nexus-stage/src/canvas-ops.ts +175 -0
  65. package/nexus-stage/src/canvas-wire.ts +156 -0
  66. package/nexus-stage/src/edges.ts +72 -0
  67. package/nexus-stage/src/fault.ts +56 -0
  68. package/nexus-stage/src/federation.ts +234 -0
  69. package/nexus-stage/src/layout-doc.ts +143 -0
  70. package/nexus-stage/src/layout-wire.ts +158 -0
  71. package/nexus-stage/src/main.ts +361 -0
  72. package/nexus-stage/src/planning-menus.ts +352 -0
  73. package/nexus-stage/src/planning-nodelist.ts +294 -0
  74. package/nexus-stage/src/planning-types.ts +111 -0
  75. package/nexus-stage/src/planning-view.ts +1313 -0
  76. package/nexus-stage/src/planning.css +280 -0
  77. package/nexus-stage/src/seat.ts +35 -0
  78. package/nexus-stage/src/stage-name.ts +13 -0
  79. package/nexus-stage/src/tokens.ts +32 -0
  80. package/nexus-stage/src/viewport.ts +83 -0
  81. package/nexus-stage/src/workbench.css +267 -0
  82. package/nexus-stage/src/workbench.ts +1530 -0
  83. package/nexus-stage/src/world.ts +509 -0
  84. package/nexus-stage/tsconfig.json +25 -0
  85. package/nexus-stage/vite.config.ts +16 -0
  86. package/package.json +83 -22
@@ -0,0 +1,509 @@
1
+ /**
2
+ * The 2D planning-canvas world model: joined session nodes, team-frame
3
+ * rectangles, the selection set, and the drag/marquee mutations. Pure
4
+ * state + math, no DOM and no three (structural types redeclared here so
5
+ * this module stays unit-testable from the root suite; main.ts passes the
6
+ * topology.ts rows straight in - they satisfy these shapes structurally).
7
+ *
8
+ * Coordinate contract: a node's persisted {x, y} is its CARD CENTER (the
9
+ * 3D scene writes mesh centers into the same layout document), so every
10
+ * rectangle math goes through nodeRect(). Team frames own their saved
11
+ * rects; a frame absent from the layout derives one from its members'
12
+ * card bounding box. Group drag works from a pointerdown snapshot and
13
+ * applies total deltas - never incremental accumulation, so a long drag
14
+ * cannot drift.
15
+ * @module nexus-stage/world
16
+ */
17
+
18
+ import { clampDoc, type LayoutDoc, type LayoutPoint, type LayoutRect } from './layout-doc'
19
+ import { hashId } from './seat'
20
+ import type { WorldBounds } from './viewport'
21
+
22
+ /** Card geometry: the single source both CSS and edge math must agree with. */
23
+ export const NODE_W = 172
24
+ export const NODE_H = 56
25
+
26
+ /**
27
+ * Deterministic 2D fallback seating. The 3D scene's polar ring (radius
28
+ * 12-30) is sized for unit spheres - 172px cards dropped onto it stack
29
+ * into one pile. Unarranged cards instead spread on three card-scaled
30
+ * rings (48 hash slots, y compressed for wide screens), so a fresh
31
+ * canvas is readable before the first save; once anything is saved, the
32
+ * persisted document wins in BOTH modes and this is only history.
33
+ */
34
+ const PLAN_RINGS = [150, 290, 430]
35
+ const PLAN_SLOTS = 16
36
+
37
+ export function planSeatFor(sid: string): { x: number; y: number } {
38
+ const h = hashId(sid)
39
+ const ring = Math.floor(h / PLAN_SLOTS) % PLAN_RINGS.length
40
+ const slot = h % PLAN_SLOTS
41
+ const angle = (slot / PLAN_SLOTS) * Math.PI * 2 + ring * (Math.PI / PLAN_SLOTS)
42
+ const r = PLAN_RINGS[ring]!
43
+ return { x: Math.round(Math.cos(angle) * r), y: Math.round(Math.sin(angle) * r * 0.6) }
44
+ }
45
+
46
+ /**
47
+ * Home team per member id: the first team in state order that lists it.
48
+ * That is the team whose membership edge is solid (edges.ts); every later
49
+ * membership is a dashed 跨队 edge. A cross-team member therefore sits in
50
+ * its home frame only — the other frames reach it by the dashed edge
51
+ * instead of stretching over the home frame.
52
+ */
53
+ export function homeTeams(teams: ReadonlyArray<TeamLite>): Map<string, string> {
54
+ const home = new Map<string, string>()
55
+ for (const team of teams) {
56
+ for (const m of team.members) if (!home.has(m.id)) home.set(m.id, team.name)
57
+ }
58
+ return home
59
+ }
60
+
61
+ const CLUSTER_GAP = 64
62
+ const CLUSTER_COL_GAP = 24
63
+ const CLUSTER_ROW_GAP = 20
64
+ const CLUSTERS_PER_ROW = 4
65
+ /** Same padding deriveInitialFrame wraps around member cards. */
66
+ const FRAME_PAD_X = 24
67
+ const FRAME_PAD_TOP = 44
68
+ const FRAME_PAD_BOTTOM = 24
69
+
70
+ /**
71
+ * First-run seats grouped by team: each team's home members fill a small
72
+ * grid (2–3 columns), clusters sit side by side with a 64px gutter and wrap
73
+ * every four teams; members with no canvas team (declared roster teams
74
+ * only) form a trailing cluster. Hash ring seats ignored teams, so the
75
+ * frames derived from scattered members nested inside each other and their
76
+ * centered titles printed on top of one another. A row also wraps before it
77
+ * outgrows `maxRowW` (the board width), so a portrait phone stacks teams
78
+ * instead of fitting one long strip at ~30%. Deterministic for a fixed
79
+ * state order and width. Only ids in `ids` are seated.
80
+ */
81
+ export function planTeamClusters(teams: ReadonlyArray<TeamLite>, ids: ReadonlyArray<string>, maxRowW = Number.POSITIVE_INFINITY): Map<string, { x: number; y: number }> {
82
+ const want = new Set(ids)
83
+ const home = homeTeams(teams)
84
+ const groups: string[][] = teams.map(t => [...new Set(t.members.map(m => m.id))].filter(id => want.has(id) && home.get(id) === t.name))
85
+ groups.push(ids.filter(id => !home.has(id)))
86
+ const seats = new Map<string, { x: number; y: number }>()
87
+ let cursorX = 0
88
+ let cursorY = 0
89
+ let rowH = 0
90
+ let placed = 0
91
+ for (const group of groups) {
92
+ if (group.length === 0) continue
93
+ const cols = Math.min(3, Math.max(group.length > 1 ? 2 : 1, Math.ceil(Math.sqrt(group.length))))
94
+ const rows = Math.ceil(group.length / cols)
95
+ const w = Math.max(260, cols * NODE_W + (cols - 1) * CLUSTER_COL_GAP + FRAME_PAD_X * 2)
96
+ const h = Math.max(160, rows * NODE_H + (rows - 1) * CLUSTER_ROW_GAP + FRAME_PAD_TOP + FRAME_PAD_BOTTOM)
97
+ if (placed > 0 && (placed % CLUSTERS_PER_ROW === 0 || cursorX + w > maxRowW)) {
98
+ cursorX = 0
99
+ cursorY += rowH + CLUSTER_GAP
100
+ rowH = 0
101
+ }
102
+ group.forEach((id, i) => {
103
+ const c = i % cols
104
+ const r = Math.floor(i / cols)
105
+ seats.set(id, {
106
+ x: cursorX + FRAME_PAD_X + NODE_W / 2 + c * (NODE_W + CLUSTER_COL_GAP),
107
+ y: cursorY + FRAME_PAD_TOP + NODE_H / 2 + r * (NODE_H + CLUSTER_ROW_GAP),
108
+ })
109
+ })
110
+ cursorX += w + CLUSTER_GAP
111
+ rowH = Math.max(rowH, h)
112
+ placed += 1
113
+ }
114
+ return seats
115
+ }
116
+
117
+ /** Breathing room kept between two unarranged cards. */
118
+ const SEAT_GAP = 12
119
+
120
+ /**
121
+ * Collision-free fallback seat: the hashed ring seat when it is clear of
122
+ * every card in `occupied`, else the next clear slot walking the rings
123
+ * outward from the hashed one. The bare hash put 8 sessions into 48
124
+ * slots and stacked pairs pixel-for-pixel on top of each other (and the
125
+ * inner ring's slots sit ~60px apart, so neighbours overlapped too) —
126
+ * a fresh board read as fewer nodes than the fleet had. Deterministic for
127
+ * a fixed input order; when every ring slot is taken, cards queue on a
128
+ * grid row under the outer ring.
129
+ */
130
+ export function planSeatClear(sid: string, occupied: ReadonlyArray<{ readonly x: number; readonly y: number }>): { x: number; y: number } {
131
+ const clear = (p: { x: number; y: number }): boolean => occupied.every(o =>
132
+ Math.abs(o.x - p.x) >= NODE_W + SEAT_GAP || Math.abs(o.y - p.y) >= NODE_H + SEAT_GAP)
133
+ const first = planSeatFor(sid)
134
+ if (clear(first)) return first
135
+ const h = hashId(sid)
136
+ const total = PLAN_SLOTS * PLAN_RINGS.length
137
+ const start = (Math.floor(h / PLAN_SLOTS) % PLAN_RINGS.length) * PLAN_SLOTS + (h % PLAN_SLOTS)
138
+ for (let k = 1; k < total; k++) {
139
+ const i = (start + k) % total
140
+ const ring = Math.floor(i / PLAN_SLOTS)
141
+ const angle = ((i % PLAN_SLOTS) / PLAN_SLOTS) * Math.PI * 2 + ring * (Math.PI / PLAN_SLOTS)
142
+ const r = PLAN_RINGS[ring]!
143
+ const p = { x: Math.round(Math.cos(angle) * r), y: Math.round(Math.sin(angle) * r * 0.6) }
144
+ if (clear(p)) return p
145
+ }
146
+ const outer = PLAN_RINGS[PLAN_RINGS.length - 1]!
147
+ for (let row = 0; ; row++) {
148
+ for (let col = 0; col < 6; col++) {
149
+ const p = { x: Math.round(-outer + col * (NODE_W + SEAT_GAP)), y: Math.round(outer * 0.6 + NODE_H * 2 + row * (NODE_H + SEAT_GAP)) }
150
+ if (clear(p)) return p
151
+ }
152
+ }
153
+ }
154
+
155
+ /** Minimal session row shape the model needs (topology.SessionRow satisfies it). */
156
+ export interface SessionLite { id: string; label: string; team: string; name?: string; joined: boolean; live?: boolean; teams?: readonly string[] }
157
+
158
+ /** Minimal team shape (topology.CanvasTeam satisfies it). `team` is the
159
+ * mono route alias `<host>/canvas/<name>` when the payload carries it. */
160
+ export interface TeamLite { name: string; team?: string; members: ReadonlyArray<{ id: string }> }
161
+
162
+ /** One node's memberships in state order: team name + index (member order = routing priority). */
163
+ export interface Membership { readonly team: string; readonly index: number }
164
+
165
+ /** A placed session node: center position plus everything the card renders. */
166
+ export interface WorldNode {
167
+ x: number
168
+ y: number
169
+ readonly id: string
170
+ /** Rendered attributes refresh on each poll; the position never does. */
171
+ label: string
172
+ /** The route alias `<team>/<id8>` the state payload provides. */
173
+ team: string
174
+ name?: string
175
+ live: boolean
176
+ /** Peer node (PR D v1 ruling): a remote host entry rendered as a card.
177
+ * Excluded from local content bounds; no memberships, ever. */
178
+ remote?: boolean
179
+ /** Full peer URL (identity is the bare host:port in the node id). */
180
+ peerUrl?: string
181
+ /** Reachability score from a2a_probe (peers only). */
182
+ score?: number
183
+ /** Declared roster teams (a2a/teams.json) — the network-declaration half
184
+ * of "teamed"; canvas memberships are the visual half. Peers: never. */
185
+ teams?: readonly string[]
186
+ memberships: ReadonlyArray<Membership>
187
+ }
188
+
189
+ /** A team frame rectangle in world space (top-left + size). */
190
+ export interface FrameGeom { x: number; y: number; w: number; h: number }
191
+
192
+ /** Snapshot for group drag: the frame rect plus member centers at pointerdown. */
193
+ export interface FrameDragSnapshot {
194
+ readonly name: string
195
+ readonly rect: FrameGeom
196
+ readonly centers: ReadonlyArray<readonly [string, number, number]>
197
+ }
198
+
199
+ /** The card rectangle (top-left + size) for a node centered at (x, y). */
200
+ export function nodeRect(n: { readonly x: number; readonly y: number }): LayoutRect {
201
+ return { x: n.x - NODE_W / 2, y: n.y - NODE_H / 2, w: NODE_W, h: NODE_H }
202
+ }
203
+
204
+ /**
205
+ * Derive an initial frame rect from member card rects: bounding box plus
206
+ * padding (extra headroom on top for the straddling titlebar), minimum
207
+ * 260x160, rounded to integers - deterministic, so the same roster always
208
+ * proposes the same frame until the user moves something.
209
+ */
210
+ export function deriveInitialFrame(memberRects: ReadonlyArray<LayoutRect>): FrameGeom {
211
+ if (memberRects.length === 0) return { x: 0, y: 0, w: 260, h: 160 }
212
+ let minX = Number.POSITIVE_INFINITY
213
+ let minY = Number.POSITIVE_INFINITY
214
+ let maxX = Number.NEGATIVE_INFINITY
215
+ let maxY = Number.NEGATIVE_INFINITY
216
+ for (const r of memberRects) {
217
+ minX = Math.min(minX, r.x)
218
+ minY = Math.min(minY, r.y)
219
+ maxX = Math.max(maxX, r.x + r.w)
220
+ maxY = Math.max(maxY, r.y + r.h)
221
+ }
222
+ const padX = 24
223
+ const padTop = 44
224
+ const padBottom = 24
225
+ const w = Math.max(260, Math.round(maxX - minX + padX * 2))
226
+ const h = Math.max(160, Math.round(maxY - minY + padTop + padBottom))
227
+ return { x: Math.round(minX - padX), y: Math.round(minY - padTop), w, h }
228
+ }
229
+
230
+ /** The planning-canvas model. */
231
+ export class WorldModel {
232
+ private revisionCounter = 0
233
+
234
+ /** Monotonic mutation counter: the view re-renders only when this moves. */
235
+ get revision(): number { return this.revisionCounter }
236
+
237
+ private nodes = new Map<string, WorldNode>()
238
+ private frames = new Map<string, FrameGeom>()
239
+ private selection = new Set<string>()
240
+ viewport = { x: 0, y: 0, scale: 1 }
241
+
242
+ /** Read-only node access for rendering/edges. */
243
+ getNode(id: string): WorldNode | undefined { return this.nodes.get(id) }
244
+ allNodes(): ReadonlyArray<WorldNode> { return [...this.nodes.values()] }
245
+ nodeIds(): ReadonlyArray<string> { return [...this.nodes.keys()] }
246
+ getFrame(name: string): FrameGeom | undefined { return this.frames.get(name) }
247
+ allFrames(): ReadonlyArray<[string, FrameGeom]> { return [...this.frames.entries()] }
248
+ isSelected(id: string): boolean { return this.selection.has(id) }
249
+ selectedIds(): ReadonlyArray<string> { return [...this.selection] }
250
+
251
+ private bump(): void { this.revisionCounter += 1 }
252
+
253
+ /**
254
+ * Move nodes by a world-space delta (drag/nudge); also moves each
255
+ * selected node out of no frame - frames track rects, not membership,
256
+ * so positions and rects stay independent.
257
+ */
258
+ dragNodes(ids: ReadonlyArray<string>, dx: number, dy: number): void {
259
+ let moved = false
260
+ for (const id of ids) {
261
+ const n = this.nodes.get(id)
262
+ if (n === undefined) continue
263
+ n.x += dx
264
+ n.y += dy
265
+ moved = true
266
+ }
267
+ if (moved) this.bump()
268
+ }
269
+
270
+ /**
271
+ * Start a frame group drag: snapshot the rect and member centers so the
272
+ * drag applies total deltas from this origin (no float drift). Returns
273
+ * undefined for an unknown frame.
274
+ */
275
+ beginFrameDrag(name: string): FrameDragSnapshot | undefined {
276
+ const rect = this.frames.get(name)
277
+ if (rect === undefined) return undefined
278
+ const centers: Array<readonly [string, number, number]> = []
279
+ for (const n of this.nodes.values()) {
280
+ for (const m of n.memberships) {
281
+ if (m.team === name) { centers.push([n.id, n.x, n.y] as const); break }
282
+ }
283
+ }
284
+ return { name, rect: { ...rect }, centers }
285
+ }
286
+
287
+ /** Apply a total world-space delta from the drag origin to rect + members. */
288
+ applyFrameDrag(snap: FrameDragSnapshot, dx: number, dy: number): void {
289
+ const rect = this.frames.get(snap.name)
290
+ if (rect === undefined) return
291
+ rect.x = snap.rect.x + dx
292
+ rect.y = snap.rect.y + dy
293
+ for (const [id, cx, cy] of snap.centers) {
294
+ const n = this.nodes.get(id)
295
+ if (n === undefined) continue
296
+ n.x = cx + dx
297
+ n.y = cy + dy
298
+ }
299
+ this.bump()
300
+ }
301
+
302
+ /** Nudge (keyboard micro-move) - same math as drag, kept as a named act. */
303
+ nudge(ids: ReadonlyArray<string>, dx: number, dy: number): void { this.dragNodes(ids, dx, dy) }
304
+
305
+ /** Set selection: nodes whose card rect intersects the world-space marquee. */
306
+ marqueeSelect(rect: LayoutRect, additive: boolean): void {
307
+ if (!additive) this.selection.clear()
308
+ for (const n of this.nodes.values()) {
309
+ const r = nodeRect(n)
310
+ const intersects = r.x < rect.x + rect.w && r.x + r.w > rect.x && r.y < rect.y + rect.h && r.y + r.h > rect.y
311
+ if (intersects) this.selection.add(n.id)
312
+ }
313
+ this.bump()
314
+ }
315
+
316
+ /** Replace the whole selection (click / Shift-click add / Esc clears). */
317
+ setSelection(ids: ReadonlyArray<string>): void {
318
+ this.selection = new Set(ids)
319
+ this.bump()
320
+ }
321
+
322
+ /**
323
+ * Poll-cycle maintenance: insert or refresh one node without touching
324
+ * its position (the live model is authoritative between saves; a poll
325
+ * must never snap a dragged card back to its last-saved spot). New
326
+ * nodes seat at the given coordinates (saved spot or hash seat).
327
+ */
328
+ upsertNode(n: WorldNode): void {
329
+ const existing = this.nodes.get(n.id)
330
+ if (existing === undefined) {
331
+ this.nodes.set(n.id, n)
332
+ this.bump()
333
+ return
334
+ }
335
+ existing.label = n.label
336
+ existing.name = n.name
337
+ existing.live = n.live
338
+ existing.memberships = n.memberships
339
+ this.bump()
340
+ }
341
+
342
+ /** Drop a node that left the joined roster; clears it from selection. */
343
+ removeNode(id: string): void {
344
+ if (this.nodes.delete(id)) {
345
+ this.selection.delete(id)
346
+ this.bump()
347
+ }
348
+ }
349
+
350
+ /** Set or replace one frame rect (adopted layout or derived initial). */
351
+ setFrame(name: string, rect: FrameGeom): void {
352
+ this.frames.set(name, rect)
353
+ this.bump()
354
+ }
355
+
356
+ /** Drop a frame whose team vanished from the state payload. */
357
+ removeFrame(name: string): void {
358
+ if (this.frames.delete(name)) this.bump()
359
+ }
360
+
361
+ /** Update a peer node's reachability score (render attr; no bump unless it changed). */
362
+ upsertScore(id: string, score: number | undefined): void {
363
+ const n = this.nodes.get(id)
364
+ if (n === undefined || n.score === score) return
365
+ n.score = score
366
+ this.bump()
367
+ }
368
+
369
+ /** Node center positions, for the layout document builder. */
370
+ positions(): Map<string, LayoutPoint> {
371
+ const out = new Map<string, LayoutPoint>()
372
+ for (const [id, n] of this.nodes) out.set(id, { x: n.x, y: n.y })
373
+ return out
374
+ }
375
+
376
+ /** Frame rectangles, for the layout document builder. */
377
+ frameRects(): Map<string, LayoutRect> {
378
+ const out = new Map<string, LayoutRect>()
379
+ for (const [name, r] of this.frames) out.set(name, { ...r })
380
+ return out
381
+ }
382
+
383
+ /** Member ids of one team, in routing-priority (index) order. */
384
+ teamMemberIds(name: string): string[] {
385
+ const rows: Array<{ id: string; index: number }> = []
386
+ for (const n of this.nodes.values()) {
387
+ const m = n.memberships.find(x => x.team === name)
388
+ if (m !== undefined) rows.push({ id: n.id, index: m.index })
389
+ }
390
+ return rows.sort((a, b) => a.index - b.index).map(r => r.id)
391
+ }
392
+
393
+ /**
394
+ * Replace one team's roster (the single primitive every write action and
395
+ * its undo go through): entries for OTHER teams are preserved verbatim
396
+ * in their array position, this team's entries are rebuilt from `ids`
397
+ * (order = routing priority, indexes renormalized to 0..n-1 so the P
398
+ * badges stay continuous). One bump for the whole operation.
399
+ */
400
+ setTeamMembers(name: string, ids: ReadonlyArray<string>): void {
401
+ for (const n of this.nodes.values()) {
402
+ const rest = n.memberships.filter(m => m.team !== name)
403
+ const idx = ids.indexOf(n.id)
404
+ if (idx >= 0) rest.push({ team: name, index: idx })
405
+ n.memberships = rest
406
+ }
407
+ this.bump()
408
+ }
409
+
410
+ /** Union of node cards and frame rects; null when the canvas is empty. */
411
+ contentBounds(): WorldBounds | null {
412
+ let minX = Number.POSITIVE_INFINITY
413
+ let minY = Number.POSITIVE_INFINITY
414
+ let maxX = Number.NEGATIVE_INFINITY
415
+ let maxY = Number.NEGATIVE_INFINITY
416
+ const absorb = (r: LayoutRect): void => {
417
+ minX = Math.min(minX, r.x)
418
+ minY = Math.min(minY, r.y)
419
+ maxX = Math.max(maxX, r.x + r.w)
420
+ maxY = Math.max(maxY, r.y + r.h)
421
+ }
422
+ for (const n of this.nodes.values()) {
423
+ if (n.remote === true) continue // peer cards hang outside the field
424
+ absorb(nodeRect(n))
425
+ }
426
+ for (const r of this.frames.values()) absorb(r)
427
+ return minX === Number.POSITIVE_INFINITY ? null : { minX, minY, maxX, maxY }
428
+ }
429
+
430
+ /**
431
+ * Build from one state payload plus the persisted layout (when the host
432
+ * has one). Joined sessions only (same roster rule as the 3D scene);
433
+ * saved positions win, the deterministic card-scaled ring seat
434
+ * falls back - the same world spot the 3D grid plane shows for an
435
+ * unsaved node. Frames come from the layout when present; a frame with
436
+ * neither saved rect nor members is skipped. The layout is clamped with
437
+ * the host-mirror discipline, so malformed rows fall through to seats
438
+ * exactly like the 3D scene's typeof guard.
439
+ */
440
+ static fromState(input: {
441
+ sessions: ReadonlyArray<SessionLite>
442
+ teams: ReadonlyArray<TeamLite>
443
+ layout: unknown
444
+ }): WorldModel {
445
+ const model = new WorldModel()
446
+ const doc: LayoutDoc | undefined = clampDoc(input.layout)
447
+
448
+ const memberships = new Map<string, Array<Membership>>()
449
+ for (const team of input.teams) {
450
+ for (let i = 0; i < team.members.length; i++) {
451
+ const id = team.members[i]!.id
452
+ const list = memberships.get(id) ?? []
453
+ list.push({ team: team.name, index: i })
454
+ memberships.set(id, list)
455
+ }
456
+ }
457
+
458
+ for (const s of input.sessions) {
459
+ if (s.joined !== true) continue
460
+ const saved: LayoutPoint | undefined = doc?.nodes[s.id]
461
+ let x: number
462
+ let y: number
463
+ if (saved !== undefined && typeof saved.x === 'number' && typeof saved.y === 'number') {
464
+ x = saved.x
465
+ y = saved.y
466
+ } else {
467
+ const p = planSeatClear(s.id, [...model.nodes.values()])
468
+ x = p.x
469
+ y = p.y
470
+ }
471
+ model.nodes.set(s.id, {
472
+ id: s.id,
473
+ x,
474
+ y,
475
+ label: s.label,
476
+ team: s.team,
477
+ name: s.name,
478
+ live: s.live !== false,
479
+ memberships: memberships.get(s.id) ?? [],
480
+ })
481
+ }
482
+
483
+ for (const team of input.teams) {
484
+ const savedRect = doc?.frames[team.name]
485
+ if (savedRect !== undefined) {
486
+ model.frames.set(team.name, { x: savedRect.x, y: savedRect.y, w: savedRect.w, h: savedRect.h })
487
+ continue
488
+ }
489
+ const memberRects: Array<LayoutRect> = []
490
+ for (const m of team.members) {
491
+ const n = model.nodes.get(m.id)
492
+ if (n !== undefined) memberRects.push(nodeRect(n))
493
+ }
494
+ if (memberRects.length > 0) model.frames.set(team.name, deriveInitialFrame(memberRects))
495
+ }
496
+
497
+ model.viewport = doc !== undefined ? { ...doc.viewport } : { x: 0, y: 0, scale: 1 }
498
+ return model
499
+ }
500
+ }
501
+
502
+ /** Functional entry point; see {@link WorldModel.fromState} for the contract. */
503
+ export function buildWorld(input: {
504
+ sessions: ReadonlyArray<SessionLite>
505
+ teams: ReadonlyArray<TeamLite>
506
+ layout: unknown
507
+ }): WorldModel {
508
+ return WorldModel.fromState(input)
509
+ }
@@ -0,0 +1,25 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "lib": [
7
+ "ES2022",
8
+ "DOM"
9
+ ],
10
+ "strict": true,
11
+ "noEmit": true,
12
+ "skipLibCheck": true,
13
+ "paths": {
14
+ "@/*": [
15
+ "./src/*"
16
+ ]
17
+ },
18
+ "types": [
19
+ "vite/client"
20
+ ]
21
+ },
22
+ "include": [
23
+ "src"
24
+ ]
25
+ }
@@ -0,0 +1,16 @@
1
+ import { defineConfig } from 'vite'
2
+ import { fileURLToPath, URL } from 'node:url'
3
+
4
+ const distDir = fileURLToPath(new URL('../assets/nexusDist', import.meta.url))
5
+
6
+ export default defineConfig({
7
+ plugins: [],
8
+ base: './',
9
+ resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } },
10
+ build: {
11
+ outDir: distDir,
12
+ emptyOutDir: true,
13
+ sourcemap: false,
14
+ rollupOptions: { output: { inlineDynamicImports: true } },
15
+ },
16
+ })