Control and customize agent execution at every step
What is a Middleware
Middleware provides a way to hook the workflow execution and therefore also Agent and RAG, since they too are workflows.
The core Workflow execution involves calling nodes based on the events returned by other nodes. Middleware exposes hooks to step inside before and after the execution of nodes:
What can middleware do?
Control
Transform prompts, tool selection, and output formatting.
<?php
namespace App\Neuron\Middleware;
class CustomMiddleware implements WorkflowMiddleware
{
/**
* Execute before the node runs.
*/
public function before(NodeInterface $node, Event $event, WorkflowState $state): void
{
// ...
}
/**
* Execute after the node runs.
*/
public function after(NodeInterface $node, Event $result, WorkflowState $state): void
{
// ...
}
}
class MyWorkflow extends Workflow
{
/**
* Define the nodes middleware.
*/
protected function middleware(): array
{
return [
NodeOne::class => new CustomMiddleware(),
];
}
}
class MyWorkflow extends Workflow
{
/**
* Define the nodes middleware.
*/
protected function middleware(): array
{
return [
NodeOne::class => [
new CustomMiddleware(),
new AnotherMiddleware(),
]
];
}
}
class MyWorkflow extends Workflow
{
/**
* Define the global middleware.
*/
protected function globalMiddleware(): array
{
return [
new CustomMiddleware(),
];
}
}