Skip to content

Commit 31a80d6

Browse files
committed
pr feedback, cleanup, fixing tests
1 parent c51d811 commit 31a80d6

File tree

8 files changed

+451
-481
lines changed

8 files changed

+451
-481
lines changed

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

Lines changed: 356 additions & 380 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class LDAIClientImpl implements LDAIClient {
6262

6363
// Validate mode match
6464
// eslint-disable-next-line no-underscore-dangle
65-
const flagMode = value._ldMeta?.mode;
65+
const flagMode = value._ldMeta?.mode ?? 'completion';
6666
if (flagMode !== mode) {
6767
this._logger?.warn(
6868
`AI Config mode mismatch for ${key}: expected ${mode}, got ${flagMode}. Returning disabled config.`,

packages/sdk/server-ai/src/api/LDAIClient.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ export interface LDAIClient {
238238
* @param context The standard LDContext used when evaluating flags.
239239
* @param defaultValue A default value representing a standard AI chat config result.
240240
* @param variables Dictionary of values for instruction interpolation.
241+
* The variables will also be used for judge evaluation. For the judge only, the variables
242+
* `message_history` and `response_to_evaluate` are reserved and will be ignored.
243+
* @param defaultAiProvider Optional default AI provider to use.
241244
* @returns A promise that resolves to the TrackedChat instance, or null if the configuration is disabled.
242245
*
243246
* @example
@@ -258,10 +261,6 @@ export interface LDAIClient {
258261
* if (chat) {
259262
* const response = await chat.invoke("I need help with my order");
260263
* console.log(response.message.content);
261-
*
262-
* // Access configuration and tracker if needed
263-
* console.log('Model:', chat.getConfig().model?.name);
264-
* chat.getTracker().trackSuccess();
265264
* }
266265
* ```
267266
*/
@@ -290,7 +289,9 @@ export interface LDAIClient {
290289
* @param key The key identifying the AI judge configuration to use
291290
* @param context Standard LDContext used when evaluating flags
292291
* @param defaultValue A default value representing a standard AI config result
293-
* @param variables Dictionary of values for instruction interpolation
292+
* @param variables Dictionary of values for instruction interpolation.
293+
* The variables `message_history` and `response_to_evaluate` are reserved for the judge and will be ignored.
294+
* @param defaultAiProvider Optional default AI provider to use.
294295
* @returns Promise that resolves to a Judge instance or undefined if disabled/unsupported
295296
*
296297
* @example

packages/sdk/server-ai/src/api/chat/TrackedChat.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,15 @@ import { ChatResponse } from './types';
1515
*/
1616
export class TrackedChat {
1717
protected messages: LDMessage[];
18-
protected judges: Record<string, Judge>;
19-
private readonly _logger?: LDLogger;
2018

2119
constructor(
2220
protected readonly aiConfig: LDAICompletionConfig,
2321
protected readonly tracker: LDAIConfigTracker,
2422
protected readonly provider: AIProvider,
25-
judges?: Record<string, Judge>,
26-
logger?: LDLogger,
23+
protected readonly judges: Record<string, Judge> = {},
24+
private readonly _logger?: LDLogger,
2725
) {
2826
this.messages = [];
29-
this.judges = judges || {};
30-
this._logger = logger;
3127
}
3228

3329
/**
@@ -93,16 +89,14 @@ export class TrackedChat {
9389

9490
const evalResult = await judge.evaluateMessages(messages, response, judgeConfig.samplingRate);
9591

96-
// Track scores if evaluation was successful
9792
if (evalResult && evalResult.success) {
9893
this.tracker.trackEvalScores(evalResult.evals);
9994
}
10095

10196
return evalResult;
10297
});
10398

104-
// Use Promise.allSettled to ensure all evaluations complete
105-
// even if some fail
99+
// ensure all evaluations complete even if some fail
106100
const results = await Promise.allSettled(evaluationPromises);
107101

108102
return results.map((result) => (result.status === 'fulfilled' ? result.value : undefined));

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ export class LDAIConfigUtils {
8787
// eslint-disable-next-line no-underscore-dangle
8888
const flagValueMode = flagValue._ldMeta?.mode;
8989

90-
// Convert to appropriate config type based on actual mode
9190
switch (flagValueMode) {
9291
case 'agent':
9392
return this.toAgentConfig(flagValue, tracker);

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

Lines changed: 82 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
import { LDAIConfigTracker } from './LDAIConfigTracker';
22
import { VercelAISDKConfig, VercelAISDKMapOptions, VercelAISDKProvider } from './VercelAISDK';
33

4+
// ============================================================================
5+
// Foundation Types
6+
// ============================================================================
7+
8+
/**
9+
* Information about prompts.
10+
*/
11+
export interface LDMessage {
12+
/**
13+
* The role of the prompt.
14+
*/
15+
role: 'user' | 'assistant' | 'system';
16+
/**
17+
* Content for the prompt.
18+
*/
19+
content: string;
20+
}
21+
422
/**
523
* Configuration related to the model.
624
*/
@@ -28,6 +46,10 @@ export interface LDProviderConfig {
2846
name: string;
2947
}
3048

49+
// ============================================================================
50+
// Judge Types
51+
// ============================================================================
52+
3153
/**
3254
* Configuration for a single judge attachment.
3355
*/
@@ -46,6 +68,30 @@ export interface LDJudgeConfiguration {
4668
judges: LDJudge[];
4769
}
4870

71+
// ============================================================================
72+
// Base AI Config Types
73+
// ============================================================================
74+
75+
/**
76+
* Base AI Config interface for default implementations with optional enabled property.
77+
*/
78+
export interface LDAIConfigDefault {
79+
/**
80+
* Optional model configuration.
81+
*/
82+
model?: LDModelConfig;
83+
84+
/**
85+
* Optional configuration for the provider.
86+
*/
87+
provider?: LDProviderConfig;
88+
89+
/**
90+
* Whether the configuration is enabled. Defaults to false when not provided.
91+
*/
92+
enabled?: boolean;
93+
}
94+
4995
/**
5096
* Base AI Config interface without mode-specific fields.
5197
*/
@@ -81,44 +127,9 @@ export interface LDAIConfig extends Omit<LDAIConfigDefault, 'enabled'> {
81127
) => VercelAISDKConfig<TMod>;
82128
}
83129

84-
/**
85-
* Base AI Config interface for default implementations with optional enabled property.
86-
*/
87-
export interface LDAIConfigDefault {
88-
/**
89-
* Optional model configuration.
90-
*/
91-
model?: LDModelConfig;
92-
93-
/**
94-
* Optional configuration for the provider.
95-
*/
96-
provider?: LDProviderConfig;
97-
98-
/**
99-
* Whether the configuration is enabled. Defaults to false when not provided.
100-
*/
101-
enabled?: boolean;
102-
}
103-
104-
/**
105-
* Default implementation types for AI Configs with optional enabled property.
106-
*/
107-
108-
/**
109-
* Default Judge-specific AI Config with required evaluation metric key.
110-
*/
111-
export interface LDAIJudgeConfigDefault extends LDAIConfigDefault {
112-
/**
113-
* Optional prompt data for judge configurations.
114-
*/
115-
messages?: LDMessage[];
116-
/**
117-
* Evaluation metric keys for judge configurations.
118-
* The keys of the metrics that this judge can evaluate.
119-
*/
120-
evaluationMetricKeys?: string[];
121-
}
130+
// ============================================================================
131+
// Default AI Config Implementation Types
132+
// ============================================================================
122133

123134
/**
124135
* Default Agent-specific AI Config with instructions.
@@ -151,13 +162,9 @@ export interface LDAICompletionConfigDefault extends LDAIConfigDefault {
151162
}
152163

153164
/**
154-
* Non-default implementation types for AI Configs with required enabled property and tracker.
155-
*/
156-
157-
/**
158-
* Judge-specific AI Config with required evaluation metric key.
165+
* Default Judge-specific AI Config with required evaluation metric key.
159166
*/
160-
export interface LDAIJudgeConfig extends LDAIConfig {
167+
export interface LDAIJudgeConfigDefault extends LDAIConfigDefault {
161168
/**
162169
* Optional prompt data for judge configurations.
163170
*/
@@ -166,9 +173,21 @@ export interface LDAIJudgeConfig extends LDAIConfig {
166173
* Evaluation metric keys for judge configurations.
167174
* The keys of the metrics that this judge can evaluate.
168175
*/
169-
evaluationMetricKeys: string[];
176+
evaluationMetricKeys?: string[];
170177
}
171178

179+
/**
180+
* Union type for all default AI Config variants.
181+
*/
182+
export type LDAIConfigDefaultKind =
183+
| LDAIAgentConfigDefault
184+
| LDAICompletionConfigDefault
185+
| LDAIJudgeConfigDefault;
186+
187+
// ============================================================================
188+
// AI Config Implementation Types
189+
// ============================================================================
190+
172191
/**
173192
* Agent-specific AI Config with instructions.
174193
*/
@@ -200,31 +219,32 @@ export interface LDAICompletionConfig extends LDAIConfig {
200219
}
201220

202221
/**
203-
* Information about prompts.
222+
* Judge-specific AI Config with required evaluation metric key.
204223
*/
205-
export interface LDMessage {
224+
export interface LDAIJudgeConfig extends LDAIConfig {
206225
/**
207-
* The role of the prompt.
226+
* Optional prompt data for judge configurations.
208227
*/
209-
role: 'user' | 'assistant' | 'system';
228+
messages?: LDMessage[];
210229
/**
211-
* Content for the prompt.
230+
* Evaluation metric keys for judge configurations.
231+
* The keys of the metrics that this judge can evaluate.
212232
*/
213-
content: string;
233+
evaluationMetricKeys: string[];
214234
}
215235

236+
// ============================================================================
237+
// Union Types
238+
// ============================================================================
239+
216240
/**
217241
* Union type for all AI Config variants.
218242
*/
219-
export type LDAIConfigKind = LDAICompletionConfig | LDAIAgentConfig | LDAIJudgeConfig;
243+
export type LDAIConfigKind = LDAIAgentConfig | LDAICompletionConfig | LDAIJudgeConfig;
220244

221-
/**
222-
* Union type for all default AI Config variants.
223-
*/
224-
export type LDAIConfigDefaultKind =
225-
| LDAICompletionConfigDefault
226-
| LDAIAgentConfigDefault
227-
| LDAIJudgeConfigDefault;
245+
// ============================================================================
246+
// Agent-Specific Request Type
247+
// ============================================================================
228248

229249
/**
230250
* Configuration for a single agent request.
@@ -246,15 +266,9 @@ export interface LDAIAgentRequestConfig {
246266
variables?: Record<string, unknown>;
247267
}
248268

249-
/**
250-
* AI Config agent interface (extends agent config without tracker and toVercelAISDK).
251-
*/
252-
export interface LDAIAgent extends Omit<LDAIAgentConfig, 'toVercelAISDK' | 'tracker'> {
253-
/**
254-
* Instructions for the agent.
255-
*/
256-
instructions?: string;
257-
}
269+
// ============================================================================
270+
// Mode Type
271+
// ============================================================================
258272

259273
/**
260274
* Mode type for AI configurations.

0 commit comments

Comments
 (0)