pub struct MemoizingMiddleware { /* private fields */ }Expand description
Deduplicates repeat tool calls by (tool_name, hash(canonical input)).
On a cache hit (memoized tool + key present + not TTL-expired),
returns the prior ToolDispatchResult with a [cached] marker
appended and skips the inner dispatch entirely. On a cache miss,
runs the tool and stores the result. On a write-class tool call,
invalidates cache entries whose paths (per the PathExtractor)
intersect the write’s paths.
Only successful results are cached (is_error == false); errors
always re-run. Entries expire after ttl_turns turns.
§Cache size
The cache grows with distinct (tool, input) pairs up to TTL
expiry. There is no LRU eviction in this middleware; if a long
session produces many distinct inputs, pair it with a tighter
ttl_turns.
§Registration
See the module docs for the registration pattern.
Implementations§
Source§impl MemoizingMiddleware
impl MemoizingMiddleware
Sourcepub fn new(
tools: Vec<String>,
write_tools: Vec<String>,
path_extractor: Arc<dyn PathExtractor>,
ttl_turns: u32,
) -> Self
pub fn new( tools: Vec<String>, write_tools: Vec<String>, path_extractor: Arc<dyn PathExtractor>, ttl_turns: u32, ) -> Self
Construct a memoizing middleware.
§Arguments
tools— exact-match names of tools whose results to cache.write_tools— exact-match names of tools that trigger path-aware invalidation.path_extractor— required; supplies the per-tool path knowledge the framework can’t infer.ttl_turns— max turns a cache entry is valid (entry expires whencurrent_turn - turn_inserted >= ttl_turns).