Repository navigation
Expand file tree
/
Copy pathintercom.go
More file actions
413 lines (349 loc) · 9.65 KB
/
Copy pathintercom.go
File metadata and controls
413 lines (349 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package delegation
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/RealityLink-Tech/MoonHub/pkg/logger"
)
// Topic constants for intercom events
const (
// Task lifecycle
TopicTaskQueued = "task:queued"
TopicTaskCompleted = "task:completed"
TopicTaskFailed = "task:failed"
// Agent lifecycle
TopicAgentCreated = "agent:created"
TopicAgentDismissed = "agent:dismissed"
TopicAgentSuspended = "agent:suspended"
TopicAgentRevived = "agent:revived"
// Memory events
TopicMemoryUpdated = "memory:updated"
TopicMemoryConsolidated = "memory:consolidated"
// Blackboard collaboration
TopicBlackboardProposal = "blackboard:proposal"
TopicBlackboardResolved = "blackboard:resolved"
// Nudge/reminder events
TopicNudgeScheduled = "nudge:scheduled"
TopicNudgeDelivered = "nudge:delivered"
TopicNudgeSuppressed = "nudge:suppressed"
)
// EventHandler is a function that handles an intercom event
type EventHandler func(ctx context.Context, event IntercomEvent)
// UnsubscribeFunc is returned when subscribing to events
type UnsubscribeFunc func()
type intercomSub struct {
id uint64
h EventHandler
}
// Intercom is a pub/sub event system for delegation communication
type Intercom struct {
mu sync.RWMutex
handlers map[string][]intercomSub
wildcards []intercomSub
recent map[string][]IntercomEvent
maxRecent int
nextID atomic.Uint64
}
// NewIntercom creates a new intercom event system
func NewIntercom(maxRecentEvents int) *Intercom {
return &Intercom{
handlers: make(map[string][]intercomSub),
recent: make(map[string][]IntercomEvent),
maxRecent: maxRecentEvents,
}
}
// On subscribes to events on a specific topic
// Returns an unsubscribe function
func (i *Intercom) On(topic string, handler EventHandler) UnsubscribeFunc {
sid := i.nextID.Add(1)
sub := intercomSub{id: sid, h: handler}
i.mu.Lock()
i.handlers[topic] = append(i.handlers[topic], sub)
i.mu.Unlock()
logger.DebugCF("delegation", "Subscribed to topic", map[string]any{
"topic": topic,
})
return func() {
i.mu.Lock()
defer i.mu.Unlock()
subs := i.handlers[topic]
for j, s := range subs {
if s.id == sid {
i.handlers[topic] = append(subs[:j], subs[j+1:]...)
break
}
}
}
}
// OnAny subscribes to all events
// Returns an unsubscribe function
func (i *Intercom) OnAny(handler EventHandler) UnsubscribeFunc {
sid := i.nextID.Add(1)
sub := intercomSub{id: sid, h: handler}
i.mu.Lock()
i.wildcards = append(i.wildcards, sub)
i.mu.Unlock()
return func() {
i.mu.Lock()
defer i.mu.Unlock()
for j, s := range i.wildcards {
if s.id == sid {
i.wildcards = append(i.wildcards[:j], i.wildcards[j+1:]...)
break
}
}
}
}
// Emit publishes an event to all subscribers
func (i *Intercom) Emit(ctx context.Context, topic, userID string, data map[string]any) {
event := IntercomEvent{
Topic: topic,
UserID: userID,
Data: data,
Timestamp: time.Now().UnixMilli(),
}
// Store in recent events
i.storeRecent(event)
// Get handlers under read lock
i.mu.RLock()
subs := i.handlers[topic]
handlers := make([]EventHandler, len(subs))
for j, s := range subs {
handlers[j] = s.h
}
wildSubs := i.wildcards
wildcards := make([]EventHandler, len(wildSubs))
for j, s := range wildSubs {
wildcards[j] = s.h
}
i.mu.RUnlock()
// Call topic-specific handlers
for _, h := range handlers {
i.callHandler(ctx, h, event)
}
// Call wildcard handlers
for _, h := range wildcards {
i.callHandler(ctx, h, event)
}
logger.DebugCF("delegation", "Emitted event", map[string]any{
"topic": topic,
"user_id": userID,
"handlers": len(handlers) + len(wildcards),
})
}
func (i *Intercom) callHandler(ctx context.Context, h EventHandler, event IntercomEvent) {
defer func() {
if r := recover(); r != nil {
logger.ErrorCF("delegation", "Handler panic recovered", map[string]any{
"topic": event.Topic,
"panic": r,
})
}
}()
h(ctx, event)
}
func (i *Intercom) storeRecent(event IntercomEvent) {
i.mu.Lock()
defer i.mu.Unlock()
events := i.recent[event.Topic]
events = append(events, event)
// Trim to max recent
if len(events) > i.maxRecent {
events = events[len(events)-i.maxRecent:]
}
i.recent[event.Topic] = events
}
// Recent returns the most recent events for a topic
func (i *Intercom) Recent(topic string, limit int) []IntercomEvent {
i.mu.RLock()
defer i.mu.RUnlock()
events := i.recent[topic]
if limit <= 0 || limit > len(events) {
limit = len(events)
}
// Return most recent events (from the end)
result := make([]IntercomEvent, limit)
copy(result, events[len(events)-limit:])
return result
}
// RecentAll returns recent events across all topics
func (i *Intercom) RecentAll(limit int) []IntercomEvent {
i.mu.RLock()
defer i.mu.RUnlock()
var all []IntercomEvent
for _, events := range i.recent {
all = append(all, events...)
}
// Sort by timestamp (newest first)
for i := 0; i < len(all); i++ {
for j := i + 1; j < len(all); j++ {
if all[i].Timestamp < all[j].Timestamp {
all[i], all[j] = all[j], all[i]
}
}
}
if limit > 0 && len(all) > limit {
all = all[:limit]
}
return all
}
// Clear removes all handlers and recent events
func (i *Intercom) Clear() {
i.mu.Lock()
defer i.mu.Unlock()
i.handlers = make(map[string][]intercomSub)
i.wildcards = nil
i.recent = make(map[string][]IntercomEvent)
}
// Stats returns statistics about the intercom
func (i *Intercom) Stats() map[string]any {
i.mu.RLock()
defer i.mu.RUnlock()
topicCounts := make(map[string]int)
totalRecent := 0
for topic, events := range i.recent {
topicCounts[topic] = len(events)
totalRecent += len(events)
}
return map[string]any{
"topics": len(i.handlers),
"wildcard_count": len(i.wildcards),
"recent_events": totalRecent,
"recent_by_topic": topicCounts,
}
}
// BlackboardProposalWithStatus extends BlackboardProposal with resolution status
type BlackboardProposalWithStatus struct {
BlackboardProposal
Resolved bool
Resolution string
ResolvedAt time.Time
}
// Blackboard implements a simple blackboard pattern for collaboration
type Blackboard struct {
mu sync.RWMutex
proposals []BlackboardProposalWithStatus
maxEntries int
intercom *Intercom // Optional intercom for emitting events
}
// NewBlackboard creates a new blackboard for collaboration
func NewBlackboard(maxEntries int) *Blackboard {
return &Blackboard{
proposals: make([]BlackboardProposalWithStatus, 0),
maxEntries: maxEntries,
}
}
// SetIntercom sets the intercom instance for emitting events
func (b *Blackboard) SetIntercom(intercom *Intercom) {
b.mu.Lock()
defer b.mu.Unlock()
b.intercom = intercom
}
// Post adds a proposal to the blackboard.
// userID is the delegation partition (IntercomEvent.UserID); subAgentID identifies the proposing sub-agent.
func (b *Blackboard) Post(userID, subAgentID, proposal string) {
b.mu.Lock()
defer b.mu.Unlock()
b.proposals = append(b.proposals, BlackboardProposalWithStatus{
BlackboardProposal: BlackboardProposal{
SubAgentID: subAgentID,
Proposal: proposal,
Timestamp: time.Now(),
},
Resolved: false,
})
// Trim old entries
if len(b.proposals) > b.maxEntries {
b.proposals = b.proposals[len(b.proposals)-b.maxEntries:]
}
logger.DebugCF("delegation", "Blackboard proposal posted", map[string]any{
"sub_agent_id": subAgentID,
"total": len(b.proposals),
})
// Emit event if intercom is set
if b.intercom != nil {
ic := b.intercom
go ic.Emit(context.Background(), TopicBlackboardProposal, userID, map[string]any{
"sub_agent_id": subAgentID,
"proposal": proposal,
})
}
}
// Resolve marks a proposal as resolved.
func (b *Blackboard) Resolve(userID, subAgentID, resolution string) {
b.mu.Lock()
defer b.mu.Unlock()
for i := range b.proposals {
if b.proposals[i].SubAgentID == subAgentID && !b.proposals[i].Resolved {
b.proposals[i].Resolved = true
b.proposals[i].Resolution = resolution
b.proposals[i].ResolvedAt = time.Now()
propText := b.proposals[i].Proposal
logger.DebugCF("delegation", "Blackboard proposal resolved", map[string]any{
"sub_agent_id": subAgentID,
"resolution": resolution,
})
// Emit event if intercom is set
if b.intercom != nil {
ic := b.intercom
go ic.Emit(context.Background(), TopicBlackboardResolved, userID, map[string]any{
"sub_agent_id": subAgentID,
"resolution": resolution,
"proposal": propText,
})
}
return
}
}
}
// GetAll returns all proposals
func (b *Blackboard) GetAll() []BlackboardProposalWithStatus {
b.mu.RLock()
defer b.mu.RUnlock()
result := make([]BlackboardProposalWithStatus, len(b.proposals))
copy(result, b.proposals)
return result
}
// GetByAgent returns proposals from a specific agent
func (b *Blackboard) GetByAgent(subAgentID string) []BlackboardProposalWithStatus {
b.mu.RLock()
defer b.mu.RUnlock()
var result []BlackboardProposalWithStatus
for _, p := range b.proposals {
if p.SubAgentID == subAgentID {
result = append(result, p)
}
}
return result
}
// GetUnresolved returns all unresolved proposals
func (b *Blackboard) GetUnresolved() []BlackboardProposalWithStatus {
b.mu.RLock()
defer b.mu.RUnlock()
var result []BlackboardProposalWithStatus
for _, p := range b.proposals {
if !p.Resolved {
result = append(result, p)
}
}
return result
}
// GetResolved returns all resolved proposals
func (b *Blackboard) GetResolved() []BlackboardProposalWithStatus {
b.mu.RLock()
defer b.mu.RUnlock()
var result []BlackboardProposalWithStatus
for _, p := range b.proposals {
if p.Resolved {
result = append(result, p)
}
}
return result
}
// Clear removes all proposals
func (b *Blackboard) Clear() {
b.mu.Lock()
defer b.mu.Unlock()
b.proposals = b.proposals[:0]
}