@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.
- package/README.md +235 -2
- package/bin/hivey.js +98 -0
- package/native/darwin-arm64/hivey +0 -0
- package/package.json +36 -4
- package/platforms.json +3 -0
- package/repo/LICENSE +201 -0
- package/repo/NOTICE +18 -0
- package/repo/README.md +148 -0
- package/repo/install.sh +365 -0
- package/repo/pets/README.md +25 -0
- package/repo/pets/hivey-dot/CLAUDE.md +55 -0
- package/repo/pets/hivey-dot/build.sh +34 -0
- package/repo/pets/hivey-dot/main.swift +750 -0
- package/repo/pets/hivey-h/CLAUDE.md +75 -0
- package/repo/pets/hivey-h/build.sh +34 -0
- package/repo/pets/hivey-h/main.swift +739 -0
- package/repo/pets/hivey-prompt/CLAUDE.md +59 -0
- package/repo/pets/hivey-prompt/build.sh +34 -0
- package/repo/pets/hivey-prompt/main.swift +691 -0
- package/repo/pets/pet.py +288 -0
- package/repo/pets/shared/HiveyWatch.swift +437 -0
- package/repo/pets/test_pet.py +122 -0
- package/repo/plugins/README.md +108 -0
- package/repo/plugins/agent-creator/herdr-plugin.toml +17 -0
- package/repo/plugins/dashboard/dashboard.py +507 -0
- package/repo/plugins/dashboard/herdr-plugin.toml +14 -0
- package/repo/plugins/dashboard/test_dashboard.py +134 -0
- package/repo/plugins/github/github.py +261 -0
- package/repo/plugins/github/herdr-plugin.toml +14 -0
- package/repo/plugins/github/test_github.py +49 -0
- package/repo/plugins/skills/herdr-plugin.toml +11 -0
- package/repo/plugins/skills/skills.md +41 -0
- package/repo/plugins/slack-relay/connect.py +198 -0
- package/repo/plugins/slack-relay/create_channel.py +134 -0
- package/repo/plugins/slack-relay/herdr-plugin.toml +15 -0
- package/repo/plugins/slack-relay/relay.py +401 -0
- package/repo/plugins/slack-relay/slack-app-manifest.json +30 -0
- package/repo/plugins/slack-relay/test_connect.py +66 -0
- package/repo/plugins/slack-relay/test_create_channel.py +61 -0
- package/repo/plugins/slack-relay/test_relay.py +254 -0
- package/repo/plugins/swarm-creator/herdr-plugin.toml +17 -0
- package/repo/plugins/swarm-watcher/dashboard.py +333 -0
- package/repo/plugins/swarm-watcher/herdr-plugin.toml +16 -0
- package/repo/plugins/swarm-watcher/usage.py +129 -0
- package/repo/plugins/swarm-watcher/watcher.py +191 -0
- package/repo/plugins/team-template/herdr-plugin.toml +15 -0
- package/repo/plugins/team-template/setup.py +211 -0
- package/repo/plugins/team-template/test_setup.py +73 -0
- package/repo/plugins/tts/herdr-plugin.toml +15 -0
- package/repo/skills/herdr/SKILL.md +214 -0
- package/repo/skills/hivey/SKILL.md +322 -0
- package/repo/skills/hivey/scripts/handoff.py +27 -0
- package/repo/skills/hivey/scripts/new_wiki.py +143 -0
- package/repo/skills/hivey-agent-creator/SKILL.md +303 -0
- package/repo/skills/hivey-agent-creator/references/agent-claude-md.md +256 -0
- package/repo/skills/hivey-agent-creator/references/global-guidelines.md +67 -0
- package/repo/skills/hivey-agent-creator/references/reviewer-agent.md +35 -0
- package/repo/skills/hivey-agent-creator/scripts/agent_context.py +76 -0
- package/repo/skills/hivey-agent-creator/scripts/agent_gate.py +78 -0
- package/repo/skills/hivey-agent-creator/scripts/setup_agent.py +288 -0
- package/repo/skills/hivey-agy/SKILL.md +123 -0
- package/repo/skills/hivey-agy/references/gemini-prompting.md +134 -0
- package/repo/skills/hivey-agy/scripts/agy_run.py +146 -0
- package/repo/skills/hivey-codex/SKILL.md +66 -0
- package/repo/skills/hivey-context7/SKILL.md +56 -0
- package/repo/skills/hivey-create-wiki/SKILL.md +329 -0
- package/repo/skills/hivey-grok/SKILL.md +101 -0
- package/repo/skills/hivey-grok/scripts/grok_run.py +139 -0
- package/repo/skills/hivey-swarm-creator/SKILL.md +384 -0
- package/repo/skills/hivey-swarm-creator/references/agent-claude-md.md +313 -0
- package/repo/skills/hivey-swarm-creator/references/archify-report.md +24 -0
- package/repo/skills/hivey-swarm-creator/references/dashboard.md +46 -0
- package/repo/skills/hivey-swarm-creator/references/retrospective.md +23 -0
- package/repo/skills/hivey-swarm-creator/references/shared-rules.md +39 -0
- package/repo/skills/hivey-swarm-creator/references/wiki-memory.md +30 -0
- package/repo/skills/hivey-swarm-creator/scripts/setup_agent.py +76 -0
- package/repo/skills/hivey-swarm-creator/scripts/shared_rules.py +50 -0
- package/repo/skills/hivey-swarm-creator/scripts/swarm_tasks.py +171 -0
- package/repo/skills/hivey-wiki-ingest/SKILL.md +45 -0
- package/repo/skills/hivey-wiki-lint/SKILL.md +38 -0
- package/repo/skills/hivey-wiki-list/SKILL.md +46 -0
- package/repo/skills/hivey-wiki-log/SKILL.md +45 -0
- package/repo/skills/hivey-wiki-query/SKILL.md +44 -0
|
@@ -0,0 +1,750 @@
|
|
|
1
|
+
import AppKit
|
|
2
|
+
|
|
3
|
+
// Hivey — a dark graphite dot with mint agents riding a signal wave around it. Watches hivey:
|
|
4
|
+
// its dots are hivey's agents, and swarms, messages and finished work drive its moves.
|
|
5
|
+
// CLI: hivey → launch the pet
|
|
6
|
+
// hivey say "hi" → make the running pet say something
|
|
7
|
+
// hivey agent|leave|swarm|pulse|done ["text"] → play that move (and say the text)
|
|
8
|
+
|
|
9
|
+
let sayNote = Notification.Name("com.hivey.pet.say")
|
|
10
|
+
let eventNote = Notification.Name("com.hivey.pet.event")
|
|
11
|
+
let petEvents = ["agent", "leave", "swarm", "pulse", "done", "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 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
|
+
enum Palette {
|
|
44
|
+
static let bodyLight = NSColor(hex: 0x56636a)
|
|
45
|
+
static let body = NSColor(hex: 0x1f2529)
|
|
46
|
+
static let bodyDark = NSColor(hex: 0x08090b)
|
|
47
|
+
static let eye = NSColor(hex: 0xd6fff0)
|
|
48
|
+
static let mint = NSColor(hex: 0x4fd1a5)
|
|
49
|
+
static let mintGlow = NSColor(hex: 0x9cf5d4)
|
|
50
|
+
static let ink = NSColor(hex: 0x101417)
|
|
51
|
+
static let text = NSColor(hex: 0xe6f4ee)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
func lerp(_ a: CGFloat, _ b: CGFloat, _ k: CGFloat) -> CGFloat { a + (b - a) * k }
|
|
55
|
+
func lerp(_ a: NSPoint, _ b: NSPoint, _ k: CGFloat) -> NSPoint { NSPoint(x: lerp(a.x, b.x, k), y: lerp(a.y, b.y, k)) }
|
|
56
|
+
func easeInOut(_ k: CGFloat) -> CGFloat { k * k * (3 - 2 * k) }
|
|
57
|
+
func easeOutBack(_ k: CGFloat) -> CGFloat {
|
|
58
|
+
let c = 1.9, x = Double(k) - 1
|
|
59
|
+
return CGFloat(1 + (c + 1) * x * x * x + c * x * x)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// MARK: - Pet
|
|
63
|
+
|
|
64
|
+
final class PetView: NSView {
|
|
65
|
+
struct Agent { var phase: CGFloat; let born: Double; var leaving = -1.0 }
|
|
66
|
+
struct Pulse { let from: Int, to: Int; let start: Double }
|
|
67
|
+
|
|
68
|
+
// Geometry (non-flipped view coordinates)
|
|
69
|
+
let center = NSPoint(x: 120, y: 82)
|
|
70
|
+
let radius: CGFloat = 28
|
|
71
|
+
let orbit = (rx: CGFloat(74), ry: CGFloat(22), tilt: CGFloat(-0.12))
|
|
72
|
+
|
|
73
|
+
var talking = false
|
|
74
|
+
var onPoke: (() -> Void)?
|
|
75
|
+
/// hivey agents are working: messages flow between the dots more often
|
|
76
|
+
var busy = false
|
|
77
|
+
|
|
78
|
+
private var t = 0.0
|
|
79
|
+
private var spin: CGFloat = 0
|
|
80
|
+
private var agents: [Agent] = []
|
|
81
|
+
private var pulses: [Pulse] = []
|
|
82
|
+
private var nextPulse = 2.0
|
|
83
|
+
private var formation: CGFloat = 0
|
|
84
|
+
private var formationUntil = -1.0
|
|
85
|
+
private var bounceStart = -10.0
|
|
86
|
+
private var glowUntil = -1.0
|
|
87
|
+
private var nextBlink = 2.5
|
|
88
|
+
private var blinkUntil = 0.0
|
|
89
|
+
private var mouthOpen = false
|
|
90
|
+
private var nextMouth = 0.0
|
|
91
|
+
private var look = NSPoint.zero
|
|
92
|
+
|
|
93
|
+
private var dragStart: NSPoint?
|
|
94
|
+
private var windowStart = NSPoint.zero
|
|
95
|
+
private var moved = false
|
|
96
|
+
|
|
97
|
+
override init(frame: NSRect) {
|
|
98
|
+
super.init(frame: frame)
|
|
99
|
+
for _ in 0..<3 { addAgent(animated: false) }
|
|
100
|
+
let timer = Timer(timeInterval: 1.0 / 60, repeats: true) { [weak self] _ in
|
|
101
|
+
MainActor.assumeIsolated { self?.tick() }
|
|
102
|
+
}
|
|
103
|
+
RunLoop.main.add(timer, forMode: .common)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
required init?(coder: NSCoder) { fatalError() }
|
|
107
|
+
|
|
108
|
+
// MARK: Behaviors
|
|
109
|
+
|
|
110
|
+
func addAgent(animated: Bool = true) {
|
|
111
|
+
guard agents.filter({ $0.leaving < 0 }).count < 9 else { return }
|
|
112
|
+
// Arrive at the gap after the last dot; phases then spread out evenly
|
|
113
|
+
let phase = agents.last.map { $0.phase + .pi / CGFloat(agents.count) } ?? 0
|
|
114
|
+
agents.append(Agent(phase: phase, born: animated ? t : -10))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// An agent left: its dot fades off the wave
|
|
118
|
+
func removeAgent() {
|
|
119
|
+
guard let i = agents.lastIndex(where: { $0.leaving < 0 }) else { return }
|
|
120
|
+
agents[i].leaving = t
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/// Dots on the wave = hivey's agents (max 9); new ones fly out, gone ones fade off
|
|
124
|
+
func showAgents(_ count: Int) {
|
|
125
|
+
let goal = min(count, 9)
|
|
126
|
+
var present = agents.filter { $0.leaving < 0 }.count
|
|
127
|
+
while present < goal { addAgent(); present += 1 }
|
|
128
|
+
while present > goal { removeAgent(); present -= 1 }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/// One message: a pulse of light between two dots
|
|
132
|
+
func pulse() {
|
|
133
|
+
let live = agents.indices.filter { agents[$0].leaving < 0 }
|
|
134
|
+
guard live.count >= 2 else { return }
|
|
135
|
+
let pair = live.shuffled().prefix(2)
|
|
136
|
+
pulses.append(Pulse(from: pair.first!, to: pair.last!, start: t))
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
func formSwarm(for seconds: Double = 5) {
|
|
140
|
+
formationUntil = t + seconds
|
|
141
|
+
nextPulse = t + 0.6
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
func celebrate() {
|
|
145
|
+
bounceStart = t
|
|
146
|
+
glowUntil = t + 1.4
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private var happy: Bool { t < glowUntil }
|
|
150
|
+
|
|
151
|
+
// MARK: Animation
|
|
152
|
+
|
|
153
|
+
private func tick() {
|
|
154
|
+
guard window?.isVisible == true else { return } // hidden: skip animation work
|
|
155
|
+
let dt = 1.0 / 60
|
|
156
|
+
t += dt
|
|
157
|
+
let before = agents.count
|
|
158
|
+
agents.removeAll { $0.leaving >= 0 && t - $0.leaving > 0.8 } // faded off the wave
|
|
159
|
+
if agents.count != before { pulses.removeAll() } // their dot indices moved
|
|
160
|
+
|
|
161
|
+
let target: CGFloat = t < formationUntil ? 1 : 0
|
|
162
|
+
formation += (target - formation) * CGFloat(min(1, dt * 2.5))
|
|
163
|
+
spin += CGFloat(dt) * 0.55 * (1 - formation)
|
|
164
|
+
|
|
165
|
+
// Spread the agents evenly around the orbit
|
|
166
|
+
let n = CGFloat(agents.count)
|
|
167
|
+
for i in agents.indices {
|
|
168
|
+
let goal = 2 * .pi * CGFloat(i) / n
|
|
169
|
+
var d = (goal - agents[i].phase).truncatingRemainder(dividingBy: 2 * .pi)
|
|
170
|
+
if d > .pi { d -= 2 * .pi } else if d < -.pi { d += 2 * .pi }
|
|
171
|
+
agents[i].phase += d * CGFloat(dt) * 2.5
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Messages between agents: frequent in formation, occasional while idle
|
|
175
|
+
pulses.removeAll { t - $0.start > 0.7 }
|
|
176
|
+
if t >= nextPulse && agents.count >= 2 {
|
|
177
|
+
let a = Int.random(in: 0..<agents.count)
|
|
178
|
+
var b = Int.random(in: 0..<agents.count - 1)
|
|
179
|
+
if b >= a { b += 1 }
|
|
180
|
+
pulses.append(Pulse(from: a, to: b, start: t))
|
|
181
|
+
nextPulse = t + (formation > 0.5 ? Double.random(in: 0.25...0.6)
|
|
182
|
+
: busy ? Double.random(in: 0.8...1.6) : Double.random(in: 3...7))
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if t >= nextBlink {
|
|
186
|
+
blinkUntil = t + 0.13
|
|
187
|
+
nextBlink = t + Double.random(in: 2.5...5.5)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if talking {
|
|
191
|
+
if t >= nextMouth {
|
|
192
|
+
mouthOpen.toggle()
|
|
193
|
+
nextMouth = t + Double.random(in: 0.08...0.18)
|
|
194
|
+
}
|
|
195
|
+
} else {
|
|
196
|
+
mouthOpen = false
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Curious eyes follow the cursor
|
|
200
|
+
if let window {
|
|
201
|
+
let m = NSEvent.mouseLocation
|
|
202
|
+
let c = window.convertPoint(toScreen: convert(center, to: nil))
|
|
203
|
+
let dx = m.x - c.x, dy = m.y - c.y
|
|
204
|
+
let dist = max(1, hypot(dx, dy))
|
|
205
|
+
let reach = min(1, dist / 260) * 2.6
|
|
206
|
+
let goal = NSPoint(x: dx / dist * reach, y: dy / dist * reach)
|
|
207
|
+
look = lerp(look, goal, 0.12)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
needsDisplay = true
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private var bounceHeight: CGFloat {
|
|
214
|
+
let k = CGFloat((t - bounceStart) / 0.9)
|
|
215
|
+
guard k >= 0 && k < 1 else { return 0 }
|
|
216
|
+
return 6 * sin(k * .pi)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/// Point on the signal track: a tilted ellipse whose radius carries a travelling sine wave
|
|
220
|
+
private func signalPoint(_ theta: CGFloat) -> NSPoint {
|
|
221
|
+
let wave = 5 * sin(theta * 7 - CGFloat(t) * 2.6) // pushes outward from the loop
|
|
222
|
+
let ox = (orbit.rx + wave) * cos(theta), oy = (orbit.ry + wave * 0.55) * sin(theta)
|
|
223
|
+
return NSPoint(x: center.x + ox * cos(orbit.tilt) - oy * sin(orbit.tilt),
|
|
224
|
+
y: center.y + ox * sin(orbit.tilt) + oy * cos(orbit.tilt))
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/// Screen position of agent i plus its depth (+1 = far side of the track, -1 = near side).
|
|
228
|
+
private func agentPosition(_ i: Int) -> (point: NSPoint, depth: CGFloat) {
|
|
229
|
+
let a = agents[i]
|
|
230
|
+
let theta = spin + a.phase
|
|
231
|
+
let orbitPoint = signalPoint(theta)
|
|
232
|
+
|
|
233
|
+
// Formation: a gentle arc above Hivey, bobbing in sync
|
|
234
|
+
let n = CGFloat(max(agents.count - 1, 1))
|
|
235
|
+
let arc = agents.count == 1 ? CGFloat.pi / 2 : CGFloat.pi * (0.88 - 0.76 * CGFloat(i) / n)
|
|
236
|
+
let formPoint = NSPoint(x: center.x + 66 * cos(arc),
|
|
237
|
+
y: center.y + 10 + 46 * sin(arc) + 2.5 * CGFloat(sin(t * 4 + Double(i) * 0.7)))
|
|
238
|
+
|
|
239
|
+
let f = easeInOut(formation)
|
|
240
|
+
var p = lerp(orbitPoint, formPoint, f)
|
|
241
|
+
let born = CGFloat((t - a.born) / 0.55)
|
|
242
|
+
if born < 1 { p = lerp(center, p, easeInOut(max(0, born))) } // newborns fly out of Hivey
|
|
243
|
+
return (p, lerp(sin(theta), -0.5, f))
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// MARK: Drawing
|
|
247
|
+
|
|
248
|
+
override func draw(_ dirtyRect: NSRect) {
|
|
249
|
+
let ctx = NSGraphicsContext.current!.cgContext
|
|
250
|
+
let hop = bounceHeight
|
|
251
|
+
let bodyCenter = NSPoint(x: center.x, y: center.y + hop + 1.5 * CGFloat(sin(t * 1.6)))
|
|
252
|
+
|
|
253
|
+
// Soft floor shadow, smaller while in the air
|
|
254
|
+
let sw = 46 - hop * 1.2
|
|
255
|
+
NSColor(white: 0, alpha: 0.24 - hop * 0.012).setFill()
|
|
256
|
+
NSBezierPath(ovalIn: NSRect(x: center.x - sw / 2, y: center.y - radius - 14, width: sw, height: 7)).fill()
|
|
257
|
+
|
|
258
|
+
let positions = agents.indices.map { agentPosition($0) }
|
|
259
|
+
drawOrbitPath(back: true)
|
|
260
|
+
for i in agents.indices where positions[i].depth > 0 { drawAgent(i, positions[i]) }
|
|
261
|
+
drawBody(at: bodyCenter, hop: hop, ctx: ctx)
|
|
262
|
+
drawOrbitPath(back: false)
|
|
263
|
+
for i in agents.indices where positions[i].depth <= 0 { drawAgent(i, positions[i]) }
|
|
264
|
+
for p in pulses where p.from < agents.count && p.to < agents.count {
|
|
265
|
+
drawPulse(p, from: positions[p.from].point, to: positions[p.to].point, ctx: ctx)
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
private func drawOrbitPath(back: Bool) {
|
|
270
|
+
let alpha = 0.5 * (1 - formation)
|
|
271
|
+
guard alpha > 0.01 else { return }
|
|
272
|
+
let start: CGFloat = back ? 0 : .pi
|
|
273
|
+
func trace(_ from: CGFloat, _ to: CGFloat) -> NSBezierPath {
|
|
274
|
+
let path = NSBezierPath()
|
|
275
|
+
let steps = max(2, Int((to - from) * 40))
|
|
276
|
+
for s in 0...steps {
|
|
277
|
+
let p = signalPoint(from + (to - from) * CGFloat(s) / CGFloat(steps))
|
|
278
|
+
s == 0 ? path.move(to: p) : path.line(to: p)
|
|
279
|
+
}
|
|
280
|
+
path.lineCapStyle = .round
|
|
281
|
+
path.lineJoinStyle = .round
|
|
282
|
+
return path
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
NSGraphicsContext.saveGraphicsState()
|
|
286
|
+
let glow = NSShadow()
|
|
287
|
+
glow.shadowColor = Palette.mint.withAlphaComponent(0.8)
|
|
288
|
+
glow.shadowBlurRadius = 5
|
|
289
|
+
glow.set()
|
|
290
|
+
let line = trace(start, start + .pi)
|
|
291
|
+
line.lineWidth = 1.1
|
|
292
|
+
Palette.mint.withAlphaComponent(alpha * (back ? 0.55 : 1)).setStroke()
|
|
293
|
+
line.stroke()
|
|
294
|
+
|
|
295
|
+
// A brighter signal packet travelling along the wave
|
|
296
|
+
let head = CGFloat(t * 1.3).truncatingRemainder(dividingBy: 2 * .pi)
|
|
297
|
+
let from = max(start, head - 0.55), to = min(start + .pi, head)
|
|
298
|
+
if to > from {
|
|
299
|
+
let packet = trace(from, to)
|
|
300
|
+
packet.lineWidth = 1.8
|
|
301
|
+
glow.shadowColor = Palette.mintGlow
|
|
302
|
+
glow.shadowBlurRadius = 8
|
|
303
|
+
glow.set()
|
|
304
|
+
Palette.mintGlow.withAlphaComponent(min(1, alpha * 1.8) * (back ? 0.6 : 1)).setStroke()
|
|
305
|
+
packet.stroke()
|
|
306
|
+
}
|
|
307
|
+
NSGraphicsContext.restoreGraphicsState()
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
private func drawAgent(_ i: Int, _ pos: (point: NSPoint, depth: CGFloat)) {
|
|
311
|
+
let born = CGFloat((t - agents[i].born) / 0.55)
|
|
312
|
+
let leave = agents[i].leaving < 0 ? 0 : CGFloat((t - agents[i].leaving) / 0.8)
|
|
313
|
+
let scale = (born < 1 ? max(0, easeOutBack(born)) : 1) * max(0, 1 - leave)
|
|
314
|
+
let r = 4.8 * (1 - 0.22 * pos.depth) * scale
|
|
315
|
+
guard r > 0.2 else { return }
|
|
316
|
+
|
|
317
|
+
let glowing = happy || born < 1
|
|
318
|
+
let shine = glowing ? 0.5 + 0.5 * sin(t * 10) : 0
|
|
319
|
+
let fill = Palette.mint.blended(withFraction: 0.25 + 0.5 * shine, of: Palette.mintGlow)!
|
|
320
|
+
.withAlphaComponent(0.8 - 0.15 * pos.depth)
|
|
321
|
+
|
|
322
|
+
NSGraphicsContext.saveGraphicsState()
|
|
323
|
+
let glow = NSShadow()
|
|
324
|
+
glow.shadowColor = (glowing ? Palette.mintGlow : Palette.mint).withAlphaComponent(glowing ? 1 : 0.8)
|
|
325
|
+
glow.shadowBlurRadius = glowing ? 12 : 6
|
|
326
|
+
glow.set()
|
|
327
|
+
fill.setFill()
|
|
328
|
+
NSBezierPath(ovalIn: NSRect(x: pos.point.x - r, y: pos.point.y - r, width: r * 2, height: r * 2)).fill()
|
|
329
|
+
NSGraphicsContext.restoreGraphicsState()
|
|
330
|
+
|
|
331
|
+
// Tiny highlight
|
|
332
|
+
NSColor.white.withAlphaComponent(0.45).setFill()
|
|
333
|
+
let h = r * 0.38
|
|
334
|
+
NSBezierPath(ovalIn: NSRect(x: pos.point.x - r * 0.45, y: pos.point.y + r * 0.15, width: h, height: h)).fill()
|
|
335
|
+
|
|
336
|
+
// Birth ring
|
|
337
|
+
if born < 1 {
|
|
338
|
+
let ring = r + 14 * born
|
|
339
|
+
Palette.mintGlow.withAlphaComponent(0.8 * (1 - born)).setStroke()
|
|
340
|
+
let p = NSBezierPath(ovalIn: NSRect(x: pos.point.x - ring, y: pos.point.y - ring, width: ring * 2, height: ring * 2))
|
|
341
|
+
p.lineWidth = 1.2
|
|
342
|
+
p.stroke()
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
private func drawPulse(_ p: Pulse, from a: NSPoint, to b: NSPoint, ctx: CGContext) {
|
|
347
|
+
let k = CGFloat((t - p.start) / 0.7)
|
|
348
|
+
let e = easeInOut(k)
|
|
349
|
+
for trail in 0..<4 {
|
|
350
|
+
let kk = max(0, e - CGFloat(trail) * 0.06)
|
|
351
|
+
let pt = NSPoint(x: lerp(a.x, b.x, kk), y: lerp(a.y, b.y, kk) + 10 * sin(kk * .pi)) // slight arc
|
|
352
|
+
let r = 2.2 - CGFloat(trail) * 0.45
|
|
353
|
+
NSGraphicsContext.saveGraphicsState()
|
|
354
|
+
let glow = NSShadow()
|
|
355
|
+
glow.shadowColor = Palette.mintGlow
|
|
356
|
+
glow.shadowBlurRadius = 8
|
|
357
|
+
glow.set()
|
|
358
|
+
NSColor.white.withAlphaComponent((1 - CGFloat(trail) * 0.22) * sin(k * .pi)).setFill()
|
|
359
|
+
NSBezierPath(ovalIn: NSRect(x: pt.x - r, y: pt.y - r, width: r * 2, height: r * 2)).fill()
|
|
360
|
+
NSGraphicsContext.restoreGraphicsState()
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private func drawBody(at c: NSPoint, hop: CGFloat, ctx: CGContext) {
|
|
365
|
+
// Breathing plus a little stretch while airborne
|
|
366
|
+
let breath = 1 + 0.015 * CGFloat(sin(t * 1.6))
|
|
367
|
+
let rx = radius * breath * (1 - hop * 0.003)
|
|
368
|
+
let ry = radius * breath * (1 + hop * 0.005)
|
|
369
|
+
let rect = NSRect(x: c.x - rx, y: c.y - ry, width: rx * 2, height: ry * 2)
|
|
370
|
+
|
|
371
|
+
NSGraphicsContext.saveGraphicsState()
|
|
372
|
+
let glow = NSShadow()
|
|
373
|
+
glow.shadowColor = Palette.mint.withAlphaComponent(happy ? 0.95 : 0.35)
|
|
374
|
+
glow.shadowBlurRadius = happy ? 18 : 9
|
|
375
|
+
glow.set()
|
|
376
|
+
Palette.body.setFill()
|
|
377
|
+
NSBezierPath(ovalIn: rect).fill()
|
|
378
|
+
NSGraphicsContext.restoreGraphicsState()
|
|
379
|
+
|
|
380
|
+
// Satin shading: key light upper-left, mint bounce light on the underside, soft sheen
|
|
381
|
+
let space = CGColorSpace(name: CGColorSpace.sRGB)!
|
|
382
|
+
func grad(_ cs: [NSColor], _ locs: [CGFloat]) -> CGGradient {
|
|
383
|
+
CGGradient(colorsSpace: space, colors: cs.map(\.cgColor) as CFArray, locations: locs)!
|
|
384
|
+
}
|
|
385
|
+
ctx.saveGState()
|
|
386
|
+
ctx.addEllipse(in: rect)
|
|
387
|
+
ctx.clip()
|
|
388
|
+
let key = CGPoint(x: c.x - rx * 0.38, y: c.y + ry * 0.42)
|
|
389
|
+
ctx.drawRadialGradient(grad([Palette.bodyLight, Palette.body, Palette.bodyDark], [0, 0.5, 1]),
|
|
390
|
+
startCenter: key, startRadius: 0, endCenter: c, endRadius: rx * 1.1, options: [.drawsAfterEndLocation])
|
|
391
|
+
let bounce = CGPoint(x: c.x + rx * 0.2, y: c.y - ry * 1.15)
|
|
392
|
+
ctx.drawRadialGradient(grad([Palette.mint.withAlphaComponent(0.28), Palette.mint.withAlphaComponent(0)], [0, 1]),
|
|
393
|
+
startCenter: bounce, startRadius: 0, endCenter: bounce, endRadius: rx * 0.8, options: [])
|
|
394
|
+
ctx.drawRadialGradient(grad([NSColor.white.withAlphaComponent(0.22), NSColor.white.withAlphaComponent(0)], [0, 1]),
|
|
395
|
+
startCenter: key, startRadius: 0, endCenter: key, endRadius: rx * 0.5, options: [])
|
|
396
|
+
ctx.restoreGState()
|
|
397
|
+
|
|
398
|
+
// Eyes
|
|
399
|
+
let eyeY = c.y + 3 + look.y
|
|
400
|
+
for side: CGFloat in [-1, 1] {
|
|
401
|
+
let ex = c.x + side * 9.5 + look.x
|
|
402
|
+
NSGraphicsContext.saveGraphicsState()
|
|
403
|
+
let eyeGlow = NSShadow()
|
|
404
|
+
eyeGlow.shadowColor = Palette.mint.withAlphaComponent(0.8)
|
|
405
|
+
eyeGlow.shadowBlurRadius = 5
|
|
406
|
+
eyeGlow.set()
|
|
407
|
+
Palette.eye.setFill()
|
|
408
|
+
if t < blinkUntil {
|
|
409
|
+
NSBezierPath(roundedRect: NSRect(x: ex - 3.4, y: eyeY - 0.8, width: 6.8, height: 1.6),
|
|
410
|
+
xRadius: 0.8, yRadius: 0.8).fill()
|
|
411
|
+
} else {
|
|
412
|
+
NSBezierPath(roundedRect: NSRect(x: ex - 3, y: eyeY - 4.4, width: 6, height: 8.8), xRadius: 3, yRadius: 3).fill()
|
|
413
|
+
}
|
|
414
|
+
NSGraphicsContext.restoreGraphicsState()
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// Mouth only while speaking
|
|
418
|
+
guard talking else { return }
|
|
419
|
+
let mx = c.x + look.x * 0.6, my = eyeY - 9
|
|
420
|
+
Palette.eye.setFill()
|
|
421
|
+
let mh: CGFloat = mouthOpen ? 3.4 : 1.4
|
|
422
|
+
NSBezierPath(roundedRect: NSRect(x: mx - 2.4, y: my - mh / 2, width: 4.8, height: mh),
|
|
423
|
+
xRadius: mh / 2, yRadius: mh / 2).fill()
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// MARK: Mouse: drag to move, click to poke
|
|
427
|
+
|
|
428
|
+
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
|
429
|
+
|
|
430
|
+
override func mouseDown(with event: NSEvent) {
|
|
431
|
+
dragStart = NSEvent.mouseLocation
|
|
432
|
+
windowStart = window?.frame.origin ?? .zero
|
|
433
|
+
moved = false
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
override func mouseDragged(with event: NSEvent) {
|
|
437
|
+
guard let start = dragStart, let window else { return }
|
|
438
|
+
let p = NSEvent.mouseLocation
|
|
439
|
+
if abs(p.x - start.x) + abs(p.y - start.y) > 3 { moved = true }
|
|
440
|
+
window.setFrameOrigin(NSPoint(x: windowStart.x + p.x - start.x, y: windowStart.y + p.y - start.y))
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
override func mouseUp(with event: NSEvent) {
|
|
444
|
+
if !moved { onPoke?() }
|
|
445
|
+
dragStart = nil
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// MARK: - Speech bubble
|
|
450
|
+
|
|
451
|
+
final class BubbleView: NSView {
|
|
452
|
+
var text = "" { didSet { needsDisplay = true } }
|
|
453
|
+
override var isFlipped: Bool { true }
|
|
454
|
+
|
|
455
|
+
private let attrs: [NSAttributedString.Key: Any] = [
|
|
456
|
+
.font: NSFont.monospacedSystemFont(ofSize: 12, weight: .medium),
|
|
457
|
+
.foregroundColor: Palette.text,
|
|
458
|
+
]
|
|
459
|
+
|
|
460
|
+
override func draw(_ dirtyRect: NSRect) {
|
|
461
|
+
guard !text.isEmpty else { return }
|
|
462
|
+
let maxW = bounds.width - 40
|
|
463
|
+
let str = NSAttributedString(string: text, attributes: attrs)
|
|
464
|
+
let tb = str.boundingRect(with: NSSize(width: maxW, height: 1000), options: .usesLineFragmentOrigin)
|
|
465
|
+
let textH = min(ceil(tb.height), bounds.height - 30)
|
|
466
|
+
let w = ceil(tb.width) + 28, h = textH + 16
|
|
467
|
+
let box = NSRect(x: (bounds.width - w) / 2, y: bounds.height - 12 - h, width: w, height: h)
|
|
468
|
+
|
|
469
|
+
let shape = NSBezierPath(roundedRect: box, xRadius: 7, yRadius: 7)
|
|
470
|
+
let tail = NSBezierPath()
|
|
471
|
+
tail.move(to: NSPoint(x: bounds.midX - 7, y: box.maxY - 1))
|
|
472
|
+
tail.line(to: NSPoint(x: bounds.midX, y: bounds.height - 2))
|
|
473
|
+
tail.line(to: NSPoint(x: bounds.midX + 7, y: box.maxY - 1))
|
|
474
|
+
tail.close()
|
|
475
|
+
shape.append(tail)
|
|
476
|
+
|
|
477
|
+
NSGraphicsContext.saveGraphicsState()
|
|
478
|
+
let glow = NSShadow()
|
|
479
|
+
glow.shadowColor = Palette.mint.withAlphaComponent(0.5)
|
|
480
|
+
glow.shadowBlurRadius = 8
|
|
481
|
+
glow.set()
|
|
482
|
+
Palette.ink.setFill()
|
|
483
|
+
shape.fill()
|
|
484
|
+
NSGraphicsContext.restoreGraphicsState()
|
|
485
|
+
// Wide stroke then refill: only the outer half shows, so the box/tail seam disappears
|
|
486
|
+
Palette.mint.setStroke()
|
|
487
|
+
shape.lineWidth = 2.4
|
|
488
|
+
shape.stroke()
|
|
489
|
+
Palette.ink.setFill()
|
|
490
|
+
shape.fill()
|
|
491
|
+
|
|
492
|
+
str.draw(with: NSRect(x: box.minX + 14, y: box.minY + 8, width: maxW, height: textH),
|
|
493
|
+
options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine])
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// MARK: - App
|
|
498
|
+
|
|
499
|
+
final class AppDelegate: NSObject, NSApplicationDelegate {
|
|
500
|
+
var window: NSWindow!
|
|
501
|
+
let pet = PetView(frame: NSRect(x: 0, y: 0, width: 240, height: 170))
|
|
502
|
+
let bubble = BubbleView(frame: NSRect(x: 0, y: 150, width: 240, height: 90))
|
|
503
|
+
private var speech: Process?
|
|
504
|
+
private var speechInterruptible = true
|
|
505
|
+
private var hideBubble: DispatchWorkItem?
|
|
506
|
+
private var watchItem: NSMenuItem!
|
|
507
|
+
private var voiceItem: NSMenuItem!
|
|
508
|
+
private let hiveyItem = NSMenuItem(title: "hivey: looking…", action: nil, keyEquivalent: "")
|
|
509
|
+
private let watcher = HiveyWatcher()
|
|
510
|
+
private let chat = HiveyChat()
|
|
511
|
+
private lazy var switcher = PetSwitcher(current: "hivey-dot", say: { [weak self] in self?.say($0, aloud: false) },
|
|
512
|
+
turnOffHere: { [weak self] in self?.turnOff() })
|
|
513
|
+
private var watching: Bool {
|
|
514
|
+
get { UserDefaults.standard.object(forKey: "watchHivey") as? Bool ?? true }
|
|
515
|
+
set { UserDefaults.standard.set(newValue, forKey: "watchHivey") }
|
|
516
|
+
}
|
|
517
|
+
private var speakAloud: Bool {
|
|
518
|
+
get { UserDefaults.standard.object(forKey: "speakAloud") as? Bool ?? true }
|
|
519
|
+
set { UserDefaults.standard.set(newValue, forKey: "speakAloud") }
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
private let launchAgent = URL(fileURLWithPath: NSHomeDirectory() + "/Library/LaunchAgents/com.hivey.pet.plist")
|
|
523
|
+
|
|
524
|
+
func applicationDidFinishLaunching(_ notification: Notification) {
|
|
525
|
+
let size = NSSize(width: 240, height: 240)
|
|
526
|
+
window = NSWindow(contentRect: NSRect(origin: .zero, size: size), styleMask: .borderless, backing: .buffered, defer: false)
|
|
527
|
+
window.isOpaque = false
|
|
528
|
+
window.backgroundColor = .clear
|
|
529
|
+
window.hasShadow = false
|
|
530
|
+
window.level = .floating
|
|
531
|
+
window.collectionBehavior = [.canJoinAllSpaces, .stationary, .ignoresCycle]
|
|
532
|
+
|
|
533
|
+
let root = NSView(frame: NSRect(origin: .zero, size: size))
|
|
534
|
+
root.addSubview(pet)
|
|
535
|
+
root.addSubview(bubble)
|
|
536
|
+
window.contentView = root
|
|
537
|
+
|
|
538
|
+
if !window.setFrameUsingName("HiveyPet"), let screen = NSScreen.main {
|
|
539
|
+
let v = screen.visibleFrame
|
|
540
|
+
window.setFrameOrigin(NSPoint(x: v.maxX - size.width - 260, y: v.minY + 10))
|
|
541
|
+
}
|
|
542
|
+
window.setContentSize(size)
|
|
543
|
+
window.setFrameAutosaveName("HiveyPet")
|
|
544
|
+
|
|
545
|
+
let menu = NSMenu()
|
|
546
|
+
hiveyItem.isEnabled = false
|
|
547
|
+
menu.addItem(hiveyItem)
|
|
548
|
+
watchItem = menu.addItem(withTitle: "Watch hivey", action: #selector(toggleWatch), keyEquivalent: "")
|
|
549
|
+
watchItem.target = self
|
|
550
|
+
watchItem.state = watching ? .on : .off
|
|
551
|
+
voiceItem = menu.addItem(withTitle: "Speak aloud", action: #selector(toggleVoice), keyEquivalent: "")
|
|
552
|
+
voiceItem.target = self
|
|
553
|
+
voiceItem.state = speakAloud ? .on : .off
|
|
554
|
+
menu.addItem(.separator())
|
|
555
|
+
// Preview the pet's moves: they only animate the pet, nothing happens in hivey
|
|
556
|
+
let effects = NSMenu()
|
|
557
|
+
effects.addItem(withTitle: "New agent", action: #selector(newAgent), keyEquivalent: "").target = self
|
|
558
|
+
effects.addItem(withTitle: "Swarm formation", action: #selector(swarm), keyEquivalent: "").target = self
|
|
559
|
+
effects.addItem(withTitle: "Task finished", action: #selector(taskDone), keyEquivalent: "").target = self
|
|
560
|
+
let effectsItem = NSMenuItem(title: "Effects (preview only)", action: nil, keyEquivalent: "")
|
|
561
|
+
effectsItem.submenu = effects
|
|
562
|
+
menu.addItem(effectsItem)
|
|
563
|
+
menu.addItem(.separator())
|
|
564
|
+
switcher.addItems(to: menu)
|
|
565
|
+
menu.addItem(withTitle: "Quit Hivey", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "")
|
|
566
|
+
pet.menu = menu
|
|
567
|
+
// Click: talk to the hivey agent (a chat box above the pet); drag still moves it
|
|
568
|
+
pet.onPoke = { [weak self] in
|
|
569
|
+
guard let self else { return }
|
|
570
|
+
pet.celebrate()
|
|
571
|
+
chat.toggle(above: window)
|
|
572
|
+
}
|
|
573
|
+
chat.say = { [weak self] text, aloud in self?.say(text, aloud: aloud) }
|
|
574
|
+
chat.onSent = { [weak self] in self?.pet.pulse() }
|
|
575
|
+
|
|
576
|
+
// `hivey agent|leave|swarm|pulse|done ["text"]`
|
|
577
|
+
DistributedNotificationCenter.default().addObserver(forName: eventNote, object: nil, queue: .main) { [weak self] note in
|
|
578
|
+
guard let raw = note.object as? String else { return }
|
|
579
|
+
let parts = raw.split(separator: "\n", maxSplits: 1, omittingEmptySubsequences: false)
|
|
580
|
+
let text = parts.count > 1 ? String(parts[1]) : ""
|
|
581
|
+
MainActor.assumeIsolated { self?.play(String(parts[0]), text) }
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
watcher.onUpdate = { [weak self] snapshot, events in
|
|
585
|
+
MainActor.assumeIsolated { self?.react(snapshot, events) }
|
|
586
|
+
}
|
|
587
|
+
watcher.onLastWindowClosed = { NSApp.terminate(nil) } // back with the next hivey window
|
|
588
|
+
if watching { watcher.start() } else { hiveyItem.title = "hivey: not watching" }
|
|
589
|
+
|
|
590
|
+
DistributedNotificationCenter.default().addObserver(forName: sayNote, object: nil, queue: .main) { [weak self] note in
|
|
591
|
+
guard let text = note.object as? String, !text.isEmpty else { return }
|
|
592
|
+
MainActor.assumeIsolated { self?.say(text) }
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
// Appear: fade in, happy bounce, welcome
|
|
597
|
+
window.alphaValue = 0
|
|
598
|
+
window.orderFrontRegardless()
|
|
599
|
+
NSAnimationContext.runAnimationGroup { $0.duration = 0.7; window.animator().alphaValue = 1 }
|
|
600
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) { [weak self] in
|
|
601
|
+
self?.pet.celebrate()
|
|
602
|
+
self?.say("Welcome! I'm Hivey.")
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
func say(_ text: String, aloud: Bool = true) {
|
|
607
|
+
if speechInterruptible { speech?.terminate() } // hivey's lines queue instead
|
|
608
|
+
hideBubble?.cancel()
|
|
609
|
+
bubble.text = text
|
|
610
|
+
guard aloud && speakAloud else { scheduleHide(after: 2.5); return }
|
|
611
|
+
pet.talking = true
|
|
612
|
+
|
|
613
|
+
// `say` reads text from stdin, so text starting with "-" is never parsed as a flag
|
|
614
|
+
let (p, interruptible) = speechProcess()
|
|
615
|
+
speechInterruptible = interruptible
|
|
616
|
+
let pipe = Pipe()
|
|
617
|
+
p.standardInput = pipe
|
|
618
|
+
p.terminationHandler = { [weak self] proc in
|
|
619
|
+
DispatchQueue.main.async { self?.finished(proc) }
|
|
620
|
+
}
|
|
621
|
+
speech = p
|
|
622
|
+
do { try p.run() } catch { finished(p); return }
|
|
623
|
+
pipe.fileHandleForWriting.write(Data(text.utf8))
|
|
624
|
+
try? pipe.fileHandleForWriting.close()
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
private func finished(_ p: Process) {
|
|
628
|
+
guard speech === p else { return } // a newer utterance replaced this one
|
|
629
|
+
speech = nil
|
|
630
|
+
pet.talking = false
|
|
631
|
+
scheduleHide(after: 2)
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
private func scheduleHide(after seconds: Double) {
|
|
635
|
+
let work = DispatchWorkItem { [weak self] in self?.bubble.text = "" }
|
|
636
|
+
hideBubble = work
|
|
637
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + seconds, execute: work)
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/// One move by name (event CLI), optionally saying `text`
|
|
641
|
+
private func play(_ event: String, _ text: String) {
|
|
642
|
+
switch event {
|
|
643
|
+
case "agent": pet.addAgent()
|
|
644
|
+
case "leave": pet.removeAgent()
|
|
645
|
+
case "swarm": pet.formSwarm()
|
|
646
|
+
case "pulse": pet.pulse()
|
|
647
|
+
case "done": pet.celebrate()
|
|
648
|
+
case "chat": chat.toggle(above: window); return // same as clicking the pet
|
|
649
|
+
default: return
|
|
650
|
+
}
|
|
651
|
+
if !text.isEmpty { say(text) }
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/// hivey changed: the dots are its agents; one line per look, most urgent first
|
|
655
|
+
private func react(_ snapshot: HiveySnapshot, _ events: [HiveyEvent]) {
|
|
656
|
+
guard watching else { return }
|
|
657
|
+
if watcher.hiveyPath == nil {
|
|
658
|
+
hiveyItem.title = "hivey: not installed"
|
|
659
|
+
} else if snapshot.swarms.isEmpty {
|
|
660
|
+
hiveyItem.title = "hivey: nothing running"
|
|
661
|
+
} else {
|
|
662
|
+
hiveyItem.title = "hivey: \(snapshot.status.count) agents, \(snapshot.working) working"
|
|
663
|
+
pet.showAgents(snapshot.status.count)
|
|
664
|
+
}
|
|
665
|
+
pet.busy = snapshot.working > 0
|
|
666
|
+
|
|
667
|
+
var line: (text: String, aloud: Bool, rank: Int)?
|
|
668
|
+
func offer(_ text: String, aloud: Bool, rank: Int) {
|
|
669
|
+
if line == nil || rank > line!.rank { line = (text, aloud, rank) }
|
|
670
|
+
}
|
|
671
|
+
var pulses = 0
|
|
672
|
+
for event in events {
|
|
673
|
+
switch event {
|
|
674
|
+
case .launched(let slug):
|
|
675
|
+
pet.formSwarm()
|
|
676
|
+
offer("\(slug) is starting.", aloud: true, rank: 3)
|
|
677
|
+
case .needsYou(let names):
|
|
678
|
+
offer(needsYouLine(names), aloud: true, rank: 4)
|
|
679
|
+
case .finished(let name):
|
|
680
|
+
pet.celebrate()
|
|
681
|
+
offer("\(name) finished.", aloud: false, rank: 1)
|
|
682
|
+
case .message(let fromHuman, let replyFrom, let text):
|
|
683
|
+
if fromHuman { offer("On it.", aloud: false, rank: 0) }
|
|
684
|
+
if let replyFrom { offer(replyLine(from: replyFrom, text: text), aloud: true, rank: 2) }
|
|
685
|
+
if pulses < 3 {
|
|
686
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3 * Double(pulses)) { [weak self] in
|
|
687
|
+
self?.pet.pulse()
|
|
688
|
+
}
|
|
689
|
+
pulses += 1
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
if let line { say(line.text, aloud: line.aloud) }
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
private func needsYouLine(_ names: [String]) -> String {
|
|
697
|
+
switch names.count {
|
|
698
|
+
case 1: return "\(names[0]) needs you."
|
|
699
|
+
case 2: return "\(names[0]) and \(names[1]) need you."
|
|
700
|
+
default: return "\(names.count) agents need you."
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
@objc func toggleVoice() {
|
|
705
|
+
speakAloud.toggle()
|
|
706
|
+
voiceItem.state = speakAloud ? .on : .off
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
@objc func toggleWatch() {
|
|
710
|
+
watching.toggle()
|
|
711
|
+
watchItem.state = watching ? .on : .off
|
|
712
|
+
if watching {
|
|
713
|
+
hiveyItem.title = "hivey: looking…"
|
|
714
|
+
watcher.start()
|
|
715
|
+
} else {
|
|
716
|
+
watcher.stop()
|
|
717
|
+
pet.busy = false
|
|
718
|
+
hiveyItem.title = "hivey: not watching"
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
/// Without hivey: remove any old login item and quit
|
|
723
|
+
private func turnOff() {
|
|
724
|
+
setLogin(false)
|
|
725
|
+
NSApp.terminate(nil)
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
@objc func newAgent() { pet.addAgent() }
|
|
729
|
+
@objc func swarm() { pet.formSwarm() }
|
|
730
|
+
@objc func taskDone() { pet.celebrate() }
|
|
731
|
+
|
|
732
|
+
private func setLogin(_ on: Bool) {
|
|
733
|
+
guard on else { try? FileManager.default.removeItem(at: launchAgent); return }
|
|
734
|
+
let plist: [String: Any] = [
|
|
735
|
+
"Label": "com.hivey.pet",
|
|
736
|
+
"ProgramArguments": ["/usr/bin/open", "-a", Bundle.main.bundlePath],
|
|
737
|
+
"RunAtLoad": true,
|
|
738
|
+
]
|
|
739
|
+
try? FileManager.default.createDirectory(at: launchAgent.deletingLastPathComponent(), withIntermediateDirectories: true)
|
|
740
|
+
if let data = try? PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0) {
|
|
741
|
+
try? data.write(to: launchAgent)
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
let app = NSApplication.shared
|
|
747
|
+
app.setActivationPolicy(.accessory)
|
|
748
|
+
let delegate = AppDelegate()
|
|
749
|
+
app.delegate = delegate
|
|
750
|
+
app.run()
|