Skip to content

Commit 463650d

Browse files
better way to retrieve configs
1 parent ecce952 commit 463650d

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

libs/langchain/src/agents/middlewareAgent/middleware/promptCaching.ts

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,25 @@ const contextSchema = z.object({
1515
* Whether to enable prompt caching.
1616
* @default true
1717
*/
18-
enableCaching: z.boolean().default(DEFAULT_ENABLE_CACHING),
18+
enableCaching: z.boolean().optional(),
1919
/**
2020
* The time-to-live for the cached prompt.
2121
* @default "5m"
2222
*/
23-
ttl: z.enum(["5m", "1h"]).default(DEFAULT_TTL),
23+
ttl: z.enum(["5m", "1h"]).optional(),
2424
/**
2525
* The minimum number of messages required before caching is applied.
2626
* @default 3
2727
*/
28-
minMessagesToCache: z.number().default(DEFAULT_MIN_MESSAGES_TO_CACHE),
28+
minMessagesToCache: z.number().optional(),
2929
/**
3030
* The behavior to take when an unsupported model is used.
3131
* - "ignore" will ignore the unsupported model and continue without caching.
3232
* - "warn" will warn the user and continue without caching.
3333
* - "raise" will raise an error and stop the agent.
3434
* @default "warn"
3535
*/
36-
unsupportedModelBehavior: z
37-
.enum(["ignore", "warn", "raise"])
38-
.default(DEFAULT_UNSUPPORTED_MODEL_BEHAVIOR),
36+
unsupportedModelBehavior: z.enum(["ignore", "warn", "raise"]).optional(),
3937
});
4038
export type PromptCachingMiddlewareConfig = Partial<
4139
InferInteropZodInput<typeof contextSchema>
@@ -174,32 +172,21 @@ export function anthropicPromptCachingMiddleware(
174172
contextSchema,
175173
modifyModelRequest: (request, state, runtime) => {
176174
/**
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
180176
*/
181177
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;
189182
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;
196186
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;
203190

204191
// Skip if caching is disabled
205192
if (!enableCaching || !request.model) {

0 commit comments

Comments
 (0)