import * as KnowYourAI from '@know-your-ai/node';
import { firewallIntegration } from '@know-your-ai/firewall';
import { GoogleGenAI } from '@google/genai';
KnowYourAI.init({
dsn: process.env.KNOW_YOUR_AI_DSN!,
environment: 'production',
traceMode: true,
integrations: [
KnowYourAI.googleGenAIIntegration(),
firewallIntegration({
baseUrl: process.env.FIREWALL_URL!,
apiKey: process.env.FIREWALL_API_KEY!,
onInputViolation: 'block',
onOutputViolation: 'callback',
riskThreshold: 0.7,
violationCallback: async (ctx) => {
await logViolation(ctx);
if (ctx.validation.risks.some(r => r.score >= 0.9)) {
await alertSecurityTeam(ctx);
}
},
}),
],
});
// Custom hooks for agent-specific policies
const hooks = KnowYourAI.getHookManager();
// 1. Only allow approved models
hooks.addBeforeRequestHook(async (ctx) => {
if (!['gemini-2.0-flash', 'gemini-1.5-pro'].includes(ctx.model)) {
return { action: 'block', reason: `Unauthorized model: ${ctx.model}` };
}
}, 'model-policy');
// 2. Cap token usage to prevent runaway costs
hooks.addBeforeRequestHook(async (ctx) => {
if (ctx.requestParams?.maxTokens && ctx.requestParams.maxTokens > 8192) {
return {
action: 'modify',
modified: { requestParams: { ...ctx.requestParams, maxTokens: 8192 } },
};
}
}, 'token-cap');
// 3. Audit all tool calls
hooks.addAfterResponseHook(async (ctx) => {
if (ctx.toolCalls?.length) {
console.log(`[Audit] ${ctx.model} called tools: ${ctx.toolCalls.map(t => t.name).join(', ')}`);
}
}, 'tool-audit');
// Now all AI calls are monitored, firewalled, and policy-enforced
const genAI = new GoogleGenAI({ apiKey: process.env.GOOGLE_API_KEY! });
const client = KnowYourAI.instrumentGoogleGenAIClient(genAI);