registerSkillHooks.ts
utils/hooks/registerSkillHooks.ts
No strong subsystem tag
65
Lines
2059
Bytes
1
Exports
5
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 general runtime concerns. It contains 65 lines, 5 detected imports, and 1 detected exports.
Important relationships
Detected exports
registerSkillHooks
Keywords
hooksskillhookparameventnameappstatesetappstatesessionidskillnameregisteredcount
Detected imports
src/entrypoints/agentSdkTypes.jssrc/state/AppState.js../debug.js../settings/types.js./sessionHooks.js
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
import { HOOK_EVENTS } from 'src/entrypoints/agentSdkTypes.js'
import type { AppState } from 'src/state/AppState.js'
import { logForDebugging } from '../debug.js'
import type { HooksSettings } from '../settings/types.js'
import { addSessionHook, removeSessionHook } from './sessionHooks.js'
/**
* Registers hooks from a skill's frontmatter as session hooks.
*
* Hooks are registered as session-scoped hooks that persist for the duration
* of the session. If a hook has `once: true`, it will be automatically removed
* after its first successful execution.
*
* @param setAppState - Function to update the app state
* @param sessionId - The current session ID
* @param hooks - The hooks settings from the skill's frontmatter
* @param skillName - The name of the skill (for logging)
* @param skillRoot - The base directory of the skill (for CLAUDE_PLUGIN_ROOT env var)
*/
export function registerSkillHooks(
setAppState: (updater: (prev: AppState) => AppState) => void,
sessionId: string,
hooks: HooksSettings,
skillName: string,
skillRoot?: string,
): void {
let registeredCount = 0
for (const eventName of HOOK_EVENTS) {
const matchers = hooks[eventName]
if (!matchers) continue
for (const matcher of matchers) {
for (const hook of matcher.hooks) {
// For once: true hooks, use onHookSuccess callback to remove after execution
const onHookSuccess = hook.once
? () => {
logForDebugging(
`Removing one-shot hook for event ${eventName} in skill '${skillName}'`,
)
removeSessionHook(setAppState, sessionId, eventName, hook)
}
: undefined
addSessionHook(
setAppState,
sessionId,
eventName,
matcher.matcher || '',
hook,
onHookSuccess,
skillRoot,
)
registeredCount++
}
}
}
if (registeredCount > 0) {
logForDebugging(
`Registered ${registeredCount} hooks from skill '${skillName}'`,
)
}
}