24 lines
619 B
TypeScript
24 lines
619 B
TypeScript
type EventName = 'process:start' | 'agent:start' | 'step:before' | 'tool:before' | 'tool:after' | 'agent:end';
|
|
type Listener = (data: any) => void | Promise<void>;
|
|
|
|
export class HookBus {
|
|
private listeners = new Map<EventName, Listener[]>();
|
|
|
|
on(event: EventName, fn: Listener) {
|
|
if (!this.listeners.has(event)) {
|
|
this.listeners.set(event, []);
|
|
}
|
|
this.listeners.get(event)!.push(fn);
|
|
}
|
|
|
|
async emit(event: EventName, data: any) {
|
|
const fns = this.listeners.get(event);
|
|
if (!fns) return;
|
|
for (const fn of fns) {
|
|
await fn(data);
|
|
}
|
|
}
|
|
}
|
|
|
|
export const hooks = new HookBus();
|