Model Invocation Lifecycle
onAiMessage
The onAiMessage event fires after the model has finished streaming its response and Fragola has fully assembled the assistant message (including text and tool calls).
Usage
This event fires before the message is appended to the agent's internal history.
It is useful for:
- Redacting sensitive information generated by the AI before it is stored or passed to tools.
- Logging the final generated output.
Payload Details
agent.onAiMessage(({ message, context }) => {
// Intercept and mutate the assistant's message
if (message.content && typeof message.content === 'string') {
message.content = message.content.replace(/CONFIDENTIAL/g, "***");
}
return message;
});