@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,691 @@
1
+ import AppKit
2
+
3
+ // Hivey Prompt — a satin charcoal-teal sphere with a mint `>_` face, leading three agent spheres.
4
+ // Watches hivey: its swarms, agents and messages drive attend / work / emit / complete.
5
+ // CLI: hivey-prompt → launch the pet
6
+ // hivey-prompt say "hi" → make the running pet say something
7
+ // hivey-prompt attend|work|emit|complete ["text"] → play that move (and say the text)
8
+
9
+ let sayNote = Notification.Name("com.hivey.prompt.say")
10
+ let eventNote = Notification.Name("com.hivey.prompt.event")
11
+ let petEvents = ["attend", "work", "emit", "complete", "chat"]
12
+
13
+ let cliArgs = CommandLine.arguments
14
+ if cliArgs.count >= 2 && cliArgs[1] == "say" {
15
+ let text = cliArgs.dropFirst(2).joined(separator: " ")
16
+ DistributedNotificationCenter.default().postNotificationName(sayNote, object: text, userInfo: nil, deliverImmediately: true)
17
+ exit(0)
18
+ }
19
+ if cliArgs.count >= 2 && petEvents.contains(cliArgs[1]) {
20
+ // "event\ntext": the object is a plain string, which distributed notifications always carry
21
+ let text = cliArgs.dropFirst(2).joined(separator: " ")
22
+ DistributedNotificationCenter.default().postNotificationName(eventNote, object: cliArgs[1] + "\n" + text, userInfo: nil, deliverImmediately: true)
23
+ exit(0)
24
+ }
25
+
26
+ // One Hivey Prompt per desktop
27
+ if let id = Bundle.main.bundleIdentifier,
28
+ NSRunningApplication.runningApplications(withBundleIdentifier: id).count > 1 {
29
+ exit(0)
30
+ }
31
+
32
+ // MARK: - Palette
33
+
34
+ extension NSColor {
35
+ convenience init(hex: Int, alpha: CGFloat = 1) {
36
+ self.init(srgbRed: CGFloat((hex >> 16) & 0xff) / 255,
37
+ green: CGFloat((hex >> 8) & 0xff) / 255,
38
+ blue: CGFloat(hex & 0xff) / 255,
39
+ alpha: alpha)
40
+ }
41
+ }
42
+
43
+ /// Shading ramp for a satin sphere lit from the upper left
44
+ struct Tone {
45
+ let light: NSColor, base: NSColor, dark: NSColor, bounce: NSColor
46
+
47
+ /// Push toward the background haze (for spheres further away)
48
+ func dimmed(_ k: CGFloat) -> Tone {
49
+ let haze = NSColor(hex: 0x1a2426)
50
+ return Tone(light: light.blended(withFraction: k, of: haze)!, base: base.blended(withFraction: k, of: haze)!,
51
+ dark: dark.blended(withFraction: k * 0.5, of: haze)!, bounce: bounce.blended(withFraction: k, of: haze)!)
52
+ }
53
+ }
54
+
55
+ enum Palette {
56
+ static let mint = NSColor(hex: 0x8ff0cc)
57
+ static let mintGlow = NSColor(hex: 0xc4ffea)
58
+ static let ink = NSColor(hex: 0x14191b)
59
+ static let paper = NSColor(hex: 0xfafcfb)
60
+
61
+ static let hivey = Tone(light: NSColor(hex: 0x5d8c88), base: NSColor(hex: 0x24403f),
62
+ dark: NSColor(hex: 0x0a1314), bounce: NSColor(hex: 0x3f7a6c))
63
+ static let emerald = Tone(light: NSColor(hex: 0x8af0c4), base: NSColor(hex: 0x23a874),
64
+ dark: NSColor(hex: 0x0b4a33), bounce: NSColor(hex: 0x5fd6a6))
65
+ static let mutedMint = Tone(light: NSColor(hex: 0xd6f2e6), base: NSColor(hex: 0x86bfa9),
66
+ dark: NSColor(hex: 0x345a4c), bounce: NSColor(hex: 0xb4e2cf))
67
+ }
68
+
69
+ func lerp(_ a: CGFloat, _ b: CGFloat, _ k: CGFloat) -> CGFloat { a + (b - a) * k }
70
+ func easeInOut(_ k: CGFloat) -> CGFloat { k * k * (3 - 2 * k) }
71
+
72
+ let sRGB = CGColorSpace(name: CGColorSpace.sRGB)!
73
+
74
+ func gradient(_ colors: [NSColor], _ locations: [CGFloat]) -> CGGradient {
75
+ CGGradient(colorsSpace: sRGB, colors: colors.map(\.cgColor) as CFArray, locations: locations)!
76
+ }
77
+
78
+ /// Satin sphere: directional key light, dark falloff, bounce light on the underside, soft specular
79
+ func drawSphere(_ ctx: CGContext, _ c: CGPoint, _ r: CGFloat, _ tone: Tone) {
80
+ ctx.saveGState()
81
+ ctx.addEllipse(in: CGRect(x: c.x - r, y: c.y - r, width: r * 2, height: r * 2))
82
+ ctx.clip()
83
+
84
+ ctx.drawRadialGradient(gradient([tone.light, tone.base, tone.dark], [0, 0.48, 1]),
85
+ startCenter: CGPoint(x: c.x - r * 0.38, y: c.y + r * 0.42), startRadius: 0,
86
+ endCenter: c, endRadius: r * 1.1, options: [.drawsAfterEndLocation])
87
+
88
+ // Light bouncing up from the floor onto the lower edge
89
+ ctx.drawRadialGradient(gradient([tone.bounce.withAlphaComponent(0.55), tone.bounce.withAlphaComponent(0)], [0, 1]),
90
+ startCenter: CGPoint(x: c.x + r * 0.2, y: c.y - r * 1.15), startRadius: 0,
91
+ endCenter: CGPoint(x: c.x + r * 0.2, y: c.y - r * 1.15), endRadius: r * 0.85, options: [])
92
+
93
+ // Broad satin sheen + small crisp highlight
94
+ let spec = CGPoint(x: c.x - r * 0.36, y: c.y + r * 0.46)
95
+ ctx.drawRadialGradient(gradient([NSColor.white.withAlphaComponent(0.28), NSColor.white.withAlphaComponent(0)], [0, 1]),
96
+ startCenter: spec, startRadius: 0, endCenter: spec, endRadius: r * 0.55, options: [])
97
+ ctx.drawRadialGradient(gradient([NSColor.white.withAlphaComponent(0.55), NSColor.white.withAlphaComponent(0)], [0, 1]),
98
+ startCenter: spec, startRadius: 0, endCenter: spec, endRadius: r * 0.16, options: [])
99
+ ctx.restoreGState()
100
+ }
101
+
102
+ /// Soft contact shadow on the floor
103
+ func drawShadow(_ ctx: CGContext, _ c: CGPoint, width: CGFloat, alpha: CGFloat) {
104
+ ctx.saveGState()
105
+ ctx.translateBy(x: c.x, y: c.y)
106
+ ctx.scaleBy(x: 1, y: 0.22)
107
+ ctx.drawRadialGradient(gradient([NSColor(white: 0, alpha: alpha), NSColor(white: 0, alpha: 0)], [0, 1]),
108
+ startCenter: .zero, startRadius: 0, endCenter: .zero, endRadius: width / 2, options: [])
109
+ ctx.restoreGState()
110
+ }
111
+
112
+ // MARK: - Pet
113
+
114
+ final class PetView: NSView {
115
+ enum Mode { case idle, gather, swarm }
116
+
117
+ struct Agent {
118
+ let tone: Tone
119
+ let size: CGFloat
120
+ let seed: Double
121
+ var pos: CGPoint
122
+ var z: CGFloat // -1 = behind Hivey, +1 = nearer the viewer
123
+ var vel = CGVector.zero
124
+ var vz: CGFloat = 0
125
+ }
126
+ struct Pulse { let from: Int, to: Int; let start: Double } // index -1 = Hivey
127
+
128
+ let center = CGPoint(x: 120, y: 74)
129
+ let radius: CGFloat = 28
130
+ private var floorY: CGFloat { center.y - radius - 9 }
131
+
132
+ // Target spots per mode: (x, y, z) for each of the three agents
133
+ private let spots: [Mode: [(CGFloat, CGFloat, CGFloat)]] = [
134
+ .idle: [(68, 54, 0.75), (178, 104, -0.65), (52, 108, -0.25)],
135
+ .gather: [(90, 50, 0.85), (152, 96, -0.55), (86, 100, -0.45)],
136
+ .swarm: [(36, 80, 0.2), (204, 80, 0.2), (120, 132, -0.7)],
137
+ ]
138
+
139
+ var talking = false
140
+ var onPoke: (() -> Void)?
141
+
142
+ private var t = 0.0
143
+ private var mode = Mode.idle
144
+ private var modeUntil = 0.0
145
+ private var agents: [Agent] = []
146
+ private var pulses: [Pulse] = []
147
+ private var nextPulse = 4.0
148
+ private var nodStart = -10.0
149
+ private var glowUntil = -1.0
150
+ private var tilt: CGFloat = 0
151
+ private var typing: CGFloat = 1
152
+
153
+ private var dragStart: NSPoint?
154
+ private var windowStart = NSPoint.zero
155
+ private var moved = false
156
+
157
+ override init(frame: NSRect) {
158
+ super.init(frame: frame)
159
+ let tones = [Palette.emerald, Palette.mutedMint, Palette.emerald]
160
+ let sizes: [CGFloat] = [9, 8, 7.5]
161
+ for (i, s) in spots[.idle]!.enumerated() {
162
+ agents.append(Agent(tone: tones[i], size: sizes[i], seed: Double(i) * 2.1, pos: CGPoint(x: s.0, y: s.1), z: s.2))
163
+ }
164
+ let timer = Timer(timeInterval: 1.0 / 60, repeats: true) { [weak self] _ in
165
+ MainActor.assumeIsolated { self?.tick() }
166
+ }
167
+ RunLoop.main.add(timer, forMode: .common)
168
+ }
169
+
170
+ required init?(coder: NSCoder) { fatalError() }
171
+
172
+ // MARK: Behaviors
173
+
174
+ /// Receiving instructions: Hivey tilts in attention, agents gather close
175
+ func attend(for seconds: Double = 3) { setMode(.gather, for: seconds) }
176
+
177
+ /// Swarm at work: agents spread into formation, messages flow, cursor pulses
178
+ func work(for seconds: Double = 8) {
179
+ setMode(.swarm, for: seconds)
180
+ nextPulse = t + 0.6
181
+ }
182
+
183
+ /// hivey agents are working: stay in formation (an attention gather is not interrupted)
184
+ func keepWorking() {
185
+ switch mode {
186
+ case .gather: return
187
+ case .swarm: modeUntil = max(modeUntil, t + 2.5)
188
+ case .idle: work(for: 2.5)
189
+ }
190
+ }
191
+
192
+ /// One message: a single pulse between two agents (or an agent and Hivey)
193
+ func pulse() {
194
+ let a = Int.random(in: 0..<agents.count)
195
+ var b = Int.random(in: -1..<agents.count - 1)
196
+ if b >= a { b += 1 }
197
+ pulses.append(Pulse(from: a, to: b, start: t))
198
+ }
199
+
200
+ /// Dispatch: a message pulse from Hivey to each agent
201
+ func emit() {
202
+ for i in agents.indices { pulses.append(Pulse(from: -1, to: i, start: t + Double(i) * 0.12)) }
203
+ }
204
+
205
+ /// Task complete: agents report in, Hivey gives a restrained nod and a brief glow
206
+ func complete() {
207
+ setMode(.idle, for: 0)
208
+ for i in agents.indices { pulses.append(Pulse(from: i, to: -1, start: t + Double(i) * 0.1)) }
209
+ nodStart = t + 0.35
210
+ glowUntil = t + 1.5
211
+ }
212
+
213
+ private func setMode(_ m: Mode, for seconds: Double) {
214
+ mode = m
215
+ modeUntil = t + seconds
216
+ }
217
+
218
+ private var working: Bool { mode == .swarm }
219
+
220
+ // MARK: Animation
221
+
222
+ private func tick() {
223
+ guard window?.isVisible == true else { return } // hidden: skip animation work
224
+ let dt = 1.0 / 60
225
+ t += dt
226
+ if mode != .idle && t >= modeUntil { mode = .idle }
227
+
228
+ tilt += ((mode == .gather ? -0.13 : 0) - tilt) * CGFloat(min(1, dt * 5))
229
+
230
+ // Deliberate spring toward each agent's spot, plus a quiet hover
231
+ let targets = spots[mode]!
232
+ for i in agents.indices {
233
+ var a = agents[i]
234
+ let s = targets[i]
235
+ let sync = mode == .swarm ? sin(t * 2.4) * 2 : sin(t * 1.1 + a.seed) * 2.4
236
+ let goal = CGPoint(x: s.0 + CGFloat(sin(t * 0.7 + a.seed * 1.7)) * (mode == .swarm ? 0 : 1.5),
237
+ y: s.1 + CGFloat(sync))
238
+ let k: CGFloat = 9, damping = CGFloat(exp(-6.0 * dt))
239
+ a.vel.dx = (a.vel.dx + (goal.x - a.pos.x) * k * CGFloat(dt)) * damping
240
+ a.vel.dy = (a.vel.dy + (goal.y - a.pos.y) * k * CGFloat(dt)) * damping
241
+ a.vz = (a.vz + (s.2 - a.z) * k * CGFloat(dt)) * damping
242
+ a.pos.x += a.vel.dx * CGFloat(dt)
243
+ a.pos.y += a.vel.dy * CGFloat(dt)
244
+ a.z += a.vz * CGFloat(dt)
245
+ agents[i] = a
246
+ }
247
+
248
+ // Agents talk: steady while the swarm works, occasional otherwise
249
+ pulses.removeAll { t - $0.start > 0.7 }
250
+ if t >= nextPulse {
251
+ let a = Int.random(in: 0..<agents.count)
252
+ var b = Int.random(in: -1..<agents.count - 1)
253
+ if b >= a { b += 1 }
254
+ pulses.append(Pulse(from: a, to: b, start: t))
255
+ nextPulse = t + (working ? Double.random(in: 0.35...0.8) : Double.random(in: 4...8))
256
+ }
257
+
258
+ // While speaking, the `_` flickers like text being typed
259
+ typing = talking ? CGFloat.random(in: 0.35...1.3) * 0.25 + typing * 0.75 : typing + (1 - typing) * 0.2
260
+
261
+ needsDisplay = true
262
+ }
263
+
264
+ /// 0 → 1 → 0 over the nod
265
+ private var nodAmount: CGFloat {
266
+ let k = (t - nodStart) / 0.8
267
+ guard k >= 0 && k < 1 else { return 0 }
268
+ return CGFloat(sin(k * .pi))
269
+ }
270
+
271
+ /// Cursor opacity: slow blink at rest, steady pulse while working
272
+ private var cursorAlpha: CGFloat {
273
+ if working { return 0.55 + 0.45 * CGFloat(sin(t * 6)) }
274
+ return t.truncatingRemainder(dividingBy: 1.6) < 1.0 ? 1 : 0.15
275
+ }
276
+
277
+ private func agentRadius(_ a: Agent) -> CGFloat { a.size * (1 + 0.18 * a.z) }
278
+
279
+ // MARK: Drawing
280
+
281
+ override func draw(_ dirtyRect: NSRect) {
282
+ let ctx = NSGraphicsContext.current!.cgContext
283
+ let rise = 1.8 * CGFloat(sin(t * 1.4))
284
+ let c = CGPoint(x: center.x, y: center.y + rise - 2.5 * nodAmount)
285
+
286
+ // Floor shadows: further agents sit higher on the floor plane
287
+ drawShadow(ctx, CGPoint(x: center.x + 3, y: floorY), width: 66 - rise * 1.5, alpha: 0.42)
288
+ for a in agents {
289
+ let height = max(0, a.pos.y - floorY)
290
+ drawShadow(ctx, CGPoint(x: a.pos.x + 2, y: floorY - a.z * 9), width: agentRadius(a) * 2.6,
291
+ alpha: 0.28 * (1 - min(1, height / 140)))
292
+ }
293
+
294
+ let order = agents.indices.sorted { agents[$0].z < agents[$1].z }
295
+ for i in order where agents[i].z < 0 { drawAgent(ctx, agents[i]) }
296
+ drawBody(ctx, at: c)
297
+ for i in order where agents[i].z >= 0 { drawAgent(ctx, agents[i]) }
298
+
299
+ for p in pulses where t >= p.start {
300
+ let a = p.from < 0 ? c : agents[p.from].pos
301
+ let b = p.to < 0 ? c : agents[p.to].pos
302
+ drawPulse(p, from: a, to: b)
303
+ }
304
+ }
305
+
306
+ private func drawAgent(_ ctx: CGContext, _ a: Agent) {
307
+ drawSphere(ctx, a.pos, agentRadius(a), a.tone.dimmed(max(0, -a.z) * 0.35))
308
+ }
309
+
310
+ private func drawBody(_ ctx: CGContext, at c: CGPoint) {
311
+ let glowing = t < glowUntil
312
+ if glowing {
313
+ // Brief mint glow around the body
314
+ let k = CGFloat((glowUntil - t) / 1.5)
315
+ ctx.saveGState()
316
+ ctx.setShadow(offset: .zero, blur: 18, color: Palette.mint.withAlphaComponent(0.85 * k).cgColor)
317
+ Palette.ink.setFill()
318
+ NSBezierPath(ovalIn: NSRect(x: c.x - radius + 1, y: c.y - radius + 1, width: radius * 2 - 2, height: radius * 2 - 2)).fill()
319
+ ctx.restoreGState()
320
+ }
321
+ drawSphere(ctx, c, radius, Palette.hivey)
322
+
323
+ // `>_` face, tilting with attention and dipping with the nod
324
+ ctx.saveGState()
325
+ ctx.translateBy(x: c.x + tilt * 18, y: c.y - 1.5 * nodAmount)
326
+ ctx.rotate(by: tilt)
327
+ ctx.setShadow(offset: .zero, blur: 4, color: Palette.mint.withAlphaComponent(0.55).cgColor)
328
+
329
+ Palette.mint.setStroke()
330
+ let chevron = NSBezierPath()
331
+ chevron.move(to: NSPoint(x: -11, y: 7))
332
+ chevron.line(to: NSPoint(x: -3, y: 1))
333
+ chevron.line(to: NSPoint(x: -11, y: -5))
334
+ chevron.lineWidth = 3
335
+ chevron.lineCapStyle = .round
336
+ chevron.lineJoinStyle = .round
337
+ chevron.stroke()
338
+
339
+ Palette.mint.withAlphaComponent(talking ? 1 : cursorAlpha).setFill()
340
+ NSBezierPath(roundedRect: NSRect(x: 1, y: -6.5, width: 11 * typing, height: 3), xRadius: 1.5, yRadius: 1.5).fill()
341
+ ctx.restoreGState()
342
+ }
343
+
344
+ private func drawPulse(_ p: Pulse, from a: CGPoint, to b: CGPoint) {
345
+ let k = CGFloat((t - p.start) / 0.7)
346
+ let e = easeInOut(k)
347
+ for trail in 0..<4 {
348
+ let kk = max(0, e - CGFloat(trail) * 0.06)
349
+ let pt = NSPoint(x: lerp(a.x, b.x, kk), y: lerp(a.y, b.y, kk) + 5 * sin(kk * .pi))
350
+ let r = 2.1 - CGFloat(trail) * 0.42
351
+ NSGraphicsContext.saveGraphicsState()
352
+ let glow = NSShadow()
353
+ glow.shadowColor = Palette.mintGlow
354
+ glow.shadowBlurRadius = 7
355
+ glow.set()
356
+ NSColor.white.withAlphaComponent((1 - CGFloat(trail) * 0.22) * sin(k * .pi)).setFill()
357
+ NSBezierPath(ovalIn: NSRect(x: pt.x - r, y: pt.y - r, width: r * 2, height: r * 2)).fill()
358
+ NSGraphicsContext.restoreGraphicsState()
359
+ }
360
+ }
361
+
362
+ // MARK: Mouse: drag to move, click to poke
363
+
364
+ override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
365
+
366
+ override func mouseDown(with event: NSEvent) {
367
+ dragStart = NSEvent.mouseLocation
368
+ windowStart = window?.frame.origin ?? .zero
369
+ moved = false
370
+ }
371
+
372
+ override func mouseDragged(with event: NSEvent) {
373
+ guard let start = dragStart, let window else { return }
374
+ let p = NSEvent.mouseLocation
375
+ if abs(p.x - start.x) + abs(p.y - start.y) > 3 { moved = true }
376
+ window.setFrameOrigin(NSPoint(x: windowStart.x + p.x - start.x, y: windowStart.y + p.y - start.y))
377
+ }
378
+
379
+ override func mouseUp(with event: NSEvent) {
380
+ if !moved { onPoke?() }
381
+ dragStart = nil
382
+ }
383
+ }
384
+
385
+ // MARK: - Speech bubble
386
+
387
+ final class BubbleView: NSView {
388
+ var text = "" { didSet { needsDisplay = true } }
389
+ override var isFlipped: Bool { true }
390
+
391
+ private let attrs: [NSAttributedString.Key: Any] = [
392
+ .font: NSFont.monospacedSystemFont(ofSize: 12, weight: .medium),
393
+ .foregroundColor: Palette.paper,
394
+ ]
395
+
396
+ override func draw(_ dirtyRect: NSRect) {
397
+ guard !text.isEmpty else { return }
398
+ let maxW = bounds.width - 30
399
+ let str = NSAttributedString(string: text, attributes: attrs)
400
+ let tb = str.boundingRect(with: NSSize(width: maxW, height: 1000), options: .usesLineFragmentOrigin)
401
+ let textH = min(ceil(tb.height), bounds.height - 30)
402
+ let w = ceil(tb.width) + 24, h = textH + 14
403
+ let box = NSRect(x: (bounds.width - w) / 2, y: bounds.height - 12 - h, width: w, height: h)
404
+
405
+ // Terminal-style dark bubble with a mint edge
406
+ let shape = NSBezierPath(roundedRect: box, xRadius: 7, yRadius: 7)
407
+ let tail = NSBezierPath()
408
+ tail.move(to: NSPoint(x: bounds.midX - 6, y: box.maxY - 1))
409
+ tail.line(to: NSPoint(x: bounds.midX, y: bounds.height - 3))
410
+ tail.line(to: NSPoint(x: bounds.midX + 6, y: box.maxY - 1))
411
+ tail.close()
412
+ shape.append(tail)
413
+
414
+ NSGraphicsContext.saveGraphicsState()
415
+ let glow = NSShadow()
416
+ glow.shadowColor = Palette.mint.withAlphaComponent(0.5)
417
+ glow.shadowBlurRadius = 8
418
+ glow.set()
419
+ Palette.ink.setFill()
420
+ shape.fill()
421
+ NSGraphicsContext.restoreGraphicsState()
422
+ // Wide stroke then refill: only the outer half shows, so the box/tail seam disappears
423
+ Palette.mint.setStroke()
424
+ shape.lineWidth = 2.4
425
+ shape.stroke()
426
+ Palette.ink.setFill()
427
+ shape.fill()
428
+
429
+ str.draw(with: NSRect(x: box.minX + 12, y: box.minY + 7, width: maxW, height: textH),
430
+ options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine])
431
+ }
432
+ }
433
+
434
+ // MARK: - App
435
+
436
+ final class AppDelegate: NSObject, NSApplicationDelegate {
437
+ var window: NSWindow!
438
+ let pet = PetView(frame: NSRect(x: 0, y: 0, width: 240, height: 150))
439
+ let bubble = BubbleView(frame: NSRect(x: 0, y: 134, width: 240, height: 80))
440
+ private var speech: Process?
441
+ private var speechInterruptible = true
442
+ private var hideBubble: DispatchWorkItem?
443
+ private var watchItem: NSMenuItem!
444
+ private var voiceItem: NSMenuItem!
445
+ private let hiveyItem = NSMenuItem(title: "hivey: looking…", action: nil, keyEquivalent: "")
446
+ private let watcher = HiveyWatcher()
447
+ private let chat = HiveyChat()
448
+ private lazy var switcher = PetSwitcher(current: "hivey-prompt", say: { [weak self] in self?.say($0, aloud: false) },
449
+ turnOffHere: { [weak self] in self?.turnOff() })
450
+ private var watching: Bool {
451
+ get { UserDefaults.standard.object(forKey: "watchHivey") as? Bool ?? true }
452
+ set { UserDefaults.standard.set(newValue, forKey: "watchHivey") }
453
+ }
454
+ private var speakAloud: Bool {
455
+ get { UserDefaults.standard.object(forKey: "speakAloud") as? Bool ?? true }
456
+ set { UserDefaults.standard.set(newValue, forKey: "speakAloud") }
457
+ }
458
+
459
+ private let launchAgent = URL(fileURLWithPath: NSHomeDirectory() + "/Library/LaunchAgents/com.hivey.prompt.plist")
460
+
461
+ func applicationDidFinishLaunching(_ notification: Notification) {
462
+ let size = NSSize(width: 240, height: 214)
463
+ window = NSWindow(contentRect: NSRect(origin: .zero, size: size), styleMask: .borderless, backing: .buffered, defer: false)
464
+ window.isOpaque = false
465
+ window.backgroundColor = .clear
466
+ window.hasShadow = false
467
+ window.level = .floating
468
+ window.collectionBehavior = [.canJoinAllSpaces, .stationary, .ignoresCycle]
469
+
470
+ let root = NSView(frame: NSRect(origin: .zero, size: size))
471
+ root.addSubview(pet)
472
+ root.addSubview(bubble)
473
+ window.contentView = root
474
+
475
+ if !window.setFrameUsingName("HiveyPromptPet"), let screen = NSScreen.main {
476
+ let v = screen.visibleFrame
477
+ window.setFrameOrigin(NSPoint(x: v.maxX - size.width - 520, y: v.minY + 10))
478
+ }
479
+ window.setContentSize(size)
480
+ window.setFrameAutosaveName("HiveyPromptPet")
481
+
482
+ let menu = NSMenu()
483
+ hiveyItem.isEnabled = false
484
+ menu.addItem(hiveyItem)
485
+ watchItem = menu.addItem(withTitle: "Watch hivey", action: #selector(toggleWatch), keyEquivalent: "")
486
+ watchItem.target = self
487
+ watchItem.state = watching ? .on : .off
488
+ voiceItem = menu.addItem(withTitle: "Speak aloud", action: #selector(toggleVoice), keyEquivalent: "")
489
+ voiceItem.target = self
490
+ voiceItem.state = speakAloud ? .on : .off
491
+ menu.addItem(.separator())
492
+ // Preview the pet's moves: they only animate the pet, nothing happens in hivey
493
+ let effects = NSMenu()
494
+ effects.addItem(withTitle: "Give instructions", action: #selector(instruct), keyEquivalent: "").target = self
495
+ effects.addItem(withTitle: "Start swarm", action: #selector(startWork), keyEquivalent: "").target = self
496
+ effects.addItem(withTitle: "Dispatch message", action: #selector(dispatch), keyEquivalent: "").target = self
497
+ effects.addItem(withTitle: "Task complete", action: #selector(taskDone), keyEquivalent: "").target = self
498
+ let effectsItem = NSMenuItem(title: "Effects (preview only)", action: nil, keyEquivalent: "")
499
+ effectsItem.submenu = effects
500
+ menu.addItem(effectsItem)
501
+ menu.addItem(.separator())
502
+ switcher.addItems(to: menu)
503
+ menu.addItem(withTitle: "Quit Hivey Prompt", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "")
504
+ pet.menu = menu
505
+ // Click: talk to the hivey agent (a chat box above the pet); drag still moves it
506
+ pet.onPoke = { [weak self] in
507
+ guard let self else { return }
508
+ pet.attend()
509
+ chat.toggle(above: window)
510
+ }
511
+ chat.say = { [weak self] text, aloud in self?.say(text, aloud: aloud) }
512
+ chat.onSent = { [weak self] in self?.pet.attend() }
513
+
514
+ // `hivey-prompt attend|work|emit|complete ["text"]`
515
+ DistributedNotificationCenter.default().addObserver(forName: eventNote, object: nil, queue: .main) { [weak self] note in
516
+ guard let raw = note.object as? String else { return }
517
+ let parts = raw.split(separator: "\n", maxSplits: 1, omittingEmptySubsequences: false)
518
+ let text = parts.count > 1 ? String(parts[1]) : ""
519
+ MainActor.assumeIsolated { self?.play(String(parts[0]), text) }
520
+ }
521
+
522
+ watcher.onUpdate = { [weak self] snapshot, events in
523
+ MainActor.assumeIsolated { self?.react(snapshot, events) }
524
+ }
525
+ watcher.onLastWindowClosed = { NSApp.terminate(nil) } // back with the next hivey window
526
+ if watching { watcher.start() } else { hiveyItem.title = "hivey: not watching" }
527
+
528
+ DistributedNotificationCenter.default().addObserver(forName: sayNote, object: nil, queue: .main) { [weak self] note in
529
+ guard let text = note.object as? String, !text.isEmpty else { return }
530
+ MainActor.assumeIsolated {
531
+ self?.pet.attend()
532
+ self?.say(text)
533
+ }
534
+ }
535
+
536
+
537
+ // Appear: fade in, Hivey checks in with its team, welcome
538
+ window.alphaValue = 0
539
+ window.orderFrontRegardless()
540
+ NSAnimationContext.runAnimationGroup { $0.duration = 0.7; window.animator().alphaValue = 1 }
541
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) { [weak self] in
542
+ self?.pet.emit()
543
+ self?.pet.complete()
544
+ self?.say("Welcome. I'm Hivey.")
545
+ }
546
+ }
547
+
548
+ func say(_ text: String, aloud: Bool = true) {
549
+ if speechInterruptible { speech?.terminate() } // hivey's lines queue instead
550
+ hideBubble?.cancel()
551
+ bubble.text = text
552
+ guard aloud && speakAloud else { scheduleHide(after: 2.5); return }
553
+ pet.talking = true
554
+
555
+ // `say` reads text from stdin, so text starting with "-" is never parsed as a flag
556
+ let (p, interruptible) = speechProcess()
557
+ speechInterruptible = interruptible
558
+ let pipe = Pipe()
559
+ p.standardInput = pipe
560
+ p.terminationHandler = { [weak self] proc in
561
+ DispatchQueue.main.async { self?.finished(proc) }
562
+ }
563
+ speech = p
564
+ do { try p.run() } catch { finished(p); return }
565
+ pipe.fileHandleForWriting.write(Data(text.utf8))
566
+ try? pipe.fileHandleForWriting.close()
567
+ }
568
+
569
+ private func finished(_ p: Process) {
570
+ guard speech === p else { return } // a newer utterance replaced this one
571
+ speech = nil
572
+ pet.talking = false
573
+ scheduleHide(after: 2)
574
+ }
575
+
576
+ private func scheduleHide(after seconds: Double) {
577
+ let work = DispatchWorkItem { [weak self] in self?.bubble.text = "" }
578
+ hideBubble = work
579
+ DispatchQueue.main.asyncAfter(deadline: .now() + seconds, execute: work)
580
+ }
581
+
582
+ /// One move by name (event CLI), optionally saying `text`
583
+ private func play(_ event: String, _ text: String) {
584
+ switch event {
585
+ case "attend": pet.attend()
586
+ case "work": pet.work()
587
+ case "emit": pet.emit()
588
+ case "complete": pet.complete()
589
+ case "chat": chat.toggle(above: window); return // same as clicking the pet
590
+ default: return
591
+ }
592
+ if !text.isEmpty { say(text) }
593
+ }
594
+
595
+ /// hivey changed: act it out with attend / work / emit / complete; one line per look
596
+ private func react(_ snapshot: HiveySnapshot, _ events: [HiveyEvent]) {
597
+ guard watching else { return }
598
+ if watcher.hiveyPath == nil {
599
+ hiveyItem.title = "hivey: not installed"
600
+ } else if snapshot.swarms.isEmpty {
601
+ hiveyItem.title = "hivey: nothing running"
602
+ } else {
603
+ hiveyItem.title = "hivey: \(snapshot.status.count) agents, \(snapshot.working) working"
604
+ }
605
+ if snapshot.working > 0 { pet.keepWorking() }
606
+
607
+ var line: (text: String, aloud: Bool, rank: Int)?
608
+ func offer(_ text: String, aloud: Bool, rank: Int) {
609
+ if line == nil || rank > line!.rank { line = (text, aloud, rank) }
610
+ }
611
+ var pulses = 0
612
+ for event in events {
613
+ switch event {
614
+ case .launched(let slug):
615
+ pet.work()
616
+ offer("\(slug) is starting.", aloud: true, rank: 3)
617
+ case .needsYou(let names):
618
+ pet.attend(for: 4)
619
+ offer(needsYouLine(names), aloud: true, rank: 4)
620
+ case .finished(let name):
621
+ pet.complete()
622
+ offer("\(name) finished.", aloud: false, rank: 1)
623
+ case .message(let fromHuman, let replyFrom, let text):
624
+ if fromHuman { pet.attend() }
625
+ if let replyFrom { offer(replyLine(from: replyFrom, text: text), aloud: true, rank: 2) }
626
+ if pulses < 3 {
627
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.3 * Double(pulses)) { [weak self] in
628
+ self?.pet.pulse()
629
+ }
630
+ pulses += 1
631
+ }
632
+ }
633
+ }
634
+ if let line { say(line.text, aloud: line.aloud) }
635
+ }
636
+
637
+ private func needsYouLine(_ names: [String]) -> String {
638
+ switch names.count {
639
+ case 1: return "\(names[0]) needs you."
640
+ case 2: return "\(names[0]) and \(names[1]) need you."
641
+ default: return "\(names.count) agents need you."
642
+ }
643
+ }
644
+
645
+ @objc func toggleVoice() {
646
+ speakAloud.toggle()
647
+ voiceItem.state = speakAloud ? .on : .off
648
+ }
649
+
650
+ @objc func toggleWatch() {
651
+ watching.toggle()
652
+ watchItem.state = watching ? .on : .off
653
+ if watching {
654
+ hiveyItem.title = "hivey: looking…"
655
+ watcher.start()
656
+ } else {
657
+ watcher.stop()
658
+ hiveyItem.title = "hivey: not watching"
659
+ }
660
+ }
661
+
662
+ /// Without hivey: remove any old login item and quit
663
+ private func turnOff() {
664
+ setLogin(false)
665
+ NSApp.terminate(nil)
666
+ }
667
+
668
+ @objc func instruct() { pet.attend() }
669
+ @objc func startWork() { pet.work() }
670
+ @objc func dispatch() { pet.emit() }
671
+ @objc func taskDone() { pet.complete() }
672
+
673
+ private func setLogin(_ on: Bool) {
674
+ guard on else { try? FileManager.default.removeItem(at: launchAgent); return }
675
+ let plist: [String: Any] = [
676
+ "Label": "com.hivey.prompt",
677
+ "ProgramArguments": ["/usr/bin/open", "-a", Bundle.main.bundlePath],
678
+ "RunAtLoad": true,
679
+ ]
680
+ try? FileManager.default.createDirectory(at: launchAgent.deletingLastPathComponent(), withIntermediateDirectories: true)
681
+ if let data = try? PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0) {
682
+ try? data.write(to: launchAgent)
683
+ }
684
+ }
685
+ }
686
+
687
+ let app = NSApplication.shared
688
+ app.setActivationPolicy(.accessory)
689
+ let delegate = AppDelegate()
690
+ app.delegate = delegate
691
+ app.run()