contextWindowMiddlewareDef function
The middleware definition to register from the plugin's
GenkitPlugin.middleware hook.
The config is a plain Map<String, dynamic> ({maxInputTokens?, reserveOutputTokens?}) rather than a schemantic type. Genkit serializes a
middleware ref's config through _configToMap (which calls toJson())
before it reaches the factory on the reflection/Dev-UI path; a Map survives
that verbatim, whereas a plain value class with no toJson() would be
nulled out and drop every trimContext argument. A Map keeps the config
intact on both the in-process and serialized paths with no code
generation.
Implementation
GenerateMiddlewareDef<Map<String, dynamic>> contextWindowMiddlewareDef() {
return defineMiddleware<Map<String, dynamic>>(
name: kContextWindowMiddlewareName,
create: (config, ctx) => ContextWindowMiddleware(
maxInputTokens: (config?['maxInputTokens'] as num?)?.toInt(),
reserveOutputTokens: (config?['reserveOutputTokens'] as num?)?.toInt(),
),
);
}