mirror of
https://github.com/openai/codex.git
synced 2026-10-23 18:08:45 +00:00
## Why After `hooks/list` exposes the hook inventory, clients need a way to persist user hook preferences, make those changes effective in already-open sessions, and distinguish user-controllable hooks from managed requirements without adding another bespoke app-server write API. ## What - Extends `hooks/list` entries with effective `enabled` state. - Persists user-level hook state under `hooks.state.<hook-id>` so the model can grow beyond a single boolean over time. - Uses the existing `config/batchWrite` path for hook state updates instead of introducing a dedicated hook write RPC. - Refreshes live session hook engines after config writes so already-open threads observe updated enablement without a restart. ## Stack 1. openai/codex#19705 2. openai/codex#19778 3. This PR - openai/codex#19840 4. openai/codex#19882 ## Reviewer Notes The generated schema files account for much of the raw diff. The core behavior is in: - `hooks/src/config_rules.rs`, which resolves per-hook user state from the config layer stack. - `hooks/src/engine/discovery.rs`, which projects effective enablement into `hooks/list` from source-derived managedness. - `config/src/hook_config.rs`, which defines the new `hooks.state` representation. - `core/src/session/mod.rs`, which rebuilds live hook state after user config reloads. --------- Co-authored-by: Codex <noreply@openai.com>
164 lines
4.6 KiB
Rust
164 lines
4.6 KiB
Rust
use std::collections::BTreeMap;
|
|
use std::path::Path;
|
|
use std::path::PathBuf;
|
|
|
|
use codex_protocol::protocol::HookEventName;
|
|
use schemars::JsonSchema;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
|
|
pub struct HooksFile {
|
|
#[serde(default)]
|
|
pub hooks: HookEventsToml,
|
|
}
|
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
|
|
pub struct HooksToml {
|
|
#[serde(flatten)]
|
|
pub events: HookEventsToml,
|
|
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
|
pub state: BTreeMap<String, HookStateToml>,
|
|
}
|
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
|
|
pub struct HookStateToml {
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub enabled: Option<bool>,
|
|
}
|
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
|
|
pub struct HookEventsToml {
|
|
#[serde(rename = "PreToolUse", default)]
|
|
pub pre_tool_use: Vec<MatcherGroup>,
|
|
#[serde(rename = "PermissionRequest", default)]
|
|
pub permission_request: Vec<MatcherGroup>,
|
|
#[serde(rename = "PostToolUse", default)]
|
|
pub post_tool_use: Vec<MatcherGroup>,
|
|
#[serde(rename = "SessionStart", default)]
|
|
pub session_start: Vec<MatcherGroup>,
|
|
#[serde(rename = "UserPromptSubmit", default)]
|
|
pub user_prompt_submit: Vec<MatcherGroup>,
|
|
#[serde(rename = "Stop", default)]
|
|
pub stop: Vec<MatcherGroup>,
|
|
}
|
|
|
|
impl HookEventsToml {
|
|
pub fn is_empty(&self) -> bool {
|
|
let Self {
|
|
pre_tool_use,
|
|
permission_request,
|
|
post_tool_use,
|
|
session_start,
|
|
user_prompt_submit,
|
|
stop,
|
|
} = self;
|
|
pre_tool_use.is_empty()
|
|
&& permission_request.is_empty()
|
|
&& post_tool_use.is_empty()
|
|
&& session_start.is_empty()
|
|
&& user_prompt_submit.is_empty()
|
|
&& stop.is_empty()
|
|
}
|
|
|
|
pub fn handler_count(&self) -> usize {
|
|
let Self {
|
|
pre_tool_use,
|
|
permission_request,
|
|
post_tool_use,
|
|
session_start,
|
|
user_prompt_submit,
|
|
stop,
|
|
} = self;
|
|
[
|
|
pre_tool_use,
|
|
permission_request,
|
|
post_tool_use,
|
|
session_start,
|
|
user_prompt_submit,
|
|
stop,
|
|
]
|
|
.into_iter()
|
|
.flatten()
|
|
.map(|group| group.hooks.len())
|
|
.sum()
|
|
}
|
|
|
|
pub fn into_matcher_groups(self) -> [(HookEventName, Vec<MatcherGroup>); 6] {
|
|
[
|
|
(HookEventName::PreToolUse, self.pre_tool_use),
|
|
(HookEventName::PermissionRequest, self.permission_request),
|
|
(HookEventName::PostToolUse, self.post_tool_use),
|
|
(HookEventName::SessionStart, self.session_start),
|
|
(HookEventName::UserPromptSubmit, self.user_prompt_submit),
|
|
(HookEventName::Stop, self.stop),
|
|
]
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
|
|
pub struct MatcherGroup {
|
|
#[serde(default)]
|
|
pub matcher: Option<String>,
|
|
#[serde(default)]
|
|
pub hooks: Vec<HookHandlerConfig>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
|
|
#[serde(tag = "type")]
|
|
pub enum HookHandlerConfig {
|
|
#[serde(rename = "command")]
|
|
Command {
|
|
command: String,
|
|
#[serde(default, rename = "timeout")]
|
|
timeout_sec: Option<u64>,
|
|
#[serde(default)]
|
|
r#async: bool,
|
|
#[serde(default, rename = "statusMessage")]
|
|
status_message: Option<String>,
|
|
},
|
|
#[serde(rename = "prompt")]
|
|
Prompt {},
|
|
#[serde(rename = "agent")]
|
|
Agent {},
|
|
}
|
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub struct ManagedHooksRequirementsToml {
|
|
pub managed_dir: Option<PathBuf>,
|
|
pub windows_managed_dir: Option<PathBuf>,
|
|
#[serde(flatten)]
|
|
pub hooks: HookEventsToml,
|
|
}
|
|
|
|
impl ManagedHooksRequirementsToml {
|
|
pub fn is_empty(&self) -> bool {
|
|
let Self {
|
|
managed_dir,
|
|
windows_managed_dir,
|
|
hooks,
|
|
} = self;
|
|
managed_dir.is_none() && windows_managed_dir.is_none() && hooks.is_empty()
|
|
}
|
|
|
|
pub fn handler_count(&self) -> usize {
|
|
self.hooks.handler_count()
|
|
}
|
|
|
|
pub fn managed_dir_for_current_platform(&self) -> Option<&Path> {
|
|
#[cfg(windows)]
|
|
{
|
|
self.windows_managed_dir.as_deref()
|
|
}
|
|
|
|
#[cfg(not(windows))]
|
|
{
|
|
self.managed_dir.as_deref()
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "hooks_tests.rs"]
|
|
mod tests;
|