diff --git a/packages/sdk/ai/src/LDAIClientImpl.ts b/packages/sdk/ai/src/LDAIClientImpl.ts new file mode 100644 index 000000000..a515cce20 --- /dev/null +++ b/packages/sdk/ai/src/LDAIClientImpl.ts @@ -0,0 +1,67 @@ +import Mustache from 'mustache'; + +import { LDClient, LDContext } from '@launchdarkly/node-server-sdk'; + +import { LDAIClient } from './api/AIClient'; +import { LDAIConfig, LDGenerationConfig, LDModelConfig, LDPrompt } from './api/config'; +import { LDAIConfigTrackerImpl } from './LDAIConfigTrackerImpl'; + +/** + * Metadata assorted with a model configuration variation. + */ +interface LDMeta { + versionId: string; +} + +/** + * Interface for the model configuration variation returned by LaunchDarkly. This is the internal + * typing and not meant for exposure to the application developer. + */ +interface VariationContent { + model?: LDModelConfig; + prompt?: LDPrompt[]; + _ldMeta?: LDMeta; +} + +export class AIClientImpl implements LDAIClient { + private _ldClient: LDClient; + + constructor(ldClient: LDClient) { + this._ldClient = ldClient; + } + + interpolateTemplate(template: string, variables: Record): string { + return Mustache.render(template, variables, undefined, { escape: (item: any) => item }); + } + + async modelConfig( + key: string, + context: LDContext, + defaultValue: TDefault, + variables?: Record, + ): Promise { + const value: VariationContent = await this._ldClient.variation(key, context, defaultValue); + const config: LDGenerationConfig = { ...value }; + const allVariables = { ldctx: context, ...variables }; + + if (value.prompt) { + config.prompt = value.prompt.map((entry: any) => ({ + ...entry, + content: this.interpolateTemplate(entry.content, allVariables), + })); + } + + return { + config, + // eslint-disable-next-line no-underscore-dangle + tracker: new LDAIConfigTrackerImpl( + this._ldClient, + key, + // eslint-disable-next-line no-underscore-dangle + value._ldMeta?.versionId ?? '', + context, + ), + noConfiguration: Object.keys(value).length === 0, + }; + } +} diff --git a/packages/sdk/ai/src/api/AIClient.ts b/packages/sdk/ai/src/api/AIClient.ts index 9fd7da41b..4f4914756 100644 --- a/packages/sdk/ai/src/api/AIClient.ts +++ b/packages/sdk/ai/src/api/AIClient.ts @@ -5,7 +5,7 @@ import { LDAIConfig, LDGenerationConfig } from './config/LDAIConfig'; * Interface for performing AI operations using LaunchDarkly. */ -export interface AIClient { +export interface LDAIClient { /** * Parses and interpolates a template string with the provided variables. * diff --git a/packages/sdk/ai/src/index.ts b/packages/sdk/ai/src/index.ts index fb11b093a..51884f9c2 100644 --- a/packages/sdk/ai/src/index.ts +++ b/packages/sdk/ai/src/index.ts @@ -1,70 +1,14 @@ -import Mustache from 'mustache'; +import { LDClient } from '@launchdarkly/node-server-sdk'; -import { LDClient, LDContext } from '@launchdarkly/node-server-sdk'; - -import { AIClient } from './api/AIClient'; -import { LDAIConfig, LDGenerationConfig, LDModelConfig, LDPrompt } from './api/config'; -import { LDAIConfigTrackerImpl } from './LDAIConfigTrackerImpl'; - -interface LDMeta { - versionId: string; -} - -interface VariationContent { - model?: LDModelConfig; - prompt?: LDPrompt[]; - _ldMeta?: LDMeta; -} - -export class AIClientImpl implements AIClient { - private _ldClient: LDClient; - - constructor(ldClient: LDClient) { - this._ldClient = ldClient; - } - - interpolateTemplate(template: string, variables: Record): string { - return Mustache.render(template, variables, undefined, { escape: (item: any) => item }); - } - - async modelConfig( - key: string, - context: LDContext, - defaultValue: TDefault, - variables?: Record, - ): Promise { - const value: VariationContent = await this._ldClient.variation(key, context, defaultValue); - const config: LDGenerationConfig = { ...value }; - const allVariables = { ldctx: context, ...variables }; - - if (value.prompt) { - config.prompt = value.prompt.map((entry: any) => ({ - ...entry, - content: this.interpolateTemplate(entry.content, allVariables), - })); - } - - return { - config, - // eslint-disable-next-line no-underscore-dangle - tracker: new LDAIConfigTrackerImpl( - this._ldClient, - key, - // eslint-disable-next-line no-underscore-dangle - value._ldMeta?.versionId ?? '', - context, - ), - noConfiguration: Object.keys(value).length === 0, - }; - } -} +import { LDAIClient } from './api/AIClient'; +import { AIClientImpl } from './LDAIClientImpl'; /** * Initialize a new AI client. This client will be used to perform any AI operations. * @param ldClient The base LaunchDarkly client. * @returns A new AI client. */ -export function initAi(ldClient: LDClient): AIClient { +export function initAi(ldClient: LDClient): LDAIClient { return new AIClientImpl(ldClient); }