Questa pagina non è ancora tradotta in italiano: qui la trovi in inglese.
Recipes
Give the agent procedures on your pages, approved outside your code: sources, steps, rows of a grid, limits, and how each recipe is governed.
In questa pagina
Use this page to give the agent procedures on your pages: fill a form and save it, find a row in a list and open it. A recipe is a sequence of steps on one page that the chat widget runs in the user's browser. The model sees it as one of the UI actions of that page, with a name, a description and parameters. Your code does not hold the recipes. A source that you register gives them to MentorAgent, for example from a console where someone approves them.
When to choose it
Section titled “When to choose it”- Choose it when the procedure already exists in your pages, and you want the agent to run it for the user without a change to your code.
- Use an action instead when the work can run on the server: an action calls your services directly, with no page in between.
- Use a UI action of the page when the procedure belongs to the code of the page: the page registers it with
RegisterUIAction.
A recipe runs only on its own page, while the user has that page open. The model does not open the page to run the recipe. In 1.5.0, navigate_to opens only the pages that carry [MentorPage], so a recipe on a page that only the AutoMentor knows runs only when the user is already there. Recipes do not use the manifest of a scan or the list of pages the agent knows: a recipe has its own selectors.
Who approves a recipe, and who runs it
Section titled “Who approves a recipe, and who runs it”A recipe clicks what the user could click, with the user's rights, in the user's browser. Three parts of your application decide about it:
| Who | Decides | Where |
|---|---|---|
| Your source | Which recipes exist, and whether each one was approved: a signature, an approval in a console | IMentorRecipeSource, below |
| Your action policies | Whether the recipe is offered and whether a call runs, at every turn | The gate, as for every UI action |
| The user | Each run of a recipe that asks for a confirmation | The confirmation box of the widget |
Write a source
Section titled “Write a source”A source gives the recipes of one session. IApprovedRecipes is an interface of your own, which reads the approved recipes where you keep them, for example in a database or a console. ApprovedRecipeSource is the source, and implements IMentorRecipeSource:
// Where the approved recipes are kept: a console, a database, a signed file
public interface IApprovedRecipes
{
Task<IReadOnlyList<MentorRecipeDefinition>> ForSessionAsync(
string? sessionId, CancellationToken ct);
}public sealed class ApprovedRecipeSource(IApprovedRecipes approved)
: IMentorRecipeSource
{
// Asked once, when a session begins: the session keeps what it got
public async Task<MentorRecipeSourceResult> LoadAsync(
MentorRecipeSourceContext context, CancellationToken ct = default)
=> new(await approved.ForSessionAsync(context.SessionId, ct), [])
{
// A UI action of the page with the same name wins
Precedence = MentorRecipePrecedence.Code,
};
}Register the source in Program.cs, next to AddMentorAgent. The second line registers an observer, which is optional: it tells you when a recipe is left out, as shown below:
services.AddMentorRecipeSource<ApprovedRecipeSource>();
services.AddMentorAgentSourceObserver<RecipeAlerts>();When a session begins, it asks every source once, in the order of registration, and keeps the recipes that it got. An open conversation does not change when a source changes: the next session sees the change.
Write a recipe
Section titled “Write a recipe”A recipe is a MentorRecipeDefinition, which your source reads from where the recipe is approved. To show its fields, this example creates one in code. It fills the form of a new invoice and saves it, for users with the role Billing, after the user confirms:
// Fills the form of a new invoice and saves it
public static MentorRecipeDefinition CreateInvoice() => new()
{
Name = "create_invoice",
Description = "Creates an invoice for a customer",
Route = "/invoices/new",
Parameters =
[
new() { Name = "customer", Description = "The customer's name" },
new()
{
Name = "amount",
Description = "The amount, in euros",
Type = MentorRecipeParameterType.Number,
},
],
Steps =
[
new() { Kind = MentorRecipeStepKind.Set,
Selector = "#customer", Value = "{customer}" },
new() { Kind = MentorRecipeStepKind.Set,
Selector = "#amount", Value = "{amount}" },
new() { Kind = MentorRecipeStepKind.Click, Selector = "#save" },
],
Roles = ["Billing"],
RequiresConfirmation = true,
};- Name and route. A recipe is known by its route and its name, so two pages may each have a recipe of the same name. The route is written as the router writes it, such as
/invoices/{id:int}, with no query and no fragment. - What the model reads. The model reads the name, the description and the parameters of the recipe, with their types and descriptions. It never reads a selector, or a value that the recipe writes itself.
- Parameters. The value of a step is either a fixed text, or one parameter written as
{name}. The parameter must be the whole value. To write a brace in a fixed text, write it twice. MentorAgent checks the value that the model gives against the type of the parameter (text, number, or true or false) before anything runs. - Roles and confirmation. A user who has none of the
Rolesis refused, and nothing runs. WithRequiresConfirmation, the recipe waits for the user's yes, and the confirmation box shows the description of the recipe and the values that the model gave. MentorAgent checks the roles again after the yes.
The steps
Section titled “The steps”| Kind | What it does |
|---|---|
Set | Writes the value into a field, as typing would, so that @bind, React's controlled fields and Angular's forms receive it |
Choose | Chooses the option whose label is the value, in a select or an ARIA combobox |
Check | Checks a check box or a radio button, or unchecks it with the value false |
Click | Clicks the element |
Wait | Waits for the element to appear, up to 10 seconds |
Row | Finds a row by the text of its key cell, then clicks inside it |
Between two steps, the runner waits until the page stops changing, for 5 seconds at most. The browser answers that the recipe was done, or which step was not done and why. It never sends a value of the page.
A row of a table or a grid
Section titled “A row of a table or a grid”A Row step finds the row whose key cell reads the value that the model gave. The comparison happens in the browser, so no text of a row leaves it:
// Finds an invoice in the list by its number, and opens it
public static MentorRecipeDefinition OpenInvoice() => new()
{
Name = "open_invoice",
Description = "Opens an invoice from the list, by its number",
Route = "/invoices",
Parameters =
[
new() { Name = "number", Description = "The invoice number" },
],
Steps =
[
new()
{
Kind = MentorRecipeStepKind.Row,
Selector = "#invoices tbody tr", // the rows
Cell = "td:nth-child(1)", // the key cell of a row
Value = "{number}", // the key to find
Filter = "#invoice-filter", // the page's own filter
Target = "a.open", // what to click in the row
},
],
};- The page's filter first. With
Filter, a row that is not in the page is searched with the page's own filter field. The step writes the key there, and waits up to 5 seconds for the page to answer, also when the page filters on its server. - A grid that renders as it scrolls. If the row is still missing, the step scrolls the rows' container from the top, a page at a time, so that a virtualized grid, such as a QuickGrid with
Virtualize, renders the rows further down. Give the grid's table a class or an id of your own for the selector, for example with theClassparameter of QuickGrid. - Then it clicks
Targetinside the row. WithoutTarget, the row is only brought into view. - Rows left out. Nothing inside
data-mentor-excludeis read. A key that only excluded rows hold is not found.
If no row has the key after the last row, the step is not done. Its reason is no-row, and the model reads it.
Limits
Section titled “Limits”MentorAgent checks each recipe of a source against MentorRecipeLimits:
| Limit | Value |
|---|---|
| Name of a recipe or a parameter | 1 to 64 letters, digits, _ and - |
| Steps | 1 to 50 |
| Parameters | 10 at most, each with a name of its own |
| Roles | 20 at most, of 256 characters each |
| Description | 1,024 characters |
| Selector, cell, target, filter, value | 1,024 characters each |
| Route | 256 characters |
The steps that the browser receives, with the model's values in them, are 16 KB at most. If the model's values make them larger, the model reads an error, and nothing reaches the browser.
When a recipe is left out
Section titled “When a recipe is left out”MentorAgent leaves out a recipe that breaks a rule, and always says why, as the rule recipe.rejected-with-reason asks. It writes a warning in the log, and sends a report to every IMentorAgentSourceObserver. The report comes every time a session asks a source, also when the source gave nothing:
// Tells whoever approves the recipes which ones were left out, and why
public sealed class RecipeAlerts(ILogger<RecipeAlerts> logger)
: IMentorAgentSourceObserver
{
public void OnAgentsLoaded(MentorAgentSourceReport report) { }
// Called before the session's first answer: do not wait here
public void OnRecipesLoaded(MentorRecipeSourceReport report)
{
foreach (var rejected in report.Rejected)
logger.LogWarning("Recipe {Route} {Name} left out: {Reason}",
rejected.Route, rejected.Name, rejected.ReasonCode);
}
}| Code | Why |
|---|---|
invalid-name | The name is not 1 to 64 letters, digits, _ and - |
invalid-definition | A description, a selector, a row's key cell or key, or a parameter's description is missing |
invalid-route | The route does not start with /, or has a query or a fragment |
invalid-steps | No step, more than 50, or a kind that MentorAgent does not know |
invalid-parameters | More than 10 parameters, two with one name, a name that is not valid, or a type that is not known |
unknown-parameter | A step names, between braces, something that is not one of the recipe's parameters |
invalid-value | A step's text has a single brace |
too-long | A field is longer than its limit, or there are too many roles |
duplicate | A recipe of the same route and name was given before, by this source or another: the first stays |
source-failed | The whole source threw. The report has the exception's type only, and the other sources are still asked |
A source can also leave out recipes itself, in MentorRecipeSourceResult.Rejected, with these codes or with codes of its own.
A recipe and a UI action with the same name
Section titled “A recipe and a UI action with the same name”When a recipe has the name of a UI action that the page registers in code, the source says which one the model gets, with Precedence:
MentorRecipePrecedence.Code, the default: the page's UI action is offered, and the recipe is not.MentorRecipePrecedence.Source: the recipe is offered, and the UI action is not.
Any other tool always has priority over a recipe: an action of your application, a tool of a Model Context Protocol (MCP) server, or one of MentorAgent's own tools. The model is never offered two tools with one name, and the log says once for the session which one lost.
When a step does not find its element
Section titled “When a step does not find its element”A step stops the recipe when its element:
- is not in the page;
- is hidden or disabled;
- is inside
data-mentor-exclude; - is not the kind of element that the step acts on;
- has no option with the label, for a
Choosestep.
The runner never looks for a similar element. Then:
- the model reads that the recipe was not done, at which step and why;
- the recipe is turned off, in memory until the application restarts, and is no longer offered;
- your action observers receive
Failedwithelement-missing, and the step and the reason inDetail; - the log says so once, with the recipe, the step and the reason, and no value.
The recipe stays off only in that version. A recipe with the same route and name is a new version when anything changes: a step, a selector, a parameter, a role, the confirmation or the description. A new version is on. Correct the recipe where you approve it, and the next sessions get the new version.
Govern a recipe
Section titled “Govern a recipe”A recipe is governed as a UI action of its page, so your action policies decide about it like any other action:
- they are asked when the recipe is offered and when it is called, with
KindUI, the page's name and the recipe's name, its roles and its confirmation; - a policy that does not offer it keeps it out of the turn, and a call to it is still refused with
not-offered; - a policy that refuses the call stops it with
not-authorized, and nothing runs; - a confirmation that a policy adds makes it wait for the user's yes.
A policy that refuses every action, such as one that turns the AI off, stops every recipe: none is offered and none runs.
Where a recipe runs
Section titled “Where a recipe runs”- Blazor Server and .NET MAUI Blazor Hybrid. The steps run through the JavaScript runtime of the circuit, or of the app's web view.
- Blazor WebAssembly. Before each message, the client of
Novior.MentorAgent.Blazorsends the path of the page shown, without its query or fragment, and the hub offers the recipes of that page. A client that sends no path is offered none.- The hub asks the client to run a recipe with the event
RecipeRequested, and the client answers withPOST /mentor/recipe-result. Only the user who owns the connection may answer. - A browser that does not answer within 2 minutes ends the call with an error, and the recipe stays on.
- The hub asks the client to run a recipe with the event
- React, Angular and other front ends.
Novior.MentorAgent.Serverserves/mentor/agent.js, a module with the same runner, which a front end ties to its own hub connection: see Run recipes in your front end.
In every case, the model waits for the browser's answer before it goes on.
The rules
Section titled “The rules”The rules of recipes are the recipe area of ACTION-RULES.md in the MentorAgent repository (external site). Each rule has a name that never changes, and a test in the library.
Vedi anche
Sezione “Vedi anche”- Ruoli, conferma e governance — Ruoli e approvazione human-in-the-loop per i tool di un agente AI in .NET: controlli nel codice, non nel prompt, su ogni canale, e cosa resta a te.
- Action policies and observers — The types that let your code decide on each action, record every decision of the gate, know who confirmed a call, and list the actions.
- Pages the agent knows — With the AutoMentor on, MentorAgent knows the pages of your Blazor app from their routes, and what a scan or your users saw of each one.
- MentorAgent 1.5.0 release notes — What's new in MentorAgent 1.5.0: pages known from your routes, a scan of the pages, recipes, forms and grids as UI actions, and the cost per agent and user.
