UNPKG

@juspay/neurolink

Version:

Universal AI Development Platform with working MCP integration, multi-provider support, voice (TTS/STT/realtime), and professional CLI. 58+ external MCP servers discoverable, multimodal file processing, RAG pipelines. Build, test, and deploy AI applicatio

283 lines (282 loc) 9.8 kB
/** * Dynamic Argument Resolution Utilities * * Provides utilities for resolving dynamic arguments to their actual values, * with support for caching, memoization, fallbacks, and conditional resolution. * * @module dynamic/dynamicResolver */ import type { DynamicArgument, DynamicResolutionContext, ResolutionOptions, ResolutionResult, DynamicConfig, ResolvedConfig } from "../types/index.js"; /** * Resolution cache for dynamic arguments */ declare class ResolutionCache { private cache; private cleanupInterval; constructor(cleanupIntervalMs?: number); get<T>(key: string): T | undefined; set<T>(key: string, value: T, ttl: number): void; delete(key: string): boolean; clear(): void; size(): number; private startCleanup; destroy(): void; } declare const globalCache: ResolutionCache; /** * Resolve a dynamic argument to its actual value * * @template T - The expected resolved type * @param argument - The dynamic argument to resolve * @param context - Resolution context (optional for static values) * @param options - Resolution options * @returns Resolution result with value and metadata * * @example Resolve static value * ```typescript * const result = await resolveDynamicArgument("gpt-4o"); * console.log(result.value); // "gpt-4o" * console.log(result.resolutionType); // "static" * ``` * * @example Resolve context-aware function * ```typescript * const modelSelector = ({ requestContext }) => * requestContext.tenant?.plan === "enterprise" ? "claude-3-opus" : "claude-3-sonnet"; * * const result = await resolveDynamicArgument(modelSelector, { * requestContext: { requestId: "123", tenant: { id: "t1", plan: "enterprise" } } * }); * console.log(result.value); // "claude-3-opus" * ``` */ export declare function resolveDynamicArgument<T>(argument: DynamicArgument<T>, context?: DynamicResolutionContext, options?: ResolutionOptions): Promise<ResolutionResult<T>>; /** * Resolve multiple dynamic arguments in parallel * * @example * ```typescript * const [model, temperature] = await resolveDynamicArguments( * [ * ({ requestContext }) => requestContext.user?.preferences?.preferredModel || "gpt-4o", * 0.7, * ], * context * ); * ``` */ export declare function resolveDynamicArguments<T extends readonly unknown[]>(arguments_: { [K in keyof T]: DynamicArgument<T[K]>; }, context?: DynamicResolutionContext, options?: ResolutionOptions): Promise<{ [K in keyof T]: T[K]; }>; /** * Resolve all properties of a dynamic configuration object * * @example * ```typescript * const dynamicConfig = { * model: ({ requestContext }) => requestContext.tenant?.settings?.defaultModel || "gpt-4o", * temperature: 0.7, * maxTokens: async () => (await fetchConfig()).maxTokens, * }; * * const resolved = await resolveDynamicConfig(dynamicConfig, context); * // resolved.model, resolved.temperature, resolved.maxTokens are all resolved values * ``` */ export declare function resolveDynamicConfig<T extends Record<string, unknown>>(config: DynamicConfig<T>, context?: DynamicResolutionContext, options?: ResolutionOptions): Promise<ResolvedConfig<T>>; /** * Create a memoized dynamic argument that caches its result * * @example * ```typescript * const expensiveModelSelector = memoizeDynamicArgument( * async ({ requestContext }) => { * const config = await fetchTenantConfig(requestContext.tenant?.id); * return config.preferredModel; * }, * { cacheTtl: 300000 } // Cache for 5 minutes * ); * ``` */ export declare function memoizeDynamicArgument<T>(argument: DynamicArgument<T>, options?: { cacheTtl?: number; cacheKey?: string; }): DynamicArgument<T>; /** * Create a dynamic argument with fallback chain * * @example * ```typescript * const modelWithFallback = withFallback( * ({ requestContext }) => requestContext.user?.preferences?.preferredModel, * ({ requestContext }) => requestContext.tenant?.settings?.defaultModel, * "gpt-4o" // Final static fallback * ); * ``` */ export declare function withFallback<T>(...arguments_: DynamicArgument<T | undefined | null>[]): DynamicArgument<T>; /** * Create a conditional dynamic argument * * @example * ```typescript * const conditionalModel = conditional( * ({ requestContext }) => requestContext.tenant?.plan === "enterprise", * "claude-3-opus", // If true * "claude-3-sonnet" // If false * ); * ``` */ export declare function conditional<T>(condition: DynamicArgument<boolean>, ifTrue: DynamicArgument<T>, ifFalse: DynamicArgument<T>): DynamicArgument<T>; /** * Create a mapped dynamic argument that transforms the result * * @example * ```typescript * const upperCaseModel = mapDynamicArgument( * ({ requestContext }) => requestContext.user?.preferences?.preferredModel, * (model) => model?.toUpperCase() * ); * ``` */ export declare function mapDynamicArgument<T, U>(argument: DynamicArgument<T>, transform: (value: T) => U | Promise<U>): DynamicArgument<U>; /** * Create a dynamic argument that combines multiple arguments * * @example * ```typescript * const combinedConfig = combineDynamicArguments( * [ * ({ requestContext }) => requestContext.user?.preferences?.preferredModel, * ({ requestContext }) => requestContext.tenant?.settings?.defaultTemperature, * ], * ([model, temperature]) => ({ model: model || "gpt-4o", temperature: temperature || 0.7 }) * ); * ``` */ export declare function combineDynamicArguments<T extends readonly unknown[], U>(arguments_: { [K in keyof T]: DynamicArgument<T[K]>; }, combiner: (values: T) => U | Promise<U>): DynamicArgument<U>; /** * Check if a value contains any dynamic arguments (is a function) */ export declare function hasDynamicArgument<T>(value: DynamicArgument<T>): boolean; /** * Check if an object has any dynamic properties */ export declare function hasDynamicProperties<T extends Record<string, unknown>>(config: DynamicConfig<T>): boolean; /** * Clear the global resolution cache */ export declare function clearResolutionCache(): void; /** * Get resolution cache statistics */ export declare function getResolutionCacheStats(): { size: number; }; /** * Destroy the resolver (cleanup intervals, etc.) */ export declare function destroyResolver(): void; /** * Interpolate environment variables in a string * * Supports syntax: * - ${VAR} - Simple substitution * - ${VAR:-default} - Use default if VAR is unset or empty * - ${VAR:+replacement} - Use replacement if VAR is set and non-empty * * @example * ```typescript * interpolateEnvVars("Model: ${DEFAULT_MODEL:-gpt-4o}"); * // Returns "Model: gpt-4o" if DEFAULT_MODEL is not set * * interpolateEnvVars("API Key: ${OPENAI_API_KEY}"); * // Returns "API Key: sk-xxx..." if OPENAI_API_KEY is set * * interpolateEnvVars("Debug: ${DEBUG:+enabled}"); * // Returns "Debug: enabled" if DEBUG is set, "Debug: " otherwise * ``` */ export declare function interpolateEnvVars(input: string, customEnv?: Record<string, string | undefined>): string; /** * Create a dynamic argument that interpolates environment variables * * @example * ```typescript * const model = fromEnv("${PREFERRED_MODEL:-gpt-4o}"); * // Resolves to value of PREFERRED_MODEL or "gpt-4o" as fallback * ``` */ export declare function fromEnv(template: string): DynamicArgument<string>; /** * Create a dynamic argument from a single environment variable * * @example * ```typescript * const apiKey = envVar("OPENAI_API_KEY"); * // Resolves to value of OPENAI_API_KEY or undefined * * const model = envVar("DEFAULT_MODEL", "gpt-4o"); * // Resolves to DEFAULT_MODEL value or "gpt-4o" as default * ``` */ export declare function envVar<T extends string = string>(name: string, defaultValue?: T): DynamicArgument<T | undefined>; /** * Create a dynamic argument that selects from environment-based configurations * * @example * ```typescript * const model = envSwitch("NODE_ENV", { * development: "gpt-4o-mini", * production: "gpt-4o", * test: "gpt-3.5-turbo", * }, "gpt-4o-mini"); * ``` */ export declare function envSwitch<T>(envVarName: string, options: Record<string, T>, defaultValue: T): DynamicArgument<T>; /** * Create a dynamic argument that parses a JSON value from an environment variable * * @example * ```typescript * // If RATE_LIMITS='{"requestsPerMinute": 100, "tokensPerDay": 50000}' * const rateLimits = envJson<RateLimits>("RATE_LIMITS", { requestsPerMinute: 10 }); * ``` */ export declare function envJson<T>(name: string, defaultValue?: T): DynamicArgument<T | undefined>; /** * Create a dynamic argument that reads a number from an environment variable * * @example * ```typescript * const maxTokens = envNumber("MAX_TOKENS", 1000); * const temperature = envNumber("TEMPERATURE", 0.7); * ``` */ export declare function envNumber(name: string, defaultValue?: number): DynamicArgument<number | undefined>; /** * Create a dynamic argument that reads a boolean from an environment variable * * @example * ```typescript * const enableDebug = envBoolean("DEBUG", false); * const enableTools = envBoolean("ENABLE_TOOLS", true); * ``` */ export declare function envBoolean(name: string, defaultValue?: boolean): DynamicArgument<boolean | undefined>; /** * Create a dynamic argument that reads a comma-separated list from an environment variable * * @example * ```typescript * // If ALLOWED_PROVIDERS='openai,anthropic,vertex' * const providers = envList("ALLOWED_PROVIDERS", ["openai"]); * // Returns ["openai", "anthropic", "vertex"] * ``` */ export declare function envList(name: string, defaultValue?: string[], separator?: string): DynamicArgument<string[] | undefined>; export { globalCache as resolutionCache };