-
Notifications
You must be signed in to change notification settings - Fork 20
feat: enable real-time streaming of generator tool preliminary results #128
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
Merged
+571
−43
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9833f47
feat: enable real-time streaming of generator tool preliminary results
mattapperson 44da0fe
fix: address code review issues
mattapperson 78e5be4
test: use claude-haiku-4.5 for flaky chat-style tools test
mattapperson 7236091
refactor: use optional chaining instead of try-catch for broadcaster …
mattapperson 7b81cf9
style: add braces to single-line if statement
mattapperson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| /** | ||
| * A push-based event broadcaster that supports multiple concurrent consumers. | ||
| * Similar to ReusableReadableStream but for push-based events from tool execution. | ||
| * | ||
| * Each consumer gets their own position in the buffer and receives all events | ||
| * from their join point onward. This enables real-time streaming of generator | ||
| * tool preliminary results to multiple consumers simultaneously. | ||
| * | ||
| * @template T - The event type being broadcast | ||
| */ | ||
| export class ToolEventBroadcaster<T> { | ||
| private buffer: T[] = []; | ||
| private consumers = new Map<number, ConsumerState>(); | ||
| private nextConsumerId = 0; | ||
| private isComplete = false; | ||
| private completionError: Error | null = null; | ||
|
|
||
| /** | ||
| * Push a new event to all consumers. | ||
| * Events are buffered so late-joining consumers can catch up. | ||
| */ | ||
| push(event: T): void { | ||
| if (this.isComplete) { | ||
| return; | ||
| } | ||
| this.buffer.push(event); | ||
| this.notifyWaitingConsumers(); | ||
| } | ||
|
|
||
| /** | ||
| * Mark the broadcaster as complete - no more events will be pushed. | ||
| * Optionally pass an error to signal failure to all consumers. | ||
| * Cleans up buffer and consumers after completion. | ||
| */ | ||
| complete(error?: Error): void { | ||
| this.isComplete = true; | ||
| this.completionError = error ?? null; | ||
| this.notifyWaitingConsumers(); | ||
| // Schedule cleanup after consumers have processed completion | ||
| queueMicrotask(() => this.cleanup()); | ||
| } | ||
|
|
||
| /** | ||
| * Clean up resources after all consumers have finished. | ||
| * Called automatically after complete(), but can be called manually. | ||
| */ | ||
| private cleanup(): void { | ||
| // Only cleanup if complete and all consumers are done | ||
| if (this.isComplete && this.consumers.size === 0) { | ||
| this.buffer = []; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create a new consumer that can independently iterate over events. | ||
| * Consumers can join at any time and will receive events from position 0. | ||
| * Multiple consumers can be created and will all receive the same events. | ||
| */ | ||
| createConsumer(): AsyncIterableIterator<T> { | ||
| const consumerId = this.nextConsumerId++; | ||
| const state: ConsumerState = { | ||
| position: 0, | ||
| waitingPromise: null, | ||
| cancelled: false, | ||
| }; | ||
| this.consumers.set(consumerId, state); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-this-alias | ||
| const self = this; | ||
|
|
||
| return { | ||
| async next(): Promise<IteratorResult<T>> { | ||
| const consumer = self.consumers.get(consumerId); | ||
| if (!consumer) { | ||
| return { done: true, value: undefined }; | ||
| } | ||
|
|
||
| if (consumer.cancelled) { | ||
| return { done: true, value: undefined }; | ||
| } | ||
|
|
||
| // Return buffered event if available | ||
| if (consumer.position < self.buffer.length) { | ||
| const value = self.buffer[consumer.position]!; | ||
| consumer.position++; | ||
| return { done: false, value }; | ||
| } | ||
|
|
||
| // If complete and caught up, we're done | ||
| if (self.isComplete) { | ||
| self.consumers.delete(consumerId); | ||
| self.cleanup(); | ||
| if (self.completionError) { | ||
| throw self.completionError; | ||
| } | ||
| return { done: true, value: undefined }; | ||
| } | ||
|
|
||
| // Set up waiting promise FIRST to avoid race condition | ||
| const waitPromise = new Promise<void>((resolve, reject) => { | ||
| consumer.waitingPromise = { resolve, reject }; | ||
|
|
||
| // Immediately check if we should resolve after setting up promise | ||
| if ( | ||
| self.isComplete || | ||
| self.completionError || | ||
| consumer.position < self.buffer.length | ||
| ) { | ||
| resolve(); | ||
| } | ||
| }); | ||
|
|
||
| await waitPromise; | ||
| consumer.waitingPromise = null; | ||
|
|
||
| // Recursively try again after waking up | ||
| return this.next(); | ||
| }, | ||
|
|
||
| async return(): Promise<IteratorResult<T>> { | ||
| const consumer = self.consumers.get(consumerId); | ||
| if (consumer) { | ||
| consumer.cancelled = true; | ||
| self.consumers.delete(consumerId); | ||
| self.cleanup(); | ||
| } | ||
| return { done: true, value: undefined }; | ||
| }, | ||
|
|
||
| async throw(e?: unknown): Promise<IteratorResult<T>> { | ||
| const consumer = self.consumers.get(consumerId); | ||
| if (consumer) { | ||
| consumer.cancelled = true; | ||
| self.consumers.delete(consumerId); | ||
| self.cleanup(); | ||
| } | ||
| throw e; | ||
| }, | ||
|
|
||
| [Symbol.asyncIterator]() { | ||
| return this; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Notify all waiting consumers that new data is available or stream completed | ||
| */ | ||
| private notifyWaitingConsumers(): void { | ||
| for (const consumer of this.consumers.values()) { | ||
| if (consumer.waitingPromise) { | ||
| if (this.completionError) { | ||
| consumer.waitingPromise.reject(this.completionError); | ||
| } else { | ||
| consumer.waitingPromise.resolve(); | ||
| } | ||
| consumer.waitingPromise = null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| interface ConsumerState { | ||
| position: number; | ||
| waitingPromise: { | ||
| resolve: () => void; | ||
| reject: (error: Error) => void; | ||
| } | null; | ||
| cancelled: boolean; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.