import type { HookEvent } from '../../src/types.js'; import { writeAuditLog } from '../../src/utils/logger.js'; const SUBSCRIBED_EVENTS = new Set([ 'message:received', 'message:sent', 'command:new', 'command:reset', 'command:stop', 'gateway:startup', 'agent:bootstrap', ]); const handler = async (event: HookEvent): Promise => { try { const { type, action, context, sessionKey, timestamp } = event; const key = `${type}:${action}`; if (!SUBSCRIBED_EVENTS.has(key)) return; const metadata: Record = {}; if (type === 'message' && context.content) { metadata.messageLength = context.content.length; } if (context.success !== undefined) { metadata.success = context.success; } writeAuditLog({ timestamp: timestamp.toISOString(), event: key, sessionKey, channel: context.channelId, isGroup: context.isGroup, alerts: [], metadata, }); } catch (err) { console.error( '[security-guardrails:audit-logger] Error:', err instanceof Error ? err.message : String(err), ); } }; export default handler;