make prompt caching optional, run clippy

This commit is contained in:
Chris Beck
2025-12-09 09:02:08 -07:00
parent 19b8561b9b
commit 4a143bd6d2
2 changed files with 18 additions and 10 deletions
+3
View File
@@ -45,6 +45,9 @@ pub struct ClaudeConfig {
/// Maximum tool use iterations before giving up.
#[conf(long, env, default_value = "10")]
pub claude_max_iterations: u32,
/// Enable prompt caching (adds cache_control to system prompts).
#[conf(long, env)]
pub prompt_caching: bool,
/// Compaction configuration.
#[conf(flatten, prefix)]
pub compaction: CompactionConfig,
+15 -10
View File
@@ -253,9 +253,11 @@ impl ClaudeWorker {
if !self.summary.is_empty() {
system.push(SystemContent::text(&self.summary));
}
// Mark the last one as cached
if let Some(last) = system.last_mut() {
*last = std::mem::take(last).cached();
// Mark the last one as cached if prompt caching is enabled
if self.config.prompt_caching
&& let Some(last) = system.last_mut()
{
last.set_cached();
}
let request_body = MessagesRequest {
@@ -347,14 +349,16 @@ impl ClaudeWorker {
let mut system: Vec<SystemContent> = self
.system_prompts
.iter()
.map(|text| SystemContent::text(text))
.map(SystemContent::text)
.collect();
if !self.summary.is_empty() {
system.push(SystemContent::text(&self.summary));
}
// Mark the last one as cached
if let Some(last) = system.last_mut() {
*last = std::mem::take(last).cached();
// Mark the last one as cached if prompt caching is enabled
if self.config.prompt_caching
&& let Some(last) = system.last_mut()
{
last.set_cached();
}
for iteration in 0..max_iterations {
@@ -506,9 +510,8 @@ impl SystemContent {
}
}
fn cached(mut self) -> Self {
fn set_cached(&mut self) {
self.cache_control = Some(CacheControl::ephemeral());
self
}
}
@@ -558,7 +561,9 @@ impl MessageContent {
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum ContentBlock {
Text { text: Box<str> },
Text {
text: Box<str>,
},
ToolUse {
id: Box<str>,
name: Box<str>,