Files
paperclip/server/src/services/live-events.ts
Forgotten c9c75bbc0a Implement agent runtime services and WebSocket realtime
Expand heartbeat service with full run executor, wakeup coordinator,
and adapter lifecycle. Add run-log-store for pluggable log persistence.
Add live-events service and WebSocket handler for realtime updates.
Expand agent and issue routes with runtime operations. Add ws dependency.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 12:24:43 -06:00

41 lines
1006 B
TypeScript

import { EventEmitter } from "node:events";
import type { LiveEvent, LiveEventType } from "@paperclip/shared";
type LiveEventPayload = Record<string, unknown>;
type LiveEventListener = (event: LiveEvent) => void;
const emitter = new EventEmitter();
emitter.setMaxListeners(0);
let nextEventId = 0;
function toLiveEvent(input: {
companyId: string;
type: LiveEventType;
payload?: LiveEventPayload;
}): LiveEvent {
nextEventId += 1;
return {
id: nextEventId,
companyId: input.companyId,
type: input.type,
createdAt: new Date().toISOString(),
payload: input.payload ?? {},
};
}
export function publishLiveEvent(input: {
companyId: string;
type: LiveEventType;
payload?: LiveEventPayload;
}) {
const event = toLiveEvent(input);
emitter.emit(input.companyId, event);
return event;
}
export function subscribeCompanyLiveEvents(companyId: string, listener: LiveEventListener) {
emitter.on(companyId, listener);
return () => emitter.off(companyId, listener);
}