|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.common.memorycontainer; |
| 7 | + |
| 8 | +import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; |
| 9 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.AGENT_ID_FIELD; |
| 10 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.CREATED_TIME_FIELD; |
| 11 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.LAST_UPDATED_TIME_FIELD; |
| 12 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.MEMORY_EMBEDDING_FIELD; |
| 13 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.MEMORY_FIELD; |
| 14 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.MEMORY_TYPE_FIELD; |
| 15 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.ROLE_FIELD; |
| 16 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.SESSION_ID_FIELD; |
| 17 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.TAGS_FIELD; |
| 18 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.USER_ID_FIELD; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.time.Instant; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 26 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 27 | +import org.opensearch.core.common.io.stream.Writeable; |
| 28 | +import org.opensearch.core.xcontent.ToXContentObject; |
| 29 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 30 | +import org.opensearch.core.xcontent.XContentParser; |
| 31 | + |
| 32 | +import lombok.Builder; |
| 33 | +import lombok.Getter; |
| 34 | +import lombok.Setter; |
| 35 | + |
| 36 | +/** |
| 37 | + * Represents a memory entry in a memory container |
| 38 | + */ |
| 39 | +@Getter |
| 40 | +@Setter |
| 41 | +@Builder |
| 42 | +public class MLMemory implements ToXContentObject, Writeable { |
| 43 | + |
| 44 | + // Core fields |
| 45 | + private String sessionId; |
| 46 | + private String memory; |
| 47 | + private MemoryType memoryType; |
| 48 | + |
| 49 | + // Optional fields |
| 50 | + private String userId; |
| 51 | + private String agentId; |
| 52 | + private String role; |
| 53 | + private Map<String, String> tags; |
| 54 | + |
| 55 | + // System fields |
| 56 | + private Instant createdTime; |
| 57 | + private Instant lastUpdatedTime; |
| 58 | + |
| 59 | + // Vector/embedding field (optional, for semantic storage) |
| 60 | + private Object memoryEmbedding; |
| 61 | + |
| 62 | + @Builder |
| 63 | + public MLMemory( |
| 64 | + String sessionId, |
| 65 | + String memory, |
| 66 | + MemoryType memoryType, |
| 67 | + String userId, |
| 68 | + String agentId, |
| 69 | + String role, |
| 70 | + Map<String, String> tags, |
| 71 | + Instant createdTime, |
| 72 | + Instant lastUpdatedTime, |
| 73 | + Object memoryEmbedding |
| 74 | + ) { |
| 75 | + this.sessionId = sessionId; |
| 76 | + this.memory = memory; |
| 77 | + this.memoryType = memoryType; |
| 78 | + this.userId = userId; |
| 79 | + this.agentId = agentId; |
| 80 | + this.role = role; |
| 81 | + this.tags = tags; |
| 82 | + this.createdTime = createdTime; |
| 83 | + this.lastUpdatedTime = lastUpdatedTime; |
| 84 | + this.memoryEmbedding = memoryEmbedding; |
| 85 | + } |
| 86 | + |
| 87 | + public MLMemory(StreamInput in) throws IOException { |
| 88 | + this.sessionId = in.readString(); |
| 89 | + this.memory = in.readString(); |
| 90 | + this.memoryType = in.readEnum(MemoryType.class); |
| 91 | + this.userId = in.readOptionalString(); |
| 92 | + this.agentId = in.readOptionalString(); |
| 93 | + this.role = in.readOptionalString(); |
| 94 | + if (in.readBoolean()) { |
| 95 | + this.tags = in.readMap(StreamInput::readString, StreamInput::readString); |
| 96 | + } |
| 97 | + this.createdTime = in.readInstant(); |
| 98 | + this.lastUpdatedTime = in.readInstant(); |
| 99 | + // Note: memoryEmbedding is not serialized in StreamInput/Output as it's typically handled separately |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void writeTo(StreamOutput out) throws IOException { |
| 104 | + out.writeString(sessionId); |
| 105 | + out.writeString(memory); |
| 106 | + out.writeEnum(memoryType); |
| 107 | + out.writeOptionalString(userId); |
| 108 | + out.writeOptionalString(agentId); |
| 109 | + out.writeOptionalString(role); |
| 110 | + if (tags != null && !tags.isEmpty()) { |
| 111 | + out.writeBoolean(true); |
| 112 | + out.writeMap(tags, StreamOutput::writeString, StreamOutput::writeString); |
| 113 | + } else { |
| 114 | + out.writeBoolean(false); |
| 115 | + } |
| 116 | + out.writeInstant(createdTime); |
| 117 | + out.writeInstant(lastUpdatedTime); |
| 118 | + // Note: memoryEmbedding is not serialized in StreamInput/Output as it's typically handled separately |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 123 | + builder.startObject(); |
| 124 | + builder.field(SESSION_ID_FIELD, sessionId); |
| 125 | + builder.field(MEMORY_FIELD, memory); |
| 126 | + builder.field(MEMORY_TYPE_FIELD, memoryType.getValue()); |
| 127 | + |
| 128 | + if (userId != null) { |
| 129 | + builder.field(USER_ID_FIELD, userId); |
| 130 | + } |
| 131 | + if (agentId != null) { |
| 132 | + builder.field(AGENT_ID_FIELD, agentId); |
| 133 | + } |
| 134 | + if (role != null) { |
| 135 | + builder.field(ROLE_FIELD, role); |
| 136 | + } |
| 137 | + if (tags != null && !tags.isEmpty()) { |
| 138 | + builder.field(TAGS_FIELD, tags); |
| 139 | + } |
| 140 | + |
| 141 | + builder.field(CREATED_TIME_FIELD, createdTime.toEpochMilli()); |
| 142 | + builder.field(LAST_UPDATED_TIME_FIELD, lastUpdatedTime.toEpochMilli()); |
| 143 | + |
| 144 | + if (memoryEmbedding != null) { |
| 145 | + builder.field(MEMORY_EMBEDDING_FIELD, memoryEmbedding); |
| 146 | + } |
| 147 | + |
| 148 | + builder.endObject(); |
| 149 | + return builder; |
| 150 | + } |
| 151 | + |
| 152 | + public static MLMemory parse(XContentParser parser) throws IOException { |
| 153 | + String sessionId = null; |
| 154 | + String memory = null; |
| 155 | + MemoryType memoryType = null; |
| 156 | + String userId = null; |
| 157 | + String agentId = null; |
| 158 | + String role = null; |
| 159 | + Map<String, String> tags = null; |
| 160 | + Instant createdTime = null; |
| 161 | + Instant lastUpdatedTime = null; |
| 162 | + Object memoryEmbedding = null; |
| 163 | + |
| 164 | + ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); |
| 165 | + while (parser.nextToken() != XContentParser.Token.END_OBJECT) { |
| 166 | + String fieldName = parser.currentName(); |
| 167 | + parser.nextToken(); |
| 168 | + |
| 169 | + switch (fieldName) { |
| 170 | + case SESSION_ID_FIELD: |
| 171 | + sessionId = parser.text(); |
| 172 | + break; |
| 173 | + case MEMORY_FIELD: |
| 174 | + memory = parser.text(); |
| 175 | + break; |
| 176 | + case MEMORY_TYPE_FIELD: |
| 177 | + memoryType = MemoryType.fromString(parser.text()); |
| 178 | + break; |
| 179 | + case USER_ID_FIELD: |
| 180 | + userId = parser.text(); |
| 181 | + break; |
| 182 | + case AGENT_ID_FIELD: |
| 183 | + agentId = parser.text(); |
| 184 | + break; |
| 185 | + case ROLE_FIELD: |
| 186 | + role = parser.text(); |
| 187 | + break; |
| 188 | + case TAGS_FIELD: |
| 189 | + Map<String, Object> tagsMap = parser.map(); |
| 190 | + if (tagsMap != null) { |
| 191 | + tags = new HashMap<>(); |
| 192 | + for (Map.Entry<String, Object> entry : tagsMap.entrySet()) { |
| 193 | + if (entry.getValue() != null) { |
| 194 | + tags.put(entry.getKey(), entry.getValue().toString()); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + break; |
| 199 | + case CREATED_TIME_FIELD: |
| 200 | + createdTime = Instant.ofEpochMilli(parser.longValue()); |
| 201 | + break; |
| 202 | + case LAST_UPDATED_TIME_FIELD: |
| 203 | + lastUpdatedTime = Instant.ofEpochMilli(parser.longValue()); |
| 204 | + break; |
| 205 | + case MEMORY_EMBEDDING_FIELD: |
| 206 | + // Parse embedding as generic object (could be array or sparse map) |
| 207 | + memoryEmbedding = parser.map(); |
| 208 | + break; |
| 209 | + default: |
| 210 | + parser.skipChildren(); |
| 211 | + break; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + return MLMemory |
| 216 | + .builder() |
| 217 | + .sessionId(sessionId) |
| 218 | + .memory(memory) |
| 219 | + .memoryType(memoryType) |
| 220 | + .userId(userId) |
| 221 | + .agentId(agentId) |
| 222 | + .role(role) |
| 223 | + .tags(tags) |
| 224 | + .createdTime(createdTime) |
| 225 | + .lastUpdatedTime(lastUpdatedTime) |
| 226 | + .memoryEmbedding(memoryEmbedding) |
| 227 | + .build(); |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * Convert to a Map for indexing |
| 232 | + */ |
| 233 | + public Map<String, Object> toIndexMap() { |
| 234 | + Map<String, Object> map = Map |
| 235 | + .of( |
| 236 | + SESSION_ID_FIELD, |
| 237 | + sessionId, |
| 238 | + MEMORY_FIELD, |
| 239 | + memory, |
| 240 | + MEMORY_TYPE_FIELD, |
| 241 | + memoryType.getValue(), |
| 242 | + CREATED_TIME_FIELD, |
| 243 | + createdTime.toEpochMilli(), |
| 244 | + LAST_UPDATED_TIME_FIELD, |
| 245 | + lastUpdatedTime.toEpochMilli() |
| 246 | + ); |
| 247 | + |
| 248 | + // Use mutable map for optional fields |
| 249 | + Map<String, Object> result = new java.util.HashMap<>(map); |
| 250 | + |
| 251 | + if (userId != null) { |
| 252 | + result.put(USER_ID_FIELD, userId); |
| 253 | + } |
| 254 | + if (agentId != null) { |
| 255 | + result.put(AGENT_ID_FIELD, agentId); |
| 256 | + } |
| 257 | + if (role != null) { |
| 258 | + result.put(ROLE_FIELD, role); |
| 259 | + } |
| 260 | + if (tags != null && !tags.isEmpty()) { |
| 261 | + result.put(TAGS_FIELD, tags); |
| 262 | + } |
| 263 | + if (memoryEmbedding != null) { |
| 264 | + result.put(MEMORY_EMBEDDING_FIELD, memoryEmbedding); |
| 265 | + } |
| 266 | + |
| 267 | + return result; |
| 268 | + } |
| 269 | +} |
0 commit comments