|
1 | 1 | package com.launchdarkly.sdk.server.ai; |
2 | 2 |
|
| 3 | +import com.launchdarkly.sdk.server.ai.datamodel.LDAITrackingTypes.AIMetrics; |
| 4 | +import com.launchdarkly.sdk.server.ai.datamodel.LDAITrackingTypes.FeedbackKind; |
| 5 | +import com.launchdarkly.sdk.server.ai.datamodel.LDAITrackingTypes.JudgeResult; |
| 6 | +import com.launchdarkly.sdk.server.ai.datamodel.LDAITrackingTypes.MetricSummary; |
| 7 | +import com.launchdarkly.sdk.server.ai.datamodel.LDAITrackingTypes.TokenUsage; |
| 8 | +import com.launchdarkly.sdk.server.ai.datamodel.LDAITrackingTypes.TrackData; |
| 9 | + |
| 10 | +import java.time.Duration; |
| 11 | +import java.util.List; |
| 12 | +import java.util.concurrent.Callable; |
| 13 | +import java.util.function.Function; |
| 14 | + |
3 | 15 | /** |
4 | 16 | * Reports events related to a single AI run of an {@link AIConfig}. |
5 | 17 | * <p> |
6 | | - * A tracker is obtained from a retrieved config via {@link AIConfig#createTracker()}. Each tracker |
7 | | - * corresponds to one AI run and is used to record metrics such as model usage, duration, and |
8 | | - * feedback against the AI Config it was created from. |
| 18 | + * A tracker is obtained from a retrieved config via {@link AIConfig#createTracker()}, or |
| 19 | + * reconstructed from a resumption token via {@link LDAIClient#createTracker(String, com.launchdarkly.sdk.LDContext)}. |
| 20 | + * Each tracker corresponds to one AI run and is used to record metrics such as model usage, |
| 21 | + * duration, and feedback against the AI Config it was created from. |
| 22 | + * <p> |
| 23 | + * Most tracking methods are at-most-once: a second call to the same method on the same tracker |
| 24 | + * is silently dropped. {@link #trackToolCall(String)} and {@link #trackJudgeResult(JudgeResult)} |
| 25 | + * are multi-fire — each call records a distinct event. |
9 | 26 | * <p> |
10 | | - * <strong>This interface is an intentional placeholder.</strong> The metric- and feedback-reporting |
11 | | - * methods (and resumption-token support) are introduced in a later step of the AI SDK build-out; it |
12 | | - * is defined here so that the public config types expose a stable {@code createTracker()} surface. |
13 | | - * The only implementation in this release is an internal no-op. |
| 27 | + * Implementations are thread-safe. |
14 | 28 | */ |
15 | 29 | public interface LDAIConfigTracker { |
| 30 | + |
| 31 | + /** |
| 32 | + * Returns the correlation metadata for this tracker's run. |
| 33 | + * |
| 34 | + * @return the track data, never {@code null} |
| 35 | + */ |
| 36 | + TrackData getTrackData(); |
| 37 | + |
| 38 | + /** |
| 39 | + * Returns the resumption token for this run. |
| 40 | + * <p> |
| 41 | + * The resumption token encodes the run's identity and can be passed to |
| 42 | + * {@link LDAIClient#createTracker(String, com.launchdarkly.sdk.LDContext)} to reconstruct a |
| 43 | + * tracker on a subsequent request (for example, in a streaming scenario). |
| 44 | + * |
| 45 | + * @return the resumption token, or {@code null} if not available |
| 46 | + */ |
| 47 | + String getResumptionToken(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Records the duration of the AI generation. |
| 51 | + * <p> |
| 52 | + * At-most-once: subsequent calls on the same tracker are silently dropped. |
| 53 | + * |
| 54 | + * @param duration the duration; ignored if {@code null} |
| 55 | + */ |
| 56 | + void trackDuration(Duration duration); |
| 57 | + |
| 58 | + /** |
| 59 | + * Executes the given operation and records its wall-clock duration. |
| 60 | + * <p> |
| 61 | + * The duration is recorded even if the operation throws. Equivalent to wrapping the operation |
| 62 | + * in a try/finally that calls {@link #trackDuration(Duration)}. |
| 63 | + * |
| 64 | + * @param <T> the return type of the operation |
| 65 | + * @param operation the operation to execute and time; must not be {@code null} |
| 66 | + * @return the result of the operation |
| 67 | + * @throws Exception if the operation throws |
| 68 | + */ |
| 69 | + <T> T trackDurationOf(Callable<T> operation) throws Exception; |
| 70 | + |
| 71 | + /** |
| 72 | + * Records the time from request start to receipt of the first token. |
| 73 | + * <p> |
| 74 | + * At-most-once: subsequent calls on the same tracker are silently dropped. |
| 75 | + * |
| 76 | + * @param duration the time to first token; ignored if {@code null} |
| 77 | + */ |
| 78 | + void trackTimeToFirstToken(Duration duration); |
| 79 | + |
| 80 | + /** |
| 81 | + * Records that the AI generation succeeded. |
| 82 | + * <p> |
| 83 | + * At-most-once and mutually exclusive with {@link #trackError()}: whichever is called first wins. |
| 84 | + */ |
| 85 | + void trackSuccess(); |
| 86 | + |
| 87 | + /** |
| 88 | + * Records that the AI generation failed. |
| 89 | + * <p> |
| 90 | + * At-most-once and mutually exclusive with {@link #trackSuccess()}: whichever is called first wins. |
| 91 | + */ |
| 92 | + void trackError(); |
| 93 | + |
| 94 | + /** |
| 95 | + * Records user feedback for this AI generation. |
| 96 | + * <p> |
| 97 | + * At-most-once: subsequent calls on the same tracker are silently dropped. |
| 98 | + * |
| 99 | + * @param kind the feedback kind; ignored if {@code null} |
| 100 | + */ |
| 101 | + void trackFeedback(FeedbackKind kind); |
| 102 | + |
| 103 | + /** |
| 104 | + * Records token usage for this AI generation. |
| 105 | + * <p> |
| 106 | + * At-most-once: subsequent calls on the same tracker are silently dropped. Calls where all |
| 107 | + * counts are zero do not consume the at-most-once slot. |
| 108 | + * |
| 109 | + * @param tokens the token usage; ignored if {@code null} |
| 110 | + */ |
| 111 | + void trackTokens(TokenUsage tokens); |
| 112 | + |
| 113 | + /** |
| 114 | + * Records a single tool call made during this AI generation. |
| 115 | + * <p> |
| 116 | + * Multi-fire: every call emits an event. |
| 117 | + * |
| 118 | + * @param toolKey the tool key; ignored if {@code null} |
| 119 | + */ |
| 120 | + void trackToolCall(String toolKey); |
| 121 | + |
| 122 | + /** |
| 123 | + * Records multiple tool calls made during this AI generation. |
| 124 | + * <p> |
| 125 | + * Equivalent to calling {@link #trackToolCall(String)} for each key. |
| 126 | + * |
| 127 | + * @param toolKeys the tool keys; ignored if {@code null} |
| 128 | + */ |
| 129 | + void trackToolCalls(List<String> toolKeys); |
| 130 | + |
| 131 | + /** |
| 132 | + * Records the result of a judge evaluation. |
| 133 | + * <p> |
| 134 | + * Multi-fire per judge metric key. The result is silently skipped if it was not sampled, if |
| 135 | + * the evaluation did not succeed, or if the metric key or score is absent. |
| 136 | + * |
| 137 | + * @param result the judge result; ignored if {@code null} |
| 138 | + */ |
| 139 | + void trackJudgeResult(JudgeResult result); |
| 140 | + |
| 141 | + /** |
| 142 | + * Executes the given operation and tracks its metrics using the extracted {@link AIMetrics}. |
| 143 | + * <p> |
| 144 | + * Tracks duration (preferring runner-reported duration when present), success or error, tokens, |
| 145 | + * and tool calls. If the operation throws, {@link #trackError()} is called and the exception |
| 146 | + * is re-thrown. |
| 147 | + * |
| 148 | + * @param <T> the return type of the operation |
| 149 | + * @param metricsExtractor a function that extracts {@link AIMetrics} from the operation result; |
| 150 | + * exceptions from the extractor propagate to the caller |
| 151 | + * @param operation the AI operation to execute; must not be {@code null} |
| 152 | + * @return the result of the operation |
| 153 | + * @throws Exception if the operation or the metrics extractor throws |
| 154 | + */ |
| 155 | + <T> T trackMetricsOf( |
| 156 | + Function<? super T, AIMetrics> metricsExtractor, |
| 157 | + Callable<T> operation) throws Exception; |
| 158 | + |
| 159 | + /** |
| 160 | + * Returns a snapshot of all metrics tracked so far on this tracker. |
| 161 | + * |
| 162 | + * @return the metric summary, never {@code null} |
| 163 | + */ |
| 164 | + MetricSummary getSummary(); |
16 | 165 | } |
0 commit comments