@intflows/genkit-guard 0.0.8-alpha.1 → 0.0.9-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,120 +0,0 @@
1
- import { analyzeIntentStructured, detectInjection } from '../intent/intentAnalyzer.js';
2
- import { detectPII } from '../pii/detector.js';
3
- import { PiiTokenizer } from '../pii/tokenizer.js';
4
- export function guard(config) {
5
- return async (req, next) => {
6
- const input = req.prompt ||
7
- req.messages?.[req.messages.length - 1]?.content?.[0]?.text ||
8
- "";
9
- // -------------------------
10
- // 1. INTENT ANALYSIS
11
- // -------------------------
12
- const isInjection = await detectInjection(input);
13
- if (isInjection) {
14
- console.warn(`[Intent Guard] Prompt injection pattern detected in input`);
15
- return block("Prompt injection detected", {
16
- reason: "pattern_match"
17
- });
18
- }
19
- console.log(`[Intent Guard] Analyzing intent for user's input`);
20
- const intentResult = await analyzeIntentStructured(input, config.intent.semantic.intents, config.intent.semantic.threshold);
21
- console.log(`[Intent Guard] Detected intent: ${intentResult.intent} (score: ${intentResult.score.toFixed(2)})`);
22
- if (!intentResult.allowed) {
23
- console.warn(`[Intent Guard] Intent "${intentResult.intent}" not allowed ${intentResult.allowed}`);
24
- return block("Intent not allowed", {
25
- intent: intentResult.intent,
26
- score: intentResult.score
27
- });
28
- }
29
- // -------------------------
30
- // 2. PII DETECTION + MASKING
31
- // -------------------------
32
- const piiResponse = await detectPII(input, {
33
- model: config?.pii?.model,
34
- mode: config?.pii?.mode
35
- });
36
- const piiMatches = piiResponse?.matches || [];
37
- console.log(`[PII Guard] Detected PII: ${piiMatches.length} matches found` + (piiResponse.classifier ? ' (classifier output present)' : ''));
38
- const tokenizer = new PiiTokenizer(); // <-- SINGLE INSTANCE
39
- const piiResult = tokenizer.mask(input, piiMatches);
40
- console.log(`[PII Guard] Masked PII: ${piiResult.piiTypes.length} types found`);
41
- // Attach tokenizer so response can unmask
42
- req.metadata = {
43
- ...req.metadata,
44
- piiTokenizer: tokenizer,
45
- intent: intentResult.intent,
46
- score: intentResult.score,
47
- piiDetected: piiMatches.length > 0,
48
- piiTypes: piiResult.piiTypes,
49
- maskedInput: piiResult.maskedText,
50
- piiModel: config?.pii?.model,
51
- piiMode: config?.pii?.mode,
52
- piiClassifierOutput: piiResponse.classifier
53
- };
54
- // Replace input
55
- req.prompt = piiResult.maskedText;
56
- req.messages = [
57
- {
58
- role: 'user',
59
- content: [{ text: piiResult.maskedText }],
60
- },
61
- ];
62
- // -------------------------
63
- // 3. LLM CALL
64
- // -------------------------
65
- const res = await next(req);
66
- // -------------------------
67
- // 4. RESPONSE UNMASK
68
- // -------------------------
69
- console.log(`[PII Guard] Unmasking response if needed`);
70
- if (tokenizer) {
71
- /**
72
- * RECURSIVE TRANSFORMER
73
- * This will find every string in the Genkit response (no matter if it's in
74
- * candidates, message, custom, or output) and unmask it.
75
- */
76
- const transform = (obj) => {
77
- // 1. If it's a string, unmask it
78
- if (typeof obj === 'string') {
79
- return tokenizer.unmask(obj);
80
- }
81
- // 2. If it's an array, transform each element
82
- if (Array.isArray(obj)) {
83
- return obj.map(transform);
84
- }
85
- // 3. If it's an object, transform each value
86
- if (obj !== null && typeof obj === 'object') {
87
- // Note: We iterate keys and mutate the object directly
88
- // to ensure Genkit's internal references are updated.
89
- for (const key of Object.keys(obj)) {
90
- obj[key] = transform(obj[key]);
91
- }
92
- return obj;
93
- }
94
- // 4. Return as-is for numbers/booleans/null
95
- return obj;
96
- };
97
- // ----------------------------------------------------------------------
98
- // 5. Transform the entire response object in-place to unmask all strings
99
- // ----------------------------------------------------------------------
100
- transform(res);
101
- console.log("[PII Guard] Deep unmasking complete across all candidates and custom fields.");
102
- }
103
- // ---------------------------------------------------------
104
- // 6. Return the modified response with unmasked content
105
- // ---------------------------------------------------------
106
- return res;
107
- };
108
- }
109
- // Helper to create a blocked response
110
- function block(message, metadata) {
111
- return {
112
- finishReason: 'blocked',
113
- output: {
114
- type: "error",
115
- status: "BLOCKED",
116
- message
117
- },
118
- metadata
119
- };
120
- }
@@ -1,10 +0,0 @@
1
- export declare function detectPII(text: string, opts?: {
2
- model?: string;
3
- mode?: 'ner' | 'classifier';
4
- }): Promise<{
5
- matches: {
6
- type: string;
7
- value: string;
8
- }[];
9
- classifier: any;
10
- }>;
@@ -1,47 +0,0 @@
1
- import { ModelSingleton } from '../util/singleton.js';
2
- const REGEX_RULES = [
3
- // EMAIL (keep your existing one)
4
- { type: 'EMAIL', pattern: /\b[\w\.-]+@[\w\.-]+\.\w{2,}\b/gi },
5
- // AU MOBILE (04xx xxx xxx or +61 4xx xxx xxx)
6
- { type: 'AU_MOBILE', pattern: /\b(?:\+?61|0)4\d{2}[-\s]?\d{3}[-\s]?\d{3}\b/g },
7
- // AU LANDLINE (02, 03, 07, 08)
8
- { type: 'AU_LANDLINE', pattern: /\b(?:\+?61[-\s]?)?(?:2|3|7|8)\d{1}[-\s]?\d{4}[-\s]?\d{4}\b/g },
9
- // MEDICARE NUMBER (10 digits, often grouped 4-5-1)
10
- { type: 'MEDICARE', pattern: /\b\d{4}[-\s]?\d{5}[-\s]?\d\b/g },
11
- // TFN (9 digits)
12
- { type: 'TFN', pattern: /\b\d{3}[-\s]?\d{3}[-\s]?\d{3}\b/g },
13
- // ABN (11 digits)
14
- { type: 'ABN', pattern: /\b\d{2}[-\s]?\d{3}[-\s]?\d{3}[-\s]?\d{3}\b/g },
15
- // CREDIT CARD (keep your existing one if needed)
16
- { type: 'CREDIT_CARD', pattern: /\b(?:\d[ -]*?){13,16}\b/g }
17
- ];
18
- export async function detectPII(text, opts) {
19
- const mode = opts?.mode ?? 'ner';
20
- const model = opts?.model;
21
- const results = [];
22
- // ---- REGEX (always run) ----
23
- for (const rule of REGEX_RULES) {
24
- const matches = text.match(rule.pattern) || [];
25
- matches.forEach(m => results.push({ type: rule.type, value: m }));
26
- }
27
- // ---- NER ----
28
- let classifierOutput = undefined;
29
- if (mode === 'ner') {
30
- const ner = await ModelSingleton.getNER(model);
31
- const entities = await ner(text);
32
- for (const e of entities) {
33
- if (e.entity && e.entity.includes('PER')) {
34
- results.push({ type: 'NAME', value: (e.word || '').replace(/##/g, '') });
35
- }
36
- }
37
- }
38
- else {
39
- // classifier mode: we call the classifier and return its output alongside regex matches.
40
- const cls = await ModelSingleton.getPIIClassifier(model);
41
- classifierOutput = await cls(text);
42
- }
43
- return {
44
- matches: results,
45
- classifier: classifierOutput
46
- };
47
- }
@@ -1,19 +0,0 @@
1
- export type PiiResult = {
2
- maskedText: string;
3
- pii: Record<string, string>;
4
- piiTypes: string[];
5
- };
6
- export declare class PiiTokenizer {
7
- private vault;
8
- private counter;
9
- private piiTypes;
10
- private createToken;
11
- mask(text: string, matches: {
12
- type: string;
13
- value: string;
14
- }[]): PiiResult;
15
- unmask(text: string): string;
16
- getVault(): {
17
- [k: string]: string;
18
- };
19
- }
@@ -1,32 +0,0 @@
1
- export class PiiTokenizer {
2
- vault = new Map();
3
- counter = 0;
4
- piiTypes = new Set();
5
- createToken(type) {
6
- return `[[${type}_${this.counter++}]]`;
7
- }
8
- mask(text, matches) {
9
- let masked = text;
10
- for (const match of matches) {
11
- const token = this.createToken(match.type);
12
- this.vault.set(token, match.value);
13
- this.piiTypes.add(match.type.toLowerCase());
14
- masked = masked.split(match.value).join(token);
15
- }
16
- return {
17
- maskedText: masked,
18
- pii: Object.fromEntries(this.vault),
19
- piiTypes: Array.from(this.piiTypes)
20
- };
21
- }
22
- unmask(text) {
23
- let result = text;
24
- this.vault.forEach((value, token) => {
25
- result = result.split(token).join(value); // Global replacement
26
- });
27
- return result;
28
- }
29
- getVault() {
30
- return Object.fromEntries(this.vault);
31
- }
32
- }
@@ -1,14 +0,0 @@
1
- export declare class ModelSingleton {
2
- private static extractors;
3
- private static nerClassifiers;
4
- private static textClassifiers;
5
- static init(): void;
6
- static getExtractor(modelName?: string): Promise<any>;
7
- static getNER(modelName?: string): Promise<any>;
8
- static getPIIClassifier(modelName?: string): Promise<any>;
9
- static preload(models?: {
10
- extractor?: string;
11
- ner?: string;
12
- pii?: string;
13
- }): Promise<void>;
14
- }
@@ -1,60 +0,0 @@
1
- import { pipeline, env } from '@huggingface/transformers';
2
- import path from 'path';
3
- import fs from 'fs';
4
- import { fileURLToPath } from 'url';
5
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
6
- env.allowRemoteModels = false;
7
- env.localModelPath = path.join(__dirname, '../../models');
8
- export class ModelSingleton {
9
- static extractors = new Map();
10
- static nerClassifiers = new Map();
11
- static textClassifiers = new Map();
12
- static init() {
13
- // Always resolve model path relative to the client app, not the library
14
- const projectRoot = process.cwd();
15
- const modelPath = path.join(projectRoot, "models");
16
- // Ensure directory exists
17
- if (!fs.existsSync(modelPath)) {
18
- fs.mkdirSync(modelPath, { recursive: true });
19
- }
20
- env.cacheDir = modelPath;
21
- env.localModelPath = modelPath;
22
- // Allow remote download if missing
23
- env.allowRemoteModels = true;
24
- console.log("[Guard] Using model directory:", modelPath);
25
- }
26
- static async getExtractor(modelName = 'Xenova/all-MiniLM-L6-v2') {
27
- if (!this.extractors.has(modelName)) {
28
- this.init();
29
- const inst = await pipeline('feature-extraction', modelName);
30
- this.extractors.set(modelName, inst);
31
- }
32
- return this.extractors.get(modelName);
33
- }
34
- static async getNER(modelName = 'Xenova/bert-base-NER') {
35
- if (!this.nerClassifiers.has(modelName)) {
36
- this.init();
37
- const inst = await pipeline('token-classification', modelName);
38
- this.nerClassifiers.set(modelName, inst);
39
- }
40
- return this.nerClassifiers.get(modelName);
41
- }
42
- static async getPIIClassifier(modelName = 'openai/privacy-filter') {
43
- if (!this.textClassifiers.has(modelName)) {
44
- this.init();
45
- const inst = await pipeline('text-classification', modelName);
46
- this.textClassifiers.set(modelName, inst);
47
- }
48
- return this.textClassifiers.get(modelName);
49
- }
50
- static async preload(models) {
51
- const tasks = [];
52
- if (models?.extractor)
53
- tasks.push(this.getExtractor(models.extractor));
54
- if (models?.ner)
55
- tasks.push(this.getNER(models.ner));
56
- if (models?.pii)
57
- tasks.push(this.getPIIClassifier(models.pii));
58
- await Promise.all(tasks);
59
- }
60
- }