import {
Agent,
openai,
defineTool,
PiiGuard,
InputGuardrail,
OutputGuardrail,
EventBus,
} from "@radaros/core";
import { z } from "zod";
const eventBus = new EventBus();
eventBus.on("agent.*", (event) => {
console.log(`[audit] ${event.type}:`, JSON.stringify(event.data));
});
const piiGuard = new PiiGuard({
builtIn: ["email", "phone", "ssn", "creditCard"],
action: "placeholder",
rehydrate: true,
});
const injectionGuard: InputGuardrail = {
name: "injection-blocker",
execute: async (input) => {
const suspicious = /(DROP\s+TABLE|DELETE\s+FROM|<script>)/i.test(input);
return { passed: !suspicious, reason: suspicious ? "Blocked: injection attempt" : undefined };
},
};
const outputPiiGuard: OutputGuardrail = {
name: "output-pii-filter",
execute: async (output) => {
const hasSSN = /\b\d{3}-\d{2}-\d{4}\b/.test(output);
return {
passed: !hasSSN,
reason: hasSSN ? "Response contains SSN" : undefined,
sanitized: hasSSN ? output.replace(/\b\d{3}-\d{2}-\d{4}\b/g, "[REDACTED]") : undefined,
};
},
};
const deleteRecord = defineTool({
name: "delete_record",
description: "Delete a database record",
parameters: z.object({ table: z.string(), id: z.string() }),
execute: async ({ table, id }) => `Deleted ${table}/${id}`,
});
const runQuery = defineTool({
name: "run_query",
description: "Run a read-only SQL query",
parameters: z.object({ sql: z.string() }),
execute: async ({ sql }) => `Query result for: ${sql.slice(0, 40)}`,
sandbox: {
timeout: 5_000,
maxMemoryMB: 64,
allowNetwork: false,
allowFS: { read: ["/data/readonly"] },
},
});
const agent = new Agent({
name: "hardened-ops",
model: openai("gpt-4o"),
instructions: "You are an operations assistant with strict security controls.",
tools: [deleteRecord, runQuery],
eventBus,
retry: { maxRetries: 3, initialDelayMs: 500, backoffMultiplier: 2 },
loopHooks: {
beforeLLMCall: piiGuard.toBeforeLLMCallHook(),
afterToolExec: piiGuard.toAfterToolExecHook(),
},
guardrails: {
input: [injectionGuard, piiGuard.toInputGuardrail()],
output: [outputPiiGuard],
outputAction: "sanitize",
},
approval: {
policy: ["delete_record"],
timeout: 60_000,
timeoutAction: "deny",
onApproval: async (request) => {
console.log(`[approval] ${request.toolName}:`, request.args);
return { approved: true, reason: "Auto-approved in staging" };
},
},
});
const result = await agent.run(
"Look up all orders for alice@acme.com and delete any cancelled ones.",
);
console.log("Final:", result.text);
console.log("Rehydrated:", piiGuard.rehydrate(result.text));