credo-ts icon indicating copy to clipboard operation
credo-ts copied to clipboard

add agent middleware

Open TimoGlastra opened this issue 5 years ago • 4 comments

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 AgentMiddleware interface
  • [ ] call middleware before/after passing to handler (see question below)
    • I think Dispatcher would be the best place to call middleware
  • [ ] 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.

TimoGlastra avatar Sep 06 '20 11:09 TimoGlastra

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..

ajile-in avatar Sep 08 '20 22:09 ajile-in

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.

gnarula avatar Sep 11 '20 08:09 gnarula

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.

ajile-in avatar Sep 11 '20 14:09 ajile-in

@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 ?

bilal12367 avatar Mar 29 '23 12:03 bilal12367