Skip to content

Commit f208b34

Browse files
committed
Fix agent type update
Signed-off-by: Owais Kazi <[email protected]>
1 parent 66bcde7 commit f208b34

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

common/src/main/java/org/opensearch/ml/common/transport/agent/MLAgentUpdateInput.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ public class MLAgentUpdateInput implements ToXContentObject, Writeable {
5050
public static final String MEMORY_TYPE_FIELD = "type";
5151
public static final String MEMORY_SESSION_ID_FIELD = "session_id";
5252
public static final String MEMORY_WINDOW_SIZE_FIELD = "window_size";
53+
public static final String TYPE_FIELD = "type";
5354
public static final String APP_TYPE_FIELD = "app_type";
5455
public static final String LAST_UPDATED_TIME_FIELD = "last_updated_time";
5556

5657
@Getter
5758
private String agentId;
5859
private String name;
60+
private String type;
5961
private String description;
6062
private String llmModelId;
6163
private Map<String, String> llmParameters;
@@ -72,6 +74,7 @@ public class MLAgentUpdateInput implements ToXContentObject, Writeable {
7274
public MLAgentUpdateInput(
7375
String agentId,
7476
String name,
77+
String type,
7578
String description,
7679
String llmModelId,
7780
Map<String, String> llmParameters,
@@ -86,6 +89,7 @@ public MLAgentUpdateInput(
8689
) {
8790
this.agentId = agentId;
8891
this.name = name;
92+
this.type = type;
8993
this.description = description;
9094
this.llmModelId = llmModelId;
9195
this.llmParameters = llmParameters;
@@ -104,6 +108,7 @@ public MLAgentUpdateInput(StreamInput in) throws IOException {
104108
Version streamInputVersion = in.getVersion();
105109
agentId = in.readString();
106110
name = in.readOptionalString();
111+
type = in.readOptionalString();
107112
description = in.readOptionalString();
108113
llmModelId = in.readOptionalString();
109114
if (in.readBoolean()) {
@@ -134,6 +139,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
134139
if (name != null) {
135140
builder.field(AGENT_NAME_FIELD, name);
136141
}
142+
if (type != null) {
143+
builder.field(TYPE_FIELD, type);
144+
}
137145
if (description != null) {
138146
builder.field(DESCRIPTION_FIELD, description);
139147
}
@@ -184,6 +192,7 @@ public void writeTo(StreamOutput out) throws IOException {
184192
Version streamOutputVersion = out.getVersion();
185193
out.writeString(agentId);
186194
out.writeOptionalString(name);
195+
out.writeOptionalString(type);
187196
out.writeOptionalString(description);
188197
out.writeOptionalString(llmModelId);
189198
if (llmParameters != null && !llmParameters.isEmpty()) {
@@ -220,6 +229,7 @@ public void writeTo(StreamOutput out) throws IOException {
220229
public static MLAgentUpdateInput parse(XContentParser parser) throws IOException {
221230
String agentId = null;
222231
String name = null;
232+
String type = null;
223233
String description = null;
224234
String llmModelId = null;
225235
Map<String, String> llmParameters = null;
@@ -243,6 +253,9 @@ public static MLAgentUpdateInput parse(XContentParser parser) throws IOException
243253
case AGENT_NAME_FIELD:
244254
name = parser.text();
245255
break;
256+
case TYPE_FIELD:
257+
type = parser.text();
258+
break;
246259
case DESCRIPTION_FIELD:
247260
description = parser.text();
248261
break;
@@ -313,6 +326,7 @@ public static MLAgentUpdateInput parse(XContentParser parser) throws IOException
313326
return new MLAgentUpdateInput(
314327
agentId,
315328
name,
329+
type,
316330
description,
317331
llmModelId,
318332
llmParameters,
@@ -328,6 +342,9 @@ public static MLAgentUpdateInput parse(XContentParser parser) throws IOException
328342
}
329343

330344
public MLAgent toMLAgent(MLAgent originalAgent) {
345+
if (type != null && !type.equals(originalAgent.getType())) {
346+
throw new IllegalArgumentException("Agent type cannot be updated");
347+
}
331348
LLMSpec finalLlm;
332349
if (llmModelId == null && (llmParameters == null || llmParameters.isEmpty())) {
333350
finalLlm = originalAgent.getLlm();

common/src/test/java/org/opensearch/ml/common/transport/agent/MLAgentUpdateInputTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ public void testParseWithAllFields() throws Exception {
392392
{
393393
"agent_id": "test-agent-id",
394394
"name": "test-agent",
395+
"type": "flow",
395396
"description": "test description",
396397
"llm": {
397398
"model_id": "test-model-id",
@@ -423,6 +424,7 @@ public void testParseWithAllFields() throws Exception {
423424
""";
424425
testParseFromJsonString(inputStr, parsedInput -> {
425426
assertEquals("test-agent", parsedInput.getName());
427+
assertEquals("flow", parsedInput.getType());
426428
assertEquals("test description", parsedInput.getDescription());
427429
assertEquals("test-model-id", parsedInput.getLlmModelId());
428430
assertEquals(1, parsedInput.getTools().size());
@@ -959,6 +961,41 @@ public void testCombinedLLMAndMemoryPartialUpdates() {
959961
assertEquals(Integer.valueOf(10), updatedAgent.getMemory().getWindowSize()); // Updated
960962
}
961963

964+
@Test
965+
public void testAgentTypeValidation() {
966+
MLAgent originalAgent = MLAgent.builder().type(MLAgentType.FLOW.name()).name("Test Agent").build();
967+
968+
// Same type should be allowed
969+
MLAgentUpdateInput sameTypeInput = MLAgentUpdateInput
970+
.builder()
971+
.agentId("test-agent-id")
972+
.type(MLAgentType.FLOW.name())
973+
.name("Updated Name")
974+
.build();
975+
976+
MLAgent updatedAgent = sameTypeInput.toMLAgent(originalAgent);
977+
assertEquals(MLAgentType.FLOW.name(), updatedAgent.getType());
978+
assertEquals("Updated Name", updatedAgent.getName());
979+
980+
// Different type should throw error
981+
MLAgentUpdateInput differentTypeInput = MLAgentUpdateInput
982+
.builder()
983+
.agentId("test-agent-id")
984+
.type(MLAgentType.CONVERSATIONAL.name())
985+
.name("Updated Name")
986+
.build();
987+
988+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { differentTypeInput.toMLAgent(originalAgent); });
989+
assertEquals("Agent type cannot be updated", e.getMessage());
990+
991+
// No type provided should work (original type)
992+
MLAgentUpdateInput noTypeInput = MLAgentUpdateInput.builder().agentId("test-agent-id").name("Updated Name").build();
993+
994+
MLAgent originalAgentType = noTypeInput.toMLAgent(originalAgent);
995+
assertEquals(MLAgentType.FLOW.name(), originalAgentType.getType());
996+
assertEquals("Updated Name", originalAgentType.getName());
997+
}
998+
962999
@Test
9631000
public void testStreamInputOutputWithVersion() throws IOException {
9641001
MLAgentUpdateInput input = MLAgentUpdateInput

0 commit comments

Comments
 (0)