Commit 29f31fc
authored
feat: Add template config methods to AI SDK (#184)
## Summary
Adds `completionConfigTemplate`, `agentConfigTemplate`, and
`judgeConfigTemplate` to `LDAIClient`. These methods return the same
config types as their non-template counterparts but skip Mustache
interpolation, preserving `{{variable}}` and `{{ldctx.key}}`
placeholders verbatim. Useful for displaying prompt previews, storing
templates for later rendering, or auditing prompt content without
variable substitution.
### Template methods on `LDAIClient`
```java
AICompletionConfig completionConfigTemplate(String key, LDContext context,
AICompletionConfigDefault defaultValue);
AIAgentConfig agentConfigTemplate(String key, LDContext context,
AIAgentConfigDefault defaultValue);
AIJudgeConfig judgeConfigTemplate(String key, LDContext context,
AIJudgeConfigDefault defaultValue);
```
Each template method fires a `-template` suffixed usage event
(`$ld:ai:usage:completion-config-template`,
`$ld:ai:usage:agent-config-template`,
`$ld:ai:usage:judge-config-template`) and does **not** fire the standard
usage event. The `variables` parameter is omitted since interpolation is
skipped.
### `LDAIClientImpl` changes
The private `evaluate`, `buildConfig`, and `buildConfigFromDefault`
methods accept a new `boolean interpolate` parameter. When `false`,
`interpolateMessages()` / `interpolate()` calls are skipped and
messages/instructions are passed through as-is from the parsed flag
value or caller-supplied default. All existing call sites pass `true` —
behavior is unchanged.
```java
// Standard path
public AICompletionConfig completionConfig(String key, LDContext context, ...) {
client.trackMetric(TRACK_USAGE_COMPLETION_CONFIG, context, LDValue.of(key), 1);
return (AICompletionConfig) evaluate(key, context, effectiveDefault, Mode.COMPLETION, variables, true);
}
// Template path
public AICompletionConfig completionConfigTemplate(String key, LDContext context, ...) {
client.trackMetric(TRACK_USAGE_COMPLETION_CONFIG_TEMPLATE, context, LDValue.of(key), 1);
return (AICompletionConfig) evaluate(key, context, effectiveDefault, Mode.COMPLETION, null, false);
}
```
### Migration
**None required.** `LDAIClient` gains three new members — this is
additive only. Existing consumers that call the SDK through the concrete
`LDAIClientImpl` class are unaffected. Consumers that implement
`LDAIClient` in test doubles will need to add stub implementations for
the three new methods.
## Test plan
- [ ] `./gradlew :lib:sdk:server-ai:test` passes
- [ ] `completionConfigTemplateFiresTemplateUsageEvent` — verifies the
correct `-template` tracking event is emitted
- [ ] `completionConfigTemplatePreservesPlaceholders` — `{{name}}`
survives in message content
- [ ] `completionConfigTemplateDoesNotInterpolateLdctx` —
`{{ldctx.key}}` is **not** substituted with the context key
- [ ] `completionConfigTemplateNullDefaultYieldsDisabled` — absent flag
with null default returns disabled config
- [ ] `completionConfigTemplateHasTracker` — `createTracker()` returns
non-null
- [ ] `agentConfigTemplate*` — same five scenarios for agent configs
(instructions rather than messages)
- [ ] `judgeConfigTemplate*` — same five scenarios for judge configs
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Low Risk**
> Additive API and a guarded code path; existing config retrieval still
interpolates and is covered by existing plus new tests.
>
> **Overview**
> Adds **`completionConfigTemplate`**, **`agentConfigTemplate`**, and
**`judgeConfigTemplate`** on `LDAIClient` so callers can evaluate flags
and get the same typed configs while leaving Mustache placeholders
(including `{{ldctx.*}}`) unchanged—intended for previews, auditing, and
storing templates for later rendering.
>
> `LDAIClientImpl` threads a new **`interpolate`** flag through
`evaluate` / `buildConfig` / `buildConfigFromDefault`; existing
`*Config` paths still pass **`true`**, while template methods pass
**`false`**, omit variables, and emit **`-template`** usage metrics
instead of the standard config usage events. Unit tests cover tracking,
placeholder preservation, disabled defaults, and trackers.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
82dcf60. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 74c697d commit 29f31fc
3 files changed
Lines changed: 264 additions & 19 deletions
File tree
- lib/sdk/server-ai/src
- main/java/com/launchdarkly/sdk/server/ai
- test/java/com/launchdarkly/sdk/server/ai
Lines changed: 46 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
86 | 132 | | |
87 | 133 | | |
88 | 134 | | |
| |||
Lines changed: 68 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
53 | 56 | | |
54 | 57 | | |
55 | 58 | | |
| |||
100 | 103 | | |
101 | 104 | | |
102 | 105 | | |
103 | | - | |
| 106 | + | |
104 | 107 | | |
105 | 108 | | |
106 | 109 | | |
| |||
148 | 151 | | |
149 | 152 | | |
150 | 153 | | |
151 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
152 | 188 | | |
153 | 189 | | |
154 | 190 | | |
| |||
161 | 197 | | |
162 | 198 | | |
163 | 199 | | |
164 | | - | |
| 200 | + | |
165 | 201 | | |
166 | 202 | | |
167 | 203 | | |
| |||
174 | 210 | | |
175 | 211 | | |
176 | 212 | | |
177 | | - | |
178 | | - | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
179 | 216 | | |
180 | 217 | | |
181 | 218 | | |
| |||
184 | 221 | | |
185 | 222 | | |
186 | 223 | | |
| 224 | + | |
187 | 225 | | |
188 | 226 | | |
189 | 227 | | |
190 | 228 | | |
191 | 229 | | |
192 | 230 | | |
193 | 231 | | |
194 | | - | |
| 232 | + | |
195 | 233 | | |
196 | 234 | | |
197 | 235 | | |
| |||
201 | 239 | | |
202 | 240 | | |
203 | 241 | | |
204 | | - | |
| 242 | + | |
205 | 243 | | |
206 | 244 | | |
207 | | - | |
| 245 | + | |
208 | 246 | | |
209 | 247 | | |
210 | 248 | | |
211 | 249 | | |
212 | 250 | | |
213 | 251 | | |
214 | 252 | | |
215 | | - | |
216 | | - | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
217 | 256 | | |
218 | 257 | | |
219 | 258 | | |
| |||
222 | 261 | | |
223 | 262 | | |
224 | 263 | | |
| 264 | + | |
225 | 265 | | |
226 | 266 | | |
227 | 267 | | |
| |||
233 | 273 | | |
234 | 274 | | |
235 | 275 | | |
236 | | - | |
| 276 | + | |
| 277 | + | |
237 | 278 | | |
238 | 279 | | |
239 | 280 | | |
| |||
244 | 285 | | |
245 | 286 | | |
246 | 287 | | |
247 | | - | |
| 288 | + | |
| 289 | + | |
248 | 290 | | |
249 | 291 | | |
250 | 292 | | |
| |||
254 | 296 | | |
255 | 297 | | |
256 | 298 | | |
257 | | - | |
| 299 | + | |
| 300 | + | |
258 | 301 | | |
259 | 302 | | |
260 | 303 | | |
| |||
264 | 307 | | |
265 | 308 | | |
266 | 309 | | |
267 | | - | |
| 310 | + | |
| 311 | + | |
268 | 312 | | |
269 | 313 | | |
270 | 314 | | |
271 | 315 | | |
272 | 316 | | |
273 | 317 | | |
274 | | - | |
275 | | - | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
276 | 321 | | |
277 | 322 | | |
278 | 323 | | |
| |||
281 | 326 | | |
282 | 327 | | |
283 | 328 | | |
| 329 | + | |
284 | 330 | | |
285 | 331 | | |
286 | 332 | | |
| |||
293 | 339 | | |
294 | 340 | | |
295 | 341 | | |
296 | | - | |
| 342 | + | |
| 343 | + | |
297 | 344 | | |
298 | 345 | | |
299 | 346 | | |
| |||
306 | 353 | | |
307 | 354 | | |
308 | 355 | | |
309 | | - | |
| 356 | + | |
| 357 | + | |
310 | 358 | | |
311 | 359 | | |
312 | 360 | | |
| |||
318 | 366 | | |
319 | 367 | | |
320 | 368 | | |
321 | | - | |
| 369 | + | |
| 370 | + | |
322 | 371 | | |
323 | 372 | | |
324 | 373 | | |
| |||
0 commit comments