credo-ts
credo-ts copied to clipboard
add agent middleware
As a developer I sometimes want to apply custom logic for all incoming messages. We already have handlers that allow to implement logic for a custom message type. I think it would be beneficial to introduce the concept of middleware into aries-framework-javascript.
aries-framework-dotnet also implements the concept of middleware which we can take as an example
Checklist
- [ ] create
AgentMiddlewareinterface - [ ] call middleware before/after passing to handler (see question below)
- I think
Dispatcherwould be the best place to call middleware
- I think
- [ ] add function to register middleware to
Agent(likeregisterHandlers)- one function to register them all, or function that can be called multiple times?
Questions
- When should middleware be called?
- Before passing to handler / after passing to handler / both?
- .NET runs the middleware after passing a message to the handler.
Use cases
Probably a lot more, but this are two I can think of at the moment.
This would be a nice enhancement @TimoGlastra .
However, in my opinion, the AFJ library should define the AgentMiddleware abstraction / interfaces, and NOT the concrete implementations of the Middleware. Those are normally provided in the runtime like Node.js Web framework-level like Express, Oak, etc.
AF .NET also does the same way I described. For example the interface is defined here in the Framework abstraction:
.. and the implementation is in
Happy to discuss if there are other ideas, or if I am missing something here..
I'd suggest having the middleware execute both, before the call to the handler and after the result of the handler is handled (i.e. outgoing message is sent).
The latter would specially be useful for scenarios where we need to be certain that the other party has received the message.
Example: In ConnectionService, we mark the state as COMPLETE before the ack message is sent. Ideally we'd want that to be done after the message is send successfully.
We discussed & agreed in today's meeting that the middleware implementation will be runtime-agnostic. So that it may be consumed in Node.js Express, Deno or React-Native.
@TimoGlastra @ajile-in @gnarula Sir do we have any library currently available in AFJ, from which we can apply custom logic to all incoming messages like the IAgentMiddleware in .NET ?