classifierShared.ts
1 /** 2 * Shared infrastructure for classifier-based permission systems. 3 * 4 * This module provides common types, schemas, and utilities used by both: 5 * - bashClassifier.ts (semantic Bash command matching) 6 * - yoloClassifier.ts (YOLO mode security classification) 7 */ 8 9 import type { BetaContentBlock } from '@anthropic-ai/sdk/resources/beta/messages.js' 10 import type { z } from 'zod/v4' 11 12 /** 13 * Extract tool use block from message content by tool name. 14 */ 15 export function extractToolUseBlock( 16 content: BetaContentBlock[], 17 toolName: string, 18 ): Extract<BetaContentBlock, { type: 'tool_use' }> | null { 19 const block = content.find(b => b.type === 'tool_use' && b.name === toolName) 20 if (!block || block.type !== 'tool_use') { 21 return null 22 } 23 return block 24 } 25 26 /** 27 * Parse and validate classifier response from tool use block. 28 * Returns null if parsing fails. 29 */ 30 export function parseClassifierResponse<T extends z.ZodTypeAny>( 31 toolUseBlock: Extract<BetaContentBlock, { type: 'tool_use' }>, 32 schema: T, 33 ): z.infer<T> | null { 34 const parseResult = schema.safeParse(toolUseBlock.input) 35 if (!parseResult.success) { 36 return null 37 } 38 return parseResult.data 39 }