Skip to content

Commit f413816

Browse files
committed
add hooks in /context
1 parent 6e855da commit f413816

File tree

1 file changed

+85
-46
lines changed

1 file changed

+85
-46
lines changed

crates/q_chat/src/lib.rs

Lines changed: 85 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,9 @@ impl ChatContext {
16431643
if let Some(context_manager) = &mut self.conversation_state.context_manager {
16441644
match subcommand {
16451645
command::ContextSubcommand::Show { expand } => {
1646+
fn map_chat_error(e: ErrReport) -> ChatError {
1647+
ChatError::Custom(e.to_string().into())
1648+
}
16461649
// Display global context
16471650
execute!(
16481651
self.output,
@@ -1682,6 +1685,26 @@ impl ChatContext {
16821685
}
16831686
}
16841687

1688+
queue!(
1689+
self.output,
1690+
style::SetAttribute(Attribute::Bold),
1691+
style::SetForegroundColor(Color::DarkYellow),
1692+
style::Print("\n 🔧 Hooks:\n")
1693+
)?;
1694+
Self::print_hook_section(
1695+
&mut self.output,
1696+
&context_manager.global_config.hooks,
1697+
HookTrigger::ConversationStart,
1698+
)
1699+
.map_err(map_chat_error)?;
1700+
1701+
Self::print_hook_section(
1702+
&mut self.output,
1703+
&context_manager.global_config.hooks,
1704+
HookTrigger::PerPrompt,
1705+
)
1706+
.map_err(map_chat_error)?;
1707+
16851708
// Display profile context
16861709
execute!(
16871710
self.output,
@@ -1721,6 +1744,26 @@ impl ChatContext {
17211744
execute!(self.output, style::Print("\n"))?;
17221745
}
17231746

1747+
queue!(
1748+
self.output,
1749+
style::SetAttribute(Attribute::Bold),
1750+
style::SetForegroundColor(Color::DarkYellow),
1751+
style::Print(" 🔧 Hooks:\n")
1752+
)?;
1753+
Self::print_hook_section(
1754+
&mut self.output,
1755+
&context_manager.profile_config.hooks,
1756+
HookTrigger::ConversationStart,
1757+
)
1758+
.map_err(map_chat_error)?;
1759+
Self::print_hook_section(
1760+
&mut self.output,
1761+
&context_manager.profile_config.hooks,
1762+
HookTrigger::PerPrompt,
1763+
)
1764+
.map_err(map_chat_error)?;
1765+
execute!(self.output, style::Print("\n"))?;
1766+
17241767
if global_context_files.is_empty() && profile_context_files.is_empty() {
17251768
execute!(
17261769
self.output,
@@ -2049,48 +2092,6 @@ impl ChatContext {
20492092
},
20502093
}
20512094
} else {
2052-
fn print_hook_section(
2053-
output: &mut impl Write,
2054-
hooks: &HashMap<String, Hook>,
2055-
trigger: HookTrigger,
2056-
) -> Result<()> {
2057-
let section = match trigger {
2058-
HookTrigger::ConversationStart => "Conversation Start",
2059-
HookTrigger::PerPrompt => "Per Prompt",
2060-
};
2061-
let hooks: Vec<(&String, &Hook)> =
2062-
hooks.iter().filter(|(_, h)| h.trigger == trigger).collect();
2063-
2064-
queue!(
2065-
output,
2066-
style::SetForegroundColor(Color::Cyan),
2067-
style::Print(format!(" {section}:\n")),
2068-
style::SetForegroundColor(Color::Reset),
2069-
)?;
2070-
2071-
if hooks.is_empty() {
2072-
queue!(
2073-
output,
2074-
style::SetForegroundColor(Color::DarkGrey),
2075-
style::Print(" <none>\n"),
2076-
style::SetForegroundColor(Color::Reset)
2077-
)?;
2078-
} else {
2079-
for (name, hook) in hooks {
2080-
if hook.disabled {
2081-
queue!(
2082-
output,
2083-
style::SetForegroundColor(Color::DarkGrey),
2084-
style::Print(format!(" {} (disabled)\n", name)),
2085-
style::SetForegroundColor(Color::Reset)
2086-
)?;
2087-
} else {
2088-
queue!(output, style::Print(format!(" {}\n", name)),)?;
2089-
}
2090-
}
2091-
}
2092-
Ok(())
2093-
}
20942095
queue!(
20952096
self.output,
20962097
style::SetAttribute(Attribute::Bold),
@@ -2099,13 +2100,13 @@ impl ChatContext {
20992100
style::SetAttribute(Attribute::Reset),
21002101
)?;
21012102

2102-
print_hook_section(
2103+
Self::print_hook_section(
21032104
&mut self.output,
21042105
&context_manager.global_config.hooks,
21052106
HookTrigger::ConversationStart,
21062107
)
21072108
.map_err(map_chat_error)?;
2108-
print_hook_section(
2109+
Self::print_hook_section(
21092110
&mut self.output,
21102111
&context_manager.global_config.hooks,
21112112
HookTrigger::PerPrompt,
@@ -2120,13 +2121,13 @@ impl ChatContext {
21202121
style::SetAttribute(Attribute::Reset),
21212122
)?;
21222123

2123-
print_hook_section(
2124+
Self::print_hook_section(
21242125
&mut self.output,
21252126
&context_manager.profile_config.hooks,
21262127
HookTrigger::ConversationStart,
21272128
)
21282129
.map_err(map_chat_error)?;
2129-
print_hook_section(
2130+
Self::print_hook_section(
21302131
&mut self.output,
21312132
&context_manager.profile_config.hooks,
21322133
HookTrigger::PerPrompt,
@@ -2703,6 +2704,44 @@ impl ChatContext {
27032704
})
27042705
}
27052706

2707+
fn print_hook_section(output: &mut impl Write, hooks: &HashMap<String, Hook>, trigger: HookTrigger) -> Result<()> {
2708+
let section = match trigger {
2709+
HookTrigger::ConversationStart => "Conversation Start",
2710+
HookTrigger::PerPrompt => "Per Prompt",
2711+
};
2712+
let hooks: Vec<(&String, &Hook)> = hooks.iter().filter(|(_, h)| h.trigger == trigger).collect();
2713+
2714+
queue!(
2715+
output,
2716+
style::SetForegroundColor(Color::Cyan),
2717+
style::Print(format!(" {section}:\n")),
2718+
style::SetForegroundColor(Color::Reset),
2719+
)?;
2720+
2721+
if hooks.is_empty() {
2722+
queue!(
2723+
output,
2724+
style::SetForegroundColor(Color::DarkGrey),
2725+
style::Print(" <none>\n"),
2726+
style::SetForegroundColor(Color::Reset)
2727+
)?;
2728+
} else {
2729+
for (name, hook) in hooks {
2730+
if hook.disabled {
2731+
queue!(
2732+
output,
2733+
style::SetForegroundColor(Color::DarkGrey),
2734+
style::Print(format!(" {} (disabled)\n", name)),
2735+
style::SetForegroundColor(Color::Reset)
2736+
)?;
2737+
} else {
2738+
queue!(output, style::Print(format!(" {}\n", name)),)?;
2739+
}
2740+
}
2741+
}
2742+
Ok(())
2743+
}
2744+
27062745
async fn tool_use_execute(&mut self, mut tool_uses: Vec<QueuedTool>) -> Result<ChatState, ChatError> {
27072746
// Verify tools have permissions.
27082747
for (index, tool) in tool_uses.iter_mut().enumerate() {

0 commit comments

Comments
 (0)