classifierShared.ts
utils/permissions/classifierShared.ts
40
Lines
1174
Bytes
2
Exports
2
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with shell-safety, permissions. It contains 40 lines, 2 detected imports, and 2 detected exports.
Important relationships
- utils/permissions/PermissionMode.ts
- utils/permissions/PermissionPromptToolResultSchema.ts
- utils/permissions/PermissionResult.ts
- utils/permissions/PermissionRule.ts
- utils/permissions/PermissionUpdate.ts
- utils/permissions/PermissionUpdateSchema.ts
- utils/permissions/autoModeState.ts
- utils/permissions/bashClassifier.ts
Detected exports
extractToolUseBlockparseClassifierResponse
Keywords
blockbetacontentblocktool_useextracttoolcontentparseresultnametoolnametooluseblock
Detected imports
@anthropic-ai/sdk/resources/beta/messages.jszod/v4
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
/**
* Shared infrastructure for classifier-based permission systems.
*
* This module provides common types, schemas, and utilities used by both:
* - bashClassifier.ts (semantic Bash command matching)
* - yoloClassifier.ts (YOLO mode security classification)
*/
import type { BetaContentBlock } from '@anthropic-ai/sdk/resources/beta/messages.js'
import type { z } from 'zod/v4'
/**
* Extract tool use block from message content by tool name.
*/
export function extractToolUseBlock(
content: BetaContentBlock[],
toolName: string,
): Extract<BetaContentBlock, { type: 'tool_use' }> | null {
const block = content.find(b => b.type === 'tool_use' && b.name === toolName)
if (!block || block.type !== 'tool_use') {
return null
}
return block
}
/**
* Parse and validate classifier response from tool use block.
* Returns null if parsing fails.
*/
export function parseClassifierResponse<T extends z.ZodTypeAny>(
toolUseBlock: Extract<BetaContentBlock, { type: 'tool_use' }>,
schema: T,
): z.infer<T> | null {
const parseResult = schema.safeParse(toolUseBlock.input)
if (!parseResult.success) {
return null
}
return parseResult.data
}