Skip to main content

synwire_agent/middleware/
prompt_caching.rs

1//! Prompt caching middleware — marks messages for provider-side caching.
2
3use synwire_core::BoxFuture;
4use synwire_core::agents::error::AgentError;
5use synwire_core::agents::middleware::{Middleware, MiddlewareInput, MiddlewareResult};
6
7/// Middleware that adds cache control hints to the last user message.
8///
9/// Injects `cache_control: { type: "ephemeral" }` onto the last user message
10/// so providers like Anthropic can cache it at that breakpoint.
11#[derive(Debug, Default)]
12pub struct PromptCachingMiddleware;
13
14impl Middleware for PromptCachingMiddleware {
15    fn name(&self) -> &'static str {
16        "prompt_caching"
17    }
18
19    fn process(
20        &self,
21        mut input: MiddlewareInput,
22    ) -> BoxFuture<'_, Result<MiddlewareResult, AgentError>> {
23        Box::pin(async move {
24            // Mark the last user message for caching.
25            if let Some(last) = input
26                .messages
27                .iter_mut()
28                .rev()
29                .find(|m| m.get("role").and_then(|r| r.as_str()) == Some("user"))
30                && let Some(obj) = last.as_object_mut()
31            {
32                let _ = obj.insert(
33                    "cache_control".to_string(),
34                    serde_json::json!({ "type": "ephemeral" }),
35                );
36            }
37            Ok(MiddlewareResult::Continue(input))
38        })
39    }
40}