Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.launchdarkly.sdk.server.ai.internal;

import com.launchdarkly.sdk.LDValue;
import com.launchdarkly.sdk.LDValueConverter;
import com.launchdarkly.sdk.LDValueType;
import com.launchdarkly.sdk.server.ai.datamodel.LDAIConfigTypes.JudgeConfiguration;
import com.launchdarkly.sdk.server.ai.datamodel.LDAIConfigTypes.Message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.launchdarkly.sdk.server.ai.internal;

import com.launchdarkly.sdk.LDContext;
import com.launchdarkly.sdk.LDContextEncoder;
import com.launchdarkly.sdk.server.ai.internal.mustache.Mustache;
import com.launchdarkly.sdk.server.ai.internal.mustache.Template;

Expand Down Expand Up @@ -61,7 +62,7 @@ public String interpolate(String template, Map<String, Object> variables, LDCont
merged.putAll(variables);
}
// ldctx is added last so it always wins over any caller-supplied "ldctx" entry.
merged.put("ldctx", contextToMap(context));
merged.put("ldctx", LDContextEncoder.encode(context));
return render(template, merged);
}

Expand All @@ -83,51 +84,4 @@ private String render(String template, Map<String, Object> variables) {
Template compiled = templateCache.computeIfAbsent(template, compiler::compile);
return compiled.execute(variables);
}

/**
* Encodes the evaluation context directly into the nested map structure exposed to templates as
* {@code ldctx}, without round-tripping through JSON serialization. A single-kind context becomes
* a map of its attributes; a multi-kind context becomes
* {@code {"kind":"multi", "key":<fully-qualified key>, <kind>: {...}}} with one nested map per
* individual context.
*/
private static Map<String, Object> contextToMap(LDContext context) {
if (context == null || !context.isValid()) {
return new HashMap<>();
}
if (context.isMultiple()) {
Map<String, Object> map = new HashMap<>();
map.put("kind", "multi");
map.put("key", context.getFullyQualifiedKey());
int count = context.getIndividualContextCount();
for (int i = 0; i < count; i++) {
LDContext individual = context.getIndividualContext(i);
if (individual != null) {
// Mirror LaunchDarkly's standard context JSON: the per-kind objects nested under a
// multi-kind context omit "kind" because it is already implied by the property key.
map.put(individual.getKind().toString(), singleContextToMap(individual, false));
}
}
return map;
}
return singleContextToMap(context, true);
}

private static Map<String, Object> singleContextToMap(LDContext context, boolean includeKind) {
Map<String, Object> map = new HashMap<>();
if (includeKind) {
map.put("kind", context.getKind().toString());
}
map.put("key", context.getKey());
if (context.getName() != null) {
map.put("name", context.getName());
}
map.put("anonymous", context.isAnonymous());
// Custom attribute values can be arbitrary JSON; convert each LDValue to a plain Java value
// (depth-capped) so nested objects/arrays remain addressable from templates.
for (String attribute : context.getCustomAttributeNames()) {
map.put(attribute, LDValueConverter.toJavaObject(context.getValue(attribute)));
}
return map;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.launchdarkly.sdk;

import java.util.HashMap;
import java.util.Map;

/**
* Encodes an {@link LDContext} into a plain nested {@code Map<String, Object>} structure without
* round-tripping through JSON serialization. Leaf attribute values are converted by
* {@link LDValueConverter}.
* <p>
* Output shape:
* <ul>
* <li>A {@code null} or invalid context produces an empty map.</li>
* <li>A single-kind context produces a map containing {@code kind}, {@code key},
* {@code name} (only when non-null), {@code anonymous} (always present), and one entry for
* each custom attribute.</li>
* <li>A multi-kind context produces
* {@code {"kind":"multi", "key":<fullyQualifiedKey>, <kindName>:{...}, ...}} where each
* per-kind nested map omits {@code kind} (it is implied by the property key).</li>
* </ul>
*/
public final class LDContextEncoder {

private LDContextEncoder() {
}

/**
* Encodes an {@link LDContext} into a plain nested {@code Map<String, Object>}.
*
* @param context the context to encode; may be {@code null}
* @return the encoded map; never {@code null} (an empty map is returned for invalid or null input)
*/
public static Map<String, Object> encode(LDContext context) {
if (context == null || !context.isValid()) {
return new HashMap<>();
}
if (context.isMultiple()) {
Map<String, Object> map = new HashMap<>();
map.put("kind", "multi");
int count = context.getIndividualContextCount();
for (int i = 0; i < count; i++) {
LDContext individual = context.getIndividualContext(i);
if (individual != null) {
// Mirror LaunchDarkly's standard context JSON: per-kind objects nested under a
// multi-kind context omit "kind" because it is already implied by the property key.
map.put(individual.getKind().toString(), encodeSingle(individual, false));
}
}
// Written after the loop so it always wins, even if a member kind is named "key".
map.put("key", context.getFullyQualifiedKey());
return map;
}
return encodeSingle(context, true);
}

private static Map<String, Object> encodeSingle(LDContext context, boolean includeKind) {
Map<String, Object> map = new HashMap<>();
if (includeKind) {
map.put("kind", context.getKind().toString());
}
map.put("key", context.getKey());
if (context.getName() != null) {
map.put("name", context.getName());
}
map.put("anonymous", context.isAnonymous());
// Custom attribute values can be arbitrary JSON; convert each LDValue to a plain Java value
// (depth-capped) so nested objects/arrays are fully traversable.
for (String attribute : context.getCustomAttributeNames()) {
map.put(attribute, LDValueConverter.toJavaObject(context.getValue(attribute)));
}
return map;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
package com.launchdarkly.sdk.server.ai.internal;

import com.launchdarkly.sdk.LDValue;
import com.launchdarkly.sdk.LDValueType;
package com.launchdarkly.sdk;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -14,17 +11,15 @@
* ({@link String}, {@link Long}, {@link Double}, {@link Boolean}, {@link List}, {@link Map}, or
* {@code null}).
* <p>
* This is used to expose AI Config {@code model.parameters} / {@code model.custom} and tool
* parameters to callers without leaking the {@link LDValue} type onto the public surface.
* <p>
* Conversion is defensive: it never throws on malformed or pathological input. Numbers are decoded
* to {@link Long} when they are mathematically integral and within the IEEE-754 exact-integer range
* ({@code |value| <= 2^53}); otherwise they are decoded to {@link Double}. Whole numbers outside
* {@code ±2^53} cannot be represented exactly and are returned as the nearest {@link Double}.
* Conversion depth is capped (see {@link #MAX_DEPTH}); values nested more deeply than the cap are
* dropped (rendered as {@code null}) to bound stack usage on adversarial input.
* <p>
* This class is an internal implementation detail and is not part of the supported API.
* Object fields are stored in a {@link LinkedHashMap} to preserve insertion order, and all
* returned collections are unmodifiable.
*/
public final class LDValueConverter {
/**
Expand Down
Loading
Loading