Skip to content

chore: Convert JSON LDValue into the AiConfig Object #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: lc/SDK-1192/AI-config-feature-branch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
chore: Convert JSON LDValue into the AiConfig Object
  • Loading branch information
louis-launchdarkly committed May 8, 2025
commit 0a8ad3fa4798651187cce0439712f6c67d85da7f
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package com.launchdarkly.sdk.server.ai;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import com.launchdarkly.logging.LDLogger;
import com.launchdarkly.sdk.server.interfaces.LDClientInterface;
import com.launchdarkly.sdk.LDValue;
import com.launchdarkly.sdk.LDValueType;
import com.launchdarkly.sdk.server.ai.datamodel.AiConfig;
import com.launchdarkly.sdk.server.ai.datamodel.Message;
import com.launchdarkly.sdk.server.ai.datamodel.Meta;
import com.launchdarkly.sdk.server.ai.datamodel.Model;
import com.launchdarkly.sdk.server.ai.datamodel.Provider;
import com.launchdarkly.sdk.server.ai.datamodel.Role;
import com.launchdarkly.sdk.server.ai.interfaces.LDAiClientInterface;

/**
* The LaunchDarkly AI client. The client is capable of retrieving AI Configs from LaunchDarkly,
* and generating events specific to usage of the AI Config when interacting with model providers.
* The LaunchDarkly AI client. The client is capable of retrieving AI Configs
* from LaunchDarkly,
* and generating events specific to usage of the AI Config when interacting
* with model providers.
*/
public class LDAiClient implements LDAiClientInterface {
private LDClientInterface client;
Expand All @@ -15,14 +30,151 @@ public class LDAiClient implements LDAiClientInterface {
/**
* Creates a {@link LDAiClient}
*
* @param client LaunchDarkly Java Server SDK
* @param client LaunchDarkly Java Server SDK
*/
public LDAiClient(LDClientInterface client) {
if(client == null) {
//Error
if (client == null) {
// Error
} else {
this.client = client;
this.logger = client.getLogger();
}
}

/**
* Method to convert the JSON variable into the AiConfig object
*
* Currently, I am doing this in a mutable way, just to verify the logic convert
* LDValue into AiConfig.
* It is possible we need a builder to create immutable version of this for ease
* of use in a later PR.
*
* @param value
* @param key
*/
protected AiConfig parseAiConfig(LDValue value, String key) {
AiConfig result = new AiConfig();

try {
// Verify the whole value is a JSON object
if (value == null || value.getType() != LDValueType.OBJECT) {
throw new AiConfigParseException("Input to parseAiConfig must be a JSON object");
}

// Convert the _meta JSON object into Meta
LDValue valueMeta = value.get("_ldMeta");
if (valueMeta == LDValue.ofNull() || valueMeta.getType() != LDValueType.OBJECT) {
throw new AiConfigParseException("_ldMeta must be a JSON object");
}

Meta meta = new Meta();
// TODO: Do we expect customer calling this to build default value?
// If we do, then some of the values would be null
meta.setEnabled(ldValueNullCheck(valueMeta.get("enabled")).booleanValue());
meta.setVariationKey(ldValueNullCheck(valueMeta.get("variationKey")).stringValue());
Optional<Integer> version = Optional.of(valueMeta.get("version").intValue());
meta.setVersion(version);
result.setMeta(meta);

// Convert the optional messages from an JSON array of JSON objects into Message
// Q: Does it even make sense to have 0 messages?
LDValue valueMessages = value.get("messages");
if (valueMeta == LDValue.ofNull() || valueMessages.getType() != LDValueType.ARRAY) {
throw new AiConfigParseException("messages must be a JSON array");
}

List<Message> messages = new ArrayList<Message>();
for (LDValue valueMessage : valueMessages.values()) {
if (valueMessage == LDValue.ofNull() || valueMessage.getType() != LDValueType.OBJECT) {
throw new AiConfigParseException("individual message must be a JSON object");
}

Message message = new Message();
message.setContent(ldValueNullCheck(valueMessage.get("content")).stringValue());
// TODO: For absolute safety, we need to check this is one out of the three
// possible enum
message.setRole(Role.valueOf(valueMessage.get("role").stringValue().toUpperCase()));
messages.add(message);
}
result.setMessages(messages);

// Convert the optional model from an JSON object of with parameters and custom
// into Model
LDValue valueModel = value.get("model");
if (valueModel == LDValue.ofNull() || valueModel.getType() != LDValueType.OBJECT) {
throw new AiConfigParseException("model must be a JSON object");
}

Model model = new Model();
model.setName(ldValueNullCheck(valueModel.get("name")).stringValue());

LDValue valueParameters = valueModel.get("parameters");
if (valueParameters.getType() != LDValueType.NULL) {
if (valueParameters.getType() != LDValueType.OBJECT) {
throw new AiConfigParseException("non-null parameters must be a JSON object");
}

HashMap<String, LDValue> parameters = new HashMap<>();
for (String k : valueParameters.keys()) {
parameters.put(k, valueParameters.get(k));
}
model.setParameters(parameters);
} else {
// Parameters is optional - so we can just set null and proceed

// TODO: Mustash validation somewhere
model.setParameters(null);
}

LDValue valueCustom = valueModel.get("custom");
if (valueCustom.getType() != LDValueType.NULL) {
if (valueCustom.getType() != LDValueType.OBJECT) {
throw new AiConfigParseException("non-null custom must be a JSON object");
}

HashMap<String, LDValue> custom = new HashMap<>();
for (String k : valueCustom.keys()) {
custom.put(k, valueCustom.get(k));
}
model.setCustom(custom);
} else {
// Custom is optional - we can just set null and proceed
model.setCustom(null);
}
result.setModel(model);

// Convert the optional provider from an JSON object of with name into Provider
LDValue valueProvider = value.get("provider");
if (valueProvider.getType() != LDValueType.NULL) {
if (valueProvider.getType() != LDValueType.OBJECT) {
throw new AiConfigParseException("non-null provider must be a JSON object");
}

Provider provider = new Provider();
provider.setName(ldValueNullCheck(valueProvider.get("name")).stringValue());
result.setProvider(provider);
} else {
// Provider is optional - we can just set null and proceed
result.setProvider(null);
}
} catch (AiConfigParseException e) {
// logger.error(e.getMessage());
return null;
}

return result;
}

protected <T> T ldValueNullCheck(T ldValue) throws AiConfigParseException {
if (ldValue == LDValue.ofNull()) {
throw new AiConfigParseException("Unexpected Null value for non-optional field");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably helpful to add field name.

}
return ldValue;
}

class AiConfigParseException extends Exception {
AiConfigParseException(String exceptionMessage) {
super(exceptionMessage);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add nullable and nonnull annotations on fields to help with consumption.

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.launchdarkly.sdk.server.ai.datamodel;

import java.util.List;

public class AiConfig {
private List<Message> messages;

private Meta meta;

private Model model;

private Provider provider;

public List<Message> getMessages() {
return messages;
}

public void setMessages(List<Message> messages) {
this.messages = messages;
}

public Meta getMeta() {
return meta;
}

public void setMeta(Meta meta) {
this.meta = meta;
}

public Model getModel() {
return model;
}

public void setModel(Model model) {
this.model = model;
}

public Provider getProvider() {
return provider;
}

public void setProvider(Provider provider) {
this.provider = provider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.launchdarkly.sdk.server.ai.datamodel;

public class Message {
private String content;

private Role role;

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public Role getRole() {
return role;
}

public void setRole(Role role) {
this.role = role;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.launchdarkly.sdk.server.ai.datamodel;

import java.util.Optional;

public class Meta {
/**
* The variation key.
*/
private String variationKey;

/**
* The variation version.
*/
private Optional<Integer> version;

/**
* If the config is enabled.
*/
private boolean enabled;

// Getters and Setters

public String getVariationKey() {
return variationKey;
}

public void setVariationKey(String variationKey) {
this.variationKey = variationKey;
}

public Optional<Integer> getVersion() {
return version;
}

public void setVersion(Optional<Integer> version) {
this.version = version;
}

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.launchdarkly.sdk.server.ai.datamodel;

import java.util.HashMap;

import com.launchdarkly.sdk.LDValue;

public class Model {
private String name;

private HashMap<String, LDValue> parameters;

private HashMap<String, LDValue> custom;

// Getters and Setters

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public HashMap<String, LDValue> getParameters() {
return parameters;
}

public void setParameters(HashMap<String, LDValue> parameters) {
this.parameters = parameters;
}

public HashMap<String, LDValue> getCustom() {
return custom;
}

public void setCustom(HashMap<String, LDValue> custom) {
this.custom = custom;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.launchdarkly.sdk.server.ai.datamodel;

public class Provider {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Loading