-
Notifications
You must be signed in to change notification settings - Fork 281
Add WithoutBroadcasting attribute to skip oversized stream events #752
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
Open
pushpak1300
wants to merge
5
commits into
0.x
Choose a base branch
from
broadcast-without-attribute
base: 0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6cb75cf
Add WithoutBroadcasting attribute to skip oversized stream events
pushpak1300 7a5ffdb
Resolve WithoutBroadcasting skip set once and cover broadcast paths
pushpak1300 11fcbae
Guard stream event broadcasts against BroadcastException
pushpak1300 6c73c32
Validate excluded events and centralize the broadcast skip check
pushpak1300 fec6a98
Rename broadcast skip predicate to excludes
pushpak1300 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php | ||
|
|
||
| namespace Laravel\Ai\Attributes; | ||
|
|
||
| use Attribute; | ||
| use InvalidArgumentException; | ||
| use Laravel\Ai\Streaming\Events\StreamEvent; | ||
| use ReflectionClass; | ||
|
|
||
| #[Attribute(Attribute::TARGET_CLASS)] | ||
| final class WithoutBroadcasting | ||
| { | ||
| /** | ||
| * The stream event classes that should not be broadcast. | ||
| * | ||
| * @var array<int, class-string<StreamEvent>> | ||
| */ | ||
| public array $events; | ||
|
|
||
| /** | ||
| * Create a new attribute instance. | ||
| * | ||
| * @param class-string<StreamEvent> ...$events | ||
| */ | ||
| public function __construct(string ...$events) | ||
|
pushpak1300 marked this conversation as resolved.
|
||
| { | ||
| foreach ($events as $event) { | ||
| if (! is_subclass_of($event, StreamEvent::class)) { | ||
| throw new InvalidArgumentException("[{$event}] is not a valid ".StreamEvent::class.' to exclude from broadcasting.'); | ||
| } | ||
| } | ||
|
|
||
| $this->events = $events; | ||
| } | ||
|
|
||
| /** | ||
| * Determine if the given event is excluded by the resolved skip set. | ||
| * | ||
| * @param array<int, class-string<StreamEvent>> $events | ||
| */ | ||
| public static function excludes(array $events, StreamEvent $event): bool | ||
| { | ||
| return in_array($event::class, $events, true); | ||
| } | ||
|
|
||
| /** | ||
| * Get the stream event classes that should not be broadcast for the target agent. | ||
| * | ||
| * @return array<int, class-string<StreamEvent>> | ||
| */ | ||
| public static function eventsFor(?object $target): array | ||
| { | ||
| if ($target === null) { | ||
| return []; | ||
| } | ||
|
|
||
| $attributes = (new ReflectionClass($target))->getAttributes(self::class); | ||
|
|
||
| if ($attributes === []) { | ||
| return []; | ||
| } | ||
|
|
||
| return $attributes[0]->newInstance()->events; | ||
| } | ||
| } | ||
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
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,81 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Broadcasting\AnonymousEvent; | ||
| use Illuminate\Broadcasting\Channel; | ||
| use Illuminate\Support\Facades\Event; | ||
| use Laravel\Ai\Attributes\WithoutBroadcasting; | ||
| use Laravel\Ai\Jobs\BroadcastAgent; | ||
| use Laravel\Ai\Streaming\Events\TextDelta; | ||
| use Laravel\Ai\Streaming\Events\ToolCall; | ||
| use Laravel\Ai\Streaming\Events\ToolResult; | ||
| use Tests\Fixtures\Agents\AssistantAgent; | ||
| use Tests\Fixtures\Agents\NonBroadcastingTextAgent; | ||
| use Tests\Fixtures\Agents\NonBroadcastingToolAgent; | ||
|
|
||
| test('it rejects event classes that are not stream events', function () { | ||
| expect(fn () => new WithoutBroadcasting('App\Events\Typo')) | ||
| ->toThrow(InvalidArgumentException::class); | ||
| }); | ||
|
|
||
| test('no events are withheld when the attribute is absent', function () { | ||
| expect(WithoutBroadcasting::eventsFor(new AssistantAgent))->toBe([]); | ||
| }); | ||
|
|
||
| test('listed events are withheld when the attribute is present', function () { | ||
| expect(WithoutBroadcasting::eventsFor(new NonBroadcastingToolAgent)) | ||
| ->toContain(ToolResult::class) | ||
| ->toContain(ToolCall::class); | ||
| }); | ||
|
|
||
| test('events not listed in the attribute are not withheld', function () { | ||
| expect(WithoutBroadcasting::eventsFor(new NonBroadcastingToolAgent)) | ||
| ->not->toContain(TextDelta::class); | ||
| }); | ||
|
|
||
| test('a null target withholds nothing', function () { | ||
| expect(WithoutBroadcasting::eventsFor(null))->toBe([]); | ||
| }); | ||
|
|
||
| test('broadcast withholds listed events from the channel but assembles the full response', function () { | ||
| Event::fake(); | ||
| NonBroadcastingTextAgent::fake(['Hello world']); | ||
|
|
||
| $received = null; | ||
|
|
||
| (new NonBroadcastingTextAgent) | ||
| ->broadcastNow('Say hello', new Channel('test-channel')) | ||
| ->then(function ($response) use (&$received) { | ||
| $received = $response; | ||
| }); | ||
|
|
||
| Event::assertNotDispatched(AnonymousEvent::class, fn (AnonymousEvent $e) => $e->broadcastAs() === 'text_delta'); | ||
| Event::assertDispatched(AnonymousEvent::class, fn (AnonymousEvent $e) => $e->broadcastAs() === 'stream_start'); | ||
|
|
||
| expect($received)->not->toBeNull('then() callback was never invoked') | ||
| ->and($received->text)->toBe('Hello world'); | ||
| }); | ||
|
|
||
| test('broadcast agent withholds listed events from the channel but resolves the full response', function () { | ||
| Event::fake(); | ||
| NonBroadcastingTextAgent::fake(['Hello world']); | ||
|
|
||
| $received = null; | ||
|
|
||
| $job = new BroadcastAgent( | ||
| agent: new NonBroadcastingTextAgent, | ||
| prompt: 'Say hello', | ||
| channels: new Channel('test-channel'), | ||
| ); | ||
|
|
||
| $job->then(function ($response) use (&$received) { | ||
| $received = $response; | ||
| }); | ||
|
|
||
| $job->handle(); | ||
|
|
||
| Event::assertNotDispatched(AnonymousEvent::class, fn (AnonymousEvent $e) => $e->broadcastAs() === 'text_delta'); | ||
| Event::assertDispatched(AnonymousEvent::class, fn (AnonymousEvent $e) => $e->broadcastAs() === 'stream_start'); | ||
|
|
||
| expect($received)->not->toBeNull('then() callback was never invoked') | ||
| ->and($received->text)->toBe('Hello world'); | ||
| }); |
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,19 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Fixtures\Agents; | ||
|
|
||
| use Laravel\Ai\Attributes\WithoutBroadcasting; | ||
| use Laravel\Ai\Contracts\Agent; | ||
| use Laravel\Ai\Promptable; | ||
| use Laravel\Ai\Streaming\Events\TextDelta; | ||
|
|
||
| #[WithoutBroadcasting(TextDelta::class)] | ||
| class NonBroadcastingTextAgent implements Agent | ||
| { | ||
| use Promptable; | ||
|
|
||
| public function instructions(): string | ||
| { | ||
| return 'You are a helpful assistant.'; | ||
| } | ||
| } |
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,20 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Fixtures\Agents; | ||
|
|
||
| use Laravel\Ai\Attributes\WithoutBroadcasting; | ||
| use Laravel\Ai\Contracts\Agent; | ||
| use Laravel\Ai\Promptable; | ||
| use Laravel\Ai\Streaming\Events\ToolCall; | ||
| use Laravel\Ai\Streaming\Events\ToolResult; | ||
|
|
||
| #[WithoutBroadcasting(ToolResult::class, ToolCall::class)] | ||
| class NonBroadcastingToolAgent implements Agent | ||
| { | ||
| use Promptable; | ||
|
|
||
| public function instructions(): string | ||
| { | ||
| return 'You are a helpful assistant.'; | ||
| } | ||
| } |
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.