Skip to content

Commit 367d369

Browse files
authored
chore: Move graphKey to LDAIConfigTracker constructor (#1279)
1 parent d640d8e commit 367d369

5 files changed

Lines changed: 223 additions & 118 deletions

File tree

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,14 @@ describe('config evaluation', () => {
139139
const evaluateSpy = jest.spyOn(client as any, '_evaluate');
140140
const result = await client.agentConfig(key, testContext, defaultValue, variables);
141141

142-
expect(evaluateSpy).toHaveBeenCalledWith(key, testContext, defaultValue, 'agent', variables);
142+
expect(evaluateSpy).toHaveBeenCalledWith(
143+
key,
144+
testContext,
145+
defaultValue,
146+
'agent',
147+
variables,
148+
undefined,
149+
);
143150
expect(result.instructions).toBe(
144151
'You are a helpful assistant. Your name is John and your score is 42',
145152
);
@@ -464,7 +471,14 @@ describe('agentConfig method', () => {
464471
key,
465472
1,
466473
);
467-
expect(evaluateSpy).toHaveBeenCalledWith(key, testContext, defaultValue, 'agent', variables);
474+
expect(evaluateSpy).toHaveBeenCalledWith(
475+
key,
476+
testContext,
477+
defaultValue,
478+
'agent',
479+
variables,
480+
undefined,
481+
);
468482
expect(result).toBe(mockConfig);
469483
evaluateSpy.mockRestore();
470484
});

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

Lines changed: 138 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ describe('trackToolCall', () => {
933933
);
934934
});
935935

936-
it('includes graphKey when provided', () => {
936+
it('includes graphKey when set on constructor', () => {
937937
const tracker = new LDAIConfigTrackerImpl(
938938
mockLdClient,
939939
testRunId,
@@ -943,9 +943,10 @@ describe('trackToolCall', () => {
943943
modelName,
944944
providerName,
945945
testContext,
946+
'my-graph',
946947
);
947948

948-
tracker.trackToolCall('my-tool', 'my-graph');
949+
tracker.trackToolCall('my-tool');
949950

950951
expect(mockTrack).toHaveBeenCalledWith(
951952
'$ld:ai:tool_call',
@@ -993,8 +994,8 @@ describe('trackToolCalls', () => {
993994
});
994995
});
995996

996-
describe('graphKey parameter support', () => {
997-
it('includes graphKey in trackDuration event', () => {
997+
describe('graphKey constructor support', () => {
998+
it('includes graphKey in trackDuration event when set on constructor', () => {
998999
const tracker = new LDAIConfigTrackerImpl(
9991000
mockLdClient,
10001001
testRunId,
@@ -1004,9 +1005,10 @@ describe('graphKey parameter support', () => {
10041005
modelName,
10051006
providerName,
10061007
testContext,
1008+
'my-graph',
10071009
);
10081010

1009-
tracker.trackDuration(1000, 'my-graph');
1011+
tracker.trackDuration(1000);
10101012

10111013
expect(mockTrack).toHaveBeenCalledWith(
10121014
'$ld:ai:duration:total',
@@ -1016,7 +1018,7 @@ describe('graphKey parameter support', () => {
10161018
);
10171019
});
10181020

1019-
it('includes graphKey in trackSuccess event', () => {
1021+
it('includes graphKey in trackSuccess event when set on constructor', () => {
10201022
const tracker = new LDAIConfigTrackerImpl(
10211023
mockLdClient,
10221024
testRunId,
@@ -1026,9 +1028,10 @@ describe('graphKey parameter support', () => {
10261028
modelName,
10271029
providerName,
10281030
testContext,
1031+
'my-graph',
10291032
);
10301033

1031-
tracker.trackSuccess('my-graph');
1034+
tracker.trackSuccess();
10321035

10331036
expect(mockTrack).toHaveBeenCalledWith(
10341037
'$ld:ai:generation:success',
@@ -1038,7 +1041,7 @@ describe('graphKey parameter support', () => {
10381041
);
10391042
});
10401043

1041-
it('does not include graphKey when not provided', () => {
1044+
it('does not include graphKey when not set on constructor', () => {
10421045
const tracker = new LDAIConfigTrackerImpl(
10431046
mockLdClient,
10441047
testRunId,
@@ -1059,6 +1062,41 @@ describe('graphKey parameter support', () => {
10591062
1,
10601063
);
10611064
});
1065+
1066+
it('includes graphKey in getTrackData when set on constructor', () => {
1067+
const tracker = new LDAIConfigTrackerImpl(
1068+
mockLdClient,
1069+
testRunId,
1070+
configKey,
1071+
variationKey,
1072+
version,
1073+
modelName,
1074+
providerName,
1075+
testContext,
1076+
'my-graph',
1077+
);
1078+
1079+
expect(tracker.getTrackData()).toEqual({
1080+
...getExpectedTrackData(),
1081+
graphKey: 'my-graph',
1082+
});
1083+
});
1084+
1085+
it('does not include graphKey in getTrackData when not set', () => {
1086+
const tracker = new LDAIConfigTrackerImpl(
1087+
mockLdClient,
1088+
testRunId,
1089+
configKey,
1090+
variationKey,
1091+
version,
1092+
modelName,
1093+
providerName,
1094+
testContext,
1095+
);
1096+
1097+
expect(tracker.getTrackData()).toEqual(getExpectedTrackData());
1098+
expect('graphKey' in tracker.getTrackData()).toBe(false);
1099+
});
10621100
});
10631101

10641102
describe('at-most-once semantics', () => {
@@ -1311,4 +1349,96 @@ describe('fromResumptionToken', () => {
13111349
1,
13121350
);
13131351
});
1352+
1353+
it('includes graphKey in resumption token when set on constructor', () => {
1354+
const tracker = new LDAIConfigTrackerImpl(
1355+
mockLdClient,
1356+
testRunId,
1357+
configKey,
1358+
variationKey,
1359+
version,
1360+
modelName,
1361+
providerName,
1362+
testContext,
1363+
'my-graph',
1364+
);
1365+
1366+
const token = tracker.resumptionToken;
1367+
const decoded = JSON.parse(Buffer.from(token, 'base64url').toString('utf8'));
1368+
1369+
expect(decoded).toEqual({
1370+
runId: testRunId,
1371+
configKey,
1372+
variationKey,
1373+
version,
1374+
graphKey: 'my-graph',
1375+
});
1376+
});
1377+
1378+
it('does not include graphKey in resumption token when not set', () => {
1379+
const tracker = new LDAIConfigTrackerImpl(
1380+
mockLdClient,
1381+
testRunId,
1382+
configKey,
1383+
variationKey,
1384+
version,
1385+
modelName,
1386+
providerName,
1387+
testContext,
1388+
);
1389+
1390+
const token = tracker.resumptionToken;
1391+
const decoded = JSON.parse(Buffer.from(token, 'base64url').toString('utf8'));
1392+
1393+
expect(decoded).toEqual({
1394+
runId: testRunId,
1395+
configKey,
1396+
variationKey,
1397+
version,
1398+
});
1399+
expect('graphKey' in decoded).toBe(false);
1400+
});
1401+
1402+
it('reconstructs tracker with graphKey from resumption token', () => {
1403+
const original = new LDAIConfigTrackerImpl(
1404+
mockLdClient,
1405+
testRunId,
1406+
configKey,
1407+
variationKey,
1408+
version,
1409+
modelName,
1410+
providerName,
1411+
testContext,
1412+
'my-graph',
1413+
);
1414+
1415+
const reconstructed = LDAIConfigTrackerImpl.fromResumptionToken(
1416+
original.resumptionToken,
1417+
mockLdClient,
1418+
testContext,
1419+
);
1420+
1421+
expect(reconstructed.getTrackData().graphKey).toBe('my-graph');
1422+
});
1423+
1424+
it('reconstructed tracker without graphKey does not include graphKey in track data', () => {
1425+
const original = new LDAIConfigTrackerImpl(
1426+
mockLdClient,
1427+
testRunId,
1428+
configKey,
1429+
variationKey,
1430+
version,
1431+
modelName,
1432+
providerName,
1433+
testContext,
1434+
);
1435+
1436+
const reconstructed = LDAIConfigTrackerImpl.fromResumptionToken(
1437+
original.resumptionToken,
1438+
mockLdClient,
1439+
testContext,
1440+
);
1441+
1442+
expect('graphKey' in reconstructed.getTrackData()).toBe(false);
1443+
});
13141444
});

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class LDAIClientImpl implements LDAIClient {
7474
defaultValue: LDAIConfigDefaultKind,
7575
mode: LDAIConfigMode,
7676
variables?: Record<string, unknown>,
77+
graphKey?: string,
7778
): Promise<LDAIConfigKind> {
7879
const ldFlagValue = LDAIConfigUtils.toFlagValue(defaultValue, mode);
7980

@@ -101,6 +102,7 @@ export class LDAIClientImpl implements LDAIClient {
101102
value.model?.name ?? '',
102103
value.provider?.name ?? '',
103104
context,
105+
graphKey,
104106
);
105107

106108
const config = LDAIConfigUtils.fromFlagValue(key, value, trackerFactory);
@@ -217,21 +219,25 @@ export class LDAIClientImpl implements LDAIClient {
217219
return this._judgeConfig(key, context, defaultValue ?? disabledAIConfig, variables);
218220
}
219221

222+
private async _agentConfig(
223+
key: string,
224+
context: LDContext,
225+
defaultValue: LDAIAgentConfigDefault,
226+
variables?: Record<string, unknown>,
227+
graphKey?: string,
228+
): Promise<LDAIAgentConfig> {
229+
const config = await this._evaluate(key, context, defaultValue, 'agent', variables, graphKey);
230+
return config as LDAIAgentConfig;
231+
}
232+
220233
async agentConfig(
221234
key: string,
222235
context: LDContext,
223236
defaultValue?: LDAIAgentConfigDefault,
224237
variables?: Record<string, unknown>,
225238
): Promise<LDAIAgentConfig> {
226239
this._ldClient.track(TRACK_USAGE_AGENT_CONFIG, context, key, 1);
227-
const config = await this._evaluate(
228-
key,
229-
context,
230-
defaultValue ?? disabledAIConfig,
231-
'agent',
232-
variables,
233-
);
234-
return config as LDAIAgentConfig;
240+
return this._agentConfig(key, context, defaultValue ?? disabledAIConfig, variables);
235241
}
236242

237243
/**
@@ -261,14 +267,13 @@ export class LDAIClientImpl implements LDAIClient {
261267

262268
await Promise.all(
263269
agentConfigs.map(async (config) => {
264-
const agent = await this._evaluate(
270+
const agent = await this._agentConfig(
265271
config.key,
266272
context,
267273
config.defaultValue ?? disabledAIConfig,
268-
'agent',
269274
config.variables,
270275
);
271-
agents[config.key as T[number]['key']] = agent as LDAIAgentConfig;
276+
agents[config.key as T[number]['key']] = agent;
272277
}),
273278
);
274279

0 commit comments

Comments
 (0)