@songsid/agend 2.4.0-beta.15 → 2.4.0-beta.17

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 (217) hide show
  1. package/dist/accounts-profiles.d.ts +1 -1
  2. package/dist/accounts-view.d.ts +3 -0
  3. package/dist/accounts-view.js.map +1 -1
  4. package/dist/auth-api.js +6 -0
  5. package/dist/auth-api.js.map +1 -1
  6. package/dist/backend/claude-code.d.ts +15 -1
  7. package/dist/backend/claude-code.js +64 -8
  8. package/dist/backend/claude-code.js.map +1 -1
  9. package/dist/backend/kiro.js +6 -1
  10. package/dist/backend/kiro.js.map +1 -1
  11. package/dist/backend/types.d.ts +8 -0
  12. package/dist/backend/types.js.map +1 -1
  13. package/dist/backend-command.d.ts +4 -2
  14. package/dist/backend-command.js +3 -0
  15. package/dist/backend-command.js.map +1 -1
  16. package/dist/browser-backend-signins.d.ts +1 -0
  17. package/dist/browser-backend-signins.js +3 -1
  18. package/dist/browser-backend-signins.js.map +1 -1
  19. package/dist/build-provenance.json +1 -1
  20. package/dist/classic-channel-manager.d.ts +1 -0
  21. package/dist/classic-channel-manager.js +4 -1
  22. package/dist/classic-channel-manager.js.map +1 -1
  23. package/dist/claude-credential-change.d.ts +22 -0
  24. package/dist/claude-credential-change.js +47 -0
  25. package/dist/claude-credential-change.js.map +1 -0
  26. package/dist/claude-credential-launch.d.ts +63 -0
  27. package/dist/claude-credential-launch.js +315 -0
  28. package/dist/claude-credential-launch.js.map +1 -0
  29. package/dist/claude-credential-runner.d.ts +1 -0
  30. package/dist/claude-credential-runner.js +56 -0
  31. package/dist/claude-credential-runner.js.map +1 -0
  32. package/dist/claude-credential-vault.d.ts +57 -0
  33. package/dist/claude-credential-vault.js +277 -0
  34. package/dist/claude-credential-vault.js.map +1 -0
  35. package/dist/claude-request-observation.d.ts +7 -0
  36. package/dist/claude-request-observation.js +26 -0
  37. package/dist/claude-request-observation.js.map +1 -0
  38. package/dist/claude-setup-token-transport.d.ts +9 -0
  39. package/dist/claude-setup-token-transport.js +82 -0
  40. package/dist/claude-setup-token-transport.js.map +1 -0
  41. package/dist/claude-setup-token.d.ts +51 -0
  42. package/dist/claude-setup-token.js +83 -0
  43. package/dist/claude-setup-token.js.map +1 -0
  44. package/dist/cli-audited.js +15 -12
  45. package/dist/cli-audited.js.map +1 -1
  46. package/dist/command-report.d.ts +11 -0
  47. package/dist/command-report.js +2 -0
  48. package/dist/command-report.js.map +1 -0
  49. package/dist/config-validator.js +9 -0
  50. package/dist/config-validator.js.map +1 -1
  51. package/dist/config.js +4 -2
  52. package/dist/config.js.map +1 -1
  53. package/dist/daemon.d.ts +5 -0
  54. package/dist/daemon.js +65 -3
  55. package/dist/daemon.js.map +1 -1
  56. package/dist/fleet-level-config.d.ts +1 -1
  57. package/dist/fleet-level-config.js +1 -0
  58. package/dist/fleet-level-config.js.map +1 -1
  59. package/dist/fleet-manager.d.ts +53 -2
  60. package/dist/fleet-manager.js +511 -22
  61. package/dist/fleet-manager.js.map +1 -1
  62. package/dist/general-knowledge/skills/web-appearance/SKILL.md +4 -1
  63. package/dist/general-knowledge/skills/web-dashboard/SKILL.md +25 -6
  64. package/dist/host-monitoring-config.d.ts +15 -0
  65. package/dist/host-monitoring-config.js +36 -0
  66. package/dist/host-monitoring-config.js.map +1 -0
  67. package/dist/host-monitoring-history-files.d.ts +2 -0
  68. package/dist/host-monitoring-history-files.js +16 -0
  69. package/dist/host-monitoring-history-files.js.map +1 -0
  70. package/dist/host-monitoring-history-policy.d.ts +55 -0
  71. package/dist/host-monitoring-history-policy.js +93 -0
  72. package/dist/host-monitoring-history-policy.js.map +1 -0
  73. package/dist/host-monitoring-history-store.d.ts +52 -0
  74. package/dist/host-monitoring-history-store.js +176 -0
  75. package/dist/host-monitoring-history-store.js.map +1 -0
  76. package/dist/host-monitoring-history-worker.d.ts +1 -0
  77. package/dist/host-monitoring-history-worker.js +50 -0
  78. package/dist/host-monitoring-history-worker.js.map +1 -0
  79. package/dist/host-monitoring-history.d.ts +71 -0
  80. package/dist/host-monitoring-history.js +265 -0
  81. package/dist/host-monitoring-history.js.map +1 -0
  82. package/dist/host-monitoring-platform.d.ts +73 -0
  83. package/dist/host-monitoring-platform.js +273 -0
  84. package/dist/host-monitoring-platform.js.map +1 -0
  85. package/dist/host-monitoring-runtime.d.ts +34 -0
  86. package/dist/host-monitoring-runtime.js +80 -0
  87. package/dist/host-monitoring-runtime.js.map +1 -0
  88. package/dist/host-monitoring-settings-store.d.ts +5 -0
  89. package/dist/host-monitoring-settings-store.js +49 -0
  90. package/dist/host-monitoring-settings-store.js.map +1 -0
  91. package/dist/host-monitoring.d.ts +106 -0
  92. package/dist/host-monitoring.js +157 -0
  93. package/dist/host-monitoring.js.map +1 -0
  94. package/dist/hub/group-policy/resources.d.ts +1 -1
  95. package/dist/instance-config-impact.js +1 -1
  96. package/dist/instance-config-impact.js.map +1 -1
  97. package/dist/instructions.js +1 -0
  98. package/dist/instructions.js.map +1 -1
  99. package/dist/locale.d.ts +2 -0
  100. package/dist/locale.js +13 -1
  101. package/dist/locale.js.map +1 -1
  102. package/dist/office-share-page.js +1 -1
  103. package/dist/office-share-page.js.map +1 -1
  104. package/dist/profile-signins.d.ts +1 -0
  105. package/dist/profile-signins.js +3 -1
  106. package/dist/profile-signins.js.map +1 -1
  107. package/dist/public-web-gateway.js +6 -0
  108. package/dist/public-web-gateway.js.map +1 -1
  109. package/dist/public-web-link.d.ts +21 -2
  110. package/dist/public-web-link.js +22 -2
  111. package/dist/public-web-link.js.map +1 -1
  112. package/dist/runtime-cli-install.js +17 -16
  113. package/dist/runtime-cli-install.js.map +1 -1
  114. package/dist/settings-api.d.ts +15 -0
  115. package/dist/settings-api.js +154 -8
  116. package/dist/settings-api.js.map +1 -1
  117. package/dist/settings-baseline.d.ts +1 -0
  118. package/dist/settings-baseline.js +5 -4
  119. package/dist/settings-baseline.js.map +1 -1
  120. package/dist/settings-change.js +10 -0
  121. package/dist/settings-change.js.map +1 -1
  122. package/dist/settings-effect.d.ts +2 -0
  123. package/dist/settings-effect.js +15 -1
  124. package/dist/settings-effect.js.map +1 -1
  125. package/dist/topic-commands.d.ts +7 -1
  126. package/dist/topic-commands.js +98 -88
  127. package/dist/topic-commands.js.map +1 -1
  128. package/dist/transcript-monitor.d.ts +1 -0
  129. package/dist/transcript-monitor.js +18 -0
  130. package/dist/transcript-monitor.js.map +1 -1
  131. package/dist/types.d.ts +2 -0
  132. package/dist/ui/accounts-view.js +16 -8
  133. package/dist/ui/backend-signin.js +3 -1
  134. package/dist/ui/chat-commands.js +2 -0
  135. package/dist/ui/chat-popout-transport.js +49 -19
  136. package/dist/ui/chat-popout.js +15 -1
  137. package/dist/ui/chat-store.js +1 -1
  138. package/dist/ui/claude-credentials.js +123 -0
  139. package/dist/ui/claude-setup-token.js +36 -0
  140. package/dist/ui/contact-auth-client.js +7 -2
  141. package/dist/ui/contact-auth-strings.js +45 -14
  142. package/dist/ui/contact-authorizations.js +47 -26
  143. package/dist/ui/contact-needs.js +4 -4
  144. package/dist/ui/fleet-canvas.js +3 -2
  145. package/dist/ui/fleet-office-card.js +414 -41
  146. package/dist/ui/fleet-office-dialogue.js +3 -3
  147. package/dist/ui/fleet-office-pack.d.ts +67 -1
  148. package/dist/ui/fleet-office-pack.js +412 -18
  149. package/dist/ui/fleet-office-play.js +401 -3
  150. package/dist/ui/fleet-office-skin.js +133 -21
  151. package/dist/ui/fleet-office-store.js +41 -0
  152. package/dist/ui/fleet-office-zones.js +34 -9
  153. package/dist/ui/fleet-office.js +513 -62
  154. package/dist/ui/host-health-model.js +42 -0
  155. package/dist/ui/host-health.js +74 -0
  156. package/dist/ui/office-share-page.js +5 -1
  157. package/dist/ui/panel-chat.js +146 -46
  158. package/dist/ui/panel-fleet.js +24 -11
  159. package/dist/ui/panel-hub.js +5 -1
  160. package/dist/ui/panel-settings.js +5 -4
  161. package/dist/ui/preview-popout.js +134 -0
  162. package/dist/ui/reply-file-preview.js +1 -1
  163. package/dist/ui/search-dialog.js +8 -1
  164. package/dist/ui/settings-capabilities.js +2 -0
  165. package/dist/ui/settings-host-monitoring.js +69 -0
  166. package/dist/ui/settings-registry.js +4 -1
  167. package/dist/ui/settings-shared.js +3 -2
  168. package/dist/ui/shared/accounts.css +8 -4
  169. package/dist/ui/shared/agent-sheet.js +5 -2
  170. package/dist/ui/shared/app-route.js +1 -1
  171. package/dist/ui/shared/app.css +248 -27
  172. package/dist/ui/shared/app.js +8 -1
  173. package/dist/ui/shared/chat-strings.js +10 -4
  174. package/dist/ui/shared/chat-thread.js +42 -10
  175. package/dist/ui/shared/info-tip.js +64 -0
  176. package/dist/ui/shared/instance-nav.js +47 -5
  177. package/dist/ui/shared/jump-idle.js +36 -0
  178. package/dist/ui/shared/office-dialogue.css +12 -0
  179. package/dist/ui/shared/preview-panel.js +44 -18
  180. package/dist/ui/shared/preview-popout.css +11 -0
  181. package/dist/ui/shared/theme.js +15 -1
  182. package/dist/ui/shared/ui-menu.js +17 -11
  183. package/dist/ui/shared/usage-dialog.js +9 -34
  184. package/dist/ui/shared/view-strings.js +2 -0
  185. package/dist/usage/i18n-keys.d.ts +1 -1
  186. package/dist/usage/i18n-keys.js +1 -1
  187. package/dist/usage/i18n-keys.js.map +1 -1
  188. package/dist/usage/providers.d.ts +1 -0
  189. package/dist/usage/providers.js +14 -1
  190. package/dist/usage/providers.js.map +1 -1
  191. package/dist/usage/usage-api.js +5 -2
  192. package/dist/usage/usage-api.js.map +1 -1
  193. package/dist/web-api.d.ts +6 -1
  194. package/dist/web-api.js +83 -9
  195. package/dist/web-api.js.map +1 -1
  196. package/dist/web-chat-history.d.ts +7 -1
  197. package/dist/web-chat-history.js +19 -3
  198. package/dist/web-chat-history.js.map +1 -1
  199. package/dist/web-claude-credentials.d.ts +31 -0
  200. package/dist/web-claude-credentials.js +138 -0
  201. package/dist/web-claude-credentials.js.map +1 -0
  202. package/dist/web-commands.d.ts +5 -2
  203. package/dist/web-commands.js +4 -2
  204. package/dist/web-commands.js.map +1 -1
  205. package/dist/web-office-skins.d.ts +7 -2
  206. package/dist/web-office-skins.js +42 -16
  207. package/dist/web-office-skins.js.map +1 -1
  208. package/dist/web-session.d.ts +20 -1
  209. package/dist/web-session.js +27 -0
  210. package/dist/web-session.js.map +1 -1
  211. package/dist/web-shell-routes.d.ts +1 -1
  212. package/dist/web-shell-routes.js +1 -1
  213. package/dist/web-shell-routes.js.map +1 -1
  214. package/dist/web-terminal.d.ts +4 -0
  215. package/dist/web-terminal.js +16 -1
  216. package/dist/web-terminal.js.map +1 -1
  217. package/package.json +1 -1
@@ -1,10 +1,15 @@
1
1
  // M-3293 (#4119, the light version): play in the Office — pick a person up and drop them anywhere (they walk back to their
2
- // seat along the usual route), and pat or feed them (a few hearts rising over the head and a speech bubble). The
3
- // viewer's own: never stored on the host, never in a share link, never sent to an agent (no request at all), no
4
- // tokens. Pure — fleet-office.js wires it to the pointer, the walkers and the scene.
2
+ // seat along the usual route), and pat or feed them (a few hearts rising over the head and a speech bubble). M-3432
3
+ // (#4255): custom snacks from the active skin pack — drag onto a character to feed (plays optional `eat` strip), or drop
4
+ // on empty floor so nearby idle characters race to it (first eats, others scatter). The viewer's own: never stored on the
5
+ // host, never in a share link, never sent to an agent (unless Tell is ticked on their card). Pure — fleet-office.js wires
6
+ // it to the pointer, the walkers and the scene.
5
7
  //
6
8
  // Who may play: a single zones office on the dashboard itself (not All offices, not a share link's page, not while Edit
7
9
  // layout is open), with the viewer's "Drag and pat" on (per device, on by default) — and never with reduced motion.
10
+ import { LOGICAL, SNACK_LIMITS, own, stripDurationMs } from "./fleet-office-pack.js";
11
+ import { nameHash } from "./fleet-office-model.js";
12
+ import { routeStaysInArea, WALK } from "./fleet-office-zones.js";
8
13
 
9
14
  /**
10
15
  * LONG_MS: a touch held this long without moving picks a person up (a finger that moves first pans, as before); SLOP:
@@ -14,6 +19,20 @@
14
19
  export const PLAY = { LONG_MS: 450, SLOP: 6, REACT_MS: 1800, HEARTS: 3, RISE: 26 };
15
20
  export const PLAY_KINDS = ["pat", "feed"];
16
21
 
22
+ /** M-3432 (#4255): limits and timing for custom snacks, eating, and placed-snack races. */
23
+ export const SNACK = {
24
+ MAX_PLACED: 6,
25
+ MAX_RACERS: 4,
26
+ TTL_MS: 30_000,
27
+ CLAIM_DIST: 18,
28
+ RACE_SPEED_MULT: 1.25,
29
+ SPEED_VARIANCE: 0.08,
30
+ EAT_MIN_MS: 800,
31
+ EAT_MAX_MS: 10_000,
32
+ EAT_DEFAULT_MS: 1800,
33
+ SCATTER_MS: 1800,
34
+ };
35
+
17
36
  /** Whether the viewer may play here: their toggle on, motion allowed, a single zones office of their own dashboard. */
18
37
  export function playAllowed({ on, still, zones, overview, layout, shared }) {
19
38
  return !!on && !still && !!zones && !overview && !layout && !shared;
@@ -82,3 +101,382 @@ export function pruneReactions(map, t) {
82
101
  for (const [n, r] of [...map]) if (t - r.t0 >= PLAY.REACT_MS || t < r.t0) map.delete(n);
83
102
  return map.size > 0;
84
103
  }
104
+
105
+ const SCATTER_WORDS = { en: "Too slow!", "zh-TW": "被搶先了!" };
106
+ /** M-3432 (#4255): brief bubble for a losing snack racer heading back to their seat. */
107
+ export const scatterWords = (lang = "en") => SCATTER_WORDS[lang] || SCATTER_WORDS.en;
108
+
109
+ /** M-3432 (#4255): how long a character eats (ms) — clamped to [EAT_MIN_MS, EAT_MAX_MS], or EAT_DEFAULT_MS without an eat strip. */
110
+ export function eatDurationMs(charEatStrip = null, snackEatStrip = null) {
111
+ const ms = Math.max(stripDurationMs(charEatStrip), stripDurationMs(snackEatStrip));
112
+ return ms > 0 ? Math.max(SNACK.EAT_MIN_MS, Math.min(SNACK.EAT_MAX_MS, ms)) : SNACK.EAT_DEFAULT_MS;
113
+ }
114
+
115
+ /** M-3432 (#4255): deterministic speed multiplier for a racer chasing a placed snack (1.25× ± 8%). */
116
+ export function racerSpeedMult(name, snackKey) {
117
+ const h = nameHash(`${name}\u0000${snackKey}`) % 1001;
118
+ const frac = h / 500 - 1;
119
+ return SNACK.RACE_SPEED_MULT * (1 + frac * SNACK.SPEED_VARIANCE);
120
+ }
121
+
122
+ function areaOfPoint(areas, pt) {
123
+ if (!areas || typeof areas !== "object") return null;
124
+ for (const [k, a] of Object.entries(areas)) {
125
+ const r = a && a.rect;
126
+ if (r && pt.x >= r.x && pt.x <= r.x + r.w && pt.y >= r.y && pt.y <= r.y + r.h) return k;
127
+ }
128
+ return null;
129
+ }
130
+
131
+ /**
132
+ * M-3432 (#4255): client-local snack manager for a single zones office.
133
+ * Handles direct feeds, placed snacks on the floor (max MAX_PLACED, TTL_MS expiry),
134
+ * nearby idle racers (max MAX_RACERS per snack, honoring stayInArea), claim → eat, and losing-racer scatter.
135
+ */
136
+ export function createSnacks() {
137
+ let seq = 0;
138
+ let placed = [];
139
+ const eating = new Map();
140
+ const scatter = new Map();
141
+
142
+ const busySet = () => {
143
+ const busy = new Set(eating.keys());
144
+ for (const p of placed) for (const n of p.racers) busy.add(n);
145
+ return busy;
146
+ };
147
+
148
+ const pickRacers = (item, t, { desks = [], walkers = null, hidden = null, ownArea = false, graph = null } = {}, cap = SNACK.MAX_RACERS) => {
149
+ if (cap <= 0) return [];
150
+ const busy = busySet();
151
+ const candidates = [];
152
+ for (const d of desks) {
153
+ const name = d && d.node && d.node.name;
154
+ if (!name || d.node.state !== "idle" || d.night) continue;
155
+ if (busy.has(name) || (hidden && hidden.has(name))) continue;
156
+ if (ownArea && (item.area === null || d.area !== item.area)) continue;
157
+ const pos = (walkers && walkers.at(name, t)) || d.seat;
158
+ if (!pos) continue;
159
+ if (ownArea && graph && !routeStaysInArea(pos, { x: item.x, y: item.y }, graph, d.area, d.seat)) continue;
160
+ const dist = Math.hypot(pos.x - item.x, pos.y - item.y);
161
+ if (dist > item.radius) continue;
162
+ candidates.push({ name, dist, eta: dist / racerSpeedMult(name, item.id) });
163
+ }
164
+ candidates.sort((a, b) => (a.eta - b.eta) || (a.dist - b.dist) || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
165
+ return candidates.slice(0, cap).map((c) => c.name);
166
+ };
167
+
168
+ const feed = (name, snack, { x, y, t, charEatStrip = null }) => {
169
+ for (const p of placed) p.racers = p.racers.filter((n) => n !== name);
170
+ scatter.delete(name);
171
+ const durationMs = eatDurationMs(charEatStrip, snack && snack.eat);
172
+ eating.set(name, { snackId: snack ? snack.id : null, x, y, t0: t, until: t + durationMs });
173
+ return { durationMs };
174
+ };
175
+
176
+ return {
177
+ feed,
178
+ place(snack, at, t, ctx = {}) {
179
+ if (!snack || !at) return null;
180
+ const x = Math.max(16, Math.min(LOGICAL.w - 16, Math.round(at.x)));
181
+ const y = Math.max(16, Math.min(LOGICAL.h - 16, Math.round(at.y)));
182
+ while (placed.length >= SNACK.MAX_PLACED) placed.shift();
183
+ const item = {
184
+ id: `sn-${++seq}`,
185
+ snackId: snack.id,
186
+ x,
187
+ y,
188
+ t0: t,
189
+ radius: snack.radius ?? SNACK_LIMITS.defaultRadius,
190
+ area: areaOfPoint(ctx.areas, { x, y }),
191
+ racers: [],
192
+ };
193
+ item.racers = pickRacers(item, t, ctx, SNACK.MAX_RACERS);
194
+ placed.push(item);
195
+ return item;
196
+ },
197
+ step(t, ctx = {}) {
198
+ const { desks = [], walkers = null, hidden = null, ownArea = false, graph = null, snacksById = null, charEatOf = null, onClaim = null } = ctx;
199
+ let changed = false;
200
+ for (const [n, e] of [...eating]) if (t >= e.until || t < e.t0) { eating.delete(n); changed = true; }
201
+ for (const [n, s] of [...scatter]) if (t >= s.until || t < s.t0) scatter.delete(n);
202
+ const deskByName = new Map(desks.filter((d) => d && d.node).map((d) => [d.node.name, d]));
203
+ const kept = [];
204
+ for (const p of placed) {
205
+ if (t - p.t0 >= SNACK.TTL_MS || t < p.t0) { changed = true; continue; }
206
+ const beforeLen = p.racers.length;
207
+ p.racers = p.racers.filter((n) => {
208
+ const d = deskByName.get(n);
209
+ if (!d || d.node.state !== "idle" || d.night || eating.has(n) || (hidden && hidden.has(n))) return false;
210
+ if (ownArea) {
211
+ if (p.area === null || d.area !== p.area) return false;
212
+ const pos = (walkers && walkers.at(n, t)) || d.seat;
213
+ if (graph && pos && !routeStaysInArea(pos, { x: p.x, y: p.y }, graph, d.area, d.seat)) return false;
214
+ }
215
+ return true;
216
+ });
217
+ if (p.racers.length !== beforeLen) changed = true;
218
+ // Check if any racer reached CLAIM_DIST.
219
+ const reached = [];
220
+ for (const n of p.racers) {
221
+ const d = deskByName.get(n);
222
+ const pos = (walkers && walkers.at(n, t)) || (d && d.seat);
223
+ if (!pos) continue;
224
+ const dist = Math.hypot(pos.x - p.x, pos.y - p.y);
225
+ if (dist <= SNACK.CLAIM_DIST) reached.push({ name: n, dist, speed: racerSpeedMult(n, p.id) });
226
+ }
227
+ if (reached.length) {
228
+ reached.sort((a, b) => (a.dist - b.dist) || (b.speed - a.speed) || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
229
+ const winner = reached[0].name;
230
+ const losers = p.racers.filter((n) => n !== winner);
231
+ const snack = snacksById ? snacksById.get(p.snackId) || null : null;
232
+ const charEat = typeof charEatOf === "function" ? charEatOf(winner) : null;
233
+ feed(winner, snack, { x: p.x, y: p.y, t, charEatStrip: charEat });
234
+ for (const loser of losers) scatter.set(loser, { t0: t, until: t + SNACK.SCATTER_MS });
235
+ if (typeof onClaim === "function") onClaim(winner, snack, p);
236
+ changed = true;
237
+ continue;
238
+ }
239
+ if (p.racers.length < SNACK.MAX_RACERS) {
240
+ const added = pickRacers(p, t, { desks, walkers, hidden, ownArea, graph }, SNACK.MAX_RACERS - p.racers.length);
241
+ if (added.length) { p.racers.push(...added); changed = true; }
242
+ }
243
+ kept.push(p);
244
+ }
245
+ placed = kept;
246
+ return changed;
247
+ },
248
+ targets(t) {
249
+ const out = new Map();
250
+ for (const [name, e] of eating) {
251
+ if (t >= e.t0 && t < e.until) out.set(name, { x: e.x, y: e.y });
252
+ }
253
+ for (const p of placed) {
254
+ for (const name of p.racers) {
255
+ if (!out.has(name)) out.set(name, { x: p.x, y: p.y, speed: WALK.SPEED * racerSpeedMult(name, p.id) });
256
+ }
257
+ }
258
+ return out;
259
+ },
260
+ placedList() { return [...placed]; },
261
+ eatingOf(name, t) {
262
+ const e = eating.get(name);
263
+ return e && t >= e.t0 && t < e.until ? e : null;
264
+ },
265
+ scatterOf(name, t) {
266
+ const s = scatter.get(name);
267
+ return s && t >= s.t0 && t < s.until ? s : null;
268
+ },
269
+ active(t) {
270
+ if (placed.length > 0) return true;
271
+ for (const e of eating.values()) if (t >= e.t0 && t < e.until) return true;
272
+ for (const s of scatter.values()) if (t >= s.t0 && t < s.until) return true;
273
+ return false;
274
+ },
275
+ /** M-3445: stop a picked-up character's active snack race/eating so dragging takes clean priority. */
276
+ cancel(name) {
277
+ if (!name) return;
278
+ eating.delete(name);
279
+ scatter.delete(name);
280
+ for (const p of placed) p.racers = p.racers.filter((n) => n !== name);
281
+ },
282
+ clear() { placed = []; eating.clear(); scatter.clear(); },
283
+ };
284
+ }
285
+
286
+ /** M-3445 (#4266): logical hit radius for dropping a dragged character onto another seat in their area. */
287
+ export const SEAT_DRAG = { DROP_RADIUS: 36 };
288
+
289
+ /** M-3445 (#4266): whether `name` is allowed by #2691 reservation rules to sit on `seat`. */
290
+ export function canSitOnSeat(seat, name) {
291
+ if (!seat || !name) return false;
292
+ return !seat.for || seat.for.includes(name) || seat.lend === true;
293
+ }
294
+
295
+ /**
296
+ * M-3445 (#4266): every seat in `name`'s current area that `name` can legally be dropped onto without
297
+ * violating #2691 seat reservations (owners keep priority over borrowers; an owner with a free/own reserved
298
+ * seat chooses among their reserved seats). Returns [{ area, slot, x, y, current, occupant }].
299
+ */
300
+ export function seatDropTargets(pack, desks, name) {
301
+ if (!pack || !pack.areas || !Array.isArray(desks) || !name) return [];
302
+ const d = desks.find((x) => x && x.node && x.node.name === name);
303
+ if (!d) return [];
304
+ const area = own(pack.areas, d.area);
305
+ if (!area || !Array.isArray(area.seats) || !area.seats.length) return [];
306
+ const occBySlot = new Map();
307
+ for (const other of desks) {
308
+ if (!other || !other.node || other.node.name === name || other.area !== d.area || other.slot < 0) continue;
309
+ occBySlot.set(other.slot, other.node.name);
310
+ }
311
+ const mine = (s, who) => !!s.for && s.for.includes(who);
312
+ const ownedSlots = [];
313
+ for (let i = 0; i < area.seats.length; i++) if (mine(area.seats[i], name)) ownedSlots.push(i);
314
+ const hasOwnClaim = ownedSlots.some((i) => {
315
+ if (i === d.slot) return true;
316
+ const occ = occBySlot.get(i);
317
+ return !occ || !mine(area.seats[i], occ);
318
+ });
319
+ const out = [];
320
+ for (let i = 0; i < area.seats.length; i++) {
321
+ const s = area.seats[i], occ = occBySlot.get(i) || null;
322
+ if (hasOwnClaim) {
323
+ if (!mine(s, name)) continue;
324
+ if (occ && mine(s, occ)) {
325
+ const oldSeat = d.slot >= 0 ? area.seats[d.slot] : null;
326
+ if (!oldSeat || !mine(oldSeat, occ)) continue;
327
+ }
328
+ } else {
329
+ if (!canSitOnSeat(s, name)) continue;
330
+ if (occ && mine(s, occ)) continue;
331
+ }
332
+ out.push({ area: d.area, slot: i, x: s.x, y: s.y, current: i === d.slot, occupant: occ });
333
+ }
334
+ return out;
335
+ }
336
+
337
+ /** M-3445 (#4266): nearest legal seat in `name`'s current area within `radius` of logical point `at`, or null. */
338
+ export function findSeatDropTarget(pack, desks, name, at, radius = SEAT_DRAG.DROP_RADIUS) {
339
+ if (!at || !Number.isFinite(at.x) || !Number.isFinite(at.y)) return null;
340
+ let best = null, bestDist = radius;
341
+ for (const cand of seatDropTargets(pack, desks, name)) {
342
+ const dist = Math.hypot(at.x - cand.x, at.y - cand.y);
343
+ if (dist <= bestDist && (!best || dist < bestDist || cand.slot < best.slot)) {
344
+ best = cand;
345
+ bestDist = dist;
346
+ }
347
+ }
348
+ return best;
349
+ }
350
+
351
+ /**
352
+ * M-3445 (#4266): apply dropping `name` onto `target` seat in their area.
353
+ * Updates live `seats` map and per-office `prefs` ({ [name]: { [area]: slot } }), swapping cleanly when the
354
+ * target is occupied and the vacated seat is legal for the previous occupant, and clearing stale prefs for
355
+ * taken slots so other agents returning to the area later do not collide.
356
+ */
357
+ export function applySeatDrop({ pack, desks, seats, prefs, name, target }) {
358
+ const nextSeats = new Map(seats || []);
359
+ const nextPrefs = {};
360
+ for (const [k, v] of Object.entries(prefs || {})) {
361
+ if (v && typeof v === "object" && !Array.isArray(v)) nextPrefs[k] = { ...v };
362
+ }
363
+ const clearSlotPref = (areaName, slot, keepName) => {
364
+ for (const [who, byArea] of Object.entries(nextPrefs)) {
365
+ if (who === keepName || !byArea) continue;
366
+ if (own(byArea, areaName) === slot) {
367
+ delete byArea[areaName];
368
+ if (!Object.keys(byArea).length) delete nextPrefs[who];
369
+ }
370
+ }
371
+ };
372
+ const d = Array.isArray(desks) ? desks.find((x) => x && x.node && x.node.name === name) : null;
373
+ const area = pack && pack.areas ? own(pack.areas, target.area) : null;
374
+ nextSeats.set(name, { area: target.area, slot: target.slot, x: target.x, y: target.y });
375
+ nextPrefs[name] = { ...(nextPrefs[name] || {}), [target.area]: target.slot };
376
+ clearSlotPref(target.area, target.slot, name);
377
+ let swapped = null;
378
+ const occ = target.occupant;
379
+ if (occ && occ !== name) {
380
+ const oldSlot = d && d.area === target.area && d.slot >= 0 ? d.slot : -1;
381
+ const oldSeat = oldSlot >= 0 && area && area.seats ? area.seats[oldSlot] : null;
382
+ if (oldSeat && canSitOnSeat(oldSeat, occ)) {
383
+ nextSeats.set(occ, { area: target.area, slot: oldSlot, x: oldSeat.x, y: oldSeat.y });
384
+ nextPrefs[occ] = { ...(nextPrefs[occ] || {}), [target.area]: oldSlot };
385
+ clearSlotPref(target.area, oldSlot, occ);
386
+ swapped = { name: occ, slot: oldSlot, seat: { x: oldSeat.x, y: oldSeat.y } };
387
+ } else {
388
+ nextSeats.delete(occ);
389
+ if (nextPrefs[occ]) {
390
+ delete nextPrefs[occ][target.area];
391
+ if (!Object.keys(nextPrefs[occ]).length) delete nextPrefs[occ];
392
+ }
393
+ }
394
+ }
395
+ return { seats: nextSeats, prefs: nextPrefs, swapped };
396
+ }
397
+
398
+ /**
399
+ * M-3445 (#4266): when `stayInArea` is enabled and a dragged character is dropped on empty floor (not on a seat),
400
+ * ensure they land at a point whose walk back to `seat` stays inside `area` (clamping to the area rect, or
401
+ * returning straight to `seat` if no in-area route exists).
402
+ */
403
+ export function clampDropForArea(at, seat, graph, area) {
404
+ if (!seat) return at;
405
+ if (!at || !graph || !graph.areas) return seat;
406
+ const a = own(graph.areas, area);
407
+ if (!a || !a.rect) return seat;
408
+ const r = a.rect;
409
+ const inArea = (p) => p.x >= r.x && p.x <= r.x + r.w && p.y >= r.y && p.y <= r.y + r.h;
410
+ if (inArea(at) && routeStaysInArea(at, seat, graph, area, null)) return at;
411
+ const pad = Math.min(4, r.w / 2, r.h / 2);
412
+ const clamped = {
413
+ x: Math.max(r.x + pad, Math.min(r.x + r.w - pad, at.x)),
414
+ y: Math.max(r.y + pad, Math.min(r.y + r.h - pad, at.y)),
415
+ };
416
+ return routeStaysInArea(clamped, seat, graph, area, null) ? clamped : { x: seat.x, y: seat.y };
417
+ }
418
+
419
+ /**
420
+ * M-3432 (#4255): owned snack-drag lifecycle controller.
421
+ * Manages the four global pointer/key listeners with a single cancellation handle and generation fence so
422
+ * drag replacement, play loss, Office/skin changes, and unmount immediately detach all listeners and block stale commits.
423
+ */
424
+ export function createSnackDrag(win = globalThis) {
425
+ let gen = 0;
426
+ let cancelActive = null;
427
+ return {
428
+ active() { return cancelActive !== null; },
429
+ start({ pid, sx, sy, slop = PLAY.SLOP, onMove, onCommit, onCancel }) {
430
+ if (cancelActive) cancelActive();
431
+ const myGen = ++gen;
432
+ let dragging = false, lastDrop = null, live = true;
433
+ const detach = () => {
434
+ if (!live) return;
435
+ live = false;
436
+ win.removeEventListener?.("pointermove", onMoveEv, true);
437
+ win.removeEventListener?.("pointerup", onUpEv, true);
438
+ win.removeEventListener?.("pointercancel", onCancelEv, true);
439
+ win.removeEventListener?.("keydown", onKeyEv, true);
440
+ if (cancelActive === cancel) cancelActive = null;
441
+ };
442
+ const cancel = () => {
443
+ if (!live) return;
444
+ const wasDragging = dragging;
445
+ detach();
446
+ lastDrop = null;
447
+ if (typeof onCancel === "function") onCancel({ dragging: wasDragging });
448
+ };
449
+ const finish = (commit) => {
450
+ if (!live || myGen !== gen) return;
451
+ const drop = lastDrop, wasDragging = dragging;
452
+ detach();
453
+ lastDrop = null;
454
+ if (commit && wasDragging && drop) {
455
+ if (typeof onCommit === "function") onCommit(drop);
456
+ } else if (typeof onCancel === "function") {
457
+ onCancel({ dragging: wasDragging });
458
+ }
459
+ };
460
+ const onMoveEv = (ev) => {
461
+ if (!live || myGen !== gen || ev.pointerId !== pid) return;
462
+ const starting = !dragging && Math.hypot(ev.clientX - sx, ev.clientY - sy) > slop;
463
+ if (starting) dragging = true;
464
+ if (!dragging) return;
465
+ lastDrop = typeof onMove === "function" ? onMove(ev, { starting }) : null;
466
+ };
467
+ const onUpEv = (ev) => { if (ev.pointerId === pid) finish(true); };
468
+ const onCancelEv = (ev) => { if (ev.pointerId === pid) finish(false); };
469
+ const onKeyEv = (ev) => { if (ev.key === "Escape") { ev.preventDefault?.(); finish(false); } };
470
+ cancelActive = cancel;
471
+ win.addEventListener?.("pointermove", onMoveEv, true);
472
+ win.addEventListener?.("pointerup", onUpEv, true);
473
+ win.addEventListener?.("pointercancel", onCancelEv, true);
474
+ win.addEventListener?.("keydown", onKeyEv, true);
475
+ return cancel;
476
+ },
477
+ retire() {
478
+ gen++;
479
+ if (cancelActive) cancelActive();
480
+ },
481
+ };
482
+ }