1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
impl Client {
/// Starts a request from a text prompt, applying UserPromptSubmit hooks first.
/// The prompt enters history before HTTP I/O, even on transport failure.
/// Empty prompts are retained for tool continuation. Starting a new request
/// discards pending output from the previous one.
pub async fn send(&mut self, prompt: &str) -> Result<()> {
use crate::hooks::UserPromptSubmitEvent;
self.interrupted.store(false, Ordering::SeqCst);
self.discard_pending_output();
let mut final_prompt = prompt.to_string();
if !self.options.hooks().user_prompt_submit.is_empty() {
let history_snapshot = serialize_history_snapshot(&self.history)?;
let event = UserPromptSubmitEvent::new(final_prompt.clone(), history_snapshot);
if let Some(decision) = self.options.hooks().execute_user_prompt_submit(event).await {
if !decision.continue_execution() {
return Err(Error::other(format!(
"Prompt blocked by hook: {}",
decision.reason().unwrap_or("")
)));
}
if let Some(modified) = decision.modified_prompt() {
final_prompt = modified.to_string();
}
}
}
self.history.push(Message::user(final_prompt));
self.start_request().await
}
// The empty user turn that continues an automatic tool loop. The caller never submitted it,
// so UserPromptSubmit hooks must not see, block or rewrite it; `send("")` is still the
// caller's own spelling of a manual continuation and goes through the hooks.
async fn continue_after_tools(&mut self) -> Result<()> {
self.discard_pending_output();
self.history.push(Message::user(String::new()));
self.start_request().await
}
}