|
| 1 | +use std::collections::BTreeMap; |
| 2 | + |
| 3 | +use serde::Deserialize; |
| 4 | +use serde::Serialize; |
| 5 | + |
| 6 | +/// External hook events understood by Devo configuration. |
| 7 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] |
| 8 | +pub enum HookEvent { |
| 9 | + PreToolUse, |
| 10 | + PostToolUse, |
| 11 | + PostToolUseFailure, |
| 12 | + Notification, |
| 13 | + UserPromptSubmit, |
| 14 | + SessionStart, |
| 15 | + SessionEnd, |
| 16 | + Stop, |
| 17 | + StopFailure, |
| 18 | + SubagentStart, |
| 19 | + SubagentStop, |
| 20 | + PreCompact, |
| 21 | + PostCompact, |
| 22 | + PermissionRequest, |
| 23 | + PermissionDenied, |
| 24 | + Setup, |
| 25 | + TeammateIdle, |
| 26 | + TaskCreated, |
| 27 | + TaskCompleted, |
| 28 | + Elicitation, |
| 29 | + ElicitationResult, |
| 30 | + ConfigChange, |
| 31 | + WorktreeCreate, |
| 32 | + WorktreeRemove, |
| 33 | + InstructionsLoaded, |
| 34 | + CwdChanged, |
| 35 | + FileChanged, |
| 36 | +} |
| 37 | + |
| 38 | +/// Top-level hook configuration keyed by event name. |
| 39 | +#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] |
| 40 | +#[serde(transparent)] |
| 41 | +pub struct HooksConfig(pub BTreeMap<HookEvent, Vec<HookMatcherConfig>>); |
| 42 | + |
| 43 | +impl HooksConfig { |
| 44 | + pub fn is_empty(&self) -> bool { |
| 45 | + self.0.values().all(Vec::is_empty) |
| 46 | + } |
| 47 | + |
| 48 | + pub fn matchers_for(&self, event: HookEvent) -> &[HookMatcherConfig] { |
| 49 | + self.0.get(&event).map_or(&[], Vec::as_slice) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/// A matcher groups one or more hooks for an event-specific match query. |
| 54 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 55 | +pub struct HookMatcherConfig { |
| 56 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 57 | + pub matcher: Option<String>, |
| 58 | + #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 59 | + pub hooks: Vec<HookCommandConfig>, |
| 60 | +} |
| 61 | + |
| 62 | +/// Persistable hook command definitions. |
| 63 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 64 | +#[serde(tag = "type", rename_all = "snake_case")] |
| 65 | +pub enum HookCommandConfig { |
| 66 | + Command(CommandHookConfig), |
| 67 | + Prompt(PromptHookConfig), |
| 68 | + Agent(AgentHookConfig), |
| 69 | + Http(HttpHookConfig), |
| 70 | +} |
| 71 | + |
| 72 | +/// Shell command hook definition. |
| 73 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 74 | +pub struct CommandHookConfig { |
| 75 | + pub command: String, |
| 76 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 77 | + pub shell: Option<HookShell>, |
| 78 | + #[serde(rename = "if", default, skip_serializing_if = "Option::is_none")] |
| 79 | + pub condition: Option<String>, |
| 80 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 81 | + pub timeout: Option<u64>, |
| 82 | + #[serde( |
| 83 | + rename = "statusMessage", |
| 84 | + alias = "status_message", |
| 85 | + default, |
| 86 | + skip_serializing_if = "Option::is_none" |
| 87 | + )] |
| 88 | + pub status_message: Option<String>, |
| 89 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 90 | + pub once: Option<bool>, |
| 91 | + #[serde(rename = "async", default, skip_serializing_if = "Option::is_none")] |
| 92 | + pub async_hook: Option<bool>, |
| 93 | + #[serde( |
| 94 | + rename = "asyncRewake", |
| 95 | + default, |
| 96 | + skip_serializing_if = "Option::is_none" |
| 97 | + )] |
| 98 | + pub async_rewake: Option<bool>, |
| 99 | +} |
| 100 | + |
| 101 | +/// Shell used to run a command hook. |
| 102 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] |
| 103 | +pub enum HookShell { |
| 104 | + #[serde(rename = "bash")] |
| 105 | + Bash, |
| 106 | + #[serde(rename = "powershell", alias = "power_shell")] |
| 107 | + PowerShell, |
| 108 | +} |
| 109 | + |
| 110 | +/// Parsed but currently unsupported LLM prompt hook definition. |
| 111 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 112 | +pub struct PromptHookConfig { |
| 113 | + pub prompt: String, |
| 114 | + #[serde(rename = "if", default, skip_serializing_if = "Option::is_none")] |
| 115 | + pub condition: Option<String>, |
| 116 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 117 | + pub timeout: Option<u64>, |
| 118 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 119 | + pub model: Option<String>, |
| 120 | + #[serde( |
| 121 | + rename = "statusMessage", |
| 122 | + alias = "status_message", |
| 123 | + default, |
| 124 | + skip_serializing_if = "Option::is_none" |
| 125 | + )] |
| 126 | + pub status_message: Option<String>, |
| 127 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 128 | + pub once: Option<bool>, |
| 129 | +} |
| 130 | + |
| 131 | +/// Parsed but currently unsupported agent hook definition. |
| 132 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 133 | +pub struct AgentHookConfig { |
| 134 | + pub prompt: String, |
| 135 | + #[serde(rename = "if", default, skip_serializing_if = "Option::is_none")] |
| 136 | + pub condition: Option<String>, |
| 137 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 138 | + pub timeout: Option<u64>, |
| 139 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 140 | + pub model: Option<String>, |
| 141 | + #[serde( |
| 142 | + rename = "statusMessage", |
| 143 | + alias = "status_message", |
| 144 | + default, |
| 145 | + skip_serializing_if = "Option::is_none" |
| 146 | + )] |
| 147 | + pub status_message: Option<String>, |
| 148 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 149 | + pub once: Option<bool>, |
| 150 | +} |
| 151 | + |
| 152 | +/// Parsed but currently unsupported HTTP hook definition. |
| 153 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 154 | +pub struct HttpHookConfig { |
| 155 | + pub url: String, |
| 156 | + #[serde(rename = "if", default, skip_serializing_if = "Option::is_none")] |
| 157 | + pub condition: Option<String>, |
| 158 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 159 | + pub timeout: Option<u64>, |
| 160 | + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] |
| 161 | + pub headers: BTreeMap<String, String>, |
| 162 | + #[serde( |
| 163 | + rename = "allowedEnvVars", |
| 164 | + default, |
| 165 | + skip_serializing_if = "Vec::is_empty" |
| 166 | + )] |
| 167 | + pub allowed_env_vars: Vec<String>, |
| 168 | + #[serde( |
| 169 | + rename = "statusMessage", |
| 170 | + alias = "status_message", |
| 171 | + default, |
| 172 | + skip_serializing_if = "Option::is_none" |
| 173 | + )] |
| 174 | + pub status_message: Option<String>, |
| 175 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 176 | + pub once: Option<bool>, |
| 177 | +} |
0 commit comments