Skip to content

feat: add PII detection middleware to community packages#47

Open
code-xpertai[bot] wants to merge 1 commit into
developfrom
feature/pii-middleware
Open

feat: add PII detection middleware to community packages#47
code-xpertai[bot] wants to merge 1 commit into
developfrom
feature/pii-middleware

Conversation

@code-xpertai

@code-xpertai code-xpertai Bot commented Feb 15, 2026

Copy link
Copy Markdown

Summary

Adds a PII (Personally Identifiable Information) detection and redaction middleware to the community packages. This middleware helps protect user privacy by detecting and redacting sensitive information in agent messages.

Features

  • Multiple built-in detectors: SSN (US Social Security Numbers), email addresses, phone numbers, and credit card numbers
  • Custom detector support: Users can provide their own detection logic for specialized PII patterns
  • Flexible redaction strategies: hash (base64 hash), mask (character replacement), remove (delete), replace (custom text)
  • Configurable options: Control masking characters, replacement text, and length preservation
  • Real-time processing: Redacts PII from both user input and model output
  • Privacy by design: Helps agents comply with privacy regulations and data protection standards

Implementation Details

  • Follows the same patterns as the existing tool-call-limit middleware
  • Uses @xpert-ai/plugin-sdk and @nestjs/common for integration
  • Supports both built-in and custom detector functions
  • Includes comprehensive documentation and usage examples

Usage Example

// Custom detector function (similar to LangChain example)
function detectSSN(content: string): PIIMatch[] {
  const matches: PIIMatch[] = [];
  const pattern = /\d{3}-\d{2}-\d{4}/g;
  let match: RegExpExecArray | null;

  while ((match = pattern.exec(content)) !== null) {
    const ssn = match[0];
    const firstThree = parseInt(ssn.substring(0, 3), 10);
    if (firstThree !== 0 && firstThree !== 666 && !(firstThree >= 900 && firstThree <= 999)) {
      matches.push({
        text: ssn,
        start: match.index ?? 0,
        end: (match.index ?? 0) + ssn.length,
      });
    }
  }
  return matches;
}

// In agent configuration
{
  "type": "PiiMiddleware",
  "options": {
    "detector": detectSSN,
    "strategy": "hash"
  }
}

Files Added

  • community/middlewares/pii/ - Complete PII middleware package
    • src/index.ts - Plugin entry point
    • src/lib/pii.ts - Core middleware implementation
    • src/lib/pii.module.ts - NestJS module
    • src/lib/types.ts - Type definitions
    • package.json - Package configuration
    • README.md - Comprehensive documentation
    • tsconfig.json - TypeScript configuration

Testing

  • TypeScript compilation successful
  • Follows existing middleware patterns
  • Ready for integration testing with Xpert AI agents

Dependencies

  • Peer dependencies match existing middleware patterns
  • No breaking changes to existing codebase
  • Compatible with current Xpert AI plugin system

- Create PII middleware package with built-in detectors for SSN, email, phone, credit card, and IP addresses
- Support custom detector functions with flexible configuration
- Implement multiple redaction strategies: hash, mask, remove, replace
- Add state tracking for PII detection counts
- Include comprehensive documentation and examples
- Follow tool-call-limit middleware architecture pattern
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants