11package com .launchdarkly .sdk .server .ai ;
22
3- import static java .util .Arrays .binarySearch ;
4-
53import java .util .ArrayList ;
64import java .util .HashMap ;
75import java .util .List ;
1614import com .launchdarkly .sdk .server .ai .datamodel .Meta ;
1715import com .launchdarkly .sdk .server .ai .datamodel .Model ;
1816import com .launchdarkly .sdk .server .ai .datamodel .Provider ;
19- import com .launchdarkly .sdk .server .ai .datamodel .Role ;
2017import com .launchdarkly .sdk .server .ai .interfaces .LDAiClientInterface ;
2118
2219/**
@@ -46,11 +43,13 @@ public LDAiClient(LDClientInterface client) {
4643 /**
4744 * Method to convert the JSON variable into the AiConfig object
4845 *
49- * If the parsing failed, the code will log an error and
46+ * If the parsing failed, the code will log an error and
5047 * return a well formed but with nullable value nulled and disabled AIConfig
5148 *
52- * Doing all the error checks, so if somehow LD backend return incorrect value types, there is logging
53- * This also opens up the possibility of allowing customer to build this using a JSON string in the future
49+ * Doing all the error checks, so if somehow LD backend return incorrect value
50+ * types, there is logging
51+ * This also opens up the possibility of allowing customer to build this using a
52+ * JSON string in the future
5453 *
5554 * @param value
5655 * @param key
@@ -59,53 +58,60 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
5958 boolean enabled = false ;
6059
6160 // Verify the whole value is a JSON object
62- if (!checkValueWithFailureLogging (value , LDValueType .OBJECT , logger , "Input to parseAiConfig must be a JSON object" )) {
61+ if (!checkValueWithFailureLogging (value , LDValueType .OBJECT , logger ,
62+ "Input to parseAiConfig must be a JSON object" )) {
6363 return new AiConfig (enabled , null , null , null , null );
6464 }
6565
6666 // Convert the _meta JSON object into Meta
6767 LDValue valueMeta = value .get ("_ldMeta" );
6868 if (!checkValueWithFailureLogging (valueMeta , LDValueType .OBJECT , logger , "_ldMeta must be a JSON object" )) {
69- // Q: If we can't read _meta, enabled by spec would be defaulted to false. Does it even matter the rest of the values?
69+ // Q: If we can't read _meta, enabled by spec would be defaulted to false. Does
70+ // it even matter the rest of the values?
7071 return new AiConfig (enabled , null , null , null , null );
7172 }
7273
73- // The booleanValue will get false if that value something that we are not expecting, which is good
74+ // The booleanValue will get false if that value something that we are not
75+ // expecting, which is good
7476 enabled = valueMeta .get ("enabled" ).booleanValue ();
7577
7678 String variationKey = null ;
77- if (checkValueWithFailureLogging (valueMeta .get ("variationKey" ), LDValueType .STRING , logger , "variationKey should be a string" )) {
79+ if (checkValueWithFailureLogging (valueMeta .get ("variationKey" ), LDValueType .STRING , logger ,
80+ "variationKey should be a string" )) {
7881 variationKey = valueMeta .get ("variationKey" ).stringValue ();
7982 }
8083 // Create Meta using constructor
8184 Meta meta = new Meta (
82- variationKey ,
83- Optional .of (valueMeta .get ("version" ).intValue ())
84- );
85+ variationKey ,
86+ Optional .of (valueMeta .get ("version" ).intValue ()));
8587
8688 // Convert the optional model from an JSON object of with parameters and custom
8789 // into Model
8890 Model model = null ;
8991
9092 LDValue valueModel = value .get ("model" );
91- if (checkValueWithFailureLogging (valueModel , LDValueType .OBJECT , logger , "model if exists must be a JSON object" )) {
92- if (checkValueWithFailureLogging (valueModel .get ("name" ), LDValueType .STRING , logger , "model name must be a string and is required" )) {
93+ if (checkValueWithFailureLogging (valueModel , LDValueType .OBJECT , logger ,
94+ "model if exists must be a JSON object" )) {
95+ if (checkValueWithFailureLogging (valueModel .get ("name" ), LDValueType .STRING , logger ,
96+ "model name must be a string and is required" )) {
9397 String modelName = valueModel .get ("name" ).stringValue ();
9498
9599 // Prepare parameters and custom maps for Model
96100 HashMap <String , LDValue > parameters = null ;
97101 HashMap <String , LDValue > custom = null ;
98102
99103 LDValue valueParameters = valueModel .get ("parameters" );
100- if (checkValueWithFailureLogging (valueParameters , LDValueType .OBJECT , logger , "non-null parameters must be a JSON object" )) {
104+ if (checkValueWithFailureLogging (valueParameters , LDValueType .OBJECT , logger ,
105+ "non-null parameters must be a JSON object" )) {
101106 parameters = new HashMap <>();
102107 for (String k : valueParameters .keys ()) {
103108 parameters .put (k , valueParameters .get (k ));
104109 }
105110 }
106-
111+
107112 LDValue valueCustom = valueModel .get ("custom" );
108- if (checkValueWithFailureLogging (valueCustom , LDValueType .OBJECT , logger , "non-null custom must be a JSON object" )) {
113+ if (checkValueWithFailureLogging (valueCustom , LDValueType .OBJECT , logger ,
114+ "non-null custom must be a JSON object" )) {
109115
110116 custom = new HashMap <>();
111117 for (String k : valueCustom .keys ()) {
@@ -122,7 +128,8 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
122128 List <Message > messages = null ;
123129
124130 LDValue valueMessages = value .get ("messages" );
125- if (checkValueWithFailureLogging (valueMessages , LDValueType .ARRAY , logger , "messages if exists must be a JSON array" )) {
131+ if (checkValueWithFailureLogging (valueMessages , LDValueType .ARRAY , logger ,
132+ "messages if exists must be a JSON array" )) {
126133 messages = new ArrayList <Message >();
127134 valueMessages .valuesAs (new Message .MessageConverter ());
128135 for (Message message : valueMessages .valuesAs (new Message .MessageConverter ())) {
@@ -134,8 +141,10 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
134141 LDValue valueProvider = value .get ("provider" );
135142 String providerName = null ;
136143
137- if (checkValueWithFailureLogging (valueProvider , LDValueType .OBJECT , logger , "non-null provider must be a JSON object" )) {
138- if (checkValueWithFailureLogging (valueProvider .get ("name" ), LDValueType .STRING , logger , "provider name must be a String" )) {
144+ if (checkValueWithFailureLogging (valueProvider , LDValueType .OBJECT , logger ,
145+ "non-null provider must be a JSON object" )) {
146+ if (checkValueWithFailureLogging (valueProvider .get ("name" ), LDValueType .STRING , logger ,
147+ "provider name must be a String" )) {
139148 providerName = valueProvider .get ("name" ).stringValue ();
140149 }
141150 }
@@ -145,7 +154,8 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
145154 return new AiConfig (enabled , meta , model , messages , provider );
146155 }
147156
148- protected boolean checkValueWithFailureLogging (LDValue ldValue , LDValueType expectedType , LDLogger logger , String message ) {
157+ protected boolean checkValueWithFailureLogging (LDValue ldValue , LDValueType expectedType , LDLogger logger ,
158+ String message ) {
149159 if (ldValue .getType () != expectedType ) {
150160 if (logger != null ) {
151161 logger .error (message );
0 commit comments