@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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { guard } from './middleware/middleware.js';
1
+ export { guard, guardAction, guardMiddleware, guardPlugin } from './middleware/middleware.js';
2
2
  export * from './core/types.js';
3
3
  /**
4
4
  * Pre-load the model to avoid cold-start delay on first user request.
package/dist/index.js CHANGED
@@ -1,12 +1,30 @@
1
1
  import { ModelSingleton } from './util/singleton.js';
2
2
  // export { intentGuard, piiGuard } from './middleware/middleware.js';
3
- export { guard } from './middleware/middleware.js';
3
+ export { guard, guardAction, guardMiddleware, guardPlugin } from './middleware/middleware.js';
4
4
  export * from './core/types.js';
5
+ function logGuardEvent(eventName, body, attributes = {}) {
6
+ console.log(JSON.stringify({
7
+ timestamp: new Date().toISOString(),
8
+ severityText: 'INFO',
9
+ severityNumber: 9,
10
+ body,
11
+ resource: {
12
+ attributes: {
13
+ 'service.name': '@intflows/genkit-guard',
14
+ },
15
+ },
16
+ attributes: {
17
+ 'event.name': eventName,
18
+ 'code.namespace': 'genkit-guard',
19
+ ...attributes,
20
+ },
21
+ }));
22
+ }
5
23
  /**
6
24
  * Pre-load the model to avoid cold-start delay on first user request.
7
25
  */
8
26
  export async function initGuard(config) {
9
- console.log('[Guard] Loading local models...');
27
+ logGuardEvent('guard.models.loading', 'Loading local guard models');
10
28
  const extractorModel = config?.models?.extractor ?? 'Xenova/all-MiniLM-L6-v2';
11
29
  const piiModel = config?.pii?.model;
12
30
  const piiMode = config?.pii?.mode ?? 'ner';
@@ -18,5 +36,7 @@ export async function initGuard(config) {
18
36
  tasks.push(ModelSingleton.getPIIClassifier(piiModel ?? 'openai/privacy-filter'));
19
37
  }
20
38
  await Promise.all(tasks);
21
- console.log('[Guard] Models loaded');
39
+ logGuardEvent('guard.models.loaded', 'Local guard models loaded', {
40
+ piiMode,
41
+ });
22
42
  }
@@ -1 +1,380 @@
1
- export declare function guard(config: any): (req: any, next: any) => Promise<any>;
1
+ import { z } from 'genkit';
2
+ declare const guardConfigSchema: z.ZodObject<{
3
+ intent: z.ZodOptional<z.ZodObject<{
4
+ mode: z.ZodOptional<z.ZodString>;
5
+ allowedIntent: z.ZodOptional<z.ZodString>;
6
+ semantic: z.ZodObject<{
7
+ threshold: z.ZodOptional<z.ZodNumber>;
8
+ intents: z.ZodRecord<z.ZodString, z.ZodString>;
9
+ }, "strip", z.ZodTypeAny, {
10
+ intents: Record<string, string>;
11
+ threshold?: number | undefined;
12
+ }, {
13
+ intents: Record<string, string>;
14
+ threshold?: number | undefined;
15
+ }>;
16
+ }, "strip", z.ZodTypeAny, {
17
+ semantic: {
18
+ intents: Record<string, string>;
19
+ threshold?: number | undefined;
20
+ };
21
+ mode?: string | undefined;
22
+ allowedIntent?: string | undefined;
23
+ }, {
24
+ semantic: {
25
+ intents: Record<string, string>;
26
+ threshold?: number | undefined;
27
+ };
28
+ mode?: string | undefined;
29
+ allowedIntent?: string | undefined;
30
+ }>>;
31
+ pii: z.ZodOptional<z.ZodObject<{
32
+ reversible: z.ZodOptional<z.ZodBoolean>;
33
+ model: z.ZodOptional<z.ZodString>;
34
+ mode: z.ZodOptional<z.ZodEnum<["ner", "classifier"]>>;
35
+ }, "strip", z.ZodTypeAny, {
36
+ mode?: "ner" | "classifier" | undefined;
37
+ reversible?: boolean | undefined;
38
+ model?: string | undefined;
39
+ }, {
40
+ mode?: "ner" | "classifier" | undefined;
41
+ reversible?: boolean | undefined;
42
+ model?: string | undefined;
43
+ }>>;
44
+ logging: z.ZodOptional<z.ZodObject<{
45
+ enabled: z.ZodOptional<z.ZodBoolean>;
46
+ level: z.ZodOptional<z.ZodEnum<["debug", "info", "warn", "error"]>>;
47
+ serviceName: z.ZodOptional<z.ZodString>;
48
+ }, "strip", z.ZodTypeAny, {
49
+ enabled?: boolean | undefined;
50
+ level?: "debug" | "info" | "warn" | "error" | undefined;
51
+ serviceName?: string | undefined;
52
+ }, {
53
+ enabled?: boolean | undefined;
54
+ level?: "debug" | "info" | "warn" | "error" | undefined;
55
+ serviceName?: string | undefined;
56
+ }>>;
57
+ models: z.ZodOptional<z.ZodObject<{
58
+ extractor: z.ZodOptional<z.ZodString>;
59
+ }, "strip", z.ZodTypeAny, {
60
+ extractor?: string | undefined;
61
+ }, {
62
+ extractor?: string | undefined;
63
+ }>>;
64
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
65
+ intent: z.ZodOptional<z.ZodObject<{
66
+ mode: z.ZodOptional<z.ZodString>;
67
+ allowedIntent: z.ZodOptional<z.ZodString>;
68
+ semantic: z.ZodObject<{
69
+ threshold: z.ZodOptional<z.ZodNumber>;
70
+ intents: z.ZodRecord<z.ZodString, z.ZodString>;
71
+ }, "strip", z.ZodTypeAny, {
72
+ intents: Record<string, string>;
73
+ threshold?: number | undefined;
74
+ }, {
75
+ intents: Record<string, string>;
76
+ threshold?: number | undefined;
77
+ }>;
78
+ }, "strip", z.ZodTypeAny, {
79
+ semantic: {
80
+ intents: Record<string, string>;
81
+ threshold?: number | undefined;
82
+ };
83
+ mode?: string | undefined;
84
+ allowedIntent?: string | undefined;
85
+ }, {
86
+ semantic: {
87
+ intents: Record<string, string>;
88
+ threshold?: number | undefined;
89
+ };
90
+ mode?: string | undefined;
91
+ allowedIntent?: string | undefined;
92
+ }>>;
93
+ pii: z.ZodOptional<z.ZodObject<{
94
+ reversible: z.ZodOptional<z.ZodBoolean>;
95
+ model: z.ZodOptional<z.ZodString>;
96
+ mode: z.ZodOptional<z.ZodEnum<["ner", "classifier"]>>;
97
+ }, "strip", z.ZodTypeAny, {
98
+ mode?: "ner" | "classifier" | undefined;
99
+ reversible?: boolean | undefined;
100
+ model?: string | undefined;
101
+ }, {
102
+ mode?: "ner" | "classifier" | undefined;
103
+ reversible?: boolean | undefined;
104
+ model?: string | undefined;
105
+ }>>;
106
+ logging: z.ZodOptional<z.ZodObject<{
107
+ enabled: z.ZodOptional<z.ZodBoolean>;
108
+ level: z.ZodOptional<z.ZodEnum<["debug", "info", "warn", "error"]>>;
109
+ serviceName: z.ZodOptional<z.ZodString>;
110
+ }, "strip", z.ZodTypeAny, {
111
+ enabled?: boolean | undefined;
112
+ level?: "debug" | "info" | "warn" | "error" | undefined;
113
+ serviceName?: string | undefined;
114
+ }, {
115
+ enabled?: boolean | undefined;
116
+ level?: "debug" | "info" | "warn" | "error" | undefined;
117
+ serviceName?: string | undefined;
118
+ }>>;
119
+ models: z.ZodOptional<z.ZodObject<{
120
+ extractor: z.ZodOptional<z.ZodString>;
121
+ }, "strip", z.ZodTypeAny, {
122
+ extractor?: string | undefined;
123
+ }, {
124
+ extractor?: string | undefined;
125
+ }>>;
126
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
127
+ intent: z.ZodOptional<z.ZodObject<{
128
+ mode: z.ZodOptional<z.ZodString>;
129
+ allowedIntent: z.ZodOptional<z.ZodString>;
130
+ semantic: z.ZodObject<{
131
+ threshold: z.ZodOptional<z.ZodNumber>;
132
+ intents: z.ZodRecord<z.ZodString, z.ZodString>;
133
+ }, "strip", z.ZodTypeAny, {
134
+ intents: Record<string, string>;
135
+ threshold?: number | undefined;
136
+ }, {
137
+ intents: Record<string, string>;
138
+ threshold?: number | undefined;
139
+ }>;
140
+ }, "strip", z.ZodTypeAny, {
141
+ semantic: {
142
+ intents: Record<string, string>;
143
+ threshold?: number | undefined;
144
+ };
145
+ mode?: string | undefined;
146
+ allowedIntent?: string | undefined;
147
+ }, {
148
+ semantic: {
149
+ intents: Record<string, string>;
150
+ threshold?: number | undefined;
151
+ };
152
+ mode?: string | undefined;
153
+ allowedIntent?: string | undefined;
154
+ }>>;
155
+ pii: z.ZodOptional<z.ZodObject<{
156
+ reversible: z.ZodOptional<z.ZodBoolean>;
157
+ model: z.ZodOptional<z.ZodString>;
158
+ mode: z.ZodOptional<z.ZodEnum<["ner", "classifier"]>>;
159
+ }, "strip", z.ZodTypeAny, {
160
+ mode?: "ner" | "classifier" | undefined;
161
+ reversible?: boolean | undefined;
162
+ model?: string | undefined;
163
+ }, {
164
+ mode?: "ner" | "classifier" | undefined;
165
+ reversible?: boolean | undefined;
166
+ model?: string | undefined;
167
+ }>>;
168
+ logging: z.ZodOptional<z.ZodObject<{
169
+ enabled: z.ZodOptional<z.ZodBoolean>;
170
+ level: z.ZodOptional<z.ZodEnum<["debug", "info", "warn", "error"]>>;
171
+ serviceName: z.ZodOptional<z.ZodString>;
172
+ }, "strip", z.ZodTypeAny, {
173
+ enabled?: boolean | undefined;
174
+ level?: "debug" | "info" | "warn" | "error" | undefined;
175
+ serviceName?: string | undefined;
176
+ }, {
177
+ enabled?: boolean | undefined;
178
+ level?: "debug" | "info" | "warn" | "error" | undefined;
179
+ serviceName?: string | undefined;
180
+ }>>;
181
+ models: z.ZodOptional<z.ZodObject<{
182
+ extractor: z.ZodOptional<z.ZodString>;
183
+ }, "strip", z.ZodTypeAny, {
184
+ extractor?: string | undefined;
185
+ }, {
186
+ extractor?: string | undefined;
187
+ }>>;
188
+ }, z.ZodTypeAny, "passthrough">>;
189
+ type GuardConfig = z.infer<typeof guardConfigSchema>;
190
+ export declare const guardMiddleware: import("genkit").GenerateMiddleware<z.ZodObject<{
191
+ intent: z.ZodOptional<z.ZodObject<{
192
+ mode: z.ZodOptional<z.ZodString>;
193
+ allowedIntent: z.ZodOptional<z.ZodString>;
194
+ semantic: z.ZodObject<{
195
+ threshold: z.ZodOptional<z.ZodNumber>;
196
+ intents: z.ZodRecord<z.ZodString, z.ZodString>;
197
+ }, "strip", z.ZodTypeAny, {
198
+ intents: Record<string, string>;
199
+ threshold?: number | undefined;
200
+ }, {
201
+ intents: Record<string, string>;
202
+ threshold?: number | undefined;
203
+ }>;
204
+ }, "strip", z.ZodTypeAny, {
205
+ semantic: {
206
+ intents: Record<string, string>;
207
+ threshold?: number | undefined;
208
+ };
209
+ mode?: string | undefined;
210
+ allowedIntent?: string | undefined;
211
+ }, {
212
+ semantic: {
213
+ intents: Record<string, string>;
214
+ threshold?: number | undefined;
215
+ };
216
+ mode?: string | undefined;
217
+ allowedIntent?: string | undefined;
218
+ }>>;
219
+ pii: z.ZodOptional<z.ZodObject<{
220
+ reversible: z.ZodOptional<z.ZodBoolean>;
221
+ model: z.ZodOptional<z.ZodString>;
222
+ mode: z.ZodOptional<z.ZodEnum<["ner", "classifier"]>>;
223
+ }, "strip", z.ZodTypeAny, {
224
+ mode?: "ner" | "classifier" | undefined;
225
+ reversible?: boolean | undefined;
226
+ model?: string | undefined;
227
+ }, {
228
+ mode?: "ner" | "classifier" | undefined;
229
+ reversible?: boolean | undefined;
230
+ model?: string | undefined;
231
+ }>>;
232
+ logging: z.ZodOptional<z.ZodObject<{
233
+ enabled: z.ZodOptional<z.ZodBoolean>;
234
+ level: z.ZodOptional<z.ZodEnum<["debug", "info", "warn", "error"]>>;
235
+ serviceName: z.ZodOptional<z.ZodString>;
236
+ }, "strip", z.ZodTypeAny, {
237
+ enabled?: boolean | undefined;
238
+ level?: "debug" | "info" | "warn" | "error" | undefined;
239
+ serviceName?: string | undefined;
240
+ }, {
241
+ enabled?: boolean | undefined;
242
+ level?: "debug" | "info" | "warn" | "error" | undefined;
243
+ serviceName?: string | undefined;
244
+ }>>;
245
+ models: z.ZodOptional<z.ZodObject<{
246
+ extractor: z.ZodOptional<z.ZodString>;
247
+ }, "strip", z.ZodTypeAny, {
248
+ extractor?: string | undefined;
249
+ }, {
250
+ extractor?: string | undefined;
251
+ }>>;
252
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
253
+ intent: z.ZodOptional<z.ZodObject<{
254
+ mode: z.ZodOptional<z.ZodString>;
255
+ allowedIntent: z.ZodOptional<z.ZodString>;
256
+ semantic: z.ZodObject<{
257
+ threshold: z.ZodOptional<z.ZodNumber>;
258
+ intents: z.ZodRecord<z.ZodString, z.ZodString>;
259
+ }, "strip", z.ZodTypeAny, {
260
+ intents: Record<string, string>;
261
+ threshold?: number | undefined;
262
+ }, {
263
+ intents: Record<string, string>;
264
+ threshold?: number | undefined;
265
+ }>;
266
+ }, "strip", z.ZodTypeAny, {
267
+ semantic: {
268
+ intents: Record<string, string>;
269
+ threshold?: number | undefined;
270
+ };
271
+ mode?: string | undefined;
272
+ allowedIntent?: string | undefined;
273
+ }, {
274
+ semantic: {
275
+ intents: Record<string, string>;
276
+ threshold?: number | undefined;
277
+ };
278
+ mode?: string | undefined;
279
+ allowedIntent?: string | undefined;
280
+ }>>;
281
+ pii: z.ZodOptional<z.ZodObject<{
282
+ reversible: z.ZodOptional<z.ZodBoolean>;
283
+ model: z.ZodOptional<z.ZodString>;
284
+ mode: z.ZodOptional<z.ZodEnum<["ner", "classifier"]>>;
285
+ }, "strip", z.ZodTypeAny, {
286
+ mode?: "ner" | "classifier" | undefined;
287
+ reversible?: boolean | undefined;
288
+ model?: string | undefined;
289
+ }, {
290
+ mode?: "ner" | "classifier" | undefined;
291
+ reversible?: boolean | undefined;
292
+ model?: string | undefined;
293
+ }>>;
294
+ logging: z.ZodOptional<z.ZodObject<{
295
+ enabled: z.ZodOptional<z.ZodBoolean>;
296
+ level: z.ZodOptional<z.ZodEnum<["debug", "info", "warn", "error"]>>;
297
+ serviceName: z.ZodOptional<z.ZodString>;
298
+ }, "strip", z.ZodTypeAny, {
299
+ enabled?: boolean | undefined;
300
+ level?: "debug" | "info" | "warn" | "error" | undefined;
301
+ serviceName?: string | undefined;
302
+ }, {
303
+ enabled?: boolean | undefined;
304
+ level?: "debug" | "info" | "warn" | "error" | undefined;
305
+ serviceName?: string | undefined;
306
+ }>>;
307
+ models: z.ZodOptional<z.ZodObject<{
308
+ extractor: z.ZodOptional<z.ZodString>;
309
+ }, "strip", z.ZodTypeAny, {
310
+ extractor?: string | undefined;
311
+ }, {
312
+ extractor?: string | undefined;
313
+ }>>;
314
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
315
+ intent: z.ZodOptional<z.ZodObject<{
316
+ mode: z.ZodOptional<z.ZodString>;
317
+ allowedIntent: z.ZodOptional<z.ZodString>;
318
+ semantic: z.ZodObject<{
319
+ threshold: z.ZodOptional<z.ZodNumber>;
320
+ intents: z.ZodRecord<z.ZodString, z.ZodString>;
321
+ }, "strip", z.ZodTypeAny, {
322
+ intents: Record<string, string>;
323
+ threshold?: number | undefined;
324
+ }, {
325
+ intents: Record<string, string>;
326
+ threshold?: number | undefined;
327
+ }>;
328
+ }, "strip", z.ZodTypeAny, {
329
+ semantic: {
330
+ intents: Record<string, string>;
331
+ threshold?: number | undefined;
332
+ };
333
+ mode?: string | undefined;
334
+ allowedIntent?: string | undefined;
335
+ }, {
336
+ semantic: {
337
+ intents: Record<string, string>;
338
+ threshold?: number | undefined;
339
+ };
340
+ mode?: string | undefined;
341
+ allowedIntent?: string | undefined;
342
+ }>>;
343
+ pii: z.ZodOptional<z.ZodObject<{
344
+ reversible: z.ZodOptional<z.ZodBoolean>;
345
+ model: z.ZodOptional<z.ZodString>;
346
+ mode: z.ZodOptional<z.ZodEnum<["ner", "classifier"]>>;
347
+ }, "strip", z.ZodTypeAny, {
348
+ mode?: "ner" | "classifier" | undefined;
349
+ reversible?: boolean | undefined;
350
+ model?: string | undefined;
351
+ }, {
352
+ mode?: "ner" | "classifier" | undefined;
353
+ reversible?: boolean | undefined;
354
+ model?: string | undefined;
355
+ }>>;
356
+ logging: z.ZodOptional<z.ZodObject<{
357
+ enabled: z.ZodOptional<z.ZodBoolean>;
358
+ level: z.ZodOptional<z.ZodEnum<["debug", "info", "warn", "error"]>>;
359
+ serviceName: z.ZodOptional<z.ZodString>;
360
+ }, "strip", z.ZodTypeAny, {
361
+ enabled?: boolean | undefined;
362
+ level?: "debug" | "info" | "warn" | "error" | undefined;
363
+ serviceName?: string | undefined;
364
+ }, {
365
+ enabled?: boolean | undefined;
366
+ level?: "debug" | "info" | "warn" | "error" | undefined;
367
+ serviceName?: string | undefined;
368
+ }>>;
369
+ models: z.ZodOptional<z.ZodObject<{
370
+ extractor: z.ZodOptional<z.ZodString>;
371
+ }, "strip", z.ZodTypeAny, {
372
+ extractor?: string | undefined;
373
+ }, {
374
+ extractor?: string | undefined;
375
+ }>>;
376
+ }, z.ZodTypeAny, "passthrough">>, void>;
377
+ export declare const guardPlugin: (pluginOptions: void) => import("@genkit-ai/ai").GenkitPluginV2;
378
+ export declare function guard(config?: GuardConfig): (req: any, ctxOrNext: any, maybeNext?: any) => Promise<any>;
379
+ export declare const guardAction: typeof guard;
380
+ export {};