MCPcopy Create free account
hub / github.com/Noumena-Network/code / prepareIfConditionMatcher

Function prepareIfConditionMatcher

src/utils/hooks.ts:1394–1425  ·  view source on GitHub ↗

* Prepare a matcher for hook `if` conditions. Expensive work (tool lookup, * Zod validation, tree-sitter parsing for Bash) happens once here; the * returned closure is called per hook. Returns undefined for non-tool events.

(
  hookInput: HookInput,
  tools: Tools | undefined,
)

Source from the content-addressed store, hash-verified

1392 * returned closure is called per hook. Returns undefined for non-tool events.
1393 */
1394async function prepareIfConditionMatcher(
1395 hookInput: HookInput,
1396 tools: Tools | undefined,
1397): Promise<IfConditionMatcher | undefined> {
1398 if (
1399 hookInput.hook_event_name !== 'PreToolUse' &&
1400 hookInput.hook_event_name !== 'PostToolUse' &&
1401 hookInput.hook_event_name !== 'PostToolUseFailure' &&
1402 hookInput.hook_event_name !== 'PermissionRequest'
1403 ) {
1404 return undefined
1405 }
1406
1407 const toolName = normalizeLegacyToolName(hookInput.tool_name)
1408 const tool = tools && findToolByName(tools, hookInput.tool_name)
1409 const input = tool?.inputSchema.safeParse(hookInput.tool_input)
1410 const patternMatcher =
1411 input?.success && tool?.preparePermissionMatcher
1412 ? await tool.preparePermissionMatcher(input.data)
1413 : undefined
1414
1415 return ifCondition => {
1416 const parsed = permissionRuleValueFromString(ifCondition)
1417 if (normalizeLegacyToolName(parsed.toolName) !== toolName) {
1418 return false
1419 }
1420 if (!parsed.ruleContent) {
1421 return true
1422 }
1423 return patternMatcher ? patternMatcher(parsed.ruleContent) : false
1424 }
1425}
1426
1427type FunctionHookMatcher = {
1428 matcher: string

Callers 1

getMatchingHooksFunction · 0.85

Calls 3

normalizeLegacyToolNameFunction · 0.85
findToolByNameFunction · 0.85

Tested by

no test coverage detected