Skip to content

Commit 6106c82

Browse files
committed
Refactoring and cleanup.
1 parent b6804f0 commit 6106c82

File tree

3 files changed

+72
-61
lines changed

3 files changed

+72
-61
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import Mustache from 'mustache';
2+
3+
import { LDClient, LDContext } from '@launchdarkly/node-server-sdk';
4+
5+
import { LDAIClient } from './api/AIClient';
6+
import { LDAIConfig, LDGenerationConfig, LDModelConfig, LDPrompt } from './api/config';
7+
import { LDAIConfigTrackerImpl } from './LDAIConfigTrackerImpl';
8+
9+
/**
10+
* Metadata assorted with a model configuration variation.
11+
*/
12+
interface LDMeta {
13+
versionId: string;
14+
}
15+
16+
/**
17+
* Interface for the model configuration variation returned by LaunchDarkly. This is the internal
18+
* typing and not meant for exposure to the application developer.
19+
*/
20+
interface VariationContent {
21+
model?: LDModelConfig;
22+
prompt?: LDPrompt[];
23+
_ldMeta?: LDMeta;
24+
}
25+
26+
export class AIClientImpl implements LDAIClient {
27+
private _ldClient: LDClient;
28+
29+
constructor(ldClient: LDClient) {
30+
this._ldClient = ldClient;
31+
}
32+
33+
interpolateTemplate(template: string, variables: Record<string, unknown>): string {
34+
return Mustache.render(template, variables, undefined, { escape: (item: any) => item });
35+
}
36+
37+
async modelConfig<TDefault extends LDGenerationConfig>(
38+
key: string,
39+
context: LDContext,
40+
defaultValue: TDefault,
41+
variables?: Record<string, unknown>,
42+
): Promise<LDAIConfig> {
43+
const value: VariationContent = await this._ldClient.variation(key, context, defaultValue);
44+
const config: LDGenerationConfig = { ...value };
45+
const allVariables = { ldctx: context, ...variables };
46+
47+
if (value.prompt) {
48+
config.prompt = value.prompt.map((entry: any) => ({
49+
...entry,
50+
content: this.interpolateTemplate(entry.content, allVariables),
51+
}));
52+
}
53+
54+
return {
55+
config,
56+
// eslint-disable-next-line no-underscore-dangle
57+
tracker: new LDAIConfigTrackerImpl(
58+
this._ldClient,
59+
key,
60+
// eslint-disable-next-line no-underscore-dangle
61+
value._ldMeta?.versionId ?? '',
62+
context,
63+
),
64+
noConfiguration: Object.keys(value).length === 0,
65+
};
66+
}
67+
}

packages/sdk/ai/src/api/AIClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { LDAIConfig, LDGenerationConfig } from './config/LDAIConfig';
55
* Interface for performing AI operations using LaunchDarkly.
66
*/
77

8-
export interface AIClient {
8+
export interface LDAIClient {
99
/**
1010
* Parses and interpolates a template string with the provided variables.
1111
*

packages/sdk/ai/src/index.ts

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,14 @@
1-
import Mustache from 'mustache';
1+
import { LDClient } from '@launchdarkly/node-server-sdk';
22

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';
615

626
/**
637
* Initialize a new AI client. This client will be used to perform any AI operations.
648
* @param ldClient The base LaunchDarkly client.
659
* @returns A new AI client.
6610
*/
67-
export function initAi(ldClient: LDClient): AIClient {
11+
export function initAi(ldClient: LDClient): LDAIClient {
6812
return new AIClientImpl(ldClient);
6913
}
7014

0 commit comments

Comments
 (0)