Skip to content

@PiiPolicy

The @PiiPolicy decorator (policies/*.pii-policy.ts) defines personal-data protection rules applied at the LLM and tool boundaries as well as across structured logs and traces. @PiiPolicy supports two forms: declarative policies (mapping built-in PiiKind / PiiGroup targets to PiiAction transformations like reversible HMAC tokens <email:7f3a91>) and custom detectors implementing PiiPolicyHandler.findMatches(text).

Complete @PiiPolicy examples (declarative and custom detector)

Section titled “Complete @PiiPolicy examples (declarative and custom detector)”

Every @PiiPolicy class name ends with PiiPolicy and its name option ends with -pii-policy:

Option Applies To Type Required Description
name Both string Yes Unique kebab-case identifier ending with -pii-policy (e.g., "customer-pii-policy").
description Declarative string Yes Explains which PII categories this policy governs.
rules Declarative readonly [PiiRule, ...] Yes Non-empty list of pii(PiiKind | PiiGroup, PiiAction.*) rules; specific PiiKind rules override broader PiiGroup rules.
detectWithModel Declarative string No Chat model used to detect unstructured PII (such as person names and street addresses) in free text.
pseudonymKey Declarative Secret No secret("PII_PSEUDONYM_KEY") used for PiiAction.replaceWithToken(); falls back to @Application({ pseudonymKey }).
action Custom Detector MaskingPiiAction Yes PiiAction.replaceWithToken(), replaceWithKind(), or replacePart() applied to every [start, end) match returned by findMatches().
deps Both readonly Token[] No Constructor dependencies injected into a PiiRefusal or PiiPolicyHandler class.
Section titled “What related entities work with @PiiPolicy?”
Related Entity Import Path Purpose
pii(target, action) "@agboss/core" Binds a PiiKind or PiiGroup to a PiiAction inside @PiiPolicy({ rules }).
PiiKind & PiiGroup "@agboss/core" Built-in PII categories (Email, Phone, CardNumber, Iban, IpAddress, PersonName, StreetAddress, Government, Financial, Contact).
PiiAction "@agboss/core" Action factories: replaceWithToken() (reversible HMAC <email:7f3a91> restored on tool calls), replaceWithKind(), replacePart({ keepLast, keepDomain }), and blockWithRefusal().
PiiPolicyHandler & PiiRefusal "@agboss/core" Interfaces implemented by custom range detectors (findMatches(text)) and localized refusal builders (refusal(kind)).
testPii(Policies, options?) "@agboss/core/testing" Isolated unit runner that tests masking, reversible token restoration, and refusal triggers offline.