Skip to content

Commit f96592e

Browse files
committed
Fix agent type update
Signed-off-by: Owais Kazi <[email protected]>
1 parent 3fca6bd commit f96592e

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
@@ -51,12 +51,14 @@ public class MLAgentUpdateInput implements ToXContentObject, Writeable {
5151
public static final String MEMORY_TYPE_FIELD = "type";
5252
public static final String MEMORY_SESSION_ID_FIELD = "session_id";
5353
public static final String MEMORY_WINDOW_SIZE_FIELD = "window_size";
54+
public static final String TYPE_FIELD = "type";
5455
public static final String APP_TYPE_FIELD = "app_type";
5556
public static final String LAST_UPDATED_TIME_FIELD = "last_updated_time";
5657

5758
@Getter
5859
private String agentId;
5960
private String name;
61+
private String type;
6062
private String description;
6163
private String llmModelId;
6264
private Map<String, String> llmParameters;
@@ -73,6 +75,7 @@ public class MLAgentUpdateInput implements ToXContentObject, Writeable {
7375
public MLAgentUpdateInput(
7476
String agentId,
7577
String name,
78+
String type,
7679
String description,
7780
String llmModelId,
7881
Map<String, String> llmParameters,
@@ -87,6 +90,7 @@ public MLAgentUpdateInput(
8790
) {
8891
this.agentId = agentId;
8992
this.name = name;
93+
this.type = type;
9094
this.description = description;
9195
this.llmModelId = llmModelId;
9296
this.llmParameters = llmParameters;
@@ -105,6 +109,7 @@ public MLAgentUpdateInput(StreamInput in) throws IOException {
105109
Version streamInputVersion = in.getVersion();
106110
agentId = in.readString();
107111
name = in.readOptionalString();
112+
type = in.readOptionalString();
108113
description = in.readOptionalString();
109114
llmModelId = in.readOptionalString();
110115
if (in.readBoolean()) {
@@ -135,6 +140,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
135140
if (name != null) {
136141
builder.field(AGENT_NAME_FIELD, name);
137142
}
143+
if (type != null) {
144+
builder.field(TYPE_FIELD, type);
145+
}
138146
if (description != null) {
139147
builder.field(DESCRIPTION_FIELD, description);
140148
}
@@ -185,6 +193,7 @@ public void writeTo(StreamOutput out) throws IOException {
185193
Version streamOutputVersion = out.getVersion();
186194
out.writeString(agentId);
187195
out.writeOptionalString(name);
196+
out.writeOptionalString(type);
188197
out.writeOptionalString(description);
189198
out.writeOptionalString(llmModelId);
190199
if (llmParameters != null && !llmParameters.isEmpty()) {
@@ -221,6 +230,7 @@ public void writeTo(StreamOutput out) throws IOException {
221230
public static MLAgentUpdateInput parse(XContentParser parser) throws IOException {
222231
String agentId = null;
223232
String name = null;
233+
String type = null;
224234
String description = null;
225235
String llmModelId = null;
226236
Map<String, String> llmParameters = null;
@@ -244,6 +254,9 @@ public static MLAgentUpdateInput parse(XContentParser parser) throws IOException
244254
case AGENT_NAME_FIELD:
245255
name = parser.text();
246256
break;
257+
case TYPE_FIELD:
258+
type = parser.text();
259+
break;
247260
case DESCRIPTION_FIELD:
248261
description = parser.text();
249262
break;
@@ -314,6 +327,7 @@ public static MLAgentUpdateInput parse(XContentParser parser) throws IOException
314327
return new MLAgentUpdateInput(
315328
agentId,
316329
name,
330+
type,
317331
description,
318332
llmModelId,
319333
llmParameters,
@@ -329,6 +343,9 @@ public static MLAgentUpdateInput parse(XContentParser parser) throws IOException
329343
}
330344

331345
public MLAgent toMLAgent(MLAgent originalAgent) {
346+
if (type != null && !type.equals(originalAgent.getType())) {
347+
throw new IllegalArgumentException("Agent type cannot be updated");
348+
}
332349
LLMSpec finalLlm;
333350
if (llmModelId == null && (llmParameters == null || llmParameters.isEmpty())) {
334351
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)