|
1 | | -import Mustache from 'mustache'; |
| 1 | +import { LDClient } from '@launchdarkly/node-server-sdk'; |
2 | 2 |
|
3 | | -import { LDClient, LDContext } from '@launchdarkly/node-server-sdk'; |
4 | | - |
5 | | -import { AIClient } from './api/AIClient'; |
6 | | -import { LDAIConfig, LDGenerationConfig, LDModelConfig, LDPrompt } from './api/config'; |
7 | | -import { LDAIConfigTrackerImpl } from './LDAIConfigTrackerImpl'; |
8 | | - |
9 | | -interface LDMeta { |
10 | | - versionId: string; |
11 | | -} |
12 | | - |
13 | | -interface VariationContent { |
14 | | - model?: LDModelConfig; |
15 | | - prompt?: LDPrompt[]; |
16 | | - _ldMeta?: LDMeta; |
17 | | -} |
18 | | - |
19 | | -export class AIClientImpl implements AIClient { |
20 | | - private _ldClient: LDClient; |
21 | | - |
22 | | - constructor(ldClient: LDClient) { |
23 | | - this._ldClient = ldClient; |
24 | | - } |
25 | | - |
26 | | - interpolateTemplate(template: string, variables: Record<string, unknown>): string { |
27 | | - return Mustache.render(template, variables, undefined, { escape: (item: any) => item }); |
28 | | - } |
29 | | - |
30 | | - async modelConfig<TDefault extends LDGenerationConfig>( |
31 | | - key: string, |
32 | | - context: LDContext, |
33 | | - defaultValue: TDefault, |
34 | | - variables?: Record<string, unknown>, |
35 | | - ): Promise<LDAIConfig> { |
36 | | - const value: VariationContent = await this._ldClient.variation(key, context, defaultValue); |
37 | | - const config: LDGenerationConfig = { ...value }; |
38 | | - const allVariables = { ldctx: context, ...variables }; |
39 | | - |
40 | | - if (value.prompt) { |
41 | | - config.prompt = value.prompt.map((entry: any) => ({ |
42 | | - ...entry, |
43 | | - content: this.interpolateTemplate(entry.content, allVariables), |
44 | | - })); |
45 | | - } |
46 | | - |
47 | | - return { |
48 | | - config, |
49 | | - // eslint-disable-next-line no-underscore-dangle |
50 | | - tracker: new LDAIConfigTrackerImpl( |
51 | | - this._ldClient, |
52 | | - key, |
53 | | - // eslint-disable-next-line no-underscore-dangle |
54 | | - value._ldMeta?.versionId ?? '', |
55 | | - context, |
56 | | - ), |
57 | | - noConfiguration: Object.keys(value).length === 0, |
58 | | - }; |
59 | | - } |
60 | | -} |
| 3 | +import { LDAIClient } from './api/AIClient'; |
| 4 | +import { AIClientImpl } from './LDAIClientImpl'; |
61 | 5 |
|
62 | 6 | /** |
63 | 7 | * Initialize a new AI client. This client will be used to perform any AI operations. |
64 | 8 | * @param ldClient The base LaunchDarkly client. |
65 | 9 | * @returns A new AI client. |
66 | 10 | */ |
67 | | -export function initAi(ldClient: LDClient): AIClient { |
| 11 | +export function initAi(ldClient: LDClient): LDAIClient { |
68 | 12 | return new AIClientImpl(ldClient); |
69 | 13 | } |
70 | 14 |
|
|
0 commit comments