Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/hk-core/src/adapter/claude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ impl AgentAdapter for ClaudeAdapter {
RemoteMcpSchema::TypeAndUrl
}

fn supports_hook_kind(&self, _kind: super::HookKind) -> bool {
true
}
fn translate_hook_event(&self, event: &str) -> Option<String> {
super::hook_events::to_claude(event)
}
Expand Down
51 changes: 1 addition & 50 deletions crates/hk-core/src/adapter/codex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ impl CodexAdapter {
Self { home }
}

fn parse_json(path: &Path) -> Option<serde_json::Value> {
let content = std::fs::read_to_string(path).ok()?;
serde_json::from_str(&content).ok()
}

/// Resolved doc filename fallback list, in the order Codex itself searches.
/// Reads `project_doc_fallback_filenames` from `~/.codex/config.toml` if
/// the user has overridden the default; falls back to the built-in list
Expand Down Expand Up @@ -300,51 +295,7 @@ impl AgentAdapter for CodexAdapter {
}

fn read_hooks_from(&self, path: &Path) -> Vec<HookEntry> {
let Some(config) = Self::parse_json(path) else {
return vec![];
};
let Some(hooks) = config.get("hooks").and_then(|v| v.as_object()) else {
return vec![];
};
let mut entries = Vec::new();
for (event, hook_list) in hooks {
let Some(arr) = hook_list.as_array() else {
continue;
};
for hook in arr {
let matcher = hook
.get("matcher")
.and_then(|v| v.as_str())
.map(String::from);
if let Some(cmds) = hook.get("hooks").and_then(|v| v.as_array()) {
for cmd in cmds {
// String format: "echo test"
let cmd_str = if let Some(s) = cmd.as_str() {
Some(s.to_string())
}
// Object format: {"type": "command", "command": "echo test"}
else if let Some(s) = cmd.get("command").and_then(|v| v.as_str()) {
Some(s.to_string())
}
// Prompt/agent hook: {"type": "prompt", "prompt": "..."}
else {
cmd.get("prompt")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
};
if let Some(command) = cmd_str {
entries.push(HookEntry {
event: event.clone(),
matcher: matcher.clone(),
command,
enabled: true,
});
}
}
}
}
}
entries
super::read_claude_like_hooks(path)
}

fn read_plugins(&self) -> Vec<PluginEntry> {
Expand Down
50 changes: 30 additions & 20 deletions crates/hk-core/src/adapter/copilot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,15 @@ impl AgentAdapter for CopilotAdapter {
RemoteMcpSchema::TypeAndUrl
}

/// PascalCase events match Claude's tool names, camelCase events
/// Copilot's runtime names.
fn hook_tool_names(&self, event: &str) -> super::hook_tools::ToolNames {
if event.starts_with(char::is_uppercase) {
super::hook_tools::ToolNames::Claude
} else {
super::hook_tools::ToolNames::CopilotRuntime
}
}
fn translate_hook_event(&self, event: &str) -> Option<String> {
super::hook_events::to_copilot(event)
}
Expand Down Expand Up @@ -421,30 +430,16 @@ impl AgentAdapter for CopilotAdapter {
continue;
};
for hook in arr {
// Copilot format: {"type": "command", "command": "..."} or platform-specific {"osx": "...", "linux": "...", "windows": "..."}
let cmd = hook
.get("command")
.and_then(|v| v.as_str())
.or_else(|| {
#[cfg(target_os = "macos")]
{
hook.get("osx").and_then(|v| v.as_str())
}
#[cfg(target_os = "windows")]
{
hook.get("windows").and_then(|v| v.as_str())
}
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
{
hook.get("linux").and_then(|v| v.as_str())
}
})
.or_else(|| hook.get("bash").and_then(|v| v.as_str()));
let cmd = hook_command(hook);
if let Some(cmd_str) = cmd {
entries.push(HookEntry {
event: event.clone(),
matcher: None,
matcher: hook
.get("matcher")
.and_then(|v| v.as_str())
.map(String::from),
command: cmd_str.to_string(),
kind: super::HookKind::Command,
enabled: true,
});
}
Expand All @@ -455,6 +450,21 @@ impl AgentAdapter for CopilotAdapter {
}
}

/// The command a Copilot hook entry runs here: `command`, else the key for
/// this platform, else `bash`.
pub(crate) fn hook_command(hook: &serde_json::Value) -> Option<&str> {
let platform_key = if cfg!(target_os = "macos") {
"osx"
} else if cfg!(target_os = "windows") {
"windows"
} else {
"linux"
};
["command", platform_key, "bash"]
.iter()
.find_map(|key| hook.get(key).and_then(|v| v.as_str()))
}

#[cfg(test)]
mod tests {
use super::super::{AgentAdapter, McpTransport};
Expand Down
17 changes: 13 additions & 4 deletions crates/hk-core/src/adapter/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ impl AgentAdapter for CursorAdapter {
RemoteMcpSchema::PlainUrl
}

fn supports_hook_kind(&self, kind: super::HookKind) -> bool {
kind != super::HookKind::Agent
}
fn hook_tool_names(&self, _event: &str) -> super::hook_tools::ToolNames {
super::hook_tools::ToolNames::Cursor
}
fn translate_hook_event(&self, event: &str) -> Option<String> {
super::hook_events::to_cursor(event)
}
Expand All @@ -140,12 +146,15 @@ impl AgentAdapter for CursorAdapter {
continue;
};
for hook in arr {
// Cursor format: {"command": "..."} — no matcher, no nested hooks array
if let Some(cmd) = hook.get("command").and_then(|v| v.as_str()) {
if let Some((command, kind)) = hook.as_object().and(super::hook_handler(hook)) {
entries.push(HookEntry {
event: event.clone(),
matcher: None,
command: cmd.to_string(),
matcher: hook
.get("matcher")
.and_then(|v| v.as_str())
.map(String::from),
command: command.to_string(),
kind,
enabled: true,
});
}
Expand Down
49 changes: 4 additions & 45 deletions crates/hk-core/src/adapter/gemini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ impl AgentAdapter for GeminiAdapter {
entries
}

fn hook_tool_names(&self, _event: &str) -> super::hook_tools::ToolNames {
super::hook_tools::ToolNames::Gemini
}
fn translate_hook_event(&self, event: &str) -> Option<String> {
super::hook_events::to_gemini(event)
}
Expand All @@ -275,51 +278,7 @@ impl AgentAdapter for GeminiAdapter {
}

fn read_hooks_from(&self, path: &Path) -> Vec<HookEntry> {
let Some(settings) = Self::parse_json(path) else {
return vec![];
};
let Some(hooks) = settings.get("hooks").and_then(|v| v.as_object()) else {
return vec![];
};
let mut entries = Vec::new();
for (event, hook_list) in hooks {
let Some(arr) = hook_list.as_array() else {
continue;
};
for hook in arr {
let matcher = hook
.get("matcher")
.and_then(|v| v.as_str())
.map(String::from);
if let Some(cmds) = hook.get("hooks").and_then(|v| v.as_array()) {
for cmd in cmds {
// String format: "echo test"
let cmd_str = if let Some(s) = cmd.as_str() {
Some(s.to_string())
}
// Object format: {"type": "command", "command": "echo test"}
else if let Some(s) = cmd.get("command").and_then(|v| v.as_str()) {
Some(s.to_string())
}
// Prompt/agent hook: {"type": "prompt", "prompt": "..."}
else {
cmd.get("prompt")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
};
if let Some(command) = cmd_str {
entries.push(HookEntry {
event: event.clone(),
matcher: matcher.clone(),
command,
enabled: true,
});
}
}
}
}
}
entries
super::read_claude_like_hooks(path)
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/hk-core/src/adapter/grok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ impl GrokAdapter {
event: event.canonical.to_string(),
matcher: matcher.clone(),
command,
kind: super::HookKind::Command,
enabled,
},
));
Expand Down
4 changes: 4 additions & 0 deletions crates/hk-core/src/adapter/hermes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,17 @@ impl AgentAdapter for HermesAdapter {
event: event.to_string(),
matcher,
command: command.to_string(),
kind: super::HookKind::Command,
enabled: true,
});
}
}
out
}

fn hook_tool_names(&self, _event: &str) -> super::hook_tools::ToolNames {
super::hook_tools::ToolNames::Hermes
}
fn translate_hook_event(&self, event: &str) -> Option<String> {
super::hook_events::to_hermes(event)
}
Expand Down
Loading
Loading