1
1
package com .launchdarkly .sdk .server .ai ;
2
2
3
- import static java .util .Arrays .binarySearch ;
4
-
5
3
import java .util .ArrayList ;
6
4
import java .util .HashMap ;
7
5
import java .util .List ;
16
14
import com .launchdarkly .sdk .server .ai .datamodel .Meta ;
17
15
import com .launchdarkly .sdk .server .ai .datamodel .Model ;
18
16
import com .launchdarkly .sdk .server .ai .datamodel .Provider ;
19
- import com .launchdarkly .sdk .server .ai .datamodel .Role ;
20
17
import com .launchdarkly .sdk .server .ai .interfaces .LDAiClientInterface ;
21
18
22
19
/**
@@ -46,11 +43,13 @@ public LDAiClient(LDClientInterface client) {
46
43
/**
47
44
* Method to convert the JSON variable into the AiConfig object
48
45
*
49
- * If the parsing failed, the code will log an error and
46
+ * If the parsing failed, the code will log an error and
50
47
* return a well formed but with nullable value nulled and disabled AIConfig
51
48
*
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
54
53
*
55
54
* @param value
56
55
* @param key
@@ -59,53 +58,60 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
59
58
boolean enabled = false ;
60
59
61
60
// 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" )) {
63
63
return new AiConfig (enabled , null , null , null , null );
64
64
}
65
65
66
66
// Convert the _meta JSON object into Meta
67
67
LDValue valueMeta = value .get ("_ldMeta" );
68
68
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?
70
71
return new AiConfig (enabled , null , null , null , null );
71
72
}
72
73
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
74
76
enabled = valueMeta .get ("enabled" ).booleanValue ();
75
77
76
78
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" )) {
78
81
variationKey = valueMeta .get ("variationKey" ).stringValue ();
79
82
}
80
83
// Create Meta using constructor
81
84
Meta meta = new Meta (
82
- variationKey ,
83
- Optional .of (valueMeta .get ("version" ).intValue ())
84
- );
85
+ variationKey ,
86
+ Optional .of (valueMeta .get ("version" ).intValue ()));
85
87
86
88
// Convert the optional model from an JSON object of with parameters and custom
87
89
// into Model
88
90
Model model = null ;
89
91
90
92
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" )) {
93
97
String modelName = valueModel .get ("name" ).stringValue ();
94
98
95
99
// Prepare parameters and custom maps for Model
96
100
HashMap <String , LDValue > parameters = null ;
97
101
HashMap <String , LDValue > custom = null ;
98
102
99
103
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" )) {
101
106
parameters = new HashMap <>();
102
107
for (String k : valueParameters .keys ()) {
103
108
parameters .put (k , valueParameters .get (k ));
104
109
}
105
110
}
106
-
111
+
107
112
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" )) {
109
115
110
116
custom = new HashMap <>();
111
117
for (String k : valueCustom .keys ()) {
@@ -122,7 +128,8 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
122
128
List <Message > messages = null ;
123
129
124
130
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" )) {
126
133
messages = new ArrayList <Message >();
127
134
valueMessages .valuesAs (new Message .MessageConverter ());
128
135
for (Message message : valueMessages .valuesAs (new Message .MessageConverter ())) {
@@ -134,8 +141,10 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
134
141
LDValue valueProvider = value .get ("provider" );
135
142
String providerName = null ;
136
143
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" )) {
139
148
providerName = valueProvider .get ("name" ).stringValue ();
140
149
}
141
150
}
@@ -145,7 +154,8 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
145
154
return new AiConfig (enabled , meta , model , messages , provider );
146
155
}
147
156
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 ) {
149
159
if (ldValue .getType () != expectedType ) {
150
160
if (logger != null ) {
151
161
logger .error (message );
0 commit comments