-
Notifications
You must be signed in to change notification settings - Fork 183
adding agentCore memory integration in agent execution #4149
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import org.opensearch.ml.common.output.model.ModelTensorOutput; | ||
import org.opensearch.ml.common.output.model.ModelTensors; | ||
|
||
import com.google.gson.JsonSyntaxException; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.jayway.jsonpath.JsonPath; | ||
import com.jayway.jsonpath.PathNotFoundException; | ||
|
@@ -93,9 +94,42 @@ public static Map<String, String> extractInputParameters(Map<String, String> par | |
StringSubstitutor stringSubstitutor = new StringSubstitutor(parameters, "${parameters.", "}"); | ||
String input = stringSubstitutor.replace(parameters.get("input")); | ||
extractedParameters.put("input", input); | ||
Map<String, String> inputParameters = gson | ||
.fromJson(input, TypeToken.getParameterized(Map.class, String.class, String.class).getType()); | ||
extractedParameters.putAll(inputParameters); | ||
|
||
// Check if input is a JSON object or array | ||
String trimmedInput = input.trim(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a reason for these changes? i would avoid parameter parsing changes unless 100% required for memory related changes |
||
if (trimmedInput.startsWith("{")) { | ||
// Input is a JSON object - try parsing as Map<String, String> first (existing behavior) | ||
try { | ||
Map<String, String> inputParameters = gson | ||
.fromJson(input, TypeToken.getParameterized(Map.class, String.class, String.class).getType()); | ||
extractedParameters.putAll(inputParameters); | ||
Comment on lines
+103
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we use this please:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's keep this method as in the above PR if possible, thanks |
||
} catch (JsonSyntaxException e) { | ||
// Fallback: handle mixed types (arrays, objects, etc.) for cases like {"index": ["*"]} | ||
try { | ||
Map<String, Object> inputParameters = gson | ||
.fromJson(input, TypeToken.getParameterized(Map.class, String.class, Object.class).getType()); | ||
|
||
// Convert non-string values to JSON strings for tool compatibility | ||
for (Map.Entry<String, Object> entry : inputParameters.entrySet()) { | ||
String key = entry.getKey(); | ||
Object value = entry.getValue(); | ||
|
||
if (value instanceof String) { | ||
extractedParameters.put(key, (String) value); // Keep strings as-is | ||
} else { | ||
extractedParameters.put(key, gson.toJson(value)); // Convert arrays/objects to JSON strings | ||
} | ||
} | ||
} catch (Exception fallbackException) { | ||
// If both approaches fail, log original error and continue | ||
log.info("fail extract parameters from key 'input' due to" + e.getMessage()); | ||
} | ||
} | ||
} else if (trimmedInput.startsWith("[")) { | ||
// Input is a JSON array - skip parsing as it's not a parameter map | ||
log.debug("Input is a JSON array, skipping parameter extraction"); | ||
} | ||
// If it's neither object nor array, it's likely a plain string - keep as is | ||
} catch (Exception exception) { | ||
log.info("fail extract parameters from key 'input' due to" + exception.getMessage()); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,4 +132,26 @@ public void testConstructorWithStreamInput_VersionCompatibility() throws IOExcep | |
assertEquals("testAgentId", inputNewVersion.getAgentId()); | ||
assertEquals("testTenantId", inputNewVersion.getTenantId()); // tenantId should be populated for newer versions | ||
} | ||
|
||
@Test | ||
public void testMemoryGetterSetter() { | ||
// Test memory field getter/setter functionality | ||
AgentMLInput input = new AgentMLInput("testAgent", null, FunctionName.AGENT, null); | ||
|
||
// Initially memory should be null | ||
assertNull("Memory should be null initially", input.getMemory()); | ||
|
||
// Set memory and verify | ||
Map<String, Object> memoryMap = new HashMap<>(); | ||
memoryMap.put("type", "bedrock_agentcore_memory"); | ||
memoryMap.put("memory_arn", "test-arn"); | ||
memoryMap.put("region", "us-east-1"); | ||
|
||
input.setMemory(memoryMap); | ||
|
||
assertNotNull("Memory should not be null after setting", input.getMemory()); | ||
assertEquals("bedrock_agentcore_memory", input.getMemory().get("type")); | ||
assertEquals("test-arn", input.getMemory().get("memory_arn")); | ||
assertEquals("us-east-1", input.getMemory().get("region")); | ||
Comment on lines
+150
to
+155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test is only testing the setter, not sure how useful that is |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use constants (leaving nitpicks so we can track them, they can be addressed later)