redactionHooks function

RedactionHooks redactionHooks(
  1. SecretRedactor redactor
)

Builds the agent hooks that redact redactor's values:

  • afterToolCall masks TextContent blocks of every tool result before it enters the transcript (and therefore the session JSONL).
  • transformContext masks text in the message list sent to the provider (belt-and-braces: user messages, assistant text/thinking, and any tool result that bypassed afterToolCall).

Implementation

RedactionHooks redactionHooks(SecretRedactor redactor) {
  AfterToolCallResult? afterToolCall(
    AfterToolCallContext context,
    CancelToken? cancelToken,
  ) {
    var changed = false;
    final content = [
      for (final block in context.result.content)
        _redactBlock(redactor, block, onChange: () => changed = true),
    ];
    return changed ? AfterToolCallResult(content: content) : null;
  }

  List<Message> transformContext(
    List<Message> messages,
    CancelToken? cancelToken,
  ) {
    return [for (final message in messages) _redactMessage(redactor, message)];
  }

  return (afterToolCall: afterToolCall, transformContext: transformContext);
}