Skip to content

Commit 034a89d

Browse files
committed
fix: Add support for graph metric tracking (#1269)
1 parent c72ec22 commit 034a89d

9 files changed

Lines changed: 928 additions & 43 deletions

File tree

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

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,3 +877,148 @@ describe('trackJudgeResponse', () => {
877877
);
878878
});
879879
});
880+
881+
describe('trackToolCall', () => {
882+
it('tracks a single tool call', () => {
883+
const tracker = new LDAIConfigTrackerImpl(
884+
mockLdClient,
885+
configKey,
886+
variationKey,
887+
version,
888+
modelName,
889+
providerName,
890+
testContext,
891+
);
892+
893+
tracker.trackToolCall('my-tool');
894+
895+
expect(mockTrack).toHaveBeenCalledWith(
896+
'$ld:ai:tool_call',
897+
testContext,
898+
{ ...getExpectedTrackData(), toolKey: 'my-tool' },
899+
1,
900+
);
901+
});
902+
903+
it('includes graphKey when provided', () => {
904+
const tracker = new LDAIConfigTrackerImpl(
905+
mockLdClient,
906+
configKey,
907+
variationKey,
908+
version,
909+
modelName,
910+
providerName,
911+
testContext,
912+
);
913+
914+
tracker.trackToolCall('my-tool', 'my-graph');
915+
916+
expect(mockTrack).toHaveBeenCalledWith(
917+
'$ld:ai:tool_call',
918+
testContext,
919+
{ ...getExpectedTrackData(), graphKey: 'my-graph', toolKey: 'my-tool' },
920+
1,
921+
);
922+
});
923+
});
924+
925+
describe('trackToolCalls', () => {
926+
it('tracks multiple tool calls', () => {
927+
const tracker = new LDAIConfigTrackerImpl(
928+
mockLdClient,
929+
configKey,
930+
variationKey,
931+
version,
932+
modelName,
933+
providerName,
934+
testContext,
935+
);
936+
937+
tracker.trackToolCalls(['tool-a', 'tool-b', 'tool-c']);
938+
939+
expect(mockTrack).toHaveBeenCalledTimes(3);
940+
expect(mockTrack).toHaveBeenCalledWith(
941+
'$ld:ai:tool_call',
942+
testContext,
943+
{ ...getExpectedTrackData(), toolKey: 'tool-a' },
944+
1,
945+
);
946+
expect(mockTrack).toHaveBeenCalledWith(
947+
'$ld:ai:tool_call',
948+
testContext,
949+
{ ...getExpectedTrackData(), toolKey: 'tool-b' },
950+
1,
951+
);
952+
expect(mockTrack).toHaveBeenCalledWith(
953+
'$ld:ai:tool_call',
954+
testContext,
955+
{ ...getExpectedTrackData(), toolKey: 'tool-c' },
956+
1,
957+
);
958+
});
959+
});
960+
961+
describe('graphKey parameter support', () => {
962+
it('includes graphKey in trackDuration event', () => {
963+
const tracker = new LDAIConfigTrackerImpl(
964+
mockLdClient,
965+
configKey,
966+
variationKey,
967+
version,
968+
modelName,
969+
providerName,
970+
testContext,
971+
);
972+
973+
tracker.trackDuration(1000, 'my-graph');
974+
975+
expect(mockTrack).toHaveBeenCalledWith(
976+
'$ld:ai:duration:total',
977+
testContext,
978+
{ ...getExpectedTrackData(), graphKey: 'my-graph' },
979+
1000,
980+
);
981+
});
982+
983+
it('includes graphKey in trackSuccess event', () => {
984+
const tracker = new LDAIConfigTrackerImpl(
985+
mockLdClient,
986+
configKey,
987+
variationKey,
988+
version,
989+
modelName,
990+
providerName,
991+
testContext,
992+
);
993+
994+
tracker.trackSuccess('my-graph');
995+
996+
expect(mockTrack).toHaveBeenCalledWith(
997+
'$ld:ai:generation:success',
998+
testContext,
999+
{ ...getExpectedTrackData(), graphKey: 'my-graph' },
1000+
1,
1001+
);
1002+
});
1003+
1004+
it('does not include graphKey when not provided', () => {
1005+
const tracker = new LDAIConfigTrackerImpl(
1006+
mockLdClient,
1007+
configKey,
1008+
variationKey,
1009+
version,
1010+
modelName,
1011+
providerName,
1012+
testContext,
1013+
);
1014+
1015+
tracker.trackSuccess();
1016+
1017+
expect(mockTrack).toHaveBeenCalledWith(
1018+
'$ld:ai:generation:success',
1019+
testContext,
1020+
getExpectedTrackData(),
1021+
1,
1022+
);
1023+
});
1024+
});

0 commit comments

Comments
 (0)