generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 186
Introduce hook and context management to OpenSearch Agents #4388
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
Open
mingshl
wants to merge
14
commits into
opensearch-project:main
Choose a base branch
from
mingshl:feature/context_manager_hooks_inline_create
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+9,845
−1,551
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
56c6640
add hooks in ml-commons (#4326)
Zhangxunmt f6678d6
initiate context management api with hook implementation (#4345)
mingshl 56fc15d
Add Context Manager to PER (#4379)
mingshl fc0d896
add inner create context management to agent register api
mingshl 846a9ba
add code coverage
mingshl 9b87b57
allow context management hook register in during agent execute
mingshl 3d3f5cd
add code coverage
mingshl 35c8ffd
add more code coverage
mingshl d90572d
add validation check
mingshl 4b01984
adapt to inplace update for context
mingshl 564b572
Allow context management inline create in register agent without stor…
mingshl 8963f08
Update the POST_TOOL hook emit saving to agentic memory (#4408)
mingshl 8bb6138
bump supported version to 3.4
mingshl 2a1f919
fix manager name during inline create
mingshl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
common/src/main/java/org/opensearch/ml/common/contextmanager/ActivationRule.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.ml.common.contextmanager; | ||
|
|
||
| /** | ||
| * Interface for activation rules that determine when a context manager should execute. | ||
| * Activation rules evaluate runtime conditions based on the current context state. | ||
| */ | ||
| public interface ActivationRule { | ||
|
|
||
| /** | ||
| * Evaluate whether the activation condition is met. | ||
| * @param context the current context state | ||
| * @return true if the condition is met and the manager should activate, false otherwise | ||
| */ | ||
| boolean evaluate(ContextManagerContext context); | ||
|
|
||
| /** | ||
| * Get a description of this activation rule for logging and debugging. | ||
| * @return a human-readable description of the rule | ||
| */ | ||
| String getDescription(); | ||
| } |
148 changes: 148 additions & 0 deletions
148
common/src/main/java/org/opensearch/ml/common/contextmanager/ActivationRuleFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.ml.common.contextmanager; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import lombok.extern.log4j.Log4j2; | ||
|
|
||
| /** | ||
| * Factory class for creating activation rules from configuration. | ||
| * Supports creating rules from configuration maps and combining multiple rules. | ||
| */ | ||
| @Log4j2 | ||
| public class ActivationRuleFactory { | ||
|
|
||
| public static final String TOKENS_EXCEED_KEY = "tokens_exceed"; | ||
| public static final String MESSAGE_COUNT_EXCEED_KEY = "message_count_exceed"; | ||
|
|
||
| /** | ||
| * Create activation rules from a configuration map. | ||
| * @param activationConfig the configuration map containing rule definitions | ||
| * @return a list of activation rules, or empty list if no valid rules found | ||
| */ | ||
| public static List<ActivationRule> createRules(Map<String, Object> activationConfig) { | ||
| List<ActivationRule> rules = new ArrayList<>(); | ||
|
|
||
| if (activationConfig == null || activationConfig.isEmpty()) { | ||
| return rules; | ||
| } | ||
|
|
||
| // Create tokens_exceed rule | ||
| if (activationConfig.containsKey(TOKENS_EXCEED_KEY)) { | ||
| try { | ||
| Object tokenValue = activationConfig.get(TOKENS_EXCEED_KEY); | ||
| int tokenThreshold = parseIntegerValue(tokenValue, TOKENS_EXCEED_KEY); | ||
| if (tokenThreshold > 0) { | ||
| rules.add(new TokensExceedRule(tokenThreshold)); | ||
| log.debug("Created TokensExceedRule with threshold: {}", tokenThreshold); | ||
| } else { | ||
| throw new IllegalArgumentException("Invalid token threshold value: " + tokenValue + ". Must be positive integer."); | ||
| } | ||
| } catch (Exception e) { | ||
| log.error("Failed to create TokensExceedRule: {}", e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| // Create message_count_exceed rule | ||
| if (activationConfig.containsKey(MESSAGE_COUNT_EXCEED_KEY)) { | ||
| try { | ||
| Object messageValue = activationConfig.get(MESSAGE_COUNT_EXCEED_KEY); | ||
| int messageThreshold = parseIntegerValue(messageValue, MESSAGE_COUNT_EXCEED_KEY); | ||
| if (messageThreshold > 0) { | ||
| rules.add(new MessageCountExceedRule(messageThreshold)); | ||
| log.debug("Created MessageCountExceedRule with threshold: {}", messageThreshold); | ||
| } else { | ||
| throw new IllegalArgumentException( | ||
| "Invalid message count threshold value: " + messageValue + ". Must be positive integer." | ||
| ); | ||
| } | ||
| } catch (Exception e) { | ||
| log.error("Failed to create MessageCountExceedRule: {}", e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| return rules; | ||
| } | ||
|
|
||
| /** | ||
| * Create a composite rule that requires ALL rules to be satisfied (AND logic). | ||
| * @param rules the list of rules to combine | ||
| * @return a composite rule, or null if the list is empty | ||
| */ | ||
| public static ActivationRule createCompositeRule(List<ActivationRule> rules) { | ||
| if (rules == null || rules.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| if (rules.size() == 1) { | ||
| return rules.get(0); | ||
| } | ||
|
|
||
| return new CompositeActivationRule(rules); | ||
| } | ||
|
|
||
| /** | ||
| * Parse an integer value from configuration, handling various input types. | ||
| * @param value the value to parse | ||
| * @param fieldName the field name for error reporting | ||
| * @return the parsed integer value | ||
| * @throws IllegalArgumentException if the value cannot be parsed | ||
| */ | ||
| private static int parseIntegerValue(Object value, String fieldName) { | ||
| if (value instanceof Integer) { | ||
| return (Integer) value; | ||
| } else if (value instanceof Number) { | ||
| return ((Number) value).intValue(); | ||
| } else if (value instanceof String) { | ||
| try { | ||
| return Integer.parseInt((String) value); | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException("Invalid integer value for " + fieldName + ": " + value); | ||
| } | ||
| } else { | ||
| throw new IllegalArgumentException("Unsupported value type for " + fieldName + ": " + value.getClass().getSimpleName()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Composite activation rule that implements AND logic for multiple rules. | ||
| */ | ||
| private static class CompositeActivationRule implements ActivationRule { | ||
| private final List<ActivationRule> rules; | ||
|
|
||
| public CompositeActivationRule(List<ActivationRule> rules) { | ||
| this.rules = new ArrayList<>(rules); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean evaluate(ContextManagerContext context) { | ||
| // All rules must evaluate to true (AND logic) | ||
| for (ActivationRule rule : rules) { | ||
| if (!rule.evaluate(context)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append("composite_rule: ["); | ||
| for (int i = 0; i < rules.size(); i++) { | ||
| if (i > 0) { | ||
| sb.append(" AND "); | ||
| } | ||
| sb.append(rules.get(i).getDescription()); | ||
| } | ||
| sb.append("]"); | ||
| return sb.toString(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Shouldn't this be 3.4 then as 3.3 is already released?
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.
nice catch!I was building upon a 3.3.2 cluster, in main branch, I need to change to 3.4.0
MINIMAL_SUPPORTED_VERSION_FOR_CONTEXT_MANAGEMENT = CommonValue.VERSION_3_4_0