use cancellation token in claude API as well

This commit is contained in:
Chris Beck
2025-12-14 11:48:46 -07:00
parent 14a56cecfe
commit d025b8f587
+16 -10
View File
@@ -358,15 +358,17 @@ impl ClaudeAssistant {
tools: tools.clone(), tools: tools.clone(),
}; };
let response = self let response = tokio::select! {
.client result = self
.post(&self.config.claude_api_url) .client
.header("x-api-key", &self.api_key) .post(&self.config.claude_api_url)
.header("anthropic-version", ANTHROPIC_API_VERSION) .header("x-api-key", &self.api_key)
.header("content-type", "application/json") .header("anthropic-version", ANTHROPIC_API_VERSION)
.json(&request_body) .header("content-type", "application/json")
.send() .json(&request_body)
.await?; .send() => result?,
_ = cancel.cancelled() => return Ok(None),
};
if !response.status().is_success() { if !response.status().is_success() {
let error: ErrorResponse = response.json().await?; let error: ErrorResponse = response.json().await?;
@@ -394,7 +396,11 @@ impl ClaudeAssistant {
} }
info!("Claude tool use: {}({})", name, input); info!("Claude tool use: {}({})", name, input);
let (result_text, is_error) = match executor.execute(name, input).await { let tool_result = tokio::select! {
result = executor.execute(name, input) => result,
_ = cancel.cancelled() => return Ok(None),
};
let (result_text, is_error) = match tool_result {
Ok(tool_result) => { Ok(tool_result) => {
info!("Tool result: {}", tool_result.text); info!("Tool result: {}", tool_result.text);
attachments.extend(tool_result.attachments); attachments.extend(tool_result.attachments);