Skip to content

Commit 806ee26

Browse files
glsukkiSukruth Gowdru Lingaraju
andauthored
fix: remove NotRequried as it is supported only in python 3.11 (#125)
* feat: integrate metadata support for short-term-memory (STM) * fix: remove NotRequried as it is supported only in python 3.11 * fix: fix all linting and formatting issues --------- Co-authored-by: Sukruth Gowdru Lingaraju <[email protected]>
1 parent 1bdddf6 commit 806ee26

File tree

4 files changed

+96
-125
lines changed

4 files changed

+96
-125
lines changed

src/bedrock_agentcore/memory/models/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44

55
from .DictWrapper import DictWrapper
66
from .filters import (
7-
StringValue,
8-
MetadataValue,
9-
MetadataKey,
7+
EventMetadataFilter,
108
LeftExpression,
9+
MetadataKey,
10+
MetadataValue,
1111
OperatorType,
1212
RightExpression,
13-
EventMetadataFilter,
13+
StringValue,
1414
)
1515

16+
1617
class ActorSummary(DictWrapper):
1718
"""A class representing an actor summary."""
1819

@@ -84,6 +85,7 @@ def __init__(self, session_summary: Dict[str, Any]):
8485
"""
8586
super().__init__(session_summary)
8687

88+
8789
__all__ = [
8890
"DictWrapper",
8991
"ActorSummary",
Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
"""Event metadata filter models for querying events based on metadata."""
2+
13
from enum import Enum
2-
from typing import Optional, TypedDict, Union, NotRequired
4+
from typing import Optional, TypedDict, Union
5+
36

47
class StringValue(TypedDict):
58
"""Value associated with the `eventMetadata` key."""
9+
610
stringValue: str
7-
11+
812
@staticmethod
9-
def build(value: str) -> 'StringValue':
10-
return {
11-
"stringValue": value
12-
}
13+
def build(value: str) -> "StringValue":
14+
"""Build a StringValue from a string."""
15+
return {"stringValue": value}
16+
1317

1418
MetadataValue = Union[StringValue]
1519
"""
@@ -24,68 +28,75 @@ def build(value: str) -> 'StringValue':
2428
Union type representing metadata key.
2529
"""
2630

31+
2732
class LeftExpression(TypedDict):
28-
"""
29-
Left operand of the event metadata filter expression.
30-
"""
33+
"""Left operand of the event metadata filter expression."""
34+
3135
metadataKey: MetadataKey
32-
36+
3337
@staticmethod
34-
def build(key: str) -> 'LeftExpression':
35-
"""Builds the `metadataKey` for `LeftExpression`"""
36-
return {
37-
"metadataKey": key
38-
}
38+
def build(key: str) -> "LeftExpression":
39+
"""Builds the `metadataKey` for `LeftExpression`."""
40+
return {"metadataKey": key}
41+
3942

4043
class OperatorType(Enum):
41-
"""
42-
Operator applied to the event metadata filter expression.
43-
44+
"""Operator applied to the event metadata filter expression.
45+
4446
Currently supports:
4547
- `EQUALS_TO`
4648
- `EXISTS`
4749
- `NOT_EXISTS`
4850
"""
51+
4952
EQUALS_TO = "EQUALS_TO"
5053
EXISTS = "EXISTS"
5154
NOT_EXISTS = "NOT_EXISTS"
5255

56+
5357
class RightExpression(TypedDict):
54-
"""
55-
Right operand of the event metadata filter expression.
56-
58+
"""Right operand of the event metadata filter expression.
59+
5760
Variants:
5861
- StringValue: {"metadataValue": {"stringValue": str}}
5962
"""
63+
6064
metadataValue: MetadataValue
6165

6266
@staticmethod
63-
def build(value: str) -> 'RightExpression':
64-
"""Builds the `RightExpression` for `stringValue` type"""
67+
def build(value: str) -> "RightExpression":
68+
"""Builds the `RightExpression` for `stringValue` type."""
6569
return {"metadataValue": StringValue.build(value)}
6670

71+
6772
class EventMetadataFilter(TypedDict):
68-
"""
69-
Filter expression for retrieving events based on metadata associated with an event.
70-
73+
"""Filter expression for retrieving events based on metadata associated with an event.
74+
7175
Args:
7276
left: `LeftExpression` of the event metadata filter expression.
7377
operator: `OperatorType` applied to the event metadata filter expression.
7478
right: Optional `RightExpression` of the event metadata filter expression.
7579
"""
80+
7681
left: LeftExpression
7782
operator: OperatorType
78-
right: NotRequired[RightExpression]
79-
80-
def build_expression(left_operand: LeftExpression, operator: OperatorType, right_operand: Optional[RightExpression] = None) -> 'EventMetadataFilter':
81-
"""
82-
This method builds the required event metadata filter expression into the `EventMetadataFilterExpression` type when querying listEvents.
83-
84-
Args:
83+
right: Optional[RightExpression]
84+
85+
def build_expression(
86+
left_operand: LeftExpression,
87+
operator: OperatorType,
88+
right_operand: Optional[RightExpression] = None,
89+
) -> "EventMetadataFilter":
90+
"""Build the required event metadata filter expression.
91+
92+
This method builds the required event metadata filter expression into the
93+
`EventMetadataFilterExpression` type when querying listEvents.
94+
95+
Args:
8596
left_operand: Left operand of the event metadata filter expression
8697
operator: Operator applied to the event metadata filter expression
8798
right_operand: Optional right_operand of the event metadata filter expression.
88-
99+
89100
Example:
90101
```
91102
left_operand = LeftExpression.build_key(key='location')
@@ -108,11 +119,8 @@ def build_expression(left_operand: LeftExpression, operator: OperatorType, right
108119
}
109120
```
110121
"""
111-
filter = {
112-
'left': left_operand,
113-
'operator': operator.value
114-
}
115-
122+
filter = {"left": left_operand, "operator": operator.value}
123+
116124
if right_operand:
117-
filter['right'] = right_operand
118-
return filter
125+
filter["right"] = right_operand
126+
return filter

src/bedrock_agentcore/memory/session.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
DictWrapper,
1717
Event,
1818
EventMessage,
19+
EventMetadataFilter,
1920
MemoryRecord,
20-
SessionSummary,
2121
MetadataValue,
22-
EventMetadataFilter
22+
SessionSummary,
2323
)
2424

2525
logger = logging.getLogger(__name__)
@@ -428,7 +428,7 @@ def add_turns(
428428

429429
if branch:
430430
params["branch"] = branch
431-
431+
432432
if metadata:
433433
params["metadata"] = metadata
434434

@@ -490,6 +490,7 @@ def list_events(
490490
session_id: Session identifier
491491
branch_name: Optional branch name to filter events (None for all branches)
492492
include_parent_branches: Whether to include parent branch events (only applies with branch_name)
493+
eventMetadata: Optional list of event metadata filters to apply
493494
max_results: Maximum number of events to return
494495
include_payload: Whether to include event payloads in response
495496
@@ -505,12 +506,12 @@ def list_events(
505506
506507
# Get events from a specific branch
507508
branch_events = client.list_events(actor_id, session_id, branch_name="test-branch")
508-
509+
509510
#### Get events with event metadata filter
510511
```
511512
filtered_events_with_metadata = client.list_events(
512513
actor_id=actor_id,
513-
session_id=session_id,
514+
session_id=session_id,
514515
eventMetadata=[
515516
{
516517
'left': {
@@ -522,7 +523,7 @@ def list_events(
522523
'stringValue': 'NYC'
523524
}
524525
}
525-
}
526+
}
526527
]
527528
)
528529
```
@@ -544,7 +545,7 @@ def list_events(
544545
'stringValue': 'NYC'
545546
}
546547
}
547-
}
548+
}
548549
]
549550
)
550551
```
@@ -577,9 +578,7 @@ def list_events(
577578

578579
# Add eventMetadata filter if specified
579580
if eventMetadata:
580-
params["filter"] = {
581-
"eventMetadata": eventMetadata
582-
}
581+
params["filter"] = {"eventMetadata": eventMetadata}
583582

584583
response = self._data_plane_client.list_events(**params)
585584

0 commit comments

Comments
 (0)