Skip to content

Commit 5b6ef04

Browse files
chore: hide mapper and expose methods directly on config object
1 parent 2bff08d commit 5b6ef04

File tree

7 files changed

+49
-35
lines changed

7 files changed

+49
-35
lines changed

packages/sdk/server-ai/__tests__/LDAIClientImpl.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ it('returns config with interpolated messagess', async () => {
5656
{ role: 'user', content: 'Score: 42' },
5757
],
5858
tracker: expect.any(Object),
59-
mapper: expect.any(Object),
6059
enabled: true,
60+
toVercelAISDK: expect.any(Function),
6161
});
6262
});
6363

@@ -102,8 +102,8 @@ it('handles missing metadata in variation', async () => {
102102
model: { name: 'example-provider', parameters: { name: 'imagination' } },
103103
messages: [{ role: 'system', content: 'Hello' }],
104104
tracker: expect.any(Object),
105-
mapper: expect.any(Object),
106105
enabled: false,
106+
toVercelAISDK: expect.any(Function),
107107
});
108108
});
109109

@@ -126,8 +126,8 @@ it('passes the default value to the underlying client', async () => {
126126
messages: defaultValue.messages,
127127
provider: defaultValue.provider,
128128
tracker: expect.any(Object),
129-
mapper: expect.any(Object),
130129
enabled: false,
130+
toVercelAISDK: expect.any(Function),
131131
});
132132

133133
expect(mockLdClient.variation).toHaveBeenCalledWith(key, testContext, defaultValue);

packages/sdk/server-ai/__tests__/LDAIConfigMapperImpl.test.ts renamed to packages/sdk/server-ai/__tests__/LDAIConfigMapper.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { LDMessage, VercelAISDKMapOptions } from '../src/api/config';
2-
import { LDAIConfigMapperImpl } from '../src/LDAIConfigMapperImpl';
2+
import { LDAIConfigMapper } from '../src/LDAIConfigMapper';
33

44
describe('_findParameter', () => {
55
it('handles undefined model and messages', () => {
6-
const mapper = new LDAIConfigMapperImpl();
6+
const mapper = new LDAIConfigMapper();
77
// eslint-disable-next-line @typescript-eslint/dot-notation
88
expect(mapper['_findParameter']<number>('test-param')).toBeUndefined();
99
});
1010

1111
it('handles parameter not found', () => {
12-
const mapper = new LDAIConfigMapperImpl({
12+
const mapper = new LDAIConfigMapper({
1313
name: 'test-ai-model',
1414
parameters: {
1515
'test-param': 123,
@@ -23,7 +23,7 @@ describe('_findParameter', () => {
2323
});
2424

2525
it('finds parameter from single model parameter', () => {
26-
const mapper = new LDAIConfigMapperImpl({
26+
const mapper = new LDAIConfigMapper({
2727
name: 'test-ai-model',
2828
parameters: {
2929
'test-param': 123,
@@ -34,7 +34,7 @@ describe('_findParameter', () => {
3434
});
3535

3636
it('finds parameter from multiple model parameters', () => {
37-
const mapper = new LDAIConfigMapperImpl({
37+
const mapper = new LDAIConfigMapper({
3838
name: 'test-ai-model',
3939
parameters: {
4040
testParam: 123,
@@ -45,7 +45,7 @@ describe('_findParameter', () => {
4545
});
4646

4747
it('finds parameter from single model custom parameter', () => {
48-
const mapper = new LDAIConfigMapperImpl({
48+
const mapper = new LDAIConfigMapper({
4949
name: 'test-ai-model',
5050
custom: {
5151
'test-param': 123,
@@ -56,7 +56,7 @@ describe('_findParameter', () => {
5656
});
5757

5858
it('finds parameter from multiple model custom parameters', () => {
59-
const mapper = new LDAIConfigMapperImpl({
59+
const mapper = new LDAIConfigMapper({
6060
name: 'test-ai-model',
6161
custom: {
6262
testParam: 123,
@@ -67,7 +67,7 @@ describe('_findParameter', () => {
6767
});
6868

6969
it('gives precedence to model parameters over model custom parameters', () => {
70-
const mapper = new LDAIConfigMapperImpl({
70+
const mapper = new LDAIConfigMapper({
7171
name: 'test-ai-model',
7272
parameters: {
7373
'test-param': 123,
@@ -97,7 +97,7 @@ describe('toVercelAIAISDK', () => {
9797
});
9898

9999
it('handles undefined model and messages', () => {
100-
const mapper = new LDAIConfigMapperImpl();
100+
const mapper = new LDAIConfigMapper();
101101
const result = mapper.toVercelAISDK(mockProvider);
102102

103103
expect(mockProvider).toHaveBeenCalledWith('');
@@ -110,7 +110,7 @@ describe('toVercelAIAISDK', () => {
110110
});
111111

112112
it('uses additional messages', () => {
113-
const mapper = new LDAIConfigMapperImpl({ name: 'test-ai-model' });
113+
const mapper = new LDAIConfigMapper({ name: 'test-ai-model' });
114114
const result = mapper.toVercelAISDK(mockProvider, mockOptions);
115115

116116
expect(mockProvider).toHaveBeenCalledWith('test-ai-model');
@@ -123,7 +123,7 @@ describe('toVercelAIAISDK', () => {
123123
});
124124

125125
it('combines config messages and additional messages', () => {
126-
const mapper = new LDAIConfigMapperImpl({ name: 'test-ai-model' }, undefined, mockMessages);
126+
const mapper = new LDAIConfigMapper({ name: 'test-ai-model' }, undefined, mockMessages);
127127
const result = mapper.toVercelAISDK(mockProvider, mockOptions);
128128

129129
expect(mockProvider).toHaveBeenCalledWith('test-ai-model');
@@ -136,7 +136,7 @@ describe('toVercelAIAISDK', () => {
136136
});
137137

138138
it('requests parameters correctly', () => {
139-
const mapper = new LDAIConfigMapperImpl({ name: 'test-ai-model' }, undefined, mockMessages);
139+
const mapper = new LDAIConfigMapper({ name: 'test-ai-model' }, undefined, mockMessages);
140140
const findParameterMock = jest.spyOn(mapper as any, '_findParameter');
141141
const result = mapper.toVercelAISDK(mockProvider);
142142

packages/sdk/server-ai/src/LDAIClientImpl.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@ import * as Mustache from 'mustache';
22

33
import { LDContext } from '@launchdarkly/js-server-sdk-common';
44

5-
import { LDAIConfig, LDAIDefaults, LDMessage, LDModelConfig, LDProviderConfig } from './api/config';
5+
import {
6+
LDAIConfig,
7+
LDAIDefaults,
8+
LDMessage,
9+
LDModelConfig,
10+
LDProviderConfig,
11+
VercelAISDKConfig,
12+
VercelAISDKMapOptions,
13+
VercelAISDKProvider,
14+
} from './api/config';
615
import { LDAIClient } from './api/LDAIClient';
7-
import { LDAIConfigMapperImpl } from './LDAIConfigMapperImpl';
16+
import { LDAIConfigMapper } from './LDAIConfigMapper';
817
import { LDAIConfigTrackerImpl } from './LDAIConfigTrackerImpl';
918
import { LDClientMin } from './LDClientMin';
1019

@@ -53,7 +62,7 @@ export class LDAIClientImpl implements LDAIClient {
5362
);
5463
// eslint-disable-next-line no-underscore-dangle
5564
const enabled = !!value._ldMeta?.enabled;
56-
const config: Omit<LDAIConfig, 'mapper'> = {
65+
const config: Omit<LDAIConfig, 'toVercelAISDK'> = {
5766
tracker,
5867
enabled,
5968
};
@@ -74,9 +83,14 @@ export class LDAIClientImpl implements LDAIClient {
7483
}));
7584
}
7685

86+
const mapper = new LDAIConfigMapper(config.model, config.provider, config.messages);
87+
7788
return {
7889
...config,
79-
mapper: new LDAIConfigMapperImpl(config.model, config.provider, config.messages),
90+
toVercelAISDK: <TMod>(
91+
provider: VercelAISDKProvider<TMod> | Record<string, VercelAISDKProvider<TMod>>,
92+
options?: VercelAISDKMapOptions | undefined,
93+
): VercelAISDKConfig<TMod> => mapper.toVercelAISDK(provider, options),
8094
};
8195
}
8296
}

packages/sdk/server-ai/src/LDAIConfigMapperImpl.ts renamed to packages/sdk/server-ai/src/LDAIConfigMapper.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
LDAIConfigMapper,
32
LDMessage,
43
LDModelConfig,
54
LDProviderConfig,
@@ -8,7 +7,7 @@ import {
87
VercelAISDKProvider,
98
} from './api/config';
109

11-
export class LDAIConfigMapperImpl implements LDAIConfigMapper {
10+
export class LDAIConfigMapper {
1211
constructor(
1312
private _model?: LDModelConfig | undefined,
1413
private _provider?: LDProviderConfig | undefined,

packages/sdk/server-ai/src/api/config/LDAIConfig.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { LDAIConfigMapper } from './LDAIConfigMapper';
21
import { LDAIConfigTracker } from './LDAIConfigTracker';
2+
import { VercelAISDKConfig, VercelAISDKMapOptions, VercelAISDKProvider } from './VercelAISDK';
33

44
/**
55
* Configuration related to the model.
@@ -66,21 +66,29 @@ export interface LDAIConfig {
6666
tracker: LDAIConfigTracker;
6767

6868
/**
69-
* A mapper which can be used to map configuration for selected supported providers.
69+
* Whether the configuration is enabled.
7070
*/
71-
mapper: LDAIConfigMapper;
71+
enabled: boolean;
7272

7373
/**
74-
* Whether the configuration is enabled.
74+
* Maps this AI config to a format usable direcly in Vercel AI SDK generateText()
75+
* and streamText() methods.
76+
*
77+
* @param provider A Vercel AI SDK Provider or a map of provider names to Vercel AI SDK Providers.
78+
* @param options Optional mapping options.
79+
* @returns A configuration directly usable in Vercel AI SDK generateText() and streamText()
7580
*/
76-
enabled: boolean;
81+
toVercelAISDK: <TMod>(
82+
provider: VercelAISDKProvider<TMod> | Record<string, VercelAISDKProvider<TMod>>,
83+
options?: VercelAISDKMapOptions | undefined,
84+
) => VercelAISDKConfig<TMod>;
7785
}
7886

7987
/**
8088
* Default value for a `modelConfig`. This is the same as the LDAIConfig, but it does not include
8189
* a tracker or mapper, and `enabled` is optional.
8290
*/
83-
export type LDAIDefaults = Omit<LDAIConfig, 'tracker' | 'mapper' | 'enabled'> & {
91+
export type LDAIDefaults = Omit<LDAIConfig, 'tracker' | 'enabled' | 'toVercelAISDK'> & {
8492
/**
8593
* Whether the configuration is enabled.
8694
*

packages/sdk/server-ai/src/api/config/LDAIConfigMapper.ts renamed to packages/sdk/server-ai/src/api/config/VercelAISDK.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,3 @@ export interface VercelAISDKConfig<TMod> {
1818
stopSequences?: string[] | undefined;
1919
seed?: number | undefined;
2020
}
21-
22-
export interface LDAIConfigMapper {
23-
toVercelAISDK: <TMod>(
24-
provider: VercelAISDKProvider<TMod> | Record<string, VercelAISDKProvider<TMod>>,
25-
options?: VercelAISDKMapOptions | undefined,
26-
) => VercelAISDKConfig<TMod>;
27-
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './LDAIConfig';
2-
export * from './LDAIConfigMapper';
2+
export * from './VercelAISDK';
33
export { LDAIConfigTracker } from './LDAIConfigTracker';

0 commit comments

Comments
 (0)