File size: 7,884 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | /**
* Hook Zod schemas extracted to break import cycles.
*
* This file contains hook-related schema definitions that were originally
* in src/utils/settings/types.ts. By extracting them here, we break the
* circular dependency between settings/types.ts and plugins/schemas.ts.
*
* Both files now import from this shared location instead of each other.
*/
import { HOOK_EVENTS, type HookEvent } from 'src/entrypoints/agentSdkTypes.js'
import { z } from 'zod/v4'
import { lazySchema } from '../utils/lazySchema.js'
import { SHELL_TYPES } from '../utils/shell/shellProvider.js'
// Shared schema for the `if` condition field.
// Uses permission rule syntax (e.g., "Bash(git *)", "Read(*.ts)") to filter hooks
// before spawning. Evaluated against the hook input's tool_name and tool_input.
const IfConditionSchema = lazySchema(() =>
z
.string()
.optional()
.describe(
'Permission rule syntax to filter when this hook runs (e.g., "Bash(git *)"). ' +
'Only runs if the tool call matches the pattern. Avoids spawning hooks for non-matching commands.',
),
)
// Internal factory for individual hook schemas (shared between exported
// discriminated union members and the HookCommandSchema factory)
function buildHookSchemas() {
const BashCommandHookSchema = z.object({
type: z.literal('command').describe('Shell command hook type'),
command: z.string().describe('Shell command to execute'),
if: IfConditionSchema(),
shell: z
.enum(SHELL_TYPES)
.optional()
.describe(
"Shell interpreter. 'bash' uses your $SHELL (bash/zsh/sh); 'powershell' uses pwsh. Defaults to bash.",
),
timeout: z
.number()
.positive()
.optional()
.describe('Timeout in seconds for this specific command'),
statusMessage: z
.string()
.optional()
.describe('Custom status message to display in spinner while hook runs'),
once: z
.boolean()
.optional()
.describe('If true, hook runs once and is removed after execution'),
async: z
.boolean()
.optional()
.describe('If true, hook runs in background without blocking'),
asyncRewake: z
.boolean()
.optional()
.describe(
'If true, hook runs in background and wakes the model on exit code 2 (blocking error). Implies async.',
),
})
const PromptHookSchema = z.object({
type: z.literal('prompt').describe('LLM prompt hook type'),
prompt: z
.string()
.describe(
'Prompt to evaluate with LLM. Use $ARGUMENTS placeholder for hook input JSON.',
),
if: IfConditionSchema(),
timeout: z
.number()
.positive()
.optional()
.describe('Timeout in seconds for this specific prompt evaluation'),
// @[MODEL LAUNCH]: Update the example model ID in the .describe() strings below (prompt + agent hooks).
model: z
.string()
.optional()
.describe(
'Model to use for this prompt hook (e.g., "claude-sonnet-4-6"). If not specified, uses the default small fast model.',
),
statusMessage: z
.string()
.optional()
.describe('Custom status message to display in spinner while hook runs'),
once: z
.boolean()
.optional()
.describe('If true, hook runs once and is removed after execution'),
})
const HttpHookSchema = z.object({
type: z.literal('http').describe('HTTP hook type'),
url: z.string().url().describe('URL to POST the hook input JSON to'),
if: IfConditionSchema(),
timeout: z
.number()
.positive()
.optional()
.describe('Timeout in seconds for this specific request'),
headers: z
.record(z.string(), z.string())
.optional()
.describe(
'Additional headers to include in the request. Values may reference environment variables using $VAR_NAME or ${VAR_NAME} syntax (e.g., "Authorization": "Bearer $MY_TOKEN"). Only variables listed in allowedEnvVars will be interpolated.',
),
allowedEnvVars: z
.array(z.string())
.optional()
.describe(
'Explicit list of environment variable names that may be interpolated in header values. Only variables listed here will be resolved; all other $VAR references are left as empty strings. Required for env var interpolation to work.',
),
statusMessage: z
.string()
.optional()
.describe('Custom status message to display in spinner while hook runs'),
once: z
.boolean()
.optional()
.describe('If true, hook runs once and is removed after execution'),
})
const AgentHookSchema = z.object({
type: z.literal('agent').describe('Agentic verifier hook type'),
// DO NOT add .transform() here. This schema is used by parseSettingsFile,
// and updateSettingsForSource round-trips the parsed result through
// JSON.stringify — a transformed function value is silently dropped,
// deleting the user's prompt from settings.json (gh-24920, CC-79). The
// transform (from #10594) wrapped the string in `(_msgs) => prompt`
// for a programmatic-construction use case in ExitPlanModeV2Tool that
// has since been refactored into VerifyPlanExecutionTool, which no
// longer constructs AgentHook objects at all.
prompt: z
.string()
.describe(
'Prompt describing what to verify (e.g. "Verify that unit tests ran and passed."). Use $ARGUMENTS placeholder for hook input JSON.',
),
if: IfConditionSchema(),
timeout: z
.number()
.positive()
.optional()
.describe('Timeout in seconds for agent execution (default 60)'),
model: z
.string()
.optional()
.describe(
'Model to use for this agent hook (e.g., "claude-sonnet-4-6"). If not specified, uses Haiku.',
),
statusMessage: z
.string()
.optional()
.describe('Custom status message to display in spinner while hook runs'),
once: z
.boolean()
.optional()
.describe('If true, hook runs once and is removed after execution'),
})
return {
BashCommandHookSchema,
PromptHookSchema,
HttpHookSchema,
AgentHookSchema,
}
}
/**
* Schema for hook command (excludes function hooks - they can't be persisted)
*/
export const HookCommandSchema = lazySchema(() => {
const {
BashCommandHookSchema,
PromptHookSchema,
AgentHookSchema,
HttpHookSchema,
} = buildHookSchemas()
return z.discriminatedUnion('type', [
BashCommandHookSchema,
PromptHookSchema,
AgentHookSchema,
HttpHookSchema,
])
})
/**
* Schema for matcher configuration with multiple hooks
*/
export const HookMatcherSchema = lazySchema(() =>
z.object({
matcher: z
.string()
.optional()
.describe('String pattern to match (e.g. tool names like "Write")'), // String (e.g. Write) to match values related to the hook event, e.g. tool names
hooks: z
.array(HookCommandSchema())
.describe('List of hooks to execute when the matcher matches'),
}),
)
/**
* Schema for hooks configuration
* The key is the hook event. The value is an array of matcher configurations.
* Uses partialRecord since not all hook events need to be defined.
*/
export const HooksSchema = lazySchema(() =>
z.partialRecord(z.enum(HOOK_EVENTS), z.array(HookMatcherSchema())),
)
// Inferred types from schemas
export type HookCommand = z.infer<ReturnType<typeof HookCommandSchema>>
export type BashCommandHook = Extract<HookCommand, { type: 'command' }>
export type PromptHook = Extract<HookCommand, { type: 'prompt' }>
export type AgentHook = Extract<HookCommand, { type: 'agent' }>
export type HttpHook = Extract<HookCommand, { type: 'http' }>
export type HookMatcher = z.infer<ReturnType<typeof HookMatcherSchema>>
export type HooksSettings = Partial<Record<HookEvent, HookMatcher[]>>
|