forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-event-bus.ts
More file actions
45 lines (38 loc) · 1.08 KB
/
Copy pathdebug-event-bus.ts
File metadata and controls
45 lines (38 loc) · 1.08 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
import { BaseEvent } from "@ag-ui/client";
import { DebugEventEnvelope } from "@copilotkit/shared";
export type DebugEventListener = (envelope: DebugEventEnvelope) => void;
export class DebugEventBus {
private listeners = new Set<DebugEventListener>();
subscribe(listener: DebugEventListener): () => void {
this.listeners.add(listener);
return () => {
this.listeners.delete(listener);
};
}
broadcast(
event: BaseEvent,
metadata: { agentId: string; threadId: string; runId: string },
): void {
if (this.listeners.size === 0) return;
const envelope: DebugEventEnvelope = {
timestamp: Date.now(),
agentId: metadata.agentId,
threadId: metadata.threadId,
runId: metadata.runId,
event,
};
for (const listener of this.listeners) {
try {
listener(envelope);
} catch (err) {
console.warn(
"[DebugEventBus] Listener error suppressed:",
err instanceof Error ? err.message : err,
);
}
}
}
get listenerCount(): number {
return this.listeners.size;
}
}