@genkit-ai/core
Version:
Genkit AI framework core libraries.
1 lines • 16.9 kB
Source Map (JSON)
{"version":3,"sources":["../src/config.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { NodeSDKConfiguration } from '@opentelemetry/sdk-node';\nimport fs from 'fs';\nimport path from 'path';\nimport { FlowStateStore } from './flowTypes.js';\nimport { LocalFileFlowStateStore } from './localFileFlowStateStore.js';\nimport { logger } from './logging.js';\nimport { PluginProvider } from './plugin.js';\nimport * as registry from './registry.js';\nimport { AsyncProvider } from './registry.js';\nimport {\n LoggerConfig,\n TelemetryConfig,\n TelemetryOptions,\n} from './telemetryTypes.js';\nimport { TraceStore, enableTracingAndMetrics } from './tracing.js';\nimport { LocalFileTraceStore } from './tracing/localFileTraceStore.js';\n\nexport * from './plugin.js';\n\nexport let config: Config;\nexport interface ConfigOptions {\n plugins?: PluginProvider[];\n traceStore?: string;\n flowStateStore?: string;\n enableTracingAndMetrics?: boolean;\n logLevel?: 'error' | 'warn' | 'info' | 'debug';\n promptDir?: string;\n telemetry?: TelemetryOptions;\n defaultModel?: {\n name: string | { name: string };\n config?: Record<string, any>;\n };\n}\n\nclass Config {\n /** Developer-configured options. */\n readonly options: ConfigOptions;\n readonly configuredEnvs = new Set<string>(['dev']);\n\n private telemetryConfig: AsyncProvider<TelemetryConfig>;\n private loggerConfig?: AsyncProvider<LoggerConfig>;\n\n constructor(options: ConfigOptions) {\n this.options = options;\n this.telemetryConfig = async () =>\n <TelemetryConfig>{\n getConfig() {\n return {} as Partial<NodeSDKConfiguration>;\n },\n };\n this.configure();\n }\n\n /**\n * Returns a flow state store instance for the running environment.\n * If no store is configured, will throw an error.\n */\n public async getFlowStateStore(): Promise<FlowStateStore> {\n const flowStateStore = await registry.lookupFlowStateStore(getCurrentEnv());\n if (!flowStateStore) {\n throw new Error('No flow store is configured.');\n }\n return flowStateStore;\n }\n\n /**\n * Returns a trace store instance for the running environment.\n * If no store is configured, will return undefined.\n */\n public async getTraceStore(): Promise<TraceStore | undefined> {\n return await registry.lookupTraceStore(getCurrentEnv());\n }\n\n /**\n * Returns the configuration for exporting Telemetry data for the current\n * environment.\n */\n public getTelemetryConfig(): Promise<TelemetryConfig> {\n return this.telemetryConfig();\n }\n\n /**\n * Configures the system.\n */\n private configure() {\n if (this.options.logLevel) {\n logger.setLogLevel(this.options.logLevel);\n }\n\n this.options.plugins?.forEach((plugin) => {\n logger.debug(`Registering plugin ${plugin.name}...`);\n registry.registerPluginProvider(plugin.name, {\n name: plugin.name,\n async initializer() {\n logger.info(`Initializing plugin ${plugin.name}:`);\n return await plugin.initializer();\n },\n });\n });\n\n if (this.options.telemetry?.logger) {\n const loggerPluginName = this.options.telemetry.logger;\n logger.debug('Registering logging exporters...');\n logger.debug(` - all environments: ${loggerPluginName}`);\n this.loggerConfig = async () =>\n this.resolveLoggerConfig(loggerPluginName);\n }\n\n if (this.options.telemetry?.instrumentation) {\n const telemetryPluginName = this.options.telemetry.instrumentation;\n logger.debug('Registering telemetry exporters...');\n logger.debug(` - all environments: ${telemetryPluginName}`);\n this.telemetryConfig = async () =>\n this.resolveTelemetryConfig(telemetryPluginName);\n }\n\n logger.debug('Registering flow state stores...');\n if (isDevEnv()) {\n registry.registerFlowStateStore(\n 'dev',\n async () => new LocalFileFlowStateStore()\n );\n logger.debug('Registered dev flow state store.');\n }\n if (this.options.flowStateStore) {\n const flowStorePluginName = this.options.flowStateStore;\n logger.debug(` - prod: ${flowStorePluginName}`);\n this.configuredEnvs.add('prod');\n registry.registerFlowStateStore('prod', () =>\n this.resolveFlowStateStore(flowStorePluginName)\n );\n }\n\n logger.debug('Registering trace stores...');\n if (isDevEnv()) {\n registry.registerTraceStore('dev', async () => new LocalFileTraceStore());\n logger.debug('Registered dev trace store.');\n }\n if (this.options.traceStore) {\n const traceStorePluginName = this.options.traceStore;\n logger.debug(` - prod: ${traceStorePluginName}`);\n this.configuredEnvs.add('prod');\n registry.registerTraceStore('prod', () =>\n this.resolveTraceStore(traceStorePluginName)\n );\n if (isDevEnv()) {\n logger.info(\n 'In dev mode `traceStore` is defaulted to local file store.'\n );\n }\n } else {\n logger.info(\n '`traceStore` is not specified in the config; Traces are not going to be persisted in prod.'\n );\n }\n }\n\n /**\n * Sets up the tracing and logging as configured.\n *\n * Note: the logging configuration must come after tracing has been enabled to\n * ensure that all tracing instrumentations are applied.\n * See limitations described here:\n * https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-instrumentation#limitations\n */\n async setupTracingAndLogging() {\n if (this.options.enableTracingAndMetrics) {\n enableTracingAndMetrics(\n await this.getTelemetryConfig(),\n await this.getTraceStore()\n );\n }\n if (this.loggerConfig) {\n logger.init(await this.loggerConfig());\n }\n }\n\n /**\n * Resolves flow state store provided by the specified plugin.\n */\n private async resolveFlowStateStore(pluginName: string) {\n let flowStoreId;\n if (pluginName.includes('/')) {\n const tokens = pluginName.split('/', 2);\n pluginName = tokens[0];\n flowStoreId = tokens[1];\n }\n const plugin = await registry.initializePlugin(pluginName);\n let provider = plugin?.flowStateStore;\n if (!provider) {\n throw new Error(\n 'Unable to resolve provided `flowStateStore` for plugin: ' + pluginName\n );\n }\n if (!Array.isArray(provider)) {\n provider = [provider];\n }\n if (provider.length === 1 && !flowStoreId) {\n return provider[0].value;\n }\n if (provider.length > 1 && !flowStoreId) {\n throw new Error(\n `Plugin ${pluginName} provides more than one flow state store implementation (${provider.map((p) => p.id).join(', ')}), please specify the flow state store id (e.g. \"${pluginName}/${provider[0].id}\")`\n );\n }\n const p = provider.find((p) => p.id === flowStoreId);\n if (!p) {\n throw new Error(\n `Plugin ${pluginName} does not provide flow state store ${flowStoreId}`\n );\n }\n return p.value;\n }\n\n /**\n * Resolves trace store provided by the specified plugin.\n */\n private async resolveTraceStore(pluginName: string) {\n let traceStoreId;\n if (pluginName.includes('/')) {\n const tokens = pluginName.split('/', 2);\n pluginName = tokens[0];\n traceStoreId = tokens[1];\n }\n const plugin = await registry.initializePlugin(pluginName);\n let provider = plugin?.traceStore;\n if (!provider) {\n throw new Error(\n 'Unable to resolve provided `traceStore` for plugin: ' + pluginName\n );\n }\n if (!Array.isArray(provider)) {\n provider = [provider];\n }\n if (provider.length === 1 && !traceStoreId) {\n return provider[0].value;\n }\n if (provider.length > 1 && !traceStoreId) {\n throw new Error(\n `Plugin ${pluginName} provides more than one trace store implementation (${provider.map((p) => p.id).join(', ')}), please specify the trace store id (e.g. \"${pluginName}/${provider[0].id}\")`\n );\n }\n const p = provider.find((p) => p.id === traceStoreId);\n if (!p) {\n throw new Error(\n `Plugin ${pluginName} does not provide trace store ${traceStoreId}`\n );\n }\n return p.value;\n }\n\n /**\n * Resolves the telemetry configuration provided by the specified plugin.\n */\n private async resolveTelemetryConfig(pluginName: string) {\n const plugin = await registry.initializePlugin(pluginName);\n const provider = plugin?.telemetry?.instrumentation;\n\n if (!provider) {\n throw new Error(\n 'Unable to resolve provider `telemetry.instrumentation` for plugin: ' +\n pluginName\n );\n }\n return provider.value;\n }\n\n /**\n * Resolves the logging configuration provided by the specified plugin.\n */\n private async resolveLoggerConfig(pluginName: string) {\n const plugin = await registry.initializePlugin(pluginName);\n const provider = plugin?.telemetry?.logger;\n\n if (!provider) {\n throw new Error(\n 'Unable to resolve provider `telemetry.logger` for plugin: ' +\n pluginName\n );\n }\n return provider.value;\n }\n}\n\n/**\n * Configures Genkit with a set of options. This should be called from `genkit.configig.js`.\n */\nexport function configureGenkit(options: ConfigOptions): Config {\n if (config) {\n logger.warn('configureGenkit was already called');\n }\n config = new Config(options);\n config.setupTracingAndLogging();\n return config;\n}\n\n/**\n * Locates `genkit.config.js` and loads the file so that the config can be registered.\n */\nexport function initializeGenkit(cfg?: Config) {\n // Already initialized.\n if (config || cfg) {\n return;\n }\n const configPath = findGenkitConfig();\n if (!configPath) {\n throw Error(\n 'Unable to find genkit.config.js in any of the parent directories.'\n );\n }\n // Loading the config file will automatically register the config.\n require(configPath);\n}\n\n/**\n * @returns The current environment that the app code is running in.\n */\nexport function getCurrentEnv(): string {\n return process.env.GENKIT_ENV || 'prod';\n}\n\n/** Whether current env is `dev`. */\nexport function isDevEnv(): boolean {\n return getCurrentEnv() === 'dev';\n}\n\n/**\n * Locates `genkit.config.js` and returns the path.\n */\nfunction findGenkitConfig() {\n let current = require?.main?.filename;\n if (!current) {\n throw new Error('Unable to resolve package root.');\n }\n while (path.resolve(current, '..') !== current) {\n if (fs.existsSync(path.resolve(current, 'genkit.config.js'))) {\n return path.resolve(current, 'genkit.config.js');\n }\n current = path.resolve(current, '..');\n }\n return undefined;\n}\n\nexport function __hardResetConfigForTesting() {\n (config as any) = undefined;\n}\n"],"mappings":";;;AAiBA,OAAO,QAAQ;AACf,OAAO,UAAU;AAEjB,SAAS,+BAA+B;AACxC,SAAS,cAAc;AAEvB,YAAY,cAAc;AAO1B,SAAqB,+BAA+B;AACpD,SAAS,2BAA2B;AAEpC,cAAc;AAEP,IAAI;AAeX,MAAM,OAAO;AAAA,EAQX,YAAY,SAAwB;AALpC,SAAS,iBAAiB,oBAAI,IAAY,CAAC,KAAK,CAAC;AAM/C,SAAK,UAAU;AACf,SAAK,kBAAkB,MAAS;AACb;AAAA,QACf,YAAY;AACV,iBAAO,CAAC;AAAA,QACV;AAAA,MACF;AAAA;AACF,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMa,oBAA6C;AAAA;AACxD,YAAM,iBAAiB,MAAM,SAAS,qBAAqB,cAAc,CAAC;AAC1E,UAAI,CAAC,gBAAgB;AACnB,cAAM,IAAI,MAAM,8BAA8B;AAAA,MAChD;AACA,aA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