-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
4409 lines (3883 loc) · 160 KB
/
Copy pathplugin.js
File metadata and controls
4409 lines (3883 loc) · 160 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Hermes Office — a floor of desks for every Bot Mode agent.
*
* Same data as Bot Mode: profiles.list, ui_meta hermes-bots, host.state.busy.
* Click a nameplate to give them a task. That task lands in the same
* Bot Chat session Bot Mode already uses. Hover, pet, and drag the face.
*/
import {
atom,
cn,
haptic,
host,
PALETTE_AREA,
profileColor,
ROUTES_AREA,
SIDEBAR_NAV_AREA,
STATUSBAR_AREAS,
Tip,
useQuery,
useValue
} from '@hermes/plugin-sdk'
import { Fragment, useEffect, useRef, useState } from 'react'
import { jsx, jsxs } from 'react/jsx-runtime'
const ID = 'hermes-office'
const ROSTER_KEY = [ID, 'roster']
const META_NS = 'hermes-bots'
const DRAG_PX = 8
const SLEEP_HOLD_MS = 1200
const $seats = atom({})
const $drag = atom(null)
const $fx = atom({})
const $peekUntil = atom(0)
const $walks = atom({})
const $roam = atom({})
const $clockKind = atom('digital')
const $clockPos = atom(null)
const $selected = atom(null)
const $focusTask = atom(0)
const $jobs = atom({})
const $backdrop = atom('carpet')
const $game = atom(null)
const $pizza = atom({ winner: null, at: 0 })
const $puffs = atom([])
const $planes = atom([])
const $trophies = atom({})
const $lastTask = atom({})
const $week = atom(null)
const $month = atom(null)
const $hint = atom('off')
const $news = atom({})
const $ritual = atom({ hour: -1, at: 0 })
const RITUAL_MS = 2800
const RITUAL_WINDOW_MS = 10 * 60 * 1000
const $petPing = atom({})
const OFFICE_NS = 'hermes-office'
const BORED_MS = 2 * 24 * 60 * 60 * 1000
let puffSeq = 0
// A paper plane from the task bar to a desk. Root relative coordinates.
function flyPlane(from, to) {
if (!from || !to) {
return
}
const id = ++puffSeq
$planes.set([...$planes.get(), { id, from, to }])
setTimeout(() => {
$planes.set($planes.get().filter(p => p.id !== id))
}, 900)
}
// One more finished task on the shelf for this bot. Kept locally for speed and
// mirrored onto the bot's profile (ui_meta, our own namespace) so the count
// follows the profile rather than this machine.
function addTrophy(name) {
const count = ($trophies.get()[name] || 0) + 1
const next = { ...$trophies.get(), [name]: count }
$trophies.set(next)
savePref('trophies', next)
try {
Promise.resolve(
host.request('profiles.configure', { name, ui_meta: { [OFFICE_NS]: { stars: count } } })
).catch(() => undefined)
} catch {
/* older gateway */
}
}
// If the profile already carries more stars than we know about (another
// machine, or a fresh install), take the higher number.
function seedTrophies(roster) {
const local = $trophies.get()
let changed = false
const next = { ...local }
for (const bot of roster || []) {
const stars = Number(bot?.ui_meta?.[OFFICE_NS]?.stars || 0)
if (stars > (next[bot.name] || 0)) {
next[bot.name] = stars
changed = true
}
}
if (changed) {
$trophies.set(next)
savePref('trophies', next)
}
}
// Employee of the month: tasks per bot this calendar month.
function bumpMonth(name) {
const next = monthBump($month.get(), name, Date.now())
$month.set(next)
savePref('month', next)
}
// No board for this month yet (first run of the feature, or a fresh install
// with stars on the profiles): start it from the all time stars so the wall is
// not empty. From then on it counts real completions and resets on the first.
function seedMonth(roster) {
const cur = $month.get()
const start = monthStart(new Date())
if (cur) {
return
}
const stars = $trophies.get()
const tasks = {}
for (const bot of roster || []) {
if (stars[bot.name] > 0) {
tasks[bot.name] = stars[bot.name]
}
}
const holder = monthLeader({ tasks }, null)
if (!holder) {
return
}
const next = { start, tasks, holder, seeded: true }
$month.set(next)
savePref('month', next)
}
// Weekly recap: a few counters that reset every Monday.
function bumpWeek(key, name) {
const now = Date.now()
const next = weekBump($week.get(), key, name, now)
$week.set(next)
savePref('week', next)
}
// Job done: confetti at the desk, a trophy, then off to the bar.
// Two paths can see the same completion (the job poller and the focused
// busy edge). Each round gets a token in startRound; a round celebrates once.
function celebrate(name) {
const now = Date.now()
const row = $fx.get()[name] || {}
const round = completionToken(row)
if (round === null) {
return
}
patchFx(name, { doneRound: round, clapUntil: now + 1100, confettiUntil: now + 950, bangUntil: now + 1500, nap: false, goBar: true, goHome: false })
addTrophy(name)
bumpWeek('tasks', name)
bumpMonth(name)
leaveNote(name, now)
advanceHint('play')
}
// A finished task leaves a note on the desk until the chat is opened.
function leaveNote(name, at) {
const next = { ...$news.get(), [name]: at || Date.now() }
$news.set(next)
savePref('news', next)
}
function readNote(name) {
if (!$news.get()[name]) {
return
}
const next = { ...$news.get() }
delete next[name]
$news.set(next)
savePref('news', next)
}
// A little dust ring at a foot position. Gone after half a second.
function puffAt(x, y) {
const id = ++puffSeq
$puffs.set([...$puffs.get(), { id, x, y, t0: Date.now() }])
setTimeout(() => {
$puffs.set($puffs.get().filter(p => p.id !== id))
}, 520)
}
// Honour the OS "reduce motion" setting for the bouncy bits. Walks stay.
let reducedCache = null
function reducedMotion() {
if (reducedCache === null) {
reducedCache = typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches
}
return reducedCache
}
const PIZZA_MS = 14000
// Room skins. Every skin is a flat wall band plus a seamless floor tile, drawn
// as tiny SVGs and embedded as data URIs (Hermes loads plugin.js through a blob
// URL, so sibling image files are not served). Nothing here has a vanishing
// point: the paper-doll sprites and CSS desks sit on this floor, so the floor
// has to be the same flat plane they are.
const WALL_H = 86
function svgUri(svg) {
const flat = svg.replace(/\s+/g, ' ').replace(/> </g, '><').trim()
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(flat)}`
}
function svgTile(width, height, body) {
return svgUri(`<svg xmlns='http://www.w3.org/2000/svg' width='${width}' height='${height}'>${body}</svg>`)
}
function speckle(points, fill, r = 1) {
return `<g fill='${fill}'>${points.map(([x, y]) => `<circle cx='${x}' cy='${y}' r='${r}'/>`).join('')}</g>`
}
// Running bond bricks. Tile width must be a multiple of brick + joint and the
// colour list must be exactly four long so the half-offset rows wrap cleanly.
function brickRows(width, height, brick, joint, mortar, colors) {
const step = brick.w + joint
const rowH = brick.h + joint
let out = `<rect width='${width}' height='${height}' fill='${mortar}'/>`
for (let row = 0, y = 0; y < height; row++, y += rowH) {
const offset = row % 2 ? -Math.floor(step / 2) : 0
for (let i = 0, x = offset; x < width; i++, x += step) {
out += `<rect x='${x}' y='${y}' width='${brick.w}' height='${brick.h}' rx='1' fill='${colors[(row * 3 + i) % colors.length]}'/>`
}
}
return out
}
// Straight-on floorboards. Horizontal planks, staggered end joints, no taper.
function plankRows(width, plankH, colors, seam, joints) {
let out = ''
colors.forEach((fill, row) => {
const y = row * plankH
out += `<rect x='0' y='${y}' width='${width}' height='${plankH}' fill='${fill}'/>`
out += `<rect x='0' y='${y}' width='${width}' height='1' fill='rgba(255,255,255,.14)'/>`
out += `<rect x='0' y='${y + plankH - 1}' width='${width}' height='1' fill='${seam}'/>`
out += `<rect x='${joints[row]}' y='${y}' width='2' height='${plankH}' fill='${seam}'/>`
out += `<rect x='${(joints[row] + 9) % width}' y='${y + 7}' width='26' height='1' fill='rgba(0,0,0,.09)'/>`
out += `<rect x='${(joints[row] + 61) % width}' y='${y + 15}' width='34' height='1' fill='rgba(0,0,0,.08)'/>`
})
return out
}
// Top-down dance floor. A fixed 4x4 pattern so the tile repeats without seams.
function checkerTiles(cell, colors, grid) {
const map = [
[0, 1, 2, 1],
[1, 3, 1, 0],
[2, 1, 0, 1],
[1, 0, 1, 3]
]
let out = ''
map.forEach((row, r) => {
row.forEach((c, i) => {
const x = i * cell
const y = r * cell
out += `<rect x='${x}' y='${y}' width='${cell}' height='${cell}' fill='${colors[c]}'/>`
out += `<rect x='${x + 2.5}' y='${y + 2.5}' width='${cell - 5}' height='${cell - 5}' fill='none' stroke='rgba(255,255,255,${c === 3 ? '.35' : '.1'})' stroke-width='1'/>`
})
})
const size = cell * 4
return `${out}<path d='M0 0h${size}M0 ${cell}h${size}M0 ${cell * 2}h${size}M0 ${cell * 3}h${size}M0 0v${size}M${cell} 0v${size}M${cell * 2} 0v${size}M${cell * 3} 0v${size}' stroke='${grid}' stroke-width='2'/>`
}
const OFFICE_SKINS = {
carpet: {
wallColor: '#ebe2d1',
wallSize: '160px 86px',
wall: svgTile(160, WALL_H, `
<rect width='160' height='86' fill='#ebe2d1'/>
${speckle([[23, 14], [71, 38], [118, 22], [143, 49], [47, 51], [95, 9], [12, 44], [131, 8]], '#e1d6c2')}
<rect y='60' width='160' height='3' fill='#c9b99d'/>
<rect y='60' width='160' height='1' fill='#f7f1e4'/>
<rect y='63' width='160' height='17' fill='#dccfb7'/>
<g fill='#c6b699'><rect x='39' y='66' width='2' height='11'/><rect x='79' y='66' width='2' height='11'/><rect x='119' y='66' width='2' height='11'/><rect x='159' y='66' width='1' height='11'/><rect y='66' width='1' height='11'/></g>
<rect y='80' width='160' height='6' fill='#8a755b'/>
<rect y='80' width='160' height='1' fill='#aa937b'/>
`),
floorColor: '#587e8f',
floorSize: '96px 96px',
floor: svgTile(96, 96, `
<rect width='96' height='96' fill='#587e8f'/>
<rect x='48' width='48' height='48' fill='#557b8c'/>
<rect y='48' width='48' height='48' fill='#557b8c'/>
${speckle([[6, 9], [21, 30], [39, 14], [30, 42], [11, 38], [58, 6], [70, 27], [88, 12], [79, 41], [63, 39], [9, 57], [27, 74], [41, 60], [18, 89], [36, 84], [55, 60], [73, 77], [89, 58], [66, 90], [84, 86], [46, 24], [90, 30], [2, 26], [70, 62], [14, 76]], '#668c9c')}
${speckle([[16, 20], [33, 5], [75, 16], [52, 34], [24, 62], [4, 78], [92, 70], [60, 72], [44, 92], [80, 50], [38, 70], [86, 94]], '#4b6f80')}
<path d='M48.5 0v96M0 48.5h96' stroke='#4d7283' stroke-width='1'/>
`)
},
loft: {
wallColor: '#c9baa9',
wallSize: '120px 86px',
wall: svgTile(120, WALL_H, `
${brickRows(120, 80, { w: 28, h: 12 }, 2, '#c9baa9', ['#b25b41', '#a75237', '#bc6448', '#9e4b33'])}
<rect y='80' width='120' height='6' fill='#45454b'/>
<rect y='80' width='120' height='1' fill='#6c6c74'/>
`),
floorColor: '#c99b64',
floorSize: '192px 96px',
floor: svgTile(192, 96, `
${plankRows(192, 24, ['#cfa46c', '#c49862', '#d6ab74', '#bf915b'], '#8f6540', [40, 130, 88, 8])}
<ellipse cx='150' cy='11' rx='3' ry='2' fill='#a2774a'/><ellipse cx='150' cy='11' rx='1.2' ry='.8' fill='#7d552f'/>
<ellipse cx='58' cy='60' rx='2.6' ry='1.8' fill='#a2774a'/><ellipse cx='58' cy='60' rx='1' ry='.7' fill='#7d552f'/>
`)
},
garden: {
wallColor: '#bde0f4',
wallSize: '160px 86px',
wall: svgTile(160, WALL_H, `
<defs><linearGradient id='sky' x1='0' y1='0' x2='0' y2='1'><stop offset='0' stop-color='#a9d5f0'/><stop offset='1' stop-color='#dceff9'/></linearGradient></defs>
<rect width='160' height='86' fill='url(#sky)'/>
<g fill='#fff' opacity='.9'><ellipse cx='36' cy='22' rx='14' ry='6'/><ellipse cx='44' cy='18' rx='9' ry='6'/><ellipse cx='28' cy='19' rx='8' ry='5'/><ellipse cx='118' cy='34' rx='11' ry='5'/><ellipse cx='124' cy='31' rx='7' ry='5'/></g>
<g fill='#4c9440'><circle cx='0' cy='64' r='13'/><circle cx='22' cy='62' r='14'/><circle cx='44' cy='65' r='12'/><circle cx='66' cy='61' r='14'/><circle cx='88' cy='64' r='13'/><circle cx='110' cy='62' r='14'/><circle cx='132' cy='65' r='12'/><circle cx='154' cy='62' r='13'/></g>
<g fill='#63b04f'><circle cx='11' cy='58' r='9'/><circle cx='34' cy='56' r='9'/><circle cx='56' cy='58' r='9'/><circle cx='78' cy='55' r='9'/><circle cx='100' cy='58' r='9'/><circle cx='122' cy='56' r='9'/><circle cx='144' cy='58' r='9'/></g>
<rect y='66' width='160' height='14' fill='#3f7f36'/>
<g fill='#f7f4ea'>
<rect y='60' width='160' height='3' rx='1'/><rect y='72' width='160' height='3' rx='1'/>
<path d='M4 52l4 -5l4 5v28h-8zM24 52l4 -5l4 5v28h-8zM44 52l4 -5l4 5v28h-8zM64 52l4 -5l4 5v28h-8zM84 52l4 -5l4 5v28h-8zM104 52l4 -5l4 5v28h-8zM124 52l4 -5l4 5v28h-8zM144 52l4 -5l4 5v28h-8z'/>
</g>
<g fill='#d9d3c2'><rect x='10' y='52' width='2' height='28'/><rect x='30' y='52' width='2' height='28'/><rect x='50' y='52' width='2' height='28'/><rect x='70' y='52' width='2' height='28'/><rect x='90' y='52' width='2' height='28'/><rect x='110' y='52' width='2' height='28'/><rect x='130' y='52' width='2' height='28'/><rect x='150' y='52' width='2' height='28'/></g>
<rect y='80' width='160' height='6' fill='#6a4b30'/>
<rect y='80' width='160' height='1' fill='#8a6a48'/>
`),
floorColor: '#72b455',
floorSize: '144px 144px',
floor: svgTile(144, 144, `
<rect width='144' height='144' fill='#72b455'/>
<g fill='#69ac4d'><ellipse cx='30' cy='104' rx='20' ry='10'/><ellipse cx='110' cy='34' rx='18' ry='9'/><ellipse cx='126' cy='118' rx='14' ry='8'/></g>
<g stroke='#5a9a40' stroke-width='1.4' stroke-linecap='round'>
<path d='M8 12l2 -5M15 30l2 -5M31 8l-2 -5M40 40l2 -5M6 50l2 -5M52 58l2 -5M62 82l-2 -5M84 66l2 -5M90 44l-2 -5M70 6l2 -5M26 88l2 -5M46 76l-2 -5M78 90l2 -5M36 22l2 -5M58 34l-2 -5M12 66l-2 -5M104 12l2 -5M118 58l-2 -5M134 22l2 -5M96 96l2 -5M112 80l-2 -5M138 70l2 -5M20 122l2 -5M48 110l-2 -5M64 130l2 -5M88 118l2 -5M104 138l-2 -5M130 96l2 -5M6 138l2 -5M40 138l-2 -5'/>
</g>
<g stroke='#8fd06c' stroke-width='1.4' stroke-linecap='round'>
<path d='M22 18l2 -5M44 26l-2 -5M60 12l2 -5M80 32l2 -5M14 42l2 -5M30 60l-2 -5M52 90l2 -5M88 82l-2 -5M72 50l2 -5M92 10l-2 -5M4 92l2 -5M40 90l2 -5M110 20l2 -5M126 46l-2 -5M100 66l2 -5M140 88l2 -5M120 110l-2 -5M56 120l2 -5M76 138l-2 -5M28 132l2 -5M8 112l2 -5M136 130l-2 -5'/>
</g>
<g><circle cx='24' cy='46' r='2.4' fill='#fff'/><circle cx='28' cy='42' r='2.4' fill='#fff'/><circle cx='28' cy='50' r='2.4' fill='#fff'/><circle cx='32' cy='46' r='2.4' fill='#fff'/><circle cx='28' cy='46' r='2' fill='#f7c948'/></g>
<g><circle cx='104' cy='100' r='2.4' fill='#fff'/><circle cx='108' cy='96' r='2.4' fill='#fff'/><circle cx='108' cy='104' r='2.4' fill='#fff'/><circle cx='112' cy='100' r='2.4' fill='#fff'/><circle cx='108' cy='100' r='2' fill='#f7c948'/></g>
<g fill='#4f8f38'><circle cx='72' cy='72' r='2'/><circle cx='75' cy='69' r='2'/><circle cx='75' cy='75' r='2'/></g>
<g fill='#4f8f38'><circle cx='128' cy='16' r='2'/><circle cx='131' cy='13' r='2'/><circle cx='131' cy='19' r='2'/></g>
`)
},
nightclub: {
wallColor: '#1b1030',
wallSize: '160px 86px',
wall: svgTile(160, WALL_H, `
<rect width='160' height='86' fill='#1b1030'/>
${speckle([[14, 12], [38, 30], [61, 8], [92, 22], [121, 14], [147, 36], [27, 44], [76, 40], [106, 46], [138, 6]], '#f9a8d4', 1)}
${speckle([[50, 20], [84, 10], [131, 28], [8, 34], [116, 40], [154, 18], [66, 48], [98, 4]], '#8fe9ff', 1)}
<rect y='58' width='160' height='9' fill='#ff4fb0' opacity='.18'/>
<rect y='61' width='160' height='3' rx='1.5' fill='#ff4fb0'/>
<rect y='62' width='160' height='1' fill='#ffd0ea'/>
<rect y='69' width='160' height='9' fill='#48e0ff' opacity='.16'/>
<rect y='72' width='160' height='3' rx='1.5' fill='#48e0ff'/>
<rect y='73' width='160' height='1' fill='#d8f8ff'/>
<rect y='80' width='160' height='6' fill='#0b0614'/>
<rect y='80' width='160' height='1' fill='#48e0ff' opacity='.5'/>
`),
floorColor: '#2c1656',
floorSize: '160px 160px',
floor: svgTile(160, 160, checkerTiles(40, ['#3a1a64', '#2c1656', '#4c1f74', '#7a2f8e'], '#0e0618'))
},
pizza: {
wallColor: '#f4e9d6',
wallSize: '160px 86px',
wall: svgTile(160, WALL_H, `
<rect width='160' height='86' fill='#f4e9d6'/>
<g fill='#c9302c'><rect width='20' height='16'/><rect x='40' width='20' height='16'/><rect x='80' width='20' height='16'/><rect x='120' width='20' height='16'/></g>
<g fill='#fbf5ea'><rect x='20' width='20' height='16'/><rect x='60' width='20' height='16'/><rect x='100' width='20' height='16'/><rect x='140' width='20' height='16'/></g>
<g fill='#c9302c'><circle cx='10' cy='16' r='10'/><circle cx='50' cy='16' r='10'/><circle cx='90' cy='16' r='10'/><circle cx='130' cy='16' r='10'/></g>
<g fill='#fbf5ea'><circle cx='30' cy='16' r='10'/><circle cx='70' cy='16' r='10'/><circle cx='110' cy='16' r='10'/><circle cx='150' cy='16' r='10'/></g>
<rect y='26' width='160' height='60' fill='#f4e9d6'/>
<rect y='26' width='160' height='3' fill='rgba(0,0,0,.12)'/>
<g stroke='#6a4a3a' stroke-width='1.2' fill='none'><path d='M0 36 Q20 44 40 36 T80 36 T120 36 T160 36'/></g>
<g fill='#f7d34a'><circle cx='10' cy='39' r='2.4'/><circle cx='30' cy='41' r='2.4'/><circle cx='50' cy='39' r='2.4'/><circle cx='70' cy='41' r='2.4'/><circle cx='90' cy='39' r='2.4'/><circle cx='110' cy='41' r='2.4'/><circle cx='130' cy='39' r='2.4'/><circle cx='150' cy='41' r='2.4'/></g>
<g fill='#c9302c'><rect y='64' width='16' height='16'/><rect x='32' y='64' width='16' height='16'/><rect x='64' y='64' width='16' height='16'/><rect x='96' y='64' width='16' height='16'/><rect x='128' y='64' width='16' height='16'/></g>
<g fill='#fbf5ea'><rect x='16' y='64' width='16' height='16'/><rect x='48' y='64' width='16' height='16'/><rect x='80' y='64' width='16' height='16'/><rect x='112' y='64' width='16' height='16'/><rect x='144' y='64' width='16' height='16'/></g>
<rect y='62' width='160' height='2' fill='#b89b7a'/>
<rect y='80' width='160' height='6' fill='#7a5238'/>
<rect y='80' width='160' height='1' fill='#a07858'/>
`),
floorColor: '#f1e6d4',
floorSize: '48px 48px',
floor: svgTile(48, 48, `
<rect width='48' height='48' fill='#f1e6d4'/>
<rect width='24' height='24' fill='#dc8f86'/>
<rect x='24' y='24' width='24' height='24' fill='#dc8f86'/>
<path d='M24.5 0v48M0 24.5h48M0 .5h48M.5 0v48' stroke='rgba(90,50,40,.14)' stroke-width='1'/>
`)
}
}
function skinCss(name, skin) {
const night = 'linear-gradient(rgba(9,11,42,.52), rgba(9,11,42,.52))'
const floor = `url("${skin.floor}") 0 ${WALL_H}px / ${skin.floorSize} repeat local`
const wall = `url("${skin.wall}") 0 0 / ${skin.wallSize} repeat-x`
return `
.office-room.is-${name} { background: ${floor}, ${skin.floorColor}; }
.office-room.is-${name} .office-wall { background: ${wall}, ${skin.wallColor}; }
.office-root.is-night .office-room.is-${name} { background: ${night} 0 0 / auto repeat local, ${floor}, ${skin.floorColor}; }
.office-root.is-night .office-room.is-${name} .office-wall { background: ${night}, ${wall}, ${skin.wallColor}; }`
}
const BOT_CHAT_TITLE = 'Bot Chat'
const chatCreates = new Map()
const jobPollers = new Map()
let pluginCtx = null
function useTurnBusy() {
return Boolean(useValue(host.state.busy))
}
function usePulse(ms = 200) {
const [now, setNow] = useState(() => Date.now())
useEffect(() => {
let id = 0
let live = true
if (ms <= 32 && typeof requestAnimationFrame === 'function') {
const tick = () => {
if (!live) {
return
}
setNow(Date.now())
id = requestAnimationFrame(tick)
}
id = requestAnimationFrame(tick)
return () => {
live = false
cancelAnimationFrame(id)
}
}
id = setInterval(() => setNow(Date.now()), ms)
return () => clearInterval(id)
}, [ms])
return now
}
function useRoster() {
return useQuery({
queryKey: ROSTER_KEY,
queryFn: () => host.request('profiles.list', {}),
refetchInterval: 5000,
staleTime: 5000,
retry: true,
retryDelay: attempt => Math.min(15000, 1000 * 2 ** attempt)
})
}
function displayName(bot, meta) {
if (meta?.title?.trim()) {
return meta.title.trim()
}
if ((bot.name || '').trim().toLowerCase() === 'default' && !bot.title) {
return 'Hermes'
}
const raw = (bot.title || bot.name || '').replace(/[-_]+/g, ' ').trim()
return raw.replace(/\b\w/g, ch => ch.toUpperCase())
}
function botHandle(name) {
return (name || '').trim().toLowerCase() === 'default' ? 'hermes' : name
}
function botMeta(bot) {
const raw = bot?.ui_meta?.[META_NS]
return raw && typeof raw === 'object' ? raw : {}
}
const $avatars = atom({})
const avatarInflight = new Set()
function pullAvatars(roster) {
for (const bot of roster || []) {
if (!bot.has_avatar || $avatars.get()[bot.name] || avatarInflight.has(bot.name)) {
continue
}
avatarInflight.add(bot.name)
host
.request('profiles.get_asset', { name: bot.name, asset: 'avatar' })
.then(res => {
if (res?.found && res.data) {
$avatars.set({ ...$avatars.get(), [bot.name]: res.data })
}
})
.catch(() => undefined)
.finally(() => avatarInflight.delete(bot.name))
}
}
function botLook(bot) {
const meta = botMeta(bot)
const name = bot.name || 'agent'
const isPrimary = name.trim().toLowerCase() === 'default'
const color = meta.color || (isPrimary ? '#8b5cf6' : profileColor(name) || '#8b5cf6')
const cached = $avatars.get()[name]
return {
color,
image: typeof meta.image === 'string' ? meta.image : cached || null,
title: displayName(bot, meta)
}
}
function deskMood({ isActive, turnBusy, tasked }) {
if (tasked || (isActive && turnBusy)) {
return 'think'
}
return 'idle'
}
// Small stable number per name, for staggering blinks and the like.
function nameHash(name) {
let h = 0
for (const ch of String(name || '')) {
h = (h * 31 + ch.charCodeAt(0)) % 100003
}
return h
}
// Type the screen text out one letter at a time once a bot starts thinking.
function typedText(text, elapsedMs, cps = 28) {
const full = String(text || '')
const n = Math.max(0, Math.floor((elapsedMs || 0) / (1000 / cps)))
if (n >= full.length) {
return full
}
return full.slice(0, n) + '\u258d'
}
function faceMood({ held, asleep, pet, clap, stretch, shy, peek, think, bored }) {
if (held && asleep) {
return 'sleep'
}
if (held) {
return 'held'
}
if (asleep) {
return 'sleep'
}
if (pet) {
return 'pet'
}
if (clap) {
return 'clap'
}
if (stretch) {
return 'stretch'
}
if (shy) {
return 'shy'
}
if (peek) {
return 'peek'
}
if (think) {
return 'think'
}
if (bored) {
return 'bored'
}
return 'idle'
}
function movedEnough(a, b) {
if (!a || !b) {
return false
}
const dx = a.x - b.x
const dy = a.y - b.y
return dx * dx + dy * dy >= DRAG_PX * DRAG_PX
}
function near(a, b, r) {
if (!a || !b) {
return false
}
const dx = a.x - b.x
const dy = a.y - b.y
return dx * dx + dy * dy <= r * r
}
function isNightHour(date = new Date()) {
const hour = date.getHours()
return hour >= 19 || hour < 7
}
// One clock for everything that depends on the time of day. Night is the same
// window the room tint uses, so the sky can never disagree with the room.
// `t` runs 0..1 across the sun's arc (7am to 7pm) or the moon's (7pm to 7am).
function skyState(date = new Date()) {
const h = date.getHours() + date.getMinutes() / 60
const night = isNightHour(date)
const t = night ? (((h - 19 + 24) % 24) / 12) : ((h - 7) / 12)
const dusk = !night && h >= 17.5
const dawn = !night && h < 8.5
return { night, t: Math.max(0, Math.min(1, t)), tone: night ? 'night' : dusk ? 'dusk' : dawn ? 'dawn' : 'day' }
}
function headerLine(names, one, many) {
const list = (names || []).filter(Boolean)
if (!list.length) {
return ''
}
const shown = list.slice(0, 2).join(', ')
const more = list.length > 2 ? ` +${list.length - 2}` : ''
return `${shown}${more} ${list.length === 1 ? one : many}`
}
// Steady state labels fade after a moment so a full floor stays calm.
function quietStatus(text) {
return text === 'here' || text === 'at desk' || text === 'exploring'
}
// Which round a completion belongs to, or null if that round already
// celebrated. Rounds without a token (old state) count as round 0.
function completionToken(row) {
const round = row?.round || 0
return row?.doneRound === round ? null : round
}
// A bot that has had no task for days, and is idle at its desk, is bored.
function isBored(lastTaskAt, now, thresholdMs = BORED_MS_SLICE) {
if (!lastTaskAt) {
return false
}
return (now || 0) - lastTaskAt > thresholdMs
}
// Monday 00:00 local for the week that contains `date`.
function weekStart(date = new Date()) {
const d = new Date(date.getFullYear(), date.getMonth(), date.getDate())
const day = (d.getDay() + 6) % 7
d.setDate(d.getDate() - day)
return d.getTime()
}
// Bump a weekly counter. Starts a fresh week when the Monday moved on.
function weekBump(stats, key, name, now) {
const start = weekStart(new Date(now || Date.now()))
const base = stats && stats.start === start ? stats : { start, tasks: 0, hops: 0, pizzas: {} }
const next = { ...base, pizzas: { ...(base.pizzas || {}) } }
if (key === 'pizza') {
next.pizzas[name] = (next.pizzas[name] || 0) + 1
} else if (key === 'tasks' || key === 'hops') {
next[key] = (next[key] || 0) + 1
}
return next
}
// First of the month, local midnight.
function monthStart(date = new Date()) {
return new Date(date.getFullYear(), date.getMonth(), 1).getTime()
}
function monthBump(stats, name, now) {
const start = monthStart(new Date(now || Date.now()))
const base = stats && stats.start === start ? stats : { start, tasks: {}, holder: null }
const tasks = { ...(base.tasks || {}), [name]: ((base.tasks || {})[name] || 0) + 1 }
const holder = monthLeader({ ...base, tasks }, base.holder)
return { start, tasks, holder }
}
// Who has the most tasks this month. Ties keep the current holder, so a bot
// has to pass them, not just match them, to take the frame.
function monthLeader(stats, prevHolder) {
const tasks = (stats && stats.tasks) || {}
let best = null
let bestN = 0
for (const [name, n] of Object.entries(tasks)) {
if (n > bestN || (n === bestN && name === prevHolder)) {
best = name
bestN = n
}
}
return bestN > 0 ? best : null
}
function weekLine(stats) {
if (!stats || (!stats.tasks && !stats.hops && !Object.keys(stats.pizzas || {}).length)) {
return null
}
const bits = []
if (stats.tasks) {
bits.push(`${stats.tasks} task${stats.tasks === 1 ? '' : 's'}`)
}
const eaters = Object.entries(stats.pizzas || {}).sort((a, b) => b[1] - a[1])
if (eaters.length) {
const [who, n] = eaters[0]
bits.push(`${who} ate ${n} pizza${n === 1 ? '' : 's'}`)
}
if (stats.hops) {
bits.push(`${stats.hops} hop${stats.hops === 1 ? '' : 's'}`)
}
return `This week: ${bits.join(', ')}`
}
function clockLabel(date = new Date()) {
const h = String(date.getHours()).padStart(2, '0')
const m = String(date.getMinutes()).padStart(2, '0')
return `${h}:${m}`
}
function clockHands(date = new Date()) {
const h = date.getHours()
const m = date.getMinutes()
return {
hour: (h % 12) * 30 + m * 0.5,
minute: m * 6
}
}
function nextClockKind(kind) {
return kind === 'digital' ? 'analog' : 'digital'
}
function pickBotChatRow(rows, pinned) {
const list = Array.isArray(rows) ? rows : []
if (pinned && list.some(row => row && row.id === pinned)) {
return pinned
}
const titled = list.find(row => (row?.title || '').trim() === BOT_CHAT_TITLE)
if (titled?.id) {
return titled.id
}
return null
}
function resolvePicked(roster, selected, activeProfile) {
const name = selected || activeProfile
if (name && roster.some(bot => bot.name === name)) {
return name
}
return roster[0]?.name || null
}
function savePref(key, value) {
try {
Promise.resolve(pluginCtx?.storage?.set?.(key, value)).catch(() => undefined)
} catch {
/* no storage */
}
}
function outputText(bot) {
return (bot.last_session?.preview || '').trim()
}
function previewLine(bot) {
const text = outputText(bot)
if (!text) {
return 'Waiting for a task'
}
return text.length > 72 ? `${text.slice(0, 71)}…` : text
}
function stickyText(bot) {
const text = (bot.last_session?.preview || '').trim()
if (!text) {
return ''
}
return text.length > 20 ? `${text.slice(0, 19)}…` : text
}
function easeInOut(t) {
const x = Math.max(0, Math.min(1, t))
return x < 0.5 ? 2 * x * x : 1 - (2 - 2 * x) * (2 - 2 * x) / 2
}
function roamMs(from, to) {
if (!from || !to) {
return 2000
}
const dx = to.x - from.x
const dy = to.y - from.y
return Math.max(1400, Math.min(4200, Math.sqrt(dx * dx + dy * dy) * 18))
}
function backdropNames() {
return ['carpet', 'loft', 'garden', 'nightclub', 'pizza']
}
function nextBackdrop(kind) {
const all = backdropNames()
const i = all.indexOf(kind)
return all[((i < 0 ? 0 : i) + 1) % all.length]
}
function idleBotNames(roster, jobs, activeProfile, turnBusy) {
return (Array.isArray(roster) ? roster : [])
.filter(
bot =>
deskMood({
isActive: bot.name === activeProfile,
turnBusy,
tasked: Boolean(jobs && jobs[bot.name])
}) === 'idle'
)
.map(bot => bot.name)
}
const FACE_HALF = 21
const HOP_ROWS = [[1], [2], [3, 4], [5], [6, 7], [8]]
const BORED_MS_SLICE = 2 * 24 * 60 * 60 * 1000
// Out along the rows, turn at the end, and hop back down.
function hopCourse(rows) {
const out = Array.isArray(rows) ? rows : []
if (out.length < 2) {
return out.slice()
}
return out.concat(out.slice(0, -1).reverse())
}
function chairCountForGame(playerCount) {
return Math.max(0, (playerCount || 0) - 1)
}
function pickFreeStool(stools, taken, radius = 40) {
const seats = Array.isArray(stools) ? stools : []
const used = Array.isArray(taken) ? taken : []
if (!seats.length) {
return null
}
return seats.find(stool => !used.some(spot => near(stool, spot, radius))) || null
}
function nextBarStand(stools, taken, radius = 40) {
const free = pickFreeStool(stools, taken, radius)
if (free) {
return free
}
const seats = Array.isArray(stools) ? stools : []
const last = seats[seats.length - 1]
if (!last) {
return null
}
const n = (Array.isArray(taken) ? taken : []).length
return { id: `stand-${n}`, x: last.x - 20, y: last.y + 18 }
}
// Pizza parlor rule: one pizza on the counter per round. A round starts when
// anyone is given a task. The first bot to finish and reach the counter takes
// the slice, everyone after that gets "no pizza".
function freshPizza(now) {
return { winner: null, at: now }
}
function claimPizza(pizza, name, now) {
const current = pizza || freshPizza(now)
if (current.winner) {
return { pizza: current, won: current.winner === name }
}
return { pizza: { winner: name, at: now }, won: true }
}
const CHAIR_PX = 30
// Musical chairs live in the middle of the box, backs together in a small ring.
// Positions are the chair's top-left; `gameRing` says how far out the players circle.
function boxCenter(box) {
const area = box || { x0: 12, y0: 92, x1: 360, y1: 280 }
return { x: (area.x0 + area.x1) / 2, y: (area.y0 + area.y1) / 2 }
}
function chairRingRadius(count) {
return count <= 1 ? 0 : count === 2 ? 22 : 18 + count * 5
}
function placeChairs(n, box) {
const count = Math.max(0, n || 0)
const center = boxCenter(box)
const radius = chairRingRadius(count)
const chairs = []
for (let i = 0; i < count; i++) {
const angle = -Math.PI / 2 + (i / Math.max(1, count)) * Math.PI * 2
chairs.push({
id: `c${i}`,
x: Math.round(center.x + Math.cos(angle) * radius - CHAIR_PX / 2),
y: Math.round(center.y + Math.sin(angle) * radius - CHAIR_PX / 2)
})
}
return chairs
}
// Where the players walk while the music plays: a wider ring around the chairs.
function gameRing(box, count) {
const center = boxCenter(box)
const area = box || { x0: 12, y0: 92, x1: 360, y1: 280 }
const room = Math.min((area.x1 - area.x0) / 2, (area.y1 - area.y0) / 2) - 26
const radius = Math.max(56, Math.min(chairRingRadius(count) + 84, room))
return { center, radius }
}
// Next stop on the ring: keep going clockwise from wherever the player is now.
function ringPoint(ring, from, step = 0.9) {
const dx = (from?.x ?? ring.center.x) + FACE_HALF - ring.center.x
const dy = (from?.y ?? ring.center.y) + FACE_HALF - ring.center.y
const angle = Math.atan2(dy, dx) + step
return {
x: ring.center.x + Math.cos(angle) * ring.radius - FACE_HALF,
y: ring.center.y + Math.sin(angle) * ring.radius - FACE_HALF
}
}
function assignChairs(players, chairs) {
const people = Array.isArray(players) ? players : []
const seats = Array.isArray(chairs) ? chairs : []
const pairs = []
for (const person of people) {
for (const chair of seats) {
const dx = (person.x || 0) - (chair.x || 0)
const dy = (person.y || 0) - (chair.y || 0)
pairs.push({ name: person.name, chair, d: dx * dx + dy * dy })
}
}
pairs.sort((a, b) => a.d - b.d)
const assigned = {}
const usedP = new Set()
const usedC = new Set()
for (const pair of pairs) {
if (usedP.has(pair.name) || usedC.has(pair.chair.id)) {
continue
}
assigned[pair.name] = pair.chair
usedP.add(pair.name)
usedC.add(pair.chair.id)