Skip to content

Introduce event dispatching#144

Merged
JasonTheAdams merged 9 commits intotrunkfrom
event-dispatching
Dec 12, 2025
Merged

Introduce event dispatching#144
JasonTheAdams merged 9 commits intotrunkfrom
event-dispatching

Conversation

@JasonTheAdams
Copy link
Copy Markdown
Member

Resolves #142

This introduces an abstract system for event dispatching using PSR-14 interfaces. An event dispatcher is set on the AiClient which can then be used throughout the system. There are currently two events:

  • AfterPromptSentEvent
  • BeforePromptSentEvent

We could add more, of course, but these seem like the two most sensible.

Being abstract, it's up to the implementing system to connect this to whatever concrete event system they have. It's also their responsibility to set up the PSR-14 event listeners, which would be fired when the dispatching is fulfilled. This makes the actual event system within the PHP AI Client pretty lightweight.

An intention here is to have a WP AI Client counterpart which hooks this up to the WordPress actions system.

@JasonTheAdams JasonTheAdams self-assigned this Dec 5, 2025
@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 5, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: JasonTheAdams <jason_the_adams@git.wordpress.org>
Co-authored-by: felixarntz <flixos90@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@JasonTheAdams
Copy link
Copy Markdown
Member Author

@felixarntz Do you think it's worth adding our own event interface so subsequent dispatchers can identify any events coming from our system? I think so, yeah? 🤔

Copy link
Copy Markdown
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JasonTheAdams I think it's a solid start, but I think we can refine a couple things.

src/AiClient.php Outdated
{
if (self::$eventDispatcher !== null) {
/** @var T */
return self::$eventDispatcher->dispatch($event);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether we want to be a bit safer here.

I don't love that an event listener can arbitrarily replace the entire instance with another instance. I think we'll want to remain in control of what can and what cannot be modified.

While PSR-14 doesn't mandate that, it doesn't mean we cannot be stricter. I think we should include here a check to ensure that the return value is $modified_event === $event, and otherwise throw.

Yes, that technically means we wouldn't even need to return it, since objects are modified by reference. But I think this is preferable since that leaves us in control.

For example, right now you have a setMessages method on the event implementations, but having that doesn't even matter, since a listener could just create a new instance of the class and replace everything. I think that's not great.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some thought, I agree with your reasoning but not the placement. Hahah!

I think this should be a decision of the actual event dispatcher. Seeing as we're trying to be framework agnostic, we don't want to assume that our WP use case reflects how everyone wants event dispatching to work. This is something we can put in our own dispatcher in WP AI Client, but I don't think should be done here.

@felixarntz felixarntz added this to the 0.4.0 milestone Dec 5, 2025
@felixarntz felixarntz added the [Feature] New feature to highlight in changelogs. label Dec 5, 2025
@felixarntz
Copy link
Copy Markdown
Member

@JasonTheAdams

Do you think it's worth adding our own event interface so subsequent dispatchers can identify any events coming from our system? I think so, yeah? 🤔

I like that idea, yeah. But it probably should simply be an empty interface. It'll allow to do exactly what you're saying, but otherwise we can adhere to the regular constraint of just object.

In the WP AI Client for example, we can check for the class name of the event and based on that fire specific action hooks with names that make sense according to the event class name.

Copy link
Copy Markdown
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JasonTheAdams Looks great! Two minor points.

@JasonTheAdams JasonTheAdams merged commit 61b5144 into trunk Dec 12, 2025
7 checks passed
@JasonTheAdams JasonTheAdams deleted the event-dispatching branch December 12, 2025 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] New feature to highlight in changelogs.

2 participants