@launchdarkly/server-sdk-ai 0.19.1 → 1.0.0
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.
- package/CHANGELOG.md +67 -0
- package/README.md +13 -16
- package/dist/index.cjs +617 -478
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +680 -443
- package/dist/index.d.ts +680 -443
- package/dist/index.js +613 -473
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
import * as common from '@launchdarkly/js-server-sdk-common';
|
|
2
2
|
import { LDLogger as LDLogger$1, LDContext, LDFlagValue } from '@launchdarkly/js-server-sdk-common';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Result from a judge evaluation containing score, reasoning, and metadata.
|
|
6
|
+
*/
|
|
7
|
+
interface LDJudgeResult {
|
|
8
|
+
/** The key of the judge configuration that was used to generate this result */
|
|
9
|
+
judgeConfigKey?: string;
|
|
10
|
+
/** Whether the evaluation completed successfully */
|
|
11
|
+
success: boolean;
|
|
12
|
+
/** Error message if evaluation failed */
|
|
13
|
+
errorMessage?: string;
|
|
14
|
+
/** Whether this evaluation was sampled (i.e. actually run). False when skipped by sampling. */
|
|
15
|
+
sampled: boolean;
|
|
16
|
+
/** The metric key for this evaluation */
|
|
17
|
+
metricKey?: string;
|
|
18
|
+
/** Score between 0.0 and 1.0 indicating the evaluation result */
|
|
19
|
+
score?: number;
|
|
20
|
+
/** Reasoning behind the provided score */
|
|
21
|
+
reasoning?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Feedback about the generated content.
|
|
26
|
+
*/
|
|
27
|
+
declare enum LDFeedbackKind {
|
|
28
|
+
/**
|
|
29
|
+
* The sentiment was positive.
|
|
30
|
+
*/
|
|
31
|
+
Positive = "positive",
|
|
32
|
+
/**
|
|
33
|
+
* The sentiment is negative.
|
|
34
|
+
*/
|
|
35
|
+
Negative = "negative"
|
|
36
|
+
}
|
|
37
|
+
|
|
4
38
|
/**
|
|
5
39
|
* Information about token usage.
|
|
6
40
|
*/
|
|
@@ -32,105 +66,116 @@ interface LDAIMetrics {
|
|
|
32
66
|
* Token usage information for the operation.
|
|
33
67
|
* This will be undefined if no token usage data is available.
|
|
34
68
|
*/
|
|
35
|
-
|
|
69
|
+
tokens?: LDTokenUsage;
|
|
70
|
+
/**
|
|
71
|
+
* List of tool call identifiers made during the operation.
|
|
72
|
+
* This will be undefined if no tool calls were made.
|
|
73
|
+
*/
|
|
74
|
+
toolCalls?: string[];
|
|
75
|
+
/**
|
|
76
|
+
* Duration of the operation in milliseconds.
|
|
77
|
+
* This will be undefined if duration was not tracked.
|
|
78
|
+
*/
|
|
79
|
+
durationMs?: number;
|
|
36
80
|
}
|
|
37
81
|
|
|
38
82
|
/**
|
|
39
|
-
*
|
|
83
|
+
* Summary metrics returned in a ManagedResult or from LDAIConfigTracker.getSummary().
|
|
84
|
+
* Provides a flat view of the key metrics for the completed operation.
|
|
40
85
|
*/
|
|
41
|
-
interface
|
|
42
|
-
/** The structured data returned by the model */
|
|
43
|
-
data: Record<string, unknown>;
|
|
44
|
-
/** The raw response from the model */
|
|
45
|
-
rawResponse: string;
|
|
86
|
+
interface LDAIMetricSummary {
|
|
46
87
|
/**
|
|
47
|
-
*
|
|
88
|
+
* Whether the AI operation was successful.
|
|
48
89
|
*/
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
90
|
+
success?: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Token usage information, if available.
|
|
93
|
+
*/
|
|
94
|
+
tokens?: LDTokenUsage;
|
|
95
|
+
/**
|
|
96
|
+
* List of tool call identifiers made during the operation, if any.
|
|
97
|
+
*/
|
|
98
|
+
toolCalls?: string[];
|
|
99
|
+
/**
|
|
100
|
+
* Duration of the operation in milliseconds, if tracked.
|
|
101
|
+
*/
|
|
102
|
+
durationMs?: number;
|
|
103
|
+
/**
|
|
104
|
+
* Time to first token in milliseconds, if tracked.
|
|
105
|
+
*/
|
|
106
|
+
timeToFirstTokenMs?: number;
|
|
107
|
+
/**
|
|
108
|
+
* User feedback sentiment for this generation, if provided.
|
|
109
|
+
*/
|
|
110
|
+
feedback?: {
|
|
111
|
+
kind: LDFeedbackKind;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Resumption token for deferred feedback association.
|
|
115
|
+
*/
|
|
116
|
+
resumptionToken?: string;
|
|
69
117
|
}
|
|
70
|
-
|
|
71
|
-
declare function createBedrockTokenUsage(data: {
|
|
72
|
-
totalTokens?: number;
|
|
73
|
-
inputTokens?: number;
|
|
74
|
-
outputTokens?: number;
|
|
75
|
-
}): LDTokenUsage;
|
|
76
|
-
|
|
77
|
-
declare function createOpenAiUsage(data: {
|
|
78
|
-
total_tokens?: number;
|
|
79
|
-
prompt_tokens?: number;
|
|
80
|
-
completion_tokens?: number;
|
|
81
|
-
}): LDTokenUsage;
|
|
82
|
-
|
|
83
118
|
/**
|
|
84
|
-
*
|
|
119
|
+
* The result returned by a Runner (provider-level) invocation.
|
|
120
|
+
* Providers implement Runner and return RunnerResult from run().
|
|
121
|
+
* This type does NOT include evaluations — those are wired in the managed layer.
|
|
85
122
|
*/
|
|
86
|
-
|
|
123
|
+
interface RunnerResult {
|
|
87
124
|
/**
|
|
88
|
-
* The
|
|
125
|
+
* The text content of the model's response.
|
|
89
126
|
*/
|
|
90
|
-
|
|
127
|
+
content: string;
|
|
91
128
|
/**
|
|
92
|
-
*
|
|
129
|
+
* Metrics information for the operation.
|
|
93
130
|
*/
|
|
94
|
-
|
|
131
|
+
metrics: LDAIMetrics;
|
|
132
|
+
/**
|
|
133
|
+
* The raw response object from the provider, if available.
|
|
134
|
+
*/
|
|
135
|
+
raw?: unknown;
|
|
136
|
+
/**
|
|
137
|
+
* Parsed structured output, if the provider returned structured data.
|
|
138
|
+
*/
|
|
139
|
+
parsed?: Record<string, unknown>;
|
|
95
140
|
}
|
|
96
|
-
|
|
97
|
-
declare function createVercelAISDKTokenUsage(data: {
|
|
98
|
-
totalTokens?: number;
|
|
99
|
-
inputTokens?: number;
|
|
100
|
-
promptTokens?: number;
|
|
101
|
-
outputTokens?: number;
|
|
102
|
-
completionTokens?: number;
|
|
103
|
-
}): LDTokenUsage;
|
|
104
|
-
|
|
105
141
|
/**
|
|
106
|
-
*
|
|
142
|
+
* The result returned by a managed model invocation (ManagedModel.run()).
|
|
143
|
+
* Includes a promise for asynchronous judge evaluations.
|
|
107
144
|
*/
|
|
108
|
-
interface
|
|
145
|
+
interface ManagedResult {
|
|
109
146
|
/**
|
|
110
|
-
* The
|
|
147
|
+
* The text content of the model's response.
|
|
111
148
|
*/
|
|
112
|
-
|
|
149
|
+
content: string;
|
|
113
150
|
/**
|
|
114
|
-
*
|
|
151
|
+
* Summarized metrics for this invocation.
|
|
115
152
|
*/
|
|
116
|
-
|
|
153
|
+
metrics: LDAIMetricSummary;
|
|
117
154
|
/**
|
|
118
|
-
*
|
|
155
|
+
* The raw response object from the provider, if available.
|
|
119
156
|
*/
|
|
120
|
-
|
|
157
|
+
raw?: unknown;
|
|
121
158
|
/**
|
|
122
|
-
*
|
|
159
|
+
* Parsed structured output, if available.
|
|
123
160
|
*/
|
|
124
|
-
|
|
125
|
-
kind: LDFeedbackKind;
|
|
126
|
-
};
|
|
161
|
+
parsed?: Record<string, unknown>;
|
|
127
162
|
/**
|
|
128
|
-
*
|
|
163
|
+
* Promise that resolves to the judge evaluation results.
|
|
164
|
+
* This promise encapsulates both evaluation and tracking
|
|
165
|
+
* (tracker.trackJudgeResult is called when it resolves).
|
|
166
|
+
* Awaiting this promise guarantees both evaluation and tracking are complete.
|
|
129
167
|
*/
|
|
130
|
-
|
|
168
|
+
evaluations: Promise<LDJudgeResult[]>;
|
|
131
169
|
}
|
|
170
|
+
|
|
132
171
|
/**
|
|
133
|
-
* The LDAIConfigTracker
|
|
172
|
+
* The LDAIConfigTracker records metrics for a single AI run.
|
|
173
|
+
*
|
|
174
|
+
* All events a tracker emits share a runId (a UUIDv4) so LaunchDarkly can
|
|
175
|
+
* correlate them in metrics views. See individual track methods for their
|
|
176
|
+
* specific semantics. Call `createTracker` on the AI Config to start a new
|
|
177
|
+
* run. A resumption token preserves the runId, so events emitted by a
|
|
178
|
+
* tracker reconstructed in another process correlate with the original run.
|
|
134
179
|
*/
|
|
135
180
|
interface LDAIConfigTracker {
|
|
136
181
|
/**
|
|
@@ -149,51 +194,49 @@ interface LDAIConfigTracker {
|
|
|
149
194
|
* A URL-safe Base64-encoded token that encodes the tracker's runId, configKey,
|
|
150
195
|
* variationKey, and version. Pass this to AIClient.createTracker() to reconstruct
|
|
151
196
|
* the tracker across process boundaries (e.g. for associating deferred feedback
|
|
152
|
-
* with the original
|
|
197
|
+
* with the original AI run).
|
|
153
198
|
*/
|
|
154
199
|
readonly resumptionToken: string;
|
|
155
200
|
/**
|
|
156
201
|
* Track the duration of generation.
|
|
157
202
|
*
|
|
158
|
-
* At-most-once per execution: subsequent calls on the same tracker are dropped
|
|
159
|
-
* with a warning. Use createTracker() on the config result to obtain a fresh
|
|
160
|
-
* tracker for a new execution.
|
|
161
|
-
*
|
|
162
203
|
* Ideally this would not include overhead time such as network communication.
|
|
163
204
|
*
|
|
164
205
|
* @param durationMs The duration in milliseconds.
|
|
206
|
+
*
|
|
207
|
+
* @remarks Records at most once per Tracker; further calls are ignored.
|
|
165
208
|
*/
|
|
166
209
|
trackDuration(durationMs: number): void;
|
|
167
210
|
/**
|
|
168
211
|
* Track information about token usage.
|
|
169
212
|
*
|
|
170
|
-
* At-most-once per execution: subsequent calls on the same tracker are dropped
|
|
171
|
-
* with a warning.
|
|
172
|
-
*
|
|
173
213
|
* @param tokens Token usage information.
|
|
214
|
+
*
|
|
215
|
+
* @remarks Records at most once per Tracker; further calls are ignored.
|
|
174
216
|
*/
|
|
175
217
|
trackTokens(tokens: LDTokenUsage): void;
|
|
176
218
|
/**
|
|
177
219
|
* Generation was successful.
|
|
178
220
|
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
221
|
+
* @remarks Records at most once per Tracker. trackSuccess and trackError share
|
|
222
|
+
* state; only one of the two can record per Tracker, and subsequent calls are
|
|
223
|
+
* ignored.
|
|
181
224
|
*/
|
|
182
225
|
trackSuccess(): void;
|
|
183
226
|
/**
|
|
184
227
|
* An error was encountered during generation.
|
|
185
228
|
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
229
|
+
* @remarks Records at most once per Tracker. trackSuccess and trackError share
|
|
230
|
+
* state; only one of the two can record per Tracker, and subsequent calls are
|
|
231
|
+
* ignored.
|
|
188
232
|
*/
|
|
189
233
|
trackError(): void;
|
|
190
234
|
/**
|
|
191
235
|
* Track sentiment about the generation.
|
|
192
236
|
*
|
|
193
|
-
* At-most-once per execution: subsequent calls on the same tracker are dropped
|
|
194
|
-
* with a warning.
|
|
195
|
-
*
|
|
196
237
|
* @param feedback Feedback about the generation.
|
|
238
|
+
*
|
|
239
|
+
* @remarks Records at most once per Tracker; further calls are ignored.
|
|
197
240
|
*/
|
|
198
241
|
trackFeedback(feedback: {
|
|
199
242
|
kind: LDFeedbackKind;
|
|
@@ -201,10 +244,9 @@ interface LDAIConfigTracker {
|
|
|
201
244
|
/**
|
|
202
245
|
* Track the time to first token for this generation.
|
|
203
246
|
*
|
|
204
|
-
* At-most-once per execution: subsequent calls on the same tracker are dropped
|
|
205
|
-
* with a warning.
|
|
206
|
-
*
|
|
207
247
|
* @param timeToFirstTokenMs The duration in milliseconds.
|
|
248
|
+
*
|
|
249
|
+
* @remarks Records at most once per Tracker; further calls are ignored.
|
|
208
250
|
*/
|
|
209
251
|
trackTimeToFirstToken(timeToFirstTokenMs: number): void;
|
|
210
252
|
/**
|
|
@@ -213,22 +255,31 @@ interface LDAIConfigTracker {
|
|
|
213
255
|
* No event is emitted when the result was not sampled (result.sampled is false).
|
|
214
256
|
*
|
|
215
257
|
* @param result Judge result containing score, reasoning, and metadata
|
|
258
|
+
*
|
|
259
|
+
* @remarks May be called multiple times per Tracker; each call records the
|
|
260
|
+
* scores from the given response.
|
|
216
261
|
*/
|
|
217
262
|
trackJudgeResult(result: LDJudgeResult): void;
|
|
218
263
|
/**
|
|
219
264
|
* Track a single tool invocation.
|
|
220
265
|
*
|
|
221
266
|
* @param toolKey The identifier of the tool that was invoked.
|
|
267
|
+
*
|
|
268
|
+
* @remarks May be called multiple times per Tracker; each call records the
|
|
269
|
+
* given tool call.
|
|
222
270
|
*/
|
|
223
271
|
trackToolCall(toolKey: string): void;
|
|
224
272
|
/**
|
|
225
273
|
* Track multiple tool invocations.
|
|
226
274
|
*
|
|
227
275
|
* @param toolKeys The identifiers of the tools that were invoked.
|
|
276
|
+
*
|
|
277
|
+
* @remarks May be called multiple times per Tracker; each call records the
|
|
278
|
+
* given tool calls.
|
|
228
279
|
*/
|
|
229
280
|
trackToolCalls(toolKeys: string[]): void;
|
|
230
281
|
/**
|
|
231
|
-
* Track the duration of
|
|
282
|
+
* Track the duration of the provided function.
|
|
232
283
|
*
|
|
233
284
|
* If the provided function throws, then this method will also throw.
|
|
234
285
|
* In the case the provided function throws, this function will still record the duration.
|
|
@@ -237,30 +288,38 @@ interface LDAIConfigTracker {
|
|
|
237
288
|
*
|
|
238
289
|
* @param func The function to track the duration of.
|
|
239
290
|
* @returns The result of the function.
|
|
291
|
+
*
|
|
292
|
+
* @remarks Because each inner metric is at-most-once per Tracker, calling
|
|
293
|
+
* this twice on the same Tracker will run the inner function again but
|
|
294
|
+
* produce no additional metric events.
|
|
240
295
|
*/
|
|
241
296
|
trackDurationOf(func: () => Promise<any>): Promise<any>;
|
|
242
297
|
/**
|
|
243
298
|
* Track metrics for a generic AI operation.
|
|
244
299
|
*
|
|
245
|
-
* This function will track the duration of the
|
|
300
|
+
* This function will track the duration of the AI run, extract metrics using the provided
|
|
246
301
|
* metrics extractor function, and track success or error status accordingly.
|
|
247
302
|
*
|
|
248
303
|
* If the provided function throws, then this method will also throw.
|
|
249
304
|
* In the case the provided function throws, this function will record the duration and an error.
|
|
250
|
-
* A failed
|
|
305
|
+
* A failed AI run will not have any token usage data.
|
|
251
306
|
*
|
|
252
|
-
* @param metricsExtractor Function that extracts LDAIMetrics from the
|
|
253
|
-
* @param func Function which executes the
|
|
254
|
-
* @returns The result of the
|
|
307
|
+
* @param metricsExtractor Function that extracts LDAIMetrics from the AI run result
|
|
308
|
+
* @param func Function which executes the AI run
|
|
309
|
+
* @returns The result of the AI run
|
|
310
|
+
*
|
|
311
|
+
* @remarks Subsequent calls re-run the inner function but emit only metrics
|
|
312
|
+
* not already recorded on this Tracker. Call createTracker on the AI Config
|
|
313
|
+
* to start a new run.
|
|
255
314
|
*/
|
|
256
315
|
trackMetricsOf<TRes>(metricsExtractor: (result: TRes) => LDAIMetrics, func: () => Promise<TRes>): Promise<TRes>;
|
|
257
316
|
/**
|
|
258
317
|
* Track metrics for a streaming AI operation.
|
|
259
318
|
*
|
|
260
|
-
* This function will track the duration of the
|
|
319
|
+
* This function will track the duration of the AI run, extract metrics using the provided
|
|
261
320
|
* metrics extractor function, and track success or error status accordingly.
|
|
262
321
|
*
|
|
263
|
-
* Unlike trackMetricsOf, this method is designed for streaming
|
|
322
|
+
* Unlike trackMetricsOf, this method is designed for streaming AI runs where:
|
|
264
323
|
* - The stream is created and returned immediately (synchronously)
|
|
265
324
|
* - Metrics are extracted asynchronously in the background once the stream completes
|
|
266
325
|
* - Duration is tracked from stream creation to metrics extraction completion
|
|
@@ -274,69 +333,12 @@ interface LDAIConfigTracker {
|
|
|
274
333
|
* @param streamCreator Function that creates and returns the stream (synchronous)
|
|
275
334
|
* @param metricsExtractor Function that asynchronously extracts metrics from the stream
|
|
276
335
|
* @returns The stream result (returned immediately, not a Promise)
|
|
277
|
-
*/
|
|
278
|
-
trackStreamMetricsOf<TStream>(streamCreator: () => TStream, metricsExtractor: (stream: TStream) => Promise<LDAIMetrics>): TStream;
|
|
279
|
-
/**
|
|
280
|
-
* Track an OpenAI operation.
|
|
281
336
|
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
* In the case the provided function throws, this function will record the duration and an error.
|
|
286
|
-
* A failed operation will not have any token usage data.
|
|
287
|
-
*
|
|
288
|
-
* @param func Function which executes the operation.
|
|
289
|
-
* @returns The result of the operation.
|
|
290
|
-
*/
|
|
291
|
-
trackOpenAIMetrics<TRes extends {
|
|
292
|
-
usage?: {
|
|
293
|
-
total_tokens?: number;
|
|
294
|
-
prompt_tokens?: number;
|
|
295
|
-
completion_tokens?: number;
|
|
296
|
-
};
|
|
297
|
-
}>(func: () => Promise<TRes>): Promise<TRes>;
|
|
298
|
-
/**
|
|
299
|
-
* Track an operation which uses Bedrock.
|
|
300
|
-
*
|
|
301
|
-
* This function will track the duration of the operation, the token usage, and the success or error status.
|
|
302
|
-
*
|
|
303
|
-
* @param res The result of the Bedrock operation.
|
|
304
|
-
* @returns The input operation.
|
|
337
|
+
* @remarks Subsequent calls re-run the inner function but emit only metrics
|
|
338
|
+
* not already recorded on this Tracker. Call createTracker on the AI Config
|
|
339
|
+
* to start a new run.
|
|
305
340
|
*/
|
|
306
|
-
|
|
307
|
-
$metadata: {
|
|
308
|
-
httpStatusCode?: number;
|
|
309
|
-
};
|
|
310
|
-
metrics?: {
|
|
311
|
-
latencyMs?: number;
|
|
312
|
-
};
|
|
313
|
-
usage?: {
|
|
314
|
-
inputTokens?: number;
|
|
315
|
-
outputTokens?: number;
|
|
316
|
-
totalTokens?: number;
|
|
317
|
-
};
|
|
318
|
-
}>(res: TRes): TRes;
|
|
319
|
-
/**
|
|
320
|
-
* Track a Vercel AI SDK generateText operation.
|
|
321
|
-
*
|
|
322
|
-
* This function will track the duration of the operation, the token usage, and the success or error status.
|
|
323
|
-
*
|
|
324
|
-
* If the provided function throws, then this method will also throw.
|
|
325
|
-
* In the case the provided function throws, this function will record the duration and an error.
|
|
326
|
-
* A failed operation will not have any token usage data.
|
|
327
|
-
*
|
|
328
|
-
* @param func Function which executes the operation.
|
|
329
|
-
* @returns The result of the operation.
|
|
330
|
-
*/
|
|
331
|
-
trackVercelAISDKGenerateTextMetrics<TRes extends {
|
|
332
|
-
usage?: {
|
|
333
|
-
totalTokens?: number;
|
|
334
|
-
inputTokens?: number;
|
|
335
|
-
promptTokens?: number;
|
|
336
|
-
outputTokens?: number;
|
|
337
|
-
completionTokens?: number;
|
|
338
|
-
};
|
|
339
|
-
}>(func: () => Promise<TRes>): Promise<TRes>;
|
|
341
|
+
trackStreamMetricsOf<TStream>(streamCreator: () => TStream, metricsExtractor: (stream: TStream) => Promise<LDAIMetrics>): TStream;
|
|
340
342
|
/**
|
|
341
343
|
* Get a summary of the tracked metrics.
|
|
342
344
|
*/
|
|
@@ -444,11 +446,12 @@ interface LDAIConfig extends Omit<LDAIConfigDefault, 'enabled'> {
|
|
|
444
446
|
*/
|
|
445
447
|
enabled: boolean;
|
|
446
448
|
/**
|
|
447
|
-
* Creates a new tracker for
|
|
448
|
-
*
|
|
449
|
-
*
|
|
449
|
+
* Creates a new tracker for a fresh AI run. Each call mints a new runId (a
|
|
450
|
+
* UUIDv4) that LaunchDarkly uses to correlate the run's events in metrics
|
|
451
|
+
* views. Call this once per AI run; metrics from different runIds cannot be
|
|
452
|
+
* combined.
|
|
450
453
|
*/
|
|
451
|
-
createTracker
|
|
454
|
+
createTracker: () => LDAIConfigTracker;
|
|
452
455
|
}
|
|
453
456
|
/**
|
|
454
457
|
* Default Agent-specific AI Config with instructions.
|
|
@@ -503,12 +506,6 @@ interface LDAIJudgeConfigDefault extends LDAIConfigDefault {
|
|
|
503
506
|
* The key of the metric that this judge can evaluate.
|
|
504
507
|
*/
|
|
505
508
|
evaluationMetricKey?: string;
|
|
506
|
-
/**
|
|
507
|
-
* Evaluation metric keys for judge configurations (legacy).
|
|
508
|
-
* The keys of the metrics that this judge can evaluate.
|
|
509
|
-
* @deprecated Use evaluationMetricKey instead. This field is kept for legacy support.
|
|
510
|
-
*/
|
|
511
|
-
evaluationMetricKeys?: string[];
|
|
512
509
|
}
|
|
513
510
|
/**
|
|
514
511
|
* Union type for all default AI Config variants.
|
|
@@ -567,12 +564,6 @@ interface LDAIJudgeConfig extends LDAIConfig {
|
|
|
567
564
|
* The key of the metric that this judge can evaluate.
|
|
568
565
|
*/
|
|
569
566
|
evaluationMetricKey?: string;
|
|
570
|
-
/**
|
|
571
|
-
* Evaluation metric keys for judge configurations (legacy).
|
|
572
|
-
* The keys of the metrics that this judge can evaluate.
|
|
573
|
-
* @deprecated Use evaluationMetricKey instead. This field is kept for legacy support.
|
|
574
|
-
*/
|
|
575
|
-
evaluationMetricKeys?: string[];
|
|
576
567
|
}
|
|
577
568
|
/**
|
|
578
569
|
* Union type for all AI Config variants.
|
|
@@ -601,244 +592,136 @@ interface LDAIAgentRequestConfig {
|
|
|
601
592
|
type LDAIConfigMode = 'completion' | 'agent' | 'judge';
|
|
602
593
|
|
|
603
594
|
/**
|
|
604
|
-
*
|
|
595
|
+
* Represents a directed edge in an agent graph, connecting a source node to a target node.
|
|
605
596
|
*/
|
|
606
|
-
interface
|
|
607
|
-
/**
|
|
608
|
-
* The response message from the AI.
|
|
609
|
-
*/
|
|
610
|
-
message: LDMessage;
|
|
597
|
+
interface LDGraphEdge {
|
|
611
598
|
/**
|
|
612
|
-
*
|
|
599
|
+
* The key of the target AIAgentConfig node.
|
|
613
600
|
*/
|
|
614
|
-
|
|
601
|
+
key: string;
|
|
615
602
|
/**
|
|
616
|
-
*
|
|
617
|
-
* Only present when judges are configured for evaluation.
|
|
603
|
+
* Optional handoff options that customize how data flows between nodes.
|
|
618
604
|
*/
|
|
619
|
-
|
|
605
|
+
handoff?: Record<string, unknown>;
|
|
620
606
|
}
|
|
621
|
-
|
|
622
607
|
/**
|
|
623
|
-
*
|
|
624
|
-
* This
|
|
625
|
-
* to integrate with LaunchDarkly's tracking and configuration capabilities.
|
|
626
|
-
*
|
|
627
|
-
* Following the AICHAT spec recommendation to use base classes with non-abstract methods
|
|
628
|
-
* for better extensibility and backwards compatibility.
|
|
608
|
+
* Raw flag value for an agent graph configuration as returned by LaunchDarkly.
|
|
609
|
+
* This represents the data structure delivered by LaunchDarkly for graph configurations.
|
|
629
610
|
*/
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
* and return a ChatResponse with the result and metrics.
|
|
637
|
-
*
|
|
638
|
-
* Default implementation takes no action and returns a placeholder response.
|
|
639
|
-
* Provider implementations should override this method.
|
|
640
|
-
*
|
|
641
|
-
* @param messages Array of LDMessage objects representing the conversation
|
|
642
|
-
* @returns Promise that resolves to a ChatResponse containing the model's response
|
|
643
|
-
*/
|
|
644
|
-
invokeModel(_messages: LDMessage[]): Promise<ChatResponse>;
|
|
611
|
+
interface LDAgentGraphFlagValue {
|
|
612
|
+
_ldMeta?: {
|
|
613
|
+
variationKey?: string;
|
|
614
|
+
version?: number;
|
|
615
|
+
enabled?: boolean;
|
|
616
|
+
};
|
|
645
617
|
/**
|
|
646
|
-
*
|
|
647
|
-
* This method should convert messages to provider format, invoke the model with
|
|
648
|
-
* structured output configuration, and return a structured response.
|
|
649
|
-
*
|
|
650
|
-
* Default implementation takes no action and returns a placeholder response.
|
|
651
|
-
* Provider implementations should override this method.
|
|
652
|
-
*
|
|
653
|
-
* @param messages Array of LDMessage objects representing the conversation
|
|
654
|
-
* @param responseStructure Dictionary of output configurations keyed by output name
|
|
655
|
-
* @returns Promise that resolves to a structured response
|
|
618
|
+
* The key of the root AIAgentConfig in the graph.
|
|
656
619
|
*/
|
|
657
|
-
|
|
620
|
+
root: string;
|
|
658
621
|
/**
|
|
659
|
-
*
|
|
660
|
-
* Each provider implementation must provide their own static create method
|
|
661
|
-
* that accepts an AIConfig and returns a configured instance.
|
|
662
|
-
*
|
|
663
|
-
* @param aiConfig The LaunchDarkly AI configuration
|
|
664
|
-
* @param logger Optional logger for the provider
|
|
665
|
-
* @returns Promise that resolves to a configured provider instance
|
|
622
|
+
* Object mapping source agent config keys to arrays of target edges.
|
|
666
623
|
*/
|
|
667
|
-
|
|
624
|
+
edges?: Record<string, LDGraphEdge[]>;
|
|
668
625
|
}
|
|
669
|
-
|
|
670
626
|
/**
|
|
671
|
-
*
|
|
627
|
+
* Summarized graph-level metrics for a completed graph invocation, as
|
|
628
|
+
* returned by {@link ManagedAgentGraph.run} via {@link ManagedGraphResult.metrics}.
|
|
672
629
|
*
|
|
673
|
-
*
|
|
674
|
-
*
|
|
630
|
+
* For the tracker-layer incremental view (where fields populate as tracking
|
|
631
|
+
* calls arrive), see {@link LDGraphTracker.getSummary}, which returns a
|
|
632
|
+
* `Partial<LDAIGraphMetricSummary>`.
|
|
675
633
|
*/
|
|
676
|
-
|
|
677
|
-
private readonly _aiConfig;
|
|
678
|
-
private readonly _aiProvider;
|
|
679
|
-
private readonly _logger?;
|
|
680
|
-
constructor(_aiConfig: LDAIJudgeConfig, _aiProvider: AIProvider, logger?: LDLogger$1);
|
|
634
|
+
interface LDAIGraphMetricSummary {
|
|
681
635
|
/**
|
|
682
|
-
*
|
|
683
|
-
* Falls back to the first valid (non-empty, non-whitespace) value in evaluationMetricKeys if evaluationMetricKey is not provided.
|
|
684
|
-
* Treats empty strings and whitespace-only strings as invalid.
|
|
685
|
-
* @returns The evaluation metric key, or undefined if not available
|
|
636
|
+
* Whether the graph invocation succeeded.
|
|
686
637
|
*/
|
|
687
|
-
|
|
688
|
-
/**
|
|
689
|
-
* Evaluates an AI response using the judge's configuration.
|
|
690
|
-
*
|
|
691
|
-
* @param input The input prompt or question that was provided to the AI
|
|
692
|
-
* @param output The AI-generated response to be evaluated
|
|
693
|
-
* @param samplingRate Sampling rate (0-1) to determine if evaluation should be processed (defaults to 1)
|
|
694
|
-
* @returns Promise that resolves to evaluation results
|
|
695
|
-
*/
|
|
696
|
-
evaluate(input: string, output: string, samplingRate?: number): Promise<LDJudgeResult>;
|
|
697
|
-
/**
|
|
698
|
-
* Evaluates an AI response from chat messages and response.
|
|
699
|
-
*
|
|
700
|
-
* @param messages Array of messages representing the conversation history
|
|
701
|
-
* @param response The AI response to be evaluated
|
|
702
|
-
* @param samplingRatio Sampling ratio (0-1) to determine if evaluation should be processed (defaults to 1)
|
|
703
|
-
* @returns Promise that resolves to evaluation results
|
|
704
|
-
*/
|
|
705
|
-
evaluateMessages(messages: LDMessage[], response: ChatResponse, samplingRatio?: number): Promise<LDJudgeResult>;
|
|
638
|
+
success: boolean;
|
|
706
639
|
/**
|
|
707
|
-
*
|
|
640
|
+
* Execution path through the graph as an ordered array of config keys.
|
|
708
641
|
*/
|
|
709
|
-
|
|
642
|
+
path: string[];
|
|
710
643
|
/**
|
|
711
|
-
*
|
|
644
|
+
* Per-node metric summaries keyed by agent config key.
|
|
712
645
|
*/
|
|
713
|
-
|
|
646
|
+
nodeMetrics: Record<string, LDAIMetricSummary>;
|
|
714
647
|
/**
|
|
715
|
-
*
|
|
648
|
+
* Total graph execution duration in milliseconds, if tracked.
|
|
716
649
|
*/
|
|
717
|
-
|
|
650
|
+
durationMs?: number;
|
|
718
651
|
/**
|
|
719
|
-
*
|
|
652
|
+
* Aggregate token usage across the entire graph invocation, if available.
|
|
720
653
|
*/
|
|
721
|
-
|
|
654
|
+
tokens?: LDTokenUsage;
|
|
722
655
|
/**
|
|
723
|
-
*
|
|
724
|
-
* Returns score and reasoning, or undefined if parsing fails.
|
|
656
|
+
* Resumption token for deferred feedback association.
|
|
725
657
|
*/
|
|
726
|
-
|
|
658
|
+
resumptionToken?: string;
|
|
727
659
|
}
|
|
728
|
-
|
|
729
660
|
/**
|
|
730
|
-
*
|
|
731
|
-
*
|
|
732
|
-
* This class handles conversation management and tracking, while delegating
|
|
733
|
-
* the actual model invocation to the provider.
|
|
661
|
+
* Graph-level metrics for a completed graph run, as returned by a graph runner.
|
|
662
|
+
* Does NOT include handoffs or evaluations — those are managed-layer concerns.
|
|
734
663
|
*/
|
|
735
|
-
|
|
736
|
-
protected readonly aiConfig: LDAICompletionConfig;
|
|
737
|
-
protected readonly provider: AIProvider;
|
|
738
|
-
protected readonly judges: Record<string, Judge>;
|
|
739
|
-
private readonly _logger?;
|
|
740
|
-
protected messages: LDMessage[];
|
|
741
|
-
constructor(aiConfig: LDAICompletionConfig, provider: AIProvider, judges?: Record<string, Judge>, _logger?: LDLogger$1 | undefined);
|
|
742
|
-
/**
|
|
743
|
-
* Invoke the chat model with a prompt string.
|
|
744
|
-
* This method handles conversation management and tracking, delegating to the provider's invokeModel method.
|
|
745
|
-
*/
|
|
746
|
-
invoke(prompt: string): Promise<ChatResponse>;
|
|
664
|
+
interface LDAIGraphMetrics {
|
|
747
665
|
/**
|
|
748
|
-
*
|
|
749
|
-
* Returns a promise that resolves to an array of evaluation results.
|
|
750
|
-
*
|
|
751
|
-
* @param messages Array of messages representing the conversation history
|
|
752
|
-
* @param response The AI response to be evaluated
|
|
753
|
-
* @returns Promise resolving to array of judge evaluation results
|
|
754
|
-
*/
|
|
755
|
-
private _evaluateWithJudges;
|
|
756
|
-
/**
|
|
757
|
-
* Get the underlying AI configuration used to initialize this TrackedChat.
|
|
666
|
+
* Whether the graph invocation succeeded.
|
|
758
667
|
*/
|
|
759
|
-
|
|
668
|
+
success: boolean;
|
|
760
669
|
/**
|
|
761
|
-
*
|
|
762
|
-
* This provides direct access to the provider for advanced use cases.
|
|
670
|
+
* Execution path through the graph as an ordered array of config keys.
|
|
763
671
|
*/
|
|
764
|
-
|
|
672
|
+
path: string[];
|
|
765
673
|
/**
|
|
766
|
-
*
|
|
767
|
-
* Returns a record of judge instances keyed by their configuration keys.
|
|
674
|
+
* Total graph execution duration in milliseconds, if tracked.
|
|
768
675
|
*/
|
|
769
|
-
|
|
676
|
+
durationMs?: number;
|
|
770
677
|
/**
|
|
771
|
-
*
|
|
772
|
-
* Adds messages to the conversation history without invoking the model,
|
|
773
|
-
* which is useful for managing multi-turn conversations or injecting context.
|
|
774
|
-
*
|
|
775
|
-
* @param messages Array of messages to append to the conversation history
|
|
678
|
+
* Aggregate token usage across the entire graph invocation, if available.
|
|
776
679
|
*/
|
|
777
|
-
|
|
680
|
+
tokens?: LDTokenUsage;
|
|
778
681
|
/**
|
|
779
|
-
*
|
|
780
|
-
*
|
|
781
|
-
* @param includeConfigMessages Whether to include the config messages from the AIConfig.
|
|
782
|
-
* Defaults to false.
|
|
783
|
-
* @returns Array of messages. When includeConfigMessages is true, returns both config
|
|
784
|
-
* messages and conversation history with config messages prepended. When false,
|
|
785
|
-
* returns only the conversation history messages.
|
|
682
|
+
* Per-node metrics keyed by agent config key.
|
|
786
683
|
*/
|
|
787
|
-
|
|
684
|
+
nodeMetrics: Record<string, LDAIMetrics>;
|
|
788
685
|
}
|
|
789
|
-
|
|
790
686
|
/**
|
|
791
|
-
*
|
|
687
|
+
* The result returned by a graph runner invocation (provider-level).
|
|
688
|
+
* Does NOT include evaluations or handoffs.
|
|
792
689
|
*/
|
|
793
|
-
interface
|
|
690
|
+
interface AgentGraphRunnerResult {
|
|
794
691
|
/**
|
|
795
|
-
* The
|
|
692
|
+
* The text content of the graph's final response.
|
|
796
693
|
*/
|
|
797
|
-
|
|
798
|
-
/**
|
|
799
|
-
* Optional handoff options that customize how data flows between nodes.
|
|
800
|
-
*/
|
|
801
|
-
handoff?: Record<string, unknown>;
|
|
802
|
-
}
|
|
803
|
-
/**
|
|
804
|
-
* Raw flag value for an agent graph configuration as returned by LaunchDarkly.
|
|
805
|
-
* This represents the data structure delivered by LaunchDarkly for graph configurations.
|
|
806
|
-
*/
|
|
807
|
-
interface LDAgentGraphFlagValue {
|
|
808
|
-
_ldMeta?: {
|
|
809
|
-
variationKey?: string;
|
|
810
|
-
version?: number;
|
|
811
|
-
enabled?: boolean;
|
|
812
|
-
};
|
|
694
|
+
content: string;
|
|
813
695
|
/**
|
|
814
|
-
*
|
|
696
|
+
* Graph-level metrics for this invocation.
|
|
815
697
|
*/
|
|
816
|
-
|
|
698
|
+
metrics: LDAIGraphMetrics;
|
|
817
699
|
/**
|
|
818
|
-
*
|
|
700
|
+
* The raw response object from the provider, if available.
|
|
819
701
|
*/
|
|
820
|
-
|
|
702
|
+
raw?: unknown;
|
|
821
703
|
}
|
|
822
704
|
/**
|
|
823
|
-
*
|
|
705
|
+
* The result returned by a managed graph invocation (ManagedAgentGraph.run()).
|
|
824
706
|
*/
|
|
825
|
-
interface
|
|
707
|
+
interface ManagedGraphResult {
|
|
826
708
|
/**
|
|
827
|
-
*
|
|
709
|
+
* The text content of the graph's final response.
|
|
828
710
|
*/
|
|
829
|
-
|
|
711
|
+
content: string;
|
|
830
712
|
/**
|
|
831
|
-
*
|
|
713
|
+
* Summarized metrics for this graph invocation.
|
|
832
714
|
*/
|
|
833
|
-
|
|
715
|
+
metrics: LDAIGraphMetricSummary;
|
|
834
716
|
/**
|
|
835
|
-
*
|
|
717
|
+
* The raw response object from the provider, if available.
|
|
836
718
|
*/
|
|
837
|
-
|
|
719
|
+
raw?: unknown;
|
|
838
720
|
/**
|
|
839
|
-
*
|
|
721
|
+
* Promise that resolves to the judge evaluation results.
|
|
722
|
+
* Awaiting this promise guarantees both evaluation and tracking are complete.
|
|
840
723
|
*/
|
|
841
|
-
|
|
724
|
+
evaluations: Promise<LDJudgeResult[]>;
|
|
842
725
|
}
|
|
843
726
|
/**
|
|
844
727
|
* Tracking metadata returned by {@link LDGraphTracker.getTrackData}.
|
|
@@ -863,7 +746,111 @@ interface LDGraphTrackData {
|
|
|
863
746
|
}
|
|
864
747
|
|
|
865
748
|
/**
|
|
866
|
-
*
|
|
749
|
+
* Runner protocol for AI model providers.
|
|
750
|
+
*
|
|
751
|
+
* A single Runner interface covers completion, agent, and judge use cases.
|
|
752
|
+
* For structured output (e.g., judge evaluation), pass an `outputType` schema
|
|
753
|
+
* and access the parsed result via `RunnerResult.parsed`.
|
|
754
|
+
*/
|
|
755
|
+
interface Runner {
|
|
756
|
+
/**
|
|
757
|
+
* Invoke the model with the given input string.
|
|
758
|
+
*
|
|
759
|
+
* @param input The string input to the model.
|
|
760
|
+
* @param outputType Optional JSON schema for structured output. When provided,
|
|
761
|
+
* the model should return structured data accessible via `RunnerResult.parsed`.
|
|
762
|
+
* @returns Promise resolving to a RunnerResult.
|
|
763
|
+
*/
|
|
764
|
+
run(input: string, outputType?: Record<string, unknown>): Promise<RunnerResult>;
|
|
765
|
+
}
|
|
766
|
+
/**
|
|
767
|
+
* Runner protocol for agent graph providers.
|
|
768
|
+
*
|
|
769
|
+
* Providers implementing AgentGraphRunner can execute an entire agent graph
|
|
770
|
+
* and return a structured AgentGraphRunnerResult.
|
|
771
|
+
*/
|
|
772
|
+
interface AgentGraphRunner {
|
|
773
|
+
/**
|
|
774
|
+
* Execute the agent graph with the given input.
|
|
775
|
+
*
|
|
776
|
+
* @param input The user input to process through the graph.
|
|
777
|
+
* @returns Promise resolving to an AgentGraphRunnerResult.
|
|
778
|
+
*/
|
|
779
|
+
run(input: string): Promise<AgentGraphRunnerResult>;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
/**
|
|
783
|
+
* ManagedAgent provides agent invocation with automatic tracking and automatic
|
|
784
|
+
* judge evaluation.
|
|
785
|
+
*
|
|
786
|
+
* The class is stateless: each `run()` call sends the prompt directly to the
|
|
787
|
+
* underlying `Runner` and returns a `ManagedResult`. Conversation history,
|
|
788
|
+
* if any, must be managed by the caller (or by the Runner implementation).
|
|
789
|
+
*
|
|
790
|
+
* Obtain an instance via `LDAIClient.createAgent()`.
|
|
791
|
+
*/
|
|
792
|
+
declare class ManagedAgent {
|
|
793
|
+
protected readonly aiAgentConfig: LDAIAgentConfig;
|
|
794
|
+
protected readonly runner: Runner;
|
|
795
|
+
private readonly _logger?;
|
|
796
|
+
constructor(aiAgentConfig: LDAIAgentConfig, runner: Runner, _logger?: LDLogger$1 | undefined);
|
|
797
|
+
/**
|
|
798
|
+
* Invoke the agent with a prompt string and return a ManagedResult.
|
|
799
|
+
*
|
|
800
|
+
* `run()` resolves before `ManagedResult.evaluations` resolves. Awaiting
|
|
801
|
+
* `evaluations` guarantees both judge evaluation and tracker.trackJudgeResult()
|
|
802
|
+
* are complete.
|
|
803
|
+
*
|
|
804
|
+
* @param prompt The user input to send to the agent.
|
|
805
|
+
* @returns Promise resolving to ManagedResult (before evaluations settle).
|
|
806
|
+
*/
|
|
807
|
+
run(prompt: string): Promise<ManagedResult>;
|
|
808
|
+
/**
|
|
809
|
+
* Get the underlying AI agent configuration used to initialize this ManagedAgent.
|
|
810
|
+
*/
|
|
811
|
+
getConfig(): LDAIAgentConfig;
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* ManagedModel provides chat-completion invocation with automatic tracking and
|
|
816
|
+
* automatic judge evaluation.
|
|
817
|
+
*
|
|
818
|
+
* The class is stateless: each `run()` call sends the prompt directly to the
|
|
819
|
+
* underlying `Runner` and returns a `ManagedResult`. Conversation history,
|
|
820
|
+
* if any, must be managed by the caller (or by the Runner implementation).
|
|
821
|
+
*
|
|
822
|
+
* Obtain an instance via `LDAIClient.createModel()`.
|
|
823
|
+
*/
|
|
824
|
+
declare class ManagedModel {
|
|
825
|
+
protected readonly aiConfig: LDAICompletionConfig;
|
|
826
|
+
protected readonly runner: Runner;
|
|
827
|
+
private readonly _logger?;
|
|
828
|
+
constructor(aiConfig: LDAICompletionConfig, runner: Runner, _logger?: LDLogger$1 | undefined);
|
|
829
|
+
/**
|
|
830
|
+
* Invoke the model with a prompt string and return a ManagedResult.
|
|
831
|
+
*
|
|
832
|
+
* `run()` resolves before `ManagedResult.evaluations` resolves. Awaiting
|
|
833
|
+
* `evaluations` guarantees both judge evaluation and tracker.trackJudgeResult()
|
|
834
|
+
* are complete.
|
|
835
|
+
*
|
|
836
|
+
* @param prompt The user input to send to the model.
|
|
837
|
+
* @returns Promise resolving to ManagedResult (before evaluations settle).
|
|
838
|
+
*/
|
|
839
|
+
run(prompt: string): Promise<ManagedResult>;
|
|
840
|
+
/**
|
|
841
|
+
* Get the underlying AI configuration used to initialize this ManagedModel.
|
|
842
|
+
*/
|
|
843
|
+
getConfig(): LDAICompletionConfig;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* The LDGraphTracker records metrics for a single AI run of an agent graph.
|
|
848
|
+
*
|
|
849
|
+
* All events a graph tracker emits share a runId (a UUIDv4) so LaunchDarkly
|
|
850
|
+
* can correlate them in metrics views. Call `createTracker` on the agent
|
|
851
|
+
* graph to start a new run. A resumption token preserves the runId, so events
|
|
852
|
+
* emitted by a tracker reconstructed in another process correlate with the
|
|
853
|
+
* original run.
|
|
867
854
|
*
|
|
868
855
|
* Graph-level methods enforce at-most-once semantics: calling the same method
|
|
869
856
|
* twice on a tracker instance drops the second call and emits a warning.
|
|
@@ -886,11 +873,20 @@ interface LDGraphTracker {
|
|
|
886
873
|
/**
|
|
887
874
|
* Returns tracking metadata to be included in every LDClient.track call.
|
|
888
875
|
*/
|
|
889
|
-
getTrackData():
|
|
876
|
+
getTrackData(): {
|
|
877
|
+
runId: string;
|
|
878
|
+
graphKey: string;
|
|
879
|
+
variationKey?: string;
|
|
880
|
+
version: number;
|
|
881
|
+
};
|
|
890
882
|
/**
|
|
891
|
-
* Returns a snapshot of all graph-level metrics tracked so far.
|
|
883
|
+
* Returns a snapshot of all graph-level metrics tracked so far. Fields
|
|
884
|
+
* populate incrementally as `track*` methods are called, so the result is
|
|
885
|
+
* a `Partial<LDAIGraphMetricSummary>`. Once the graph run has
|
|
886
|
+
* completed via `ManagedAgentGraph.run()`, prefer `ManagedGraphResult.metrics`
|
|
887
|
+
* which is fully populated.
|
|
892
888
|
*/
|
|
893
|
-
getSummary():
|
|
889
|
+
getSummary(): Partial<LDAIGraphMetricSummary>;
|
|
894
890
|
/**
|
|
895
891
|
* A URL-safe Base64-encoded (RFC 4648, no padding) token encoding the tracker's
|
|
896
892
|
* identity. Pass this token to {@link LDGraphTrackerImpl.fromResumptionToken} to
|
|
@@ -902,19 +898,19 @@ interface LDGraphTracker {
|
|
|
902
898
|
*/
|
|
903
899
|
readonly resumptionToken: string;
|
|
904
900
|
/**
|
|
905
|
-
* Tracks a successful graph
|
|
901
|
+
* Tracks a successful graph run.
|
|
906
902
|
* Emits event `$ld:ai:graph:invocation_success` with metric value `1`.
|
|
907
903
|
* At-most-once: subsequent calls are dropped with a warning.
|
|
908
904
|
*/
|
|
909
905
|
trackInvocationSuccess(): void;
|
|
910
906
|
/**
|
|
911
|
-
* Tracks an unsuccessful graph
|
|
907
|
+
* Tracks an unsuccessful graph run.
|
|
912
908
|
* Emits event `$ld:ai:graph:invocation_failure` with metric value `1`.
|
|
913
909
|
* At-most-once: subsequent calls are dropped with a warning.
|
|
914
910
|
*/
|
|
915
911
|
trackInvocationFailure(): void;
|
|
916
912
|
/**
|
|
917
|
-
* Tracks the total duration of the graph
|
|
913
|
+
* Tracks the total duration of the graph run in milliseconds.
|
|
918
914
|
* Emits event `$ld:ai:graph:duration:total` with the duration as the metric value.
|
|
919
915
|
* At-most-once: subsequent calls are dropped with a warning.
|
|
920
916
|
*
|
|
@@ -922,7 +918,7 @@ interface LDGraphTracker {
|
|
|
922
918
|
*/
|
|
923
919
|
trackDuration(durationMs: number): void;
|
|
924
920
|
/**
|
|
925
|
-
* Tracks aggregate token usage across the entire graph
|
|
921
|
+
* Tracks aggregate token usage across the entire graph run.
|
|
926
922
|
* Emits event `$ld:ai:graph:total_tokens` with the total token count as the metric value.
|
|
927
923
|
* At-most-once: subsequent calls are dropped with a warning.
|
|
928
924
|
*
|
|
@@ -930,12 +926,12 @@ interface LDGraphTracker {
|
|
|
930
926
|
*/
|
|
931
927
|
trackTotalTokens(tokens: LDTokenUsage): void;
|
|
932
928
|
/**
|
|
933
|
-
* Tracks the
|
|
929
|
+
* Tracks the path taken through the graph during this run.
|
|
934
930
|
* Emits event `$ld:ai:graph:path` with metric value `1`.
|
|
935
931
|
* The data payload includes the path array in addition to standard track data.
|
|
936
932
|
* At-most-once: subsequent calls are dropped with a warning.
|
|
937
933
|
*
|
|
938
|
-
* @param path An ordered array of agent config keys representing the
|
|
934
|
+
* @param path An ordered array of agent config keys representing the path taken.
|
|
939
935
|
*/
|
|
940
936
|
trackPath(path: string[]): void;
|
|
941
937
|
/**
|
|
@@ -1054,10 +1050,10 @@ declare class AgentGraphDefinition {
|
|
|
1054
1050
|
*/
|
|
1055
1051
|
getConfig(): LDAgentGraphFlagValue;
|
|
1056
1052
|
/**
|
|
1057
|
-
* Returns a new {@link LDGraphTracker} for
|
|
1053
|
+
* Returns a new {@link LDGraphTracker} for a fresh graph run.
|
|
1058
1054
|
*
|
|
1059
|
-
* Call this once per
|
|
1060
|
-
* that groups all events for that
|
|
1055
|
+
* Call this once per graph run. Each call produces a tracker with a fresh `runId`
|
|
1056
|
+
* that groups all events for that run.
|
|
1061
1057
|
*/
|
|
1062
1058
|
createTracker(): LDGraphTracker;
|
|
1063
1059
|
/**
|
|
@@ -1103,6 +1099,139 @@ declare class AgentGraphDefinition {
|
|
|
1103
1099
|
static collectAllKeys(graph: LDAgentGraphFlagValue): Set<string>;
|
|
1104
1100
|
}
|
|
1105
1101
|
|
|
1102
|
+
/**
|
|
1103
|
+
* Judge implementation that handles evaluation functionality and conversation management.
|
|
1104
|
+
*
|
|
1105
|
+
* According to the AIEval spec, judges are AI Configs with mode: "judge" that evaluate
|
|
1106
|
+
* other AI Configs using structured output.
|
|
1107
|
+
*/
|
|
1108
|
+
declare class Judge {
|
|
1109
|
+
private readonly _aiConfig;
|
|
1110
|
+
private readonly _runner;
|
|
1111
|
+
private readonly _sampleRate;
|
|
1112
|
+
private readonly _logger?;
|
|
1113
|
+
constructor(_aiConfig: LDAIJudgeConfig, _runner: Runner, _sampleRate?: number, logger?: LDLogger$1);
|
|
1114
|
+
/**
|
|
1115
|
+
* The default sampling rate baked in at construction. Used by `evaluate` /
|
|
1116
|
+
* `evaluateMessages` when no per-call rate is supplied.
|
|
1117
|
+
*/
|
|
1118
|
+
get sampleRate(): number;
|
|
1119
|
+
/**
|
|
1120
|
+
* Gets the evaluation metric key from the judge AI config.
|
|
1121
|
+
* Treats empty strings and whitespace-only strings as invalid.
|
|
1122
|
+
* @returns The evaluation metric key, or undefined if not available
|
|
1123
|
+
*/
|
|
1124
|
+
private _getEvaluationMetricKey;
|
|
1125
|
+
/**
|
|
1126
|
+
* Evaluates an AI response using the judge's configuration.
|
|
1127
|
+
*
|
|
1128
|
+
* @param input The input prompt or question that was provided to the AI
|
|
1129
|
+
* @param output The AI-generated response to be evaluated
|
|
1130
|
+
* @param samplingRate Sampling rate (0-1) to determine if evaluation should be processed.
|
|
1131
|
+
* When omitted, the Judge's constructor-default rate is used. An explicit `0` overrides
|
|
1132
|
+
* the default — only `undefined` falls through.
|
|
1133
|
+
* @returns Promise that resolves to evaluation results
|
|
1134
|
+
*/
|
|
1135
|
+
evaluate(input: string, output: string, samplingRate?: number): Promise<LDJudgeResult>;
|
|
1136
|
+
/**
|
|
1137
|
+
* Evaluates an AI response from chat messages and a runner result.
|
|
1138
|
+
*
|
|
1139
|
+
* Each message is rendered as `<role>: <content>` so the judge model can
|
|
1140
|
+
* distinguish speakers in the message history. Messages are joined with a
|
|
1141
|
+
* single newline.
|
|
1142
|
+
*
|
|
1143
|
+
* @param messages Array of messages representing the conversation history
|
|
1144
|
+
* @param response The runner result containing the AI-generated content to evaluate
|
|
1145
|
+
* @param samplingRatio Sampling ratio (0-1). When omitted, the Judge's
|
|
1146
|
+
* constructor-default rate is used.
|
|
1147
|
+
* @returns Promise that resolves to evaluation results
|
|
1148
|
+
*/
|
|
1149
|
+
evaluateMessages(messages: LDMessage[], response: RunnerResult, samplingRatio?: number): Promise<LDJudgeResult>;
|
|
1150
|
+
/**
|
|
1151
|
+
* Returns the AI Config used by this judge.
|
|
1152
|
+
*/
|
|
1153
|
+
getAIConfig(): LDAIJudgeConfig;
|
|
1154
|
+
/**
|
|
1155
|
+
* Returns the runner used by this judge.
|
|
1156
|
+
*/
|
|
1157
|
+
getRunner(): Runner;
|
|
1158
|
+
/**
|
|
1159
|
+
* Builds the evaluation input string passed to the runner.
|
|
1160
|
+
*
|
|
1161
|
+
* Combines the original prompt and the response into a single, well-known
|
|
1162
|
+
* format the judge model is expected to evaluate.
|
|
1163
|
+
*/
|
|
1164
|
+
private _buildEvaluationInput;
|
|
1165
|
+
/**
|
|
1166
|
+
* Parses the structured evaluation response. Expects top-level {score, reasoning}.
|
|
1167
|
+
* Returns score and reasoning, or undefined if parsing fails.
|
|
1168
|
+
*/
|
|
1169
|
+
private _parseEvaluationResponse;
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
/**
|
|
1173
|
+
* A registry of callable tools keyed by tool name.
|
|
1174
|
+
* Mirrors Python's `Dict[str, Callable]` — values are typically functions
|
|
1175
|
+
* that the provider invokes when the model requests a tool call.
|
|
1176
|
+
*/
|
|
1177
|
+
type ToolRegistry = Record<string, (...args: any[]) => unknown>;
|
|
1178
|
+
/**
|
|
1179
|
+
* Abstract base class for AI providers.
|
|
1180
|
+
*
|
|
1181
|
+
* An `AIProvider` is a per-provider factory: it is instantiated once per
|
|
1182
|
+
* provider package and is responsible for constructing focused runtime
|
|
1183
|
+
* capability objects via {@link createModel}, {@link createAgent}, and
|
|
1184
|
+
* {@link createAgentGraph}.
|
|
1185
|
+
*
|
|
1186
|
+
* Provider packages subclass `AIProvider` and override the methods they
|
|
1187
|
+
* support. The default implementations return `undefined`, mirroring Python's
|
|
1188
|
+
* base-class behaviour, so providers only need to implement the modes they
|
|
1189
|
+
* actually support.
|
|
1190
|
+
*/
|
|
1191
|
+
declare abstract class AIProvider {
|
|
1192
|
+
protected _logger?: LDLogger$1;
|
|
1193
|
+
constructor(logger?: LDLogger$1);
|
|
1194
|
+
/**
|
|
1195
|
+
* Create a Runner for a completion or judge AI Config.
|
|
1196
|
+
*
|
|
1197
|
+
* Override in provider subclasses to return a configured {@link Runner}.
|
|
1198
|
+
* Default implementation returns `undefined`.
|
|
1199
|
+
*
|
|
1200
|
+
* @param config The completion or judge AI configuration.
|
|
1201
|
+
* @param multiTurn Whether the runner should accumulate conversation history
|
|
1202
|
+
* across successive `run()` calls. Defaults to `true` (chat semantics).
|
|
1203
|
+
* Pass `false` for stateless runners such as judges where each call must
|
|
1204
|
+
* start from the initial config messages.
|
|
1205
|
+
* @returns Promise resolving to a {@link Runner}, or `undefined` if this
|
|
1206
|
+
* provider does not support model creation.
|
|
1207
|
+
*/
|
|
1208
|
+
createModel(_config: LDAICompletionConfig | LDAIJudgeConfig, _multiTurn?: boolean): Promise<Runner | undefined>;
|
|
1209
|
+
/**
|
|
1210
|
+
* Create a Runner for an agent AI Config.
|
|
1211
|
+
*
|
|
1212
|
+
* Override in provider subclasses to return a configured {@link Runner}.
|
|
1213
|
+
* Default implementation returns `undefined`.
|
|
1214
|
+
*
|
|
1215
|
+
* @param config The agent AI configuration.
|
|
1216
|
+
* @param tools Optional registry of callable tools.
|
|
1217
|
+
* @returns Promise resolving to a {@link Runner}, or `undefined` if this
|
|
1218
|
+
* provider does not support agent creation.
|
|
1219
|
+
*/
|
|
1220
|
+
createAgent(_config: LDAIAgentConfig, _tools?: ToolRegistry): Promise<Runner | undefined>;
|
|
1221
|
+
/**
|
|
1222
|
+
* Create an AgentGraphRunner for an agent graph definition.
|
|
1223
|
+
*
|
|
1224
|
+
* Override in provider subclasses to return a configured {@link AgentGraphRunner}.
|
|
1225
|
+
* Default implementation returns `undefined`.
|
|
1226
|
+
*
|
|
1227
|
+
* @param graphDef The agent graph definition.
|
|
1228
|
+
* @param tools Optional registry of callable tools.
|
|
1229
|
+
* @returns Promise resolving to an {@link AgentGraphRunner}, or `undefined` if
|
|
1230
|
+
* this provider does not support graph execution.
|
|
1231
|
+
*/
|
|
1232
|
+
createAgentGraph(_graphDef: AgentGraphDefinition, _tools?: ToolRegistry): Promise<AgentGraphRunner | undefined>;
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1106
1235
|
/**
|
|
1107
1236
|
* List of supported AI providers.
|
|
1108
1237
|
*/
|
|
@@ -1112,27 +1241,96 @@ declare const SUPPORTED_AI_PROVIDERS: readonly ["openai", "langchain", "vercel"]
|
|
|
1112
1241
|
*/
|
|
1113
1242
|
type SupportedAIProvider = (typeof SUPPORTED_AI_PROVIDERS)[number];
|
|
1114
1243
|
/**
|
|
1115
|
-
*
|
|
1244
|
+
* Sole entry point for runner creation.
|
|
1245
|
+
*
|
|
1246
|
+
* RunnerFactory is the single factory for creating {@link Runner} and
|
|
1247
|
+
* {@link AgentGraphRunner} instances. It mirrors the Python RunnerFactory
|
|
1248
|
+
* pattern: it knows about supported provider packages, loads them dynamically
|
|
1249
|
+
* via {@link _getProviderFactory}, and delegates creation to the factory
|
|
1250
|
+
* instance methods on {@link AIProvider}.
|
|
1251
|
+
*
|
|
1252
|
+
* Provider packages subclass {@link AIProvider} and override its factory
|
|
1253
|
+
* methods (`createModel`, `createAgent`, `createAgentGraph`).
|
|
1116
1254
|
*/
|
|
1117
|
-
declare class
|
|
1255
|
+
declare class RunnerFactory {
|
|
1118
1256
|
/**
|
|
1119
|
-
*
|
|
1120
|
-
*
|
|
1121
|
-
*
|
|
1257
|
+
* Load and return the AIProvider factory for the given provider type.
|
|
1258
|
+
*
|
|
1259
|
+
* This is the single place in the codebase that knows provider package names.
|
|
1260
|
+
* Each supported provider package exports a `*RunnerFactory` class that
|
|
1261
|
+
* extends {@link AIProvider}; this method instantiates it directly.
|
|
1122
1262
|
*
|
|
1123
|
-
* @param
|
|
1124
|
-
* @param logger Optional logger
|
|
1125
|
-
* @
|
|
1263
|
+
* @param providerType One of the {@link SUPPORTED_AI_PROVIDERS} values.
|
|
1264
|
+
* @param logger Optional logger forwarded to the provider factory.
|
|
1265
|
+
* @returns A configured {@link AIProvider} instance, or `undefined` if the
|
|
1266
|
+
* package cannot be loaded.
|
|
1126
1267
|
*/
|
|
1127
|
-
static
|
|
1268
|
+
private static _getProviderFactory;
|
|
1128
1269
|
/**
|
|
1129
1270
|
* Determine which providers to try based on defaultAiProvider and providerName.
|
|
1271
|
+
*
|
|
1272
|
+
* Mirrors Python's `_get_providers_to_try` helper.
|
|
1130
1273
|
*/
|
|
1131
1274
|
private static _getProvidersToTry;
|
|
1132
1275
|
/**
|
|
1133
|
-
* Try
|
|
1276
|
+
* Try each provider in order and return the first non-undefined result.
|
|
1277
|
+
*
|
|
1278
|
+
* Mirrors Python's `_with_fallback` helper. Loads each provider factory via
|
|
1279
|
+
* {@link _getProviderFactory} and calls `fn` with it. Returns the first
|
|
1280
|
+
* truthy result, or `undefined` if no provider succeeds.
|
|
1281
|
+
*
|
|
1282
|
+
* @param providers Ordered list of provider types to try.
|
|
1283
|
+
* @param fn Callback that calls the appropriate factory method on the provider.
|
|
1284
|
+
* @param logger Optional logger forwarded to each provider factory.
|
|
1285
|
+
*/
|
|
1286
|
+
private static _withFallback;
|
|
1287
|
+
/**
|
|
1288
|
+
* Create a Runner for the given AI configuration.
|
|
1289
|
+
*
|
|
1290
|
+
* Suitable for completion, judge, and agent config modes. Dynamically
|
|
1291
|
+
* loads the matching provider package via {@link _getProviderFactory} and
|
|
1292
|
+
* delegates to its {@link AIProvider.createModel} method.
|
|
1293
|
+
*
|
|
1294
|
+
* @param config The AI configuration (completion, agent, or judge).
|
|
1295
|
+
* @param logger Optional logger forwarded to the underlying provider.
|
|
1296
|
+
* @param defaultAiProvider Optional provider override
|
|
1297
|
+
* ('openai', 'langchain', 'vercel', …). When set, only that provider is
|
|
1298
|
+
* tried. When omitted, providers are tried in priority order based on the
|
|
1299
|
+
* provider name in the config.
|
|
1300
|
+
* @param multiTurn Whether the runner should accumulate conversation history
|
|
1301
|
+
* across successive `run()` calls. Defaults to `true` (chat semantics).
|
|
1302
|
+
* Judges pass `false` so each evaluation starts from the initial config
|
|
1303
|
+
* messages.
|
|
1304
|
+
* @returns A configured {@link Runner} ready to invoke the model, or
|
|
1305
|
+
* `undefined` if no suitable provider could be loaded.
|
|
1306
|
+
*/
|
|
1307
|
+
static createModel(config: LDAICompletionConfig | LDAIJudgeConfig, logger?: LDLogger$1, defaultAiProvider?: SupportedAIProvider, multiTurn?: boolean): Promise<Runner | undefined>;
|
|
1308
|
+
/**
|
|
1309
|
+
* Create a Runner for an agent AI Config.
|
|
1310
|
+
*
|
|
1311
|
+
* Delegates to the provider factory's {@link AIProvider.createAgent} method.
|
|
1312
|
+
*
|
|
1313
|
+
* @param config The agent AI configuration.
|
|
1314
|
+
* @param tools Optional registry of callable tools.
|
|
1315
|
+
* @param logger Optional logger forwarded to the underlying provider.
|
|
1316
|
+
* @param defaultAiProvider Optional provider override.
|
|
1317
|
+
* @returns A configured {@link Runner}, or `undefined` if no suitable
|
|
1318
|
+
* provider could be loaded.
|
|
1134
1319
|
*/
|
|
1135
|
-
|
|
1320
|
+
static createAgent(config: LDAIAgentConfig, tools?: ToolRegistry, logger?: LDLogger$1, defaultAiProvider?: SupportedAIProvider): Promise<Runner | undefined>;
|
|
1321
|
+
/**
|
|
1322
|
+
* Create an AgentGraphRunner for the given agent graph definition.
|
|
1323
|
+
*
|
|
1324
|
+
* Delegates to the provider factory's {@link AIProvider.createAgentGraph} method.
|
|
1325
|
+
*
|
|
1326
|
+
* @param graphDef The agent graph definition.
|
|
1327
|
+
* @param tools Optional registry of callable tools.
|
|
1328
|
+
* @param logger Optional logger forwarded to the underlying provider.
|
|
1329
|
+
* @param defaultAiProvider Optional provider override.
|
|
1330
|
+
* @returns A configured {@link AgentGraphRunner}, or `undefined` if no
|
|
1331
|
+
* suitable provider could be loaded.
|
|
1332
|
+
*/
|
|
1333
|
+
static createAgentGraph(graphDef: AgentGraphDefinition, tools?: ToolRegistry, logger?: LDLogger$1, defaultAiProvider?: SupportedAIProvider): Promise<AgentGraphRunner | undefined>;
|
|
1136
1334
|
}
|
|
1137
1335
|
|
|
1138
1336
|
/**
|
|
@@ -1153,9 +1351,11 @@ interface LDAIClient {
|
|
|
1153
1351
|
* the message content. The keys correspond to placeholders within the template, and the values
|
|
1154
1352
|
* are the corresponding replacements.
|
|
1155
1353
|
*
|
|
1156
|
-
* @returns
|
|
1157
|
-
*
|
|
1158
|
-
*
|
|
1354
|
+
* @returns An {@link LDAICompletionConfig} with `enabled`, `model`, `provider`,
|
|
1355
|
+
* `messages`, and a `createTracker()` factory. Call `createTracker()` on the
|
|
1356
|
+
* returned config to obtain a tracker for each AI run. If the configuration
|
|
1357
|
+
* cannot be accessed from LaunchDarkly, the return value will include
|
|
1358
|
+
* information from the `defaultValue`.
|
|
1159
1359
|
*
|
|
1160
1360
|
* @example
|
|
1161
1361
|
* ```
|
|
@@ -1168,35 +1368,15 @@ interface LDAIClient {
|
|
|
1168
1368
|
* provider: { name: 'openai' },
|
|
1169
1369
|
* };
|
|
1170
1370
|
*
|
|
1171
|
-
* const
|
|
1172
|
-
*
|
|
1173
|
-
*
|
|
1174
|
-
*
|
|
1175
|
-
*
|
|
1176
|
-
* modelId: "gpt-4o",
|
|
1177
|
-
* temperature: 0.2,
|
|
1178
|
-
* maxTokens: 4096,
|
|
1179
|
-
* userDefinedKey: "myValue",
|
|
1180
|
-
* },
|
|
1181
|
-
* messages: [
|
|
1182
|
-
* {
|
|
1183
|
-
* role: "system",
|
|
1184
|
-
* content: "You are an amazing GPT."
|
|
1185
|
-
* },
|
|
1186
|
-
* {
|
|
1187
|
-
* role: "user",
|
|
1188
|
-
* content: "Explain how you're an amazing GPT."
|
|
1189
|
-
* }
|
|
1190
|
-
* ],
|
|
1191
|
-
* tracker: ...
|
|
1371
|
+
* const completionConfig = await client.completionConfig(key, context, defaultValue, variables);
|
|
1372
|
+
* if (completionConfig.enabled) {
|
|
1373
|
+
* const tracker = completionConfig.createTracker();
|
|
1374
|
+
* // Use completionConfig.messages and completionConfig.model with your LLM,
|
|
1375
|
+
* // then record metrics with tracker.trackSuccess(), tracker.trackTokens(), etc.
|
|
1192
1376
|
* }
|
|
1193
1377
|
* ```
|
|
1194
1378
|
*/
|
|
1195
|
-
completionConfig(key: string, context: LDContext, defaultValue?: LDAICompletionConfigDefault, variables?: Record<string, unknown
|
|
1196
|
-
/**
|
|
1197
|
-
* @deprecated Use `completionConfig` instead. This method will be removed in a future version.
|
|
1198
|
-
*/
|
|
1199
|
-
config(key: string, context: LDContext, defaultValue?: LDAICompletionConfigDefault, variables?: Record<string, unknown>): Promise<LDAICompletionConfig>;
|
|
1379
|
+
completionConfig(key: string, context: LDContext, defaultValue?: LDAICompletionConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<LDAICompletionConfig>;
|
|
1200
1380
|
/**
|
|
1201
1381
|
* Retrieves and processes a single AI Config agent based on the provided key, LaunchDarkly context,
|
|
1202
1382
|
* and variables. This includes the model configuration and the customized instructions.
|
|
@@ -1211,9 +1391,11 @@ interface LDAIClient {
|
|
|
1211
1391
|
* the instructions. The keys correspond to placeholders within the template, and the values
|
|
1212
1392
|
* are the corresponding replacements.
|
|
1213
1393
|
*
|
|
1214
|
-
* @returns An
|
|
1215
|
-
*
|
|
1216
|
-
*
|
|
1394
|
+
* @returns An {@link LDAIAgentConfig} with customized `instructions`, `model`,
|
|
1395
|
+
* `provider`, and a `createTracker()` factory. Call `createTracker()` on the
|
|
1396
|
+
* returned config to obtain a tracker for each AI run. If the configuration
|
|
1397
|
+
* cannot be accessed from LaunchDarkly, the return value will include
|
|
1398
|
+
* information from the `defaultValue`.
|
|
1217
1399
|
*
|
|
1218
1400
|
* @example
|
|
1219
1401
|
* ```
|
|
@@ -1227,15 +1409,14 @@ interface LDAIClient {
|
|
|
1227
1409
|
* instructions: 'You are a research assistant.',
|
|
1228
1410
|
* }, variables);
|
|
1229
1411
|
*
|
|
1230
|
-
*
|
|
1231
|
-
* agentConfig.
|
|
1412
|
+
* if (agentConfig.enabled) {
|
|
1413
|
+
* const tracker = agentConfig.createTracker();
|
|
1414
|
+
* const researchResult = agentConfig.instructions; // Interpolated instructions
|
|
1415
|
+
* tracker.trackSuccess();
|
|
1416
|
+
* }
|
|
1232
1417
|
* ```
|
|
1233
1418
|
*/
|
|
1234
|
-
agentConfig(key: string, context: LDContext, defaultValue?: LDAIAgentConfigDefault, variables?: Record<string, unknown
|
|
1235
|
-
/**
|
|
1236
|
-
* @deprecated Use `agentConfig` instead. This method will be removed in a future version.
|
|
1237
|
-
*/
|
|
1238
|
-
agent(key: string, context: LDContext, defaultValue?: LDAIAgentConfigDefault, variables?: Record<string, unknown>): Promise<LDAIAgentConfig>;
|
|
1419
|
+
agentConfig(key: string, context: LDContext, defaultValue?: LDAIAgentConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<LDAIAgentConfig>;
|
|
1239
1420
|
/**
|
|
1240
1421
|
* Retrieves and processes a Judge AI Config based on the provided key, LaunchDarkly context,
|
|
1241
1422
|
* and variables. This includes the model configuration and the customized messages for evaluation.
|
|
@@ -1247,7 +1428,10 @@ interface LDAIClient {
|
|
|
1247
1428
|
* @param defaultValue Optional fallback when the configuration is not available from LaunchDarkly.
|
|
1248
1429
|
* When omitted or null, a disabled default is used.
|
|
1249
1430
|
* @param variables Optional variables for template interpolation in messages and instructions.
|
|
1250
|
-
* @returns A promise that resolves to
|
|
1431
|
+
* @returns A promise that resolves to an {@link LDAIJudgeConfig} with `enabled`,
|
|
1432
|
+
* `model`, `provider`, `messages`, `evaluationMetricKey`, and a `createTracker()`
|
|
1433
|
+
* factory. Call `createTracker()` on the returned config to obtain a tracker for
|
|
1434
|
+
* each AI run.
|
|
1251
1435
|
*
|
|
1252
1436
|
* @example
|
|
1253
1437
|
* ```typescript
|
|
@@ -1259,8 +1443,11 @@ interface LDAIClient {
|
|
|
1259
1443
|
* messages: [{ role: 'system', content: 'You are a relevance judge.' }]
|
|
1260
1444
|
* }, variables);
|
|
1261
1445
|
*
|
|
1262
|
-
*
|
|
1263
|
-
* judgeConf.
|
|
1446
|
+
* if (judgeConf.enabled) {
|
|
1447
|
+
* const tracker = judgeConf.createTracker();
|
|
1448
|
+
* // Use judgeConf.messages and judgeConf.model with your LLM,
|
|
1449
|
+
* // then record metrics with tracker.trackSuccess(), tracker.trackJudgeResult(), etc.
|
|
1450
|
+
* }
|
|
1264
1451
|
* ```
|
|
1265
1452
|
*/
|
|
1266
1453
|
judgeConfig(key: string, context: LDContext, defaultValue?: LDAIJudgeConfigDefault, variables?: Record<string, unknown>): Promise<LDAIJudgeConfig>;
|
|
@@ -1274,10 +1461,11 @@ interface LDAIClient {
|
|
|
1274
1461
|
* current environment, user, or session. This context may influence how the configuration is
|
|
1275
1462
|
* processed or personalized.
|
|
1276
1463
|
*
|
|
1277
|
-
* @returns A map of agent keys to their respective
|
|
1278
|
-
*
|
|
1279
|
-
*
|
|
1280
|
-
*
|
|
1464
|
+
* @returns A map of agent keys to their respective {@link LDAIAgentConfig}s,
|
|
1465
|
+
* each with customized `instructions` and a `createTracker()` factory. Call
|
|
1466
|
+
* `createTracker()` on a returned config to obtain a tracker for each AI run.
|
|
1467
|
+
* If a configuration cannot be accessed from LaunchDarkly, the return value
|
|
1468
|
+
* will include information from the respective `defaultValue`.
|
|
1281
1469
|
*
|
|
1282
1470
|
* @example
|
|
1283
1471
|
* ```
|
|
@@ -1306,20 +1494,18 @@ interface LDAIClient {
|
|
|
1306
1494
|
* const context = {...};
|
|
1307
1495
|
*
|
|
1308
1496
|
* const configs = await client.agentConfigs(agentConfigsList, context);
|
|
1309
|
-
*
|
|
1310
|
-
* configs["research_agent"].
|
|
1497
|
+
* if (configs["research_agent"].enabled) {
|
|
1498
|
+
* const tracker = configs["research_agent"].createTracker();
|
|
1499
|
+
* const researchResult = configs["research_agent"].instructions; // Interpolated instructions
|
|
1500
|
+
* tracker.trackSuccess();
|
|
1501
|
+
* }
|
|
1311
1502
|
* ```
|
|
1312
1503
|
*/
|
|
1313
1504
|
agentConfigs<const T extends readonly LDAIAgentRequestConfig[]>(agentConfigs: T, context: LDContext): Promise<Record<T[number]['key'], LDAIAgentConfig>>;
|
|
1314
1505
|
/**
|
|
1315
|
-
*
|
|
1316
|
-
*/
|
|
1317
|
-
agents<const T extends readonly LDAIAgentRequestConfig[]>(agentConfigs: T, context: LDContext): Promise<Record<T[number]['key'], LDAIAgentConfig>>;
|
|
1318
|
-
/**
|
|
1319
|
-
* Returns a TrackedChat instance for chat interactions.
|
|
1320
|
-
* This method serves as the primary entry point for creating TrackedChat instances from configuration.
|
|
1506
|
+
* Creates and returns a new ManagedModel instance for LLM model interactions.
|
|
1321
1507
|
*
|
|
1322
|
-
* @param key The key identifying the AI
|
|
1508
|
+
* @param key The key identifying the AI completion configuration to use.
|
|
1323
1509
|
* @param context The standard LDContext used when evaluating flags.
|
|
1324
1510
|
* @param defaultValue Optional fallback when the configuration is not available from LaunchDarkly.
|
|
1325
1511
|
* When omitted or null, a disabled default is used.
|
|
@@ -1327,7 +1513,7 @@ interface LDAIClient {
|
|
|
1327
1513
|
* The variables will also be used for judge evaluation. For the judge only, the variables
|
|
1328
1514
|
* `message_history` and `response_to_evaluate` are reserved and will be ignored.
|
|
1329
1515
|
* @param defaultAiProvider Optional default AI provider to use.
|
|
1330
|
-
* @returns A promise that resolves to the
|
|
1516
|
+
* @returns A promise that resolves to the ManagedModel instance, or undefined if the configuration is disabled.
|
|
1331
1517
|
*
|
|
1332
1518
|
* @example
|
|
1333
1519
|
* ```
|
|
@@ -1343,18 +1529,26 @@ interface LDAIClient {
|
|
|
1343
1529
|
* };
|
|
1344
1530
|
* const variables = { customerName: 'John' };
|
|
1345
1531
|
*
|
|
1346
|
-
* const
|
|
1347
|
-
* if (
|
|
1348
|
-
* const
|
|
1349
|
-
* console.log(
|
|
1532
|
+
* const model = await client.createModel(key, context, defaultValue, variables);
|
|
1533
|
+
* if (model) {
|
|
1534
|
+
* const result = await model.run("I need help with my order");
|
|
1535
|
+
* console.log(result.content);
|
|
1350
1536
|
* }
|
|
1351
1537
|
* ```
|
|
1352
1538
|
*/
|
|
1353
|
-
|
|
1539
|
+
createModel(key: string, context: LDContext, defaultValue?: LDAICompletionConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<ManagedModel | undefined>;
|
|
1354
1540
|
/**
|
|
1355
|
-
*
|
|
1541
|
+
* Creates and returns a new ManagedAgent instance for agent interactions.
|
|
1542
|
+
* Evaluations are wired automatically and exposed on ManagedResult.evaluations.
|
|
1543
|
+
*
|
|
1544
|
+
* @param key The key identifying the agent AI config to use.
|
|
1545
|
+
* @param context The standard LDContext used when evaluating flags.
|
|
1546
|
+
* @param defaultValue Optional fallback when the configuration is not available from LaunchDarkly.
|
|
1547
|
+
* @param variables Dictionary of values for instruction interpolation.
|
|
1548
|
+
* @param defaultAiProvider Optional default AI provider to use.
|
|
1549
|
+
* @returns A promise that resolves to the ManagedAgent instance, or undefined if disabled.
|
|
1356
1550
|
*/
|
|
1357
|
-
|
|
1551
|
+
createAgent(key: string, context: LDContext, defaultValue?: LDAIAgentConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<ManagedAgent | undefined>;
|
|
1358
1552
|
/**
|
|
1359
1553
|
* Creates and returns a new Judge instance for AI evaluation.
|
|
1360
1554
|
*
|
|
@@ -1365,6 +1559,8 @@ interface LDAIClient {
|
|
|
1365
1559
|
* @param variables Dictionary of values for instruction interpolation.
|
|
1366
1560
|
* The variables `message_history` and `response_to_evaluate` are reserved for the judge and will be ignored.
|
|
1367
1561
|
* @param defaultAiProvider Optional default AI provider to use.
|
|
1562
|
+
* @param sampleRate Optional default sampling rate (0-1) baked into the Judge.
|
|
1563
|
+
* Used by `Judge.evaluate()` when no per-call rate is supplied. Defaults to 1.0.
|
|
1368
1564
|
* @returns Promise that resolves to a Judge instance or undefined if disabled/unsupported
|
|
1369
1565
|
*
|
|
1370
1566
|
* @example
|
|
@@ -1388,11 +1584,11 @@ interface LDAIClient {
|
|
|
1388
1584
|
* }
|
|
1389
1585
|
* ```
|
|
1390
1586
|
*/
|
|
1391
|
-
createJudge(key: string, context: LDContext, defaultValue?: LDAIJudgeConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<Judge | undefined>;
|
|
1587
|
+
createJudge(key: string, context: LDContext, defaultValue?: LDAIJudgeConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider, sampleRate?: number): Promise<Judge | undefined>;
|
|
1392
1588
|
/**
|
|
1393
1589
|
* Reconstructs an AIConfigTracker from a resumption token string previously
|
|
1394
1590
|
* obtained from a tracker's `resumptionToken` property. Use this to associate
|
|
1395
|
-
* deferred events (such as user feedback) with the original
|
|
1591
|
+
* deferred events (such as user feedback) with the original tracker's runId.
|
|
1396
1592
|
*
|
|
1397
1593
|
* @param token A URL-safe Base64-encoded resumption token string.
|
|
1398
1594
|
* @param context The evaluation context to use for subsequent track calls.
|
|
@@ -1456,6 +1652,42 @@ interface LDClientMin {
|
|
|
1456
1652
|
readonly logger?: LDLogger$1;
|
|
1457
1653
|
}
|
|
1458
1654
|
|
|
1655
|
+
/**
|
|
1656
|
+
* ManagedAgentGraph wraps an AgentGraphDefinition and provides a managed run()
|
|
1657
|
+
* method that returns ManagedGraphResult with async judge evaluations.
|
|
1658
|
+
*
|
|
1659
|
+
* The runner function is responsible for executing the graph and returning
|
|
1660
|
+
* an AgentGraphRunnerResult. ManagedAgentGraph builds the managed result from
|
|
1661
|
+
* the runner result, including LDAIGraphMetricSummary with the graphTracker's
|
|
1662
|
+
* resumptionToken.
|
|
1663
|
+
*/
|
|
1664
|
+
declare class ManagedAgentGraph {
|
|
1665
|
+
private readonly _graphDefinition;
|
|
1666
|
+
private readonly _logger?;
|
|
1667
|
+
constructor(_graphDefinition: AgentGraphDefinition, _logger?: LDLogger$1 | undefined);
|
|
1668
|
+
/**
|
|
1669
|
+
* Runs the agent graph using the provided runner function and returns a ManagedGraphResult.
|
|
1670
|
+
*
|
|
1671
|
+
* The runner function receives the graph tracker and AgentGraphDefinition,
|
|
1672
|
+
* executes the graph, and returns an AgentGraphRunnerResult.
|
|
1673
|
+
*
|
|
1674
|
+
* run() returns before ManagedGraphResult.evaluations resolves.
|
|
1675
|
+
*
|
|
1676
|
+
* @param runner Async function that executes the graph and returns AgentGraphRunnerResult.
|
|
1677
|
+
* @returns ManagedGraphResult with LDAIGraphMetricSummary and evaluations promise.
|
|
1678
|
+
*/
|
|
1679
|
+
run(runner: (graphDefinition: AgentGraphDefinition, graphTracker: LDGraphTracker) => Promise<AgentGraphRunnerResult>): Promise<ManagedGraphResult>;
|
|
1680
|
+
/**
|
|
1681
|
+
* Converts per-node LDAIMetrics from the runner into LDAIMetricSummary by
|
|
1682
|
+
* creating a per-node tracker, firing tracking events, and calling getSummary().
|
|
1683
|
+
*/
|
|
1684
|
+
private _trackNodeMetrics;
|
|
1685
|
+
/**
|
|
1686
|
+
* Returns the underlying AgentGraphDefinition.
|
|
1687
|
+
*/
|
|
1688
|
+
getGraphDefinition(): AgentGraphDefinition;
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1459
1691
|
/**
|
|
1460
1692
|
* Concrete implementation of {@link LDGraphTracker}.
|
|
1461
1693
|
*
|
|
@@ -1483,8 +1715,13 @@ declare class LDGraphTrackerImpl implements LDGraphTracker {
|
|
|
1483
1715
|
* @param context LDContext for the new tracker.
|
|
1484
1716
|
*/
|
|
1485
1717
|
static fromResumptionToken(token: string, ldClient: LDClientMin, context: LDContext): LDGraphTrackerImpl;
|
|
1486
|
-
getTrackData():
|
|
1487
|
-
|
|
1718
|
+
getTrackData(): {
|
|
1719
|
+
runId: string;
|
|
1720
|
+
graphKey: string;
|
|
1721
|
+
variationKey?: string;
|
|
1722
|
+
version: number;
|
|
1723
|
+
};
|
|
1724
|
+
getSummary(): Partial<LDAIGraphMetricSummary>;
|
|
1488
1725
|
get resumptionToken(): string;
|
|
1489
1726
|
trackInvocationSuccess(): void;
|
|
1490
1727
|
trackInvocationFailure(): void;
|
|
@@ -1513,4 +1750,4 @@ declare class LDGraphTrackerImpl implements LDGraphTracker {
|
|
|
1513
1750
|
declare function initAi(ldClient: LDClientMin): LDAIClient;
|
|
1514
1751
|
type LDLogger = common.LDLogger;
|
|
1515
1752
|
|
|
1516
|
-
export { AIProvider,
|
|
1753
|
+
export { AIProvider, AgentGraphDefinition, AgentGraphNode, type AgentGraphRunner, type AgentGraphRunnerResult, Judge, type LDAIAgentConfig, type LDAIAgentConfigDefault, type LDAIAgentRequestConfig, type LDAIClient, type LDAICompletionConfig, type LDAICompletionConfigDefault, type LDAIConfig, type LDAIConfigDefault, type LDAIConfigDefaultKind, type LDAIConfigKind, type LDAIConfigMode, type LDAIConfigTracker, type LDAIGraphMetricSummary, type LDAIGraphMetrics, type LDAIJudgeConfig, type LDAIJudgeConfigDefault, type LDAIMetricSummary, type LDAIMetrics, type LDAgentGraphFlagValue, LDFeedbackKind, type LDGraphEdge, type LDGraphTrackData, type LDGraphTracker, LDGraphTrackerImpl, type LDJudge, type LDJudgeConfiguration, type LDJudgeResult, type LDLogger, type LDMessage, type LDModelConfig, type LDProviderConfig, type LDTokenUsage, type LDTool, ManagedAgent, ManagedAgentGraph, type ManagedGraphResult, ManagedModel, type ManagedResult, type Runner, RunnerFactory, type RunnerResult, SUPPORTED_AI_PROVIDERS, type SupportedAIProvider, type ToolRegistry, type TraversalFn, initAi };
|