@@ -15,27 +15,25 @@ const contextSchema = z.object({
15
15
* Whether to enable prompt caching.
16
16
* @default true
17
17
*/
18
- enableCaching : z . boolean ( ) . default ( DEFAULT_ENABLE_CACHING ) ,
18
+ enableCaching : z . boolean ( ) . optional ( ) ,
19
19
/**
20
20
* The time-to-live for the cached prompt.
21
21
* @default "5m"
22
22
*/
23
- ttl : z . enum ( [ "5m" , "1h" ] ) . default ( DEFAULT_TTL ) ,
23
+ ttl : z . enum ( [ "5m" , "1h" ] ) . optional ( ) ,
24
24
/**
25
25
* The minimum number of messages required before caching is applied.
26
26
* @default 3
27
27
*/
28
- minMessagesToCache : z . number ( ) . default ( DEFAULT_MIN_MESSAGES_TO_CACHE ) ,
28
+ minMessagesToCache : z . number ( ) . optional ( ) ,
29
29
/**
30
30
* The behavior to take when an unsupported model is used.
31
31
* - "ignore" will ignore the unsupported model and continue without caching.
32
32
* - "warn" will warn the user and continue without caching.
33
33
* - "raise" will raise an error and stop the agent.
34
34
* @default "warn"
35
35
*/
36
- unsupportedModelBehavior : z
37
- . enum ( [ "ignore" , "warn" , "raise" ] )
38
- . default ( DEFAULT_UNSUPPORTED_MODEL_BEHAVIOR ) ,
36
+ unsupportedModelBehavior : z . enum ( [ "ignore" , "warn" , "raise" ] ) . optional ( ) ,
39
37
} ) ;
40
38
export type PromptCachingMiddlewareConfig = Partial <
41
39
InferInteropZodInput < typeof contextSchema >
@@ -174,32 +172,21 @@ export function anthropicPromptCachingMiddleware(
174
172
contextSchema,
175
173
modifyModelRequest : ( request , state , runtime ) => {
176
174
/**
177
- * If the runtime values match the schema default values, use the middleware option
178
- * values otherwise use the runtime values. This allows to apply general configurations
179
- * for all invocations, and override them for specific invocations.
175
+ * Prefer runtime context values over middleware options values over defaults
180
176
*/
181
177
const enableCaching =
182
- runtime . context . enableCaching === DEFAULT_ENABLE_CACHING
183
- ? middlewareOptions ?. enableCaching ?? runtime . context . enableCaching
184
- : runtime . context . enableCaching ?? middlewareOptions ?. enableCaching ;
185
- const ttl =
186
- runtime . context . ttl === DEFAULT_TTL
187
- ? middlewareOptions ?. ttl ?? runtime . context . ttl
188
- : runtime . context . ttl ?? middlewareOptions ?. ttl ;
178
+ runtime . context . enableCaching ??
179
+ middlewareOptions ?. enableCaching ??
180
+ DEFAULT_ENABLE_CACHING ;
181
+ const ttl = runtime . context . ttl ?? middlewareOptions ?. ttl ?? DEFAULT_TTL ;
189
182
const minMessagesToCache =
190
- runtime . context . minMessagesToCache === DEFAULT_MIN_MESSAGES_TO_CACHE
191
- ? middlewareOptions ?. minMessagesToCache ??
192
- runtime . context . minMessagesToCache
193
- : runtime . context . minMessagesToCache ??
194
- middlewareOptions ?. minMessagesToCache ??
195
- DEFAULT_MIN_MESSAGES_TO_CACHE ;
183
+ runtime . context . minMessagesToCache ??
184
+ middlewareOptions ?. minMessagesToCache ??
185
+ DEFAULT_MIN_MESSAGES_TO_CACHE ;
196
186
const unsupportedModelBehavior =
197
- runtime . context . unsupportedModelBehavior ===
198
- DEFAULT_UNSUPPORTED_MODEL_BEHAVIOR
199
- ? middlewareOptions ?. unsupportedModelBehavior ??
200
- runtime . context . unsupportedModelBehavior
201
- : runtime . context . unsupportedModelBehavior ??
202
- middlewareOptions ?. unsupportedModelBehavior ;
187
+ runtime . context . unsupportedModelBehavior ??
188
+ middlewareOptions ?. unsupportedModelBehavior ??
189
+ DEFAULT_UNSUPPORTED_MODEL_BEHAVIOR ;
203
190
204
191
// Skip if caching is disabled
205
192
if ( ! enableCaching || ! request . model ) {
0 commit comments