@launchdarkly/server-sdk-ai 0.14.0 → 0.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/__tests__/Judge.test.ts +31 -6
  3. package/__tests__/LDAIClientImpl.test.ts +5 -0
  4. package/__tests__/TrackedChat.test.ts +1 -0
  5. package/dist/package.json +1 -1
  6. package/dist/src/LDAIClientImpl.js +2 -2
  7. package/dist/src/LDAIClientImpl.js.map +1 -1
  8. package/dist/src/LDAIConfigTrackerImpl.d.ts +2 -1
  9. package/dist/src/LDAIConfigTrackerImpl.d.ts.map +1 -1
  10. package/dist/src/LDAIConfigTrackerImpl.js +5 -1
  11. package/dist/src/LDAIConfigTrackerImpl.js.map +1 -1
  12. package/dist/src/api/chat/TrackedChat.d.ts.map +1 -1
  13. package/dist/src/api/chat/TrackedChat.js +4 -4
  14. package/dist/src/api/chat/TrackedChat.js.map +1 -1
  15. package/dist/src/api/config/LDAIConfigTracker.d.ts +7 -1
  16. package/dist/src/api/config/LDAIConfigTracker.d.ts.map +1 -1
  17. package/dist/src/api/config/LDAIConfigUtils.js +16 -12
  18. package/dist/src/api/config/LDAIConfigUtils.js.map +1 -1
  19. package/dist/src/api/config/types.d.ts +4 -0
  20. package/dist/src/api/config/types.d.ts.map +1 -1
  21. package/dist/src/api/judge/Judge.d.ts.map +1 -1
  22. package/dist/src/api/judge/Judge.js +2 -0
  23. package/dist/src/api/judge/Judge.js.map +1 -1
  24. package/dist/src/api/judge/types.d.ts +2 -0
  25. package/dist/src/api/judge/types.d.ts.map +1 -1
  26. package/docs/assets/search.js +1 -1
  27. package/docs/classes/AIProvider.html +9 -9
  28. package/docs/classes/AIProviderFactory.html +8 -8
  29. package/docs/classes/Judge.html +18 -18
  30. package/docs/classes/TrackedChat.html +19 -19
  31. package/docs/enums/LDFeedbackKind.html +6 -6
  32. package/docs/functions/createBedrockTokenUsage.html +4 -4
  33. package/docs/functions/createOpenAiUsage.html +4 -4
  34. package/docs/functions/createVercelAISDKTokenUsage.html +4 -4
  35. package/docs/functions/initAi.html +4 -4
  36. package/docs/index.html +4 -4
  37. package/docs/interfaces/ChatResponse.html +7 -7
  38. package/docs/interfaces/EvalScore.html +6 -6
  39. package/docs/interfaces/JudgeResponse.html +17 -7
  40. package/docs/interfaces/LDAIAgentConfig.html +21 -10
  41. package/docs/interfaces/LDAIAgentConfigDefault.html +9 -9
  42. package/docs/interfaces/LDAIAgentRequestConfig.html +7 -7
  43. package/docs/interfaces/LDAIClient.html +14 -14
  44. package/docs/interfaces/LDAICompletionConfig.html +21 -10
  45. package/docs/interfaces/LDAICompletionConfigDefault.html +9 -9
  46. package/docs/interfaces/LDAIConfig.html +18 -8
  47. package/docs/interfaces/LDAIConfigDefault.html +7 -7
  48. package/docs/interfaces/LDAIConfigTracker.html +39 -19
  49. package/docs/interfaces/LDAIJudgeConfig.html +21 -10
  50. package/docs/interfaces/LDAIJudgeConfigDefault.html +9 -9
  51. package/docs/interfaces/LDAIMetrics.html +6 -6
  52. package/docs/interfaces/LDJudge.html +6 -6
  53. package/docs/interfaces/LDJudgeConfiguration.html +5 -5
  54. package/docs/interfaces/LDLogger.html +3 -3
  55. package/docs/interfaces/LDMessage.html +6 -6
  56. package/docs/interfaces/LDModelConfig.html +7 -7
  57. package/docs/interfaces/LDProviderConfig.html +5 -5
  58. package/docs/interfaces/LDTokenUsage.html +7 -7
  59. package/docs/interfaces/StructuredResponse.html +7 -7
  60. package/docs/types/LDAIConfigDefaultKind.html +4 -4
  61. package/docs/types/LDAIConfigKind.html +4 -4
  62. package/docs/types/LDAIConfigMode.html +4 -4
  63. package/docs/types/SupportedAIProvider.html +4 -4
  64. package/docs/variables/SUPPORTED_AI_PROVIDERS.html +4 -4
  65. package/package.json +1 -1
  66. package/src/LDAIClientImpl.ts +2 -2
  67. package/src/LDAIConfigTrackerImpl.ts +12 -2
  68. package/src/api/chat/TrackedChat.ts +9 -5
  69. package/src/api/config/LDAIConfigTracker.ts +8 -1
  70. package/src/api/config/LDAIConfigUtils.ts +20 -9
  71. package/src/api/config/types.ts +4 -0
  72. package/src/api/judge/Judge.ts +2 -0
  73. package/src/api/judge/types.ts +2 -0
@@ -3,7 +3,7 @@ import { LDContext } from '@launchdarkly/js-server-sdk-common';
3
3
  import { name as aiSdkName, version as aiSdkVersion } from '../package.json';
4
4
  import { LDAIConfigTracker } from './api/config';
5
5
  import { LDAIMetricSummary } from './api/config/LDAIConfigTracker';
6
- import { EvalScore } from './api/judge/types';
6
+ import { EvalScore, JudgeResponse } from './api/judge/types';
7
7
  import {
8
8
  createBedrockTokenUsage,
9
9
  createOpenAiUsage,
@@ -76,12 +76,22 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
76
76
  }
77
77
 
78
78
  trackEvalScores(scores: Record<string, EvalScore>) {
79
- // Track each evaluation score individually
80
79
  Object.entries(scores).forEach(([metricKey, evalScore]) => {
81
80
  this._ldClient.track(metricKey, this._context, this.getTrackData(), evalScore.score);
82
81
  });
83
82
  }
84
83
 
84
+ trackJudgeResponse(response: JudgeResponse) {
85
+ Object.entries(response.evals).forEach(([metricKey, evalScore]) => {
86
+ this._ldClient.track(
87
+ metricKey,
88
+ this._context,
89
+ { ...this.getTrackData(), judgeConfigKey: response.judgeConfigKey },
90
+ evalScore.score,
91
+ );
92
+ });
93
+ }
94
+
85
95
  trackFeedback(feedback: { kind: LDFeedbackKind }): void {
86
96
  this._trackedMetrics.feedback = feedback;
87
97
  if (feedback.kind === LDFeedbackKind.Positive) {
@@ -84,13 +84,17 @@ export class TrackedChat {
84
84
  return undefined;
85
85
  }
86
86
 
87
- const evalResult = await judge.evaluateMessages(messages, response, judgeConfig.samplingRate);
88
-
89
- if (evalResult && evalResult.success) {
90
- this.tracker.trackEvalScores(evalResult.evals);
87
+ const judgeResponse = await judge.evaluateMessages(
88
+ messages,
89
+ response,
90
+ judgeConfig.samplingRate,
91
+ );
92
+
93
+ if (judgeResponse && judgeResponse.success) {
94
+ this.tracker.trackJudgeResponse(judgeResponse);
91
95
  }
92
96
 
93
- return evalResult;
97
+ return judgeResponse;
94
98
  });
95
99
 
96
100
  // ensure all evaluations complete even if some fail
@@ -1,4 +1,4 @@
1
- import { EvalScore } from '../judge/types';
1
+ import { EvalScore, JudgeResponse } from '../judge/types';
2
2
  import { LDAIMetrics, LDFeedbackKind, LDTokenUsage } from '../metrics';
3
3
 
4
4
  /**
@@ -94,6 +94,13 @@ export interface LDAIConfigTracker {
94
94
  */
95
95
  trackEvalScores(scores: Record<string, EvalScore>): void;
96
96
 
97
+ /**
98
+ * Track a judge response containing evaluation scores and judge configuration key.
99
+ *
100
+ * @param response Judge response containing evaluation scores and judge configuration key
101
+ */
102
+ trackJudgeResponse(response: JudgeResponse): void;
103
+
97
104
  /**
98
105
  * Track the duration of execution of the provided function.
99
106
  *
@@ -82,19 +82,23 @@ export class LDAIConfigUtils {
82
82
  * @param tracker The tracker to add to the config
83
83
  * @returns The appropriate AI configuration type
84
84
  */
85
- static fromFlagValue(flagValue: LDAIConfigFlagValue, tracker: LDAIConfigTracker): LDAIConfigKind {
85
+ static fromFlagValue(
86
+ key: string,
87
+ flagValue: LDAIConfigFlagValue,
88
+ tracker: LDAIConfigTracker,
89
+ ): LDAIConfigKind {
86
90
  // Determine the actual mode from flag value
87
91
  // eslint-disable-next-line no-underscore-dangle
88
92
  const flagValueMode = flagValue._ldMeta?.mode;
89
93
 
90
94
  switch (flagValueMode) {
91
95
  case 'agent':
92
- return this.toAgentConfig(flagValue, tracker);
96
+ return this.toAgentConfig(key, flagValue, tracker);
93
97
  case 'judge':
94
- return this.toJudgeConfig(flagValue, tracker);
98
+ return this.toJudgeConfig(key, flagValue, tracker);
95
99
  case 'completion':
96
100
  default:
97
- return this.toCompletionConfig(flagValue, tracker);
101
+ return this.toCompletionConfig(key, flagValue, tracker);
98
102
  }
99
103
  }
100
104
 
@@ -104,15 +108,17 @@ export class LDAIConfigUtils {
104
108
  * @param mode The mode for the disabled config
105
109
  * @returns A disabled config of the appropriate type
106
110
  */
107
- static createDisabledConfig(mode: LDAIConfigMode): LDAIConfigKind {
111
+ static createDisabledConfig(key: string, mode: LDAIConfigMode): LDAIConfigKind {
108
112
  switch (mode) {
109
113
  case 'agent':
110
114
  return {
115
+ key,
111
116
  enabled: false,
112
117
  tracker: undefined,
113
118
  } as LDAIAgentConfig;
114
119
  case 'judge':
115
120
  return {
121
+ key,
116
122
  enabled: false,
117
123
  tracker: undefined,
118
124
  evaluationMetricKeys: [],
@@ -121,6 +127,7 @@ export class LDAIConfigUtils {
121
127
  default:
122
128
  // Default to completion config for completion mode or any unexpected mode
123
129
  return {
130
+ key,
124
131
  enabled: false,
125
132
  tracker: undefined,
126
133
  } as LDAICompletionConfig;
@@ -133,8 +140,9 @@ export class LDAIConfigUtils {
133
140
  * @param flagValue The flag value from LaunchDarkly
134
141
  * @returns Base configuration object
135
142
  */
136
- private static _toBaseConfig(flagValue: LDAIConfigFlagValue) {
143
+ private static _toBaseConfig(key: string, flagValue: LDAIConfigFlagValue) {
137
144
  return {
145
+ key,
138
146
  // eslint-disable-next-line no-underscore-dangle
139
147
  enabled: flagValue._ldMeta?.enabled ?? false,
140
148
  model: flagValue.model,
@@ -150,11 +158,12 @@ export class LDAIConfigUtils {
150
158
  * @returns A completion configuration
151
159
  */
152
160
  static toCompletionConfig(
161
+ key: string,
153
162
  flagValue: LDAIConfigFlagValue,
154
163
  tracker: LDAIConfigTracker,
155
164
  ): LDAICompletionConfig {
156
165
  return {
157
- ...this._toBaseConfig(flagValue),
166
+ ...this._toBaseConfig(key, flagValue),
158
167
  tracker,
159
168
  messages: flagValue.messages,
160
169
  judgeConfiguration: flagValue.judgeConfiguration,
@@ -169,11 +178,12 @@ export class LDAIConfigUtils {
169
178
  * @returns An agent configuration
170
179
  */
171
180
  static toAgentConfig(
181
+ key: string,
172
182
  flagValue: LDAIConfigFlagValue,
173
183
  tracker: LDAIConfigTracker,
174
184
  ): LDAIAgentConfig {
175
185
  return {
176
- ...this._toBaseConfig(flagValue),
186
+ ...this._toBaseConfig(key, flagValue),
177
187
  tracker,
178
188
  instructions: flagValue.instructions,
179
189
  judgeConfiguration: flagValue.judgeConfiguration,
@@ -188,11 +198,12 @@ export class LDAIConfigUtils {
188
198
  * @returns A judge configuration
189
199
  */
190
200
  static toJudgeConfig(
201
+ key: string,
191
202
  flagValue: LDAIConfigFlagValue,
192
203
  tracker: LDAIConfigTracker,
193
204
  ): LDAIJudgeConfig {
194
205
  return {
195
- ...this._toBaseConfig(flagValue),
206
+ ...this._toBaseConfig(key, flagValue),
196
207
  tracker,
197
208
  messages: flagValue.messages,
198
209
  evaluationMetricKeys: flagValue.evaluationMetricKeys || [],
@@ -95,6 +95,10 @@ export interface LDAIConfigDefault {
95
95
  * Base AI Config interface without mode-specific fields.
96
96
  */
97
97
  export interface LDAIConfig extends Omit<LDAIConfigDefault, 'enabled'> {
98
+ /**
99
+ * The key of the AI Config.
100
+ */
101
+ key: string;
98
102
  /**
99
103
  * Whether the configuration is enabled.
100
104
  */
@@ -91,6 +91,7 @@ export class Judge {
91
91
  return {
92
92
  evals,
93
93
  success,
94
+ judgeConfigKey: this._aiConfig.key,
94
95
  };
95
96
  } catch (error) {
96
97
  this._logger?.error('Judge evaluation failed:', error);
@@ -98,6 +99,7 @@ export class Judge {
98
99
  evals: {},
99
100
  success: false,
100
101
  error: error instanceof Error ? error.message : 'Unknown error',
102
+ judgeConfigKey: this._aiConfig.key,
101
103
  };
102
104
  }
103
105
  }
@@ -30,6 +30,8 @@ export interface EvalScore {
30
30
  * Response from a judge evaluation containing scores and reasoning for multiple metrics.
31
31
  */
32
32
  export interface JudgeResponse {
33
+ /** The key of the judge configuration that was used to generate this response */
34
+ judgeConfigKey?: string;
33
35
  /** Dictionary where keys are metric names and values contain score and reasoning */
34
36
  evals: Record<string, EvalScore>;
35
37
  /** Whether the evaluation completed successfully */