@hivey/cli 0.0.0-stage → 0.9.36

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 (83) hide show
  1. package/README.md +235 -2
  2. package/bin/hivey.js +98 -0
  3. package/native/darwin-arm64/hivey +0 -0
  4. package/package.json +36 -4
  5. package/platforms.json +3 -0
  6. package/repo/LICENSE +201 -0
  7. package/repo/NOTICE +18 -0
  8. package/repo/README.md +148 -0
  9. package/repo/install.sh +365 -0
  10. package/repo/pets/README.md +25 -0
  11. package/repo/pets/hivey-dot/CLAUDE.md +55 -0
  12. package/repo/pets/hivey-dot/build.sh +34 -0
  13. package/repo/pets/hivey-dot/main.swift +750 -0
  14. package/repo/pets/hivey-h/CLAUDE.md +75 -0
  15. package/repo/pets/hivey-h/build.sh +34 -0
  16. package/repo/pets/hivey-h/main.swift +739 -0
  17. package/repo/pets/hivey-prompt/CLAUDE.md +59 -0
  18. package/repo/pets/hivey-prompt/build.sh +34 -0
  19. package/repo/pets/hivey-prompt/main.swift +691 -0
  20. package/repo/pets/pet.py +288 -0
  21. package/repo/pets/shared/HiveyWatch.swift +437 -0
  22. package/repo/pets/test_pet.py +122 -0
  23. package/repo/plugins/README.md +108 -0
  24. package/repo/plugins/agent-creator/herdr-plugin.toml +17 -0
  25. package/repo/plugins/dashboard/dashboard.py +507 -0
  26. package/repo/plugins/dashboard/herdr-plugin.toml +14 -0
  27. package/repo/plugins/dashboard/test_dashboard.py +134 -0
  28. package/repo/plugins/github/github.py +261 -0
  29. package/repo/plugins/github/herdr-plugin.toml +14 -0
  30. package/repo/plugins/github/test_github.py +49 -0
  31. package/repo/plugins/skills/herdr-plugin.toml +11 -0
  32. package/repo/plugins/skills/skills.md +41 -0
  33. package/repo/plugins/slack-relay/connect.py +198 -0
  34. package/repo/plugins/slack-relay/create_channel.py +134 -0
  35. package/repo/plugins/slack-relay/herdr-plugin.toml +15 -0
  36. package/repo/plugins/slack-relay/relay.py +401 -0
  37. package/repo/plugins/slack-relay/slack-app-manifest.json +30 -0
  38. package/repo/plugins/slack-relay/test_connect.py +66 -0
  39. package/repo/plugins/slack-relay/test_create_channel.py +61 -0
  40. package/repo/plugins/slack-relay/test_relay.py +254 -0
  41. package/repo/plugins/swarm-creator/herdr-plugin.toml +17 -0
  42. package/repo/plugins/swarm-watcher/dashboard.py +333 -0
  43. package/repo/plugins/swarm-watcher/herdr-plugin.toml +16 -0
  44. package/repo/plugins/swarm-watcher/usage.py +129 -0
  45. package/repo/plugins/swarm-watcher/watcher.py +191 -0
  46. package/repo/plugins/team-template/herdr-plugin.toml +15 -0
  47. package/repo/plugins/team-template/setup.py +211 -0
  48. package/repo/plugins/team-template/test_setup.py +73 -0
  49. package/repo/plugins/tts/herdr-plugin.toml +15 -0
  50. package/repo/skills/herdr/SKILL.md +214 -0
  51. package/repo/skills/hivey/SKILL.md +322 -0
  52. package/repo/skills/hivey/scripts/handoff.py +27 -0
  53. package/repo/skills/hivey/scripts/new_wiki.py +143 -0
  54. package/repo/skills/hivey-agent-creator/SKILL.md +303 -0
  55. package/repo/skills/hivey-agent-creator/references/agent-claude-md.md +256 -0
  56. package/repo/skills/hivey-agent-creator/references/global-guidelines.md +67 -0
  57. package/repo/skills/hivey-agent-creator/references/reviewer-agent.md +35 -0
  58. package/repo/skills/hivey-agent-creator/scripts/agent_context.py +76 -0
  59. package/repo/skills/hivey-agent-creator/scripts/agent_gate.py +78 -0
  60. package/repo/skills/hivey-agent-creator/scripts/setup_agent.py +288 -0
  61. package/repo/skills/hivey-agy/SKILL.md +123 -0
  62. package/repo/skills/hivey-agy/references/gemini-prompting.md +134 -0
  63. package/repo/skills/hivey-agy/scripts/agy_run.py +146 -0
  64. package/repo/skills/hivey-codex/SKILL.md +66 -0
  65. package/repo/skills/hivey-context7/SKILL.md +56 -0
  66. package/repo/skills/hivey-create-wiki/SKILL.md +329 -0
  67. package/repo/skills/hivey-grok/SKILL.md +101 -0
  68. package/repo/skills/hivey-grok/scripts/grok_run.py +139 -0
  69. package/repo/skills/hivey-swarm-creator/SKILL.md +384 -0
  70. package/repo/skills/hivey-swarm-creator/references/agent-claude-md.md +313 -0
  71. package/repo/skills/hivey-swarm-creator/references/archify-report.md +24 -0
  72. package/repo/skills/hivey-swarm-creator/references/dashboard.md +46 -0
  73. package/repo/skills/hivey-swarm-creator/references/retrospective.md +23 -0
  74. package/repo/skills/hivey-swarm-creator/references/shared-rules.md +39 -0
  75. package/repo/skills/hivey-swarm-creator/references/wiki-memory.md +30 -0
  76. package/repo/skills/hivey-swarm-creator/scripts/setup_agent.py +76 -0
  77. package/repo/skills/hivey-swarm-creator/scripts/shared_rules.py +50 -0
  78. package/repo/skills/hivey-swarm-creator/scripts/swarm_tasks.py +171 -0
  79. package/repo/skills/hivey-wiki-ingest/SKILL.md +45 -0
  80. package/repo/skills/hivey-wiki-lint/SKILL.md +38 -0
  81. package/repo/skills/hivey-wiki-list/SKILL.md +46 -0
  82. package/repo/skills/hivey-wiki-log/SKILL.md +45 -0
  83. package/repo/skills/hivey-wiki-query/SKILL.md +44 -0
@@ -0,0 +1,739 @@
1
+ import AppKit
2
+
3
+ // Hivey — a living, three-dimensional "H": five fitted graphite sections, mint eyes in the bridge.
4
+ // Animated desktop pet that watches hivey: swarms, agents and their messages drive its animations.
5
+ // CLI: hivey-h → launch the pet
6
+ // hivey-h say "hi" → make the running pet say something
7
+ // hivey-h assign|listen|think|message|complete ["text"]
8
+ // → play that animation (and say the text, if any)
9
+
10
+ let sayNote = Notification.Name("com.hivey.h.say")
11
+ let eventNote = Notification.Name("com.hivey.h.event")
12
+ let petEvents = ["assign", "listen", "think", "message", "complete", "chat"]
13
+
14
+ let cliArgs = CommandLine.arguments
15
+ if cliArgs.count >= 2 && cliArgs[1] == "say" {
16
+ let text = cliArgs.dropFirst(2).joined(separator: " ")
17
+ DistributedNotificationCenter.default().postNotificationName(sayNote, object: text, userInfo: nil, deliverImmediately: true)
18
+ exit(0)
19
+ }
20
+ if cliArgs.count >= 2 && petEvents.contains(cliArgs[1]) {
21
+ // "event\ntext": the object is a plain string, which distributed notifications always carry
22
+ let text = cliArgs.dropFirst(2).joined(separator: " ")
23
+ DistributedNotificationCenter.default().postNotificationName(eventNote, object: cliArgs[1] + "\n" + text, userInfo: nil, deliverImmediately: true)
24
+ exit(0)
25
+ }
26
+ if cliArgs.count >= 2 && ["-h", "--help", "help"].contains(cliArgs[1]) {
27
+ print("""
28
+ usage: hivey-h launch the pet (it watches hivey on its own)
29
+ hivey-h say "text" make the running pet say something
30
+ hivey-h assign|listen|think|message|complete ["text"]
31
+ play that animation, optionally saying the text
32
+ """)
33
+ exit(0)
34
+ }
35
+
36
+ // One Hivey per desktop
37
+ if let id = Bundle.main.bundleIdentifier,
38
+ NSRunningApplication.runningApplications(withBundleIdentifier: id).count > 1 {
39
+ exit(0)
40
+ }
41
+
42
+ // MARK: - Palette
43
+
44
+ extension NSColor {
45
+ convenience init(hex: Int, alpha: CGFloat = 1) {
46
+ self.init(srgbRed: CGFloat((hex >> 16) & 0xff) / 255,
47
+ green: CGFloat((hex >> 8) & 0xff) / 255,
48
+ blue: CGFloat(hex & 0xff) / 255,
49
+ alpha: alpha)
50
+ }
51
+ }
52
+
53
+ enum Palette {
54
+ static let faceLight = NSColor(hex: 0x5b676d)
55
+ static let face = NSColor(hex: 0x262d31)
56
+ static let faceDark = NSColor(hex: 0x101315)
57
+ static let sideNear = NSColor(hex: 0x171b1e)
58
+ static let sideFar = NSColor(hex: 0x050607)
59
+ static let mint = NSColor(hex: 0x8ff0cc)
60
+ static let mintGlow = NSColor(hex: 0xc4ffea)
61
+ static let ink = NSColor(hex: 0x111518)
62
+ static let paper = NSColor(hex: 0xe6f4ee)
63
+ }
64
+
65
+ let sRGB = CGColorSpace(name: CGColorSpace.sRGB)!
66
+
67
+ func gradient(_ colors: [NSColor], _ locations: [CGFloat]) -> CGGradient {
68
+ CGGradient(colorsSpace: sRGB, colors: colors.map(\.cgColor) as CFArray, locations: locations)!
69
+ }
70
+
71
+ func lerp(_ a: CGFloat, _ b: CGFloat, _ k: CGFloat) -> CGFloat { a + (b - a) * k }
72
+ func easeInOut(_ k: CGFloat) -> CGFloat { k * k * (3 - 2 * k) }
73
+
74
+ // MARK: - Pet
75
+
76
+ final class PetView: NSView {
77
+ /// One fitted section of the H; `spread` is where it slides when the H opens up
78
+ struct Section { let rect: CGRect; let spread: CGVector }
79
+ /// A light pulse travelling along a polyline (through the bridge or along a connection)
80
+ struct Pulse { let start: Double; let route: Int } // route -1 = through the bridge, 0...3 = to a section
81
+
82
+ // Assembled H: two uprights (each a top and bottom section) joined by the bridge
83
+ private let sections: [Section] = [
84
+ Section(rect: CGRect(x: 70, y: 80, width: 20, height: 39), spread: CGVector(dx: -13, dy: 7)), // left top
85
+ Section(rect: CGRect(x: 70, y: 41, width: 20, height: 39), spread: CGVector(dx: -13, dy: -7)), // left bottom
86
+ Section(rect: CGRect(x: 130, y: 80, width: 20, height: 39), spread: CGVector(dx: 13, dy: 7)), // right top
87
+ Section(rect: CGRect(x: 130, y: 41, width: 20, height: 39), spread: CGVector(dx: 13, dy: -7)), // right bottom
88
+ Section(rect: CGRect(x: 90, y: 70, width: 40, height: 20), spread: .zero), // bridge
89
+ ]
90
+ private var bridge: CGRect { sections[4].rect }
91
+ private let pivot = CGPoint(x: 110, y: 41) // base of the H: leans and nods pivot here
92
+
93
+ var talking = false
94
+ var onPoke: (() -> Void)?
95
+ /// hivey agents shown as mint dots on the floor: bright = working, dim = idle
96
+ var agentCount = (working: 0, idle: 0) { didSet { needsDisplay = true } }
97
+
98
+ private var t = 0.0
99
+ private var spread: CGFloat = 1 // starts open; settles into an H on launch
100
+ private var spreadVel: CGFloat = 0
101
+ private var assignUntil = -1.0
102
+ private var listenUntil = -1.0
103
+ private var thinkUntil = -1.0
104
+ private var lean: CGFloat = 0
105
+ private var shift = CGPoint.zero // idle micro-shifts: x offset, rotation
106
+ private var shiftGoal = CGPoint.zero
107
+ private var nextShift = 3.0
108
+ private var nodStart = -10.0
109
+ private var glowUntil = -1.0
110
+ private var pulses: [Pulse] = []
111
+ private var nextPulse = 0.0
112
+ private var nextBlink = 2.5
113
+ private var blinkUntil = 0.0
114
+ private var look = CGPoint.zero
115
+
116
+ private var dragStart: NSPoint?
117
+ private var windowStart = NSPoint.zero
118
+ private var moved = false
119
+
120
+ override init(frame: NSRect) {
121
+ super.init(frame: frame)
122
+ let timer = Timer(timeInterval: 1.0 / 60, repeats: true) { [weak self] _ in
123
+ MainActor.assumeIsolated { self?.tick() }
124
+ }
125
+ RunLoop.main.add(timer, forMode: .common)
126
+ }
127
+
128
+ required init?(coder: NSCoder) { fatalError() }
129
+
130
+ // MARK: Behaviors
131
+
132
+ /// Assigning work: sections slide outward, linked by fine mint connections; messages flow
133
+ func assign(for seconds: Double = 6) {
134
+ assignUntil = t + seconds
135
+ nextPulse = t + 0.5
136
+ }
137
+
138
+ /// Listening: lean forward slightly
139
+ func listen(for seconds: Double = 2.5) { listenUntil = t + seconds }
140
+
141
+ /// Thinking: a soft pulse moves across the bridge
142
+ func think(for seconds: Double = 4) { thinkUntil = t + seconds }
143
+
144
+ /// Agents communicating: a brief pulse travels through the bridge
145
+ func message() { pulses.append(Pulse(start: t, route: -1)) }
146
+
147
+ /// Task finished: sections settle back into a complete H, restrained nod, brief mint glow
148
+ func complete() {
149
+ assignUntil = -1
150
+ thinkUntil = -1
151
+ nodStart = t + 0.45
152
+ glowUntil = t + 1.6
153
+ }
154
+
155
+ private var assigning: Bool { t < assignUntil }
156
+ private var thinking: Bool { t < thinkUntil || talking }
157
+
158
+ // MARK: Animation
159
+
160
+ private func tick() {
161
+ guard window?.isVisible == true else { return } // hidden: skip animation work
162
+ let dt = 1.0 / 60
163
+ t += dt
164
+
165
+ // Sections spring open/closed with a small, precise settle
166
+ let goal: CGFloat = assigning ? 1 : 0
167
+ spreadVel = (spreadVel + (goal - spread) * 46 * CGFloat(dt)) * CGFloat(exp(-8.5 * dt))
168
+ spread += spreadVel * CGFloat(dt)
169
+
170
+ lean += ((t < listenUntil ? 1 : 0) - lean) * CGFloat(min(1, dt * 6))
171
+
172
+ // Small, deliberate idle shifts
173
+ if t >= nextShift {
174
+ nextShift = t + Double.random(in: 3.5...6.5)
175
+ shiftGoal = CGPoint(x: .random(in: -2.5...2.5), y: .random(in: -0.035...0.035))
176
+ }
177
+ shift.x += (shiftGoal.x - shift.x) * CGFloat(min(1, dt * 2.2))
178
+ shift.y += (shiftGoal.y - shift.y) * CGFloat(min(1, dt * 2.2))
179
+
180
+ // While assigning, messages run through the bridge and out along the connections
181
+ pulses.removeAll { t - $0.start > 0.8 }
182
+ if assigning && t >= nextPulse {
183
+ pulses.append(Pulse(start: t, route: Int.random(in: -1...3)))
184
+ nextPulse = t + Double.random(in: 0.35...0.75)
185
+ }
186
+
187
+ if t >= nextBlink {
188
+ blinkUntil = t + 0.13
189
+ nextBlink = t + Double.random(in: 2.5...5.5)
190
+ }
191
+
192
+ // Observant eyes drift toward the cursor (listening: straight at you)
193
+ if let window {
194
+ let m = NSEvent.mouseLocation
195
+ let c = window.convertPoint(toScreen: convert(CGPoint(x: bridge.midX, y: bridge.midY), to: nil))
196
+ let dx = m.x - c.x, dy = m.y - c.y
197
+ let dist = max(1, hypot(dx, dy))
198
+ let reach = min(1, dist / 260) * (1 - lean)
199
+ look.x += (dx / dist * reach * 3 - look.x) * 0.12
200
+ look.y += (dy / dist * reach * 1.6 - look.y) * 0.12
201
+ }
202
+
203
+ needsDisplay = true
204
+ }
205
+
206
+ private var nodAmount: CGFloat {
207
+ let k = (t - nodStart) / 0.8
208
+ guard k >= 0 && k < 1 else { return 0 }
209
+ return CGFloat(sin(k * .pi))
210
+ }
211
+
212
+ private func placed(_ i: Int) -> CGRect {
213
+ let s = sections[i]
214
+ return s.rect.offsetBy(dx: s.spread.dx * spread, dy: s.spread.dy * spread)
215
+ }
216
+
217
+ // MARK: Drawing
218
+
219
+ override func draw(_ dirtyRect: NSRect) {
220
+ let ctx = NSGraphicsContext.current!.cgContext
221
+ let rise = 1.2 * CGFloat(sin(t * 1.3))
222
+
223
+ // Soft floor shadow, widening as the sections spread
224
+ let sw = 104 + 30 * spread
225
+ ctx.saveGState()
226
+ ctx.translateBy(x: pivot.x + 3, y: pivot.y - 9)
227
+ ctx.scaleBy(x: 1, y: 0.18)
228
+ ctx.drawRadialGradient(gradient([NSColor(white: 0, alpha: 0.45), NSColor(white: 0, alpha: 0)], [0, 1]),
229
+ startCenter: .zero, startRadius: 0, endCenter: .zero, endRadius: sw / 2, options: [])
230
+ ctx.restoreGState()
231
+ drawAgentDots(ctx)
232
+
233
+ // Whole-body pose: idle shift, lean forward, nod — pivoting at the base
234
+ ctx.saveGState()
235
+ ctx.translateBy(x: pivot.x + shift.x, y: pivot.y + rise - 2 * lean - 2.5 * nodAmount)
236
+ ctx.rotate(by: shift.y)
237
+ let s = 1 + 0.045 * lean
238
+ ctx.scaleBy(x: s, y: s * (1 - 0.02 * nodAmount))
239
+ ctx.translateBy(x: -pivot.x, y: -pivot.y)
240
+
241
+ let rects = sections.indices.map { placed($0) }
242
+ for r in rects { drawSide(ctx, r) } // extrusions first, so faces always sit on top
243
+ drawConnections(ctx, rects)
244
+ for r in rects { drawFace(ctx, r) }
245
+ drawBridgeLife(ctx, rects[4])
246
+ for p in pulses { drawPulse(ctx, p, rects) }
247
+ ctx.restoreGState()
248
+ }
249
+
250
+ /// Active hivey agents: a row of small mint dots on the floor below the H (max 12 drawn)
251
+ private func drawAgentDots(_ ctx: CGContext) {
252
+ let working = min(agentCount.working, 12)
253
+ let idle = min(agentCount.idle, 12 - working)
254
+ let n = working + idle
255
+ guard n > 0 else { return }
256
+ let gap: CGFloat = 7
257
+ let x0 = pivot.x - gap * CGFloat(n - 1) / 2
258
+ ctx.saveGState()
259
+ for i in 0..<n {
260
+ let busy = i < working
261
+ // Working dots breathe gently, out of phase, so the row feels alive
262
+ let k = busy ? 0.75 + 0.25 * CGFloat(sin(t * 2.4 + Double(i) * 0.9)) : 0.45
263
+ let r: CGFloat = busy ? 1.9 : 1.5
264
+ let c = CGPoint(x: x0 + gap * CGFloat(i), y: pivot.y - 21)
265
+ ctx.setShadow(offset: .zero, blur: busy ? 5 : 0, color: Palette.mint.withAlphaComponent(0.8 * k).cgColor)
266
+ ctx.setFillColor(Palette.mint.withAlphaComponent(k).cgColor)
267
+ ctx.fillEllipse(in: CGRect(x: c.x - r, y: c.y - r, width: r * 2, height: r * 2))
268
+ }
269
+ ctx.restoreGState()
270
+ }
271
+
272
+ private func path(_ r: CGRect) -> CGPath {
273
+ CGPath(roundedRect: r, cornerWidth: 4, cornerHeight: 4, transform: nil)
274
+ }
275
+
276
+ /// Extruded depth toward the lower right
277
+ private func drawSide(_ ctx: CGContext, _ r: CGRect) {
278
+ let steps = 6
279
+ for k in stride(from: steps, through: 1, by: -1) {
280
+ let f = CGFloat(k) / CGFloat(steps)
281
+ ctx.addPath(path(r.offsetBy(dx: 0.6 * CGFloat(k), dy: -0.8 * CGFloat(k))))
282
+ ctx.setFillColor(Palette.sideNear.blended(withFraction: f, of: Palette.sideFar)!.cgColor)
283
+ ctx.fillPath()
284
+ }
285
+ }
286
+
287
+ /// Satin front face with a beveled edge
288
+ private func drawFace(_ ctx: CGContext, _ r: CGRect) {
289
+ let p = path(r)
290
+ if t < glowUntil {
291
+ let k = CGFloat((glowUntil - t) / 1.6)
292
+ ctx.saveGState()
293
+ ctx.setShadow(offset: .zero, blur: 14, color: Palette.mint.withAlphaComponent(0.9 * k).cgColor)
294
+ ctx.addPath(p)
295
+ ctx.setFillColor(Palette.face.cgColor)
296
+ ctx.fillPath()
297
+ ctx.restoreGState()
298
+ }
299
+
300
+ ctx.saveGState()
301
+ ctx.addPath(p)
302
+ ctx.clip()
303
+ let tl = CGPoint(x: r.minX, y: r.maxY), br = CGPoint(x: r.maxX, y: r.minY)
304
+ ctx.drawLinearGradient(gradient([Palette.faceLight, Palette.face, Palette.faceDark], [0, 0.45, 1]),
305
+ start: tl, end: br, options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])
306
+ let sheen = CGPoint(x: r.minX + r.width * 0.3, y: r.maxY - 3)
307
+ ctx.drawRadialGradient(gradient([NSColor.white.withAlphaComponent(0.13), NSColor.white.withAlphaComponent(0)], [0, 1]),
308
+ startCenter: sheen, startRadius: 0, endCenter: sheen, endRadius: max(r.width, r.height) * 0.7, options: [])
309
+
310
+ // Bevel: light catches the upper-left edge, the lower-right edge falls into shadow
311
+ ctx.addPath(CGPath(roundedRect: r.insetBy(dx: 1.2, dy: 1.2), cornerWidth: 3, cornerHeight: 3, transform: nil))
312
+ ctx.setLineWidth(2.4)
313
+ ctx.replacePathWithStrokedPath()
314
+ ctx.clip()
315
+ ctx.drawLinearGradient(gradient([NSColor.white.withAlphaComponent(0.34), NSColor.white.withAlphaComponent(0),
316
+ NSColor.black.withAlphaComponent(0.5)], [0, 0.5, 1]),
317
+ start: tl, end: br, options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])
318
+ ctx.restoreGState()
319
+ }
320
+
321
+ /// Endpoints of the fine mint links between the bridge and each upright section
322
+ private func connection(_ i: Int, _ rects: [CGRect]) -> (CGPoint, CGPoint) {
323
+ let b = rects[4], s = rects[i]
324
+ let left = i < 2, top = i % 2 == 0
325
+ let from = CGPoint(x: left ? b.minX : b.maxX, y: b.midY + (top ? 5 : -5))
326
+ let to = CGPoint(x: left ? s.maxX - 3 : s.minX + 3, y: top ? s.minY + 6 : s.maxY - 6)
327
+ return (from, to)
328
+ }
329
+
330
+ private func drawConnections(_ ctx: CGContext, _ rects: [CGRect]) {
331
+ guard spread > 0.04 else { return }
332
+ let alpha = min(1, spread * 1.4)
333
+ ctx.saveGState()
334
+ ctx.setShadow(offset: .zero, blur: 4, color: Palette.mint.withAlphaComponent(0.8 * alpha).cgColor)
335
+ ctx.setStrokeColor(Palette.mint.withAlphaComponent(0.85 * alpha).cgColor)
336
+ ctx.setLineWidth(1)
337
+ ctx.setLineCap(.round)
338
+ for i in 0..<4 {
339
+ let (a, b) = connection(i, rects)
340
+ ctx.move(to: a)
341
+ ctx.addLine(to: b)
342
+ }
343
+ // Each upright's two halves stay linked too
344
+ for (top, bottom) in [(0, 1), (2, 3)] {
345
+ let x = rects[top].midX
346
+ ctx.move(to: CGPoint(x: x, y: rects[top].minY + 2))
347
+ ctx.addLine(to: CGPoint(x: x, y: rects[bottom].maxY - 2))
348
+ }
349
+ ctx.strokePath()
350
+ ctx.restoreGState()
351
+ }
352
+
353
+ /// Eyes, plus the thinking pulse that sweeps across the bridge
354
+ private func drawBridgeLife(_ ctx: CGContext, _ b: CGRect) {
355
+ if thinking {
356
+ let phase = CGFloat((t * 0.75).truncatingRemainder(dividingBy: 1))
357
+ let x = b.minX - 10 + (b.width + 20) * easeInOut(phase)
358
+ ctx.saveGState()
359
+ ctx.addPath(path(b))
360
+ ctx.clip()
361
+ let c = CGPoint(x: x, y: b.midY)
362
+ ctx.drawRadialGradient(gradient([Palette.mint.withAlphaComponent(0.6), Palette.mint.withAlphaComponent(0)], [0, 1]),
363
+ startCenter: c, startRadius: 0, endCenter: c, endRadius: 17, options: [])
364
+ ctx.restoreGState()
365
+ }
366
+
367
+ let eyeY = b.midY + look.y - 1.5 * lean - 1.2 * nodAmount
368
+ ctx.saveGState()
369
+ ctx.setShadow(offset: .zero, blur: 5, color: Palette.mint.withAlphaComponent(0.85).cgColor)
370
+ ctx.setFillColor(Palette.mint.cgColor)
371
+ for side: CGFloat in [-1, 1] {
372
+ let ex = b.midX + side * 7 + look.x
373
+ let h: CGFloat = t < blinkUntil ? 1.3 : 6.6
374
+ ctx.addPath(CGPath(roundedRect: CGRect(x: ex - 2.3, y: eyeY - h / 2, width: 4.6, height: h),
375
+ cornerWidth: min(2.3, h / 2), cornerHeight: min(2.3, h / 2), transform: nil))
376
+ }
377
+ ctx.fillPath()
378
+ ctx.restoreGState()
379
+ }
380
+
381
+ private func drawPulse(_ ctx: CGContext, _ p: Pulse, _ rects: [CGRect]) {
382
+ let k = CGFloat((t - p.start) / 0.8)
383
+ guard k >= 0 && k <= 1 else { return }
384
+ let a: CGPoint, b: CGPoint
385
+ if p.route < 0 {
386
+ // Through the bridge, upright to upright
387
+ a = CGPoint(x: rects[0].midX, y: rects[4].midY)
388
+ b = CGPoint(x: rects[2].midX, y: rects[4].midY)
389
+ } else {
390
+ (a, b) = connection(p.route, rects)
391
+ }
392
+ let e = easeInOut(k)
393
+ ctx.saveGState()
394
+ ctx.setShadow(offset: .zero, blur: 7, color: Palette.mintGlow.cgColor)
395
+ for trail in 0..<4 {
396
+ let kk = max(0, e - CGFloat(trail) * 0.07)
397
+ let pt = CGPoint(x: lerp(a.x, b.x, kk), y: lerp(a.y, b.y, kk))
398
+ let r = 2 - CGFloat(trail) * 0.4
399
+ ctx.setFillColor(NSColor.white.withAlphaComponent((1 - CGFloat(trail) * 0.22) * sin(k * .pi)).cgColor)
400
+ ctx.fillEllipse(in: CGRect(x: pt.x - r, y: pt.y - r, width: r * 2, height: r * 2))
401
+ }
402
+ ctx.restoreGState()
403
+ }
404
+
405
+ // MARK: Mouse: drag to move, click to poke
406
+
407
+ override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
408
+
409
+ override func mouseDown(with event: NSEvent) {
410
+ dragStart = NSEvent.mouseLocation
411
+ windowStart = window?.frame.origin ?? .zero
412
+ moved = false
413
+ }
414
+
415
+ override func mouseDragged(with event: NSEvent) {
416
+ guard let start = dragStart, let window else { return }
417
+ let p = NSEvent.mouseLocation
418
+ if abs(p.x - start.x) + abs(p.y - start.y) > 3 { moved = true }
419
+ window.setFrameOrigin(NSPoint(x: windowStart.x + p.x - start.x, y: windowStart.y + p.y - start.y))
420
+ }
421
+
422
+ override func mouseUp(with event: NSEvent) {
423
+ if !moved { onPoke?() }
424
+ dragStart = nil
425
+ }
426
+ }
427
+
428
+ // MARK: - Speech bubble
429
+
430
+ final class BubbleView: NSView {
431
+ var text = "" { didSet { needsDisplay = true } }
432
+ override var isFlipped: Bool { true }
433
+
434
+ private let attrs: [NSAttributedString.Key: Any] = [
435
+ .font: NSFont.monospacedSystemFont(ofSize: 12, weight: .medium),
436
+ .foregroundColor: Palette.paper,
437
+ ]
438
+
439
+ override func draw(_ dirtyRect: NSRect) {
440
+ guard !text.isEmpty else { return }
441
+ let maxW = bounds.width - 30
442
+ let str = NSAttributedString(string: text, attributes: attrs)
443
+ let tb = str.boundingRect(with: NSSize(width: maxW, height: 1000), options: .usesLineFragmentOrigin)
444
+ let textH = min(ceil(tb.height), bounds.height - 30)
445
+ let w = ceil(tb.width) + 24, h = textH + 14
446
+ let box = NSRect(x: (bounds.width - w) / 2, y: bounds.height - 12 - h, width: w, height: h)
447
+
448
+ // Terminal-style dark bubble with a mint edge
449
+ let shape = NSBezierPath(roundedRect: box, xRadius: 7, yRadius: 7)
450
+ let tail = NSBezierPath()
451
+ tail.move(to: NSPoint(x: bounds.midX - 6, y: box.maxY - 1))
452
+ tail.line(to: NSPoint(x: bounds.midX, y: bounds.height - 3))
453
+ tail.line(to: NSPoint(x: bounds.midX + 6, y: box.maxY - 1))
454
+ tail.close()
455
+ shape.append(tail)
456
+
457
+ NSGraphicsContext.saveGraphicsState()
458
+ let glow = NSShadow()
459
+ glow.shadowColor = Palette.mint.withAlphaComponent(0.5)
460
+ glow.shadowBlurRadius = 8
461
+ glow.set()
462
+ Palette.ink.setFill()
463
+ shape.fill()
464
+ NSGraphicsContext.restoreGraphicsState()
465
+ // Wide stroke then refill: only the outer half shows, so the box/tail seam disappears
466
+ Palette.mint.setStroke()
467
+ shape.lineWidth = 2.4
468
+ shape.stroke()
469
+ Palette.ink.setFill()
470
+ shape.fill()
471
+
472
+ str.draw(with: NSRect(x: box.minX + 12, y: box.minY + 7, width: maxW, height: textH),
473
+ options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine])
474
+ }
475
+ }
476
+
477
+ // MARK: - App
478
+
479
+ final class AppDelegate: NSObject, NSApplicationDelegate {
480
+ var window: NSWindow!
481
+ let pet = PetView(frame: NSRect(x: 0, y: 0, width: 220, height: 150))
482
+ let bubble = BubbleView(frame: NSRect(x: 0, y: 130, width: 220, height: 80))
483
+ private var speech: Process?
484
+ private var speechInterruptible = true
485
+ private var hideBubble: DispatchWorkItem?
486
+ private var watchItem: NSMenuItem!
487
+ private var voiceItem: NSMenuItem!
488
+ private var speakAloud: Bool {
489
+ get { UserDefaults.standard.object(forKey: "speakAloud") as? Bool ?? true }
490
+ set { UserDefaults.standard.set(newValue, forKey: "speakAloud") }
491
+ }
492
+ private let hiveyItem = NSMenuItem(title: "hivey: looking…", action: nil, keyEquivalent: "")
493
+ private let watcher = HiveyWatcher()
494
+ private let chat = HiveyChat()
495
+ private lazy var switcher = PetSwitcher(current: "hivey-h", say: { [weak self] in self?.say($0, aloud: false) },
496
+ turnOffHere: { [weak self] in self?.turnOff() })
497
+ private var watching: Bool {
498
+ get { UserDefaults.standard.object(forKey: "watchHivey") as? Bool ?? true }
499
+ set { UserDefaults.standard.set(newValue, forKey: "watchHivey") }
500
+ }
501
+
502
+ private let launchAgent = URL(fileURLWithPath: NSHomeDirectory() + "/Library/LaunchAgents/com.hivey.h.plist")
503
+
504
+ func applicationDidFinishLaunching(_ notification: Notification) {
505
+ let size = NSSize(width: 220, height: 210)
506
+ window = NSWindow(contentRect: NSRect(origin: .zero, size: size), styleMask: .borderless, backing: .buffered, defer: false)
507
+ window.isOpaque = false
508
+ window.backgroundColor = .clear
509
+ window.hasShadow = false
510
+ window.level = .floating
511
+ window.collectionBehavior = [.canJoinAllSpaces, .stationary, .ignoresCycle]
512
+
513
+ let root = NSView(frame: NSRect(origin: .zero, size: size))
514
+ root.addSubview(pet)
515
+ root.addSubview(bubble)
516
+ window.contentView = root
517
+
518
+ if !window.setFrameUsingName("HiveyHPet"), let screen = NSScreen.main {
519
+ let v = screen.visibleFrame
520
+ window.setFrameOrigin(NSPoint(x: v.maxX - size.width - 760, y: v.minY + 10))
521
+ }
522
+ window.setContentSize(size)
523
+ window.setFrameAutosaveName("HiveyHPet")
524
+
525
+ let menu = NSMenu()
526
+ hiveyItem.isEnabled = false
527
+ menu.addItem(hiveyItem)
528
+ watchItem = menu.addItem(withTitle: "Watch hivey", action: #selector(toggleWatch), keyEquivalent: "")
529
+ watchItem.target = self
530
+ watchItem.state = watching ? .on : .off
531
+ voiceItem = menu.addItem(withTitle: "Speak aloud", action: #selector(toggleVoice), keyEquivalent: "")
532
+ voiceItem.target = self
533
+ voiceItem.state = speakAloud ? .on : .off
534
+ menu.addItem(.separator())
535
+ // Preview the pet's moves: they only animate the pet, nothing happens in hivey
536
+ let effects = NSMenu()
537
+ effects.addItem(withTitle: "Assign work", action: #selector(assignWork), keyEquivalent: "").target = self
538
+ effects.addItem(withTitle: "Listen", action: #selector(listen), keyEquivalent: "").target = self
539
+ effects.addItem(withTitle: "Think", action: #selector(think), keyEquivalent: "").target = self
540
+ effects.addItem(withTitle: "Send message", action: #selector(sendMessage), keyEquivalent: "").target = self
541
+ effects.addItem(withTitle: "Task complete", action: #selector(taskDone), keyEquivalent: "").target = self
542
+ let effectsItem = NSMenuItem(title: "Effects (preview only)", action: nil, keyEquivalent: "")
543
+ effectsItem.submenu = effects
544
+ menu.addItem(effectsItem)
545
+ menu.addItem(.separator())
546
+ switcher.addItems(to: menu)
547
+ menu.addItem(withTitle: "Quit Hivey", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "")
548
+ pet.menu = menu
549
+ // Click: talk to the hivey agent (a chat box above the pet); drag still moves it
550
+ pet.onPoke = { [weak self] in
551
+ guard let self else { return }
552
+ pet.listen()
553
+ chat.toggle(above: window)
554
+ }
555
+ chat.say = { [weak self] text, aloud in self?.say(text, aloud: aloud) }
556
+ chat.onSent = { [weak self] in self?.pet.listen(for: 3) }
557
+
558
+ DistributedNotificationCenter.default().addObserver(forName: sayNote, object: nil, queue: .main) { [weak self] note in
559
+ guard let text = note.object as? String, !text.isEmpty else { return }
560
+ MainActor.assumeIsolated {
561
+ self?.pet.listen()
562
+ self?.say(text)
563
+ }
564
+ }
565
+
566
+ // `hivey-h assign|listen|think|message|complete ["text"]`
567
+ DistributedNotificationCenter.default().addObserver(forName: eventNote, object: nil, queue: .main) { [weak self] note in
568
+ guard let raw = note.object as? String else { return }
569
+ let parts = raw.split(separator: "\n", maxSplits: 1, omittingEmptySubsequences: false)
570
+ let text = parts.count > 1 ? String(parts[1]) : ""
571
+ MainActor.assumeIsolated { self?.play(String(parts[0]), text) }
572
+ }
573
+
574
+ watcher.onUpdate = { [weak self] snapshot, events in
575
+ MainActor.assumeIsolated { self?.react(snapshot, events) }
576
+ }
577
+ watcher.onLastWindowClosed = { NSApp.terminate(nil) } // back with the next hivey window
578
+ if watching { watcher.start() } else { hiveyItem.title = "hivey: not watching" }
579
+
580
+
581
+ // Appear: fade in while the sections settle into a complete H, then nod, glow and welcome
582
+ window.alphaValue = 0
583
+ window.orderFrontRegardless()
584
+ NSAnimationContext.runAnimationGroup { $0.duration = 0.7; window.animator().alphaValue = 1 }
585
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
586
+ self?.pet.complete()
587
+ self?.say("Welcome. I'm Hivey.")
588
+ }
589
+ }
590
+
591
+ func say(_ text: String, aloud: Bool = true) {
592
+ if speechInterruptible { speech?.terminate() } // hivey's lines queue instead
593
+ hideBubble?.cancel()
594
+ bubble.text = text
595
+ guard aloud && speakAloud else { scheduleHide(after: 2.5); return }
596
+ pet.talking = true
597
+
598
+ // `say` reads text from stdin, so text starting with "-" is never parsed as a flag
599
+ let (p, interruptible) = speechProcess()
600
+ speechInterruptible = interruptible
601
+ let pipe = Pipe()
602
+ p.standardInput = pipe
603
+ p.terminationHandler = { [weak self] proc in
604
+ DispatchQueue.main.async { self?.finished(proc) }
605
+ }
606
+ speech = p
607
+ do { try p.run() } catch { finished(p); return }
608
+ pipe.fileHandleForWriting.write(Data(text.utf8))
609
+ try? pipe.fileHandleForWriting.close()
610
+ }
611
+
612
+ private func finished(_ p: Process) {
613
+ guard speech === p else { return } // a newer utterance replaced this one
614
+ speech = nil
615
+ pet.talking = false
616
+ scheduleHide(after: 2)
617
+ }
618
+
619
+ private func scheduleHide(after seconds: Double) {
620
+ let work = DispatchWorkItem { [weak self] in self?.bubble.text = "" }
621
+ hideBubble = work
622
+ DispatchQueue.main.asyncAfter(deadline: .now() + seconds, execute: work)
623
+ }
624
+
625
+ /// One animation by name (event CLI), optionally saying `text`
626
+ private func play(_ event: String, _ text: String) {
627
+ switch event {
628
+ case "assign": pet.assign()
629
+ case "listen": pet.listen()
630
+ case "think": pet.think()
631
+ case "message": pet.message()
632
+ case "complete": pet.complete()
633
+ case "chat": chat.toggle(above: window); return // same as clicking the pet
634
+ default: return
635
+ }
636
+ if !text.isEmpty { say(text) }
637
+ }
638
+
639
+ /// hivey changed: act it out with the existing movements; one bubble per look, most urgent first
640
+ private func react(_ snapshot: HiveySnapshot, _ events: [HiveyEvent]) {
641
+ guard watching else { return }
642
+ pet.agentCount = (snapshot.working, snapshot.idle)
643
+ if watcher.hiveyPath == nil {
644
+ hiveyItem.title = "hivey: not installed"
645
+ } else if snapshot.swarms.isEmpty {
646
+ hiveyItem.title = "hivey: nothing running"
647
+ } else {
648
+ hiveyItem.title = "hivey: \(snapshot.status.count) agents, \(snapshot.working) working"
649
+ }
650
+ if snapshot.working > 0 { pet.think(for: 2.5) } // renewed every look while agents work
651
+
652
+ var line: (text: String, aloud: Bool, rank: Int)?
653
+ func offer(_ text: String, aloud: Bool, rank: Int) {
654
+ if line == nil || rank > line!.rank { line = (text, aloud, rank) }
655
+ }
656
+ var pulses = 0
657
+ for event in events {
658
+ switch event {
659
+ case .launched(let slug):
660
+ pet.assign()
661
+ offer("\(slug) is starting.", aloud: true, rank: 3)
662
+ case .needsYou(let names):
663
+ pet.listen(for: 4)
664
+ offer(needsYouLine(names), aloud: true, rank: 4)
665
+ case .finished(let name):
666
+ pet.complete()
667
+ offer("\(name) finished.", aloud: false, rank: 1)
668
+ case .message(let fromHuman, let replyFrom, let text):
669
+ if fromHuman { pet.listen() }
670
+ if let replyFrom { offer(replyLine(from: replyFrom, text: text), aloud: true, rank: 2) }
671
+ // A few pulses, staggered so each one reads; a burst of messages stays calm
672
+ if pulses < 3 {
673
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.35 * Double(pulses)) { [weak self] in
674
+ self?.pet.message()
675
+ }
676
+ pulses += 1
677
+ }
678
+ }
679
+ }
680
+ if let line { say(line.text, aloud: line.aloud) }
681
+ }
682
+
683
+ private func needsYouLine(_ names: [String]) -> String {
684
+ switch names.count {
685
+ case 1: return "\(names[0]) needs you."
686
+ case 2: return "\(names[0]) and \(names[1]) need you."
687
+ default: return "\(names.count) agents need you."
688
+ }
689
+ }
690
+
691
+ @objc func toggleVoice() {
692
+ speakAloud.toggle()
693
+ voiceItem.state = speakAloud ? .on : .off
694
+ }
695
+
696
+ @objc func toggleWatch() {
697
+ watching.toggle()
698
+ watchItem.state = watching ? .on : .off
699
+ if watching {
700
+ hiveyItem.title = "hivey: looking…"
701
+ watcher.start()
702
+ } else {
703
+ watcher.stop()
704
+ pet.agentCount = (0, 0)
705
+ hiveyItem.title = "hivey: not watching"
706
+ }
707
+ }
708
+
709
+ @objc func assignWork() { pet.assign() }
710
+ @objc func listen() { pet.listen() }
711
+ @objc func think() { pet.think() }
712
+ @objc func sendMessage() { pet.message() }
713
+ @objc func taskDone() { pet.complete() }
714
+
715
+ /// Without hivey: remove any old login item and quit
716
+ private func turnOff() {
717
+ setLogin(false)
718
+ NSApp.terminate(nil)
719
+ }
720
+
721
+ private func setLogin(_ on: Bool) {
722
+ guard on else { try? FileManager.default.removeItem(at: launchAgent); return }
723
+ let plist: [String: Any] = [
724
+ "Label": "com.hivey.h",
725
+ "ProgramArguments": ["/usr/bin/open", "-a", Bundle.main.bundlePath],
726
+ "RunAtLoad": true,
727
+ ]
728
+ try? FileManager.default.createDirectory(at: launchAgent.deletingLastPathComponent(), withIntermediateDirectories: true)
729
+ if let data = try? PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0) {
730
+ try? data.write(to: launchAgent)
731
+ }
732
+ }
733
+ }
734
+
735
+ let app = NSApplication.shared
736
+ app.setActivationPolicy(.accessory)
737
+ let delegate = AppDelegate()
738
+ app.delegate = delegate
739
+ app.run()