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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = ["Amazon Q CLI Team ([email protected])", "Chay Nabors (nabochay@amazon
edition = "2024"
homepage = "https://aws.amazon.com/q/"
publish = false
version = "1.16.1"
version = "1.16.2"
license = "MIT OR Apache-2.0"

[workspace.dependencies]
Expand Down
16 changes: 12 additions & 4 deletions crates/chat-cli/src/cli/agent/hook.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::fmt::Display;

use schemars::JsonSchema;
Expand All @@ -11,23 +10,26 @@ const DEFAULT_TIMEOUT_MS: u64 = 30_000;
const DEFAULT_MAX_OUTPUT_SIZE: usize = 1024 * 10;
const DEFAULT_CACHE_TTL_SECONDS: u64 = 0;

#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, JsonSchema)]
pub struct Hooks(HashMap<HookTrigger, Hook>);

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq, JsonSchema, Hash)]
#[serde(rename_all = "camelCase")]
pub enum HookTrigger {
/// Triggered during agent spawn
AgentSpawn,
/// Triggered per user message submission
UserPromptSubmit,
/// Triggered before tool execution
PreToolUse,
/// Triggered after tool execution
PostToolUse,
}

impl Display for HookTrigger {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HookTrigger::AgentSpawn => write!(f, "agentSpawn"),
HookTrigger::UserPromptSubmit => write!(f, "userPromptSubmit"),
HookTrigger::PreToolUse => write!(f, "preToolUse"),
HookTrigger::PostToolUse => write!(f, "postToolUse"),
}
}
}
Expand Down Expand Up @@ -61,6 +63,11 @@ pub struct Hook {
#[serde(default = "Hook::default_cache_ttl_seconds")]
pub cache_ttl_seconds: u64,

/// Optional glob matcher for hook
/// Currently used for matching tool name of PreToolUse and PostToolUse hook
#[serde(skip_serializing_if = "Option::is_none")]
pub matcher: Option<String>,

#[schemars(skip)]
#[serde(default, skip_serializing)]
pub source: Source,
Expand All @@ -73,6 +80,7 @@ impl Hook {
timeout_ms: Self::default_timeout_ms(),
max_output_size: Self::default_max_output_size(),
cache_ttl_seconds: Self::default_cache_ttl_seconds(),
matcher: None,
source,
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/chat-cli/src/cli/agent/legacy/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl From<LegacyHook> for Option<Hook> {
timeout_ms: value.timeout_ms,
max_output_size: value.max_output_size,
cache_ttl_seconds: value.cache_ttl_seconds,
matcher: None,
source: Default::default(),
})
}
Expand Down
77 changes: 72 additions & 5 deletions crates/chat-cli/src/cli/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ mod tests {
use serde_json::json;

use super::*;
use crate::cli::agent::hook::Source;
const INPUT: &str = r#"
{
"name": "some_agent",
Expand All @@ -968,21 +969,21 @@ mod tests {
"fetch": { "command": "fetch3.1", "args": [] },
"git": { "command": "git-mcp", "args": [] }
},
"tools": [
"tools": [
"@git"
],
"toolAliases": {
"@gits/some_tool": "some_tool2"
},
"allowedTools": [
"fs_read",
"allowedTools": [
"fs_read",
"@fetch",
"@gits/git_status"
],
"resources": [
"resources": [
"file://~/my-genai-prompts/unittest.md"
],
"toolsSettings": {
"toolsSettings": {
"fs_write": { "allowedPaths": ["~/**"] },
"@git/git_status": { "git_user": "$GIT_USER" }
}
Expand Down Expand Up @@ -1353,4 +1354,70 @@ mod tests {

assert_eq!(agents.get_active().and_then(|a| a.model.as_ref()), None);
}

#[test]
fn test_agent_with_hooks() {
let agent_json = json!({
"name": "test-agent",
"hooks": {
"agentSpawn": [
{
"command": "git status"
}
],
"preToolUse": [
{
"matcher": "fs_write",
"command": "validate-tool.sh"
},
{
"matcher": "fs_read",
"command": "enforce-tdd.sh"
}
],
"postToolUse": [
{
"matcher": "fs_write",
"command": "format-python.sh"
}
]
}
});

let agent: Agent = serde_json::from_value(agent_json).expect("Failed to deserialize agent");

// Verify agent name
assert_eq!(agent.name, "test-agent");

// Verify agentSpawn hook
assert!(agent.hooks.contains_key(&HookTrigger::AgentSpawn));
let agent_spawn_hooks = &agent.hooks[&HookTrigger::AgentSpawn];
assert_eq!(agent_spawn_hooks.len(), 1);
assert_eq!(agent_spawn_hooks[0].command, "git status");
assert_eq!(agent_spawn_hooks[0].matcher, None);

// Verify preToolUse hooks
assert!(agent.hooks.contains_key(&HookTrigger::PreToolUse));
let pre_tool_hooks = &agent.hooks[&HookTrigger::PreToolUse];
assert_eq!(pre_tool_hooks.len(), 2);

assert_eq!(pre_tool_hooks[0].command, "validate-tool.sh");
assert_eq!(pre_tool_hooks[0].matcher, Some("fs_write".to_string()));

assert_eq!(pre_tool_hooks[1].command, "enforce-tdd.sh");
assert_eq!(pre_tool_hooks[1].matcher, Some("fs_read".to_string()));

// Verify postToolUse hooks
assert!(agent.hooks.contains_key(&HookTrigger::PostToolUse));

// Verify default values are set correctly
for hooks in agent.hooks.values() {
for hook in hooks {
assert_eq!(hook.timeout_ms, 30_000);
assert_eq!(hook.max_output_size, 10_240);
assert_eq!(hook.cache_ttl_seconds, 0);
assert_eq!(hook.source, Source::Agent);
}
}
}
}
6 changes: 6 additions & 0 deletions crates/chat-cli/src/cli/chat/cli/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ impl ClearArgs {
if let Some(cm) = session.conversation.context_manager.as_mut() {
cm.hook_executor.cache.clear();
}

// Reset pending tool state to prevent orphaned tool approval prompts
session.tool_uses.clear();
session.pending_tool_index = None;
session.tool_turn_start_time = None;

execute!(
session.stderr,
style::SetForegroundColor(Color::Green),
Expand Down
Loading
Loading