-
Notifications
You must be signed in to change notification settings - Fork 76
feat(server): make DefaultExecutionEventBus cross-runtime compatible #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,37 +1,93 @@ | ||||||||||||||||||
import { EventEmitter } from 'events'; | ||||||||||||||||||
import { Message, Task, TaskStatusUpdateEvent, TaskArtifactUpdateEvent } from "../../types.js"; | ||||||||||||||||||
|
||||||||||||||||||
import { | ||||||||||||||||||
Message, | ||||||||||||||||||
Task, | ||||||||||||||||||
TaskStatusUpdateEvent, | ||||||||||||||||||
TaskArtifactUpdateEvent, | ||||||||||||||||||
} from "../../types.js"; | ||||||||||||||||||
export type AgentExecutionEvent = Message | Task | TaskStatusUpdateEvent | TaskArtifactUpdateEvent; | ||||||||||||||||||
|
||||||||||||||||||
export type AgentExecutionEvent = | ||||||||||||||||||
| Message | ||||||||||||||||||
| Task | ||||||||||||||||||
| TaskStatusUpdateEvent | ||||||||||||||||||
| TaskArtifactUpdateEvent; | ||||||||||||||||||
export type EventListener = (event: AgentExecutionEvent) => void; | ||||||||||||||||||
|
||||||||||||||||||
export interface ExecutionEventBus { | ||||||||||||||||||
publish(event: AgentExecutionEvent): void; | ||||||||||||||||||
on(eventName: 'event' | 'finished', listener: (event: AgentExecutionEvent) => void): this; | ||||||||||||||||||
off(eventName: 'event' | 'finished', listener: (event: AgentExecutionEvent) => void): this; | ||||||||||||||||||
once(eventName: 'event' | 'finished', listener: (event: AgentExecutionEvent) => void): this; | ||||||||||||||||||
removeAllListeners(eventName?: 'event' | 'finished'): this; | ||||||||||||||||||
finished(): void; | ||||||||||||||||||
publish(event: AgentExecutionEvent): void; | ||||||||||||||||||
on(eventName: "event" | "finished", listener: EventListener): this; | ||||||||||||||||||
off(eventName: "event" | "finished", listener: EventListener): this; | ||||||||||||||||||
once(eventName: "event" | "finished", listener: EventListener): this; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove this once method. I don't this is used. |
||||||||||||||||||
removeAllListeners(eventName?: "event" | "finished"): this; | ||||||||||||||||||
finished(): void; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
export class DefaultExecutionEventBus extends EventEmitter implements ExecutionEventBus { | ||||||||||||||||||
constructor() { | ||||||||||||||||||
super(); | ||||||||||||||||||
export class DefaultExecutionEventBus implements ExecutionEventBus { | ||||||||||||||||||
private eventListeners: Map<string, Set<EventListener>> = new Map(); | ||||||||||||||||||
|
||||||||||||||||||
constructor() { | ||||||||||||||||||
this.eventListeners.set("event", new Set()); | ||||||||||||||||||
this.eventListeners.set("finished", new Set()); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
publish(event: AgentExecutionEvent): void { | ||||||||||||||||||
const listeners = this.eventListeners.get("event"); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the following code is copied across both publish & finished. Can we just encapsulate this as _emit(). |
||||||||||||||||||
if (listeners) { | ||||||||||||||||||
// Create a copy of listeners to avoid issues if listeners are modified during iteration | ||||||||||||||||||
const listenersCopy = Array.from(listeners); | ||||||||||||||||||
for (const listener of listenersCopy) { | ||||||||||||||||||
try { | ||||||||||||||||||
listener(event); | ||||||||||||||||||
} catch (error) { | ||||||||||||||||||
console.error("Error in event listener:", error); | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
finished(): void { | ||||||||||||||||||
// Emit finished event to 'finished' listeners | ||||||||||||||||||
const finishedListeners = this.eventListeners.get("finished"); | ||||||||||||||||||
if (finishedListeners) { | ||||||||||||||||||
const listenersCopy = Array.from(finishedListeners); | ||||||||||||||||||
for (const listener of listenersCopy) { | ||||||||||||||||||
try { | ||||||||||||||||||
// For finished event, we don't pass an event object | ||||||||||||||||||
listener({} as AgentExecutionEvent); | ||||||||||||||||||
Comment on lines
+46
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation of the The previous To maintain previous behavior and avoid unsafe type casting, it's better to call the listener without arguments. This requires a cast to
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As suggested, lets make the
Comment on lines
+46
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Casting an empty object
Suggested change
|
||||||||||||||||||
} catch (error) { | ||||||||||||||||||
console.error("Error in finished listener:", error); | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
publish(event: AgentExecutionEvent): void { | ||||||||||||||||||
this.emit('event', event); | ||||||||||||||||||
on(eventName: "event" | "finished", listener: EventListener): this { | ||||||||||||||||||
const listeners = this.eventListeners.get(eventName); | ||||||||||||||||||
if (listeners) { | ||||||||||||||||||
listeners.add(listener); | ||||||||||||||||||
} | ||||||||||||||||||
return this; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
off(eventName: "event" | "finished", listener: EventListener): this { | ||||||||||||||||||
const listeners = this.eventListeners.get(eventName); | ||||||||||||||||||
if (listeners) { | ||||||||||||||||||
listeners.delete(listener); | ||||||||||||||||||
} | ||||||||||||||||||
return this; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
once(eventName: "event" | "finished", listener: EventListener): this { | ||||||||||||||||||
const onceWrapper = (event: AgentExecutionEvent) => { | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To align with the proposed change to
Suggested change
|
||||||||||||||||||
listener(event); | ||||||||||||||||||
this.off(eventName, onceWrapper); | ||||||||||||||||||
}; | ||||||||||||||||||
return this.on(eventName, onceWrapper); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
finished(): void { | ||||||||||||||||||
this.emit('finished'); | ||||||||||||||||||
removeAllListeners(eventName?: "event" | "finished"): this { | ||||||||||||||||||
if (eventName) { | ||||||||||||||||||
const listeners = this.eventListeners.get(eventName); | ||||||||||||||||||
if (listeners) { | ||||||||||||||||||
listeners.clear(); | ||||||||||||||||||
} | ||||||||||||||||||
} else { | ||||||||||||||||||
// Remove all listeners for all events | ||||||||||||||||||
for (const listeners of this.eventListeners.values()) { | ||||||||||||||||||
listeners.clear(); | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
return this; | ||||||||||||||||||
} | ||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To safely handle events that don't have a payload, like the
finished
event, theEventListener
type should be updated to make theevent
parameter optional. This prevents type-safety issues when dispatching parameter-less events and makes the API more robust.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1