UNPKG

gaunt-sloth-assistant

Version:

[![Tests and Lint](https://github.com/Galvanized-Pukeko/gaunt-sloth-assistant/actions/workflows/unit-tests.yml/badge.svg)](https://github.com/Galvanized-Pukeko/gaunt-sloth-assistant/actions/workflows/unit-tests.yml) [![Integration Tests](https://github.co

100 lines 4.09 kB
/** * @packageDocumentation * Middleware registry for Gaunt Sloth Assistant. * * This module provides factory functions for creating predefined middleware instances * and a resolver to convert middleware configurations into middleware objects. */ import { displayWarning } from '#src/utils/consoleUtils.js'; import { debugLog } from '#src/utils/debugUtils.js'; import { anthropicPromptCachingMiddleware, summarizationMiddleware, } from 'langchain'; /** * Create Anthropic prompt caching middleware. * This middleware adds cache control headers to reduce API costs. * * @param config - Configuration for the middleware * @param gthConfig - Full Gaunt Sloth configuration * @returns Middleware object */ export async function createAnthropicPromptCachingMiddleware(config, _) { debugLog(`Creating Anthropic prompt caching middleware with TTL: ${config.ttl || 'default'}`); // Dynamic import for async initialization return Promise.resolve(anthropicPromptCachingMiddleware({ ttl: config.ttl })); } /** * Create summarization middleware. * This middleware automatically condenses conversation history when approaching token limits. * * @param config - Configuration for the middleware * @param gthConfig - Full Gaunt Sloth configuration * @returns Middleware object */ export async function createSummarizationMiddleware(config, gthConfig) { debugLog('Creating summarization middleware'); return Promise.resolve(summarizationMiddleware({ model: config.model || gthConfig.llm, maxTokensBeforeSummary: config.maxTokensBeforeSummary || 10000, messagesToKeep: config.messagesToKeep, summaryPrompt: config.summaryPrompt, })); } /** * Resolve middleware configuration into middleware instances. * Converts string identifiers and config objects into actual middleware. * * @param configs - Array of middleware configurations * @param gthConfig - Full Gaunt Sloth configuration * @returns Array of middleware instances */ export async function resolveMiddleware(configs, gthConfig) { if (!configs || configs.length === 0) { return []; } const middleware = []; // List of predefined middleware names const predefinedNames = ['anthropic-prompt-caching', 'summarization']; for (const config of configs) { try { // Handle string configuration (predefined middleware with defaults) if (typeof config === 'string') { middleware.push(await createPredefinedMiddleware(config, {}, gthConfig)); } // Handle predefined middleware with custom settings else if (typeof config === 'object' && 'name' in config && // eslint-disable-next-line @typescript-eslint/no-explicit-any predefinedNames.includes(config.name)) { const { name, ...settings } = config; middleware.push(await createPredefinedMiddleware(name, settings, gthConfig)); } // Handle custom middleware object (JS config only) else { debugLog('Adding custom middleware'); middleware.push(config); } } catch (error) { displayWarning(`Failed to create middleware: ${error instanceof Error ? error.message : String(error)}`); } } return middleware; } /** * Create a predefined middleware instance by name. * * @param name - Name of the predefined middleware * @param settings - Configuration settings for the middleware * @param gthConfig - Full Gaunt Sloth configuration * @returns Middleware instance */ async function createPredefinedMiddleware(name, settings, gthConfig) { switch (name) { case 'anthropic-prompt-caching': return createAnthropicPromptCachingMiddleware(settings, gthConfig); case 'summarization': return createSummarizationMiddleware(settings, gthConfig); default: throw new Error(`Unknown predefined middleware: ${name}`); } } //# sourceMappingURL=registry.js.map