# Conflicts: # docs/AGENTS.md # docs/config-catalog.md # packages/bash/bash-sandbox/src/index.ts # packages/bash/bash/src/session-mode.ts # packages/bash/tool-bash/README.md # packages/code-runtime/code-runtime-worker/README.md # packages/compact/compact/src/index.ts # packages/core/agent-core/README.md # packages/hooks/hooks-claude/src/config.ts # packages/hooks/hooks-claude/src/index.ts # packages/hooks/hooks-codex/src/config.ts # packages/hooks/hooks-codex/src/index.ts # packages/llm/llm/README.md # packages/session-persistence/session-persistence-jsonl/README.md # packages/session-persistence/session-persistence/README.md # packages/skill/skill-local/README.md # packages/support/acp-snapshot/README.md # packages/support/invariants/src/index.ts # packages/ui/acp/README.md # packages/ui/jsonrpc-agent/README.md # packages/ui/jsonrpc/README.md # packages/ui/permission/README.md # packages/ui/user-approval/README.md # packages/ui/user-interaction/README.md # packages/web/web-search-deepseek/README.md
105 lines
4.0 KiB
TypeScript
105 lines
4.0 KiB
TypeScript
/**
|
|
* Parse Claude Code's event-to-matcher-group hook format into shared {@link MatcherGroup}s.
|
|
* Only command hooks run; other hook types are returned as skipped so the
|
|
* bridge can warn. Plugin-root and project-directory substitutions are applied
|
|
* to commands at parse time.
|
|
* @module @deepseek-ai/dsh-hooks-claude/config
|
|
*/
|
|
|
|
import type { MatcherGroup } from '@deepseek-ai/dsh-hook-protocol'
|
|
|
|
/** A parsed CC config: event name → its matcher groups (command hooks only). */
|
|
export type ClaudeHookConfig = Record<string, MatcherGroup[]>
|
|
|
|
/** A skipped non-command hook, surfaced so the bridge can warn about it. */
|
|
export interface SkippedHook {
|
|
event: string
|
|
type: string
|
|
}
|
|
|
|
/** The outcome of parsing one config file: the runnable groups + what was skipped. */
|
|
export interface ParsedClaudeConfig {
|
|
config: ClaudeHookConfig
|
|
skipped: SkippedHook[]
|
|
}
|
|
|
|
/** Substitution variables applied to each `command` string at parse time. */
|
|
export interface SubstitutionVars {
|
|
/** Replaces `${CLAUDE_PLUGIN_ROOT}` — the plugin's root dir. */
|
|
pluginRoot?: string
|
|
/** Replaces `${CLAUDE_PROJECT_DIR}` — the project root. */
|
|
projectDir?: string
|
|
}
|
|
|
|
/** A plain (non-null, non-array) object, else undefined. */
|
|
function asObject(value: unknown): Record<string, unknown> | undefined {
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
? value as Record<string, unknown>
|
|
: undefined
|
|
}
|
|
|
|
/**
|
|
* Apply `${CLAUDE_PLUGIN_ROOT}` / `${CLAUDE_PROJECT_DIR}` substitution to a command string.
|
|
* @param command - the raw command from config.
|
|
* @param vars - the substitution values; a token whose variable is unset stays verbatim.
|
|
* @returns the command with every occurrence of each set token replaced.
|
|
*/
|
|
export function substituteCommand(command: string, vars: SubstitutionVars): string {
|
|
let out = command
|
|
if (vars.pluginRoot !== undefined) out = out.split('${CLAUDE_PLUGIN_ROOT}').join(vars.pluginRoot)
|
|
if (vars.projectDir !== undefined) out = out.split('${CLAUDE_PROJECT_DIR}').join(vars.projectDir)
|
|
return out
|
|
}
|
|
|
|
/**
|
|
* Parse either a settings `hooks` value or a bare `hooks.json` event map. Malformed entries are
|
|
* ignored rather than failing boot; non-command hooks are returned in `skipped`, and substitutions
|
|
* are applied to every surviving command.
|
|
*
|
|
* @param raw - the parsed JSON config: a settings object with a `hooks` key, or the bare
|
|
* event map.
|
|
* @param vars - substitution values applied to every surviving `command` (defaults to
|
|
* none).
|
|
* @returns the runnable per-event groups plus the skipped non-command hooks.
|
|
*/
|
|
export function parseClaudeConfig(raw: unknown, vars: SubstitutionVars = {}): ParsedClaudeConfig {
|
|
const config: ClaudeHookConfig = {}
|
|
const skipped: SkippedHook[] = []
|
|
// Accept either `{ hooks: { … } }` (a settings file) or the bare event map.
|
|
const root = asObject(raw)
|
|
const hooksMap = root ? asObject(root.hooks) ?? root : undefined
|
|
if (!hooksMap) return { config, skipped }
|
|
|
|
for (const [event, rawGroups] of Object.entries(hooksMap)) {
|
|
if (!Array.isArray(rawGroups)) continue
|
|
const groups: MatcherGroup[] = []
|
|
for (const rawGroup of rawGroups) {
|
|
const group = asObject(rawGroup)
|
|
if (!group || !Array.isArray(group.hooks)) continue
|
|
const commands: MatcherGroup['hooks'] = []
|
|
for (const rawHook of group.hooks) {
|
|
const hook = asObject(rawHook)
|
|
if (!hook) continue
|
|
const type = typeof hook.type === 'string' ? hook.type : 'command'
|
|
if (type !== 'command') {
|
|
skipped.push({ event, type })
|
|
continue
|
|
}
|
|
if (typeof hook.command !== 'string') continue
|
|
commands.push({
|
|
command: substituteCommand(hook.command, vars),
|
|
...typeof hook.timeout === 'number' ? { timeoutSec: hook.timeout } : {},
|
|
})
|
|
}
|
|
if (commands.length === 0) continue
|
|
groups.push({
|
|
...typeof group.matcher === 'string' ? { matcher: group.matcher } : {},
|
|
hooks: commands,
|
|
})
|
|
}
|
|
if (groups.length > 0) config[event] = groups
|
|
}
|
|
|
|
return { config, skipped }
|
|
}
|